-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html
1603 lines (1558 loc) · 248 KB
/
snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>3 SNP-level parent-specific read counts to allele-specific transcription | Allele-specific transcriptomics tutorial</title>
<meta name="description" content="3 SNP-level parent-specific read counts to allele-specific transcription | Allele-specific transcriptomics tutorial" />
<meta name="generator" content="bookdown 0.38 and GitBook 2.6.7" />
<meta property="og:title" content="3 SNP-level parent-specific read counts to allele-specific transcription | Allele-specific transcriptomics tutorial" />
<meta property="og:type" content="book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="3 SNP-level parent-specific read counts to allele-specific transcription | Allele-specific transcriptomics tutorial" />
<meta name="author" content="Sean T. Bresnahan" />
<meta name="date" content="2024-07-16" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<script src="libs/kePrint-0.0.1/kePrint.js"></script>
<link href="libs/lightable-0.0.1/lightable.css" rel="stylesheet" />
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">AST Tutorial</a></li>
<li class="divider"></li>
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> Introduction</a>
<ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#contents"><i class="fa fa-check"></i><b>1.1</b> Contents</a>
<ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#sequence-reads-to-snp-level-parent-specific-read-counts"><i class="fa fa-check"></i>Sequence reads to SNP-level parent-specific read counts</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#snp-level-parent-specific-read-counts-to-allele-specific-transcription"><i class="fa fa-check"></i>SNP-level parent-specific read counts to allele-specific transcription</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#data"><i class="fa fa-check"></i><b>1.2</b> Data</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><i class="fa fa-check"></i><b>2</b> Sequence reads to SNP-level parent-specific read counts</a>
<ul>
<li class="chapter" data-level="2.1" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#overview"><i class="fa fa-check"></i><b>2.1</b> Overview</a></li>
<li class="chapter" data-level="2.2" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#setup"><i class="fa fa-check"></i><b>2.2</b> Setup</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#command-line-tools"><i class="fa fa-check"></i><b>2.2.1</b> Command-line tools</a></li>
<li class="chapter" data-level="" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#project-directory-structure"><i class="fa fa-check"></i>Project directory structure</a></li>
<li class="chapter" data-level="2.2.2" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#amel_hav3.1-reference-genome-assembly-and-annotation-files"><i class="fa fa-check"></i><b>2.2.2</b> Amel_HAv3.1 reference genome assembly and annotation files</a></li>
<li class="chapter" data-level="2.2.3" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#a-note-on-parallel-processing-in-high-performance-computing-clusters"><i class="fa fa-check"></i><b>2.2.3</b> A note on parallel processing in high performance computing clusters</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#generation-of-f1-genomes-and-transcriptomes"><i class="fa fa-check"></i><b>2.3</b> Generation of F1 genomes and transcriptomes</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#wgs-read-pre-processing"><i class="fa fa-check"></i><b>2.3.1</b> WGS read pre-processing</a></li>
<li class="chapter" data-level="2.3.2" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#wgs-read-alignment-and-post-processing"><i class="fa fa-check"></i><b>2.3.2</b> WGS read alignment and post-processing</a></li>
<li class="chapter" data-level="2.3.3" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#variant-discovery-and-filtration"><i class="fa fa-check"></i><b>2.3.3</b> Variant discovery and filtration</a></li>
<li class="chapter" data-level="2.3.4" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#genome-and-transcriptome-construction"><i class="fa fa-check"></i><b>2.3.4</b> Genome and transcriptome construction</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#quantification-of-f2-allele-specific-transcription"><i class="fa fa-check"></i><b>2.4</b> Quantification of F2 allele-specific transcription</a>
<ul>
<li class="chapter" data-level="2.4.1" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#mrna-seq-read-pre-processing"><i class="fa fa-check"></i><b>2.4.1</b> mRNA-seq read pre-processing</a></li>
<li class="chapter" data-level="2.4.2" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#mrna-seq-read-alignment-to-f1-transcriptomes"><i class="fa fa-check"></i><b>2.4.2</b> mRNA-seq read alignment to F1 transcriptomes</a></li>
<li class="chapter" data-level="2.4.3" data-path="sequence-reads-to-snp-level-parent-specific-read-counts-1.html"><a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html#mrna-seq-alignment-post-processing-and-allele-specific-read-counting"><i class="fa fa-check"></i><b>2.4.3</b> mRNA-seq alignment post-processing and allele-specific read counting</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><i class="fa fa-check"></i><b>3</b> SNP-level parent-specific read counts to allele-specific transcription</a>
<ul>
<li class="chapter" data-level="3.1" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#overview-1"><i class="fa fa-check"></i><b>3.1</b> Overview</a></li>
<li class="chapter" data-level="3.2" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#setup-1"><i class="fa fa-check"></i><b>3.2</b> Setup</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#tutorial-files"><i class="fa fa-check"></i><b>3.2.1</b> Tutorial files</a></li>
<li class="chapter" data-level="3.2.2" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#packages"><i class="fa fa-check"></i><b>3.2.2</b> Packages</a></li>
<li class="chapter" data-level="3.2.3" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#custom-functions"><i class="fa fa-check"></i><b>3.2.3</b> Custom functions</a></li>
<li class="chapter" data-level="3.2.4" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#sample-metadata"><i class="fa fa-check"></i><b>3.2.4</b> Sample metadata</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#generate-sample-x-snpgene-count-matrix"><i class="fa fa-check"></i><b>3.3</b> Generate sample x SNP:gene count matrix</a>
<ul>
<li class="chapter" data-level="3.3.1" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#filter-snps-by-transcript"><i class="fa fa-check"></i><b>3.3.1</b> Filter SNPs by transcript</a></li>
<li class="chapter" data-level="3.3.2" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#merge-count-files-to-matrix"><i class="fa fa-check"></i><b>3.3.2</b> Merge count files to matrix</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#prepare-sample-x-snpgene-counts-for-analysis"><i class="fa fa-check"></i><b>3.4</b> Prepare sample x SNP:gene counts for analysis</a>
<ul>
<li class="chapter" data-level="3.4.1" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#normalize-by-library-size"><i class="fa fa-check"></i><b>3.4.1</b> Normalize by library size</a></li>
<li class="chapter" data-level="3.4.2" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#split-by-phenotype-and-filter-low-count-snps"><i class="fa fa-check"></i><b>3.4.2</b> Split by phenotype and filter low count SNPs</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#conduct-statistical-tests"><i class="fa fa-check"></i><b>3.5</b> Conduct statistical tests</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#snp-level-storer-kim-sk-binomial-exact-tests"><i class="fa fa-check"></i><b>3.5.1</b> SNP-level Storer-Kim (SK) binomial exact tests</a></li>
<li class="chapter" data-level="3.5.2" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#gene-level-general-linear-mixed-models-glimmix"><i class="fa fa-check"></i><b>3.5.2</b> Gene-level general linear mixed models (GLIMMIX)</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#allele-specific-transcription-analysis"><i class="fa fa-check"></i><b>3.6</b> Allele-specific transcription analysis</a>
<ul>
<li class="chapter" data-level="3.6.1" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#fdr-correction-and-threshold-tests"><i class="fa fa-check"></i><b>3.6.1</b> FDR correction and threshold tests</a></li>
<li class="chapter" data-level="3.6.2" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#chi-squared-tests"><i class="fa fa-check"></i><b>3.6.2</b> Chi-squared tests</a></li>
</ul></li>
<li class="chapter" data-level="3.7" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#data-visualization"><i class="fa fa-check"></i><b>3.7</b> Data visualization</a></li>
<li class="chapter" data-level="3.8" data-path="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#session-info"><i class="fa fa-check"></i><b>3.8</b> Session info</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Allele-specific transcriptomics tutorial</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1" class="section level1 hasAnchor" number="3">
<h1><span class="header-section-number">3</span> SNP-level parent-specific read counts to allele-specific transcription<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#snp-level-parent-specific-read-counts-to-allele-specific-transcription-1" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<div id="overview-1" class="section level2 hasAnchor" number="3.1">
<h2><span class="header-section-number">3.1</span> Overview<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#overview-1" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>This R tutorial follows <a href="sequence-reads-to-snp-level-parent-specific-read-counts-1.html">quantification of SNP-level parent-specific mRNA-seq read counts</a> and documents a statistical method for comparing allele-specific transcription between phenotypes in offspring derived from reciprocal crosses.</p>
<p>Sequencing data for this tutorial are described in <a href="https://doi.org/10.1101/2024.06.09.598129">Bresnahan <em>et al.</em>, 2024, “Intragenomic conflict underlies extreme phenotypic plasticity in queen-worker caste determination in honey bees (<em>Apis mellifera</em>)”, <em>bioRxiv</em></a>. See the <a href="index.html">tutorial README</a> for more details.</p>
<p>The honey bee gene annotations and the F1 SNPs intersecting honey bee genes generated in Section 2 are imported. SNPs are assigned to the longest transcript for each gene. SNPs intersecting <span class="math inline">\(n > 2\)</span> genes or <span class="math inline">\(n = 2\)</span> genes on the same strand are discarded, as it is impossible to assign SNP-level counts to genes with transcripts that overlap on the same strand. Additionally, SNPs within miRNA, tRNA, and repetitive pseudogenes are discarded as these are inappropriate for allele-specific analysis using short sequencing reads (for details, see: <a href="https://doi.org/10.1038/hdy.2014.18">Wang & Clark, 2014, <em>Heredity</em></a>.)</p>
<p>The F2 read counts at F1 SNPs generated in Section 2 are imported. SNPs with <span class="math inline">\(n < 1\)</span> count in any Lineage are discarded. In cases where the distance between SNPs is shorter than the average read length (and thus the read counts are exactly the same for both SNPs), one SNP is chosen at random. Finally, genes with <span class="math inline">\(n < 2\)</span> SNPs are discarded, as this method requires at least two independent observations per gene for statistical analysis.</p>
<p>To adjust for differences in sequencing depth between libraries, library size factors are estimated and the read counts are normalized using the median of ratios normalization (MRN) method from DESeq2.</p>
<p>For each SNP, a <a href="https://doi.org/10.1080/01621459.1990.10475318">Storer-Kim binomial exact test of two proportions</a> is conducted using the MRN counts to test the hypothesis that the proportion of maternal and paternal read counts are statistically different.</p>
<p>A general linear mixed-effects model with interaction terms (GLIMMIX) is fit for each gene to assess the effects of Parent, Lineage, and their interaction on the raw read counts at each SNP, using the log of the library size factors as an offset to adjust for variation in sequencing depth between libraries. A Wald test is performed to assess the statistical significance of the effects.</p>
<p>For a gene to be considered as showing parent-of-origin or lineage-of-origin effects, all SNPs are required to exhibit the same directional bias in the Storer-Kim test, and a strictly parent-of-origin or lineage-of-origin effect in the GLIMMIX. To avoid identifying genes with parent-of-origin effects influenced by lineage-of-origin effects, or lineage-of-origin genes influenced by parent-of-origin effects, genes with a significant interaction effect are considered unbiased. Additionally, the proportions of Lineage A reads in samples with a Lineage B mother and Lineage A father (proportion 1, or <em>p1</em>), and the proportion of Lineage A reads in samples with a Lineage A mother and Lineage B father (<em>p2</em>), are subjected to a threshold test following methods described in <a href="https://doi.org/10.1038/hdy.2014.18">Wang & Clark, 2014, <em>Heredity</em></a>.</p>
</div>
<div id="setup-1" class="section level2 hasAnchor" number="3.2">
<h2><span class="header-section-number">3.2</span> Setup<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#setup-1" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="tutorial-files" class="section level3 hasAnchor" number="3.2.1">
<h3><span class="header-section-number">3.2.1</span> Tutorial files<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#tutorial-files" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Download all files used in this tutorial <a href="AST-tutorial.zip">here</a> or via the command line:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb1-1" tabindex="-1"></a><span class="fu">wget</span> https://github.com/sbresnahan/AST-tutorial/blob/d99afee8a9b735b8ff927a921cf75ec2997fe322/AST-tutorial.zip</span></code></pre></div>
</div>
<div id="packages" class="section level3 hasAnchor" number="3.2.2">
<h3><span class="header-section-number">3.2.2</span> Packages<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#packages" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Several CRAN packages are required for this tutorial. Use <code>pacman</code> to install those that are not already on your machine.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb2-1" tabindex="-1"></a><span class="cf">if</span> (<span class="sc">!</span><span class="fu">require</span>(<span class="st">"pacman"</span>)) <span class="fu">install.packages</span>(<span class="st">"pacman"</span>)</span>
<span id="cb2-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb2-2" tabindex="-1"></a>pacman<span class="sc">::</span><span class="fu">p_load</span>(<span class="st">"tidyverse"</span>, <span class="st">"plyr"</span>, <span class="st">"Rfast"</span>, <span class="st">"tryCatchLog"</span>, </span>
<span id="cb2-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb2-3" tabindex="-1"></a> <span class="st">"lmerTest"</span>, <span class="st">"lme4"</span>, <span class="st">"car"</span>, <span class="st">"gridExtra"</span>, </span>
<span id="cb2-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb2-4" tabindex="-1"></a> <span class="st">"doParallel"</span>, <span class="st">"ggpubr"</span>, <span class="st">"grid"</span>, <span class="st">"tagcloud"</span>, </span>
<span id="cb2-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb2-5" tabindex="-1"></a> <span class="st">"ggprism"</span>)</span></code></pre></div>
<p>This tutorial also uses a few <a href="https://www.bioconductor.org/">Bioconductor</a> packages. Use <code>BiocManager</code> to install them.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb3-1" tabindex="-1"></a><span class="cf">if</span> (<span class="sc">!</span><span class="fu">require</span>(<span class="st">"BiocManager"</span>, <span class="at">quietly =</span> <span class="cn">TRUE</span>))</span>
<span id="cb3-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb3-2" tabindex="-1"></a> <span class="fu">install.packages</span>(<span class="st">"BiocManager"</span>)</span>
<span id="cb3-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb3-3" tabindex="-1"></a></span>
<span id="cb3-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb3-4" tabindex="-1"></a>BiocManager<span class="sc">::</span><span class="fu">install</span>(<span class="fu">c</span>(<span class="st">"DESeq2"</span>,<span class="st">"genomation"</span>,<span class="st">"GenomicFeatures"</span>))</span></code></pre></div>
<p>Load all packages.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb4-1" tabindex="-1"></a>pkgs <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"tidyverse"</span>, <span class="st">"plyr"</span>, <span class="st">"Rfast"</span>, <span class="st">"tryCatchLog"</span>, </span>
<span id="cb4-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb4-2" tabindex="-1"></a> <span class="st">"lmerTest"</span>, <span class="st">"lme4"</span>, <span class="st">"car"</span>, <span class="st">"gridExtra"</span>, </span>
<span id="cb4-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb4-3" tabindex="-1"></a> <span class="st">"doParallel"</span>, <span class="st">"ggpubr"</span>, <span class="st">"grid"</span>, <span class="st">"tagcloud"</span>, </span>
<span id="cb4-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb4-4" tabindex="-1"></a> <span class="st">"ggprism"</span>,<span class="st">"DESeq2"</span>,<span class="st">"genomation"</span>,<span class="st">"GenomicFeatures"</span>)</span>
<span id="cb4-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb4-5" tabindex="-1"></a><span class="fu">invisible</span>(<span class="fu">lapply</span>(pkgs, <span class="cf">function</span>(x) <span class="fu">library</span>(x, <span class="at">character.only=</span><span class="cn">TRUE</span>)))</span></code></pre></div>
</div>
<div id="custom-functions" class="section level3 hasAnchor" number="3.2.3">
<h3><span class="header-section-number">3.2.3</span> Custom functions<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#custom-functions" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>For efficiency, most of the code detailed throughout this tutorial has been compiled as custom functions, which are included in the <a href="https://github.com/sbresnahan/AST-tutorial">project repository</a> as <a href="AST-tutorial/AST_functions.R">AST-tutorial/AST_functions.R</a>. These can be loaded into R during setup:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb5-1" tabindex="-1"></a><span class="fu">source</span>(<span class="st">"AST-tutorial/AST_functions.R"</span>)</span></code></pre></div>
<p>In this tutorial, each function will first be written explicitly to document how it works before demonstrating its use.</p>
</div>
<div id="sample-metadata" class="section level3 hasAnchor" number="3.2.4">
<h3><span class="header-section-number">3.2.4</span> Sample metadata<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#sample-metadata" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Many of the custom functions described above reference the sample metadata, which must follow this specific format:</p>
<table class="table" style="margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
sample.id
</th>
<th style="text-align:left;">
parent
</th>
<th style="text-align:left;">
block
</th>
<th style="text-align:left;">
phenotype
</th>
<th style="text-align:left;">
individual
</th>
<th style="text-align:left;">
lineage
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
PARENT_FILE
</td>
<td style="text-align:left;">
M or F
</td>
<td style="text-align:left;">
integer
</td>
<td style="text-align:left;">
character
</td>
<td style="text-align:left;">
FILE
</td>
<td style="text-align:left;">
A or B
</td>
</tr>
</tbody>
</table>
<p><em>Note 1: For <code>sample.id</code>, <code>PARENT_FILE</code> corresponds to the file name suffix assigned to the SNP-level read count files generated in Section 2, with <code>PARENT</code> being the respective F1 genome to which the F2 mRNA-seq read <code>FILE</code> was aligned.</em></p>
<p><em>Note 2: <code>lineage</code> is synonymous with “maternal lineage” or “Cross” (as in Section 2). So, e.g., “lineage A” is synonymous with A-lineage mother x B-lineage father.</em></p>
<p>Sample metadata for the sequencing data described in <a href="https://doi.org/10.1101/2024.06.09.598129">Bresnahan <em>et al.</em>, 2024, <em>bioRxiv</em></a> used in this tutorial has been included in the <a href="https://github.com/sbresnahan/AST-tutorial">project repository</a> as <a href="AST-tutorial/metadata.csv">AST-tutorial/metadata.csv</a>:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb6-1" tabindex="-1"></a>metadata <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"AST-tutorial/metadata.csv"</span>)</span></code></pre></div>
</div>
</div>
<div id="generate-sample-x-snpgene-count-matrix" class="section level2 hasAnchor" number="3.3">
<h2><span class="header-section-number">3.3</span> Generate sample x SNP:gene count matrix<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#generate-sample-x-snpgene-count-matrix" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="filter-snps-by-transcript" class="section level3 hasAnchor" number="3.3.1">
<h3><span class="header-section-number">3.3.1</span> Filter SNPs by transcript<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#filter-snps-by-transcript" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The honey bee gene annotations saved as <code>$AST-tutorial/{DIR_INDEX}/Amel_HAv3.1_genes.gff3</code> and the F1 SNPs intersecting honey bee genes saved as <code>$AST-tutorial/{DIR_ANALYSIS}/SNPs_for_Analysis_Sorted.bed</code> generated in Section 2 are imported. SNPs are assigned to the longest transcript for each gene, and SNPs within miRNA, tRNA, and repetitive pseudogenes and are discarded. A list of these genes has been included in the <a href="https://github.com/sbresnahan/AST-tutorial">project repository</a> as <a href="AST-tutorial/genelist_filter.csv">AST-tutorial/genelist_filter.csv</a>.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb7-1" tabindex="-1"></a><span class="co"># Load in filter list</span></span>
<span id="cb7-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb7-2" tabindex="-1"></a>filterlist <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"AST-tutorial/genelist_filter.csv"</span>,<span class="at">header=</span>F)[,<span class="fu">c</span>(<span class="dv">1</span>)]</span>
<span id="cb7-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb7-3" tabindex="-1"></a></span>
<span id="cb7-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb7-4" tabindex="-1"></a><span class="co"># Make txdb from the honey bee gene annotations</span></span>
<span id="cb7-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb7-5" tabindex="-1"></a>txdb <span class="ot"><-</span> <span class="fu">makeTxDbFromGFF</span>(<span class="st">"AST-tutorial/INDEX/Amel_HAv3.1.gff"</span>)</span>
<span id="cb7-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb7-6" tabindex="-1"></a>transcripts <span class="ot"><-</span> <span class="fu">transcripts</span>(txdb)</span></code></pre></div>
<div id="function-filter_snps" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>filter_SNPs</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-filter_snps" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-1" tabindex="-1"></a>filter_SNPs <span class="ot"><-</span> <span class="cf">function</span>(SNP_gene_bed,exons,filterlist){</span>
<span id="cb8-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-2" tabindex="-1"></a> <span class="do">## SNP_gene_bed = bed file of SNPs intersecting genes</span></span>
<span id="cb8-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-3" tabindex="-1"></a> <span class="do">## exons = GRanges object containing exon regions</span></span>
<span id="cb8-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-4" tabindex="-1"></a> <span class="do">## filterlist = list of gene IDs to filter</span></span>
<span id="cb8-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-5" tabindex="-1"></a></span>
<span id="cb8-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-6" tabindex="-1"></a> <span class="co"># Load in the SNPs BED file</span></span>
<span id="cb8-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-7" tabindex="-1"></a> SNPs <span class="ot"><-</span> <span class="fu">readBed</span>(SNP_gene_bed)</span>
<span id="cb8-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-8" tabindex="-1"></a> <span class="co"># Get SNPs that overlap with transcripts</span></span>
<span id="cb8-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-9" tabindex="-1"></a> SNPs.exons <span class="ot"><-</span> <span class="fu">findOverlaps</span>(SNPs,exons)</span>
<span id="cb8-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-10" tabindex="-1"></a> SNPs <span class="ot"><-</span> SNPs[<span class="fu">queryHits</span>(SNPs.exons)]</span>
<span id="cb8-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-11" tabindex="-1"></a> SNPs <span class="ot"><-</span> <span class="fu">data.frame</span>(SNPs)[,<span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">7</span>)]</span>
<span id="cb8-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-12" tabindex="-1"></a> <span class="fu">names</span>(SNPs) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"chr"</span>,<span class="st">"pos"</span>,<span class="st">"SNP_gene"</span>)</span>
<span id="cb8-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-13" tabindex="-1"></a> <span class="co"># Create SNP and geneID columns by splitting SNP_gene on ":"</span></span>
<span id="cb8-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-14" tabindex="-1"></a> SNPs<span class="sc">$</span>SNP <span class="ot"><-</span> <span class="fu">as.character</span>(<span class="fu">map</span>(<span class="fu">strsplit</span>(SNPs<span class="sc">$</span>SNP_gene,<span class="at">split =</span> <span class="st">":"</span>), <span class="dv">1</span>))</span>
<span id="cb8-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-15" tabindex="-1"></a> SNPs<span class="sc">$</span>geneID <span class="ot"><-</span> <span class="fu">as.character</span>(<span class="fu">map</span>(<span class="fu">strsplit</span>(SNPs<span class="sc">$</span>SNP_gene,<span class="at">split =</span> <span class="st">":"</span>), <span class="dv">2</span>))</span>
<span id="cb8-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-16" tabindex="-1"></a> <span class="co"># Filter SNPs in genes within filterlist</span></span>
<span id="cb8-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-17" tabindex="-1"></a> SNPs <span class="ot"><-</span> SNPs[<span class="sc">!</span>SNPs<span class="sc">$</span>geneID<span class="sc">%in%</span>filterlist,]</span>
<span id="cb8-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-18" tabindex="-1"></a> <span class="co"># Delete any duplicate rows and clean up</span></span>
<span id="cb8-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-19" tabindex="-1"></a> SNPs <span class="ot"><-</span> SNPs[<span class="sc">!</span><span class="fu">duplicated</span>(SNPs),]</span>
<span id="cb8-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-20" tabindex="-1"></a> <span class="fu">rm</span>(SNPs.exons)</span>
<span id="cb8-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-21" tabindex="-1"></a> </span>
<span id="cb8-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-22" tabindex="-1"></a> <span class="co"># Output</span></span>
<span id="cb8-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-23" tabindex="-1"></a> <span class="fu">return</span>(SNPs)</span>
<span id="cb8-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb8-24" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb9-1" tabindex="-1"></a><span class="co"># Execute</span></span>
<span id="cb9-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb9-2" tabindex="-1"></a>SNPs <span class="ot"><-</span> <span class="fu">filter_SNPs</span>(<span class="st">"AST-tutorial/ANALYSIS_SETS/SNPs_for_Analysis_Sorted.bed"</span>,</span>
<span id="cb9-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb9-3" tabindex="-1"></a> transcripts,filterlist)</span>
<span id="cb9-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb9-4" tabindex="-1"></a></span>
<span id="cb9-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb9-5" tabindex="-1"></a><span class="co"># Save output</span></span>
<span id="cb9-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb9-6" tabindex="-1"></a><span class="fu">write.csv</span>(SNPs,<span class="st">"AST-tutorial/ANALYSIS/INTERMEDIATES/SNPs.csv"</span>,<span class="at">row.names=</span>F)</span></code></pre></div>
<p>Example rows:</p>
<table class="table" style="margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
chr
</th>
<th style="text-align:right;">
pos
</th>
<th style="text-align:left;">
SNP_gene
</th>
<th style="text-align:left;">
SNP
</th>
<th style="text-align:left;">
geneID
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
NC_037638.1
</td>
<td style="text-align:right;">
54752
</td>
<td style="text-align:left;">
snp_99616:LOC107964061
</td>
<td style="text-align:left;">
snp_99616
</td>
<td style="text-align:left;">
LOC107964061
</td>
</tr>
<tr>
<td style="text-align:left;">
NC_037638.1
</td>
<td style="text-align:right;">
91397
</td>
<td style="text-align:left;">
snp_99617:LOC113219112
</td>
<td style="text-align:left;">
snp_99617
</td>
<td style="text-align:left;">
LOC113219112
</td>
</tr>
<tr>
<td style="text-align:left;">
NC_037638.1
</td>
<td style="text-align:right;">
147183
</td>
<td style="text-align:left;">
snp_99618:LOC726544
</td>
<td style="text-align:left;">
snp_99618
</td>
<td style="text-align:left;">
LOC726544
</td>
</tr>
<tr>
<td style="text-align:left;">
NC_037638.1
</td>
<td style="text-align:right;">
148247
</td>
<td style="text-align:left;">
snp_99619:LOC726544
</td>
<td style="text-align:left;">
snp_99619
</td>
<td style="text-align:left;">
LOC726544
</td>
</tr>
<tr>
<td style="text-align:left;">
NC_037638.1
</td>
<td style="text-align:right;">
148360
</td>
<td style="text-align:left;">
snp_99620:LOC726544
</td>
<td style="text-align:left;">
snp_99620
</td>
<td style="text-align:left;">
LOC726544
</td>
</tr>
<tr>
<td style="text-align:left;">
NC_037638.1
</td>
<td style="text-align:right;">
148370
</td>
<td style="text-align:left;">
snp_99621:LOC726544
</td>
<td style="text-align:left;">
snp_99621
</td>
<td style="text-align:left;">
LOC726544
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="merge-count-files-to-matrix" class="section level3 hasAnchor" number="3.3.2">
<h3><span class="header-section-number">3.3.2</span> Merge count files to matrix<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#merge-count-files-to-matrix" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The F2 read counts at F1 SNPs generated in Section 2 are imported. In cases where the distance between SNPs is shorter than the average read length (and thus the read counts are exactly the same for both SNPs), one SNP is chosen at random.</p>
<div id="function-make_ase_counts_matrix" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>make_ASE_counts_matrix</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-make_ase_counts_matrix" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-1" tabindex="-1"></a>make_ASE_counts_matrix <span class="ot"><-</span> <span class="cf">function</span>(DIR_COUNTS,cores,SNPs){</span>
<span id="cb10-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-2" tabindex="-1"></a> <span class="do">## DIR_COUNTS = name of subdirectory containing count files</span></span>
<span id="cb10-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-3" tabindex="-1"></a> <span class="do">## cores = number of threads for parallel processing</span></span>
<span id="cb10-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-4" tabindex="-1"></a> <span class="do">## SNPs = dataframe generated by filter_SNPs2</span></span>
<span id="cb10-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-5" tabindex="-1"></a> </span>
<span id="cb10-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-6" tabindex="-1"></a> <span class="co"># Read in data</span></span>
<span id="cb10-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-7" tabindex="-1"></a> <span class="do">## List files ending in .txt in DIR_COUNTS</span></span>
<span id="cb10-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-8" tabindex="-1"></a> SNP_counts <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="fu">matrix</span>(<span class="at">ncol=</span><span class="dv">0</span>,<span class="at">nrow=</span><span class="fu">length</span>(SNPs<span class="sc">$</span>SNP_gene)))</span>
<span id="cb10-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-9" tabindex="-1"></a> SNP_counts<span class="sc">$</span>SNP_gene <span class="ot"><-</span> SNPs<span class="sc">$</span>SNP_gene</span>
<span id="cb10-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-10" tabindex="-1"></a> files.counts <span class="ot"><-</span> <span class="fu">list.files</span>(<span class="at">path=</span>DIR_COUNTS, <span class="at">pattern=</span><span class="st">"*.txt"</span>, </span>
<span id="cb10-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-11" tabindex="-1"></a> <span class="at">full.names=</span><span class="cn">TRUE</span>, <span class="at">recursive=</span><span class="cn">FALSE</span>)</span>
<span id="cb10-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-12" tabindex="-1"></a> <span class="do">## Left join count files by SNP_ID:geneID</span></span>
<span id="cb10-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-13" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(files.counts)){</span>
<span id="cb10-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-14" tabindex="-1"></a> <span class="fu">print</span>(i)</span>
<span id="cb10-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-15" tabindex="-1"></a> tmp.name <span class="ot"><-</span> <span class="fu">strsplit</span>(<span class="fu">strsplit</span>(files.counts[[i]], </span>
<span id="cb10-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-16" tabindex="-1"></a> <span class="at">split =</span> <span class="st">"/"</span>)[[<span class="dv">1</span>]][<span class="fu">length</span>(<span class="fu">strsplit</span>(files.counts[[i]], </span>
<span id="cb10-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-17" tabindex="-1"></a> <span class="at">split =</span> <span class="st">"/"</span>)[[<span class="dv">1</span>]])],</span>
<span id="cb10-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-18" tabindex="-1"></a> <span class="at">split =</span> <span class="st">"[.]"</span>)[[<span class="dv">1</span>]][<span class="dv">1</span>]</span>
<span id="cb10-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-19" tabindex="-1"></a> <span class="cf">if</span>(tmp.name<span class="sc">%in%</span>metadata<span class="sc">$</span>sample.id){</span>
<span id="cb10-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-20" tabindex="-1"></a> tmp <span class="ot"><-</span> <span class="fu">read.table</span>(files.counts[[i]],<span class="at">header=</span>F)[,<span class="fu">c</span>(<span class="dv">4</span>,<span class="dv">7</span>)]</span>
<span id="cb10-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-21" tabindex="-1"></a> <span class="fu">names</span>(tmp) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"SNP_gene"</span>,tmp.name)</span>
<span id="cb10-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-22" tabindex="-1"></a> SNP_counts <span class="ot"><-</span> SNP_counts <span class="sc">%>%</span> </span>
<span id="cb10-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-23" tabindex="-1"></a> <span class="fu">left_join</span>(tmp, <span class="at">by =</span> <span class="fu">c</span>(<span class="st">'SNP_gene'</span> <span class="ot">=</span> <span class="st">'SNP_gene'</span>)) </span>
<span id="cb10-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-24" tabindex="-1"></a> }</span>
<span id="cb10-25"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-25" tabindex="-1"></a> }</span>
<span id="cb10-26"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-26" tabindex="-1"></a> <span class="fu">rm</span>(tmp,files.counts,tmp.name)</span>
<span id="cb10-27"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-27" tabindex="-1"></a> </span>
<span id="cb10-28"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-28" tabindex="-1"></a> <span class="co"># Clean up dataframe</span></span>
<span id="cb10-29"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-29" tabindex="-1"></a> <span class="fu">row.names</span>(SNP_counts) <span class="ot"><-</span> SNP_counts<span class="sc">$</span>SNP_gene</span>
<span id="cb10-30"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-30" tabindex="-1"></a> SNP_counts<span class="sc">$</span>SNP_gene <span class="ot"><-</span> <span class="cn">NULL</span></span>
<span id="cb10-31"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-31" tabindex="-1"></a> SNP_counts[<span class="fu">is.na</span>(SNP_counts)] <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb10-32"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-32" tabindex="-1"></a> SNP_counts<span class="sc">$</span>gene <span class="ot"><-</span> <span class="fu">as.character</span>(<span class="fu">map</span>(<span class="fu">strsplit</span>(<span class="fu">row.names</span>(SNP_counts), <span class="at">split =</span> <span class="st">":"</span>), <span class="dv">2</span>))</span>
<span id="cb10-33"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-33" tabindex="-1"></a> </span>
<span id="cb10-34"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-34" tabindex="-1"></a> <span class="co"># Remove duplicate rows within genes</span></span>
<span id="cb10-35"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-35" tabindex="-1"></a> genelist <span class="ot"><-</span> <span class="fu">unique</span>(SNP_counts<span class="sc">$</span>gene)</span>
<span id="cb10-36"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-36" tabindex="-1"></a> delete.rows <span class="ot"><-</span> <span class="fu">list</span>()</span>
<span id="cb10-37"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-37" tabindex="-1"></a> <span class="do">## Use DoParallel to perform loop operation in parallel</span></span>
<span id="cb10-38"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-38" tabindex="-1"></a> <span class="fu">registerDoParallel</span>(<span class="at">cores=</span>cores)</span>
<span id="cb10-39"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-39" tabindex="-1"></a> delete.rows <span class="ot"><-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(genelist)) <span class="sc">%dopar%</span> {</span>
<span id="cb10-40"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-40" tabindex="-1"></a> tmp <span class="ot"><-</span> SNP_counts[SNP_counts<span class="sc">$</span>gene<span class="sc">==</span>genelist[i],]</span>
<span id="cb10-41"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-41" tabindex="-1"></a> d <span class="ot"><-</span> <span class="fu">row.names</span>(tmp[<span class="fu">duplicated</span>(tmp),])</span>
<span id="cb10-42"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-42" tabindex="-1"></a> delete.rows <span class="ot"><-</span> <span class="fu">return</span>(d)</span>
<span id="cb10-43"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-43" tabindex="-1"></a> }</span>
<span id="cb10-44"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-44" tabindex="-1"></a> SNP_counts <span class="ot"><-</span> SNP_counts[<span class="sc">!</span><span class="fu">row.names</span>(SNP_counts)<span class="sc">%in%</span><span class="fu">unlist</span>(delete.rows),]</span>
<span id="cb10-45"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-45" tabindex="-1"></a> </span>
<span id="cb10-46"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-46" tabindex="-1"></a> <span class="co"># Output</span></span>
<span id="cb10-47"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-47" tabindex="-1"></a> <span class="fu">return</span>(SNP_counts)</span>
<span id="cb10-48"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb10-48" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb11-1" tabindex="-1"></a><span class="co"># Execute</span></span>
<span id="cb11-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb11-2" tabindex="-1"></a>SNP_counts <span class="ot"><-</span> <span class="fu">make_ASE_counts_matrix</span>(<span class="st">"AST-tutorial/COUNT"</span>,<span class="dv">4</span>,SNPs)</span>
<span id="cb11-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb11-3" tabindex="-1"></a></span>
<span id="cb11-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb11-4" tabindex="-1"></a><span class="co"># Save output</span></span>
<span id="cb11-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb11-5" tabindex="-1"></a><span class="fu">write.csv</span>(SNP_counts,<span class="st">"AST-tutorial/ANALYSIS/INTERMEDIATES/SNP_gene_counts.csv"</span>)</span></code></pre></div>
<p>Example rows:</p>
<table class="table" style="margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
</th>
<th style="text-align:right;">
B3D_CMG0017_S9
</th>
<th style="text-align:right;">
B3D_CMG0018_S10
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
snp_99616:LOC107964061
</td>
<td style="text-align:right;">
8
</td>
<td style="text-align:right;">
7
</td>
</tr>
<tr>
<td style="text-align:left;">
snp_99617:LOC113219112
</td>
<td style="text-align:right;">
9
</td>
<td style="text-align:right;">
6
</td>
</tr>
<tr>
<td style="text-align:left;">
snp_99618:LOC726544
</td>
<td style="text-align:right;">
7
</td>
<td style="text-align:right;">
11
</td>
</tr>
<tr>
<td style="text-align:left;">
snp_99619:LOC726544
</td>
<td style="text-align:right;">
3
</td>
<td style="text-align:right;">
7
</td>
</tr>
<tr>
<td style="text-align:left;">
snp_1:LOC726544
</td>
<td style="text-align:right;">
3
</td>
<td style="text-align:right;">
7
</td>
</tr>
<tr>
<td style="text-align:left;">
snp_99623:LOC726544
</td>
<td style="text-align:right;">
3
</td>
<td style="text-align:right;">
8
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="prepare-sample-x-snpgene-counts-for-analysis" class="section level2 hasAnchor" number="3.4">
<h2><span class="header-section-number">3.4</span> Prepare sample x SNP:gene counts for analysis<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#prepare-sample-x-snpgene-counts-for-analysis" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="normalize-by-library-size" class="section level3 hasAnchor" number="3.4.1">
<h3><span class="header-section-number">3.4.1</span> Normalize by library size<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#normalize-by-library-size" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>In spite of every effort to standardize the library preparation and sequencing procedures, there are innumerable sources of between-sample variation that cannot be controlled for which will result in variation in sequencing depth between libraries. This is apparent here:</p>
<p><img src="bookdownproj_files/figure-html/unnamed-chunk-16-1.png" /></p>
<p>To adjust for differences in sequencing depth between libraries, library size factors are estimated and the read counts are normalized using the median of ratios normalization (MRN) method from DESeq2.</p>
<p><strong>First</strong>, sum the parent-specific counts by library and estimate library size factors:</p>
<div id="function-calcsizefactors" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>calcSizeFactors</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-calcsizefactors" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-1" tabindex="-1"></a>calcSizeFactors <span class="ot"><-</span> <span class="cf">function</span>(counts,mdata){</span>
<span id="cb12-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-2" tabindex="-1"></a> <span class="do">## counts = counts matrix generated by make_ASE_counts_matrix</span></span>
<span id="cb12-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-3" tabindex="-1"></a> <span class="do">## mdata = dataframe containing sample metadata</span></span>
<span id="cb12-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-4" tabindex="-1"></a> </span>
<span id="cb12-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-5" tabindex="-1"></a> <span class="co"># Merge parent-specific counts by library</span></span>
<span id="cb12-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-6" tabindex="-1"></a> counts <span class="ot"><-</span> counts[,<span class="fu">names</span>(counts)<span class="sc">%in%</span>mdata<span class="sc">$</span>sample.id]</span>
<span id="cb12-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-7" tabindex="-1"></a> counts_merged <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="fu">matrix</span>(<span class="at">ncol=</span><span class="dv">0</span>,<span class="at">nrow=</span><span class="fu">length</span>(<span class="fu">row.names</span>(counts))))</span>
<span id="cb12-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-8" tabindex="-1"></a> samples <span class="ot"><-</span> <span class="fu">unique</span>(mdata<span class="sc">$</span>individual)</span>
<span id="cb12-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-9" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(samples)){</span>
<span id="cb12-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-10" tabindex="-1"></a> tmp <span class="ot"><-</span> counts[,<span class="fu">names</span>(counts)<span class="sc">%in%</span>mdata[mdata<span class="sc">$</span>individual<span class="sc">==</span>samples[i],<span class="st">"sample.id"</span>]]</span>
<span id="cb12-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-11" tabindex="-1"></a> tmp.merged <span class="ot"><-</span> <span class="fu">rowSums</span>(tmp)</span>
<span id="cb12-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-12" tabindex="-1"></a> counts_merged <span class="ot"><-</span> <span class="fu">cbind</span>(counts_merged,tmp.merged)</span>
<span id="cb12-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-13" tabindex="-1"></a> }</span>
<span id="cb12-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-14" tabindex="-1"></a> <span class="fu">names</span>(counts_merged) <span class="ot"><-</span> samples</span>
<span id="cb12-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-15" tabindex="-1"></a> mdata.merged <span class="ot"><-</span> mdata[,<span class="fu">c</span>(<span class="st">"individual"</span>,</span>
<span id="cb12-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-16" tabindex="-1"></a> <span class="st">"phenotype"</span>,</span>
<span id="cb12-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-17" tabindex="-1"></a> <span class="st">"lineage"</span>)]</span>
<span id="cb12-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-18" tabindex="-1"></a> mdata.merged <span class="ot"><-</span> mdata.merged[<span class="sc">!</span><span class="fu">duplicated</span>(mdata.merged),]</span>
<span id="cb12-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-19" tabindex="-1"></a> </span>
<span id="cb12-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-20" tabindex="-1"></a> <span class="co"># Estimate library size factors using the median of ratios normalization method from DESeq2</span></span>
<span id="cb12-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-21" tabindex="-1"></a> dds <span class="ot"><-</span> <span class="fu">DESeqDataSetFromMatrix</span>(<span class="at">countData =</span> counts_merged, <span class="at">colData =</span> mdata.merged, </span>
<span id="cb12-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-22" tabindex="-1"></a> <span class="at">design =</span> <span class="sc">~</span> lineage <span class="sc">+</span> phenotype)</span>
<span id="cb12-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-23" tabindex="-1"></a> dds <span class="ot"><-</span> <span class="fu">estimateSizeFactors</span>(dds)</span>
<span id="cb12-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-24" tabindex="-1"></a> sizeFactors <span class="ot"><-</span> <span class="fu">sizeFactors</span>(dds)</span>
<span id="cb12-25"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-25" tabindex="-1"></a> sF2meta <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">individual=</span><span class="fu">names</span>(sizeFactors))</span>
<span id="cb12-26"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-26" tabindex="-1"></a> sF2meta<span class="sc">$</span>sF <span class="ot"><-</span> <span class="fu">as.numeric</span>(sizeFactors)</span>
<span id="cb12-27"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-27" tabindex="-1"></a> sF2meta <span class="ot"><-</span> <span class="fu">left_join</span>(sF2meta,mdata[,<span class="fu">c</span>(<span class="st">"individual"</span>,</span>
<span id="cb12-28"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-28" tabindex="-1"></a> <span class="st">"sample.id"</span>)],</span>
<span id="cb12-29"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-29" tabindex="-1"></a> <span class="at">multiple =</span> <span class="st">"all"</span>)</span>
<span id="cb12-30"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-30" tabindex="-1"></a> sF2meta <span class="ot"><-</span> sF2meta[,<span class="fu">c</span>(<span class="dv">3</span>,<span class="dv">2</span>)]</span>
<span id="cb12-31"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-31" tabindex="-1"></a> sF2meta <span class="ot"><-</span> sF2meta[<span class="fu">match</span>(<span class="fu">names</span>(counts), sF2meta<span class="sc">$</span>sample.id),]</span>
<span id="cb12-32"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-32" tabindex="-1"></a> sFs <span class="ot"><-</span> sF2meta<span class="sc">$</span>sF</span>
<span id="cb12-33"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-33" tabindex="-1"></a> <span class="fu">names</span>(sFs) <span class="ot"><-</span> sF2meta<span class="sc">$</span>sample.id</span>
<span id="cb12-34"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-34" tabindex="-1"></a> </span>
<span id="cb12-35"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-35" tabindex="-1"></a> <span class="co"># Output</span></span>
<span id="cb12-36"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-36" tabindex="-1"></a> <span class="fu">return</span>(sFs)</span>
<span id="cb12-37"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb12-37" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb13-1" tabindex="-1"></a><span class="co"># Execute</span></span>
<span id="cb13-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb13-2" tabindex="-1"></a>size_factors <span class="ot"><-</span> <span class="fu">calcSizeFactors</span>(SNP_counts,metadata)</span></code></pre></div>
<p><strong>Second</strong>, perform the MRN method on the parent-specific counts using the library size factors calculated above.</p>
</div>
<div id="function-normalizeasreadcounts" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>normalizeASReadCounts</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-normalizeasreadcounts" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-1" tabindex="-1"></a>normalizeASReadCounts <span class="ot"><-</span> <span class="cf">function</span>(counts,size_factors){</span>
<span id="cb14-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-2" tabindex="-1"></a> <span class="do">## counts = counts matrix generated by make_ASE_counts_matrix</span></span>
<span id="cb14-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-3" tabindex="-1"></a> <span class="do">## size_factors = array from calcSizeFactors</span></span>
<span id="cb14-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-4" tabindex="-1"></a> </span>
<span id="cb14-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-5" tabindex="-1"></a> <span class="co"># Normalize counts for each library by parent</span></span>
<span id="cb14-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-6" tabindex="-1"></a> mdata <span class="ot"><-</span> metadata[metadata<span class="sc">$</span>sample.id<span class="sc">%in%</span><span class="fu">names</span>(counts),]</span>
<span id="cb14-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-7" tabindex="-1"></a> counts <span class="ot"><-</span> counts[,<span class="fu">names</span>(counts)<span class="sc">%in%</span>mdata<span class="sc">$</span>sample.id]</span>
<span id="cb14-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-8" tabindex="-1"></a> as.dds <span class="ot"><-</span> <span class="fu">DESeqDataSetFromMatrix</span>(<span class="at">countData =</span> counts, <span class="at">colData =</span> mdata, </span>
<span id="cb14-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-9" tabindex="-1"></a> <span class="at">design =</span> <span class="sc">~</span> lineage<span class="sc">+</span>phenotype)</span>
<span id="cb14-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-10" tabindex="-1"></a> <span class="fu">sizeFactors</span>(as.dds) <span class="ot">=</span> size_factors</span>
<span id="cb14-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-11" tabindex="-1"></a> counts_normalized <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="fu">counts</span>(as.dds, <span class="at">normalized=</span><span class="cn">TRUE</span>))</span>
<span id="cb14-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-12" tabindex="-1"></a> </span>
<span id="cb14-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-13" tabindex="-1"></a> <span class="co"># Output</span></span>
<span id="cb14-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-14" tabindex="-1"></a> <span class="fu">return</span>(counts_normalized)</span>
<span id="cb14-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb14-15" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb15-1" tabindex="-1"></a><span class="co"># Execute</span></span>
<span id="cb15-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb15-2" tabindex="-1"></a>SNP_counts_normalized <span class="ot"><-</span> <span class="fu">normalizeASReadCounts</span>(SNP_counts,size_factors)</span>
<span id="cb15-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb15-3" tabindex="-1"></a></span>
<span id="cb15-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb15-4" tabindex="-1"></a><span class="co"># Save output</span></span>
<span id="cb15-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb15-5" tabindex="-1"></a><span class="fu">write.csv</span>(SNP_counts_normalized,<span class="st">"AST-tutorial/ANALYSIS/INTERMEDIATES/SNP_gene_counts_normalized.csv"</span>)</span></code></pre></div>
<p><img src="bookdownproj_files/figure-html/unnamed-chunk-22-1.png" /></p>
</div>
</div>
<div id="split-by-phenotype-and-filter-low-count-snps" class="section level3 hasAnchor" number="3.4.2">
<h3><span class="header-section-number">3.4.2</span> Split by phenotype and filter low count SNPs<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#split-by-phenotype-and-filter-low-count-snps" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Further filtering steps are performed <em>after</em> splitting the count matrix by phenotype. This is important as the set of transcribed genes is expected to vary between phenotypes. For the remainder of the tutorial, these phenotypes will be referred to in the code as “WL” and “QL” to match the sample metadata.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-1" tabindex="-1"></a>WL.IDs <span class="ot"><-</span> metadata[metadata<span class="sc">$</span>phenotype<span class="sc">==</span><span class="st">"WL"</span>,<span class="st">"sample.id"</span>]</span>
<span id="cb16-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-2" tabindex="-1"></a>WL_counts_normalized <span class="ot"><-</span> SNP_counts_normalized[,<span class="fu">names</span>(SNP_counts_normalized)<span class="sc">%in%</span>WL.IDs]</span>
<span id="cb16-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-3" tabindex="-1"></a>WL_counts <span class="ot"><-</span> SNP_counts[<span class="fu">row.names</span>(SNP_counts)<span class="sc">%in%</span><span class="fu">row.names</span>(WL_counts_normalized),</span>
<span id="cb16-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-4" tabindex="-1"></a> <span class="fu">names</span>(SNP_counts)<span class="sc">%in%</span>WL.IDs]</span>
<span id="cb16-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-5" tabindex="-1"></a></span>
<span id="cb16-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-6" tabindex="-1"></a>QL.IDs <span class="ot"><-</span> metadata[metadata<span class="sc">$</span>phenotype<span class="sc">==</span><span class="st">"QL"</span>,<span class="st">"sample.id"</span>]</span>
<span id="cb16-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-7" tabindex="-1"></a>QL_counts_normalized <span class="ot"><-</span> SNP_counts_normalized[,<span class="fu">names</span>(SNP_counts_normalized)<span class="sc">%in%</span>QL.IDs]</span>
<span id="cb16-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-8" tabindex="-1"></a>QL_counts <span class="ot"><-</span> SNP_counts[<span class="fu">row.names</span>(SNP_counts)<span class="sc">%in%</span><span class="fu">row.names</span>(QL_counts_normalized),</span>
<span id="cb16-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb16-9" tabindex="-1"></a> <span class="fu">names</span>(SNP_counts)<span class="sc">%in%</span>QL.IDs]</span></code></pre></div>
<p>SNPs with <span class="math inline">\(n < 1\)</span> count in any Lineage are discarded. Finally, genes with <span class="math inline">\(n < 2\)</span> SNPs are discarded, as this method requires at least two independent observations per gene for statistical analysis.</p>
<p><em>Note: here is where the utility of functions really becomes apparent. For the remainder of the tutorial, the same functions will be applied to the “WL” and “QL” count matrices.</em></p>
<div id="function-filter_counts" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>filter_counts</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-filter_counts" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-1" tabindex="-1"></a>filter_counts <span class="ot"><-</span> <span class="cf">function</span>(counts,lcf){</span>
<span id="cb17-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-2" tabindex="-1"></a> <span class="co"># counts = phenotype-specific counts matrix</span></span>
<span id="cb17-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-3" tabindex="-1"></a> <span class="co"># lcf = low count filter threshold (integer)</span></span>
<span id="cb17-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-4" tabindex="-1"></a> </span>
<span id="cb17-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-5" tabindex="-1"></a> <span class="co"># Remove rows with < lcf counts counts by Lineage</span></span>
<span id="cb17-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-6" tabindex="-1"></a> LA <span class="ot"><-</span> metadata[metadata<span class="sc">$</span>lineage<span class="sc">==</span><span class="st">"A"</span>,<span class="st">"sample.id"</span>]</span>
<span id="cb17-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-7" tabindex="-1"></a> LB <span class="ot"><-</span> metadata[metadata<span class="sc">$</span>lineage<span class="sc">==</span><span class="st">"B"</span>,<span class="st">"sample.id"</span>]</span>
<span id="cb17-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-8" tabindex="-1"></a> counts <span class="ot"><-</span> counts[<span class="fu">rowSums</span>(counts[,<span class="fu">names</span>(counts)<span class="sc">%in%</span>LA])<span class="sc">></span>lcf,]</span>
<span id="cb17-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-9" tabindex="-1"></a> counts <span class="ot"><-</span> counts[<span class="fu">rowSums</span>(counts[,<span class="fu">names</span>(counts)<span class="sc">%in%</span>LB])<span class="sc">></span>lcf,]</span>
<span id="cb17-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-10" tabindex="-1"></a> </span>
<span id="cb17-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-11" tabindex="-1"></a> <span class="co"># Flag rows with greater than 10000 counts</span></span>
<span id="cb17-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-12" tabindex="-1"></a> <span class="do">## Different functions are used for SK tests with < and > 10000 counts for computational efficiency</span></span>
<span id="cb17-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-13" tabindex="-1"></a> counts<span class="sc">$</span>SUM <span class="ot"><-</span> <span class="fu">rowSums</span>(counts)</span>
<span id="cb17-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-14" tabindex="-1"></a> counts<span class="sc">$</span>SKrow <span class="ot"><-</span> F</span>
<span id="cb17-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-15" tabindex="-1"></a> counts[counts<span class="sc">$</span>SUM<span class="sc"><</span><span class="dv">10000</span>,<span class="st">"SKrow"</span>] <span class="ot"><-</span> T</span>
<span id="cb17-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-16" tabindex="-1"></a> counts<span class="sc">$</span>SUM <span class="ot"><-</span> <span class="cn">NULL</span></span>
<span id="cb17-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-17" tabindex="-1"></a> </span>
<span id="cb17-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-18" tabindex="-1"></a> <span class="co"># Remove genes with < 2 SNPs</span></span>
<span id="cb17-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-19" tabindex="-1"></a> counts<span class="sc">$</span>gene <span class="ot"><-</span> <span class="fu">as.character</span>(<span class="fu">map</span>(<span class="fu">strsplit</span>(<span class="fu">row.names</span>(counts), <span class="at">split =</span> <span class="st">":"</span>), <span class="dv">2</span>))</span>
<span id="cb17-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-20" tabindex="-1"></a> genelist <span class="ot"><-</span> <span class="fu">unique</span>(counts<span class="sc">$</span>gene)</span>
<span id="cb17-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-21" tabindex="-1"></a> delete.rows <span class="ot"><-</span> <span class="fu">list</span>()</span>
<span id="cb17-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-22" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(genelist)){</span>
<span id="cb17-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-23" tabindex="-1"></a> tmp <span class="ot"><-</span> counts[counts<span class="sc">$</span>gene<span class="sc">==</span>genelist[i],]</span>
<span id="cb17-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-24" tabindex="-1"></a> tmp <span class="ot"><-</span> tmp[<span class="sc">!</span><span class="fu">duplicated</span>(tmp),]</span>
<span id="cb17-25"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-25" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(<span class="fu">row.names</span>(tmp))<span class="sc"><</span><span class="dv">2</span>){</span>
<span id="cb17-26"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-26" tabindex="-1"></a> delete.rows <span class="ot"><-</span> <span class="fu">append</span>(delete.rows,genelist[i])</span>
<span id="cb17-27"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-27" tabindex="-1"></a> }</span>
<span id="cb17-28"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-28" tabindex="-1"></a> }</span>
<span id="cb17-29"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-29" tabindex="-1"></a> counts <span class="ot"><-</span> counts[<span class="sc">!</span>counts<span class="sc">$</span>gene<span class="sc">%in%</span><span class="fu">unlist</span>(delete.rows),]</span>
<span id="cb17-30"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-30" tabindex="-1"></a> counts<span class="sc">$</span>gene <span class="ot"><-</span> <span class="cn">NULL</span></span>
<span id="cb17-31"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-31" tabindex="-1"></a> </span>
<span id="cb17-32"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-32" tabindex="-1"></a> <span class="co"># Return filtered counts</span></span>
<span id="cb17-33"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-33" tabindex="-1"></a> <span class="fu">return</span>(counts)</span>
<span id="cb17-34"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb17-34" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-1" tabindex="-1"></a><span class="co"># Execute</span></span>
<span id="cb18-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-2" tabindex="-1"></a>WL_counts_normalized <span class="ot"><-</span> <span class="fu">filter_counts</span>(WL_counts_normalized,<span class="dv">0</span>)</span>
<span id="cb18-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-3" tabindex="-1"></a>WL_counts <span class="ot"><-</span> WL_counts[<span class="fu">row.names</span>(WL_counts)<span class="sc">%in%</span><span class="fu">row.names</span>(WL_counts_normalized),]</span>
<span id="cb18-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-4" tabindex="-1"></a>QL_counts_normalized <span class="ot"><-</span> <span class="fu">filter_counts</span>(QL_counts_normalized,<span class="dv">0</span>)</span>
<span id="cb18-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-5" tabindex="-1"></a>QL_counts <span class="ot"><-</span> QL_counts[<span class="fu">row.names</span>(QL_counts)<span class="sc">%in%</span><span class="fu">row.names</span>(QL_counts_normalized),]</span>
<span id="cb18-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-6" tabindex="-1"></a></span>
<span id="cb18-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-7" tabindex="-1"></a><span class="co"># Save output</span></span>
<span id="cb18-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-8" tabindex="-1"></a><span class="fu">write.csv</span>(WL_counts_normalized,<span class="st">"AST-tutorial/ANALYSIS/INTERMEDIATES/G53xY39_WL_counts_normalized.csv"</span>)</span>
<span id="cb18-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-9" tabindex="-1"></a><span class="fu">write.csv</span>(WL_counts,<span class="st">"AST-tutorial/ANALYSIS/INTERMEDIATES/G53xY39_WL_counts.csv"</span>)</span>
<span id="cb18-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-10" tabindex="-1"></a><span class="fu">write.csv</span>(QL_counts_normalized,<span class="st">"AST-tutorial/ANALYSIS/INTERMEDIATES/G53xY39_QL_counts_normalized.csv"</span>)</span>
<span id="cb18-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb18-11" tabindex="-1"></a><span class="fu">write.csv</span>(QL_counts,<span class="st">"AST-tutorial/ANALYSIS/INTERMEDIATES/G53xY39_QL_counts.csv"</span>)</span></code></pre></div>
</div>
</div>
</div>
<div id="conduct-statistical-tests" class="section level2 hasAnchor" number="3.5">
<h2><span class="header-section-number">3.5</span> Conduct statistical tests<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#conduct-statistical-tests" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The data are now ready for statistical analysis of allele-specific transcription. Storer-Kim, GLIMMIX, and threshold tests will be performed for each phenotype, separately, and then a Chi-squared test will be performed to compare the distribution of genes showing parent- and lineage-biased transcription between phenotypes.</p>
<div id="snp-level-storer-kim-sk-binomial-exact-tests" class="section level3 hasAnchor" number="3.5.1">
<h3><span class="header-section-number">3.5.1</span> SNP-level Storer-Kim (SK) binomial exact tests<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#snp-level-storer-kim-sk-binomial-exact-tests" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>For each SNP, a <a href="https://doi.org/10.1080/01621459.1990.10475318">Storer-Kim binomial exact test of two proportions</a> is conducted using the MRN counts to test the hypothesis that the proportion of maternal and paternal read counts are statistically different.</p>
<div id="function-twobinom" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>twobinom</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-twobinom" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<p>This test is performed using a modified version of the <code>twobinom</code> function from <a href="https://cran.r-project.org/web/packages/WRS2/index.html">WRS2</a>. For computational efficiency, the <code>outer()</code> command has been replaced with <code>Rfast::Outer()</code>.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-1" tabindex="-1"></a>twobinom<span class="ot"><-</span><span class="cf">function</span>(r1,n1,r2,n2,<span class="at">alpha=</span>.<span class="dv">05</span>){</span>
<span id="cb19-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-2" tabindex="-1"></a> <span class="co"># r1 = success in group 1</span></span>
<span id="cb19-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-3" tabindex="-1"></a> <span class="co"># n1 = total in group 1</span></span>
<span id="cb19-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-4" tabindex="-1"></a> <span class="co"># r2 = success in group 2</span></span>
<span id="cb19-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-5" tabindex="-1"></a> <span class="co"># r2 = total in group 2</span></span>
<span id="cb19-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-6" tabindex="-1"></a> n1p<span class="ot"><-</span>n1<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb19-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-7" tabindex="-1"></a> n2p<span class="ot"><-</span>n2<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb19-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-8" tabindex="-1"></a> n1m<span class="ot"><-</span>n1<span class="dv">-1</span></span>
<span id="cb19-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-9" tabindex="-1"></a> n2m<span class="ot"><-</span>n2<span class="dv">-1</span></span>
<span id="cb19-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-10" tabindex="-1"></a> q <span class="ot"><-</span> r1<span class="sc">/</span>n1</span>
<span id="cb19-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-11" tabindex="-1"></a> p <span class="ot"><-</span> r2<span class="sc">/</span>n2</span>
<span id="cb19-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-12" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">is.na</span>(q)){q <span class="ot"><-</span> <span class="dv">0</span>}</span>
<span id="cb19-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-13" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">is.na</span>(p)){p <span class="ot"><-</span> <span class="dv">0</span>}</span>
<span id="cb19-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-14" tabindex="-1"></a> chk<span class="ot"><-</span><span class="fu">abs</span>(q<span class="sc">-</span>p)</span>
<span id="cb19-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-15" tabindex="-1"></a> x<span class="ot"><-</span><span class="fu">c</span>(<span class="dv">0</span><span class="sc">:</span>n1)<span class="sc">/</span>n1</span>
<span id="cb19-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-16" tabindex="-1"></a> y<span class="ot"><-</span><span class="fu">c</span>(<span class="dv">0</span><span class="sc">:</span>n2)<span class="sc">/</span>n2 </span>
<span id="cb19-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-17" tabindex="-1"></a> phat<span class="ot"><-</span>(r1<span class="sc">+</span>r2)<span class="sc">/</span>(n1<span class="sc">+</span>n2)</span>
<span id="cb19-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-18" tabindex="-1"></a> m1<span class="ot"><-</span><span class="fu">t</span>(<span class="fu">Outer</span>(x,y,<span class="st">"-"</span>))</span>
<span id="cb19-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-19" tabindex="-1"></a> m2<span class="ot"><-</span><span class="fu">matrix</span>(<span class="dv">1</span>,n1p,n2p)</span>
<span id="cb19-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-20" tabindex="-1"></a> flag<span class="ot"><-</span>(<span class="fu">abs</span>(m1)<span class="sc">>=</span>chk)</span>
<span id="cb19-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-21" tabindex="-1"></a> m3<span class="ot"><-</span>m2<span class="sc">*</span>flag</span>
<span id="cb19-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-22" tabindex="-1"></a> <span class="fu">rm</span>(m1,m2,flag)</span>
<span id="cb19-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-23" tabindex="-1"></a> xv<span class="ot"><-</span><span class="fu">c</span>(<span class="dv">1</span><span class="sc">:</span>n1)</span>
<span id="cb19-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-24" tabindex="-1"></a> yv<span class="ot"><-</span><span class="fu">c</span>(<span class="dv">1</span><span class="sc">:</span>n2)</span>
<span id="cb19-25"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-25" tabindex="-1"></a> xv1<span class="ot"><-</span>n1<span class="sc">-</span>xv<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb19-26"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-26" tabindex="-1"></a> yv1<span class="ot"><-</span>n2<span class="sc">-</span>yv<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb19-27"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-27" tabindex="-1"></a> dis1<span class="ot"><-</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="fu">pbeta</span>(phat,xv,xv1))</span>
<span id="cb19-28"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-28" tabindex="-1"></a> dis2<span class="ot"><-</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="fu">pbeta</span>(phat,yv,yv1))</span>
<span id="cb19-29"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-29" tabindex="-1"></a> pd1<span class="ot"><-</span><span class="cn">NA</span></span>
<span id="cb19-30"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-30" tabindex="-1"></a> pd2<span class="ot"><-</span><span class="cn">NA</span></span>
<span id="cb19-31"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-31" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>n1){pd1[i]<span class="ot"><-</span>dis1[i]<span class="sc">-</span>dis1[i<span class="sc">+</span><span class="dv">1</span>]}</span>
<span id="cb19-32"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-32" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>n2){pd2[i]<span class="ot"><-</span>dis2[i]<span class="sc">-</span>dis2[i<span class="sc">+</span><span class="dv">1</span>]}</span>
<span id="cb19-33"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-33" tabindex="-1"></a> pd1[n1p]<span class="ot"><-</span>phat<span class="sc">^</span>n1</span>
<span id="cb19-34"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-34" tabindex="-1"></a> pd2[n2p]<span class="ot"><-</span>phat<span class="sc">^</span>n2</span>
<span id="cb19-35"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-35" tabindex="-1"></a> m4<span class="ot"><-</span><span class="fu">t</span>(<span class="fu">Outer</span>(pd1,pd2,<span class="st">"*"</span>))</span>
<span id="cb19-36"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-36" tabindex="-1"></a> test<span class="ot"><-</span><span class="fu">sum</span>(m3<span class="sc">*</span>m4)</span>
<span id="cb19-37"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-37" tabindex="-1"></a> <span class="fu">rm</span>(m3,m4)</span>
<span id="cb19-38"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-38" tabindex="-1"></a> <span class="fu">list</span>(<span class="at">p.value=</span>test,<span class="at">p1=</span>q,<span class="at">p2=</span>p,<span class="at">est.dif=</span>q<span class="sc">-</span>p)</span>
<span id="cb19-39"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb19-39" tabindex="-1"></a>}</span></code></pre></div>
</div>
<div id="function-ast.sk" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>AST.SK</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-ast.sk" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<p>This is a wrapper function to perform the Storer-Kim test comparing the proportion of maternal to paternal read counts on each SNP in the count matrix. The Storer-Kim test has greater statistical power for low count observations, but is computationally expensive to perform and crashes when the total counts for a SNP are <em>n</em> > 10,000. Therefore, <code>filter_counts</code> flagged these rows, and <code>AST.SK</code> will instead perform a Fisher’s Exact test of two proportions, which is equivalent to a Storer-Kim test for high count observations. The test <em>p</em>-value is reported for each SNP.</p>
<p><em>Note: because this test is computationally expensive, executing <code>AST.SK</code> will take approximately 2hrs per 10,000 SNPs.</em></p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-1" tabindex="-1"></a>AST.SK <span class="ot"><-</span> <span class="cf">function</span>(counts,phenotype,cores){</span>
<span id="cb20-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-2" tabindex="-1"></a> <span class="co"># counts = phenotype-specific counts matrix</span></span>
<span id="cb20-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-3" tabindex="-1"></a> <span class="co"># phenotype = corresponding phenotype of counts</span></span>
<span id="cb20-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-4" tabindex="-1"></a> <span class="co"># cores = # of threads for multithreaded search</span></span>
<span id="cb20-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-5" tabindex="-1"></a> </span>
<span id="cb20-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-6" tabindex="-1"></a> <span class="co"># Split data by pat and mat</span></span>
<span id="cb20-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-7" tabindex="-1"></a> pat.exp <span class="ot"><-</span> counts[,metadata[metadata<span class="sc">$</span>parent<span class="sc">%in%</span><span class="fu">c</span>(<span class="st">"M"</span>)<span class="sc">&</span></span>
<span id="cb20-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-8" tabindex="-1"></a> metadata<span class="sc">$</span>phenotype<span class="sc">==</span>phenotype,<span class="st">"sample.id"</span>]]</span>
<span id="cb20-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-9" tabindex="-1"></a> mat.exp <span class="ot"><-</span> counts[,metadata[metadata<span class="sc">$</span>parent<span class="sc">%in%</span><span class="fu">c</span>(<span class="st">"F"</span>)<span class="sc">&</span></span>
<span id="cb20-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-10" tabindex="-1"></a> metadata<span class="sc">$</span>phenotype<span class="sc">==</span>phenotype,<span class="st">"sample.id"</span>]]</span>
<span id="cb20-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-11" tabindex="-1"></a> <span class="co"># Set up for DoParallel</span></span>
<span id="cb20-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-12" tabindex="-1"></a> SKrows<span class="ot">=</span>counts<span class="sc">$</span>SKrow</span>
<span id="cb20-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-13" tabindex="-1"></a> <span class="fu">registerDoParallel</span>(<span class="at">cores=</span>cores)</span>
<span id="cb20-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-14" tabindex="-1"></a> i.len<span class="ot">=</span><span class="fu">length</span>(<span class="fu">row.names</span>(pat.exp))</span>
<span id="cb20-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-15" tabindex="-1"></a> </span>
<span id="cb20-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-16" tabindex="-1"></a> <span class="co"># For each row, conduct an SK test and return the p-value</span></span>
<span id="cb20-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-17" tabindex="-1"></a> return.df <span class="ot"><-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span>i.len, <span class="at">.combine=</span>rbind,</span>
<span id="cb20-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-18" tabindex="-1"></a> <span class="at">.export=</span><span class="fu">ls</span>(<span class="fu">globalenv</span>()),<span class="at">.packages=</span><span class="st">"Rfast"</span>) <span class="sc">%dopar%</span> {</span>
<span id="cb20-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-19" tabindex="-1"></a> SNP_gene<span class="ot">=</span><span class="fu">row.names</span>(pat.exp[i,])</span>
<span id="cb20-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-20" tabindex="-1"></a> p1.s<span class="ot">=</span><span class="fu">sum</span>(pat.exp[i,])</span>
<span id="cb20-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-21" tabindex="-1"></a> p2.s<span class="ot">=</span><span class="fu">sum</span>(mat.exp[i,])</span>
<span id="cb20-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-22" tabindex="-1"></a> p.o<span class="ot">=</span><span class="fu">sum</span>(p1.s,p2.s)</span>
<span id="cb20-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-23" tabindex="-1"></a> <span class="cf">if</span>(SKrows[i]<span class="sc">==</span>T){</span>
<span id="cb20-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-24" tabindex="-1"></a> test<span class="ot">=</span><span class="fu">twobinom</span>(<span class="at">r1=</span>p1.s,<span class="at">n1=</span>p.o,</span>
<span id="cb20-25"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-25" tabindex="-1"></a> <span class="at">r2=</span>p2.s,<span class="at">n2=</span>p.o)<span class="sc">$</span>p.value</span>
<span id="cb20-26"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-26" tabindex="-1"></a> }<span class="cf">else</span>{</span>
<span id="cb20-27"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-27" tabindex="-1"></a> test<span class="ot">=</span><span class="fu">fisher.test</span>(<span class="fu">matrix</span>(<span class="fu">c</span>(p1.s,p2.s,</span>
<span id="cb20-28"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-28" tabindex="-1"></a> p2.s,p1.s),<span class="at">ncol =</span> <span class="dv">2</span>))<span class="sc">$</span>p.value</span>
<span id="cb20-29"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-29" tabindex="-1"></a> }</span>
<span id="cb20-30"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-30" tabindex="-1"></a> return.append<span class="ot">=</span><span class="fu">data.frame</span>(<span class="at">SNP_gene=</span>SNP_gene,<span class="at">p=</span>test)</span>
<span id="cb20-31"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-31" tabindex="-1"></a> <span class="fu">return</span>(return.append)</span>
<span id="cb20-32"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-32" tabindex="-1"></a> }</span>
<span id="cb20-33"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-33" tabindex="-1"></a> return.df<span class="ot">=</span>return.df[<span class="fu">match</span>(<span class="fu">row.names</span>(pat.exp), return.df<span class="sc">$</span>SNP_gene),]</span>
<span id="cb20-34"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-34" tabindex="-1"></a> </span>
<span id="cb20-35"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-35" tabindex="-1"></a> <span class="co"># Return</span></span>
<span id="cb20-36"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-36" tabindex="-1"></a> <span class="fu">return</span>(return.df)</span>
<span id="cb20-37"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb20-37" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb21-1" tabindex="-1"></a><span class="co"># Execute</span></span>
<span id="cb21-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb21-2" tabindex="-1"></a>WL.SK <span class="ot"><-</span> <span class="fu">AST.SK</span>(WL_counts_normalized,<span class="st">"WL"</span>,<span class="dv">6</span>)</span>
<span id="cb21-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb21-3" tabindex="-1"></a>QL.SK <span class="ot"><-</span> <span class="fu">AST.SK</span>(QL_counts_normalized,<span class="st">"QL"</span>,<span class="dv">6</span>)</span>
<span id="cb21-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb21-4" tabindex="-1"></a></span>
<span id="cb21-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb21-5" tabindex="-1"></a><span class="co"># Save output</span></span>
<span id="cb21-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb21-6" tabindex="-1"></a><span class="fu">write.csv</span>(WL.SK,<span class="st">"AST-tutorial/ANALYSIS/RESULTS/WLSK.csv"</span>, <span class="at">row.names=</span>F)</span>
<span id="cb21-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb21-7" tabindex="-1"></a><span class="fu">write.csv</span>(QL.SK,<span class="st">"AST-tutorial/ANALYSIS/RESULTS/QLSK.csv"</span>, <span class="at">row.names=</span>F)</span></code></pre></div>
</div>
</div>
<div id="gene-level-general-linear-mixed-models-glimmix" class="section level3 hasAnchor" number="3.5.2">
<h3><span class="header-section-number">3.5.2</span> Gene-level general linear mixed models (GLIMMIX)<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#gene-level-general-linear-mixed-models-glimmix" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>A general linear mixed-effects model with interaction terms (GLIMMIX) is fit for each gene to assess the effects of Parent, Lineage, and their interaction on the raw read counts at each SNP, using the log of the library size factors as an offset to adjust for variation in sequencing depth between libraries.</p>
<div id="function-ast.glimmix" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>AST.GLIMMIX</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-ast.glimmix" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<p>This is a wrapper function to fit a GLIMMIX for each gene in the count matrix and perform Wald tests to assess the statistical significance of the effects (parent, lineage, and their interaction). Wald test <em>p</em>-values are reported for each effect for each gene. Genes for which a model could not be fit due to errors are returned with a <em>p</em>-value of “1” for each effect.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-1" tabindex="-1"></a>AST.GLIMMIX <span class="ot"><-</span> <span class="cf">function</span>(counts,size_factors,cores){</span>
<span id="cb22-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-2" tabindex="-1"></a> <span class="co"># counts = phenotype-specific counts matrix</span></span>
<span id="cb22-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-3" tabindex="-1"></a> <span class="co"># phenotype = corresponding phenotype of counts</span></span>
<span id="cb22-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-4" tabindex="-1"></a> <span class="co"># cores = # of threads for multithreaded search</span></span>
<span id="cb22-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-5" tabindex="-1"></a> <span class="co"># size_factors = object from calcSizeFactors</span></span>
<span id="cb22-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-6" tabindex="-1"></a> </span>
<span id="cb22-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-7" tabindex="-1"></a> sizeFactors <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="fu">t</span>(size_factors[<span class="fu">names</span>(size_factors)<span class="sc">%in%</span><span class="fu">names</span>(counts)]))</span>
<span id="cb22-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-8" tabindex="-1"></a> sizeFactors <span class="ot"><-</span> <span class="fu">gather</span>(sizeFactors, sample.id, sizeFactor, </span>
<span id="cb22-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-9" tabindex="-1"></a> <span class="fu">names</span>(sizeFactors),<span class="at">factor_key=</span><span class="cn">FALSE</span>)</span>
<span id="cb22-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-10" tabindex="-1"></a> counts<span class="sc">$</span>SNP_gene <span class="ot"><-</span> <span class="fu">row.names</span>(counts)</span>
<span id="cb22-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-11" tabindex="-1"></a> counts<span class="sc">$</span>geneID <span class="ot"><-</span> <span class="fu">as.character</span>(<span class="fu">unlist</span>(<span class="fu">map</span>(<span class="fu">strsplit</span>(counts<span class="sc">$</span>SNP_gene, </span>
<span id="cb22-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-12" tabindex="-1"></a> <span class="at">split =</span> <span class="st">":"</span>), <span class="dv">2</span>)))</span>
<span id="cb22-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-13" tabindex="-1"></a> genelist <span class="ot"><-</span> <span class="fu">unique</span>(counts<span class="sc">$</span>geneID)</span>
<span id="cb22-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-14" tabindex="-1"></a> <span class="fu">registerDoParallel</span>(<span class="at">cores=</span>cores)</span>
<span id="cb22-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-15" tabindex="-1"></a> i.len <span class="ot"><-</span> <span class="fu">length</span>(genelist)</span>
<span id="cb22-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-16" tabindex="-1"></a> </span>
<span id="cb22-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-17" tabindex="-1"></a> df.out <span class="ot"><-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span>i.len,<span class="at">.combine=</span>rbind) <span class="sc">%dopar%</span> {</span>
<span id="cb22-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-18" tabindex="-1"></a> counts.sub <span class="ot"><-</span> counts[counts<span class="sc">$</span>geneID<span class="sc">==</span>genelist[i],]</span>
<span id="cb22-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-19" tabindex="-1"></a> counts.sub<span class="sc">$</span>geneID <span class="ot"><-</span> <span class="cn">NULL</span></span>
<span id="cb22-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-20" tabindex="-1"></a> counts.sub <span class="ot"><-</span> <span class="fu">gather</span>(counts.sub, sample.id, count, </span>
<span id="cb22-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-21" tabindex="-1"></a> <span class="fu">names</span>(counts.sub), <span class="sc">-</span>SNP_gene, <span class="at">factor_key=</span><span class="cn">TRUE</span>)</span>
<span id="cb22-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-22" tabindex="-1"></a> counts.sub <span class="ot"><-</span> <span class="fu">join</span>(counts.sub, metadata, <span class="at">by =</span> <span class="st">"sample.id"</span>)</span>
<span id="cb22-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-23" tabindex="-1"></a> counts.sub <span class="ot"><-</span> <span class="fu">join</span>(counts.sub,sizeFactors,<span class="at">by=</span><span class="st">"sample.id"</span>)</span>
<span id="cb22-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-24" tabindex="-1"></a> counts.sub<span class="sc">$</span>parent <span class="ot"><-</span> <span class="fu">as.factor</span>(<span class="fu">str_sub</span>(counts.sub<span class="sc">$</span>parent,<span class="sc">-</span><span class="dv">1</span>,<span class="sc">-</span><span class="dv">1</span>))</span>
<span id="cb22-25"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-25" tabindex="-1"></a> counts.sub<span class="sc">$</span>SNP_gene <span class="ot"><-</span> <span class="fu">as.factor</span>(counts.sub<span class="sc">$</span>SNP_gene)</span>
<span id="cb22-26"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-26" tabindex="-1"></a> counts.sub<span class="sc">$</span>lineage <span class="ot"><-</span> <span class="fu">as.factor</span>(counts.sub<span class="sc">$</span>lineage)</span>
<span id="cb22-27"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-27" tabindex="-1"></a> counts.sub<span class="sc">$</span>individual <span class="ot"><-</span> <span class="fu">as.factor</span>(counts.sub<span class="sc">$</span>individual)</span>
<span id="cb22-28"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-28" tabindex="-1"></a> testfail <span class="ot"><-</span> F</span>
<span id="cb22-29"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-29" tabindex="-1"></a> test <span class="ot"><-</span> <span class="st">"null"</span></span>
<span id="cb22-30"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-30" tabindex="-1"></a> <span class="fu">tryCatchLog</span>(test <span class="ot"><-</span> <span class="fu">lmer</span>(count<span class="sc">~</span>parent<span class="sc">+</span>lineage<span class="sc">+</span>parent<span class="sc">*</span>lineage<span class="sc">+</span>(<span class="dv">1</span><span class="sc">|</span>SNP_gene)<span class="sc">+</span></span>
<span id="cb22-31"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-31" tabindex="-1"></a> (<span class="dv">1</span><span class="sc">|</span>individual)<span class="sc">+</span><span class="fu">offset</span>(<span class="fu">log</span>(sizeFactor)),<span class="at">data=</span>counts.sub), </span>
<span id="cb22-32"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-32" tabindex="-1"></a> <span class="at">error =</span> <span class="cf">function</span>(e) {testfail <span class="ot"><-</span> T})</span>
<span id="cb22-33"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-33" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">class</span>(test)<span class="sc">==</span><span class="st">"character"</span>){testfail <span class="ot"><-</span> T}</span>
<span id="cb22-34"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-34" tabindex="-1"></a> <span class="cf">if</span>(testfail<span class="sc">==</span>F){</span>
<span id="cb22-35"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-35" tabindex="-1"></a> test <span class="ot"><-</span> <span class="fu">summary</span>(test)</span>
<span id="cb22-36"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-36" tabindex="-1"></a> parent.p.list <span class="ot"><-</span> test[[<span class="st">"coefficients"</span>]][<span class="dv">2</span>,<span class="dv">5</span>]</span>
<span id="cb22-37"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-37" tabindex="-1"></a> Lineage.p.list <span class="ot"><-</span> test[[<span class="st">"coefficients"</span>]][<span class="dv">3</span>,<span class="dv">5</span>]</span>
<span id="cb22-38"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-38" tabindex="-1"></a> parent.Lineage.p.list <span class="ot"><-</span> test[[<span class="st">"coefficients"</span>]][<span class="dv">4</span>,<span class="dv">5</span>]</span>
<span id="cb22-39"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-39" tabindex="-1"></a> }<span class="cf">else</span>{</span>
<span id="cb22-40"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-40" tabindex="-1"></a> parent.p.list <span class="ot"><-</span> <span class="dv">1</span></span>
<span id="cb22-41"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-41" tabindex="-1"></a> Lineage.p.list <span class="ot"><-</span> <span class="dv">1</span></span>
<span id="cb22-42"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-42" tabindex="-1"></a> parent.Lineage.p.list <span class="ot"><-</span> <span class="dv">1</span></span>
<span id="cb22-43"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-43" tabindex="-1"></a> }</span>
<span id="cb22-44"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-44" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">data.frame</span>(<span class="at">ID=</span>genelist[i],</span>
<span id="cb22-45"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-45" tabindex="-1"></a> <span class="at">parent.p=</span>parent.p.list,</span>
<span id="cb22-46"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-46" tabindex="-1"></a> <span class="at">Lineage.p=</span>Lineage.p.list,</span>
<span id="cb22-47"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-47" tabindex="-1"></a> <span class="at">parentXLineage.p=</span>parent.Lineage.p.list))</span>
<span id="cb22-48"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-48" tabindex="-1"></a> }</span>
<span id="cb22-49"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-49" tabindex="-1"></a> <span class="fu">return</span>(df.out)</span>
<span id="cb22-50"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb22-50" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb23-1" tabindex="-1"></a><span class="co"># Execute</span></span>
<span id="cb23-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb23-2" tabindex="-1"></a>WL.GLIMMIX <span class="ot"><-</span> <span class="fu">AST.GLIMMIX</span>(WL_counts,size_factors,<span class="dv">6</span>)</span>
<span id="cb23-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb23-3" tabindex="-1"></a>QL.GLIMMIX <span class="ot"><-</span> <span class="fu">AST.GLIMMIX</span>(QL_counts,size_factors,<span class="dv">6</span>)</span>
<span id="cb23-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb23-4" tabindex="-1"></a></span>
<span id="cb23-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb23-5" tabindex="-1"></a><span class="co"># Save output</span></span>
<span id="cb23-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb23-6" tabindex="-1"></a><span class="fu">write.csv</span>(WL.GLIMMIX,<span class="st">"AST-tutorial/ANALYSIS/RESULTS/WLGLIMMIX.csv"</span>, <span class="at">row.names=</span>F)</span>
<span id="cb23-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb23-7" tabindex="-1"></a><span class="fu">write.csv</span>(QL.GLIMMIX,<span class="st">"AST-tutorial/ANALYSIS/RESULTS/QLGLIMMIX.csv"</span>, <span class="at">row.names=</span>F)</span></code></pre></div>
</div>
</div>
</div>
<div id="allele-specific-transcription-analysis" class="section level2 hasAnchor" number="3.6">
<h2><span class="header-section-number">3.6</span> Allele-specific transcription analysis<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#allele-specific-transcription-analysis" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="fdr-correction-and-threshold-tests" class="section level3 hasAnchor" number="3.6.1">
<h3><span class="header-section-number">3.6.1</span> FDR correction and threshold tests<a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#fdr-correction-and-threshold-tests" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The False Discovery Rate (FDR) is now computed for the Storer-Kim and GLIMMIX (Wald) test results, requiring FDR < 0.05 for significance. For a gene to be considered as showing parent-of-origin or lineage-of-origin effects, all SNPs are required to exhibit the same directional bias (i.e., maternal or paternal) in the Storer-Kim test, and a strictly parent-of-origin or lineage-of-origin effect in the GLIMMIX. To avoid identifying genes with parent-of-origin effects influenced by lineage-of-origin effects, or lineage-of-origin genes influenced by parent-of-origin effects, genes with a significant interaction effect are considered unbiased.</p>
<p>Additionally, the proportions of Lineage A reads in samples with a Lineage B mother and Lineage A father (proportion 1, or <em>p1</em>), and the proportion of Lineage A reads in samples with a Lineage A mother and Lineage B father (<em>p2</em>), are subjected to a threshold test following methods described in <a href="https://doi.org/10.1038/hdy.2014.18">Wang & Clark, 2014, <em>Heredity</em></a>. Specifically, thresholds of <em>p1</em><0.4 and <em>p2</em>>0.6 are required for maternal bias, <em>p1</em>>0.6 and <em>p2</em><0.4 for paternal bias, <em>p1</em><0.4 and <em>p2</em><0.4 for lineage B bias, and <em>p1</em>>0.6 and <em>p2</em>>0.6 for lineage A bias.</p>
<p><em>Note: remember here that <code>lineage</code> in <code>metadata.csv</code> is synonymous with “maternal lineage” or “Cross” (as in Section 2). So, e.g., “lineage A” is synonymous with A-lineage mother x B-lineage father.</em></p>
<div id="function-ast.analysis" class="section level5 unnumbered hasAnchor">
<h5>Function: <code>AST.Analysis</code><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#function-ast.analysis" class="anchor-section" aria-label="Anchor link to header"></a></h5>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-1" tabindex="-1"></a>AST.Analysis <span class="ot"><-</span> <span class="cf">function</span>(counts,phenotype,SK,GLIMMIX){</span>
<span id="cb24-2"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-2" tabindex="-1"></a> <span class="co"># counts = phenotype-specific count matrix</span></span>
<span id="cb24-3"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-3" tabindex="-1"></a> <span class="co"># phenotype = corresponding phenotype of counts</span></span>
<span id="cb24-4"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-4" tabindex="-1"></a> <span class="co"># SK = object from AST.SK</span></span>
<span id="cb24-5"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-5" tabindex="-1"></a> <span class="co"># GLIMMIX = object from AST.GLIMMIX</span></span>
<span id="cb24-6"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-6" tabindex="-1"></a> </span>
<span id="cb24-7"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-7" tabindex="-1"></a> <span class="co"># Split count matrices by lineage and parent for plotting</span></span>
<span id="cb24-8"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-8" tabindex="-1"></a> counts <span class="ot"><-</span> counts[,<span class="fu">names</span>(counts)<span class="sc">%in%</span>metadata<span class="sc">$</span>sample.id]</span>
<span id="cb24-9"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-9" tabindex="-1"></a> p1.pat <span class="ot"><-</span> counts[,metadata[metadata<span class="sc">$</span>parent<span class="sc">%in%</span><span class="fu">c</span>(<span class="st">"M"</span>)<span class="sc">&</span>metadata<span class="sc">$</span>lineage<span class="sc">==</span><span class="st">"B"</span><span class="sc">&</span>metadata<span class="sc">$</span>phenotype<span class="sc">==</span>phenotype,<span class="st">"sample.id"</span>]]</span>
<span id="cb24-10"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-10" tabindex="-1"></a> p1.mat <span class="ot"><-</span> counts[,metadata[metadata<span class="sc">$</span>parent<span class="sc">%in%</span><span class="fu">c</span>(<span class="st">"F"</span>)<span class="sc">&</span>metadata<span class="sc">$</span>lineage<span class="sc">==</span><span class="st">"B"</span><span class="sc">&</span>metadata<span class="sc">$</span>phenotype<span class="sc">==</span>phenotype,<span class="st">"sample.id"</span>]]</span>
<span id="cb24-11"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-11" tabindex="-1"></a> p2.pat <span class="ot"><-</span> counts[,metadata[metadata<span class="sc">$</span>parent<span class="sc">%in%</span><span class="fu">c</span>(<span class="st">"M"</span>)<span class="sc">&</span>metadata<span class="sc">$</span>lineage<span class="sc">==</span><span class="st">"A"</span><span class="sc">&</span>metadata<span class="sc">$</span>phenotype<span class="sc">==</span>phenotype,<span class="st">"sample.id"</span>]]</span>
<span id="cb24-12"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-12" tabindex="-1"></a> p2.mat <span class="ot"><-</span> counts[,metadata[metadata<span class="sc">$</span>parent<span class="sc">%in%</span><span class="fu">c</span>(<span class="st">"F"</span>)<span class="sc">&</span>metadata<span class="sc">$</span>lineage<span class="sc">==</span><span class="st">"A"</span><span class="sc">&</span>metadata<span class="sc">$</span>phenotype<span class="sc">==</span>phenotype,<span class="st">"sample.id"</span>]]</span>
<span id="cb24-13"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-13" tabindex="-1"></a> </span>
<span id="cb24-14"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-14" tabindex="-1"></a> <span class="co"># Set up a data.frame to plot p1 and p2 for each SNP</span></span>
<span id="cb24-15"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-15" tabindex="-1"></a> p1.plot <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="fu">rowSums</span>(p1.pat)<span class="sc">/</span>(<span class="fu">rowSums</span>(p1.mat)<span class="sc">+</span><span class="fu">rowSums</span>(p1.pat)))</span>
<span id="cb24-16"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-16" tabindex="-1"></a> <span class="fu">names</span>(p1.plot) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"p1"</span>)</span>
<span id="cb24-17"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-17" tabindex="-1"></a> p1.plot[<span class="fu">is.nan</span>(p1.plot<span class="sc">$</span>p1),<span class="st">"p1"</span>] <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb24-18"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-18" tabindex="-1"></a> p2.plot <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="fu">rowSums</span>(p2.mat)<span class="sc">/</span>(<span class="fu">rowSums</span>(p2.mat)<span class="sc">+</span><span class="fu">rowSums</span>(p2.pat)))</span>
<span id="cb24-19"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-19" tabindex="-1"></a> <span class="fu">names</span>(p2.plot) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"p2"</span>)</span>
<span id="cb24-20"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-20" tabindex="-1"></a> p2.plot[<span class="fu">is.nan</span>(p2.plot<span class="sc">$</span>p2),<span class="st">"p2"</span>] <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb24-21"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-21" tabindex="-1"></a> plot <span class="ot"><-</span> <span class="fu">cbind</span>(p1.plot,p2.plot)</span>
<span id="cb24-22"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-22" tabindex="-1"></a> </span>
<span id="cb24-23"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-23" tabindex="-1"></a> <span class="co"># Join results of Storer-Kim tests</span></span>
<span id="cb24-24"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-24" tabindex="-1"></a> plot <span class="ot"><-</span> plot[<span class="fu">row.names</span>(plot)<span class="sc">%in%</span>SK<span class="sc">$</span>SNP_gene,]</span>
<span id="cb24-25"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-25" tabindex="-1"></a> plot<span class="sc">$</span>SK.p <span class="ot"><-</span> SK<span class="sc">$</span>p</span>
<span id="cb24-26"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-26" tabindex="-1"></a> plot<span class="sc">$</span>SNP_gene <span class="ot"><-</span> <span class="fu">row.names</span>(plot)</span>
<span id="cb24-27"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-27" tabindex="-1"></a> plot<span class="sc">$</span>gene <span class="ot"><-</span> <span class="fu">as.character</span>(<span class="fu">map</span>(<span class="fu">strsplit</span>(plot<span class="sc">$</span>SNP_gene, <span class="at">split =</span> <span class="st">":"</span>), <span class="dv">2</span>))</span>
<span id="cb24-28"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-28" tabindex="-1"></a> </span>
<span id="cb24-29"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-29" tabindex="-1"></a> <span class="co"># Reformat output from GLIMMMIX models</span></span>
<span id="cb24-30"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-30" tabindex="-1"></a> GLIMMIX.biased <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">gene=</span>GLIMMIX<span class="sc">$</span>ID,</span>
<span id="cb24-31"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-31" tabindex="-1"></a> <span class="at">parent.p=</span>GLIMMIX<span class="sc">$</span>parent.p,</span>
<span id="cb24-32"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-32" tabindex="-1"></a> <span class="at">Lineage.p=</span>GLIMMIX<span class="sc">$</span>Lineage.p,</span>
<span id="cb24-33"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-33" tabindex="-1"></a> <span class="at">parentXLineage.p=</span>GLIMMIX<span class="sc">$</span>parentXLineage.p)</span>
<span id="cb24-34"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-34" tabindex="-1"></a> </span>
<span id="cb24-35"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-35" tabindex="-1"></a> <span class="co"># Correct for multiple testing</span></span>
<span id="cb24-36"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-36" tabindex="-1"></a> plot<span class="sc">$</span>SK.padj <span class="ot"><-</span> <span class="fu">p.adjust</span>(plot<span class="sc">$</span>SK.p,<span class="st">"BH"</span>)</span>
<span id="cb24-37"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-37" tabindex="-1"></a> plot<span class="sc">$</span>bias <span class="ot"><-</span> <span class="st">"NA"</span></span>
<span id="cb24-38"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-38" tabindex="-1"></a> GLIMMIX<span class="sc">$</span>parent.padj <span class="ot"><-</span> <span class="fu">p.adjust</span>(GLIMMIX<span class="sc">$</span>parent.p,<span class="st">"BH"</span>)</span>
<span id="cb24-39"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-39" tabindex="-1"></a> GLIMMIX<span class="sc">$</span>Lineage.padj <span class="ot"><-</span> <span class="fu">p.adjust</span>(GLIMMIX<span class="sc">$</span>Lineage.p,<span class="st">"BH"</span>)</span>
<span id="cb24-40"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-40" tabindex="-1"></a> GLIMMIX<span class="sc">$</span>parentXLineage.padj <span class="ot"><-</span> <span class="fu">p.adjust</span>(GLIMMIX<span class="sc">$</span>parentXLineage.p,<span class="st">"BH"</span>)</span>
<span id="cb24-41"><a href="snp-level-parent-specific-read-counts-to-allele-specific-transcription-1.html#cb24-41" tabindex="-1"></a> GLIMMIX.biased <span class="ot"><-</span> GLIMMIX[GLIMMIX<span class="sc">$</span>parent.padj<span class="sc"><</span><span class="fl">0.05</span><span class="sc">|</span>GLIMMIX<span class="sc">$</span>Lineage.padj<span class="sc"><</span><span class="fl">0.05</span>,<span class="dv">1</span>]</span>