Exemplo n.º 1
0
 def test_write_sequences_to_fasta_file(self):
     """Tests writing to a FASTA file"""
     seqs = st.read_sequences_from_fasta_file('testdata/fasta_test.fa')
     with open('/tmp/fasta_tmp.fa', 'w') as outputfile:
         st.write_sequences_to_fasta_file(outputfile, seqs)
     seqs2 = st.read_sequences_from_fasta_file('/tmp/fasta_tmp.fa')
     self.assertEquals(seqs, seqs2)
Exemplo n.º 2
0
 def test_write_sequences_to_fasta_file(self):
     """Tests writing to a FASTA file"""
     seqs = st.read_sequences_from_fasta_file('testdata/fasta_test.fa')
     with open('/tmp/fasta_tmp.fa', 'w') as outputfile:
         st.write_sequences_to_fasta_file(outputfile, seqs)
     seqs2 = st.read_sequences_from_fasta_file('/tmp/fasta_tmp.fa')
     self.assertEquals(seqs, seqs2)
Exemplo n.º 3
0
 def test_write_sequences_to_fasta_file_empty_seqs(self):
     """Tests ensures that only non-empty sequences will be written to FASTA"""
     seqs = [['seq1', 'TATATA'], ['seq2', '']]
     with open('/tmp/fasta_tmp.fa', 'w') as outputfile:
         st.write_sequences_to_fasta_file(outputfile, seqs)
     seqs2 = st.read_sequences_from_fasta_file('/tmp/fasta_tmp.fa')
     self.assertEquals(1, len(seqs2))
     self.assertEquals(seqs[0][0], seqs2[0][0])
     self.assertEquals(seqs[0][1], seqs2[0][1])
Exemplo n.º 4
0
 def test_write_sequences_to_fasta_file_empty_seqs(self):
     """Tests ensures that only non-empty sequences will be written to FASTA"""
     seqs = [['seq1', 'TATATA'], ['seq2', '']]
     with open('/tmp/fasta_tmp.fa', 'w') as outputfile:
         st.write_sequences_to_fasta_file(outputfile, seqs)
     seqs2 = st.read_sequences_from_fasta_file('/tmp/fasta_tmp.fa')
     self.assertEquals(1, len(seqs2))
     self.assertEquals(seqs[0][0], seqs2[0][0])
     self.assertEquals(seqs[0][1], seqs2[0][1])
Exemplo n.º 5
0
 def make_sequence_file(self, seqs):
     """Creates a FASTA file from a list of(feature_id, sequence)
     pairs"""
     filename = None
     with tempfile.NamedTemporaryFile(mode='w+', prefix='memeseqs',
                                      delete=False) as outfile:
         filename = outfile.name
         st.write_sequences_to_fasta_file(outfile, seqs)
     return filename
Exemplo n.º 6
0
    def __call__(self, params):
        """call the runner like a function"""
        with tempfile.NamedTemporaryFile(prefix='weeder.fasta',
                                         delete=False) as outfile:
            filename = outfile.name
            logging.debug("Run Weeder on FASTA file: '%s'", filename)
            st.write_sequences_to_fasta_file(outfile, params.seqs.items())

        try:
            dbfile = None
            meme_outfile, pssms = weeder.run_weeder(filename, params, self.config_params,
                                                    self.meme_suite.bgmodel)
            if len(pssms) == 0:
                logging.debug('no PSSMS generated, skipping cluster')
                return meme.MemeRunResult([], {}, [])

            dbfile = self.meme_suite.make_sequence_file(
                [(feature_id, locseq[1])
                 for feature_id, locseq in params.used_seqs.items()])
            logging.debug("# PSSMS created: %d %s", len(pssms), str([i.consensus_motif() for i in pssms]))
            logging.debug("run MAST on '%s', dbfile: '%s'", meme_outfile, dbfile)

            motif_infos = []
            for i in xrange(len(pssms)):
                pssm = pssms[i]
                motif_infos.append(meme.MemeMotifInfo(pssm.values, i + 1,
                                                      pssm.sequence_length(),
                                                      len(pssm.sites),
                                                      None, pssm.e_value,
                                                      pssm.sites))
            mast_out = self.meme_suite.mast(meme_outfile, dbfile,
                                            self.meme_suite.global_background_file())
            if 'keep_mastout' in self.config_params['debug']:
                with open('%s.mast' % meme_outfile, 'w') as outfile:
                    outfile.write(mast_out)
            pe_values, annotations = self.meme_suite.read_mast_output(mast_out,
                                                                      params.seqs.keys())
            return meme.MemeRunResult(pe_values, annotations, motif_infos)
        except Exception as e:
            logging.exception(e)
            return meme.MemeRunResult([], {}, [])
        finally:
            if self.__remove_tempfiles:
                for fileExtension in ['', '.wee', '.mix', '.html', '.meme', '.1.f1', '.1.f2', '.2.f1', '.2.f2']:
                    tmpName = filename + fileExtension
                    if os.path.exists(tmpName):
                        try:
                            os.remove(tmpName)
                        except:
                            logging.warn("could not remove tmp file:'%s'", tmpName)
                try:
                    if dbfile:
                        os.remove(dbfile)
                except:
                    logging.warn("could not remove tmp file:'%s'", dbfile)
Exemplo n.º 7
0
    def __call__(self, params):
        """call the runner like a function"""
        with tempfile.NamedTemporaryFile(prefix='weeder.fasta',
                                         delete=False) as outfile:
            filename = outfile.name
            logging.debug("Run Weeder on FASTA file: '%s'", filename)
            st.write_sequences_to_fasta_file(outfile, params.seqs.items())

        try:
            dbfile = None
            meme_outfile, pssms = weeder.run_weeder(filename, params,
                                                    self.config_params,
                                                    self.meme_suite.bgmodel)
            if len(pssms) == 0:
                logging.debug('no PSSMS generated, skipping cluster')
                return meme.MemeRunResult([], {}, [])

            dbfile = self.meme_suite.make_sequence_file([
                (feature_id, locseq[1])
                for feature_id, locseq in params.used_seqs.items()
            ])
            logging.debug("# PSSMS created: %d %s", len(pssms),
                          str([i.consensus_motif() for i in pssms]))
            logging.debug("run MAST on '%s', dbfile: '%s'", meme_outfile,
                          dbfile)

            motif_infos = []
            for i in xrange(len(pssms)):
                pssm = pssms[i]
                motif_infos.append(
                    meme.MemeMotifInfo(pssm.values, i + 1,
                                       pssm.sequence_length(), len(pssm.sites),
                                       None, pssm.e_value, pssm.sites))
            mast_out = self.meme_suite.mast(
                meme_outfile, dbfile, self.meme_suite.global_background_file())
            if 'keep_mastout' in self.config_params['debug']:
                with open('%s.mast' % meme_outfile, 'w') as outfile:
                    outfile.write(mast_out)
            pe_values, annotations = self.meme_suite.read_mast_output(
                mast_out, params.seqs.keys())
            return meme.MemeRunResult(pe_values, annotations, motif_infos)
        except Exception as e:
            logging.exception(e)
            return meme.MemeRunResult([], {}, [])
        finally:
            if self.__remove_tempfiles:
                for fileExtension in [
                        '', '.wee', '.mix', '.html', '.meme', '.1.f1', '.1.f2',
                        '.2.f1', '.2.f2'
                ]:
                    tmpName = filename + fileExtension
                    if os.path.exists(tmpName):
                        try:
                            os.remove(tmpName)
                        except:
                            logging.warn("could not remove tmp file:'%s'",
                                         tmpName)
                try:
                    if dbfile:
                        os.remove(dbfile)
                except:
                    logging.warn("could not remove tmp file:'%s'", dbfile)