0
$\begingroup$

I have a data frame on R, called qIns8, with 14 columns and 204 rows. the column called "subject tax id" has NCBI taxIDs for the corresponding row, I need a script to add another column to the data frame called Organism for which the script has to get the organism name (NCBI taxname) from the column subject tax id. I already tried this script using Taxize package, but it did not work:

getTAXname <- function(tax_id) {
  name <- tryCatch({
    tax_name <- sci_name(tax_id)
    return(tax_name)
  }, error = function(e) {
    return(NA)
  })
  return(name)
}

# Apply the function to each row of the dataframe
qIns8$Organism <- sapply(qIns8$subject_tax_id, getTAXname)

# Check the updated dataframe
head(qIns8)

I also tried this from Taxonomizr package and it did not work either:

getTaxonomy(qIns8$`subject tax id`, sqlFile = qIns8$`Organism`)
$\endgroup$
2
  • $\begingroup$ Please make sure to format code as code. See bioinformatics.stackexchange.com/help/formatting. $\endgroup$
    – terdon
    Nov 16, 2023 at 9:28
  • $\begingroup$ Please edit your question and explain what "didn't work" means. How did it fail? Was there an error? What error? Was there no error but you didn't get the expected result? What did you get? What were you expecting? Also, try to give us an example we can use. Simplify your script to the minimum code that reproduces the issue, provide us with a simple data frame that we can also load and try our solutions out with. $\endgroup$
    – terdon
    Nov 16, 2023 at 9:30

1 Answer 1

1
$\begingroup$

I developed the function and it worked perfectly,

Converst_taxID_to_taxName <- function(tax_id) {
  tax_info <- entrez_summary(db="taxonomy", id=tax_id)
  return(tax_info$scientificname)
}

# usage example
myDataFrame <- myDataFrame %>%
mutate(Organism = sapply(`subject tax id`, Converst_taxID_to_taxName))

# in case one tax ID has a problem or cannot be proceeded, it puts "NA" and automatically goes the next ID
Converst_taxID_to_taxName2 <- function(tax_id) {
  tryCatch({
    tax_info <- entrez_summary(db="taxonomy", id=tax_id)
    return(tax_info$scientificname)
  }, error = function(e) {
    return(NA)
  })
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.