annotate CHM_Advanced.R @ 3:9359e0a0d40b draft

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