def testScaleEdgesNoLens(self): newick_list = [ '(5,((4,3),2),1);', '(5,(4,3,2),1);', '(5,((4,3),2),1);', '(5,(4,3),2,1);', '(5,((4,3),2),1);', '(5,4,3,2,1);' ] tree_list = dendropy.TreeList(stream=StringIO("""%s""" % "\n".join(newick_list)), schema="newick") for n, tree in enumerate(tree_list): treemanip.scale_edges(tree, 2.0) self.assertEqual(newick_list[n], "%s;" % tree.as_newick_string())
def testScaleEdgesNoLens(self): newick_list = ['(5,((4,3),2),1);', '(5,(4,3,2),1);', '(5,((4,3),2),1);', '(5,(4,3),2,1);', '(5,((4,3),2),1);', '(5,4,3,2,1);'] tree_list = dendropy.TreeList( stream=StringIO("""%s""" % "\n".join(newick_list)), schema="newick") for n, tree in enumerate(tree_list): treemanip.scale_edges(tree, 2.0) self.assertEqual(newick_list[n], "%s;" % tree.as_newick_string())
def testScaleEdgesRealTest(self): newick_list = ['(5:3,((4:1,3:1):1.5,2:3),1:0);', '(5:7.5,4:1,3:-2,2:4,1:.1);'] doubled = ['(5:6.0,((4:2.0,3:2.0):3.0,2:6.0),1:0.0);', '(5:15.0,4:2.0,3:-4.0,2:8.0,1:0.2);'] as_f = ['(5:3.0,((4:1.0,3:1.0):1.5,2:3.0),1:0.0);', '(5:7.5,4:1.0,3:-2.0,2:4.0,1:0.1);'] tree_list = dendropy.TreeList( stream=StringIO("""%s""" % "\n".join(newick_list)), schema="newick") for n, tree in enumerate(tree_list): treemanip.scale_edges(tree, 2) self.assertEqual(doubled[n], "%s;" % tree.as_newick_string()) for n, tree in enumerate(tree_list): treemanip.scale_edges(tree, .5) self.assertEqual(as_f[n], "%s;" % tree.as_newick_string())
if __name__ == '__main__': from optparse import OptionParser parser = OptionParser() parser.add_option("-m", "--multiplier", dest="multip", default=1.0, type="float", help="The multiplier used for every branch length.") parser.add_option("-n", "--nexus", dest="schema", action="store_const", const="NEXUS", default="NEWICK", help="Tree is in NEXUS schema.") (options, args) = parser.parse_args() if len(args) > 1: sys.exit("At most one argument (a newick tree string with branch lengths) can be specified") if len(args) == 1: s = open(args[0], 'rU') else: newick = sys.stdin.read() s = StringIO.StringIO(newick) multip = options.multip d = DataSet() d.read(s, schema=options.schema, rooted=True) if len(d.tree_lists) == 0: sys.exit("No trees found in file.") for tb in d.tree_lists: for tree in tb: scale_edges(tree, multip) d.write_to_stream(sys.stdout, schema=options.schema)
help="The multiplier used for every branch length.") parser.add_option("-n", "--nexus", dest="schema", action="store_const", const="NEXUS", default="NEWICK", help="Tree is in NEXUS schema.") (options, args) = parser.parse_args() if len(args) > 1: sys.exit( "At most one argument (a newick tree string with branch lengths) can be specified" ) if len(args) == 1: s = open(args[0], 'rU') else: newick = sys.stdin.read() s = StringIO.StringIO(newick) multip = options.multip d = DataSet() d.read(s, schema=options.schema, rooted=True) if len(d.tree_lists) == 0: sys.exit("No trees found in file.") for tb in d.tree_lists: for tree in tb: scale_edges(tree, multip) d.write_to_stream(sys.stdout, schema=options.schema)