GRanges objects don't support [[, as.list(), lapply(), or unlist() at the moment
1
0
Entering edit mode
13 months ago
Antonia • 0

I am trying to get the combined peaks from a list of objects in atac data and I am running into the following error.

GRanges objects don't support [[, as.list(), lapply(), or unlist() at the moment

My code is

combined.peaks <- reduce(c(peaks$sample1, peaks$sample2,
                           peaks$sample3, peaks$sample4
                           ))

This was working a few days ago btw... Is this a new bug?

Thanks!

T

GRanges • 3.9k views
ADD COMMENT
0
Entering edit mode

Please add a reproducible example via dput.

ADD REPLY
0
Entering edit mode
inputFiles <- c("./sample1/outs/fragments.tsv.gz", "./sample2/outs/fragments.tsv.gz", "./sample3/outs/fragments.tsv.gz", "./sample4/outs/fragments.tsv.gz")'

names(inputFiles) = c("sample1", "sample2", "sample3", "sample4") barcodes = list() peaks = GRangesList()

for(i in names(matrixFiles)){ barcodes[[i]] = as.data.frame(readr::read_tsv(file = paste0(matrixFiles[i],"filtered_peak_bc_matrix/barcodes.tsv")), col_names = F)[,1] p = as.data.frame(readr::read_tsv(file = paste0(matrixFiles[i],"filtered_peak_bc_matrix/peaks.bed")), col_names = F)[,1:3] colnames(p) = c("chr","start","end") peaks[[i]] = makeGRangesFromDataFrame(df = p) }

combined.peaks <- reduce(c(peaks$sample1, peaks$sample2, peaks$sample3, peaks$sample4 ))
ADD REPLY
0
Entering edit mode

This is obviously not reproducible for anyone that does not have your fragments.tsv.gz files. Please follow ATPoint's advice and use dput on objects.

ADD REPLY
0
Entering edit mode

We still do not know how exactly your GRanges objects look, that is why I asked for dput.

This works:

gr0 <- GRanges(Rle(c("chr2", "chr2", "chr1", "chr3"), c(1, 3, 2, 4)),
               IRanges(1:10, width=10:1))

gr1 <- gr0
gr2 <- gr0

reduce(c(gr0, gr1, gr2))

so if you need help provide data (not code, and not as a screenshot)

ADD REPLY
0
Entering edit mode

I am sorry for that. This is what my object looks like. Is this more useful to you for providing a solution?

> peaks
GRangesList object of length 11:
$sample1
GRanges object with 98860 ranges and 0 metadata columns:
          seqnames            ranges strand
             <Rle>         <IRanges>  <Rle>
      [1]     chr1     629770-630550      *
      [2]     chr1     633794-634259      *
      [3]     chr1     778203-779262      *
      [4]     chr1     817083-817625      *
      [5]     chr1     821110-821501      *
      ...      ...               ...    ...
  [98856]     chrY 56868863-56871397      *
  [98857]     chrY 56872276-56872281      *
  [98858]     chrY 56873716-56874522      *
  [98859]     chrY 56877566-56877837      *
  [98860]     chrY 56879680-56880015      *
  -------
  seqinfo: 24 sequences from an unspecified genome; no seqlengths

...
<10 more elements>
ADD REPLY
0
Entering edit mode

Please show the output of

dput(sapply(peaks, function(x) head(x, 2), simplify=FALSE))
ADD REPLY
0
Entering edit mode
list(AML5226 = new("GRanges", seqnames = new("Rle", values = structure(1L, levels = c("chr1", 
"chr2", "chr3", "chr4", "chr5", "chr6", "chr7", "chr8", "chr9", 
"chr10", "chr11", "chr12", "chr13", "chr14", "chr15", "chr16", 
"chr17", "chr18", "chr19", "chr20", "chr21", "chr22", "chrX", 
"chrY"), class = "factor"), lengths = 2L, elementMetadata = NULL, 
    metadata = list()), ranges = new("IRanges", start = c(629770L, 
633794L), width = c(781L, 466L), NAMES = NULL, elementType = "ANY", 
    elementMetadata = NULL, metadata = list()), strand = new("Rle", 
    values = structure(3L, levels = c("+", "-", "*"), class = "factor"), 
    lengths = 2L, elementMetadata = NULL, metadata = list()), 
    seqinfo = new("Seqinfo", seqnames = c("chr1", "chr2", "chr3", 
    "chr4", "chr5", "chr6", "chr7", "chr8", "chr9", "chr10", 
    "chr11", "chr12", "chr13", "chr14", "chr15", "chr16", "chr17", 
    "chr18", "chr19", "chr20", "chr21", "chr22", "chrX", "chrY"
    ), seqlengths = c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_), is_circular = c(NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA), genome = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_)), elementMetadata = new("DFrame", rownames = NULL, 
        nrows = 2L, elementType = "ANY", elementMetadata = NULL, 
        metadata = list(), listData = structure(list(), names = character(0))), 
    elementType = "ANY", metadata = list()),
ADD REPLY
1
Entering edit mode

It's incomplete. Without a reproducible this thread is pointless.

ADD REPLY
0
Entering edit mode

This is a long output and it's not accepted by biostars to be posted. Can I send this to you via email?

ADD REPLY
1
Entering edit mode
ADD REPLY
0
Entering edit mode
13 months ago

Something is probably wrong with your files and one of the entries is not a GRanges object

combined.peaks <- reduce(unlist(GRangesList(c(peaks$sample1, peaks$sample2,peaks$sample3, peaks$sample4))))

You probably need to provide the head() of the peaks$sample1 etc...for us to be certain

ADD COMMENT
0
Entering edit mode

This was working on Friday, it is not working now. So, I don't think this is related to my data, but rather something to GRanges.

ADD REPLY
0
Entering edit mode

Did you update R?

ADD REPLY
0
Entering edit mode

No, nothing should have been changed.

ADD REPLY
0
Entering edit mode

Something has changed...

what does lapply(peaks,length) give you?

ADD REPLY
0
Entering edit mode
> lapply(peaks,length)
$sample1
[1] 98860

$sample2
[1] 76862

$sample3
[1] 120326

$sample4
[1] 85919
ADD REPLY
0
Entering edit mode

Please use the formatting bar (especially the code option) to present your post better. You can use backticks for inline code (`text` becomes text), or select a chunk of text and use the highlighted button to format it as a code block. If your code has long lines with a single command, break those lines into multiple lines with proper escape sequences so they're easier to read and still run when copy-pasted. I've done it for you this time.
code_formatting

ADD REPLY
0
Entering edit mode

What happens if you do like:

reduce(c(peaks$sample1,peaks$sample2))

and like the same for 1 and 3 and 1 and 4... etc.. Do they all fail with the same error or do some work?

ADD REPLY
0
Entering edit mode

yes, any combo of those fails.

ADD REPLY
0
Entering edit mode

Your peaks object is probably a GRanges instead of a GRangesList.

What about trying to set:

peaks <- GRangesList(peaks)

Does the error change?

ADD REPLY
0
Entering edit mode

I have set this up already.

ADD REPLY
1
Entering edit mode

Hi, Were you able to resolve this? I had the same err msg and a google search landed me here. So, posting what I think was the reason in my case at least -

## example GRanges object
summary(my.sel.peaks.gr)
# [1] "GRanges object with 7536 ranges and 0 metadata columns"
##
library(tidyverse)
## Among other msgs of conflicts
# purrr::reduce()       masks GenomicRanges::reduce(), IRanges::reduce()
##
## So -
reduce(my.sel.peaks.gr)
# Error in getListElement(x, i, ...) : 
#   GRanges objects don't support [[, as.list(), lapply(), or unlist() at
#   the moment
##
## But -
GenomicRanges::reduce(my.sel.peaks.gr)
# GRanges object with 7269 ranges and 0 metadata columns:
#          seqnames            ranges strand
#             <Rle>         <IRanges>  <Rle>
#      [1]     chr1     629793-630083      *
#      [2]     chr1     632733-633039      *
#      [3]     chr1     633868-634183      *
#      ...      ...               ...    ...
#   [7269]     chrY 19567023-19567392      *
#   -------
#   seqinfo: 24 sequences from an unspecified genome; no seqlengths
##

So, explicitly specifying the package from where I wanted to use reduce from, resolved the issue. Selected session info -

R version 4.2.0 (2022-04-22)
GenomicRanges_1.50.0
tidyverse_2.0.0
purrr_1.0.1
ADD REPLY

Login before adding your answer.

Traffic: 2725 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