基于近似近邻的连锁聚类
ann-linkage-clustering的Python项目详细描述
找到共同丰富的基因组
目的
从大量样本中分析基因丰度数据并计算 哪一组基因在所有样本中的丰度相似。 这些基因在生物学上是有联系的,比如 全基因组鸟枪序列的亚基因组分析 从同一个基因组中,发现的数量往往相似。
代码可用性
此存储库中的代码以两种不同的格式提供。那里
是一个python代码库(ann_linkage_clustering
在pypi中),它可以
用于直接从pandas数据帧生成cags。还有一个
Docker映像,该映像将与脚本find cags.py一起运行。
下面的文档描述了可用的端到端工作流
使用Docker图像和单包装脚本。
输入数据格式
假设所有输入数据都是json格式(gzip可选)。
每个单独样本的相关数据是
每个样品。输入文件必须包含一个列表,其中每个元素
是一个包含基因id的
dict
使用另一个
键进行度量
对于最初的开发,我们假设每个输入文件是一个
dict
,结果位于该dict
内的单个键。
在未来,我们可能会支持更灵活的提取
来自不同结构的文件的结果,但对于第一个过程,我们将
就这样吧。
因此,用户必须指定的功能是:
- json中基因丰度列表的键(例如"结果")
- 列表中每个元素中的基因id键(例如"id")
- 每个元素内丰度度量的键(例如"深度")
下面是一个json格式的示例:
{"results":[{"id":"gene_1","depth":1.1},{"id":"gene_2","depth":0.2},{"id":"gene_3","depth":3000.015},],"logs":["any other data","that you would like","to include in this file is just fine."]}
注意:所有丰度度量值必须>;=0
从任何数据帧运行
如果有任何其他格式的数据,也可以使用此代码查找CAG。
最大的区别是这个脚本执行一些数据规范化,这些数据规范化非常
乐于助人。例如,如果使用的是余弦距离,则最好使用
表示缺勤为零。所以如果你使用居中对数比(CLR)
标准化方法,你真的需要在所有的样本中设置一个标准的截止值,
将最低值修剪为该值,然后将最低值设置为零。这就是一切
通过find cags.py
自动完成,但您完全可以使用相同的函数
使用任何其他输入数据格式或标准化方法制作CAGS。
您可以遵循find cags.py
脚本中的工作流,它基本上遵循
这个工作流程(假设df
是包含基因的丰富数据的数据框架
行和列中的样本:
frommultiprocessingimportPoolfromann_linkage_clustering.libimportmake_cags_with_annfromann_linkage_clustering.libimportiteratively_refine_cagsfromann_linkage_clustering.libimportmake_nmslib_index# Maximum distance threshold (use any value)max_dist=0.2# Distance metric (only 'cosine' is supported)distance_metric="cosine"# Multiprocessing pool (pick any number of threads, in this case `1`)threads=1pool=Pool(threads)# Linkage type (only `average` is fully supported)linkage_type="average"# Make the ANN indexindex=make_nmslib_index(df)# Make the CAGs in the first roundcags=make_cags_with_ann(index,max_dist,df,pool,threads=threads,distance_metric=distance_metric,linkage_type=linkage_type)# Iteratively refine the CAGs (this is the part that is hardedcoded to # use average linkage clustering, while the step above could technically# use any of `complete`, `single`, `average`, etc.)iteratively_refine_cags(cags,df.copy(),max_dist,distance_metric=distance_metric,linkage_type=linkage_type,threads=threads)
最后,cags对象是一个包含 所有已识别的组。
样本表
要将单个文件与示例名称链接,用户将指定
示例表,它是一个json文件,格式为dict
,带有示例
名称作为键,文件位置作为值。
数据位置
目前,我们将支持在本地文件系统中找到的数据 或AWS S3。
测试数据集
为了测试,我将使用一组json,其中包含
一组微生物样本的单个基因。数据在这个
测试/
文件夹。另外还有一个json文件,指示哪个示例
使用哪个文件,它被格式化为一个简单的dict(键是示例名称
值是文件位置),位于tests/sample_sheet.json
标准化
--normalize
度量接受三个值,clr
,median
和sum
。在每种情况下
每个样本中每个基因的丰度指标除以
该基因内所有基因丰度指标的中位数
或和
。
样品。当计算中位数
时,只有非零丰度的基因
被考虑。对于clr
,每个值除以
取样,然后取对数10。所有零值都用最小值填充
整个数据集的值(使它们在样本中相等,而不是
对测序深度敏感。
近似最近邻
近似近邻算法 nmslib正在用于创建CAGS。 此实现在 基准
距离度量
距离度量现在硬编码为余弦相似性。这受到
ann在nmslib
中的可用功能,因此已标准化为
代码库的其他部分也一样。
改进
在找到cags之后,算法将迭代连接与每个cags非常相似的cags。 其他合计。这增加了最终CAG的保真度,并减轻了 人工神经网络的灵敏度限制。
调用
usage: find-cags.py [-h] --sample-sheet SAMPLE_SHEET --output-prefix
OUTPUT_PREFIX --output-folder OUTPUT_FOLDER
[--normalization NORMALIZATION] [--max-dist MAX_DIST]
[--temp-folder TEMP_FOLDER] [--results-key RESULTS_KEY]
[--abundance-key ABUNDANCE_KEY]
[--gene-id-key GENE_ID_KEY] [--threads THREADS]
[--min-samples MIN_SAMPLES] [--clr-floor CLR_FLOOR]
[--test]
Find a set of co-abundant genes
optional arguments:
-h, --help show this help message and exit
--sample-sheet SAMPLE_SHEET
Location for sample sheet (.json[.gz]).
--output-prefix OUTPUT_PREFIX
Prefix for output files.
--output-folder OUTPUT_FOLDER
Folder to place results. (Supported: s3://, or local
path).
--normalization NORMALIZATION
Normalization factor per-sample (median, sum, or clr).
--max-dist MAX_DIST Maximum cosine distance for clustering.
--temp-folder TEMP_FOLDER
Folder for temporary files.
--results-key RESULTS_KEY
Key identifying the list of gene abundances for each
sample JSON.
--abundance-key ABUNDANCE_KEY
Key identifying the abundance value for each element
in the results list.
--gene-id-key GENE_ID_KEY
Key identifying the gene ID for each element in the
results list.
--threads THREADS Number of threads to use.
--min-samples MIN_SAMPLES
Filter genes by the number of samples they are found
in.
--clr-floor CLR_FLOOR
Set a minimum CLR value, 'auto' will use the largest
minimum value.
--test Run in testing mode and only process a subset of 2,000
genes.
usage: find-cags.py [-h] --sample-sheet SAMPLE_SHEET --output-prefix
OUTPUT_PREFIX --output-folder OUTPUT_FOLDER
[--normalization NORMALIZATION] [--max-dist MAX_DIST]
[--temp-folder TEMP_FOLDER] [--results-key RESULTS_KEY]
[--abundance-key ABUNDANCE_KEY]
[--gene-id-key GENE_ID_KEY] [--threads THREADS]
[--min-samples MIN_SAMPLES] [--clr-floor CLR_FLOOR]
[--test]
Find a set of co-abundant genes
optional arguments:
-h, --help show this help message and exit
--sample-sheet SAMPLE_SHEET
Location for sample sheet (.json[.gz]).
--output-prefix OUTPUT_PREFIX
Prefix for output files.
--output-folder OUTPUT_FOLDER
Folder to place results. (Supported: s3://, or local
path).
--normalization NORMALIZATION
Normalization factor per-sample (median, sum, or clr).
--max-dist MAX_DIST Maximum cosine distance for clustering.
--temp-folder TEMP_FOLDER
Folder for temporary files.
--results-key RESULTS_KEY
Key identifying the list of gene abundances for each
sample JSON.
--abundance-key ABUNDANCE_KEY
Key identifying the abundance value for each element
in the results list.
--gene-id-key GENE_ID_KEY
Key identifying the gene ID for each element in the
results list.
--threads THREADS Number of threads to use.
--min-samples MIN_SAMPLES
Filter genes by the number of samples they are found
in.
--clr-floor CLR_FLOOR
Set a minimum CLR value, 'auto' will use the largest
minimum value.
--test Run in testing mode and only process a subset of 2,000
genes.