-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiscFunctions.R
More file actions
1302 lines (1139 loc) · 53.5 KB
/
miscFunctions.R
File metadata and controls
1302 lines (1139 loc) · 53.5 KB
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
#A few handy R-functions
#Author: Simon Forsberg (mostly)
#Get p-value from lm object
lmp <- function (modelobject) {
if (class(modelobject) != "lm") stop("Not an object of class 'lm' ")
f <- summary(modelobject)$fstatistic
if(!is.null(f)){
p <- pf(f[1],f[2],f[3],lower.tail=F)
attributes(p) <- NULL
}
else
p <- NA
return(p)
}
#Called internally from the plot.manhattan functions
#Modified from GenABEL
sortmap <- function (chrom, map, delta = 1)
{
chnum <- as.numeric(as.factor(chrom))
ix <- order(chnum, map)
map <- map[ix]
off <- c(0, map[1:(length(map) - 1)])
off <- map - off
off[which(off <= 0)] <- delta
cummap <- cumsum(off)
#To be used by sortmap.ranges, called from plot.manhattan_highlRegion
chnum.off <- c(0, chnum[1:(length(chnum) - 1)])
chnum.off <- chnum - chnum.off
newChr <- which(chnum.off == 1)
pos2cum <- cummap[newChr] #The starting position of every chromosome in the cumulative coordinates
pos2cum[1] <- 0 #Switch from pos to cumulative pos: pos + pos2cum
names(pos2cum) <- unique(chrom)
out <- list()
out$ix <- ix
out$cummap <- cummap
out$chnum <- chnum
out$pos2cum <- pos2cum
out
}
#Internal function called by plot.manhattan_highlRegions
#Modified from GenABEL
sortmap.ranges <- function (ranges, snp.map)
{
#ranges - A GRanges object with the ranges to be drawn on the plot
#map - The sorted cumulative SNP positions returned by sortmap
chr <- as.character(decode(ranges@seqnames))
#From
from <- start(ranges)
from <- from + snp.map$pos2cum[chr]
#To
to <- end(ranges)
to <- to + snp.map$pos2cum[chr]
#Wrap up
out <- list()
out$cummap.from <- from
out$cummap.to <- to
out$chr <- chr
out
}
plot.manhattan <- function(pos, chr, y, log10 = T, col = wes_palette(name = 'Darjeeling1', n = 2),
cex.yaxis = 1.5, cex.xaxis = 2, ylim = NULL, zoom.chr = NULL, zoom.pos = NULL,
padj.xaxis = NA, cex = .5, ...){
#Partially recycled from GenABELs plot function
#pos = vector with genomic positions
#chr = vector with chromosome info matching pos
#Input has to be sorted
require(wesanderson)
#Make continuous positions for plotting
newmap <- sortmap(chr, pos)
mymap <- newmap$cummap
if(log10)
y <- -log10(y)
if(is.null(zoom.chr)){
if(is.null(ylim))
ylim <- c(0,max(y, na.rm = T))
xlim = c(min(mymap), max(mymap))
chrom.num <- as.numeric(as.factor(as.character(chr)))
chind <- chrom.num%%length(col)
idxCH <- which(chind == 0)
plot(mymap[idxCH], y[idxCH], xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, col = col[length(col)], pch = 19, cex = cex, xlab = '', ylab = '', ...)
axis(side = 2, cex.axis = cex.yaxis)
for (colidx in c(1:(length(col) - 1))) {
idxCH <- which(chind == colidx)
points(mymap[idxCH], y[idxCH], col = col[colidx], pch = 19, cex = .5, ylim = ylim, xlim = xlim, ...)
}
#Axis
chrom.uniq <- unique(chr)
chpos <- c()
for (j in 1:length(chrom.uniq))
chpos[j] <- mean(mymap[chr == chrom.uniq[j]])
axis(side = 1, at = chpos, labels = chrom.uniq, cex.axis = cex.xaxis, padj = padj.xaxis)
}
else{
if(!(zoom.chr %in% chr))
stop(paste('No chromosome named', zoom.chr, 'found in the chr vector', chr))
y <- y[chr == zoom.chr]
pos <- pos[chr == zoom.chr]
chr <- chr[chr == zoom.chr]
if(is.null(zoom.pos))
zoom.pos <- c(min(pos) - 1, max(pos) + 1)
plot(pos[pos > zoom.pos[1] & pos < zoom.pos[2]], y[pos > zoom.pos[1] & pos < zoom.pos[2]],
xaxt = 'n', yaxt = 'n', col = col[1], pch = 19, cex = cex, xlab = '', ylab = '', ...)
axis(side = 2, cex.axis = cex.yaxis)
axis(side = 1, cex.axis = cex.xaxis, padj = padj.xaxis)
}
}
plot.manhattan2 <- function(pos, chr, y, log10 = T, cex.yaxis = 1.5, cex.xaxis = 2,
ylim = NULL, zoom.chr = NULL, zoom.pos = NULL, col = NULL,
xlab = '', ylab = '', xaxt = 's', yaxt = 's', cex = .5, ...){
#Partially recycled from GenABELs plot function
#Different design than plot.manhattan
#pos = vector with genomic positions
#chr = vector with chromosome info matching pos
#y = vector OR a list with vectors. If a list, the results will be overlayed with different colors
#Input has to be sorted
require(wesanderson)
#Make continuous positions for plotting
newmap <- sortmap(chr, pos)
mymap <- newmap$cummap
if(log10){
if(class(y) == 'list')
y <- lapply(y, function(x){-log10(x)})
else
y <- -log10(y)
}
#Multiple results to be overlaid?
if(class(y) == 'list'){
if(is.null(col))
col = wes_palette(name = 'Darjeeling1', n = length(y))
y.list <- y[2:length(y)]
y <- y[[1]]
}
else if(is.null(col))
col = wes_palette(name = 'Darjeeling1', n = 1)
if(is.null(zoom.chr)){
if(is.null(ylim))
ylim <- c(0,max(y, na.rm = T))
xlim = c(min(mymap), max(mymap))
#Setup plot with highlighted chrs
plot(mymap, y, xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, xlab = '', ylab = '', type = 'n')
chrom.uniq <- unique(chr)
chpos <- c()
for (j in 1:length(chrom.uniq)){
chpos[j] <- mean(mymap[chr == chrom.uniq[j]])
#Draw rectangle for "even" chrs
if(j %% 2 == 0){
rect(xleft = min(mymap[chr == chrom.uniq[j]]), ybottom = ylim[1] - 10, xright = max(mymap[chr == chrom.uniq[j]]), ytop = ylim[2] + 10,
col = rgb(220,220,220, maxColorValue = 255, alpha = 120), border = NA)
}
}
axis(side = 1, at = chpos, labels = chrom.uniq, cex.axis = cex.xaxis)
#Draw plot
par(new = T)
plot(mymap, y, xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, col = col[1], pch = 19, cex = cex, xlab = xlab, ylab = ylab, ...)
axis(side = 2, cex.axis = cex.yaxis)
#Overlay more things?
if(exists('y.list')){
for(j in 1:length(y.list)){
points(mymap, y.list[[j]], xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, col = col[j+1], pch = 19, cex = cex, xlab = '', ylab = '', ...)
}
}
}
else{
if(!(zoom.chr %in% chr))
stop(paste('No chromosome named', zoom.chr, 'found in the chr vector', chr))
y <- y[chr == zoom.chr]
pos <- pos[chr == zoom.chr]
if(is.null(zoom.pos))
zoom.pos <- c(min(pos) - 1, max(pos) + 1)
#Draw plot
plot(pos[pos > zoom.pos[1] & pos < zoom.pos[2]],
y[pos > zoom.pos[1] & pos < zoom.pos[2]],
xaxt = 'n', yaxt = 'n', col = col[1], pch = 19, cex = cex, xlab = '', ylab = '', ylim = ylim, ...)
#Overlay more things?
if(exists('y.list')){
for(j in 1:length(y.list)){
y.overlay <- y.list[[j]][chr == zoom.chr]
points(pos[pos > zoom.pos[1] & pos < zoom.pos[2]],
y.overlay[pos > zoom.pos[1] & pos < zoom.pos[2]],
xaxt = 'n', yaxt = 'n', ylim = ylim, col = col[j+1], pch = 19, cex = cex, xlab = '', ylab = '', ...)
}
}
if(yaxt != 'n')
axis(side = 2, cex.axis = cex.yaxis)
if(xaxt != 'n')
axis(side = 1, cex.axis = cex.xaxis)
}
}
plot.manhattan3 <- function(pos, chr, y, log10 = T, cex.yaxis = 1.5,
cex.xaxis = 2, ylim = NULL, zoom.chr = NULL,
zoom.pos = NULL, pch = 19, cex = .5,
highlSNP = NULL, col.highl = 'red', cex.highl = 2, pch.highl = 17,
col = 'gray', padj.xaxis = NA, ...){
#Partially recycled from GenABELs plot function
#The idea with this function is to customly color each SNP. For instance, by LD to a focal SNP
#Input has to be sorted
#pos = vector with genomic positions
#chr = vector with chromosome info matching pos
#y = vector with values to put on the y
require(wesanderson)
# stopifnot(!is.null(col))
#Make continuous positions for plotting
newmap <- sortmap(chr, pos)
mymap <- newmap$cummap
if(log10)
y <- -log10(y)
# pch <- rep(19, length(pos))
# cex <- rep(.5, length(pos))
# col <- rep(col, length(pos))
# if(!is.null(highlSNP)){ #highlight SNP
# pch[highlSNP] <- pch.highl
# cex[highlSNP] <- cex.highl
# col[highlSNP] <- col.highl
# }
if(is.null(zoom.chr)){
if(is.null(ylim))
ylim <- c(0,max(y, na.rm = T))
xlim = c(min(mymap), max(mymap))
#Setup plot with highlighted chrs
plot(mymap, y, xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, xlab = '', ylab = '', type = 'n')
chrom.uniq <- unique(chr)
chpos <- c()
for (j in 1:length(chrom.uniq)){
chpos[j] <- mean(mymap[chr == chrom.uniq[j]])
#Draw rectangle for "even" chrs
if(j %% 2 == 0){
rect(xleft = min(mymap[chr == chrom.uniq[j]]), ybottom = ylim[1] - 10, xright = max(mymap[chr == chrom.uniq[j]]), ytop = ylim[2] + 10,
col = rgb(220,220,220, maxColorValue = 255, alpha = 120), border = NA)
}
}
axis(side = 1, at = chpos, labels = chrom.uniq, cex.axis = cex.xaxis, padj = padj.xaxis)
#Draw plot
par(new = T)
plot(mymap, y, xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, xlab = '', ylab = '', pch = pch, cex = cex, col = col, ...)
axis(side = 2, cex.axis = cex.yaxis)
#The highlighted SNPs
points(mymap[highlSNP], y[highlSNP], ylim = ylim, xlim = xlim,
col = col.highl, pch = pch.highl, cex = cex.highl)
}
else{
if(!(zoom.chr %in% chr))
stop(paste('No chromosome named', zoom.chr, 'found in the chr vector', chr))
if(is.numeric(highlSNP))
highlSNP <- 1:length(y) %in% highlSNP #Convert from indices to logical
# pch <- pch[chr == zoom.chr]
# cex <- cex[chr == zoom.chr]
# col <- col[chr == zoom.chr]
# chr <- chr[chr == zoom.chr]
y <- y[chr == zoom.chr]
pos <- pos[chr == zoom.chr]
highlSNP <- highlSNP[chr == zoom.chr]
if(is.null(zoom.pos))
zoom.pos <- c(min(pos) - 1, max(pos) + 1)
# plot(pos[pos > zoom.pos[1] & pos < zoom.pos[2]], y[pos > zoom.pos[1] & pos < zoom.pos[2]], xaxt = 'n', yaxt = 'n',
# col = col[pos > zoom.pos[1] & pos < zoom.pos[2]],
# pch = pch[pos > zoom.pos[1] & pos < zoom.pos[2]],
# cex = cex[pos > zoom.pos[1] & pos < zoom.pos[2]], xlab = '', ylab = '', ...)
plot(pos[pos > zoom.pos[1] & pos < zoom.pos[2]], y[pos > zoom.pos[1] & pos < zoom.pos[2]],
xaxt = 'n', yaxt = 'n', xlab = '', ylab = '',
pch = pch, cex = cex, ...)
axis(side = 2, cex.axis = cex.yaxis)
axis(side = 1, cex.axis = cex.xaxis)
#The highlighted SNPs
points(pos[pos > zoom.pos[1] & pos < zoom.pos[2] & highlSNP],
y[pos > zoom.pos[1] & pos < zoom.pos[2] & highlSNP],
col = col.highl, pch = pch.highl, cex = cex.highl)
}
}
#WARNING: Currently not working for multiple chromosomes. sortmap.ranges needs to be fixed to plot the ranges in the right places
#I believe I've fixed this
plot.manhattan_highlRegions <- function(pos, chr, y, log10 = T, cex.yaxis = 1.5, cex.xaxis = 2,
ylim = NULL, zoom.chr = NULL, zoom.pos = NULL, col = NULL,
xlab = '', ylab = '', ranges, ranges.col = 'black', shadeRanges = F, ...){
#Partially recycled from GenABELs plot function
#Different design than plot.manhattan
#pos = vector with genomic positions
#chr = vector with chromosome info matching pos
#y = vector OR a list with vectors. If a list, the results will be overlayed with different colors
#Input has to be sorted
require(wesanderson)
stopifnot(class(ranges) == 'GRanges')
#Make continuous positions for plotting
#The SNPs
newmap <- sortmap(chr, pos)
mymap <- newmap$cummap
#The ranges
newmap.ranges <- sortmap.ranges(ranges, newmap) #Fixed I believe
if(log10){
if(class(y) == 'list')
y <- lapply(y, function(x){-log10(x)})
else
y <- -log10(y)
}
#Multiple results to be overlaid?
if(class(y) == 'list'){
if(is.null(col))
col = wes_palette(name = 'Darjeeling1', n = length(y))
y.list <- y[2:length(y)]
y <- y[[1]]
}
else if(is.null(col))
col = wes_palette(name = 'Darjeeling1', n = 1)
if(is.null(zoom.chr)){
if(is.null(ylim))
ylim <- c(-2, max(y, na.rm = T))
xlim = c(min(mymap), max(mymap))
#Setup plot with highlighted chrs
plot(mymap, y, xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, xlab = '', ylab = '', type = 'n')
chrom.uniq <- unique(chr)
chpos <- c()
for (j in 1:length(chrom.uniq)){
chpos[j] <- mean(mymap[chr == chrom.uniq[j]])
#Draw rectangle for "even" chrs
if(j %% 2 == 0){
rect(xleft = min(mymap[chr == chrom.uniq[j]]), ybottom = ylim[1] - 10, xright = max(mymap[chr == chrom.uniq[j]]), ytop = ylim[2] + 10,
col = rgb(220,220,220, maxColorValue = 255, alpha = 120), border = NA)
}
}
axis(side = 1, at = chpos, labels = chrom.uniq, cex.axis = cex.xaxis)
#Draw plot
par(new = T)
plot(mymap, y, xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, col = col[1], pch = 19, cex = .5, xlab = xlab, ylab = ylab, ...)
axis(side = 2, cex.axis = cex.yaxis)
#Overlay more things?
if(exists('y.list')){
for(j in 1:length(y.list)){
points(mymap, y.list[[j]], xaxt = 'n', yaxt = 'n', ylim = ylim, xlim = xlim, col = col[j+1], pch = 19, cex = .5, xlab = '', ylab = '', ...)
}
}
#Draw the ranges
rect(xleft = newmap.ranges$cummap.from, xright = newmap.ranges$cummap.to, ybottom = -2, ytop = -1, col = ranges.col)
if(shadeRanges)
rect(xleft = newmap.ranges$cummap.from, xright = newmap.ranges$cummap.to, ybottom = ylim[1] - 10, ytop = ylim[2] + 10,
col = rgb(220,220,220, maxColorValue = 255, alpha = 120), border = rgb(220,220,220, maxColorValue = 255, alpha = 120))
}
else{
if(!(zoom.chr %in% chr))
stop(paste('No chromosome named', zoom.chr, 'found in the chr vector'))
y <- y[chr == zoom.chr]
pos <- pos[chr == zoom.chr]
if(is.null(ylim))
ylim <- c(-2, max(y, na.rm = T))
if(is.null(zoom.pos))
zoom.pos <- c(min(pos) - 1, max(pos) + 1)
#Draw plot
plot(pos[pos > zoom.pos[1] & pos < zoom.pos[2]],
y[pos > zoom.pos[1] & pos < zoom.pos[2]],
xaxt = 'n', yaxt = 'n', col = col[1], pch = 19, cex = .5, xlab = '', ylab = '', ylim = ylim, ...)
#Overlay more things?
if(exists('y.list')){
for(j in 1:length(y.list)){
y.overlay <- y.list[[j]][chr == zoom.chr]
points(pos[pos > zoom.pos[1] & pos < zoom.pos[2]],
y.overlay[pos > zoom.pos[1] & pos < zoom.pos[2]],
xaxt = 'n', yaxt = 'n', ylim = ylim, col = col[j+1], pch = 19, cex = .5, xlab = '', ylab = '', ...)
}
}
axis(side = 2, cex.axis = cex.yaxis)
axis(side = 1, cex.axis = cex.xaxis)
#Draw the ranges
from <- start(ranges)
to <- end(ranges)
ranges.chr <- as.character(decode(ranges@seqnames))
if(!(zoom.chr %in% ranges.chr))
warning(paste('No chromosome named', zoom.chr, 'found in the ranges object'))
else{
rect(xleft = from[ranges.chr == zoom.chr], xright = to[ranges.chr == zoom.chr], ybottom = -2, ytop = -1, col = ranges.col)
if(shadeRanges)
rect(xleft = from[ranges.chr == zoom.chr], xright = to[ranges.chr == zoom.chr], ybottom = ylim[1] - 10, ytop = ylim[2] + 10,
col = rgb(220,220,220, maxColorValue = 255, alpha = 120), border = rgb(220,220,220, maxColorValue = 255, alpha = 120))
}
}
}
midpoints <- function(x, dp=2){
lower <- as.numeric(gsub(',.*','',gsub('\\(|\\[|\\)|\\]','', x)))
upper <- as.numeric(gsub('.*,','',gsub('\\(|\\[|\\)|\\]','', x)))
return(round(lower+(upper-lower)/2, dp))
}
plot.freqVStime <- function(freqs, freqs.names = NULL, legendPos = 'topleft', cex.legend = 1){
#A function just to plot allele freq VS timepoint in the hs selection experiment
if(is.null(freqs.names)){
freqs.names <- colnames(freqs)
}
freq.generation <- numeric(18)
freq.treatment <- numeric(18)
freq.generation[grep(pattern = 'G1_', freqs.names)] <- 1
freq.generation[grep(pattern = 'G11_', freqs.names)] <- 2
freq.generation[grep(pattern = 'G25_', freqs.names)] <- 3
freq.treatment[grep(pattern = 'N1|N2|N3', freqs.names)] <- 'control'
freq.treatment[grep(pattern = 'N4|N5|N6', freqs.names)] <- 'hs'
pal <- wes_palette(name = 'BottleRocket2', 2)
col <- character(18)
col[freq.treatment == 'hs'] <- pal[1]
col[freq.treatment == 'control'] <- pal[2]
plot(freq.generation, unlist(freqs), pch = 19, col = col, xlab = '', ylab = '', xaxt = 'n', yaxt = 'n', cex = 2)
axis(side = 1, at = 1:3, labels = c('G1', 'G11', 'G25'), cex.axis = 2)
axis(side = 2, cex.axis = 1.5)
mtext(text = 'Allele frequency', side = 2, cex = 2, line = 2.5)
# abline(a = case1.scan$Intercept[nr], b = case1.scan$generation[nr], col = pal[2], lty = 2, lwd = 2)
# abline(a = case1.scan$Intercept[nr] + case1.scan$treatment[nr], b = case1.scan$generation[nr] + case1.scan$`generation:treatment`[nr], col = pal[1], lty = 2, lwd = 2)
legend(legendPos, c('HS', "control"), col = pal, pch = 19, cex = cex.legend)
}
plot.freqVStime_v2 <- function(freqs, freqs.names = NULL, legendPos = 'topleft', cex.points = 3,
cex.legend = 1, cex.lab = 2.5, cex.axis = 1.5, pal = wes_palette(name = 'BottleRocket2', 2),
lines = F, lwd = 1, legend = T, ...){
#A function just to plot allele freq VS timepoint in the hs selection experiment
require(wesanderson)
if(is.null(freqs.names)){
freqs.names <- colnames(freqs)
}
freq.generation <- numeric()
freq.treatment <- numeric()
freq.generation[grep(pattern = 'G1_', freqs.names)] <- 1
freq.generation[grep(pattern = 'G11_', freqs.names)] <- 2
freq.generation[grep(pattern = 'G25_', freqs.names)] <- 3
freq.generation[grep(pattern = 'G100_', freqs.names)] <- 4
freq.treatment[grep(pattern = 'N1|N2|N3', freqs.names)] <- 'control'
freq.treatment[grep(pattern = 'N4|N5|N6', freqs.names)] <- 'hs'
col <- character()
col[freq.treatment == 'hs'] <- pal[1]
col[freq.treatment == 'control'] <- pal[2]
par(mar = c(5,5,4,2) + .1)
plot(freq.generation, unlist(freqs), pch = 19, col = col, xlab = '', ylab = '', xaxt = 'n', yaxt = 'n', cex = cex.points, ...)
axis(side = 1, at = 1:4, labels = c('1', '11', '25', '100'), cex.axis = cex.axis)
mtext(text = 'Generation', side = 1, cex = cex.lab, line = 3)
axis(side = 2, cex.axis = cex.axis)
mtext(text = 'Allele frequency', side = 2, cex = cex.lab, line = 3)
# abline(a = case1.scan$Intercept[nr], b = case1.scan$generation[nr], col = pal[2], lty = 2, lwd = 2)
# abline(a = case1.scan$Intercept[nr] + case1.scan$treatment[nr], b = case1.scan$generation[nr] + case1.scan$`generation:treatment`[nr], col = pal[1], lty = 2, lwd = 2)
if(legend)
legend(legendPos, c('High Sugar', "Control"), col = pal, pch = 19, cex = cex.legend)
if(lines){
pops <- paste0('N', 1:6)
for (i in 1:6) {
idx <- grep(pops[i], freqs.names)
pop.freqs <- freqs[, idx, with = F]
pop.gen <- freq.generation[idx]
pop.col <- unique(col[idx])
stopifnot(length(pop.col) == 1)
lines(x = pop.gen[order(pop.gen)], y = pop.freqs[, order(pop.gen), with = F], col = pop.col, lwd = lwd)
}
}
}
plot.contBoxes <- function(x, y, col = NULL, spacer1 = .01, spacer2 = 3, xlab = 'Simulated h2', ylab = 'Estimated h2', legend.text = NULL, legend.title = NULL, legend.pos = 'topleft', legend.cex = 1, ...){
if(is.null(col)){
library(wesanderson)
cols <- wes_palette('Darjeeling2', ncol(y))
}
#Set up grouping variable
tmp <- seq(from = 0, length.out = ncol(y), by = spacer1)
tmp <- tmp - mean(tmp)
grouping <- rep(x, ncol(y)) + rep(tmp, each = length(x))
#Set up between "group" spacing
box <- boxplot(unlist(y) ~ grouping, xaxt = 'n', yaxt = 'n', col = cols, plot = F)
tmp <- rep(seq(0, length.out = length(unique(x)), by = 2), each = ncol(y))
xPos <- rep(1:ncol(y), length(unique(x))) + tmp*spacer2
#pos for x-axis
start <- mean(xPos[1:ncol(y)])
end <- mean(xPos[(length(xPos) - ncol(y) + 1):length(xPos)])
xlab.pos <- seq(start, end, length.out = length(unique(x)))
#plot
boxplot(unlist(y) ~ grouping, xaxt = 'n', at = xPos, yaxt = 'n', col = cols, ...)
grid(10,10)
par(new = T)
boxplot(unlist(y) ~ grouping, xaxt = 'n', at = xPos, yaxt = 'n', col = cols, ...)
axis(side = 1, at = xlab.pos, labels = unique(x), las = 2, cex.axis = 2)
if(par('usr')[4] < max(x))
axis(side = 2, cex.axis = 2)
else
axis(side = 2, at = unique(x), labels = unique(x), cex.axis = 2)
mtext(text = xlab, side = 1, line = 4, cex = 2, font = 2)
mtext(text = ylab, side = 2, line = 2.5, cex = 2, font = 2)
# lines(x = c(0,25.5), y = c(0, .9), lty = 2, lwd = 3)
if(!is.null(legend.text))
legend(legend.pos, legend.text, col = cols, title = legend.title, pch = 15, cex = legend.cex)
}
# sim_pop <- function(N = 200, M = 1000, Fst = 0.1, maf_max = 0.5, maf_min = 0.05, seed = 1){ #Stolen from https://variani.github.io/bigcov/vignettes/popstrat.html
# set.seed(seed)
# maf_values <- runif(M, maf_min, maf_max)
#
# freq1 <- sapply(1:M, function(i) rbeta(1,
# maf_values[i] * (1 - Fst) / Fst,
# (1 - maf_values[i]) * (1 - Fst) / Fst))
# freq2 <- sapply(1:M, function(i) rbeta(1,
# maf_values[i] * (1 - Fst) / Fst,
# (1 - maf_values[i]) * (1 - Fst) / Fst))
#
# gdat1 <- sapply(1:M, function(i) sample(c(0, 1, 2), N, replace = TRUE,
# prob = c(((1 - freq1[i])^2), (2 * freq1[i] * (1 - freq1[i])), (freq1[i]^2))))
# gdat2 <- sapply(1:M, function(i) sample(c(0, 1, 2), N, replace = TRUE,
# prob = c(((1 - freq2[i])^2), (2 * freq2[i] * (1 - freq2[i])), (freq2[i]^2))))
#
# gdat <- rbind(gdat1, gdat2)
# return(gdat)
# }
sim_pop <- function(N = 200, M = 1000, Fst = 0.1, maf_max = 0.5, maf_min = 0.05, seed = 1, nrPops = 2){ #Stolen from https://variani.github.io/bigcov/vignettes/popstrat.html
#My generalized version
set.seed(seed)
maf_values <- runif(M, maf_min, maf_max)
gdat <- matrix(ncol = M, nrow = N*nrPops)
k <- 1
for(j in 1:nrPops){
freq.pop <- sapply(1:M, function(i) rbeta(1,
maf_values[i] * (1 - Fst) / Fst,
(1 - maf_values[i]) * (1 - Fst) / Fst))
gdat.pop <- sapply(1:M, function(i) sample(c(0, 1, 2), N, replace = TRUE,
prob = c(((1 - freq.pop[i])^2), (2 * freq.pop[i] * (1 - freq.pop[i])), (freq.pop[i]^2))))
gdat[k:(k+N-1), ] <- gdat.pop
k <- k + N
}
return(gdat)
}
boxplot.snp.twoWay <- function(marker1.geno, marker2.geno, y, legend = F, names = c('SNP1', 'SNP2'), ...){
box <- boxplot(y ~ as.matrix(marker1.geno)*as.matrix(marker2.geno),
col = 'lightblue', las = 1, frame=F, cex.axis = .8, xaxt = "n", ...)
grid()
par(new = T)
boxplot(y ~ as.matrix(marker1.geno)*as.matrix(marker2.geno), col = 'lightblue', las = 1, frame=F, cex.axis = .8, xaxt = "n", ...)
axis(1, at=1:length(box$n), labels=paste("n =", box$n), line=2, lty=0, cex.axis = .8)
labels.marker1 <- gsub(pattern = "(.*)\\..*", replacement = "\\1", x = box$names)
labels.marker2 <- gsub(pattern = ".*\\.(.*)", replacement = "\\1", x = box$names)
labels.marker2.nrPerClass <- table(labels.marker2)[1]
tmp <- 1 + (labels.marker2.nrPerClass - 1)/2
tmp2 <- sapply(X = 2:length(unique(labels.marker2)) - 1, FUN = function(x){tmp + x*labels.marker2.nrPerClass })
labels.marker2.at <- c(tmp, tmp2)
axis(1, at = 1:length(labels.marker1), labels = labels.marker1, cex.axis = .8)
labels.marker2.table <- table(labels.marker2)
j <- labels.marker2.table[1]
for(i in 2:length(labels.marker2.table)){
abline(v = j + .5)
j <- j + labels.marker2.table[1]
}
if(legend){
marker1.uniqGeno <- paste(unique(marker1.geno), collapse=" ")
marker2.uniqGeno <- paste(unique(marker2.geno), collapse=" ")
legend("topleft", c(paste(names[2], ":", marker2.uniqGeno, collapse=""),
paste(names[1], ":", marker1.uniqGeno, collapse="")),
cex=.8, text.col = c("red", "black"))
axis(3, at = labels.marker2.at, labels=unique(labels.marker2), cex.axis = .8, col.axis = "red")
}
else{
axis(3, at = labels.marker2.at, labels=unique(labels.marker2), cex.axis = .8)
}
}
fst <- function(p1, p2){ #Simplistic Fst calculation from two allele freqs. From Noah
pbar=(p1+p2)/2
num=((p1**2 + p2**2)/2 - pbar**2)
den=(pbar*(1-pbar))
fst=num/den
return(fst)
}
LDheatmap.hacked <- function (gdat, genetic.distances = NULL, distances = "physical",
LDmeasure = "r", title = "Pairwise LD", add.map = TRUE, add.key = TRUE,
geneMapLocation = 0.15, geneMapLabelX = NULL, geneMapLabelY = NULL,
SNP.name = NULL, color = NULL, newpage = TRUE, name = "ldheatmap",
vp.name = NULL, pop = FALSE, flip = NULL, text = FALSE)
{
requireNamespace("grid")
if (is.null(color)) {
if (inherits(gdat, "LDheatmap"))
color <- gdat$color
else color <- grey.colors(20)
}
if (is.null(flip)) {
if (inherits(gdat, "LDheatmap") && !is.null(gdat$flipVP))
flip <- TRUE
else flip <- FALSE
}
if (is.null(genetic.distances)) {
if (inherits(gdat, "data.frame"))
genetic.distances = 1000 * (1:ncol(gdat))
else if (inherits(gdat, "matrix"))
genetic.distances = 1000 * (1:length(gdat[1, ]))
else genetic.distances = gdat$genetic.distances
}
if (inherits(gdat, "SnpMatrix")) {
if (!is.vector(genetic.distances)) {
stop("Distance should be in the form of a vector")
}
o <- order(genetic.distances)
genetic.distances <- genetic.distances[o]
gdat <- gdat[, o]
if (LDmeasure == "r")
LDmatrix <- snpStats::ld(gdat, depth = ncol(gdat) -
1, stats = "R.squared")
else if (LDmeasure == "D")
LDmatrix <- snpStats::ld(gdat, depth = ncol(gdat) -
1, stats = "D.prime")
else stop("Invalid LD measurement, choose r or D'.")
LDmatrix <- as.matrix(LDmatrix)
LDmatrix[lower.tri(LDmatrix, diag = TRUE)] <- NA
}
else if (inherits(gdat, "data.frame")) {
for (i in 1:ncol(gdat)) {
if (!genetics::is.genotype(gdat[, i]))
stop("column ", i, " is not a genotype object\n")
}
gvars <- unlist(sapply(gdat, function(x) genetics::nallele(x) ==
2))
genetic.distances <- genetic.distances[gvars]
gdat <- gdat[gvars]
if (!is.vector(genetic.distances)) {
stop("Distance should be in the form of a vector")
}
o <- order(genetic.distances)
genetic.distances <- genetic.distances[o]
gdat <- gdat[, o]
myLD <- genetics::LD(gdat)
if (LDmeasure == "r")
LDmatrix <- myLD[[LDmeasure]]^2
else if (LDmeasure == "D'")
LDmatrix <- abs(myLD[[LDmeasure]])
else stop("Invalid LD measurement, choose r or D'.")
}
else if (inherits(gdat, "LDheatmap")) {
LDmatrix <- gdat$LDmatrix
distances <- gdat$distances
}
else if (inherits(gdat, "matrix")) {
if (nrow(gdat) != ncol(gdat))
stop("The matrix of linkage disequilibrium measurements must be a square matrix")
LDmatrix <- gdat
LDmatrix[lower.tri(LDmatrix, diag = TRUE)] <- NA
}
else if (!missing(gdat))
stop(paste("No method for an object of class", class(gdat)))
else stop("Need to supply LD matrix or genotypes")
heatmapVP <- viewport(width = unit(0.8, "snpc"), height = unit(0.8,
"snpc"), name = vp.name)
flipVP <- viewport(width = unit(0.8, "snpc"), height = unit(0.8,
"snpc"), y = 0.6, angle = -45, name = "flipVP")
if (color[1] == "blueToRed")
color = rainbow(20, start = 4/6, end = 0, s = 0.7)[20:1]
if (newpage)
grid.newpage()
mybreak <- 0:length(color)/length(color)
imgLDmatrix <- LDmatrix
byrow <- ifelse(flip, FALSE, TRUE)
colcut <- as.character(cut(1 - imgLDmatrix, mybreak, labels = as.character(color),
include.lowest = TRUE))
if (is.numeric(color))
colcut <- as.integer(colcut)
ImageRect <- LDheatmap:::makeImageRect(dim(LDmatrix)[1], dim(LDmatrix)[2],
colcut, name = "heatmap", byrow)
ImageText <- NULL
if (text)
ImageText <- makeImageText(dim(LDmatrix)[1], dim(LDmatrix)[2],
round(imgLDmatrix, digits = 2), name = "heatmaptext")
title <- textGrob(title, 0.5, 1.05, gp = gpar(cex = 1), name = "title")
if (flip) {
ImageRect <- editGrob(ImageRect, vp = flipVP)
if (text) {
ImageText <- makeImageText(dim(LDmatrix)[1], dim(LDmatrix)[2],
round(imgLDmatrix, digits = 2), name = "heatmaptext",
flip = TRUE)
textVal <- ImageText
ImageText <- editGrob(ImageText, vp = flipVP, rot = 0,
just = c("right", "top"))
}
}
heatMap <- gTree(children = gList(ImageRect, ImageText, title),
name = "heatMap")
nsnps <- ncol(LDmatrix)
step <- 1/(nsnps - 1)
# ind <- match(SNP.name, row.names(LDmatrix), nomatch = 0)
ind <- 1:nrow(LDmatrix)
geneMapVP <- NULL
if (flip)
geneMapVP <- flipVP
geneMap <- LDheatmap:::LDheatmapMapNew.add(nsnps, genetic.distances = genetic.distances,
geneMapLocation = geneMapLocation, add.map, geneMapLabelX = geneMapLabelX,
geneMapLabelY = geneMapLabelY, distances = distances,
vp = geneMapVP, SNP.name = SNP.name, ind = ind, flip = flip)
if (add.key)
Key <- LDheatmap:::LDheatmapLegend.add(color, LDmeasure, heatmapVP)
else Key <- NULL
LDheatmapGrob <- gTree(children = gList(heatMap, geneMap,
Key), vp = heatmapVP, name = name, cl = "ldheatmap")
grid.draw(LDheatmapGrob)
if (pop) {
downViewport(heatmapVP$name)
popViewport()
}
ldheatmap <- list(LDmatrix = LDmatrix, LDheatmapGrob = LDheatmapGrob,
heatmapVP = heatmapVP, flipVP = geneMapVP, genetic.distances = genetic.distances,
distances = distances, color = color)
class(ldheatmap) <- "LDheatmap"
invisible(ldheatmap)
}
matrixPairsWhere <- function(x, expr, rNames = NULL, cNames = NULL, symmetric = F, ignoreDiag = T){
#The function retrieve elements in x that fulfills expr and returns 'long' format like:
#rowname colname value
# . . .
# . . .
#Useful for instance when examining cor and distance matrices
# x = A matrix
# expr = An expression given as a string. Use x to refer to the matrix. Example: 'x < .5' or 'x > 0 & x < 2'
if(is.null(rNames))
rNames <- rownames(x)
if(is.null(cNames))
cNames <- colnames(x)
if(symmetric){ #Intended for cor/distance matrices etc that are symmetric
stopifnot(nrow(x) == ncol(x))
x[lower.tri(x)] <- NA
if(ignoreDiag)
diag(x) <- NA #Skip diagonal. Self identity in the case of cor matrix
}
pairs <- which(eval(parse(text = expr))) #Evaluates the expression
#Convert every idx to a pair of col/row idx
row.idx <- pairs %% nrow(x)
row.idx[row.idx == 0] <- nrow(x) #Elements on the last row
col.idx <- pairs/nrow(x)
col.idx[col.idx %% 1 != 0] <- floor(col.idx[col.idx %% 1 != 0] + 1) #Ignore elements on the last row
#Wrap up
result <- data.frame(rowname = rNames[row.idx], colname = cNames[col.idx], val = x[pairs], stringsAsFactors = F)
# if(rmDupl){ #Useful to remove duplicates when x is symmetric
# names1 <- paste(result$rowname, result$colname, sep = '_')
# names2 <- paste(result$colname, result$rowname, sep = '_')
# dupl <- duplicated(names1)
# duplFlip <- duplicated(data.frame(names1, names2))
#
# if(rmSelfPair)
# return()
# else
# return(result[!(dupl | duplFlip)])
# }
# else
return(result)
}
matrix2pairs <- function (m, samples) {
#Extract pairwise values from a matrix and return in long format:
#rowname colname value
# . . .
# . . .
#Stolen from extract.val in the phylip package
i <- cbind(match(samples[, 1], rownames(m)), match(samples[, 2], colnames(m)))
m[i]
}
ll <- function(sort = F, units = 'auto'){
if(sort)
sort(sapply(ls(envir=.GlobalEnv), function(x){format(object.size(get(x)), units = units)}), decreasing = T)
else
sapply(ls(envir=.GlobalEnv), function(x){format(object.size(get(x)), units = units)})
}
plot.wgcna_softThreshold <- function(sft, cex.text = 0.9){
#Code recycled from the WGCNA tutorials
par(mfrow = c(1,2))
# Scale-free topology fit index as a function of the soft-thresholding power
plot(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
xlab="Soft Threshold (power)", ylab="Scale Free Topology Model Fit,signed R^2", type="n",
main = "Scale independence")
text(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
labels=sft$fitIndices$Power, cex=cex.text, col="red");
# this line corresponds to using an R^2 cut-off of h
abline(h=0.80,col="red")
# Mean connectivity as a function of the soft-thresholding power
plot(sft$fitIndices[,1], sft$fitIndices[,5],
xlab="Soft Threshold (power)",ylab="Mean Connectivity", type="n",
main = paste("Mean connectivity"))
text(sft$fitIndices[,1], sft$fitIndices[,5], labels=sft$fitIndices$Power, cex=cex.text,col="red")
}
add.alpha <- function(col, alpha=1){
#Add alpha (transparency) to a color vector
#col = vector with colors (in a form accepted by col2rgb)
#alpha = alpha value between 0 & 1. Can be one number or a vector matching col
if(missing(col))
stop("Please provide a vector of colors")
col.rgb <- rbind(sapply(col, col2rgb)/255, alpha)
apply(col.rgb, 2,
function(x)
rgb(x[1], x[2], x[3], alpha=x[4]))
}
plotGenoContTable <- function(geno, panelText = "frac", ...){
if(ncol(geno) != 2)
stop("geno should be a genotype matrix with two columns/markers")
if(!require(lattice))
stop("Lattice package not found")
if(!panelText %in% c("frac", "counts"))
stop("panelText needs to be \"frac\" or \"counts\"")
if(class(geno) == "matrix")
geno <- as.data.frame(geno)
geno.table <- table(geno)
#Expected table
geno.table_margin1 <- margin.table(geno.table, margin = 1)
geno.table_margin2 <- margin.table(geno.table, margin = 2)
geno.table_exp <- geno.table_margin1 %*% t(geno.table_margin2) / sum(geno.table)
if(panelText == "counts"){
myPanel <- function(x, y, z, ...) { #stolen and modified from stack overflow: http://stackoverflow.com/questions/22827677/entering-cell-values-from-a-matrix-into-a-levelplot-made-in-lattice-in-r
panel.levelplot(x,y,z,...)
#Write the observed counts
panel.text(as.numeric(x) - .1, as.numeric(y) + .1, "obs:")
panel.text(as.numeric(x) + .1, as.numeric(y) + .1, t(geno.table)[cbind(x,y)])
#Write the expected counts
panel.text(as.numeric(x) - .1, as.numeric(y) - .1, "exp:")
panel.text(as.numeric(x) + .1, as.numeric(y) - .1, round(t(geno.table_exp))[cbind(x,y)])
}
}
else{
myPanel <- function(x, y, z, ...) { #stolen and modified from stack overflow: http://stackoverflow.com/questions/22827677/entering-cell-values-from-a-matrix-into-a-levelplot-made-in-lattice-in-r
panel.levelplot(x,y,z,...)
panel.text(x, y, format(t(geno.table)/t(geno.table_exp), digits = 2)[cbind(x,y)]) ## use handy matrix indexing
}
}
print(levelplot(t(geno.table), panel = myPanel, ...))
}
#Use to color stuff by a numeric variable
num2color <- function(x, cols = NULL){
if(is.null(cols)){
library(RColorBrewer)
cols <- brewer.pal(4, "Blues")
}
# Define colour palette
pal <- colorRampPalette(cols)
# Rank variable for colour assignment
order <- findInterval(x, sort(x))
# Create colors
pal(length(x))[order]
}
#Mats function to plot a gene based on a GRanges object
plot_gtf <- function(gtf_gr, outfile = NULL, display_id = F, x_lim = NULL, y_lim = NULL){
if(!is.null(outfile)){
pdf(file = outfile, width = 8, height = 4)
}
level_count <- max(rowSums(table(gtf_gr$gene_id, gtf_gr$transcript_id) > 0)) + 2
if(is.null(x_lim)) x_lim <- c(min(start(gtf_gr)), max(end(gtf_gr)))
if(is.null(y_lim)) y_lim <- c(0, level_count)
plot(x = mean(start(gtf_gr)), y = level_count /2, xlim = x_lim, ylim = y_lim, type = "n", main = paste("Chr", unique(gtf_gr@seqnames)), xlab = "Position", ylab = "")
genes <- unique(gtf_gr$gene_id)
for (gene in genes){
current_lvl <- level_count
gene_entry <- gtf_gr$type == "gene" & gtf_gr$gene_id == gene
text(x = mean(c(start(gtf_gr)[gene_entry], end(gtf_gr)[gene_entry])), y = current_lvl - 0.6, labels = gene, cex = 0.8)
current_lvl <- current_lvl - 1
segments(x0 = start(gtf_gr)[gene_entry], x1 = end(gtf_gr)[gene_entry], y0 = current_lvl, col = "firebrick", lwd = 5)
transcript_entries <- which(gtf_gr$type == "transcript" & gtf_gr$gene_id == gene)
current_lvl <- current_lvl - 1
for(transcript in transcript_entries){
transcript_id <- gtf_gr$transcript_id[transcript]
if(display_id){
text(x = start(gtf_gr)[transcript], y = current_lvl + 0.4, labels = transcript_id, cex = 0.8, pos = 4)
}
segments(x0 = start(gtf_gr)[transcript], x1 = end(gtf_gr)[transcript], y0 = current_lvl, col = "black", lwd = 2)
exon_entries <- gtf_gr$type == "CDS" & gtf_gr$transcript_id == transcript_id
rect(xleft = start(gtf_gr)[exon_entries], xright = end(gtf_gr)[exon_entries ], ybottom = current_lvl - 0.2, ytop = current_lvl + 0.2, col = "black")
if(any(gtf_gr$type == "five_prime_utr" & gtf_gr$transcript_id == transcript_id)){
utr5_entries <- gtf_gr$type == "five_prime_utr" & gtf_gr$transcript_id == transcript_id
rect(xleft = start(gtf_gr)[utr5_entries], xright = end(gtf_gr)[utr5_entries], ybottom = current_lvl - 0.2, ytop = current_lvl + 0.2, col = "firebrick1")
}
if(any(gtf_gr$type == "three_prime_utr" & gtf_gr$transcript_id == transcript_id)){
utr3_entries <- gtf_gr$type == "three_prime_utr" & gtf_gr$transcript_id == transcript_id
rect(xleft = start(gtf_gr)[utr3_entries], xright = end(gtf_gr)[utr3_entries], ybottom = current_lvl - 0.2, ytop = current_lvl + 0.2, col = "firebrick4")
}
current_lvl <- current_lvl - 1
}
}
if(!is.null(outfile)){
dev.off()
}
}
#Mats function
plot_cnv_coverage <- function(cnv_list, pdf_file = "~/Projects/Herring/doc/cnv_v2.0.2/cnv_coverage.pdf", size_df = Ch_v2.0.2_sizes){
del_GR_list <- cnv_list$del
dup_GR_list <- cnv_list$dup
del_cov_v2.0.2 <- coverage(unlist(del_GR_list))
del_cov_v2.0.2_df <- as.data.frame(ranges(del_cov_v2.0.2))
del_cov_v2.0.2_df[,"cov"] <- unlist(runValue(del_cov_v2.0.2))
del_cov_v2.0.2_df[,"global_start"] <- del_cov_v2.0.2_df[,"start"] + size_df[match(del_cov_v2.0.2_df[, "group_name"], size_df[,"name"]), "offset"]
del_cov_v2.0.2_df[,"global_end"] <- del_cov_v2.0.2_df[,"end"] + size_df[match(del_cov_v2.0.2_df[, "group_name"], size_df[,"name"]), "offset"]
dup_cov_v2.0.2 <- coverage(unlist(dup_GR_list))
dup_cov_v2.0.2_df <- as.data.frame(ranges(dup_cov_v2.0.2))
dup_cov_v2.0.2_df[,"cov"] <- unlist(runValue(dup_cov_v2.0.2))
dup_cov_v2.0.2_df[,"global_start"] <- dup_cov_v2.0.2_df[,"start"] + size_df[match(dup_cov_v2.0.2_df[, "group_name"], size_df[,"name"]), "offset"]
dup_cov_v2.0.2_df[,"global_end"] <- dup_cov_v2.0.2_df[,"end"] + size_df[match(dup_cov_v2.0.2_df[, "group_name"], size_df[,"name"]), "offset"]