Skip to content
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
50 changes: 50 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: "release"}
- {os: macOS-latest, r: "release"}
- {os: ubuntu-latest, r: "release"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v2

- name: Install dependencies
run: |
install.packages(c("remotes", "rcmdcheck", "covr", "testthat"))
remotes::install_deps(dependencies = TRUE)
shell: Rscript {0}

- name: Check
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Test coverage
run: covr::codecov()
shell: Rscript {0}

28 changes: 28 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v2

- name: Install dependencies
run: |
install.packages(c("remotes", "covr"))
remotes::install_deps(dependencies = TRUE)
shell: Rscript {0}

- name: Test coverage
run: covr::codecov()
shell: Rscript {0}

2 changes: 1 addition & 1 deletion R/colocboost_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ get_integrated_weight <- function(avWeight, weight_fudge_factor = 1.5){
get_in_cos <- function(weights, coverage = 0.95){

temp <- order(weights, decreasing=T)
csets <- temp[1:min(which(cumsum(weights[temp]) > coverage))] # 95%
csets <- temp[1:min(which(cumsum(weights[temp]) >= coverage))] # 95%
return(list(csets))

}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# ColocBoost for multi-context colocalization in molecular QTL and GWAS studies
[![Codecov test coverage](https://codecov.io/gh/StatFunGen/colocboost/branch/master/graph/badge.svg)](https://codecov.io/gh/StatFunGen/colocboost?branch=master)

This R package implements ColocBoost --- motivated and designed for colocalization analysis ([first formulated here](https://journals.plos.org/plosgenetics/article?id=10.1371/journal.pgen.1004383)) of multiple genetic association studies --- as a multi-task learning approach to variable selection regression with highly correlated predictors and sparse effects, based on frequentist statistical inference. It provides statistical evidence to identify which subsets of predictors have non-zero effects on which subsets of response variables.

Expand Down
106 changes: 106 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# colocboost unit-testing

## Overview

This repository contains a comprehensive testing framework for the [colocboost](https://github.com/StatFunGen/colocboost) R package. The framework is designed to ensure the reliability and correctness of the package's functionality through automated testing.

## Quick Start

1. Navigate to test folder:
```bash
cd tests
```

2. **First time use**: run the setup script to install required packages and configure the testing environment:
```r
# install.packages(c("devtools", "testthat", "covr", "roxygen2"))
source("setup_testthat.R")
```

3. Run all tests:
```r
devtools::load_all()
devtools::test()
```
or,
```bash
Rscript run_tests.R
```
To test one file:
```r
devtools::test_active_file("testthat/test_colocboost.R")
```

## Files and Structure

- `setup_testthat.R`: Script to set up the testthat infrastructure
- `run_tests.R`: Script to run all tests and generate test coverage reports
- `testthat/`: Directory containing test files
- `test_package.R`: Tests for basic package functionality
- `test_colocboost.R`: Tests for the main colocalization functions
- `test_utils.R`: Tests for utility functions
- `test_model.R`: Tests for model fitting and prediction functions
- `.github/workflows/`: GitHub Actions workflow configurations

## How To Use

### Running Tests Locally

To run the tests locally, you can use:

```r
devtools::test()
```

Or run individual test files:

```r
devtools::test_file("testthat/test_colocboost.R")
```

### Adding New Tests

When adding new functionality to the colocboost package, corresponding tests should be added to maintain test coverage. Follow these steps:

1. Identify which test file should contain the new tests, or create a new test file if necessary
2. Write test functions using the testthat package's expectations
3. Run the tests to ensure they pass

Example test:

```r
test_that("new_function produces expected output", {
# Arrange
input_data <- prepare_test_data()

# Act
result <- new_function(input_data)

# Assert
expect_equal(result$some_value, expected_value)
expect_true(is.data.frame(result$data))
})
```

### Test Coverage

The `covr` package is used to measure test coverage, which indicates what percentage of your code is being tested. Aim for at least 80% coverage for a reliable package.

To generate a test coverage report:
```r
library(covr)
coverage <- package_coverage()
report(coverage)
```

## GitHub Actions Workflow

This testing framework includes GitHub Actions workflows that automatically run the tests on every push and pull request. The workflows test the package on multiple operating systems (Windows, macOS, and Linux) to ensure cross-platform compatibility.

The workflow is defined in `.github/workflows/R-CMD-check.yaml` and automatically runs:
- R CMD check
- Test coverage reporting

Test results and coverage statistics are available on the GitHub Actions page after each push or pull request.

For more information, check the [testthat documentation](https://testthat.r-lib.org/).
39 changes: 39 additions & 0 deletions tests/run_tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env Rscript

# Script to install the package and run tests

# Check for required packages
required_packages <- c("devtools", "testthat", "covr")
for (pkg in required_packages) {
if (!requireNamespace(pkg, quietly = TRUE)) {
stop(sprintf("Cannot find required package: %s\n", pkg))
}
}

# Path to working directory
args <- commandArgs(trailingOnly = TRUE)
if (length(args) > 0) {
work_dir <- args[1]
} else {
work_dir <- getwd()
}

setwd(work_dir)
cat(sprintf("Working directory: %s\n", work_dir))

# Install the package
# cat("Installing colocboost package...\n")
# devtools::install(".", dependencies = FALSE, quiet = TRUE)

# Run tests
cat("Running tests...\n")
devtools::load_all('../')
testthat::test_dir("testthat/")

# Calculate test coverage
cat("Calculating test coverage...\n")
coverage <- covr::package_coverage()
print(coverage)
covr::report(coverage)

cat("Tests completed.\n")
130 changes: 130 additions & 0 deletions tests/setup_testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
setwd("../")

# Install necessary packages
if (!requireNamespace("devtools", quietly = TRUE)) {
stop("devtools not found")
}
if (!requireNamespace("testthat", quietly = TRUE)) {
stop("testthat not found")
}
if (!requireNamespace("covr", quietly = TRUE)) {
stop("covr not found")
}
if (!requireNamespace("roxygen2", quietly = TRUE)) {
stop("roxygen2 not found")
}

# Set up testthat infrastructure
if (!dir.exists("tests/testthat")) {
devtools::use_testthat()
}

# Create GitHub Actions workflow for continuous integration
if (!dir.exists(".github/workflows")) {
dir.create(".github/workflows", recursive = TRUE, showWarnings = FALSE)
}

# Write GitHub Actions workflow file
workflow_file <- ".github/workflows/R-CMD-check.yaml"
writeLines(
'
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: "release"}
- {os: macOS-latest, r: "release"}
- {os: ubuntu-latest, r: "release"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v2

- name: Install dependencies
run: |
install.packages(c("remotes", "rcmdcheck", "covr", "testthat"))
remotes::install_deps(dependencies = TRUE)
shell: Rscript {0}

- name: Check
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Test coverage
run: covr::codecov()
shell: Rscript {0}
', workflow_file)

# Create a test coverage workflow
coverage_file <- ".github/workflows/test-coverage.yaml"
writeLines(
'
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v2

- name: Install dependencies
run: |
install.packages(c("remotes", "covr"))
remotes::install_deps(dependencies = TRUE)
shell: Rscript {0}

- name: Test coverage
run: covr::codecov()
shell: Rscript {0}
', coverage_file)

# Add code coverage badge to README.md
readme_file <- "README.md"
if (file.exists(readme_file)) {
readme_content <- readLines(readme_file)
if (!any(grepl("codecov", readme_content))) {
badge <- "[![Codecov test coverage](https://codecov.io/gh/StatFunGen/colocboost/branch/master/graph/badge.svg)](https://codecov.io/gh/StatFunGen/colocboost?branch=master)"
# Add badge after the first line if it's a title
if (length(readme_content) > 0) {
readme_content <- c(readme_content[1], badge, readme_content[-1])
} else {
readme_content <- c("# colocboost", badge)
}
writeLines(readme_content, readme_file)
}
}

# Set up basic testthat structure
message("testthat setup complete. Next, create test files for each R file in the package.")
2 changes: 1 addition & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
library(testthat)
library(colocboost)

test_check("colocboost")
test_check("colocboost")
Loading
Loading