-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRIN2.0.ALEX.library.03.15.2023.R
3285 lines (2672 loc) · 140 KB
/
GRIN2.0.ALEX.library.03.15.2023.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
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
#################################################
# GRIN2.0-ALEX library
#######################################################
# get.chrom.length: Retrieve chromosome size data from chr.info txt file available on
# the UCSC genome browser based on the specified genome assembly.
get.chrom.length=function(genome.assembly) # one of four genome assemblies that include "Human_GRCh38", "Human_GRCh37", "Mouse_HGCm39" and "Mouse_HGCm38" can be specified
{
# retrieve chromosome size data for GRCh38 (hg38) genome build
if (genome.assembly=="Human_GRCh38")
{
chr.size.hg38= circlize::read.chromInfo(species = "hg38")
chr.size.hg38=as.data.frame(chr.size.hg38)
chr.size=cbind.data.frame(chrom=chr.size.hg38$chromosome,
size=chr.size.hg38$chr.len)
chr.size$chrom<-gsub("chr","",as.character(chr.size$chrom))
return(chr.size)
}
# retrieve chromosome size data for GRCh37 (hg19) genome build
if (genome.assembly=="Human_GRCh37")
{
chr.size.hg19= circlize::read.chromInfo(species = "hg19")
chr.size.hg19=as.data.frame(chr.size.hg19)
chr.size=cbind.data.frame(chrom=chr.size.hg19$chromosome,
size=chr.size.hg19$chr.len)
chr.size$chrom<-gsub("chr","",as.character(chr.size$chrom))
return(chr.size)
}
# retrieve chromosome size data for Mouse_HGCm39 (mm39) genome build
if (genome.assembly=="Mouse_HGCm39")
{
chr.size.mm39= circlize::read.chromInfo(species = "mm39")
chr.size.mm39=as.data.frame(chr.size.mm39)
chr.size=cbind.data.frame(chrom=chr.size.mm39$chromosome,
size=chr.size.mm39$chr.len)
chr.size$chrom<-gsub("chr","",as.character(chr.size$chrom))
return(chr.size)
}
# retrieve chromosome size data for Mouse_HGCm38 (mm10) genome build
if (genome.assembly=="Mouse_HGCm38")
{
chr.size.mm38= circlize::read.chromInfo(species = "mm10")
chr.size.mm38=as.data.frame(chr.size.mm38)
chr.size=cbind.data.frame(chrom=chr.size.mm38$chromosome,
size=chr.size.mm38$chr.len)
chr.size$chrom<-gsub("chr","",as.character(chr.size$chrom))
return(chr.size)
}
}
################################################################################
# get.ensembl.annotation: Retrieve gene and regulatory features annotation data from ensembl biomaRt
# database based on the specified genome assembly.
get.ensembl.annotation=function(genome.assembly) # one of four genome assemblies that include "Human_GRCh38", "Human_GRCh37", "Mouse_HGCm39" and "Mouse_HGCm38" can be specified
{
# retrieve gene annotation data for human_GRCh38 (hg38) genome assembly from ensembl biomaRt version 104
if (genome.assembly=="Human_GRCh38")
{
ensembl_GRCh38 = useEnsembl(biomart="genes",
dataset="hsapiens_gene_ensembl", # specify dataset for homosapiens
version = "104") # specifying version is critical to get stable search results. If we did not specify version, query will extract data from the most updated version
chromosomes = c(1:22, "X", "Y") # specify chromosomes of interst
hg38_gene_annotation <- getBM(attributes=c('ensembl_gene_id', 'chromosome_name',
'start_position','end_position',
'description','external_gene_name',
'gene_biotype', 'strand', 'band'),
filters = 'chromosome_name', values = chromosomes,
mart = ensembl_GRCh38) # attributes specify data to be retreived from biomaRt database
gene=hg38_gene_annotation[,1]
chrom=hg38_gene_annotation[,2]
loc.start=hg38_gene_annotation[,3]
loc.end=hg38_gene_annotation[,4]
description=hg38_gene_annotation[,5]
gene.name=hg38_gene_annotation[,6]
biotype=hg38_gene_annotation[,7]
chrom.strand=hg38_gene_annotation[,8]
chrom.band=hg38_gene_annotation[,9]
gene.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description,
gene.name=gene.name,
biotype=biotype,
chrom.strand=chrom.strand,
chrom.band=chrom.band)
# To retrieve data for regulatory features mapped to GRCh38
regulatory.hg38 = useEnsembl(biomart="regulation",
dataset="hsapiens_regulatory_feature", # regulatory features includes (promoters, promoter flanking regions, enhancers, CTCF binding sites, TF binding sites and open chromatin regions)
version = '104') # specify version for stable results
regulatory.features=c("Promoter", "Promoter Flanking Region", "Enhancer",
"CTCF Binding Site",
"TF binding site", "Open chromatin")
hg38.regulatory= getBM(attributes=c("regulatory_stable_id", "chromosome_name",
"chromosome_start","chromosome_end",
"feature_type_description"),
filters =c("regulatory_feature_type_name", "chromosome_name")
, values =list(regulatory.features,chromosomes) , # We are excluding regulatory features not mapped to chr1:22, X, Y
mart =regulatory.hg38)
gene=hg38.regulatory[,1]
chrom=hg38.regulatory[,2]
loc.start=hg38.regulatory[,3]
loc.end=hg38.regulatory[,4]
description=hg38.regulatory[,5]
regulation.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description)
res=list(gene.annotation=gene.data, # gene.data represent annotated genes
reg.annotation=regulation.data) # regulation.data represent predicted regulatory features
return(res)
}
# retrieve gene annotation data for human_GRCh37 (hg19) genome assembly from ensembl biomaRt version 75
if (genome.assembly=="Human_GRCh37")
{
ensemblGRCh37 <- useEnsembl(biomart = 'ensembl',
dataset = 'hsapiens_gene_ensembl',
version = '75')
chromosomes = c(1:22, "X", "Y")
hg19_gene_annotation <- getBM(attributes=c('ensembl_gene_id', 'chromosome_name',
'start_position','end_position',
'description','external_gene_id',
'gene_biotype', 'strand', 'band'),
filters = 'chromosome_name', values = chromosomes,
mart = ensemblGRCh37)
gene=hg19_gene_annotation[,1]
chrom=hg19_gene_annotation[,2]
loc.start=hg19_gene_annotation[,3]
loc.end=hg19_gene_annotation[,4]
description=hg19_gene_annotation[,5]
gene.name=hg19_gene_annotation[,6]
biotype=hg19_gene_annotation[,7]
chrom.strand=hg19_gene_annotation[,8]
chrom.band=hg19_gene_annotation[,9]
gene.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description,
gene.name=gene.name,
biotype=biotype,
chrom.strand=chrom.strand,
chrom.band=chrom.band)
# To retrieve data for regulatory features mapped to GRCh37
regulatory.hg19 = useEnsembl(biomart="regulation",
dataset="hsapiens_regulatory_feature",
version = 'GRCh37')
regulatory.features=c("Promoter", "Promoter Flanking Region", "Enhancer",
"CTCF Binding Site",
"TF binding site", "Open chromatin")
hg19.regulatory= getBM(attributes=c("regulatory_stable_id", "chromosome_name",
"chromosome_start","chromosome_end",
"feature_type_description"),
filters =c("regulatory_feature_type_name", "chromosome_name")
, values =list(regulatory.features,chromosomes) , # We are excluding regulatory features not mapped to chr1:22, X, Y
mart =regulatory.hg19)
gene=hg19.regulatory[,1]
chrom=hg19.regulatory[,2]
loc.start=hg19.regulatory[,3]
loc.end=hg19.regulatory[,4]
description=hg19.regulatory[,5]
regulation.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description)
res=list(gene.annotation=gene.data,
reg.annotation=regulation.data)
return(res)
}
# retrieve gene annotation data for Mouse_HGCm39 genome assembly from ensembl biomaRt version 104
if (genome.assembly=="Mouse_HGCm39")
{
ensembl.HGCm39 = useEnsembl(biomart="genes", dataset="mmusculus_gene_ensembl",
version = "104")
chromosomes = c(1:19, "X", "Y")
HGCm39.gene_annotation <- getBM(attributes=c('ensembl_gene_id', 'chromosome_name',
'start_position','end_position',
'description','external_gene_name',
'gene_biotype', 'strand', 'band'),
filters = 'chromosome_name', values = chromosomes,
mart = ensembl.HGCm39)
gene=HGCm39.gene_annotation[,1]
chrom=HGCm39.gene_annotation[,2]
loc.start=HGCm39.gene_annotation[,3]
loc.end=HGCm39.gene_annotation[,4]
description=HGCm39.gene_annotation[,5]
gene.name=HGCm39.gene_annotation[,6]
biotype=HGCm39.gene_annotation[,7]
chrom.strand=HGCm39.gene_annotation[,8]
chrom.band=HGCm39.gene_annotation[,9]
gene.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description,
gene.name=gene.name,
biotype=biotype,
chrom.strand=chrom.strand,
chrom.band=chrom.band)
# To retrieve data for mouse_regulatory features mapped to mm39
regulatory.mm39 = useEnsembl(biomart="regulation",
dataset="mmusculus_regulatory_feature",
version = '104')
regulatory.features=c("Promoter", "Promoter Flanking Region", "Enhancer",
"CTCF Binding Site",
"TF binding site", "Open chromatin")
mm39.regulatory= getBM(attributes=c("regulatory_stable_id", "chromosome_name",
"chromosome_start","chromosome_end",
"feature_type_description"),
filters =c("regulatory_feature_type_name", "chromosome_name")
, values =list(regulatory.features,chromosomes) , # We are excluding regulatory features not mapped to chr1:22, X, Y
mart =regulatory.mm39)
gene=mm39.regulatory[,1]
chrom=mm39.regulatory[,2]
loc.start=mm39.regulatory[,3]
loc.end=mm39.regulatory[,4]
description=mm39.regulatory[,5]
regulation.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description)
res=list(gene.annotation=gene.data,
reg.annotation=regulation.data)
return(res)
}
# retrieve gene annotation data for Mouse_HGCm38 from ensembl biomaRt version 102
if (genome.assembly=="Mouse_HGCm38")
{
ensemblGRCm38 <- useEnsembl(biomart = 'genes',
dataset = 'mmusculus_gene_ensembl',
version = '102')
chromosomes = c(1:19, "X", "Y")
Mouse_mm10.gene_annotation <- getBM(attributes=c('ensembl_gene_id', 'chromosome_name',
'start_position','end_position',
'description','external_gene_name',
'gene_biotype', 'strand', 'band'),
filters = 'chromosome_name', values = chromosomes,
mart = ensemblGRCm38)
gene=Mouse_mm10.gene_annotation[,1]
chrom=Mouse_mm10.gene_annotation[,2]
loc.start=Mouse_mm10.gene_annotation[,3]
loc.end=Mouse_mm10.gene_annotation[,4]
description=Mouse_mm10.gene_annotation[,5]
gene.name=Mouse_mm10.gene_annotation[,6]
biotype=Mouse_mm10.gene_annotation[,7]
chrom.strand=Mouse_mm10.gene_annotation[,8]
chrom.band=Mouse_mm10.gene_annotation[,9]
gene.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description,
gene.name=gene.name,
biotype=biotype,
chrom.strand=chrom.strand,
chrom.band=chrom.band)
# To retrieve data for mouse_regulatory features mapped to mm10
regulatory.mm10 = useEnsembl(biomart="regulation",
dataset="mmusculus_regulatory_feature",
version = '102')
regulatory.features=c("Promoter", "Promoter Flanking Region", "Enhancer",
"CTCF Binding Site",
"TF binding site", "Open chromatin")
mm10.regulatory= getBM(attributes=c("regulatory_stable_id", "chromosome_name",
"chromosome_start","chromosome_end",
"feature_type_description"),
filters =c("regulatory_feature_type_name", "chromosome_name")
, values =list(regulatory.features,chromosomes) , # We are excluding regulatory features not mapped to chr1:22, X, Y
mart =regulatory.mm10)
gene=mm10.regulatory[,1]
chrom=mm10.regulatory[,2]
loc.start=mm10.regulatory[,3]
loc.end=mm10.regulatory[,4]
description=mm10.regulatory[,5]
regulation.data=cbind.data.frame(gene=gene,
chrom=chrom,
loc.start=loc.start,
loc.end=loc.end,
description=description)
res=list(gene.annotation=gene.data,
reg.annotation=regulation.data)
return(res)
}
}
#################################################################################
# order.index.gene.data: This function order and index gene data by chromosome,
# start location, and end location.
order.index.gene.data=function(gene.data)
{
g=nrow(gene.data)
gene.ord=order(gene.data[,"chrom"],
gene.data[,"loc.start"],
gene.data[,"loc.end"])
gene.data=gene.data[gene.ord,]
new.chrom=which(gene.data[-1,"chrom"]!=gene.data[-g,"chrom"])
chr.start=c(1,new.chrom+1)
chr.end=c(new.chrom,g)
gene.index=cbind.data.frame(chrom=gene.data[chr.start,"chrom"],
row.start=chr.start,
row.end=chr.end)
gene.data$gene.row=1:g
res=list(gene.data=gene.data,
gene.index=gene.index)
return(res)
}
#################################################################################
# order.index.lsn.data: This function order and index lesion data by type,
# chromosome, and subject.
order.index.lsn.data=function(lsn.data)
{
l=nrow(lsn.data)
lsn.ord=order(lsn.data[,"lsn.type"],
lsn.data[,"chrom"],
lsn.data[,"ID"])
lsn.data=lsn.data[lsn.ord,]
lsn.chng=which((lsn.data[-1,"lsn.type"]!=lsn.data[-l,"lsn.type"])|
(lsn.data[-1,"chrom"]!=lsn.data[-l,"chrom"])|
(lsn.data[-1,"ID"]!=lsn.data[-l,"ID"]))
lsn.start=c(1,lsn.chng+1)
lsn.end=c(lsn.chng,l)
lsn.index=cbind.data.frame(lsn.type=lsn.data[lsn.start,"lsn.type"],
chrom=lsn.data[lsn.start,"chrom"],
ID=lsn.data[lsn.start,"ID"],
row.start=lsn.start,
row.end=lsn.end)
lsn.data$lsn.row=1:l
res=list(lsn.data=lsn.data,
lsn.index=lsn.index)
return(res)
}
##################################################################################
# prep.gene.lsn.data: This function prepare gene and lesion data for later computations.
prep.gene.lsn.data=function(lsn.data, # lesion data file with five columns: "ID" which is patient ID, "chrom" chromosome on which the lesion is located, "loc.start" lesion start position, "loc.end" lesion end position, "lsn.type" lesion category such as gain, loss, mutation, etc...
gene.data, # gene annotation data with four required columns: "gene" has ensembl ID, "chrom" chromosome on which the gene is located, "loc.start" gene start position, "loc.end" which is the gene end position
mess.freq=10) # message frequency: display message every mess.freq^{th} lesion block
{
saf=options()$stringsAsFactors
options(stringsAsFactors=F)
# order lesion data by type, chromosome, and subject
lsn.dset=order.index.lsn.data(lsn.data)
lsn.data=lsn.dset$lsn.data
lsn.index=lsn.dset$lsn.index
# order and index gene locus data by chromosome and position
gene.dset=order.index.gene.data(gene.data)
gene.data=gene.dset$gene.data
gene.index=gene.dset$gene.index
# Extract some basic information
g=nrow(gene.data) # number of genes
l=nrow(lsn.data) # number of lesions
# Create gene position data
message(paste0("Formatting gene position data for counting: ",date()))
gene.pos.data=rbind.data.frame(cbind.data.frame(ID="", # gene start data
lsn.type="",
lsn.row=NA,
gene=gene.data[,"gene"],
gene.row=gene.data[,"gene.row"],
chrom=gene.data[,"chrom"],
pos=gene.data[,"loc.start"],
cty=1),
cbind.data.frame(ID="", # gene end data
lsn.type="",
lsn.row=NA,
gene=gene.data[,"gene"],
gene.row=gene.data[,"gene.row"],
chrom=gene.data[,"chrom"],
pos=gene.data[,"loc.end"],
cty=4)
)
# order gene position data
ord=order(gene.pos.data[,"chrom"],
gene.pos.data[,"pos"],
gene.pos.data[,"cty"])
gene.pos.data=gene.pos.data[ord,]
# Create lesion position data with one row for each edge of each lesion
message(paste0("Formatting lesion position data for counting: ",date()))
lsn.pos.data=rbind.data.frame(cbind.data.frame(ID=lsn.data[,"ID"],
lsn.type=lsn.data[,"lsn.type"],
lsn.row=lsn.data[,"lsn.row"],
gene="",
gene.row=NA,
chrom=lsn.data[,"chrom"],
pos=lsn.data[,"loc.start"],
cty=2),
cbind.data.frame(ID=lsn.data[,"ID"],
lsn.type=lsn.data[,"lsn.type"],
lsn.row=lsn.data[,"lsn.row"],
gene="",
gene.row=NA,
chrom=lsn.data[,"chrom"],
pos=lsn.data[,"loc.end"],
cty=3))
# order lesion position data
ord=order(lsn.pos.data[,"chrom"],
lsn.pos.data[,"pos"],
lsn.pos.data[,"cty"])
lsn.pos.data=lsn.pos.data[ord,]
# Combine gene & lesion data
message(paste0("Combining formatted gene and lesion postion data: ",date()))
gene.lsn.data=rbind.data.frame(gene.pos.data,
lsn.pos.data)
# Order and index gene & lesion data
ord=order(gene.lsn.data[,"chrom"],
gene.lsn.data[,"pos"],
gene.lsn.data[,"cty"])
gene.lsn.data=gene.lsn.data[ord,]
m=nrow(gene.lsn.data)
gene.lsn.data[,"glp.row"]=1:m
# compute vector to order gene.lsn.data by lsn.row and gene.row
ord=order(gene.lsn.data[,"lsn.row"],
gene.lsn.data[,"gene.row"],
gene.lsn.data[,"cty"])
# use that vector to add gene.lsn.data row.start and row.end indices to lsn.data
lsn.pos=gene.lsn.data[ord[1:(2*l)],]
lsn.data[,"glp.row.start"]=lsn.pos[2*(1:l)-1,"glp.row"]
lsn.data[,"glp.row.end"]=lsn.pos[2*(1:l),"glp.row"]
# use that vector to add gene.lsn.data row.start and row.end indices to gene.data
gene.pos=gene.lsn.data[ord[-(1:(2*l))],]
gene.data[,"glp.row.start"]=gene.pos[2*(1:g)-1,"glp.row"]
gene.data[,"glp.row.end"]=gene.pos[2*(1:g),"glp.row"]
# Double-check table pointers from lsn.data and gene.data to gene.lsn.data
message(paste0("Verifying structure of combined gene and lesion data: ",date()))
glp.gene.start=gene.lsn.data[gene.data$glp.row.start,c("gene","chrom","pos")]
colnames(glp.gene.start)=c("gene","chrom","loc.start")
ok.glp.gene.start=all(glp.gene.start==gene.data[,c("gene","chrom","loc.start")])
glp.gene.end=gene.lsn.data[gene.data$glp.row.end,c("gene","chrom","pos")]
colnames(glp.gene.end)=c("gene","chrom","loc.end")
ok.glp.gene.end=all(glp.gene.end==gene.data[,c("gene","chrom","loc.end")])
glp.lsn.start=gene.lsn.data[lsn.data$glp.row.start,c("ID","chrom","pos","lsn.type")]
colnames(glp.lsn.start)=c("ID","chrom","loc.start","lsn.type")
ok.glp.lsn.start=all(glp.lsn.start==lsn.data[,c("ID","chrom","loc.start","lsn.type")])
glp.lsn.end=gene.lsn.data[lsn.data$glp.row.end,c("ID","chrom","pos","lsn.type")]
colnames(glp.lsn.end)=c("ID","chrom","loc.end","lsn.type")
ok.glp.lsn.end=all(glp.lsn.end==lsn.data[,c("ID","chrom","loc.end","lsn.type")])
# Double-check table pointers from gene.lsn.data to gene.data and lsn.data
glp.gene.start=gene.lsn.data[gene.lsn.data$cty==1,c("gene.row","gene","chrom","pos")]
ok.gene.start=all(glp.gene.start[,c("gene","chrom","pos")]==gene.data[glp.gene.start$gene.row,c("gene","chrom","loc.start")])
glp.gene.end=gene.lsn.data[gene.lsn.data$cty==4,c("gene.row","gene","chrom","pos")]
ok.gene.end=all(glp.gene.end[,c("gene","chrom","pos")]==gene.data[glp.gene.end$gene.row,c("gene","chrom","loc.end")])
glp.lsn.start=gene.lsn.data[gene.lsn.data$cty==2,c("lsn.row","ID","chrom","pos","lsn.type")]
ok.lsn.start=all(glp.lsn.start[,c("ID","chrom","pos","lsn.type")]==lsn.data[glp.lsn.start$lsn.row,c("ID","chrom","loc.start","lsn.type")])
glp.lsn.end=gene.lsn.data[gene.lsn.data$cty==3,c("lsn.row","ID","chrom","pos","lsn.type")]
ok.lsn.end=all(glp.lsn.end[,c("ID","chrom","pos","lsn.type")]==lsn.data[glp.lsn.end$lsn.row,c("ID","chrom","loc.end","lsn.type")])
all.ok=all(c(ok.glp.gene.start,ok.glp.gene.end,
ok.glp.lsn.start,ok.glp.lsn.end,
ok.gene.start,ok.gene.end,
ok.lsn.start,ok.lsn.end))
if (!all.ok)
stop("Error in constructing and indexing combined lesion and gene data.")
message(paste0("Verified correct construction and indexing of combined lesion and gene data: ",date()))
return(list(lsn.data=lsn.data, # Input lesion data
gene.data=gene.data, # Input gene annotation data
gene.lsn.data=gene.lsn.data, # Data.frame ordered by gene and lesions start position. Gene start position is coded as 1 in the cty column and gene end position is coded as 4. Lesion start position is coded as 2 in the cty column and lesion end position is coded as 3.
gene.index=gene.index, # Data.frame that shows ordered row start and row end for each chromosome in the gene.lsn.data table
lsn.index=lsn.index)) # Data.frame that shows row start and row end for each lesion in the gene.lsn.data table
}
#################################################################################
# find.gene.lsn.overlaps: Use the results of prep.gene.lsn.data function to find
# lesion-gene overlaps.
find.gene.lsn.overlaps=function(gl.data) # A list of five data.frames that represent the output results of the prep.gene.lsn.data function
{
gene.data=gl.data$gene.data
lsn.data=gl.data$lsn.data
lsn.index=gl.data$lsn.index
gene.index=gl.data$gene.index
gene.lsn.data=gl.data$gene.lsn.data
m=nrow(gene.lsn.data)
message(paste0("Scanning through combined lesion and gene data to find gene-lesion overlaps: ",date()))
gene.row.mtch=NULL # initialize vector for rows of gene data matched to rows of lesion data
lsn.row.mtch=NULL # initialize vector for rows of lesion data matched to rows of gene data
current.genes=NULL # initialize vector of genes overlapping this point of the scan
current.lsns=NULL # initialize vector of lesions overlapping this point of the scan
for (i in 1:m) # loop over rows of gene.lsn.data
{
# enter a gene
if (gene.lsn.data$cty[i]==1)
{
# add this gene to the set of current genes
current.genes=c(current.genes,
gene.lsn.data$gene.row[i])
# match this gene to set of current lesions
lsn.row.mtch=c(lsn.row.mtch,current.lsns) # add current lesions to lsn.row.mtch
gene.row.mtch=c(gene.row.mtch, # add this gene for each current lesion
rep(gene.lsn.data$gene.row[i],
length(current.lsns)))
}
# exit a gene
if (gene.lsn.data$cty[i]==4)
{
# drop this gene from the set of current genes
current.genes=setdiff(current.genes,
gene.lsn.data$gene.row[i])
}
if (gene.lsn.data$cty[i]==2) # enter a lesion
{
lsn.row.mtch=c(lsn.row.mtch,
rep(gene.lsn.data$lsn.row[i],length(current.genes)))
gene.row.mtch=c(gene.row.mtch,current.genes)
current.lsns=c(current.lsns,
gene.lsn.data$lsn.row[i])
}
if (gene.lsn.data$cty[i]==3)
{
current.lsns=setdiff(current.lsns,
gene.lsn.data$lsn.row[i])
}
}
message(paste0("Completed scan of combined gene-lesion data: ",date()))
# Generate the gene-lesion hit data
gene.lsn.hits=cbind.data.frame(gene.data[gene.row.mtch,
c("gene.row","gene","chrom","loc.start","loc.end")],
lsn.data[lsn.row.mtch,
c("lsn.row","ID","chrom","loc.start","loc.end","lsn.type")])
colnames(gene.lsn.hits)=c("gene.row","gene","gene.chrom","gene.loc.start","gene.loc.end",
"lsn.row","ID","lsn.chrom","lsn.loc.start","lsn.loc.end","lsn.type")
res=list(lsn.data=lsn.data, # Input lesion data
gene.data=gene.data, # Input gene annotation data
gene.lsn.data=gene.lsn.data, # Data.frame ordered by gene and lesions start position. Gene start position is coded as 1 in the cty column and gene end position is coded as 4. Lesion start position is coded as 2 in the cty column and lesion end position is coded as 3
gene.lsn.hits=gene.lsn.hits, # Each row represent a gene overlapped by a certain lesion. Gene column shows the overlapped gene and ID column has the patient ID
gene.index=gene.index, # Data.frame that shows row start and row end for each chromosome in the gene.lsn.data table
lsn.index=lsn.index) # Data.frame that shows row start and row end for each lesion in the gene.lsn.data table
return(res)
}
##################################################################################
# count.hits: The function computes the number of hits affecting each gene by lesion
# type. It also compute the number of subjects with a hit in each annotated gene.
count.hits=function(ov.data) # result of find.gene.lsn.overlaps function
{
lsn.data=ov.data$lsn.data
lsn.index=ov.data$lsn.index
gene.lsn.hits=ov.data$gene.lsn.hits
gene.lsn.data=ov.data$gene.lsn.data
gene.data=ov.data$gene.data
gene.index=ov.data$gene.index
g=nrow(gene.data)
# Compute the number of hits matrix
lsn.types=sort(unique(lsn.index[,"lsn.type"]))
k=length(lsn.types)
nhit.mtx=matrix(0,g,k)
colnames(nhit.mtx)=lsn.types
nhit.tbl=table(gene.lsn.hits$gene.row,
gene.lsn.hits$lsn.type)
nhit.rows=as.numeric(rownames(nhit.tbl))
for (i in 1:ncol(nhit.tbl))
nhit.mtx[nhit.rows,colnames(nhit.tbl)[i]]=nhit.tbl[,i]
# Compute the matrix of the number of subjects with a hit
gene.subj.type=paste0(gene.lsn.hits$gene.row,"_",
gene.lsn.hits$ID,"_",
gene.lsn.hits$lsn.type)
dup.gene.subj.type=duplicated(gene.subj.type)
subj.gene.hits=gene.lsn.hits[!dup.gene.subj.type,]
nsubj.mtx=matrix(0,g,k)
colnames(nsubj.mtx)=lsn.types
nsubj.tbl=table(subj.gene.hits$gene.row,
subj.gene.hits$lsn.type)
nsubj.rows=as.numeric(rownames(nsubj.tbl))
for (i in 1:ncol(nsubj.tbl))
nsubj.mtx[nsubj.rows,colnames(nsubj.tbl)[i]]=nsubj.tbl[,i]
res=list(lsn.data=lsn.data, # Input lesion data
lsn.index=lsn.index, # Data.frame that shows row start and row end for each lesion in the gene.lsn.data table
gene.data=gene.data, # Input gene annotation data
gene.index=gene.index, # Data.frame that shows ordered row start and row end for each chromosome in the gene.lsn.data table
nhit.mtx=nhit.mtx, # A data matrix with number of hits in each gene by lesion type
nsubj.mtx=nsubj.mtx, # A data matrix with number of affected subjects by lesion type
gene.lsn.data=gene.lsn.hits, # Each row represent a gene overlapped by a certain lesion. Column "gene" shows the overlapped gene and ID column has the patient ID
glp.data=gene.lsn.data) # Data.frame ordered by gene and lesions start position. Gene start position is coded as 1 in the cty column and gene end position is coded as 4. Lesion start position is coded as 2 in the cty column and lesion end position is coded as 3
return(res)
}
#################################################################################
# Following four functions row.prob.subj.hit, row.bern.conv, p.order and pc06.fdr
# will be used by prob.hits function to compute p-values for the probablity of each gene
# to be affected by one or a constellation of multiple types of lesions
# row.prob.subj.hit: Compute the probability that a subject has a hit for each gene.
row.prob.subj.hit=function(P, # matrix of lesion hit probabilities, rows for genes, columns for lesion types
IDs) # vector of subject IDs for each lesion, length must equal ncol(P)
{
if (length(IDs)!=ncol(P))
stop("length(IDs) must equal ncol(P).")
g=nrow(P)
l=ncol(P)
ord=order(IDs)
IDs=IDs[ord]
P=matrix(P[,ord],g,l)
l=length(IDs)
new.ID=which(IDs[-1]!=IDs[-l])
ID.start=c(1,new.ID+1)
ID.end=c(new.ID,l)
n=length(ID.start)
m=nrow(P)
Pr=matrix(NA,m,n)
for (i in 1:n)
{
pr.mtx=matrix(P[,ID.start[i]:ID.end[i]],m,ID.end[i]-ID.start[i]+1)
Pr[,i]=1-exp(rowSums(log(1-pr.mtx)))
}
return(Pr)
}
################################################################################
# row.bern.conv: This function Compute a convolution of Bernoullis for each row of
# a Bernoulli success probability matrix.
row.bern.conv=function(P, # matrix of lesion hit probabilities, rows for genes, columns for lesion types
max.x=NULL) # Maximum number of subjects or maximum number of hits
{
m=nrow(P)
n=ncol(P)
if (is.null(max.x))
max.x=(ncol(P))
Pr=matrix(0,m,max.x+1)
Pr[,1]=1
for (i in 1:n)
{
P1=Pr*P[,i]
P0=Pr*(1-P[,i])
Pr0=P0
Pr1=cbind(0,P1)
Pr1[,max.x+1]=Pr1[,max.x+1]+Pr1[,max.x+2]
Pr=Pr0+Pr1[,-(max.x+2)]
}
rs.Pr=rowSums(Pr)
Pr=Pr/rs.Pr
return(Pr)
}
#################################################################################
# p.order: This function compute ordered p-values and prepare the data for the
# constellation test which evaluates if the gene is affected by multiple types of lesions.
p.order=function(P) # matrix of lesion hit probabilities, rows for genes, columns for lesion types
{
k=ncol(P)
p.mtx=apply(P,1,sort,na.last=T)
p.mtx=t(p.mtx)
#n.pvals=rowSums(!is.na(p.mtx))
res=p.mtx
for (i in 1:k)
{
res[,i]=pbeta(p.mtx[,i],i,k-i+1)
}
return(res)
}
###############################################################################
# pc06.fdr: Compute FDR with the Pounds & Cheng (2006) estimator of
# the proportion of tests with a true null (pi.hat)
# Reference: https://pubmed.ncbi.nlm.nih.gov/16777905/
pc06.fdr=function(p)
{
pi.hat=min(1,2*mean(p,na.rm=T))
q=pi.hat*p.adjust(p,method="fdr")
return(q)
}
#################################################################################
# prob.hits: The function evaluates the probablity of a gene to be affected by one
# or a constellation of multiple types of lesions.
prob.hits=function(hit.cnt, # Output results of the count.hits function with number of subjects and number of hits affecting each locus
chr.size=NULL) # A data table showing the size of the 22 autosomes, in addition to X and Y chromosomes in base pairs. It should has two columns named "chrom" with the chromosome number and "size" for the size of the chromosome in base pairs.
{
if (is.null(chr.size))
chr.size=impute.chrom.size(hit.cnt$lsn.data,
hit.cnt$gene.data)
###################
# order and index gene.lsn.data
ord=order(hit.cnt$gene.lsn.data$lsn.type,
hit.cnt$gene.lsn.data$lsn.chrom,
hit.cnt$gene.lsn.data$gene.row,
hit.cnt$gene.lsn.data$ID)
hit.cnt$gene.lsn.data=hit.cnt$gene.lsn.data[ord,]
m=nrow(hit.cnt$gene.lsn.data)
new.sect=which((hit.cnt$gene.lsn.data$gene.chrom[-1]!=hit.cnt$gene.lsn.data$gene.chrom[-m])|
(hit.cnt$gene.lsn.data$lsn.type[-1]!=hit.cnt$gene.lsn.data$lsn.type[-m])|
(hit.cnt$gene.lsn.data$gene.row[-1]!=hit.cnt$gene.lsn.data$gene.row[-m]))
sect.start=c(1,new.sect+1)
sect.end=c(new.sect,m)
gene.lsn.index=cbind.data.frame(lsn.type=hit.cnt$gene.lsn.data$lsn.type[sect.start],
chrom=hit.cnt$gene.lsn.data$gene.chrom[sect.start],
gene.row=hit.cnt$gene.lsn.data$gene.row[sect.start],
row.start=sect.start,
row.end=sect.end,
n.lsns=sect.end-sect.start+1)
k=nrow(gene.lsn.index)
new.chr=which(gene.lsn.index$chrom[-1]!=gene.lsn.index$chrom[-k])
chr.start=c(1,new.chr+1)
chr.end=c(new.chr,k)
gene.lsn.chr.index=cbind.data.frame(lsn.type=gene.lsn.index$lsn.type[chr.start],
chrom=gene.lsn.index$chrom[chr.start],
row.start=chr.start,
row.end=chr.end,
n.rows=chr.end-chr.start+1)
nr.li=nrow(hit.cnt$lsn.index)
new.chr=which((hit.cnt$lsn.index$lsn.type[-1]!=hit.cnt$lsn.index$lsn.type[-nr.li])|
(hit.cnt$lsn.index$chrom[-1]!=hit.cnt$lsn.index$chrom[-nr.li]))
chr.start=c(1,new.chr+1)
chr.end=c(new.chr,nr.li)
lsn.chr.index=cbind.data.frame(lsn.type=hit.cnt$lsn.index$lsn.type[chr.start],
chrom=hit.cnt$lsn.index$chrom[chr.start],
row.start=chr.start,
row.end=chr.end)
b=nrow(gene.lsn.chr.index)
g=nrow(hit.cnt$nhit.mtx)
nlt=ncol(hit.cnt$nhit.mtx)
p.nsubj=p.nhit=matrix(1,g,nlt)
colnames(p.nsubj)=colnames(p.nhit)=colnames(hit.cnt$nhit.mtx)
for (i in 1:b)
{
# find rows for affected genes
gli.start.row=gene.lsn.chr.index$row.start[i]
gli.end.row=gene.lsn.chr.index$row.end[i]
gld.start.row=gene.lsn.index$row.start[gli.start.row]
gld.end.row=gene.lsn.index$row.end[gli.end.row]
gld.rows=gld.start.row:gld.end.row
gene.rows=unique(hit.cnt$gene.lsn.data$gene.row[gld.rows])
n.genes=length(gene.rows)
# find rows for lesions of this type on this chromosomes
lsn.chr.mtch=which((lsn.chr.index$lsn.type==gene.lsn.chr.index$lsn.type[i])&
(lsn.chr.index$chrom==gene.lsn.chr.index$chrom[i]))
lsn.index.start.row=lsn.chr.index$row.start[lsn.chr.mtch]
lsn.index.end.row=lsn.chr.index$row.end[lsn.chr.mtch]
lsn.start.row=hit.cnt$lsn.index$row.start[lsn.index.start.row]
lsn.end.row=hit.cnt$lsn.index$row.end[lsn.index.end.row]
lsn.rows=lsn.start.row:lsn.end.row
n.lsns=length(lsn.rows)
lsn.type=hit.cnt$lsn.data$lsn.type[lsn.start.row]
message(paste0("Computing p-values for ",
n.genes," gene(s) on chromosome ",
gene.lsn.chr.index$chrom[i],
" affected by ",
n.lsns," ",lsn.type,
" (data block ",i," of ",b,"): ",date()))
# find chromosome size
chr.mtch=which(gene.lsn.chr.index$chrom[i]==chr.size$chrom)
chrom.size=chr.size$size[chr.mtch]
# obtain gene sizes, lesion sizes, and gene hit probabilities
lsn.size=hit.cnt$lsn.data$loc.end[lsn.rows]-hit.cnt$lsn.data$loc.start[lsn.rows]+1
gene.size=hit.cnt$gene.data$loc.end[gene.rows]-hit.cnt$gene.data$loc.start[gene.rows]+1
log.pr=log(rep(lsn.size,each=n.genes)+rep(gene.size,times=n.lsns))-log(chrom.size)
pr.gene.hit=matrix(exp(log.pr),n.genes,n.lsns)
pr.gene.hit[pr.gene.hit>1]=1
lsn.subj.IDs=hit.cnt$lsn.data$ID[lsn.rows]
pr.subj=row.prob.subj.hit(pr.gene.hit,lsn.subj.IDs)
max.nsubj=max(hit.cnt$nsubj.mtx[gene.rows,lsn.type])
max.nhit=max(hit.cnt$nhit.mtx[gene.rows,lsn.type])
pr.nhit=row.bern.conv(pr.gene.hit,max.nhit)
pr.nsubj=row.bern.conv(pr.subj,max.nsubj)
for (j in 1:n.genes)
{
nsubj=hit.cnt$nsubj.mtx[gene.rows[j],lsn.type]
nhit=hit.cnt$nhit.mtx[gene.rows[j],lsn.type]
p.nsubj[gene.rows[j],lsn.type]=sum(pr.nsubj[j,(nsubj+1):(max.nsubj+1)])
p.nhit[gene.rows[j],lsn.type]=sum(pr.nhit[j,(nhit+1):(max.nhit+1)])
}
}
rownames(p.nhit)=rownames(hit.cnt$nhit.mtx)
rownames(p.nsubj)=rownames(hit.cnt$nsubj.mtx)
colnames(hit.cnt$nhit.mtx)=paste0("nhit.",colnames(hit.cnt$nhit.mtx))
colnames(hit.cnt$nsubj.mtx)=paste0("nsubj.",colnames(hit.cnt$nsubj.mtx))
colnames(p.nhit)=paste0("p.",colnames(hit.cnt$nhit.mtx))
colnames(p.nsubj)=paste0("p.",colnames(hit.cnt$nsubj.mtx))
# Compute q-values
message(paste0("Computing q-values: ",date()))
q.nhit=p.nhit
q.nsubj=p.nsubj
for (i in 1:ncol(q.nhit))
{
pi.hat=min(1,2*mean(p.nhit[,i],na.rm=T))
q.nhit[,i]=pi.hat*p.adjust(p.nhit[,i],method="fdr")
pi.hat=min(1,2*mean(p.nsubj[,i],na.rm=T))
q.nsubj[,i]=pi.hat*p.adjust(p.nsubj[,i],method="fdr")
}
colnames(q.nhit)=paste0("q.",colnames(hit.cnt$nhit.mtx))
colnames(q.nsubj)=paste0("q.",colnames(hit.cnt$nsubj.mtx))
# Now get ordered p-values
message(paste0("Computing p-values for number of lesion types affecting genes: ",date()))
p.ord.nhit=p.order(p.nhit)
colnames(p.ord.nhit)=paste0("p",1:ncol(p.nhit),".nhit")
p.ord.nsubj=p.order(p.nsubj)
colnames(p.ord.nsubj)=paste0("p",1:ncol(p.nsubj),".nsubj")
# q-values of ordered p-values
q.ord.nhit=p.ord.nhit
q.ord.nsubj=p.ord.nsubj
message(paste0("Computing q-values for number of lesion types affecting genes: ",date()))
for (i in 1:ncol(p.ord.nhit))
{
pi.hat=min(1,2*mean(p.ord.nhit[,i],na.rm=T))
q.ord.nhit[,i]=pi.hat*p.adjust(p.ord.nhit[,i],method="fdr")
pi.hat=min(1,2*mean(p.ord.nsubj[,i],na.rm=T))
q.ord.nsubj[,i]=pi.hat*p.adjust(p.ord.nsubj[,i],method="fdr")
}
colnames(q.ord.nsubj)=paste0("q",1:ncol(p.nsubj),".nsubj")
colnames(q.ord.nhit)=paste0("q",1:ncol(p.nhit),".nhit")
gd.clms=setdiff(colnames(hit.cnt$gene.data),c("glp.row.start","glp.row.end"))
lsn.clms=setdiff(colnames(hit.cnt$lsn.data),c("glp.row.start","glp.row.end"))
gd.clms=c("gene.row",setdiff(gd.clms,"gene.row"))
lsn.clms=c("lsn.row",setdiff(lsn.clms,"lsn.row"))
gene.res=cbind.data.frame(hit.cnt$gene.data[,gd.clms],
hit.cnt$nsubj.mtx,
p.nsubj,
q.nsubj,
p.ord.nsubj,
q.ord.nsubj,
hit.cnt$nhit.mtx,
p.nhit,
q.nhit,
p.ord.nhit,
q.ord.nhit)
res=list(gene.hits=gene.res, # A data table of GRIN results that includes gene annotation, number of subjects and number of hits affecting each locus. in addition, p and FDR adjusted q-values showing the probability of each locus being affected by one or a constellation of multiple types of lesions are also included in the GRIN results.
lsn.data=hit.cnt$lsn.data[,lsn.clms], # Input lesion data
gene.data=hit.cnt$gene.data[,gd.clms], # Input gene data
gene.lsn.data=hit.cnt$gene.lsn.data, # Each row represent a gene overlapped by a certain lesion. Column "gene" shows the overlapped gene and ID column has the patient ID
chr.size=chr.size, # A data table showing the size of the 22 autosomes, in addition to X and Y chromosomes in base pairs
gene.index=hit.cnt$gene.index, # Data.frame with overlapped gene-lesion data rows that belong to each chromosome in the gene.lsn.data table.
lsn.index=hit.cnt$lsn.index) # Data.frame that shows the overlapped gene-lesion data rows taht belong to each lesion in the gene.lsn.data table
return(res)
}
##############################################################################
# grin.stats: The function run the Genomic Random Interval (GRIN) analysis to
# determine whether a certain locus has an abundance of lesions that is
# statistically significant.
grin.stats=function(lsn.data, # data.frame with columns ID (subject identifier), chrom (chromosome on which the lesion is located), loc.start (lesion start position), loc.end (lesion end position), lsn.type (lesion category for example gain, mutation, etc..)
gene.data=NULL, # data.frame with columns gene (ensembl ID), chrom (chromosome on which the gene is located), loc.start (gene statrt position), loc.end (gene end position)
chr.size=NULL, # data.frame with columns chrom (chromosome number) and size (size of the chromosome in base pairs)
genome.version=NULL) # character string with genome version. Currently, four genome assemblies are accepted including "Human_GRCh38", "Human_GRCh37", "Mouse_HGCm39", and "Mouse_HGCm38"
{
if (is.null(genome.version)&&(is.null(gene.data)||is.null(chr.size)))
{
genome.version=select.list(c("Human_GRCh38",
"Human_GRCh37",
"Mouse_HGCm39",
"Mouse_HGCm38"))
}
if (is.character(genome.version))
{
ensembl.data=get.ensembl.annotation(genome.version)
if (is.null(gene.data))
{
gene.data=ensembl.data$gene.annotation
}
chrom.size=get.chrom.length(genome.version)