def _presort_by_abundance(self, seq_path): """ Preform pre-sorting of input by abundance """ # Turn off uclust's sorting self.Params["suppress_sort"] = True # Get a temp file name for the sorted fasta file sorted_input_seqs_filepath = get_tmp_filename(prefix=self.Name, suffix=".fasta") # Sort input seqs by abundance, and write to the temp # file sort_fasta_by_abundance(open(seq_path, "U"), open(sorted_input_seqs_filepath, "w")) # Return the sorted sequences filepath return sorted_input_seqs_filepath
def test_sort_fasta_by_abundance(self): """sort_fasta_by_abundance functions as expected""" class FakeOutF(object): def __init__(self): self.s = "" def write(self,s): self.s += s actual = FakeOutF() expected = "" sort_fasta_by_abundance([],actual) self.assertEqual(actual.s,expected) # no sorting necessary actual = FakeOutF() expected1 = "\n".join(['>s1','ACCGT','>s2 comment','ATTGC','']) expected2 = "\n".join(['>s2 comment','ATTGC','>s1','ACCGT','']) sort_fasta_by_abundance(['>s1','ACCGT','>s2 comment','ATTGC'],actual) # order is unimportant here self.assertTrue(actual.s == expected1 or actual.s == expected2) # sorting necessary actual = FakeOutF() inseqs = ['>s1','ACCGT', '>s2 comment','ATTGC', '>s3 blah','ATTGC'] expected = "\n".join(['>s2 comment','ATTGC', '>s3 blah','ATTGC', '>s1','ACCGT','']) sort_fasta_by_abundance(inseqs,actual) self.assertEqual(actual.s,expected)