Skip to content

Nearest Genes Function #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ VignetteBuilder: knitr
License: BSD_2_clause + file LICENSE
biocViews: Software, GenomeAnnotation, GenomeAssembly, DataRepresentation, Sequencing,
Coverage, FunctionalGenomics, Visualization
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
URL: http://code.databio.org/GenomicDistributions
BugReports: http://github.com/databio/GenomicDistributions
Encoding: UTF-8
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(calcFeatureDist)
export(calcFeatureDistRefTSS)
export(calcGCContent)
export(calcGCContentRef)
export(calcNearestGenes)
export(calcNearestNeighbors)
export(calcNeighborDist)
export(calcOpenSignal)
Expand Down
34 changes: 34 additions & 0 deletions R/nearest-genes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Given a query and set of annotations, this function will calculate
#' the nearest annotation to each region in the region set, as well
#' as the nearest gene type and the distance to the nearest gene.
#'
#' @param query A GRanges or GRangesList object with query sets
#' @param annotations A GRanges or GRangesList object with annotation sets
#'
#' @return A data table that contains observations for each genomic region
#' and the associated aforementioned annotations.
#' @export
#' @examples
#' queryFile = system.file("extdata", "vistaEnhancers.bed.gz", package="GenomicDistributions")
#' query = rtracklayer::import(queryFile)
#' data(TSS_hg19)
#'
#' queryAnnotated = calcNearestGenes(query, TSS_hg19)
calcNearestGenes = function(query, annotation, gene_name_key="gene_id", gene_type_key="gene_biotype") {
.validateInputs(list(query=c("GRanges","GRangesList")))

# calculate the nearest annotations to given query
nearestIds = nearest(query, annotations)

# annotate neaest gene and type
query$nearest_gene = annotations[nearestIDs]$gene_id
query$nearest_gene_type = annotations[nearestIds]$gene_biotype

# annotate on the distance as well
query$nearest_distance = distance(query, annotations[nearestIds])

# dump a query to a data table and return
dt = grToDt(query)

return(dt)
}
36 changes: 36 additions & 0 deletions man/calcNearestGenes.Rd

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