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/soma.h>
19 #include <morphio/properties.h>
20 #include <morphio/types.h>
21 
22 namespace morphio {
23 namespace mut {
24 // TODO: not sure why this is here
25 bool _checkDuplicatePoint(const std::shared_ptr<Section>& parent,
26  const std::shared_ptr<Section>& current);
27 
30 {
31  public:
32  Morphology()
33  : _soma(std::make_shared<Soma>())
34  , _cellProperties(
35  std::make_shared<morphio::Property::CellLevel>(morphio::Property::CellLevel())) {}
36 
46  explicit Morphology(const std::string& uri, unsigned int options = NO_MODIFIER);
47 
49  explicit Morphology(const HighFive::Group& group, unsigned int options = NO_MODIFIER);
50 
52  Morphology(const morphio::mut::Morphology& morphology, unsigned int options = NO_MODIFIER);
53 
55  explicit Morphology(const morphio::Morphology& morphology, unsigned int options = NO_MODIFIER);
56 
57  virtual ~Morphology();
58 
60  const std::vector<std::shared_ptr<Section>>& rootSections() const noexcept {
61  return _rootSections;
62  }
63 
65  const std::map<uint32_t, std::shared_ptr<Section>>& sections() const noexcept {
66  return _sections;
67  }
68 
74  std::shared_ptr<Soma>& soma() noexcept {
75  return _soma;
76  }
77 
83  const std::shared_ptr<Soma>& soma() const noexcept {
84  return _soma;
85  }
86 
88  Mitochondria& mitochondria() noexcept {
89  return _mitochondria;
90  }
91 
93  const Mitochondria& mitochondria() const noexcept {
94  return _mitochondria;
95  }
96 
99  return _endoplasmicReticulum;
100  }
103  const EndoplasmicReticulum& endoplasmicReticulum() const noexcept {
104  return _endoplasmicReticulum;
105  }
106 
108  const std::vector<Property::Annotation>& annotations() const noexcept {
109  return _cellProperties->_annotations;
110  }
111 
113  const std::vector<Property::Marker>& markers() const noexcept {
114  return _cellProperties->_markers;
115  }
116 
122  const std::shared_ptr<Section>& section(uint32_t id) const {
123  return _sections.at(id);
124  }
125 
131  depth_iterator depth_begin() const;
132  depth_iterator depth_end() const;
133 
141  breadth_iterator breadth_end() const;
142 
151  void deleteSection(std::shared_ptr<Section> section, bool recursive = true);
152 
158  std::shared_ptr<Section> appendRootSection(const morphio::Section&, bool recursive = false);
159 
165  std::shared_ptr<Section> appendRootSection(const std::shared_ptr<Section>& section,
166  bool recursive = false);
167 
169  std::shared_ptr<Section> appendRootSection(const Property::PointLevel&,
170  SectionType sectionType);
171 
172  void applyModifiers(unsigned int modifierFlags);
173 
175  SomaType somaType() const noexcept {
176  return _soma->type();
177  }
178 
180  CellFamily cellFamily() const noexcept {
181  return _cellProperties->_cellFamily;
182  }
183 
185  MorphologyVersion version() const noexcept {
186  return _cellProperties->_version;
187  }
188 
190  void write(const std::string& filename) const;
191 
192  void addAnnotation(const morphio::Property::Annotation& annotation) {
193  _cellProperties->_annotations.push_back(annotation);
194  }
195 
196  void addMarker(const morphio::Property::Marker& marker) {
197  _cellProperties->_markers.push_back(marker);
198  }
199 
201  Property::Properties buildReadOnly() const;
202 
208  std::unordered_map<int, std::vector<unsigned int>> connectivity();
209 
214  void removeUnifurcations();
215  void removeUnifurcations(const morphio::readers::DebugInfo& debugInfo);
216 
217  std::shared_ptr<Soma> _soma;
218  std::shared_ptr<morphio::Property::CellLevel> _cellProperties;
219  EndoplasmicReticulum _endoplasmicReticulum;
220  morphio::Property::DendriticSpine::Level _dendriticSpineLevel;
221 
222  private:
223  std::vector<std::shared_ptr<Section>> _rootSections;
224  std::map<uint32_t, std::shared_ptr<Section>> _sections;
225 
226  Mitochondria _mitochondria;
227 
228  std::map<uint32_t, uint32_t> _parent;
229  std::map<uint32_t, std::vector<std::shared_ptr<Section>>> _children;
230 
231  uint32_t _counter = 0;
232 
233  uint32_t _register(const std::shared_ptr<Section>&);
235 
236  void eraseByValue(std::vector<std::shared_ptr<Section>>& vec,
237  const std::shared_ptr<Section> section);
238 
239  friend class Section;
240  friend void modifiers::nrn_order(morphio::mut::Morphology& morpho);
241  friend bool diff(const Morphology& left,
242  const Morphology& right,
243  morphio::enums::LogLevel verbose);
244 };
245 
246 } // namespace mut
247 } // namespace morphio
morphio::mut::Morphology::mitochondria
const Mitochondria & mitochondria() const noexcept
Return the mitochondria container class.
Definition: morphology.h:93
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:65
morphio::mut::Morphology::somaType
SomaType somaType() const noexcept
Return the soma type.
Definition: morphology.h:175
morphio::Section
Definition: section.h:36
morphio::mut::Morphology::endoplasmicReticulum
const EndoplasmicReticulum & endoplasmicReticulum() const noexcept
Return the endoplasmic reticulum container class.
Definition: morphology.h:103
morphio::enums::NO_MODIFIER
@ NO_MODIFIER
Read morphology as is without any modification.
Definition: enums.h:18
morphio::Morphology
Definition: morphology.h:24
morphio::mut::Morphology::depth_begin
depth_iterator depth_begin() const
morphio::Property::Annotation
Definition: properties.h:153
morphio::mut::Morphology::soma
const std::shared_ptr< Soma > & soma() const noexcept
Definition: morphology.h:83
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:113
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::diff
friend bool diff(const Morphology &left, const Morphology &right, morphio::enums::LogLevel verbose)
morphio::mut::Morphology::cellFamily
CellFamily cellFamily() const noexcept
Return the cell family (neuron or glia)
Definition: morphology.h:180
morphio::Property::CellLevel
Definition: properties.h:187
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:60
morphio::mut::Morphology::section
const std::shared_ptr< Section > & section(uint32_t id) const
Definition: morphology.h:122
morphio::readers::DebugInfo
Definition: errorMessages.h:38
morphio::Property::Marker
Definition: properties.h:177
morphio::mut::Morphology::version
MorphologyVersion version() const noexcept
Return the version.
Definition: morphology.h:185
morphio::mut::Mitochondria
Definition: mitochondria.h:25
morphio::mut::Morphology::mitochondria
Mitochondria & mitochondria() noexcept
Return the mitochondria container class.
Definition: morphology.h:88
morphio::mut::Morphology::endoplasmicReticulum
EndoplasmicReticulum & endoplasmicReticulum() noexcept
Return the endoplasmic reticulum container class.
Definition: morphology.h:98
morphio::mut::Morphology::removeUnifurcations
void removeUnifurcations()
morphio::Property::DendriticSpine::Level
Definition: properties.h:135
morphio::mut::Morphology::buildReadOnly
Property::Properties buildReadOnly() const
Return the data structure used to create read-only morphologies.
morphio::readers::ErrorMessages
Definition: errorMessages.h:86
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:108
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:29
morphio::mut::Morphology::breadth_begin
breadth_iterator breadth_begin() const
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:74
morphio::Property::PointLevel
Definition: properties.h:62