refactoredCPPNeuronMesher
neurongraph.h
Go to the documentation of this file.
1 
16 #ifndef NEURONGRAPH_H
17 #define NEURONGRAPH_H
18 
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 #include <map>
25 #include <set>
26 #include <queue>
27 #include <cmath>
28 #include <numeric>
29 #include <limits>
30 #include <iomanip>
31 #include <algorithm>
32 #include <filesystem>
33 
43 struct SWCNode {
45  int id;
46 
48  int pid;
49 
61  int type;
62 
64  double x;
65 
67  double y;
68 
70  double z;
71 
73  double radius;
74 };
75 
91 class NeuronGraph {
92  private:
94  std::map<int,SWCNode> nodes;
95 
97  std::map<int, std::vector<int>> edges;
98 
107  std::map<int, std::vector<int>> getNeighborMap(const std::map<int, SWCNode>& nodeSet) const;
108  public:
114  NeuronGraph() {nodes.clear(); edges.clear();};
115 
122  NeuronGraph(std::string filename) {this->readFromFileUGXorSWC(filename);};
123 
128  NeuronGraph(const std::map<int, SWCNode>& nodeSet);
129 
137  void addNode(const SWCNode &node);
138 
146  void setNodes(const std::map<int,SWCNode>& nodeSet);
147 
157  bool isTopologicallySorted(const std::map<int, SWCNode>& nodeSet) const;
158 
163  bool isTopologicallySorted() const {return this->isTopologicallySorted(this->nodes);};
164 
176  std::map<int, SWCNode> topologicalSort(const std::map<int, SWCNode>& nodeSet) const;
177 
182  std::map<int, SWCNode> topologicalSort() const {return this->topologicalSort(this->nodes);};
183 
191  bool hasSomaSegment(const std::map<int, SWCNode>& nodeSet) const;
192 
197  bool hasSomaSegment() const {return this->hasSomaSegment(this->nodes);};
198 
206  bool isSomaMissing(const std::map<int, SWCNode>& nodeSet) const;
207 
212  bool isSomaMissing() const {return this->isSomaMissing(this->nodes);};
213 
222  std::map<int, SWCNode> removeSomaSegment(const std::map<int, SWCNode>& inputNodes) const;
223 
228  std::map<int, SWCNode> removeSomaSegment() const {return this->removeSomaSegment(this->nodes);};
229 
238  std::map<int, SWCNode> setSoma(const std::map<int, SWCNode>& nodeSet) const;
239 
244  std::map<int, SWCNode> setSoma() const {return this->setSoma(this->nodes);};
245 
256  std::map<int, SWCNode> preprocess(const std::map<int, SWCNode> & nodeSet) const;
257 
265  void readFromFile(const std::string& filename);
266 
274  void writeToFile(const std::map<int,SWCNode> & nodeSet,
275  const std::string& filename);
276 
281  void writeToFile(const std::string& filename) {this->writeToFile(this->nodes, filename);};
282 
290  void readFromFileUGX(const std::string& filename);
291 
301  void readFromFileUGXorSWC(const std::string& filename);
302 
310  void writeToFileUGX(const std::map<int,SWCNode> & nodeSet,
311  const std::string& filename);
312 
317  void writeToFileUGX(const std::string& filename) {this->writeToFileUGX(this->nodes, filename);};
318 
326  void swc2ugx(const std::string& inputfile, const std::string& outputfile);
327 
335  void ugx2swc(const std::string& inputfile, const std::string& outputfile);
336 
341  int numberOfNodes() {return this->nodes.size();};
342 
347  int numberOfEdges() {return this->edges.size();};
348 
353  auto getNodes() { return this->nodes; };
354 
364  std::map<int, SWCNode> splitEdges(const std::map<int, SWCNode>& nodeSet) const;
365 
370  std::map<int, SWCNode> splitEdges() const {return this->splitEdges(this->nodes);};
371 
379  std::vector<std::map<int, SWCNode>> splitEdgesN(const std::map<int, SWCNode>& nodeSet, int N) const;
380 
385  std::vector<std::map<int, SWCNode>> splitEdgesN(int N) const {return this->splitEdgesN(this->nodes, N);};
386 
396  std::map<int,std::map<int,SWCNode>> getTrunks(const std::map<int,SWCNode>& nodeSet, bool resetIndex = false) const;
397 
402  std::map<int,std::map<int,SWCNode>> getTrunks(bool resetIndex = false) const {return this->getTrunks(this->nodes, resetIndex);};
403 
410  std::map<int, int> getTrunkParentMap(const std::map<int, SWCNode>& nodeSet,
411  const std::map<int, std::map<int, SWCNode>>& trunkNodeSets) const;
412 
420  std::map<int, SWCNode> assembleTrunks(const std::map<int, std::map<int, SWCNode>>& trunkNodeSets) const;
421 
431  std::map<int, SWCNode> assembleTrunks(const std::map<int, std::map<int, SWCNode>>& resampledTrunks,
432  const std::map<int,int>& trunkParentMap);
433 
440  std::map<int, SWCNode> linearSplineResampleTrunk(const std::map<int, SWCNode>& trunk, double& delta) const;
441 
448  std::map<int, std::map<int, SWCNode>> allLinearSplineResampledTrunks(std::map<int, std::map<int, SWCNode>>& trunks, double& delta) const;
449 
459  std::map<int, SWCNode> cubicSplineResampleTrunk(const std::map<int, SWCNode>& trunk, double& delta) const;
460 
467  std::map<int, std::map<int, SWCNode>> allCubicSplineResampledTrunks(std::map<int, std::map<int, SWCNode>>& trunks, double& delta) const;
468 
480  std::map<int, std::map<int,SWCNode>> generateRefinements(const std::map<int,SWCNode>& nodeSet,
481  double& delta,
482  int& N,
483  std::string& method);
484 
489  std::map<int, std::map<int,SWCNode>> generateRefinements(double& delta,
490  int& N,
491  std::string& method) {
492  return this->generateRefinements(this->nodes, delta, N, method);
493  };
494 };
495 
496 #endif // NEURONGRAPH_H
Class for representing and processing neuron morphology graphs.
Definition: neurongraph.h:91
std::map< int, SWCNode > topologicalSort() const
Definition: neurongraph.h:182
bool isTopologicallySorted() const
Definition: neurongraph.h:163
std::map< int, SWCNode > removeSomaSegment() const
Definition: neurongraph.h:228
void writeToFile(const std::string &filename)
Definition: neurongraph.h:281
std::map< int, std::map< int, SWCNode > > allLinearSplineResampledTrunks(std::map< int, std::map< int, SWCNode >> &trunks, double &delta) const
Resamples all trunks using linear interpolation.
Definition: neurontrunks.cpp:281
std::map< int, std::map< int, SWCNode > > allCubicSplineResampledTrunks(std::map< int, std::map< int, SWCNode >> &trunks, double &delta) const
Resamples all trunks using cubic spline interpolation.
Definition: neurontrunks.cpp:318
void readFromFileUGXorSWC(const std::string &filename)
Reads a neuron file, automatically detecting the format.
Definition: neurongraph.cpp:241
std::map< int, SWCNode > nodes
Map of node IDs to SWCNode objects.
Definition: neurongraph.h:94
void ugx2swc(const std::string &inputfile, const std::string &outputfile)
Converts a UGX file to SWC format.
Definition: neurongraph.cpp:289
std::map< int, SWCNode > linearSplineResampleTrunk(const std::map< int, SWCNode > &trunk, double &delta) const
Resamples a single trunk using linear interpolation.
Definition: neurontrunks.cpp:360
void addNode(const SWCNode &node)
Adds a single node to the graph.
Definition: neurongraph.cpp:70
std::map< int, SWCNode > preprocess(const std::map< int, SWCNode > &nodeSet) const
Applies standard preprocessing steps to a set of nodes.
Definition: neurongraph.cpp:94
void writeToFile(const std::map< int, SWCNode > &nodeSet, const std::string &filename)
Writes a set of nodes to an SWC file.
Definition: neurongraph.cpp:198
void setNodes(const std::map< int, SWCNode > &nodeSet)
Replaces the current set of nodes in the graph.
Definition: neurongraph.cpp:50
std::vector< std::map< int, SWCNode > > splitEdgesN(int N) const
Definition: neurongraph.h:385
std::map< int, std::map< int, SWCNode > > generateRefinements(double &delta, int &N, std::string &method)
Definition: neurongraph.h:489
std::map< int, SWCNode > setSoma() const
Definition: neurongraph.h:244
std::map< int, std::map< int, SWCNode > > getTrunks(bool resetIndex=false) const
Definition: neurongraph.h:402
std::map< int, std::vector< int > > getNeighborMap(const std::map< int, SWCNode > &nodeSet) const
Builds a neighbor map from a set of nodes.
Definition: neurontrunks.cpp:46
std::map< int, SWCNode > assembleTrunks(const std::map< int, std::map< int, SWCNode >> &trunkNodeSets) const
Combines multiple trunks into a single node set.
Definition: neurontrunks.cpp:228
void readFromFileUGX(const std::string &filename)
Reads neuron data from a UGX file.
Definition: neuronugx.cpp:244
NeuronGraph(std::string filename)
Constructor that loads a neuron from file.
Definition: neurongraph.h:122
void readFromFile(const std::string &filename)
Reads neuron data from an SWC file.
Definition: neurongraph.cpp:127
void writeToFileUGX(const std::map< int, SWCNode > &nodeSet, const std::string &filename)
Writes a set of nodes to a UGX file.
Definition: neuronugx.cpp:61
int numberOfNodes()
Returns the number of nodes in the graph.
Definition: neurongraph.h:341
std::map< int, int > getTrunkParentMap(const std::map< int, SWCNode > &nodeSet, const std::map< int, std::map< int, SWCNode >> &trunkNodeSets) const
Creates a mapping from trunk IDs to their parent trunk IDs.
Definition: neurontrunks.cpp:171
void writeToFileUGX(const std::string &filename)
Definition: neurongraph.h:317
auto getNodes()
Returns a copy of all nodes in the graph.
Definition: neurongraph.h:353
std::map< int, SWCNode > splitEdges() const
Definition: neurongraph.h:370
std::map< int, std::map< int, SWCNode > > generateRefinements(const std::map< int, SWCNode > &nodeSet, double &delta, int &N, std::string &method)
Generates multiple levels of refined neuron morphologies.
Definition: neurontrunks.cpp:697
std::vector< std::map< int, SWCNode > > splitEdgesN(const std::map< int, SWCNode > &nodeSet, int N) const
Applies edge splitting N times recursively.
Definition: neuronoperations.cpp:193
NeuronGraph()
Default constructor.
Definition: neurongraph.h:114
int numberOfEdges()
Returns the number of edges in the graph.
Definition: neurongraph.h:347
bool hasSomaSegment() const
Definition: neurongraph.h:197
bool isSomaMissing() const
Definition: neurongraph.h:212
std::map< int, SWCNode > cubicSplineResampleTrunk(const std::map< int, SWCNode > &trunk, double &delta) const
Resamples a single trunk using cubic spline interpolation.
Definition: neurontrunks.cpp:458
std::map< int, std::map< int, SWCNode > > getTrunks(const std::map< int, SWCNode > &nodeSet, bool resetIndex=false) const
Extracts trunk segments from a neuron morphology.
Definition: neurontrunks.cpp:85
std::map< int, std::vector< int > > edges
Adjacency list representation of the neuron graph.
Definition: neurongraph.h:97
void swc2ugx(const std::string &inputfile, const std::string &outputfile)
Converts an SWC file to UGX format.
Definition: neurongraph.cpp:268
float delta
Definition: test_bindings.py:66
Structure representing a single node in an SWC neuron morphology.
Definition: neurongraph.h:43
double z
Z-coordinate in 3D space.
Definition: neurongraph.h:70
double radius
Radius of the neural process at this node.
Definition: neurongraph.h:73
int pid
Parent node ID (-1 for root nodes)
Definition: neurongraph.h:48
int type
Node type identifier.
Definition: neurongraph.h:61
int id
Unique identifier for the node.
Definition: neurongraph.h:45
double x
X-coordinate in 3D space.
Definition: neurongraph.h:64
double y
Y-coordinate in 3D space.
Definition: neurongraph.h:67