Answers.Body: <p>According to the documentation (<code>?Biostrings::DNAStringSet</code>):</p>
  
  <blockquote>
    <p><code>width(x)</code>: A vector of non-negative integers containing the number of
              letters for each element in <code>x</code>.  Note that <code>width(x)</code> is
              also defined for a character vector with no NAs and is
              equivalent to <code>nchar(x, type="bytes")</code>.</p>
    
    <p><code>names(x)</code>: <code>NULL</code> or a character vector of the same length as <code>x</code>
              containing a short user-provided description or comment for
              each element in <code>x</code>.  These are the only data in an
              XStringSet object that can safely be changed by the user. All
              the other data are immutable!  As a general recommendation,
              the user should never try to modify an object by accessing
              its slots directly.</p>
    
    <p><code>as.character(x, use.names=TRUE)</code>: Converts <code>x</code> to a character vector
              of the same length as <code>x</code>.  The <code>use.names</code> argument controls
              whether or not <code>names(x)</code> should be propagated to the names
              of the returned vector.</p>
  </blockquote>
  
  <p>This should be enough for you to create a <code>data.frame</code>:</p>
  
  <p><code>dss2df &lt;- function(dss) data.frame(width=width(dss), seq=as.character(dss), names=names(dss))
  </code></p>
  
  <p>Also, if you have a huge <code>DNAStringSet</code>, perhaps you do not want to convert all the sequences to characters; storing sequences as characters supposedly is less efficient. For plotting it might be sufficient to have just widths.</p>
  
  <p><code>qplot(width(dss), geom='histogram'))</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-26T08:35:40.497Z
Answers.LastActivityDate: 2018-10-26T08:42:10.457Z
Answers.LastEditDate: 2018-10-26T08:42:10.457Z
Answers.UserDisplayName: Iakov Davydov
Body: <p>I am working on Mac OS X. I am using R version 3.5.1.</p>
  
  <p>I have important a FASTA file into R using <code>Biostrings::readDNAStringSet</code>. This creates a DNAStringSet object in R.</p>
  
  <p>I would like to transform it into a data frame so that I can then plot the widths with ggplot2.</p>
  
  <p><code>as.data.frame</code> on my DNAStringSet object returns a data frame with only one column. Is there a function that returns a data frame with columns <code>width</code>, <code>seq</code> and <code>names</code> present in the DNAStringSet object?</p>
  
Tags: <r><bioconductor><format-conversion><biostrings>
Title: How to transform a DNAStringSet from the Bioconductor package Biostrings to a data frame?
ThreadText: # How to transform a DNAStringSet from the Bioconductor package Biostrings to a data frame?
  
  (Asked by: username_0 on 2018-10-25)
  
  I am working on Mac OS X. I am using R version 3.5.1.
  
  I have important a FASTA file into R using `Biostrings::readDNAStringSet`. This creates a DNAStringSet object in R.
  
  I would like to transform it into a data frame so that I can then plot the widths with ggplot2.
  
  `as.data.frame` on my DNAStringSet object returns a data frame with only one column. Is there a function that returns a data frame with columns `width`, `seq` and `names` present in the DNAStringSet object?
  
  **Tags:** r, bioconductor, format-conversion, biostrings
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-26. Score: 8 | Accepted answer
  
  According to the documentation (`?Biostrings::DNAStringSet`):
  
  > `width(x)`: A vector of non-negative integers containing the number of
  > letters for each element in `x`. Note that `width(x)` is
  > also defined for a character vector with no NAs and is
  > equivalent to `nchar(x, type="bytes")`.
  >
  > `names(x)`: `NULL` or a character vector of the same length as `x`
  > containing a short user-provided description or comment for
  > each element in `x`. These are the only data in an
  > XStringSet object that can safely be changed by the user. All
  > the other data are immutable! As a general recommendation,
  > the user should never try to modify an object by accessing
  > its slots directly.
  >
  > `as.character(x, use.names=TRUE)`: Converts `x` to a character vector
  > of the same length as `x`. The `use.names` argument controls
  > whether or not `names(x)` should be propagated to the names
  > of the returned vector.
  
  This should be enough for you to create a `data.frame`:
  
  `dss2df <- function(dss) data.frame(width=width(dss), seq=as.character(dss), names=names(dss))`
  
  Also, if you have a huge `DNAStringSet`, perhaps you do not want to convert all the sequences to characters; storing sequences as characters supposedly is less efficient. For plotting it might be sufficient to have just widths.
  
  `qplot(width(dss), geom='histogram'))`
  
  
  ---
  
  