def fetch_results(cls, results, db, what='record'): '''Fetch records that were found by BLAST search from the database @param results: an iterable of Bio.Blast.Record.Blast objects (aka BlastRecords) @param db: name of the BLAST database to get records from @param what: string, one of "record", "alignment" or "hsp" @return: list of SeqRecord objects''' #parse results into queries queries = [] for record in results: for alignment in record.alignments: q = cls.Query(alignment, what) if not q: continue queries.append(q) if not queries: return None #fetch records from database out_file = mktmp_name('.fasta') entry_batch = mktmp_name('.qry') qstr = '\n'.join(str(q) for q in queries) with open(entry_batch, 'w') as out: out.write(qstr) try: cline = BlastDBcmdCommandline(db=db, out=out_file, entry_batch=entry_batch) out, err = cline() if err: print '\nError in blastdbcmd call:\n%s\n%s\n%s' % (cline, out, err) return None #parse results records = list(SeqIO.parse(out_file, 'fasta')) return records except Exception, e: print e return None
def align(cls, seq_records, outfile=None): '''Align given sequences @param seq_records: a list of SeqRecords objects @param outfile: a filename for the output alignment or None @return: if the outfile is none, return an AlignmentExt object; otherwise return True on success. In both cases return None on error.''' if not outfile: outfile = mktmp_name('.aln.fasta') remove_out = True else: remove_out = False msafile = mktmp_fasta(seq_records) args = dict(thread=cpu_count, input=msafile) if len(seq_records) < 10000: args['auto'] = True else: args['parttree'] = True args['partsize'] = 1000 ali = None if run_cline(MafftCommandline(**args), stdout=outfile): if remove_out: ali = AlignmentExt.from_msa(AlignIO.read(outfile, 'fasta')) else: ali = True if remove_out: safe_unlink(outfile) safe_unlink(msafile) return ali
def align(cls, seq_records, outfile=None): '''Align given sequences @param seq_records: a list of SeqRecords objects @param outfile: a filename for the output alignment or None @return: if the outfile is none, return an AlignmentExt object; otherwise return True on success. In both cases return None on error.''' if not outfile: outfile = mktmp_name('.aln.fasta') remove_out = True else: remove_out = False msafile = mktmp_fasta(seq_records) args = dict(thread=-1, input=msafile) if len(seq_records) < 10000: args['auto'] = True else: args['parttree'] = True args['partsize'] = 1000 ali = None if run_cline(MafftCommandline(**args), stdout=outfile): if os.path.isfile(outfile) and os.path.getsize(outfile) > 0: if remove_out: ali = AlignmentExt.from_msa(AlignIO.read(outfile, 'fasta')) else: ali = True else: ali = False if remove_out: safe_unlink(outfile) safe_unlink(msafile) return ali
def mktmp(cls, alignments, schema='fasta'): outfile = mktmp_name('.%s' % schema) if cls.save(alignments, outfile, schema): return outfile else: safe_unlink(outfile) return None
def fetch_queries(cls, queries, db): out_file = mktmp_name('.fasta') entry_batch = mktmp_name('.qry') qstr = '\n'.join(str(q) for q in queries) with open(entry_batch, 'w') as out: out.write(qstr) try: cline = BlastDBcmdCommandline(db=db, out=out_file, entry_batch=entry_batch) out, err = cline() if err: print '\nError in blastdbcmd call:\n%s\n%s\n%s' % (cline, out, err) return None #parse results records = list(SeqIO.parse(out_file, 'fasta')) return records except Exception, e: print e return None
def build_fast_tree(cls, alignment, outfile): '''Build an approximate-ML tree with fasttree. @param outfile: output filename @param alignment: alignment object or a filename of an alignment in fasta format''' print 'Building an approximate-ML tree with fasttree.' if isinstance(alignment, MultipleSeqAlignment): alnfile = mktmp_name('.aln.fasta') cls.save(alignment, alnfile) elif isinstance(alignment, basestring): alnfile = alignment else: raise TypeError('Unsupported type of alignment argument: %s' % type(alignment)) args = dict(input=alnfile, out=outfile, pseudo=1) if num_fasta_records(alnfile) >= 10000: args['fastest'] = True args['boot'] = 100 if not run_cline(FastTreeCommandline(**args), name=cls.strip_ext(outfile)): return False print 'Done\n' return True
def hmmsearch_recs(hmm, recs, **kwargs): recfile = mktmp_fasta(recs) hmm_out = mktmp_name('.hmm.txt') try: cline = HMMSearchCommandline(hmmfile=hmm, seqdb=recfile, o=hmm_out, cpu=cpu_count, seed=0, **kwargs) stdout, stderr = cline() print stdout if stderr: sys.stderr.write(stderr) sys.stderr.flush() #parse hmmsearch results with open(hmm_out) as inp: return list(Hmmer3TextParser(inp)) except Exception, e: print 'Error while running hmmsearch.' print e return None
def blast(cls, command, **kwargs): '''Generic wrapper for blast commandline programs''' results_file = kwargs.pop('save_results_to', None) parse_results = kwargs.pop('parse_results', False) if results_file: bout = results_file else: parse_results = True bout = mktmp_name('.xml') try: cline = cls._clines[command] cmd = cline(outfmt=5, out=bout, **kwargs) out, err = cmd() if err: print ('\nError message from %s:\n%s\n%s\n%s' % (command, cmd, out, err)) if parse_results: with open(bout) as inp: results = list(NCBIXML.parse(inp)) return results except Exception, e: print '\nError while performing local %s:\n%s' % (command, estr(e)) return None
def blast(cls, command, **kwargs): '''Generic wrapper for blast commandline programs''' results_file = kwargs.pop('save_results_to', None) parse_results = kwargs.pop('parse_results', False) if results_file: bout = results_file else: parse_results = True bout = mktmp_name('.xml') try: cline = cls._clines[command] cmd = cline(outfmt=5, out=bout, **kwargs) out, err = cmd() if err: print ('\nError while performing local %s:\n%s\n%s\n%s' % (command, cmd, out, err)) return None if parse_results: with open(bout) as inp: results = list(NCBIXML.parse(inp)) return results except Exception, e: print '\nError while performing local %s:' % command print e return None
def mktmp(cls, alignments, schema='fasta'): outfile = mktmp_name('.%s' % schema) cls.save(alignments, outfile, schema)