library('microbiomeMarker')
library("tidyverse")
library('phyloseq')
library('magrittr')
otu <- read.csv("merged_otutable.csv",header = TRUE,row.names = 1)
otu %<>% as.matrix()
otu %>% head()
env <- read.csv("metadata.csv",header = TRUE,row.names = 1)
tax <- read.csv("merged_taxonomy.csv",header = TRUE,row.names = 1)
tax %<>% as.matrix()
physeq <- phyloseq(
otu_table(otu,taxa_are_rows = TRUE),
tax_table(tax), # 数据格式必须是矩阵,否则会改变行列名
sample_data(env)
)
physeq
adj <- tax_table(physeq) %>% apply(.,2,function(x) length(unique(x)))
lefse <- run_lefse(
physeq,
norm = "CPM",
group = "species",
multigrp_strat = TRUE,
wilcoxon_cutoff = 0.05, # 筛选阈值根据自己数据设置。
kw_cutoff = 0.05/adj, # 筛选阈值根据自己数据设置,此处进行bonferroni校正。
bootstrap_n = 50, # LDA的bootstrap迭代次数。
lda_cutoff = 2 # 筛选阈值根据自己数据设置。
)
lefse %>% marker_table() -> res.diff
#write.csv(res.diff,"bacspe.diff.csv",quote = FALSE)
cols <- RColorBrewer::brewer.pal(2, "Dark2")
pdf("bac_spe_lefse_cladogram.pdf",
width = unit(16,"cm"),
height = unit(16,"cm"),
family="Times")
plot_cladogram(
lefse,
# 颜色数需要与enrich_group分类水平数保持一致。#
color = cols[seq_along(res.diff$enrich_group %>% unique)],
only_marker = TRUE,# FALSE图中展示所有数据,TRUE表示只展示差异显著数据。
branch_size = 0.2, # 分支粗细
alpha = 0.2,# 阴影颜色透明度
node_size_scale = 1, # 节点大小
node_size_offset = 1.1,
clade_label_level = 7, # 添加分支标签的最大分类单元,其余分类单元注释作为图例,数值越大代表的分类单元越高。
clade_label_font_size = 2,# 分支标签大小
annotation_shape = 22,
annotation_shape_size = 2,
marker_legend_param = list(
ncol = 2, # 图例排2列。
direction = "horizontal" # 水平放置。
)
) +
theme(
plot.margin = margin(0, 0, 0, 0),
legend.position = "bottom"
)
dev.off()
Error in if (n <= length(x)) { : missing value where TRUE/FALSE needed
group=2时出现的error,n>3时不会出现。曾阴差阳错解决过,找不回来方法了,求解决方法