I am new to Python. I am trying to take genome fasta file containing 8 chromosome sequences as input, blast it against a query sequence and extract the top 50 hits. Hre's my code:
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Blast import NCBIXML
from Bio.Blast.Applications import NcbiblastnCommandline
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC
import os
db = list(SeqIO.parse('genome.fasta', 'fasta'))
for i in range(len(db)):
print "Chromosome_"+str(i+1)
print('Doing the BLAST and retrieving the results...')
output_handle = open("dbn.fasta", "w")
nseq=db[i]
SeqIO.write(nseq, output_handle, "fasta")
output_handle.close()
os.system("makeblastdb -in dbn.fasta -dbtype nucl -out dbn")
result_handle= NcbiblastnCommandline(query="sequence.fasta", db = "dbn", outfmt= 5, out="my_blast_"+str(i)+".xml", evalue = 0.00001, task = "megablast")
stderr, stdout = result_handle()
E_VALUE_THRESH = 1e-100
c=0
print "Extracting hits for Chromosome"+str(i+1)+"in another file"
for record in NCBIXML.parse(open("my_blast_"+str(i)+".xml")):
if record.alignments:
for align in record.alignments:
for hsp in align.hsps:
if hsp.expect < E_VALUE_THRESH:
if c>50: break
start=hsp.sbjct_start
end=hsp.sbjct_end
newSeq = db[i].seq[:end]
newSeq = newSeq[start:]
ids=db[i].id+str(c)
myseq[c]=SeqRecord(Seq(newSeq,IUPAC.DNA))
myseq[c].id=str(ids)
c=c+1
output_handler = open("example_"+str(i)+".fasta", "w")
SeqIO.write(myseq, output_handler, "fasta")
output_handler.close()
It gives me an error on line 33:
AttributeError: 'module' object has no attribute 'DNA'
I tried to remove IUPAC.DNA, making the line:
myseq[c]=SeqRecord(Seq(newSeq)
It gives me a type error:
TypeError: The sequence data given to a Seq object should be a string (not another Seq object etc)
Is there a way to solve this, and is there any other way to create a multi-fasta list variable?