0
|
1 #!/usr/bin/env Rscript
|
|
2 argspec <- c("remove.tcga.normals.R removes TCGA normal samples from a given matrix
|
|
3
|
|
4 Usage:
|
|
5 remove.tcga.normals.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
|
3
|
15 ## some helper fn's
|
|
16 write.2.tab <- function( mat,
|
|
17 fname ) {
|
|
18 mat <- rbind( colnames( mat ), mat )
|
|
19 mat <- cbind( c( "ID", rownames( mat )[-1] ),
|
|
20 mat )
|
|
21 write.table( mat, fname, sep="\t", row.names=FALSE, col.names=FALSE, quote=FALSE )
|
|
22 }
|
0
|
23 lib.load.quiet <- function( package ) {
|
|
24 package <- as.character(substitute(package))
|
|
25 suppressPackageStartupMessages( do.call( "library", list( package=package ) ) )
|
|
26 }
|
|
27 lib.load.quiet(getopt)
|
|
28
|
|
29 spec <- matrix( c( "data.fname", "d", 1, "character",
|
3
|
30 "output.fname", "o", 2, "character",
|
|
31 "return.normals", "r", 2, "character",
|
|
32 "out.norm.fname", "O", 2, "character"
|
0
|
33 ),
|
|
34 nc=4,
|
|
35 byrow=TRUE
|
|
36 )
|
|
37
|
|
38 opt <- getopt( spec=spec )
|
|
39 if ( is.null( opt$output.fname ) ) { opt$output.fname <- 'merge_merge.tumors.tab' }
|
3
|
40 if ( is.null( opt$return.normals ) ) {
|
|
41 opt$return.normals <- FALSE
|
|
42 } else {
|
|
43 opt$return.normals <- ( tolower( opt$return.normals ) %in% "yes" )
|
|
44 }
|
|
45 if ( is.null( opt$out.norm.fname ) ) { opt$out.norm.fname <- 'merge_merge.normals.tab' }
|
0
|
46
|
|
47 mat <- as.matrix( read.delim( opt$data.fname, row.names=1, check.names=FALSE ) )
|
3
|
48 norms <- matrix( NA, nc=0, nr=nrow( mat ), dimnames=list( rownames( mat ), c() ) )
|
|
49 if ( length( strsplit( colnames( mat ), "-" )[[1]] ) > 3 ) {
|
0
|
50 cnames <- sapply( strsplit( colnames( mat ), "-" ), function(x) x[4] )
|
|
51 norms <- grepl( "^1", cnames )
|
|
52
|
|
53 if ( sum( norms ) > 0 ) {
|
|
54 tumors <- ! norms
|
3
|
55 norms <- mat[, norms ]
|
0
|
56 mat <- mat[, tumors ]
|
|
57 }
|
3
|
58 } else {
|
|
59 if ( opt$return.normals ) {
|
|
60 writeLines( "TCGA ID barcodes in supplied file only provide patient sample info (no aliquot components are in IDs)\n", opt$out.norm.fname )
|
|
61 }
|
0
|
62 }
|
3
|
63 write.2.tab( mat, opt$output.fname )
|
|
64 if ( opt$return.normals ) {
|
|
65 if ( ncol( norms ) > 0 ) {
|
|
66 write.2.tab( norms, opt$out.norm.fname )
|
|
67 } else {
|
|
68 writeLines( "no normals found in supplied matrix\n", opt$out.norm.fname )
|
|
69 }
|
|
70 }
|