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
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 finally: safe_unlink(entry_batch) safe_unlink(out_file) def _s2s_blast_batch(self, queries, subjects, subject_locs=None, evalue=0.001, command='blastn', **kwargs): queries_len = len(queries) subjects_len = len(subjects) results = [[None for _s in subjects] for _q in queries] pairs = list(itertools.product(xrange(queries_len), xrange(subjects_len))) ignore_none_locs = kwargs.pop('ignore_none_locs', False) shuffle(pairs) @MultiprocessingBase.data_mapper @shelf_result def worker(qs, queries, subjects, subject_locs): query = queries[qs[0]] subject = subjects[qs[1]] if query is None or subject is None: return None
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 finally: safe_unlink(entry_batch) safe_unlink(out_file) @classmethod 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