Exemple #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
 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
Exemple #3
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)
Exemple #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)
     return None
Exemple #5
0
 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
Exemple #6
0
 def hmmbuild(alignment, outfile, name=None, **kwargs):
     unlink_file = False
     if isinstance(alignment, str): msafile = alignment
     elif isinstance(alignment, MultipleSeqAlignment):
         msafile = AlignmentUtils.mktmp(alignment)
         if not msafile: return False
         unlink_file = True
     else:
         print 'Alignment must be either a filename or an instance of MultipleSeqAlignment'
         return False
     if not name: name = FilenameParser.strip_ext(os.path.basename(outfile))
     ret = run_cline(HMMBuildCommandline(input=msafile,
                                         out=outfile,
                                         n=name,
                                         cpu=cpu_count,
                                         seed=0,
                                         **kwargs),
                     _msg='Unable to build HMM profile')
     if unlink_file: os.unlink(msafile)
     return ret
Exemple #7
0
 def hmmbuild(alignment, outfile, name=None, **kwargs):
     msafile = AlignmentUtils.mktmp(alignment)
     if not name: name = FilenameParser.strip_ext(outfile)
     return run_cline(HMMBuildCommandline(input=msafile, out=outfile, 
                                          n=name, cpu=cpu_count, seed=0, **kwargs), 
                      _msg = 'Unable to build HMM profile')