def start_blast(cmd, seq, seqString=None, seqDict=None, **kwargs): """Run blast and return results.""" p = classutil.FilePopen(cmd, stdin=classutil.PIPE, stdout=classutil.PIPE, **kwargs) if seqString is None: seqString = seq if seqDict is not None: # write all seqs to nonblocking ifile for seqID, seq in seqDict.iteritems(): write_fasta(p.stdin, seq) seqID = None else: # just write one query sequence seqID = write_fasta(p.stdin, seqString) if p.wait(): # blast returned error code raise OSError('command %s failed' % ' '.join(cmd)) return seqID, p
def repeat_mask(seq, progname='RepeatMasker', opts=()): 'Run RepeatMasker on a sequence, return lowercase-masked string' ## fd, temppath = tempfile.mkstemp() ## ofile = os.fdopen(fd, 'w') # text file p = classutil.FilePopen([progname] + list(opts), stdin=classutil.PIPE, stdinFlag=None) write_fasta(p.stdin, seq, reformatter=lambda x:x.upper()) # save uppercase! try: if p.wait(): raise OSError('command %s failed' % ' '.join(p.args[0])) ifile = file(p._stdin_path + '.masked', 'rU') # text file try: for id,title,seq_masked in read_fasta(ifile): break # JUST READ ONE SEQUENCE finally: ifile.close() finally: # clean up our temp files no matter what happened p.close() # close temp stdin file for fpath in glob.glob(p._stdin_path + '.*'): try: os.remove(fpath) except OSError: pass return seq_masked # ONLY THE REPEATS ARE IN LOWERCASE NOW