class Test_TreeHandler_Regression_Mesquite(unittest.TestCase):
    """Regression: Test that we can parse MESQUITE taxa blocks"""
    def setUp(self):
        self.nex = NexusReader(
            os.path.join(REGRESSION_DIR, 'mesquite_formatted_branches.trees')
        )

    def test_attributes(self):
        assert len(self.nex.trees.attributes) == 2
        assert self.nex.trees.attributes[0] == \
            """Title 'Trees from "temp.trees"';"""
        assert self.nex.trees.attributes[1] == \
            """LINK Taxa = Untitled_Block_of_Taxa;"""

    def test_found_trees(self):
        assert self.nex.trees.ntrees == 1

    def test_found_taxa(self):
        assert len(self.nex.trees.taxa) == 3
        assert 'A' in self.nex.trees.taxa
        assert 'B' in self.nex.trees.taxa
        assert 'C' in self.nex.trees.taxa

    def test_was_translated(self):
        assert self.nex.trees.was_translated

    def test_translation(self):
        assert self.nex.trees.translators['1'] == 'A'
        assert self.nex.trees.translators['2'] == 'B'
        assert self.nex.trees.translators['3'] == 'C'

    def test_write(self):
        written = self.nex.write()
        assert """Title 'Trees from "temp.trees"';""" in written
        assert """LINK Taxa = Untitled_Block_of_Taxa;""" in written
示例#2
0
class Test_TaxaHandler_Regression_Mesquite(unittest.TestCase):
    """Regression: Test that we can parse MESQUITE taxa blocks"""
    def setUp(self):
        self.nex = NexusReader(os.path.join(REGRESSION_DIR, 'mesquite_taxa_block.nex'))
        
    def test_taxa_block(self):
        for taxon in ['A', 'B', 'C']:
            assert taxon in self.nex.taxa
        # did we get the right number of taxa in the matrix?
        assert self.nex.taxa.ntaxa == len(self.nex.taxa.taxa) == 3
        
    def test_taxa_block_attributes(self):
        assert 'taxa' in self.nex.blocks
        assert len(self.nex.taxa.attributes) == 1
        assert 'TITLE Untitled_Block_of_Taxa;' in self.nex.taxa.attributes
    
    def test_write(self):
        expected_patterns = [
            '^begin taxa;$',
            '^\s+TITLE Untitled_Block_of_Taxa;$',
            '^\s+dimensions ntax=3;$',
            '^\s+taxlabels$',
            "^\s+\[1\] 'A'$",
            "^\s+\[2\] 'B'$",
            "^\s+\[3\] 'C'$",
            '^;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), 'Expected "%s"' % expected
示例#3
0
class Test_TaxaHandler_Regression_Mesquite(unittest.TestCase):
    """Regression: Test that we can parse MESQUITE taxa blocks"""
    def setUp(self):
        self.nex = NexusReader(
            os.path.join(REGRESSION_DIR, 'mesquite_taxa_block.nex'))

    def test_taxa_block(self):
        for taxon in ['A', 'B', 'C']:
            assert taxon in self.nex.taxa
        # did we get the right number of taxa in the matrix?
        assert self.nex.taxa.ntaxa == len(self.nex.taxa.taxa) == 3

    def test_taxa_block_attributes(self):
        assert 'taxa' in self.nex.blocks
        assert len(self.nex.taxa.attributes) == 1
        assert 'TITLE Untitled_Block_of_Taxa;' in self.nex.taxa.attributes

    def test_write(self):
        expected_patterns = [
            '^begin taxa;$',
            '^\s+TITLE Untitled_Block_of_Taxa;$',
            '^\s+dimensions ntax=3;$',
            '^\s+taxlabels$',
            "^\s+\[1\] 'A'$",
            "^\s+\[2\] 'B'$",
            "^\s+\[3\] 'C'$",
            '^;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written,
                             re.MULTILINE), 'Expected "%s"' % expected
示例#4
0
class Test_TreeHandler_Regression_Mesquite(unittest.TestCase):
    """Regression: Test that we can parse MESQUITE taxa blocks"""
    def setUp(self):
        self.nex = NexusReader(
            os.path.join(REGRESSION_DIR, 'mesquite_formatted_branches.trees'))

    def test_attributes(self):
        assert len(self.nex.trees.attributes) == 2
        assert self.nex.trees.attributes[
            0] == """Title 'Trees from "temp.trees"';"""
        assert self.nex.trees.attributes[
            1] == """LINK Taxa = Untitled_Block_of_Taxa;"""

    def test_found_trees(self):
        assert self.nex.trees.ntrees == 1

    def test_found_taxa(self):
        assert len(self.nex.trees.taxa) == 3
        assert 'A' in self.nex.trees.taxa
        assert 'B' in self.nex.trees.taxa
        assert 'C' in self.nex.trees.taxa

    def test_was_translated(self):
        assert self.nex.trees.was_translated == True

    def test_translation(self):
        assert self.nex.trees.translators['1'] == 'A'
        assert self.nex.trees.translators['2'] == 'B'
        assert self.nex.trees.translators['3'] == 'C'

    def test_write(self):
        written = self.nex.write()
        assert """Title 'Trees from "temp.trees"';""" in written
        assert """LINK Taxa = Untitled_Block_of_Taxa;""" in written
class Test_DataHandler_Regression_Mesquite(unittest.TestCase):
    """Regression: Test that we can parse MESQUITE data blocks"""

    def setUp(self):
        self.nex = NexusReader()
        self.nex.read_string("""
        #NEXUS

        Begin data;
        TITLE Untitled_Block_of_Taxa;
        LINK Taxa = Untitled_Block_of_Taxa;
        Dimensions ntax=2 nchar=2;
        Format datatype=standard gap=- symbols="01";
        Matrix
        Harry              00
        Simon              01
            ;
        End;
        """)
    
    def test_attributes(self):
        assert len(self.nex.data.attributes) == 2
        assert self.nex.data.attributes[0] == \
            """TITLE Untitled_Block_of_Taxa;"""
        assert self.nex.data.attributes[1] == \
            """LINK Taxa = Untitled_Block_of_Taxa;"""

    def test_write(self):
        expected_patterns = [
            '^begin data;$',
            '^\s+TITLE Untitled_Block_of_Taxa;$',
            '^\s+LINK Taxa = Untitled_Block_of_Taxa;$',
            '^\s+dimensions ntax=2 nchar=2;$',
            '^\s+format datatype=standard gap=- symbols="01";$',
            "^matrix$",
            "^Harry\s+00",
            "^Simon\s+01$",
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected
示例#6
0
class Test_DataHandler_Regression_Mesquite(unittest.TestCase):
    """Regression: Test that we can parse MESQUITE data blocks"""
    def setUp(self):
        self.nex = NexusReader()
        self.nex.read_string("""
        #NEXUS 
        
        Begin data;
        TITLE something;
        Dimensions ntax=2 nchar=2;
        Format datatype=standard symbols="01" gap=-;
        Matrix
        Harry              00
        Simon              01
            ;
        End;
        """)

    def test_attr_find(self):
        assert len(self.nex.data.attributes) == 1

    def test_write(self):
        expected_patterns = [
            '^begin data;$',
            '^\s+TITLE something;$',
            '^\s+dimensions ntax=2 nchar=2;$',
            '^\s+format datatype=standard symbols="01" gap=-;$',
            "^matrix$",
            "^Harry\s+00",
            "^Simon\s+01$",
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written,
                             re.MULTILINE), 'Expected "%s"' % expected
示例#7
0
__doc__ = """deinterleave - python-nexus tools v%(version)s
Converts an interleaved nexus to a simple nexus.
""" % {'version': VERSION,}

if __name__ == '__main__':
    #set up command-line options
    from optparse import OptionParser
    parser = OptionParser(usage="usage: %prog fudge.nex output.nex")
    options, args = parser.parse_args()
    
    try:
        nexusname = args[0]
    except IndexError:
        print(__doc__)
        print("Author: %s\n" % __author__)
        parser.print_help()
        sys.exit()
    
    try:
        newnexus = args[1]
    except IndexError:
        newnexus = None
        
    nexus = NexusReader(nexusname)
    
    if newnexus is not None:
        nexus.write_to_file(newnexus)
        print("New nexus written to %s" % newnexus)
    else:
        print(nexus.write())
class Test_Manipulation_Data(unittest.TestCase):
    """Test the manipulation of data in the NexusReader"""
    
    def setUp(self):
        self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))
    
    def test_add_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.add_taxon('Elvis', ['1', '2'])
        assert self.nex.data.ntaxa == 5
        assert self.nex.data.matrix['Elvis'] == ['1', '2']
        assert 'Elvis' in self.nex.data.taxa
        assert 'Elvis' in self.nex.data.matrix
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=5 nchar=2;$',
            '^\s+format datatype=standard symbols="01" gap=-;$',
            '^matrix$',
            '^Simon\s+01$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^Elvis\s+12$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), 'Expected "%s"' % expected
    
    def test_delete_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.del_taxon('Simon')
        assert self.nex.data.ntaxa == 3
        
        assert 'Simon' not in self.nex.data.taxa
        assert 'Simon' not in self.nex.data.matrix
        
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=3 nchar=2;$',
            '^\s+format datatype=standard symbols="01" gap=-;$',
            '^matrix$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), 'Expected "%s"' % expected
        
        # should NOT be here
        assert re.search('^Simon\s+01$', written, re.MULTILINE) == None, \
            'Expected Taxon "Simon" to be Deleted'
        
    def test_add_character(self):
        pass
        
    def test_delete_character(self):
        pass

    def test_edit_charlabels(self):
        pass
        

    
# TreeHandler
# self.translators = {}
# self.attributes = []
# self.taxa = []
# self.trees = []
# ntrees
示例#9
0
#!/usr/bin/env python
#coding=utf-8

import ete3
from nexus import NexusReader

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(
        description='reroots and cleans Michael et al.')
    parser.add_argument("input", help='filename')
    parser.add_argument("output", help='filename')
    args = parser.parse_args()

    nex = NexusReader(args.input)

    for i, tree in enumerate(nex.trees.trees):
        # make tree into newick for ete3
        tree = nex.trees.trees[i].split(" = ")[1].strip().lstrip()
        tree = tree.replace("[&U]", "")  # remove unrooted flag if present
        tree = ete3.Tree(tree, format=0)

        # reroot
        tree.set_outgroup('Mawe')
        nex.trees.trees[i] = 'tree tg_%d [&R] = %s' % (i, tree.write(format=5))

    with open(args.output, 'w') as out:
        out.write(nex.write())
        sys.exit("No trees found in found %s!" % nexusname)
    if options.quiet is False:
        print "%d trees found with %d translated taxa" % (nexus.trees.ntrees, len(nexus.trees.translators))

    # Delete trees
    if options.deltree:
        nexus = run_deltree(options.deltree, nexus, options.quiet)

    # Resample trees
    if options.resample:
        nexus = run_resample(options.resample, nexus, options.quiet)

    # Randomly sample trees
    if options.random:
        nexus = run_random(options.random, nexus, options.quiet)

    # remove comments
    if options.removecomments:
        nexus = run_removecomments(nexus, options.quiet)

    # detranslate
    if options.detranslate:
        nexus = run_detranslate(nexus, options.quiet)

    if newnexus is not None:
        nexus.write_to_file(newnexus)
        if options.quiet is False:
            print "New nexus with %d trees written to %s" % (nexus.trees.ntrees, newnexus)
    else:
        print nexus.write()
示例#11
0
 def test_write(self):
     nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.trees'))
     text = open(os.path.join(EXAMPLE_DIR, 'example.trees')).read()
     assert text == nex.write()
class Test_Manipulation_Data(unittest.TestCase):
    """Test the manipulation of data in the NexusReader"""

    def setUp(self):
        self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))

    def test_add_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.add_taxon('Elvis', ['1', '2'])
        assert self.nex.data.ntaxa == 5
        assert self.nex.data.matrix['Elvis'] == ['1', '2']
        assert 'Elvis' in self.nex.data.taxa
        assert 'Elvis' in self.nex.data.matrix
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=5 nchar=2;$',
            '^\s+format datatype=standard gap=- symbols="012";$',
            '^matrix$',
            '^Simon\s+01$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^Elvis\s+12$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected
        
    def test_delete_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.del_taxon('Simon')
        assert self.nex.data.ntaxa == 3

        assert 'Simon' not in self.nex.data.taxa
        assert 'Simon' not in self.nex.data.matrix

        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=3 nchar=2;$',
            '^\s+format datatype=standard gap=- symbols="01";$',
            '^matrix$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected

        # should NOT be here
        assert re.search('^Simon\s+01$', written, re.MULTILINE) is None, \
            'Expected Taxon "Simon" to be Deleted'

    def test_add_character(self):
        assert self.nex.data.nchar == 2
        for taxon in self.nex.data.matrix:
            self.nex.data.matrix[taxon].append('9')
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=4 nchar=3;$',
            '^\s+format datatype=standard gap=- symbols="019";$',
            '^matrix$',
            '^Simon\s+019$',
            '^Louise\s+119$',
            '^Betty\s+109$',
            '^Harry\s+009$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected
            
    def test_delete_character(self):
        assert self.nex.data.nchar == 2
        for taxon in self.nex.data.matrix:
            self.nex.data.matrix[taxon].pop()
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=4 nchar=1;$',
            '^\s+format datatype=standard gap=- symbols="01";$',
            '^matrix$',
            '^Simon\s+0$',
            '^Louise\s+1$',
            '^Betty\s+1$',
            '^Harry\s+0$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected
示例#13
0
class Test_DataHandler_SimpleNexusFormat(unittest.TestCase):
    expected = {
        'Harry': ['0', '0'],
        'Simon': ['0', '1'],
        'Betty': ['1', '0'],
        'Louise': ['1', '1'],
    }

    def setUp(self):
        self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))

    def test_block_find(self):
        assert 'data' in self.nex.blocks
        assert hasattr(self.nex, 'data')
        assert self.nex.data == self.nex.data

    def test_raw(self):
        assert self.nex.data.block == [
            'Begin data;', 'Dimensions ntax=4 nchar=2;',
            'Format datatype=standard symbols="01" gap=-;', 'Matrix',
            'Harry              00', 'Simon              01',
            'Betty              10', 'Louise             11', ';'
        ]

    def test_format_string(self):
        # did we get the expected tokens in the format string?
        expected = {'datatype': 'standard', 'gap': '-', 'symbols': '01'}
        for k, v in expected.items():
            assert self.nex.data.format[k] == v, \
                "%s should equal %s and not %s" % (k, v, self.nex.data.format[k])
        # did we get the right number of tokens?
        assert len(self.nex.data.format) == len(expected)

    def test_taxa(self):
        # did we get the right taxa in the matrix?
        for taxon in self.expected:
            assert taxon in self.nex.data.matrix
        # did we get the right number of taxa in the matrix?
        assert self.nex.data.ntaxa == len(self.expected) == len(
            self.nex.data.taxa)

    def test_characters(self):
        # did we parse the characters properly?
        assert self.nex.data.nchar == 2
        for taxon, expected in self.expected.items():
            assert self.nex.data.matrix[taxon] == expected

    def test_iterable(self):
        for taxon, block in self.nex.data:
            assert block == self.expected[taxon]

    def test_parse_format_line(self):
        d = DataHandler()
        f = d.parse_format_line('Format datatype=standard symbols="01" gap=-;')
        assert f[
            'datatype'] == 'standard', "Expected 'standard', but got '%s'" % f[
                'datatype']
        assert f[
            'symbols'] == '01', "Expected '01', but got '%s'" % f['symbols']
        assert f['gap'] == '-', "Expected 'gap', but got '%s'" % f['gap']

        f = d.parse_format_line(
            'FORMAT datatype=RNA missing=? gap=- symbols="ACGU" labels interleave;'
        )
        assert f['datatype'] == 'rna', "Expected 'rna', but got '%s'" % f[
            'datatype']
        assert f['missing'] == '?', "Expected '?', but got '%s'" % f['missing']
        assert f['gap'] == '-', "Expected '-', but got '%s'" % f['gap']
        assert f['symbols'] == 'acgu', "Expected 'acgu', but got '%s'" % f[
            'symbols']
        assert f[
            'labels'] == True, "Expected <True>, but got '%s'" % f['labels']
        assert f['interleave'] == True, "Expected <True>, but got '%s'" % f[
            'interleave']

    def test_write(self):
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=4 nchar=2;$',
            '^\s+format datatype=standard symbols="01" gap=-;$',
            '^matrix$',
            '^Simon\s+01$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written,
                             re.MULTILINE), 'Expected "%s"' % expected

    def test__load_characters(self):
        for site, data in self.nex.data.characters.items():
            for taxon, value in data.items():
                assert value == self.expected[taxon][site]

    def test_get_site(self):
        for i in (0, 1):
            site_data = self.nex.data.characters[i]
            for taxon, value in site_data.items():
                assert self.expected[taxon][i] == value

    def test_incorrect_dimensions_warnings_ntaxa(self):
        nex = NexusReader()
        with warnings.catch_warnings(record=True) as w:
            nex.read_string("""Begin data;
                Dimensions ntax=5 nchar=1;
                Format datatype=standard symbols="01" gap=-;
                Matrix
                Harry              1
                ;""")
            assert len(w) == 1, 'Expected 1 warning, got %r' % w
            assert issubclass(w[-1].category, UserWarning)
            assert "Expected" in str(w[-1].message)
            assert nex.data.nchar == 1

    def test_incorrect_dimensions_warnings_nchar(self):
        with warnings.catch_warnings(record=True) as w:
            nex = NexusReader()
            nex.read_string("""Begin data;
                Dimensions ntax=1 nchar=5;
                Format datatype=standard symbols="01" gap=-;
                Matrix
                Harry              1
                ;""")
            assert len(w) == 1, 'Expected 1 warning, got %r' % w
            assert issubclass(w[-1].category, UserWarning)
            assert "Expected" in str(w[-1].message)
            assert nex.data.nchar == 1
示例#14
0
 def test_write(self):
     nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.trees'))
     text = open(os.path.join(EXAMPLE_DIR, 'example.trees')).read()
     assert text == nex.write()
示例#15
0
class Test_DataHandler_CharacterBlockNexusFormat(unittest.TestCase):
    def setUp(self):
        self.nex = NexusReader(
            os.path.join(EXAMPLE_DIR, 'example-characters.nex'))

    def test_block_find(self):
        assert 'data' in self.nex.blocks

    def test_charblock_find(self):
        assert hasattr(self.nex.data, 'characters')

    def test_taxa(self):
        assert self.nex.data.ntaxa == 5

    def test_data(self):
        assert self.nex.data.nchar == 5

    def test_charlabels(self):
        assert self.nex.data.charlabels[0] == 'CHAR_A'
        assert self.nex.data.charlabels[1] == 'CHAR_B'
        assert self.nex.data.charlabels[2] == 'CHAR_C'
        assert self.nex.data.charlabels[3] == 'CHAR_D'
        assert self.nex.data.charlabels[4] == 'CHAR_E'

    def test_label_parsing(self):
        assert 'CHAR_A' in self.nex.data.characters
        assert 'CHAR_B' in self.nex.data.characters
        assert 'CHAR_C' in self.nex.data.characters
        assert 'CHAR_D' in self.nex.data.characters
        assert 'CHAR_E' in self.nex.data.characters

    def test_matrix(self):
        for taxon in ("A", "B", "C", "D", "E"):
            for index, expected_value in enumerate(("A", "B", "C", "D", "E")):
                assert self.nex.data.matrix[taxon][index] == expected_value

    def test_characters(self):
        for site in ("A", "B", "C", "D", "E"):
            # All sites in CHAR_A are state "A", and all in CHAR_B and "B" etc
            for t in ("A", "B", "C", "D", "E"):
                assert self.nex.data.characters["CHAR_%s" % site][t] == site

    def test_write(self):
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=5 nchar=5;$',
            '^\s+format gap=- missing=\?;$',
            '^\s+charstatelabels$',
            '^\s+1\s+CHAR_A,$',
            '^\s+2\s+CHAR_B,$',
            '^\s+3\s+CHAR_C,$',
            '^\s+4\s+CHAR_D,$',
            '^\s+5\s+CHAR_E$',
            '^matrix$',
            '^A\s+ABCDE$',
            '^B\s+ABCDE$',
            '^C\s+ABCDE$',
            '^D\s+ABCDE$',
            '^E\s+ABCDE$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected
示例#16
0
        print "%d trees found with %d translated taxa" % \
            (nexus.trees.ntrees, len(nexus.trees.translators))

    # Delete trees
    if options.deltree:
        nexus = run_deltree(options.deltree, nexus, options.quiet)

    # Resample trees
    if options.resample:
        nexus = run_resample(options.resample, nexus, options.quiet)

    # Randomly sample trees
    if options.random:
        nexus = run_random(options.random, nexus, options.quiet)

    # remove comments
    if options.removecomments:
        nexus = run_removecomments(nexus, options.quiet)

    # detranslate
    if options.detranslate:
        nexus = run_detranslate(nexus, options.quiet)

    if newnexus is not None:
        nexus.write_to_file(newnexus)
        if options.quiet is False:
            print "New nexus with %d trees written to %s" % (
                nexus.trees.ntrees, newnexus)
    else:
        print nexus.write()
class Test_Manipulation_Data(unittest.TestCase):
    """Test the manipulation of data in the NexusReader"""
    def setUp(self):
        self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))

    def test_add_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.add_taxon('Elvis', ['1', '2'])
        assert self.nex.data.ntaxa == 5
        assert self.nex.data.matrix['Elvis'] == ['1', '2']
        assert 'Elvis' in self.nex.data.taxa
        assert 'Elvis' in self.nex.data.matrix
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=5 nchar=2;$',
            '^\s+format datatype=standard symbols="01" gap=-;$',
            '^matrix$',
            '^Simon\s+01$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^Elvis\s+12$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written,
                             re.MULTILINE), 'Expected "%s"' % expected

    def test_delete_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.del_taxon('Simon')
        assert self.nex.data.ntaxa == 3

        assert 'Simon' not in self.nex.data.taxa
        assert 'Simon' not in self.nex.data.matrix

        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=3 nchar=2;$',
            '^\s+format datatype=standard symbols="01" gap=-;$',
            '^matrix$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written,
                             re.MULTILINE), 'Expected "%s"' % expected

        # should NOT be here
        assert re.search('^Simon\s+01$', written, re.MULTILINE) == None, \
            'Expected Taxon "Simon" to be Deleted'

    def test_add_character(self):
        pass

    def test_delete_character(self):
        pass

    def test_edit_charlabels(self):
        pass


# TreeHandler
# self.translators = {}
# self.attributes = []
# self.taxa = []
# self.trees = []
# ntrees
示例#18
0
class Test_DataHandler_SimpleNexusFormat(unittest.TestCase):
    expected = {
        'Harry': ['0', '0'],
        'Simon': ['0', '1'],
        'Betty': ['1', '0'],
        'Louise': ['1', '1'],
    }
    def setUp(self):
        self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))
        
    def test_block_find(self):
        assert 'data' in self.nex.blocks
        assert hasattr(self.nex, 'data')
        assert self.nex.data == self.nex.data
        
    def test_raw(self):
        assert self.nex.data.block == [
            'Begin data;', 
            'Dimensions ntax=4 nchar=2;', 
            'Format datatype=standard symbols="01" gap=-;', 
            'Matrix', 
            'Harry              00', 
            'Simon              01', 
            'Betty              10', 
            'Louise             11', 
            ';'
        ]
        
    def test_format_string(self):
        # did we get the expected tokens in the format string?
        expected = {'datatype': 'standard', 'gap': '-', 'symbols': '01'}
        for k, v in expected.items():
            assert self.nex.data.format[k] == v, \
                "%s should equal %s and not %s" % (k, v, self.nex.data.format[k])
        # did we get the right number of tokens?
        assert len(self.nex.data.format) == len(expected)
        
    def test_taxa(self):
        # did we get the right taxa in the matrix?
        for taxon in self.expected:
            assert taxon in self.nex.data.matrix
        # did we get the right number of taxa in the matrix?
        assert self.nex.data.ntaxa == len(self.expected) == len(self.nex.data.taxa)
        
    def test_characters(self):
        # did we parse the characters properly? 
        assert self.nex.data.nchar == 2
        for taxon, expected in self.expected.items():
            assert self.nex.data.matrix[taxon] == expected

    def test_iterable(self):
        for taxon, block in self.nex.data:
            assert block == self.expected[taxon]
            
    def test_parse_format_line(self):
        d = DataHandler()
        f = d.parse_format_line('Format datatype=standard symbols="01" gap=-;')
        assert f['datatype'] == 'standard', "Expected 'standard', but got '%s'" % f['datatype']
        assert f['symbols'] == '01', "Expected '01', but got '%s'" % f['symbols']
        assert f['gap'] == '-', "Expected 'gap', but got '%s'" % f['gap']
        
        f = d.parse_format_line('FORMAT datatype=RNA missing=? gap=- symbols="ACGU" labels interleave;')
        assert f['datatype'] == 'rna', "Expected 'rna', but got '%s'" % f['datatype']
        assert f['missing'] == '?', "Expected '?', but got '%s'" % f['missing']
        assert f['gap'] == '-', "Expected '-', but got '%s'" % f['gap']
        assert f['symbols'] == 'acgu', "Expected 'acgu', but got '%s'" % f['symbols']
        assert f['labels'] == True, "Expected <True>, but got '%s'" % f['labels']
        assert f['interleave'] == True, "Expected <True>, but got '%s'" % f['interleave']
    
    def test_write(self):
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=4 nchar=2;$',
            '^\s+format datatype=standard symbols="01" gap=-;$',
            '^matrix$',
            '^Simon\s+01$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), 'Expected "%s"' % expected

    def test__load_characters(self):
        for site, data in self.nex.data.characters.items():
            for taxon, value in data.items():
                assert value == self.expected[taxon][site]

    def test_get_site(self):
        for i in (0, 1):
            site_data = self.nex.data.characters[i]
            for taxon, value in site_data.items():
                assert self.expected[taxon][i] == value
    
    def test_incorrect_dimensions_warnings_ntaxa(self):
        nex = NexusReader()
        with warnings.catch_warnings(record=True) as w:
            nex.read_string(
                """Begin data;
                Dimensions ntax=5 nchar=1;
                Format datatype=standard symbols="01" gap=-;
                Matrix
                Harry              1
                ;""")
            assert len(w) == 1, 'Expected 1 warning, got %r' % w 
            assert issubclass(w[-1].category, UserWarning)
            assert "Expected" in str(w[-1].message)
            assert nex.data.nchar == 1
        
    def test_incorrect_dimensions_warnings_nchar(self):
        with warnings.catch_warnings(record=True) as w:
            nex = NexusReader()
            nex.read_string(
                """Begin data;
                Dimensions ntax=1 nchar=5;
                Format datatype=standard symbols="01" gap=-;
                Matrix
                Harry              1
                ;""")
            assert len(w) == 1, 'Expected 1 warning, got %r' % w 
            assert issubclass(w[-1].category, UserWarning)
            assert "Expected" in str(w[-1].message)
            assert nex.data.nchar == 1
示例#19
0
        print("%d trees found with %d translated taxa" %
              (nexus.trees.ntrees, len(nexus.trees.translators)))

    # Delete trees
    if options.deltree:
        nexus = run_deltree(options.deltree, nexus, options.quiet)

    # Resample trees
    if options.resample:
        nexus = run_resample(options.resample, nexus, options.quiet)

    # Randomly sample trees
    if options.random:
        nexus = run_random(options.random, nexus, options.quiet)

    # remove comments
    if options.removecomments:
        nexus = run_removecomments(nexus, options.quiet)

    # detranslate
    if options.detranslate:
        nexus = run_detranslate(nexus)

    if newnexus is not None:
        nexus.write_to_file(newnexus)
        if not options.quiet:
            print("New nexus with %d trees written to %s" %
                  (nexus.trees.ntrees, newnexus))
    else:
        print(nexus.write())
class Test_Manipulation_Data(unittest.TestCase):
    """Test the manipulation of data in the NexusReader"""

    def setUp(self):
        self.nex = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))

    def test_add_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.add_taxon('Elvis', ['1', '2'])
        assert self.nex.data.ntaxa == 5
        assert self.nex.data.matrix['Elvis'] == ['1', '2']
        assert 'Elvis' in self.nex.data.taxa
        assert 'Elvis' in self.nex.data.matrix
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=5 nchar=2;$',
            '^\s+format datatype=standard gap=- symbols="012";$',
            '^matrix$',
            '^Simon\s+01$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^Elvis\s+12$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected
        
    def test_delete_taxa(self):
        assert self.nex.data.ntaxa == 4
        self.nex.data.del_taxon('Simon')
        assert self.nex.data.ntaxa == 3

        assert 'Simon' not in self.nex.data.taxa
        assert 'Simon' not in self.nex.data.matrix

        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=3 nchar=2;$',
            '^\s+format datatype=standard gap=- symbols="01";$',
            '^matrix$',
            '^Louise\s+11$',
            '^Betty\s+10$',
            '^Harry\s+00$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected

        # should NOT be here
        assert re.search('^Simon\s+01$', written, re.MULTILINE) is None, \
            'Expected Taxon "Simon" to be Deleted'

    def test_add_character(self):
        assert self.nex.data.nchar == 2
        for taxon in self.nex.data.matrix:
            self.nex.data.matrix[taxon].append('9')
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=4 nchar=3;$',
            '^\s+format datatype=standard gap=- symbols="019";$',
            '^matrix$',
            '^Simon\s+019$',
            '^Louise\s+119$',
            '^Betty\s+109$',
            '^Harry\s+009$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected
            
    def test_delete_character(self):
        assert self.nex.data.nchar == 2
        for taxon in self.nex.data.matrix:
            self.nex.data.matrix[taxon].pop()
        expected_patterns = [
            '^begin data;$',
            '^\s+dimensions ntax=4 nchar=1;$',
            '^\s+format datatype=standard gap=- symbols="01";$',
            '^matrix$',
            '^Simon\s+0$',
            '^Louise\s+1$',
            '^Betty\s+1$',
            '^Harry\s+0$',
            '^\s+;$',
            '^end;$',
        ]
        written = self.nex.write()
        for expected in expected_patterns:
            assert re.search(expected, written, re.MULTILINE), \
                'Expected "%s"' % expected