Biopython教程

Biopython聚类分析

Biopython聚类分析详细操作教程
聚类分析会将一组对象归为同一组。这个概念主要用于数据挖掘,统计数据分析,机器学习,模式识别,图像分析,生物信息学等。可以通过各种算法来了解群集如何广泛用于不同的分析中。
根据生物信息学,聚类分析主要用于基因表达数据分析中,以寻找具有相似基因表达的基因组。在本章中,我们将检查Biopython中的重要算法,以了解在真实数据集上进行聚类的基础知识。Biopython使用Bio.Cluster模块来实现所有算法。它支持以下算法-
层次聚类 K-聚类 自组织映射 主成分分析
下面对以上算法进行简要介绍。

1. 层次聚类

层次聚类用于通过距离度量将每个节点链接到其最近的邻居,并创建一个聚类。Bio.Cluster节点具有三个属性:left, right 和 distance。下面创建一个简单的集群,如下所示:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Cluster import Node
>>> n = Node(1,10)
>>> n.left = 11
>>> n.right = 0
>>> n.distance = 1
>>> print(n)
(11, 0): 1
如果要构建基于树的集群,请使用以下命令 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> 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模块执行分层聚类。假设距离是在数组中定义的。如下代码 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> import numpy as np
>>> distance = array([[1,2,3],[4,5,6],[3,5,7]])
现在,在树簇中添加距离数组。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Cluster import treecluster
>>> cluster = treecluster(distance)
>>> print(cluster)
(2, 1): 0.666667
(-1, 0): 9.66667
上面的函数返回一个树集群对象。该对象包含节点,其中项目数聚集为行或列。

2. K-聚类

它是一种划分算法,分为k-均值,中位数和medoids聚类。下面我们来简要地了解每个聚类。
K均值聚类这种方法在数据挖掘中很流行。该算法的目标是找到数据中的组,组的数量由变量K表示。这个算法基于提供的功能迭代地将每个数据点分配给K个组之一。数据点基于特征相似性进行聚类。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> 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聚类
该方法基于给定的一组项目,使用距离矩阵和用户传递的簇数。考虑如下定义的距离矩阵 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> distance = array([[1,2,3],[4,5,6],[3,5,7]])
可以使用以下命令计算k-medoids聚类 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Cluster import kmedoids
>>> clusterid, error, found = kmedoids(distance)
让我们看看下面一个例子。
kcluster函数将数据矩阵作为输入,而不是Seq实例。需要将序列转换为矩阵,并将其提供给kcluster函数。将数据转换为仅包含数字元素的矩阵的一种方法是使用numpy.fromstring函数。它基本上将序列中的每个字母转换为其对应的ASCII。这将创建一个二维序列的编码序列,kcluster函数将其识别并用于对序列进行聚类。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> 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]

3. 自组织映射

这种方法是一种人工神经网络。它由Kohonen开发,通常称为Kohonen映射。它根据矩形拓扑将项目组织到群集中,让我们使用相同的数组距离创建一个简单的集群,如下所示:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> 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是一个具有两列的数组,其中行数等于被集群的项目数,而data是一个具有行或列维的数组。

4. 主成分分析

主成分分析对于可视化高维数据很有用。它是一种使用线性代数和统计数据的简单矩阵运算来计算原始数据到相同数量或更少维的投影的方法。主成分分析返回元组列平均值,坐标,成分和特征值。下面我们来通过一段代码学习这个概念 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> 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模块,如下所示:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> 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.]
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4