comparison cluster.tools/extract.cons.cluster.from.result.R @ 2:b442996b66ae draft

Uploaded
author peter-waltman
date Wed, 27 Feb 2013 20:17:04 -0500
parents
children
comparison
equal deleted inserted replaced
1:e25d2bece0a2 2:b442996b66ae
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 )
40
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 ]]
54 save( file=opt$output.select.rdata, treecl.res, cl, select.result, data )
55 } else {
56 stop( 'no valid output format specified\n' )
57 }
58 } else {
59 out.string <- paste( "choice of k =", ch.k.select, "not available in this result file. Max k = ", max( as.numeric( names(cls) ) ), "\n" )
60 cat( out.string, file=opt$cluster.class.out )
61 }
62