写在前面

因为我们测定的样本不总是可以匹配上的,但是最少的样本也不能太少,我们测定的三个样本做相关其实还是被质疑,但是测定了5或者6个重复,这个时候直接将样本比较多的样本过滤掉比较少的样本不就可以了吗?这样做一次是没有什么意义的,但是如果将这个过程重复多次,让每个重复都使用的上,就会大大增加结果的可行性。当然审稿人就可以承认了。

例如下面这个文章

参考原文 ismej Metabolic regulation of the maize rhizobiome by benzoxazinoids

作者信息 :T. E. Anne Cotton1,2 ● Pierre Pétriacq1,2,3,5 ● Duncan D. Cameron1,2 ● Moaed Al Meselmani1,2,4 ● Roland Schwarzenbacher1,2 ● Stephen A. Rolfe 1,2 ● Jurriaan Ton 1,2

发表:2019年

作者亲自书写:

The correlations are calcuated on an average (as the data are not paired) but this will give a false sense of precision in some interactions. One way to cope with this is to randomly assign ions to specific OTU counts and calculate the correlation. If we do this multiple times and then take the average (and SD) we have a better measure of the true correlation.

To see what occurs by chance we can assign the ions randomly across all of the samples rather than just within a treatment. Some false positives will occur by chance but we can then assess whether we get more in specific interactions.

Algorithm

8 bacterial samples

6 metabolomic samples

Within a treatment assign 6 of the metabolomic samples at random to the bacterial samples

Calculate the correlation coefficient

Repeat this process x times and calculate the mean value

那么这个过程是如何实现的呢,下面我带大家一起来实现这个过程:

构建随机OTU矩阵

library(tidyverse)
library(vegan)
library (plyr)
# 构建OTU表格
otu<-matrix(sample(1:500, 100),ncol=16,nrow = 300)
row.names(otu) = paste("ASV_",1:nrow(otu),sep = "")
colnames(otu) = c(paste("A",1:4,sep = ""),paste("B",1:4,sep = ""),paste("C",1:4,sep = ""),paste("D",1:4,sep = ""))
head(otu)
##        A1  A2  A3  A4  B1  B2  B3  B4  C1  C2  C3  C4  D1  D2  D3  D4
## ASV_1  42  42  42  42  42  42  42  42  42  42  42  42  42  42  42  42
## ASV_2 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259
## ASV_3 407 407 407 407 407 407 407 407 407 407 407 407 407 407 407 407
## ASV_4 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272
## ASV_5 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209
## ASV_6 304 304 304 304 304 304 304 304 304 304 304 304 304 304 304 304
#--相对丰度标准化
otunorm = t(t(otu)/colSums(otu))
otunorm = as.data.frame(t(otunorm))otunorm$Group = c(rep("A",4),rep("B",4),rep("C",4),rep("D",4))

构建代谢组随机矩阵

代谢组表格和otu表格处理相同,但是有的重复为3个,有的重复为5个,不仅仅自己不统一重复数量,同时和otu的重复数量也不一致。

# 构建代谢组矩阵
#GC<-matrix(sample(1:500, 100),ncol=16,nrow = 100)
row.names(GC) = paste("GC_",1:nrow(GC),sep = "")
colnames(GC) = c(paste("A",1:3,sep = ""),paste("B",1:5,sep = ""),paste("C",1:3,sep = ""),paste("D",1:5,sep = ""))
head(GC)
##       A1  A2  A3  B1  B2  B3  B4  B5  C1  C2  C3  D1  D2  D3  D4  D5
## GC_1 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186
## GC_2 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171
## GC_3  50  50  50  50  50  50  50  50  50  50  50  50  50  50  50  50
## GC_4 321 321 321 321 321 321 321 321 321 321 321 321 321 321 321 321
## GC_5 419 419 419 419 419 419 419 419 419 419 419 419 419 419 419 419
## GC_6 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199
#--相对丰度标准化
GCnorm = t(t(GC)/colSums(GC))
# mapGC = data.frame(ID =c(paste("A",1:3,sep = ""),paste("B",1:5,sep = ""),paste("C",1:3,sep = ""),paste("D",1:5,sep = "")),
#                     Group = c(rep("A",3),rep("B",5),rep("C",3),rep("D",5)))
GCnorm = as.data.frame(t(GCnorm))GCnorm$Group = c(rep("A",3),rep("B",5),rep("C",3),rep("D",5))

提取各自的重复数量

#make the code robust to allow different sample depths
otu_reps<-data.frame(dplyr::count(otunorm,Group))
met_reps<-data.frame(dplyr::count(GCnorm,Group))
#get the minimum number
otu_min_reps<-min(otu_reps$n)
otu_min_reps
## [1] 4
met_min_reps<-min(met_reps$n)
met_min_reps
## [1] 3
min_reps=min(otu_min_reps,met_min_reps)
min_reps
## [1] 3

准备运行参数

相关参数和运行结果存储对象,顺便做一个进度条

depth=min_reps
repeats=10
cormethod = "spearman"
pb <- txtProgressBar(min=1,max=repeats,style=3,width=50)cor_list<-vector("list",repeats)
a = 1
R = c()
p = c()
for (a in 1:repeats){Sys.sleep(0.1)setTxtProgressBar(pb,a)#this has two effects  - it randomises the order and reduces the sample number to depthotu_select<-data.frame(otunorm %>% group_by(Group) %>% sample_n(depth))otu_select$Group = NULLdistotu <- vegan::vegdist(otu_select) met_select<-data.frame(GCnorm %>% group_by(Group) %>% sample_n(depth))met_select$Group = NULLdistGC <- vegan::vegdist(otu_select,"euclid") res <- vegan::mantel(distotu, distGC, method="spearman")p[a] = res$signifR[a] = res$statistic
}
## |                                                        |                                                  |   0%
result = data.frame(R_mean = mean(R),R_sd = sd(R),p_mean = mean(p),p_sd = sd(p))

这里的R值和p值都是NA,因为通过随机矩阵并没有差异,所以距离矩阵都是0,使得两个模拟数据不存在相关或者差异。

result
##   R_mean R_sd p_mean p_sd
## 1     NA   NA     NA   NA

往期精品(点击图片直达文字对应教程)

机器学习

后台回复“生信宝典福利第一波”或点击阅读原文获取教程合集

硬核干货:如果样本量不一一样多,或者不是一一对应关系,如何做差异?相关?...相关推荐

  1. e站app里站hosts_硬核干货区 | E站的国际站运营知识星球上线啦

    朋友们大家好,E站已上线了知识星球内容库和问答圈,以阿里国际站,Wordpress自建站,Google SEO的更新内容为主,用原理技术的视角+全栈运营的思维去加成对店铺的运营和优化,知道为什么要这样 ...

  2. 硬核干货:网易云音乐如何做产品创新

    网易云音乐是大家非常熟悉和喜欢的一款产品,作为一个音乐类应用,成功融合进了社交等元素,满足了多样化的用户需求.那么今天产品系就带大家揭秘网易云音乐是怎样做产品的,而揭秘者正是网易云音乐的产品总监沈博文 ...

  3. 【硬核干货】4500字、10个案例分享几个Python可视化小技巧,助你绘制高质量图表...

    大家好,这里是俊欣,又是新的一周,好吧,打工人真的是太苦了 一般在Python当中,我们用于绘制图表的模块最基础的可能就是matplotlib了,今天小编分享几个用该模块进行可视化制作的技巧,帮助你绘 ...

  4. 硬核干货来了!手把手教你实现热力图!

    以下内容转载自腾讯位置服务公众号的文章<硬核干货来了!鹅厂前端工程师手把手教你实现热力图!> 作者:腾讯位置服务 链接:https://mp.weixin.qq.com/s/bgS7uFl ...

  5. mysql long类型_怒肝两个月MySQL源码,我总结出这篇2W字的MySQL协议详解(超硬核干货)!!...

    点击上方蓝色"冰河技术",关注并选择"设为星标" 持之以恒,贵在坚持,每天进步一点点! 作者个人研发的在高并发场景下,提供的简单.稳定.可扩展的延迟消息队列框架 ...

  6. 硬核干货合集!500+篇Java干货技术文章整理|资源|书单|工具|面试指南|强烈建议打开!

    今天给大家推荐一位在阿里做Java的朋友给大家,他是公众号[程序员书单]的作者黄小斜. 他的公众号[程序员书单]这两年来累积了200多篇优质原创文章,独家原创的系列文章有<五分钟学编程>系 ...

  7. 程序员必须掌握的 CPU 硬核干货!

    作者 | cxuan 责编 | maozz 大家都是程序员,大家都是和计算机打交道的程序员,大家都是和计算机中软件硬件打交道的程序员,大家都是和CPU打交道的程序员,所以,不管你是玩儿硬件的还是做软件 ...

  8. 硬核干货Java集合详解

    目录 一.问题是最好的老师 二.集合的由来 三.数组存在的问题 四.数组和集合的区别? 五.集合是什么? 六.集合整体架构图 七.集合架构图详解 1.Collection 2.List ArrayLi ...

  9. 信通院AI白皮书:硬核干货一文打尽,从技术流派到应用趋势【附下载】

    来源:智东西 摘要:从产业发展的角度,分析AI技术现状.问题以及趋势,盘点智能语音.语义理解.计算机视觉等相关应用. 自2016年AlphaGo击败李世石之后,人工智能(AI)这个再度翻红的科技热词已 ...

最新文章

  1. 脑机接口成唯一沟通方式,渐冻症晚期父亲终向4岁儿子表达爱意
  2. pycharm快捷键不能用了
  3. 这群程序员疯了!他们想成为IT界最会带货的男人
  4. Spring MVC 安全示例
  5. Odoo10参考系列--混合而有用的类
  6. C#将数据库图片显示在pictureBox
  7. Machine Learning - IV. Linear Regression with Multiple Variables多变量线性规划 (Week 2)
  8. IDEA汉化,中文包和汉化包以及中文版jar下载(更新了2018-2018.2.3版本)
  9. Aria2Gee 教程
  10. 前端vue使用ECharts如何制作精美统计图
  11. 操作系统 第七章 文件管理
  12. 有关爬虫加载Ajax数据或请求json数据集的(快速高效)方法
  13. 干货分享 | 最新机器学习视频教程与数据集下载(持续更新......)
  14. 2019CSP初赛基础知识整理
  15. html多行多列的表单,如何制作多行多列的表格
  16. ”核高基“培育”外国种“(COM)究竟是谁的责任?
  17. WebRTC ADM 源码流程分析
  18. layui框架的使用
  19. 通达OA 2016系统连接ORACLE 11g数据库(图文)
  20. c语言中 字母对应的数值,c语言字母对应ascii码 实型数据,与字符型数据

热门文章

  1. 【操作系统】分页内存管理
  2. 【JavaScript】Canvas绘图整理
  3. 怎样成为一个好的技术领导者
  4. 区块链技术公司谈找到合适的激励机制
  5. 大数据时代最值得关注的15大技术趋势
  6. 分享一个关于网站适应性的解决方案
  7. 苹果新技术或让无线充电更便捷
  8. string类的写时拷贝
  9. java你可能不知道的事(2)--堆和栈
  10. 寂寞的hasLayout