Mercurial > repos > iuc > ancombc
annotate ancombc.R @ 1:6fbedba4d983 draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 04e758f00aa0613ea0b6996813126c4001da070b
author | iuc |
---|---|
date | Wed, 01 Nov 2023 08:48:16 +0000 |
parents | 939c59ab61cf |
children |
rev | line source |
---|---|
0
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
1 #!/usr/bin/env Rscript |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
2 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
3 suppressPackageStartupMessages(library("ANCOMBC")) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
4 suppressPackageStartupMessages(library("data.table")) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
5 suppressPackageStartupMessages(library("optparse")) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
6 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
7 option_list <- list( |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
8 make_option(c("--phyloseq"), action = "store", dest = "phyloseq", help = "File containing a phyloseq object"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
9 make_option(c("--formula"), action = "store", dest = "formula", help = "Formula"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
10 make_option(c("--p_adj_method"), action = "store", dest = "p_adj_method", help = "Method to adjust p-values"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
11 make_option(c("--zero_cut"), action = "store", dest = "zero_cut", type = "double", help = "Minimum taxa prevalence"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
12 make_option(c("--lib_cut"), action = "store", dest = "lib_cut", type = "integer", help = "Thrshold for filtering samples based on library sizes"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
13 make_option(c("--group"), action = "store", dest = "group", help = "Name of the group variable in the metadata"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
14 make_option(c("--struc_zero"), action = "store", dest = "struc_zero", help = "Detect structural zeros based on group"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
15 make_option(c("--neg_lb"), action = "store", dest = "neg_lb", help = "Classify a taxon as a structural zero using its asymptotic lower bound"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
16 make_option(c("--tol"), action = "store", dest = "tol", type = "double", help = "Iteration convergence tolerance for the E-M algorithm"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
17 make_option(c("--max_iter"), action = "store", dest = "max_iter", help = "Maximum number of iterations for the E-M algorithm"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
18 make_option(c("--conserve"), action = "store", dest = "conserve", help = "Use a conservative variance estimator for the test statistic"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
19 make_option(c("--alpha"), action = "store", dest = "alpha", help = "Level of significance"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
20 make_option(c("--global"), action = "store", dest = "global", help = "Perform global test"), |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
21 make_option(c("--output_dir"), action = "store", dest = "output_dir", help = "Output directory") |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
22 ) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
23 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
24 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
25 args <- parse_args(parser, positional_arguments = TRUE) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
26 opt <- args$options |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
27 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
28 get_boolean_value <- function(val) { |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
29 if (val == "true") { |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
30 return(TRUE) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
31 } else { |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
32 return(FALSE) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
33 } |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
34 } |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
35 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
36 get_file_path <- function(dir, file_name) { |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
37 file_path <- paste(dir, file_name, sep = "/") |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
38 return(file_path) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
39 } |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
40 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
41 write_data_frame <- function(dir, file_name, data_frame) { |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
42 file_path <- get_file_path(dir, file_name) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
43 write.table(data_frame, file = file_path, quote = FALSE, row.names = TRUE, col.names = TRUE, sep = "\t") |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
44 } |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
45 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
46 # Convert boolean values to boolean. |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
47 struc_zero <- get_boolean_value(opt$struc_zero) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
48 neg_lb <- get_boolean_value(opt$neg_lb) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
49 conserve <- get_boolean_value(opt$conserve) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
50 global <- get_boolean_value(opt$global) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
51 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
52 # Construct a phyloseq object. |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
53 phyloseq_obj <- readRDS(opt$phyloseq) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
54 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
55 # Construct an ANCOM-BC object. |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
56 ancombc_obj <- ancombc(phyloseq = phyloseq_obj, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
57 formula = opt$formula, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
58 p_adj_method = opt$p_adj_method, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
59 zero_cut = opt$zero_cut, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
60 lib_cut = opt$lib_cut, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
61 group = opt$group, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
62 struc_zero = struc_zero, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
63 neg_lb = neg_lb, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
64 tol = opt$tol, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
65 max_iter = opt$max_iter, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
66 conserve = conserve, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
67 alpha = opt$alpha, |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
68 global = global) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
69 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
70 res <- ancombc_obj$res |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
71 |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
72 # Write the outputs. |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
73 write_data_frame(opt$output_dir, "feature_table.tabular", ancombc_obj$feature_table) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
74 write_data_frame(opt$output_dir, "zero_ind.tabular", ancombc_obj$zero_ind) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
75 write.csv2(ancombc_obj$samp_frac, file = get_file_path(opt$output_dir, "samp_frac.tabular"), row.names = FALSE, col.names = FALSE, sep = "\t") |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
76 write_data_frame(opt$output_dir, "resid.tabular", ancombc_obj$resid) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
77 write(ancombc_obj$delta_em, file = get_file_path(opt$output_dir, "delta_em.tabular")) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
78 write(ancombc_obj$delta_wls, file = get_file_path(opt$output_dir, "delta_wls.tabular")) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
79 write_data_frame(opt$output_dir, "res_beta.tabular", res$beta) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
80 write_data_frame(opt$output_dir, "res_se.tabular", res$se) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
81 write_data_frame(opt$output_dir, "res_W.tabular", res$W) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
82 write_data_frame(opt$output_dir, "res_p_val.tabular", res$p_val) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
83 write_data_frame(opt$output_dir, "res_q_val.tabular", res$q_val) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
84 write_data_frame(opt$output_dir, "res_diff_abn.tabular", res$diff_abn) |
939c59ab61cf
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ancombc commit 045979180e44c683b5e0760f802af66c05abcae8
iuc
parents:
diff
changeset
|
85 write_data_frame(opt$output_dir, "res_global.tabular", ancombc_obj$res_global) |