def __init__(self): GagCmdBase.__init__(self) self.prompt = "GAG> " self.controller = ConsoleController()
class GagCmd(GagCmdBase): ## Setup, loading and saving sessions, exit, etc. intro = " \n ,, \n ~++=:, =OMNZ: :=?77I=, \n :7DMMMMMMD7: ~DMMMMZ =$DMMMMMMMM8+ \n :7NMMMMMMMMMMN7, $MMMMMN= ~ZMMMMMMMMMMMMZ, \n ?NMMMNZ+~+IONMMN? INMMNMMMI INMMMDZ?~,,:+I?: \n +NMMN7: :?$I: INMMNZMMMZ :$MMMD? \n :8MMM? 7MMMD+=NMM8, :OMMMO~ \n 7MMMZ, 7MMMD= ,8MMM? ZMMMZ, \n :8MMN+ ,$MMMN+ IMMMO ~NMMD~ ,+$ZI+==~, \n ~DMMD~ ~7OZ$Z8NNZ: +DMMMD$I?~:~DMMN+ +NMM8 IMMMMMMMMO: \n $MMM8: ,OMMMMMMMN= INMMN$$MMMMMMMMMMZ ~DMMMI ~8MMMMMMMZ: \n ,ZMMMD?, ?NMMMNZ~ INMMD+ =ONMMMMMMMMN~ +DMMM$: ,=$MMMD: \n ,$MMMMM87+IONMMMI ?NMMN= :=+??OMMM$ =8MMMM87=, ,+8MMMZ \n +8MMMMMMMMMMMO~ ~D0GE? =NMM$ :$NMMMMMMNMMMMMO: \n :I8NMMMMNO+, :OMD? +Z7~ :I8NMMMMMMMDI, \n :~~: ,,, ~?7$7?~ \n \n "+\ "\nWelcome to the GAG console!\n"+\ "Type 'help' for available commands.\n" no_genome_message = "\nIt looks like no genome is currently loaded. Try the 'load' command.\n"+\ "Type 'help load' to learn how to use it, or just 'help' for general advice.\n" def __init__(self): GagCmdBase.__init__(self) self.prompt = "GAG> " self.controller = ConsoleController() def precmd(self, line): line = GagCmdBase.precmd(self, line) print('----------------------------------------') return line def postcmd(self, stop, line): stop = GagCmdBase.postcmd(self, stop, line) print('----------------------------------------') return stop def help_load(self): print( "\nThis command takes you the GAG LOAD menu. There you can specify the location of" ) print("your files and load them into memory.") print( "Alternately, just type 'load <path>' and avoid the submenu altogether.\n" ) def do_load(self, line): path_to_load = line.strip() loadcmd = LoadCmd(self.prompt, self.controller, path_to_load) loadcmd.cmdloop() def help_flag(self): print( "\nThis command takes you to the GAG FLAG menu. There you can flag features based" ) print( "on certain criteria to mark suspicious data. Alternately, just type:\n" ) print("'remove <criteria> <value>'\n") print("if you've done this before :)\n") def do_flag(self, line): if self.controller.genome_is_loaded(): filtercmd = FlagCmd(self.prompt, self.controller, line) filtercmd.cmdloop() else: print(self.no_genome_message) def help_remove(self): print( "\nThis command takes you to the GAG REMOVE menu. There you can remove features based" ) print( "on certain criteria to filter out bad data. Alternately, just type:\n" ) print("'remove <criteria> <value>'\n") print("if you've done this before :)\n") def do_remove(self, line): if self.controller.genome_is_loaded(): filtercmd = RemoveCmd(self.prompt, self.controller, line) filtercmd.cmdloop() else: print(self.no_genome_message) def help_fix(self): print( "\nThis command takes you to the GAG FIX menu. There you can apply fixes to the genome," ) print( "to resolve issues such as terminal Ns, invalid start and stop codons, etc." ) print( "Fixes are applied when you type the 'info' command or when you write" ) print("the genome to a file.") print( "Alternately, just type 'fix <name_of_fix> if you've done this before :)\n" ) def do_fix(self, line): if self.controller.genome_is_loaded(): name_of_fix = line.strip() fixcmd = FixCmd(self.prompt, self.controller, name_of_fix) fixcmd.cmdloop() else: print(self.no_genome_message) def help_write(self): print("\nThis command takes you to the GAG WRITE menu.") print("There you can write your genome to a folder.\n") def do_write(self, line): if self.controller.genome_is_loaded(): writecmd = WriteCmd(self.prompt, self.controller, line) # Pass args to next console for parsing writecmd.cmdloop() else: print(self.no_genome_message) def help_view(self): print("\nThis command takes you to the GAG VIEW menu.") print("There you can view cds, gene or sequence features on screen.\n") def do_view(self, line): if self.controller.genome_is_loaded(): viewcmd = ViewCmd(self.prompt, self.controller, line) viewcmd.cmdloop() else: print(self.no_genome_message) def help_exit(self): print("\nExit this console.\n") def do_exit(self, line): return True def help_info(self): print("\nPrints summary statistics about original genome (from file)" +\ " and modified genome (filters and fixes applied).") print("May take a moment to run.\n") def do_info(self, line): if self.controller.genome_is_loaded(): print(try_catch(self.controller.stats, None)) else: print(self.no_genome_message)
class GagCmd(GagCmdBase): ## Setup, loading and saving sessions, exit, etc. intro = " \n ,, \n ~++=:, =OMNZ: :=?77I=, \n :7DMMMMMMD7: ~DMMMMZ =$DMMMMMMMM8+ \n :7NMMMMMMMMMMN7, $MMMMMN= ~ZMMMMMMMMMMMMZ, \n ?NMMMNZ+~+IONMMN? INMMNMMMI INMMMDZ?~,,:+I?: \n +NMMN7: :?$I: INMMNZMMMZ :$MMMD? \n :8MMM? 7MMMD+=NMM8, :OMMMO~ \n 7MMMZ, 7MMMD= ,8MMM? ZMMMZ, \n :8MMN+ ,$MMMN+ IMMMO ~NMMD~ ,+$ZI+==~, \n ~DMMD~ ~7OZ$Z8NNZ: +DMMMD$I?~:~DMMN+ +NMM8 IMMMMMMMMO: \n $MMM8: ,OMMMMMMMN= INMMN$$MMMMMMMMMMZ ~DMMMI ~8MMMMMMMZ: \n ,ZMMMD?, ?NMMMNZ~ INMMD+ =ONMMMMMMMMN~ +DMMM$: ,=$MMMD: \n ,$MMMMM87+IONMMMI ?NMMN= :=+??OMMM$ =8MMMM87=, ,+8MMMZ \n +8MMMMMMMMMMMO~ ~D0GE? =NMM$ :$NMMMMMMNMMMMMO: \n :I8NMMMMNO+, :OMD? +Z7~ :I8NMMMMMMMDI, \n :~~: ,,, ~?7$7?~ \n \n "+\ "\nWelcome to the GAG console!\n"+\ "Type 'help' for available commands.\n" no_genome_message = "\nIt looks like no genome is currently loaded. Try the 'load' command.\n"+\ "Type 'help load' to learn how to use it, or just 'help' for general advice.\n" def __init__(self): GagCmdBase.__init__(self) self.prompt = "GAG> " self.controller = ConsoleController() def precmd(self, line): line = GagCmdBase.precmd(self, line) print('----------------------------------------') return line def postcmd(self, stop, line): stop = GagCmdBase.postcmd(self, stop, line) print('----------------------------------------') return stop def help_load(self): print("\nThis command takes you the GAG LOAD menu. There you can specify the location of") print("your files and load them into memory.") print("Alternately, just type 'load <path>' and avoid the submenu altogether.\n") def do_load(self, line): path_to_load = line.strip() loadcmd = LoadCmd(self.prompt, self.controller, path_to_load) loadcmd.cmdloop() def help_flag(self): print("\nThis command takes you to the GAG FLAG menu. There you can flag features based") print("on certain criteria to mark suspicious data. Alternately, just type:\n") print("'remove <criteria> <value>'\n") print("if you've done this before :)\n") def do_flag(self, line): if self.controller.genome_is_loaded(): filtercmd = FlagCmd(self.prompt, self.controller, line) filtercmd.cmdloop() else: print(self.no_genome_message) def help_remove(self): print("\nThis command takes you to the GAG REMOVE menu. There you can remove features based") print("on certain criteria to filter out bad data. Alternately, just type:\n") print("'remove <criteria> <value>'\n") print("if you've done this before :)\n") def do_remove(self, line): if self.controller.genome_is_loaded(): filtercmd = RemoveCmd(self.prompt, self.controller, line) filtercmd.cmdloop() else: print(self.no_genome_message) def help_fix(self): print("\nThis command takes you to the GAG FIX menu. There you can apply fixes to the genome,") print("to resolve issues such as terminal Ns, invalid start and stop codons, etc.") print("Fixes are applied when you type the 'info' command or when you write") print("the genome to a file.") print("Alternately, just type 'fix <name_of_fix> if you've done this before :)\n") def do_fix(self, line): if self.controller.genome_is_loaded(): name_of_fix = line.strip() fixcmd = FixCmd(self.prompt, self.controller, name_of_fix) fixcmd.cmdloop() else: print(self.no_genome_message) def help_write(self): print("\nThis command takes you to the GAG WRITE menu.") print("There you can write your genome to a folder.\n") def do_write(self, line): if self.controller.genome_is_loaded(): writecmd = WriteCmd(self.prompt, self.controller, line) # Pass args to next console for parsing writecmd.cmdloop() else: print(self.no_genome_message) def help_view(self): print("\nThis command takes you to the GAG VIEW menu.") print("There you can view cds, gene or sequence features on screen.\n") def do_view(self, line): if self.controller.genome_is_loaded(): viewcmd = ViewCmd(self.prompt, self.controller, line) viewcmd.cmdloop() else: print(self.no_genome_message) def help_exit(self): print("\nExit this console.\n") def do_exit(self, line): return True def help_info(self): print("\nPrints summary statistics about original genome (from file)" +\ " and modified genome (filters and fixes applied).") print("May take a moment to run.\n") def do_info(self, line): if self.controller.genome_is_loaded(): print(try_catch(self.controller.stats, None)) else: print(self.no_genome_message)
def setUp(self): self.ctrlr = ConsoleController()
class TestConsoleController(unittest.TestCase): def setUp(self): self.ctrlr = ConsoleController() def test_constructor(self): self.assertEqual("ConsoleController", self.ctrlr.__class__.__name__) def test_format_list_with_strings(self): mylist = ["one", "two", "three"] expected = "one, two, three\n" self.assertEquals(format_list_with_strings(mylist), expected) def test_format_list_with_strings_one_entry(self): self.assertEquals(format_list_with_strings(["foo"]), "foo\n") def test_format_list_with_strings_two_entries(self): self.assertEquals(format_list_with_strings(["foo", "bar"]), "foo, bar\n") def setup_seqs(self): self.ctrlr.seqs.append(Sequence("seq1", "GATTACA")) self.ctrlr.seqs.append(Sequence("seq2", "ATTAC")) self.ctrlr.seqs.append(Sequence("seq3", "ACGTACGT")) def setup_seqs_and_genes(self): self.setup_seqs() gene1 = Mock() gene1.seq_name = "seq2" gene1.identifier = "gene1" self.ctrlr.add_gene(gene1) gene2 = Mock() gene2.seq_name = "seq2" gene2.identifier = "gene2" self.ctrlr.add_gene(gene2) gene3 = Mock() gene3.seq_name = "seq3" gene3.identifier = "gene3" self.ctrlr.add_gene(gene3) def test_clear_seqs(self): self.setup_seqs() self.assertEquals(3, len(self.ctrlr.seqs)) self.ctrlr.clear_seqs() self.assertEquals(0, len(self.ctrlr.seqs)) def test_contains_mrna(self): mockseq = Mock() mockseq.contains_mrna.return_value = True self.ctrlr.seqs = [mockseq] self.assertTrue(self.ctrlr.contains_mrna("foo_mrna")) mockseq.contains_mrna.assert_called_with("foo_mrna") def test_contains_gene(self): mockseq = Mock() mockseq.contains_gene.return_value = True self.ctrlr.seqs = [mockseq] self.assertTrue(self.ctrlr.contains_gene("foo_gene")) mockseq.contains_gene.assert_called_with("foo_gene") def test_contains_seq(self): self.setup_seqs() self.assertTrue(self.ctrlr.contains_seq("seq1")) self.assertFalse(self.ctrlr.contains_seq("foo_seq")) def test_get_n_seq_ids(self): self.setup_seqs() expected = "First 3 seq ids are: seq1, seq2, seq3\n" self.assertEquals(self.ctrlr.get_n_seq_ids(3), expected) def test_get_n_seq_ids_when_n_is_larger_than_num_seqs(self): self.setup_seqs() expected = "First 3 seq ids are: seq1, seq2, seq3\n" self.assertEquals(self.ctrlr.get_n_seq_ids(8), expected) def test_get_n_seq_ids_when_no_seqs(self): expected = "No sequences currently in memory.\n" self.assertEquals(self.ctrlr.get_n_seq_ids(8), expected) def test_add_gene(self): self.setup_seqs() gene1 = Mock() gene1.seq_name = "seq1" self.assertEquals(0, len(self.ctrlr.seqs[0].genes)) self.ctrlr.add_gene(gene1) self.assertEquals(1, len(self.ctrlr.seqs[0].genes)) def test_get_n_gene_ids(self): self.ctrlr.seqs = [Mock(), Mock(), Mock()] self.ctrlr.seqs[0].get_gene_ids.return_value = ["gene1a", "gene1b", "gene1c"] self.ctrlr.seqs[1].get_gene_ids.return_value = ["gene2a", "gene2b", "gene2c"] self.ctrlr.seqs[2].get_gene_ids.return_value = ["gene3a", "gene3b", "gene3c"] expected = "First 4 gene ids are: gene1a, gene1b, gene1c, gene2a\n" self.assertEquals(self.ctrlr.get_n_gene_ids(4), expected) def test_get_n_mrna_ids(self): self.ctrlr.seqs = [Mock(), Mock(), Mock()] self.ctrlr.seqs[0].get_mrna_ids.return_value = ["mrna1a", "mrna1b", "mrna1c"] self.ctrlr.seqs[1].get_mrna_ids.return_value = ["mrna2a", "mrna2b", "mrna2c"] self.ctrlr.seqs[2].get_mrna_ids.return_value = ["mrna3a", "mrna3b", "mrna3c"] expected = "First 7 mrna ids are: mrna1a, mrna1b, mrna1c, mrna2a, mrna2b, mrna2c, mrna3a\n" self.assertEquals(self.ctrlr.get_n_mrna_ids(7), expected) def test_read_fasta(self): self.assertFalse(self.ctrlr.seqs) self.ctrlr.read_fasta("walkthrough/basic/genome.fasta") self.assertTrue(self.ctrlr.seqs) def test_read_gff(self): self.ctrlr.read_fasta("walkthrough/basic/genome.fasta") self.assertFalse(self.ctrlr.seqs[0].genes) self.ctrlr.read_gff("walkthrough/basic/genome.gff") self.assertTrue(self.ctrlr.seqs[0].genes) def test_barfseq_no_args(self): pass line = "" # Shouldn't throw error self.assertTrue(self.ctrlr.barf_seq(line)) def test_barfseq(self): self.setup_seqs() result = self.ctrlr.barf_seq("seq1 1 3") self.assertEquals("GAT", result) def test_can_write_to_path(self): self.assertFalse(self.ctrlr.can_write_to_path("src/")) self.assertFalse(self.ctrlr.can_write_to_path("gag.py")) self.assertTrue(self.ctrlr.can_write_to_path("no_such_directory/no_such_subdirectory/no_such_file.txt"))
class TestConsoleController(unittest.TestCase): def setUp(self): self.ctrlr = ConsoleController() def test_constructor(self): self.assertEqual('ConsoleController', self.ctrlr.__class__.__name__) def test_format_list_with_strings(self): mylist = ["one", "two", "three"] expected = "one, two, three\n" self.assertEquals(format_list_with_strings(mylist), expected) def test_format_list_with_strings_one_entry(self): self.assertEquals(format_list_with_strings(["foo"]), "foo\n") def test_format_list_with_strings_two_entries(self): self.assertEquals(format_list_with_strings(["foo", "bar"]), "foo, bar\n") def setup_seqs(self): self.ctrlr.seqs.append(Sequence("seq1", "GATTACA")) self.ctrlr.seqs.append(Sequence("seq2", "ATTAC")) self.ctrlr.seqs.append(Sequence("seq3", "ACGTACGT")) def setup_seqs_and_genes(self): self.setup_seqs() gene1 = Mock() gene1.seq_name = "seq2" gene1.identifier = "gene1" self.ctrlr.add_gene(gene1) gene2 = Mock() gene2.seq_name = "seq2" gene2.identifier = "gene2" self.ctrlr.add_gene(gene2) gene3 = Mock() gene3.seq_name = "seq3" gene3.identifier = "gene3" self.ctrlr.add_gene(gene3) def test_clear_seqs(self): self.setup_seqs() self.assertEquals(3, len(self.ctrlr.seqs)) self.ctrlr.clear_seqs() self.assertEquals(0, len(self.ctrlr.seqs)) def test_contains_mrna(self): mockseq = Mock() mockseq.contains_mrna.return_value = True self.ctrlr.seqs = [mockseq] self.assertTrue(self.ctrlr.contains_mrna("foo_mrna")) mockseq.contains_mrna.assert_called_with("foo_mrna") def test_contains_gene(self): mockseq = Mock() mockseq.contains_gene.return_value = True self.ctrlr.seqs = [mockseq] self.assertTrue(self.ctrlr.contains_gene("foo_gene")) mockseq.contains_gene.assert_called_with("foo_gene") def test_contains_seq(self): self.setup_seqs() self.assertTrue(self.ctrlr.contains_seq("seq1")) self.assertFalse(self.ctrlr.contains_seq("foo_seq")) def test_get_n_seq_ids(self): self.setup_seqs() expected = "First 3 seq ids are: seq1, seq2, seq3\n" self.assertEquals(self.ctrlr.get_n_seq_ids(3), expected) def test_get_n_seq_ids_when_n_is_larger_than_num_seqs(self): self.setup_seqs() expected = "First 3 seq ids are: seq1, seq2, seq3\n" self.assertEquals(self.ctrlr.get_n_seq_ids(8), expected) def test_get_n_seq_ids_when_no_seqs(self): expected = "No sequences currently in memory.\n" self.assertEquals(self.ctrlr.get_n_seq_ids(8), expected) def test_add_gene(self): self.setup_seqs() gene1 = Mock() gene1.seq_name = "seq1" self.assertEquals(0, len(self.ctrlr.seqs[0].genes)) self.ctrlr.add_gene(gene1) self.assertEquals(1, len(self.ctrlr.seqs[0].genes)) def test_get_n_gene_ids(self): self.ctrlr.seqs = [Mock(), Mock(), Mock()] self.ctrlr.seqs[0].get_gene_ids.return_value = ["gene1a", "gene1b", "gene1c"] self.ctrlr.seqs[1].get_gene_ids.return_value = ["gene2a", "gene2b", "gene2c"] self.ctrlr.seqs[2].get_gene_ids.return_value = ["gene3a", "gene3b", "gene3c"] expected = "First 4 gene ids are: gene1a, gene1b, gene1c, gene2a\n" self.assertEquals(self.ctrlr.get_n_gene_ids(4), expected) def test_get_n_mrna_ids(self): self.ctrlr.seqs = [Mock(), Mock(), Mock()] self.ctrlr.seqs[0].get_mrna_ids.return_value = ["mrna1a", "mrna1b", "mrna1c"] self.ctrlr.seqs[1].get_mrna_ids.return_value = ["mrna2a", "mrna2b", "mrna2c"] self.ctrlr.seqs[2].get_mrna_ids.return_value = ["mrna3a", "mrna3b", "mrna3c"] expected = "First 7 mrna ids are: mrna1a, mrna1b, mrna1c, mrna2a, mrna2b, mrna2c, mrna3a\n" self.assertEquals(self.ctrlr.get_n_mrna_ids(7), expected) def test_read_fasta(self): self.assertFalse(self.ctrlr.seqs) self.ctrlr.read_fasta("walkthrough/basic/genome.fasta") self.assertTrue(self.ctrlr.seqs) def test_read_gff(self): self.ctrlr.read_fasta("walkthrough/basic/genome.fasta") self.assertFalse(self.ctrlr.seqs[0].genes) self.ctrlr.read_gff("walkthrough/basic/genome.gff") self.assertTrue(self.ctrlr.seqs[0].genes) def test_barfseq_no_args(self): pass line = "" # Shouldn't throw error self.assertTrue(self.ctrlr.barf_seq(line)) def test_barfseq(self): self.setup_seqs() result = self.ctrlr.barf_seq("seq1 1 3") self.assertEquals("GAT", result) def test_can_write_to_path(self): self.assertFalse(self.ctrlr.can_write_to_path("src/")) self.assertFalse(self.ctrlr.can_write_to_path("gag.py")) self.assertTrue(self.ctrlr.can_write_to_path("no_such_directory/no_such_subdirectory/no_such_file.txt"))