This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Error passing generated fasta to blastn in biopython

I'm running a script which finds regions of interest, outputs them to a fasta, then passes them to blastn. Both steps work independently, but the script wont wait for the file to be generated and says:

./seq_targeter.py -i ecoli.fa -o tester -B
Testing if regions match guidelines.
Running blastn for all generated sequences.
('', 'Warning: [blastn] Query is Empty!\n')
All done, see files: tester-table.txt, tester.fasta and tester.bln

The script is basically this:

def main():
    outfasta = open(args.o + ".fasta", 'w')  
    seqTargeter(outfasta)               # Outputs a fasta file as expected
    blastFunc(args.o + ".fasta")      # Outputs a blank file and gives error above

seqTargeter(outfasta):
    print "Testing if regions match guidelines."
    .................
    outfasta.write('...........')

def blastFunc(blastfasta):
    print "Running blastn for all generated sequences."
    blastn_cline = NcbiblastnCommandline(query=blastfasta, db=args.d, outfmt=7, out=args.o + ".bln")()
    print blastn_cline

If I comment out the seqTargeter parts and let the script run on the already generated output.fasta file, then blastn will be called correctly and give the correct output. But if I want them both to work after the other, then nothing. I've been looking at something like this from Stackexchange, but I think the file exists doesn't work as it's still being generated...

Thanks

biopython blast blastn

Reposted comment as answer.

Perfect, totally what I'd missed. If you put this as an answer I could accept it.

1 answer

outfasta = open(args.o + ".fasta", 'w')

It's never closed again from what I see. The file content will not be properly written unless you close the file. Even if this is not the issue, try to use with open(file, 'w') as out: instead as it will close itself properly.

Log in to answer this question.