-
Notifications
You must be signed in to change notification settings - Fork 1
/
Genotyping.Rmd
4246 lines (3029 loc) · 146 KB
/
Genotyping.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Bullsharks in Texas bays & estuaries"
output:
html_notebook:
code_folding: hide
df_print: paged
highlight: kate
theme: flatly
toc: yes
---
```{r load libraries, message=FALSE, warning=FALSE}
source("scr/libraries.R")
source("scr/ggplot.R")
source("scr/VCFfilterstats.R")
source("scr/HaplotypR.R")
source("scr/xtrafunctions.R")
source("scr/genind.R")
```
# Receiving and demultiplexing libraries
## Demultiplex samples
Make sure index is in the file name of the downloaded sequences before demultiplexing; input file barcode then index; `demultiplex.txt` is in UNIX format.
### Demultiplex CLE-2
Demultiplex using only single enzyme.
```{bash demultiplex CLE-2, eval=FALSE, include=FALSE}
# create demultiplexed sequence folder
mkdir /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-2
cd /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-2
# demultiplex files
demultiplex.pl -i Demultiplex_CLE-2.txt -o Extract_CLE-2.sh -e1 sphI -p /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-2 -d /home/DATA/BULLSHARKS/Cle-2
chmod 755 Extract_CLE-2.sh
./Extract_CLE-2.sh
# delete unnecessary files generated during demultiplexing
rm sample*
```
### Demultiplex CLE-3
Demultiplex using only single enzyme.
```{bash demultiplex CLE-3, eval=FALSE, include=FALSE}
# create demultiplexed sequence folder
mkdir /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-3
cd /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-3
# demultiplex files
demultiplex.pl -i Demultiplex_CLE-3.txt -o Extract_CLE-3.sh -e1 sphI -p /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-3 -d /home/DATA/BULLSHARKS/Cle-3
chmod 755 Extract_CLE-3.sh
./Extract_CLE-3.sh
# delete unnecessary files generated during demultiplexing
rm sample*
```
## Quality control demultiplexed reads
Parse process radtags log file.
```{r parse process radtags, message=FALSE, warning=FALSE}
# if already run unattach list
rm(l)
# create empty list
l <- list()
# CLE-2 ----
# number of samples in index
index <- "CGATGT"
lib <- "CLE2"
n_samples <- 29
# read in summary per barcode
l[["CLE2-2"]] <- read_table2("data/SEQ/CLE2_CGATGT_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# number of samples in index
index <- "TGACCA"
lib <- "CLE2"
n_samples <- 29
# read in summary per barcode
l[["CLE2-4"]] <- read_table2("data/SEQ/CLE2_TGACCA_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# number of samples in index
index <- "CAGATC"
lib <- "CLE2"
n_samples <- 29
# read in summary per barcode
l[["CLE2-7"]] <- read_table2("data/SEQ/CLE2_CAGATC_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# number of samples in index
index <- "TAGCTT"
lib <- "CLE2"
n_samples <- 29
# read in summary per barcode
l[["CLE2-10"]] <- read_table2("data/SEQ/CLE2_TAGCTT_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# CLE-3 ----
# number of samples in index
index <- "CGATGT"
lib <- "CLE2"
n_samples <- 29
# read in summary per barcode
l[["CLE3-2"]] <- read_table2("data/SEQ/CLE3_CGATGT_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# number of samples in index
index <- "TGACCA"
lib <- "CLE3"
n_samples <- 29
# read in summary per barcode
l[["CLE3-4"]] <- read_table2("data/SEQ/CLE3_TGACCA_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# number of samples in index
index <- "CAGATC"
lib <- "CLE3"
n_samples <- 29
# read in summary per barcode
l[["CLE3-7"]] <- read_table2("data/SEQ/CLE3_CAGATC_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# number of samples in index
index <- "TAGCTT"
lib <- "CLE3"
n_samples <- 29
# read in summary per barcode
l[["CLE3-10"]] <- read_table2("data/SEQ/CLE3_TAGCTT_radtags.log",
skip = 13, n_max = n_samples,
col_names = c("BARCODE", "TOTAL_READS", "AMBIG_READS", "LQ_READS", "RETAINED")) %>%
mutate(PROP_RETAINED = RETAINED/TOTAL_READS,
INDEX = index,
LIBRARY = lib) %>%
select(LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS, RETAINED, AMBIG_READS, LQ_READS)
# create single data frame
radtagslog <- ldply(l, data.frame) %>%
select(-`.id`, LQ_READS) %>%
unite(LIB_IDX, LIBRARY, INDEX, sep = "_", remove = FALSE)
write_delim(radtagslog, "results/all.radtags.log")
```
Compare demultiplexed reads per library & index.
Plot distributions of total and proportion of retained reads.
```{r plot reads, fig.height=18, fig.width=6, message=TRUE, warning=TRUE}
radtagslog %>%
select(LIB_IDX, LIBRARY, INDEX, BARCODE, PROP_RETAINED, TOTAL_READS) %>%
gather(key = STAT, value = READS, 5:6) %>%
ggplot(aes(x = READS)) +
geom_histogram(color = "black", fill = "darkorange") +
labs(x = "reads") +
facet_grid(LIB_IDX ~ STAT, scales = "free") +
theme_standard
```
# Reference construction
Values chosen for MiSeq Reference:
* **c** = 0.8
* **K1** = 5
* **K2** = 6
# Read mapping
## Map reads using BWA (in **dDocent** pipeline)
Transfer copy of `reference.fasta` and associated files for K1 = 5 and K2 = 6 into each directory containing demultiplexed and quality trimmed sequences
Any renaming of files needs to happen before `fastq`-files are mapped. The population designation (before underscore) is used by `FreeBayes` to call SNPs so it is important that population designation make biological sense. All files should be named `POP_PLATE-WELL_SAMPLENAMES`.
Run `dDocent` from within each Library directory to map reads to `reference.fasta`.
```{bash run dDocent, eval=FALSE, include=FALSE}
cd /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-2
dDocent
cd /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-3
dDocent
```
## QA/QC read mapping
### Query mapping statistics
During the mapping stage, `dDocent` calls `BWA` to map reads from the individuals in the folder to the generated MiSeqReference and create a `-RG.bam`-file for each individual. The second column of a BAM (or SAM) file contains FLAGs with binary encoded information on mapping, pairedness etc. that can be used to compare the mapping efficiency of the generated MiSeq references.
Count number of reads and mapped reads using `samtools idxstats <aln-RG.bam>` which will retrieve and print stats in the bam-file. The output is TAB-delimited with each line consisting of reference sequence name, sequence length, # mapped reads and # unmapped (empty) reads. `samtools` can also be be used to query `samtools flagstat file.bam` which returns an output containing the number of reads for which each flag is true.
#### Run Flagstats
dDocent writes out a file called `bamlist.list` that contains all the bam files that were generated during read mapping in `dDocent` using `BWA`. Write script to gather flagstats from all `bam`-files.
```{r flagstats script}
# write script to gather flagstats
l <- list()
l[["CLE2"]] <- read_table2("data/SEQ/CLE-2/bamlist.list", col_names = "BAM") %>%
mutate(PATH = "data/SEQ/CLE-2/",
COMMAND = "samtools flagstat",
OUT = ">> data/SEQ/CLE2.flagstats") %>%
select( COMMAND, PATH, BAM, OUT) %>%
unite(FILE, 2:3, sep = "")
l[["CLE3"]] <- read_table2("data/SEQ/CLE-3/bamlist.list", col_names = "BAM") %>%
mutate(PATH = "data/SEQ/CLE-3/",
COMMAND = "samtools flagstat",
OUT = ">> data/SEQ/CLE3.flagstats") %>%
select( COMMAND, PATH, BAM, OUT) %>%
unite(FILE, 2:3, sep = "")
bam <- ldply(l, data.frame) %>%
select(-`.id`)
write_delim(bam, "scr/flagstats.sh", delim = "\t", col_names = FALSE)
```
Run flagstats.
```{bash run flagstats, eval=FALSE, include=FALSE}
chmod 755 scr/flagstats.sh
./scr/flagstats.sh
```
#### Run idxstats
Write script to gather idxstats from all `bam`-files.
```{r script idxstats}
# write script to gather idxstats
l <- list()
l[["CLE2"]] <- read_table2("data/SEQ/CLE-2/bamlist.list", col_names = "BAM") %>%
mutate(PATH = "data/SEQ/CLE-2/",
COMMAND = "samtools idxstats",
OUT = ">> data/SEQ/CLE2.idxstats") %>%
select( COMMAND, PATH, BAM, OUT) %>%
unite(FILE, 2:3, sep = "")
l[["CLE3"]] <- read_table2("data/SEQ/CLE-3/bamlist.list", col_names = "BAM") %>%
mutate(PATH = "data/SEQ/CLE-3/",
COMMAND = "samtools idxstats",
OUT = ">> data/SEQ/CLE3.idxstats") %>%
select( COMMAND, PATH, BAM, OUT) %>%
unite(FILE, 2:3, sep = "")
bam <- ldply(l, data.frame) %>%
select(-`.id`)
write_delim(bam, "scr/idxstats.sh", delim = "\t", col_names = FALSE)
```
Run indxstats.
```{bash run idxstats, eval=FALSE, include=FALSE}
chmod 755 scr/idxstats.sh
./scr/idxstats.sh
```
### Format stats files into tidy data sets
Appending the file results in the information per individual being printed in a new set of row being appended to the file, i.e. there will be as many rows for a given locus as individuals were mapped. The file can be re-formatted and summary statistics calculated using dplyr and tidyr.
Format idxstats:
```{r format idxstats}
# create vectors of files to be imported, reference codes, K1 and K2, dataframe names
filenames <- list.files(path = "data/SEQ", pattern = "*.idxstats")
names <- substr(filenames, 1, 8)
lib <- substr(filenames, 1, 4)
# import data
for (i in names){
filepath <- file.path("data/SEQ", paste(i, 'stats', sep =""))
assign(i, read.table(filepath, sep = "", header = FALSE,
col.names = c("Locus", "Length", "Reads_Mapped", 'blank')) %>%
select(1:3))
}
# # make sure to delete old list if rerunning the code
# rm(dflist_idx)
# rm(MapStats.idx)
# Create list of one dataframe per idxstats file and group by locus
dflist_idx <- lapply(ls(pattern = "*.idx"), get)
for (df in 1:length(dflist_idx)){
x <- dflist_idx[[df]]
x[['Locus']] <- as.character(x[['Locus']])
x = x %>% group_by(Locus)
dflist_idx[[df]] <- x
}
# Create new dataframes with summary stats per library and bind into final output/dataframe
MapStats.idx <- data.frame()
for (df in 1:length(dflist_idx)){
x = summarize(dflist_idx[[df]],
Length = mean(Length),
Mean_Mapped = mean(Reads_Mapped),
Sum_Mapped = sum(Reads_Mapped),
Min_Mapped = min(Reads_Mapped),
Max_Mapped = max(Reads_Mapped),
SD_Mapped = sd(Reads_Mapped))
x[x == 0] <- NA
temp <- summarize(x, Mean_Mapped_Non0 = mean(Mean_Mapped, na.rm = TRUE)) %>%
mutate(Lib = lib[df],
Not_Mapped = nrow(filter(x, is.na(Sum_Mapped))),
N_Loci_Ref = nrow(x)) %>%
select(Lib, N_Loci_Ref, Not_Mapped, Mean_Mapped_Non0)
MapStats.idx <- bind_rows(MapStats.idx, temp)
}
MapStats.idx <- MapStats.idx %>%
mutate(PROP_EMPTY = round(Not_Mapped/N_Loci_Ref*100, digits = 2),
CONTIGS_MAPPED = N_Loci_Ref - Not_Mapped)
write.table(MapStats.idx, "results/MapStats.idx", quote = FALSE)
# remove large (duplicate) files
rm(CLE2.idx)
rm(CLE3.idx)
MapStats.idx
```
Format flagstats
```{r format flagstats}
# Files to be imported
filenames <- list.files(path='data/SEQ', pattern = '*.flagstats')
# create vectors of files to be imported
names <- substr(filenames, 1, 9)
lib <- substr(filenames, 1, 4)
# import data
for (i in names){
filepath <- file.path('data/SEQ', paste(i, 'stats', sep =""))
assign(i, read.csv(filepath, sep = "+", header = FALSE,
col.names = c("N_Reads", "CAT"),
stringsAsFactors = FALSE) %>%
select(1:2))
}
# Create list of one dataframe per flagstats file and create tidy data set
# should be 3 elements/libraries
rm(dflist_flag)
dflist_flag <- lapply(ls(pattern = "*flag"), get)
# Change N_Reads to numeric
for (df in 1:length(dflist_flag)){
x <- dflist_flag[[df]]
x[['N_Reads']] <- as.numeric(x[['N_Reads']])
dflist_flag[[df]] <- x
}
for (df in 1:length(dflist_flag)){
x <- dflist_flag[[df]]
n <- nrow(x)/14
x <- x %>%
filter(grepl("0 mapped|properly paired|mapQ>=5", CAT)) %>%
mutate(MAPSTAT = ifelse(grepl("mapQ>=5", CAT), "Mismatch",
ifelse(grepl("properly", CAT), "Prop_Paired", "Mapped"))) %>%
mutate(Ind = c(rep(1:n, each = 3))) %>%
# not sure if extra individual in there somehow
select(4, 3, 1) %>%
spread(MAPSTAT, N_Reads)
dflist_flag[[df]] <- x
}
# Create new dataframes with summary stats and add to main final data frame
MapStats.flag <- data.frame()
for (df in 1:length(dflist_flag)){
x = summarize(dflist_flag[[df]], Sum_Mapped = sum(Mapped),
Reads_Mapped = mean(Mapped),
Sum_Paired = sum(Prop_Paired),
Mean_Paired = mean(Prop_Paired),
Sum_Mismatch = sum(Mismatch),
Mean_Mismatch = mean(Mismatch)) %>%
mutate(Lib = lib[df]) %>%
select(7, 1:6)
MapStats.flag <- bind_rows(MapStats.flag, x)
}
# write to file
write.table(MapStats.flag, "results/MapStats.flag", quote = FALSE)
MapStats.flag
# combine files
mapstats <- left_join(MapStats.idx, MapStats.flag) %>%
mutate(PROP_MISMATCH = Sum_Mismatch/Sum_Mapped)
# write summary stats file
write.table(mapstats, file = "results/BWA_mapping.stats", quote = FALSE, sep = " ")
```
### Evaluate & compare mapping results
Compare number of reference contigs for which no reads mapped to per library.
```{r plot mapstats, fig.height=4, fig.width=12}
# plot no of loci vs "empty" loci
p1 <- ggplot(mapstats, aes(x = Lib, y = PROP_EMPTY)) +
geom_bar(stat = "identity", color = "black", fill = "darkorange") +
scale_y_continuous(limits = c(0, 6)) +
labs(x = "", y = "% contigs w/no reads mapped") +
theme_standard
p2 <- ggplot(mapstats, aes(x = Lib, y = Reads_Mapped)) +
geom_bar(stat = "identity", color = "black", fill = "darkorange") +
labs(x = "library", y = "mean reads mapped per indv") +
theme_standard
p3 <- ggplot(mapstats, aes(x = Lib, y = PROP_MISMATCH)) +
geom_bar(stat = "identity", color = "black", fill = "darkorange") +
labs(x = "",y = "% reads not mapped as pair") +
theme_standard
multiplot(p1, p2, p3, cols = 3)
```
# SNP calling
Transfer copy of `reference.fasta` and associated files for K1 = 4 and K2 = 1 into SNP calling directory.
Create symlinks from all `fq`, `bam` and `bam.bai`-files for each separately mapped library in `SNP_Calling` folder.
```{bash softlink files, eval=FALSE, include=FALSE}
ln -s /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-2/*.fq.gz /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling
ln -s /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-2/*.bam* /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling
ln -s /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-3/*.fq.gz /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling
ln -s /home/soleary/BULLSHARKS/CLE_POPGEN/data/SEQ/CLE-3/*.bam* /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling
# remove unnecessary files
cd /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling
rm cat-RRG.bam*
```
Execute `dDocent` from within `SNP_Calling`-folder to call variants across all individuals (all libraries).
```{bash call SNPs, eval=FALSE, include=FALSE}
cd /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling
dDocent
```
File `TotalrawSNPs.vcf` contains all raw SNP/INDEL calls. Do not need to keep links of `fq.gz`-, `bam`-, `.bam.bai`-files after SNPs have been called. Copy `TotalRaSNPs.vcf` to `VCF_Filtering` for SNP filtering.
```{bash copy TotalRawSNPs for filtering, eval=FALSE, include=FALSE}
cd /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling
rm *fq.gz *bam*
cp /home/soleary/BULLSHARKS/CLE_POPGEN/data/SNP_Calling/TotalRawSNPs.vcf /home/soleary/BULLSHARKS/CLE_POPGEN/data/VCF/temp/
```
# SNP filtering
dDocent uses FreeBayes to call SNPs and write a VCF-file `TotalrawSNPs.vcf`. This data set was filtered to remove low quality and artefactual SNP sites, paralogs and low quality individuals based on levels of missing data, minimum/maximum read depth, genotype call rate, and minor allele frequencies. Contigs may contain more than one SNP; the script `rad_haplotyper.pl` was used to create haplotypes for each locus.
## Raw data
### Individuals/Populations sampled
Generate a list of all individuals included in the SNP data set called by FreeBayes in the dDocent pipeline.
```{bash write raw indv}
vcfsamplenames data/VCF/temp/TotalRawSNPs.vcf > data/VCF/raw.ind
```
Use `Ind_raw` file to write text files of individuals in each library and in regional groupings.
```{r create Ind files, message=FALSE, warning=FALSE}
# import individuals in raw data set ----
Ind_RAW <- read.table("data/VCF/raw.ind",
header = FALSE, stringsAsFactors = FALSE,
col.names = "LIB_ID") %>%
separate(LIB_ID, into = c("SP", "WELL", "SAMPLE_ID"), sep = "_", remove = FALSE, extra = "merge")
# View(Ind_RAW)
# identify duplicates
temp <- Ind_RAW %>%
group_by(SAMPLE_ID) %>%
count() %>%
filter(n > 1) %>%
select(SAMPLE_ID)
dup <- Ind_RAW %>%
filter(SAMPLE_ID %in% temp$SAMPLE_ID) %>%
select(LIB_ID)
# View(temp)
write.table(dup, "data/VCF/duplicate.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
# create files with individuals by population ----
SampleInfo <- read_delim("data/POPGEN/SampleInfo.txt", delim = "\t")
Ind_RAW <- left_join(Ind_RAW, SampleInfo)
temp <- Ind_RAW %>%
filter(POP == "ARA") %>%
select(LIB_ID)
write.table(temp, "data/VCF/ARA.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
temp <- Ind_RAW %>%
filter(POP == "CC") %>%
select(LIB_ID)
write.table(temp, "data/VCF/CC.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
temp <- Ind_RAW %>%
filter(POP == "GAL") %>%
select(LIB_ID)
write.table(temp, "data/VCF/GAL.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
temp <- Ind_RAW %>%
filter(POP == "MAT") %>%
select(LIB_ID)
write.table(temp, "data/VCF/MAT.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
temp <- Ind_RAW %>%
filter(POP == "SA") %>%
select(LIB_ID)
write.table(temp, "data/VCF/SA.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
temp <- Ind_RAW %>%
filter(POP == "SL") %>%
select(LIB_ID)
write.table(temp, "data/VCF/SL.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
# create files with individuals per library ----
temp <- Ind_RAW %>%
filter(grepl("2A", WELL) |
grepl("2B", WELL)) %>%
select(LIB_ID)
write.table(temp, "data/VCF/CLE2.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
temp <- Ind_RAW %>%
filter(grepl("3A", WELL) |
grepl("3B", WELL)) %>%
select(LIB_ID)
write.table(temp, "data/VCF/CLE3.ind",
col.names = FALSE, row.names = FALSE, quote = FALSE)
```
### Compare Individual & SNP stats
Use `VCFtools` to create stats files for depth, missing data, heterozygosity and site quality for the raw data.
```{bash query raw stats}
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/CLE_raw --depth
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/CLE_raw --site-mean-depth
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/CLE_raw --site-quality
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/CLE_raw --missing-indv
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/CLE_raw --missing-site
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/CLE_raw --het
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/CLE_raw --singletons
```
Compare raw stats.
```{r stats raw, fig.height=20, fig.width=10, message=FALSE, warning=FALSE}
# load stats files ----
ind_stats_raw <- read.ind.stats(dir = "data/VCF", vcf = "CLE_raw") %>%
separate(INDV, into = c("SP", "LIB", "SAMPLE_ID"), sep = "_", remove = FALSE)
loc_stats_raw <- read.loc.stats(dir = "data/VCF/", vcf = "CLE_raw")
# plot missing data per indv ----
p1 <- ggplot(ind_stats_raw, aes(x = MISS_CLE_raw)) +
geom_histogram(binwidth = .01, color = "black", fill = "darkorange") +
geom_vline(aes(xintercept = mean(MISS_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 0.5),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "missing data per indv") +
theme_standard
# plot read depth per indv ----
p2 <- ggplot(ind_stats_raw, aes(x = MEAN_DEPTH_CLE_raw)) +
geom_histogram(binwidth = 10, color = "black", fill = "darkorange") +
geom_vline(aes(xintercept = mean(MEAN_DEPTH_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 20),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "mean read depth per indv") +
theme_standard
# plot depth vs missing ----
p3 <- ggplot(ind_stats_raw, aes(x = MEAN_DEPTH_CLE_raw, y = MISS_CLE_raw)) +
geom_point() +
geom_vline(aes(xintercept = mean(MEAN_DEPTH_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 20),
color = "darkblue", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = mean(MISS_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = 0.5),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "mean depth per indv", y = "% missing data") +
theme_standard
# plot Fis per indv ----
p4 <- ggplot(ind_stats_raw, aes(x = Fis_CLE_raw)) +
geom_histogram(binwidth = .01, color = "black", fill = "darkorange") +
geom_vline(aes(xintercept = mean(Fis_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 0),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "Fis per indv") +
theme_standard
# plot Fis vs missing data per indv ----
p5 <- ggplot(ind_stats_raw, aes(x = Fis_CLE_raw, y = MISS_CLE_raw)) +
geom_point() +
geom_vline(aes(xintercept = mean(Fis_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 0),
color = "darkblue", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = mean(MISS_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = 0.5),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "Fis per indv", y = "% missing data") +
theme_standard
# plot Fis vs mean depth per indv ----
p6 <- ggplot(ind_stats_raw, aes(x = Fis_CLE_raw, y = MEAN_DEPTH_CLE_raw)) +
geom_point() +
geom_vline(aes(xintercept = mean(Fis_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 0),
color = "darkblue", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = mean(MEAN_DEPTH_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = 20),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "Fis per indv", y = "mean depth per indv") +
theme_standard
# plot distribution missing data per locus ----
p7 <- ggplot(loc_stats_raw, aes(x = MISS_CLE_raw)) +
geom_histogram(binwidth = 0.01, color = "black", fill = "darkorange") +
geom_vline(aes(xintercept = mean(MISS_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 0.1),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "% missing data per locus") +
theme_standard
# plot distribution mean read depth ----
p8 <- ggplot(loc_stats_raw, aes(x = MEAN_DEPTH_CLE_raw)) +
geom_histogram(binwidth = 20, color = "black", fill = "darkorange") +
geom_vline(aes(xintercept = mean(MEAN_DEPTH_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 20),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "mean read depth per locus") +
theme_standard
# plot read depth vs missing data ----
p9 <- ggplot(loc_stats_raw, aes(x = MEAN_DEPTH_CLE_raw, y = MISS_CLE_raw)) +
geom_point() +
geom_vline(aes(xintercept = mean(MEAN_DEPTH_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 20),
color = "darkblue", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = mean(MISS_CLE_raw, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = 0.1),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "mean depth per locus", y = "% missing data") +
theme_standard
# plot depth vs SNP quality ----
site_qual <- read.table("data/VCF/CLE_raw.lqual",
header = TRUE, stringsAsFactors = FALSE) %>%
mutate(PROB = 10^(-QUAL/10))
temp <- data.frame(loc_stats_raw$MEAN_DEPTH_CLE_raw, site_qual$QUAL) %>%
rename(depth = loc_stats_raw.MEAN_DEPTH_CLE_raw, qual = site_qual.QUAL)
p10 <- ggplot(temp, aes(x = depth, y = qual)) +
geom_point(size = 1) +
geom_vline(aes(xintercept = mean(depth, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = 20),
color = "darkblue", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = mean(qual, na.rm = TRUE)),
color = "red", linetype = "dashed", size = 1) +
geom_hline(aes(yintercept = 20),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "mean depth per locus", y = "SNP quality") +
theme_standard
# plot number of SNPs per contig vs. mean depth ----
temp <- loc_stats_raw %>%
count(CHR)
p11 <- left_join(temp, loc_stats_raw) %>%
ggplot() +
geom_point(aes(x = n, y = MEAN_DEPTH_CLE_raw)) +
labs(x = "number of SNPs per contig", y = "mean depth") +
theme_standard
# plot no of SNPs per locus ----
p12 <- loc_stats_raw %>%
count(CHR) %>%
ggplot(aes(x = n)) +
geom_histogram(binwidth = 1, color = "black", fill = "darkorange") +
labs(x = "number of SNPs per locus") +
theme_standard
mraw <- multiplot(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, cols=2)
```
## Choose threshold values for quality score, coverage, missing data, minor alleles and mapping/variant calling artifacts
### Filter 0: Remove LQ individuals
Remove (known) low quality individuals from data set:
Identify low quality individuals to remove from the data set; defined as individuals with a mean coverage of three or less reads across all loci and more than 85% missing data:
```{r lq indv}
LQindv <- ind_stats_raw %>%
filter(MEAN_DEPTH_CLE_raw < 5 | MISS_CLE_raw >= 0.75) %>%
select(INDV)
# View(LQindv)
write_delim(LQindv, "data/VCF/LQ_raw.ind", delim = "\t")
```
Remove low quality individuals and decompose indels.
```{bash filter lq indv, eval=FALSE, include=FALSE}
# decompose indels
vcfallelicprimitives data/VCF/temp/TotalRawSNPs.vcf --keep-info --keep-geno > data/VCF/temp/CLE.prim.vcf
# retain only SNPs / remove LQ individuals
vcftools --vcf data/VCF/temp/CLE.prim.vcf --out data/VCF/temp/CLE.F0 --remove-indels --remove data/VCF/LQ_raw.ind --recode --recode-INFO-all
# get duplicates stats
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/F0 --keep data/VCF/duplicate.ind --012
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/F0 --keep data/VCF/duplicate.ind --geno-depth
vcftools --vcf data/VCF/temp/TotalRawSNPs.vcf --out data/VCF/F0 --keep data/VCF/duplicate.ind --singletons
# query stats
vcftools --vcf data/VCF/temp/CLE.F0.recode.vcf --out data/VCF/CLE.F0 --depth
vcftools --vcf data/VCF/temp/CLE.F0.recode.vcf --out data/VCF/CLE.F0 --site-mean-depth
vcftools --vcf data/VCF/temp/CLE.F0.recode.vcf --out data/VCF/CLE.F0 --missing-indv
vcftools --vcf data/VCF/temp/CLE.F0.recode.vcf --out data/VCF/CLE.F0 --missing-site
vcftools --vcf data/VCF/temp/CLE.F0.recode.vcf --out data/VCF/CLE.F0 --het
vcftools --vcf data/VCF/temp/CLE.F0.recode.vcf --out data/VCF/CLE.F0 --singletons
vcftools --vcf data/VCF/temp/CLE.F0.recode.vcf --out data/VCF/CLE.F0 --geno-depth
```
Compare stats:
```{r stats F0, fig.height=20, fig.width=10, message=TRUE, warning=TRUE}
# load stats files ----
ind_stats_F0 <- read.ind.stats(dir = "data/VCF", vcf = "CLE.F0") %>%
separate(INDV, into = c("SP", "LIB", "SAMPLE_ID"), sep = "_", remove = FALSE, extra = "merge")
loc_stats_F0 <- read.loc.stats(dir = "data/VCF/", vcf = "CLE.F0")
```
Import singletons and genotype depth file to create list of loci to exclude.
```{r read singleton genotype depth, message=FALSE, warning=FALSE}
# number of individuals
n <- nrow(ind_stats_F0)+2
# read singletons file
singletons <- read_table2("data/VCF/CLE.F0.singletons") %>%
mutate(VARIANT = ifelse(ALLELE %in% c("A", "T", "C", "G"), "SNP", "INDEL"))
chrom <- unique(singletons$CHROM)
# read genotype depth file and join with singletons
gdepth <- read_table2("data/VCF/CLE.F0.gdepth") %>%
filter(CHROM %in% chrom) %>%
gather(key = INDV, value = DEPTH, 3:n)
singletons <- left_join(singletons, gdepth)
```
Data set contains `nrow(singletons)` singletons.
```{r doubletons}
# filter doubletons
doubletons <- singletons %>%
filter(`SINGLETON/DOUBLETON` == "D")
```
Of those `nrow(doubletons)` are called as a homozygote in one individual.
```{r singletons per contig, fig.height=3, fig.width=4}
# number of contigs in data set
contigs_total <- loc_stats_F0 %>%
distinct(CHROM)
contigs_singletons <- singletons %>%
distinct(CHROM)
contigs <- singletons %>%
count(CHROM)
ggplot(contigs, aes(x = n)) +
geom_histogram(binwidth = 1, color = "black", fill = "darkorange") +
geom_vline(aes(xintercept = mean(n, na.rm = TRUE)),
color = "darkblue", linetype = "dashed", size = 1) +
labs(x = "number of singletons per locus") +
theme_standard
```
The data set contains `nrow(contigs_total)` contigs, `nrow(contigs_singletons)` (`2round( (nrow(contigs_singletons)/nrow(contigs_total)*100), digits = 2)`%) contain singletons.
```{r distribution singpletons per contig, fig.height=3, fig.width=6}
contigs <- singletons %>%
group_by(`SINGLETON/DOUBLETON`) %>%
count(CHROM)
ggplot(contigs, aes(x = n)) +
geom_histogram(binwidth = 1, color = "black", fill = "darkorange") +
facet_grid(. ~ `SINGLETON/DOUBLETON`) +
labs(x = "number of doubletons/singeltons per locus") +
theme_standard
```
Target is 20 reads per locus; identify number of singletons/doubletons with depth < 10 reads.
```{r}
count(singletons, DEPTH <= 5)
```
Distriubtion of genotype depth per singleton/doubleton.
```{r distribution geno depth per singleton, fig.height=3, fig.width=6}
ggplot(singletons, aes(x = DEPTH)) +
geom_histogram(binwidth = 20, color = "black", fill = "darkorange") +
facet_grid(. ~ `SINGLETON/DOUBLETON`, scales = "free") +
labs(x = "read depth") +
scale_y_sqrt() +
theme_standard
```
Quantiles distribution of read depth for SNP loci called in only one individual.
```{r quantile singleton depth}
quantile(singletons$DEPTH, probs = c(.05, .25, .5, .75, .95, .99))