Mercurial > repos > recetox > waveica
comparison waveica_wrapper.R @ 0:2461d20911c9 draft
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
| author | recetox |
|---|---|
| date | Thu, 18 Mar 2021 15:53:38 +0000 |
| parents | |
| children | 2bcfd5b450bb |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:2461d20911c9 |
|---|---|
| 1 waveica <- function( | |
| 2 data, | |
| 3 wavelet_filter, | |
| 4 wavelet_length, | |
| 5 k, | |
| 6 t, | |
| 7 t2, | |
| 8 alpha, | |
| 9 exclude_blanks | |
| 10 ) { | |
| 11 | |
| 12 # get input from the Galaxy, preprocess data | |
| 13 data <- read.csv(data, header = TRUE, row.names = "sample_name") | |
| 14 data <- preprocess_data(data) | |
| 15 | |
| 16 # remove blanks from dataset | |
| 17 if (exclude_blanks) { | |
| 18 data <- exclude_group(data) | |
| 19 } | |
| 20 | |
| 21 # separate data into features, batch and group | |
| 22 features <- data[, -c(1:4)] | |
| 23 group <- as.numeric(data$class) | |
| 24 batch <- data$batch | |
| 25 | |
| 26 # run WaveICA | |
| 27 normalized_data <- WaveICA::WaveICA( | |
| 28 data = features, | |
| 29 wf = get_wf(wavelet_filter, wavelet_length), | |
| 30 batch = batch, | |
| 31 group = group, | |
| 32 K = k, | |
| 33 t = t, | |
| 34 t2 = t2, | |
| 35 alpha = alpha | |
| 36 ) | |
| 37 | |
| 38 return(normalized_data) | |
| 39 } | |
| 40 | |
| 41 | |
| 42 # Sort data, set numerical values for groups | |
| 43 preprocess_data <- function(data) { | |
| 44 # sort data by injection order | |
| 45 data <- data[order(data$injectionOrder, decreasing = FALSE), ] | |
| 46 | |
| 47 data$class[data$class == "blank"] <- 0 | |
| 48 data$class[data$class == "sample"] <- 1 | |
| 49 data$class[data$class == "QC"] <- 2 | |
| 50 | |
| 51 return(data) | |
| 52 } | |
| 53 | |
| 54 | |
| 55 # Create appropriate input for R wavelets function | |
| 56 get_wf <- function(wavelet_filter, wavelet_length) { | |
| 57 wf <- paste(wavelet_filter, wavelet_length, sep = "") | |
| 58 | |
| 59 # exception to the wavelet function | |
| 60 if (wf == "d2") { | |
| 61 wf <- "haar" | |
| 62 } | |
| 63 | |
| 64 return(wf) | |
| 65 } | |
| 66 | |
| 67 | |
| 68 # Exclude blanks from a dataframe | |
| 69 exclude_group <- function(data) { | |
| 70 row_idx_to_exclude <- which(data$class %in% 0) | |
| 71 if (length(row_idx_to_exclude) > 1) { | |
| 72 data_without_blanks <- data[-c(row_idx_to_exclude), ] | |
| 73 msg <- paste("Blank samples have been excluded from the dataframe.\n") | |
| 74 cat(msg) | |
| 75 return(data_without_blanks) | |
| 76 } | |
| 77 else { | |
| 78 return(data) | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 | |
| 83 # Store output of WaveICA in a tsv file | |
| 84 store_data <- function(normalized_data, output) { | |
| 85 write.table(normalized_data, file = output, sep = "\t", col.names = NA) | |
| 86 cat("Normalization has been completed.\n") | |
| 87 } |
