<p>Your issue is that <code>entrez_fetch()</code> returns the fasta record(s) as a single string with the definition lines and sequences concatenated. Also, newline characters (<code>\n</code>) appear in place of line breaks, with double newlines (<code>\n\n</code>) separating different records. Here's a function that creates a <code>DNAStringSet</code> given a list of nucleotide identifiers without writing each fasta to a file:</p>
<pre><code>entrez2dss &lt;-
  function(id_list) {
    
    require(rentrez)    # v1.2.3
    require(Biostrings) # v2.66.0
    require(stringr)    # v1.5.0
    
    # fetch all sequences, trim empty elements
    raw_ &lt;-
      entrez_fetch(db = &quot;nucleotide&quot;,
                   rettype = &quot;fasta&quot;,
                   id = id_list) |&gt;
      str_split_1(pattern = &quot;\n\n&quot;) |&gt;
      head(-1)
    
    # get sequence names
    names_ &lt;- 
      gsub(pattern = &quot;\n.*&quot;,
           replacement = &quot;&quot;,
           x = raw_) |&gt;
      gsub(pattern = &quot;^&gt;&quot;,
           replacement  = &quot;&quot;)
    
    # get sequences
    seqs_ &lt;-
      sub(pattern = &quot;.*?\n&quot;,
          replacement = &quot;&quot;,
          x = raw_) |&gt;
      gsub(pattern = &quot;\n&quot;,
           replacement = &quot;&quot;)
    
    # create named DNAStringSet object
    dss &lt;- DNAStringSet(x = seqs_)
    names(dss) &lt;- names_
    dss
    
  }

seqlist &lt;- c(&quot;NC_045512&quot;, &quot;OR558592&quot;)
cov2 &lt;- entrez2dss(seqlist)
</code></pre>

CC BY-SA 4.0
2023-10-16T21:16:10.583Z
2023-10-17T13:54:39.363Z
2023-10-17T13:54:39.363Z
acvill
<p>The previous error message was because the <code>entrez_fetch()</code> function returns the sequence as a character string, not a file path. To save the sequence to a file, I can use the <code>writeLines()</code> function. For example, the following code will save the sequence to a file named <code>cov2.fasta</code>:</p>
<pre class="lang-r prettyprint-override"><code># Download the FASTA sequence
cov2 &lt;- entrez_fetch(db = &quot;nucleotide&quot;, rettype = &quot;fasta&quot;, id = c(&quot;NC_045512&quot;, &quot;OR558592&quot;))
# Save the sequence to a file
writeLines(cov2, &quot;cov2.fasta&quot;)
# Read the FASTA sequence into a DNAStringSet object
cov2_dna &lt;- readDNAStringSet(&quot;cov2.fasta&quot;)
</code></pre>

CC BY-SA 4.0
2023-10-17T08:49:25.983Z
2023-10-17T15:12:48.453Z
2023-10-17T15:12:48.453Z
Farzad Beikpour
<p>I wish to download a fasta sequence from NCBI using &quot;rentrez&quot; package and then save it in an object, then I want to have this object as a DNAStringSet object to work on the sequence using &quot;Biostrings&quot; capabilities.</p>
<p>I leave my incorrect code here so I would be grateful if you could help me out.</p>
<pre class="lang-r prettyprint-override"><code>cov2 &lt;- entrez_fetch(db=&quot;nucleotide&quot; , rettype = &quot;fasta&quot;, id = &quot;NC_045512&quot;)
cov2 &lt;- readDNAStringSet(cov2)
...
Error in .Call2(&quot;new_input_filexp&quot;, filepath, PACKAGE = &quot;XVector&quot;) : 
  cannot open file '&gt;NC_045512.2 
</code></pre>

CC BY-SA 4.0
2023-10-14T11:11:52.153Z
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Community
CC BY-SA 4.0
2023-10-15T15:15:13.310Z
Check what the value of `cov2` is after the first command. `entrez_fetch()` returns the fasta record, but `readDNAStringSet()` requires a file path as input.
Cloudberry
<r><fasta><bioconductor><biostrings>
Making a DNAStringSet object
# Making a DNAStringSet object

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

I wish to download a fasta sequence from NCBI using "rentrez" package and then save it in an object, then I want to have this object as a DNAStringSet object to work on the sequence using "Biostrings" capabilities.
I leave my incorrect code here so I would be grateful if you could help me out.
cov2 <- entrez_fetch(db="nucleotide" , rettype = "fasta", id = "NC_045512")
cov2 <- readDNAStringSet(cov2)
...
Error in .Call2("new_input_filexp", filepath, PACKAGE = "XVector") : 
  cannot open file '>NC_045512.2

**Tags:** r, fasta, bioconductor, biostrings
> **Comments:**
> **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  
> **username_2**: Check what the value of `cov2` is after the first command. `entrez_fetch()` returns the fasta record, but `readDNAStringSet()` requires a file path as input.  

---

## 2 answers

### Answer by username_3 on 2023-10-16. Score: 1

Your issue is that entrez_fetch() returns the fasta record(s) as a single string with the definition lines and sequences concatenated. Also, newline characters (\n) appear in place of line breaks, with double newlines (\n\n) separating different records. Here's a function that creates a DNAStringSet given a list of nucleotide identifiers without writing each fasta to a file:
entrez2dss <-
  function(id_list) {
    
    require(rentrez)    # v1.2.3
    require(Biostrings) # v2.66.0
    require(stringr)    # v1.5.0
    
    # fetch all sequences, trim empty elements
    raw_ <-
      entrez_fetch(db = "nucleotide",
                   rettype = "fasta",
                   id = id_list) |>
      str_split_1(pattern = "\n\n") |>
      head(-1)
    
    # get sequence names
    names_ <- 
      gsub(pattern = "\n.*",
           replacement = "",
           x = raw_) |>
      gsub(pattern = "^>",
           replacement  = "")
    
    # get sequences
    seqs_ <-
      sub(pattern = ".*?\n",
          replacement = "",
          x = raw_) |>
      gsub(pattern = "\n",
           replacement = "")
    
    # create named DNAStringSet object
    dss <- DNAStringSet(x = seqs_)
    names(dss) <- names_
    dss
    
  }

seqlist <- c("NC_045512", "OR558592")
cov2 <- entrez2dss(seqlist)


---

### Answer by username_0 on 2023-10-17. Score: 1

The previous error message was because the entrez_fetch() function returns the sequence as a character string, not a file path. To save the sequence to a file, I can use the writeLines() function. For example, the following code will save the sequence to a file named cov2.fasta:
# Download the FASTA sequence
cov2 <- entrez_fetch(db = "nucleotide", rettype = "fasta", id = c("NC_045512", "OR558592"))
# Save the sequence to a file
writeLines(cov2, "cov2.fasta")
# Read the FASTA sequence into a DNAStringSet object
cov2_dna <- readDNAStringSet("cov2.fasta")


---

