0
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 argspec <- c("hclust.R help TBD
|
|
4 \n\n")
|
|
5 args <- commandArgs(TRUE)
|
|
6 if ( length( args ) == 1 && args =="--help") {
|
|
7 write(argspec, stderr())
|
|
8 q();
|
|
9 }
|
|
10
|
|
11 lib.load.quiet <- function( package ) {
|
|
12 package <- as.character(substitute(package))
|
|
13 suppressPackageStartupMessages( do.call( "library", list( package=package ) ) )
|
|
14 }
|
|
15 lib.load.quiet(getopt)
|
|
16 lib.load.quiet( amap )
|
|
17 ## if any of the faster clustering methods are available on this system, load them
|
|
18 if ( any( c( 'flashClust', 'fastcluster' ) %in% installed.packages() ) ) {
|
|
19 if ( 'flashClust' %in% installed.packages() ) {
|
|
20 lib.load.quiet( flashClust )
|
|
21 } else {
|
|
22 if ( 'fastcluster' %in% installed.packages() ) {
|
|
23 lib.load.quiet( fastcluster )
|
|
24 }
|
|
25 }
|
|
26 }
|
|
27
|
|
28 spec <- matrix( c( "data.fname", "d", 1, "character",
|
|
29 "num.k", "k", 1, "integer",
|
|
30 "distance.metric", "m", 2, "character",
|
|
31 "dist.obj", "D", 2, "logical",
|
|
32 "direction", "n", 2, "character",
|
|
33 "linkage", "l", 2, "character",
|
|
34 "output.name", "o", 2, "character"
|
|
35 ),
|
|
36 nc=4,
|
|
37 byrow=TRUE
|
|
38 )
|
|
39
|
|
40 opt <- getopt( spec=spec )
|
|
41
|
|
42 if ( is.null( opt$distance.metric ) ) { opt$distance.metric <- "euclidean" }
|
|
43 if ( is.null( opt$dist.obj ) ) { opt$dist.obj <- FALSE }
|
|
44 if ( is.null( opt$direction ) ) { opt$direction <- "cols" }
|
|
45 if ( is.null( opt$linkage ) ) { opt$linkage <- "average" }
|
|
46 if ( is.null( opt$num.k ) ) { opt$num.k <- 10 }
|
|
47 if ( is.null( opt$output.name ) ) { opt$output.name <- "hclust.result.rda" }
|
|
48
|
|
49 data <- as.matrix( read.delim( opt$data.fname, header=T, row.names=1 , check.names=FALSE ) )
|
|
50 if ( opt$direction == "cols" ) {
|
|
51 ## need to transpose b/c both kmeans & pam cluster the rows
|
|
52 ## this shouldn't have an effect upon a distance matrix
|
|
53 data <- t( data )
|
|
54 }
|
|
55 if ( opt$num.k > nrow( data ) ) {
|
|
56 err.msg <- paste( "K specified is greater than the number of elements (", opt$direction, ") in data matrix to be clustereed\n", sep="" )
|
|
57 stop( err.msg )
|
|
58 }
|
|
59
|
|
60 if ( opt$dist.obj ) {
|
|
61 dist.mat <- as.dist( data )
|
|
62 } else {
|
|
63 ## we're going to use the amap Dist function, but they misname their correlation
|
|
64 ## functions, so re-name them correctly
|
|
65 amap.distance <- c( "euclidean", "maximum", "manhattan", "canberra", "binary",
|
|
66 "pearson", "abspearson", "correlation", "abscorrelation", "spearman", "kendall" )
|
|
67 names( amap.distance ) <- c( "euclidean", "maximum", "manhattan", "canberra", "binary",
|
|
68 "cosine", "abscosine", "pearson", "abspearson", "spearman", "kendall" )
|
|
69
|
|
70 if ( ! opt$distance.metric %in% names( amap.distance ) ) stop("unsupported distance.")
|
|
71 dist.mat <- Dist( data, method=as.character( amap.distance[ opt$distance.metric ] ) )
|
|
72 attr( dist.mat, "method" ) <- opt$distance.metric
|
|
73 }
|
|
74
|
|
75 ## now, do the clustering
|
|
76 treecl.res <- hclust( dist.mat, method=opt$linkage )
|
|
77 cutree.res <- cutree( treecl.res, k=opt$num.k )
|
|
78 ##cl <- cbind( names( cutree.res ), as.numeric( cutree.res ) )
|
|
79 ##colnames( cl ) <- c( "ID", "class" )
|
|
80
|
|
81 if ( opt$direction == "cols" ) {
|
|
82 ## need to re-transpose the data back to it's original dimensionality
|
|
83 data <- t( data )
|
|
84 }
|
|
85
|
|
86 cl <- cutree.res
|
|
87 save( file=opt$output.name, treecl.res, cl, data )
|