forked from ruffleymr/Athaliana_Natural_Selection_Conflict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtlasofPhenotypes.Rmd
9435 lines (7580 loc) · 414 KB
/
AtlasofPhenotypes.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: |
| Genomic conflicts of natural selection for ecologically-relevant traits
| Supplemental Appendix
author: |
| **Megan Ruffley, Laura Leventhal, Shannon Hateley,
| Sue Rhee , Moises Exposito-Alonso**
| 1 Department of Plant Biology, Carnegie Institution for Science, California, USA
| 2 Department of Biology, Stanford University, California, USA
| correspondence: [email protected]
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
toc: yes
toc_depth: '2'
df_print: paged
mainfont: Times New Roman
indent: yes
editor_options:
chunk_output_type: console
preview: viewer
---
```{r echo=FALSE, eval=T, message=FALSE, warning=FALSE, errors=FALSE, paged.print=FALSE}
# pdf_document:
# toc: yes
# toc_depth: 2
# mypath <- "~/safedata/natvar" ##the .rmd is in natvar/analyses/
# knitr::opts_knit$set(root.dir = mypath, warning=FALSE, errors=FALSE)
# setwd("./safedata/natvar/")
setwd("/natvar/")
#devtools::install(".")
#devtools::load_all("~/safedata/natvar") # natvar
#install.packages("ggplot2", dependencies = T, lib="/home/mruffley/R/x86_64-pc-linux-gnu-library/3.6")
#devtools::install_version("ggplot2", version = "3.3.5", )
library(knitr)
library(tinytex)
#tinytex::install_tinytex()
#remotes::install_github('rstudio/rmarkdown')
library(dplyr)
library(ggplot2)
#library(moiR)
library(data.table)
#library(corrplot)
library(cowplot)
library(caret)
#library(ggfortify)
theme_set(theme_cowplot())
#install.packages("caret", dependencies=T)
#remove.packages("ggplot2")
#source("~/safedata/natvar/analysesphenotypeselection/R/phenoselection_multi_FUNCTIONS.R")
```
--------------------------------------------------------------------------------
################################################################################
# I. Phenotypic landscape of _Arabidopsis thaliana_
################################################################################
################################################################################
## I.1 Description and Curation of Phenotypes
################################################################################
### Data retrieval
The phenotypes come from a combination of laboratory and field experiments that use a subset of the 1001 genomes A. thaliana ecotypes Nearly 600 phenotypes come from the Arapheno database (Seren et al. 2017), but the remainder are not included in the public database. We also rely heavily on fitness data from 8 outdoor field experiments where fitness was measured under high and low rainfall environments (Exposito-Alonso et al. 2019). In total, data from 1862 phenotypes were gathered from over 108 published sources (Table SI.1). In the case of replicated ecotypes within an experiment with multiple measures for the same phenotype, we took the mean of the replicates.
```{r, echo=FALSE, eval=F}
#d<-read.table("./data/pheno_fromgoogle.tsv",header = T)
d <- read.table(file="./data/atlas_dir.tsv", header = T, sep="\t")
d <- d[,-4]
#write.table(d, "./tables/Allphenotype_Info_tableSI1.tsv", col.names = T, row.names = F, quote = F, sep="\t")
knitr::kable(d, col.names = colnames(d), caption = "Table SI.1 Phenotypes gathered for this study with the original publication, general name of the phenotype, number of 1001 genomes accessions used in the original study, general function category, and stress strategy classification.")
```
### Missing Data
We assembled the phenotype data for the 1001 genomes individuals of _Arabidopsis thaliana_, which is specifically 1,135 individuals. A there was not complete coverage of phenotype data across all accessions. Most phenotypes had less than 25% coverage across the 1,135 accessions, a few had data for several hundred accessions, and no phenotype had complete coverage across all accessions (__Fig. SI.1__).
```{r, echo=F, eval=T, fig.cap="Figure SI.1 Phenotypic coverage across 1001 Genomes A. thaliana Accessions", fig.height=2.5, fig.width=6}
RERUN=F
if(RERUN){
pheno<- read.table(file="./data/atlas1001_phenotypes_matrix_MR.csv", sep=",", header = T)
dim(pheno)
id<-pheno[,1]
pheno<- pheno[,-1]
phenomiss<-apply(pheno,2,function(x) sum(!is.na(x)) / length(x) )
accmiss<-apply(pheno,1,function(x)sum(!is.na(x)) / length(x))
plot_grid(
qplot(phenomiss, xlab="% of accessions",ylab="# phenotypes",bins=25)+
scale_x_continuous(breaks = seq(0,1,by=0.25),labels =seq(0,1,by=0.25)*100 ),
qplot(accmiss, xlab="% of phenotypes",ylab="# accessions",bins=25)+
scale_x_continuous(breaks = seq(0,1,by=0.25),labels =seq(0,1,by=0.25)*100 )
) -> phenotype_missingness
write.table(file = './tables/atlas1001_phenotype_missingness-peraccession.tsv',
x = data.frame(perc_miss=accmiss,id=id), sep = "\t", quote = F, row.names = F)
write.table(file = './tables/atlas1001_phenotype_missingness-perphenotype.tsv',
x = data.frame(perc_miss=phenomiss,phenotype=names(phenomiss)), sep = "\t", quote = F, row.names = F)
save(file = "./figs/tmpobjects/phenotype_missingness.rda",phenotype_missingness)
pdf(file="./figs/missingdatasummary.pdf", height=3, width = 3)
qplot(phenomiss, xlab="% of accessions",ylab="# phenotypes",bins=25)+
scale_x_continuous(breaks = seq(0,1,by=0.25),labels =seq(0,1,by=0.25)*100 )
dev.off()
phenotype_missingness
}else{
load("./figs/tmpobjects/phenotype_missingness.rda")
phenotype_missingness
}
```
### General function and drought adaptation classification
We classified all phenotypes into a general functional category (__Fig. SI.2A__), and were able to classify 1282 phenotypes as related to one of three drought adaptation strategies; _escape_, _avoidance_, and _tolerance_ (__Fig. SI.2B__, Kooyers 2015). We classified escape phenotypes as those that contribute to late germination, rapid growth, and fast reproduction. This encapsulates phenotypes related to dormancy induction and germination rate, growth rate, vernalization, and all traits related to reproduction, importantly flowering time and days to fruit. Avoidance phenotypes are those that are related to endure drought with advantageous phenotypes that both conserve and locate water, these include root growth and angle related phenotypes, leaf area, biomass accumulation, stomata density and size, and delta_C13, which represents a metric of water use efficiency. Tolerance phenotypes are primarily metabolite related, as they may play an important role in dealing with desiccation and osmotic regulation. _A. thaliana_ is not observed as employing a tolerance strategy for drought adaptation, and so it is possible many of the metabolites are unrelated to drought adaptation, or even tolerance. The few metabolites we know of to be associated with drought and temperature stress, such as absiscic acid accumulation (ABA) and rhamnose, we associate with the avoidance strategy. Other than those two exceptions, we do not consider the tolerance classified phenotypes.
```{r, echo=FALSE, eval=T, fig.cap="Figure SI.2 Raw counts of phenotypes classified as general phenotype categories and drought response strategies.", fig.height=3, fig.width=5}
## chunk working, tested 9/20
RERUN=F
if(RERUN){
library(future)
d<-read.table("./data/pheno_fromgoogle.tsv",header = T)
d1<-table(d$phenotypecategory) %>% as.data.frame()
d2<-table(d$stressstrategy) %>% as.data.frame()
head(d)
d[grep("DOG", d$phenotype),]
d[grep("root", d$phenotype),]
grep("root", d$phenotype)
d[d$phenotype=="Growth_rate",]
# d[d$phenotype=="Growth_rate",]
# d_es <- d %>% filter(stressstrategy=="Avoidance")
# summary(d_es$numaccessions)
p1<-ggplot(d1) + geom_col(aes(x = Var1, y=Freq, fill=Var1 ), color='white', alpha=0.5) + xlab("") + ylab("# phenotypes")+
theme(axis.text.x = element_text(angle = 45,hjust = 1)) +
scale_fill_manual("",values = c("Defense"=alpha("red3"),"Development"=alpha('darkorange1'),
"Ionomics"=alpha("goldenrod1"),
"Metabolite"=alpha("yellow"), "Microbiome"=alpha("limegreen"),
"Reproduction"=alpha("dodgerblue1" ))) +
theme(legend.position = c(.7, .7), legend.key.size =unit(.5, 'cm'),
legend.text = element_text(size=5), legend.title = element_blank())
p2<-ggplot(d2) + geom_col(aes(x = Var1, y=Freq ,fill=Var1),color='white', alpha=0.5) + xlab("") +
scale_fill_manual("",values = c("Avoidance"=alpha("red"),"Escape"=alpha('green4'), "Tolerance"=alpha("navy")))+
ylab("") +
theme(axis.text.x = element_text(angle = 45,hjust = 1)) +
#theme(plot.margin=margin(l=-0.5,unit="cm")) +
theme(legend.key.size =unit(.5, 'cm'), legend.position = c(.01, .8),
legend.text = element_text(size=5), legend.title = element_blank())
PhenoCatStrat_Histograms<-plot_grid(p1,p2,nrow=1 ,rel_widths = c(1.6,1),align = "hv")
save(file = "./figs/tmpobjects/PhenoCatStrat_Histograms.rda",PhenoCatStrat_Histograms)
pdf(file="./figs/PhenoCatStrat_Histograms.pdf", width = 5, height=3.5)
PhenoCatStrat_Histograms
dev.off()
}else{
load("./figs/tmpobjects/PhenoCatStrat_Histograms.rda")
PhenoCatStrat_Histograms
#![Histogram of phenotypes in functional categories (left) and drought response strategies (right)](../figs/PhenoHistogram.jpg){width=40% height=40%}
}
```
### Phenotypic Imputation
We used the R package *missForest* to do a full dataset imputation (Stekhoven and Bühlmann 2012). This was done iteratively for each phenotype and all 1,135 individuals, with all other phenotypes being used as predictors. The out-of-bag approach resulted in an average normalized root mean squared error (NRMSE) of 0.195 (__Figure 3__). We report the NRMSE for all phenotypes in Supplemental Table [tables/]. Although useful for certain purposes, such as identifying ecotypes with potential extreme values in traits of interest for further experimentation, we do not recommend using this imputed matrix as face value.
```{r, echo=F, eval=T, fig.cap="Figure SI.3 Normalized root-MSE for all phenotypes.", fig.width=3, fig.height=2.5, warning=F}
RERUN=F
if(RERUN){
#getwd()
#setwd("./safedata/natvar")
library(missForest)
library(stats)
pheno<- read.table(file="./data/atlas1001_phenotypes_matrix.csv", sep=",", header = T)
pheno[pheno== -9]<-NA ## turn -9 into NA
#sum(pheno== -9, na.rm = T) ##check
dimp<- missForest(pheno[,-1], variablewise=T,maxiter = 5)
#has lower NRMSE at 0.2098436 on June 8 2021
atlas1001_phenotype_matrix_imputed_withID=data.frame(pheno$V1,dimp$ximp)
names(atlas1001_phenotype_matrix_imputed_withID)[1]<-"id"
error <- data.frame(pheno=colnames(pheno[,-1]), MSE=dimp$OOBerror)
NRMSE_list<-c()
varExp_list <- c()
for (i in 1:ncol(pheno)-1){
n <- sqrt(error[i,2]) / (max(pheno[,i+1], na.rm=T) - min(pheno[,i+1], na.rm=T))
NRMSE_list <- c(NRMSE_list, n)
}
mean(NRMSE_list[-which(is.na(NRMSE_list)) ]) ## 0.1909089
error$NRMSE <- NRMSE_list
nrmse_hist <- qplot(error[,3], xlab="NRMSE",ylab="# accessions",bins=25)+
scale_x_continuous(breaks = seq(0,1,by=0.25),labels =seq(0,1,by=0.25) )
### Save
# write.table(atlas1001_phenotype_matrix_imputed_onlypheno,
# file = 'data/atlas1001_phenotype_matrix_imputed_withID.csv',
# row.names = F, quote = F, col.names = T)
write.table(atlas1001_phenotype_matrix_imputed_onlypheno, file="data/atlas1001_phenotype_matrix_imputed_withID.csv", sep=",", row.names = F, quote = F, col.names = T)
write.table(error, file="./tables/atlas1001_imputationaccuracy-perphenotype.tsv", sep="\t", quote = F, row.names = F)
save(file="./figs/tmpobjects/nrmse_hist.rda", nrmse_hist)
tmp <- read.table(file="./data/atlas1001_phenotype_matrix_imputed_withID.csv", sep=",", header = T)
head(tmp)
sum(tmp$rFitness_mli == -9)
#use_data(atlas1001_phenotype_matrix_imputed_onlypheno, overwrite = T)
}else{
error<- read.table(file="./tables/atlas1001_imputationaccuracy-perphenotype.tsv", header = T)
head(error)
#er <- error[error$pheno %in% colnames(pheno), ]
#colnames(pheno)[(!colnames(pheno) %in% er$pheno)]
#sum(duplicated(colnames(pheno)))
error$nMSE <- "NA"
i<-1
for (i in 1:nrow(error)){
error$nMSE[i] <- error$MSE[i]/var(pheno[,i+1], na.rm = T)
}
error$NRMSE <- as.numeric(error$NRMSE)
range(error$NRMSE, na.rm = )
summary(error$NRMSE)
error[error$NRMSE==1, ]
p<-error %>% filter(NRMSE<0.155)
nrow(p)
u<-error %>% filter(NRMSE>0.234)
nrow(u)
df <- error %>% filter(pheno!="Agro_PPV_infection")
mean_nrmse <- mean(df$NRMSE, na.rm = T)
summary(df$NRMSE)
hpd_interval <- HPDinterval(as.mcmc(df$NRMSE), prob = 0.95)
l <-mean_nrmse - IQR(df$NRMSE, na.rm = T)/2
u <- mean_nrmse + IQR(df$NRMSE, na.rm = T)/2
sd_nrmse <- sd(df$NRMSE, na.rm = T)
# Calculate standard error
se_nrmse <- sd_nrmse / sqrt(nrow(df))
margin_error <- qt(0.975, df = nrow(df) - 1) * se_nrmse
# Calculate confidence interval
ci_lower <- mean_nrmse - margin_error
ci_upper <- mean_nrmse + margin_error
library(coda)
library(ggplot)
error$NRMSE
p1 <- ggplot(error, aes(x = NRMSE)) +
geom_histogram(binwidth = 0.1, fill = "blue", color = "black") + # Adjust binwidth and colors as needed
labs(x = "NRMSE", y = "Frequency") + # Customize titles +
geom_vline(xintercept = c(0.071, 0.36), linetype = "dashed", color = "red", size = 1) + # Add vertical lines at 'l' and 'u'
theme_minimal()
p1
pdf(file="./figs/")
mean(error$nMSE, na.rm=T)
sum(error$nMSE>1.0, na.rm=T)
length(na.omit(error$nMSE))
nrow(error)
nrmse_hist <- qplot(error[,3], xlab="NRMSE",ylab="# accessions")+
scale_x_continuous(breaks = seq(0,1,by=0.25),labels =seq(0,1,by=0.25) ) +
geom_vline(xintercept = c(0.071, 0.36), linetype = "dashed", color = "black", size = .8)
pdf(file="./figs/NRMSE.pdf", width = 3, height = 3)
nrmse_hist
dev.off()
}
```
################################################################################
## I.2 Principal Components Analysis
################################################################################
We use principal components analysis (PCA) to decorrelate and decompose the escape and avoidance phenotypic variation. We also use a subset of 515 accessions out of the 1,135 accessions associated with the 1001 genomes accessions (1001 Genomes Project). We first isolated specific phenotypes related to dormancy, vernalization, germination, flowering time, leaf traits, roots, stomata, growth rates, and stress response. This narrowed down the phenotypes from 509 to 205, from here we removed highly correlated phenotypes (R2 > 0.7), resulting in a total of 64 phenotypes. We show the overall number of how many of each phenotype we use below (__Fig. SI.4__).
```{r, echo=F, eval=T, fig.cap="Figure SI.4 Counts of phenotypes and phenotype categories used in PCA.", fig.height=3, fig.width=3}
## this chunk narrows down which phenotypes used in the PCA
RERUN=F
if(RERUN){
atlasstrategies<-read.table("./data/pheno_fromgoogle.tsv",header = T)
pheno <- read.table(file = 'data/atlas1001_phenotype_matrix_imputed_withID.csv', sep=" ", header = T)
#raw_pheno <- read.table(file = './data/atlas1001_phenotypes_matrix.csv')
# load field experiment information to get the 515 accessions
load("./data/d4.rda")
idsfield<-unique(d4$id)
whichfield<-which(pheno$id %in% idsfield)
idex515<-whichfield
load("./data/phenotypenames.rda")
FT_phenos<-c("Flowering_time","FT16","FT10", "FT", "FT.1", "FT.2" )
moiFT <- colnames(pheno)[grep("FT_", colnames(pheno))][1:8]
dtf <- colnames(pheno)[grep("DTF", colnames(pheno))]
ft_atwell_phenos <- c("X32_FT_Duration_GH",
"X52_LC_Duration_GH", "X29_FLC", "X69_FRI", "X58_LFS_GH","X86_FT_Field",
"X49_FT_GH", "X31_FT10", "X30_FT16", "X102_FT22", "X89_LD", "X34_LDV",
"X47_SD", "X104_SDV", "X37_0W", "X11_2W", "X100_4W", "X48_8W", "X99_8W_GH_FT", "X53_0W_GH_FT")
allflowering <- c(FT_phenos, ft_atwell_phenos, moiFT, dtf)
allstomata<-c("StomatalIndex_mild_drought","StomatalIndex_well_watered","SIL_vs_SIC","Stomatal_density_in_cotyledon",
"Stomatal_density_in_first_leaf","Stomatal_index_in_cotyledon","stomata_density","stomatasize", "Stomatal_index_in_first_leaf")
allstress<-c("Delta_13C","drought_index", "ABA_96h_low_water_potential", "rhamnose_1_exp2", "proline_exp1", "leucine_exp1")
allgrowth<-c("RGR","Growth_rate","rosette_DM","RosetteArea_mild_drought","RosetteArea_well_watered", "X90_Seedling_Growth", "RGRbv",
"RGRav")
allgermination<-c("base_perc" , "d11_10C_perc" , "d11_4C_perc" , "d15_10C_perc" , "d15_4C_perc",
"d22_10C_perc" , "d22_4C_perc" , "d32_10C_perc", "d32_4C_perc" , "d4_10C_perc" , "d4_4C_perc",
"d8_10C_perc" , "d8_4C_perc" ,"DSDS50","DSDS10","DSDS90", "DTgerm","X39_After_Vern_Growth","X72_Vern_Growth",
"germination_3days", "germination_7days", "X45_Germ_22","X59_Germ_10", "X60_Germ_16", "X68_Germ_in_dark" ,
"X294_Germination_rate_21C", "X106_Secondary_Dormancy", "X94_Seed_Dormancy",
"Germination_10C", "Germination_22C", "Germination_30C")
leaf <- c("First_leaf_area","X92_Leaf_serr_22","X71_Trichome_avg_C","X6_Leaf_serr_10","X77_Trichome_avg_JA")
allroot<-dplyr::filter(data.frame(phenotypenames), X1=="Busch_Slovak_PlantCell_2014_PID_24920330")$X2
importanttraits<-c(allstomata,
allgrowth,
allgermination,
allflowering,leaf,
allroot,allstress)
write.table(importanttraits, file="./data/importanttraits.csv",sep=",", col.names = F, quote = F, row.names = F )
pheno[1:5,1:5]
dim(pheno)
idex515
id <- pheno[idex515,1]
target_pheno<- pheno[idex515,colnames(pheno) %in% importanttraits]
#importanttraits[which(!importanttraits %in% colnames(target_pheno))] ## should be 0
target_pheno <- apply(target_pheno,2,fn)
target_pheno <- apply(target_pheno,2,scale)
target_pheno[1:5,1:5]
?scale
Targ_cor<-cor(target_pheno)
hist(target_pheno[,20])
#corrplot(Targ_cor, method = "color", type = "lower", diag = F,tl.cex = .35, tl.srt = 45)
## Remove traits so that two traits are not more correlated than 0.7
decorrelate<-findCorrelation(
Targ_cor,
cutoff = 0.7,
verbose = FALSE,
names = FALSE
)
decorrelate<- decorrelate[ !(decorrelate %in% which(colnames(target_pheno) %in% c("ABA","Delta_13C", "RGR", "FT16", "Growth_rate")))]
target_pheno<-target_pheno[,-decorrelate]
target_pheno <- cbind(id, target_pheno)
head(target_pheno)
saveRDS(target_pheno, file="./data/TargetPhenoMatrix.rda")
## manually categorize phenotypes
phenoTypes <- data.frame(pheno=colnames(target_pheno), cat=rep(NA, 64))
phenoTypes$cat[grep("Root", phenoTypes$pheno)] <- rep("roots", length(grep("Root", phenoTypes$pheno)))
phenoTypes$cat[grep("root", phenoTypes$pheno)] <- rep("roots", length(grep("root", phenoTypes$pheno)))
phenoTypes$cat[c(grep("FT", phenoTypes$pheno), 40,48)] <- rep("floweing time", length(grep("FT", phenoTypes$pheno)))
phenoTypes$cat[grep("DTF", phenoTypes$pheno)] <- rep("floweing time", length(grep("DTF", phenoTypes$pheno)))
phenoTypes$cat[grep("Germ", phenoTypes$pheno)] <- rep("germination", length(grep("Germ", phenoTypes$pheno)))
phenoTypes$cat[c(grep("germ", phenoTypes$pheno),53)] <- rep("germination", length(grep("germ", phenoTypes$pheno))+1)
phenoTypes$cat[grep("DSDS", phenoTypes$pheno)] <- rep("dormancy", length(grep("DSDS", phenoTypes$pheno)))
phenoTypes$cat[grep("Dorm", phenoTypes$pheno)] <- rep("dormancy", length(grep("DSDS", phenoTypes$pheno)))
phenoTypes$cat[grep("Stom", phenoTypes$pheno)] <- rep("stomata", length(grep("Stom", phenoTypes$pheno)))
phenoTypes$cat[grep("Grow", phenoTypes$pheno)][1:2] <- rep("dormancy", 2)
phenoTypes$cat[grep("Grow", phenoTypes$pheno)][3:4] <- rep("growth", length(grep("Grow", phenoTypes$pheno))-2)
phenoTypes$cat[grep("RGR", phenoTypes$pheno)] <- rep("growth", length(grep("RGR", phenoTypes$pheno)))
phenoTypes$cat[c(grep("stom", phenoTypes$pheno),36)] <- rep("stomata", length(grep("stom", phenoTypes$pheno))+1)
phenoTypes$cat[grep("Rose", phenoTypes$pheno)] <- rep("growth", length(grep("Rose", phenoTypes$pheno)))
phenoTypes$cat[grep("Delta", phenoTypes$pheno)] <- rep("stress", length(grep("Delta", phenoTypes$pheno)))
phenoTypes$cat[c(grep("leaf", phenoTypes$pheno), 45, 50, 52)] <- rep("leaf", length(grep("leaf", phenoTypes$pheno)))
phenoTypes$cat[c(30, 31, 32, 58, 59, 63, 64 )]<- rep("stress", 7)
cats <- phenoTypes %>% count(cat)
TargetphenotypeCat<- ggplot(cats) + geom_col(aes(x = cat, y=n ), color='gray') + xlab("") + ylab("# phenotypes")+
theme(axis.text.x = element_text(angle = 45,hjust = 1)) +
# scale_fill_manual("",values = c("dormancy"=transparent("gray"),"floweing time"=transparent('gray'),
# "germination"=transparent("goldenrod1"),
# "growth"=transparent("yellow"), "roots"=transparent("limegreen"),
# "stomata"=transparent("dodgerblue1" ), "stress"=transparent("purple"),
# "leaf"=transparent("pink"))) +
theme(legend.position = c(.75, .7), legend.key.size =unit(.5, 'cm'),
legend.text = element_text(size=8), legend.title = element_blank())
TargetphenotypeCat
save(file="./figs/tmpobjects/TargetphenotypeCategories.rda", TargetphenotypeCat)
pdf("./figs/TargetphenotypeCat.pdf", width = 4, height = 3)
TargetphenotypeCat
dev.off()
}else{
load("./figs/tmpobjects/TargetphenotypeCategories.rda")
TargetphenotypeCat
}
```
```{r, echo=F, eval=T, warning=F, fig.cap="Figure 1A (in main text). PCA axes 1 (14.7%) and 2 (8.6%) of 64 target phenotypes for the subsetted 515 accessions for which we have corresponding common garden fitness data for.", fig.height=4, fig.width=6}
##This chunk makes Figure 1A.
RERUN=F
if(RERUN){
pheno<- read.table(file="./data/atlas1001_phenotypes_matrix.csv", sep=",", header = T)
fitness <- c(colnames(pheno)[grep("Fitness", colnames(pheno))][c(3,4,7,8)], colnames(pheno)[grep("Survival", colnames(pheno))][c(3,4,7,8)], colnames(pheno)[grep("Seed", colnames(pheno))][c(3,4,7,8)])
phenofit <- pheno[,colnames(pheno) %in% fitness]
phenofit[phenofit == -9] <- NA
which(phenofit==-9)
phenofit<-data.frame(id=pheno$V1, phenofit)
dim(phenofit)
phenofit <- phenofit %>% mutate(normFit = normalize.(rFitness_mlp)) %>%
mutate(normSurv = normalize.(rSurvival_fruit_mlp))
saveRDS(phenofit, file="./data/phenofit.rda")
fit_dat <- merge(target_pheno, by="id", phenofit)
## run pca
target_pheno<-readRDS(file="./data/TargetPhenoMatrix.rda")
target_pheno[1:5,1:5]
pcaTarget<-prcomp(target_pheno[,-1])
saveRDS(pcaTarget, file="./data/pcaTarget.rda")
load<-pcaTarget$sdev^2 /sum(pcaTarget$sdev^2)
load
cumsum(pcaTarget$sdev^2/sum(pcaTarget$sdev^2))
# Allload<-as.matrix(pcaTarget$rotation[,1:length(pcaTarget$sdev)])
#
# ## check loadings
# sort(abs(Allload[,1]), decreasing = T)[1:20] ## FT, Growth_rate
# sort(abs(Allload[,2]), decreasing = T)[1:20] ## Root growth
# sort(abs(Allload[,3]), decreasing = T)[1:20]
#
pcaallplot<-autoplot(pcaTarget,x = 1,
loadings = T, loadings.label = T,
loadings.colour = "darkgrey",loadings.label.colour="black",
loadings.label.size = 4.5)
plot_grid(pcaallplot)
library(RColorBrewer)
# df <- data.frame(pc1= pcaTarget$x[,1] ,
# pc2 = -pcaTarget$x[,2] ,
# pc3 = pcaTarget$x[,3])
df <- data.frame(pc1= pcaTarget$x[,1] * load[1],
pc2 = -pcaTarget$x[,2] * load[2],
pc3 = pcaTarget$x[,3] * load[1],
fit = fit_dat$normFit,
surv = fit_dat$normSurv)
## Make palette
normalize.<-function(x) (x-min(x,na.rm=T)) / (max(x,na.rm=T)-min(x,na.rm=T))
mypalette<-rgb(
1-normalize.(df$pc1),
normalize.(df$pc2),
normalize.(df$pc1)
)
# sort(abs(Allload[,1]), decreasing = T)[1:30] ## FT, Growth_rate
# sort(abs(Allload[,2]), decreasing = T)[1:30] ## Root growth
# sort(abs(Allload[,3]), decreasing = T)[1:20]
#png(file = './figs/PCrainbowScatterPlot_scaled.png')
pdf(file = './figs/PCrainbowScatterPlot_scaled.pdf')
PCplot_scaled <- ggplot(data=df, aes(x=pc1, y=pc2)) +
geom_point( aes(size=surv),color=mypalette, alpha=0.95) +
ylab("PC2")+ xlab("PC1 ") + ylim(-1,1) + xlim(-1,1.1)+
scale_size(range = c(2, 5)) +
geom_hline(yintercept=0, linetype="dotted", color="grey50")+
geom_vline(xintercept=0, linetype="dotted", color="grey50")+
theme(legend.position = "right", legend.text = element_text("none")) +
annotate("segment", x = 0, xend = -0.-0.02, y = 0, yend = -.94, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = -0.03, y =-.96, label = "% germination")+
annotate("segment", x = 0, xend = -.5, y = 0, yend = .32, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = -.62, y = .36, label = "dormancy") +
annotate("segment", x = 0, xend = .78, y = 0, yend = .23, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .83, y = .27, label = "flowering time") +
annotate("segment", x = 0, xend = .48, y = 0, yend = .16, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .54, y = .2, label = "vernalization") +
annotate("segment", x = 0, xend = .73, y = 0, yend = .1, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .88, y = .1, label = "delta_13C") +
annotate("segment", x = 0, xend = .65, y = 0, yend = .45, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .76, y = .46, label = "FLC exp") +
annotate("segment", x = 0, xend = .80, y = 0, yend = 0, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .92, y = -.01, label = "FRI exp") +
annotate("segment", x = 0, xend = .88, y = 0, yend =-.13, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = 1, y = -.17, label = "growth rate") +
annotate("segment", x = 0, xend = .42, y = 0, yend =-.12, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .43, y = -.14, label = "leaf area") +
annotate("segment", x = 0, xend = -.68, y = 0, yend = .18, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = -.74, y = .2, label = "RGR") +
annotate("segment", x = 0, xend = .26, y = 0, yend = .77, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .3, y =.81, label = "root RGR") +
annotate("segment", x = 0, xend = .1, y = 0, yend = .63, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .1, y =.67, label = "root angle") +
annotate("segment", x = 0, xend = -.15, y = 0, yend = -.8, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = -.1, y =-.85, label = "root horizontal index") +
annotate("segment", x = 0, xend = -.26, y = 0, yend = .34, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = -.3, y =.37, label = "ABA") +
# annotate("segment", x = 0, xend = -3.7, y = 0, yend = -6.3, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = -4.4, y =-6.5, label = "rhamnose") +
annotate("segment", x = 0, xend = .53, y = 0, yend = -.47, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
annotate("text", x = .7, y =-.5, label = "stomatal denisty")+
coord_equal()
plot_grid(PCplot_scaled)
dev.off()
save(PCplot_scaled, file="./figs/tmpobjects/Figure_1A_PCcolorgram_scaled.rda")
#png(file = './figs/PCrainbowScatterPlot.png')
# pdf(file = './figs/PCrainbowScatterPlot.pdf')
# PCplot <- ggplot(data=df, aes(x=pc1, y=pc2)) +
# geom_point(size=5, color=mypalette, alpha=0.95) +
# ylab("PC2")+ xlab("PC1 ") + ylim(-10,11) + xlim(-8,9) +
# geom_hline(yintercept=0, linetype="dotted", color="grey50")+
# geom_vline(xintercept=0, linetype="dotted", color="grey50")+
# theme(legend.position = "right", legend.text = element_text("none")) +
# annotate("segment", x = 0, xend = -0.-0.2, y = 0, yend = -9.4, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = -0.3, y =-9.6, label = "% germination")+
# annotate("segment", x = 0, xend = -5, y = 0, yend = 3.2, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = -6.2, y = 3.6, label = "dormancy") +
# annotate("segment", x = 0, xend = 7.3, y = 0, yend = 2, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 7.7, y = 2.5, label = "flowering time") +
# annotate("segment", x = 0, xend = 4.8, y = 0, yend = 1.6, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 5.4, y = 2, label = "vernalization") +
# annotate("segment", x = 0, xend = 5.7, y = 0, yend = 1, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 7, y = 1, label = "delta_13C") +
# annotate("segment", x = 0, xend = 6.2, y = 0, yend = 4.5, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 7.1, y = 4.6, label = "FLC exp") +
# annotate("segment", x = 0, xend = 7, y = 0, yend = 0, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 8, y = -.1, label = "FRI exp") +
# annotate("segment", x = 0, xend = 8.2, y = 0, yend =-1.3, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 7.9, y = -1.9, label = "growth rate") +
# annotate("segment", x = 0, xend = 4.2, y = 0, yend =-1.2, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 4.3, y = -1.4, label = "leaf area") +
# annotate("segment", x = 0, xend = -6.8, y = 0, yend = 1.8, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = -7.4, y = 2., label = "RGR") +
# annotate("segment", x = 0, xend = 2.6, y = 0, yend = 7.7, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 3, y =8.1, label = "root RGR") +
# annotate("segment", x = 0, xend = 1., y = 0, yend = 6.3, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 1, y =6.7, label = "root angle") +
# annotate("segment", x = 0, xend = -1.5, y = 0, yend = -8, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = -1, y =-8.5, label = "root horizontal index") +
# annotate("segment", x = 0, xend = -2.6, y = 0, yend = 3.4, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = -3, y =3.7, label = "ABA") +
# # annotate("segment", x = 0, xend = -3.7, y = 0, yend = -6.3, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# # annotate("text", x = -4.4, y =-6.5, label = "rhamnose") +
# annotate("segment", x = 0, xend = 5.3, y = 0, yend = -4.7, arrow = arrow(length = unit(.3,"cm")), color="grey20", size=.5) +
# annotate("text", x = 7, y =-3.8, label = "stomatal denisty")
#
# plot_grid(PCplot)
# dev.off()
# save(PCplot, file="./figs/tmpobjects/Figure_1A_PCcolorgram.rda")
}else{
load(file="./figs/tmpobjects/Figure_1A_PCcolorgram.rda")
plot_grid(PCplot)
}
```
The PCA captures 23% of the total phenotype variation in the first two axes (Fig. 1A). We were curious how this phenotypic variation looked in all of the 1001 genomes individuals within the Eurasian limits (999 ecotypes). We find the PCA with all of these ecotypes explains the same amount of variation in the first two axes (23.6%) as the PCA with the subset of samples.
```{r, echo=F, eval=T, fig.cap="Figure SI.5 PCA axes 1 (15.6%) and 2 (7.9%) of 64 target phenotypes for all Eurasian accessions of A. thaliana (n=999)."}
RERUN=F
if(RERUN){
atlasstrategies<-read.table("./data/pheno_fromgoogle.tsv",header = T)
pheno <- read.table(file = 'data/atlas1001_phenotype_matrix_imputed_withID.csv', sep=" ", header = T)
dimp<-read.csv(file = './data/atlas_phenotype_matrix_withid.csv')
tmp_data <-dimp[dimp[,1] %in% pheno$id,1:5]
tmp_data <- tmp_data %>% filter(longitude>-15 &longitude<90) %>% filter(latitude>32 &latitude<65)
tmp_pheno <- pheno[pheno$id %in% tmp_data$id,]
importanttraits <- read.table(file="./data/importanttraits.csv")
tmp_pheno <- tmp_pheno[,colnames(tmp_pheno) %in% importanttraits[,1]]
tmp_pheno <- apply(tmp_pheno,2,fn)
tmp_pheno <- apply(tmp_pheno,2,scale)
tmp_cor<-cor(tmp_pheno)
#corrplot(tmp_cor, method = "color", type = "lower", diag = F,tl.cex = .75)
## Remove traits so that two traits are not more correlated than 0.7
decorrelate<-findCorrelation(
tmp_cor,
cutoff = 0.7,
verbose = FALSE,
names = FALSE
)
decorrelate<- decorrelate[ !(decorrelate %in% which(colnames(tmp_pheno) %in% c("ABA","Delta_13C", "RGR", "FT16", "Growth_rate")))]
tmp_pheno<-tmp_pheno[,-decorrelate]
tmp_pca<-prcomp(tmp_pheno)
tmp_pca$sdev^2/sum(tmp_pca$sdev^2)
cumsum(tmp_pca$sdev^2/sum(tmp_pca$sdev^2))
pcaallplot<-autoplot(tmp_pca,
loadings = T, loadings.label = T, alpha=0.1,
loadings.colour = "darkgrey",loadings.label.colour="black",
loadings.label.size = 2.5)
saveRDS(file="./figs/tmpobjects/1001_pcaallplot.rda", pcaallplot)
pdf(file="./figs/1001_pcaLoadingsPlot.pdf", width = 6, height = 4)
plot_grid(pcaallplot)
dev.off()
}else{
pcaallplot <- readRDS(file="./figs/tmpobjects/1001_pcaallplot.rda")
# pcaallplot <- readRDS(file="./figs/tmpobjects/1001_pcaallplot_scaled.rda")
plot_grid(pcaallplot)
}
```
################################################################################
## I.3 Phenotype and Climate Associations
################################################################################
### Phenotype-phenotype associations
We measured how many phenotypes were correlated with flowering time, WUE, and growth rate using Pearson's correlation coefficient and a significance threshold of 0.05 (__Table SI.3__). We find that the phenotypic trade-off between WUE and flowering time is not an isolated trade-off, but actually involved in a massive suite of correlated complex traits related to both seasonal and drought adaptation. We find that 36-39% of all traits classified as escape or avoidance are significantly correlated with flowering time 32, 17% with WUE 28, and 25% with growth rate.
```{r, echo=F, eval=T, message=F, warning=F}
RERUN=F
if(RERUN){
library(Hmisc)
## correlation Ft and WUE with all escape/avoid phenotype variables
pheno <- read.csv(file = './data/atlas1001_phenotypes_matrix_MR.csv')
pheno[1:5,1:5]
d<-read.table("./data/pheno_fromgoogle.tsv",header = T)
head(d)
ss<- as.character(d$phenotype[d$stressstrategy=="Escape"][1:125])
ss2 <- as.character(d$phenotype[d$stressstrategy=="Avoidance"][1:384])
ss <- c(ss, ss2)
colnames(pheno) <- gsub("X", "", colnames(pheno))
sum(colnames(pheno) %in% ss)
ss_pheno <- pheno[,colnames(pheno) %in% ss]
#ss_pheno <- apply(ss_pheno, 2, as.numeric)
87/509
ft_wue <- cbind(ss_pheno[,"FT16"], ss_pheno[,"Delta_13C"], ss_pheno[,"FT10"], ss_pheno[,"FT_mlp"], ss_pheno[,"FT_mli"], ss_pheno[,"Growth_rate"])
colnames(ft_wue) <- c("FT16", "Delta_13C", "FT10", "FT_mlp", "FT_mli", "Growth_rate")
ss_pheno <- cbind(ft_wue, ss_pheno[, !colnames(ss_pheno) %in% c("FT16", "Delta_13C", "FT10", "FT_mlp", "FT_mli", "Growth_rate")])
ss_pheno[1:5,1:15]
dim(ss_pheno)
ss_pheno_cor_test <- rcorr(ss_pheno, type = "pearson")
ss_pheno_cor_test$r[ss_pheno_cor_test$P > 0.05] <- NA
dim(ss_pheno_cor_test$r)
ss_pheno_cor_test$r[,1:6]
ss_pheno_cors_signif <- data.frame(ss_pheno_cor_test$r[order(ss_pheno_cor_test$r[,2]),1:6])
dim(ss_pheno_cors_signif)
head(ss_pheno_cors_signif)
write.table(ss_pheno_cors_signif, file="./tables/FT_WUE_GR_TraitAssociations_signif_only_TableSI3.csv", sep=",",
quote = F, row.names = T, col.names = T)
### Summary of how many traits in escape and avoidance are signif correlated with FT and WUE
colSums(!is.na(ss_pheno_cors_signif))/506
}else{
ss_pheno_cors_signif <- read.table(file="./tables/FT_WUE_GR_TraitAssociations_signif_only_TableSI3.csv", sep=",", header = T)
colSums(!is.na(ss_pheno_cors_signif))/506
}
```
### Phenotype-latitude associations
Using the latitude of the original collection location associated with the focal 515 A. thaliana accessions, we estimated Pearson's correlation coefficient for the target 64 phenotypes and latitude. We also correlated latitude with the PC axes. We find that PC 1 is highly correlated with latitude across the natural range (Pearson’s r = 0.35 , p-value < 2.2x10-16), as are the phenotypes associated with PC1 (__Table SI.4__)
```{r, echo=F, eval=F, message=F, warning=F}
## This chunk makes Table SI.4
RERUN=F
if(RERUN){
target_pheno <- readRDS(file="./data/TargetPhenoMatrix.rda")
head(target_pheno)
dim(target_pheno)
# load field experiment information to get the 515 accessions
load("./data/d4.rda")
idsfield<-unique(d4$id)
## phenotype
dimp<-read.csv(file = './data/atlas_phenotype_matrix_withid.csv')
dimp <- dimp[,c("id", "name", "latitude", "longitude", "FT16", "Delta_13C", "X94_Seed_Dormancy" )]
index515_2029 <- which(dimp$id %in% idsfield)
head(dimp)
## correlation of target phenotypes and lattitude
tp_dimp <- merge(target_pheno, by.x="id", dimp, by.y="id")
head(tp_dimp)
head(tp_dimp[,c(67, 2:65)])
target_pheno_andLat <- tp_dimp[,c(67, 2:65)]
target_pheno_andLat_cors <- cor(target_pheno_andLat)
lat_pheno_cors <- target_pheno_andLat_cors[-1,1]
lat_pheno_cors <- data.frame(names(lat_pheno_cors), lat_pheno_cors)
colnames(lat_pheno_cors) <- c("phenotype","latitude")
lat_pheno_cors <- lat_pheno_cors[order(lat_pheno_cors[,2]),c(1,2)]
write.table(lat_pheno_cors, file="./tables/lat_targetpheno_cors_TableSI4.tsv", row.names = F, col.names = T, quote = F, sep="\t")
## PCs correlation with latitude
## also look at how PCs correlate with climate data
pcaTarget <- readRDS(file="./data/pcaTarget.rda")
pcs_withid <- cbind(id, pcaTarget$x)
head(pcaTarget)
head(pcs_withid)
load("./data/d4.rda")
idsfield<-unique(d4$id)
pheno <- read.csv(file = './data/atlas_phenotype_matrix_withid.csv')
whichfield<-which(pheno$id %in% idsfield)
idex515_2029<-whichfield
# saveRDS(idex515, file="./data/idex515_2029.rda")
#
# ## some specific correlation tests
head(df)
df_withpcs <- merge(df, by.x="id", pcs_withid, by.y="id")
head(df_withpcs)
dorm_lat_cor <- cor.test(df$dorm, df$lat)
#
pcs_clim <- data.frame(pc1 = pcaTarget$x[,1], pc2 = pcaTarget$x[,2],lat=dimp$latitude[idex515], long=dimp$longitude[idex515], clim[idex515, ])
pcs_clim_cor <- cor(pcs_clim, use = "pairwise.complete.obs", method = "pearson")
# pcs_clim_cor[order(pcs_clim_cor[,1]), 1:2]
# pc2 + cor with bio17, negative cor bio15
cor.test(y=df_withpcs$PC1,x= df_withpcs$lat, method = "pearson")
}else{
lat_pheno_cors <- read.table(file="./tables/lat_targetpheno_cors_TableSI4.tsv", header=T, sep="\t")
knitr::kable(lat_pheno_cors, col.names = colnames(lat_pheno_cors), caption = "Table SI.4 Pearson's correlation coefficient estimates of target phenotypes with latitude.", fixed_thread=T )
}
```
### Phenotype-climate associations
We downloaded bioclimatic, temperature, precipitation, and evapotranspiration rate estimates from WORLDCLIM 2.0 for all of the localities associated with the 1001 genomes accessions. We again subsetted the accessions to the 515 for which we have fitness data available and estimated Pearson's correlation coefficient for various associations of climate data with target phenotypes such as flowering time, WUE (measured as delta C13), and growth rate (__Table SI.5__). Additionally, we fit linear models of the climate data as a function of the target phenotypes. We did this both exclusively with just the climate data and phenotypes, but we also fit linear models using genomic principle components (PCs) from the 1001 genomes individuals and latitude as covariates in the model (__Fig. 1B__)
```{r, echo=F, eval=T, ,message=F, warning=F, fig.cap="Figure 1B,C,D (main text). (top) Flowering time and Delta_C13 association. (middle) Climate Associations with floweing time, and (bottom) delta_C13", fig.height=8, fig.width=3.5}
## This chunk makes Figures 1 B, C & D
RERUN=F
if(RERUN){
setwd("./safedata/natvar/")
library(raster)
library(missForest)
library(rgdal)
library(tidyverse)
library(Hmisc)
library(corrplot)
library(cowplot)
theme_set(theme_cowplot())
# load field experiment information to get the 515 accessions
load("./data/d4.rda")
idsfield<-unique(d4$id)
index515_2029 <- which(dimp$id %in% idsfield)
index515 <- which(dimp$id %in% idsfield)
## phenotype
dimp_2029 <- read.csv(file = './data/atlas_phenotype_matrix_withid.csv') ##2029
dimp2 <- dimp_2029[, c("id", "latitude", "longitude")]
head(dimp2)
dimp_1001 <- read.csv(file = './data/atlas1001_phenotypes_matrix_MR.csv')
dimp_1001[1:5,1:5]
dimp1 <- dimp_1001[, c("id", "FT16", "FT10", "FT_mli", "FT_mlp", "Flowering_time", "Delta_13C","Growth_rate", "X94_Seed_Dormancy", "rSurvival_fruit_mlp", "rSurvival_fruit_mli", "rFitness_mlp", "rFitness_mli" )]
dimp1[1:5,1:5]
# head(dimp2)
#dimp1[,-1] <- apply(dimp1[,-1], 2, scale)
dimp <- merge(dimp1, dimp2, by="id", all.x=T)
head(dimp)
## climate
clim <- read.table(file="./climate/2029gclimate.csv", sep=",",header = T)
#clim <- read.table(file="./climate/worldclim2/2029g_climate_accessions.csv", sep=",",header = T)
head(clim)
clim <- data.frame(id=dimp2$id, clim)
## Genetic PCs
gimp <- read.csv(file="./data/atlas_phenotype_matrix_imputedwithpcs.csv")
gimp <- data.frame(id=dimp2$id, gimp[,1:20])
cgimp <- merge(clim, gimp, by="id")
# head(dimp)
## merge back and only the 1001 accessions
df <- merge(dimp, cgimp, by="id")
head(df)
df <- data.frame(df)
## get mean of temp. min and max, mean precip, and meanpet
df$meanMaxTemp <- apply(df[,c(57:68)], 1, mean)
df$meanMinTemp <- apply(df[,c(45:51)], 1, mean)
df$meanPrecip <- apply(df[,c(33:39)], 1, mean)
df$meanPET <- apply(df[,c(69:75)], 1, mean)
df <- df %>% filter(longitude>-15 &longitude<90) %>% filter(latitude>32 &latitude<65)
head(df)
#### check key correlations
## wue and ft
cor.test(df$FT16, df$Delta_13C)
cor.test(df$FT16, df$bio18)
df <- apply(df[,-1], 2, as.numeric)
head(df)
dim(df)
df_cor_test <- rcorr(df[], type = "pearson")
## remove non-sig. corrs from table
df_cor_test$r[df_cor_test$P > 0.05] <- NA
sort(df_cor_test$r[,1])
df_cors_signif <- df_cor_test$r[order(df_cor_test$r[,1]),1:14]
head(df_cors_signif)
df_cors_mens <- df_cors_signif[grep("mean", rownames(df_cors_signif)),]
df_cors_bio <- df_cors_signif[grep("bio", rownames(df_cors_signif)),]
df_cors_prec <- df_cors_signif[grep("prec", rownames(df_cors_signif)),]
df_cors_temp <- df_cors_signif[c(grep("tmin", rownames(df_cors_signif)), grep("tmax", rownames(df_cors_signif))),]
write_out <- rbind(df_cors_mens, df_cors_bio,df_cors_prec, df_cors_temp )
write.table(write_out, file="./tables/Trait-Clima-Assoc-SuppT4.csv", sep=",",
quote = F, row.names = T, col.names = T)
# write.table(df_cors_bio, file="./tables/TraitBIOCLIMAssociations_signif_only_notlimitedto515.csv", sep=",",
# quote = F, row.names = T, col.names = T)
#
# write.table(df_cors_signif, file="./tables/TraitClimateAssociations_signif_only.csv", sep=",",
# quote = F, row.names = T, col.names = T)
# df_cor[1:5,1:5]
# df_cor <- df_cor[order(df_cor[,1]),1:6]
# #df_cor[grep("bio", rownames(df_cor)),]
# write.table(df_cor, file="./tables/TraitClimateAssociations_TableSI5.csv", sep=",",
# quote = F, row.names = T, col.names = T)
# df_cor <- read.table(file="./tables/TraitClimateAssociations_TableSI5.csv", sep=",", header=T)
#
plot(lm(df$FT16~df$bio12))
plot(y=df$FT16, x=df$bio12)
cor.test(df$FT16, df$bio1)
## look at correlation of ft and wue, while accounting for latitude and 7 genetic PCs
df_i <- df[index515, ]
df[1:5,1:5]
mypalette <- readRDS(file="./data/mypalette.rda")
colnames(df)[3:16] <- c("lat", "long", "ft", "ft2", "ft3","ft4","ft5", "wue", "gr", "dorm", "survmlp", "survmli", "fitmlp", "fitmli")
df <- data.frame(df)
fit <- lm(wue ~ ft + lat + GPC1 + GPC2 + GPC3 + GPC4 + GPC5 + GPC6 + GPC7 , data = df)
summary(fit)
df$wue
lildf <- na.omit(data.frame(ft = df$FT16,
bio= df$bio17,
pal = mypalette))
df$ft
cor.test(lildf$ft, lildf$bio)
ft_annprecip <- ggplot(lildf, aes(x=bio, y=ft)) + geom_point(color=lildf$pal, alpha=0.7) +
scale_color_gradient(low="white", high="black") + #ylim(1.75,5) +
geom_smooth(aes(x=bio, y=ft), method=glm , color="#b2182b", se=T ) +
labs(x="mean Annual precip. (mm)", y="Flowering time @ 16C")
ft_annprecip
pdf(file="./figs/ft_annual_precip_plot.pdf", height = 4, width = 4)
ft_annprecip
dev.off()
summary(lm(data=df_2029, FT16 ~ bio12 + latitude + GPC1 + GPC2 + GPC3 + GPC4 + GPC5 ))
#dim(justphenos)
df$survmli[df$survmli==-9] <- NA
df$fitmli[df$fitmli==-9] <- NA
normalize.<-function(x) (x-min(x,na.rm=T)) / (max(x,na.rm=T)-min(x,na.rm=T))
#df$survmlp <- scale(unlist(df$survmlp))
df$survmlp <- normalize.(df$survmlp)
df$fitmlp <- normalize.(df$fitmlp)
df$survmli <- normalize.(df$survmli)
hist(df$survmlp)
r2 <- cor.test(df$wue, df$ft, method = "pearson")
r2
hist(df$survmlp)
lildf <- na.omit(data.frame(ft = df$ft,
wue = df$wue,
fit = df$fitmlp,
fit2 = df$survmlp,
pal = mypalette))
cor.test(lildf$ft, lildf$wue)
range(lildf$fit2)
breaks <- c(0, .25, .5, .75, 1) # Define the breaks
sizes <- c(1, 1.5, 2, 3.5, 5) # Define the sizes corresponding to each break
ft_wue_plot <- ggplot(lildf, aes(x=ft, y=wue)) + geom_point(aes(size=fit2), color=lildf$pal, alpha=0.7) +
scale_color_gradient(low="white", high="black") + #ylim(1.75,5) +
geom_smooth(aes(x=ft, y=wue), method=glm , color="#b2182b", se=T ) +
labs(x="Flowering time @ 16C", y="Delta_13C") +
scale_size(limits = c(0.53, 0.82), range = c(2, 5))
ft_wue_plot
plot_grid(ft_wue_plot, ft_wue_plot2, ft_wue_plot3, ft_wue_plot4, labels = c("surv.mlp", "surv.mli", "fit.mlp", "fit.mli"))
pdf(file="./figs/FtWUEscatterPlot.pdf", width = 4, height = 3)
ft_wue_plot
dev.off()
colnames(df)
df$meanMaxtemp <- apply(df[,c(58:69)], MARGIN = 1, FUN = mean)
head(df)
fit <- lm(FT16 ~ meanMaxtemp + latitude + GPC1 + GPC2 + GPC3 + GPC4, data = df)
summary(fit)
r2 <- cor.test(x=df$meanMaxtemp, y=df$FT10)
r2
lildf <- na.omit(data.frame(ft = df$ft,
bio1 = df$bio17,
fit = df$fitmlp,
fit2 = df$survmlp,
pal = mypalette))
FT_temp_plot_color <- ggplot(lildf, aes(y=ft, x=bio1)) +
geom_point(aes(size=fit2), color=lildf$pal, alpha=.7) +
scale_color_gradient(low="white", high="black") +
geom_smooth(aes(y=ft, x=bio1), method=glm , color="#b2182b", se=T ) +
labs(y="Flowering time (days)", x="Precip. of Warmest Quarter") +
xlim(50,250) +
scale_size(limits = c(0.53, 0.82), range = c(2, 5))
FT_temp_plot_color
pdf(file="./figs/wue-ft-climate-ft.pdf", width = 4.3, height = 6)
plot_grid(ft_wue_plot, FT_temp_plot_color, ncol=1, nrow=2)
dev.off()
fit <- lm(FT16 ~ Athprecip + latitude + GPC1 + GPC2 + GPC3 + GPC4, data = df)
summary(fit)
colnames(df)
colnames(df)
df$Athprecip <- apply(df[,c(34:40)], MARGIN = 1, FUN = mean)
r2 <- cor.test(x=df$Athprecip, y=df$FT16)
r2
lildf <- na.omit(data.frame(wue = df$wue,
bio1 = df$bio12,
pal = mypalette))
wue_AthPrecip_plot_color <- ggplot(lildf, aes(y=wue, x=bio1)) + geom_point(color=lildf$pal, cex=3, alpha=.7) +
scale_color_gradient(low="white", high="black") +
geom_smooth(aes(y=wue, x=bio1), method=glm , color="#b2182b", se=T ) +
labs(y="Flowering time (days)", x="Mean Precip. Jan-Jul (mm)")
wue_AthPrecip_plot_color
wue_pet_plot <- ggplot(df) + geom_point(aes(y=wue, x=meanpet, color=lat), cex=2, alpha=1) +
scale_color_gradient(low="white", high="black") + xlim(1.75,5) +
geom_smooth(aes(y=wue, x=meanpet), method=glm , color="#b2182b", se=T ) +
labs(y="Delta_13C", x="Mean Evapotranspiration")
# + annotate("text", y = -32, x =4.1, label = paste0("italic(p) <",
# format(lm$p.value, digits=2)), parse = TRUE, size=5) +
# annotate("text", y = -32.5, x = 4.1, label = paste("italic(R^2) ==",
# format(lm$estimate, digits=2)), parse = TRUE, size=5, col="#b2182b")
#wue_pet_plot
pdf(file="./figs/EvaptWUEscatterPlot.pdf")
wue_pet_plot
dev.off()
dim(df)
## try something other than summer precip (biol17), trying biol 16 precip in wettest quarter
head(df)
colnames(df)
df$lifetimePET <- apply(df[,c(65:69)], FUN = mean, MARGIN = 1)