---
title: "2. Selecting and filtering species"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{2. Selecting and filtering species}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE,
  warning = FALSE
)
```

### Selecting a list of species
Selecting a list of target species is a common task in macroecological and conservation studies. For example, a researcher may seek to model the distribution of exclusively endemic trees within a designated Biome, a particular State, or a specific vegetation type.

By applying different filters to the database, anyone can obtain a verified taxonomic list of Brazilian species of algae, fungi, and plants for any Brazilian state, region, biome, and vegetation type. Additionally, the filter can be applied by family, genus, life form, habitat, endemism level, origin, nomenclatural status, and taxonomic status.
In this vignette, users will learn how to use *florabr* package to select a list of species based on these features.

## Loading data
Before you begin, use the `load_florabr` function to load the data. For more detailed information on obtaining and loading the data, please refer to [Getting started with florabr] - how to reference vignette???

```{r}
library(florabr)
#Folder where you stored the data with the function get_florabr()
#Load data
bf <- load_florabr(data_dir = my_dir,
                   data_version = "Latest_available",
                   type = "short") #short version
#> Loading version 393.401
```

## Selecting a list of species
One of the primary objectives of this package is to assist in selecting a species list based on taxonomic classification (Kingdom, Group, Family, and Genus), characteristics (life form, habitat), and distribution (federal states, biomes, vegetation types, and endemism). Specifically, you can filter by:

* kingdom, group, subgroup, family and genus;
* lifeForm and habitat;
* biome, state and vegetation with confirmed occurrences;
* endemism (endemic or non-endemic to Brazil);
* origin (Native, Cultivated or Naturalized in Brazil);
* taxonomicStatus and nomenclaturalStatus of the species.

To explore all available options for each filter, use the `get_attributes()` function with the desired attribute. This function will provide a data.frame with the available options to use in the `select_species()` function.

```{r}
#Get available options to filter by lifeForm
get_attributes(data = bf,
               attribute = "lifeForm",
               kingdom = "Plantae")
#>               lifeForm
#> 1      Aquatic-Benthos
#> 2      Aquatic-Neuston
#> 3     Aquatic-Plankton
#> 4                Shrub
#> 5                 Tree
#> 6               Bamboo
#> 7              Cushion
#> 8             Dendroid
#> 9              Unknown
#> 10          Dracaenoid
#> 13                Herb
#> 14          Flabellate
#> 15             Foliose
#> 16 Liana/scandent/vine
#> 19           Palm_tree
#> 21             Pendent
#> 23            Subshrub
#> 24           Succulent
#> 25            Thallose
#> 26                 Mat
#> 27                Weft
#> 28                Tuft

#Get available options to filter by  Biome
get_attributes(data = bf,
               attribute = "Biome",
               kingdom = "Plantae")
#>                 biome
#> 1              Amazon
#> 2     Atlantic_Forest
#> 3            Caatinga
#> 4             Cerrado
#> 5               Pampa
#> 6            Pantanal
#> 7 Not_found_in_brazil

#Get available options to filter by vegetation
get_attributes(data = bf,
               attribute = "vegetationType",
               kingdom = "Plantae")
#>                         vegetation
#> 1                   Anthropic_Area
#> 2                         Caatinga
#> 3            Amazonian_Campinarana
#> 4          High_Altitude_Grassland
#> 5                    Flooded_Field
#> 6                        Grassland
#> 7             Highland_Rocky_Field
#> 8                         Carrasco
#> 9                          Cerrado
#> 10                  Gallery_Forest
#> 11          Inundated_Forest_Igapo
#> 12              Terra_Firme_Forest
#> 13                Inundated_Forest
#> 14    Seasonallly_Deciduous_Forest
#> 15       Seasonal_Evergreen_Forest
#> 16 Seasonally_Semideciduous_Forest
#> 17                      Rainforest
#> 18      Mixed_Ombrophyllous_Forest
#> 19                        Mangrove
#> 20                      Palm_Grove
#> 21                        Restinga
#> 22               Amazonian_Savanna
#> 23              Aquatic_vegetation
#> 24         Rock_outcrop_vegetation
#> 25             Not_found_in_Brazil
```

As an illustration, let's consider the scenario where we aim to retrieve a list of all native and endemic trees with confirmed occurrences in the Atlantic Forest:

```{r}
af_spp <- select_species(data = bf,
                      include_subspecies = FALSE, include_variety = FALSE,
                      kingdom = "Plantae", group = "All", subgroup = "All",
                      family = "All", genus = "All",
                      lifeForm = "Tree", #Specify tree species
                      filter_lifeForm = "in",
                      habitat = "All", filter_habitat = "in",
                      biome = "Atlantic_Forest", #Occuring in the At. Forest
                      filter_biome = "in", #In Atlantic Forest
                      state = "All", filter_state = "in",
                      vegetation = "All", filter_vegetation = "in",
                      endemism = "Endemic", #Only endemics to Brazil
                      origin = "Native", #Only natives
                      taxonomicStatus = "Accepted",
                      nomenclaturalStatus = "All")
nrow(af_spp)
#> [1] 2372
```

The filter returned 2372 species that meet the specified criteria. It's important to note that these selections include species with confirmed occurrences in the Atlantic Forest, and some of them may also have confirmed occurrences in other biomes.

```{r}
#First 7 unique values of biomes in the filtered dataset
unique(af_spp$Biome)[1:7]
#> [1] "Atlantic_Forest"                        
#> [2] "Atlantic_Forest;Cerrado"                
#> [3] "Atlantic_Forest;Caatinga"               
#> [4] "Amazon;Atlantic_Forest;Caatinga;Cerrado"
#> [5] "Amazon;Atlantic_Forest;Cerrado"         
#> [6] "Atlantic_Forest;Caatinga;Cerrado"       
#> [7] "Amazon;Atlantic_Forest"
```

If you wish to exclusively select species with confirmed occurrences in the Atlantic Forest, modify the *filter_biome* parameter to "only":

```{r}
only_af_spp <- select_species(data = bf,
                      include_subspecies = FALSE, include_variety = FALSE,
                      kingdom = "Plantae", group = "All", subgroup = "All",
                      family = "All", genus = "All",
                      lifeForm = "Tree", #Specify tree species
                      filter_lifeForm = "in",
                      habitat = "All", filter_habitat = "in",
                      biome = "Atlantic_Forest", #Occuring in the At. Forest
                      filter_biome = "only", #ONLY in Atlantic Forest
                      state = "All", filter_state = "in",
                      vegetation = "All", filter_vegetation = "in",
                      endemism = "Endemic", #Only endemics to Brazil
                      origin = "Native", #Only natives
                      taxonomicStatus = "Accepted",
                      nomenclaturalStatus = "All")
nrow(only_af_spp)
#> [1] 1858
unique(only_af_spp$biome)
#> [1] "Atlantic_Forest"
```

Now, the filter has resulted in 1858 species, all exclusively confined to the Atlantic Forest biome.

Furthermore, the package offers the flexibility to apply various filtering options (please consult `?select_species` for comprehensive details). For instance, consider the scenario where we aim to compile a list of native and endemic trees with confirmed occurrences limited solely to the Atlantic Forest biome and with confirmed occurrences in the states of Paraná (PR), Santa Catarina (SC), and Rio Grande do Sul (RS):

```{r}
south_af_spp <- select_species(data = bf,
                      include_subspecies = FALSE, include_variety = FALSE,
                      kingdom = "Plantae", group = "All", subgroup = "All",
                      family = "All", genus = "All",
                      lifeForm = "Tree", #Specify tree species
                      filter_lifeForm = "in",
                      habitat = "All", filter_habitat = "in",
                      biome = "Atlantic_Forest", #Occuring in the At. Forest
                      filter_biome = "only", #Only in Atlantic Forest
                      state = c("PR", "SC", "RS"), #states - Use the acronynms
                      filter_state = "in", #IN at least one of these states
                      vegetation = "All", filter_vegetation = "in",
                      endemism = "Endemic", #Only endemics to Brazil
                      origin = "Native", #Only natives
                      taxonomicStatus = "Accepted",
                      nomenclaturalStatus = "All")
nrow(south_af_spp)
#> [1] 372

#First 10 unique values of states in the filtered dataset
unique(south_af_spp$states)[1:10]
#>  [1] "BA;ES;PR;RJ;SC;SP"               "AL;BA;CE;ES;MA;MG;PB;PE;PR;RJ;SE;SP"
#>  [3] "PR;RS;SC"                        "BA;CE;ES;MA;MG;PE;PR;RJ;SP"         
#>  [5] "MG;PR;RJ;SC;SP"                  "MG;PR;RJ;RS;SC;SP"                  
#>  [7] "BA;ES;MG;PR;RJ;SC;SP"            "MG;PR;RS;SC;SP"                     
#>  [9] "PR;RJ;RS;SC;SP"                  "ES;MG;PR;RJ;RS;SC;SP"
```

By utilizing *filter_state = "in"*, our selection encompassed species occurring in all three states, as well as those appearing in only two or even one of them. To impose a more rigorous criterion, selecting solely those species with confirmed occurrences in all three states, we can use *filter_state = "and"*:

```{r}
south_af_spp2 <- select_species(data = bf,
                      include_subspecies = FALSE, include_variety = FALSE,
                      kingdom = "Plantae", group = "All", subgroup = "All",
                      family = "All", genus = "All",
                      lifeForm = "Tree", #Specify tree species
                      filter_lifeForm = "in",
                      habitat = "All", filter_habitat = "in",
                      biome = "Atlantic_Forest", #Occurring in the At. Forest
                      filter_biome = "only", #Only in Atlantic Forest
                      state = c("PR", "SC", "RS"), #states - Use the acronynms
                      filter_state = "and", #PR and SC and RS
                      vegetation = "All", filter_vegetation = "in",
                      endemism = "Endemic", #Only endemics to Brazil
                      origin = "Native", #Only natives
                      taxonomicStatus = "Accepted",
                      nomenclaturalStatus = "All")
nrow(south_af_spp2)
#> [1] 29

#All unique states in the filtered dataset
unique(south_af_spp2$states)
#> [1] "PR;RS;SC"       "MG;PR;RS;SC;SP" "PR;RS;SC;SP"    "ES;PR;RS;SC;SP"
#> [5] "MG;PR;RS;SC"
```

Now, our selection consists solely of species with confirmed occurrences in all of the specified states. However, by utilizing the "and" argument, we permit the filter to include species with occurrences in additional states. To confine the filter exclusively to species with confirmed occurrences in all three states, without any occurrences elsewhere, we can use *filter_state = "only"*:

```{r}
south_af_spp3 <- select_species(data = bf,
                      include_subspecies = FALSE, include_variety = FALSE,
                      kingdom = "Plantae", group = "All", subgroup = "All",
                      family = "All", genus = "All",
                      lifeForm = "Tree", #Specify tree species
                      filter_lifeForm = "in",
                      habitat = "All", filter_habitat = "in",
                      biome = "Atlantic_Forest", #Occuring in the At. Forest
                      filter_biome = "only", #Only in Atlantic Forest
                      state = c("PR", "SC", "RS"), #states - Use the acronynms
                      filter_state = "only", #PR and SC and RS, no other else
                      vegetation = "All", filter_vegetation = "in",
                      endemism = "Endemic", #Only endemics to Brazil
                      origin = "Native", #Only natives
                      taxonomicStatus = "Accepted",
                      nomenclaturalStatus = "All")
nrow(south_af_spp3)
#> [1] 13

#The unique state in the filtered dataset
unique(south_af_spp3$states)
#> [1] "PR;RS;SC"
```
Now, the filter return only 13 species, all of them occurring in PR, SC and RS; with no recorded occurrences in any other states.

## Checking names and subsetting species
### Extracting binomial name
In addition to selecting a species list based on their characteristics, the package also includes a function for subsetting species by name. Please note that this function exclusively operates with binomial names (Genus + specificEpithet), such as *Araucaria angustifolia*, and does not support complete scientific names (including Author or infraspecificEpithet), such as *Araucaria angustifolia (Bertol.) Kuntze* or *Araucaria angustifolia var. stricta Reitz*.

If you have species with complete scientific names, you can extract the binomial names using the function `get_binomial()`. Don't worry about excessive spaces between words; the function will remove any extra spaces from the names.

```{r}
complete_names <- c("Araucaria brasiliana var. ridolfiana (Pi.Savi) Gordon",
                    " Solanum restingae  S.Knapp",
                    "Butia cattarinensis  Noblick & Lorenzi   ",
                    "Homo   sapiens")
#Human specie was used just as an example that will be used to illustrate the 
#next function
binomial_names <- get_binomial(species_names = complete_names)
binomial_names
#> [1] "Araucaria brasiliana" "Solanum restingae"    "Butia cattarinensis" 
#> [4] "Homo sapiens"
```
### Checking the species names
Additionally, you can verify the spelling, nomenclatural status, and taxonomic status of species names using the check_names() function. If the function is unable to locate the name of a species in the database (due to a typo, for example), it can suggest potential names based on similarities to other entries in the database.
To see how the function works, let's utilize the previously created binomial_names dataset:

```{r}
#Create example
checked_names <- check_names(data = bf, 
                             species = binomial_names,
                             max_distance = 0.1,
                             kingdom = "Plantae")
checked_names
#             input_name           Spelling       Suggested_name Distance taxonomicStatus nomenclaturalStatus           acceptedName        family
# 1 Araucaria brasiliana            Correct Araucaria brasiliana        0         Synonym                <NA> Araucaria angustifolia Araucariaceae
# 2 Araucaria brasiliana            Correct Araucaria brasiliana        0         Synonym        Illegitimate Araucaria angustifolia Araucariaceae
# 3    Solanum restingae            Correct    Solanum restingae        0        Accepted             Correct      Solanum restingae    Solanaceae
# 4  Butia cattarinensis Probably_incorrect   Butia catarinensis        1        Accepted                <NA>     Butia catarinensis     Arecaceae
# 5         Homo sapiens          Not_found                 <NA>       NA            <NA>                <NA>                   <NA>          <NA>
```

We can see that *Araucaria brasiliana* is spelling correctly, but it is a synonym of *Araucaria angustifolia*.
*Solanum restingae* is spelling correctly and it is an accepted name.
In the case of *Butia cattarinensis*, the spelling appears to be potentially incorrect (as the name wasn't found in the database); however, a similar name, *Butia catarinensis*, is suggested by the function. The 'Distance' column indicates the Levenshtein edit distance between the input and the suggested name.
The spelling of *Homo sapiens* was flagged as Not_found (as expected, given that *Homo sapiens* is not a plant!). Consequently, the name was not located in the database, and there were no comparable names available.

### Subsetting species

To retrieve species information from the Flora e Funga do Brasil database, employ the `subset_species()` function. For optimal performance, we highly recommend utilizing the `get_binomial()` and `check_names()` functions beforehand. This ensures that you're exclusively working with species present in the Flora e Funga do Brasil database.
To see how the function works, let's use the accepted names in *checked_names* created previously:

```{r}
#Get only accepted names
accepted_names <- unique(checked_names$acceptedName)
accepted_names <- na.omit(accepted_names) #Remove NA

#Subset species
my_sp <- subset_species(data = bf, species = accepted_names,
                      include_subspecies = FALSE,
                      include_variety = FALSE,
                      kingdom = "Plantae")
my_sp
#>                      species                          scientificName
#> 11785      Solanum restingae               Solanum restingae S.Knapp
#> 26790 Araucaria angustifolia Araucaria angustifolia (Bertol.) Kuntze
#> 99881     Butia catarinensis    Butia catarinensis Noblick & Lorenzi
#>       acceptedName kingdom       Group Subgroup        family     genus
#> 11785         <NA> Plantae Angiosperms     <NA>    Solanaceae   Solanum
#> 26790         <NA> Plantae Gymnosperms     <NA> Araucariaceae Araucaria
#> 99881         <NA> Plantae Angiosperms     <NA>     Arecaceae     Butia
#>        lifeForm     habitat                 Biome            States
#> 11785     Shrub Terrestrial       Atlantic_Forest                BA
#> 26790      Tree Terrestrial Atlantic_Forest;Pampa MG;PR;RJ;RS;SC;SP
#> 99881 Palm_tree Terrestrial Atlantic_Forest;Pampa             RS;SC
#>       vegetationType
#> 11785 Restinga
#> 26790 High_Altitude_Grassland;Mixed_Ombrophyllous_Forest;
#>       Seasonally_Semideciduous_Forest
#> 99881 Restinga
#>       origin    endemism taxonomicStatus nomenclaturalStatus
#> 11785 Native     Endemic        Accepted             Correct
#> 26790 Native Non-endemic        Accepted             Correct
#> 99881 Native     Endemic        Accepted                <NA>
#>       vernacularName
#> 11785 <NA>
#> 26790 araucaria, pinheiro-do-parana, curi, pinheiro-brasileiro, 
#> pinho-do-parana
#> 99881 <NA>
#>       taxonRank
#> 11785   Species
#> 26790   Species
#> 99881   Species
```

We can also include subspecies and/or varieties:

```{r}
my_sp2 <- subset_species(data = bf, species = accepted_names,
                      include_subspecies = TRUE,
                      include_variety = TRUE,
                      kingdom = "Plantae")
my_sp2[1:5,]
#>                      species                            scientificName
#> 11785      Solanum restingae                 Solanum restingae S.Knapp
#> 26790 Araucaria angustifolia   Araucaria angustifolia (Bertol.) Kuntze
#> 35204 Araucaria angustifolia    Araucaria angustifolia var. alba Reitz
#> 35205 Araucaria angustifolia  Araucaria angustifolia var. caiova Reitz
#> 35206 Araucaria angustifolia Araucaria angustifolia var. caiuva Mattos
#>                 acceptedName kingdom       Group Subgroup        family
#> 11785                   <NA> Plantae Angiosperms     <NA>    Solanaceae
#> 26790                   <NA> Plantae Gymnosperms     <NA> Araucariaceae
#> 35204 Araucaria angustifolia Plantae Gymnosperms     <NA> Araucariaceae
#> 35205 Araucaria angustifolia Plantae Gymnosperms     <NA> Araucariaceae
#> 35206 Araucaria angustifolia Plantae Gymnosperms     <NA> Araucariaceae
#>           genus lifeForm     habitat                 Biome            States
#> 11785   Solanum    Shrub Terrestrial       Atlantic_Forest                BA
#> 26790 Araucaria     Tree Terrestrial Atlantic_Forest;Pampa MG;PR;RJ;RS;SC;SP
#> 35204 Araucaria                                                             
#> 35205 Araucaria                                                             
#> 35206 Araucaria                                                             
#>       vegetationType
#> 11785 Restinga
#> 26790 High_Altitude_Grassland;Mixed_Ombrophyllous_Forest;
#>       Seasonally_Semideciduous_Forest
#> 35204                                                                         
#> 35205                                                                          
#> 35206                                                                        
#>       origin    endemism taxonomicStatus nomenclaturalStatus
#> 11785 Native     Endemic        Accepted             Correct
#> 26790 Native Non-endemic        Accepted             Correct
#> 35204   <NA>        <NA>         Synonym                <NA>
#> 35205   <NA>        <NA>         Synonym                <NA>
#> 35206   <NA>        <NA>         Synonym                <NA>
#>       vernacularName
#> 11785 <NA>
#> 26790 araucaria, pinheiro-do-parana, curi, pinheiro-brasileiro,
#> pinho-do-parana
#> 35204 <NA>
#> 35205 <NA>
#> 35206 <NA>
#>       taxonRank
#> 11785   Species
#> 26790   Species
#> 35204   Variety
#> 35205   Variety
#> 35206   Variety
```

### Retrieving synonyms
We can retrieve all synonyms of a species list. This can be particularly useful, for example, when searching for records of a species and all its synonyms (as listed in the Flora e Funga do Brasil) in online databases like GBIF. To accomplish this, utilize the function get_synonym. To understand how the function works, let's search for the synonyms of two species:
```{r}
spp <- c("Araucaria angustifolia", "Adesmia paranensis")
spp_syn <- get_synonym(data = bf, species = spp)
spp_syn
#>                       synonym           acceptedName taxonomicStatus      nomenclaturalStatus
#> 35323    Araucaria brasiliana Araucaria angustifolia         Synonym                     <NA>
#> 35325  Araucaria brasiliensis Araucaria angustifolia         Synonym                     <NA>
#> 35327        Araucaria dioica Araucaria angustifolia         Synonym                     <NA>
#> 35328       Araucaria elegans Araucaria angustifolia         Synonym                     <NA>
#> 35329    Araucaria ridolfiana Araucaria angustifolia         Synonym                     <NA>
#> 35330       Araucaria saviana Araucaria angustifolia         Synonym                     <NA>
#> 35332   Columbea angustifolia Araucaria angustifolia         Synonym Legitimate_but_incorrect
#> 35333     Columbea brasiliana Araucaria angustifolia         Synonym Legitimate_but_incorrect
#> 60644            Pinus dioica Araucaria angustifolia         Synonym                     <NA>
#> 141020     Araucaria bibbiani Araucaria angustifolia         Synonym                     <NA>
#> 141021   Araucaria lindleyana Araucaria angustifolia         Synonym                     <NA>
#> 141041   Araucaria brasiliana Araucaria angustifolia         Synonym             Illegitimate
#> 85308    Adesmia psoraleoides     Adesmia paranensis         Synonym Legitimate_but_incorrect
```
We can see that *Araucaria angustifolia* has 12 synonyms in Flora e Funga do Brasil, while *Adesmia paranensis* has one synonym.

