All*_*son 0 python bioinformatics fasta biopython python-2.7
例如,我有一个包含以下序列的fasta文件:
>human1
AGGGCGSTGC
>human2
GCTTGCGCTAG
>human3
TTCGCTAG
Run Code Online (Sandbox Code Playgroud)
如何使用python读取具有以下内容的文本文件来提取序列?1表示真,0表示假.仅提取值为1的序列.
示例文本文件:
0
1
1
Run Code Online (Sandbox Code Playgroud)
预期产量:
>human2
GCTTGCGCTAG
>human3
TTCGCTAG
Run Code Online (Sandbox Code Playgroud)
为此,最好使用biopython
from Bio import SeqIO
mask = ["1"==_.strip() for _ in open("mask.txt")]
seqs = [seq for seq in SeqIO.parse(open("input.fasta"), "fasta")]
seqs_filter = [seq for flag, seq in zip(mask, seqs) if flag]
for seq in seqs_filter:
print seq.format("fasta")
Run Code Online (Sandbox Code Playgroud)
你得到:
>human2 GCTTGCGCTAG >human3 TTCGCTAG
说明
解析fasta:格式fasta可能有几行序列(检查fasta格式),最好使用专门的库来读取(解析器)并写入输出
mask:我读取de mask文件并转换为boolean[False, True, True]
filter:使用zip函数为每个序列匹配他的掩码,然后我使用list comprehensions进行过滤