如何使用biopython提取和修剪fasta序列

时间:2017-11-07 16:00:09

标签: python biopython fasta

每个人,我是python的新手,正在努力使用biopython做一个小任务。我有两个文件 - 一个包含id列表和相关的number.eg

id.txt

tr_F6LMO6_F6LMO6_9LE  25
tr_F6ISE0_F6ISE0_9LE  17
tr_F6HSF4_F6HSF4_9LE  27
tr_F6PLK9_F6PLK9_9LE  19
tr_F6HOT8_F6HOT8_9LE  29

包含大量fasta序列的第二个文件。下面是

fasta_db.fasta

    >tr|F6LMO6|F6LMO6_9LEHG Transporter
    MLAPETRRKRLFSLIFLCTILTTRDLLSVGIFQPSHNARYGGMGGTNLAIGGSPMDIGTN
    PANLGLSSKKELEFGVSLPYIRSVYTDKLQDPDPNLAYTNSQNYNVLAPLPYIAIRIPIT
    EKLTYGGGVYVPGGGNGNVSELNRATPNGQTFQNWSGLNISGPIGDSRRIKESYSSTFYV

   >tr|F6ISE0|F6ISE0_9LEHG peptidase domain protein OMat str.  
    MPILKVAFVSFVLLVFSLPSFAEEKTDFDGVRKAVVQIKVYSQAINPYSPWTTDGVRASS
    GTGFLIGKKRILTNAHVVSNAKFIQVQRYNQTEWYRVKILFIAHDCDLAILEAEDGQFYK

   >tr|F6HSF4|F6HSF4_9LEHG hypothetical protein,  
    MNLRSYIREIQVGLLCILVFLMSLYLLYFESKSRGASVKEILGNVSFRYKTAQRKFPDRM
    LWEDLEQGMSVFDKDSVRTDEASEAVVHLNSGTQIELDPQSMVVLQLKENREILHLGEGS


   >tr|F6PLK9|F6PLK9_9LEHG Uncharacterized protein mano str. 
   MRKITGSYSKISLLTLLFLIGFTVLQSETNSFSLSSFTLRDLRLQKSESGNNFIELSPRD
   RKQGGELFFDFEEDEASNLQDKTGGYRVLSSSYLVDSAQAHTGKRSARFAGKRSGIKISG

我希望将第一个文件中的id与第二个文件匹配,并在删除长度后将这些匹配的seq打印在新文件中(从1到25,以eq为单位)。

例如输出[25(与id,第一个文件相关联的值),当id匹配时,aa从开始时删除。)

fasta_pruned.fasta

>tr|F6LMO6|F6LMO6_9LEHG Transporter     
LLSVGIFQPSHNARYGGMGGTNLAIGGSPMDIGTNPANLGLSSKKELEFGVSL
PYIRSVYTDKLQDPDPNLAYTNSQNYNVLAPLPYIAIRIPITEKLTYGGGVYV
PGGGNGNVSELNRATPNGQTFQNWSGLNISGPIGDSRRIKESYSSTFYV

Biopython cookbook在我脑海中不再是python编程的新手。感谢您提供任何帮助。

我试着搞砸了。就是这样。

from Bio import SeqIO
from Bio import Seq

f1 = open('fasta_pruned.fasta','w')


lengthdict = dict() 
with open("seqid_len.txt") as seqlengths:
    for line in seqlengths:
        split_IDlength  = line.strip().split(' ')
        lengthdict[split_IDlength[0]] = split_IDlength[1]


with open("species.fasta","rU") as spe:
    for record in SeqIO.parse(spe,"fasta"):
        if record[0] == '>' :
            split_header = line.split('|')
            accession_ID = split_header[1]
            if accession_ID in lengthdict:
                f1.write(str(seq_record.id) + "\n")
                f1.write(str(seq_record_seq[split_IDlength[1]-1:]))



f1.close()

1 个答案:

答案 0 :(得分:2)

除了一些阻止它提供所需输出的小东西外,你的代码几乎包含所有内容:

  • 您的文件id.txt在ID和位置之间有两个空格。你拿第二个在这种情况下是空的元素。
  • 读取文件时,它被解释为字符串,但您希望该位置为整数

    lengthdict[split_IDlength[0]] = int(split_IDlength[-1])
    
  • 您的ID非常相似但不完全相同,唯一相同的部分是可用于映射两个文件的6个字符标识符(在您假设它起作用之前仔细检查)。拥有相同的键使映射更容易。

    f1 = open('fasta_pruned.fasta', 'w')
    
    fasta = dict()
    with open("species.fasta","rU") as spe:
        for record in SeqIO.parse(spe, "fasta"):
            fasta[record.id.split('|')[1]] = record
    
    lengthdict = dict() 
    with open("seqid_len.txt") as seqlengths:
        for line in seqlengths:
            split_IDlength  = line.strip().split(' ')
            lengthdict[split_IDlength[0].split('_')[1]] = int(split_IDlength[1])
    
    for k, v in lengthdict.items():
        if fasta.get(k) is None:
            continue
        print('>' + k)
        print(fasta[k].seq[v:])
        f1.write('>{}\n'.format(k))
        f1.write(str(fasta[k].seq[v:]) + '\n')
    
    f1.close()
    

输出:

>F6LMO6
LLSVGIFQPSHNARYGGMGGTNLAIGGSPMDIGTNPANLGLSSKKELEFGVSLPYIRSVYTDKLQDPDPNLAYTNSQNYNVLAPLPYIAIRIPITEKLTYGGGVYVPGGGNGNVSELNRATPNGQTFQNWSGLNISGPIGDSRRIKESYSSTFYV
>F6ISE0
LPSFAEEKTDFDGVRKAVVQIKVYSQAINPYSPWTTDGVRASSGTGFLIGKKRILTNAHVVSNAKFIQVQRYNQTEWYRVKILFIAHDCDLAILEAEDGQFYK
>F6HSF4
YFESKSRGASVKEILGNVSFRYKTAQRKFPDRMLWEDLEQGMSVFDKDSVRTDEASEAVVHLNSGTQIELDPQSMVVLQLKENREILHLGEGS
>F6PLK9
IGFTVLQSETNSFSLSSFTLRDLRLQKSESGNNFIELSPRDRKQGGELFFDFEEDEASNLQDKTGGYRVLSSSYLVDSAQAHTGKRSARFAGKRSGIKISG
>F6HOT8