annotate CHM.R @ 34:58437af2d675 draft

Uploaded
author insilico-bob
date Thu, 08 Feb 2018 14:45:46 -0500
parents 16593e40c2cd
children 605ec840a06b
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 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
25
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
26 colOrder <- createOrdering(dataMatrix, colOrderMethod, "col", colDistanceMeasure, colAgglomerationMethod)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
27 if (colOrderMethod == "Hierarchical") {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
28 writeHCDataTSVs(colOrder, colDendroFile, colOrderFile)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
29 writeHCCut(colOrder, colCut, paste(colOrderFile,".cut", sep=""))
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
30 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
31 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
32
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
33 #creates output files for hclust ordering
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
34 writeHCDataTSVs<-function(uDend, outputHCDataFileName, outputHCOrderFileName)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
35 {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
36 data<-cbind(uDend$merge, uDend$height, deparse.level=0)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
37 colnames(data)<-c("A", "B", "Height")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
38 write.table(data, file = outputHCDataFileName, append = FALSE, quote = FALSE, sep = "\t", row.names=FALSE)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
39
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
40 data=matrix(,length(uDend$labels),2);
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
41 for (i in 1:length(uDend$labels)) {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
42 data[i,1] = uDend$labels[i];
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
43 data[i,2] = which(uDend$order==i);
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
44 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
45 colnames(data)<-c("Id", "Order")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
46 write.table(data, file = outputHCOrderFileName, append = FALSE, quote = FALSE, sep = "\t", row.names=FALSE)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
47 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
48
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
49 #creates a classification file based on user specified cut of dendrogram
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
50 writeHCCut<-function(uDend, cutNum, outputCutFileName)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
51 {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
52 if (cutNum < 2) {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
53 return()
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
54 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
55 print (paste("Writing cut file ", outputCutFileName))
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
56 cut <- cutree(uDend, cutNum);
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
57 id <- names(cut);
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
58 data=matrix(,length(cut),2);
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
59 for (i in 1:length(cut)) {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
60 data[i,1] = id[i];
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
61 data[i,2] = sprintf("Cluster %d", cut[i]);
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
62 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
63
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
64 write.table(data, file = outputCutFileName, append = FALSE, quote = FALSE, sep = "\t", row.names=FALSE, col.names = FALSE);
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
65 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
66
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
67
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
68 createOrdering<-function(matrixData, orderMethod, direction, distanceMeasure, agglomerationMethod)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
69 {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
70 ordering <- NULL
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
71
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
72 if (orderMethod == "Hierarchical")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
73 {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
74
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
75 # Compute dendrogram for "Distance Metric"
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
76 distVals <- NULL
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
77 if(direction=="row") {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
78 if (distanceMeasure == "correlation") {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
79 geneGeneCor <- cor(t(matrixData), use="pairwise")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
80 distVals <- as.dist((1-geneGeneCor)/2)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
81 } else {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
82 distVals <- dist(matrixData, method=distanceMeasure)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
83 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
84 } else { #column
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
85 if (distanceMeasure == "correlation") {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
86 geneGeneCor <- cor(matrixData, use="pairwise")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
87 distVals <- as.dist((1-geneGeneCor)/2)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
88 } else {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
89 distVals <- dist(t(matrixData), method=distanceMeasure)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
90 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
91 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
92
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
93 # if (agglomerationMethod == "ward") {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
94 # ordering <- hclust(distVals * distVals, method="ward.D2")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
95 # } else {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
96 ordering <- hclust(distVals, method=agglomerationMethod)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
97 # }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
98 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
99 else if (orderMethod == "Random")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
100 {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
101 if(direction=="row") {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
102 headerList <- rownames(matrixData)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
103 ordering <- sample(headerList, length(headerList))
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
104 } else {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
105 headerList <- colnames(matrixData)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
106 ordering <- sample(headerList, length(headerList))
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
107 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
108 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
109 else if (orderMethod == "Original")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
110 {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
111 if(direction=="row") {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
112 ordering <- rownames(matrixData)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
113 } else {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
114 ordering <- colnames(matrixData)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
115 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
116 } else {
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
117 stop("createOrdering -- failed to find ordering method")
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
118 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
119 return(ordering)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
120 }
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
121 ### Initialize command line arguments and call performDataOrdering
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
122
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
123 options(warn=-1)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
124
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
125 args = commandArgs(TRUE)
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
126
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
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])
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
128
603d5c39e8dc Uploaded
insilico-bob
parents:
diff changeset
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]))