Question: Cannot coerce class 'structure("IRanges", package = "IRanges")' into a data.frame error.
0
gravatar for Aishwarya.Kulkarni
5.3 years ago by
United States
Aishwarya.Kulkarni20 wrote:

Hi,

I am trying to output a result of matchPWM and I get the following error when I try to output it using write command:

write(as.data.frame(data), "chr9_RBPJ_R.txt", sep="\n")
cannot coerce class 'structure("IRanges", package = "IRanges")' into a data.frame

where data is 

data<-list(hits = as(hits, "IRanges"), scores = scores)

hits=results of matchPWM and scores are the scores for each hit.

here is the sessioninfo

> sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] BSgenome.Hsapiens.UCSC.hg19_1.3.19 BSgenome_1.26.1
[3] GenomicRanges_1.10.7               Biostrings_2.26.3
[5] IRanges_1.16.6                     BiocGenerics_0.4.0

loaded via a namespace (and not attached):
[1] parallel_2.15.3 stats4_2.15.3   tools_2.15.3

Thanks in advance

R • 5.9k views
ADD COMMENTlink modified 4.6 years ago by Biostar ♦♦ 20 • written 5.3 years ago by Aishwarya.Kulkarni20

Why would you expect there to be a coercion method for a custom structure?

ADD REPLYlink written 5.3 years ago by Devon Ryan92k

ok, if that's the case then how do I output the structure into a file?

ADD REPLYlink written 5.3 years ago by Aishwarya.Kulkarni20

help(save)

ADD REPLYlink written 5.3 years ago by Devon Ryan92k

I did try saving it with save , however it gives me a gibberish output in the file.

ADD REPLYlink written 5.3 years ago by Aishwarya.Kulkarni20

The results from save() aren't meant to be human readable. If you want this written to a text file then you'll need to either write a method to convert it to a type that can be directly written as text or write your own method to write it in some convenient text format.

ADD REPLYlink written 5.3 years ago by Devon Ryan92k

cat(data, file="Report.out")

ADD REPLYlink written 5.3 years ago by insilico12310

save() is meant for saving your current workspace which can be read back from the file at a
     later date by using the function ‘load’. It is not for printing the output to a file

ADD REPLYlink written 5.3 years ago by insilico12310

save() can save individual objects, not just the workspace.

ADD REPLYlink written 5.3 years ago by Devon Ryan92k

Ya individual objects can be saved, but it can be retrieved to the workspace by load() method. But here she wants to print the data to a file, not for loading the data to the workspace.

ADD REPLYlink written 5.3 years ago by insilico12310

Thanks a lot!

ADD REPLYlink written 5.3 years ago by Aishwarya.Kulkarni20

when I give the command, it gives me an error

cat(hits, file="ABC.txt")

Error in cat(list(...), file, sep, fill, labels, append) : 

  argument 1 (type 'S4') cannot be handled by 'cat'

> class(hits)

[1] "XStringViews"

attr(,"package")

[1] "Biostrings"

ADD REPLYlink modified 5.3 years ago • written 5.3 years ago by Aishwarya.Kulkarni20

Are you trying to write the pair wise alignments to a file? Can you give some code to show your data & what functions you used? Some reproducible example?

ADD REPLYlink modified 5.3 years ago • written 5.3 years ago by komal.rathi3.5k

Hi I am using matchPWM a function to scan a position weight matrix on and sequence to get the hits, and I am trying to output these hits into a file. 

ADD REPLYlink modified 5.3 years ago • written 5.3 years ago by Aishwarya.Kulkarni20

you can use sink

It can output any variable into a file including XStringViews

 

ADD REPLYlink modified 5.3 years ago • written 5.3 years ago by insilico12310

Ok it is still difficult to debug your problem without a reproducible example but anyway, does your "data" object look like this:

  start      end width
    [1]       96      103     8 [ATAAACAT]
    [2]      264      271     8 [GTAAGCAG]
    [3]      317      324     8 [GCAAACAA]
    [4]      488      495     8 [GTAAAGAG]
    [5]     1400     1407     8 [GGAAACAC]

And do you want to write this entire set to a file or just the matched sequences given within the square brackets?

ADD REPLYlink written 5.3 years ago by komal.rathi3.5k

Yes exactly!

ADD REPLYlink written 5.3 years ago by Aishwarya.Kulkarni20

then you can sink subject(hits) to a file

ADD REPLYlink modified 5.3 years ago • written 5.3 years ago by insilico12310

I didn't quiet understand, do you mean cat(hits, file="ABC.txt") ?

ADD REPLYlink written 5.3 years ago by Aishwarya.Kulkarni20

use it to save the hits

sink("ABC.txt")
hits
sink()

 

ADD REPLYlink written 5.3 years ago by insilico12310

Give some code to show your data, or you can try use sink

 

sink("ABC.txt")
hits
sink()

 

ADD REPLYlink modified 5.3 years ago • written 5.3 years ago by insilico12310
2
gravatar for komal.rathi
5.3 years ago by
komal.rathi3.5k
Children's Hospital of Philadelphia, Philadelphia, PA
komal.rathi3.5k wrote:

Okay, so this may be not the best method but works,

> class(dat)
[1] "XStringViews"
attr(,"package")
[1] "Biostrings"
> x <- data.frame(start=start(dat),end=end(dat),width=width(dat),as(dat,"XStringSet"))

x is a data.frame object and you can write it to a file like you'd normally do.

Update: Aishwarya.Kulkarni I have edited my code to make it faster. Give it a try.

ADD COMMENTlink modified 5.3 years ago • written 5.3 years ago by komal.rathi3.5k
1
gravatar for brentp
5.3 years ago by
brentp23k
Salt Lake City, UT
brentp23k wrote:

You can use `as.data.frame` on a GRanges object and it can have your scores attached, so:

 

library(GenomicRanges)

scores = c("good", "better", "best")
g = GRanges(seqnames=c("1", "2", "3"), ranges=IRanges(start=c(100, 200, 300), width=20))

values(g) = scores
print(as.data.frame(g))

 

Outputs:

  seqnames start end width strand  value
1        1   100 119    20      *   good
2        2   200 219    20      * better
3        3   300 319    20      *   best
ADD COMMENTlink modified 5.3 years ago • written 5.3 years ago by brentp23k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 3173 users visited in the last hour