This is a test version of Biostars. For the public version, visit https://www.biostars.org.
problems to makeblastdb, the fa file is not being recognised.

I have the above code to make a data base from different folder, and I am getting always the same error, 'contigs.fa' no recognized. I post my code, and hopefully someone can help me to find the error.

import glob, sys, os, subprocess
from Bio.Blast import NCBIXML
from Bio import SeqIO

def database_project():

    #folders where my data are store
    #looking for the files within the folder H*
    files = glob.glob('/home/my_name/velvet_results2/H*')
    for file in files:

        #looking for contigs.fa in each folder
        contigs=glob.glob(file + '/*.fa')
        print contigs


        #extract name of folder
        temporary_db = os.path.basename(file) 

        output= '/home/mu_name/velvet_results2/' + temporary_db +'_db'
        if os.path.exists(output):
            print 'This folder already exist'
        else:
            os.makedirs(output)


        cmd=['makeblastdb', '-in', 'contigs.fa', '-dbtype', 'nucl', '-out', 'temporary_db','-title', 'temporary_db']
        my_file=subprocess.Popen(cmd)
        my_file.wait()
        #return database_id
        print 'The database is done'
database_project()
blast

2 answers

Thanks guys, I found the problem, glob.glob gives a list, in this case with one element. In the previous script, I was not telling which element to take. So I changed contgs.fa by contigs[0], and it worked perfectly.

cmd=['makeblastdb', '-in', contigs[0], '-dbtype', 'nucl', '-out', file + '/temporary_db','-title', 'temporary_db']

Glad you figured it out, and learned something. I think your script can be condensed, and improved, but if it works, you're moving yourself forward with your skills.

import glob, sys, os, subprocess
from Bio.Blast import NCBIXML
from Bio import SeqIO

def database_project():
    os.chdir('/home/my_name/velvet_results2')
    dirs = os.walk(os.getcwd())
    for d in dirs:
         os.chdir(d)
         for file in glob.glob('*.fa')
             temp_db = d.split('/')[-1] + '_db'

             try: 
                  os.mkdirs(temp_db)
             except OSError:
                  print 'The folder already exists.'

             os.chdir(temp_db)
             subprocess.call('makeblasdb -in contigs.fa -dbtype nucl', shell=True)

database_project()

looks like you need a closing bracket in the line dirs = os.walk(os.getcwd()

Log in to answer this question.