本文整理汇总了Python中skbio.TreeNode.from_linkage_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python TreeNode.from_linkage_matrix方法的具体用法?Python TreeNode.from_linkage_matrix怎么用?Python TreeNode.from_linkage_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skbio.TreeNode
的用法示例。
在下文中一共展示了TreeNode.from_linkage_matrix方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: progressive_msa_and_tree
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def progressive_msa_and_tree(
sequences,
pairwise_aligner,
sequence_distance_fn=kmer_distance,
guide_tree=None,
display_aln=False,
display_tree=False,
):
""" Perform progressive msa of sequences and build a UPGMA tree
Parameters
----------
sequences : skbio.SequenceCollection
The sequences to be aligned.
pairwise_aligner : function
Function that should be used to perform the pairwise alignments,
for example skbio.Alignment.global_pairwise_align_nucleotide. Must
support skbio.BiologicalSequence objects or skbio.Alignment objects
as input.
sequence_distance_fn : function
Function that returns and skbio.DistanceMatrix given an
skbio.SequenceCollection. This will be used to build a guide tree if
one is not provided.
guide_tree : skbio.TreeNode, optional
The tree that should be used to guide the alignment process.
display_aln : bool, optional
Print the alignment before returning.
display_tree : bool, optional
Print the tree before returning.
Returns
-------
skbio.alignment
skbio.TreeNode
"""
if guide_tree is None:
guide_dm = sequences.distances(sequence_distance_fn)
guide_lm = average(guide_dm.condensed_form())
guide_tree = TreeNode.from_linkage_matrix(guide_lm, guide_dm.ids)
msa = progressive_msa(sequences, guide_tree, pairwise_aligner=pairwise_aligner)
if display_aln:
print(msa)
msa_dm = msa.distances()
msa_lm = average(msa_dm.condensed_form())
msa_tree = TreeNode.from_linkage_matrix(msa_lm, msa_dm.ids)
if display_tree:
print("\nOutput tree:")
d = dendrogram(
msa_lm, labels=msa_dm.ids, orientation="right", link_color_func=lambda x: "black", leaf_font_size=24
)
return msa, msa_tree
示例2: progressive_msa_and_tree
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def progressive_msa_and_tree(sequences,
pairwise_aligner,
metric=kmer_distance,
guide_tree=None,
display_aln=False,
display_tree=False):
""" Perform progressive msa of sequences and build a UPGMA tree
Parameters
----------
sequences : skbio.SequenceCollection
The sequences to be aligned.
pairwise_aligner : function
Function that should be used to perform the pairwise alignments,
for example skbio.alignment.global_pairwise_align_nucleotide. Must
support skbio.Sequence objects or skbio.TabularMSA objects
as input.
metric : function, optional
Function that returns a single distance value when given a pair of
skbio.Sequence objects. This will be used to build a guide tree if one
is not provided.
guide_tree : skbio.TreeNode, optional
The tree that should be used to guide the alignment process.
display_aln : bool, optional
Print the alignment before returning.
display_tree : bool, optional
Print the tree before returning.
Returns
-------
skbio.alignment
skbio.TreeNode
"""
if guide_tree is None:
guide_dm = DistanceMatrix.from_iterable(
sequences, metric=metric, key='id')
guide_lm = average(guide_dm.condensed_form())
guide_tree = TreeNode.from_linkage_matrix(guide_lm, guide_dm.ids)
msa = progressive_msa(sequences, guide_tree,
pairwise_aligner=pairwise_aligner)
if display_aln:
print(msa)
msa_dm = DistanceMatrix.from_iterable(msa, metric=metric, key='id')
msa_lm = average(msa_dm.condensed_form())
msa_tree = TreeNode.from_linkage_matrix(msa_lm, msa_dm.ids)
if display_tree:
print("\nOutput tree:")
d = dendrogram(msa_lm, labels=msa_dm.ids, orientation='right',
link_color_func=lambda x: 'black', leaf_font_size=24)
return msa, msa_tree
示例3: tree_from_distance_matrix
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def tree_from_distance_matrix(dm, metric):
if metric == "upgma":
lm = sp.cluster.hierarchy.average(dm.condensed_form())
tree = TreeNode.from_linkage_matrix(lm, dm.ids)
elif metric == "nj":
tree = skbio.tree.nj(dm)
else:
raise ValueError("Unknown metric: %s. Supported metrics are 'upgma' and 'nj'." % metric)
return tree
示例4: setUp
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def setUp(self):
np.random.seed(0)
x = np.random.rand(10)
dm = DistanceMatrix.from_iterable(x, lambda x, y: np.abs(x-y))
lm = ward(dm.condensed_form())
ids = np.arange(len(x)).astype(np.str)
self.tree = TreeNode.from_linkage_matrix(lm, ids)
# initialize tree with branch length and named internal nodes
for i, n in enumerate(self.tree.postorder(include_self=True)):
n.length = 1
if not n.is_tip():
n.name = "y%d" % i
示例5: test_linkage_matrix
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def test_linkage_matrix(self):
# Ensure matches: http://www.southampton.ac.uk/~re1u06/teaching/upgma/
id_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
linkage = np.asarray([[1.0, 5.0, 1.0, 2.0],
[0.0, 3.0, 8.0, 2.0],
[6.0, 7.0, 12.5, 3.0],
[8.0, 9.0, 16.5, 5.0],
[2.0, 10.0, 29.0, 6.0],
[4.0, 11.0, 34.0, 7.0]])
tree = TreeNode.from_linkage_matrix(linkage, id_list)
self.assertEqual("(E:17.0,(C:14.5,((A:4.0,D:4.0):4.25,(G:6.25,(B:0.5,"
"F:0.5):5.75):2.0):6.25):2.5);\n",
str(tree))
示例6: rank_linkage
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def rank_linkage(r, method='average'):
r""" Hierchical Clustering on feature ranks.
The hierarchy is built based on the rank values of the features given
an input vector `r` of ranks. The distance between two features :math:`x`
and :math:`y` can be defined as
.. math::
d(x, y) = (r(x) - r(y))^2
Where :math:`r(x)` is the rank of the features. Hierarchical clustering is
then performed using :math:`d(x, y)` as the distance metric.
This can be useful for constructing principal balances.
Parameters
----------
r : pd.Series
Continuous vector representing some ordering of the features in X.
method : str
Clustering method. (default='average')
Returns
-------
skbio.TreeNode
Tree for constructing principal balances.
Examples
--------
>>> import pandas as pd
>>> from gneiss.cluster import rank_linkage
>>> ranks = pd.Series([1, 2, 4, 5],
... index=['o1', 'o2', 'o3', 'o4'])
>>> tree = rank_linkage(ranks)
>>> print(tree.ascii_art())
/-o1
/y1------|
| \-o2
-y0------|
| /-o3
\y2------|
\-o4
"""
dm = DistanceMatrix.from_iterable(r, euclidean)
lm = linkage(dm.condensed_form(), method)
t = TreeNode.from_linkage_matrix(lm, r.index)
t = rename_internal_nodes(t)
return t
示例7: setUp
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def setUp(self):
np.random.seed(0)
self.table = pd.DataFrame(np.random.random((5, 5)),
index=['0', '1', '2', '3', '4'],
columns=['0', '1', '2', '3', '4'])
num_otus = 5 # otus
x = np.random.rand(num_otus)
dm = DistanceMatrix.from_iterable(x, lambda x, y: np.abs(x-y))
lm = ward(dm.condensed_form())
t = TreeNode.from_linkage_matrix(lm, np.arange(len(x)).astype(np.str))
self.t = SquareDendrogram.from_tree(t)
self.md = pd.Series(['a', 'a', 'a', 'b', 'b'],
index=['0', '1', '2', '3', '4'])
for i, n in enumerate(t.postorder()):
if not n.is_tip():
n.name = "y%d" % i
n.length = np.random.rand()*3
self.highlights = pd.DataFrame({'y8': ['#FF0000', '#00FF00'],
'y6': ['#0000FF', '#F0000F']}).T
示例8: test_basic_plot
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def test_basic_plot(self):
self.maxDiff = None
exp_edges = {'dest_node': ['0', '1', '2', 'y3'],
'edge_color': ['#00FF00', '#00FF00',
'#00FF00', '#FF0000'],
'edge_width': [2, 2, 2, 2],
'src_node': ['y3', 'y4', 'y3', 'y4'],
'x0': [338.2612593838583,
193.1688862557773,
338.2612593838583,
193.1688862557773],
'x1': [487.5, 12.499999999999972,
324.89684138234867, 338.2612593838583],
'y0': [271.7282256126416,
365.95231443706376,
271.7282256126416,
365.95231443706376],
'y1': [347.7691620070637,
483.2800610261029,
16.719938973897143,
271.7282256126416]}
exp_nodes = {'child0': [np.nan, np.nan, np.nan, '0', '1'],
'child1': [np.nan, np.nan, np.nan, '2', 'y3'],
'color': ['#1C9099', '#1C9099', '#1C9099',
'#FF999F', '#FF999F'],
'hover_var': [None, None, None, None, None],
'is_tip': [True, True, True, False, False],
'node_size': [10, 10, 10, 10, 10],
'x': [487.5,
12.499999999999972,
324.89684138234867,
338.26125938385832,
193.16888625577729],
'y': [347.7691620070637,
483.28006102610289,
16.719938973897143,
271.72822561264161,
365.95231443706376]}
np.random.seed(0)
num_otus = 3 # otus
x = np.random.rand(num_otus)
dm = DistanceMatrix.from_iterable(x, lambda x, y: np.abs(x-y))
lm = ward(dm.condensed_form())
t = TreeNode.from_linkage_matrix(lm, np.arange(len(x)).astype(np.str))
t = UnrootedDendrogram.from_tree(t)
# incorporate colors in tree
for i, n in enumerate(t.postorder(include_self=True)):
if not n.is_tip():
n.name = "y%d" % i
n.color = '#FF999F'
n.edge_color = '#FF0000'
n.node_size = 10
else:
n.color = '#1C9099'
n.edge_color = '#00FF00'
n.node_size = 10
n.length = np.random.rand()*3
n.edge_width = 2
p = radialplot(t, node_color='color', edge_color='edge_color',
node_size='node_size', edge_width='edge_width')
for e in exp_edges.keys():
self.assertListEqual(
list(p.renderers[0].data_source.data[e]),
exp_edges[e])
for e in exp_nodes.keys():
self.assertListEqual(
list(p.renderers[1].data_source.data[e]),
exp_nodes[e])
self.assertTrue(isinstance(t, TreeNode))
示例9: progressive_msa
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def progressive_msa(sequences, pairwise_aligner, guide_tree=None,
metric=kmer_distance):
""" Perform progressive msa of sequences
Parameters
----------
sequences : skbio.SequenceCollection
The sequences to be aligned.
metric : function, optional
Function that returns a single distance value when given a pair of
skbio.Sequence objects. This will be used to build a guide tree if one
is not provided.
guide_tree : skbio.TreeNode, optional
The tree that should be used to guide the alignment process.
pairwise_aligner : function
Function that should be used to perform the pairwise alignments,
for example skbio.alignment.global_pairwise_align_nucleotide. Must
support skbio.Sequence objects or skbio.TabularMSA objects
as input.
Returns
-------
skbio.TabularMSA
"""
if guide_tree is None:
guide_dm = DistanceMatrix.from_iterable(
sequences, metric=metric, key='id')
guide_lm = sp.cluster.hierarchy.average(guide_dm.condensed_form())
guide_tree = TreeNode.from_linkage_matrix(guide_lm, guide_dm.ids)
seq_lookup = {s.metadata['id']: s for i, s in enumerate(sequences)}
c1, c2 = guide_tree.children
if c1.is_tip():
c1_aln = seq_lookup[c1.name]
else:
c1_aln = progressive_msa(sequences, pairwise_aligner, c1)
if c2.is_tip():
c2_aln = seq_lookup[c2.name]
else:
c2_aln = progressive_msa(sequences, pairwise_aligner, c2)
alignment, _, _ = pairwise_aligner(c1_aln, c2_aln)
# this is a temporary hack as the aligners in skbio 0.4.1 are dropping
# metadata - this makes sure that the right metadata is associated with
# the sequence after alignment
if isinstance(c1_aln, Sequence):
alignment[0].metadata = c1_aln.metadata
len_c1_aln = 1
else:
for i in range(len(c1_aln)):
alignment[i].metadata = c1_aln[i].metadata
len_c1_aln = len(c1_aln)
if isinstance(c2_aln, Sequence):
alignment[1].metadata = c2_aln.metadata
else:
for i in range(len(c2_aln)):
alignment[len_c1_aln + i].metadata = c2_aln[i].metadata
return alignment
示例10: correlation_linkage
# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_linkage_matrix [as 别名]
def correlation_linkage(X, method='ward'):
r"""
Hierarchical Clustering based on proportionality.
The hierarchy is built based on the correlationity between
any two pairs of features. Specifically the correlation between
two features :math:`x` and :math:`y` is measured by
.. math::
p(x, y) = var (\ln \frac{x}{y})
If :math:`p(x, y)` is very small, then :math:`x` and :math:`y`
are said to be highly correlation. A hierarchical clustering is
then performed using this correlation as a distance metric.
This can be useful for constructing principal balances [1]_.
Parameters
----------
X : pd.DataFrame
Contingency table where the samples are rows and the features
are columns.
method : str
Clustering method. (default='ward')
Returns
-------
skbio.TreeNode
Tree for constructing principal balances.
References
----------
.. [1] Pawlowsky-Glahn V, Egozcue JJ, and Tolosana-Delgado R.
Principal Balances (2011).
Examples
--------
>>> import pandas as pd
>>> from gneiss.cluster import correlation_linkage
>>> table = pd.DataFrame([[1, 1, 0, 0, 0],
... [0, 1, 1, 0, 0],
... [0, 0, 1, 1, 0],
... [0, 0, 0, 1, 1]],
... columns=['s1', 's2', 's3', 's4', 's5'],
... index=['o1', 'o2', 'o3', 'o4']).T
>>> tree = correlation_linkage(table+0.1)
>>> print(tree.ascii_art())
/-o1
/y1------|
| \-o2
-y0------|
| /-o3
\y2------|
\-o4
"""
dm = variation_matrix(X)
lm = linkage(dm.condensed_form(), method=method)
t = TreeNode.from_linkage_matrix(lm, X.columns)
t = rename_internal_nodes(t)
return t