-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_process_data.R
1414 lines (1007 loc) · 51.8 KB
/
1_process_data.R
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
#' @author Brian Maitner
# Load libraries
library(tidyverse)
library(arrow)
source("R/try_to_parquet.R")
##########################################################
library(data.table)
ferns <- fread("manual_downloads/Darwinian_shortfalls/fern_list.txt", quote = "", header = F, col.names = "family") # list of ferns
moss <- fread("manual_downloads/Darwinian_shortfalls/bryophyta_list.txt", quote = "", header = F, col.names = "family") # list of mosses
# Load WCVP
wcvp <- read.table(file = "manual_downloads/WCVP/wcvp_distribution.txt",
sep = "|",
header = TRUE,
quote = "",
fill = TRUE,
encoding = "UTF-8")
# How much of the world is covered by the wcvp?
# tdwg <- read_sf("manual_downloads/TDWG/old_lv3/level3.shp")
#
# tdwg %>%
# st_transform(crs = st_crs(6933)) %>%
# mutate(area = st_area(geometry)) %>%
# mutate(in_wcvp = LEVEL_3_CO %in% wcvp$area_code_l3) -> tdwg
#
# tdwg$LEVEL_NAME[which(tdwg$in_wcvp == FALSE)] # Only Bouvet Island doesn't have records. They also have no vascular plants.
#
# tdwg %>%
# group_by(in_wcvp) %>%
# summarise(total_area = sum(area)) %>%
# st_drop_geometry() %>%
# ungroup() %>%
# mutate(all_area = sum(total_area))%>%
# mutate(pct_area = (total_area/all_area) * 100)-> tdwg
#
# tdwg
#
# rm(tdwg)
wcvp_names <- read.table(file = "manual_downloads/WCVP/wcvp_names.txt",
sep = "|",
header = TRUE,
quote = "",
fill = TRUE,
encoding = "UTF-8")
merge(x = wcvp,
y = wcvp_names,
all.x = TRUE) %>%
filter(taxon_rank == "Species") %>% #include only species
filter(taxon_status == "Accepted") %>% #only accepted names
filter(!is.na(accepted_plant_name_id)) %>% #only accepted names
filter(extinct == 0) %>% #only extant species
filter(introduced == 0) -> wcvp #exclude introduced
# Remove or correct mistaken Area codes
shape <- rgdal::readOGR("manual_downloads/Darwinian_shortfalls/level3.shp")
wcvp$area_code_l3 <- toupper(wcvp$area_code_l3)
wcvp <- wcvp[wcvp$area_code_l3 %in% shape$LEVEL_3_CO,]
#Toss unneeded columns
wcvp %>%
select("area_code_l3","introduced","extinct","location_doubtful","taxon_rank","taxon_status","family","genus","species","taxon_name") -> wcvp
rm(wcvp_names)
#Make sure mosses are not present mosses
wcvp <- subset(wcvp, !family %in% moss$family)
#Make a duplicate dataset without ferns (for comparison to the Darwinian paper)
wcvp_no_ferns <- subset(wcvp, !family %in% ferns$family)
# saveRDS(object = wcvp, file = "manual_downloads/WCVP/wcvp_cleaned.RDS")
# saveRDS(object = wcvp_no_ferns, file = "manual_downloads/WCVP/wcvp_cleaned_no_ferns.RDS",)
n_species <- length(unique(wcvp$taxon_name))
############################################################
# Get TRY data
# Properly format a list of traits to be pasted into their website
# This needs to be broken into smaller chunks so that the TRY website can handle it
# traits <- read.table(file = "manual_downloads/TRY_trait_list_10_03_2022.txt",
# skip = 3,
# sep = "\t",
# stringsAsFactors = FALSE)
#
#
# traits <- traits[,1:5]
# traits$ObsNum <- as.numeric(traits$ObsNum)
# colnames(traits) <- traits[1,]
# traits <- traits[2:nrow(traits),]
#
# traits <- traits[order(traits$ObsNum,decreasing = TRUE),]
#
# paste(traits$TraitID[1:10],collapse = ",") # 19811
# paste(traits$TraitID[11:20],collapse = ",") # 19812
# paste(traits$TraitID[21:100],collapse = ",") # 19822
# paste(traits$TraitID[101:1000],collapse = ",") # 19823
# paste(traits$TraitID[1001:length(traits$TraitID)],collapse = ",") # 19824
# Convert the dataset to a more useful format
# 19811
# try_to_parquet(file = "manual_downloads/TRY/03-11-2022/19811.txt",
# output_directory = "manual_downloads/TRY/TRY_parquet/",
# batch_size = 70000)
# 19812
# try_to_parquet(file = "manual_downloads/TRY/03-11-2022/19812.txt",
# output_directory = "manual_downloads/TRY/TRY_parquet/",
# batch_size = 80000)
# 19822
# try_to_parquet(file = "manual_downloads/TRY/03-11-2022/19822.txt",
# output_directory = "manual_downloads/TRY/TRY_parquet/",
# batch_size = 80000)
# 19823
# try_to_parquet(file = "manual_downloads/TRY/03-11-2022/19823.txt",
# output_directory = "manual_downloads/TRY/TRY_parquet/",
# batch_size = 80000)
# 19824
# try_to_parquet(file = "manual_downloads/TRY/03-11-2022/19824.txt",
# output_directory = "manual_downloads/TRY/TRY_parquet/",
# batch_size = 80000)
# "Load" the full dataset (everything in the folder)
traits <- arrow::open_dataset(sources = "manual_downloads/TRY/TRY_parquet/")
# examine the data structure
traits$schema
traits$metadata
traits %>%
select(AccSpeciesName) %>%
collect() %>%
unique() -> trait_sp_names
traits %>%
select(TraitName) %>%
collect() %>%
unique() -> traits_names_og
library(TNRS)
#matching the trait names using WCVP for consistency
# TNRS(taxonomic_names = trait_sp_names,
# sources = "wcvp") -> tnrsed_trait_sp_names
#saveRDS(object = tnrsed_trait_sp_names,file = "data/tnrsed_names.RDS")
tnrsed_trait_sp_names <- readRDS("data/tnrsed_names.RDS")
#generate a list of traits for annotation
# we want to know the number of species with data
# traits %>%
# filter(!is.na(TraitID)) %>%
# group_by(AccSpeciesName, TraitName) %>%
# count() %>%
# ungroup() %>%
# group_by(TraitName) %>%
# count() %>%
# collect() %>%
# mutate(pct_coverage = round(x = (n/n_species) *100,digits = 2)) %>%
# write.csv(file = "manual_downloads/trait_annotation/trait_list.csv",
# row.names = FALSE)
trait_list <- read.csv("manual_downloads/trait_annotation/trait_list.csv")
#fix the weird characters that break
trait_list %>%
mutate(TraitName = gsub(pattern = "…", replacement = "…",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "â€\u009d", replacement = "”",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "“", replacement = "“",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "“", replacement = "“",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "Â",
replacement = "",
x=TraitName,fixed = TRUE))-> trait_list
hist(log10(trait_list$n), breaks = 100, xlab = "log10(n species with data)", main = "")
# Count the number of observations per species x trait
#this bit of code makes the first pass at the estimating the number of species per trait
traits %>%
filter(!is.na(TraitID))%>%
group_by(AccSpeciesName, TraitName, TraitID) %>%
count() %>%
collect() -> trait_summary #1 636 710
# this bit of code "corrects" erroneous TraitIDs if multiple exist for the same trait name. It simply assumes the more common TraitID is correct
trait_summary %>%
group_by(TraitName,TraitID) %>%
summarise(total_n = sum(n)) %>%
mutate(max_n = max(total_n)) %>%
filter(total_n == max_n) %>%
select(TraitName,TraitID) %>%
inner_join(trait_summary %>%
rename(raw_TraitID = TraitID)) %>%
group_by(TraitName,TraitID,AccSpeciesName)%>%
summarize(n = sum(n)) -> trait_summary
trait_summary %>%
select(TraitName,TraitID)%>%
unique() -> tID_lookup
#make sure names are still all good
if(any(which(!tID_lookup$TraitName %in% traits_names_og$TraitName))){
stop("check trait names")
}
#correct or toss names that cannot be matched
trait_summary[which(trait_summary$AccSpeciesName %in% wcvp$taxon_name),] -> good_names
trait_summary[which(!trait_summary$AccSpeciesName %in% wcvp$taxon_name),] -> bad_names
(nrow(good_names)+nrow(bad_names))==nrow(trait_summary)
#how many names could be matched exactly?
length(unique(trait_summary$AccSpeciesName)) #171663
length(unique(good_names$AccSpeciesName)) #116896
length(unique(good_names$AccSpeciesName))/
length(unique(trait_summary$AccSpeciesName)) #68% of names match the wcvp exactly, 116,896
length(unique(bad_names$AccSpeciesName))/
length(unique(trait_summary$AccSpeciesName)) #32% of names aren't matched
#54,767 bad names
merge(x = bad_names,
y = tnrsed_trait_sp_names,
by.x = "AccSpeciesName",
by.y = "Name_submitted",
all.x = TRUE,
all.y = FALSE) -> bad_names
#how many bad names can be matched to a species?
length(unique(bad_names$AccSpeciesName[which(!bad_names$Accepted_species == "")]))/
length(unique(bad_names$AccSpeciesName)) #67% can be matched to a species (36825/54767)
length(unique(bad_names$AccSpeciesName[which(bad_names$Accepted_species == "")]))/
length(unique(bad_names$AccSpeciesName)) #32% aren't matched to species, 17931/54767
bad_names %>%
#filter(Accepted_name_rank == "species") %>% #toss names that couldn't be matched to species
filter(Accepted_species != "") %>% #toss names that couldn't be matched to species
select(AccSpeciesName, TraitName, n, Accepted_species) %>%
mutate(acc_name_og = AccSpeciesName)%>%
mutate(AccSpeciesName = Accepted_species) %>%
select(-Accepted_species) -> bad_names
#Fix hybrid notation issue
# wcvp uses "×" for hybrids
# TNRS uses "x"
bad_names$AccSpeciesName <- gsub(pattern = " x ",
replacement = " × ",
x = bad_names$AccSpeciesName)
#How many names do we get to keep?
length(unique(bad_names$acc_name_og[which(bad_names$AccSpeciesName %in% wcvp$taxon_name)]))#35548
length(unique(bad_names$acc_name_og[which(!bad_names$AccSpeciesName %in% wcvp$taxon_name)]))#1277
#Toss this field now that I have the counts
bad_names %>%
select(-acc_name_og) -> bad_names
#Only keep TNRSed names that match to our list of accepted taxa
bad_names[which(!bad_names$AccSpeciesName %in% wcvp$taxon_name),] -> bad_names_to_toss
bad_names[which(bad_names$AccSpeciesName %in% wcvp$taxon_name),] -> bad_names
length(unique(bad_names$AccSpeciesName)) #25881
length(unique(bad_names_to_toss$AccSpeciesName))#1102
#% of names that can be rescued = 25881/54767 = 47%
#36825 + 17931 (species vs non-species)
#35548 + 1277 (kept vs tossed)
#35548/54767 # 65% kept
#17931/54767 # 33 tossed (no species)
#1277/54767 # 2% tossed (not in WCVP)
#Add the totals for the good and bad names together (needed in case a bad name matched to a good name that was already present)
rbind(good_names, bad_names) %>%
group_by(AccSpeciesName, TraitName) %>%
summarize(n = sum(n)) -> trait_summary # 1 463 531
#get counts of # of traits, # of species
length(unique(trait_summary$AccSpeciesName)) #130 305
length(unique(trait_summary$TraitName)) #1918
trait_summary %>%
select(-n)%>%
unique()%>%
nrow() #1 463 531
#make sure names are still all good
if(any(which(!trait_summary$TraitName %in% traits_names_og$TraitName))){
stop("check trait names")
}
##########################################################
# Selecting focal traits
#update trait list per the new coverage
trait_summary %>%
group_by(AccSpeciesName, TraitName) %>%
count() %>%
ungroup() %>%
group_by(TraitName) %>%
count() %>%
collect() %>%
mutate(pct_coverage_clean = (n/n_species)*100) %>%
rename(n_clean = n)-> trait_list_v2
merge(x = trait_list, y = trait_list_v2, by = "TraitName", all = TRUE) ->
trait_list
rm(trait_list_v2)
trait_list$n_clean[which(is.na(trait_list$n_clean))] <- 0
trait_list$pct_coverage_clean[which(is.na(trait_list$pct_coverage_clean))] <- 0
# How many traits have at least 1% coverage
length(which(trait_list$pct_coverage_clean >= 1)) #82
#2027-82 = 1945 excluded by this
# How many traits with observation of at least 1%, and are general?
length(which(trait_list$pct_coverage_clean >= 1 &
trait_list$general == 1)) #53
#82 - 53 = 29 traits excluded by these criteria
# Completeness per trait
#Best coverage
trait_list[which.max(trait_list$pct_coverage_clean),]
#Worst coverage
trait_list[which.min(trait_list$n_clean),]
length(which(trait_list$n_clean==1))
length(which(trait_list$n_clean==0))
length(which(trait_list$n_clean <= 1))
hist(trait_list$pct_coverage_clean,breaks = 100,main = "Histogram of Trait Coverage",xlab = "Percent Coverage")
hist(log10(trait_list$pct_coverage_clean),breaks = 100,main = "Histogram of Trait Coverage",xlab = "log(Percent Coverage)")
trait_list %>%
ggplot(mapping = aes(pct_coverage_clean))+
geom_histogram(fill = "magenta")+
#xlim(c(0,10000))+
scale_x_log10(labels = scales::label_number(accuracy = .0001),
breaks=c(0,0.0001,0.001,0.01,.1,1,10,100),
limits=c(0.0001,100))+
xlab("Percent Completeness (log scale)")+
ggtitle("Histogram of Trait Completeness")+
geom_vline(xintercept = 1,lty=2) -> trait_hist
ggsave(filename = "plots/trait_histogram.jpg",
plot = trait_hist,height = 3,width = 5,units = "in")
#averages
mean(na.omit(trait_list$pct_coverage_clean))# 0.21 %
median(na.omit(trait_list$pct_coverage_clean))# 0.0051 %
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
Mode(trait_list$pct_coverage_clean)
Mode(trait_list$n_clean)
#subset to traits with 1% coverage or more
trait_list[which(trait_list$pct_coverage_clean >= 1),] %>%
arrange(pct_coverage_clean)
trait_list[which(trait_list$pct_coverage_clean >= 1),] %>%
summarise(mean_coverage = mean(pct_coverage_clean),
median_coverage = median(pct_coverage_clean),
Mode_coverage = Mode(pct_coverage_clean))
traits_for_main_analysis <-
trait_list %>%
filter(pct_coverage_clean >= 1 & general == 1)
trait_summary_for_main_analysis <-
trait_summary %>%
filter(TraitName %in% traits_for_main_analysis$TraitName)
# saveRDS(object = trait_summary,file = "data/trait_summary_overall.RDS")
# saveRDS(object = trait_summary_for_main_analysis,file = "data/trait_summary_for_main_analysis.RDS")
# saveRDS(object = trait_list,file = "data/trait_list_w_coverage.RDS")
# saveRDS(object = tID_lookup,file = "data/tID_lookup.RDS")
trait_summary_for_main_analysis <- readRDS("data/trait_summary_for_main_analysis.RDS")
trait_list_w_coverage <- readRDS("data/trait_list_w_coverage.RDS")
# Per reviewer 2's suggestion, create a table containing traits and coverage for the 55 focal traits
trait_summary_for_main_analysis %>%
group_by(TraitName) %>%
summarise(species_with_data = n()) %>%
inner_join(y = tID_lookup ) %>%
arrange(-species_with_data) -> focal_trait_coverage
# write.csv(x = focal_trait_coverage,
# file = "tables/focal_trait_coverage.csv",row.names = FALSE)
focal_trait_coverage %>%
mutate(pct_species_with_data = (species_with_data/n_species)*100) -> focal_trait_coverage
min(focal_trait_coverage$pct_species_with_data) #1.00 %
max(focal_trait_coverage$pct_species_with_data) #32.28 %
mean(focal_trait_coverage$pct_species_with_data) #3.68 %
median(focal_trait_coverage$pct_species_with_data) #1.88
###########################################################
# We need a dataset that lists completeness as a function of country x trait, which we can join to the shapefile for plotting or use in other analyses
# try: species, trait, number of observations
# wcvp: species, country
source("R/get_trait_coverage.R")
# trait_coverage <-
# get_trait_coverage(wcvp = wcvp,
# trait_summary = trait_summary_for_main_analysis)
#
# saveRDS(object = trait_coverage,
# file = "data/focal_trait_coverage.rds")
# family_trait_coverage
# source("R/get_family_trait_coverage.R")
#
# family_trait_coverage <-
# get_family_trait_coverage(wcvp = wcvp,
# trait_summary = trait_summary_for_main_analysis,
# temp_file = "temp/temp_family_trait_coverage.RDS")
#
# saveRDS(object = family_trait_coverage,
# file = "data/focal_trait_coverage_family.rds")
family_trait_coverage <- readRDS("data/focal_trait_coverage_family.rds")
trait_coverage <- read_rds("data/focal_trait_coverage.rds")
# TDWG polygons from https://github.com/tdwg/wgsrpd/tree/master/level3 on 3/25/2022
colnames(trait_coverage)
coverage_wide <-
trait_coverage %>%
pivot_wider(id_cols = area,
names_from = trait,
values_from = completeness)
countries <- sf::read_sf("manual_downloads/TDWG/level3.shp")
plot(countries[1])
countries <-merge(x = countries,
y = coverage_wide,
by.x = "LEVEL3_COD",
by.y = "area")
ggplot(data = countries)+
geom_sf(aes(fill = 100 * `Leaf dry mass per leaf fresh mass (leaf dry matter content, LDMC)`))+
scale_fill_viridis_c(option = "plasma")+
labs(fill = "%")+
ggtitle("Leaf dry mass per leaf fresh mass (leaf dry matter content, LDMC)")
ggplot(data = countries)+
geom_sf(aes(fill = 100 * `Plant growth form`))+
scale_fill_viridis_c(option = "plasma")+
labs(fill = "%")+
ggtitle("Plant growth form")
ggplot(data = countries)+
geom_sf(aes(fill = 100 * `Plant height vegetative`))+
scale_fill_viridis_c(option = "plasma")+
labs(fill = "%")+
ggtitle("Plant height vegetative")
ggplot(data = countries)+
geom_sf(aes(fill = 100 * `Leaf area per leaf dry mass (specific leaf area, SLA or 1/LMA): undefined if petiole is in- or excluded`))+
scale_fill_viridis_c(option = "plasma")+
labs(fill = "%")+
ggtitle("Leaf area per leaf dry mass (specific leaf area, SLA or 1/LMA): undefined if petiole is in- or excluded")
# Numbers for trait coverage
min(trait_coverage$completeness) # 0 pct completeness
max(trait_coverage$completeness) # 100 pct completeness
mean(trait_coverage$completeness) #19.4 %
median(trait_coverage$completeness) # 12.7 %
mean_completeness_across_country_trait <-
trait_coverage %>%
group_by(trait) %>%
summarise(mean_coverage = mean(completeness))
min(mean_completeness_across_country_trait$mean_coverage)*100 #5.93
max(mean_completeness_across_country_trait$mean_coverage)*100 # 70.63
mean(mean_completeness_across_country_trait$mean_coverage)*100 # 19.39
median(mean_completeness_across_country_trait$mean_coverage)*100 #15.42
##############################################################################
#Trait coverage across all traits
# source("R/get_trait_coverage.R")
# wcvp <-readRDS(file = "manual_downloads/WCVP/wcvp_cleaned.RDS")
# trait_summary <- readRDS(file = "data/trait_summary_overall.RDS")
#
# total_trait_coverage <-
# get_trait_coverage(wcvp = wcvp,
# trait_summary = trait_summary)
#
#
# saveRDS(object = total_trait_coverage,file = "data/total_trait_coverage.RDS")
total_trait_coverage <- readRDS("data/total_trait_coverage.RDS")
##############################################################################
# Generate a dataset of traits measured within given botanical countries
# First, we'll wrangle the metadata of 3 types: country, state, latitude and longitude
#ObservationID is a primary key
#
traits <- arrow::open_dataset(sources = "manual_downloads/TRY/TRY_parquet/")
traits %>%
select(DataName,TraitID)%>%
filter(is.na(TraitID)) %>%
select(DataName)%>%
collect()%>%
unique() -> trait_md_options
# Get MD useful for inferring trait location (country or state or lat/long)
#this will include anything with country, lat/long, or state, EXCEPT where they include qualifiers that cast doubt on the location
traits %>%
filter(is.na(TraitID)) %>%
select(ObservationID, DataName, OrigValueStr)%>%
filter((grepl(pattern = "country",ignore.case = TRUE,x = DataName)|
grepl(pattern = "state",ignore.case = TRUE,x = DataName)|
grepl(pattern = "latitude",ignore.case = TRUE,x = DataName)|
grepl(pattern = "longitude",ignore.case = TRUE,x = DataName)) &
!grepl(pattern = "provenance",ignore.case = TRUE,x = DataName) &
!grepl(pattern = "origin",ignore.case = TRUE,x = DataName) &
!grepl(pattern = "maximum",ignore.case = TRUE,x = DataName)&
!grepl(pattern = "minimum",ignore.case = TRUE,x = DataName)) %>%
collect() %>%
unique() %>%
pivot_wider(id_cols = ObservationID,
names_from = DataName,
values_from = OrigValueStr) -> useful_md #707k
source("R/get_countries.R")
tdwg <- read_sf("manual_downloads/TDWG/old_lv3/level3.shp")
useful_md <- get_countries(useful_md = useful_md, tdwg = tdwg)
#How many of the records couldn't be assigned to a bot country?
#og useful md is 707 402 if same number isn't returned, modify to record og number
useful_md %>%
select(ObservationID, LEVEL_NAME) %>%
filter(is.na(LEVEL_NAME)) -> useless_md
#Toss anything without a LEVEL3 name
useful_md %>%
select(ObservationID, LEVEL_NAME) %>%
filter(!is.na(LEVEL_NAME)) -> useful_md
message(nrow(useless_md)/(nrow(useless_md)+nrow(useful_md))*100,"% of metadata cannot be used due to errors, etc.")
#8% of md can't be used
#Now, pull only the trait observations with an ObservationID in the useful_md dataset
#and append country to traits where possible
traits %>%
filter(!is.na(TraitID)) %>%
select(ObservationID, AccSpeciesName, TraitName) %>%
filter(ObservationID %in% useful_md$ObservationID) %>%
collect() %>%
inner_join(y = useful_md,by = "ObservationID")%>%
group_by(AccSpeciesName, TraitName, LEVEL_NAME) %>%
count() -> stc #stc = species + trait + country
#matching the species names using WCVP for consistency (note that this could be better done by simply using the previous set of TNRS output. But I've already written the code, so meh.)
# TNRS(taxonomic_names = unique(stc$AccSpeciesName),
# sources = "wcvp") -> tnrsed_stc_names
#
# saveRDS(object = tnrsed_stc_names,file = "data/tnrsed_georef_names.RDS")
tnrsed_stc_names <- readRDS("data/tnrsed_georef_names.RDS")
stc[which(stc$AccSpeciesName %in% wcvp$taxon_name),] -> good_stc_names
stc[which(!stc$AccSpeciesName %in% wcvp$taxon_name),] -> bad_stc_names
nrow(good_stc_names)/nrow(stc) #81.95 % of georef names match exactly
nrow(bad_stc_names)/nrow(stc) #18.05 % of georef names don't match exactly
merge(x = bad_stc_names,
y = tnrsed_stc_names,
by.x = "AccSpeciesName",
by.y = "Name_submitted",
all.x = TRUE,
all.y = FALSE) %>%
#filter(Accepted_name_rank == "species") %>% #toss names that couldn't be matched to species
filter(Accepted_species != "") %>% #toss names that couldn't be matched to species
select(AccSpeciesName, TraitName,LEVEL_NAME, n, Accepted_species) %>%
mutate(AccSpeciesName = Accepted_species) %>%
select(-Accepted_species) -> bad_stc_names
#Fix hybrid notation issue
# wcvp uses "×" for hybrids
# TNRS uses "x"
bad_stc_names$AccSpeciesName <- gsub(pattern = " x ",
replacement = " × ",
x = bad_stc_names$AccSpeciesName)
#Toss names that don't match to our list of accepted taxa
nrow(bad_stc_names[which(!bad_stc_names$AccSpeciesName %in% wcvp$taxon_name),])/nrow(bad_stc_names) #only 3.70% of the non-matching georef names could not be rescued
nrow(bad_stc_names[which(bad_stc_names$AccSpeciesName %in% wcvp$taxon_name),])/nrow(bad_stc_names) # 96.30% of the non-matching georef names COULD be rescued
bad_stc_names[which(bad_stc_names$AccSpeciesName %in% wcvp$taxon_name),] -> bad_stc_names
#Add the totals for the good and bad names together (needed in case a bad name matched to a good name that was already present)
rbind(good_stc_names, bad_stc_names) %>%
group_by(AccSpeciesName, TraitName, LEVEL_NAME) %>%
summarize(n = sum(n)) -> stc
#We need to know the number of fraction of trait x species combinations overall
trait_list_stc <- read.csv("manual_downloads/trait_annotation/trait_list.csv")
trait_list_stc %>%
mutate(TraitName = gsub(pattern = "…", replacement = "…",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "â€\u009d", replacement = "”",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "“", replacement = "“",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "“", replacement = "“",x=TraitName))%>%
mutate(TraitName = gsub(pattern = "Â",
replacement = "",
x=TraitName,fixed = TRUE))-> trait_list_stc
stc %>%
group_by(AccSpeciesName, TraitName) %>%
count() %>%
ungroup() %>%
group_by(TraitName) %>%
count() %>%
collect() %>%
mutate(pct_coverage_clean = round(x = (n/n_species) *100,digits = 2))%>%
rename(n_clean = n) -> stc_trait_list_v2
merge(x = trait_list_stc,
y = stc_trait_list_v2,
by = "TraitName", all = TRUE) ->
stc_trait_list
rm(stc_trait_list_v2)
stc_trait_list$pct_coverage_clean[which(is.na(stc_trait_list$pct_coverage_clean))] <- 0
stc_trait_list$n_clean[which(is.na(stc_trait_list$n_clean))] <- 0
# What is the mean coverage across all georeferenced traits?
mean(stc_trait_list$pct_coverage_clean) #0.067%
max(stc_trait_list$pct_coverage_clean) #5.54%
# How many traits have at least one georef value?
stc_trait_list$TraitName[which(stc_trait_list$n_clean>0)]%>%
unique()%>%
length() #1553
(stc_trait_list$TraitName[which(stc_trait_list$n_clean>0)]%>%
unique()%>%
length())/nrow(stc_trait_list) #76.61% of traits have georeferenced values
# How many traits with observation of at least 1%, and are general?
length(which(stc_trait_list$pct_coverage_clean >= 1 &
stc_trait_list$general == 1)) #28
traits_for_geo_analysis <-
stc_trait_list %>%
filter(pct_coverage_clean >= 1 & general == 1)
# coverage stats for the focal geo dataset
traits_for_geo_analysis[which.max(traits_for_geo_analysis$pct_coverage_clean),]#5.54
traits_for_geo_analysis[which.min(traits_for_geo_analysis$pct_coverage_clean),]#1.10
mean(traits_for_geo_analysis$pct_coverage_clean)#1.85
median(traits_for_geo_analysis$pct_coverage_clean)#1.64
trait_summary_for_geo_analysis <-
stc %>%
filter(TraitName %in% traits_for_geo_analysis$TraitName)
nrow(trait_summary_for_geo_analysis)
#run through a country-specific version of get_trait_coverage()
# source("R/get_georeference_trait_coverage.R")
# georeferenced_trait_coverage <-
# get_georeferenced_trait_coverage(wcvp = wcvp,
# trait_summary_for_geo_analysis = trait_summary_for_geo_analysis,
# tdwg = tdwg,
# temp_file = "temp/temp_georeferenced_trait_coverage.RDS")
#
# saveRDS(object = georeferenced_trait_coverage,
# file = "data/focal_georeferenced_trait_coverage.rds")
georeferenced_trait_coverage <- read_rds("data/focal_georeferenced_trait_coverage.rds")
# TDWG polygons from https://github.com/tdwg/wgsrpd/tree/master/level3 on 3/25/2022
georeferenced_coverage_wide <-
georeferenced_trait_coverage %>%
pivot_wider(id_cols = area,
names_from = trait,
values_from = completeness)
countries <- sf::read_sf("manual_downloads/TDWG/level3.shp")
plot(countries[1])
geo_countries <-merge(x = countries,
y = georeferenced_coverage_wide,
by.x = "LEVEL3_COD",
by.y = "area")
ggplot(data = geo_countries)+
geom_sf(aes(fill = 100 * `Leaf dry mass per leaf fresh mass (leaf dry matter content, LDMC)`))+
scale_fill_viridis_c(option = "plasma")+
labs(fill = "%")+
ggtitle("Leaf dry mass per leaf fresh mass (leaf dry matter content, LDMC)")
#get overall trait coverage
# wcvp x n traits = expected
stc %>%
group_by(TraitName)%>%
summarise(n_obs = n())%>%
mutate(frac_cov = n_obs/(nrow(wcvp)) )-> stc_overall_coverage
mean(stc_overall_coverage$frac_cov)*100
max(stc_overall_coverage$frac_cov)
stc_overall_coverage %>% slice_max(order_by = frac_cov)
#How many trais even had georeferenced data?
length(unique(stc_overall_coverage$TraitName))
# Geo focal trait coverage
georeferenced_trait_coverage %>%
slice_max(order_by = completeness) #66.6 % coverage (antarctica)
mean(georeferenced_trait_coverage$completeness)*100
############################################
#Wood traits: would need to figure out what the woody species are by
# any species listed as having a trait measurement for wood
# any species with growth form implying woodiness
# species noted as woody
traits <- arrow::open_dataset(sources = "manual_downloads/TRY/TRY_parquet/")
traits %>%
group_by(TraitName) %>%
summarize(count = n()) %>%
collect() -> trait_counts
trait_counts%>%
filter(grepl(pattern = "wood", ignore.case = TRUE,x = TraitName)|
grepl(pattern = "tree", ignore.case = TRUE,x = TraitName))%>%
filter(TraitName != "Stem specific density (SSD) or wood density (stem dry mass per stem fresh volume)")%>%
filter(TraitName != "Plant woodiness") -> wood_traits
traits %>%
select(AccSpeciesName,TraitName) %>%
filter(TraitName %in% wood_traits$TraitName) %>%
select(AccSpeciesName) %>%
collect() %>%
unique() -> trs #these are species whose traits imply woodiness
traits %>%
filter(TraitName == "Plant growth form") %>%
select(AccSpeciesName, TraitName, OrigValueStr) %>%
collect() %>%
filter(grepl(pattern = "tree",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "shrub",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "liana",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "woody",x = OrigValueStr,ignore.case = TRUE)) %>%
select(AccSpeciesName) %>%
unique() -> gfs #species explicitlt stated as having a woody growth form
traits %>%
filter(TraitName == "Plant woodiness") %>%
select(AccSpeciesName, TraitName, OrigValueStr) %>%
collect()%>%
filter(grepl(pattern = "woody",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "woody/nonwoody",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "w",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "W",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "wood at base",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "woody at base",x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "woody rootstock" ,x = OrigValueStr,ignore.case = TRUE)|
grepl(pattern = "True" ,x = OrigValueStr,ignore.case = TRUE)) %>%
select(AccSpeciesName)-> wps #species stated as woody
putative_wood <- bind_rows(gfs,trs,wps) %>% unique()
rm(gfs,trs,wps)
# putative_wood <- TNRS::TNRS(taxonomic_names = putative_wood$AccSpeciesName,
# sources = "wcvp") %>%
# filter(Accepted_species != "")
#
# saveRDS(object = putative_wood,file = "data/tnrsed_wood.RDS")
putative_wood <- readRDS("data/tnrsed_wood.RDS")
putative_wood$Accepted_species <- gsub(pattern = " x ",
replacement = " × ",
x = putative_wood$Accepted_species)
# Load WCVP
wcvp <- read.table(file = "manual_downloads/WCVP/wcvp_distribution.txt",
sep = "|",
header = TRUE,
quote = "",
fill = TRUE,
encoding = "UTF-8")
wcvp_names <- read.table(file = "manual_downloads/WCVP/wcvp_names.txt",
sep = "|",
header = TRUE,
quote = "",
fill = TRUE,
encoding = "UTF-8")
merge(x = wcvp,
y = wcvp_names,
all.x = TRUE) %>%
filter(taxon_rank == "Species") %>% #include only species
filter(taxon_status == "Accepted") %>% #only accepted names
filter(!is.na(accepted_plant_name_id)) %>% #only accepted names
filter(extinct == 0) %>% #only extant species
filter(introduced == 0) -> wcvp #exclude introduced
# Remove or correct mistaken Area codes
shape <- rgdal::readOGR("manual_downloads/Darwinian_shortfalls/level3.shp")
wcvp$area_code_l3 <- toupper(wcvp$area_code_l3)
wcvp <- wcvp[wcvp$area_code_l3 %in% shape$LEVEL_3_CO,]
rm(wcvp_names, shape)
gc()
# Subset WCVP to woody species
wcvp_wood <-
wcvp %>%
filter(taxon_name %in% putative_wood$Accepted_species)
# saveRDS(object = wcvp_wood,
# file = "manual_downloads/WCVP/wcvp_cleaned_woody.RDS")
wcvp_wood <- readRDS(file = "manual_downloads/WCVP/wcvp_cleaned_woody.RDS")
rm(wcvp)
#need trait summary for wood traits
traits %>%
filter(grepl(pattern = "wood", ignore.case = TRUE,x = TraitName)) %>% #filter to only traits pertaining to wood
filter(TraitName != "Stem specific density (SSD) or wood density (stem dry mass per stem fresh volume)") %>% #toss this trait which could also be applied to non-wood
filter(TraitName != "Plant woodiness") %>% #toss this trait which could also be applied to non-wood
select(AccSpeciesName,TraitName) %>%
group_by(AccSpeciesName, TraitName)%>%
summarize(n = n())%>%
collect() -> wood_trait_summary
#check/fix wood species names
good_wood_summary <- wood_trait_summary[which(wood_trait_summary$AccSpeciesName %in% wcvp_wood$taxon_name),]
bad_wood_summary <- wood_trait_summary[which(!wood_trait_summary$AccSpeciesName %in% wcvp_wood$taxon_name),]
bad_wood_summary%>%
dplyr::select(AccSpeciesName)%>%
ungroup()%>%
unique()%>%
mutate(ID=row_number())%>%
select(ID,AccSpeciesName)-> bad_wood_names
# TNRSed_bad_wood_names <- TNRS::TNRS(taxonomic_names = bad_wood_names,
# sources = "wcvp")
# saveRDS(object = TNRSed_bad_wood_names,file = "data/tnrsed_bad_wood.RDS")
TNRSed_bad_wood_names <- readRDS("data/tnrsed_bad_wood.RDS")
bad_wood_names <-
bad_wood_names %>%
inner_join(y = TNRSed_bad_wood_names %>%
mutate(ID=as.numeric(ID)),
by = "ID")
bad_wood_summary <-
bad_wood_names %>%
select(AccSpeciesName, Accepted_species) %>%
right_join(bad_wood_summary)%>%
dplyr::select(-AccSpeciesName)%>%
rename("AccSpeciesName" = Accepted_species)
wood_trait_summary <-
good_wood_summary%>%
bind_rows(bad_wood_summary)%>%
group_by(AccSpeciesName,TraitName)%>%
summarise(n=sum(n))%>%
filter(AccSpeciesName != "")
rm(good_wood_summary,bad_wood_names)
source("R/get_trait_coverage.R")
# wood_trait_coverage <-
# get_trait_coverage(wcvp = wcvp_wood,
# trait_summary = wood_trait_summary)
#