【问题标题】:parsing information out of sequencing data从测序数据中解析信息
【发布时间】:2017-12-30 02:23:18
【问题描述】:

我有一个 txt 文件,它是一个转换后的 fasta 文件,它只有一个我有兴趣分析的特定区域。看起来是这样的

CTGGCCGCGCTGACTCCTCTCGCT

CTCGCAGCACTGACTCCTCTTGCG

CTAGCCGCTCTGACTCCGCTAGCG

CTCGCTGCCCTCACACCTCTTGCA

CTCGCAGCACTGACTCCTCTTGCG

CTCGCAGCACTAACACCCCTAGCT

CTCGCTGCTCTGACTCCTCTCGCC

CTGGCCGCGCTGACTCCTCTCGCT

我目前正在使用 excel 对每个位置的核苷酸多样性进行一些计算。有些文件有 200,000 次读取,因此这使得 excel 文件难以处理。我认为使用 python 或 R 必须有一种更简单的方法来做到这一点。

基本上,我想获取包含序列列表的 .txt 文件,并使用此等式 -p(log2(p)) 测量每个位置的核苷酸多样性。有谁知道除了excel之外如何做到这一点?

非常感谢您的任何帮助。

【问题讨论】:

  • 在 Python 中检查 readlines() 和 count() 函数
  • 请阅读(1)我如何提出一个好问题,(2)如何创建 MCVE 以及(3)如何在 R 中提供一个最小的可重现示例。然后编辑和改进你的相应地提问。即,从您的实际问题中抽象出来......请dput()您的数据具有预期的结果。

标签: python r linux bioinformatics sequencing


【解决方案1】:

如果您可以使用 fasta 文件,那可能会更好,因为有 专门设计用于该格式的软件包。

在这里,我在 R 中给出了一个解决方案,使用包 seqinr 以及 dplyrtidyverse 的一部分)用于处理数据。

如果这是您的 fasta 文件(基于您的序列):

>seq1
CTGGCCGCGCTGACTCCTCTCGCT
>seq2
CTCGCAGCACTGACTCCTCTTGCG
>seq3
CTAGCCGCTCTGACTCCGCTAGCG
>seq4
CTCGCTGCCCTCACACCTCTTGCA
>seq5
CTCGCAGCACTGACTCCTCTTGCG
>seq6
CTCGCAGCACTAACACCCCTAGCT
>seq7
CTCGCTGCTCTGACTCCTCTCGCC
>seq8
CTGGCCGCGCTGACTCCTCTCGCT

您可以使用 seqinr 包将其读入 R:

# Load the packages
library(tidyverse) # I use this package for manipulating data.frames later on
library(seqinr)

# Read the fasta file - use the path relevant for you
seqs <- read.fasta("~/path/to/your/file/example_fasta.fa")

这将返回一个list 对象,其中包含尽可能多的元素 文件中的序列。

针对您的特定问题 - 计算每个职位的多样性指标 -
我们可以使用seqinr 包中的两个有用函数:

  • getFrag() 对序列进行子集化
  • count()计算每个核苷酸的频率

例如,如果我们想要第一个位置的核苷酸频率 我们的序列,我们可以这样做:

# Get position 1
pos1 <- getFrag(seqs, begin = 1, end = 1)

# Calculate frequency of each nucleotide
count(pos1, wordsize = 1, freq = TRUE)

a c g t 
0 1 0 0 

向我们展示了第一个位置只包含一个“C”。

以下是一种以编程方式“循环”所有位置并执行以下操作的方法 我们可能感兴趣的计算:

# Obtain fragment lenghts - assuming all sequences are the same length!
l <- length(seqs[[1]])

# Use the `lapply` function to estimate frequency for each position
p <- lapply(1:l, function(i, seqs){
  # Obtain the nucleotide for the current position
  pos_seq <- getFrag(seqs, i, i)

  # Get the frequency of each nucleotide
  pos_freq <- count(pos_seq, 1, freq = TRUE)

  # Convert to data.frame, rename variables more sensibly
  ## and add information about the nucleotide position
  pos_freq <- pos_freq %>% 
    as.data.frame() %>%
    rename(nuc = Var1, freq = Freq) %>% 
    mutate(pos = i)
}, seqs = seqs)

# The output of the above is a list.
## We now bind all tables to a single data.frame
## Remove nucleotides with zero frequency
## And estimate entropy and expected heterozygosity for each position
diversity <- p %>% 
  bind_rows() %>% 
  filter(freq > 0) %>% 
  group_by(pos) %>% 
  summarise(shannon_entropy = -sum(freq * log2(freq)),
            het = 1 - sum(freq^2), 
            n_nuc = n())

这些计算的输出现在如下所示:

head(diversity)

# A tibble: 6 x 4
    pos shannon_entropy     het n_nuc
  <int>           <dbl>   <dbl> <int>
1     1        0.000000 0.00000     1
2     2        0.000000 0.00000     1
3     3        1.298795 0.53125     3
4     4        0.000000 0.00000     1
5     5        0.000000 0.00000     1
6     6        1.561278 0.65625     3

这是一个更直观的视图(使用ggplot2,也是tidyverse 包的一部分):

ggplot(diversity, aes(pos, shannon_entropy)) + 
  geom_line() +
  geom_point(aes(colour = factor(n_nuc))) +
  labs(x = "Position (bp)", y = "Shannon Entropy", 
       colour = "Number of\nnucleotides")

更新:

要将其应用于多个 fasta 文件,这是一种可能性 (我没有测试这段代码,但这样的东西应该可以工作):

# Find all the fasta files of interest
## use a pattern that matches the file extension of your files
fasta_files <- list.files("~/path/to/your/fasta/directory", 
                          pattern = ".fa", full.names = TRUE)

# Use lapply to apply the code above to each file
my_diversities <- lapply(fasta_files, function(f){
  # Read the fasta file
  seqs <- read.fasta(f)

  # Obtain fragment lenghts - assuming all sequences are the same length!
  l <- length(seqs[[1]])

  # .... ETC - Copy the code above until ....
  diversity <- p %>% 
    bind_rows() %>% 
    filter(freq > 0) %>% 
    group_by(pos) %>% 
    summarise(shannon_entropy = -sum(freq * log2(freq)),
              het = 1 - sum(freq^2), 
              n_nuc = n())
})

# The output is a list of tables. 
## You can then bind them together, 
## ensuring the name of the file is added as a new column "file_name"

names(my_diversities) <- basename(fasta_files) # name the list elements
my_diversities <- bind_rows(my_diversities, .id = "file_name") # bind tables

这将为您提供每个文件的多样性表。然后您可以使用ggplot2 将其可视化,类似于我上面所做的,但也许使用facets 将每个文件的多样性分隔到不同的面板中。

【讨论】:

  • 雨果,这是一个了不起的答案。效果惊人。
  • 为了扩展这一点,我想用数百个单独的 fasta 文件来做这件事。有没有一种方法可以连接所有这些文件并一次运行相同的分析?
  • @JamesWeger 如果您连接所有文件,那么多样性度量将基于所有序列一起(这是您想要的吗?或者您是否希望分别为每个 fasta 文件计算多样性? )。如果您确实想合并所有 fasta 文件,请使用 lapply 阅读它们,例如 here,然后将它们与 do.call("c", your_list_of_fastas) 结合使用。然后您将拥有一个包含所有序列的对象,您可以运行答案中的代码。
  • 抱歉,我想将它们分开,我想我想运行一个循环。我应该也可以为此使用 lapply 吗?
  • @JamesWeger 查看更新后的答案。另外,请考虑接受正确的答案。很高兴这对您有所帮助,但下次别忘了成为 more specific with your question
【解决方案2】:

您可以打开并阅读您的文件:

plist=[]
with open('test.txt', 'r') as infile:
     for i in infile:
           # make calculation of 'p' for each line here
           plist.append(p)

然后用你plist来计算你的熵

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2017-05-12
    • 2011-09-24
    • 1970-01-01
    • 2019-07-23
    • 2011-07-14
    相关资源
    最近更新 更多