def test_read_error_on_malformed(self): outfile = os.path.join(self.tmpdir, 'read-error.txt') with open(outfile, 'w') as handle: handle.write('Aasasd\n') t = TreeMaker() with self.assertRaises(ValueError): t.read(outfile)
def test_read_skips_empty_lines(self): outfile = os.path.join(self.tmpdir, 'read-empty.txt') with open(outfile, 'w') as handle: handle.write('A a\n') handle.write('\n') handle.write('\n') handle.write('B b\n') t = TreeMaker() t.read(outfile) assert str(t.tree) == '(A,B)'
def test_read_with_tabs(self): outfile = os.path.join(self.tmpdir, 'read.txt') with open(outfile, 'w') as handle: handle.write('A\ta\n') handle.write('AB1\ta, b\n') handle.write('AB2\ta, b\n') handle.write('C\tc\n') t = TreeMaker() t.read(outfile) assert str(self.t.tree) == str(t.tree)
def test_add_from(self): taxa = [ ('A1', 'family a, subgroup 1'), ('A2', 'family a, subgroup 2'), ('B1a', 'family b, subgroup 1'), ('B1b', 'family b, subgroup 1'), ('B2', 'family b, subgroup 2'), ] t = TreeMaker() t.add_from(taxa) assert str(t.tree) == "((A1,A2),((B1a,B1b),B2))"
def test_add(self): t = TreeMaker() t.add('A', 'a') t.add('AB1', 'a, b') t.add('AB2', 'a, b') t.add('C', 'c') assert str(t.tree) == "((A,(AB1,AB2)),C)"
def test_error_on_bad_taxon(self): t = TreeMaker() with self.assertRaises(ValueError): t.add('A (x)', 'a') with self.assertRaises(ValueError): t.add('A)', 'a') with self.assertRaises(ValueError): t.add('A(', 'a')
def test_add_2(self): t = TreeMaker() t.add('A1', 'family a, subgroup 1') t.add('A2', 'family a, subgroup 2') t.add('B1a', 'family b, subgroup 1') t.add('B1b', 'family b, subgroup 1') t.add('B2', 'family b, subgroup 2') assert str(t.tree) == "((A1,A2),((B1a,B1b),B2))"
def make_tree(*taxa): # We create a dict to lookup Glottolog languoids by name, ISO- or Glottocode. langs = {} for lang in Glottolog().languoids(): if lang.iso: langs[lang.iso] = lang langs[lang.name] = lang langs[lang.id] = lang t = TreeMaker() for taxon in taxa: if taxon not in langs: print('unknown taxon: {0}'.format(taxon)) continue t.add(taxon, ', '.join(l[1] for l in langs[taxon].lineage)) return t
def setUpClass(cls): cls.t = TreeMaker() cls.t.add('A', 'a') cls.t.add('AB1', 'a, b') cls.t.add('AB2', 'a, b') cls.t.add('C', 'c') cls.tmpdir = mkdtemp()
def test_add_from_exception(self): taxa = [ ('A1', 'family a, subgroup 1'), ('A2family a, subgroup 2'), # oops. ('B1a', 'family b, subgroup 1'), ] with self.assertRaises(ValueError): t = TreeMaker().add_from(taxa)
#!/usr/bin/env python # coding=utf-8 import os try: from treemaker import TreeMaker except ImportError: raise ImportError("Please install treemaker") from tools import get_table, safe_name if __name__ == "__main__": import argparse parser = argparse.ArgumentParser( description="Constructs a tree from the glottolog data file") parser.add_argument("glottfile", help="Glottolog data file") parser.add_argument("treefile", help="Treefile to save as") args = parser.parse_args() if not os.path.isfile(args.glottfile): raise IOError("File %s not found" % args.glottfile) t = TreeMaker() t.add_from((safe_name(o.Language), o.Classification) for o in get_table(args.glottfile)) t.write_to_file(args.treefile)
def test_error_on_duplicate(self): t = TreeMaker() t.add('A', 'a') with self.assertRaises(ValueError): t.add('A', 'a')
def test_parse_classification(self): t = TreeMaker() assert t.parse_classification("a, b, c") == ['a', 'b', 'c'] assert t.parse_classification("family a, subgroup 1") == [ 'family a', 'subgroup 1' ]