Mercurial > repos > peter-waltman > ucsc_cluster_tools2
comparison cluster.tools/impute.knn.R @ 0:0decf3fd54bc draft
Uploaded
author | peter-waltman |
---|---|
date | Thu, 28 Feb 2013 01:45:39 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0decf3fd54bc |
---|---|
1 #!/usr/bin/env Rscript | |
2 argspec <- c("impute.knn.R replaces missing values, using the impute.knn function from the impute package | |
3 | |
4 Usage: | |
5 impute.knn.R -d <data.file> | |
6 Optional: | |
7 -o <output_file> | |
8 \n\n") | |
9 args <- commandArgs(TRUE) | |
10 if ( length( args ) == 1 && args =="--help") { | |
11 write(argspec, stderr()) | |
12 q(); | |
13 } | |
14 | |
15 lib.load.quiet <- function( package ) { | |
16 package <- as.character(substitute(package)) | |
17 suppressPackageStartupMessages( do.call( "library", list( package=package ) ) ) | |
18 } | |
19 lib.load.quiet(getopt) | |
20 lib.load.quiet( impute ) | |
21 | |
22 spec <- matrix( c( "data.fname", "d", 1, "character", | |
23 "output.fname", "o", 2, "character" | |
24 ), | |
25 nc=4, | |
26 byrow=TRUE | |
27 ) | |
28 | |
29 opt <- getopt( spec=spec ) | |
30 | |
31 data <- as.matrix( read.delim( opt$data.fname, row.names=1, check.names=FALSE ) ) | |
32 if ( is.null( opt$replacement.val ) ) { opt$replacement.val <- NA } | |
33 if ( is.null( opt$output.fname ) ) { opt$output.fname <- paste( "impute.knn", basename( opt$data.fname ), sep="." ) } | |
34 | |
35 ## Set any NA, NAN or Inf entries to 0 | |
36 if ( is.nan(data) ) { | |
37 data[ is.nan( data ) ] <- NA | |
38 } | |
39 if ( is.infinite(data) ) { | |
40 data[ is.infinite( data ) ] <- NA | |
41 } | |
42 | |
43 data <- impute.knn( data )$data | |
44 write.table( data, opt$output.fname, sep="\t", quote=FALSE, col.names=NA ) |