-1

I am new to R and trying to figure out how to print/save a class S4 object that is nested into a list.

This is the structure of the list. The list contains many of these mm9 fasta sequence subsets like the one in the figure. They contain different numbers of rows listing the matches between an input sequence across many fasta files:

the list

If I try to use write.table like:

write.table(tgt_cacgtg, file="tgt_cacgtg.csv", sep=",")

I get:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, arguments imply differing number of rows: 1, 6, 5, 2, 7, 4, 11, 3, 17, 8, 9

If I try to use sink I get a txt file that lists a max of 11 rows per mm9 fasta sequence and gives me something like this otherwise:

$`mm9_chr7_149721485_149878684_+_tgt17'
Views on a 157199-letter BString subject
   subject: ggaccagctcagcaggggcagggggcaggagcagatccctggag...gagtctgtgttctcactccggcctaaaacttttgccacactctc

   views:
          start    end width
     [1]   3291   3296     6 [cacgtg]
     [2]  11561  11566     6 [cacgtg]
     [3]  16247  16252     6 [cacgtg]
     [4]  24249  24254     6 [cacgtg]
     [5]  26048  26053     6 [cacgtg]
     ...    ...    ...   ... ...
   [13]  86453  86458     6 [cacgtg]
   [14] 119849 119854     6 [cacgtg]
   [15] 129404 129409     6 [cacgtg]
   [16] 145612 145617     6 [cacgtg]
   [17] 150437 150442     6 [cacgtg]

Any suggestion on how I can save the class S4 into a file?

John Paul
  • 12,196
  • 6
  • 55
  • 75
Luca
  • 1
  • Welcome to StackOverflow! I've made some relatively minor edits to your question to hopefully make it clearer and easier for people to answer well. The main thing is that I've in-lined your screenshot of the list structure, so people don't have to go off to imgur to see it. It would be better still if you can edit the question include that information in a code block as text here rather than an image. – DaveyDaveDave Aug 17 '18 at 08:10

1 Answers1

0

write table is only meant to write rectangular objects (like data.frames) to a file. You might think your object is rectangular, but the extra metadata stored in its class structure can't be saved that way.

There is a function save which can save (almost) any kind of R object:

save(tgt_cacgtg,file="tgt_cacgtg.RData")

It can then be loaded with

load("tgt_cacgtg.RData")

(Important: don't assign the value of load!

load("tgt_cacgtg.RData") -> tgt_cacgtg

will not do what you want. The object is automatically loaded with its old name)

JDL
  • 1,496
  • 10
  • 18
  • My bad that I did not explain more clearly that I need a .csv (or even .txt) file listing each of the ranges and the line of text with the fasta sequence identifier. – Luca Aug 18 '18 at 16:35
  • I'm not familiar with the particular class you are using but does calling `as.data.frame` on either the object or its constituent parts produce anything usable? If not, then look at the help page describing the class. You may be able to get what you want by extracting a particular slot or slots of the object using the `@` operator. – JDL Aug 20 '18 at 07:14