替换dna序列fi中某个位置的核苷酸

2024-04-25 01:03:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个fasta文件,另一个文件包含位置,我想用默认设置替换每个序列的某个位置,例如,我的位置文件看起来像 a/c120,我的替换表看起来像a/c to W,所以我想得到一个新的fasta文件,其中120的位置被替换为W

这个程序是用Python编写的

所以第一个问题是我不能到达正确的位置,例如,如果我使用 我的序列号[0:3],我有序列名!不是顺序。 位置文件看起来像 id1 219空调

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")

Tags: 文件importaddidlinepositionopenrecord
1条回答
网友
1楼 · 发布于 2024-04-25 01:03:45

您的代码有点混乱,不应该:

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")

比利时:

^{pr2}$

我是一个需要核对核苷酸序列和快速序列的计数器吗

您还可以通过以下方式迭代fasta_序列:

^{3}$

id是每个元素的id并对其序列进行排序。在

更新

我想我明白你想做什么,浏览文件中的每个记录,检查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")

相关问题 更多 >