diff --git a/R/accnum.R b/R/accnum.R new file mode 100644 index 00000000..2be24c70 --- /dev/null +++ b/R/accnum.R @@ -0,0 +1,68 @@ + + + + +#' Convert a UniProt ID to an NCBI Entrez Gene ID +#' +#' This function takes a single UniProt ID and returns the corresponding NCBI Entrez Gene ID. +#' It uses the `org.Hs.eg.db` package to perform the mapping. +#' +#' @author Klangina +#' @param uniprot_id A string representing a single UniProt ID. +#' @return A string representing the corresponding NCBI Entrez Gene ID. Returns `NA` if no mapping is found. +#' @examples +#' \dontrun{ +#' uniprot_id <- "P04217" +#' entrez_id <- up2ncbi(uniprot_id) +#' print(entrez_id) +#' } +#' @importFrom AnnotationDbi select +#' @import org.Hs.eg.db +#' @export +up2ncbi <- function(uniprot_id) { + # Use the select function to map the UniProt ID to an Entrez Gene ID + result <- AnnotationDbi::select(org.Hs.eg.db, + keys = uniprot_id, + columns = "ENTREZID", + keytype = "UNIPROT") + + # Check if the result is not empty and return the first Entrez ID + if (nrow(result) > 0 && !is.na(result$ENTREZID[1])) { + return(as.character(result$ENTREZID[1])) + } else { + return(NA) # Return NA if no mapping is found + } +} + + + +#' Convert an NCBI Entrez Gene ID to a UniProt ID +#' +#' This function takes a single NCBI Entrez Gene ID and returns the corresponding UniProt ID. +#' It uses the `org.Hs.eg.db` package to perform the mapping. +#' +#' @param entrez_id A string representing a single NCBI Entrez Gene ID. +#' @return A string representing the corresponding UniProt ID. Returns `NA` if no mapping is found. +#' @examples +#' \dontrun{ +#' entrez_id <- "3586" +#' uniprot_id <- ncbi2up(entrez_id) +#' print(uniprot_id) +#' } +#' @importFrom AnnotationDbi select +#' @import org.Hs.eg.db +#' @export +ncbi2up <- function(entrez_id) { + # Use the select function to map the Entrez Gene ID to a UniProt ID + result <- AnnotationDbi::select(org.Hs.eg.db, + keys = entrez_id, + columns = "UNIPROT", + keytype = "ENTREZID") + + # Check if the result is not empty and return the first UniProt ID + if (nrow(result) > 0 && !is.na(result$UNIPROT[1])) { + return(as.character(result$UNIPROT[1])) + } else { + return(NA) # Return NA if no mapping is found + } +} \ No newline at end of file