Skip to content

Instantly share code, notes, and snippets.

@davipatti
Last active December 16, 2020 16:08
Show Gist options
  • Save davipatti/f33d0fdbd4e3af24486a2d821104c4ad to your computer and use it in GitHub Desktop.
Save davipatti/f33d0fdbd4e3af24486a2d821104c4ad to your computer and use it in GitHub Desktop.
Driving NCBI BLAST from python without writing to disk
import os
from Bio.Blast.Applications import NcbiblastpCommandline
from Bio import SeqIO
# Grab any sequence to blast
with open("db.fasta", "r") as f:
record = next(SeqIO.parse(f, "fasta"))
read, write = os.pipe()
parent = os.fork()
if parent:
os.close(write)
read = os.fdopen(read)
cline = NcbiblastpCommandline(db="db.fasta")
cline(stdin=read.read())
exit()
else:
os.close(read)
write = os.fdopen(write, "w")
SeqIO.write(record, write, "fasta")
write.close()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment