HiSpaR - R Package for Hi-C Spatial Analysis

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.

Features

Installation

Prerequisites

Before installing HiSpaR, ensure you have the following:

System Requirements

Required Libraries

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-devel

OpenMP:

# macOS (Homebrew)
brew install libomp

# Usually included with GCC on Linux
# For Ubuntu/Debian: sudo apt-get install libomp-dev

R Package Dependencies

install.packages(c("Rcpp", "RcppArmadillo", "devtools"))

Installing from Bioconductor

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("HiSpaR")

Platform-Specific Notes

macOS

If you encounter OpenMP errors, create or edit ~/.R/Makevars:

PKG_CXXFLAGS = -Xclang -fopenmp
PKG_LIBS = -lomp

Linux

Usually works out of the box if OpenMP is installed with GCC.

Windows

  1. Install Rtools
  2. Install Armadillo (more complex on Windows, may require pre-compiled libraries)

Main Functions

hispa_analyze()

Run complete HiSpa MCMC analysis on a Hi-C contact matrix following the exact workflow from the C++ implementation.

Key Parameters:

Output Files: All results are automatically saved to output_dir:

Workflow:

  1. Load contact matrix from input file / matrix
  2. Assign loci to clusters (k-means)
  3. Build cluster relationships
  4. Initialize structure (random or cluster-based)
  5. Assemble global structure
  6. Run main MCMC algorithm
  7. Save all results to output directory

Example: Basic Analysis

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')

Documentation

After installation, access help documentation:

?hispa_analyze

Performance Tips

  1. Start with fewer iterations for testing (e.g., 1000-2000)
  2. Use clustering for large matrices (>200 loci)
  3. Parallel processing: OpenMP automatically uses multiple cores
  4. Save samples sparingly: Only when needed for diagnostics

Troubleshooting

"Armadillo not found" error

Ensure Armadillo is installed and can be found by pkg-config:

pkg-config --modversion armadillo

OpenMP linking errors on macOS

Add to ~/.R/Makevars:

PKG_CXXFLAGS = -Xclang -fopenmp
PKG_LIBS = -lomp
LDFLAGS = -L/usr/local/opt/libomp/lib
CPPFLAGS = -I/usr/local/opt/libomp/include

Compilation errors

  1. Ensure C++17 support: clang++ --version or g++ --version
  2. Update R packages: update.packages()
  3. Reinstall Rcpp: install.packages("Rcpp", type = "source")

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Contact

Acknowledgments

HiSpaR wraps the HiSpa C++ library developed by Yingcheng Luo.