分析序列d中的信息

2024-04-25 04:46:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我对一个文件很感兴趣。看起来像这样

CTGGCCGCTGCTCCTCTCGCT公司

CTCGCAGCATCTCCTCTTGCG公司

ctagcgcctctgactccgctagcg

CTCGCTGCCCTCTCACCTCTTGCA

CTCGCAGCATCTCCTCTTGCG公司

CTCGCAGCACTAACCCTAGTAGCT

CTCGCTGCTCTGCTCCTCGCC

CTGGCCGCTGCTCCTCTCGCT公司

我目前正在使用excel对每个位置的核苷酸多样性进行一些计算。有些文件有大约200000次读取,所以这使得excel文件难以处理。我想使用python或R一定有更简单的方法来实现这一点

基本上,我想用带有序列列表的.txt文件,用这个等式-p(log2(p))测量每个位置的核苷酸多样性。有人知道除了excel之外,还有什么方法可以做到这一点吗?在

非常感谢您的帮助。在


Tags: 文件方法txt列表公司序列excel核苷酸
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来计算你的熵

如果你能从档案里找到更好的方法 专门设计用于该格式的包。在

在这里,我在R中使用包seqinrdplyr(是tidyverse)的一部分,用于操作数据。在

如果这是你的fasta文件(根据你的顺序):

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

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

^{pr2}$

这将返回一个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")

nuc_diversity_example

更新:

要将其应用于多个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将每个文件的多样性分离到不同的面板中。在

相关问题 更多 >