-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisd_crete_differential_abundance.R
executable file
·149 lines (137 loc) · 7.03 KB
/
isd_crete_differential_abundance.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env Rscript
###############################################################################
# script name: isd_crete_compositionality.R
# developed by: Savvas Paragkamian
# framework: ISD Crete
###############################################################################
# GOAL:
# Aim of this script is to normalise the reads of each sample biodiversity using
# the ANCOMBC methodology
#
###############################################################################
# 75 in minutes
# OUTPUT: RDS files of each run
###############################################################################
# usage:./scripts/isd_crete_compositionality.R
###############################################################################
library(magrittr)
library(dplyr)
library(tibble)
library(readr)
library(tidyr)
library(mia)
library(ANCOMBC)
library(phyloseq)
community_matrix_l <- read_delim("results/community_matrix_l.tsv",delim="\t")
community_matrix <- readRDS("results/community_matrix.RDS") |> as.matrix() |> t()
tax_tab <- readRDS("results/tax_tab.RDS")
results <- readRDS("results/ancombc2_results.RDS")
metadata <- read_delim("results/sample_metadata.tsv", delim="\t")
tax_tab1 <- community_matrix_l %>%
distinct(scientificName, Kingdom, Phylum, Class, Order, Family, Genus, Species) %>%
as.matrix()
tax_tab <- tax_tab1[,-1]
rownames(tax_tab) <- tax_tab1[,1]
metadata <- metadata %>% filter(ENA_RUN %in% colnames(community_matrix))
# ancombc analysis
assays = S4Vectors::SimpleList(counts = community_matrix)
smd = S4Vectors::DataFrame(metadata)
tax_tab = S4Vectors::DataFrame(tax_tab)
#otu_mat = matrix(sample(1:100, 100, replace = TRUE), nrow = 10, ncol = 10)
#rownames(otu_mat) = paste0("taxon", 1:nrow(otu_mat))
#colnames(otu_mat) = paste0("sample", 1:ncol(otu_mat))
#assays = SimpleList(counts = otu_mat)
## create TSE
tse = TreeSummarizedExperiment::TreeSummarizedExperiment(assays = assays, colData = smd)
set.seed(123)
print("Starting ancombc2 for Label2")
output_label2 = ancombc2(data = tse, assay_name = "counts", tax_level = NULL,
fix_formula = "LABEL2", rand_formula = NULL,
p_adj_method = "holm", pseudo_sens = TRUE,
prv_cut = 0.10, lib_cut = 1000, s0_perc = 0.05,
group = "LABEL2", struc_zero = FALSE, neg_lb = FALSE,
alpha = 0.05, n_cl = 2, verbose = TRUE,
global = FALSE, pairwise = TRUE,
dunnet = FALSE, trend = FALSE,
iter_control = list(tol = 1e-5, max_iter = 20,
verbose = FALSE),
em_control = list(tol = 1e-5, max_iter = 100),
lme_control = NULL,
mdfdr_control = list(fwer_ctrl_method = "holm", B = 100),
trend_control = NULL)
saveRDS(output_label2, "results/ancombc2_label2_results.RDS")
#print("Starting ancombc2 for bioclim")
#output_clim = ancombc2(data = tse, assay_name = "counts", tax_level = NULL,
# fix_formula = "elevation_bin + bio_1 + bio_12 + elevation", rand_formula = NULL,
# p_adj_method = "holm", pseudo_sens = TRUE,
# prv_cut = 0.10, lib_cut = 1000, s0_perc = 0.05,
# group = "elevation_bin", struc_zero = FALSE, neg_lb = FALSE,
# alpha = 0.05, n_cl = 2, verbose = TRUE,
# global = FALSE, pairwise = TRUE,
# dunnet = FALSE, trend = FALSE,
# iter_control = list(tol = 1e-5, max_iter = 20,
# verbose = FALSE),
# em_control = list(tol = 1e-5, max_iter = 100),
# lme_control = NULL,
# mdfdr_control = list(fwer_ctrl_method = "holm", B = 100),
# trend_control = NULL)
#
#saveRDS(output_clim, "results/ancombc2_results_clim.RDS")
#
#print("Starting ancombc2 for metadata")
#output_metadata = ancombc2(data = tse, assay_name = "counts", tax_level = NULL,
# fix_formula = "elevation_bin + total_nitrogen + water_content + total_organic_carbon + carbon_nitrogen_ratio", rand_formula = NULL,
# p_adj_method = "holm", pseudo_sens = TRUE,
# prv_cut = 0.10, lib_cut = 1000, s0_perc = 0.05,
# group = "elevation_bin", struc_zero = FALSE, neg_lb = FALSE,
# alpha = 0.05, n_cl = 2, verbose = TRUE,
# global = FALSE, pairwise = TRUE,
# dunnet = FALSE, trend = FALSE,
# iter_control = list(tol = 1e-5, max_iter = 20,
# verbose = FALSE),
# em_control = list(tol = 1e-5, max_iter = 100),
# lme_control = NULL,
# mdfdr_control = list(fwer_ctrl_method = "holm", B = 100),
# trend_control = NULL)
#
#saveRDS(output_metadata, "results/ancombc2_results_metadata.RDS")
#
### geology
#print("Starting ancombc2 for geology")
#
#output_geology = ancombc2(data = tse, assay_name = "counts", tax_level = NULL,
# fix_formula = "geology_na + total_nitrogen", rand_formula = NULL,
# p_adj_method = "holm", pseudo_sens = TRUE,
# prv_cut = 0.10, lib_cut = 1000, s0_perc = 0.05,
# group = "geology_na", struc_zero = FALSE, neg_lb = FALSE,
# alpha = 0.05, n_cl = 2, verbose = TRUE,
# global = FALSE, pairwise = TRUE,
# dunnet = FALSE, trend = FALSE,
# iter_control = list(tol = 1e-5, max_iter = 20,
# verbose = FALSE),
# em_control = list(tol = 1e-5, max_iter = 100),
# lme_control = NULL,
# mdfdr_control = list(fwer_ctrl_method = "holm", B = 100),
# trend_control = NULL)
#
#saveRDS(output_geology, "results/ancombc2_geology_results.RDS")
#
############################## results ##################################
#
#output_label2 <- readRDS( "results/ancombc2_label2_results.RDS")
#
significant_label2 <- filter(output_label2$res,if_any(where(is.logical),~ . =="TRUE"))
write_delim(output_label2$res, "results/ancombc2_label2_results_only.tsv", delim="\t")
write_delim(significant_label2, "results/ancombc2_significant_label2.tsv", delim="\t")
#
#output_geology<- readRDS( "results/ancombc2_geology_results.RDS")
#significant_geology <- filter(output_geology$res,if_any(where(is.logical),~ . =="TRUE"))
#write_delim(output_geology$res, "results/ancombc2_geology_results_only.tsv", delim="\t")
#
#write_delim(significant_geology, "results/ancombc2_significant_geology.tsv", delim="\t")
#output_metadata <- readRDS("results/ancombc2_results_metadata.RDS")
#significant_metadata <- filter(output_metadata$res,if_any(where(is.logical),~ . =="TRUE"))
#write_delim(significant_metadata, "results/ancombc2_significant_metadata.tsv", delim="\t")
#output_clim <- readRDS("results/ancombc2_results_clim.RDS")
#significant_clim <- filter(output_clim$res,if_any(where(is.logical),~ . =="TRUE"))
#write_delim(significant_clim, "results/ancombc2_significant_clim.tsv", delim="\t")