0
|
1 #!/usr/bin/env Rscript
|
|
2 # Extract k cluster assignment from consensus clustering result Script by Peter Waltman
|
|
3 # Nov. 12, 2012
|
|
4 # License under Creative Commons Attribution 3.0 Unported (CC BY 3.0)
|
|
5 #
|
|
6 ##usage, options and doc goes here
|
|
7 argspec <- c("galaxy.extract.cons.clustering.from.result.R extracts a cluster assignment for a specified
|
|
8 value of K from a specified consensus cluster result file.
|
|
9
|
|
10
|
|
11 Usage:
|
|
12 galaxy.extract.cons.cluster.from.result.R -r <results_file> -k <k_select>
|
|
13 Optional:
|
|
14 -o consensus class output file # tab-delimitted file format
|
|
15 \n\n")
|
|
16 args <- commandArgs(TRUE)
|
|
17 if ( length( args ) == 1 && args =="--help") {
|
|
18 write( argspec, stderr() )
|
|
19 q();
|
|
20 }
|
|
21
|
|
22 library(getopt)
|
|
23
|
|
24 spec <- matrix( c( "results.file", "r", 1, "character",
|
|
25 "k.select", "k", 1, "integer",
|
|
26 "cluster.class.out", "o", 2, "character",
|
|
27 "output.select.rdata", "d", 2, "character"
|
|
28 ),
|
|
29 nc=4,
|
|
30 byrow=T
|
|
31 )
|
|
32 opt <- getopt( spec=spec )
|
|
33 if ( is.null( opt$output.select.rdata ) ) { opt$output.select.rdata <- "select.RData" }
|
|
34 ##if ( is.null( opt$cluster.class.out) ) { opt$cluster.class.out <- "select.cls" }
|
|
35
|
|
36 load( opt$results.file )
|
|
37 cons.matrices <- lapply( results[ 2:length(results) ], '[[', 'consensusMatrix' )
|
|
38 cls <- lapply( results[ 2:length(results) ], '[[', 'consensusClass' )
|
|
39 names( cons.matrices ) <- names( cls ) <- 2:length( results )
|
7
|
40 save.image( '~/tmp/extract.dbg.rda' )
|
0
|
41 ch.k.select <- as.character( opt$k.select )
|
|
42 if ( ch.k.select %in% names( cls ) ) {
|
|
43 ## get the consensusClass file that's associated with the k.select
|
|
44 cl <- cls[[ ch.k.select ]]
|
|
45
|
|
46 if ( ! is.null( opt$cluster.class.out ) ) {
|
|
47 cl <- cbind( names( cl ), as.integer(cl) )
|
|
48 colnames( cl ) <- c( "ID", "class" )
|
|
49 write.table( cl, opt$cluster.class.out, sep="\t", row.names=FALSE, quote=FALSE )
|
|
50 } else if ( ! is.null( opt$output.select.rdata ) ) {
|
|
51 ## re-order the samples to follow the cluster assignment
|
|
52 treecl.res <- results[[ opt$k.select ]]$consensusTree
|
|
53 select.result <- results[[ opt$k.select ]]
|
7
|
54
|
|
55 if ( length(cl) == ncol(data) ) {
|
|
56 names( cl ) <- treecl.res$labels <- select.result$consensusTree$labels <- colnames(data)
|
|
57 } else if ( length(cl) == nrow(data) ) {
|
|
58 names( cl ) <- treecl.res$labels <- select.result$consensusTree$labels <- rownames(data)
|
|
59 } else {
|
|
60 stop( "Number of clustered elements not equal to either number of rows or columns of data matrix\n" )
|
|
61 }
|
|
62
|
0
|
63 save( file=opt$output.select.rdata, treecl.res, cl, select.result, data )
|
|
64 } else {
|
|
65 stop( 'no valid output format specified\n' )
|
|
66 }
|
|
67 } else {
|
|
68 out.string <- paste( "choice of k =", ch.k.select, "not available in this result file. Max k = ", max( as.numeric( names(cls) ) ), "\n" )
|
|
69 cat( out.string, file=opt$cluster.class.out )
|
|
70 }
|
|
71
|