3
|
1 ### This method generates a row and column ordering given an input matrix and ordering methods.
|
|
2 ###
|
|
3 ### matrixData - numeric matrix
|
|
4 ### rowOrderMethod - Hierarchical, Original, Random
|
|
5 ### rowDistanceMeasure - For clustering, distance measure. May be: euclidean, binary, manhattan, maximum, canberra, minkowski, or correlation.
|
|
6 ### rowAgglomerationMethod - For clustering, agglomeration method. May be: 'average' for Average Linkage, 'complete' for Complete Linkage,
|
|
7 ### 'single' for Single Linkage, 'ward', 'mcquitty', 'median', or 'centroid'.
|
|
8 ### colOrderMethod
|
|
9 ### colDistanceMeasure
|
|
10 ### colAgglomerationMethod
|
|
11 ### rowOrderFile - output file of order of rows
|
|
12 ### rowDendroFile - output file of row dendrogram
|
|
13 ### colOrderFile - output file of order of cols
|
|
14 ### colDendroFile - output file of col dendrogram
|
|
15 ### rowCut - For rows the number of classifications to automatically generate based on dendrogram into a classification file. 0 for turned off.
|
|
16 ### colCut - For columns the number of classifications to automatically generate based on dendrogram into a classification file. 0 for turned off.
|
|
17
|
|
18 performDataOrdering<-function(dataFile, rowOrderMethod, rowDistanceMeasure, rowAgglomerationMethod, colOrderMethod, colDistanceMeasure, colAgglomerationMethod,rowOrderFile, colOrderFile, rowDendroFile, colDendroFile, rowCut, colCut)
|
|
19 {
|
|
20 dataMatrix = read.table(dataFile, header=TRUE, sep = "\t", row.names = 1, as.is=TRUE, na.strings=c("NA","N/A","-","?"))
|
|
21 rowOrder <- createOrdering(dataMatrix, rowOrderMethod, "row", rowDistanceMeasure, rowAgglomerationMethod)
|
|
22 if (rowOrderMethod == "Hierarchical") {
|
|
23 writeHCDataTSVs(rowOrder, rowDendroFile, rowOrderFile)
|
|
24 }
|
|
25
|
|
26 colOrder <- createOrdering(dataMatrix, colOrderMethod, "col", colDistanceMeasure, colAgglomerationMethod)
|
|
27 if (colOrderMethod == "Hierarchical") {
|
|
28 writeHCDataTSVs(colOrder, colDendroFile, colOrderFile)
|
|
29 writeHCCut(colOrder, colCut, paste(colOrderFile,".cut", sep=""))
|
|
30 }
|
|
31 }
|
|
32
|
|
33 #creates output files for hclust ordering
|
|
34 writeHCDataTSVs<-function(uDend, outputHCDataFileName, outputHCOrderFileName)
|
|
35 {
|
|
36 data<-cbind(uDend$merge, uDend$height, deparse.level=0)
|
|
37 colnames(data)<-c("A", "B", "Height")
|
|
38 write.table(data, file = outputHCDataFileName, append = FALSE, quote = FALSE, sep = "\t", row.names=FALSE)
|
|
39
|
|
40 data=matrix(,length(uDend$labels),2);
|
|
41 for (i in 1:length(uDend$labels)) {
|
|
42 data[i,1] = uDend$labels[i];
|
|
43 data[i,2] = which(uDend$order==i);
|
|
44 }
|
|
45 colnames(data)<-c("Id", "Order")
|
|
46 write.table(data, file = outputHCOrderFileName, append = FALSE, quote = FALSE, sep = "\t", row.names=FALSE)
|
|
47 }
|
|
48
|
|
49 #creates a classification file based on user specified cut of dendrogram
|
|
50 writeHCCut<-function(uDend, cutNum, outputCutFileName)
|
|
51 {
|
|
52 if (cutNum < 2) {
|
|
53 return()
|
|
54 }
|
|
55 print (paste("Writing cut file ", outputCutFileName))
|
|
56 cut <- cutree(uDend, cutNum);
|
|
57 id <- names(cut);
|
|
58 data=matrix(,length(cut),2);
|
|
59 for (i in 1:length(cut)) {
|
|
60 data[i,1] = id[i];
|
|
61 data[i,2] = sprintf("Cluster %d", cut[i]);
|
|
62 }
|
|
63
|
|
64 write.table(data, file = outputCutFileName, append = FALSE, quote = FALSE, sep = "\t", row.names=FALSE, col.names = FALSE);
|
|
65 }
|
|
66
|
|
67
|
|
68 createOrdering<-function(matrixData, orderMethod, direction, distanceMeasure, agglomerationMethod)
|
|
69 {
|
|
70 ordering <- NULL
|
|
71
|
|
72 if (orderMethod == "Hierarchical")
|
|
73 {
|
|
74
|
|
75 # Compute dendrogram for "Distance Metric"
|
|
76 distVals <- NULL
|
|
77 if(direction=="row") {
|
|
78 if (distanceMeasure == "correlation") {
|
|
79 geneGeneCor <- cor(t(matrixData), use="pairwise")
|
|
80 distVals <- as.dist((1-geneGeneCor)/2)
|
|
81 } else {
|
|
82 distVals <- dist(matrixData, method=distanceMeasure)
|
|
83 }
|
|
84 } else { #column
|
|
85 if (distanceMeasure == "correlation") {
|
|
86 geneGeneCor <- cor(matrixData, use="pairwise")
|
|
87 distVals <- as.dist((1-geneGeneCor)/2)
|
|
88 } else {
|
|
89 distVals <- dist(t(matrixData), method=distanceMeasure)
|
|
90 }
|
|
91 }
|
|
92
|
|
93 # if (agglomerationMethod == "ward") {
|
|
94 # ordering <- hclust(distVals * distVals, method="ward.D2")
|
|
95 # } else {
|
|
96 ordering <- hclust(distVals, method=agglomerationMethod)
|
|
97 # }
|
|
98 }
|
|
99 else if (orderMethod == "Random")
|
|
100 {
|
|
101 if(direction=="row") {
|
|
102 headerList <- rownames(matrixData)
|
|
103 ordering <- sample(headerList, length(headerList))
|
|
104 } else {
|
|
105 headerList <- colnames(matrixData)
|
|
106 ordering <- sample(headerList, length(headerList))
|
|
107 }
|
|
108 }
|
|
109 else if (orderMethod == "Original")
|
|
110 {
|
|
111 if(direction=="row") {
|
|
112 ordering <- rownames(matrixData)
|
|
113 } else {
|
|
114 ordering <- colnames(matrixData)
|
|
115 }
|
|
116 } else {
|
|
117 stop("createOrdering -- failed to find ordering method")
|
|
118 }
|
|
119 return(ordering)
|
|
120 }
|
|
121 ### Initialize command line arguments and call performDataOrdering
|
|
122
|
|
123 options(warn=-1)
|
|
124
|
|
125 args = commandArgs(TRUE)
|
|
126
|
|
127 performDataOrdering(dataFile=args[1], rowOrderMethod=args[2], rowDistanceMeasure=args[3], rowAgglomerationMethod=args[4], colOrderMethod=args[5], colDistanceMeasure=args[6], colAgglomerationMethod=args[7],rowOrderFile=args[8], colOrderFile=args[9], rowDendroFile=args[10], colDendroFile=args[11], rowCut=args[12], colCut=args[13])
|
|
128
|
|
129 #suppressWarnings(performDataOrdering(dataFile=args[1], rowOrderMethod=args[2], rowDistanceMeasure=args[3], rowAgglomerationMethod=args[4], colOrderMethod=args[5], colDistanceMeasure=args[6], colAgglomerationMethod=args[7],rowOrderFile=args[8], colOrderFile=args[9], rowDendroFile=args[10], colDendroFile=args[11]))
|