annotate CHM.R @ 5:94f5ea40c807 draft

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