如何使用Python过滤掉基于给定数据的序列?

时间:2015-07-08 08:16:21

标签: python filter filtering bioinformatics fasta

我会根据给定的文件A.fasta过滤掉我不想要的序列。原始文件包含所有序列,而fasta文件实际上是一个以序列ID开头的文件,后面是由A,T,C,G表示的核苷酸。任何人都可以帮助我吗?

A.fasta

>chr12:15747942-15747949
TGACATCA
>chr2:130918058-130918065
TGACCTCA

Original.fasta

>chr3:99679938-99679945
TGACGTAA
>chr9:135822160-135822167
TGACCTCA
>chr12:15747942-15747949
TGACATCA
>chr2:130918058-130918065
TGACCTCA
>chr2:38430457-38430464
TGACCTCA
>chr1:112381724-112381731
TGACATCA

C.fasta的预期输出

>chr3:99679938-99679945
TGACGTAA
>chr9:135822160-135822167
TGACCTCA
>chr2:38430457-38430464
TGACCTCA
>chr1:112381724-112381731
TGACATCA

代码

import sys
import warnings
from Bio import SeqIO
from Bio import BiopythonDeprecationWarning
warnings.simplefilter('ignore',BiopythonDeprecationWarning)

fasta_file = sys.argv[1]  # Input fasta file
remove_file = sys.argv[2] # Input wanted file, one gene name per line
result_file = sys.argv[3] # Output fasta file

remove = set()
with open(remove_file) as f:
    for line in f:
        line = line.strip()
        if line != "":
            remove.add(line)

fasta_sequences = SeqIO.parse(open(fasta_file),'fasta')

with open(result_file, "w") as f:
    for seq in fasta_sequences:
        nuc = seq.seq.tostring()
        if nuc not in remove and len(nuc) > 0:
            SeqIO.write([seq], f, "fasta")

上面的代码会过滤掉重复的序列,但如果它出现在输出中,我想保留重复的序列

1 个答案:

答案 0 :(得分:1)

查看BioPython。以下是使用该解决方案的解决方案:

from Bio import SeqIO

input_file = 'a.fasta'
merge_file = 'original.fasta'
output_file = 'results.fasta'
exclude = set()
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
    exclude.add(fasta.id)

fasta_sequences = SeqIO.parse(open(merge_file),'fasta')
with open(output_file, 'w') as output_handle:
   for fasta in fasta_sequences:
        if fasta.id not in exclude:
            SeqIO.write([fasta], output_handle, "fasta")