Biopython - 聚类分析

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

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

在本章中,我们将检查重要的Biopython中的算法可以理解真实数据集上聚类的基本原理.

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

  • 分层聚类

  • K  - 聚类

  • 自组织地图

  • 主成分分析

让我们简单介绍一下上面的算法.

分层聚类

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

>>> 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  - 意味着,中位数和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-medians Clustering

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

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对应字符.

这将创建一个编码序列的2D数组,kcluster函数识别并用于聚类序列.

>>> 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]]

在这里, clusterid 是一个包含两列的数组,其中行数等于已群集的项目数,数据是一个维度为行或列的数组.

主成分分析

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

主成分分析返回元组列,坐标,组件和特征值.让我们看一下这个概念的基础知识.

>>> 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.]