How to query a GRanges object into a object that can be queried again?

49 Views Asked by At

"I loaded a TxDb object created from Gencode and queried it for the exons, then I used lapply to get all the LAST exons from each transcript that has >1 exon:

#Load db:
Gencode <- loadDb("gencode.v39.basic.annotation.sqlite")

#Exons by transcript
Exons <- exonsBy(Gencode, by = "tx", use.names=TRUE)

#Get Exon Counts
ExonCount <- as.data.frame(elementNROWS(Exons))

#Get the last exon for every transcript
lapply(1:nrow(ExonCount), function(x) Exons[[x]][ExonCount$`elementNROWS(Exons)`[x]])

Next, I want to perform actions on these ranges. Such as shift() or anythign else. However I get a warning:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘resize’ for signature ‘"list"’

My question is, how can I get this object back into a format I can manipulate with GRanges functions?

Alternatively, how can I get the final exon of every transcripts in a way that avoids this problem?

1

There are 1 best solutions below

0
Sky Scraper On

I do not know how to query the final exon in another way, but I did find a way to reformat the lapply output:

My attempt was to concatenate each individual element from the lapply() function back into a GRanges object:

  #Function to take each element in the list objects and put concatenate into a GRanges Object:

ListOfGRangesToGRangeList <- function(ListToConvert) {
  ConvertedObject <- GRangesList(ListToConvert[[1]], ListToConvert[[2]])
  for (x in 3:length(ListToConvert)){
    ConvertedObject <- GRangesList(unlist(ConvertedObject), ListToConvert[[x]])
    ConvertedObject <- unlist(ConvertedObject)
  }
  return(ConvertedObject)

or

ListOfGRangesToGRangeList <- function(ListToConvert) {
  ConvertedObject <- c(ListToConvert[[1]], ListToConvert[[2]])
  for (x in 3:length(ListToConvert)){
    ConvertedObject <- c(ConvertedObject, ListToConvert[[x]])
  }
  return(ConvertedObject)