Cannot coerce class 'structure("IRanges", package = "IRanges")' into a data.frame error.
2
0
Entering edit mode
9.8 years ago

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 • 9.9k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

help(save)

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

Thanks a lot!

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

You can use sink

It can output any variable into a file including XStringViews

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

Yes exactly!

ADD REPLY
0
Entering edit mode

then you can sink subject(hits) to a file

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Use it to save the hits

sink("ABC.txt")
hits
sink()
ADD REPLY
0
Entering edit mode

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

sink("ABC.txt")
hits
sink()
ADD REPLY
2
Entering edit mode
9.8 years ago
komal.rathi ★ 4.1k

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 COMMENT
1
Entering edit mode
9.8 years ago
brentp 24k

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 COMMENT

Login before adding your answer.

Traffic: 1439 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6