-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot.r
302 lines (251 loc) · 15 KB
/
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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
args = commandArgs(TRUE)
RE_len = args[1] # either 4 or 6
if(length(args)>1) { report_dir = args[2] } else { report_dir = './report' }
rainbow_w_offset <- function(L, offset = NA){
if(is.na(offset)) offset = floor(L/2)
color = rainbow(L+ offset)[1:L]
return( color )
}
library(plotosaurus)
library(stringr)
source("https://raw.githubusercontent.com/SooLee/d3forNozzleR/master/interactive_multiline_d3prep.r") # for d3 integration
plot_orientation_proportion_vs_distance <- function(x, RE_len, xlim=c(2,5), no_xlabel=FALSE){
matplot(x$distance,x[,c('proportion.Inner','proportion.Outer','proportion.Right','proportion.Left')],pch=19,type='o',xlab="", ylab="Proportion",lwd=1,lty=1, col=COLOR4(), axes=F, xlim=xlim)
exp_axis(xlim,1)
axis(2)
box()
if(no_xlabel==FALSE) mtext("distance",side=1,line=2.5)
legend("topright",c('Inner','Outer','Right','Left'),col=COLOR4(),bty="n",pch=19,lwd=1,lty=1)
if(RE_len==4) { abline(v=3.5, lty=2, col="grey") # 4-cutters at 3kb
} else { abline(v=4.5, lty=2, col="grey") } # 6-cutters at 30kb
}
plot_orientation_log10count_vs_distance <- function(x, RE_len, xlim=c(2,5), no_xlabel=FALSE){
ind = which(x$distance>=xlim[1] & x$distance<=xlim[2] & x$log10count.Inner!=-100 & x$log10count.Outer!=-100 & x$log10count.Right!=-100 & x$log10count.Left!=-100)
ylim=range(x[ind,c('log10count.Inner','log10count.Outer','log10count.Right','log10count.Left')])
matplot(x$distance,x[,c('log10count.Inner','log10count.Outer','log10count.Right','log10count.Left')],pch=19,type='o',xlab="", ylab="Contact frequency",lwd=1,lty=1,ylim=ylim, col=COLOR4(), axes=F, xlim=xlim)
exp_axis(xlim,1)
exp_axis(ylim,2)
box()
if(no_xlabel==FALSE) mtext("distance",side=1,line=2.5)
legend("bottomright", c('Inner','Outer','Right','Left'), col=COLOR4(), bty="n", pch=19, lwd=1, lty=1)
if(RE_len==4) { abline(v=3.5, lty=2, col="grey") # 4-cutters at 3kb
} else { abline(v=4.5, lty=2, col="grey") } # 6-cutters at 30kb
}
plot_contact_probability_vs_distance <- function(x, xlim=c(3.2,8), no_xlabel=FALSE) {
ylim=range(x$log10prob)
plot(x$distance,x$log10prob,pch=19,type='o',xlab="", ylab="Contact probability",lwd=1,lty=1,ylim=ylim,axes=F,xlim=xlim)
exp_axis(xlim,1)
exp_axis(ylim,2)
box()
if(no_xlabel==FALSE) mtext("distance",side=1,line=2.5)
abline(v=4, lty=2, col="grey80")
abline(v=5.5, lty=2, col="grey80")
# slope
tad=which(x$distance>=4 & x$distance<=5.5)
xx=x$distance[tad]
yy=x$log10prob[tad]
tad_slope=glm(yy~xx)$coefficients[2]
tad_intercept=glm(yy~xx)$coefficients[1]
spacing=abs(yy[1]*0.07)
text(xx[1], yy[1]+spacing, paste("slope=",round(tad_slope,2),sep=' '), pos=4)
abline(a=tad_intercept + spacing, b=tad_slope, lty=2)
return(round(tad_slope,2));
}
# entropy
entropy<-function(xx)-sum(xx*log2(xx))
plot_entropy <- function(x, xlim=c(2,5), plt=c(0.2,0.95,0.2,0.95)){
par(las=1,lend=2)
par(plt=plt)
entropies=apply(x[,c('proportion.Inner','proportion.Outer','proportion.Right','proportion.Left')],1,entropy)
plot(x$distance, entropies,type='o',pch=19,ylab="entropy",xlab="distance",xlim=xlim,axes=F)
exp_axis(xlim,1)
axis(2)
}
plot_sd_with_cutoff <- function(x, xlim=c(2,5)){
x=x[which(x$distance>=xlim[1] & x$distance<=xlim[2]),];
sds=apply(x[,c('proportion.Inner','proportion.Outer','proportion.Right','proportion.Left')],1,sd)
plot(x$distance, log10(sds), type='o',pch=19,ylab="sd",xlab="distance",xlim=xlim,axes=F, col=COLOR3B()[1])
exp_axis(xlim,1)
exp_axis(log10(sds), 2, 3)
box()
almost_converged = which(sds<0.02)
well_converged = which(sds<0.005)
points(x$distance[almost_converged], log10(sds[almost_converged]), col=COLOR3B()[2], pch=19)
points(x$distance[well_converged], log10(sds[well_converged]), col=COLOR3B()[3], pch=19)
abline(v=x[almost_converged[1] ,"distance"],col=COLOR3B()[2],lty=2)
abline(v=x[well_converged[1] ,"distance"],col=COLOR3B()[3],lty=2)
legend("topright",c("sd<0.02","sd<0.005"),col=COLOR3B()[2:3],pch=19,bty='n')
## determination for convergence
if(RE_len==4) { conv_ind = which(x$distance>=3.5) } else { conv_ind = which(x$distance>=4.5) }
if(length(setdiff(conv_ind, well_converged))==0) { return('Very Good');
} else if(length(setdiff(conv_ind, almost_converged))==0) { return('Good');
} else { return('Not converged'); }
}
plot_contact_frequency_vs_genomic_separation_per_chr <- function(x) {
y=data.frame(x[,grep('log10prob_per_chr',colnames(x))])
valid = which(x$distance > 3.5 & x$distance < 7)
y=y[valid,]
ylim = range(apply(y,2,function(xx){ ind =which(xx>-90); if(length(ind)==0) return(NA) else return( range(xx[ind])) }), na.rm=T)
L = ncol(y)
chrnames = sub("log10prob_per_chr.","",colnames(y))
plt0 = par('plt', no.readonly=FALSE)
plt1 = plt0; plt2= plt0
pltx.plotend = (plt0[2] - plt0[1]) * 7/8 + plt0[1]
pltx.legendstart = (plt0[2] - plt0[1]) * 29/32 + plt0[1]
plt1[2] = pltx.plotend
plt2[1] = pltx.legendstart
par(plt=plt1)
matplot(x$distance[valid],y,ylim=ylim, pch=19, type='l', lty=1, lwd=1, col=rainbow_w_offset(L), xlab="genomic separation", ylab="Contact frequency", axes=F)
exp_axis(x$distance[valid], 1)
exp_axis(ylim, 2)
box()
abline(v=4, lty=2, col="grey80")
abline(v=5.5, lty=2, col="grey80")
par(new=T, plt=plt2)
plot.new(); plot.window(c(0,1),c(0,1), xaxs='i', yaxs='i')
legend("topleft", chrnames, col = rainbow_w_offset(L), lty=1, lwd=1, bty='n' ,xpd=T, cex=0.8)
}
prep_plot_contact_frequency_vs_genomic_separation_per_chr_d3 <- function(x, sample_name, report_dir, xmin=3.5, xmax=7) {
ycolumns = colnames(x)[grep('log10prob_per_chr',colnames(x))]
y = data.frame(x[,ycolumns])
valid = which(x$distance > xmin & x$distance < xmax)
y=y[valid,]
ylim = range(apply(y,2,function(xx){ ind =which(xx>-90); if(length(ind)==0) return(NA) else return( range(xx[ind])) }), na.rm=T)
ymin= ylim[1]
ymax = ylim[2]
L = ncol(y)
chrnames = sub("log10prob_per_chr.","",colnames(y))
colors = substr(str_to_lower(rainbow_w_offset(L)),1,7)
xlab = "log10 genomic separation"
ylab = "log10 contact frequency"
div_id = paste("d3div_contact_frequency_vs_genomic_separation_per_chr_", sample_name, "___", sep="")
js_file_prefix = "distanceplot_perchr"
tsvfile = paste("./", sample_name, ".plot_table.out", sep="")
tsvcoldata = data.frame(columnx="distance", columny=ycolumns, color=colors, offcolor="#999999", name=chrnames, stringsAsFactors=FALSE)
return( create_d3_js_for_interactive_multiline_plot(sample_name, xmin, xmax, ymin, ymax, div_id, xlab, ylab, report_dir, js_file_prefix, tsvfile, tsvcoldata) )
}
########################
require( "Nozzle.R1" )
generate_pairsqc_report <- function ( sample_name = NA, d3_prep_res = NULL) {
if(is.na(sample_name)) {
report_title = "PairsQC Report"
} else {
report_title = paste("PairsQC Report for sample", sample_name, sep=" ")
}
# Phase 1: create report elements
r <- newCustomReport( report_title );
d3script <- newHtml (d3_prep_res[[1]]$html_common_script_tag);
s1 <- newSection( "Summary table" );
s2 <- newSection( "Proportion of read orientation versus genomic separation");
s3 <- newSection( "Number of reads versus genomic separation, stratified by read orientation" );
s4 <- newSection( "Contact probability versus genomic separation" );
s5 <- newSection( "Contact probability versus genomic separation, per chromosome" );
references <- newSection( "References" );
# References
refRao = newCitation( authors= 'Rao et al.', title= 'A 3D Map of the Human Genome at Kilobase Resolution Reveals Principles of Chromatin Looping', publication= 'Cell', issue= '159:7', pages='1665-1680', year='2014' )
refSanborn = newCitation( authors= 'Sanborn et al.', title= 'Chromatin extrusion explains key features of loop and domain formation in wild-type and engineered genome', publication= 'Proc. Natl. Acad. Sci. USA', issue= '112:47', pages='E6456-E6465', year='2015' )
refImakaev = newCitation( authors= 'Imakaev et al.', title= 'Iterative correction of Hi-C data reveals hallmarks of chromosome organization', publication= 'Nature Methods', issue= '9:10', pages='999-1003', year='2012' )
# section 1
table_files = list.files('.', pattern='*summary.out$')
sample_names = sub('.?summary.out','',table_files, perl=T, fixed=F)
tableData1 = do.call(data.frame, sapply(table_files, function(table_file){
tableData=read.table(table_file,stringsAsFactors=F, header=F, sep="\t")
colnames(tableData) = c('QC field','value')
return(tableData)
}, simplify=F))[,c(1,seq(2,2*length(table_files),2))]
colnames(tableData1) = c('QC field', sample_names)
t1 <- newTable( tableData1, significantDigits=0, exportId="TABLE_1", "Summary Table" );
p1 <- newParagraph( "Cis-to-trans ratio is computed as the ratio of long-range cis reads (>20kb) to trans reads plus long-range cis reads. Typically, cis-to-trans ratio higher than 40% is required. Percentage of long-range cis reads is the ratio of long-range cis reads to total number of reads. Minimum 15% is required and 40% or higher suggests a good library", asReference( refRao ), ". Convergence is determined as standard deviation of proportions of four read orientations to be <0.002 (Very good) or <0.05 (Good) (See below section ", asStrong("Proportion of read orientation versus genomic separation"), "). The slope of log10 contact probability vs distance between 10kb ~ 300kb representing TAD is also provided as well. (See below section ", asStrong("Contact probability versus genomic separation"), ".)");
# section 2
# using sample_names from section 1 (same ordering)
png2_files = paste('plots/', sample_names, '.proportion.png',sep='')
pdf2_files = gsub('png$','pdf',png2_files)
f2list <- mapply(function(png2, pdf2, sample_name) {
newFigure( png2, fileHighRes=pdf2, exportId=paste("FIGURE_2", sample_name, sep="."),
paste("Proportion of read orientation versus genomic separation : ", asStrong(sample_name), sep=""));
}, png2_files, pdf2_files, sample_names, SIMPLIFY=FALSE)
eq2a = '10^3.5'
eq2b = '10^4.5'
p2 <- newParagraph("Contact frequency (number of reads, left) and proportion of reads (right) are shown, stratified by read orientation. Good four-cutter and six-cutter samples would converge at ~3kb (", eq2a, ") and ~30kb (", eq2b, "), respectively", asReference( refRao ), ". Convergence is determined by the standard deviation of the proportions being less than 0.005.")
# section 3
#png3= 'plots/log10counts.png'
#pdf3= 'plots/log10counts.pdf'
#f3 <- newFigure( png3, fileHighRes=pdf3, exportId="FIGURE_3", "Number of reads versus genomic separation, stratified by read orientation");
# section 4
# using sample_names from section 1 (same ordering)
png4_files = paste('plots/', sample_names, '.log10prob.png',sep='')
pdf4_files = gsub('png$','pdf',png4_files)
f4list <- mapply(function(png4, pdf4, sample_name) {
newFigure( png4, fileHighRes=pdf4,
paste("Contact probability versus genomic separation : ", asStrong(sample_name), sep=""));
}, png4_files, pdf4_files, sample_names, SIMPLIFY=FALSE)
p4 <- newParagraph( "Contact probability (number of reads, normalized by number of bins and bin size) is shown with respect to genomic separation between mates", asReference( refImakaev), asReference( refSanborn ), ". The slope between distance 10kb ~ 300kb (10^4 ~ 10^5.5) representing a TAD is calculated. A good mitotic sample would have a slope close to ~ -0.76", asReference( refSanborn ), "." );
# section 5
# this section has d3 elements
ss5divlist <- mapply(function(sn, k) newHtml(paste("<div class='figure'><div class='caption'><p><strong>Interactive Figure ",k,". </strong>Contact probability versus genomic separation, per chromosome : ", asStrong(sn), "</div><div id='", d3_prep_res[[sn]]$div_id, "'></div></div>",d3_prep_res[[sn]]$html_script_tag, sep="")), sample_names, 1:length(sample_names), SIMPLIFY=FALSE);
# Phase 2: assemble report structure bottom-up
s1 <- addTo( s1, p1, t1);
s2 <- addTo( s2, p2);
for(f2 in f2list) { s2 <- addTo( s2, f2); }
#s3 <- addTo( s3, f3);
s4 <- addTo( s4, p4 );
for(f4 in f4list) { s4 <- addTo( s4, f4); }
# s5 <- addTo( s5, f5);
for(i in 1:length(sample_names)) { s5 <- addTo( s5, ss5divlist[[i]] ); }
references <- addTo( references, refRao, refImakaev, refSanborn )
#r <- addTo( r, s1, s2, s3, s4, references );
#r <- addTo( r, s1, s2, s4, s5, references );
r <- addTo( r, d3script, s1, s2, s4, s5, references );
# Phase 3: render report to file
writeReport( r, filename="pairsqc_report" ); # w/o extension
}
##################
cwd = getwd()
sample_names = gsub('.?plot_table.out$', '', list.files(report_dir, pattern='*.plot_table.out$'), perl=T, fixed=F)
plot_for_sample<-function(sample_name, report_dir) {
setwd(cwd)
plot_dir = paste(report_dir,"plots",sep="/")
plot_table_file = paste(report_dir, "/", sample_name, ".plot_table.out", sep="")
x=read.table(plot_table_file,sep="\t",stringsAsFactors=F,header=T)
dir.create(plot_dir, showWarnings = FALSE, recursive = TRUE)
setwd(plot_dir)
res = pngpdf_preset( list(
function() plot_orientation_log10count_vs_distance(x, RE_len),
function() plot_orientation_proportion_vs_distance(x, RE_len, no_xlabel=T),
function() plot_sd_with_cutoff(x)
), paste(sample_name, "proportion", sep="."), stylefunc0, get_preset(3), add.date=FALSE
)
convergence_determinant = res[[3]];
# pngpdf_preset( list(
# function() plot_contact_probability_vs_distance(x),
# function() plot_contact_frequency_vs_genomic_separation_per_chr(x)
# ), paste(sample_name, "log10prob", sep="."), stylefunc0, get_preset(2,'h50'), add.date=FALSE
#)
res = pngpdf_preset( list(
function() plot_contact_probability_vs_distance(x)
), paste(sample_name, "log10prob", sep="."), stylefunc0, get_preset(1), add.date=FALSE
)
slope = res[[1]]
#pngpdf.nodate( function()plot_contact_probability_vs_distance(x) ,"log10prob")
#pngpdf.nodate( function()plot_contact_frequency_vs_genomic_separation_per_chr(x), "log10prob_chr", height=5.5 )
#pngpdf.nodate( function()plot_entropy(x), "entropy", height=4)
# d3 plot
setwd(cwd)
d3plot_prep_res = prep_plot_contact_frequency_vs_genomic_separation_per_chr_d3(x, sample_name, report_dir)
## printing cis-trans stats and post-plot stats to summary.out
res_all = as.data.frame(rbind(c("convergence",convergence_determinant), c("slope", slope)), stringsAsFactors=FALSE)
colnames(res_all)=c('V1','V2')
cis_file = paste(report_dir, '/', sample_name, ".cis_to_trans.out", sep="");
res_file = paste(report_dir, '/', sample_name, ".summary.out", sep="");
original_res = read.table(cis_file, stringsAsFactors=FALSE, header=FALSE, sep="\t");
colnames(original_res)=c('V1','V2')
res_all = rbind(original_res, res_all)
write.table(res_all, res_file, quote=FALSE, col.names=FALSE, row.names=FALSE, sep="\t")
setwd(plot_dir)
return(d3plot_prep_res)
}
d3_prep_res = sapply(sample_names, plot_for_sample, report_dir = report_dir, simplify=FALSE)
print(d3_prep_res)
setwd(cwd)
setwd(report_dir)
generate_pairsqc_report(d3_prep_res = d3_prep_res)