annotate Dotplot_Release/R_dotPlot_hc.R @ 21:cfac73f960c8 draft

Uploaded
author bornea
date Thu, 14 Apr 2016 16:34:43 -0400
parents bc752a05f16d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
bc752a05f16d Uploaded
bornea
parents:
diff changeset
1 #!/usr/bin/env Rscript
bc752a05f16d Uploaded
bornea
parents:
diff changeset
2
bc752a05f16d Uploaded
bornea
parents:
diff changeset
3 args <- commandArgs(trailingOnly = TRUE)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
4
bc752a05f16d Uploaded
bornea
parents:
diff changeset
5 pheatmapj_loc <- paste(args[6],"pheatmap_j.R",sep="/")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
6 heatmap2j_loc <- paste(args[6],"heatmap_2j.R",sep="/")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
7
bc752a05f16d Uploaded
bornea
parents:
diff changeset
8 library('latticeExtra')
bc752a05f16d Uploaded
bornea
parents:
diff changeset
9 library('RColorBrewer')
bc752a05f16d Uploaded
bornea
parents:
diff changeset
10 library('grid')
bc752a05f16d Uploaded
bornea
parents:
diff changeset
11 library(reshape2)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
12 library('gplots')
bc752a05f16d Uploaded
bornea
parents:
diff changeset
13 library('gtools')
bc752a05f16d Uploaded
bornea
parents:
diff changeset
14 source(pheatmapj_loc)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
15 source(heatmap2j_loc)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
16
bc752a05f16d Uploaded
bornea
parents:
diff changeset
17 data.file <- read.table("SC_data.txt", sep="\t", header=TRUE, row.names=1) ### import spectral count data
bc752a05f16d Uploaded
bornea
parents:
diff changeset
18 data.file2 <- read.table("FDR_data.txt", sep="\t", header=TRUE, row.names=1) ### import FDR count data
bc752a05f16d Uploaded
bornea
parents:
diff changeset
19
bc752a05f16d Uploaded
bornea
parents:
diff changeset
20 #setting parameters
bc752a05f16d Uploaded
bornea
parents:
diff changeset
21
bc752a05f16d Uploaded
bornea
parents:
diff changeset
22 Sfirst=as.numeric(args[1]) #first FDR cutoff
bc752a05f16d Uploaded
bornea
parents:
diff changeset
23 Ssecond=as.numeric(args[2]) #second FDR cutoff
bc752a05f16d Uploaded
bornea
parents:
diff changeset
24 maxp=as.integer(args[3]) #maximum value for a spectral count
bc752a05f16d Uploaded
bornea
parents:
diff changeset
25 methd <- args[4]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
26 dist_methd <- args[5]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
27
bc752a05f16d Uploaded
bornea
parents:
diff changeset
28 #determine bait and prey ordering
bc752a05f16d Uploaded
bornea
parents:
diff changeset
29
bc752a05f16d Uploaded
bornea
parents:
diff changeset
30 dist_bait <- dist(as.matrix(t(data.file)), method= dist_methd) # "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski"
bc752a05f16d Uploaded
bornea
parents:
diff changeset
31 dist_prey <- dist(as.matrix(data.file), method= dist_methd)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
32
bc752a05f16d Uploaded
bornea
parents:
diff changeset
33 if(methd == "ward"){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
34 dist_bait <- dist_bait^2 #comment out this line and the next if not using Ward's method of clustering
bc752a05f16d Uploaded
bornea
parents:
diff changeset
35 dist_prey <- dist_prey^2
bc752a05f16d Uploaded
bornea
parents:
diff changeset
36 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
37
bc752a05f16d Uploaded
bornea
parents:
diff changeset
38 hc_bait <- hclust(dist_bait, method = methd) # method = "average", "single", "complete", "ward", "mcquitty", "median" or "centroid"
bc752a05f16d Uploaded
bornea
parents:
diff changeset
39 hc_prey <- hclust(dist_prey, method = methd)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
40
bc752a05f16d Uploaded
bornea
parents:
diff changeset
41 data.file = data.file[hc_prey$order, , drop = FALSE]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
42 data.file = data.file[, hc_bait$order, drop = FALSE]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
43 data.file2 = data.file2[hc_prey$order, , drop = FALSE]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
44 data.file2 = data.file2[, hc_bait$order, drop = FALSE]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
45
bc752a05f16d Uploaded
bornea
parents:
diff changeset
46 x_ord=factor(row.names(data.file), levels=row.names(data.file))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
47 y_ord=factor(names(data.file[1,]), levels=names(data.file[1,]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
48
bc752a05f16d Uploaded
bornea
parents:
diff changeset
49 df<-data.frame(y=rep(y_ord, nrow(data.file))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
50 ,x=rep(x_ord, each=ncol(data.file))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
51 ,z1=as.vector(t(data.file)) # Circle color
bc752a05f16d Uploaded
bornea
parents:
diff changeset
52 ,z2=as.vector(t(data.file/apply(data.file,1,max))) # Circle size
bc752a05f16d Uploaded
bornea
parents:
diff changeset
53 ,z3=as.vector(t(data.file2)) # FDR
bc752a05f16d Uploaded
bornea
parents:
diff changeset
54 )
bc752a05f16d Uploaded
bornea
parents:
diff changeset
55
bc752a05f16d Uploaded
bornea
parents:
diff changeset
56 df$z1[df$z1>maxp] <- maxp #maximum value for spectral count
bc752a05f16d Uploaded
bornea
parents:
diff changeset
57 df$z2[df$z2==0] <- NA
bc752a05f16d Uploaded
bornea
parents:
diff changeset
58 df$z3[df$z3>Ssecond] <- 0.05*maxp
bc752a05f16d Uploaded
bornea
parents:
diff changeset
59 df$z3[df$z3<=Ssecond & df$z3>Sfirst] <- 0.5*maxp
bc752a05f16d Uploaded
bornea
parents:
diff changeset
60 df$z3[df$z3<=Sfirst] <- 1*maxp
bc752a05f16d Uploaded
bornea
parents:
diff changeset
61 df$z4 <- df$z1
bc752a05f16d Uploaded
bornea
parents:
diff changeset
62 df$z4[df$z4==0] <- 0
bc752a05f16d Uploaded
bornea
parents:
diff changeset
63 df$z4[df$z4>0] <- 2.5
bc752a05f16d Uploaded
bornea
parents:
diff changeset
64
bc752a05f16d Uploaded
bornea
parents:
diff changeset
65 # The labeling for the colorkey
bc752a05f16d Uploaded
bornea
parents:
diff changeset
66
bc752a05f16d Uploaded
bornea
parents:
diff changeset
67 labelat = c(0, maxp)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
68 labeltext = c(0, maxp)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
69
bc752a05f16d Uploaded
bornea
parents:
diff changeset
70 # color scheme to use
bc752a05f16d Uploaded
bornea
parents:
diff changeset
71
bc752a05f16d Uploaded
bornea
parents:
diff changeset
72 nmb.colors<-maxp
bc752a05f16d Uploaded
bornea
parents:
diff changeset
73 z.colors<-grey(rev(seq(0,0.9,0.9/nmb.colors))) #grayscale color scale
bc752a05f16d Uploaded
bornea
parents:
diff changeset
74
bc752a05f16d Uploaded
bornea
parents:
diff changeset
75 #plot dotplot
bc752a05f16d Uploaded
bornea
parents:
diff changeset
76
bc752a05f16d Uploaded
bornea
parents:
diff changeset
77 pl <- levelplot(z1~x*y, data=df
bc752a05f16d Uploaded
bornea
parents:
diff changeset
78 ,col.regions =z.colors #terrain.colors(100)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
79 ,scales = list(x = list(rot = 90), y=list(cex=0.8), tck=0) # rotates X,Y labels and changes scale
bc752a05f16d Uploaded
bornea
parents:
diff changeset
80 ,colorkey = FALSE
bc752a05f16d Uploaded
bornea
parents:
diff changeset
81 #,colorkey = list(space="bottom", width=1.5, height=0.3, labels=list(at = labelat, labels = labeltext)) #put colorkey at top with my labeling scheme
bc752a05f16d Uploaded
bornea
parents:
diff changeset
82 ,xlab="Prey", ylab="Bait"
bc752a05f16d Uploaded
bornea
parents:
diff changeset
83 ,panel=function(x,y,z,...,col.regions){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
84 print(x)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
85 z.c<-df$z1[ (df$x %in% as.character(x)) & (df$y %in% y)]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
86 z.2<-df$z2[ (df$x %in% as.character(x)) & (df$y %in% y)]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
87 z.3<-df$z3
bc752a05f16d Uploaded
bornea
parents:
diff changeset
88 z.4<-df$z4
bc752a05f16d Uploaded
bornea
parents:
diff changeset
89 panel.xyplot(x,y
bc752a05f16d Uploaded
bornea
parents:
diff changeset
90 ,as.table=TRUE
bc752a05f16d Uploaded
bornea
parents:
diff changeset
91 ,pch=21 # point type to use (circles in this case)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
92 ,cex=((z.2-min(z.2,na.rm=TRUE))/(max(z.2,na.rm=TRUE)-min(z.2,na.rm=TRUE)))*3 #circle size
bc752a05f16d Uploaded
bornea
parents:
diff changeset
93 ,fill=z.colors[floor((z.c-min(z.c,na.rm=TRUE))*nmb.colors/(max(z.c,na.rm=TRUE)-min(z.c,na.rm=TRUE)))+1] # circle colors
bc752a05f16d Uploaded
bornea
parents:
diff changeset
94 ,col=z.colors[1+z.3] # border colors
bc752a05f16d Uploaded
bornea
parents:
diff changeset
95 ,lex=z.4 #border thickness
bc752a05f16d Uploaded
bornea
parents:
diff changeset
96 )
bc752a05f16d Uploaded
bornea
parents:
diff changeset
97 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
98 #,main="Fold change" # graph main title
bc752a05f16d Uploaded
bornea
parents:
diff changeset
99 )
bc752a05f16d Uploaded
bornea
parents:
diff changeset
100 if(ncol(data.file) > 4) ht=3.5+(0.36*((ncol(data.file)-1)-4)) else ht=3.5
bc752a05f16d Uploaded
bornea
parents:
diff changeset
101 if(nrow(data.file) > 20) wd=8.25+(0.29*(nrow(data.file) -20)) else wd=5.7+(0.28*(nrow(data.file) -10))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
102 pdf("dotplot.pdf", onefile = FALSE, paper = "special", height = ht, width = wd, pointsize = 2)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
103 print(pl)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
104 dev.off()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
105
bc752a05f16d Uploaded
bornea
parents:
diff changeset
106 #plot bait vs prey heatmap
bc752a05f16d Uploaded
bornea
parents:
diff changeset
107
bc752a05f16d Uploaded
bornea
parents:
diff changeset
108 heat_df <- acast(df, y~x, value.var="z1")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
109 heat_df <- apply(heat_df, 2, rev)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
110
bc752a05f16d Uploaded
bornea
parents:
diff changeset
111 if(ncol(data.file) > 4) ht=3.5+(0.1*((ncol(data.file)-1)-4)) else ht=3.5
bc752a05f16d Uploaded
bornea
parents:
diff changeset
112 if(nrow(data.file) > 20) wd=8.25+(0.1*(nrow(data.file)-20)) else wd=5+(0.1*(nrow(data.file)-10))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
113 pdf("heatmap_borders.pdf", onefile = FALSE, paper = "special", height = ht, width = wd, pointsize = 2)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
114 pheatmap_j(heat_df, scale="none", border_color="black", border_width = 0.1, cluster_rows=FALSE, cluster_cols=FALSE, col=colorRampPalette(c("#FFFFFF", brewer.pal(9,"Blues")))(100))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
115 dev.off()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
116
bc752a05f16d Uploaded
bornea
parents:
diff changeset
117 pdf("heatmap_no_borders.pdf", onefile = FALSE, paper = "special", height = ht, width = wd, pointsize = 2)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
118 pheatmap_j(heat_df, scale="none", border_color=NA, cluster_rows=FALSE, cluster_cols=FALSE, col=colorRampPalette(c("#FFFFFF", brewer.pal(9,"Blues")))(100))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
119 dev.off()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
120
bc752a05f16d Uploaded
bornea
parents:
diff changeset
121 #plot bait vs bait heatmap using dist matrix
bc752a05f16d Uploaded
bornea
parents:
diff changeset
122 dist_bait <- dist_bait/max(dist_bait)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
123 pdf("bait2bait.pdf", onefile = FALSE, paper = "special")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
124 heatmap_2j(as.matrix(dist_bait), trace="none", scale="none", density.info="none", col=rev(colorRampPalette(c("#FFFFFF", brewer.pal(9,"Blues")))(100)), xMin=0, xMax=1, margins=c(1.5*max(nchar(rownames(as.matrix(dist_bait)))),1.5*max(nchar(colnames(as.matrix(dist_bait))))))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
125 dev.off()