This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Cannot coerce class 'structure("IRanges", package = "IRanges")' into a data.frame error.

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

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

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

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

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.

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

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

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.

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"

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?

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.

You can use sink

It can output any variable into a file including XStringViews

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?

then you can sink subject(hits) to a file

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

Use it to save the hits

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

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

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

2 answers

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.

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

Log in to answer this question.