Biopython 聚类分析


通常,聚类分析是将一组对象分组到同一组中。这个概念主要用于数据挖掘、统计数据分析、机器学习、模式识别、图像分析、生物信息学等。它可以通过各种算法来实现,以了解集群如何广泛用于不同的分析。

根据生物信息学,聚类分析主要用于基因表达数据分析,以寻找具有相似基因表达的基因组。

在本章中,我们将检查 Biopython 中的重要算法,以了解在真实数据集上进行聚类的基础知识。

Biopython 使用 Bio.Cluster 模块来实现所有算法。它支持以下算法:

  • 层次聚类
  • K - 聚类
  • 自组织地图
  • 主成分分析

让我们简单介绍一下上述算法。

层次聚类


层次聚类用于通过距离度量将每个节点链接到其最近的邻居并创建一个集群。 Bio.Cluster 节点具有三个属性:左、右和距离。让我们创建一个简单的集群,如下所示:

>>> from Bio.Cluster import Node 
>>> n = Node(1,10) 
>>> n.left = 11 
>>> n.right = 0 
>>> n.distance = 1 
>>> print(n) 
(11, 0): 1

如果要构建基于树的聚类,请使用以下命令:

>>> n1 = [Node(1, 2, 0.2), Node(0, -1, 0.5)] >>> n1_tree = Tree(n1) 
>>> print(n1_tree) 
(1, 2): 0.2 
(0, -1): 0.5 
>>> print(n1_tree[0]) 
(1, 2): 0.2

让我们使用 Bio.Cluster 模块执行层次聚类。

考虑距离是在数组中定义的。

>>> import numpy as np 
>>> distance = array([[1,2,3],[4,5,6],[3,5,7]])

现在在树簇中添加距离数组。

>>> from Bio.Cluster import treecluster 
>>> cluster = treecluster(distance) 
>>> print(cluster) 
(2, 1): 0.666667 
(-1, 0): 9.66667

上面的函数返回一个树簇对象。此对象包含节点,其中项目的数量聚集为行或列。

K - 聚类


它是一种划分算法,分为k-means、median和medoids聚类。让我们简要了解每个聚类。

K-means 聚类

这种方法在数据挖掘中很流行。该算法的目标是在数据中找到组,组数由变量 K 表示。

该算法迭代地工作以根据提供的特征将每个数据点分配给 K 组之一。数据点基于特征相似性进行聚类。

>>> from Bio.Cluster import kcluster 
>>> from numpy import array 
>>> data = array([[1, 2], [3, 4], [5, 6]]) 
>>> clusterid, error,found = kcluster(data) 
>>> print(clusterid) [0 0 1] 
>>> print(found) 
1

K-中位数聚类

它是另一种类型的聚类算法,它计算每个聚类的平均值以确定其质心。

K-medoids 聚类

这种方法基于给定的一组项目,使用距离矩阵和用户通过的集群数量。

考虑如下定义的距离矩阵:

>>> distance = array([[1,2,3],[4,5,6],[3,5,7]])

我们可以使用以下命令计算 k-medoids 聚类:

>>> from Bio.Cluster import kmedoids 
>>> clusterid, error, found = kmedoids(distance)

让我们考虑一个例子。

kcluster 函数将数据矩阵作为输入,而不是 Seq 实例。你需要将序列转换为矩阵并将其提供给 kcluster 函数。

将数据转换为仅包含数字元素的矩阵的一种方法是使用 numpy.fromstring 功能。它基本上将序列中的每个字母转换为其对应的 ASCII 字母。

这将创建 kcluster 函数识别并用于对序列进行聚类的编码序列的 2D 数组。

>>> from Bio.Cluster import kcluster 
>>> import numpy as np 
>>> sequence = [ 'AGCT','CGTA','AAGT','TCCG'] 
>>> matrix = np.asarray([np.fromstring(s, dtype=np.uint8) for s in sequence]) 
>>> clusterid,error,found = kcluster(matrix) 
>>> print(clusterid) [1 0 0 1]

自组织地图


这种方法是一种人工神经网络。它由 Kohonen 开发,通常称为 Kohonen 地图。它根据矩形拓扑将项目组织成集群。

让我们使用相同的数组距离创建一个简单的集群,如下所示:

>>> from Bio.Cluster import somcluster 
>>> from numpy import array 
>>> data = array([[1, 2], [3, 4], [5, 6]]) 
>>> clusterid,map = somcluster(data) 

>>> print(map) 
[[[-1.36032469 0.38667395]] 
    [[-0.41170578 1.35295911]]]

>>> print(clusterid) 
[[1 0]
    [1 0]
    [1 0]]

Here, 集群ID 是一个包含两列的数组,其中行数等于聚类的项目数,并且 data 是一个维度为行或列的数组。

主成分分析


主成分分析对于可视化高维数据很有用。它是一种使用线性代数和统计中的简单矩阵运算来计算原始数据到相同数量或更少维度的投影的方法。

主成分分析返回元组 columnmean、坐标、分量和特征值。让我们来看看这个概念的基础。

>>> from numpy import array 
>>> from numpy import mean 
>>> from numpy import cov 
>>> from numpy.linalg import eig 

# define a matrix 
>>> A = array([[1, 2], [3, 4], [5, 6]]) 

>>> print(A) 
[[1 2]
    [3 4]
    [5 6]]
 
# calculate the mean of each column 
>>> M = mean(A.T, axis = 1) 
>>> print(M) 
[ 3. 4.] 

# center columns by subtracting column means 
>>> C = A - M

>>> print(C) 
[[-2. -2.]
    [ 0. 0.]
    [ 2. 2.]]

# calculate covariance matrix of centered matrix 
>>> V = cov(C.T) 

>>> print(V) 
[[ 4. 4.]
    [ 4. 4.]]
 
# eigendecomposition of covariance matrix 
>>> values, vectors = eig(V) 

>>> print(vectors) 
[[ 0.70710678 -0.70710678]
    [ 0.70710678 0.70710678]]
 
>>> print(values) 
[ 8. 0.]

让我们将相同的矩形矩阵数据应用于 Bio.Cluster 模块,定义如下:

>>> from Bio.Cluster import pca 
>>> from numpy import array 
>>> data = array([[1, 2], [3, 4], [5, 6]]) 
>>> columnmean, coordinates, components, eigenvalues = pca(data) 
>>> print(columnmean) 
[ 3. 4.] 
>>> print(coordinates) 
[[-2.82842712 0. ]
    [ 0.         0. ]
    [ 2.82842712 0. ]]
>>> print(components) 
[[ 0.70710678 0.70710678]
    [ 0.70710678 -0.70710678]]
>>> print(eigenvalues) 
[ 4. 0.]