R/whatFish.R

#' Names and codes for fish species
#'
#' @description Functions to convert the scientific name, English name, the FAO
#'   X3A codes and the WoRMS AphiaIDs of fish species and taxonomic groupings.
#'
#' @details The rendering of the Scientific name of the organism differs
#'   slightly between the FAO and WoRMS lists. By default the function searches
#'   and returns the WoRMS scientific name.
#'
#' @param x A character vector of the Scientific name, the English name or the
#'   X3A code for the fish
#' @param scientific The form of the Scientific name to search on and return,
#'   options are `"WoRMS"` or `"FAO"`, see Details
#'
#' @author Alastair Pout \email{a.pout@marlab.ac.uk}
#'
#' @return Returns a list of the the Scientific name, English name, the X3A code
#'   and the AphiaID, regardless of which of these it is passed.
#'
#' @export
#'
#' @importFrom utils data
#'
#' @md
#'
#' @examples
#' # the Scientific name
#' whatFish("Gadus morhua")
#'
#' # the FAO code
#' whatFish("POK")
#'
#' # the WoRMS code
#' whatFish("127146")
#'
#' # the English name
#' whatFish("Norway lobster")


whatFish <- function (x, scientific = "WoRMS") {
    #data(ASFIS_WoRMS)
    sppTab <- fishPiCodes::ASFIS_WoRMS
    if (scientific == "WoRMS")
        sppTab$SN <- sppTab$WoRMSname
    if (scientific == "FAO")
        sppTab$SN <- sppTab$Scientific_name
    if (!(scientific %in% c("FAO", "WoRMS")))
        stop("scientific needs to be one of FAO or WoRMS")
    if (any(sppTab$X3A_CODE %in% x)) {
        index <- which(sppTab$X3A_CODE %in% x)
        index2 <- match(x, sppTab$X3A_CODE[index])
        Sname <- as.character(sppTab$SN[index[index2]])
        Ename <- as.character(sppTab$English_name[index[index2]])
        Aphia <- as.character(sppTab$AphiaID[index[index2]])
        Cname <- x
    }
    if (any(sppTab$SN %in% x)) {
        index <- which(sppTab$SN %in% x)
        index2 <- match(x, sppTab$SN[index])
        Cname <- as.character(sppTab$X3A_CODE[index[index2]])
        Ename <- as.character(sppTab$English_name[index[index2]])
        Aphia <- as.character(sppTab$AphiaID[index[index2]])
        Sname <- x
    }
    if (any(sppTab$English_name %in% x)) {
        index <- which(sppTab$English_name %in% x)
        index2 <- match(x, sppTab$English_name[index])
        Cname <- as.character(sppTab$X3A_CODE[index[index2]])
        Sname <- as.character(sppTab$SN[index[index2]])
        Aphia <- as.character(sppTab$AphiaID[index[index2]])
        Ename <- x
    }
    if (any(sppTab$AphiaID %in% x)) {
        index <- which(sppTab$AphiaID %in% x)
        index2 <- match(x, sppTab$AphiaID[index])
        Cname <- as.character(sppTab$X3A_CODE[index[index2]])
        Sname <- as.character(sppTab$SN[index[index2]])
        Ename <- as.character(sppTab$English_name[index[index2]])
        Aphia <- x
    }
    out <- list(Scientific = Sname, English = Ename, Code = Cname,
        AphiaID = Aphia)
    return(out)
}
MarineScotlandScience/fishPiCodes documentation built on Dec. 17, 2021, 3:12 a.m.