本文整理汇总了Python中NewickIO.parse方法的典型用法代码示例。如果您正苦于以下问题:Python NewickIO.parse方法的具体用法?Python NewickIO.parse怎么用?Python NewickIO.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NewickIO
的用法示例。
在下文中一共展示了NewickIO.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_response_content
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_response_content(fs):
# read the query tree
query_tree = NewickIO.parse(fs.query, FelTree.NewickTree)
# read the reference tree
reference_tree = NewickIO.parse(fs.reference, FelTree.NewickTree)
# calculate the loss using the requested loss function
if fs.uniform:
loss_numerator = TreeComparison.get_split_distance(
query_tree, reference_tree)
elif fs.weighted:
loss_numerator = TreeComparison.get_weighted_split_distance(
query_tree, reference_tree)
# do the normalization if requested
if fs.normalize:
if fs.uniform:
loss_denominator = float(
TreeComparison.get_nontrivial_split_count(reference_tree))
elif fs.weighted:
loss_denominator = float(
TreeComparison.get_weighted_split_count(reference_tree))
else:
loss_denominator = 1
# return the response
if loss_denominator:
return str(loss_numerator / loss_denominator) + '\n'
else:
return 'normalization failed\n'
示例2: test_get_split_distance
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def test_get_split_distance(self):
"""
Test the function that gets the number of missing nontrivial partitions.
"""
# define some trees
tree_string_a = '((A:1, B:1):1, C:1, (D:1, E:1):1);'
tree_string_b = '((A:1, B:1):1, D:1, (C:1, E:1):1);'
tree_string_c = '((A:1, D:1):1, C:1, (B:1, E:1):1);'
tree_string_d = '((A:1, D:1):1, (C:1, B:1, E:1):1);'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
tree_c = NewickIO.parse(tree_string_c, FelTree.NewickTree)
tree_d = NewickIO.parse(tree_string_d, FelTree.NewickTree)
# the distance from a tree to itself should be zero
self.assertEqual(get_split_distance(tree_a, tree_a), 0)
self.assertEqual(get_split_distance(tree_b, tree_b), 0)
self.assertEqual(get_split_distance(tree_c, tree_c), 0)
self.assertEqual(get_split_distance(tree_d, tree_d), 0)
# some of the distances are symmetric
self.assertEqual(get_split_distance(tree_a, tree_b), 1)
self.assertEqual(get_split_distance(tree_b, tree_a), 1)
self.assertEqual(get_split_distance(tree_b, tree_c), 2)
self.assertEqual(get_split_distance(tree_c, tree_b), 2)
self.assertEqual(get_split_distance(tree_a, tree_c), 2)
self.assertEqual(get_split_distance(tree_c, tree_a), 2)
# it is possible for the distance to be asymmetric if internal nodes are not order 3
self.assertEqual(get_split_distance(tree_a, tree_d), 1)
self.assertEqual(get_split_distance(tree_d, tree_a), 2)
示例3: do_distance_analysis
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def do_distance_analysis(X):
# get the matrix of squared distances
labels = list("0123")
# reconstruct the matrix of Euclidean distances from a tree
D_sqrt = np.array([[np.linalg.norm(y - x) for x in X] for y in X])
sqrt_tree = NeighborJoining.make_tree(D_sqrt, labels)
sqrt_tree_string = NewickIO.get_newick_string(sqrt_tree)
sqrt_feltree = NewickIO.parse(sqrt_tree_string, FelTree.NewickTree)
D_sqrt_reconstructed = np.array(sqrt_feltree.get_distance_matrix(labels))
# reconstruct the matrix of squared Euclidean distances from a tree
D = D_sqrt ** 2
tree = NeighborJoining.make_tree(D, labels)
tree_string = NewickIO.get_newick_string(tree)
feltree = NewickIO.parse(tree_string, FelTree.NewickTree)
D_reconstructed = np.array(feltree.get_distance_matrix(labels))
# start writing
out = StringIO()
# matrix of Euclidean distances and its reconstruction from a tree
print >> out, "matrix of Euclidean distances between tetrahedron vertices:"
print >> out, D_sqrt
print >> out, "neighbor joining tree constructed from D = non-squared Euclidean distances (unusual):"
print >> out, sqrt_tree_string
print >> out, "distance matrix implied by this tree:"
print >> out, D_sqrt_reconstructed
# matrix of squared Euclidean distances and its reconstruction from a tree
print >> out, "matrix of squared distances between tetrahedron vertices:"
print >> out, D
print >> out, "neighbor joining tree constructed from D = squared Euclidean distances (normal):"
print >> out, tree_string
print >> out, "distance matrix implied by this tree:"
print >> out, D_reconstructed
return out.getvalue().strip()
示例4: get_response_content
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_response_content(fs):
# get the set of names
selection = Util.get_stripped_lines(StringIO(fs.names))
# get the tree
tree = NewickIO.parse(fs.tree, FelTree.NewickTree)
# assert that the name selection is compatible with the tree
selected_name_set = set(selection)
possible_name_set = set(node.get_name() for node in tree.gen_tips())
extra_names = selected_name_set - possible_name_set
if extra_names:
msg_a = "the following selected names "
msg_b = "are not valid tips: %s" % str(tuple(extra_names))
raise HandlingError(msg_a + msg_b)
# get the pruned tree
simple_tree = NewickIO.parse(fs.tree, Newick.NewickTree)
pruned_tree = get_pruned_tree(simple_tree, selected_name_set)
# begin writing the result
out = StringIO()
trees = (tree, pruned_tree)
tree_names = ("the original tree", "the pruned tree")
for tree, tree_name in zip(trees, tree_names):
print >> out, "calculating splits of %s:" % tree_name
print >> out, process_tree(tree, tree_name, fs.show_newick, fs.show_art)
# return the response
return out.getvalue()
示例5: _create_trees
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def _create_trees(self):
"""
Create the full tree and the pruned tree.
The full tree is a Newick.NewickTree,
and the pruned tree is a FelTree.NewickTree object.
"""
# create the full tree
self.full_tree = NewickIO.parse(self.newick_string, Newick.NewickTree)
# create the pruned tree through a temporary tree that will be modified
temp_tree = NewickIO.parse(self.newick_string, Newick.NewickTree)
remove_redundant_nodes(temp_tree)
pruned_newick_string = NewickIO.get_newick_string(temp_tree)
self.pruned_tree = NewickIO.parse(pruned_newick_string, FelTree.NewickTree)
示例6: test_get_weighted_split_distance
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def test_get_weighted_split_distance(self):
"""
Test the function that gets the number of missing nontrivial partitions.
"""
# define some trees
tree_string_a = '((A:1, B:1):1, (C:1, D:1):1, (E:1, F:1):1);'
tree_string_b = '(((A:1, B:1):1, C:1):1, D:1, (E:1, F:1):1);'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
# the distance from a tree to itself should be zero
self.assertEqual(get_weighted_split_distance(tree_a, tree_a), 0)
self.assertEqual(get_weighted_split_distance(tree_b, tree_b), 0)
# the distance is not necessarily symmetric
self.assertEqual(get_weighted_split_distance(tree_a, tree_b), 20)
self.assertEqual(get_weighted_split_distance(tree_b, tree_a), 15)
示例7: get_response_content
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_response_content(fs):
# read the tree
tree = NewickIO.parse(fs.tree, FelTree.NewickTree)
# get ordered identifiers
ordered_tip_name_id_pairs = list(sorted(set((node.get_name(), id(node))
for node in tree.gen_tips())))
ordered_tip_names, ordered_tip_ids = zip(*ordered_tip_name_id_pairs)
ordered_internal_ids = [id(node)
for node in tree.preorder() if not node.is_tip()]
ordered_ids = list(ordered_tip_ids) + ordered_internal_ids
# get the distance matrices
full_D = tree.get_partial_distance_matrix(ordered_ids)
partial_D = tree.get_partial_distance_matrix(ordered_tip_ids)
# get the balaji matrices
full_R = Clustering.get_R_balaji(full_D)
partial_R = Clustering.get_R_balaji(partial_D)
# Get the fiedler eigenvector and another eigenvector
# for the full and the partial balaji matrices.
full_va, full_vb = get_eigenvectors(full_R)
partial_va, partial_vb = get_eigenvectors(partial_R)
# create the response
out = StringIO()
print >> out, 'Fiedler vector associated with the graph'
print >> out, 'for which the internal nodes are hidden:'
print >> out, str(tuple(partial_va))
print >> out
print >> out, 'The tip subvector of the Fiedler vector'
print >> out, 'associated with the graph of the full tree:'
print >> out, str(tuple(full_va[:len(ordered_tip_ids)]))
# write the response
return out.getvalue()
示例8: get_form
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_form():
"""
@return: a list of form objects
"""
tree = NewickIO.parse(g_default_string, FelTree.NewickTree)
formatted_tree_string = NewickIO.get_narrow_newick_string(tree, 60)
return [Form.MultiLine('tree', 'tree', formatted_tree_string)]
示例9: get_default_original_tree
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_default_original_tree():
tree = NewickIO.parse(g_default_string, FelTree.NewickTree)
for node in tree.preorder():
blen = node.get_branch_length()
if blen is not None:
node.set_branch_length(blen * 0.5)
return tree
示例10: process
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def process(tree_string):
"""
@param tree_string: a newick string
@return: a multi-line string that summarizes the results
"""
np.set_printoptions(linewidth=200)
out = StringIO()
# build the newick tree from the string
tree = NewickIO.parse(tree_string, FelTree.NewickTree)
# get ordered names and ids
ordered_ids, ordered_names = get_ordered_ids_and_names(tree)
# get the distance matrix with ordered indices including all nodes in the tree
nvertices = len(list(tree.preorder()))
nleaves = len(list(tree.gen_tips()))
id_to_index = dict((myid, i) for i, myid in enumerate(ordered_ids))
D = np.array(tree.get_partial_distance_matrix(ordered_ids))
# define mass vectors
m_uniform_unscaled = [1]*nvertices
m_degenerate_unscaled = [1]*nleaves + [0]*(nvertices-nleaves)
m_uniform = np.array(m_uniform_unscaled, dtype=float) / sum(m_uniform_unscaled)
m_degenerate = np.array(m_degenerate_unscaled, dtype=float) / sum(m_degenerate_unscaled)
# show some of the distance matrices
print >> out, 'ordered names:'
print >> out, ordered_names
print >> out
print >> out, 'embedded points with mass uniformly distributed among all vertices:'
print >> out, Euclid.edm_to_weighted_points(D, m_uniform)
print >> out
print >> out, 'embedded points with mass uniformly distributed among the leaves:'
print >> out, Euclid.edm_to_weighted_points(D, m_degenerate)
print >> out
# return the response
return out.getvalue().strip()
示例11: get_form
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_form():
"""
@return: the body of a form
"""
# define the default tree string
tree = NewickIO.parse(g_default_string, FelTree.NewickTree)
formatted_tree_string = NewickIO.get_narrow_newick_string(tree, 60)
# define the form objects
form_objects = [
Form.MultiLine('tree', 'newick tree',
formatted_tree_string),
Form.Integer('length', 'use sequences that are this long',
100, low=1),
Form.RadioGroup('assumption', 'distance matrix sampling model', [
Form.RadioItem('infinite_alleles', 'infinite alleles', True),
Form.RadioItem('jukes_cantor',
'Jukes-Cantor model (4 alleles)')]),
Form.RadioGroup('infinity', 'infinite distance estimates', [
Form.RadioItem('reject_infinity', 'reject these matrices'),
Form.RadioItem('replace_infinity',
'replace inf with 20', True)]),
Form.RadioGroup('zero', 'distance estimates of zero', [
Form.RadioItem('reject_zero', 'reject these matrices'),
Form.RadioItem('replace_zero', 'use .00001 instead of zero'),
Form.RadioItem('remain_zero', 'use 0 unmodified', True)])]
return form_objects
示例12: test_felsenstein
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def test_felsenstein(self):
tree = NewickIO.parse(g_felsenstein_tree_string, FelTree.NewickTree)
ordered_names = ('a', 'b', 'c', 'd', 'e')
C_expected = np.dot(g_contrast_matrix, np.diag(1/np.sqrt(g_contrast_variances)))
contrasts, variances = get_contrasts_and_variances(tree, ordered_names)
C_observed = np.dot(np.array(contrasts).T, np.diag(1/np.sqrt(np.array(variances))))
"""
print
print 'felsenstein variances:'
print g_contrast_variances
print 'observed variances:'
print variances
print
print 'felsenstein contrast matrix:'
print C_expected
print 'observed contrast matrix:'
print C_observed
L_expected = np.dot(C_expected, C_expected.T)
L_observed = np.dot(C_observed, C_observed.T)
print 'felsenstein L matrix:'
print L_expected
print 'observed L matrix:'
print L_observed
D = np.array(tree.get_distance_matrix(ordered_names))
L = Euclid.edm_to_laplacian(D)
print 'L matrix derived from the D matrix:'
print L
"""
pass
示例13: get_response_content
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_response_content(fs):
# read the tree
tree = NewickIO.parse(fs.tree, Newick.NewickTree)
# begin the response
out = StringIO()
# remove the branch length associated with the root
if tree.get_root().blen is not None:
print >> out, 'the root originally had a branch length of', tree.get_root().blen
tree.get_root().blen = None
else:
print >> out, 'the root did not originally have a branch length'
# force a trifurcation at the root
if tree.get_root().get_child_count() < 3:
print >> out, 'the original root had', tree.get_root().get_child_count(), 'children'
max_children, best_child = max((child.get_child_count(), child) for child in tree.get_root().gen_children())
old_root = tree.get_root()
tree.reroot(best_child)
tree.remove_node(old_root)
print >> out, 'the new root has', tree.get_root().get_child_count(), 'children'
else:
print >> out, 'the root has', tree.get_root().get_child_count(), 'children'
# remove names of internal nodes
nremoved_names = 0
for node in tree.preorder():
if node.has_children() and node.name is not None:
node.name = None
nremoved_names += 1
print >> out, 'removed', nremoved_names, 'internal node names'
# draw the new formatted newick string after a break
print >> out
formatted_tree_string = NewickIO.get_narrow_newick_string(tree, 120)
print >> out, formatted_tree_string
# return the response
return out.getvalue()
示例14: get_response_content
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_response_content(fs):
# get the tree
tree = NewickIO.parse(fs.tree_string, FelTree.NewickTree)
# get information about the tree topology
internal = [id(node) for node in tree.gen_internal_nodes()]
tips = [id(node) for node in tree.gen_tips()]
vertices = internal + tips
ntips = len(tips)
ninternal = len(internal)
nvertices = len(vertices)
# get the ordered ids with the leaves first
ordered_ids = vertices
# get the full distance matrix
D = np.array(tree.get_partial_distance_matrix(ordered_ids))
# compute the two matrices to be compared
p = ninternal
q = ntips
N = fs.N
aug_a = get_aug_a(D, p, q, N)
aug_b = get_aug_b(D, p, q, N)
# show the output
out = StringIO()
print >> out, "-(1/2)MEDE'M':"
print >> out, aug_a
print >> out
print >> out, "-(1/2)HMDM'H:"
print >> out, aug_b
print >> out
print >> out, 'allclose:', np.allclose(aug_a, aug_b)
return out.getvalue()
示例15: get_form
# 需要导入模块: import NewickIO [as 别名]
# 或者: from NewickIO import parse [as 别名]
def get_form():
"""
@return: the body of a form
"""
# define the default tree string
tree = NewickIO.parse(g_default_string, FelTree.NewickTree)
formatted_tree_string = NewickIO.get_narrow_newick_string(tree, 60)
# define the form objects
form_objects = [
Form.MultiLine("tree", "newick tree", formatted_tree_string),
Form.RadioGroup(
"matrix",
"nodes used for the distance matrix",
[
RadioItem("standard", "tips only", True),
RadioItem("augmented", "all nodes"),
RadioItem("named", "all named nodes"),
],
),
Form.CheckGroup(
"output_options",
"output options",
[
CheckItem("show_split", "exact criterion partition", True),
CheckItem("show_value", "exact criterion value", True),
CheckItem("show_value_minus_trace", "exact criterion value minus trace", True),
CheckItem("show_fiedler_split", "show the spectral sign partition", True),
CheckItem("show_fiedler_eigenvector", "show the eigenvector of interest", True),
CheckItem("show_labels", "ordered labels", True),
CheckItem("show_distance_matrix", "distance matrix", True),
CheckItem("show_M_matrix", "M matrix", True),
],
),
]
return form_objects