Skip to content

Commit

Permalink
Merge pull request #119 from charles-plessy/annot_from_TxDB
Browse files Browse the repository at this point in the history
annotateCTSS method for TxDB objects

Closes #30, #53
  • Loading branch information
charles-plessy authored May 31, 2024
2 parents be47c26 + 00ef961 commit e32db0c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Imports:
formula.tools,
GenomeInfoDb,
GenomicAlignments,
GenomicFeatures,
GenomicRanges (>= 1.37.16),
ggplot2 (>= 2.2.0),
gtools,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ importFrom(GenomeInfoDb,seqnames)
importFrom(GenomeInfoDb,sortSeqlevels)
importFrom(GenomicAlignments,qwidth)
importFrom(GenomicAlignments,readGAlignments)
importFrom(GenomicFeatures,exons)
importFrom(GenomicFeatures,genes)
importFrom(GenomicFeatures,promoters)
importFrom(GenomicFeatures,transcripts)
importFrom(GenomicRanges,GPos)
importFrom(GenomicRanges,GRanges)
importFrom(GenomicRanges,GRangesList)
Expand Down
45 changes: 45 additions & 0 deletions R/Annotations.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ msScope_annotation <- function(libs) {
#' annotateCTSS(exampleCAGEexp, exampleZv9_annot)
#' colData(exampleCAGEexp)
#'
#' @importFrom GenomicFeatures genes
#'
#' @export

setGeneric("annotateCTSS", function(object, ranges, upstream=500, downstream=500)
Expand All @@ -459,6 +461,22 @@ setMethod("annotateCTSS", c("CAGEexp", "GRanges"), function (object, ranges, ups
object
})

#' @rdname annotateCTSS

setMethod("annotateCTSS", c("CAGEexp", "TxDb"), function (object, ranges){
g <- genes(ranges)
g$gene_name <- g$gene_id
annotateCTSS(object, g)
CTSScoordinatesGR(object)$genes <- ranges2genes(CTSScoordinatesGR(object), g)
CTSScoordinatesGR(object)$annotation <- txdb2annot(CTSScoordinatesGR(object), ranges)

annot <- sapply( CTSStagCountDF(object)
, function(X) tapply(X, CTSScoordinatesGR(object)$annotation, sum))
colData(object)[levels(CTSScoordinatesGR(object)$annotation)] <- DataFrame(t(annot))
validObject(object)
object
})

#' `annotateTagClusters` annotates the _tag clusters_ of a _CAGEr_ object.
#'
#' @return `annotateTagClusters` returns the input object with the same
Expand Down Expand Up @@ -619,6 +637,33 @@ ranges2annot <- function(ranges, annot, upstream=500, downstream=500) {
Rle(annot)
}

#' @name txdb2annot
#'
#' @rdname ranges2annot
#'
#' @importFrom GenomicFeatures promoters exons transcripts

txdb2annot <- function(ranges, annot) {
findOverlapsBool <- function(A, B) {
overlap <- findOverlaps(A, B)
overlap <- as(overlap, "List")
any(overlap)
}

classes <- c("promoter", "exon", "intron", "unknown")
p <- findOverlapsBool(ranges, trim(suppressWarnings(promoters(annot, 500, 500))))
e <- findOverlapsBool(ranges, exons(annot))
t <- findOverlapsBool(ranges, transcripts(annot))
annot <- sapply( 1:length(ranges), function(i) {
if (p[i]) {classes[1]}
else if (e[i]) {classes[2]}
else if (t[i]) {classes[3]}
else {classes[4]}
})

annot <- factor(annot, levels = classes)
Rle(annot)
}

#' ranges2genes
#'
Expand Down
3 changes: 3 additions & 0 deletions man/annotateCTSS.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/ranges2annot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e32db0c

Please sign in to comment.