morphology.h
1 /* Copyright (c) 2013-2023, EPFL/Blue Brain Project
2  *
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <map>
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include <morphio/errorMessages.h>
14 #include <morphio/exceptions.h>
15 #include <morphio/mut/endoplasmic_reticulum.h>
16 #include <morphio/mut/mitochondria.h>
17 #include <morphio/mut/modifiers.h>
18 #include <morphio/mut/section.h>
19 #include <morphio/mut/soma.h>
20 #include <morphio/properties.h>
21 #include <morphio/types.h>
22 #include <morphio/warning_handling.h>
23 
24 
25 namespace morphio {
26 namespace mut {
27 // TODO: not sure why this is here
28 bool _checkDuplicatePoint(const std::shared_ptr<Section>& parent,
29  const std::shared_ptr<Section>& current);
30 
33 {
34  public:
35  Morphology(std::shared_ptr<WarningHandler> warning_handler = nullptr)
36  : _soma(std::make_shared<Soma>())
37  , _cellProperties(std::make_shared<Property::CellLevel>())
38  , _handler(warning_handler ? warning_handler : morphio::getWarningHandler()) {}
39 
49  explicit Morphology(const std::string& uri,
50  unsigned int options = NO_MODIFIER,
51  std::shared_ptr<WarningHandler> warning_handler = nullptr);
52 
54  explicit Morphology(const HighFive::Group& group,
55  unsigned int options = NO_MODIFIER,
56  std::shared_ptr<WarningHandler> warning_handler = nullptr);
57 
59  Morphology(const mut::Morphology& morphology,
60  unsigned int options = NO_MODIFIER,
61  std::shared_ptr<WarningHandler> warning_handler = nullptr);
62 
64  explicit Morphology(const morphio::Morphology& morphology,
65  unsigned int options = NO_MODIFIER,
66  std::shared_ptr<WarningHandler> warning_handler = nullptr);
67 
68  virtual ~Morphology();
69 
71  const std::vector<std::shared_ptr<Section>>& rootSections() const noexcept {
72  return _rootSections;
73  }
74 
76  const std::map<uint32_t, std::shared_ptr<Section>>& sections() const noexcept {
77  return _sections;
78  }
79 
85  std::shared_ptr<Soma>& soma() noexcept {
86  return _soma;
87  }
88 
94  const std::shared_ptr<Soma>& soma() const noexcept {
95  return _soma;
96  }
97 
99  Mitochondria& mitochondria() noexcept {
100  return _mitochondria;
101  }
102 
104  const Mitochondria& mitochondria() const noexcept {
105  return _mitochondria;
106  }
107 
110  return _endoplasmicReticulum;
111  }
114  const EndoplasmicReticulum& endoplasmicReticulum() const noexcept {
115  return _endoplasmicReticulum;
116  }
117 
119  const std::vector<Property::Annotation>& annotations() const noexcept {
120  return _cellProperties->_annotations;
121  }
122 
124  const std::vector<Property::Marker>& markers() const noexcept {
125  return _cellProperties->_markers;
126  }
127 
133  const std::shared_ptr<Section>& section(uint32_t id) const {
134  return _sections.at(id);
135  }
136 
142  depth_iterator depth_begin() const;
143  depth_iterator depth_end() const;
144 
152  breadth_iterator breadth_end() const;
153 
162  void deleteSection(std::shared_ptr<Section> section, bool recursive = true);
163 
169  std::shared_ptr<Section> appendRootSection(const morphio::Section&, bool recursive = false);
170 
176  std::shared_ptr<Section> appendRootSection(const std::shared_ptr<Section>& section,
177  bool recursive = false);
178 
180  std::shared_ptr<Section> appendRootSection(const Property::PointLevel&,
181  SectionType sectionType);
182 
183  void applyModifiers(unsigned int modifierFlags);
184 
186  SomaType somaType() const noexcept {
187  return _soma->type();
188  }
189 
191  CellFamily cellFamily() const noexcept {
192  return _cellProperties->_cellFamily;
193  }
194 
196  MorphologyVersion version() const noexcept {
197  return _cellProperties->_version;
198  }
199 
201  void write(const std::string& filename) const;
202 
203  void addAnnotation(const Property::Annotation& annotation) {
204  _cellProperties->_annotations.push_back(annotation);
205  }
206 
207  void addMarker(const Property::Marker& marker) {
208  _cellProperties->_markers.push_back(marker);
209  }
210 
212  Property::Properties buildReadOnly() const;
213 
219  std::unordered_map<int, std::vector<unsigned int>> connectivity();
220 
225  void removeUnifurcations();
226 
227  std::shared_ptr<WarningHandler> getWarningHandler() const {
228  return _handler;
229  }
230 
231  std::shared_ptr<Soma> _soma;
232  std::shared_ptr<Property::CellLevel> _cellProperties;
233  EndoplasmicReticulum _endoplasmicReticulum;
234  Property::DendriticSpine::Level _dendriticSpineLevel;
235 
236  private:
237  std::vector<std::shared_ptr<Section>> _rootSections;
238  std::map<uint32_t, std::shared_ptr<Section>> _sections;
239 
240  Mitochondria _mitochondria;
241 
242  std::map<uint32_t, uint32_t> _parent;
243  std::map<uint32_t, std::vector<std::shared_ptr<Section>>> _children;
244 
245  uint32_t _counter = 0;
246 
247  uint32_t _register(const std::shared_ptr<Section>&);
248 
249  void eraseByValue(std::vector<std::shared_ptr<Section>>& vec,
250  const std::shared_ptr<Section>& section);
251 
252  std::shared_ptr<WarningHandler> _handler;
253  std::string _uri;
254 
255  friend class Section;
256  friend void modifiers::nrn_order(mut::Morphology& morpho);
257  friend bool diff(const Morphology& left, const Morphology& right, enums::LogLevel verbose);
258 };
259 
260 } // namespace mut
261 } // namespace morphio
morphio::mut::Morphology::mitochondria
const Mitochondria & mitochondria() const noexcept
Return the mitochondria container class.
Definition: morphology.h:104
morphio::enums::SomaType
SomaType
Definition: enums.h:59
morphio::depth_iterator_t
Definition: section_iterators.hpp:82
morphio::enums::SectionType
SectionType
Definition: enums.h:69
morphio::mut::Morphology::sections
const std::map< uint32_t, std::shared_ptr< Section > > & sections() const noexcept
Returns the dictionary id -> Section for this tree.
Definition: morphology.h:76
morphio::mut::Morphology::somaType
SomaType somaType() const noexcept
Return the soma type.
Definition: morphology.h:186
morphio::Section
Definition: section.h:36
morphio::mut::Morphology::endoplasmicReticulum
const EndoplasmicReticulum & endoplasmicReticulum() const noexcept
Return the endoplasmic reticulum container class.
Definition: morphology.h:114
morphio::enums::NO_MODIFIER
@ NO_MODIFIER
Read morphology as is without any modification.
Definition: enums.h:18
morphio::Morphology
Definition: morphology.h:26
morphio::mut::Morphology::depth_begin
depth_iterator depth_begin() const
morphio::Property::Annotation
Definition: properties.h:152
morphio::mut::Morphology::soma
const std::shared_ptr< Soma > & soma() const noexcept
Definition: morphology.h:94
morphio::mut::EndoplasmicReticulum
Definition: endoplasmic_reticulum.h:13
morphio::mut::Morphology::markers
const std::vector< Property::Marker > & markers() const noexcept
Return the markers from the ASC file.
Definition: morphology.h:124
morphio::mut::Morphology::deleteSection
void deleteSection(std::shared_ptr< Section > section, bool recursive=true)
morphio::enums::CellFamily
CellFamily
Definition: enums.h:52
morphio::mut::Morphology::cellFamily
CellFamily cellFamily() const noexcept
Return the cell family (neuron or glia)
Definition: morphology.h:191
morphio::mut::Morphology::rootSections
const std::vector< std::shared_ptr< Section > > & rootSections() const noexcept
Returns all section ids at the tree root.
Definition: morphology.h:71
morphio::mut::Morphology::section
const std::shared_ptr< Section > & section(uint32_t id) const
Definition: morphology.h:133
morphio::Property::Marker
Definition: properties.h:176
morphio::mut::Morphology::version
MorphologyVersion version() const noexcept
Return the version.
Definition: morphology.h:196
morphio::mut::Mitochondria
Definition: mitochondria.h:25
morphio::mut::Morphology::mitochondria
Mitochondria & mitochondria() noexcept
Return the mitochondria container class.
Definition: morphology.h:99
morphio::mut::Morphology::endoplasmicReticulum
EndoplasmicReticulum & endoplasmicReticulum() noexcept
Return the endoplasmic reticulum container class.
Definition: morphology.h:109
morphio::mut::Morphology::removeUnifurcations
void removeUnifurcations()
morphio::mut::Morphology::buildReadOnly
Property::Properties buildReadOnly() const
Return the data structure used to create read-only morphologies.
morphio::breadth_iterator_t
Definition: section_iterators.hpp:53
morphio::mut::Morphology::annotations
const std::vector< Property::Annotation > & annotations() const noexcept
Return the annotation objects.
Definition: morphology.h:119
morphio::mut::Morphology::write
void write(const std::string &filename) const
Write file to H5, SWC, ASC format depending on filename extension.
morphio::mut::Morphology::connectivity
std::unordered_map< int, std::vector< unsigned int > > connectivity()
morphio::mut::Morphology
Definition: morphology.h:32
morphio::mut::Morphology::breadth_begin
breadth_iterator breadth_begin() const
morphio::mut::Morphology::diff
friend bool diff(const Morphology &left, const Morphology &right, enums::LogLevel verbose)
morphio::mut::Morphology::appendRootSection
std::shared_ptr< Section > appendRootSection(const morphio::Section &, bool recursive=false)
morphio::mut::Morphology::soma
std::shared_ptr< Soma > & soma() noexcept
Definition: morphology.h:85
morphio::Property::PointLevel
Definition: properties.h:62