16s α多樣性指數(shù)計算和可視化


Title:

  • 16s多樣性分析和可視化(16s diversity analysis and visualization )

Author:

  • GCC

Date:

  • 2019年4月

1. clean數(shù)據(jù)

加載第三方包

#setup
knitr::opts_chunk$set(warning = FALSE, message = FALSE,tidy=TRUE, tidy.opts = list(blank = TRUE, width.cutoff=23),dpi = 600)
options(stringsAsFactors = FALSE)
#加載第三方包
library(dplyr)
library(ggplot2)
library(reshape)
library(vegan)
library(tidyr)
library(formatR)

導(dǎo)入備注信息和種豐度

#導(dǎo)入數(shù)據(jù)
setwd('C:\\Users\\GCC\\FMT')
annotations <-
        readxl::read_xlsx('./annotation.xlsx') %>% select(., oldID, ID)
species <- read.delim('./種豐度.txt', sep = '\t')

重新標(biāo)注樣本信息

#重新對樣本命名
newNames <- annotations$I
names(newNames) <- annotations$oldID
newNames <- c(FeatureID = 'FeatureID', newNames)
speciesNew <- plyr::rename(species, newNames)
speciesNew <-
        speciesNew[, stringr::str_detect(colnames(speciesNew), pattern = '(negativeControl)|(FeatureID)|(treat)|(wt)|(model).*')]

2. alpha多樣性分析

Simpson指數(shù):Simpson反映的是優(yōu)勢種在群落中的地位和作用,若一個群落中優(yōu)勢種占的多,其他非優(yōu)勢物種所占的比例則會減少,那么Simpson 指數(shù)值較大,這說明群落多樣性較低。
Shannon指數(shù):Shannon值越大,說明群落多樣性越高。

#多樣性分析
set.seed(0408)
H <- diversity(t(speciesNew[, -1]))
#計算Shannon-Wiener指數(shù)
shannon = diversity(t(speciesNew[,-1]), "shannon")
#計算Simpson指數(shù)
simpson = diversity(t(speciesNew[,-1]), "simpson")
#計算Inverse Simpson指數(shù)
insimpson = diversity(t(speciesNew[,-1]), "inv")

pairs(cbind(H, simpson, insimpson), pch = "+", col = "blue")
# Species richness (S) and Pielou's evenness (J):
S <- specnumber(t(speciesNew[, -1])) 
J <- H / log(S)
diversitys <-
        data.frame(shannon = shannon,
                        simpson = simpson,
                        insimpson = insimpson)
diversitys <-
        diversitys[stringr::str_detect(rownames(diversitys), '[^(_)].*'), ]
diversitys <-
        within(diversitys,
        {
        time <-
        stringr::str_split(rownames(diversitys), '_', simplify = T)[, 2] %>% factor(., levels = c(1:10))
        group <-
        stringr::str_extract(rownames(diversitys), '[^(_)]*')
        }) %>% gather(.,
        key = 'index',
        value = 'values',
        c(shannon, simpson, insimpson))

3. 作圖

#index-plotting, fig.align='center', fig.cap = 'the diversity index in different group'
p1 <-
        diversitys %>% ggplot(., aes(time, values, fill = group)) + geom_boxplot(alpha =
        0.8, show.legend = FALSE) + geom_point(
        position = position_jitter(),
        size = 1.5,
        alpha = 0.8,
        aes(color = group),
        show.legend = FALSE
        ) + facet_grid(index ~ group, scales = 'free', labeller = as_labeller(
        c(
        model = 'model',
        negativeControl = 'negative',
        treat = 'treat',
        wt = 'wild type',
        shannon = 'Shannon',
        simpson = 'Simpson',
        insimpson = 'Insimpson'
        )
        )) + labs(x = '', y = 'Alpha Diversity Measure') + theme(
        panel.grid = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(color = 'black', fill = NA)
        ) #+ ggpubr::stat_compare_means(aes(group=interaction(time,group)))

print(p1)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容