def test_compile_mothur_script(self): """Mothur._compile_mothur_script() should return valid Mothur script""" app = Mothur() app._input_filename = 'test.fasta' observed_script = app._compile_mothur_script() expected_script = ( '#unique.seqs(fasta=test.fasta); ' 'dist.seqs(fasta=test.unique.fasta); ' 'read.dist(column=test.unique.dist, name=test.names); ' 'cluster(method=furthest)') self.assertEqual(observed_script, expected_script)
def test_call_with_multiline_string(self): """Mothur.__call__() should return correct otu's for input as single string""" app = Mothur() result = app(self.small_fasta) observed_otus = result['otu list'].read() self.assertEquals(observed_otus, self.small_otus) result.cleanUp()
def test_call_with_complement(self): """Mothur.__call__() should return correct otu's for input sequences which are reverse complements""" app = Mothur() result = app(self.complement_fasta) observed_otus = result['otu list'].read() self.assertEquals(observed_otus, self.complement_otus) result.cleanUp()
def test_get_help(self): """Mothur.getHelp() should return help string""" expected_help = ( 'See manual, available on the MOTHUR wiki:\n' 'http://schloss.micro.umass.edu/mothur/' ) self.assertEqual(Mothur.getHelp(), expected_help)
def test_call_with_lines(self): """Mothur.__call__() should return correct otu's for input as lines""" lines = self.small_fasta.split('\n') app = Mothur(InputHandler='_input_as_lines') result = app(lines) observed_otus = result['otu list'].read() self.assertEquals(observed_otus, self.small_otus) result.cleanUp()
def test_call_with_working_dir(self): """Mothur.__call__() should return correct otu's when input dir is changed""" working_dir = mkdtemp() app = Mothur(WorkingDir=working_dir) result = app(self.small_fasta) observed_otus = result['otu list'].read() self.assertEquals(observed_otus, self.small_otus) result.cleanUp() rmdir(working_dir)
def test_call_with_path(self): """Mothur.__call__() should return correct otu's for input as path""" working_dir = mkdtemp() _, filename = mkstemp(dir=working_dir, suffix='.fasta') with open(filename, 'w') as f: f.write(self.small_fasta) app = Mothur(InputHandler='_input_as_path', WorkingDir=working_dir) result = app(filename) observed_otus = result['otu list'].read() self.assertEquals(observed_otus, self.small_otus) remove(filename) result.cleanUp() rmdir(working_dir)
def test_get_result_paths(self): """Mothur._get_result_paths() should guess correct output paths""" app = Mothur() app._input_filename = 'test.fasta' observed_paths = { 'distance matrix': app._derive_dist_path(), 'otu list': app._derive_list_path(), 'rank abundance': app._derive_rank_abundance_path(), 'species abundance': app._derive_species_abundance_path(), 'unique names': app._derive_names_path(), 'unique seqs': app._derive_unique_path(), } expected_paths = { 'distance matrix': 'test.unique.dist', 'otu list': 'test.unique.fn.list', 'rank abundance': 'test.unique.fn.rabund', 'species abundance': 'test.unique.fn.sabund', 'unique names': 'test.names', 'unique seqs': 'test.unique.fasta', } self.assertEqual(observed_paths, expected_paths)
def __call__(self, seq_path, result_path=None, log_path=None): """Returns dict mapping {otu_id:[seq_ids]} for each otu. Parameters: seq_path: path to file of sequences result_path: path to file of results. If specified, should dump the result to the desired path instead of returning it. log_path: path to log, which should include dump of params. """ app = Mothur(InputHandler='_input_as_path') app.Parameters['method'].on(self.Params['Algorithm']) results = app(seq_path) parsed_otus = mothur_parse(results['otu list']) clusters = self.__pick_clusters(parsed_otus) results.cleanUp() # From here down, this is all copied straight from # CdHitOtuPicker, and prime for refactoring into a private # method of OtuPicker if result_path: # if the user provided a result_path, write the # results to file with one tab-separated line per # cluster of = open(result_path, 'w') for i, cluster in enumerate(clusters): of.write('%s\t%s\n' % (i, '\t'.join(cluster))) of.close() result = None log_str = 'Result path: %s' % result_path else: # if the user did not provide a result_path, store # the clusters in a dict of {otu_id:[seq_ids]}, where # otu_id is arbitrary result = dict(enumerate(clusters)) log_str = 'Result path: None, returned as dict.' if log_path: # if the user provided a log file path, log the run log_file = open(log_path, 'w') log_file.write(str(self)) log_file.write('\n') log_file.write('%s\n' % log_str) # return the result (note this is None if the data was # written to file) return result
def test_working_directory(self): """Mothur.WorkingDir attribute should not be cast to FilePath object""" app = Mothur(WorkingDir='/tmp') self.assertEquals(str(app.WorkingDir), '/tmp')
def test_get_help(self): """Mothur.getHelp() should return help string""" expected_help = ('See manual, available on the MOTHUR wiki:\n' 'http://schloss.micro.umass.edu/mothur/') self.assertEqual(Mothur.getHelp(), expected_help)