R/srsIn.R

Defines functions srs_insitu

Documented in srs_insitu

#' @title Sample representativeness score estimation (In-situ conservation indicators).
#' @name srs_insitu
#' @description Performs an estimation of sample representativeness score for in-situ gap analysis (SRSin) using Khoury et al., (2019) methodology
#' This function uses counts from herbarium an germplasm accessions and calculate the SRS in-situ score as:
#'  \deqn{SRSin = Number of germplasm accessions in protected areas / Number of herbarium accessions in protected areas}
#'
#' @param species A name species compiled using '_'  to call occurrences files from Workspace/parameter/occurrences folder
#' @param Workspace A forder where the pipeline will be executed
#' @param  run_version The version of the analysis used (e.g 'v1')
#'
#' @return It returns a data frame file saved at gap_analysis folder with eight fields:
#'
#' \tabular{lcc}{
#'  ID \tab Species name \cr
#'  NTOTAL \tab Number of records available for a given species in protected areas \cr
#'  NTOTAL_COORDS \tab Number of records with geographical coordinates available for a given species in protected areas  \cr
#'  NG \tab Number of germplasm accessions available for a given species in protected areas  \cr
#'  NG_COORDS \tab Number of germplasm accessions with geographical coordinates Savailable for a given species in protected areas  \cr
#'  NH \tab Number of herbarium accessions available for a given specie in protected areas s \cr
#'  NH_COORDS \tab Number of herbarium accessions with geographical coordinates Savailable for a given species in protected areas  \cr
#'  SRS \tab SRSin score calculated from a CSV file summarizing the number of records for a given species in protected areas  \cr
#' }
#'
#' @examples srs_insitu('Cucurbita_digitata',Workspace,'v1')
#'
#' Workspace  <-  'E:/CIAT/workspace/Workspace_test/workspace'
#' run_version  <- 'v1'
#' species_list <- c('Cucurbita_cordata',
#'  'Cucurbita_digitata',
#'  'Cucurbita_foetidissima',
#'  'Cucurbita_palmata')
#'
#'  run_version <-'v1'
#
#' lapply(1:length(species_list),function(i){
#'    species <- species_list[[i]]
#'    x <- srs_insitu(species,Workspace,run_version)
#'    print(paste0(species,' DONE!'))
#' })
#'
#'@references
#'
#'Ramírez-Villegas, J., Khoury, C., Jarvis, A., Debouck, D. G., & Guarino, L. (2010).
#'A Gap Analysis Methodology for Collecting Crop Genepools: A Case Study with Phaseolus Beans.
#'PLOS ONE, 5(10), e13497. Retrieved from https://doi.org/10.1371/journal.pone.0013497
#'
#' Khoury, C. K., Amariles, D., Soto, J. S., Diaz, M. V., Sotelo, S., Sosa, C. C., … Jarvis, A. (2019).
#' Comprehensiveness of conservation of useful wild plants: An operational indicator for biodiversity
#' and sustainable development targets. Ecological Indicators. https://doi.org/10.1016/j.ecolind.2018.11.016
#'
#' @export

###
# Calculate the proportion of points that fall within a protected areas. Insitu SRS
# 20191002
# carver.dan1@gmail.com
###
srs_insitu <- function(species_list,occurrenceData, raster_list){

  #importFrom("methods", "as")
  #importFrom("stats", "complete.cases", "filter", "median")
  #importFrom("utils", "data", "memory.limit", "read.csv", "write.csv")

  # Load in protect areas
  proArea <- raster(system.file("data/protectedArea/wdpa_reclass.tif",
                                    package = "gapAnalysisR"))
  # create an empty dataframe
  df <- data.frame(matrix(ncol = 2, nrow = length(species_list)))
  colnames(df) <- c("species", "SRSin")

  for(i in 1:length(species_list)){
    # pull the sdm to mask for
    for(j in 1:length(raster_list)){
      if(grepl(j, i, ignore.case = TRUE)){
        sdm <- raster_list[[j]]
      }
    }
    # restrict protect areas those that are present in the model threshold
    ##**double check about this step with jullian/chrys/colin**
    proArea1 <- raster::crop(x = proArea,y = sdm)
    sdm[sdm == 0]<-NA
    proAreaSpecies <- sdm * proArea1

    # filter by specific species
    occData1 <- occurrenceData %>%
        dplyr::filter(taxon == species_list[i])
    totalNum <- nrow(occData1)

    # extract values to all points
    sp::coordinates(occData1) <- ~longitude+latitude
    sp::proj4string(occData1) <- CRS("+proj=longlat +datum=WGS84")
    protectPoints <- sum(!is.na(raster::extract(x = proArea1,y = occData1)))

    #define SRS
    if(protectPoints >= 0 ){
      srsInsitu <- 100 *(protectPoints/totalNum)
    }else{
      srsInsitu <- 0
    }
  # add values to empty df
    df$species[i] <- as.character(species_list[i])
    df$SRSin[i] <- srsInsitu
  }
return(df)
}
dcarver1/gapAnalysisR documentation built on Feb. 29, 2020, 12:13 p.m.