comparison center_scale.R @ 0:bcbd7179d8ec draft

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