-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepOriCodeNB_forsubmission_final.Rmd
1397 lines (1076 loc) · 48.4 KB
/
RepOriCodeNB_forsubmission_final.Rmd
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Analysis of Replication Origins on 3D chromatin contact networks"
output: html_notebook
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = "/home/vera/dbpersonal/Dropbox_work/RepOriData")
```
by Vera Pancaldi ([email protected])
This code produces the analysis for the paper
Three-dimensional connectivity and chromatin environment mediate the activation efficiency of mammalian DNA replication origins
Karolina Jodkowska1#, Vera Pancaldi2#, Ricardo Almeida^, Maria Rigau^, Osvaldo Graña-Castro, José M. Fernández-Justel, Sara Rodríguez-Acebes, Miriam Rubio, Enrique Carrillo, David Pisano, Fátima Al-Sharour, Alfonso Valencia, María Gómez, Juan Méndez*
Contents :
Loading and creation of origin bedfiles, origin networks, dataframe of origin efficiencies in all conditions
Randomization of origins preserving TSS
Assortativity analysis
Analysis of distance spanned in interactions (inter/intra TAD)
```{r environment, echo=TRUE, results=TRUE, message=FALSE}
datapath='/home/vera/dbpersonal/Dropbox_work/RepOriData'
source('assort_script.R')
source('coloursdef.R')
require(igraph)
require('data.table')
require(GenomicRanges)
```
Reading PCHiC data
```{r results=TRUE}
####Read PCHiC data
load(paste(datapath,'mESC_wt_and_KO.Rdata', sep='/'))
baitnames=unique(paste(chicmore[,1], chicmore[,2], chicmore[,3], sep='_'))
targets=unique(paste(chicmore[,6], chicmore[,7], chicmore[,8], sep='_'))
chicme=setdiff(targets,baitnames)
v1=chicmore[,c(1,2,3)]
colnames(v1)=c('chr', 'start', 'end')
v2=chicmore[,c(6,7,8)]
colnames(v2)=c('chr', 'start', 'end')
chicmore_all=rbind(v1, v2)
chicmore_allnodup=chicmore_all[!duplicated(chicmore_all),]
chicmore_allnodup[,1]=paste(rep('chr', nrow(chicmore_allnodup)),chicmore_allnodup[,1], sep='')
library(GenomicRanges)
bedchicmore=with(chicmore_allnodup, GRanges(chr, IRanges(start, end)))
bedchicmore$ID=paste(gsub('chr','',chicmore_allnodup[,1]), chicmore_allnodup[,2], chicmore_allnodup[,3], sep='_')
bedchicmoreall=with(chicmore_allnodup, GRanges(chr, IRanges(start, end)))
##keep ids of origins without chr to spot difference with network fragments
bedchicall=bedchicmore
chicwt=chicmore[which(chicmore$mESC_wt>5),]
v1=chicwt[,c(1,2,3)]
colnames(v1)=c('chr', 'start', 'end')
v2=chicwt[,c(6,7,8)]
colnames(v2)=c('chr', 'start', 'end')
chicwt_all=rbind(v1, v2)
chicwt_allnodup=chicwt_all[!duplicated(chicwt_all),]
chicwt_allnodup[,1]=paste(rep('chr', nrow(chicwt_allnodup)),chicwt_allnodup[,1], sep='')
library(GenomicRanges)
bedchicwt=with(chicwt_allnodup, GRanges(chr, IRanges(start, end)))
bedchicwt$ID=paste(gsub('chr','',chicwt_allnodup[,1]), chicwt_allnodup[,2], chicwt_allnodup[,3], sep='_')
bedchicmore=bedchicwt ####!!!!!!!!!!!!!!careful!, from now on bedchicmore is only for WT network
summary(abs(chicwt$dist))
chicwtmat=cbind(paste(chicwt[,1], chicwt[,2], chicwt[,3], sep='_'), paste(chicwt[,6], chicwt[,7], chicwt[,8], sep='_'), chicwt[,12])
Gchicwt=simplify(graph.data.frame(chicwtmat, vertices=NULL, directed=F)) #IGRAPH UN-- 55845 72231)
#Gchicwt
Gchicwtprom=induced.subgraph(Gchicwt, V(Gchicwt)[which(V(Gchicwt)$name %in% baitnames)])
Gchicwtprom=induced.subgraph(Gchicwtprom, V(Gchicwtprom)[which(degree(Gchicwtprom) >0)])
#Gchicwtprom
Gchicwtpromoe=Gchicwt-Gchicwtprom
Gchicwtpromoe=induced.subgraph(Gchicwtpromoe, V(Gchicwtpromoe)[which(degree(Gchicwtpromoe) >0)])
#Gchicwtpromoe
save(bedchicmore, Gchicwt, Gchicwtprom, Gchicwtpromoe, file='PCHiC_nets.Rdata')
# Print bait annotations for cytoscape}
###Print bait annotations for cytoscape
baitannot=chicwt[,c('baitChr','baitStart', 'baitEnd','baitName')]
baitannot=baitannot[!duplicated(baitannot),]
baitannot[,4]=sapply(as.vector(baitannot[,4]), function(x) {
return(unlist(strsplit(x, split='-'))[1])
})
rownames(baitannot)=paste(baitannot[,1], baitannot[,2], baitannot[,3], sep='_')
write.table(data.frame(rownames(baitannot),baitannot), paste(datapath,'PCHiC_baitannot_forcyto2019new.txt', sep='\t'), quote=F, sep='\t', row.names=F)
head(baitannot)
write.table(data.frame(rownames(baitannot), rep('prom', nrow(baitannot)) ), paste(datapath,'PCHiC_newproms.txt', sep='\t'), quote=F, sep='\t', row.names=F)
```
Load and process origin data with efficiencies
```{r results=TRUE }
###read efficiencies
#the files contain the following information for each ORI:
#chr|start|stop|read coverage|region length|efficiency
pa=paste(datapath,'backcor_downs_means_mm9/', sep='/')
fs=paste(pa,list.files(pa), sep='')
pb=paste(datapath,'backcor_downs_means_mm9/means_mm9/', sep='/')
fs=c(fs, paste(pb, list.files(pb), sep=''))
fs=fs[grep('.bed', fs)]
prematlist=list()
for (f in fs){
prematlist[[f]]=read.table(f, sep='\t', header=T)
}
a=sapply(names(prematlist), function(x){
vec=unlist(strsplit(x,split='/',x))
return(vec[grep('.bed', vec)])
})
names(prematlist)=as.vector(a)
names(prematlist)=gsub('mean_efficiency_', '', names(prematlist))
names(prematlist)=gsub('_mm9.bed', '', names(prematlist))
names(prematlist)[grep('JAN', names(prematlist))]='ALL-ORI'
prematlist=lapply(prematlist, function(x){
x=x[!duplicated(x),]
rownames(x)=paste(x[,1], x[,2],x[,3], sep='_')
return(x)
})
names(prematlist)
matlist=list()
matlist[['APH']]=prematlist[['aph']]
matlist[['APH-R']]=prematlist[['aph_responsive']]
matlist[['CDC6']]=prematlist[['cdc6']]
matlist[['CDC6-R']]=prematlist[['cdc6_responsive']]
matlist[['APH+CDC6-R']]=prematlist[['both_aph_cdc6']]
matlist[['WT']]=prematlist[['wt']]
matlist[['WT-nonCOMM']]=prematlist[['wt_NOT_constitutive']]
matlist[['APHnoconst']]=prematlist[['aph_NOT_constitutive']]
matlist[['CDC6noconst']]=prematlist[['cdc6_NOT_constitutive']]
matlist[['COMM']]=prematlist[['constitutive']]
matlist[['WTexAPH']]=prematlist[['wt_exclusive_vsAPH']]
matlist[['WTexCDC6']]=prematlist[['wt_exclusive_vsCDC6']]
matlist[['ALL-ORI']]=prematlist[['ALL-ORI']]
numoris=sapply(matlist, function(x){return(nrow(x))})
print(numoris)
```
Align origins to PCHiC fragments
```{r}
#Align bed files to fragments
bedlist=list()
for (m in names(matlist)){
colnames(matlist[[m]])=c('chrom', 'start', 'end', 'rawscore', 'normraw', 'cornotnorm', 'cornorm')
bedlist[[m]]=with(matlist[[m]], GRanges(chrom, IRanges(start, end)))
mcols(bedlist[[m]])=matlist[[m]][,c(4,5, 6, 7)]
bedlist[[m]]$ID=paste(matlist[[m]][,1],matlist[[m]][,2],matlist[[m]][,3], sep='_' )
}
names(bedlist)
######################################################################################################
#Efficiency calculations
matchl=list()
for (m in names(bedlist)){
#overlap this with bp fragments
over=findOverlaps(bedlist[[m]],bedchicmore)
match_hit <- data.frame(mcols(bedlist[[m]])[queryHits(over),],as.data.frame(mcols(bedchicmore)[subjectHits(over),]) )
colnames(match_hit)[6]='fragment'
matchl[[m]]=match_hit[!duplicated(match_hit),]
}
oril=lapply(matchl, function(x){
return(unique(x$fragment))
})
names(oril)=names(matchl)
Classic_oril=oril
save(bedlist, matlist, Classic_oril, file='../RepOriData/Data_may19.Rdata')
print(sapply(oril, length))
##statistics of origin networks
orinodes=as.vector(unique(unlist(oril)))
Pnodes=intersect(baitnames, V(Gchicwt)$name)
Onodes=setdiff(V(Gchicwt)$name, baitnames)
```
Calculate stats of mapping of oris to PCHiC fragments
```{r}
##Calculate how many origins are mapped to fragments in each dataset
mappedoril=sapply(matchl, function(x){
return(length(unique(x$ID)))
})
mappedoril_P=sapply(matchl, function(x){
return(length (unique( x[which(x$fragment %in% Pnodes),'ID'])))
})
mappedoril_PP=sapply(matchl, function(x){
return(length (unique( x[which(x$fragment %in% V(Gchicwtprom)$name),'ID'])))
})
mappedoril_O=sapply(matchl, function(x){
return(length (unique( x[which(x$fragment %in% Onodes),'ID'])))
})
mappedoril_PO=sapply(matchl, function(x){
return(length (unique( x[which(x$fragment %in% V(Gchicwtpromoe)$name),'ID'])))
})
mappedfrag=sapply(matchl, function(x){
return(length(unique(x$fragment)))
})
mapwt=matchl[[1]][,c(5,6)]
mapAllOri=matchl[['ALL-ORI']][,c(5,6)]
##Calculate how many origins match inside the same fragment
table(as.vector(table(mapAllOri$fragment)))
##Calculate how many fragments are assigned to each origin
table(as.vector(table(mapAllOri$ID)))
Orinum=table(mapwt$fragment)
##statistics of origin networks
orinodes=as.vector(unique(unlist(oril)))
Pnodes=intersect(baitnames, V(Gchicwt)$name)
Onodes=setdiff(V(Gchicwt)$name, baitnames)
Poris=intersect(Pnodes, orinodes)
Ooris=intersect(Onodes, orinodes)
PPoris=intersect(V(Gchicwtprom)$name, orinodes)
POEoris=intersect(V(Gchicwtpromoe)$name, orinodes)
Poriswt=intersect(Pnodes, oril[['WT']])
Ooriswt=intersect(Onodes, oril[['WT']])
PPoriswt=intersect(V(Gchicwtprom)$name, oril[['WT']])
POEoriswt=intersect(V(Gchicwtpromoe)$name, oril[['WT']])
```
Construct Ori-Net
```{r}
### Consider also nodes that have 0 degree in specific dataset for the moment
Classic_Goril=lapply(oril, function(x){
gra=induced.subgraph(Gchicwt, V(Gchicwt)[which(V(Gchicwt)$name %in% x)], impl='copy_and_delete')
gra=(simplify(gra))
#gra=delete.vertices(gra,V(gra)[which(degree(gra)==0)])
return(gra)
})
Goril_all=Reduce(graph.union, Classic_Goril)
Goril_all=simplify(Goril_all, remove.multiple=TRUE)
Goril=Classic_Goril
Classic_bedlist=bedlist
Classic_Goril_leaves=lapply(oril, function(x){
gra=induced.subgraph(Gchicwt, V(Gchicwt)[which(V(Gchicwt)$name %in% x)], impl='copy_and_delete')
gra=(simplify(gra))
return(gra)
})
save(Classic_Goril, Classic_bedlist, Classic_featefmat,Goril_all,Classic_Goril_leaves, orinodes, file=paste(datapath,'ClassicOri.Rdata', sep='/'))
write.table(get.edgelist(Goril[['ALL-ORI']]), paste(datapath,'PCHiC_AllOriWT.txt', sep='/'), quote=F, sep='\t')
OriNet=delete.vertices(Goril[['ALL-ORI']], V(Goril[['ALL-ORI']])[which(degree(Goril[['ALL-ORI']])==0)])
```
Load and process randomized origins
```{r Now do randomizations}
##now compare with random origins matching distanc to TSS or not
f=list.files(paste(datapath,'May2019_TSSRandom/TSSdistpres/wt_all/', sep='/'))
fn=f[grep('normal', f)]
fs=f[grep('TSS', f)]
randoritssl=list()
for (i in fs){
randoritssmat=read.table(paste(paste(datapath,'May2019_TSSRandom/TSSdistpres/wt_all/', sep='/'),i, sep=''), sep='\t', header=T)
randtssbed=with(randoritssmat, GRanges(chr, IRanges(start, end)))
mcols(randtssbed)=randoritssmat[,7]
colnames(mcols(randtssbed))='ef'
randtssbed$ID=paste(seqnames(randtssbed), start(randtssbed), end(randtssbed), sep='_')
randoritssl[[i]]=randtssbed
}
randmatchl=list()
for (m in names(randoritssl)){
#overlap this with bp fragments
over=findOverlaps(randoritssl[[m]],bedchicmore)
match_hit <- data.frame(mcols(randoritssl[[m]])[queryHits(over),],as.data.frame(mcols(bedchicmore)[subjectHits(over),]) )
colnames(match_hit)[3]='fragment'
randmatchl[[m]]=match_hit[!duplicated(match_hit),]
}
randoril=lapply(randmatchl, function(x){
return(unique(x$fragment))
})
mappedrandoril=lapply(randmatchl, function(x){
return(as.vector(unique(x$ID)))
})
mappedrandoril_P=lapply(randmatchl, function(x){
return(as.vector( unique( x[which(x$fragment %in% baitnames),'ID'])))
})
mappedrandoril_PP=lapply(randmatchl, function(x){
return(as.vector( unique( x[which(x$fragment %in% V(Gchicwtprom)$name),'ID'])))
})
mappedrandoril_O=lapply(randmatchl, function(x){
return(as.vector( unique( x[which(x$fragment %in% Onodes),'ID'])))
})
mappedrandoril_PO=lapply(randmatchl, function(x){
return(as.vector( unique( x[which(x$fragment %in% V(Gchicwtpromoe)$name),'ID'])))
})
names(randoril)=names(randmatchl)
randorinotssl=list()
for (i in fn){
randoritssmat=read.table(paste(paste(datapath,'May2019_TSSRandom/TSSdistpres/wt_all/', sep='/'),i, sep=''), sep='\t', header=T)
randtssbed=with(randoritssmat, GRanges(chr, IRanges(start, end)))
mcols(randtssbed)=randoritssmat[,7]
colnames(mcols(randtssbed))='ef'
randtssbed$ID=paste(seqnames(randtssbed), start(randtssbed), end(randtssbed), sep='_')
randorinotssl[[i]]=randtssbed
}
randmatchlno=list()
for (m in names(randorinotssl)){
#overlap this with bp fragments
over=findOverlaps(randorinotssl[[m]],bedchicmore)
match_hit <- data.frame(mcols(randorinotssl[[m]])[queryHits(over),],as.data.frame(mcols(bedchicmore)[subjectHits(over),]) )
colnames(match_hit)[3]='fragment'
randmatchlno[[m]]=match_hit[!duplicated(match_hit),]
}
randorilno=lapply(randmatchlno, function(x){
return(unique(x$fragment))
})
mappedrandorilnoTSS=lapply(randmatchlno, function(x){
return(as.vector(unique(x$ID)))
})
names(randorilno)=names(randmatchlno)
mappedrandorilnoTSS_P=lapply(randmatchlno, function(x){
return( as.vector( unique( x[which(x$fragment %in% Pnodes),'ID'])))
})
mappedrandorilnoTSS_PP=lapply(randmatchlno, function(x){
return( as.vector( unique( x[which(x$fragment %in% V(Gchicwtprom)$name) ,'ID'])))
})
mappedrandorilnoTSS_O=lapply(randmatchlno, function(x){
return( as.vector( unique( x[which(x$fragment %in% Onodes),'ID'])))
})
mappedrandorilnoTSS_PO=lapply(randmatchlno, function(x){
return( as.vector( unique( x[which(x$fragment %in% V(Gchicwtpromoe)$name) ,'ID'])))
})
orisumtable=cbind(mappedoril[['WT']],mappedoril_P[['WT']], mappedoril_O[['WT']], mappedoril_PP[['WT']],mappedoril_PO[['WT']] )
orisumtable_rand=cbind(mean(sapply(mappedrandoril, length)),mean(sapply(mappedrandoril_P, length)), mean(sapply(mappedrandoril_O, length)), mean(sapply(mappedrandoril_PP, length)), mean(sapply(mappedrandoril_PO, length)))
orisumtable_randMIN=cbind(min(sapply(mappedrandoril, length)),min(sapply(mappedrandoril_P, length)), min(sapply(mappedrandoril_O, length)), min(sapply(mappedrandoril_PP, length)), min(sapply(mappedrandoril_PO, length)))
orisumtable_randMAX=cbind(max(sapply(mappedrandoril, length)),max(sapply(mappedrandoril_P, length)), max(sapply(mappedrandoril_O, length)), max(sapply(mappedrandoril_PP, length)), max(sapply(mappedrandoril_PO, length)))
orisumtable_randnoTSS=cbind(mean(sapply(mappedrandorilnoTSS, length)),mean(sapply(mappedrandorilnoTSS_P, length)), mean(sapply(mappedrandorilnoTSS_O, length)), mean(sapply(mappedrandorilnoTSS_PP, length)), mean(sapply(mappedrandorilnoTSS_PO, length)))
orisumtable_randnoTSSMIN=cbind(min(sapply(mappedrandorilnoTSS, length)),min(sapply(mappedrandorilnoTSS_P, length)), min(sapply(mappedrandorilnoTSS_O, length)), min(sapply(mappedrandorilnoTSS_PP, length)), min(sapply(mappedrandorilnoTSS_PO, length)))
orisumtable_randnoTSSMAX=cbind(max(sapply(mappedrandorilnoTSS, length)),max(sapply(mappedrandorilnoTSS_P, length)), max(sapply(mappedrandorilnoTSS_O, length)), max(sapply(mappedrandorilnoTSS_PP, length)), max(sapply(mappedrandorilnoTSS_PO, length)))
orisum_tot=rbind(orisumtable, orisumtable_randnoTSS,orisumtable_randnoTSSMIN, orisumtable_randnoTSSMAX, orisumtable_rand,orisumtable_randMIN, orisumtable_randMAX )
rownames(orisum_tot)=c('WT', 'Origin Randomization Mean', 'Origin Randomization Min', 'Origin Randomization Max', 'TSS distance-preserving origin randomization', 'TSS distance-preserving origin randomization Min', 'TSS distance-preserving origin randomization Max')
colnames(orisum_tot)=c('PCHiC', 'P nodes', 'O nodes', 'PP subnetwork', 'PO subnetwork')
write.table(orisum_tot, paste(datapath,'/OriStats.tsv', sep='/'), sep='\t')
summary(sapply(randoritssl, length))
summary(sapply(randorinotssl, length))
```
Random origins characterization
```{r Random origins}
###make networks of random oris
randorilinPP=sapply(randoril, function(x){
return(length(intersect(V(Gchicwtprom)$name,x )))
})
randorilinPOE=sapply(randoril, function(x){
return(length(intersect(V(Gchicwtpromoe)$name,x )))
})
randorilinPnodes=sapply(randoril, function(x){
return(length(intersect(Pnodes,x )))
})
randorilinOnodes=sapply(randoril, function(x){
return(length(intersect(Onodes,x )))
})
randorilinPCHiCnodes=sapply(randoril, function(x){
return(length(intersect(V(Gchicwt)$name,x )))
})
randorilinAllP=sapply(randoril, function(x){
return(length(intersect(baitnames,x )))
})
randoril_presence=c(mean(randorilinPnodes),mean(randorilinOnodes), mean(randorilinPCHiCnodes),mean(randorilinPP), mean(randorilinPOE) )
###now look at noTSS
randorilnoinPP=sapply(randorilno, function(x){
return(length(intersect(V(Gchicwtprom)$name,x )))
})
randorilnoinPOE=sapply(randorilno, function(x){
return(length(intersect(V(Gchicwtpromoe)$name,x )))
})
randorilnoinPnodes=sapply(randorilno, function(x){
return(length(intersect(Pnodes,x )))
})
randorilnoinOnodes=sapply(randorilno, function(x){
return(length(intersect(Onodes,x )))
})
randorilnoinPCHiCnodes=sapply(randorilno, function(x){
return(length(intersect(V(Gchicwt)$name,x )))
})
randorilnoinAllP=sapply(randorilno, function(x){
return(length(intersect(baitnames,x )))
})
randorilno_presence=c(mean(randorilnoinPnodes),mean(randorilnoinOnodes), mean(randorilnoinPCHiCnodes),mean(randorilnoinPP), mean(randorilnoinPOE) )
```
Calculate OriAs
```{r}
##'###############################################################Without efficiency
vecl=lapply(oril[sel], function(x){
vec=rep(0, length(V(Gchicwt)))
vec[which(V(Gchicwt)$name %in% x)]<-1
return(vec)
})
featmat=Reduce(cbind, vecl)
colnames(featmat)=gsub('ef', '',names(oril[sel]))
colnames(featmat)[ncol(featmat)]='ALL-ORI'
rownames(featmat)=V(Gchicwt)$name
OriAs=calc_assort(Gchicwt, featmat)
OriAspp=calc_assort(Gchicwtprom, featmat)
OriAspoe=calc_assort(Gchicwtpromoe, featmat)
randoriAsl=list()
for (i in 1:100){
#print(i)
randoriAsl[[i]]=apply(featmat, 2, function(x){
proms=which(rownames(featmat) %in% baitnames)
rand=rep(0, length(x))
rand[proms]=sample(x[proms], length(proms),replace=F)
oes=which(rownames(featmat) %in% chicme)
rand[oes]=sample(x[oes], length(oes),replace=F)
return(rand)
})
}
##calculate rand chas
chasoriAsrandpresproml=lapply(randoriAsl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtprom, x))
})
chasoriAsrandprespromoel=lapply(randoriAsl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtpromoe, x))
})
chasoriAsrandprespromlvec=lapply(chasoriAsrandpresproml, unlist)
chasoriAsrandprespromldf=as.data.frame(chasoriAsrandprespromlvec)
chasoriAsrandprespromoelvec=lapply(chasoriAsrandprespromoel, unlist)
chasoriAsrandprespromoeldf=as.data.frame(chasoriAsrandprespromoelvec)
####randomization of whole PCHiC origins (without efficiency)
randsimpl=list()
for (i in 1:100){
#print(i)
randsimpl[[i]]=apply(featmat, 2, function(x){
rand=sample(x, length(x),replace=F)
return(rand)
})
}
##calculate rand chas
chasrandsimpl=lapply(randsimpl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwt, x))
})
chasrandsimplvec=lapply(chasrandsimpl, unlist)
chasrandsimpldf=as.data.frame(chasrandsimplvec)
```
Plots of OriAs
```{r}
####look for assortativity of oris in whole network
pdf(paste(datapath,'Figures/Supp/Supp4B_OriAs_wholePCHiC.pdf', sep='/'))
plot( colSums(featmat)[sel],unlist(OriAs), ylim=c(-0.05,0.1), pch=20,col=cols[sel],xlab='Number of nodes containing origins in PCHiC', ylab='OriAs in PCHiC network',
main='Assortativity of origins in PCHiC network')
for (i in 1:100){
points( colSums(featmat)[sel],chasrandsimpldf[,i], col=cols[sel])
}
text( colSums(featmat)[sel],unlist(OriAs), pos=1,offset=-6, sel, cex=1, srt=90)
points( colSums(featmat)[sel],unlist(OriAs), col='black')
abline(h=0)
dev.off()
pdf(paste(datapath,'Figures/Supp/Supp4C_OriAs_POE.pdf', sep='/'))
plot( colSums(featmat[which(rownames(featmat) %in% V(Gchicwtpromoe)$name),])[sel],unlist(OriAspoe), ylim=c(-0.2,0.1), pch=20,col=cols[sel],xlab='Number of nodes containing origins in PO subnetwork', ylab='OriAs in PO subnetwork',
main='Assortativity of origins in PCHiC POE network')
for (i in 1:100){
points( colSums(featmat[which(rownames(featmat) %in% V(Gchicwtpromoe)$name),])[sel],chasoriAsrandprespromoeldf[,i], col=cols[sel])
}
text( colSums(featmat[which(rownames(featmat) %in% V(Gchicwtpromoe)$name),])[sel],unlist(OriAspoe), pos=1,offset=-4, sel, cex=1, srt=90)
points( colSums(featmat[which(rownames(featmat) %in% V(Gchicwtpromoe)$name),])[sel],unlist(OriAspoe), col='black')
abline(h=0)
dev.off()
```
OriAs of randomized origins
```{r}
###now do it with random tss oris
randvecl=lapply(randoril, function(x){
vec=rep(0, length(V(Gchicwt)))
vec[which(V(Gchicwt)$name %in% x)]<-1
return(vec)
})
randfeatmat=Reduce(cbind, randvecl)
colnames(randfeatmat)=names(randoritssl)
rownames(randfeatmat)=V(Gchicwt)$name
RandOriAspp=calc_assort(Gchicwtprom, randfeatmat)
RandOriAspoe=calc_assort(Gchicwtpromoe, randfeatmat)
randrandoriAsl=list()
for (i in 1:100){
#print(i)
randrandoriAsl[[i]]=apply(randfeatmat, 2, function(x){
proms=which(rownames(randfeatmat) %in% baitnames)
rand=rep(0, length(x))
rand[proms]=sample(x[proms], length(proms),replace=F)
oes=which(rownames(featmat) %in% chicme)
rand[oes]=sample(x[oes], length(oes),replace=F)
return(rand)
})
}
##calculate rand chas
chasoriRandAsrandpresproml=lapply(randrandoriAsl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtprom, x))
})
chasoriRandAsrandprespromoel=lapply(randrandoriAsl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtpromoe, x))
})
chasoriRandAsrandprespromlvec=lapply(chasoriRandAsrandpresproml, unlist)
chasoriRandAsrandprespromldf=as.data.frame(chasoriRandAsrandprespromlvec)
```
Final plots - no efficiency
```{r}
#####
pdf(paste(datapath,'Figures/Assortativity/Figure4C_OriAs_PP_withrandtss.pdf', sep='/'))
plot( colSums(featmat[which(rownames(featmat) %in% baitnames),]),OriAspp,ylim=c(-0.02,0.2 ), pch=20,col=cols[sel],xlab='Number of nodes in PP subnetwork containing origins', ylab='OriAs in PP subnetwork',
main='Assortativity of origin presence in promoter subnetwork\n classic dataset')
for (i in 1:100){
points( colSums(featmat[which(rownames(featmat) %in% baitnames),]),chasoriAsrandprespromldf[,i], col=cols[sel])
}
text( colSums(featmat[which(rownames(featmat) %in% baitnames),]),OriAspp, pos=1,offset=-4, labels=names(OriAspp), cex=1, srt=90)
points( colSums(featmat[which(rownames(featmat) %in% baitnames),]),OriAspp, col='black')
abline(h=0)
for (i in 1:100){
points( colSums(randfeatmat[which(rownames(randfeatmat) %in% baitnames),]),chasoriRandAsrandprespromldf[,i], col='grey')
}
#text((colSums(randfeatmat[which(rownames(randfeatmat) %in% baitnames),])[1]),RandOriAspp[1], 'WT1 Rand',pos=1,offset=-4, cex=1, srt=90)
text((colSums(randfeatmat[which(rownames(randfeatmat) %in% baitnames),])[20]-100),RandOriAspp[20], 'WT Rand',pos=1,offset=-4, cex=1, srt=90)
points(colSums(randfeatmat[which(rownames(randfeatmat) %in% baitnames),]),RandOriAspp, pch=20, col='black' )
points(colSums(randfeatmat[which(rownames(randfeatmat) %in% baitnames),]), RandOriAspp)
dev.off()
```
Calculate fragment efficiencies and make efficiency feature matrix
```{r Calculate fragment efficiency}
require(data.table)
###now need to calculate efficiency per fragment
agefl=lapply(matchl, function(x){
data.dt <- data.table(x[,c(1,2,3,4,6)])
setkey(data.dt, fragment)
b=data.frame(data.dt[, lapply(.SD, mean), by =fragment])
rownames(b)=b[,1]
agef=b
return(agef)
})
names(agefl)
#Calculate fragment efficiency filling in zero values for frags without origins}
ageflext=lapply(agefl, function(x){
out=setdiff(V(Gchicwt)$name , rownames(x))
outval=cbind(out, rep(0, length(out)))
outval=data.frame(outval, outval[,-1], outval[,-1], outval[,-1])
colnames(outval)=colnames(agefl[[1]])
return(rbind(x,data.frame(outval)))
})
names(agefl)
head(agefl[[1]])
##Make a list of feature matrices with all efficiencies
#Choose directly the measure
measure='cornorm'
featefmat=matrix(0, ncol=length(matchl), nrow=length(V(Gchicwt)))
rownames(featefmat)=V(Gchicwt)$name
colnames(featefmat)=names(matchl)
intersec=intersect(rownames(featefmat),rownames(ageflext[[1]]) )
featefmat[intersec,1]=ageflext[[1]][intersec,measure]
for (i in names(ageflext)){
intersec=intersect(rownames(featefmat),rownames(ageflext[[i]]) )
featefmat[intersec,i]=ageflext[[i]][intersec,measure]
}
featefmat=apply(featefmat, c(1,2), as.numeric)
colnames(featefmat)=names(matchl)
#oriefAS=calc_assort(Gchicwtprom, featefmat)
write.table(data.frame(rownames(featefmat),featefmat), paste(datapath, 'Classic_efficiencies_forcyto.txt',sep='/'), quote=F, sep='\t', row.names=F)
Classic_featefmat=featefmat
```
*Calculate and plot OriEfAs*
```{r}
### Calculate assortativity
chasoripp=unlist(calc_assort(Gchicwtprom, featefmat))
chasoripoe=unlist(calc_assort(Gchicwtpromoe, featefmat))
chasori=calc_assort(Gchicwt,featefmat)
```
Perform permutations
```{r results=FALSE}
###Now do permutations that preserves proms and oes
randpresl=list()
for (i in 1:100){
#print(i)
randpresl[[i]]=apply(featefmat, 2, function(x){
proms=which(rownames(featefmat) %in% baitnames)
rand=rep(0, length(x))
rand[proms]=sample(x[proms], length(proms),replace=F)
oes=which(rownames(featefmat) %in% chicme)
rand[oes]=sample(x[oes], length(oes),replace=F)
return(rand)
})
}
##calculate rand chas
chasorirandpresproml=lapply(randpresl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtprom, x))
})
chasorirandprespromoel=lapply(randpresl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtpromoe, x))
})
chasorirandprespromlvec=lapply(chasorirandpresproml, unlist)
chasorirandprespromldf=as.data.frame(chasorirandprespromlvec)
chasorirandprespromoelvec=lapply(chasorirandprespromoel, unlist)
chasorirandprespromoeldf=as.data.frame(chasorirandprespromoelvec)
##randomization for whole PCHiC
randl=list()
for (i in 1:100){
#print(i)
randl[[i]]=apply(featefmat, 2, function(x){
rand=sample(x, length(x),replace=F)
return(rand)
})
}
##calculate rand chas
chasorirandl=lapply(randl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwt, x))
})
chasorirandlvec=lapply(chasorirandl, unlist)
chasorirandldf=as.data.frame(chasorirandlvec)
```
Calculation of ChAs of randomized oris
```{r}
###now need to calculate efficiency per fragment
Grandoritssl=lapply(randoril, function(x){
gra=induced.subgraph(Gchicwt, V(Gchicwt)[which(V(Gchicwt)$name %in% x)])
gra=delete.vertices(gra, V(gra)[which(degree(gra)==0)])
return(gra)
})
library(data.table)
###now need to calculate efficiency per fragment
randagefl=lapply(randmatchl, function(x){
data.dt <- data.table(x[,c(3,1)])
setkey(data.dt, fragment)
b=data.frame(data.dt[, lapply(.SD, mean), by =fragment])
rownames(b)=b[,1]
agef=b
return(agef)
})
##same value as adding 0 to fragments with no oris.
randageflext=lapply(randagefl, function(x){
out=setdiff(V(Gchicwt)$name , rownames(x))
outval=cbind(out, rep(0, length(out)))
outval=data.frame(outval)
colnames(outval)=colnames(randagefl[[1]])
rownames(outval)=outval[,1]
outval[,2]=as.numeric(outval[,2])
return(rbind(x,data.frame(outval)))
})
measure='ef'
featrandefmat=matrix(0, ncol=length(randmatchl), nrow=length(V(Gchicwt)))
rownames(featrandefmat)=V(Gchicwt)$name
colnames(featrandefmat)=names(randmatchl)
intersec=intersect(rownames(featrandefmat),rownames(randageflext[[1]]) )
featrandefmat[intersec,1]=randageflext[[1]][intersec,measure]
for (i in names(randageflext)){
intersec=intersect(rownames(featrandefmat),rownames(randageflext[[i]]) )
featrandefmat[intersec,i]=randageflext[[i]][intersec,measure]
}
featrandefmat=apply(featrandefmat, c(1,2), as.numeric)
colnames(featrandefmat)=names(randmatchl)
##same value as adding 0 to fragments with no oris.
randoripp=unlist(calc_assort(Gchicwtprom, featrandefmat))
randoripoe=unlist(calc_assort(Gchicwtpromoe, featrandefmat))
rrandpresl=list()
for (i in 1:100){
#print(i)
rrandpresl[[i]]=apply(featrandefmat, 2, function(x){
proms=which(rownames(featrandefmat) %in% baitnames)
rand=rep(0, length(x))
rand[proms]=sample(x[proms], length(proms),replace=F)
oes=which(rownames(featrandefmat) %in% chicme)
rand[oes]=sample(x[oes], length(oes),replace=F)
return(rand)
})
}
##calculate rand chas
chasrandorirandpresproml=lapply(rrandpresl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtprom, x))
})
chasrandorirandprespromoel=lapply(rrandpresl, function(x){
rownames(x)=V(Gchicwt)$name
return(calc_assort(Gchicwtpromoe, x))
})
chasrandorirandprespromlvec=lapply(chasrandorirandpresproml, unlist)
chasrandorirandprespromldf=as.data.frame(chasrandorirandprespromlvec)
chasrandorirandprespromoelvec=lapply(chasrandorirandprespromoel, unlist)
chasrandorirandprespromoeldf=as.data.frame(chasrandorirandprespromoelvec)
randnames=sapply(colnames(featrandefmat), function(x){
return(unlist(strsplit(x, split='_'))[7])
})
```
OriEfAs - with efficiency and randomizations
```{r}
pdf(paste(datapath,'Figures/Supp/Supp5D_OriEfAsPP_randoriTSSnew_alldata.pdf', sep='/'))
plot(colMeans(featefmat[which(rownames(featefmat) %in% baitnames),sel]), chasoripp[sel], col=cols[sel], pch=20,ylim=c(-0.05, 0.2),xlim=c(0.2,4),xlab='Mean origin efficiency in P nodes', ylab='OriEfAs in PP subnetwork', main='Assortativity of origin efficiency in promoter subnetwork\n classic dataset',
cex=1.5, cex.axis=1.5, cex.lab=1.5)
text(colMeans(featefmat[which(rownames(featefmat) %in% baitnames),sel]), chasoripp[sel], labels=sel, pos=1,offset=-3.5, cex=1, srt=90)
abline(h=0)
for (i in 1:100){
points( colMeans(featefmat[which(rownames(featefmat) %in% baitnames),sel]),chasorirandprespromldf[sel,i], col=cols[sel])
}
for (i in 1:100){
points( colMeans(featrandefmat[which(rownames(featrandefmat) %in% baitnames),]),chasrandorirandprespromldf[,i], col='grey', pch=20)
}
text(colMeans(featrandefmat[which(rownames(featrandefmat) %in% baitnames),])[1],randoripp[1], 'WT1 Rand',pos=1,offset=-3.5, cex=1, srt=90)
text(colMeans(featrandefmat[which(rownames(featrandefmat) %in% baitnames),])[20],randoripp[20], 'WT2 Rand',pos=1,offset=-3.5, cex=1, srt=90)
points(colMeans(featrandefmat[which(rownames(featrandefmat) %in% baitnames),]),randoripp, pch=20, col='black' )
points(colMeans(featefmat[which(rownames(featefmat) %in% baitnames),sel]), chasoripp[sel])
dev.off()
pdf(paste(datapath,'Figures/Supp/Supp5E_OriEfAsPO_randoriTSSnew_alldata_PO.pdf', sep='/'))
plot(colMeans(featefmat[which(rownames(featefmat) %in% V(Gchicwtpromoe)$name),sel]), chasoripoe[sel], col=cols[sel], pch=20,ylim=c(-0.205, 0.17),xlim=c(0.2,2),xlab='Mean origin efficiency in PO nodes', ylab='OriEfAs in PO subnetwork', main='Assortativity of origin efficiency in PO network')
text(colMeans(featefmat[which(rownames(featefmat) %in% V(Gchicwtpromoe)$name),sel]),chasoripoe[sel], labels=sel, pos=1,offset=-3.5, cex=1, srt=90)
abline(h=0)
for (i in 1:100){
points( colMeans(featefmat[which(rownames(featefmat) %in% V(Gchicwtpromoe)$name),sel]),chasorirandprespromoeldf[sel,i], col=cols[sel])
}
for (i in 1:100){
points( colMeans(featrandefmat[which(rownames(featrandefmat) %in% V(Gchicwtpromoe)$name),]),chasrandorirandprespromoeldf[,i], col='grey', pch=20)
}
text(colMeans(featrandefmat[which(rownames(featrandefmat) %in% V(Gchicwtpromoe)$name),])[1],randoripoe[1], 'WT1 Rand',pos=1,offset=-3.5, cex=1, srt=90)
text(colMeans(featrandefmat[which(rownames(featrandefmat) %in% V(Gchicwtpromoe)$name),])[20],randoripoe[20], 'WT2 Rand',pos=1,offset=-3.5, cex=1, srt=90)
points(colMeans(featrandefmat[which(rownames(featrandefmat) %in% V(Gchicwtpromoe)$name),]),randoripoe, pch=20, col='black' )
points(colMeans(featefmat[which(rownames(featefmat) %in% V(Gchicwtpromoe)$name),sel]), chasoripoe[sel])
dev.off()
```
***Characterise distances spanned by contacts in different networks***
```{r}
#Measure distance spanned
chicwtmat=data.frame(paste(chicwt[,1], chicwt[,2], chicwt[,3], sep='_'), paste(chicwt[,6], chicwt[,7], chicwt[,8], sep='_'), rowMeans(data.frame(chicwt[,2],chicwt[,3])-rowMeans(data.frame(chicwt[,7], chicwt[,8]))))
colnames(chicwtmat)[3]=c('dist')
chicwtmat[,3]=as.numeric(chicwtmat[,3])
Gchicwtdist=graph.data.frame(chicwtmat, directed=F)
Gchicwtdist=simplify(graph.data.frame(chicwtmat, directed=F),edge.attr.comb = 'min', remove.loops=T)
Gdist=Gchicwtdist
Gdistori=induced.subgraph(Gdist, V(Gdist)[which(V(Gdist)$name %in% orinodes)])
GdistoriAPH=induced.subgraph(Gdist, V(Gdist)[which(V(Gdist)$name %in% oril[['APH']])])
GdistoriCDC6=induced.subgraph(Gdist, V(Gdist)[which(V(Gdist)$name %in% oril[['CDC6']])])
GdistoriWT=induced.subgraph(Gdist, V(Gdist)[which(V(Gdist)$name %in% oril[['WT']])])
GdistoriAllOriWT=induced.subgraph(Gdist, V(Gdist)[which(V(Gdist)$name %in% oril[['ALL-ORI']])])
GdistoriCOMM=induced.subgraph(Gdist, V(Gdist)[which(V(Gdist)$name %in% oril[['COMM']])])
Gdistnonori=induced.subgraph(Gdist, V(Gdist)[-which(V(Gdist)$name %in% orinodes)])
###Make a list of ori enets with enames
Goril=Classic_Goril
Goril_enames=lapply(Goril, function(x){
el=get.edgelist(x)
E(x)$enames=paste(el[,1], el[,2], sep='-')
return(x)
})
summary(abs(as.numeric(E(Gdistori)$dist)))
summary(abs(as.numeric(E(Gdistnonori)$dist)))
distori=abs(as.numeric(E(Gdistori)$dist))[!is.na(as.numeric(E(Gdistori)$dist))]
distnonori=abs(as.numeric(E(Gdistnonori)$dist))[!is.na(as.numeric(E(Gdistnonori)$dist))]
distPCHiC=abs(as.numeric(E(Gdist)$dist))[!is.na(as.numeric(E(Gdist)$dist))]
##Find peaks function to identify peaks in distribution
find_peaks <- function (x, m){
shape <- diff(sign(diff(x, na.pad = FALSE)))
pks <- sapply(which(shape < 0), FUN = function(i){
z <- i - m + 1
z <- ifelse(z > 0, z, 1)
w <- i + m + 1
w <- ifelse(w < length(x), w, length(x))
if(all(x[c(z : i, (i + 2) : w)] <= x[i + 1])) return(i + 1) else return(numeric(0))
})
pks <- unlist(pks)
pks
}
peaknonoris=find_peaks(density(log10(distnonori))$y, 0.1)
peakoris=find_peaks(density(log10(distori))$y, 0.1)
peakPCHiC=find_peaks(density(log10(distPCHiC))$y, 0.1)
```
Distance distribution plots (not in paper)
```{r}
#pdf(paste(datapath,'Figures/DistanceDep/Distance_distributions_ori.pdf', sep='/'))
plot(density(log10(distori)),ylim=c(0,0.9), type='l',lwd=3,xlab='Distance spanned (log10)', main='Distribution of distances in ori-net')
#plot(density(log10(distnonori)), col='grey',lwd=3, xlab='Distance spanned (log10)', main='Distribution of distances in network\ngrey non ori PCHiC,black PCHiC orinet')
#axis(side=1, at=c(3,4,5,6,7,8), labels=c(1,10,100,1000,10000, 100000))
points(density(log10(distori)), type='l',lwd=3)
#abline(v=density(log10(distnonori))$x[peaknonoris], col='grey')
abline(v=density(log10(distori))$x[peakoris], col='grey')
print ('ori peaks')
density(log10(distori))$x[peakoris]
print ('nonori peaks')
#density(log10(distnonori))$x[peaknonoris]
#dev.off()
#pdf(paste(datapath,'Figures/DistanceDep/Distance_distributions_ori_nonori.pdf', sep='/'))
plot(density(log10(distori)),ylim=c(0,0.9), type='l',lwd=3,xlab='Distance spanned (log10)', main='Distribution of distances in network\ngrey total PCHiC,black PCHiC orinet')
#plot(density(log10(distnonori)), col='grey',lwd=3, xlab='Distance spanned (log10)', main='Distribution of distances in network\ngrey non ori PCHiC,black PCHiC orinet')
#axis(side=1, at=c(3,4,5,6,7,8), labels=c(1,10,100,1000,10000, 100000))
points(density(log10(distori)), type='l',lwd=3)
#abline(v=density(log10(distnonori))$x[peaknonoris], col='grey')
abline(v=density(log10(distori))$x[peakoris], col='black')
print ('ori peaks')
density(log10(distori))$x[peakoris]
print ('nonori peaks')
#density(log10(distnonori))$x[peaknonoris]
points(density(log10(distPCHiC)), type='l',lwd=3, col='grey')
abline(v=density(log10(distPCHiC))$x[peakPCHiC], col='grey')
#dev.off()
```
Load TAD coordinates and map PCHiC fragments to TADs
```{r Comparison with tads}
tad=read.table(paste(datapath,'data/total.HindIII.combined.domain', sep='/'))
rownames(tad)=paste(rep('TAD', nrow(tad)),seq(from=1, to=nrow(tad)), sep='')
colnames(tad)=c('chr', 'start', 'end')
tadbed=with(tad, GRanges(chr, IRanges(start, end)))
tadbed$ID=rownames(tad)
tadover=findOverlaps(tadbed,bedchicmore)
tadmatch_hit <- data.frame(tadbed$ID[queryHits(tadover)],bedchicmore$ID[subjectHits(tadover)] )
tadfrag=tadmatch_hit[!duplicated(tadmatch_hit),]
colnames(tadfrag)=c('tad', 'frag')
#table(table(tadfrag$tad))
#table(table(tadfrag$frag))
summary(as.numeric(table(tadfrag$tad)))
summary(as.numeric(table(tadfrag$frag)))
tadfragproc=tadfrag[-which(table(tadfrag$frag)>1),]