forked from tidyomics/plyinteractions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add_pairdist and pair_granges functions
- Loading branch information
Showing
8 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#' Appends distance between interaction anchors | ||
#' | ||
#' Appends distance between interaction anchors, using | ||
#' `InteractionSet::pairdist` | ||
#' | ||
#' @param x The query GInteractions | ||
#' @param type A character string specifying the type of distance to compute. Can take values of "mid", "gap", "span", "diag" or "intra". | ||
#' @param colname name of column to hold pair distance values | ||
#' | ||
#' @return The GInteractions with an additional column containing the | ||
#' distance between each pair of anchors. | ||
#' | ||
#' @rdname add-pairdist | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' gi <- read.table(text = " | ||
#' chr1 100 200 chr1 5000 5100 bedpe_example1 30 + - | ||
#' chr1 1000 5000 chr2 3000 3800 bedpe_example2 100 + -", | ||
#' col.names = c( | ||
#' "seqnames1", "start1", "end1", | ||
#' "seqnames2", "start2", "end2", "name", "score", "strand1", "strand2") | ||
#' ) |> as_ginteractions() | ||
#' | ||
#' add_pairdist(gi) | ||
#' @export | ||
|
||
add_pairdist <- function(x, type = 'mid', colname = 'pairdist') { | ||
|
||
if (colname %in% names(GenomicRanges::mcols(x))){ | ||
stop(paste0(colname, " already exists in destination metadata")) | ||
} | ||
|
||
if (is.null(GenomicRanges::mcols(x))){ | ||
# handle IRanges NULL adding X column of NA's | ||
meta <- S4Vectors::DataFrame("distance" = NA_integer_) | ||
names(meta) <- colname | ||
GenomicRanges::mcols(x) <- meta | ||
} else { | ||
GenomicRanges::mcols(x)[[colname]] <- NA_integer_ | ||
} | ||
|
||
GenomicRanges::mcols(x)[[colname]] <- InteractionSet::pairdist(x, type) | ||
|
||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#' Pairwise combination of a GRanges object | ||
#' | ||
#' Create a GInteractions object from a GRanges object, | ||
#' containing all possible entry pairs | ||
#' | ||
#' @param x A GRanges object | ||
#' | ||
#' @return A GInteractions object | ||
#' | ||
#' @rdname pair-granges | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' gr <- read.table(text = " | ||
#' chr1 100 200 | ||
#' chr1 5000 5100 | ||
#' chr1 1000 5000 | ||
#' chr2 3000 3800", | ||
#' col.names = c( | ||
#' "seqnames", "start", "end" | ||
#' )) |> plyranges::as_granges() | ||
#' | ||
#' pair_granges(gr) | ||
#' @export | ||
|
||
pair_granges <- function(x) { | ||
|
||
combs <- combn(length(x), 2) | ||
InteractionSet::GInteractions(combs[1,], combs[2,], gr) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
test_that("enrich functions work", { | ||
|
||
gi <- read.table(text = " | ||
chr1 100 200 chr1 5000 5100 bedpe_example1 30 + - | ||
chr1 1000 5000 chr2 3000 3800 bedpe_example2 100 + -", | ||
col.names = c( | ||
"seqnames1", "start1", "end1", | ||
"seqnames2", "start2", "end2", "name", "score", "strand1", "strand2" | ||
) | ||
) |> as_ginteractions() | ||
|
||
add_pairdist(gi) |> expect_identical( | ||
new("GInteractions", anchor1 = 1:2, anchor2 = 3:4, regions = new("GRanges", | ||
seqnames = new("Rle", values = structure(1:2, levels = c("chr1", | ||
"chr2"), class = "factor"), lengths = c(3L, 1L), elementMetadata = NULL, | ||
metadata = list()), ranges = new("IRanges", start = c(100L, | ||
1000L, 5000L, 3000L), width = c(101L, 4001L, 101L, 801L), | ||
NAMES = NULL, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list()), strand = new("Rle", values = structure(1:2, levels = c("+", | ||
"-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL, | ||
metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1", | ||
"chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA, | ||
NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame", | ||
rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list(), listData = structure(list(), names = character(0))), | ||
elementType = "ANY", metadata = list()), NAMES = NULL, elementMetadata = new("DFrame", | ||
rownames = NULL, nrows = 2L, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list(), listData = list(name = c("bedpe_example1", | ||
"bedpe_example2"), score = c(30L, 100L), pairdist = c(4900L, | ||
NA))), metadata = list()) | ||
) | ||
|
||
add_pairdist(gi, colname = 's') |> expect_identical( | ||
new("GInteractions", anchor1 = 1:2, anchor2 = 3:4, regions = new("GRanges", | ||
seqnames = new("Rle", values = structure(1:2, levels = c("chr1", | ||
"chr2"), class = "factor"), lengths = c(3L, 1L), elementMetadata = NULL, | ||
metadata = list()), ranges = new("IRanges", start = c(100L, | ||
1000L, 5000L, 3000L), width = c(101L, 4001L, 101L, 801L), | ||
NAMES = NULL, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list()), strand = new("Rle", values = structure(1:2, levels = c("+", | ||
"-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL, | ||
metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1", | ||
"chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA, | ||
NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame", | ||
rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list(), listData = structure(list(), names = character(0))), | ||
elementType = "ANY", metadata = list()), NAMES = NULL, elementMetadata = new("DFrame", | ||
rownames = NULL, nrows = 2L, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list(), listData = list(name = c("bedpe_example1", | ||
"bedpe_example2"), score = c(30L, 100L), s = c(4900L, | ||
NA))), metadata = list()) | ||
) | ||
|
||
gr <- read.table(text = " | ||
chr1 100 200 | ||
chr1 5000 5100 | ||
chr1 1000 5000 | ||
chr2 3000 3800", | ||
col.names = c( | ||
"seqnames", "start", "end" | ||
)) |> plyranges::as_granges() | ||
|
||
pair_granges(gr) |> expect_identical( | ||
new("GInteractions", anchor1 = c(1L, 1L, 1L, 3L, 3L, 2L), anchor2 = c(3L, | ||
2L, 4L, 2L, 4L, 4L), regions = new("GRanges", seqnames = new("Rle", | ||
values = structure(1:2, levels = c("chr1", "chr2"), class = "factor"), | ||
lengths = c(3L, 1L), elementMetadata = NULL, metadata = list()), | ||
ranges = new("IRanges", start = c(100L, 1000L, 5000L, 3000L | ||
), width = c(101L, 4001L, 101L, 801L), NAMES = NULL, elementType = "ANY", | ||
elementMetadata = NULL, metadata = list()), strand = new("Rle", | ||
values = structure(3L, levels = c("+", "-", "*"), class = "factor"), | ||
lengths = 4L, elementMetadata = NULL, metadata = list()), | ||
seqinfo = new("Seqinfo", seqnames = c("chr1", "chr2"), seqlengths = c(NA_integer_, | ||
NA_integer_), is_circular = c(NA, NA), genome = c(NA_character_, | ||
NA_character_)), elementMetadata = new("DFrame", rownames = NULL, | ||
nrows = 4L, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list(), listData = structure(list(), names = character(0))), | ||
elementType = "ANY", metadata = list()), NAMES = NULL, elementMetadata = new("DFrame", | ||
rownames = NULL, nrows = 6L, elementType = "ANY", elementMetadata = NULL, | ||
metadata = list(), listData = structure(list(), names = character(0))), | ||
metadata = list() | ||
) | ||
) | ||
}) |