Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions R/coordinate-mapping-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ setGeneric("pmapFromAlignments", signature=c("x", "alignments"),
### mapToAlignments() and mapFromAlignments() methods
###

.mapFromAlignments <- function(x, alignments)
.mapFromAlignments <- function(x, alignments, style = "mapped")
{
if (!length(x) && !length(alignments))
return(GRanges(xHits=integer(), transcriptsHits=integer()))
if (is.null(xNames <- names(x)) ||
is.null(alignmentsNames <- names(alignments)))
stop ("both 'x' and 'alignments' must have names")

style <- switch(style,
mapped = 0,
sequence = 1,
readpos = 2,
stop(paste("specified unsupported style=", style)))

## name matching determines pairs
match0 <- match(alignmentsNames, alignmentsNames)
match1 <- match(xNames, alignmentsNames)
Expand All @@ -53,10 +59,10 @@ setGeneric("pmapFromAlignments", signature=c("x", "alignments"),
alignments <- alignments[alignmentsHits]
s <- .Call("query_locs_to_ref_locs",
start(x), cigar(alignments),
start(alignments), FALSE)
start(alignments), FALSE, style)
e <- .Call("query_locs_to_ref_locs",
end(x), cigar(alignments),
start(alignments), TRUE)
start(alignments), TRUE, style)
e <- pmax(e, s - 1L)

## remove non-hits
Expand All @@ -67,17 +73,23 @@ setGeneric("pmapFromAlignments", signature=c("x", "alignments"),
xHits=xHits[keep], alignmentsHits=alignmentsHits[keep])
}

.mapToAlignments <- function(x, alignments)
.mapToAlignments <- function(x, alignments, style = "sequence")
{
if (!length(x) && !length(alignments))
return(GRanges(xHits=integer(), transcriptsHits=integer()))
if (is.null(names(alignments)))
stop ("'alignments' must have names")

style <- switch(style,
mapped = 0,
sequence = 1,
readpos = 2,
stop(paste("specified unsupported style=", style)))

## map all possible pairs; returns hits only
map <- .Call("map_ref_locs_to_query_locs",
start(x), end(x), cigar(alignments),
start(alignments))
start(alignments), style)
xHits <- map[[3]]
alignmentsHits <- map[[4]]
if (length(xHits))
Expand All @@ -91,30 +103,46 @@ setGeneric("pmapFromAlignments", signature=c("x", "alignments"),

setMethod("mapToAlignments", c("IntegerRanges", "GAlignments"),
function(x, alignments, ...)
ranges(.mapToAlignments(x, alignments))
ranges(.mapToAlignments(x, alignments, ...))
)

setMethod("mapToAlignments", c("GenomicRanges", "GAlignments"),
function(x, alignments, ...)
.mapToAlignments(x, alignments)
.mapToAlignments(x, alignments, ...)
)

setMethod("mapFromAlignments", c("IntegerRanges", "GAlignments"),
function(x, alignments, ...)
ranges(.mapFromAlignments(x, alignments))
ranges(.mapFromAlignments(x, alignments, ...))
)

setMethod("mapFromAlignments", c("GenomicRanges", "GAlignments"),
function(x, alignments, ...)
.mapFromAlignments(x, alignments)
.mapFromAlignments(x, alignments, ...)
)

### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### pmapToAlignments() and pmapFromAlignments() methods
###

.pmapAlignments <- function(x, alignments, reverse)
.pmapAlignments <- function(x, alignments, reverse, style = "default")
{
if (reverse) {
style <- switch(style,
default = 0,
mapped = 0,
sequence = 1,
readpos = 2,
stop(paste("specified unsupported style=", style)))
} else {
style <- switch(style,
default = 1,
mapped = 0,
sequence = 1,
readpos = 2,
stop(paste("specified unsupported style=", style)))
}

if (length(x) && length(alignments)) {
if (length(x) != length(alignments))
stop("'x' and 'alignments' must have the same length")
Expand All @@ -128,8 +156,8 @@ setMethod("mapFromAlignments", c("GenomicRanges", "GAlignments"),
FUN <- "ref_locs_to_query_locs"
seqname <- names(alignments)
}
s <- .Call(FUN, start(x), cigar(alignments), start(alignments), FALSE)
e <- .Call(FUN, end(x), cigar(alignments), start(alignments), TRUE)
s <- .Call(FUN, start(x), cigar(alignments), start(alignments), FALSE, style)
e <- .Call(FUN, end(x), cigar(alignments), start(alignments), TRUE, style)
e <- pmax(e, s - 1L)

## non-hits
Expand All @@ -146,20 +174,20 @@ setMethod("mapFromAlignments", c("GenomicRanges", "GAlignments"),

setMethod("pmapToAlignments", c("IntegerRanges", "GAlignments"),
function(x, alignments, ...)
ranges(.pmapAlignments(x, alignments, FALSE))
ranges(.pmapAlignments(x, alignments, FALSE, ...))
)

setMethod("pmapToAlignments", c("GenomicRanges", "GAlignments"),
function(x, alignments, ...)
.pmapAlignments(ranges(x), alignments, FALSE)
.pmapAlignments(ranges(x), alignments, FALSE, ...)
)

setMethod("pmapFromAlignments", c("IntegerRanges", "GAlignments"),
function(x, alignments, ...)
ranges(.pmapAlignments(x, alignments, TRUE))
ranges(.pmapAlignments(x, alignments, TRUE, ...))
)

setMethod("pmapFromAlignments", c("GenomicRanges", "GAlignments"),
function(x, alignments, ...)
.pmapAlignments(ranges(x), alignments, TRUE)
.pmapAlignments(ranges(x), alignments, TRUE, ...)
)
20 changes: 10 additions & 10 deletions inst/unitTests/test_coordinate-mapping-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ test_ref_locs_to_query_locs <- function() {
ref <- 43425L + pos - 1L
query <- 238L
ans <- .Call("ref_locs_to_query_locs", ref, cigar, pos,
FALSE, PACKAGE="GenomicAlignments")
FALSE, 1, PACKAGE="GenomicAlignments")
checkIdentical(ans, query)

## out of bounds
ans_s <- .Call("ref_locs_to_query_locs",
start(x1[1]), cigar(align1[1]),
start(align1[1]), FALSE,
start(align1[1]), FALSE, 1,
PACKAGE="GenomicAlignments")
ans_e <- .Call("ref_locs_to_query_locs",
end(x1[1]), cigar(align1[1]),
start(align1[1]), TRUE,
start(align1[1]), TRUE, 1,
PACKAGE="GenomicAlignments")

checkIdentical(ans_s, NA_integer_)
Expand All @@ -130,11 +130,11 @@ test_query_locs_to_ref_locs <- function() {
## out of bounds
ans_s <- .Call("query_locs_to_ref_locs",
start(x1[4]), cigar(align1[1]),
start(align1[1]), FALSE,
start(align1[1]), FALSE, 1,
PACKAGE="GenomicAlignments")
ans_e <- .Call("query_locs_to_ref_locs",
end(x1[4]), cigar(align1[1]),
start(align1[1]), TRUE,
start(align1[1]), TRUE, 1,
PACKAGE="GenomicAlignments")

checkIdentical(ans_s, NA_integer_)
Expand All @@ -144,26 +144,26 @@ test_query_locs_to_ref_locs <- function() {
test_map_ref_locs_to_query_locs <- function() {
## hit
map <- .Call("map_ref_locs_to_query_locs",
12L, 16L, "11M", 10L)
12L, 16L, "11M", 10L, 1)
checkIdentical(unlist(map), c(3L, 7L, 1L, 1L))

## first record out of bounds
map <- .Call("map_ref_locs_to_query_locs",
c(5L, 12L), c(16L, 16L), "11M", 10L)
c(5L, 12L), c(16L, 16L), "11M", 10L, 1)
checkIdentical(unlist(map), c(3L, 7L, 2L, 1L))

## second record out of bounds
map <- .Call("map_ref_locs_to_query_locs",
c(12L, 5L), c(16L, 16L), "11M", 10L)
c(12L, 5L), c(16L, 16L), "11M", 10L, 1)
checkIdentical(unlist(map), c(3L, 7L, 1L, 1L))

## first alignment out of bounds
map <- .Call("map_ref_locs_to_query_locs",
12L, 16L, c("11M", "11M"), c(20L, 10L))
12L, 16L, c("11M", "11M"), c(20L, 10L), 1)
checkIdentical(unlist(map), c(3L, 7L, 1L, 2L))

## second alignment out of bounds
map <- .Call("map_ref_locs_to_query_locs",
12L, 16L, c("11M", "11M"), c(10L, 20L))
12L, 16L, c("11M", "11M"), c(10L, 20L), 1)
checkIdentical(unlist(map), c(3L, 7L, 1L, 1L))
}
12 changes: 8 additions & 4 deletions src/GenomicAlignments.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,32 @@ SEXP map_query_locs_to_ref_locs(
SEXP start,
SEXP end,
SEXP cigar,
SEXP pos
SEXP pos,
SEXP style
);

SEXP map_ref_locs_to_query_locs(
SEXP start,
SEXP end,
SEXP cigar,
SEXP pos
SEXP pos,
SEXP style
);

SEXP ref_locs_to_query_locs(
SEXP ref_locs,
SEXP cigar,
SEXP pos,
SEXP narrow_left
SEXP narrow_left,
SEXP style
);

SEXP query_locs_to_ref_locs(
SEXP query_locs,
SEXP cigar,
SEXP pos,
SEXP narrow_left
SEXP narrow_left,
SEXP style
);


Expand Down
8 changes: 4 additions & 4 deletions src/R_init_GenomicAlignments.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ static const R_CallMethodDef callMethods[] = {
CALLMETHOD_DEF(cigar_qnarrow, 3),

/* mapping_methods.c */
CALLMETHOD_DEF(map_query_locs_to_ref_locs, 4),
CALLMETHOD_DEF(map_ref_locs_to_query_locs, 4),
CALLMETHOD_DEF(ref_locs_to_query_locs, 4),
CALLMETHOD_DEF(query_locs_to_ref_locs, 4),
CALLMETHOD_DEF(map_query_locs_to_ref_locs, 5),
CALLMETHOD_DEF(map_ref_locs_to_query_locs, 5),
CALLMETHOD_DEF(ref_locs_to_query_locs, 5),
CALLMETHOD_DEF(query_locs_to_ref_locs, 5),

/* encodeOverlaps_methods.c */
CALLMETHOD_DEF(encode_overlaps1, 10),
Expand Down
Loading