Mercurial > repos > recetox > ramclustr
comparison ramclustr_wrapper.R @ 7:09cabbc3d12d draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/ramclustr commit b1cc1aebf796f170d93e3dd46ffcdefdc7b8018a
| author | recetox |
|---|---|
| date | Thu, 12 Oct 2023 13:30:54 +0000 |
| parents | 050cfef6ba65 |
| children | c043e1fd79d1 |
comparison
equal
deleted
inserted
replaced
| 6:9b716db0a786 | 7:09cabbc3d12d |
|---|---|
| 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.csv(ramclustr_obj$SpecAbund, | 6 write.csv(ramclustr_obj$SpecAbund, |
| 7 file = output_spec_abundance, | 7 file = output_spec_abundance, |
| 8 row.names = TRUE, quote = FALSE | 8 row.names = TRUE, quote = FALSE |
| 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 } | |
| 32 } | 31 } |
| 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 } | |
| 38 } | 37 } |
| 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, |
| 46 phenodata_ext = NULL, | 46 phenodata_ext = 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 <- arrow::read_parquet(ms1_featuredefinitions) | 50 ms1_featuredefinitions <- arrow::read_parquet(ms1_featuredefinitions) |
| 51 ms1_featurevalues <- arrow::read_parquet(ms1_featurevalues) | 51 ms1_featurevalues <- arrow::read_parquet(ms1_featurevalues) |
| 52 | 52 |
| 53 if (!is.null(df_phenodata)) { | 53 if (!is.null(df_phenodata)) { |
| 54 if (phenodata_ext == "csv") { | 54 if (phenodata_ext == "csv") { |
| 55 df_phenodata <- read.csv( | 55 df_phenodata <- read.csv( |
| 56 file = df_phenodata, | 56 file = df_phenodata, |
| 57 header = TRUE, check.names = FALSE | 57 header = TRUE, check.names = FALSE |
| 58 ) | 58 ) |
| 59 } else { | 59 } else { |
| 60 df_phenodata <- read.csv( | 60 df_phenodata <- read.csv( |
| 61 file = df_phenodata, | 61 file = df_phenodata, |
| 62 header = TRUE, check.names = FALSE, sep = "\t" | 62 header = TRUE, check.names = FALSE, sep = "\t" |
| 63 ) | 63 ) |
| 64 } | |
| 65 } | 64 } |
| 66 if (!is.null(exp_des)) { | 65 } |
| 67 exp_des <- load_experiment_definition(exp_des) | 66 if (!is.null(exp_des)) { |
| 68 } | 67 exp_des <- load_experiment_definition(exp_des) |
| 68 } | |
| 69 | 69 |
| 70 feature_values <- ms1_featurevalues[-1] | 70 feature_values <- ms1_featurevalues[-1] |
| 71 feature_values <- t(feature_values) | 71 feature_values <- t(feature_values) |
| 72 colnames(feature_values) <- ms1_featurevalues[[1]] | 72 colnames(feature_values) <- ms1_featurevalues[[1]] |
| 73 | 73 |
| 74 feature_definitions <- data.frame(ms1_featuredefinitions) | 74 feature_definitions <- data.frame(ms1_featuredefinitions) |
| 75 | 75 |
| 76 ramclustr_obj <- RAMClustR::rc.get.df.data( | 76 ramclustr_obj <- RAMClustR::rc.get.df.data( |
| 77 ms1_featureDefinitions = feature_definitions, | 77 ms1_featureDefinitions = feature_definitions, |
| 78 ms1_featureValues = feature_values, | 78 ms1_featureValues = feature_values, |
| 79 phenoData = df_phenodata, | 79 phenoData = df_phenodata, |
| 80 ExpDes = exp_des, | 80 ExpDes = exp_des, |
| 81 st = st, | 81 st = st, |
| 82 ensure.no.na = ensure_no_na | 82 ensure.no.na = ensure_no_na |
| 83 ) | 83 ) |
| 84 return(ramclustr_obj) | 84 return(ramclustr_obj) |
| 85 } | 85 } |
| 86 | 86 |
| 87 apply_normalisation <- function(ramclustr_obj = NULL, | 87 apply_normalisation <- function(ramclustr_obj = NULL, |
| 88 normalize_method, | 88 normalize_method, |
| 89 metadata_file = NULL, | 89 metadata_file = NULL, |
| 90 qc_inj_range, | 90 qc_inj_range, |
| 91 p_cut, | 91 p_cut, |
| 92 rsq_cut, | 92 rsq_cut, |
| 93 p_adjust) { | 93 p_adjust) { |
| 94 batch <- NULL | 94 batch <- NULL |
| 95 order <- NULL | 95 order <- NULL |
| 96 qc <- NULL | 96 qc <- NULL |
| 97 | 97 |
| 98 if (normalize_method == "TIC") { | 98 if (normalize_method == "TIC") { |
| 99 ramclustr_obj <- RAMClustR::rc.feature.normalize.tic( | 99 ramclustr_obj <- RAMClustR::rc.feature.normalize.tic( |
| 100 ramclustObj = | 100 ramclustObj = |
| 101 ramclustr_obj | 101 ramclustr_obj |
| 102 ) | 102 ) |
| 103 } else if (normalize_method == "quantile") { | 103 } else if (normalize_method == "quantile") { |
| 104 ramclustr_obj <- RAMClustR::rc.feature.normalize.quantile(ramclustr_obj) | 104 ramclustr_obj <- RAMClustR::rc.feature.normalize.quantile(ramclustr_obj) |
| 105 } else if (normalize_method == "batch.qc") { | 105 } else if (normalize_method == "batch.qc") { |
| 106 if (!(is.null(metadata_file) || metadata_file == "None")) { | 106 if (!(is.null(metadata_file) || metadata_file == "None")) { |
| 107 metadata <- read_metadata(metadata_file) | 107 metadata <- read_metadata(metadata_file) |
| 108 batch <- metadata$batch | 108 batch <- metadata$batch |
| 109 order <- metadata$order | 109 order <- metadata$order |
| 110 qc <- metadata$qc | 110 qc <- metadata$qc |
| 111 } | 111 } |
| 112 | 112 |
| 113 ramclustr_obj <- RAMClustR::rc.feature.normalize.batch.qc( | 113 ramclustr_obj <- RAMClustR::rc.feature.normalize.batch.qc( |
| 114 order = order, | 114 order = order, |
| 115 batch = batch, | 115 batch = batch, |
| 116 qc = qc, | 116 qc = qc, |
| 117 ramclustObj = ramclustr_obj, | 117 ramclustObj = ramclustr_obj, |
| 118 qc.inj.range = qc_inj_range | 118 qc.inj.range = qc_inj_range |
| 119 ) | 119 ) |
| 120 } else { | 120 } else { |
| 121 if (!(is.null(metadata_file) || metadata_file == "None")) { | 121 if (!(is.null(metadata_file) || metadata_file == "None")) { |
| 122 metadata <- read_metadata(metadata_file) | 122 metadata <- read_metadata(metadata_file) |
| 123 batch <- metadata$batch | 123 batch <- metadata$batch |
| 124 order <- metadata$order | 124 order <- metadata$order |
| 125 qc <- metadata$qc | 125 qc <- metadata$qc |
| 126 } | 126 } |
| 127 | 127 |
| 128 ramclustr_obj <- RAMClustR::rc.feature.normalize.qc( | 128 ramclustr_obj <- RAMClustR::rc.feature.normalize.qc( |
| 129 order = order, | 129 order = order, |
| 130 batch = batch, | 130 batch = batch, |
| 131 qc = qc, | 131 qc = qc, |
| 132 ramclustObj = ramclustr_obj, | 132 ramclustObj = ramclustr_obj, |
| 133 p.cut = p_cut, | 133 p.cut = p_cut, |
| 134 rsq.cut = rsq_cut, | 134 rsq.cut = rsq_cut, |
| 135 p.adjust = p_adjust | 135 p.adjust = p_adjust |
| 136 ) | 136 ) |
| 137 } | 137 } |
| 138 return(ramclustr_obj) | 138 return(ramclustr_obj) |
| 139 } | 139 } |
