Mercurial > repos > recetox > ramclustr
comparison ramclustr_wrapper.R @ 11:da7722f665f4 draft default tip
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/ramclustr commit bc3445f7c41271b0062c7674108f57708d08dd28
author | recetox |
---|---|
date | Thu, 30 May 2024 14:52:11 +0000 |
parents | 2d94da58904b |
children |
comparison
equal
deleted
inserted
replaced
10:2d94da58904b | 11:da7722f665f4 |
---|---|
1 store_output <- function(ramclustr_obj, | 1 store_output <- function(ramclustr_obj, |
2 output_merge_msp, | 2 output_merge_msp, |
3 output_spec_abundance, | 3 output_spec_abundance, |
4 msp_file) { | 4 msp_file) { |
5 RAMClustR::write.msp(ramclustr_obj, one.file = output_merge_msp) | 5 RAMClustR::write.msp(ramclustr_obj, one.file = output_merge_msp) |
6 write.table(ramclustr_obj$SpecAbund, | 6 write.table(ramclustr_obj$SpecAbund, |
7 file = output_spec_abundance, | 7 file = output_spec_abundance, |
8 row.names = TRUE, quote = FALSE, col.names = NA, sep = "\t" | 8 row.names = TRUE, quote = FALSE, col.names = NA, sep = "\t" |
9 ) | 9 ) |
10 | 10 |
11 if (!is.null(msp_file)) { | 11 if (!is.null(msp_file)) { |
12 exp_name <- ramclustr_obj$ExpDes[[1]][which( | 12 exp_name <- ramclustr_obj$ExpDes[[1]][which( |
13 row.names(ramclustr_obj$ExpDes[[1]]) == "Experiment" | 13 row.names(ramclustr_obj$ExpDes[[1]]) == "Experiment" |
14 ), 1] | 14 ), 1] |
15 filename <- paste("spectra/", exp_name, ".msp", sep = "") | 15 filename <- paste("spectra/", exp_name, ".msp", sep = "") |
16 file.copy(from = filename, to = msp_file, overwrite = TRUE) | 16 file.copy(from = filename, to = msp_file, overwrite = TRUE) |
17 } | 17 } |
18 } | 18 } |
19 | 19 |
20 load_experiment_definition <- function(filename) { | 20 load_experiment_definition <- function(filename) { |
21 experiment <- RAMClustR::defineExperiment(csv = filename) | 21 experiment <- RAMClustR::defineExperiment(csv = filename) |
22 return(experiment) | 22 return(experiment) |
23 } | 23 } |
24 | 24 |
25 read_metadata <- function(filename) { | 25 read_metadata <- function(filename) { |
26 data <- read.csv(filename, header = TRUE, stringsAsFactors = FALSE) | 26 data <- read.csv(filename, header = TRUE, stringsAsFactors = FALSE) |
27 | 27 |
28 if (!"qc" %in% colnames(data)) { | 28 if (!"qc" %in% colnames(data)) { |
29 if ("sampleType" %in% colnames(data)) { | 29 if ("sampleType" %in% colnames(data)) { |
30 data$qc <- ifelse(data$sampleType == "qc", TRUE, FALSE) | 30 data$qc <- ifelse(data$sampleType == "qc", TRUE, FALSE) |
31 } | |
31 } | 32 } |
32 } | |
33 | 33 |
34 if (!"order" %in% colnames(data)) { | 34 if (!"order" %in% colnames(data)) { |
35 if ("injectionOrder" %in% colnames(data)) { | 35 if ("injectionOrder" %in% colnames(data)) { |
36 names(data)[names(data) == "injectionOrder"] <- "order" | 36 names(data)[names(data) == "injectionOrder"] <- "order" |
37 } | |
37 } | 38 } |
38 } | |
39 | 39 |
40 return(data) | 40 return(data) |
41 } | 41 } |
42 | 42 |
43 read_ramclustr_aplcms <- function(ms1_featuredefinitions = NULL, | 43 read_ramclustr_aplcms <- function(ms1_featuredefinitions = NULL, |
44 ms1_featurevalues = NULL, | 44 ms1_featurevalues = NULL, |
45 df_phenodata = NULL, | 45 df_phenodata = NULL, |
47 exp_des = NULL, | 47 exp_des = NULL, |
48 st = NULL, | 48 st = NULL, |
49 ensure_no_na = TRUE, | 49 ensure_no_na = TRUE, |
50 ms1_featuredefinitions_ext = "parquet", | 50 ms1_featuredefinitions_ext = "parquet", |
51 ms1_featurevalues_ext = "parquet") { | 51 ms1_featurevalues_ext = "parquet") { |
52 if (ms1_featuredefinitions_ext == "parquet") { | 52 if (ms1_featuredefinitions_ext == "parquet") { |
53 ms1_featuredefinitions <- arrow::read_parquet(ms1_featuredefinitions) | 53 ms1_featuredefinitions <- arrow::read_parquet(ms1_featuredefinitions) |
54 } else { | 54 } else { |
55 ms1_featuredefinitions <- read.csv(ms1_featuredefinitions, | 55 ms1_featuredefinitions <- read.csv(ms1_featuredefinitions, |
56 header = TRUE, sep = "\t" | 56 header = TRUE, sep = "\t", check.names = FALSE |
57 ) | |
58 } | |
59 | |
60 if (ms1_featurevalues_ext == "parquet") { | |
61 ms1_featurevalues <- arrow::read_parquet(ms1_featurevalues) | |
62 } else { | |
63 ms1_featurevalues <- read.csv(ms1_featurevalues, | |
64 header = TRUE, | |
65 sep = "\t", check.names = FALSE | |
66 ) | |
67 } | |
68 | |
69 if (!is.null(df_phenodata)) { | |
70 if (phenodata_ext == "csv") { | |
71 df_phenodata <- read.csv( | |
72 file = df_phenodata, | |
73 header = TRUE, check.names = FALSE | |
74 ) | |
75 } else { | |
76 df_phenodata <- read.csv( | |
77 file = df_phenodata, | |
78 header = TRUE, check.names = FALSE, sep = "\t" | |
79 ) | |
80 } | |
81 } | |
82 if (!is.null(exp_des)) { | |
83 exp_des <- load_experiment_definition(exp_des) | |
84 } | |
85 | |
86 feature_values <- ms1_featurevalues[-1] | |
87 feature_values <- t(feature_values) | |
88 colnames(feature_values) <- ms1_featurevalues[[1]] | |
89 | |
90 feature_definitions <- data.frame(ms1_featuredefinitions) | |
91 | |
92 ramclustr_obj <- RAMClustR::rc.get.df.data( | |
93 ms1_featureDefinitions = feature_definitions, | |
94 ms1_featureValues = feature_values, | |
95 phenoData = df_phenodata, | |
96 ExpDes = exp_des, | |
97 st = st, | |
98 ensure.no.na = ensure_no_na | |
57 ) | 99 ) |
58 } | 100 return(ramclustr_obj) |
59 | |
60 if (ms1_featurevalues_ext == "parquet") { | |
61 ms1_featurevalues <- arrow::read_parquet(ms1_featurevalues) | |
62 } else { | |
63 ms1_featurevalues <- read.csv(ms1_featurevalues, header = TRUE, sep = "\t") | |
64 } | |
65 | |
66 if (!is.null(df_phenodata)) { | |
67 if (phenodata_ext == "csv") { | |
68 df_phenodata <- read.csv( | |
69 file = df_phenodata, | |
70 header = TRUE, check.names = FALSE | |
71 ) | |
72 } else { | |
73 df_phenodata <- read.csv( | |
74 file = df_phenodata, | |
75 header = TRUE, check.names = FALSE, sep = "\t" | |
76 ) | |
77 } | |
78 } | |
79 if (!is.null(exp_des)) { | |
80 exp_des <- load_experiment_definition(exp_des) | |
81 } | |
82 | |
83 feature_values <- ms1_featurevalues[-1] | |
84 feature_values <- t(feature_values) | |
85 colnames(feature_values) <- ms1_featurevalues[[1]] | |
86 | |
87 feature_definitions <- data.frame(ms1_featuredefinitions) | |
88 | |
89 ramclustr_obj <- RAMClustR::rc.get.df.data( | |
90 ms1_featureDefinitions = feature_definitions, | |
91 ms1_featureValues = feature_values, | |
92 phenoData = df_phenodata, | |
93 ExpDes = exp_des, | |
94 st = st, | |
95 ensure.no.na = ensure_no_na | |
96 ) | |
97 return(ramclustr_obj) | |
98 } | 101 } |
99 | 102 |
100 apply_normalisation <- function(ramclustr_obj = NULL, | 103 apply_normalisation <- function(ramclustr_obj = NULL, |
101 normalize_method, | 104 normalize_method, |
102 metadata_file = NULL, | 105 metadata_file = NULL, |
103 qc_inj_range, | 106 qc_inj_range, |
104 p_cut, | 107 p_cut, |
105 rsq_cut, | 108 rsq_cut, |
106 p_adjust) { | 109 p_adjust) { |
107 batch <- NULL | 110 batch <- NULL |
108 order <- NULL | 111 order <- NULL |
109 qc <- NULL | 112 qc <- NULL |
110 | 113 |
111 if (normalize_method == "TIC") { | 114 if (normalize_method == "TIC") { |
112 ramclustr_obj <- RAMClustR::rc.feature.normalize.tic( | 115 ramclustr_obj <- RAMClustR::rc.feature.normalize.tic( |
113 ramclustObj = | 116 ramclustObj = |
114 ramclustr_obj | 117 ramclustr_obj |
115 ) | 118 ) |
116 } else if (normalize_method == "quantile") { | 119 } else if (normalize_method == "quantile") { |
117 ramclustr_obj <- RAMClustR::rc.feature.normalize.quantile(ramclustr_obj) | 120 ramclustr_obj <- RAMClustR::rc.feature.normalize.quantile(ramclustr_obj) |
118 } else if (normalize_method == "batch.qc") { | 121 } else if (normalize_method == "batch.qc") { |
119 if (!(is.null(metadata_file) || metadata_file == "None")) { | 122 if (!(is.null(metadata_file) || metadata_file == "None")) { |
120 metadata <- read_metadata(metadata_file) | 123 metadata <- read_metadata(metadata_file) |
121 batch <- metadata$batch | 124 batch <- metadata$batch |
122 order <- metadata$order | 125 order <- metadata$order |
123 qc <- metadata$qc | 126 qc <- metadata$qc |
127 } | |
128 | |
129 ramclustr_obj <- RAMClustR::rc.feature.normalize.batch.qc( | |
130 order = order, | |
131 batch = batch, | |
132 qc = qc, | |
133 ramclustObj = ramclustr_obj, | |
134 qc.inj.range = qc_inj_range | |
135 ) | |
136 } else { | |
137 if (!(is.null(metadata_file) || metadata_file == "None")) { | |
138 metadata <- read_metadata(metadata_file) | |
139 batch <- metadata$batch | |
140 order <- metadata$order | |
141 qc <- metadata$qc | |
142 } | |
143 | |
144 ramclustr_obj <- RAMClustR::rc.feature.normalize.qc( | |
145 order = order, | |
146 batch = batch, | |
147 qc = qc, | |
148 ramclustObj = ramclustr_obj, | |
149 p.cut = p_cut, | |
150 rsq.cut = rsq_cut, | |
151 p.adjust = p_adjust | |
152 ) | |
124 } | 153 } |
125 | 154 return(ramclustr_obj) |
126 ramclustr_obj <- RAMClustR::rc.feature.normalize.batch.qc( | |
127 order = order, | |
128 batch = batch, | |
129 qc = qc, | |
130 ramclustObj = ramclustr_obj, | |
131 qc.inj.range = qc_inj_range | |
132 ) | |
133 } else { | |
134 if (!(is.null(metadata_file) || metadata_file == "None")) { | |
135 metadata <- read_metadata(metadata_file) | |
136 batch <- metadata$batch | |
137 order <- metadata$order | |
138 qc <- metadata$qc | |
139 } | |
140 | |
141 ramclustr_obj <- RAMClustR::rc.feature.normalize.qc( | |
142 order = order, | |
143 batch = batch, | |
144 qc = qc, | |
145 ramclustObj = ramclustr_obj, | |
146 p.cut = p_cut, | |
147 rsq.cut = rsq_cut, | |
148 p.adjust = p_adjust | |
149 ) | |
150 } | |
151 return(ramclustr_obj) | |
152 } | 155 } |