Write the output of the VariantAnnotation package to file 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 &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))


The coding dataframe looks like this:

  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


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"     &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"    ""      ""


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