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

news2024/10/5 15:24:32

介绍

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

安装Sparcc软件

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

which 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.005
  
  dat <- 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")
  
  # count
  count <- 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 abundance
  relative <- 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.05
  mpval[mpval > 0.05] <- NA
  remain <- 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.weight
  igraph.weight <- E(igraph)$weight
  
  # 做图前去掉igraph的weight权重,因为做图时某些layout会受到其影响
  E(igraph)$weight <- NA
  
  
  number_cor <- paste0("postive correlation=", sum(igraph.weight > 0), "\n",
                       "negative correlation=",  sum(igraph.weight < 0))
  
  # set edge color,postive correlation 设定为red, negative correlation设定为blue
  E.color <- igraph.weight
  E.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 size
  igraph.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 color
  igraph.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_color
  V(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: default

locale:
[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: 936

attached 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.coloradmin.cn/o/1660928.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

css--控制滚动条的显示位置

各种学习后的知识点整理归纳&#xff0c;非原创&#xff01; ① direction属性 滚动条在左侧显示② transform:scaleY() 滚动条在上侧显示 正常的滚动条会在内容超出规定的范围后在区域右侧和下侧显示在有些不正常的需求下会希望滚动条在上侧和左侧显示自己没有想到好的解决方案…

k8s部署最新版zookeeper集群(3.9.2),并配置prometheus监控

目录 zookeeper集群部署创建zookeeper文件夹namespace.yamlscripts-configmap.yamlserviceaccount.yamlstatefulset.yamlsvc-headless.yamlsvc.yamlmetrics-svc.yaml执行部署 接入prometheus访问prometheus查看接入情况导入zookeeper监控模版监控展示 zookeeper集群部署 复制粘…

YOLOv5入门(五)训练自己的目标检测模型

前言 通过前面几篇文章&#xff0c;已经完成数据集制作和环境配置&#xff08;服务器&#xff09;&#xff0c;接下来将继续实践如何开始训练自己数据集~ 往期回顾 YOLOv5入门&#xff08;一&#xff09;利用Labelimg标注自己数据集 YOLOv5入门&#xff08;二&#xff09;处…

地球行星UE5和UE4

地球行星&#xff0c;包含多种地球风格&#xff0c;可蓝图控制自转和停止&#xff0c;可材质自转. 支持版本4.21-5.4版本 下载位置&#xff1a;https://mbd.pub/o/bread/ZpWZm5lv b站工坊&#xff1a;https://gf.bilibili.com/item/detail/1105582041 _______________________…

C++进阶 | [3] 搜索二叉树

摘要&#xff1a;什么是搜索二叉树&#xff0c;实现搜索二叉树&#xff08;及递归版本&#xff09; 什么是搜索二叉树 搜索二叉树/二叉排序树/二叉查找树BST&#xff08;Binary Search Tree&#xff09;&#xff1a;特征——左小右大&#xff08;不允许重复值&#xff09;。即…

【项目实战】使用Github pages、Hexo如何10分钟内快速生成个人博客网站

文章目录 一.准备工作1.安装git2.安装node安装 cnpm 3.使用 GitHub 创建仓库&#xff0c;并配置 GitHub Pages0.Github Pages是什么1. 在 GitHub 上创建一个新仓库2. 创建您的静态网站3. 启用 GitHub Pages4. 等待构建完成5. 访问您的网站 二. Hexo1.什么是Hexo2.安装Hexo1. 安…

RAG查询改写方法概述

在RAG系统中&#xff0c;用户的查询是丰富多样的&#xff0c;可能存在措辞不准确和缺乏语义信息的问题。这导致使用原始的查询可能无法有效检索到目标文档。 因此&#xff0c;将用户查询的语义空间与文档的语义空间对齐至关重要&#xff0c;目前主要有查询改写和嵌入转换两种方…

QT创造一个新的类(柱状图的类),并关联属性和方法

1.以在UI上添加柱状图的类为例&#xff08;Histogram&#xff09; #ifndef STUDY_HISTOGRAM_H #define STUDY_HISTOGRAM_H#include <QVector> #include <QWidget>// 前向声明 QT_BEGIN_NAMESPACE class QColor; class QRect; class QString; class QPaintDevice; …

适用于 macOS 的最佳独立 HBO Max 客户端

适用于 macOS 的最佳独立 HBO Max 应用程序。不再在浏览器选项卡之间切换。只需直接从 Dock 启动 Clicker for HBO Max 即可开始狂欢。 HBO Max 客户端 Clicker for HBO Max 下载 Clicker for HBO Max mac版安装教程 软件下载完成后&#xff0c;双击pkg根据提示进行安装 Clic…

27、Qt自定义标题栏

一、说明 QtWidget及其子类有默认的标题栏&#xff0c;但是这个标题栏不能美化&#xff0c;有时候满足不了我们的使用需求&#xff0c;所以进行自定义标题栏 二、下载图标 在下面的链接中下载两种颜色的最大化、向下还原、最大化和关闭八个图片&#xff0c;并找一张当做图标…

c++opencv Project3 - License Plate Detector

俄罗斯车牌识别案例&#xff1a;实时识别车牌&#xff0c;并且读取到指定文件夹中。 惯例先展示结果图&#xff1a; 对于摄像头读取图片进行车牌匹配&#xff0c;原理和人脸识别其实是一致的。 利用训练好的模型进行匹配即可。可参考&#xff1a; 对视频实现人脸识别-CSDN博…

MySQL索引(聚簇索引、非聚簇索引)

了解MySQL索引详细&#xff0c;本文只做整理归纳&#xff1a;https://blog.csdn.net/wangfeijiu/article/details/113409719 概念 索引是对数据库表中一列或多列的值进行排序的一种结构&#xff0c;使用索引可快速访问数据库表中的特定信息。 索引分类 主键索引&#xff1a…

drawio 网页版二次开发(1):源码下载和环境搭建

目录 一 说明 二 源码地址以及下载 三 开发环境搭建 1. 前端工程地址 2. 配置开发环境 &#xff08;1&#xff09;安装 node.js &#xff08;2&#xff09;安装 serve 服务器 3. 运行 四 最后 一 说明 应公司项目要求&#xff0c;需要对drawio进行二次开发&…

Redis学习1——redis简介、基础

介绍 redis简介 Redis(Remote Dictonary Server) 是由Salvatore Sanfilippo开发的key-value缓存数据库&#xff0c;基于C语言开发。目前市面上&#xff0c;Redis和MongoDB是当前使用最广泛的NoSQL&#xff0c;而就Redis技术而言&#xff0c;它的性能十分优越&#xff0c;可以…

HackMyVM-Animetronic

目录 信息收集 arp nmap nikto whatweb WEB web信息收集 feroxbuster steghide exiftool hydra ssh连接 提权 系统信息收集 socat提权 信息收集 arp ┌──(root㉿0x00)-[~/HackMyVM] └─# arp-scan -l Interface: eth0, type: EN10MB, MAC: 08:00:27:9d:6d:7…

jenkins部署想定报错

报错&#xff1a; 解决办法&#xff1a; 登录被编译的设备&#xff0c;清楚旧代码&#xff0c;在重新执行

burp靶场xss漏洞(初级篇)

靶场地址 http://portswigger.net/web-security/all-labs#cross-site-scripting 第一关&#xff1a;反射型 1.发现搜索框直接注入payload <script>alert(111)</script> ​ 2.出现弹窗即说明攻击成功 ​ 第二关&#xff1a;存储型 1.需要在评论里插入payload …

object

object.clone() 在 Java 中&#xff0c;Object.clone() 方法执行的是浅拷贝&#xff08;shallow copy&#xff09;&#xff0c;而不是深拷贝&#xff08;deep copy&#xff09;。 浅拷贝&#xff08;Shallow Copy&#xff09;&#xff1a; 浅拷贝是指在拷贝对象时&#xff0…

Al Agent:开启智能化未来的关键角色,让机器更智能的为我们服务

文章目录 &#x1f680;Al Agent是什么&#x1f4d5;Al Agent的工作原理与技术&#x1f4aa;Al Agent应用领域&#x1f680;智能家居应用&#x1f308;医疗健康领域⭐金融服务行业&#x1f302;交通运输管理&#x1f3ac;教育培训应用 &#x1f512;Al Agent优势与挑战✊Al Age…