<blockquote>
  <p>Is there any way to force R to give me the same representation as <code>head</code>?</p>
</blockquote>

<p><code>head</code> just cuts off everything after the first six entries. The magic, as it were, happens in the default <code>print</code> method for the specific type. But the representation generated by it is only useful for ad-hoc display, not for storage in a structured data file.</p>

<p>The best solution is therefore to manually convert the column types. Since it’s only one column:</p>

<pre><code>coding$ALT = sapply(coding$ALT, as.character)
</code></pre>

<p>I’m a bit puzzled that the <code>REF</code> and <code>ALT</code> column have different types, anyway: that’s probably a bug in the {VariantAnnotation} package that should be reported.</p>

<p>Furthermore, you probably want to use <code>quote = FALSE</code> for the <code>write.table</code>. In general, <code>write.table</code> has terrible defaults. <code>readr::write_tsv</code> is almost always a better choice.</p>

CC BY-SA 3.0
2018-01-22T12:58:07.000Z
Unfortunately, if I perform `as.character(coding$ALT)` (not REF, that gets written fine) I get a `"<S4 object of class \"DNAStringSet\">"`
mgalardini
CC BY-SA 3.0
2018-01-22T13:07:30.360Z
I removed the cast to `data.frame` around the `predictCoding` function and now I get this message when trying to cast to character: `Error in as.vector(x, mode = "character") : no method for coercing this S4 class to a vector`
mgalardini
CC BY-SA 3.0
2018-01-22T13:44:30.380Z
@mgalardini That’s bizarre, it works for me. But maybe try loading the {Biostrings} package explicitly — it sounds like the method call is incorrectly dispatched (but it works for me even without attaching the package; as it should).
Konrad Rudolph
CC BY-SA 3.0
2018-01-22T13:59:13.977Z
If this continues to not work you can also try to invoke `as.character` on individual `DNAString`s, like so: `sapply(coding$REF, as.character)`. However, if the former doesn’t work I fear the latter won’t either.
Konrad Rudolph
CC BY-SA 3.0
2018-01-22T14:20:17.810Z
This last suggestion actually worked; I ignore while the others didn't... I'll update your answer, and thanks a lot!
mgalardini
CC BY-SA 3.0
2018-01-22T12:04:38.390Z
2018-01-22T14:51:48.223Z
2018-01-22T14:51:48.223Z
Konrad Rudolph
<p>I'm having some problems in exporting the annotations of some variants from a VCF file, after using the "<a href="https://bioconductor.org/packages/release/bioc/html/VariantAnnotation.html" rel="nofollow noreferrer">VariantAnnotation</a>" package from bioconductor. Below the script I've used:</p>

<pre><code>#!/usr/bin/env Rscript

library(VariantAnnotation)
library(BSgenome.Scerevisiae.UCSC.sacCer3)
library(TxDb.Scerevisiae.UCSC.sacCer3.sgdGene)

args = commandArgs(trailingOnly=TRUE)

vcf &lt;- readVcf(args[1], "sacCer3")

sc &lt;- BSgenome.Scerevisiae.UCSC.sacCer3
txdb &lt;- TxDb.Scerevisiae.UCSC.sacCer3.sgdGene

coding &lt;- data.frame(predictCoding(vcf, txdb, seqSource=sc))
</code></pre>

<p>The <code>coding</code> dataframe looks like this:</p>

<pre><code>  seqnames start end width strand paramRangeID REF     ALT QUAL FILTER
1     chrI   455 455     1      +         &lt;NA&gt;   C       T   40   PASS
2     chrI   455 455     1      +         &lt;NA&gt;   C           40   PASS
3     chrI   455 455     1      +         &lt;NA&gt;   C       G   40   PASS
4     chrI   455 455     1      +         &lt;NA&gt;   C       A   40   PASS
5     chrI   456 456     1      +         &lt;NA&gt;   T       A   40   PASS
6     chrI   456 456     1      +         &lt;NA&gt;   C CCTGCTC   40   PASS
  varAllele CDSLOC.start CDSLOC.end CDSLOC.width PROTEINLOC QUERYID TXID CDSID
1         T          121        121            1         41       1    1     1
2                    121        121            1         41       2    1     1
3         G          121        121            1         41       3    1     1
4         A          121        121            1         41       4    1     1
5         A          122        122            1         41       5    1     1
6   CCTGCTC          122        122            1         41       6    1     1
   GENEID    CONSEQUENCE REFCODON  VARCODON REFAA VARAA
1 YAL069W  nonsynonymous      CTC       TTC     L     F
2 YAL069W not translated      CTC        TC            
3 YAL069W  nonsynonymous      CTC       GTC     L     V
4 YAL069W  nonsynonymous      CTC       ATC     L     I
5 YAL069W  nonsynonymous      CTC       CAC     L     H
6 YAL069W  nonsynonymous      CTC CCCTGCTCC     L   PCS
</code></pre>

<p>But if I try to save it to file, for instance using the following command:</p>

<pre><code>write.table(coding, 'test.tsv', sep='\t', row.names=F)
</code></pre>

<p>The output table looks like this:</p>

<pre><code>"seqnames"      "start" "end"   "width" "strand"        "paramRangeID"  "REF"   "ALT"   "QUAL"  "FILTER"        "varAllele"     "CDSLOC.start"  "CDSLOC.end"    "CDSLOC.width"  "PROTEINLOC"    "QUERYID"       "TXID"  "CDSID" "GENEID"        "CONSEQUENCE"   "REFCODON"      "VARCODON"      "REFAA" "VARAA"             
"chrI"  455     455     1       "+"     NA      "C"     &lt;S4 object of class "DNAStringSet"&gt;     40      "PASS"  "T"     121     121     1       41      1       "1"     1       "YAL069W"       "nonsynonymous" "CTC"   "TTC"   "L"     "F"                            
"chrI"  455     455     1       "+"     NA      "C"     &lt;S4 object of class "DNAStringSet"&gt;     40      "PASS"  ""      121     121     1       41      2       "1"     1       "YAL069W"       "not translated" "CTC"   "TC"    ""      ""
</code></pre>

<p>Notice the <code>&lt;S4 object of class "DNAStringSet"&gt;</code> bit. Is there any way to force R to give me the same representation as <code>head</code>? I couldn't find anything in Biostrings' documentation.</p>

CC BY-SA 3.0
2018-01-22T12:04:03.827Z
You might be able to convert the DNAStringSet to character and then store it. But it might be a function available to store a GRange to a file I am not aware
llrs
<r><bioconductor><variation>
Write the output of the VariantAnnotation package to file
# Write the output of the VariantAnnotation package to file

(Asked by: username_0 on 2018-01-22)

I'm having some problems in exporting the annotations of some variants from a VCF file, after using the "VariantAnnotation" package from bioconductor. Below the script I've used:
#!/usr/bin/env Rscript

library(VariantAnnotation)
library(BSgenome.Scerevisiae.UCSC.sacCer3)
library(TxDb.Scerevisiae.UCSC.sacCer3.sgdGene)

args = commandArgs(trailingOnly=TRUE)

vcf <- readVcf(args[1], "sacCer3")

sc <- BSgenome.Scerevisiae.UCSC.sacCer3
txdb <- TxDb.Scerevisiae.UCSC.sacCer3.sgdGene

coding <- data.frame(predictCoding(vcf, txdb, seqSource=sc))

The coding dataframe looks like this:
  seqnames start end width strand paramRangeID REF     ALT QUAL FILTER
1     chrI   455 455     1      +            C       T   40   PASS
2     chrI   455 455     1      +            C           40   PASS
3     chrI   455 455     1      +            C       G   40   PASS
4     chrI   455 455     1      +            C       A   40   PASS
5     chrI   456 456     1      +            T       A   40   PASS
6     chrI   456 456     1      +            C CCTGCTC   40   PASS
  varAllele CDSLOC.start CDSLOC.end CDSLOC.width PROTEINLOC QUERYID TXID CDSID
1         T          121        121            1         41       1    1     1
2                    121        121            1         41       2    1     1
3         G          121        121            1         41       3    1     1
4         A          121        121            1         41       4    1     1
5         A          122        122            1         41       5    1     1
6   CCTGCTC          122        122            1         41       6    1     1
   GENEID    CONSEQUENCE REFCODON  VARCODON REFAA VARAA
1 YAL069W  nonsynonymous      CTC       TTC     L     F
2 YAL069W not translated      CTC        TC            
3 YAL069W  nonsynonymous      CTC       GTC     L     V
4 YAL069W  nonsynonymous      CTC       ATC     L     I
5 YAL069W  nonsynonymous      CTC       CAC     L     H
6 YAL069W  nonsynonymous      CTC CCCTGCTCC     L   PCS

But if I try to save it to file, for instance using the following command:
write.table(coding, 'test.tsv', sep='\t', row.names=F)

The output table looks like this:
"seqnames"      "start" "end"   "width" "strand"        "paramRangeID"  "REF"   "ALT"   "QUAL"  "FILTER"        "varAllele"     "CDSLOC.start"  "CDSLOC.end"    "CDSLOC.width"  "PROTEINLOC"    "QUERYID"       "TXID"  "CDSID" "GENEID"        "CONSEQUENCE"   "REFCODON"      "VARCODON"      "REFAA" "VARAA"             
"chrI"  455     455     1       "+"     NA      "C"          40      "PASS"  "T"     121     121     1       41      1       "1"     1       "YAL069W"       "nonsynonymous" "CTC"   "TTC"   "L"     "F"                            
"chrI"  455     455     1       "+"     NA      "C"          40      "PASS"  ""      121     121     1       41      2       "1"     1       "YAL069W"       "not translated" "CTC"   "TC"    ""      ""

Notice the  bit. Is there any way to force R to give me the same representation as head? I couldn't find anything in Biostrings' documentation.

**Tags:** r, bioconductor, variation
> **Comments:**
> **username_1**: You might be able to convert the DNAStringSet to character and then store it. But it might be a function available to store a GRange to a file I am not aware  

---

## 1 answers

### Answer by username_2 on 2018-01-22. Score: 2 | Accepted answer

Is there any way to force R to give me the same representation as head?

head just cuts off everything after the first six entries. The magic, as it were, happens in the default print method for the specific type. But the representation generated by it is only useful for ad-hoc display, not for storage in a structured data file.
The best solution is therefore to manually convert the column types. Since it’s only one column:
coding$ALT = sapply(coding$ALT, as.character)

I’m a bit puzzled that the REF and ALT column have different types, anyway: that’s probably a bug in the {VariantAnnotation} package that should be reported.
Furthermore, you probably want to use quote = FALSE for the write.table. In general, write.table has terrible defaults. readr::write_tsv is almost always a better choice.

> **Comments:**
> **username_0**: Unfortunately, if I perform `as.character(coding$ALT)` (not REF, that gets written fine) I get a `"<S4 object of class \"DNAStringSet\">"`  
> **username_0**: I removed the cast to `data.frame` around the `predictCoding` function and now I get this message when trying to cast to character: `Error in as.vector(x, mode = "character") : no method for coercing this S4 class to a vector`  
> **username_2**: @mgalardini That’s bizarre, it works for me. But maybe try loading the {Biostrings} package explicitly — it sounds like the method call is incorrectly dispatched (but it works for me even without attaching the package; as it should).  
> **username_2**: If this continues to not work you can also try to invoke `as.character` on individual `DNAString`s, like so: `sapply(coding$REF, as.character)`. However, if the former doesn’t work I fear the latter won’t either.  
> **username_0**: This last suggestion actually worked; I ignore while the others didn't... I'll update your answer, and thanks a lot!  

---

