make blastdb using python
2
0
Entering edit mode
7.0 years ago
luoterolb • 0

Hi, I want to run some blast locally. I have some queries and a lot of fasta files that would be my databases. But I need to convert from fasta to "database" format. I know how to do it with commandline (makeblastdb), but they are several fasta files, so I want to write a script and do it with (bio)python. I have been looking how to do it, but I haven't find out how. Can any one help me, please? Thanks!

blast database biopython • 6.5k views
ADD COMMENT
2
Entering edit mode

Using BioPython to makeblastdb a bunch of files is neither a smart learning process nor an efficient way to actually get your task done. Why use anything but the shell?

ADD REPLY
0
Entering edit mode

While you could use biopython, why not do that using a simple shell script?

ADD REPLY
0
Entering edit mode

Thanks for your answers!

ADD REPLY
0
Entering edit mode

Do not add an answer unless you're answering the question. This should be a comment reply. I'm moving it to a comment on the top level post now.

ADD REPLY
0
Entering edit mode

Well, I know I'm preety late for the topc conversation but. If you're up to UNIX, or to any other system that you can install BLAST local.

#!/usr/bin/env python
import sys
import os

#If you're using nt data in your working dir
blastn_db = "makeblastdb -in <sequence.fasta> -dbtype nucl -input_type fasta -out <database>"
os.system(blastn_db)

Instead of using bioblast or any other module, you'll simply call the os to run in the background of the shell. If found this a little bit more efficient than using bioblast/biopython, however in terms of speed I can't say if it's faster or not. Hope it helps

ADD REPLY
2
Entering edit mode
7.0 years ago
st.ph.n ★ 2.7k

just use shell as @genomax2 suggested;

Put your fasta files you want to make into dbs into a dir:

for file in *.fasta; do /path/to/blast_install/bin/makeblastdb -in $file -dbtype prot/nucl; done

if you need to use python:

#!/usr/bin/env python
import glob, subprocess
for file in glob.glob("*.fasta"):
    subprocess.call("/path/to/blast_install/bin/makeblastdb -in " + file + " -db type prot/nucl", shell=True)
ADD COMMENT
0
Entering edit mode
7.0 years ago
Joe 21k

I agree with this being a much better idea through a standard shell script but if you're adamant about doing it through python (perhaps incorporating it as part of a much longer and more complicated pipeline), you need to do something like this:

Use the subprocess module:

import subprocess 

blastdb_cmd = 'makeblastdb -in {0} -dbtype nucl -title temp_blastdb'.format(fasta)
DB_process = subprocess.Popen(blastdb_cmd,
                              shell=True,
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
DB_process.wait()

This is taken from my script here if you want to 'see it in action'.

ADD COMMENT

Login before adding your answer.

Traffic: 2547 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6