<p>I developed the function and it worked perfectly,</p>
<pre class="lang-r prettyprint-override"><code>Converst_taxID_to_taxName &lt;- function(tax_id) {
  tax_info &lt;- entrez_summary(db=&quot;taxonomy&quot;, id=tax_id)
  return(tax_info$scientificname)
}

# usage example
myDataFrame &lt;- myDataFrame %&gt;%
mutate(Organism = sapply(`subject tax id`, Converst_taxID_to_taxName))

# in case one tax ID has a problem or cannot be proceeded, it puts &quot;NA&quot; and automatically goes the next ID
Converst_taxID_to_taxName2 &lt;- function(tax_id) {
  tryCatch({
    tax_info &lt;- entrez_summary(db=&quot;taxonomy&quot;, id=tax_id)
    return(tax_info$scientificname)
  }, error = function(e) {
    return(NA)
  })
}
</code></pre>

CC BY-SA 4.0
2024-06-04T19:45:07.590Z
Adding that the function entrez_summary is from the R library rentrez.
aminards
CC BY-SA 4.0
2023-11-16T16:42:10.660Z
2023-11-16T20:26:23.270Z
2023-11-16T20:26:23.270Z
Farzad Beikpour
<p>I have a data frame on R, called qIns8, with 14 columns and 204 rows. the column called &quot;subject tax id&quot; 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:</p>
<pre class="lang-r prettyprint-override"><code>getTAXname &lt;- function(tax_id) {
  name &lt;- tryCatch({
    tax_name &lt;- sci_name(tax_id)
    return(tax_name)
  }, error = function(e) {
    return(NA)
  })
  return(name)
}

# Apply the function to each row of the dataframe
qIns8<span class="math-container">$Organism &lt;- sapply(qIns8$</span>subject_tax_id, getTAXname)

# Check the updated dataframe
head(qIns8)
</code></pre>
<p>I also tried this from Taxonomizr package and it did not work either:</p>
<pre class="lang-r prettyprint-override"><code>getTaxonomy(qIns8$`subject tax id`, sqlFile = qIns8$`Organism`)
</code></pre>

CC BY-SA 4.0
2023-11-16T09:28:55.117Z
Please make sure to format code as code. See https://bioinformatics.stackexchange.com/help/formatting.
terdon
CC BY-SA 4.0
2023-11-16T09:30:30.687Z
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.
terdon
<r><taxonomy><ncbi>
getting NCBI TAXname by R
# getting NCBI TAXname by R

(Asked by: username_0 on 2023-11-16)

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`)

**Tags:** r, taxonomy, ncbi
> **Comments:**
> **username_1**: Please make sure to format code as code. See https://bioinformatics.stackexchange.com/help/formatting.  
> **username_1**: 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.  

---

## 1 answers

### Answer by username_0 on 2023-11-16. Score: 2

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)
  })
}

> **Comments:**
> **username_2**: Adding that the function entrez_summary is from the R library rentrez.  

---

