0
|
1 #!/usr/bin/env Rscript
|
|
2 ## Script by Peter Waltman
|
|
3 ##
|
|
4 ## License under Creative Commons Attribution 3.0 Unported (CC BY 3.0)
|
|
5 ##
|
|
6 argspec <- c("TBD
|
|
7
|
|
8
|
|
9 Usage:
|
|
10 normalize.matrix.R -d <data.file>
|
|
11 Optional:
|
|
12 -o <output.name>
|
|
13 TBD
|
|
14 \n\n")
|
|
15
|
|
16 args <- commandArgs(TRUE)
|
|
17 if ( length( args ) == 1 && args =="--help") {
|
|
18 write(argspec, stderr())
|
|
19 q();
|
|
20 }
|
|
21
|
1
|
22 ## some helper fn's
|
|
23 write.2.tab <- function( mat,
|
|
24 fname ) {
|
|
25 mat <- rbind( colnames( mat ), mat )
|
|
26 mat <- cbind( c( "ID", rownames( mat )[-1] ),
|
|
27 mat )
|
|
28 write.table( mat, fname, sep="\t", row.names=FALSE, col.names=FALSE, quote=FALSE )
|
|
29 }
|
|
30
|
0
|
31 lib.load.quiet <- function( package ) {
|
|
32 package <- as.character(substitute(package))
|
|
33 suppressPackageStartupMessages( do.call( "library", list( package=package ) ) )
|
|
34 }
|
|
35 lib.load.quiet(getopt)
|
|
36
|
|
37
|
|
38 spec <- matrix( c( "data.fname", "d", 1, "character",
|
|
39 "center.rows", "r", 2, "character",
|
|
40 "var.adj.rows", "R", 2, "character",
|
|
41 "center.cols", "c", 2, "character",
|
|
42 "var.adj.cols", "C", 2, "character",
|
|
43 "output.fname", "o", 2, "character"
|
|
44 ),
|
|
45 nc=4,
|
|
46 byrow=TRUE
|
|
47 )
|
|
48
|
|
49 opt <- getopt( spec=spec )
|
|
50
|
|
51 #set some reasonable defaults for the options that are needed,
|
|
52 #but were not specified.
|
|
53 if ( is.null( opt$output.fname ) ) {
|
|
54 out.fname <- ""
|
|
55 if ( ! is.null( opt$center.rows ) ) out.fname <- paste( "row", opt$center.rows, "centered", out.fname, sep="." )
|
|
56 if ( ! is.null( opt$center.cols ) ) out.fname <- paste( "col", opt$center.cols, "centered", out.fname, sep="." )
|
|
57 if ( ! is.null( opt$var.adj.rows ) ) out.fname <- paste( "row", opt$var.adj.rows, "var.adjed", out.fname, sep="." )
|
|
58 if ( ! is.null( opt$var.adj.cols ) ) out.fname <- paste( "col", opt$var.adj.cols, "var.adjed", out.fname, sep="." )
|
|
59
|
|
60 opt$output.fname <- paste( out.fname, "centered.matrix", sep="." )
|
|
61 }
|
|
62 if ( is.null( opt$center.rows ) ) { opt$center.rows <- 'none' }
|
|
63 if ( is.null( opt$center.cols ) ) { opt$center.cols <- 'none' }
|
|
64 if ( is.null( opt$var.adj.rows ) ) { opt$var.adj.rows <- 'none' }
|
|
65 if ( is.null( opt$var.adj.cols ) ) { opt$var.adj.cols <- 'none' }
|
|
66
|
|
67 data <- as.matrix( read.delim( opt$data.fname, header=T, row.names=1 , check.names=FALSE ) )
|
|
68 my.center <- rep( 0, nrow( data ) )
|
|
69 my.var.adj <- rep( 1, nrow( data ) )
|
|
70 if ( opt$center.rows != "none" ) {
|
|
71 my.center.fn <- get( opt$center.rows )
|
|
72 ##data <- sweep( data, 1, apply( data, 1, my.center.fn, na.rm=T ) )
|
|
73 my.center <- apply( data, 1, my.center.fn, na.rm=T )
|
|
74 }
|
|
75 if ( opt$var.adj.rows != "none" ) {
|
|
76 my.var.adj.fn <- get( opt$var.adj.rows )
|
|
77 my.var.adj <- apply( data, 1, my.var.adj.fn, na.rm=T )
|
|
78 }
|
|
79 data <- t( scale( t( data ), center=my.center, scale=my.var.adj ) )
|
|
80
|
1
|
81 my.center <- rep( 0, ncol( data ) )
|
|
82 my.var.adj <- rep( 1, ncol( data ) )
|
0
|
83 if ( opt$center.cols != "none" ) {
|
|
84 my.center.fn <- get( opt$center.cols )
|
|
85 ##data <- sweep( data, 2, apply( data, 2, my.center.fn, na.rm=T ) )
|
|
86 my.center <- apply( data, 2, my.center.fn, na.rm=T )
|
|
87 }
|
|
88 my.var.adj <- rep( 1, ncol( data ) )
|
|
89 if ( opt$var.adj.cols != "none" ) {
|
|
90 my.var.adj.fn <- get( opt$var.adj.cols )
|
|
91 my.var.adj <- apply( data, 2, my.var.adj.fn, na.rm=T )
|
|
92 }
|
|
93 data <- scale( data, center=my.center, scale=my.var.adj )
|
|
94
|
1
|
95 write.2.tab( data, opt$output.fname )
|
0
|
96
|