我现在在这上面花了太多时间(+10 小时)。
输入是fasta格式的文件。 输出应该是一个包含基因 ID 和匹配模式(三种不同模式)的文本文件
我想制作自己的函数以避免编写相同的代码三次,但我现在放弃了,只编写了三次(并且工作正常)。
有没有办法使用这个:
records = list(SeqIO.parse('mytextfile.fasta', 'fasta'))
而不是我当前使用三次的代码(如下)或其他函数?这是一个学校作业,所以它也不应该太复杂,但我必须使用 Bio 和重新模块来解决它。
from Bio import SeqIO
import re
outfile = 'sekvenser.txt'
for seq_record in SeqIO.parse('prot_sequences.fasta', 'fasta'):
match = re.findall(r'W.P', str(seq_record.seq), re.I)
if match:
with open(outfile, 'a') as f:
record_string = str(seq_record.id)
newmatch = str(match)
result = record_string+'\t'+newmatch
print(result)
f.write(result + '\n')
我试过这个
records = list(SeqIO.parse('prot_sequences.fasta', 'fasta'))
new_list = []
i = r'W.P'
for i in records:
match = re.findall(i)
if match:
new_list.append(match)
print(new_list)
但它只告诉我 findall() 缺少 1 个必需的位置参数:'string'。
正如我所见,i 是一个字符串(因为我创建了变量)。显然我做错了什么。如果我尝试插入在其他代码中使用的 seq_record,它会告诉我 seq_record 未定义。我不明白我应该在代码中的 i 之后放置什么。
输入
prot_sequences.fasta
:
>one
WWWWWWCCCCCPPPPPP
>two
SSSSSSSSRRRRRRRRTTTTWWWWWWDDDDDDMMMMM
>three
QQQQQQQQQWAPTCCCCCCCWYPGGGGGGGGGGGGGG
代码:
import Bio
from Bio import SeqIO
import re
print('biopython version : ', Bio.__version__)
records = list(SeqIO.parse('prot_sequences.fasta', 'fasta'))
new_list = []
i = r'W.P'
#print(i)
for rec in records:
#print('rec : ', rec, rec.seq , type(rec.seq)
match = re.findall(i, str(rec.seq) )
if match:
new_list.append(match)
print(new_list)
输出:
biopython version : your Biopython Version
[['WAP', 'WYP']]
如果您取消评论:
#print('rec : ', rec, rec.seq , type(rec.seq)
你会看到
rec.seq
它是一个<class 'Bio.Seq.Seq'>
所以不适合作为参数来提供re.findall(pattern, string, flags=0)