def _do_test(self, content, expected): e = [ deepcopy(i) for i in NewickEventFactory(tokenizer=NewickTokenizer( stream=StringIO(content))) ] # print(e) self.assertEqual(e, expected)
def testOddQuotes(self): content = "((h_ ,'p)h p,g()[],:_)hpg;" tok = NewickTokenizer(StringIO(content)) content = "((h_ ,'p')h p,'g()[]',:_')hpg;" tok = NewickTokenizer(StringIO(content)) self.assertRaises(Exception, tok.tokens)
def _do_test(self, content, expected): self.assertEqual([i for i in NewickTokenizer(StringIO(content))], expected)
def pretty_dict_str(d, indent=2): """shows JSON indented representation of d""" b = StringIO() write_pretty_dict_str(b, d, indent=indent) return b.getvalue()
def _do_full_check_of_tree_invariants(tree, testCase, id2par=None, leaf_ids=None): post_order = [nd for nd in tree.postorder_node_iter()] post_order_ids = [] psio = set() for node in post_order: i = node._id testCase.assertNotIn(i, psio) testCase.assertIn(i, tree._id2node) psio.add(i) post_order_ids.append(i) pre_order = [nd for nd in tree.preorder_node_iter()] pre_order_ids = [] prsio = set() for node in pre_order: i = node._id prsio.add(i) pre_order_ids.append(i) testCase.assertEqual(psio, prsio) for i in tree._id2node.keys(): testCase.assertIn(i, psio) if leaf_ids is None: leaf_ids = [i for i in tree.leaf_id_iter()] for i in leaf_ids: testCase.assertIn(i, tree._leaves) if not id2par: for l in leaf_ids: testCase.assertTrue(l in psio) for node in post_order: if node.is_leaf: testCase.assertIn(node._id, leaf_ids) else: # _LOG.debug(node._children) testCase.assertNotIn(node._id, leaf_ids) else: anc_ref_count = {} anc_set = set() checked_node = set() # _LOG.debug('post_order_ids = {}'.format(post_order_ids)) for t in leaf_ids: anc_id = id2par[t] # _LOG.debug('anc_id = {}'.format(anc_id)) anc_ref_count[anc_id] = 1 + anc_ref_count.get(anc_id, 0) if anc_ref_count[anc_id] > 1: anc_set.add(anc_id) testCase.assertTrue(tree.find_node(t).is_leaf) testCase.assertIn(t, post_order_ids) if (anc_id in tree._id2node) and (anc_id == tree.find_node(anc_id)._id): testCase.assertIn(anc_id, post_order_ids) testCase.assertLess(post_order_ids.index(t), post_order_ids.index(anc_id)) testCase.assertGreater(pre_order_ids.index(t), pre_order_ids.index(anc_id)) else: testCase.assertNotIn(anc_id, psio) checked_node.add(t) while len(anc_set - checked_node) > 0: ns = set() for t in anc_set: if t in checked_node: continue testCase.assertNotIn(t, leaf_ids) testCase.assertFalse(tree.find_node(t).is_leaf) anc_id = id2par[t] # _LOG.debug('anc_id = {}'.format(anc_id)) anc_ref_count[anc_id] = 1 + anc_ref_count.get(anc_id, 0) if anc_ref_count[anc_id] > 1: ns.add(anc_id) testCase.assertIn(t, psio) checked_node.add(t) if anc_id is not None: if (anc_id in tree._id2node) and (anc_id == tree.find_node(anc_id)._id): testCase.assertIn(anc_id, psio) testCase.assertLess(post_order_ids.index(t), post_order_ids.index(anc_id)) testCase.assertGreater(pre_order_ids.index(t), pre_order_ids.index(anc_id)) else: testCase.assertNotIn(anc_id, psio) anc_set.update(ns) # _LOG.debug('anc_set = {}'.format(anc_set)) # _LOG.debug('checked_node = {}'.format(checked_node)) if len(tree._id2node) > 1: from peyotl.utility.str_util import StringIO o = StringIO() tree.write_newick(o) from peyotl.utility.tokenizer import NewickEventFactory nef = NewickEventFactory(newick=o.getvalue()) TreeWithPathsInEdges(newick_events=nef)
def __str__(self): out = StringIO() for name, sep in self.separators.items(): out.write('top-level name={}\n'.format(name)) sep.write_str(out) return out.getvalue()
def __str__(self): out = StringIO() self.write_str(out) return out.getvalue()