Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to change taxonomy #151

Open
Rafnuss opened this issue Dec 28, 2024 · 0 comments
Open

Function to change taxonomy #151

Rafnuss opened this issue Dec 28, 2024 · 0 comments

Comments

@Rafnuss
Copy link

Rafnuss commented Dec 28, 2024

One function that I've been missing is to upgrade/downgrade taxonomy to a certain level.

I have observations identified as either Otolemur garnettii or Otolemur when I could not be sure of the species. For the analysis, I would like to lump all Otolemur garnettii to the higher taxonomic level Otolemur while maintaining x$taxonomy correct. As I understand the structure, I would have to modify observation$scientificName as well as all observation$taxon.xxxx columns. Then, using update_taxonomic() it would take care of removing Otolemur garnettii, right? Would that work?

Next, a bit more complex, I have observations identified as either Paraxerus ochraceus or Paraxerus palliatus. How to lump all of these observations to the higher taxonomic level Paraxerus sp while this does not currently exists in x$taxonomy.

Here is my current solution. Maybe there is a way to generalized an make it as a function to the package? Not sure if this would be helpful.

# Define taxonomy change
  taxon_change = list(
    list(
      from = "Otolemur garnettii", # Garnett's Greater Galago
      to = "Galago" # Galago sp
    ),
    list(
      from = "Paraxerus ochraceus", # Ochre Bush Squirrel
      to = "Paraxerus" # Paraxerus sp.
    ),
    list(
      from = "Paraxerus palliatus", # Red Bush Squirrel
      to = "Paraxerus" # Paraxerus sp.
    )
  ) %>% bind_rows()

  # Needs to be performed before changing observations
  taxonomy <- camtrapdp:::taxonomic(x)

  # Update the observations table. Note that I have not added the `taxon.xxxxx` at this stage to make the change of taxa more easily
  camtrapdp::observations(x) <- camtrapdp::observations(x) %>%
    left_join(taxon_change, by = c("scientificName" = "from")) %>%
    mutate(scientificName = coalesce(to, scientificName)) %>%
    select(-to)

  # Read all taxonomy (rely on an external taxonomy with all informations) 
  wi_taxonomy <- read_csv("data/wi_taxo_mammals.csv", show_col_types = FALSE, guess_max = 8000) %>%
    # match to taxonomy structure
    transmute(
      taxonID = .data$uniqueIdentifier,
      taxonIDReference = "https://github.com/ConservationInternational/Wildlife-Insights----Data-Migration/tree/master/WI_Global_Taxonomy",
      scientificName = dplyr::case_when(
        !is.na(.data$species) & !is.na(.data$genus)
        ~ paste(.data$genus, .data$species),
        !is.na(.data$genus) ~ .data$genus,
        !is.na(.data$family) ~ .data$family,
        !is.na(.data$order) ~ .data$order,
        TRUE ~ class
      ),
      taxonRank = dplyr::case_when(
        !is.na(.data$species) & !is.na(.data$genus) ~ "species",
        !is.na(.data$genus) ~ "genus",
        !is.na(.data$family) ~ "family",
        !is.na(.data$order) ~ "order",
        TRUE ~ "class"
      ),
      kingdom = "Animalia",
      # phylum = not present, likely Chordata
      class = class,
      order = order,
      family = family,
      genus = genus,
      vernacularNames.en = commonNameEnglish
    )

  # Update taxony with the change
  taxonomy <- taxonomy %>%
    filter(!(scientificName %in% taxon_change$from)) %>%
    bind_rows(
      wi_taxonomy %>% filter(scientificName %in% taxon_change$to)
    ) %>%
    unique()

  # Add taxonomic info to observations: Same as camtrapdp::read_camtrapdp()
  if (!is.null(taxonomy)) {
    # Add taxon. as column suffix
    colnames(taxonomy) <- paste("taxon", colnames(taxonomy), sep = ".")

    # Join taxonomy with observations
    camtrapdp::observations(x) <- camtrapdp::observations(x) %>%
      dplyr::left_join(
        taxonomy,
        by = dplyr::join_by("scientificName" == "taxon.scientificName")
      )
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant