我有一个fasta文件,另一个文件包含位置,我想用默认设置替换每个序列的某个位置,例如,我的位置文件看起来像 a / c 120,我的替换表看起来像a / c到W,所以我想获得一个新的fasta文件,其中120位被替换为w。
该程序是用Python编写的
所以第一个问题是我无法找到正确的位置,例如,如果我使用的话 my_seq_id [0:3],我得到了序列名称!不是顺序。 位置文件看起来像 id1 219 A / C
from Bio import SeqIO
import sys
import string
userInput1=raw_input("enter your sequence:")
userInput2=raw_input('enter your position file:')
fasta_file=userInput1
position_file=userInput2
result_file="outfile.txt"
id_list=list()
position_list=list()
nucleotide_list=list()
with open(position_file) as f:
for line in f:
line=line.strip()
headerline = line.split()
position=headerline[1]
ID=headerline[0]
nucleotide=headerline[2]
nucleotide_list.add(nucleotide)
position_list.add(position)
id_list.add(ID)
fasta_sequence=SeqIO.parse(open(fasta_file), 'fasta')
with open(result_file, 'w') as f:
if seq_record.id in wanted and nucleotide_list="A/C":
seq_record[position_list]="W\n"
SeqIO.write([seq_record], f, "fasta")
答案 0 :(得分:0)
您的代码有点令人困惑,不应该:
fasta_sequence=SeqIO.parse(open(fasta_file), 'fasta')
with open(result_file, 'w') as f:
if seq_record.id in wanted and nucleotide_list="A/C":
seq_record[position_list]="W\n"
SeqIO.write([seq_record], f, "fasta")
是:
fasta_sequence=SeqIO.parse(open(fasta_file), 'fasta')
with open(result_file, 'w') as f:
if fasta_sequence[j].id in wanted and nucleotide_list[i]=="A/C":
fasta_sequence[j].seq[position_list]="W\n"
SeqIO.write([fasta_sequence[j]], f, "fasta")
让我通过核苷酸列表和j来经历fasta_sequence
您还可以通过这种方式迭代fasta_sequence:
for record in SeqIO.parse(StringIO(data), "fasta"):
print("%s %s" % (record.id, record.seq))
id为每个元素的id并seq其序列。
----更新----
我想我明白你想要做什么,浏览文件中的每条记录,检查id是否匹配,然后改变该位置的顺序:
#goes through each record in the file
for record in SeqIO.parse(StringIO(data), "fasta"):
# check if id is wanted
if record.id in id_list:
# get list of every item in id_list that matches record.id
positions_of_id_in_id_list = [i for i, j in enumerate(id_list) if j == record.id]
for elem_position_lists in positions_of_id_in_id_list:
# I think here you want to write the new record in the correct position (substitute "W\n" with new item. Maybe nucleotide[elem_position_lists]?)
record.seq[position_list[elem_position_lists]] = "W\n"
# write new file
SeqIO.write(fasta_sequence, f, "fasta")