1#ifndef SCRAN_IGRAPH_UTILS_HPP
2#define SCRAN_IGRAPH_UTILS_HPP
4#include "../utils/macros.hpp"
26 RNGScope(
int seed) : previous(igraph_rng_default()) {
27 if (igraph_rng_init(&rng, &igraph_rngtype_mt19937)) {
28 throw std::runtime_error(
"failed to initialize an instance of igraph's RNG");
31 if (igraph_rng_seed(&rng, seed)) {
32 igraph_rng_destroy(&rng);
33 throw std::runtime_error(
"failed to set the seed on igraph's RNG");
36 igraph_rng_set_default(&rng);
41 RNGScope(
const RNGScope& other) =
delete;
42 RNGScope& operator=(
const RNGScope& other) =
delete;
43 RNGScope(RNGScope&& other) =
delete;
44 RNGScope& operator=(RNGScope&& other) =
delete;
48 igraph_rng_set_default(previous);
49 igraph_rng_destroy(&rng);
53 igraph_rng_t* previous;
61template<
class Overlord>
64 static void try_copy(
typename Overlord::Vector& dest,
const typename Overlord::Vector& source,
bool source_active) {
66 if (Overlord::copy(dest, source)) {
67 throw std::runtime_error(
"failed to copy igraph vector of size " + std::to_string(Overlord::size(source)));
72 static void try_destroy(
typename Overlord::Vector& vector,
bool active) {
74 Overlord::destroy(vector);
79 Vector_(
size_t size = 0) {
80 if (Overlord::initialize(vector, size)) {
81 throw std::runtime_error(
"failed to initialize igraph vector of size " + std::to_string(size));
87 Vector_(
const Vector_& other) =
delete;
88 Vector_& operator=(
const Vector_& other) =
delete;
89 Vector_(Vector_&& other) =
delete;
90 Vector_& operator=(Vector_&& other) =
delete;
93 try_destroy(vector, active);
96 typename Overlord::Vector vector;
100struct IntegerVectorOverlord {
101 typedef igraph_vector_int_t Vector;
103 static igraph_error_t copy(igraph_vector_int_t& dest,
const igraph_vector_int_t& source) {
104 return igraph_vector_int_init_copy(&dest, &source);
107 static void destroy(igraph_vector_int_t& x) {
108 return igraph_vector_int_destroy(&x);
111 static igraph_integer_t size(igraph_vector_int_t& x) {
112 return igraph_vector_int_size(&x);
115 static igraph_error_t initialize(igraph_vector_int_t& x,
size_t size) {
116 return igraph_vector_int_init(&x, size);
120using IntegerVector = Vector_<IntegerVectorOverlord>;
122struct RealVectorOverlord {
123 typedef igraph_vector_t Vector;
125 static igraph_error_t copy(igraph_vector_t& dest,
const igraph_vector_t& source) {
126 return igraph_vector_init_copy(&dest, &source);
129 static void destroy(igraph_vector_t& x) {
130 return igraph_vector_destroy(&x);
133 static igraph_integer_t size(igraph_vector_t& x) {
134 return igraph_vector_size(&x);
137 static igraph_error_t initialize(igraph_vector_t& x,
size_t size) {
138 return igraph_vector_init(&x, size);
142using RealVector = Vector_<RealVectorOverlord>;
147struct IntegerMatrix {
148 IntegerMatrix(
size_t nrows = 0,
size_t ncols = 0) {
149 if (igraph_matrix_int_init(&matrix, nrows, ncols)) {
150 throw std::runtime_error(
"failed to initialize igraph " + std::to_string(nrows) +
"x" + std::to_string(ncols) +
" matrix");
156 IntegerMatrix(
const IntegerMatrix& other) =
delete;
157 IntegerMatrix& operator=(
const IntegerMatrix& other) =
delete;
158 IntegerMatrix(IntegerMatrix&& other) =
delete;
159 IntegerMatrix& operator=(IntegerMatrix&& other) =
delete;
163 igraph_matrix_int_destroy(&matrix);
167 igraph_matrix_int_t matrix;
182 static void try_copy(igraph_t& dest,
const igraph_t& source,
bool source_active) {
184 if (igraph_copy(&dest, &source)) {
185 throw std::runtime_error(
"failed to copy igraph's graph");
190 static void try_destroy(igraph_t& graph,
bool active) {
192 igraph_destroy(&graph);
200 Graph() : active(
false) {}
202 Graph(
const IntegerVector& edges,
size_t nvertices,
bool directed) :
Graph(&(edges.vector), nvertices, directed) {}
204 Graph(
const igraph_vector_int_t* edges,
size_t nvertices,
bool directed) {
205 if (igraph_create(&graph, edges, nvertices, directed)) {
206 throw std::runtime_error(
"failed to initialize igraph's graph object");
210 Graph(
const Graph& other) : active(other.active) {
211 try_copy(graph, other.graph, other.active);
215 if (
this != &other) {
216 try_destroy(graph, active);
217 try_copy(graph, other.graph, other.active);
218 active = other.active;
224 Graph(
Graph&& other) : graph(std::move(other.graph)), active(other.active) {
225 other.active =
false;
229 if (
this != &other) {
230 try_destroy(graph, active);
231 graph = std::move(other.graph);
232 active = other.active;
233 other.active =
false;
239 try_destroy(graph, active);
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
Wrapper around the igraph_t class from igraph.
Definition igraph_utils.hpp:180
const igraph_t * get_graph() const
Definition igraph_utils.hpp:256