数据分析:基于sparcc的co-occurrence网络

介绍

Sparcc是基于16s或metagenomics数据等计算组成数据之间关联关系的算法。通常使用count matrix数据。

安装Sparcc软件

git clone git@github.com:JCSzamosi/SparCC3.git
export PATH=/path/SparCC3:$PATHwhich SparCC.py

导入数据

注:使用rarefy抽平的count matrix数据

library(dplyr)
library(tibble)dat <- read.table("dat_rarefy10000_v2.tsv", header = T)

过滤数据

filter_fun <- function(prof=dat , tag="dat ", cutoff=0.005){# prof=dat # tag="dat " # cutoff=0.005dat <- cbind(prof[, 1, drop=F], prof[, -1] %>% summarise(across(everything(), function(x){x/sum(x)}))) %>%column_to_rownames("OTUID")#dat.cln <- dat[rowSums(dat) > cutoff, ]remain <- apply(dat, 1, function(x){length(x[x>cutoff])}) %>% data.frame() %>%setNames("Counts") %>%rownames_to_column("OTUID") %>%mutate(State=ifelse(Counts>1, "Remain", "Discard")) %>%filter(State == "Remain")# countcount <- prof %>% filter(OTUID%in%remain$OTUID)filename <- paste0("../dataset/Sparcc/", tag, "_rarefy10000_v2_", cutoff, ".tsv")write.table(count, file = filename, quote = F, sep = "\t", row.names = F)# relative abundancerelative <- dat %>% rownames_to_column("OTUID") %>%filter(OTUID%in%remain$OTUID)filename <- paste0("../dataset/Sparcc/", tag, "_rarefy10000_v2_", cutoff, "_rb.tsv")write.table(relative, file = filename, quote = F, sep = "\t", row.names = F)
}filter_fun(prof=dat, tag="dat", cutoff=0.005)
filter_fun(prof=dat, tag="dat", cutoff=0.001)

Result: 保留过滤后的count matrix和relative abundance matrix两类型矩阵

sparcc analysis

该过程分成两步:1.计算相关系数;2.permutation test计算p值.

  • iteration 参数使用default -i 20

  • permutation 参数: 1000次

# Step 1 - Compute correlations
python /data/share/toolkits/SparCC3/SparCC.py sxtr_rarefy10000_v2_0.001.tsv -i 20 --cor_file=sxtr_sparcc.tsv > sxtr_sparcc.log
echo "Step 1 - Compute correlations Ended successfully!"# Step 2 - Compute bootstraps
python /data/share/toolkits/SparCC3/MakeBootstraps.py sxtr_rarefy10000_v2_0.001.tsv -n 1000 -t bootstrap_#.txt -p pvals/ >> sxtr_sparcc.log
echo "Step 2 - Compute bootstraps Ended successfully!"# Step 3 - Compute p-values
for n in {0..999}; do /data/share/toolkits/SparCC3/SparCC.py pvals/bootstrap_${n}.txt -i 20 --cor_file=pvals/bootstrap_cor_${n}.txt >> sxtr_sparcc.log; done
python /data/share/toolkits/SparCC3/PseudoPvals.py sxtr_sparcc.tsv pvals/bootstrap_cor_#.txt 1000 -o pvals/pvals.two_sided.txt -t two_sided >> sxtr_sparcc.log
echo "Step 3 - Compute p-values Ended successfully!"# step 4 - Rename file
mv pvals/pvals.two_sided.txt sxtr_pvals.two_sided.tsv
mv cov_mat_SparCC.out sxtr_cov_mat_SparCC.tsv
echo "step 4 - Rename file Ended successfully!"

co-occurrence network

网络图要符合以下要求:

  1. 保留相互之间显著差异(p < 0.05)OTU;

  2. genus分类学水平表示OTU来源;

  3. OTU间相关性用颜色区分,且线条粗细代表相关系数大小;

  4. OTU点大小表示其丰度大小;

  5. 统计网络中正负相关数目;

导入画图数据

library(igraph)
library(psych)dat_cor <- read.table("dat_cov_mat_SparCC.tsv", header = T, row.names = 1)
dat_pval <- read.table("dat_pvals.two_sided.tsv", header = T, row.names = 1)
dat_rb5 <- read.table("dat_rarefy10000_v2_0.005_rb.tsv", header = T, row.names = 1)
dat_tax <- read.csv("dat_taxonomy.csv")

画图

  • 数据处理

  • 数据可视化

  • 数据存储

cornet_plot <- function(mcor=dat_cor, mpval=dat_pval, mrb=dat_rb5, tax=dat_tax, type="dat_5"){# mcor <- dat_cor# mpval <- dat_pval# mrb <- dat_rb5# tax <- dat_tax# type="dat_05"# trim all NA in pvalue < 0.05mpval[mpval > 0.05] <- NAremain <- apply(mpval, 1, function(x){length(x[!is.na(x)])}) %>% data.frame() %>%setNames("counts") %>%rownames_to_column("OTUID") %>%filter(counts > 0)remain_pval <- mpval[as.character(remain$OTUID), as.character(remain$OTUID)]# remove non significant edges remain_cor <- mcor[as.character(remain$OTUID), as.character(remain$OTUID)]for(i in 1:nrow(remain_pval)){for(j in 1:ncol(remain_pval)){if(is.na(remain_pval[i, j])){remain_cor[i, j] <- 0}}}# OTU relative abundance and taxonomy rb_tax <- mrb %>% rownames_to_column("OTUID") %>%filter(OTUID%in%as.character(remain$OTUID)) %>%group_by(OTUID) %>%rowwise() %>%mutate(SumAbundance=mean(c_across(everything()))) %>%ungroup() %>%inner_join(tax, by="OTUID") %>%dplyr::select("OTUID", "SumAbundance", "Genus") %>%mutate(Genus=gsub("g__Candidatus", "Ca.", Genus),Genus=gsub("_", " ", Genus)) %>%mutate(Genus=factor(as.character(Genus)))# 构建igraph对象igraph <- graph.adjacency(as.matrix(remain_cor), mode="undirected", weighted=TRUE, diag=FALSE)# 去掉孤立点bad.vs <- V(igraph)[degree(igraph) == 0]igraph <- delete.vertices(igraph, bad.vs)# 将igraph weight属性赋值到igraph.weightigraph.weight <- E(igraph)$weight# 做图前去掉igraph的weight权重,因为做图时某些layout会受到其影响E(igraph)$weight <- NAnumber_cor <- paste0("postive correlation=", sum(igraph.weight > 0), "\n","negative correlation=",  sum(igraph.weight < 0))# set edge color,postive correlation 设定为red, negative correlation设定为blueE.color <- igraph.weightE.color <- ifelse(E.color > 0, "red", ifelse(E.color < 0, "blue", "grey"))E(igraph)$color <- as.character(E.color)# 可以设定edge的宽 度set edge width,例如将相关系数与edge width关联E(igraph)$width <- abs(igraph.weight)# set vertices sizeigraph.size <- rb_tax %>% filter(OTUID%in%V(igraph)$name) igraph.size.new <- log((igraph.size$SumAbundance) * 1000000)V(igraph)$size <- igraph.size.new# set vertices colorigraph.col <- rb_tax %>% filter(OTUID%in%V(igraph)$name)pointcolor <- c("green","deeppink","deepskyblue","yellow","brown","pink","gray","cyan","peachpuff")pr <- levels(igraph.col$Genus)pr_color <- pointcolor[1:length(pr)]levels(igraph.col$Genus) <- pr_colorV(igraph)$color <- as.character(igraph.col$Genus)# 按模块着色# fc <- cluster_fast_greedy(igraph, weights=NULL)# modularity <- modularity(igraph, membership(fc))# comps <- membership(fc)# colbar <- rainbow(max(comps))# V(igraph)$color <- colbar[comps]filename <- paste0("../figure/03.Network/", type, "_Sparcc.pdf")pdf(file = filename, width = 10, height = 10)plot(igraph,main="Co-occurrence network",layout=layout_in_circle,edge.lty=1,edge.curved=TRUE,margin=c(0,0,0,0))legend(x=.8, y=-1, bty = "n",legend=pr,fill=pr_color, border=NA)text(x=.3, y=-1.2, labels=number_cor, cex = 1.5)dev.off()# calculate OTU remain_cor_sum <- apply(remain_cor, 1, function(x){res1 <- as.numeric(length(x[x>0]))res2 <- as.numeric(length(x[x<0]))res <- c(res1, res2)}) %>% t() %>% data.frame() %>%setNames(c("Negative", "Positive")) %>%rownames_to_column("OTUID")file_cor <- paste0("../figure/03.Network/", type, "_Sparcc_negpos.csv")write.csv(remain_cor_sum, file = file_cor, row.names = F)
}

运行画图函数

cornet_plot(mcor=dat_cor, mpval=dat_pval, mrb=dat_rb5, tax=dat_tax, type="dat_5")

在这里插入图片描述

R information

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)Matrix products: defaultlocale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    
system code page: 936attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     other attached packages:
[1] psych_2.0.9  igraph_1.2.5 tibble_3.0.3 dplyr_1.0.2 loaded via a namespace (and not attached):[1] lattice_0.20-41  crayon_1.3.4     grid_4.0.2       R6_2.5.0         nlme_3.1-150     lifecycle_0.2.0  magrittr_1.5    [8] pillar_1.4.6     rlang_0.4.7      rstudioapi_0.11  vctrs_0.3.4      generics_0.1.0   ellipsis_0.3.1   tools_4.0.2     
[15] glue_1.4.2       purrr_0.3.4      parallel_4.0.2   xfun_0.19        yaml_2.2.1       compiler_4.0.2   pkgconfig_2.0.3 
[22] mnormt_2.0.2     tmvnsim_1.0-2    knitr_1.30       tidyselect_1.1.0

参考

  1. SparCC3

  2. sparcc.pdf

  3. sparcc tutorial

  4. Co-occurrence网络图在R中的实现

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/685812.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自动驾驶学习2-毫米波雷达

1、简介 1.1 频段 毫米波波长短、频段宽,比较容易实现窄波束,雷达分辨率高,不易受干扰。波长介于1~10mm的电磁波,频率大致范围是30GHz~300GHz 毫米波雷达是测量被测物体相对距离、相对速度、方位的高精度传感器。 车载毫米波雷达主要有24GHz、60GHz、77GHz、79GHz四个频段。 …

Redis数据结构-Dict

1.3 Redis数据结构-Dict 我们知道Redis是一个键值型&#xff08;Key-Value Pair&#xff09;的数据库&#xff0c;我们可以根据键实现快速的增删改查。而键与值的映射关系正是通过Dict来实现的。 Dict由三部分组成&#xff0c;分别是&#xff1a;哈希表&#xff08;DictHashTa…

【InternLM】书生·浦语大模型报名地址

【InternLM】书生浦语大模型报名地址&#xff1a; 扫码报名

药物代谢动力学学习笔记

一、基本概念 二、经典房室模型 三、非线性药物代谢动力学 四、非房室模型 五、药代动力学与药效动力学 六、生物等效性评价 七、生物样品分析方法 基本概念 生物样品&#xff1a;生物机体的全血、血浆、血清、粪便、尿液或其他组织的样品 特异性&#xff0c;specificity&…

cmake调用MSVC编译时报C2071、C2061、C2059、C2449等错误的解决方案

文章目录 1. 问题复述2. 问题原因3. 解决方案 1. 问题复述 项目一直在linux下编译&#xff0c;这次偶然要在windows下使用msvc编译&#xff0c;结果一堆报错&#xff0c;根据报错信息发现语法也没有问题&#xff0c;报错信息如下&#xff1a; 查找文献说是因为有中文注释导致…

Rocketmq如何保证消息不丢失和幂等性

生产者 生产者通过RocketMQ提供的事务消息&#xff08;两阶段提交&#xff09;能保证消息的一致性。 第一阶段给Broker发送一个半事务消息&#xff0c;半事务消息是不能消费的消息&#xff0c;broker已经收到生产者发送的消息&#xff0c;但是并未收到生产者的二次确认&#x…

基于Springboot+Vue+Java的校园资料分享平台

&#x1f49e; 文末获取源码联系 &#x1f649; &#x1f447;&#x1f3fb; 精选专栏推荐收藏订阅 &#x1f447;&#x1f3fb; &#x1f380;《Java 精选实战项目-计算机毕业设计题目推荐-期末大作业》&#x1f618; 更多实战项目~ https://www.yuque.com/liuyixin-rotwn/ei3…

JavaScript使用 BigInt

在 JavaScript 中&#xff0c;最大的安全整数是 2 的 53 次方减 1&#xff0c;即 Number.MAX_SAFE_INTEGER&#xff0c;其值为 9007199254740991。这是因为 JavaScript 中使用双精度浮点数表示数字&#xff0c;双精度浮点数的符号位占 1 位&#xff0c;指数位占 11 位&#xff…

2024洗地机选购指南 | 怎么选洗地机不会被坑?

家里的地板总是需要打扫&#xff0c;但工作忙碌的我们往往没有足够的时间来打理。洗地机不仅能够帮助我们节省宝贵的时间&#xff0c;还能让我们的家变得一尘不染。今天&#xff0c;笔者将为大家讲讲挑选洗地机的技巧&#xff0c;告诉大家怎么挑选洗地机不会被坑&#xff0c;顺…

ArrayList底层结构和源码分析

ArrayList的注意事项 1&#xff0c;permits all, including null, ArrayList 甚至可以加入多个null&#xff0c;并且可放多个 2&#xff0c;ArrayList 是由数组来实现数据存储的 3&#xff0c;ArrayList 基本等同于Vector&#xff0c;除了ArrayList 是线程不安全但执行效率高…

ICode国际青少年编程竞赛- Python-4级训练场-while语句入门

ICode国际青少年编程竞赛- Python-4级训练场-while语句入门 1、 while Flyer.disappear():wait() Dev.step(2)2、 Dev.step(1) while Flyer.disappear():wait() Dev.step(5)3、 while Flyer[0].disappear():wait() Dev.step(3) Dev.step(-1) while Flyer[0].disappear():…

通用产品发布解决方案(家居分类表设计以及renren代码生成器的使用)

文章目录 1.商品分类表设计1.需求分析2.数据库表设计1.数据库sunliving_commodity&#xff0c;商品分类表commodity_category2.测试数据 2.代码生成器生成crud1.解压到sunliving下并聚合管理1.解压2.修改sunliving的pom.xml进行聚合管理3.刷新maven报错 parent.relativePath4.将…