<p>You could use the <code>tryCatch</code> <a href="http://127.0.0.1:30714/library/base/html/conditions.html" rel="nofollow noreferrer">function</a> to handle inputs that throw an error. A good example on how to write them can be found <a href="https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r">here</a>. Though the message indicates it's a warning, so does the function stop running when you get this message?</p>

CC BY-SA 4.0
2023-11-02T09:47:51.477Z
2023-11-02T09:47:51.477Z
dthorbur
<p>I have developed an R function to download the &quot;organism&quot; name of the numerous NCBI accession numbers which are already saved in an object.
The function:</p>
<pre><code>get_organism_names &lt;- function(accession_numbers) {
  organism_names &lt;- sapply(accession_numbers, function(accession) {
    record &lt;- entrez_summary(db = &quot;nucleotide&quot;, id = accession)
    return(record$organism)
  }, USE.NAMES = FALSE)
  names(organism_names) &lt;- accession_numbers
  return(organism_names)
}
</code></pre>
<p>Although it works for some accession numbers, for others it encounters an error stating for example:</p>
<pre><code>Warning: Esummary includes error message: Invalid uid KU253616.1 at position= 0Error: No esummary records found in file
</code></pre>
<p>How can I either solve this error or ask R to skip the problematic accession and keep downloading the rest?</p>

CC BY-SA 4.0
2023-10-25T12:53:30.697Z
Have you tried running it in a loop with a single accession each time instead of giving it all of them?
terdon
CC BY-SA 4.0
2023-10-25T12:56:07.410Z
Also, please give us all of the code. Presumably, you are loading some libraries, which ones? We need example code that we can use to reproduce the problem. See https://stackoverflow.com/help/minimal-reproducible-example. We need example input, and then code that we can run locally that reproduces the error.
terdon
<r><ncbi>
error in downloading the annotation of sequences on NCBI
# error in downloading the annotation of sequences on NCBI

(Asked by: username_0 on 2023-10-25)

I have developed an R function to download the "organism" name of the numerous NCBI accession numbers which are already saved in an object.
The function:
get_organism_names <- function(accession_numbers) {
  organism_names <- sapply(accession_numbers, function(accession) {
    record <- entrez_summary(db = "nucleotide", id = accession)
    return(record$organism)
  }, USE.NAMES = FALSE)
  names(organism_names) <- accession_numbers
  return(organism_names)
}

Although it works for some accession numbers, for others it encounters an error stating for example:
Warning: Esummary includes error message: Invalid uid KU253616.1 at position= 0Error: No esummary records found in file

How can I either solve this error or ask R to skip the problematic accession and keep downloading the rest?

**Tags:** r, ncbi
> **Comments:**
> **username_1**: Have you tried running it in a loop with a single accession each time instead of giving it all of them?  
> **username_1**: Also, please give us all of the code. Presumably, you are loading some libraries, which ones? We need example code that we can use to reproduce the problem. See https://stackoverflow.com/help/minimal-reproducible-example. We need example input, and then code that we can run locally that reproduces the error.  

---

## 1 answers

### Answer by username_2 on 2023-11-02. Score: 1

You could use the `tryCatch` [function](http://127.0.0.1:30714/library/base/html/conditions.html) to handle inputs that throw an error. A good example on how to write them can be found [here](https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r). Though the message indicates it's a warning, so does the function stop running when you get this message?


---

