HiSpaR is an R package that provides R bindings for the HiSpa C++ library, enabling hierarchical Bayesian inference of 3D chromatin structures from Hi-C contact matrices.
- 🧬 Hierarchical Bayesian modeling of 3D chromatin structure
- 🎯 MCMC sampling with adaptive proposals for efficient inference
- ⚡ High-performance C++ backend via Rcpp and RcppArmadillo
- 🔧 OpenMP parallelization for multi-core processing
Before installing HiSpaR, ensure you have the following:
- R (≥ 4.0.0)
- C++17 compatible compiler
- macOS: Clang 12.0+ (comes with Xcode Command Line Tools)
- Linux: GCC 7.0+ or Clang 5.0+
- Windows: Rtools 4.0+
Armadillo C++ Library (≥ 9.0):
# macOS (Homebrew)
brew install armadillo
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install libarmadillo-dev
# Fedora/RHEL
sudo dnf install armadillo-develOpenMP:
# macOS (Homebrew)
brew install libomp
# Usually included with GCC on Linux
# For Ubuntu/Debian: sudo apt-get install libomp-devinstall.packages(c("Rcpp", "RcppArmadillo", "devtools"))if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("HiSpaR")If you encounter OpenMP errors, create or edit ~/.R/Makevars:
PKG_CXXFLAGS = -Xclang -fopenmp
PKG_LIBS = -lompUsually works out of the box if OpenMP is installed with GCC.
- Install Rtools
- Install Armadillo (more complex on Windows, may require pre-compiled libraries)
Run complete HiSpa MCMC analysis on a Hi-C contact matrix following the exact workflow from the C++ implementation.
Key Parameters:
input_file: Path to contact matrix file (space/tab delimited text format)output_dir: Output directory path where all results will be savedmcmc_iterations: Number of MCMC iterations (default: 6000)num_clusters: Number of clusters (default: 0 for auto-detect as sqrt(n))mcmc_burn_in: Burn-in period (default: 0)mcmc_initial_sd: Initial proposal SD (default: 0.1)mcmc_sd_floor: Minimum SD (default: 0.0001)mcmc_sd_ceil: Maximum SD (default: 0.3)use_cluster_init: Use cluster-based initialization (default: FALSE)cluster_init_iterations: Iterations for cluster MCMC (default: 1000)cluster_initial_sd: SD for cluster initialization (default: 0.1)save_samples: Save MCMC trace (default: FALSE)sample_interval: Save interval (default: 50)verbose: Enable detailed output (default: TRUE)
Output Files: All results are automatically saved to output_dir:
final_positions.txt- Final 3D coordinates (n × 3 matrix)initial_positions.txt- Initial positions before MCMC
Workflow:
- Load contact matrix from input file / matrix
- Assign loci to clusters (k-means)
- Build cluster relationships
- Initialize structure (random or cluster-based)
- Assemble global structure
- Run main MCMC algorithm
- Save all results to output directory
library(HiSpaR)
# loads a hic matrix, from Su et al., 2020
data(su1_contact_mat)
# Run analysis - results saved automatically
results <- hispa_analyze(
su1_contact_mat,
output_dir = "su1",
mcmc_iterations = 4000,
mcmc_burn_in = 1000,
use_cluster_init = TRUE # strongly recommended
)
# Read results from output directory
final_pos <- results$positions
# 3D visualization (requires plotly)
library(plotly)
plot_ly(x = final_pos[,1], y = final_pos[,2], z = final_pos[,3], type = 'scatter3d', mode = 'markers+lines')After installation, access help documentation:
?hispa_analyze- Start with fewer iterations for testing (e.g., 1000-2000)
- Use clustering for large matrices (>200 loci)
- Parallel processing: OpenMP automatically uses multiple cores
- Save samples sparingly: Only when needed for diagnostics
Ensure Armadillo is installed and can be found by pkg-config:
pkg-config --modversion armadilloAdd to ~/.R/Makevars:
PKG_CXXFLAGS = -Xclang -fopenmp
PKG_LIBS = -lomp
LDFLAGS = -L/usr/local/opt/libomp/lib
CPPFLAGS = -I/usr/local/opt/libomp/include
- Ensure C++17 support:
clang++ --versionorg++ --version - Update R packages:
update.packages() - Reinstall Rcpp:
install.packages("Rcpp", type = "source")
MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
HiSpaR wraps the HiSpa C++ library developed by Yingcheng Luo.