-
Notifications
You must be signed in to change notification settings - Fork 47
/
dynamicEnhancer_plot.R
executable file
·125 lines (85 loc) · 4.35 KB
/
dynamicEnhancer_plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#dynamicEnhancer_plot.R
#produces a ranked table from the region map table
# The MIT License (MIT)
# Copyright (c) 2013 Charles Lin
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#===========================================================
#======================DEBUG PARAM==========================
#===========================================================
#name1 = 'EC_CON_BRD4'
#name2 = 'EC_TNF_BRD4'
#stitchedFile = 'mergeTest/EC_BRD4_CON_ROSE/HG18_EC_MERGED_SUPERS_-0_+0_0KB_STITCHED_ENHANCER_REGION_MAP.txt'
#outFile = gsub('REGION_MAP','DELTA',stitchedFile)
#setwd('/Volumes/bradnerlab/projects/athero')
#stitched_regions= read.delim('mergeTest/EC_BRD4_CON_ROSE/HG18_EC_MERGED_SUPERS_-0_+0_0KB_STITCHED_ENHANCER_REGION_MAP.txt',header=TRUE,sep='\t')
#===========================================================
#===============READING IN ARGUMNETS========================
#===========================================================
args <- commandArgs()
print(args[3:7])
stitchedFile = args[3]
name1 = args[4]
name2 = args[5]
median1 = as.numeric(args[6])
median2 = as.numeric(args[7])
#===========================================================
#=================MAKING DELTA TABLE========================
#===========================================================
outFile = gsub('REGION_MAP','DELTA',stitchedFile)
stitched_regions = read.delim(stitchedFile,header=TRUE)
if(ncol(stitched_regions) == 10){
factor1 = stitched_regions[,7] - stitched_regions[,8]
factor2 = stitched_regions[,9] - stitched_regions[,10]
}else{
factor1 = stitched_regions[,7]
factor2 = stitched_regions[,8]
}
#prevent negative values and 0 division
factor1[factor1 <= 1] <- 1
factor2[factor2 <= 1] <- 1
factor1 = factor1/median1
factor2 = factor2/median2
deltaFactor = log2(factor2/factor1)
deltaFactor[deltaFactor < -8] <- -8
deltaFactor[deltaFactor > 8] <- 8
deltaOrder = order(deltaFactor,decreasing=TRUE)
#write the new table
newTable = cbind(stitched_regions[deltaOrder,1:6],deltaFactor[deltaOrder],factor1[deltaOrder],factor2[deltaOrder],1:length(deltaFactor),1)
colnames(newTable)[7:11] = c(paste('LOG2_',name2,'_VS_',name1,'_CHANGE',sep=''),name1,name2,'RANK','IS_SUPER')
write.table(newTable,outFile,quote=FALSE,sep='\t',row.names=FALSE)
#===========================================================
#===============PLOTTING WATERFALL==========================
#===========================================================
colorSpectrum <- colorRampPalette(c("green","black","black","red"))(100)
#setting a color data range
minValue <- -1.5
maxValue <- 1.5
color_cuts <- seq(minValue,maxValue,length=100)
color_cuts <- c(min(deltaFactor,na.rm=TRUE), color_cuts,max(deltaFactor,na.rm=TRUE))
#add one extra min color to even out sampling
colorSpectrum <- c(colorSpectrum[1],colorSpectrum[1],colorSpectrum)
colorVector = c()
for(i in deltaOrder){
delta = deltaFactor[i]
color = colorSpectrum[max(which(color_cuts <= delta))]
colorVector =c(colorVector,color)
}
outPlot = gsub('txt','pdf',outFile)
pdf(file=outPlot,width =10,height=5)
plot(1:length(deltaFactor),deltaFactor[deltaOrder],ylim =c(-1.2*max(abs(deltaFactor)),1.2*max(abs(deltaFactor))),type='l',lwd=2,ylab=paste('Log2 change in signal ',name2,' over ',name1,sep=''),xlab=paste('Super-enhancer regions in either ',name1,' or ',name2,sep=''))
lines(1:length(deltaFactor),deltaFactor[deltaOrder],type='h',lwd=3,col=colorVector)
dev.off()