-
Notifications
You must be signed in to change notification settings - Fork 0
/
maaslin3_lite.R
230 lines (194 loc) · 8.7 KB
/
maaslin3_lite.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/Rscript
library(optparse)
library(maaslin3)
library(dplyr)
library(ggplot2)
# Define the command line options
option_list <- list(
make_option(c("-i", "--input"), type = "character", default = NULL,
help = "Path to input txt file", metavar = "character"),
make_option(c("-n", "--normalize"), type = "logical", default = FALSE,
help = "Whether to normalize the data (TRUE for TSS, FALSE for NONE)", metavar = "logical"),
make_option(c("-c", "--class"), type = "character", default = "oxygen_availability",
help = "Class for the fixed effect in the formula", metavar = "character"),
make_option(c("-s", "--subclass"), type = "character",
help = "Subclass for the fixed effect in the formula", metavar = "character"),
make_option(c("-r", "--random_component"), type = "character", default = "subject_id",
help = "Random component for the formula (e.g., subject_id)", metavar = "character"),
make_option(c("-a", "--alpha_threshold"), type = "character", default = 0.1,
help = "Maximum FDR corrected significance level", metavar = "numeric")
)
# Parse the command line options
opt_parser <- OptionParser(option_list = option_list)
opt <- parse_args(opt_parser)
if (is.null(opt$input)) {
print_help(opt_parser)
stop("Input file must be provided", call. = FALSE)
}
run_maaslin_analysis <- function(input_file, normalize, class, subclass, random_component, alpha_threshold) {
# Read input data
taxa_table <- read.csv(input_file, sep = '\t', header = F)
# Metadata setup
metadata <- t(taxa_table[1:3,])
colnames(metadata) <- metadata[1,]
metadata <- metadata[-1,]
rownames(metadata) <- paste0("Sample", 1:nrow(metadata))
metadata <- data.frame(metadata)
metadata[[class]] <- factor(metadata[[class]])
metadata[[subclass]] <- factor(metadata[[subclass]])
# Process taxa table
taxa_table <- taxa_table[-c(1:3),]
rownames_taxa_table <- taxa_table[,1]
taxa_table <- apply(taxa_table[,-1], 2, function(x){x <- as.numeric(x); x <- ifelse(x == min(x), 0, x)})
rownames(taxa_table) <- rownames_taxa_table
colnames(taxa_table) <- paste0("Sample", 1:nrow(metadata))
taxa_table <- data.frame(taxa_table)
# Set normalization method based on user input
normalization_method <- ifelse(normalize, 'TSS', 'NONE')
# Create formula dynamically based on user input
if (is.null(subclass)) {
formula_str <- paste0("~ ", class, " + (1 | ", random_component, ")")
} else {
formula_str <- paste0("~ ", class, " + ", subclass, " + (1 | ", random_component, ")")
}
# Run Maaslin3 analysis
fit_out <- maaslin3(input_data = taxa_table,
input_metadata = metadata,
min_abundance = 0,
min_prevalence = 0,
output = 'output/',
min_variance = 0,
normalization = normalization_method,
transform = 'LOG',
formula = formula_str,
plot_associations = FALSE,
save_models = FALSE,
plot_summary_plot = F,
max_significance = alpha_threshold,
subtract_median = TRUE,
augment = TRUE,
cores = 1)
# Save results in LEfSe format
maaslin_write_results_lefse_format('output', fit_out$fit_data_abundance, fit_out$fit_data_prevalence)
return(fit_out)
}
run_make_coef_plot <- function(merged_results_sig,
max_significance,
class) {
coef_plot_vars <- class
coef_plot_data <- merged_results_sig[merged_results_sig$metadata %in% coef_plot_vars,]
# Limit plotted coefficients to median +/- 10 times distance to quartiles
quantile_df <- coef_plot_data %>%
dplyr::group_by(.data$full_metadata_name) %>%
dplyr::summarise(
lower_q = median(.data$coef) - 10 *
(median(.data$coef) - quantile(.data$coef, 0.25)),
upper_q = median(.data$coef) + 10 *
(quantile(.data$coef, 0.75) - median(.data$coef))
) %>%
data.frame()
rownames(quantile_df) <- quantile_df$full_metadata_name
# Make sure insignificant coefficients don't distort the plot
coef_plot_data <-
coef_plot_data[coef_plot_data$qval_individual <
max_significance |
(coef_plot_data$coef > quantile_df[
coef_plot_data$full_metadata_name,
'lower_q'] &
coef_plot_data$coef < quantile_df[
coef_plot_data$full_metadata_name,
'upper_q']),]
# Choose breaks for plot
custom_break_fun <- function(n) {
return(function(x) {
extended_breaks <- scales::breaks_extended(n)(x)
if (max(x) > 0) {
extended_breaks <- extended_breaks[
extended_breaks <= max(x) * 0.9]
} else {
extended_breaks <- extended_breaks[
extended_breaks <= max(x) * 1.1]
}
if (min(x) > 0) {
extended_breaks <- extended_breaks[
extended_breaks >= min(x) * 1.1]
} else {
extended_breaks <- extended_breaks[
extended_breaks >= min(x) * 0.9]
}
extended_breaks
})
}
# Plot
p1 <- ggplot(coef_plot_data, aes(x = feature, y = coef, fill = value, alpha = model)) +
geom_bar(stat = "identity",
position = position_dodge(width = 0.8),
width = 0.7) +
geom_errorbar(aes(ymin = coef - stderr, ymax = coef + stderr),
position = position_dodge(width = 0.8),
width = 0.25) +
scale_fill_brewer(palette = "Dark2") +
labs(x = "Feature", y = "Coefficient", fill = "Class", alpha = "Model") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(panel.spacing.x = unit(0.5, "lines")) +
coord_flip() +
facet_wrap(
~ value,
scales = 'free_x',
ncol = length(coef_plot_vars)
) +
scale_alpha_manual(values = c('Abundance' = 1, 'Prevalence' = 0.5)) +
scale_y_continuous(
breaks = custom_break_fun(n = 6),
limits = c(
min(coef_plot_data$coef) -
quantile(coef_plot_data$stderr, 0.8),
max(coef_plot_data$coef) +
quantile(coef_plot_data$stderr, 0.8)
)
)
return(p1)
}
run_maaslin_plotting <- function(fit_out, class, alpha_threshold, first_n = 20) {
fit_data_abundance <- fit_out$fit_data_abundance
fit_data_prevalence <- fit_out$fit_data_prevalence
if (is.null(fit_data_abundance$results)) {
merged_results <- fit_data_prevalence$results
} else if (is.null(fit_data_prevalence$results)) {
merged_results <- fit_data_abundance$results
} else {
merged_results <- rbind(fit_data_abundance$results,
fit_data_prevalence$results)
}
merged_results <- maaslin3:::preprocess_merged_results(merged_results)
# Subset associations for plotting
merged_results_joint_only <-
unique(merged_results[, c('feature', 'qval_joint')])
merged_results_joint_only <-
merged_results_joint_only[
order(merged_results_joint_only$qval_joint),]
if (length(unique(merged_results_joint_only$feature)) < first_n) {
first_n <- length(unique(merged_results_joint_only$feature))
}
signif_taxa <-
unique(merged_results_joint_only$feature)[seq(first_n)]
merged_results_sig <- merged_results %>%
dplyr::filter(.data$feature %in% signif_taxa)
p1 <- run_make_coef_plot(merged_results_sig = merged_results_sig,
max_significance = alpha_threshold,
class = class)
height_out <-
5.5 + max(first_n / 5 - 5, 0) + nchar(as.character(class)) / 10
width_out <-
2 + max(nchar(merged_results$feature)) / 15
ggplot2::ggsave(
filename = 'output/summary_plot.png',
plot = p1,
dpi = 600,
width = width_out,
height = height_out)
}
# Run the analysis with the provided options
fit_out <- run_maaslin_analysis(opt$input, opt$normalize, opt$class, opt$subclass, opt$random_component, as.numeric(opt$alpha_threshold))
run_maaslin_plotting(fit_out, opt$class, as.numeric(opt$alpha_threshold), first_n = 20)