Esempio n. 1
0
 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
Esempio n. 2
0
 def blast_seq(cls, query, db='nr', evalue=0.001, command='blastn', **kwargs):
     '''Perform local blast of a SeqRecord against a database'''
     qfile = mktmp_fasta(query)
     results = cls.blast(command, query=qfile, db=db, 
                             evalue=evalue, **kwargs)
     os.unlink(qfile)
     return results
Esempio n. 3
0
 def s2f_blast(cls, query, subject_file, evalue=0.001, command='blastn', **kwargs):
     '''Perform local blast of a SeqRecord against a file with sequence(s)'''
     qfile = mktmp_fasta(query)
     results = cls.blast(command, query=qfile, subject=subject_file, 
                         evalue=evalue, **kwargs)
     os.unlink(qfile)
     return results
Esempio n. 4
0
 def format_tmp_db(cls, sequences, nucleotide=True):
     '''Create a temporary local blast database
     @param sequences: SeqRecord object to populate the database with
     @param nucleotide: if the sequences are nucleotide (True) or protein (False)
     @return: database name that includes includes its path'''
     basename = random_text(8)
     dbdir = tempfile.mkdtemp('_blastDB')
     sfile = mktmp_fasta(sequences)
     if run_cline(FormatDBCommandline(input=sfile, 
                                      protein='F' if nucleotide else 'T', 
                                      name=basename),
                  cwd=dbdir): 
         return os.path.join(dbdir, basename)
     else: shutil.rmtree(dbdir, ignore_errors=True)
Esempio n. 5
0
 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
Esempio n. 6
0
 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
Esempio n. 7
0
 def s2s_blast(cls, query, subject, evalue=0.001, command='blastn', **kwargs):
     '''Perform local blast of one SeqRecord against the other'''
     sfile = mktmp_fasta(subject)
     results = cls.s2f_blast(query, sfile, evalue, command, **kwargs)
     os.unlink(sfile)
     return results
Esempio n. 8
0
 def s2s_blast(cls, query, subject, evalue=0.001, command='blastn', **kwargs):
     '''Perform local blast of one SeqRecord against the other'''
     sfile = mktmp_fasta(subject)
     results = cls.s2f_blast(query, sfile, evalue, command, **kwargs)
     os.unlink(sfile)
     return results