comparison center_scale.R @ 3:b7daf62bde65 draft default tip

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/gsc_center_scale commit 44d7e1048de3a58983e0559581264147090fefb0
author artbio
date Thu, 07 Nov 2024 18:54:17 +0000
parents 163befe5f05b
children
comparison
equal deleted inserted replaced
2:163befe5f05b 3:b7daf62bde65
1 options(show.error.messages = FALSE, 1 options(
2 error = function() { 2 show.error.messages = FALSE,
3 cat(geterrmessage(), file = stderr()) 3 error = function() {
4 q("no", 1, FALSE) 4 cat(geterrmessage(), file = stderr())
5 } 5 q("no", 1, FALSE)
6 }
6 ) 7 )
7 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") 8 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
8 warnings() 9 warnings()
9 library(optparse) 10 library(optparse)
10 11
11 # Arguments 12 # Arguments
12 option_list <- list( 13 option_list <- list(
13 make_option( 14 make_option(
14 "--data", 15 "--data",
15 default = NA, 16 default = NA,
16 type = "character", 17 type = "character",
17 help = "Input file that contains values to transform. Must be tabular separated, 18 help = "Input file that contains values to transform. Must be tabular separated,
18 with columns and row names, variables in rows, observations in columns [default : '%default' ]" 19 with columns and row names, variables in rows, observations in columns [default : '%default' ]"
19 ), 20 ),
20 make_option( 21 make_option(
21 "--center", 22 "--center",
22 default = TRUE, 23 default = TRUE,
23 type = "logical", 24 type = "logical",
24 help = "center data to the mean [default : '%default' ]" 25 help = "center data to the mean [default : '%default' ]"
25 ), 26 ),
26 make_option( 27 make_option(
27 "--scale", 28 "--scale",
28 default = TRUE, 29 default = TRUE,
29 type = "logical", 30 type = "logical",
30 help = "scale data to standard deviation [default : '%default' ]" 31 help = "scale data to standard deviation [default : '%default' ]"
31 ), 32 ),
32 make_option( 33 make_option(
33 "--factor", 34 "--factor",
34 default = "", 35 default = "",
35 type = "character", 36 type = "character",
36 help = "A two-column observations|factor_levels table, to group observations to be transformed by levels [default : '%default' ]" 37 help = "A two-column observations|factor_levels table, to group observations to be transformed by levels [default : '%default' ]"
37 ), 38 ),
38 make_option( 39 make_option(
39 "--output", 40 "--output",
40 default = "res.tab", 41 default = "res.tab",
41 type = "character", 42 type = "character",
42 help = "Table of transformed values [default : '%default' ]" 43 help = "Table of transformed values [default : '%default' ]"
43 ) 44 )
44 ) 45 )
45 46
46 transform <- function(df, center = TRUE, scale = TRUE) { 47 transform <- function(df, center = TRUE, scale = TRUE) {
47 transfo <- scale(t(df), 48 transfo <- scale(t(df),
48 center = center, 49 center = center,
49 scale = scale 50 scale = scale
50 ) 51 )
51 return(as.data.frame(t(transfo))) 52 return(as.data.frame(t(transfo)))
52 } 53 }
53 54
54 opt <- parse_args(OptionParser(option_list = option_list), 55 opt <- parse_args(OptionParser(option_list = option_list),
55 args = commandArgs(trailingOnly = TRUE)) 56 args = commandArgs(trailingOnly = TRUE)
57 )
56 58
57 data <- read.delim( 59 data <- read.delim(
58 opt$data, 60 opt$data,
59 check.names = FALSE, 61 check.names = FALSE,
60 header = TRUE, 62 header = TRUE,
61 row.names = 1, 63 row.names = 1,
62 sep = "\t" 64 sep = "\t"
63 ) 65 )
64 66
65 if (opt$factor != "") { 67 if (opt$factor != "") {
66 data_factor <- read.delim( 68 data_factor <- read.delim(
67 opt$factor, 69 opt$factor,
68 check.names = FALSE, 70 check.names = FALSE,
69 header = TRUE, 71 header = TRUE,
70 sep = "\t", 72 sep = "\t",
71 stringsAsFactors = TRUE 73 stringsAsFactors = TRUE
72 ) 74 )
73 colnames(data_factor) <- c("cellid", "level") 75 colnames(data_factor) <- c("cellid", "level")
74 data_transformed <- data.frame(row.names = rownames(data)) 76 data_transformed <- data.frame(row.names = rownames(data))
75 for (group in levels(data_factor$level)) { 77 for (group in levels(data_factor$level)) {
76 subcells <- as.data.frame(subset(data_factor, level == group, select = cellid)) 78 subcells <- as.data.frame(subset(data_factor, level == group, select = cellid))
77 subdata <- as.data.frame(subset(data, select = as.vector(subcells$cellid))) 79 subdata <- as.data.frame(subset(data, select = as.vector(subcells$cellid)))
78 subdata_transformed <- transform(subdata, center = as.logical(opt$center), 80 subdata_transformed <- transform(subdata,
79 scale = as.logical(opt$scale)) 81 center = as.logical(opt$center),
80 data_transformed <- cbind(data_transformed, subdata_transformed) 82 scale = as.logical(opt$scale)
81 } 83 )
84 data_transformed <- cbind(data_transformed, subdata_transformed)
85 }
82 } else { 86 } else {
83 data_transformed <- transform(data, center = as.logical(opt$center), 87 data_transformed <- transform(data,
84 scale = as.logical(opt$scale)) 88 center = as.logical(opt$center),
89 scale = as.logical(opt$scale)
90 )
85 } 91 }
86 92
87 93
88 write.table( 94 write.table(
89 cbind(gene = rownames(data_transformed), data_transformed), 95 cbind(gene = rownames(data_transformed), data_transformed),
90 opt$output, 96 opt$output,
91 col.names = TRUE, 97 col.names = TRUE,
92 row.names = FALSE, 98 row.names = FALSE,
93 quote = FALSE, 99 quote = FALSE,
94 sep = "\t" 100 sep = "\t"
95 ) 101 )