【问题标题】:Python. Trying to sort a file for 3 longest gene nucleotide sequences from genbank file into fasta file using BioPythonPython。尝试使用 BioPython 将 3 个最长基因核苷酸序列的文件从 genbank 文件排序到 fasta 文件中
【发布时间】:2014-03-03 06:03:13
【问题描述】:

我对 python 比较陌生,所以请原谅这个问题带来的白痴。我有一个 genbank 文件,并编写了一段代码,它将获取前 3 个最长的基因并将它们放入一个新生成的 fasta 文件中。

from Bio import SeqIO
file="sequence.gb"
output=open("Top3.faa", "w")
record=SeqIO.parse(file, "genbank")
rec=next(record)
print('The genes with the top 3 longest lengths have beens saved in Top3.faa')
for f in rec.features:
        end=f.location.end.position
        start=f.location.start.position
        length=end-start
        bug=(rec.seq)
        if f.type=='CDS':
            if 'gene' in f.qualifiers:
                        if length>7000:
                                geneName=f.qualifiers['gene']
                                name=str(geneName)
                                lenth=str(length)
                                seq=str(bug[start:end])
                                output.write('>')
                                output.write(lenth)
                                output.write('\n')
                                output.write(seq)
                                output.write('\n')
output.close()

我正在尝试做的不是手动输入检查是否超过 7kb,而是找到一种代码方式自行完成并自动找到 3 个热门点击。任何有关我可以去哪里的帮助将不胜感激。谢谢

【问题讨论】:

    标签: python sorting biopython fasta genbank


    【解决方案1】:

    您可以保留最大的 N 个列表(及其大小)。

    像这样的东西(这可能会崩溃,因为我无法测试它,但想法就在那里:

    from Bio import SeqIO
    file="sequence.gb"
    output=open("Top3.faa", "w")
    record=SeqIO.parse(file, "genbank")
    rec=next(record)
    print('The genes with the top 3 longest lengths have beens saved in Top3.faa')
    
    # Largest genes and their size, sorted from the shortest to the longest.
    # size first, gene name next, then seq.
    largest_genes = [ (0, None, None) ] * 3;  # initialize with the number of genes you need.
    for f in rec.features:
      end = f.location.end.position
      start = f.location.start.position
      length = end-start
      bug = (rec.seq)
      if f.type=='CDS' and 'gene' in f.qualifiers:
        if length > largest_genes[0][0]:  # [0] gives the first, [0] gives the length.
          # This one is larger than the smallest one we have.
          largest_genes = largest_genes[1:]  # drop the smallest one.
          # add this one
          largest_genes.append((length, f.qualifiers['gene'], str(bug[start:end])))  
          largest_genes.sort()  # re-sort.
    
    for length, name, seq in largest_genes:   
      # name is not used but available.
      output.write('>')
      output.write(str(lenth))
      output.write('\n')
      output.write(seq)
      output.write('\n')
    output.close()
    

    【讨论】:

    • 这是我所需要的。除了将其与名称匹配外,它还可以很好地工作,但希望我能对其进行排序!感谢您的帮助
    • 我没有在示例中包含名称,因为您没有将名称输出到文件中...如果您需要,我可以添加它。
    • 对此感到抱歉……整个网站都是新手。如果您能帮我解决这个问题,我们将不胜感激,谢谢
    • 没问题。我刚刚在回复中添加了名称,如果它不起作用,请告诉我,我可能错过了一些东西;)
    猜你喜欢
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多