-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROSE_callSuper.R
218 lines (145 loc) · 8.95 KB
/
ROSE_callSuper.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#============================================================================
#==============SUPER-ENHANCER CALLING AND PLOTTING FUNCTIONS=================
#============================================================================
#This function calculates the cutoff by sliding a diagonal line and finding where it is tangential (or as close as possible)
calculate_cutoff <- function(inputVector, drawPlot=TRUE,...){
inputVector <- sort(inputVector)
inputVector[inputVector<0]<-0 #set those regions with more control than ranking equal to zero
slope <- (max(inputVector)-min(inputVector))/length(inputVector) #This is the slope of the line we want to slide. This is the diagonal.
xPt <- floor(optimize(numPts_below_line,lower=1,upper=length(inputVector),myVector= inputVector,slope=slope)$minimum) #Find the x-axis point where a line passing through that point has the minimum number of points below it. (ie. tangent)
y_cutoff <- inputVector[xPt] #The y-value at this x point. This is our cutoff.
if(drawPlot){ #if TRUE, draw the plot
plot(1:length(inputVector), inputVector,type="l",...)
b <- y_cutoff-(slope* xPt)
abline(v= xPt,h= y_cutoff,lty=2,col=8)
points(xPt,y_cutoff,pch=16,cex=0.9,col=2)
abline(coef=c(b,slope),col=2)
title(paste("x=",xPt,"\ny=",signif(y_cutoff,3),"\nFold over Median=",signif(y_cutoff/median(inputVector),3),"x\nFold over Mean=",signif(y_cutoff/mean(inputVector),3),"x",sep=""))
axis(1,sum(inputVector==0),sum(inputVector==0),col.axis="pink",col="pink") #Number of regions with zero signal
}
return(list(absolute=y_cutoff,overMedian=y_cutoff/median(inputVector),overMean=y_cutoff/mean(inputVector)))
}
#this is an accessory function, that determines the number of points below a diagnoal passing through [x,yPt]
numPts_below_line <- function(myVector,slope,x){
yPt <- myVector[x]
b <- yPt-(slope*x)
xPts <- 1:length(myVector)
return(sum(myVector<=(xPts*slope+b)))
}
convert_stitched_to_bed <- function(inputStitched,trackName,trackDescription,outputFile,splitSuper=TRUE,score=c(),superRows=c(),baseColor="0,0,0",superColor="255,0,0"){
outMatrix <- matrix(data="",ncol=4+ifelse(length(score)==nrow(inputStitched),1,0),nrow=nrow(inputStitched))
outMatrix[,1] <- as.character(inputStitched$CHROM)
outMatrix[,2] <- as.character(inputStitched$START)
outMatrix[,3] <- as.character(inputStitched$STOP)
outMatrix[,4] <- as.character(inputStitched$REGION_ID)
if(length(score)==nrow(inputStitched)){
score <- rank(score,ties.method="first")
score <- length(score)-score+1 #Stupid rank only does smallest to largest.
outMatrix[,5] <- as.character(score)
}
trackDescription <- paste(trackDescription,"\nCreated on ",format(Sys.time(), "%b %d %Y"),collapse="",sep="")
trackDescription <- gsub("\n","\t", trackDescription)
tName <- gsub(" ","_",trackName)
cat('track name="', tName,'" description="', trackDescription,'" itemRGB=On color=',baseColor,"\n",sep="",file=outputFile)
write.table(file= outputFile,outMatrix,sep="\t",quote=FALSE,row.names=FALSE,col.names=FALSE,append=TRUE)
if(splitSuper==TRUE){
cat("\ntrack name=\"Super_", tName,'" description="Super ', trackDescription,'" itemRGB=On color=', superColor,"\n",sep="",file=outputFile,append=TRUE)
write.table(file= outputFile,outMatrix[superRows,,drop=FALSE],sep="\t",quote=FALSE,row.names=FALSE,col.names=FALSE,append=TRUE)
}
}
convert_stitched_to_gateway_bed <- function(inputStitched,outputFileRoot,splitSuper=TRUE,score=c(),superRows=c()){
outMatrix <- matrix(data="",ncol=6,nrow=nrow(inputStitched))
outMatrix[,1] <- as.character(inputStitched$CHROM)
outMatrix[,2] <- as.character(inputStitched$START)
outMatrix[,3] <- as.character(inputStitched$STOP)
outMatrix[,4] <- as.character(inputStitched$REGION_ID)
if(length(score)==nrow(inputStitched)){
score <- rank(score,ties.method="first")
score <- length(score)-score+1 #Stupid rank only does smallest to largest.
outMatrix[,5] <- as.character(score)
}
outMatrix[,6] <- as.character(rep('.',nrow(outMatrix)))
outputFile1 = paste(outputFileRoot,'_Gateway_Enhancers.bed',sep='')
write.table(file= outputFile1,outMatrix,sep="\t",quote=FALSE,row.names=FALSE,col.names=FALSE,append=TRUE)
if(splitSuper==TRUE){
outputFile2 = paste(outputFileRoot,'_Gateway_SuperEnhancers.bed',sep='')
write.table(file= outputFile2,outMatrix[superRows,,drop=FALSE],sep="\t",quote=FALSE,row.names=FALSE,col.names=FALSE,append=TRUE)
}
}
writeSuperEnhancer_table <- function(superEnhancer,description,outputFile,additionalData=NA){
description <- paste("#",description,"\nCreated on ",format(Sys.time(), "%b %d %Y"),collapse="",sep="")
description <- gsub("\n","\n#",description)
cat(description,"\n",file=outputFile)
if(is.matrix(additionalData)){
if(nrow(additionalData)!=nrow(superEnhancer)){
warning("Additional data does not have the same number of rows as the number of super enhancers.\n--->>> ADDITIONAL DATA NOT INCLUDED <<<---\n")
}else{
superEnhancer <- cbind(superEnhancer,additionalData)
superEnhancer = superEnhancer[order(superEnhancer$enhancerRank),]
}
}
write.table(file=outputFile,superEnhancer,sep="\t",quote=FALSE,row.names=FALSE,append=TRUE)
}
#============================================================================
#===================SUPER-ENHANCER CALLING AND PLOTTING======================
#============================================================================
args <- commandArgs()
print('THESE ARE THE ARGUMENTS')
print(args)
#ARGS
outFolder = args[3]
enhancerFile = args[4]
enhancerName = args[5]
wceName = args[6]
#Read enhancer regions with closestGene columns
stitched_regions <- read.delim(file= enhancerFile,sep="\t")
#perform WCE subtraction. Using pipeline table to match samples to proper background.
rankBy_factor = colnames(stitched_regions)[7]
prefix = unlist(strsplit(rankBy_factor,'_'))[1]
if(wceName == 'NONE'){
rankBy_vector = as.numeric(stitched_regions[,7])
}else{
wceName = colnames(stitched_regions)[8]
print('HERE IS THE WCE NAME')
print(wceName)
rankBy_vector = as.numeric(stitched_regions[,7])-as.numeric(stitched_regions[,8])
}
#SETTING NEGATIVE VALUES IN THE rankBy_vector to 0
rankBy_vector[rankBy_vector < 0] <- 0
#FIGURING OUT THE CUTOFF
cutoff_options <- calculate_cutoff(rankBy_vector, drawPlot=FALSE,xlab=paste(rankBy_factor,'_enhancers'),ylab=paste(rankByFactor,' Signal','- ',wceName),lwd=2,col=4)
#These are the super-enhancers
superEnhancerRows <- which(rankBy_vector> cutoff_options$absolute)
typicalEnhancers = setdiff(1:nrow(stitched_regions),superEnhancerRows)
enhancerDescription <- paste(enhancerName," Enhancers\nCreated from ", enhancerFile,"\nRanked by ",rankBy_factor,"\nUsing cutoff of ",cutoff_options$absolute," for Super-Enhancers",sep="",collapse="")
#MAKING HOCKEY STICK PLOT
plotFileName = paste(outFolder,enhancerName,'_Plot_points.png',sep='')
png(filename=plotFileName,height=600,width=600)
signalOrder = order(rankBy_vector,decreasing=TRUE)
if(wceName == 'NONE'){
plot(length(rankBy_vector):1,rankBy_vector[signalOrder], col='red',xlab=paste(rankBy_factor,'_enhancers'),ylab=paste(rankBy_factor,' Signal'),pch=19,cex=2)
}else{
plot(length(rankBy_vector):1,rankBy_vector[signalOrder], col='red',xlab=paste(rankBy_factor,'_enhancers'),ylab=paste(rankBy_factor,' Signal','- ',wceName),pch=19,cex=2)
}
abline(h=cutoff_options$absolute,col='grey',lty=2)
abline(v=length(rankBy_vector)-length(superEnhancerRows),col='grey',lty=2)
lines(length(rankBy_vector):1,rankBy_vector[signalOrder],lwd=4, col='red')
text(0,0.8*max(rankBy_vector),paste(' Cutoff used: ',cutoff_options$absolute,'\n','Super-Enhancers identified: ',length(superEnhancerRows)),pos=4)
dev.off()
#Writing a bed file
bedFileName = paste(outFolder,enhancerName,'_Enhancers_withSuper.bed',sep='')
convert_stitched_to_bed(stitched_regions,paste(rankBy_factor,"Enhancers"), enhancerDescription,bedFileName,score=rankBy_vector,splitSuper=TRUE,superRows= superEnhancerRows,baseColor="0,0,0",superColor="255,0,0")
bedFileRoot = paste(outFolder,enhancerName,sep='')
convert_stitched_to_gateway_bed(stitched_regions,bedFileRoot,splitSuper=TRUE,score=rankBy_vector,superRows = superEnhancerRows)
#This matrix is just the super_enhancers
true_super_enhancers <- stitched_regions[superEnhancerRows,]
additionalTableData <- matrix(data=NA,ncol=2,nrow=nrow(stitched_regions))
colnames(additionalTableData) <- c("enhancerRank","isSuper")
additionalTableData[,1] <- nrow(stitched_regions)-rank(rankBy_vector,ties.method="first")+1
additionalTableData[,2] <- 0
additionalTableData[superEnhancerRows,2] <- 1
#Writing enhancer and super-enhancer tables with enhancers ranked and super status annotated
enhancerTableFile = paste(outFolder,enhancerName,'_AllEnhancers.table.txt',sep='')
writeSuperEnhancer_table(stitched_regions, enhancerDescription,enhancerTableFile, additionalData= additionalTableData)
superTableFile = paste(outFolder,enhancerName,'_SuperEnhancers.table.txt',sep='')
writeSuperEnhancer_table(true_super_enhancers, enhancerDescription,superTableFile, additionalData= additionalTableData[superEnhancerRows,])