-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal MLMs and QTL Analysis HF.Rmd
1426 lines (1155 loc) · 41.4 KB
/
Final MLMs and QTL Analysis HF.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: "Final MLMs and QTL Analysis"
author: "Zachary Winn"
date: "3/26/21"
output:
html_document: default
pdf_document: default
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(scipen = 999)
```
# Mixed Linear Models
In this section of the code, I am running a series of mixed linear models to derive BLUEs for each environment and across all environments.
## Read in the Data
Here I read in the data and make sure all variables are of the correct class. The data is of sub-samples taken from a population of $F_{4:5}$ infested with Hessian fly in the Fall of 2019, 2020, and 2021. The following environments were recorded in the following field designs:
```{r Site-Years, echo=FALSE}
library(knitr)
a<-c()
a<-rbind(a, c("Cunningham", "Kinston", "NC", "2018-2019", 1, "Augmented Block"))
a<-rbind(a, c("Cunningham", "Kinston", "NC", "2019-2020", 2, "Randomized Complete Block"))
a<-rbind(a, c("Cunningham", "Kinston", "NC", "2020-2021", 1, "Augmented Block"))
a<-rbind(a, c("Caswell", "Kinston", "NC", "2019-2020", 2, "Randomized Complete Block"))
a<-rbind(a, c("Hugo", "Hugo", "NC", "2020-2021", 2, "Randomized Complete Block"))
a<-as.data.frame(a)
colnames(a)<-c("Station", "City", "State", "Season", "Replications", "Design")
kable(a)
remove(a)
```
The following traits were measured:
- Infested Tillers (IT): Number of Hessian fly infested tillers from a sub-sample of a plot
- Total Tillers (TT): Number of total tillers taken in a sub-sample of a plot
- Number of Larvae/Pupae (NOP): Number of larvae/pupae in a sub-sample of a plot
- Percent Infested Tillers (PIT): $PIT=\frac{IT}{TT}$
- Number of Larvae/Pupae per Tiller (NOPPT): $PIT=\frac{NOP}{TT}$
- Number of Larvae/Pupae per Infested Tiller (NOPPIT): $PIT=\frac{NOP}{IT}$
- Normalized Difference Vegetative Index (NDVI): $NDVI=\frac{NIR-Red}{NIR+Red}$ a ratio of near infrared (NIR) and red spectra taken in a single site year via a multi-spectral camera mounted to a tractor (Cunningham 2019-2020)
**Here we will only analyze PIT, NOPPT, and NOPPIT**
```{r Data Read In, results='hide'}
#library
library(asreml)
library(tidyverse)
library(readxl)
library(knitr)
library(psych)
#read in data
rm(list = ls())
setwd("C:/Users/zwinn/OneDrive/North Carolina State University/Dissertation/Hessian Fly QTL Mapping Project/Final Directory and Paper")
hfpop<-read_excel("2018_2019_2020_2021_HFPOP_site_years.xlsx",
sheet = "All_Site_Years",
na="NA")
hfpop<-as.data.frame(hfpop)
#set data classes
hfpop[,1:8]<-lapply(hfpop[,1:8], as.factor)
hfpop[,9:ncol(hfpop)]<-lapply(hfpop[,9:ncol(hfpop)], as.numeric)
hfpop<-hfpop %>%
mutate(PIT=as.numeric(round(IT/TT, 2)),
NOPPT=as.numeric(round(NOP/TT, 2)),
NOPPIT=round(ifelse(IT==0, 0, NOP/IT),2)) %>%
select(Environment, Year, Location,
Rep, Genotype, Entry,
Tray, Row, Plot,
VR, TT, IT,
NOP, PIT, NOPPT,
NOPPIT, NDVI)
lapply(hfpop, class)
```
## Check normality assumptions
Here I look at the data to see if it is very skewed and requires some form of transformation or a GLMM.
```{r Check Normal Plots, echo=FALSE}
traits<-colnames(hfpop)[c(14:16)]
for(i in traits){
hist(hfpop[[i]], main=i, xlab=i)
boxplot(hfpop[[i]], main=i)
}
```
## Run the models within locations
Now I am going to get adjusted means from each environment to run in a QTL analysis. Results will be at the end of the file.
```{r Within Models}
env<-levels(hfpop$Environment)
mlms<-list()
cv <- function(trait_val) {
mean_of_trait <- mean(trait_val, na.rm=T)
sd_of_trait <- sd(trait_val, na.rm=T)
cv_value <- (sd_of_trait/mean_of_trait)*100
return(cv_value)
}
for (j in env){
for (i in traits){
#Parse out location
location<-hfpop %>% filter(Environment==j)
location<-as.data.frame(location)
#Announce Location Trait Combination
print(paste("------- Analyzing trait", i, "in", j,"-------"))
if(length(unique(is.na(location[,i])))==1){
#Announce decision
print(paste(i, " was not taken in ", j, "; aborting loop.", sep = ""))
}else{
#Check Design
if(j=="WIL19"){
#Announce decision
print(paste(j, "was unreplicated in augmented design, runing augmented block model"))
#drop NA
location<-location %>% drop_na(i)
#Run Model
mlm <- asreml(fixed = location[,i]~1+Entry,
random = ~Tray,
residual = ~units,
data=location,
family= asr_inverse.gaussian())
#Print Anova Table
print(kable(wald(mlm), caption = paste("ANOVA Tabel for", i, "in", j)))
#Print Fit Criteria
crits<-as.data.frame(rbind(summary(mlm)$loglik,summary(mlm)$aic, summary(mlm)$bic))
rownames(crits)<-c("LogLik","AIC","BIC")
colnames(crits)<-"Value"
print(kable(crits, caption=paste("Fit Criteria Tabel for", i, "in", j)))
#Print CV
print("CV Expressed as a Percent")
print(cv(trait_val = location[,i]))
#Print Residuals
plot(resid(mlm),main =paste(i, j))
i=(paste(i,"_",j,sep = ""))
mlms[[i]]<-mlm
remove(crits,mlm,location)
}else
if(j=="CUN21"){
#Announce decision
print(paste(j, "was unreplicated in augmented design, runing augmented block model"))
#drop NA
location<-location %>% drop_na(i)
#Run Model
mlm <- asreml(fixed = location[,i]~1+Entry,
random = ~Tray,
residual = ~units,
data=location,
family= asr_inverse.gaussian())
#Print Anova Table
print(kable(wald(mlm), caption = paste("ANOVA Tabel for", i, "in", j)))
#Print Fit Criteria
crits<-as.data.frame(rbind(summary(mlm)$loglik,summary(mlm)$aic, summary(mlm)$bic))
rownames(crits)<-c("LogLik","AIC","BIC")
colnames(crits)<-"Value"
print(kable(crits, caption=paste("Fit Criteria Tabel for", i, "in", j)))
#Print CV
print("CV Expressed as a Percent")
print(cv(trait_val = location[,i]))
#Print Residuals
plot(resid(mlm),main =paste(i, j))
i=(paste(i,"_",j,sep = ""))
mlms[[i]]<-mlm
remove(crits,mlm,location)
}else{
#Announce decision
print(paste(j, "was replicated in RCBD, runing RCBD model"))
#Run Model
mlm <- asreml(fixed = location[,i]~1+Entry,
random = ~Rep,
residual = ~units,
data=location,
family= asr_inverse.gaussian())
#Print Anova Table
print(kable(wald(mlm), caption = paste("ANOVA Tabel for", i, "in", j)))
#Print Fit Criteria
crits<-as.data.frame(rbind(summary(mlm)$loglik,summary(mlm)$aic, summary(mlm)$bic))
rownames(crits)<-c("LogLik","AIC","BIC")
colnames(crits)<-"Value"
print(kable(crits, caption=paste("Fit Criteria Tabel for", i, "in", j)))
#Print CV
print("CV Expressed as a Percent")
print(cv(trait_val = location[,i]))
#Print Residuals
plot(resid(mlm),main =paste(i, j))
i=(paste(i,"_",j,sep = ""))
mlms[[i]]<-mlm
remove(crits,mlm,location)
}
}
}
}
#pull out genotype fixed effect estimates
#add them to population mean effect intercept
BLUEs_loc<-data.frame(unique(hfpop$Entry))
colnames(BLUEs_loc)<-"Entry"
for (i in names(mlms)){
a<-mlms[[i]]
beta_hat<-a$coefficients$fixed
beta_0<-beta_hat[grep("(Intercept)", rownames(beta_hat))]
beta_1<-beta_hat[grep("Entry", rownames(beta_hat))]
BLUEs<-as.data.frame(beta_0+beta_1)
rownames(BLUEs)<-gsub("Entry_", "", rownames(beta_hat)[1:nrow(beta_hat)-1])
colnames(BLUEs)<-i
BLUEs<-rownames_to_column(BLUEs, var="Entry")
BLUEs_loc<-left_join(BLUEs_loc, BLUEs, by="Entry")
remove(a, beta_hat, beta_0, beta_1, BLUEs)
}
#remove the NA vectors from the dataframe
for(i in colnames(BLUEs_loc)){
if(sum(is.na(BLUEs_loc[,i]))==length(BLUEs_loc[,i])){
BLUEs_loc<-BLUEs_loc %>%
select(-i)
}else{
#do nothing
}
}
```
## Run the models across all locations
Things seem to check out mostly, although transformation would be a good idea for some of the very skewed data. Lets run the models and see what happens. Results will be at the end of the file.
```{r Across Models}
BLUEs_all<-list()
for (i in traits){
#Run Model
mlm<-asreml(fixed = hfpop[,i]~1+Entry,
random = ~(Location)+
(Year)+
(Rep)+
(Entry:Location)+
(Entry:Year)+
(Year:Location)+
(Entry:Year:Location),
residuals = ~(units),
data = hfpop,
maxit=75,
family= asr_inverse.gaussian())
#Print Anova Table
print(kable(wald(mlm), caption = paste("ANOVA Tabel for", i, "in", j)))
print(summary(mlm))
#Print Fit Criteria
crits<-as.data.frame(rbind(summary(mlm)$loglik,summary(mlm)$aic, summary(mlm)$bic))
rownames(crits)<-c("LogLik","AIC","BIC")
colnames(crits)<-"Value"
print(kable(crits, caption=paste("Fit Criteria Tabel for", i, "in", j)))
#Print Residuals
plot(resid(mlm),main =paste("Multienvironment",i))
#Pull out estimates
beta_hat<-mlm$coefficients$fixed
beta_0<-beta_hat[grep("(Intercept)", rownames(beta_hat))]
beta_1<-beta_hat[grep("Entry", rownames(beta_hat))]
p<-as.data.frame(beta_0+beta_1)
rownames(p)<-gsub("Entry_", "", rownames(beta_hat)[1:nrow(beta_hat)-1])
colnames(p)<-i
p<-rownames_to_column(p, var="Entry")
i=(paste(i,"_ME",sep = ""))
BLUEs_all[[i]]<-p
remove(p,crits,mlm,beta_hat, beta_0, beta_1, BLUEs)
}
q<-data.frame(unique(hfpop$Entry))
colnames(q)<-"Entry"
for (i in names(BLUEs_all)){
a<-as.data.frame(BLUEs_all[[i]])
colnames(a)<-c("Entry",i)
q<-merge.data.frame(q,a,all=T)
remove(a)
}
BLUEs_all<-as.data.frame(q)
remove(q)
```
## Heritabilities
Here I check the heritabilities of traits using the formula from Fehr's book "Principles of Culitvar Development: Theory and Technique". I run all mixed models as completely random and extract variance components. Per-plot broad sense heritability is calculated by:
$$\frac{(\sigma^2_G)}{(\sigma^2_{G}+\sigma^2_{GL}+\sigma^2_{GY}+\sigma^2_{GLY}+\sigma^2_\varepsilon)}$$
and entry mean broad sense heritability is calculated by:
$$\frac{(\sigma^2_G)}{(\sigma^2_{G}+(\frac{\sigma^2_{GL}}{l})+(\frac{\sigma^2_{GY}}{y})+(\frac{\sigma^2_{GLY}}{l*y})+(\frac{\sigma^2_\varepsilon}{l*y*r}))}$$
```{r Heritabilities}
#make an empty data frame for the heritability estimates
h2s<-data.frame(Trait=character(), Type=character(),
Estimation=numeric(), SE=numeric())
for (i in traits){
print(paste("------- Analyzing trait", i, "-------"))
n_loc<-hfpop %>%
select(Environment, Year, Location, i) %>%
drop_na() %>%
distinct(Environment)
n_loc=as.numeric(nrow(n_loc))
n_rep=n_loc*2
n_year=hfpop %>%
select(Environment, Year, Location, i) %>%
drop_na() %>%
distinct(Year)
n_year<-as.numeric(count(n_year))
if(((n_year)>=2)){
print(paste(i, "has multiple years of data"))
mlm<-asreml(fixed = hfpop[,i]~1,
random = ~ (Entry)+
(Location)+
(Year)+
(Rep)+
(Entry:Location)+
(Entry:Year)+
(Year:Location)+
(Entry:Year:Location),
residuals = ~(units),
data = hfpop,
maxit=75,
family= asr_inverse.gaussian())
pph2<-vpredict(mlm,
h2~V5/(V5+V6+V7+V8+V9))
emh2<-vpredict(mlm,
h2~V5/(V5+(V6/n_year)+(V7/n_loc)+(V8/(n_year*n_loc))+(V9/(n_year*n_loc*n_rep))))
a<-rbind(pph2,emh2)
a<-cbind(data.frame(Trait=c(i, i)), data.frame(Type=c("Per-Plot", "Entry-Mean")), a)
h2s<-rbind(h2s,a)
}else{
print(paste(i, "has a single year of data"))
mlm<-asreml(fixed = hfpop[,i]~1,
random = ~(Entry)+
(Location)+
(Entry:Location)+
(Rep),
residuals = ~ units,
data = hfpop,
maxit=75)
pph2<-vpredict(mlm,
h2~V3/(V3+V4+V5))
emh2<-vpredict(mlm,
h2~V3/(V3+(V4/n_loc)+(V5/(n_year*n_loc*n_rep))))
a<-rbind(pph2,emh2)
a<-cbind(data.frame(Trait=c(i, i)), data.frame(Type=c("Per-Plot", "Entry-Mean")), a)
h2s<-rbind(h2s,a)
}
}
kable(h2s, row.names = F, caption = "Heritabilities")
write.csv(h2s,"Heritabilities.csv", row.names = F)
```
## Format Genotype Data and Diagnostic Plots
Here I pull in the genotype data and format it to fit the phenotype data
```{r Formatting, fig.width=10, fig.height=5}
#create dataframe that r/QTL likes
BLUEs<-full_join(BLUEs_all, BLUEs_loc, by="Entry")
CHR<-BLUEs[1,]
CHR[1,2:ncol(CHR)]<-NA
CHR[,1]<-as.character(CHR[,1])
CHR[1,1]<-"Chr"
CHR[,1]<-as.factor(CHR[,1])
BLUEs<-rbind(BLUEs,CHR)
colnames(BLUEs)<-str_replace(colnames(BLUEs),"-","_")
#look at correlations among environments
for (i in traits){
if(i=="PIT"){
i=paste(i,"_",sep = "")
n<-colnames(BLUEs)[(grep(i, colnames(BLUEs)))]
n<-n[-grep("NOP", n)]
print(pairs.panels(BLUEs[,n], stars = T))
}else{
i=paste(i,"_",sep = "")
pairs.panels(BLUEs[,grep(i,colnames(BLUEs))], stars = T)
}
}
#graphic for publication
a<-BLUEs[,grep("ME",colnames(BLUEs))]
colnames(a)<-c("PIT", "NOPPT", "NOPPIT")
pairs.panels(a,stars = T)
#look at correlations of BLUEs across environments
pairs.panels(BLUEs[,c(grep("_ME", colnames(BLUEs)), grep("NDVI", colnames(BLUEs)))],
stars = T)
#create parental table
counts<-hfpop %>%
filter(Entry=="Shirley"|Entry=="LA03136E71") %>%
group_by(Environment, Entry) %>%
count(Entry)
variance<-hfpop %>%
filter(Entry=="Shirley"|Entry=="LA03136E71") %>%
group_by(Environment, Entry) %>%
summarise(varPIT=var(PIT, na.rm = T), varNOPPT=var(NOPPT, na.rm = T), varNOPPIT=var(NOPPIT, na.rm = T))
candv<-full_join(counts,variance)
se<-candv %>%
mutate(SE_PIT=varPIT/sqrt(n)) %>%
mutate(SE_NOPPT=varNOPPT/sqrt(n)) %>%
mutate(SE_NOPPIT=varNOPPIT/sqrt(n)) %>%
select(SE_PIT, SE_NOPPT, SE_NOPPIT)
means<-hfpop %>%
filter(Entry=="Shirley"|Entry=="LA03136E71") %>%
group_by(Environment, Entry) %>%
summarise(PIT=mean(PIT, na.rm=T), NOPPT=mean(NOPPT, na.rm=T), NOPPIT=mean(NOPPIT, na.rm=T))
parents<-full_join(means,se)
means_overall<-hfpop %>%
filter(Entry=="Shirley"|Entry=="LA03136E71") %>%
group_by(Entry) %>%
summarise(PIT=mean(PIT, na.rm=T), NOPPT=mean(NOPPT, na.rm=T), NOPPIT=mean(NOPPIT, na.rm=T))
counts<-hfpop %>%
filter(Entry=="Shirley"|Entry=="LA03136E71") %>%
group_by(Entry) %>%
count(Entry)
variance<-hfpop %>%
filter(Entry=="Shirley"|Entry=="LA03136E71")%>%
group_by(Entry) %>%
summarise(varPIT=var(PIT, na.rm = T), varNOPPT=var(NOPPT, na.rm = T), varNOPPIT=var(NOPPIT, na.rm = T))
candv<-full_join(counts,variance)
se<-candv %>%
mutate(SE_PIT=varPIT/sqrt(n)) %>%
mutate(SE_NOPPT=varNOPPT/sqrt(n)) %>%
mutate(SE_NOPPIT=varNOPPIT/sqrt(n)) %>%
select(SE_PIT, SE_NOPPT, SE_NOPPIT)
parents_overall<-full_join(means_overall,se)
parents<-rbind(parents,parents_overall)
write.csv(parents,"Parental Means.csv", row.names = F)
remove(counts, se, candv,
parents_overall, means,
means_overall, parents,
variance)
#read in VCF
library(gaston)
bed <- read.vcf("Shirley_x_LA03_filt.vcf.gz", convert.chr = FALSE)
structure(bed)
#write out genotype names
a<-bed@ped$id
write.csv(a, "Genotype_Names.csv", row.names = F)
#LD prune
#bed<-LD.thin(bed, threshold = 0.6)
#pull out a vector of only 7D SNPs
chr7D<-bed@snps
chr7D<-chr7D %>% filter(chr=="7D")
library(devtools)
###install_github("etnite/bwardr")
library("bwardr")
#find markers with a high level of heterozygotes
#After Three generations of inbreeding you should expect (assuming HWE)
#F2-F3
50*.5
#F3-F4
25*.5
#F4-F5
12.5*.5
#around 6.25A
drophz<-bed@snps[(bed@snps$hz>=0.10),]
drophz<-drophz$id
#find markers with a low minor allele frequency
dropmaf<-bed@snps[(bed@snps$maf<=0.05),]
dropmaf<-dropmaf$id
a<-format_qtlmap_geno(bed, par_a="LA03136E71",par_b="SHIRLEY",rm_het=T, rm_miss=T, include_pars=T, out_fmt="rqtl")
map<-a$abh
#drop SNPs with 10% heterozygosity or more
map<-map[!(map$id %in% drophz),]
#drop SNPs with a MAF of 10% or less
map<-map[!(map$id %in% dropmaf),]
#transform map
map<-as.data.frame(t(map))
map<-rownames_to_column(map, var="Entry")
colnames(map)<-map[1,]
map<-map[-c(1,3),]
map[1,1]<-"Chr"
colnames(map)[1]<-"Entry"
map[,1]<-str_replace(map[,1],"NC131.","")
map[,1]<-str_replace(map[,1],"SHIRLEY","Shirley")
map<-rownames_to_column(map, var="dummy")
map[,1]<-as.numeric(map[,1])
#merge maps
fullmap<-full_join(BLUEs, map, by="Entry")
#reorder map and clean up
fullmap<-fullmap[order(fullmap$dummy, decreasing = F),]
fullmap<-subset(fullmap, select = -c(dummy))
dropme<-c("Shirley_1","LA03136E71_1")
fullmap<-fullmap %>% filter(!Entry %in% dropme)
fullmap[1,1:ncol(BLUEs)]<-""
#remove parents
fullmap<-fullmap %>% filter(!Entry=="LA03136E71" & !Entry=="Shirley")
#convert allele coding
fullmap[fullmap=="A"]="AA"
fullmap[fullmap=="H"]="AB"
fullmap[fullmap=="B"]="BB"
#create CSV
write.csv(fullmap,"HF_Geno_Pheno_File.csv", row.names = F)
### Write a seperate file for the 7D chromosome
#pull in data and subset only the 7D markers with no filtration
bed <- read.vcf("Shirley_x_LA03_filt.vcf.gz", convert.chr = FALSE)
a<-format_qtlmap_geno(bed, par_a="LA03136E71",par_b="SHIRLEY",rm_het=T, rm_miss=T, include_pars=T, out_fmt="rqtl")
a<-a$abh
colnames(a)[2:3]<-c("chr","blank")
a<-a %>% filter(chr=="7D")
map<-a
#transform map
map<-as.data.frame(t(map))
map<-rownames_to_column(map, var="Entry")
colnames(map)<-map[1,]
map<-map[-c(1,3),]
map[1,1]<-"Chr"
colnames(map)[1]<-"Entry"
map[,1]<-str_replace(map[,1],"NC131.","")
map[,1]<-str_replace(map[,1],"SHIRLEY","Shirley")
map<-rownames_to_column(map, var="dummy")
map[,1]<-as.numeric(map[,1])
#merge maps
fullmap<-full_join(BLUEs, map, by="Entry")
#reorder map and clean up
fullmap<-fullmap[order(fullmap$dummy, decreasing = F),]
fullmap<-subset(fullmap, select = -c(dummy))
dropme<-c("Shirley_1","LA03136E71_1","183","23","189")
fullmap<-fullmap %>% filter(!Entry %in% dropme)
fullmap[1,1:ncol(BLUEs)]<-""
#remove parents
fullmap<-fullmap %>% filter(!Entry=="LA03136E71" & !Entry=="Shirley")
#convert allele coding
fullmap[fullmap=="A"]="AA"
fullmap[fullmap=="H"]="AB"
fullmap[fullmap=="B"]="BB"
#create CSV
write.csv(fullmap,"HF_Geno_Pheno_File_7D_Only.csv", row.names = F)
```
```{r Remove MLM Junk, echo=FALSE}
remove(a,bed,map,
CHR,fullmap,drophz,
dropmaf,dropme,env,
i,j,traits,
chr7D,n,n_loc,
n_rep, n_year,
emh2,h2s,mlm,pph2,mlms,
BLUEs_all, BLUEs_loc)
```
# QTL Mapping for all chromosomes
Here I take the BLUEs from the first section, and using those estimates for the QTL mapping with the genetic data prepaired in the perivous section.
## Create the genetic map
Now that I have the means aligned to the genotype data, I am going to make genetic maps for the genotype data collected
```{r Genome Map, fig.height=5, fig.width=15}
#Library packages
library(qtl)
library(ASMap)
library(parallel)
#define cores for parallel processing
ncor<-detectCores()
#read in map and data
HF<-read.cross("csv",
file="HF_Geno_Pheno_File.csv",
genotypes=c("AA","AB","BB"),
alleles=c("A","B"),
crosstype="f2",
na.strings = c("NA","-"))
#plot map
geno.image(HF)
nmar(HF)
#convert to bcfst where F=5
HF<-convert2bcsft(HF, BC.gen = 0, F.gen = 5, estimate.map = F)
#look at geno info
tab<-geno.table(HF)
distorted<-rownames(tab)[tab$P.value>=0.00001]
#pull distorted markers
HF<-pull.markers(HF, markers = distorted)
#plot map
geno.image(HF)
nmar(HF)
```
```{r Genome MST, results='hide'}
#Estimate linkage map
HFM<- mstmap(HF,
pop.type="nRIL",
id = "Entry",
p.value = 0.0001,
miss.thresh=0.05,
chr=c("1A", "1B", "1D",
"2A", "2B", "2D",
"3A", "3B", "3D",
"4A", "4B", "4D",
"5A", "5B", "5D",
"6A", "6B", "6D",
"7A", "7B", "7D"),
anchor = TRUE,
detectBadData = TRUE,
bychr = TRUE,
trace = FALSE)
```
```{r Genome Map Continued, fig.height=5, fig.width=15}
HFM<-jittermap(HFM)
summary(HFM)
names(HFM$geno)
#remove small linkage groups of a specific length
chrLengths<-c() #set up list for chromosome lengths
for (i in names(HFM$geno)){
chrLengths <- c(chrLengths, ncol(HFM$geno[[i]]$data))
}
#create data frame with number of markers per linkage group
chrLengths<-as.data.frame(cbind(names(HFM$geno), chrLengths))
colnames(chrLengths)<-c("chr", "n_markers")
chrLengths$n_markers<-as.numeric(chrLengths$n_markers)
chrLengths<-chrLengths[chrLengths$n_markers>=5,]
#new number of linkage groups
nrow(chrLengths)
chrLengths
#subset the map with the linkage groups that pass the filter
HFM<-subset(HFM, chr = chrLengths$chr)
HFM<-jittermap(HFM)
summary(HFM)
#rename linkage groups
#Names(HFM$geno) <- c("1A","1B", "1D",
# "2A", "2B", "2D",
# "3A", "3B", "3D",
# "4A", "4B", "4D",
# "5A", "5B", "5D",
# "6A", "6B", "6D",
# "7A", "7B", "7D")
#remove junk
remove(chrLengths)
#Get the names of things in your data set
names(HFM$pheno)
names(HFM)
names(HFM$geno)
#get the number of things in your data set
nphe(HFM)
nind(HFM)
nchr(HFM)
totmar(HFM)
#plot the distribution and map of markers in your data set
summary(HFM)
plotMap(HFM, horizontal = FALSE, shift = FALSE, main="LA03136E71 x Shirley Mapping Population Linkage Map")
geno.image(HFM, main="LA03136E71 x Shirley Haplotypes")
write.cross(HFM, "ASMap output",format = "csv")
#check if the markers are in the correct potion
mn<-markernames(HFM)
pos<-find.markerpos(HFM,mn)
pos<-rownames_to_column(pos,var="BP_Location")
pos<-pos %>% mutate(Marker_Name=BP_Location) %>% separate(BP_Location, into =c("a","b"), sep = "_")
colnames(pos)<-c("trash","BP","Chr","cM","Marker")
pos<-pos %>% select(Marker,Chr, BP, cM)
pos[,1:2]<-lapply(pos[,1:2], as.character)
pos[,3:4]<-lapply(pos[,3:4], as.numeric)
library(lattice)
xyplot(BP~cM|Chr, pos, pch=15, main="BP vs. cM", col = "blue")
write.csv(pos,"cM_vs_bp.csv",row.names = F)
#Check linkage group orientation and flip as needed
chr<-names(HFM$geno)
for (i in chr){
q<-subset(pos, pos$Chr==i)
if (q[1,3]>q[(nrow(q)),3]){
HFM<-flip.order(HFM, i)
print(paste("Chromsome",i,"has been flipped"))
remove(q)
}else{
print(paste("Chromsome",i,"is in correct order"))
remove(q)
}
}
#Recheck Graph
mn<-markernames(HFM)
pos<-find.markerpos(HFM,mn)
pos<-rownames_to_column(pos,var="BP_Location")
pos<-pos %>% mutate(Marker_Name=BP_Location) %>% separate(BP_Location, into =c("a","b"), sep = "_")
colnames(pos)<-c("trash","BP","Chr","cM","Marker")
pos<-pos %>% select(Marker,Chr, BP, cM)
pos[,1:2]<-lapply(pos[,1:2], as.character)
pos[,3:4]<-lapply(pos[,3:4], as.numeric)
library(lattice)
xyplot(BP~cM|Chr, pos, pch=15, main="BP vs. cM", col = "blue")
write.csv(pos,"cM_vs_bp.csv",row.names = F)
#number of cM
a<-0
for (i in names(HFM$geno)){
b<-max(HFM$geno[[i]]$map)
a<-a+b
}
print(paste("The linkage map has a total of", round(a,2), "cM"))
remove(a,b)
```
## Run Interval Mapping
Here I run several IM scans for all phenotypes.
```{r Genome Interval Mapping,fig.height=5, fig.width=15}
#calculate marker probabilities
HFM<-calc.genoprob(HFM, step=2.0, off.end=0.0, error.prob=1.0e-4, map.function="kosambi",stepwidth="fixed")
#calculate marker probabilities for a simulated genotype
HFM<-sim.geno(HFM, n.draws = 32, step = 2, off.end = 0.0, error.prob=1.0e-4, map.function="kosambi", stepwidth="fixed")
#set up list
traits<-colnames(HFM$pheno)[2:ncol(HFM$pheno)]
IM<-list()
IMPRMS<-list()
IMQTL<-list()
IMTHRESH<-list()
#run scans
for (i in traits){
print(paste("------------ Interval Mapping of", i,"------------"))
print("Interval Mapping...")
scans<-scanone(HFM,
pheno.col = i,
model = "normal",
method = "hk")
print("Done")
print("Permutational Interval Mapping...")
perms<-scanone(HFM,
pheno.col = i,
model = "normal",
method = "hk",
n.perm = 1000)
print("Done")
print("Plotting...")
threshold<-summary(perms, alpha=0.05)
plot(scans,main=paste("IM for", i))
abline(h=threshold, lty="dotted", lwd=1, col="#cc0000")
legend("topleft",legend=c("p=0.05"),col = c("#cc0000"),lty = "dotted")
print("Done")
print("Defining QTL...")
qtl<-summary(scans, perm=perms, lodcolum=1, alpha=0.05)
print(summary(scans, perm=perms, lodcolum=1, alpha=0.05))
print("Done")
if (nrow(qtl)==0){
print(paste("No QTl were found for", i))
remove(scans,perms,threshold,qtl)
}else{
print("Drawing QTL...")
colnames(scans)<-c("chr","pos","lod")
IM[[i]]<-scans
IMPRMS[[i]]<-perms
IMQTL[[i]]<-qtl
IMTHRESH[[i]]<-threshold
c<-qtl[,1]
p<-qtl[,2]
a<-subset(HFM, chr=c)
a<-sim.geno(a, n.draws = 32, step = 2,
off.end = 0.0, error.prob=1.0e-4,
map.function="kosambi", stepwidth="fixed")
madeqtl<-makeqtl(a, c, p, qtl.name = rownames(qtl), what = c("draws", "prob"))
plot(madeqtl, chr = c, horizontal= FALSE,
shift= FALSE, show.marker.names= FALSE,
alternate.chrid= FALSE, justdots= FALSE,
main=paste("Interval Mapping QTL for", i ))
print("Done")
print("Running Drop-One QTL Analysis")
qtlfit<-fitqtl(a, qtl=madeqtl, pheno.col = i, get.ests = T,
model="normal", method = "imp")
print(summary(qtlfit))
remove(scans,perms,threshold,qtl,c,p,a,madeqtl,qtlfit)
print("Done")
}
}
#Make an object of all scans for drawing
IMLOD<-as.data.frame(rownames(IM$PIT_ME))
IMLOD<-cbind(IMLOD, as.data.frame(IM$PIT_ME))
rownames(IMLOD)<-c()
IMLOD<-IMLOD[,1:3]
colnames(IMLOD)[1]<-"Marker_Location"
traits<-names(IM)
for (i in traits){
q<-as.data.frame(IM[[i]])
if(nrow(q)==0){
print(paste(i, "not detected"))
}else{
print(paste("Adding", i))
q<-rownames_to_column(q, var="Marker_Location")
q<-q %>% select(Marker_Location,lod)
colnames(q)[2]<-paste("LOD",i,sep="_")
IMLOD<-full_join(IMLOD,q, by="Marker_Location")
}
}
j<-data.frame(Marker_Location=character(),chr=character(),pos=numeric(),lod=numeric(),Trait=character())
for (i in traits){
m<-IM[[i]]
m<-m %>% rownames_to_column(var="Marker_Location") %>% mutate(Trait=i)
j<-bind_rows(j,m)
remove(m)
}
q<-as.character(IMQTL$PIT_ME$chr)
j<-j %>% filter(chr==q)
ggplot(data=j, aes(x=pos, y=lod, col=Trait))+
geom_line()+
geom_hline(yintercept = 5, col="red", lty="dotted", lwd=1)+
facet_wrap(~ chr, nrow = 1, scales="fixed")+
geom_rug(mapping = aes(x=pos), outside = F, sides = "bottom", show.legend = T)+
xlab("Chromosomes")+
ylab("LOD")+
labs(title = paste(q ,"Interval Mapping Scans With QTL"))
fullgeno<-list(IM,IMPRMS,IMTHRESH,IMQTL)
names(fullgeno)<-c("IM","IMPRMS","IMTHRESH","IMQTL")
```
# QTL mapping for 7D alone
Since there was only a single big peak on 7D for all traits, here I will do the same thing as above but with only the 7D chromosome and all available markers.
## Make the map
```{r 7D Map, fig.height=5, fig.width=15}
#Library packages
library(qtl)
library(ASMap)
library(parallel)
#define cores for parallel processing
ncor<-detectCores()
#read in map and data
HF<-read.cross("csv",
file="HF_Geno_Pheno_File_7D_Only.csv",
genotypes=c("AA","AB","BB"),
alleles=c("A","B"),
crosstype="f2",
na.strings = c("NA","-"))
#plot map
geno.image(HF)
#convert to bcfst where F=5
HF<-convert2bcsft(HF, BC.gen = 0, F.gen = 5, estimate.map = F)
#look at geno info
tab<-geno.table(HF)
distorted<-rownames(tab)[tab$P.value>=0.000001]
#pull distorted markers
HF<-pull.markers(HF, markers = distorted)
#plot map
geno.image(HF)
```
## Clean the genotypes
Here I am going to attempt to clean up the calling to reduce the size of the map and make a cleaner peak for the QTL
```{r Clean Up 7D}
Markers<-as.matrix(pull.geno(HF, chr="7D"))
#here I am going to correlate
#the marker calls of each
#marker to the next marker