【问题标题】:How to get the sequence counts (in fasta) with conditions using python?如何使用python获取带有条件的序列计数(在fasta中)?
【发布时间】:2019-04-19 02:19:54
【问题描述】:

我有一个 fasta 文件(fasta 是一个文件,其中标题行以 > 开头,后跟与该标题对应的序列行)。我想在每个>TRINITY 序列之后获取与 TRINITY 匹配的序列和以>K 开头的总序列的计数。我能够获得>TRINITY 序列的计数,但不确定如何获得相应>TRINITY 序列组的>K 的计数。如何在 python 中完成这项工作?

myfasta.fasta:

>TRINITY_DN12824_c0_g1_i1
TGGTGACCTGAATGGTCACCACGTCCATACAGA
>K00363:119:HTJ23BBXX:1:1212:18730:9403 1:N:0:CGATGTAT
CACTATTACAATTCTGATGTTTTAATTACTGAGACAT
>K00363:119:HTJ23BBXX:1:2228:9678:46223_(reversed) 1:N:0:CGATGTAT
TAGATTTAAAATAGACGCTTCCATAGA
>TRINITY_DN12824_c0_g1_i1
TGGTGACCTGAATGGTCACCACGTCCATACAGA
>K00363:119:HTJ23BBXX:1:1212:18730:9403 1:N:0:CGATGTAT
CACTATTACAATTCTGATGTTTTAATTACTGAGACAT
>TRINITY_DN555_c0_g1_i1
>K00363:119:HTJ23BBXX:1:2228:9658:46188_(reversed) 1:N:0:CGATGTAT
CGATGCTAGATTTAAAATAGACG
>K00363:119:HTJ23BBXX:1:2106:15260:10387_(reversed) 1:N:0:CGATGTAT
TTAAAATAGACGCTTCCATAGAGA

我想要的结果:

reference   reference_counts    Corresponding_K_sequences
>TRINITY_DN12824_c0_g1_i1   2   3
>TRINITY_DN555_c0_g1_i1 1   2

这是我编写的代码,它仅考虑 >TRINITY 序列计数,但无法将其扩展到它也将计算相应 >K 序列的位,因此我们将不胜感激。 跑步: python code.py myfasta.fasta output.txt

import sys
import os
from Bio import SeqIO
from collections import defaultdict
filename = sys.argv[1]
outfile = sys.argv[2]
dedup_records = defaultdict(list)

for record in SeqIO.parse(filename, "fasta"):
    #print(record)
    #print(record.id)
    if record.id.startswith('TRINITY'):
        #print(record.id)
    # Use the sequence as the key and then have a list of id's as the value
        dedup_records[str(record.seq)].append(record.id)
        #print(dedup_records)
with open(outfile, 'w') as output:
#   # to get the counts of duplicated TRINITY ids (sorted order)
    for seq, ids in sorted(dedup_records.items(), key = lambda t: len(t[1]), reverse=True):
        #output.write("{}   {}\n".format(ids,len(ids)))
        print(ids, len(ids))

【问题讨论】:

    标签: python bioinformatics biopython fasta


    【解决方案1】:

    您的想法是正确的,但您需要跟踪以“TRINITY”开头的最后一个标题并稍微改变您的结构:

    from Bio import SeqIO
    from collections import defaultdict
    
    TRIN, d = None, defaultdict(lambda: [0,0])
    
    for r in SeqIO.parse('myfasta.fasta', 'fasta'):
        if r.id.startswith('TRINITY'):
            TRIN = r.id
            d[TRIN][0] += 1
        elif r.id.startswith('K'):
            if TRIN:
                d[TRIN][1] += 1
    
    print('reference\treference_counts\tCorresponding_K_sequences')
    for k,v in d.items():
        print('{}\t{}\t{}'.format(k,v[0],v[1])) 
    

    输出:

    reference   reference_counts    Corresponding_K_sequences
    TRINITY_DN12824_c0_g1_i1    2   3
    TRINITY_DN555_c0_g1_i1  1   2
    

    【讨论】:

    • 我当然忽略了这里的序列,不确定这是否是我们想要的
    • 完美!感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多