<s> import os . path as op <EOL> import sys <EOL> import logging <EOL> from multiprocessing import Lock <EOL> from jcvi . formats . base import must_open , split <EOL> from jcvi . apps . grid import Jobs <EOL> from jcvi . apps . align import run_formatdb <EOL> from jcvi . apps . base import OptionParser , Popen <EOL> def blastplus ( out_fh , cmd , query , lock ) : <EOL> cmd += "<STR_LIT>" . format ( query ) <EOL> proc = Popen ( cmd ) <EOL> logging . debug ( "<STR_LIT>" % ( proc . pid , cmd ) ) <EOL> for row in proc . stdout : <EOL> if row [ <NUM_LIT:0> ] == '<STR_LIT:#>' : <EOL> continue <EOL> lock . acquire ( ) <EOL> out_fh . write ( row ) <EOL> out_fh . flush ( ) <EOL> lock . release ( ) <EOL> logging . debug ( "<STR_LIT>" % proc . pid ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( main . __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , default = "<STR_LIT>" "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> p . set_align ( evalue = <NUM_LIT> ) <EOL> p . add_option ( "<STR_LIT>" , default = <NUM_LIT:1> , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> p . set_cpus ( ) <EOL> p . add_option ( "<STR_LIT>" , default = <NUM_LIT:1> , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" ) <EOL> p . set_params ( ) <EOL> p . set_outfile ( ) <EOL> opts , args = p . parse_args ( ) <EOL> if len ( args ) != <NUM_LIT:2> or opts . blast_program is None : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> bfasta_fn , afasta_fn = args <EOL> for fn in ( afasta_fn , bfasta_fn ) : <EOL> assert op . exists ( fn ) <EOL> afasta_fn = op . abspath ( afasta_fn ) <EOL> bfasta_fn = op . abspath ( bfasta_fn ) <EOL> out_fh = must_open ( opts . outfile , "<STR_LIT:w>" ) <EOL> extra = opts . extra <EOL> blast_path = opts . blast_path <EOL> blast_program = opts . blast_program <EOL> blast_bin = blast_path or blast_program <EOL> if op . basename ( blast_bin ) != blast_program : <EOL> blast_bin = op . join ( blast_bin , blast_program ) <EOL> nprocs , cpus = opts . nprocs , opts . cpus <EOL> if nprocs > <NUM_LIT:1> : <EOL> logging . debug ( "<STR_LIT>" % nprocs ) <EOL> outdir = "<STR_LIT>" <EOL> fs = split ( [ afasta_fn , outdir , str ( nprocs ) ] ) <EOL> queries = fs . names <EOL> else : <EOL> queries = [ afasta_fn ] <EOL> dbtype = "<STR_LIT>" if op . basename ( blast_bin ) in ( "<STR_LIT>" , "<STR_LIT>" ) else "<STR_LIT>" <EOL> db = bfasta_fn <EOL> if dbtype == "<STR_LIT>" : <EOL> nin = db + "<STR_LIT>" <EOL> else : <EOL> nin = db + "<STR_LIT>" <EOL> nin00 = db + "<STR_LIT>" <EOL> nin = nin00 if op . exists ( nin00 ) else ( db + "<STR_LIT>" ) <EOL> run_formatdb ( infile = db , outfile = nin , dbtype = dbtype ) <EOL> lock = Lock ( ) <EOL> blastplus_template = "<STR_LIT>" <EOL> blast_cmd = blastplus_template . format ( blast_bin , bfasta_fn , opts . format ) <EOL> blast_cmd += "<STR_LIT>" . format ( opts . evalue , opts . best ) <EOL> blast_cmd += "<STR_LIT>" . format ( cpus ) <EOL> if extra : <EOL> blast_cmd += "<STR_LIT:U+0020>" + extra . strip ( ) <EOL> args = [ ( out_fh , blast_cmd , query , lock ) for query in queries ] <EOL> g = Jobs ( target = blastplus , args = args ) <EOL> g . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
