我有以下序列,它们是具有序列标题及其核苷酸的fasta格式。如何随机提取序列。例如,我想从总序列中随机选择2个序列。提供的工具是根据百分比而不是序列数提取。任何人都可以帮助我吗?
A.fasta
>chr1:1310706-1310726
GACGGTTTCCGGTTAGTGGAA
>chr1:901959-901979
GAGGGCTTTCTGGAGAAGGAG
>chr1:983001-983021
GTCCGCTTGCGGGACCTGGGG
>chr1:984333-984353
CTGGAATTCCGGGCGCTGGAG
>chr1:1154147-1154167
GAGATCGTCCGGGACCTGGGT
预期输出
>chr1:1154147-1154167
GAGATCGTCCGGGACCTGGGT
>chr1:901959-901979
GAGGGCTTTCTGGAGAAGGAG
答案 0 :(得分:7)
如果您正在使用fasta文件,请使用BioPython,以使n
序列使用random.sample:
from Bio import SeqIO
from random import sample
with open("foo.fasta") as f:
seqs = SeqIO.parse(f,"fasta")
print(sample(list(seqs), 2))
输出:
[SeqRecord(seq=Seq('GAGATCGTCCGGGACCTGGGT', SingleLetterAlphabet()), id='chr1:1154147-1154167', name='chr1:1154147-1154167', description='chr1:1154147-1154167', dbxrefs=[]), SeqRecord(seq=Seq('GTCCGCTTGCGGGACCTGGGG', SingleLetterAlphabet()), id='chr1:983001-983021', name='chr1:983001-983021', description='chr1:983001-983021', dbxrefs=[])]
如有必要,您可以提取字符串:
print([(seq.name,str(seq.seq)) for seq in sample(list(seqs),2)])
[('chr1:1310706-1310726', 'GACGGTTTCCGGTTAGTGGAA'), ('chr1:983001-983021', 'GTCCGCTTGCGGGACCTGGGG')]
如果这些行总是成对出现并且您跳过顶部的元数据,则可以压缩:
from random import sample
with open("foo.fasta") as f:
print(sample(list(zip(f, f)), 2))
这将为您提供元组中的线对:
[('>chr1:983001-983021\n', 'GTCCGCTTGCGGGACCTGGGG\n'), ('>chr1:984333-984353\n', 'CTGGAATTCCGGGCGCTGGAG\n')]
准备好写行:
from Bio import SeqIO
from random import sample
with open("foo.fasta") as f:
seqs = SeqIO.parse(f, "fasta")
samps = ((seq.name, seq.seq) for seq in sample(list(seqs),2))
for samp in samps:
print(">{}\n{}".format(*samp))
输出:
>chr1:1310706-1310726
GACGGTTTCCGGTTAGTGGAA
>chr1:983001-983021
GTCCGCTTGCGGGACCTGGGG
答案 1 :(得分:2)
鉴于您显示的文件格式,并假设文件不是太大,您不需要任何外部模块(例如biopython)来执行此操作:
import random
with open('A.fasta') as f:
data = f.read().splitlines()
for i in random.sample(range(0, len(data), 2), 2):
print data[i]
print data[i+1]
示例输出:
>chr1:984333-984353 CTGGAATTCCGGGCGCTGGAG >chr1:901959-901979 GAGGGCTTTCTGGAGAAGGAG
这只是选择2个随机序列标题(来自A.fasta的那些行,偶数索引在data
中)和它后面的行。
如果您的文件很大,那么外部模块可能会进行优化以应对更大的数据集。
答案 2 :(得分:1)
对Fasta不太了解,但是Python有一个Fasta模块(你需要先安装它)。
>>> from pyfasta import Fasta
>>> f = Fasta('tests/test1.fasta')
>>> sorted(f.keys())
['chr1', 'chr2', 'chr3']
然后你可以使用Python的随机模块中的示例函数,并随机选择任意多个...
from random import sample
sample(f, how_many_you_want)
答案 3 :(得分:0)
import sys,random
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import generic_protein
# Use: python scriptname.py number_of_random_seq infile.fasta outfile.fasta
infile = sys.argv[2] #Name of the input file
seq = list(SeqIO.parse(infile,"fasta")) #Create a list with all the sequence records
print "Input fasta file = ", infile
totseq = len(seq) #Total number of sequences in the input file
print "Number of sequences in the original file = ", totseq
randseq = int(sys.argv[1]) #Number of random sequences desired
print "Number of random sequences desired = ", randseq
if randseq > totseq:
print "The requested number of random sequences is greater that the total number of input sequences. Exiting."
exit()
outfile = sys.argv[3] #Name of the output file
print "Output fasta file = ", outfile
outrandseq = []
outlist = []
print "Randomly chosen output sequences:"
for i in range(randseq):
choose = random.randint(1,totseq-1) #Choose a random sequence record number
for j in range(len(outrandseq)): #Test to see if the random sequence record number has already been chosen
if choose == outrandseq[j]:
choose = random.randint(1,totseq-1) #Choose a new random sequence record number if the current one has already been chosen
outrandseq.append(choose)
print choose
outseq = seq[choose]
outlist.append(outseq) #Append seq record to output list
SeqIO.write(outlist, outfile, "fasta") #Write the output list to the outfile
exit()
答案 4 :(得分:0)
import sys,random
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import generic_protein
# I use this from small numbers of sequences (input file up to 10000 sequences) and it works fine.
# For very large sequence sets it may be too slow -- I just have not tried.
# Use: python scriptname.py number_of_random_seq infile.fasta outfile.fasta
infile = sys.argv[2] #Name of the input file
seq = list(SeqIO.parse(infile,"fasta")) #Create a list with all the sequence records
print "Input fasta file = ", infile
totseq = len(seq) #Total number of sequences in the input file
print "Number of sequences in the original file = ", totseq
numrandseq = int(sys.argv[1]) #Number of random sequences desired
print "Number of random sequences desired = ", numrandseq
if numrandseq > totseq:
print "The requested number of random sequences is greater that the total number of input sequences. Exiting."
exit()
outfile = sys.argv[3] #Name of the output file
print "Output fasta file = ", outfile
outrandseqset = []
i = 1
for i in range(numrandseq): #Create a list of random sequence record numbers for output
choice = random.randint(1,totseq)
outrandseqset.append(choice)
i = 1
j = 1
duplicate = 1
while duplicate: #Make sure no sequences are duplicated in the list
duplicate = 0
for i in range(numrandseq):
for j in range(i+1, numrandseq):
if outrandseqset[i] == outrandseqset[j]:
outrandseqset[j] = random.randint(1,totseq)
duplicate = 1
i = 1
print "Randomly chosen output sequences:"
for i in range(numrandseq):
print outrandseqset[i]
outlist = []
i = 1
for i in range(numrandseq): #Create the list of seq records to be written to the output file
seqnum = outrandseqset[i]
outseq = seq[seqnum]
outlist.append(outseq)
SeqIO.write(outlist, outfile, "fasta") #Write the output list to the outfile
exit()
答案 5 :(得分:-1)
取决于您是否安装了unix sort
或shuf
。如果是这样,它很容易Select random 3000 lines from a file with awk codes
grep'>' A.fasta> FILE
重叠群=
sort -R FILE | head -n2|tr "\n" " "
或
重叠群=
shuf -n2 FILE|tr "\n" " "
然后,使用samtools提取
samtools faidx A.fasta $CONTIGS