morphology.h
1 #pragma once
2 
3 #include <cstdint>
4 #include <iomanip>
5 #include <iostream>
6 #include <memory>
7 #include <ostream>
8 #include <unordered_map>
9 
10 #include <functional>
11 
12 #include <morphio/errorMessages.h>
13 #include <morphio/exceptions.h>
14 #include <morphio/mut/endoplasmic_reticulum.h>
15 #include <morphio/mut/mitochondria.h>
16 #include <morphio/mut/soma.h>
17 #include <morphio/properties.h>
18 #include <morphio/section.h>
19 #include <morphio/types.h>
20 
21 namespace morphio {
22 namespace mut {
23 bool _checkDuplicatePoint(const std::shared_ptr<Section>& parent,
24  const std::shared_ptr<Section>& current);
25 
28 {
29  public:
30  Morphology()
31  : _counter(0)
32  , _soma(std::make_shared<Soma>())
33  , _cellProperties(
34  std::make_shared<morphio::Property::CellLevel>(morphio::Property::CellLevel())) {}
35 
45  Morphology(const std::string& uri, unsigned int options = NO_MODIFIER);
46 
50  Morphology(const morphio::mut::Morphology& morphology, unsigned int options = NO_MODIFIER);
51 
55  Morphology(const morphio::Morphology& morphology, unsigned int options = NO_MODIFIER);
56 
57  virtual ~Morphology();
58 
62  inline const std::vector<std::shared_ptr<Section>>& rootSections() const noexcept;
63 
67  inline const std::map<uint32_t, std::shared_ptr<Section>>& sections() const noexcept;
68 
74  inline std::shared_ptr<Soma>& soma() noexcept;
75 
81  inline const std::shared_ptr<Soma>& soma() const noexcept;
82 
86  inline Mitochondria& mitochondria() noexcept;
90  inline const Mitochondria& mitochondria() const noexcept;
91 
95  inline EndoplasmicReticulum& endoplasmicReticulum() noexcept;
99  inline const EndoplasmicReticulum& endoplasmicReticulum() const noexcept;
100 
104  inline const std::vector<Property::Annotation>& annotations() const noexcept;
105 
109  inline const std::vector<Property::Marker>& markers() const noexcept;
110 
116  inline const std::shared_ptr<Section>& section(uint32_t id) const;
117 
123  depth_iterator depth_begin() const;
124  depth_iterator depth_end() const;
125 
133  breadth_iterator breadth_end() const;
134 
136  //
137  // Tree manipulation methods
138  //
140 
149  void deleteSection(std::shared_ptr<Section> section, bool recursive = true);
150 
156  std::shared_ptr<Section> appendRootSection(const morphio::Section&, bool recursive = false);
157 
163  std::shared_ptr<Section> appendRootSection(const std::shared_ptr<Section>& section,
164  bool recursive = false);
165 
169  std::shared_ptr<Section> appendRootSection(const Property::PointLevel&,
170  SectionType sectionType);
171 
172  void applyModifiers(unsigned int modifierFlags);
173 
177  inline SomaType somaType() const noexcept;
178 
182  inline CellFamily cellFamily() const noexcept;
183 
187  inline MorphologyVersion version() const noexcept;
188 
192  void write(const std::string& filename);
193 
194  inline void addAnnotation(const morphio::Property::Annotation& annotation);
195  inline void addMarker(const morphio::Property::Marker& marker);
196 
201 
207  std::unordered_map<int, std::vector<unsigned int>> connectivity();
208 
209 
214  void removeUnifurcations();
215  void removeUnifurcations(const morphio::readers::DebugInfo& debugInfo);
216 
220  void _raiseIfUnifurcations();
221 
222  public:
223  friend class Section;
224  friend void modifiers::nrn_order(morphio::mut::Morphology& morpho);
225  friend bool diff(const Morphology& left,
226  const Morphology& right,
227  morphio::enums::LogLevel verbose);
229 
230  uint32_t _register(const std::shared_ptr<Section>&);
231 
232  uint32_t _counter;
233  std::shared_ptr<Soma> _soma;
234  std::shared_ptr<morphio::Property::CellLevel> _cellProperties;
235  std::vector<std::shared_ptr<Section>> _rootSections;
236  std::map<uint32_t, std::shared_ptr<Section>> _sections;
237 
238  Mitochondria _mitochondria;
239  EndoplasmicReticulum _endoplasmicReticulum;
240  morphio::Property::DendriticSpine::Level _dendriticSpineLevel;
241 
242  std::map<uint32_t, uint32_t> _parent;
243  std::map<uint32_t, std::vector<std::shared_ptr<Section>>> _children;
244 
245  private:
246  void eraseByValue(std::vector<std::shared_ptr<Section>>& vec,
247  const std::shared_ptr<Section> section);
248 };
249 
250 inline const std::vector<std::shared_ptr<Section>>& Morphology::rootSections() const noexcept {
251  return _rootSections;
252 }
253 
254 inline const std::map<uint32_t, std::shared_ptr<Section>>& Morphology::sections() const noexcept {
255  return _sections;
256 }
257 
258 inline std::shared_ptr<Soma>& Morphology::soma() noexcept {
259  return _soma;
260 }
261 
262 inline const std::shared_ptr<Soma>& Morphology::soma() const noexcept {
263  return _soma;
264 }
265 
267  return _mitochondria;
268 }
269 
270 inline const Mitochondria& Morphology::mitochondria() const noexcept {
271  return _mitochondria;
272 }
273 
275  return _endoplasmicReticulum;
276 }
277 
279  return _endoplasmicReticulum;
280 }
281 
282 inline const std::shared_ptr<Section>& Morphology::section(uint32_t id) const {
283  return _sections.at(id);
284 }
285 
286 inline SomaType Morphology::somaType() const noexcept {
287  return _soma->type();
288 }
289 
290 inline const std::vector<Property::Annotation>& Morphology::annotations() const noexcept {
291  return _cellProperties->_annotations;
292 }
293 
294 inline const std::vector<Property::Marker>& Morphology::markers() const noexcept {
295  return _cellProperties->_markers;
296 }
297 
298 inline CellFamily Morphology::cellFamily() const noexcept {
299  return _cellProperties->_cellFamily;
300 }
301 
302 inline MorphologyVersion Morphology::version() const noexcept {
303  return _cellProperties->_version;
304 }
305 
306 inline void Morphology::addAnnotation(const morphio::Property::Annotation& annotation) {
307  _cellProperties->_annotations.push_back(annotation);
308 }
309 
310 inline void Morphology::addMarker(const morphio::Property::Marker& marker) {
311  _cellProperties->_markers.push_back(marker);
312 }
313 
314 } // namespace mut
315 } // namespace morphio
CellFamily cellFamily() const noexcept
Definition: morphology.h:298
Definition: section_iterators.hpp:53
const std::map< uint32_t, std::shared_ptr< Section > > & sections() const noexcept
Definition: morphology.h:254
const std::vector< Property::Annotation > & annotations() const noexcept
Definition: morphology.h:290
MorphologyVersion version() const noexcept
Definition: morphology.h:302
Definition: properties.h:180
const std::vector< Property::Marker > & markers() const noexcept
Definition: morphology.h:294
Property::Properties buildReadOnly() const
Mitochondria & mitochondria() noexcept
Definition: morphology.h:266
Read morphology as is without any modification.
Definition: enums.h:13
breadth_iterator breadth_begin() const
Definition: dendritic_spine.h:9
Definition: morphology.h:27
depth_iterator depth_begin() const
std::unordered_map< int, std::vector< unsigned int > > connectivity()
Definition: endoplasmic_reticulum.h:9
const std::vector< std::shared_ptr< Section > > & rootSections() const noexcept
Definition: morphology.h:250
SomaType somaType() const noexcept
Definition: morphology.h:286
Definition: section.h:19
Definition: section_iterators.hpp:81
std::shared_ptr< Section > appendRootSection(const morphio::Section &, bool recursive=false)
const std::shared_ptr< Section > & section(uint32_t id) const
Definition: morphology.h:282
Definition: properties.h:203
Definition: morphology.h:20
Definition: mitochondria.h:21
Definition: properties.h:170
friend bool diff(const Morphology &left, const Morphology &right, morphio::enums::LogLevel verbose)
Definition: properties.h:146
CellFamily
Definition: enums.h:44
Definition: errorMessages.h:106
Definition: properties.h:128
std::shared_ptr< Soma > & soma() noexcept
Definition: morphology.h:258
void write(const std::string &filename)
SomaType
Definition: enums.h:51
Definition: properties.h:55
SectionType
Definition: enums.h:61
Definition: errorMessages.h:33
void deleteSection(std::shared_ptr< Section > section, bool recursive=true)
Definition: section.h:36
EndoplasmicReticulum & endoplasmicReticulum() noexcept
Definition: morphology.h:274