Skip to content

Commit

Permalink
Merge pull request #17 from mattssca/test_branch
Browse files Browse the repository at this point in the history
Test New function: cyto_ranger + improvements and new unit tests
  • Loading branch information
mattssca authored Feb 6, 2024
2 parents e0dc557 + 124b01b commit d742f72
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(bin_splitter)
export(cyto_ranger)
export(gene_ranger)
export(get_gene_info)
export(purify_chr)
Expand Down
101 changes: 101 additions & 0 deletions R/cyto_ranger.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#' @title Cyto Ranger.
#'
#' @description This function takes a region or regions and returns the cytoband
#' information for the region(s).
#'
#' @details Query a region and relevant cytoband information for the specified
#' region(s). This function accepts a variety of incoming regions.
#' Either, regions can be provided as a data frame with `these_regions`.
#' If so, the following columns must exist; chrom, start, end.
#' This parameter (`these_regions`) also accept a region in "region" format,
#' (i.e chr:start-end). This can be a region or a vector of characters with
#' multiple regions. The user can also individually specify region(s) with;
#' `qchrom` (string), `qstart` (string, or integer), and `qend` (string or integer).
#' These parameters can also accept a vector of characters for multiple regions.
#' The function also handles chromosome prefixes in the returned object,
#' based on the selected `projection`.
#'
#' @param these_regions The region(s) to be queried. Can be a data frame with
#' regions with the following columns; chrom, start, end.
#' Or in a string in the following format chr:start-end.
#' @param qchrom Query chromosome (prefixed or un-prefixed),
#' Required if `these_regions` is not provided.
#' @param qstart Query start position. Required if `these_regions` is not provided.
#' @param qend Query end position. Required if `these_regions` is not provided.
#' @param projection The desired projection you want back coordinates for.
#' Available projections are hg38 and grch37. Default is hg38.
#'
#' @return A data frame with cytoband information for the specified region(s).
#'
#' @rawNamespace import(data.table, except = c("last", "first", "between", "transpose"))
#' @import dplyr
#'
#' @export
#'
#' @examples
#' #' #Example 1 - Give the function one region as a string
#' my_region = cyto_ranger(these_regions = "chr8:127735434-127742951")
#'
#' #Example 2 - Give the function multiple regions as a string
#' my_regions = cyto_ranger(these_regions = c("chr8:128747680-128753674",
#' "chr18:60790579-60987361"),
#' projection = "grch37")
#'
#' #Example 3 - Individually specify the chromosome, start and end coordinates
#' this_region = cyto_ranger(qchrom = "chr8",
#' qstart = 127735434,
#' qend = 127742951)
#'
#' #Example 4 - Individually specify multiple regions with the query parameters
#' these_regions = cyto_ranger(qchrom = c("chr8", "chr18"),
#' qstart = c(128747680, 60790579),
#' qend = c(128753674, 60987361),
#' projection = "grch37")
#'
cyto_ranger <- function(these_regions = NULL,
qchrom = NULL,
qstart = NULL,
qend = NULL,
projection = "hg38") {

#call helper to wrangle regions
region_table = BioMaesteR::purify_regions(these_regions = these_regions,
qchrom = qchrom,
qstart = qstart,
qend = qend,
projection = projection)

#deal with projections
if(projection == "hg38"){
cytobands = cytobands_hg38
}else if(projection == "grch37"){
cytobands = cytobands_grch37
}else{
stop(paste0("This function supports the following projections; hg38 and
grch37. The provided projection is: ", projection))
}

#convert to data table object
cytobands = as.data.table(cytobands)

#set keys
data.table::setkey(region_table, chrom, start, end)
data.table::setkey(cytobands, chrom, start, end)

#intersect regions
intersect = data.table::foverlaps(region_table, cytobands, nomatch = 0)

#rename columns
colnames(intersect)[6] = "region_start"
colnames(intersect)[7] = "region_end"
colnames(intersect)[2] = "cytoband_start"
colnames(intersect)[3] = "cytoband_end"

#transform object for retur
inter_df = as.data.frame(intersect) %>%
dplyr::arrange(chrom, region_start) %>%
dplyr::select(chrom, region_start, region_end, cytoBand, cytoband_start, cytoband_end) %>%
distinct(.keep_all = TRUE)

return(inter_df)
}
2 changes: 1 addition & 1 deletion R/region_ranger.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' The function also handles chromosome prefixes in the returned object,
#' based on the selected `projection`.
#'
#' @param these_regions these_regions The region(s) to be queried. Can be a data frame with
#' @param these_regions The region(s) to be queried. Can be a data frame with
#' regions with the following columns; chrom, start, end.
#' Or in a string in the following format chr:start-end.
#' @param qchrom Query chromosome (prefixed or un-prefixed),
Expand Down
70 changes: 70 additions & 0 deletions man/cyto_ranger.Rd

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

2 changes: 1 addition & 1 deletion man/region_ranger.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test_cyto_ranger.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library(testthat)

test_that("cyto_ranger throws error with invalid projection", {
expect_error(cyto_ranger(these_regions = "chr8:127735434-127742951", projection = "invalid"), "This function supports the following projections")
})

test_that("cyto_ranger returns correct columns", {
result = cyto_ranger(these_regions = "chr8:127735434-127742951")
expect_equal(colnames(result), c("chrom", "region_start", "region_end", "cytoBand", "cytoband_start", "cytoband_end"))
})

test_that("cyto_ranger returns correct cytoband information", {
result = cyto_ranger(these_regions = "chr8:127735434-127742951")
expect_equal(result$cytoBand, "q24.21")
})

0 comments on commit d742f72

Please sign in to comment.