Mercurial > repos > peter-waltman > ucsc_cluster_tools2
changeset 5:cbc3ecce98ee draft
Uploaded
author | peter-waltman |
---|---|
date | Fri, 01 Mar 2013 19:53:49 -0500 |
parents | 2c225e2f0acb |
children | 3d3a8595b981 |
files | cluster.tools/normalize.matrix.by.other.R |
diffstat | 1 files changed, 84 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cluster.tools/normalize.matrix.by.other.R Fri Mar 01 19:53:49 2013 -0500 @@ -0,0 +1,84 @@ +#!/usr/bin/env Rscript +## Script by Peter Waltman +## +## License under Creative Commons Attribution 3.0 Unported (CC BY 3.0) +## +argspec <- c("TBD + + + Usage: + normalize.matrix.R -d <data.file> + Optional: + -o <output.name> + TBD + \n\n") + +args <- commandArgs(TRUE) +if ( length( args ) == 1 && args =="--help") { + write(argspec, stderr()) + q(); +} + +## some helper fn's +write.2.tab <- function( mat, + fname ) { + mat <- rbind( colnames( mat ), mat ) + mat <- cbind( c( "ID", rownames( mat )[-1] ), + mat ) + write.table( mat, fname, sep="\t", row.names=FALSE, col.names=FALSE, quote=FALSE ) +} + +lib.load.quiet <- function( package ) { + package <- as.character(substitute(package)) + suppressPackageStartupMessages( do.call( "library", list( package=package ) ) ) +} +lib.load.quiet(getopt) + + +spec <- matrix( c( "data.fname", "d", 1, "character", + "data.fname2", "D", 1, "character", + "ctr.method", "c", 2, "character", + "output.fname", "o", 2, "character", + "return.normals", "r", 2, "character", + "out.norm.fname", "O", 2, "character" + ), + nc=4, + byrow=TRUE + ) + +opt <- getopt( spec=spec ) + +#set some reasonable defaults for the options that are needed, +#but were not specified. +if ( is.null( opt$output.fname ) ) { opt$output.fname <- 'merge_merge.tumors.tab' } +if ( is.null( opt$ctr.method ) ) { opt$ctr.method <- 'median' } +if ( is.null( opt$return.normals ) ) { + opt$return.normals <- FALSE +} else { + opt$return.normals <- ( tolower( opt$return.normals ) %in% "yes" ) +} +if ( is.null( opt$out.norm.fname ) ) { opt$out.norm.fname <- 'merge_merge.normals.tab' } + + +data <- as.matrix( read.delim( opt$data.fname, header=T, row.names=1 , check.names=FALSE ) ) +normer.data <- as.matrix( read.delim( opt$data.fname2, header=T, row.names=1 , check.names=FALSE ) ) +if ( ! all( rownames(data) %in% rownames(normer.data) ) ) { + gene.ovp <- rownames(data) + gene.ovp <- gene.ovp[ gene.ovp %in% rownames(normer.data) ] + if ( length( gene.ovp ) == 0 ) stop( 'No gene overlap between specified matrices\n' ) + + data <- data[ gene.ovp, , drop=F ] + normer.data <- normer.data[ gene.ovp, , drop=F ] +} + +my.ctr.vals <- apply( normer.data, 1, get( opt$ctr.method ), na.rm=T ) + +data <- apply( data, 2, function(x) x-my.ctr.vals ) + +write.2.tab( data, opt$output.fname ) +if ( opt$return.normals ) { + normer.data <- normer.data - my.ctr.vals + write.2.tab( normer.data, opt$out.norm.fname ) +} + +