Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New helper and hot fixes #9

Merged
merged 5 commits into from
Jan 10, 2024
Merged
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
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.