-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathauxiliary_gwastree_functions.R
1110 lines (1056 loc) · 39.9 KB
/
auxiliary_gwastree_functions.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
#!/usr/bin/R
# --------------------------------------
# Auxiliary functions for gwas analysis:
# --------------------------------------
domain = system("hostname -d", intern=TRUE)
if (length(domain) == 0) domain = ''
if (domain == 'broadinstitute.org'){
bindir='~/data/EPIMAP_ANALYSIS/bin/'
} else {
bindir='~/EPIMAP_ANALYSIS/bin/'
}
source(paste0(bindir, 'auxiliary_gwastree_plotting_functions.R'))
library(circlize)
library(dendextend)
library(ComplexHeatmap)
library(dplyr)
library(viridis)
# -----------------
# For pruning SNPs:
# -----------------
prune.snps = function(suid, df=gwdf, dist=5e3, quiet=TRUE){
subdf = df[df$uid == suid,]
keptdf = c()
chrlist = c(as.character(1:22), 'X')
keptlist = sapply(rep(0, 23), function(x){c()})
names(keptlist) = chrlist
for (i in 1:nrow(subdf)){
chrom = as.character(subdf$chrom[i])
loc = subdf$chromStart[i]
# Check if any within range/add:
if (is.null(keptlist[[chrom]])){
keptlist[[chrom]] = loc
} else {
nclose = sum(abs(loc - keptlist[[chrom]]) < dist)
if (nclose == 0){
keptlist[[chrom]] = c(keptlist[[chrom]], loc)
}
}
}
# Report out - chrom, loc, uid is sufficient:
kdf = c()
for (chrom in chrlist){
if (!is.null(keptlist[[chrom]])){
kdf = rbind(kdf, data.frame(chrom=chrom,
chromStart=keptlist[[chrom]],
uid=suid))
}
}
if (!quiet){ print(paste(suid, ': Kept', nrow(kdf), 'of',nrow(subdf))) }
return(kdf)
}
# ----------------
# Get descendants:
# ----------------
get_dec <- function(subdend, declist, parent=0){
node = attributes(unclass(subdend))$nodePar$pch
# GET ID OF NODE:
nset = declist$dec[[node]]
# if (length(nset) == 0){ declist$dec[[node]] = NA }
if (length(nset) == 0 || is.na(nset)){
if (length(subdend) == 2){
# Get subtrees and their node #s:
dend1 = subdend[[1]]
dend2 = subdend[[2]]
d1 = attributes(unclass(dend1))$nodePar$pch
d2 = attributes(unclass(dend2))$nodePar$pch
# Update each node:
declist = get_dec(dend1, declist, parent=node)
declist = get_dec(dend2, declist, parent=node)
# Merge each side's descendants:
declist$dec[[node]] = c(declist$dec[[d1]], declist$dec[[d2]])
declist$isleaf[node] = 0
} else {
# If leaf, get epigenome from matrix:
slab = labels(subdend)
declist$dec[[node]] = slab
declist$isleaf[node] = 1
}
declist$parent[node] = parent
} else { print("Already have consensus at this node") }
return(declist)
}
# -------------------------
# Functions for enrichment:
# -------------------------
run.hyper <- function(y){phyper(q=y[1]-1, m=y[3], n=y[4] - y[3], k=y[2], lower.tail=FALSE)}
run.fisher <- function(y){
table <- matrix(as.numeric(c(y[1], y[2], y[3], y[4])), ncol = 2, byrow = TRUE)
if(any(is.na(table))) p <- "error" else p <- fisher.test(table, alternative="two.sided")$p.value
return(p)
}
CUTP=12
get_pvalues = function(strait, dflist, cdlenlist, pdf, cutp=CUTP,
ingroup='diff', outgroup='cons', against.parent=TRUE){
# Find the GWAS uid with most snps:
sgw = aggregate(pValue ~ uid + trait, gwdf[gwdf$trait == strait,], length)
suid = sgw[order(sgw$pValue, decreasing=T), 'uid'][1]
# Measure numbers of ingroup SNPs (in any group):
# TODO: Allow ingroup parent?
isubdf = filter(dflist[[ingroup]], uid == suid)
isnp = rep(0, NN)
isnp[isubdf$node] = isubdf$nsnp
ilen = cdlenlist[[ingroup]]
# Make title:
title = paste('Node', capitalize(ingroup),'vs.')
# Measure numbers of outgroup SNPs (in any group + enh):
if (outgroup == 'enh'){
if (singlematch){
# osnp = filter(nnumdf, uid == suid)$queryHits
osnp = filter(nallnumdf, uid == suid)$queryHits
} else {
# osnp = filter(qnumdf, uid == suid)$queryHits
osnp = filter(qallnumdf, uid == suid)$queryHits
}
olen = length(enhind)
title = paste(title, 'All Enhancers')
} else {
osubdf = filter(dflist[[outgroup]], uid == suid) # SNPS in novel
osnp = rep(0, NN)
osnp[osubdf$node] = osubdf$nsnp
olen = cdlenlist[[outgroup]]
if (against.parent){
# Reorder snps and lengths by parent:
osnp = osnp[declist$parent]
olen = olen[declist$parent]
title = paste(title, 'Parent', capitalize(outgroup))
} else {
title = paste(title, 'Node', capitalize(outgroup))
}
}
# Hyper-geometric testing:
df = cbind(q=isnp, draw=ilen,
m=osnp, N=olen)
pout <- apply(df, 1, run.hyper)
df = data.frame(df)
df$pout = pout
df$padj = p.adjust(df$pout)
df$padj[df$padj == 0] = min(df$padj[df$padj != 0])
df$rawlog10p = -log10(df$padj)
# Cap and threshold the adjusted log10p:
df$log10p = df$rawlog10p
df$log10p[df$log10p < 3] = 0
df$log10p[df$log10p > cutp] = cutp
return(list(log10p=df$log10p, isnp=isnp, rawlp=df$rawlog10p, df=df, title=title))
}
# Test leaves against all enhancers:
get_pvalues_leaves = function(trait, dflist, cdlenlist, declist, cutp=CUTP){
if (trait %in% gwdf$trait){
sgw = aggregate(pValue ~ uid + trait, gwdf[gwdf$trait == trait,], length)
suid = as.character(sgw[order(sgw$pValue, decreasing=T), 'uid'][1])
} else if (trait %in% gwdf$uid){
suid = trait
} else {
stop(paste0("Cannot find trait/uid:", trait))
}
# Measure numbers of consensus SNPs:
isubdf = filter(dflist[['cons']], uid == suid)
isnp = rep(0, NN)
isnp[isubdf$node] = isubdf$nsnp
ilen = cdlenlist[['cons']]
# Measure numbers of enhancer snps:
if (singlematch){
osnp = filter(nallnumdf, uid == suid)$queryHits
} else {
osnp = filter(qallnumdf, uid == suid)$queryHits
}
olen = length(enhind)
# Hyper-geometric testing:
df = cbind(q=isnp, draw=ilen,
m=osnp, N=olen)
# Reduce to just leaves + test:
ind = which(declist$isleaf == 1)
df = df[ind,]
pout <- apply(df, 1, run.hyper)
df$node = ind # Node number on tree
df$pout = pout
df$padj = p.adjust(df$pout)
df$padj[df$padj == 0] = min(df$padj[df$padj != 0])
df$rawlog10p = -log10(df$padj)
# Cap and threshold the adjusted log10p:
df$log10p = df$rawlog10p
df$log10p[df$log10p < 3] = 0
df$log10p[df$log10p > cutp] = cutp
return(df)
}
# ------------------------------
# GWAS enrichment for a flat set
# ------------------------------
# INPUT:
# trait
# dflist (all trait x flat interactions)
# lenlist
get_pvalues_flat = function(trait, dflist, lenlist, qdf, NF,
cutp=CUTP, minp=3, olen=NULL){
# Traits:
if (trait %in% qdf$uid){
suid = trait
} else {
stop(paste0("Cannot find trait/uid:", trait))
}
# Measure numbers of consensus SNPs:
isubdf = filter(dflist, uid == suid)
isnp = rep(0, NF)
isnp[isubdf$node] = isubdf$nsnp
# Measure numbers of enhancer snps:
osnp = filter(qallnumdf, uid == suid)$queryHits
if (is.null(olen)){ olen = length(enhind) }
# Hyper-geometric testing:
df = cbind(q=isnp, draw=lenlist, m=osnp, N=olen)
pout <- apply(df, 1, run.hyper)
# Output dataframe:
df = data.frame(node=1:NF, pout=pout)
ll = pvalsdf.tolist(df, minp, cutp)
ll$title = ""
ll$isnp = isnp
return(ll)
}
# Take enhancer sets + intersection dataframe
# Return number of intersecting snps per enhancer set:
eval.intersections <- function(enhsets, qdf, map.enh=TRUE){
dflist = c()
NL = length(enhsets)
for (i in 1:NL){
enhset = enhsets[[i]]
# Map enhset to enhind (necessary for dflist)
if (map.enh) { enhset = enhmap[enhset] }
enhtmp = merge(qdf, data.frame(subjectHits = enhset))
if (nrow(enhtmp) > 0){
# Count number of interactions (enh-snp) in set, add to dataframe:
dfsnp = aggregate(queryHits ~ uid, enhtmp, length)
names(dfsnp) = c('uid','nsnp')
dfsnp$node = i
dflist = rbind(dflist, dfsnp)
}
}
return(dflist)
}
setup_dendrogram <- function(dend, ll, udf, declist, altline=3, cutp=CUTP, bcutoff=6, altlwd=c(1,1), palette=col3, ntop=5){
log10p = ll$log10p
# Color for strength of p-value:
bins <- cut(log10p, seq(0, cutp, length.out=length(palette)), include.lowest=T)
cc = palette[bins]
# Highlight the top hits:
sord = order(ll$rawlp, ll$isnp, decreasing=T)
tophits = head(sord, ntop)
tophits = tophits[ll$log10p[tophits] > 0] # Don't plot NS
cexlist = .35 * (log10p > 0)
cexlist[tophits] = .85
pchlist = rep(19,NN)
pchlist[tophits] = 21
dend = set(dend, 'nodes_pch', pchlist)
dend = set(dend, 'nodes_cex', cexlist)
dend = set(dend, 'nodes_col', cc)
# For counting:
dend.dec = set(dend, 'nodes_pch', 1:NN)
declist = list(dec=sapply(rep(NA, NN), list), isleaf=rep(NA, NN), parent=rep(NA, NN))
declist = get_dec(dend.dec, declist=declist)
# Get all descendant branches:
keeplp = which((log10p >= bcutoff) * (1 - declist$isleaf) == 1) # Remove if "only" leaf
hits = unique(as.character(unlist(sapply(keeplp, function(x){declist$dec[[x]]}))))
# All hits and leaf hits:
leafhits = (log10p >= bcutoff)
allhits = as.character(unlist(sapply(which(leafhits), function(x){declist$dec[[x]]})))
if (length(allhits) > 0){
hitdf = merge(data.frame(lab=allhits, nhit=1), data.frame(i=1:length(labels(dend)), lab=labels(dend)))
hitdf = aggregate(nhit ~ lab + i, hitdf, sum)
} else { hitdf = NULL }
nhits = which(labels(dend) %in% hits)
# NOTE: Could set altline to either 3 or 0 (blank - faster plotting)
dend = set(dend, "by_labels_branches_lty", value=hits, TF_values = c(1, altline))
dend = set(dend, "by_labels_branches_lwd", value=hits, TF_values = altlwd)
# Edit colors:
nl = which(declist$isleaf == 1)
udf$col = 'grey65'
udf$col[which(nl %in% which(leafhits == 1))] = 'red4'
return(list(dend=dend, hits=leafhits, udf=udf, nhits=nhits, hitdf=hitdf))
}
# TODO: Stop evaluating parent twice - store llk
# Is slow. can we speed up by not evaluating certain ones?
get_lr_pvalues = function(strait, dflist, cdlenlist, pdf, cutp=CUTP,
ingroup='diff', outgroup='cons', against.parent=TRUE){
# Trait and example place:
sgw = aggregate(pValue ~ uid + trait, gwdf[gwdf$trait == strait,], length)
suid = sgw[order(sgw$pValue, decreasing=T), 'uid'][1]
# TODO: import NN/NE
NE = nrow(enhdf)
pvals = rep(1, NN)
for (i in 1:NN){
print(paste(i, "is leaf?", declist$isleaf[i]))
p = declist$parent[i]
# Create vectors:
allsnps = rep(0, NE)
dann = rep(0, NE)
pann = rep(0, NE)
# Sets:
if (singlematch){
allsnpset = ndf[ndf$uid == suid, 'subjectHits']
} else {
allsnpset = qdf[qdf$uid == suid, 'subjectHits']
}
pset = enhmap[cdll[['cons']][[p]]]
dset = enhmap[cdll[['diff']][[i]]]
# Fill:
dann[dset] = 1
pann[pset] = 1
allsnps[allsnpset] = 1
# print(paste(sum(pann), sum(dann), sum(allsnps)))
testdf = data.frame(snp=snps, parent=pann, node=ann, allsnp=allsnps, intercept=1)
testdf$diff = testdf$node - testdf$parent
# Log reg for each (~ 3.5 seconds)
ptm <- proc.time()
nmat = cbind(pann, 1)
dmat = cbind(pann, dann, 1)
nmodflr = fastLR(x=nmat, y=testdf$allsnp)
dmodflr = fastLR(x=dmat, y=testdf$allsnp)
lrdflr = dmodflr$loglikelihood - nmodflr$loglikelihood
print(paste(sapply(c(nmodflr$loglikelihood, dmodflr$loglikelihood, lrdflr), digits=2, round)))
pvals[i] = pchisq(lrdflr, df=1, lower.tail=FALSE)
print(proc.time() - ptm)
}
# Make into dataframe:
df = data.frame(node=1:NN, parent=declist$parent[1:NN])
df$pout = pvals
df$padj = p.adjust(df$pout)
df$padj[df$padj == 0] = min(df$padj[df$padj != 0])
df$rawlog10p = -log10(df$padj)
# Cap and threshold the adjusted log10p:
df$log10p = df$rawlog10p
df$log10p[df$log10p < 3] = 0
df$log10p[df$log10p > cutp] = cutp
title = ''
return(list(log10p=df$log10p, isnp=isnp, rawlp=df$rawlog10p, df=df, title=title))
}
# -------------------------------------------
# Fast implementation of logistic regression:
# -------------------------------------------
# Auxiliary:
sigm = function(z) {1 / (1 + exp(-z))}
rsigm = function(p) {log(p / (1 - p))}
# TODO: Add stopping + throw NA if looks like it isnt converging.
calc_disjoint_lr = function(y, x1, x2=NULL, gamma=0.001, max.iter=1500, weights=NULL, pctcut=0.0001, calc.pvals=TRUE){
NE = length(y)
if (is.null(weights)){ weights = rep(1, NE) }
if (is.null(x2)){ x2 = rep(0, NE) }
# Count statistics (could be weighted)
N = length(y * weights)
NY = sum(y * weights)
NX1 = sum(x1 * weights)
NTP1 = sum(x1 * y * weights)
NFP1 = NX1 - NTP1
NX2 = sum(x2 * weights)
NTP2 = sum(x2 * y * weights)
NFP2 = NX2 - NTP2
# For intercept, not the FP/TP:
NI1 = NY - NTP1 - NTP2 # Y=1, X1=0, X2=0, I=1 (remaining snps)
NI2 = N - NY - NFP1 - NFP2 # Y=0, X1=0, X2=0, I=1 (remaining loc - NY contains NTP1, NTP2)
# Initialize coefficients:
coeff = rep(0,3)
pc = 0.01 / N # with pseudo counts relative to size of N
coeff[3] = rsigm((NY - NTP1 - NTP2)/(N - (NX1 + NX2)))
coeff[1] = rsigm((NTP1 + pc)/(NX1)) - coeff[3]
if (NX2 > 0){
coeff[2] = rsigm((NTP2 + pc)/(NX2)) - coeff[3]
} else {
coeff[2] = 0
}
j = 1
pctchange = 100
# Update equations:
while (pctchange > pctcut && j < max.iter){
j = j + 1
oldcoeff = coeff
# Update x1 (1):
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[2] + coeff[3])
inner3 = sigm(coeff[3])
coeff[1] = oldcoeff[1] +
gamma * (NTP1 * (1 - inner1) + NFP1 * (0 - inner1))
# Update x2 (2):
if (NX2 > 0){
coeff[2] = oldcoeff[2] +
gamma * (NTP2 * (1 - inner2) + NFP2 * (0 - inner2))
}
# Update intercept (3):
coeff[3] = oldcoeff[3] +
gamma * (NTP1 * (1 - inner1) + NFP1 * (0 - inner1) +
NTP2 * (1 - inner2) + NFP2 * (0 - inner2) +
NI1 * (1 - inner3) + NI2 * (0 - inner3))
# Percent updated:
pctchange = sum(abs(coeff - oldcoeff)) / sum(abs(oldcoeff))
}
# Calculate the log likelihood
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[2] + coeff[3])
inner3 = sigm(coeff[3])
llk = (NTP1 * log(inner1) + NFP1 * log(1 - inner1) +
NTP2 * log(inner2) + NFP2 * log(1 - inner2) +
NI1 * log(inner3) + NI2 * log(1 - inner3))
# Return the parameters:
out = list(loglikelihood=llk, coefficients=coeff)
if (calc.pvals){
# Calc second derivatives
# x = cbind(x1, x2, rep(1, N))
# est = exp(x %*% coeff)
# est = est / (1 + est)^2
# xw = sweep(x, 1, est, '*')
# der2 = t(xw) %*% x
# Calc estimates without matrix multiplication:
c13 = (exp(coeff[1] + coeff[3]) / (1 + exp(coeff[1] + coeff[3]))^2)
c23 = (exp(coeff[2] + coeff[3]) / (1 + exp(coeff[2] + coeff[3]))^2)
c3 = (exp(coeff[3]) / (1 + exp(coeff[3]))^2)
# Products:
der2 = matrix(0, nrow=3, ncol=3)
der2[3,3] = NX1 * c13 + NX2 * c23 + (NI1 + NI2) * c3
der2[1,1] = NX1 * c13
der2[1,3] = NX1 * c13
der2[3,1] = NX1 * c13
der2[2,2] = NX2 * c23
der2[2,3] = NX2 * c23
der2[3,2] = NX2 * c23
# Disjoint means fast Cholesky decomposition:
chdr = sqrt(der2)
chdr[3,] = 0
chdr[3,3] = sqrt(der2[3,3] - der2[1,3] - der2[2,3])
if (NX2 == 0){ chdr = chdr[c(1,3),c(1,3)] }
se <- try(chol2inv(chdr), silent=T) # Non-negligible time
# se <- try(chol2inv(chol(der2)), silent=T) # Non-negligible time
if (class(se) != 'try-error'){
se <- sqrt(diag(se))
# Calculate p-vals by Wald's test rather than chisq:
if (NX2 == 0){
wald <- coeff[c(1,3)] /se
} else {
wald <- coeff/se
}
pval <- 2 * pnorm(abs(wald), lower.tail = FALSE)
out$pvals = pval
}
}
return(out)
}
# TODO: Add stopping + throw NA if looks like it isnt converging.
# For where x2 is subset of x1
calc_subset_lr = function(y, x1, x2=NULL, gamma=0.001, max.iter=1500, weights=NULL){
NE = length(y)
if (is.null(weights)){ weights = rep(1, NE) }
if (is.null(x2)){ x2 = rep(0, NE) }
# Count statistics (could be weighted)
N = length(y * weights)
NY = sum(y * weights)
NX1 = sum(x1 * weights)
NTP1 = sum(x1 * y * weights) # Superset of NTP2
NFP1 = NX1 - NTP1 # Superset of NFP2
NX2 = sum(x2 * weights)
NTP2 = sum(x2 * y * weights)
NFP2 = NX2 - NTP2
# For intercept, not the FP/TP:
NI1 = NY - NTP1
NI2 = N - (NY + NFP1)
# Initialize coefficients:
coeff = rep(0,3)
pc = 0.000001 # with pseudo counts
coeff[3] = rsigm(NY/N)
coeff[1] = rsigm((NTP1 + pc)/(NTP1 + NFP1)) - coeff[3]
# TODO: Doublecheck?
if (NX2 > 0){
coeff[2] = rsigm((NTP2 + pc)/(NTP2 + NFP2)) - coeff[3]
} else {
coeff[2] = 0
}
j = 1
pctchange = 100
# Update equations::
while (pctchange > 0.0001 && j < max.iter){
j = j + 1
oldcoeff = coeff
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[1] + coeff[2] + coeff[3]) # Because x2 is subset of x1
inner3 = sigm(coeff[3])
# Update x1 (1):
coeff[1] = oldcoeff[1] + gamma * ((NTP1 - NTP2) * (1 - inner1) + (NFP1 - NFP2) * (0 - inner1) +
NTP2 * (1 - inner2) + NFP2 * (0 - inner2))
# Update x2 (2):
if (NX2 > 0){
coeff[2] = oldcoeff[2] + gamma * (NTP2 * (1 - inner2) + NFP2 * (0 - inner2))
}
# Update intercept (3):
coeff[3] = oldcoeff[3] + gamma * ((NTP1 - NTP2) * (1 - inner1) + (NFP1 - NFP2) * (0 - inner1) +
NTP2 * (1 - inner2) + NFP2 * (0 - inner2) +
NI1 * (1 - inner3) + NI2 * (0 - inner3))
# Percent updated:
pctchange = sum(abs(coeff - oldcoeff)) / sum(abs(oldcoeff))
}
# Calculate the log likelihood
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[1] + coeff[2] + coeff[3])
inner3 = sigm(coeff[3])
llk = ((NTP1 - NTP2) * log(inner1) + (NFP1 - NFP2) * log(1 - inner1) +
NTP2 * log(inner2) + NFP2 * log(1 - inner2) +
NI1 * log(inner3) + NI2 * log(1 - inner3))
# Return the parameters:
return(list(loglikelihood=llk, coefficients=coeff))
}
# TODO: Add stopping + throw NA if looks like it isnt converging.
# For where x2 is subset of x1
calc_subset_lr = function(y, x1, x2=NULL, gamma=0.001, max.iter=1500, weights=NULL){
NE = length(y)
if (is.null(weights)){ weights = rep(1, NE) }
if (is.null(x2)){ x2 = rep(0, NE) }
# Count statistics (could be weighted)
N = length(y * weights)
NY = sum(y * weights)
NX1 = sum(x1 * weights)
NTP1 = sum(x1 * y * weights) # Superset of NTP2
NFP1 = NX1 - NTP1 # Superset of NFP2
NX2 = sum(x2 * weights)
NTP2 = sum(x2 * y * weights)
NFP2 = NX2 - NTP2
# For intercept, not the FP/TP:
NI1 = NY - NTP1
NI2 = N - (NY + NFP1)
# Initialize coefficients:
coeff = rep(0,3)
pc = 0.000001 # with pseudo counts
coeff[3] = rsigm(NY/N)
coeff[1] = rsigm((NTP1 + pc)/(NTP1 + NFP1)) - coeff[3]
# TODO: Doublecheck?
if (NX2 > 0){
coeff[2] = rsigm((NTP2 + pc)/(NTP2 + NFP2)) - coeff[3]
} else {
coeff[2] = 0
}
j = 1
pctchange = 100
# Update equations::
while (pctchange > 0.0001 && j < max.iter){
j = j + 1
oldcoeff = coeff
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[1] + coeff[2] + coeff[3]) # Because x2 is subset of x1
inner3 = sigm(coeff[3])
# Update x1 (1):
coeff[1] = oldcoeff[1] + gamma * ((NTP1 - NTP2) * (1 - inner1) + (NFP1 - NFP2) * (0 - inner1) +
NTP2 * (1 - inner2) + NFP2 * (0 - inner2))
# Update x2 (2):
if (NX2 > 0){
coeff[2] = oldcoeff[2] + gamma * (NTP2 * (1 - inner2) + NFP2 * (0 - inner2))
}
# Update intercept (3):
coeff[3] = oldcoeff[3] + gamma * ((NTP1 - NTP2) * (1 - inner1) + (NFP1 - NFP2) * (0 - inner1) +
NTP2 * (1 - inner2) + NFP2 * (0 - inner2) +
NI1 * (1 - inner3) + NI2 * (0 - inner3))
# Percent updated:
pctchange = sum(abs(coeff - oldcoeff)) / sum(abs(oldcoeff))
}
# Calculate the log likelihood
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[1] + coeff[2] + coeff[3])
inner3 = sigm(coeff[3])
llk = ((NTP1 - NTP2) * log(inner1) + (NFP1 - NFP2) * log(1 - inner1) +
NTP2 * log(inner2) + NFP2 * log(1 - inner2) +
NI1 * log(inner3) + NI2 * log(1 - inner3))
# Return the parameters:
return(list(loglikelihood=llk, coefficients=coeff))
}
# TODO: REMOVE Testing data:
# y = allsnps
# x = cbind(pann, dann)
# weights = NULL
# General fast LR for observations and predictions in {0,1}
# TODO: Add stopping + throw NA if looks like it isnt converging.
# TODO: Extend to any sets:
# Note: Here X is a matrix with ncol = nvar
calc_general_lr = function(y, x, gamma=0.001, max.iter=1500, weights=NULL){
ptm <- proc.time()
# Count statistics:
N = length(y)
NY = sum(y)
NVAR = ncol(x)
# if (is.null(weights)){ weights = rep(1, N) }
if (NVAR == 1){ x = cbind(x, rep(0,N)) }
# Get all contingency counts
# NOTE: May be slower than manually counting ~ .3s instead of .12:
ctcounts = rep(0, 2^(NVAR + 1))
for (vy in c(0, 1)){
if (vy == 1){ iy = y } else { iy = 1 - y }
# TODO: Now extend this to work for diff # of var.
for (v1 in c(0, 1)){
if (v1 == 1){ i1 = x[,1] } else { i1 = 1 - x[,1] }
for (v2 in c(0, 1)){
if (v2 == 1){ i2 = x[,2] } else { i2 = 1 - x[,2] }
# Store counts in binary (0:7 in 1:8):
ind = (1 + vy * 1 + v1 * 2 + v2 * 4)
if (is.null(weights)){
ctcounts[ind] = sum(iy * i1 * i2)
} else {
ctcounts[ind] = sum(iy * i1 * i2 * weights)
}
}
}
}
proc.time() - ptm
# Initialize coefficients:
coeff = rep(0,3)
pc = 0.000001 # with pseudo counts
coeff[3] = rsigm(NY/N)
coeff[1] = rsigm((sum(ctcounts[1 + 1 + 2 + 4*0:1]) + pc) /
(sum(ctcounts[1 + 1 + 2 + 4*0:1]) + sum(ctcounts[1 + 0 + 2 + 4*0:1]))) - coeff[3]
if (NVAR > 1){
coeff[2] = rsigm((sum(ctcounts[1 + 1 + 2*0:1 + 4]) + pc) /
(sum(ctcounts[1 + 1 + 2*0:1 + 4]) + sum(ctcounts[1 + 0 + 2*0:1 + 4]))) - coeff[3]
} else {
coeff[2] = 0
}
j = 1
pctchange = 100
# Update equations::
while (pctchange > 0.0001 && j < max.iter){
j = j + 1
oldcoeff = coeff
# Update x1 (1):
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[2] + coeff[3])
inner3 = sigm(coeff[3])
coeff[1] = oldcoeff[1] + gamma * (NTP1 * (1 - inner1) + NFP1 * (0 - inner1))
# Update x2 (2):
if (NX2 > 0){
coeff[2] = oldcoeff[2] + gamma * (NTP2 * (1 - inner2) + NFP2 * (0 - inner2))
}
# Update intercept (3):
coeff[3] = oldcoeff[3] + gamma * (NTP1 * (1 - inner1) + NFP1 * (0 - inner1) +
NTP2 * (1 - inner2) + NFP2 * (0 - inner2) +
NI1 * (1 - inner3) + NI2 * (0 - inner3))
# Percent updated:
pctchange = sum(abs(coeff - oldcoeff)) / sum(abs(oldcoeff))
}
# Calculate the log likelihood
inner1 = sigm(coeff[1] + coeff[3])
inner2 = sigm(coeff[2] + coeff[3])
inner3 = sigm(coeff[3])
llk = (NTP1 * log(inner1) + NFP1 * log(1 - inner1) +
NTP2 * log(inner2) + NFP2 * log(1 - inner2) +
NI1 * log(inner3) + NI2 * log(1 - inner3))
# Return the parameters:
return(list(loglikelihood=llk, coefficients=coeff))
}
# Was causing issues in interpretation, leaving for posterity.
pvalsdf.tolist.old = function(df, minp, cutp){
df$padj = p.adjust(df$pout)
df$padj[df$padj == 0] = min(df$padj[df$padj != 0])
df$rawlog10p = -log10(df$padj)
# Cap and threshold the adjusted log10p:
df$log10p = df$rawlog10p
df$log10p[df$log10p < minp] = 0
df$log10p[df$log10p > cutp] = cutp
ll = list(log10p=df$log10p, rawlp=df$rawlog10p, df=df)
}
# Actually does what it says (raw vs. adjusted)
pvalsdf.tolist = function(df, minp, cutp){
df$rawlog10p = -log10(df$pout)
df$padj = p.adjust(df$pout)
df$padj[df$padj == 0] = min(df$padj[df$padj != 0])
# Cap and threshold the adjusted log10p:
df$log10p = -log10(df$padj)
df$log10p[df$log10p < minp] = 0
df$log10p[df$log10p > cutp] = cutp
ll = list(log10p=df$log10p, rawlp=df$rawlog10p, df=df)
}
# ALTERNATE REGRESSION (slower):
# dmodel = calc_disjoint_lr(y=allsnps, x1=pann, x2=dann, gamma=gamma, weights=weights)
# pmat = cbind(parent=pann, intercept=1)
# dmat = cbind(parent=pann, diff=dann, intercept=1)
# dmat = cbind(diff=dann, pmat)
# NOTE: Can do cons-p vs. cons-p \\or// cons-p vs. cons-p + diff.
# Options tend to be similar.
# Log reg for each (~ 3.5 seconds)
# p2 = declist$parent[p]
# pmodflr = fastLR(x=pmat, y=y, start=pcoeff[[p2]])
# Better log reg for each (~ .01 seconds)
alt_lr = function(y, x1, x2=NULL, gamma=0.001, max.iter=1500, weights=NULL){
require(RcppNumerical)
# Make x matrix. (NOTE: y is allsnps vector)
xmat = cbind(parent=x1, intercept=1)
if (!is.null(x2)){
xmat = cbind(parent=x1, diff=x2, intercept=1)
}
# Calculate model:
model = fastLR(x=xmat, y=y)
return(list(loglikelihood = model$loglikelihood,
coefficients=model$coeff))
}
# TODO: Clean up function + put in chunks
get_disjoint_lr = function(trait, type, against, qdf, minp=3, cutp=CUTP,
weights=NULL, verbose=FALSE, return.coeff=FALSE,
alt.lr=FALSE, max.iter=1500){
# Trait and example place:
if (trait %in% qdf$uid){
suid = trait
} else {
if (trait %in% gwdf$trait){
sgw = aggregate(pValue ~ uid + trait, gwdf[gwdf$trait == trait,], length)
suid = sgw[order(sgw$pValue, decreasing=T), 'uid'][1]
} else {
stop(paste0("Cannot find trait/uid:", trait))
}
}
NE = nrow(enhdf)
pvals = rep(1, NN)
pllk = rep(NA, NN)
dllk = rep(NA, NN)
diffllk = rep(NA, NN)
isnp = rep(0, NN)
if (return.coeff){ coeffmat = matrix(rep(0, NN * 3), nrow=NN, ncol=3) }
pcoeff = sapply(rep(NA, NN), list)
allsnpset = qdf[qdf$uid == suid, 'subjectHits']
allsnps = rep(0, NE)
allsnps[allsnpset] = 1
title = paste('Node', capitalize(type),'vs.')
if(against == 'enhancers'){
title = paste(title, 'All Enhancers')
} else {
title = paste(title, capitalize(against), capitalize(type))
}
ptm <- proc.time()
pb = txtProgressBar(min=0, max=NN, style = 3)
for (i in 1:NN){
setTxtProgressBar(pb, i)
# Create vectors:
nann = rep(0, NE)
pann = rep(0, NE)
# Sets:
if (against == 'parent'){
p = declist$parent[i]
pset = enhmap[cdll[[type]][[p]]]
} else if (against == 'sibling'){
parent = declist$parent[i]
d2 = which(declist$parent == parent)
p = d2[d2 != i]
# TODO: write get sibling
# TODO: will need different regression logic
} else if (against == 'enhancers'){
pset = 1:NE
p = 1 # Put the output here - same parent regression
# TODO: Will different regression - for difference
# All enhancers vs. all enhancers + node enhancers (separately)
# - need to update calc_disjoint_lr to do NONDISJOINT.
}
nset = enhmap[cdll[[type]][[i]]]
if (length(pset) == 0 || length(nset) == 0 || length(nset) == length(pset)){
pvals[i] = 1
} else {
# Fill vectors:
nann[nset] = 1
pann[pset] = 1
dann = nann - pann
isnp[i] = sum(nann * allsnps)
gamma = 0.01 # gamma = 0.00001
if (type == 'union' || (!is.null(weights))){ gamma = gamma * 0.01 } # Tends to have high NA issues
pll.condition = (is.na(pllk[p]) || pllk[p] > 0)
while (is.na(pllk[p]) || pllk[p] > 0){
if (alt.lr){
pmodel = alt_lr(y=allsnps, x1=pann)
} else {
pmodel = calc_disjoint_lr(y=allsnps, x1=pann, gamma=gamma, weights=weights, max.iter=max.iter)
}
pcoeff[[i]] = pmodel$coefficients
pllk[p] = pmodel$loglikelihood
pll.condition = (is.na(pllk[p]) || pllk[p] > 0)
if (pll.condition){
if (verbose) { print("NA parent LLK or bad regression, reduced gamma") }
gamma = gamma * 0.1
}
if (gamma < 10^-10) break
}
gamma = 0.0001
if (type == 'union' || (!is.null(weights))){ gamma = gamma * 0.01 } # Tends to have high NA issues
dll.condition = (is.na(dllk[i]) || is.infinite(dllk[i])
|| dllk[i] > 0 || abs(diffllk[i]) > 5000)
while (dll.condition){
# Different purposes:
if (type == 'union' || against == 'enhancers'){
if (alt.lr){
dmodel = alt_lr(y=allsnps, x1=pann, x2=nann)
} else {
dmodel = calc_subset_lr(y=allsnps, x1=pann, x2=nann, gamma=gamma, weights=weights)
}
} else if (type == 'cons') {
if (alt.lr){
dmodel = alt_lr(y=allsnps, x1=pann, x2=dann)
} else {
dmodel = calc_disjoint_lr(y=allsnps, x1=pann, x2=dann, gamma=gamma, weights=weights, max.iter=max.iter)
}
} else {
if (alt.lr){
dmodel = alt_lr(y=allsnps, x1=pann, x2=nann)
} else {
dmodel = calc_disjoint_lr(y=allsnps, x1=pann, x2=nann, gamma=gamma, weights=weights)
}
}
dllk[i] = dmodel$loglikelihood
diffllk[i] = 2 * (dllk[i] - pllk[p]) # NOTE: the LRT uses 2 x log(R1/R0)
# Check condition:
dll.condition = (is.na(dllk[i]) || is.infinite(dllk[i])
|| dllk[i] > 0 || abs(diffllk[i]) > 1000)
if (dll.condition){
if (verbose) {print("NA diff LLK or bad regression, reduced gamma"); }
gamma = gamma * 0.1
if (verbose) {print(paste(sapply(c(i, pllk[p], dllk[i], diffllk[i]), digits=2, round)))}
}
if (gamma < 10^-10) break
}
pvals[i] = pchisq(diffllk[i], df=1, lower.tail=FALSE)
if (diffllk[i] > 1000){
print(paste0("Very large difference in log likelihood reported: Check: ", trait))
print(paste(sapply(c(i, pllk[p], dllk[i], diffllk[i]), digits=2, round)))
}
if(return.coeff){ coeffmat[i,] = dmodel$coeff }
}
}
close(pb)
ptmend = proc.time() - ptm
print(ptmend)
# Make into dataframe:
df = data.frame(node=1:NN, parent=declist$parent[1:NN], pout = pvals)
ll = pvalsdf.tolist(df, minp, cutp)
ll$title= title
ll$isnp = isnp
if (return.coeff){ ll$coeff = coeffmat }
return(ll)
}
# -------------------------
# For adding genes to plot:
# -------------------------
packlist = function(x){
# Make vertical list of two-three each
x = sort(unique(x))
str = ""
# Pack, trying to keep to 4-5 lines max:
ng = length(x)
pack = 1
if (ng > 8){ pack = 3 } else if (ng > 3){ pack = 2 }
for (i in 1:(round(ng / pack - 1))){
for (j in (pack*(i-1) + 1):(pack*i)){
str = paste0(str, x[j])
if (j == pack*i){
str = paste0(str, '\n')
} else {
str = paste0(str, ', ')
}
}
}
if (length(x) %% pack == 1){ str = paste0(str, x[length(x)]) }
if (length(x) == 1){ str = x }
return(str)
}
# Function for getting linked genes:
get.linked.genes = function(ll, suid, by.cons=TRUE, minp=3, allgenes=FALSE){
# 1. Pull out the differential enhancers and their interactions:
# (associated with enrichment val)
snpdf = qdf[qdf$uid == suid, ]
names(snpdf) = c('snpid','enhid','uid')
intset = sort(snpdf$enhid)
redsnpdf = snpdf # For pruning
ldf = ll$df
ldf = ldf[order(ldf$pout),]
if (!allgenes){
ldf = ldf[ldf$rawlog10p > minp,]
}
print(head(ldf,5))
hitdf = c()
for (j in 1:nrow(ldf)){
if (nrow(redsnpdf) == 0){ break }
i = ldf$node[j]
lp = ldf$rawlog10p[j]
p = declist$parent[i]
pset = enhmap[cdll[[type]][[p]]]
nset = enhmap[cdll[[type]][[i]]]
dset = setdiff(nset, pset)
iset = intersect(intset, dset)
if (length(iset) > 0){
idf = data.frame(enhid=iset,
node=i,
log10p=lp,
uid=suid)
# 2. Prune the SNPs - keep only top assoc per SNP (by pval):
# Keeping only one SNP per node (but potentially multiple enhancers)
idf = merge(idf, redsnpdf)
# Remove the hit snps:
snpset = unique(idf$snpid)
redsnpdf = redsnpdf[!(redsnpdf$snpid %in% snpset),]
intset = sort(redsnpdf$enhid)
hitdf = rbind(hitdf, idf)
}
}
nsnp = length(unique(snpdf$snpid))
print(paste0(length(unique(hitdf$snpid))," of ",
nsnp, " snps assigned"))
print(paste0(length(unique(hitdf$snpid[hitdf$log10p > minp]))," of ",
nsnp, " snps (interaction above minp = ", minp,")"))
# 3. Get the locations of the enhancers:
hitdf = cbind(hitdf, enhdf[hitdf$enhid, c('chr','start','end', 'cls')])
hitdf$mid = (hitdf$start + hitdf$end)/2
# Link to genes:
if (!by.cons){
# Option 1: Linked genes by linking agnostic to trees:
# TODO: Need to do linking first for this!
} else {
# Option 2: Linked genes by the RNA-seq consensus:
# 3. Get consensus genes at each node:
cgdf = c()
for (i in unique(hitdf$node)){
igenes = rnall$cons[[i]]
itdf = tssdf[tssdf$gene %in% igenes, c('chr','tss', 'gene')]
itdf$node = i
cgdf = rbind(cgdf, itdf)
}
# 4. Match to nearest consensus gene at each node:
cgdf = merge(cgdf, hitdf)
cgdf$dist = abs(cgdf$tss - cgdf$mid)
cgdf = merge(cgdf, aggregate(dist ~ node + enhid, cgdf, min))
}
# 5. Get the gene names per node + make plotting func:
cgdf = merge(cgdf, gmdf)
print(unique(cgdf$symbol))
# Make legend:
genetextdf = aggregate(symbol ~ node, cgdf, packlist)
genetextdf = merge(genetextdf, nodedf)
# Color (with the node:
genetextdf = merge(genetextdf, unique(cgdf[,c('node','log10p')]))
genetextdf$color = 'black'
genetextdf$color[genetextdf$log10p < minp] = 'grey60'
return(list(nodedf=genetextdf, cgdf=cgdf))
}
# Extract numbers (for sample-size):
munge.nos = function(x){
locs = gregexpr("[0-9][0-9,]*",x)[[1]]
ids = as.numeric(locs)
lns = attributes(locs)$match.length
# Extract:
nos = sapply(1:length(ids), function(j){
num = substr(x, ids[j], ids[j]+ lns[j] - 1)
as.numeric(gsub(",","",num)) })
return(nos)
}
# ------------------------------------------
# Method for cleaning up sample sizes:
# Associate each number with text -
# look for case, control, individual, family
# Remove family and control.
# ------------------------------------------
prune.cases = function(x, only.cases=FALSE){
locs = gregexpr("[0-9][0-9,]*",x)[[1]]
ids = as.numeric(locs)
lns = attributes(locs)$match.length