将Ns添加到可变长度序列的最快方法,使它们都等于150bp

时间:2017-02-24 01:38:22

标签: bioinformatics biopython dna-sequence

说我有一个包含3个序列的fasta ......

ATTTTTGGA
AT
A

我希望我的序列数据看起来像这样:

ATTTTTGGA
ATTNNNNNN
ANNNNNNNN

是否有任何程序或脚本可以在合理的时间范围内完成此任务。我有成千上万的序列。谢谢!

我乱搞并尝试了这个,文件最后一片空白,但这是我已经得到的。

import sys
from Bio import SeqIO
from Bio.Seq import Seq
in_file = open(sys.argv[1],'r')
sequences = SeqIO.parse(in_file, "fasta")
output_in_file = open("test.fasta", "w")
for record in sequences:
    n = 150
    record.seq = record.seq + ("N" * n)
    seq = seq[:n]
output_in_file.close()
in_file.close()

1 个答案:

答案 0 :(得分:4)

改善您的代码,

import sys
from Bio import SeqIO
from Bio.Seq import Seq
with open(sys.argv[1], "r") as in_file:
    sequences = list(SeqIO.parse(in_file, "fasta"))
    n = max(map(len, sequences))   #find max len in sequences
    for record in sequences:
        record.seq = record.seq + ("N" * (n-len(record)))
    SeqIO.write(sequences, "test.fasta", "fasta")

你得到test.fasta

>id_1
ATTTTTGGA
>id_2
ATNNNNNNN
>id_3
ANNNNNNNN

表示"全部等于150bp"

import sys
from Bio import SeqIO
from Bio.Seq import Seq
with open(sys.argv[1], "r") as in_file:
    sequences = list(SeqIO.parse(in_file, "fasta"))
    n = 150
    for record in sequences:
        record.seq = record.seq + ("N" * (n-len(record)))
    SeqIO.write(sequences, "test.fasta", "fasta")

你明白了,

>id_1
ATTTTTGGANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
>id_2
ATNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
>id_3
ANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN