Skip to content

Commit

Permalink
Merge pull request #9 from mattssca/test_branch
Browse files Browse the repository at this point in the history
New helper and hot fixes
  • Loading branch information
mattssca authored Jan 10, 2024
2 parents e06265e + 0dcc7ca commit 7e6ce43
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 16 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(gene_ranger)
export(get_gene_info)
export(purify_chr)
export(purify_regions)
export(region_ranger)
import(data.table, except = c("last", "first", "between", "transpose"))
Expand Down
55 changes: 55 additions & 0 deletions R/purify_chr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' @title Purify Chromosomes.
#'
#' @description Helper function for dealing with chromosome prefixes based on
#' the selected projection.
#'
#' @details This function accepts a data frame with a column to check for
#' prefixes. The function will add or remove prefixes based on the selected
#' projection. This function expects a column named "chrom" to be in the
#' incoming data frame.
#'
#' @param projection Required parameter, needed to determine if chromosomes
#' should be prefixed or not.
#' @param incoming_table Required parameter, a data frame to check for prefixes.
#'
#' @return A data frame with the same columns as the incoming data frame, but
#' with the prefixes added or removed based on the selected projection.
#'
#' @import dplyr stringr
#'
#' @export
#'
#' @examples
#' #Example 1 - Add prefixes to a data frame
#' my_data = data.frame(chrom = c("1", "2", "3"))
#' my_data = purify_chr(projection = "hg38", incoming_table = my_data)
#'
purify_chr <- function(projection = NULL,
incoming_table = NULL) {

#checks
if(is.null(projection)){
stop("You must provide a valid projection.
Available projections are hg38 and grch37.")
}

if(is.null(incoming_table)){
stop("You must provide a data table with `incoming_table`.")
}

#deal with prefixes
if(projection == "hg38"){
if(all(!str_detect(incoming_table$chrom, "chr"))){
incoming_table = dplyr::mutate(incoming_table, chrom = paste0("chr", chrom))
}
}else if(projection == "grch37"){
if(all(str_detect(incoming_table$chrom, "chr"))){
incoming_table = dplyr::mutate(incoming_table, chrom = gsub("chr", "", chrom))
}
}else{
stop(paste0("This function supports the following projections; hg38 and
grch37. The provided projection is: ", projection))
}

return(incoming_table)
}
19 changes: 4 additions & 15 deletions R/purify_regions.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @title Purify Regions.
#'
#' @description Helper function for cleaning and standaradize regions.
#' @description Helper function for cleaning and standardize regions.
#'
#' @details This function accepts a variety of incoming regions.
#' Either, regions can be provided as a data frame with `these_regions`.
Expand Down Expand Up @@ -103,20 +103,9 @@ purify_regions <- function(these_regions = NULL,
`qchrom`, `qstart`, and `qend`")
}

#TODO: Turn this into a helper function:
#deal with chr prefixes based on selected projection
if(projection == "hg38"){
if(all(!str_detect(region_table$chrom, "chr"))){
region_table = dplyr::mutate(region_table, chrom = paste0("chr", chrom))
}
}else if(projection == "grch37"){
if(all(str_detect(region_table$chrom, "chr"))){
region_table = dplyr::mutate(region_table, chrom = gsub("chr", "", chrom))
}
}else{
stop(paste0("This function supports the following projections; hg38 and
grch37. The provided projection is: ", projection))
}
#run helper function to deal with prefixes
region_table = purify_chr(projection = projection,
incoming_table = region_table)

#enforce data types
region_table$chrom = as.character(region_table$chrom)
Expand Down
34 changes: 34 additions & 0 deletions man/purify_chr.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/purify_regions.Rd

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

Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ test_that("Expected to fail", {
expect_error(get_gene_info(these_genes = "MYC",
projection = "hg19"))
})


test_that("Check the type of the return", {
expect_true(is.data.frame(get_gene_info(these_genes = c("BCL2", "MYC"))))
})
Empty file.

0 comments on commit 7e6ce43

Please sign in to comment.