Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Biopython遍历fasta文件并修改记录id_Python_Loops_Biopython_Fasta - Fatal编程技术网

如何使用Biopython遍历fasta文件并修改记录id

如何使用Biopython遍历fasta文件并修改记录id,python,loops,biopython,fasta,Python,Loops,Biopython,Fasta,我不是程序员,我是Python新手,我正在努力自学。。。因此,我有一个包含84个条目的文件,如下所示: 1 2 3 X Y MT GL000210.1 >1 agatagctagctgatcgatcgatttttttcga >2 gagatagatattattttttttttaagagagagcgcgatcgatgc >3 agatgctagggc ... 我想更改包含84条记录的fasta文件中所有序列的记录id。以下是fasta文件的一个示例: >name aga

我不是程序员,我是Python新手,我正在努力自学。。。因此,我有一个包含84个条目的文件,如下所示:

1
2
3
X
Y
MT
GL000210.1
>1
agatagctagctgatcgatcgatttttttcga
>2
gagatagatattattttttttttaagagagagcgcgatcgatgc
>3
agatgctagggc
...
我想更改包含84条记录的fasta文件中所有序列的记录id。以下是fasta文件的一个示例:

>name
agatagctagctgatcgatcgatttttttcga
>name1
gagatagatattattttttttttaagagagagcgcgatcgatgc
>name2
agatgctagggc
...
具体来说,我想通过上面示例文件的第一个条目更改第一个记录id(以“>”开头),依此类推。到目前为止,我创建了以下脚本。我可以一个接一个地更改id,但我不知道如何同时遍历这两个文件:

from Bio import SeqIO

records = list(SeqIO.parse("new_human_v37.fasta", "fasta"))
modified_record = records[0]
print(modified_record.id.replace("old_name", "first_entry_file1"))
输出文件应如下所示:

1
2
3
X
Y
MT
GL000210.1
>1
agatagctagctgatcgatcgatttttttcga
>2
gagatagatattattttttttttaagagagagcgcgatcgatgc
>3
agatgctagggc
...

有人能提供一些帮助吗?

您可以这样做(假设第一个文件的行数与第二个文件的行数相同)。如果要生成包含修改记录的新文件

from Bio import SeqIO
lines_file = open(my_lines_file, 'r')
fout = open("example.fa", "w")
records = list(SeqIO.parse("new_human_v37.fasta", "fasta"))

for r in records:
    line = lines_file.getline()
    r.id = line.rstrip()
    SeqIO.write(fout, r, 'fasta')


lines_file.close()
fout.close()

您可以这样做(假设第一个文件的行数与第二个文件的行数相同)。如果要生成包含修改记录的新文件

from Bio import SeqIO
lines_file = open(my_lines_file, 'r')
fout = open("example.fa", "w")
records = list(SeqIO.parse("new_human_v37.fasta", "fasta"))

for r in records:
    line = lines_file.getline()
    r.id = line.rstrip()
    SeqIO.write(fout, r, 'fasta')


lines_file.close()
fout.close()

您可以这样做(假设第一个文件的行数与第二个文件的行数相同)。如果要生成包含修改记录的新文件

from Bio import SeqIO
lines_file = open(my_lines_file, 'r')
fout = open("example.fa", "w")
records = list(SeqIO.parse("new_human_v37.fasta", "fasta"))

for r in records:
    line = lines_file.getline()
    r.id = line.rstrip()
    SeqIO.write(fout, r, 'fasta')


lines_file.close()
fout.close()

您可以这样做(假设第一个文件的行数与第二个文件的行数相同)。如果要生成包含修改记录的新文件

from Bio import SeqIO
lines_file = open(my_lines_file, 'r')
fout = open("example.fa", "w")
records = list(SeqIO.parse("new_human_v37.fasta", "fasta"))

for r in records:
    line = lines_file.getline()
    r.id = line.rstrip()
    SeqIO.write(fout, r, 'fasta')


lines_file.close()
fout.close()
试试这个

# first create a new file to write into ex: "fasta_file_new.fasta"
# then run the code
fasta_file_new = open("fasta_file_new.fasta", "w")
fasta_file_read = open("new_human_v37.fasta", "r")
replace_lines = open("replacer.txt", "r")


for f in fasta_file_read.readlines():
    if f.__contains__(">"):
        fasta_file_new.write(">" + replace_lines.readline())
    else:
        fasta_file_new.write(f)


fasta_file_new.close()
fasta_file_read.close()
replace_lines.close()
试试这个

# first create a new file to write into ex: "fasta_file_new.fasta"
# then run the code
fasta_file_new = open("fasta_file_new.fasta", "w")
fasta_file_read = open("new_human_v37.fasta", "r")
replace_lines = open("replacer.txt", "r")


for f in fasta_file_read.readlines():
    if f.__contains__(">"):
        fasta_file_new.write(">" + replace_lines.readline())
    else:
        fasta_file_new.write(f)


fasta_file_new.close()
fasta_file_read.close()
replace_lines.close()
试试这个

# first create a new file to write into ex: "fasta_file_new.fasta"
# then run the code
fasta_file_new = open("fasta_file_new.fasta", "w")
fasta_file_read = open("new_human_v37.fasta", "r")
replace_lines = open("replacer.txt", "r")


for f in fasta_file_read.readlines():
    if f.__contains__(">"):
        fasta_file_new.write(">" + replace_lines.readline())
    else:
        fasta_file_new.write(f)


fasta_file_new.close()
fasta_file_read.close()
replace_lines.close()
试试这个

# first create a new file to write into ex: "fasta_file_new.fasta"
# then run the code
fasta_file_new = open("fasta_file_new.fasta", "w")
fasta_file_read = open("new_human_v37.fasta", "r")
replace_lines = open("replacer.txt", "r")


for f in fasta_file_read.readlines():
    if f.__contains__(">"):
        fasta_file_new.write(">" + replace_lines.readline())
    else:
        fasta_file_new.write(f)


fasta_file_new.close()
fasta_file_read.close()
replace_lines.close()

您应该用一个解释来包装您的代码示例,解释为什么这会起作用,以及可能出现的其他问题(如果有的话)。仅仅添加代码还不足以让答案变得有用。@O.D.P好的,我理解你是如何做到的(readline vs readlines)。我试过了,效果很好。谢谢大家的帮助,阿佩萨,很高兴知道。这是我在上的第一篇文章之一,所以我只是在学习协议。FRANK_B-没问题,帮助并贡献给这个网站是很有趣的。你应该用代码示例来解释为什么这会起作用,以及可能出现的其他问题(如果有的话)。仅仅添加代码还不足以让答案变得有用。@O.D.P好的,我理解你是如何做到的(readline vs readlines)。我试过了,效果很好。谢谢大家的帮助,阿佩萨,很高兴知道。这是我在上的第一篇文章之一,所以我只是在学习协议。FRANK_B-没问题,帮助并贡献给这个网站是很有趣的。你应该用代码示例来解释为什么这会起作用,以及可能出现的其他问题(如果有的话)。仅仅添加代码还不足以让答案变得有用。@O.D.P好的,我理解你是如何做到的(readline vs readlines)。我试过了,效果很好。谢谢大家的帮助,阿佩萨,很高兴知道。这是我在上的第一篇文章之一,所以我只是在学习协议。FRANK_B-没问题,帮助并贡献给这个网站是很有趣的。你应该用代码示例来解释为什么这会起作用,以及可能出现的其他问题(如果有的话)。仅仅添加代码还不足以让答案变得有用。@O.D.P好的,我理解你是如何做到的(readline vs readlines)。我试过了,效果很好。谢谢大家的帮助,阿佩萨,很高兴知道。这是我在上的第一篇帖子,所以我只是在学习协议。FRANK_B-没问题,帮助和贡献这个网站很有趣。