Esempio n. 1
0
def combine_nexuses(nexuslist):
    """
    Combines a list of NexusReader instances into a single nexus

    :param nexuslist: A list of NexusReader instances
    :type nexuslist: List

    :return: A NexusWriter instance

    :raises TypeError: if nexuslist is not a list of NexusReader instances
    :raises IOError: if unable to read an file in nexuslist
    :raises NexusFormatException: if a nexus file does not have a `data` block
    """
    out = NexusWriter()
    charpos = 0
    for nex_id, nex in enumerate(nexuslist, 1):
        check_for_valid_NexusReader(nex, required_blocks=['data'])

        if hasattr(nex, 'short_filename'):
            nexus_label = os.path.splitext(nex.short_filename)[0]
        else:
            nexus_label = str(nex_id)

        out.add_comment("%d - %d: %s" %
                        (charpos, charpos + nex.data.nchar - 1, nexus_label))

        for site_idx, site in enumerate(sorted(nex.data.characters), 0):
            data = nex.data.characters.get(site)
            charpos += 1
            # work out character label
            charlabel = nex.data.charlabels.get(site_idx, site_idx + 1)
            label = '%s.%s' % (nexus_label, charlabel)

            for taxon, value in data.items():
                out.add(taxon, label, value)
    return out
Esempio n. 2
0
def test_invalid():
    w = NexusWriter()
    with pytest.raises(ValueError):
        w.write()
Esempio n. 3
0
def test_write_with_no_data_but_trees():
    nex = NexusWriter()
    nex.trees.append('tree tree1 = (French,(English,Latin));')
    content = nex.write()
    assert re.search(r"BEGIN TREES", content)
    assert re.search(r"tree tree1 = \(French,\(English,Latin\)\);", content)
Esempio n. 4
0
 def setUp(self):
     self.nex = NexusWriter()
     for char in data:
         for taxon, value in data[char].items():
             self.nex.add(taxon, char, value)