使用biopython重命名交错的fastq标头

时间:2018-10-10 08:22:54

标签: python replace bioinformatics biopython fastq

为了易于使用并与另一个下游管道兼容,我尝试使用biopython更改fastq序列ID的名称。例如...从标题如下:

@D00602:32:H3LN7BCXX:1:1101:1205:2112 OP:i:1
@D00602:32:H3LN7BCXX:1:1101:1205:2112 OP:i:2
@D00602:32:H3LN7BCXX:1:1101:1182:2184 OP:i:1
@D00602:32:H3LN7BCXX:1:1101:1182:2184 OP:i:2

到标题如下:

@000000000000001  OP:i:1
@000000000000001  OP:i:2
@000000000000002  OP:i:1
@000000000000002  OP:i:2

我有一些代码,但是我似乎无法减少交替头的计数(即1,1,2,2,3,3等)

任何帮助将不胜感激。谢谢。

from Bio import SeqIO
import sys

FILE = sys.argv[1]

#Initialize numbering system at one
COUNT = 1

#Create a new dictionary for new sequence IDs
new_records=[]

for seq_record in SeqIO.parse(FILE, "fastq"):
        header = '{:0>15}'.format(COUNT)
        COUNT += 1
        print(header)
        seq_record.description = 
seq_record.description.replace(seq_record.id, "")
        seq_record.id = header
        new_records.append(seq_record)
SeqIO.write(new_records, FILE, "fastq")

* seq_record不包含“ OP:i:1”信息

1 个答案:

答案 0 :(得分:4)

假设您希望所有标签都重复,那么您要做的就是将计数除以重复的数量并将返回值四舍五入,如下所示。

from Bio import SeqIO
import sys

FILE = sys.argv[1]

#Initialize numbering system at one
COUNT = 0

#Create a new dictionary for new sequence IDs
new_records=[]

for seq_record in SeqIO.parse(FILE, "fastq"):
        header = '{:0>15}'.format(COUNT//2+1)
        COUNT += 1
        print(header)
        seq_record.description = 
seq_record.description.replace(seq_record.id, "")
        seq_record.id = header
        new_records.append(seq_record)
SeqIO.write(new_records, FILE, "fastq")