Mercurial > repos > peter-waltman > ucsc_cluster_tools2
comparison cluster.tools/remove.degenerate.values.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("remove.degenerate.values.R replaces degenerate values (NaNs & Infs) with a user-specified value | |
3 | |
4 Usage: | |
5 remove.degenerate.values.R -d <data.file> | |
6 Optional: | |
7 -r <replacement_value> (default is NA) | |
8 -o <output_file> | |
9 \n\n") | |
10 args <- commandArgs(TRUE) | |
11 if ( length( args ) == 1 && args =="--help") { | |
12 write(argspec, stderr()) | |
13 q(); | |
14 } | |
15 | |
16 lib.load.quiet <- function( package ) { | |
17 package <- as.character(substitute(package)) | |
18 suppressPackageStartupMessages( do.call( "library", list( package=package ) ) ) | |
19 } | |
20 lib.load.quiet(getopt) | |
21 | |
22 spec <- matrix( c( "data.fname", "d", 1, "character", | |
23 "replacement.val", "r", 2, "character", | |
24 "output.fname", "o", 2, "character" | |
25 ), | |
26 nc=4, | |
27 byrow=TRUE | |
28 ) | |
29 | |
30 opt <- getopt( spec=spec ) | |
31 | |
32 data <- as.matrix( read.delim( opt$data.fname, row.names=1, check.names=FALSE ) ) | |
33 if ( is.null( opt$replacement.val ) ) { opt$replacement.val <- NA } | |
34 if ( ! is.null( opt$replacement.val ) ) { opt$replacement.val <- as.integer( opt$replacement.val ) } | |
35 if ( is.null( opt$output.fname ) ) { opt$output.fname <- paste( "degenerate.vals.replaced", basename( opt$data.fname ), sep="." ) } | |
36 | |
37 ## Set any NA, NAN or Inf entries to 0 | |
38 if ( any(is.nan(data)) ) data[ is.nan( data ) ] <- opt$replacement.val | |
39 if ( any(is.infinite(data)) ) data[ is.infinite( data ) ] <- opt$replacement.val | |
40 | |
41 write.table( data, opt$output.fname, sep="\t", quote=FALSE, col.names=NA ) |