Skip to content

Commit 1ed7a63

Browse files
committed
dynamic access of gene_id and gene_biotype
1 parent b416148 commit 1ed7a63

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

R/nearest-genes.R

+15-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,21 @@ calcNearestGenes = function(query, annotations, gene_name_key="gene_id", gene_t
2525
# calculate the nearest annotations to given query
2626
nearestIds = nearest(query, annotations)
2727

28-
# annotate neaest gene and type
29-
query$nearest_gene = annotations[nearestIds]$gene_id
30-
query$nearest_gene_type = annotations[nearestIds]$gene_biotype
28+
# annotate nearest gene and type
29+
nearestGenes = annotations[nearestIds]
30+
31+
#
32+
# convert nearestGenes GRange object to data-table
33+
# and dynamically access the column that way...
34+
# this is used to circumvent the fact that we cannot
35+
# dynamically access metadata columns inside a GRanges
36+
# object like we can a datatable:
37+
# col = "gene_id"
38+
# dt[[col]]
39+
# ^^^ This doesnt work in a GRanges object.
40+
#
41+
query$nearest_gene = grToDt(nearestGenes)[[gene_name_key]]
42+
query$nearest_gene_type = grToDt(nearestGenes)[[gene_type_key]]
3143

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

0 commit comments

Comments
 (0)