Mercurial > repos > bornea > dotplot_runner
comparison Dotplot_Release/R_dotPlot_nc.R @ 0:dfa3436beb67 draft
Uploaded
| author | bornea |
|---|---|
| date | Fri, 29 Jan 2016 09:56:02 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:dfa3436beb67 |
|---|---|
| 1 #!/usr/bin/env Rscript | |
| 2 | |
| 3 args <- commandArgs(trailingOnly = TRUE) | |
| 4 | |
| 5 pheatmapj_loc <- paste(args[9],"pheatmap_j.R",sep="/") | |
| 6 | |
| 7 library('latticeExtra') | |
| 8 library('RColorBrewer') | |
| 9 library('grid') | |
| 10 library(reshape2) | |
| 11 source(pheatmapj_loc) | |
| 12 | |
| 13 data.file <- read.table("SC_data.txt", sep="\t", header=TRUE, row.names=1) ### import spectral count data | |
| 14 data.file2 <- read.table("FDR_data.txt", sep="\t", header=TRUE, row.names=1) ### import FDR count data | |
| 15 bait_l <- scan(args[4], what="") ### import bait list | |
| 16 if(args[5] == 0) prey_l <- scan(args[6], what="") ### import prey list | |
| 17 methd <- args[7] | |
| 18 dist_methd <- args[8] | |
| 19 | |
| 20 #setting parameters | |
| 21 | |
| 22 Sfirst=as.numeric(args[1]) #first FDR cutoff | |
| 23 Ssecond=as.numeric(args[2]) #second FDR cutoff | |
| 24 maxp=as.integer(args[3]) #maximum value for a spectral count | |
| 25 | |
| 26 #extract only needed data | |
| 27 | |
| 28 if(args[5] == 0){ | |
| 29 remove <- vector() | |
| 30 remove <- prey_l[prey_l %in% row.names(data.file)] | |
| 31 prey_l <- prey_l[prey_l %in% remove] | |
| 32 remove <- bait_l[bait_l %in% names(data.file)] | |
| 33 bait_l <- bait_l[bait_l %in% remove] | |
| 34 data.file <- data.file[prey_l, bait_l] | |
| 35 data.file2 <- data.file2[prey_l, bait_l] | |
| 36 } else{ | |
| 37 remove <- vector() | |
| 38 remove <- bait_l[bait_l %in% names(data.file)] | |
| 39 bait_l <- bait_l[bait_l %in% remove] | |
| 40 data.file <- data.file[, bait_l] | |
| 41 data.file2 <- data.file2[, bait_l] | |
| 42 prey_keep = apply(data.file2, 1, function(x) sum(x<=Sfirst) >= 1) | |
| 43 data.file <- data.file[prey_keep,] | |
| 44 data.file2 <- data.file2[prey_keep,] | |
| 45 } | |
| 46 | |
| 47 #determine bait and prey ordering | |
| 48 | |
| 49 y_ord=factor(names(data.file[1,]),levels=bait_l) | |
| 50 | |
| 51 if(args[5] == 0){ | |
| 52 x_ord=factor(rownames(data.file),levels=prey_l) | |
| 53 } else { | |
| 54 | |
| 55 data.file <- data.file[which(rowSums(data.file) > 0),] | |
| 56 dist_prey <- dist(as.matrix(data.file), method= dist_methd) | |
| 57 | |
| 58 if(methd == "ward"){ | |
| 59 dist_prey <- dist_prey^2 | |
| 60 } | |
| 61 | |
| 62 hc_prey <- hclust(dist_prey, method = methd) | |
| 63 | |
| 64 data.file = data.file[hc_prey$order, , drop = FALSE] | |
| 65 data.file2 = data.file2[hc_prey$order, , drop = FALSE] | |
| 66 | |
| 67 x_ord=factor(row.names(data.file), levels=row.names(data.file)) | |
| 68 } | |
| 69 | |
| 70 df<-data.frame(y=rep(y_ord, nrow(data.file)) | |
| 71 ,x=rep(x_ord, each=ncol(data.file)) | |
| 72 ,z1=as.vector(t(data.file)) # Circle color | |
| 73 ,z2=as.vector(t(data.file/apply(data.file,1,max))) # Circle size | |
| 74 ,z3=as.vector(t(data.file2)) # FDR | |
| 75 ) | |
| 76 | |
| 77 df$z1[df$z1>maxp] <- maxp #maximum value for spectral count | |
| 78 df$z2[df$z2==0] <- NA | |
| 79 df$z3[df$z3>Ssecond] <- 0.05*maxp | |
| 80 df$z3[df$z3<=Ssecond & df$z3>Sfirst] <- 0.5*maxp | |
| 81 df$z3[df$z3<=Sfirst] <- 1*maxp | |
| 82 df$z4 <- df$z1 | |
| 83 df$z4[df$z4==0] <- 0 | |
| 84 df$z4[df$z4>0] <- 2.5 | |
| 85 | |
| 86 # The labeling for the colorkey | |
| 87 | |
| 88 labelat = c(0, maxp) | |
| 89 labeltext = c(0, maxp) | |
| 90 | |
| 91 # color scheme to use | |
| 92 | |
| 93 nmb.colors<-maxp | |
| 94 z.colors<-grey(rev(seq(0,0.9,0.9/nmb.colors))) #grayscale color scale | |
| 95 | |
| 96 #plot dotplot | |
| 97 | |
| 98 pl <- levelplot(z1~x*y, data=df | |
| 99 ,col.regions =z.colors #terrain.colors(100) | |
| 100 ,scales = list(x = list(rot = 90), y=list(cex=0.8), tck=0) # rotates X,Y labels and changes scale | |
| 101 ,colorkey = FALSE | |
| 102 #,colorkey = list(space="bottom", width=1.5, height=0.3, labels=list(at = labelat, labels = labeltext)) #put colorkey at top with my labeling scheme | |
| 103 ,xlab="Prey", ylab="Bait" | |
| 104 ,panel=function(x,y,z,...,col.regions){ | |
| 105 print(x) | |
| 106 z.c<-df$z1[ (df$x %in% as.character(x)) & (df$y %in% y)] | |
| 107 z.2<-df$z2[ (df$x %in% as.character(x)) & (df$y %in% y)] | |
| 108 z.3<-df$z3 | |
| 109 z.4<-df$z4 | |
| 110 panel.xyplot(x,y | |
| 111 ,as.table=TRUE | |
| 112 ,pch=21 # point type to use (circles in this case) | |
| 113 ,cex=((z.2-min(z.2,na.rm=TRUE))/(max(z.2,na.rm=TRUE)-min(z.2,na.rm=TRUE)))*3 #circle size | |
| 114 ,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 | |
| 115 ,col=z.colors[1+z.3] # border colors | |
| 116 ,lex=z.4 #border thickness | |
| 117 ) | |
| 118 } | |
| 119 #,main="Fold change" # graph main title | |
| 120 ) | |
| 121 if(ncol(data.file) > 4) ht=3.5+(0.36*((ncol(data.file)-1)-4)) else ht=3.5 | |
| 122 if(nrow(data.file) > 20) wd=8.25+(0.29*(nrow(data.file)-20)) else wd=5.7+(0.28*(nrow(data.file)-10)) | |
| 123 pdf("dotplot.pdf", onefile = FALSE, paper = "special", height = ht, width = wd, pointsize = 2) | |
| 124 print(pl) | |
| 125 dev.off() | |
| 126 | |
| 127 #plot heatmap | |
| 128 | |
| 129 heat_df <- acast(df, y~x, value.var="z1") | |
| 130 heat_df <- apply(heat_df, 2, rev) | |
| 131 | |
| 132 if(ncol(data.file) > 4) ht=3.5+(0.1*((ncol(data.file)-1)-4)) else ht=3.5 | |
| 133 if(nrow(data.file) > 20) wd=8.25+(0.1*(nrow(data.file)-20)) else wd=5+(0.1*(nrow(data.file)-10)) | |
| 134 pdf("heatmap_borders.pdf", onefile = FALSE, paper = "special", height = ht, width = wd, pointsize = 2) | |
| 135 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)) | |
| 136 dev.off() | |
| 137 | |
| 138 pdf("heatmap_no_borders.pdf", onefile = FALSE, paper = "special", height = ht, width = wd, pointsize = 2) | |
| 139 pheatmap_j(heat_df, scale="none", border_color=NA, cluster_rows=FALSE, cluster_cols=FALSE, col=colorRampPalette(c("#FFFFFF", brewer.pal(9,"Blues")))(100)) | |
| 140 dev.off() |
