annotate CHM.R @ 41:8acca16f3921 draft

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