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 unit tests and minor improvements #13

Merged
merged 8 commits into from
Feb 5, 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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.Rproj.user
.DS_Store
my_bed.bed
4 changes: 2 additions & 2 deletions R/bin_splitter.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' specified size.
#'
#' @details This function internally calls the purify_regions function to
#' properly format the incoming regions.Thus, thuis function can accept either a
#' properly format the incoming regions.Thus, this function can accept either a
#' data frame of regions or individual region coordinates.
#'
#' @param these_regions The region(s) to be queried. Can be a data frame with
Expand Down Expand Up @@ -39,7 +39,7 @@ bin_splitter = function(these_regions = NULL,
qend = NULL,
bin_size = 1000){

#call helperto format regions
#call helper to format regions
my_regions = purify_regions(these_regions = these_regions,
qchrom = qchrom,
qstart = qstart,
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BioMaesteR
A package with convenience functions for generic genomic analysis within R.
Standardized, generic functions as well as bundled data objects that can be
called in various settings in the scope of biological analysis in R.
This package is specialized to provide bioinformatics infrastructure that can be
used in a variety of analysis.
# <img src="biomaesteR.png" align="right" alt="" width="120" />
# biomaesteR
This is biomaesteR, an R package with convenience functions commonly requested in various types of genomic analysis within R. This package is specialized to provide bioinformatics infrastructure that can be
used in a variety of genomic analysis.

This repo is also an ongoing project intended for demonstrating and following best-practices in development of R packages. This includes adequate package documentation (functions, bundled data objects, etc.), reproducible unit tests and vignettes. Upon Pull Requests, this repo also executes a GitAction workflow where the package is installed in various environments and thorough testing of the complete code base.
Binary file added biomaesteR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/bin_splitter.Rd

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

29 changes: 29 additions & 0 deletions tests/testthat/test-bin_splitter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#load packages
library(testthat)

test_that("bin_splitter returns correct number of bins", {
result <- bin_splitter(qchrom = c("chr1"), qstart = c(1), qend = c(1001), bin_size = 100)
expect_equal(nrow(result), 10)
})

test_that("bin_splitter returns correct bin size", {
result <- bin_splitter(qchrom = c("chr1"), qstart = c(1), qend = c(1000), bin_size = 100)
expect_equal(result$bin_end[1] - result$bin_start[1], 100)
})

test_that("bin_splitter handles multiple regions", {
result <- bin_splitter(qchrom = c("chr1", "chr2"), qstart = c(1, 1001), qend = c(1001, 2001), bin_size = 100)
expect_equal(nrow(result), 20)
})

test_that("bin_splitter handles regions data frame", {
my_regions <- data.frame(chrom = c("chr1", "chr2"), start = c(1, 1000), end = c(1001, 2001))
result <- bin_splitter(these_regions = my_regions, bin_size = 100)
expect_equal(nrow(result), 20)
})

test_that("bin_splitter returns correct chromosome names", {
result <- bin_splitter(qchrom = c("chr1", "chr2"), qstart = c(1, 1001), qend = c(1000, 2000), bin_size = 100)
expect_equal(unique(result$bin_chr), c("chr1", "chr2"))
})

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

test_that("gene_ranger throws error with no genes", {
expect_error(gene_ranger())
})

test_that("gene_ranger throws error with invalid projection", {
expect_error(gene_ranger(these_genes = "MYC", projection = "invalid"))
})

test_that("gene_ranger throws error with invalid return format", {
expect_error(gene_ranger(these_genes = "MYC", return_as = "invalid"))
})

test_that("gene_ranger throws error when write_to_bed is TRUE but bed_path, track_name, or track_description is not provided", {
expect_error(gene_ranger(these_genes = "MYC", write_to_bed = TRUE), "Provide a path for output bed file")
expect_error(gene_ranger(these_genes = "MYC", write_to_bed = TRUE, bed_path = "my_bed"), "Provide a track name for output bed file")
expect_error(gene_ranger(these_genes = "MYC", write_to_bed = TRUE, bed_path = "my_bed", track_name = "MYC"), "Provide a track description for output bed file")
})

test_that("gene_ranger runs successfully with valid input", {
expect_silent(gene_ranger(these_genes = "MYC"))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-get_gene_info.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#load pacakges
#load packages
library(testthat)


Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-purify_chr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
library(testthat)

test_that("purify_chr throws error with no projection", {
expect_error(purify_chr(), "You must provide a valid projection")
})

test_that("purify_chr throws error with no incoming_table", {
expect_error(purify_chr(projection = "hg38"), "You must provide a data table with `incoming_table`")
})

test_that("purify_chr throws error with invalid projection", {
expect_error(purify_chr(projection = "invalid", incoming_table = data.frame(chrom = c("1", "2", "3"))), "This function supports the following projections")
})

test_that("purify_chr adds 'chr' prefix for hg38 projection", {
df = data.frame(chrom = c("1", "2", "3"))
result = purify_chr(projection = "hg38", incoming_table = df)
expect_equal(result$chrom, c("chr1", "chr2", "chr3"))
})

test_that("purify_chr removes 'chr' prefix for grch37 projection", {
df = data.frame(chrom = c("chr1", "chr2", "chr3"))
result = purify_chr(projection = "grch37", incoming_table = df)
expect_equal(result$chrom, c("1", "2", "3"))
})
47 changes: 47 additions & 0 deletions tests/testthat/test-purify_regions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
library(testthat)

test_that("purify_regions handles single region string", {
result <- purify_regions(these_regions = "chr1:100-500")
expect_equal(nrow(result), 1)
expect_equal(result$chrom, "chr1")
expect_equal(result$start, 100)
expect_equal(result$end, 500)
})

test_that("purify_regions handles multiple region strings", {
result <- purify_regions(these_regions = c("chr1:100-500", "chr2:100-500"))
expect_equal(nrow(result), 2)
expect_equal(result$chrom, c("chr1", "chr2"))
expect_equal(result$start, c(100, 100))
expect_equal(result$end, c(500, 500))
})

test_that("purify_regions handles individual region parameters", {
result <- purify_regions(qchrom = "chr1", qstart = 100, qend = 500)
expect_equal(nrow(result), 1)
expect_equal(result$chrom, "chr1")
expect_equal(result$start, 100)
expect_equal(result$end, 500)
})

test_that("purify_regions handles multiple individual region parameters", {
result <- purify_regions(qchrom = c("chr1", "chr2"), qstart = c(100, 200), qend = c(500, 600))
expect_equal(nrow(result), 2)
expect_equal(result$chrom, c("chr1", "chr2"))
expect_equal(result$start, c(100, 200))
expect_equal(result$end, c(500, 600))
})

test_that("purify_regions handles data frame input", {
my_regions <- data.frame(chrom = c("chr1", "chr2"), start = c(100, 200), end = c(500, 600))
result <- purify_regions(these_regions = my_regions)
expect_equal(nrow(result), 2)
expect_equal(result$chrom, c("chr1", "chr2"))
expect_equal(result$start, c(100, 200))
expect_equal(result$end, c(500, 600))
})

test_that("purify_regions throws error with incorrect input", {
expect_error(purify_regions(qchrom = "chr1", qstart = 100))
expect_error(purify_regions(these_regions = data.frame(chrom = c("chr1", "chr2"), start = c(100, 200), end = c(50, 100))))
})
23 changes: 23 additions & 0 deletions tests/testthat/test-region_ranger.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
library(testthat)

test_that("region_ranger throws error with no regions", {
expect_error(region_ranger())
})

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

test_that("region_ranger returns correct number of columns when raw is FALSE", {
result = region_ranger(these_regions = "chr8:127735434-127742951")
expect_equal(ncol(result), 11)
})

test_that("region_ranger returns all columns when raw is TRUE", {
result = region_ranger(these_regions = "chr8:127735434-127742951", raw = TRUE)
expect_true(ncol(result) > 11)
})

test_that("region_ranger returns warning when no genes found", {
expect_warning(region_ranger(these_regions = "chr1:1-10"))
})
31 changes: 31 additions & 0 deletions tests/testthat/test-sanity_check_regions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
library(testthat)

test_that("sanity_check_regions throws error with no regions", {
expect_error(sanity_check_regions(), "No regions provided")
})

test_that("sanity_check_regions throws error with invalid projection", {
my_regions <- data.frame(chrom = c("chr1"), start = c(100), end = c(200))
expect_error(sanity_check_regions(incoming_regions = my_regions, projection = "invalid"), "Invalid projection specified")
})

test_that("sanity_check_regions throws error when start is greater than end", {
my_regions <- data.frame(chrom = c("chr1"), start = c(200), end = c(100))
expect_error(sanity_check_regions(incoming_regions = my_regions, projection = "hg38"), "start is greater than or equal to end")
})

test_that("sanity_check_regions throws error when start or end is outside chromosomal range", {
my_regions <- data.frame(chrom = c("chr1"), start = c(100), end = c(300000000))
expect_error(sanity_check_regions(incoming_regions = my_regions, projection = "hg38"), "Specified start or end coordinates fall outside the actual chromosomal range")
})

test_that("sanity_check_regions runs successfully with valid input", {
my_regions <- data.frame(chrom = c("chr1"), start = c(100), end = c(200))
expect_silent(sanity_check_regions(incoming_regions = my_regions, projection = "hg38"))
})

test_that("sanity_check_regions runs successfully with valid input", {
my_regions <- data.frame(chrom = c("1"), start = c(100), end = c(200))
expect_silent(sanity_check_regions(incoming_regions = my_regions, projection = "grch37"))
})

Loading