-
Notifications
You must be signed in to change notification settings - Fork 2
/
beesTaxonomy.R
166 lines (151 loc) · 6.24 KB
/
beesTaxonomy.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
# Description of the BeeBDC taxonomic dataset
# 16th of March 2023
#' Download a nearly complete taxonomy of bees globally
#'
#'
#' Downloads the taxonomic information for the bees of the world.
#' Source of taxonomy is listed under "source" but are mostly derived from the Discover Life
#' website. The data will be sourced from the BeeBDC article's Figshare.
#'
#' Note that sometimes the download might not work without restarting R. In this case, you could
#' alternatively download the dataset from the URL below and then read it in using
#' `base::readRDS("filePath.Rda")`.
#'
#'
#'
#' **Column details**
#'
#' **flags** Flags or comments about the taxon name.
#'
#' **taxonomic_status** Taxonomic status. Values are "accepted" or "synonym"
#'
#' **source** Source of the name.
#'
#' **accid** The id of the accepted taxon name or "0" if taxonomic_status == accepted.
#'
#' **id** The id number for the taxon name.
#'
#' **kingdom** The biological kingdom the taxon belongs to. For bees, kingdom == Animalia.
#'
#' **phylum** The biological phylum the taxon belongs to. For bees, phylum == Arthropoda.
#'
#' **class** The biological class the taxon belongs to. For bees, class == Insecta.
#'
#' **order** The biological order the taxon belongs to. For bees, order == Hymenoptera.
#'
#'
#' **family** The family of bee which the species belongs to.
#'
#' **subfamily** The subfamily of bee which the species belongs to.
#'
#' **tribe** The tribe of bee which the species belongs to.
#'
#' **subtribe** The subtribe of bee which the species belongs to.
#'
#' **validName** The valid scientific name as it should occur in the 'scientificName" column in a Darwin Core file.
#'
#' **canonical** The scientificName without the scientificNameAuthority.
#'
#' **canonical_withFlags** The scientificName without the scientificNameAuthority and with Discover Life taxonomy flags.
#'
#' **genus** The genus the bee species belongs to.
#'
#' **subgenus** The subgenus the bee species belongs to.
#'
#' **species** The specific epithet for the bee species.
#'
#' **infraspecies** The infraspecific epithet for the bee addressed.
#'
#' **authorship** The author who described the bee species.
#'
#' **taxon_rank** Rank for the bee taxon addressed in the entry.
#'
#' **notes** Additional notes about the name/taxon.
#'
#'
#' @param URL A character vector to the FigShare location of the dataset. The default will be to
#' the most-recent version.
#' @param ... Extra variables that can be passed to [utils::download.file()]
#'
#'
#'
#' @return A downloaded beesTaxonomy.Rda file in the [tempdir()] and the same tibble returned to
#' the environment.
#'
#'
#' @references This dataset was created using the Discover Life taxonomy.
#' Dataset is from the publication:
#' Dorey, J.B., Fischer, E.E., Chesshire, P.R., Nava-Bolaños, A., O’Reilly, R.L., Bossert, S., Collins, S.M., Lichtenberg, E.M., Tucker, E., Smith-Pardo, A., Falcon-Brindis, A., Guevara, D.A., Ribeiro, B.R., de Pedro, D., Hung, J.K.-L., Parys, K.A., McCabe, L.M., Rogan, M.S., Minckley, R.L., Velzco, S.J.E., Griswold, T., Zarrillo, T.A., Jetz, W., Sica, Y.V., Orr, M.C., Guzman, L.M., Ascher, J., Hughes, A.C. & Cobb, N.S. (2023) A globally synthesised and flagged bee occurrence dataset and cleaning workflow. Scientific Data, 10, 1–17. https://www.doi.org/10.1038/S41597-023-02626-W
#' The taxonomy data are mostly compiled from Discover Life data, www.discoverlife.org:
#' Ascher, J.S. & Pickering, J. (2020) Discover Life bee species guide and world checklist (Hymenoptera: Apoidea: Anthophila). http://www.discoverlife.org/mp/20q?guide=Apoidea_species
#'
#' @seealso [BeeBDC::taxadbToBeeBDC()] to download any other taxonomy (of any taxa or of bees)
#' and [BeeBDC::harmoniseR()] for the
#' taxon-cleaning function where these taxonomies are implemented.
#'
#' @export
#'
#' @examples
#'\dontrun{
#' beesTaxonomy <- BeeBDC::beesTaxonomy()
#'}
#'
#'
beesTaxonomy <- function(URL = "https://open.flinders.edu.au/ndownloader/files/43331472",
...){
destfile <- taxonomy <- attempt <- nAttempts <- error_funcFile <- error_func <- NULL
# Set the number of attempts
nAttempts = 5
# Set up the error message function
error_func <- function(e){
message(paste("Download attempt failed..."))
}
error_funcFile <- function(e){
message(paste("Could not read download..."))
}
# Check operating system
OS <- dplyr::if_else(.Platform$OS.type == "unix",
"MacLinux",
"Windows")
# Run a code to download the data and deal with potential internet issues
taxonomy <- NULL
attempt <- 1
suppressWarnings(
while( is.null(taxonomy) && attempt <= nAttempts) {
# Don't attempt for the last attempt
if(attempt < nAttempts){
# WINDOWS
if(OS != "MacLinux"){
# Download the file to the outPath
tryCatch(utils::download.file(URL, destfile = normalizePath(paste0(tempdir(),
"/beesTaxonomy.Rda"))),
error = error_func, warning = error_func)
# Load the file from the outPath
tryCatch(
taxonomy <- base::readRDS(normalizePath(paste0(tempdir(), "/beesTaxonomy.Rda"))),
error = error_funcFile, warning = error_funcFile)
}else{
# Download the file to the outPath
tryCatch(utils::download.file(URL, destfile = paste0(tempdir(), "/beesTaxonomy.Rda")),
error = error_func, warning = error_func)
# Load the file from the outPath
tryCatch(
taxonomy <- base::readRDS(paste0(tempdir(), "/beesTaxonomy.Rda")),
error = error_funcFile, warning = error_funcFile)
}
} # END if
if(attempt < nAttempts){
# Wait one second before the next request
if(attempt > 1){Sys.sleep(1)
print( paste("Attempt: ", attempt, " of ", nAttempts-1))} # Inform user of number of attempts
} # END IF #2
# Count the next attempt
attempt <- attempt + 1
} # END while
)
if(is.null(taxonomy)){
message(" - Taxonomy download failed. Please check your internet connection.")
}
# Return the data to the user
return(taxonomy)
} # END function