Skip to content

Commit

Permalink
Add option for root point not equal to -1 in SWC files
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Dec 16, 2022
1 parent 907137c commit b63ec83
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions include/morphio/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ enum Option {
NRN_ORDER = 1 << 3, //!< Order of neurites will be the same as in NEURON simulator
ALLOW_ROOT_BIFURCATIONS = 1 << 4, //!< Bifurcations at first point are allowed
ALLOW_SOMA_BIFURCATIONS = 1 << 5, //!< Bifurcations in soma are allowed
ALLOW_MULTIPLE_SOMATA = 1 << 6 //!< Multiple somata are allowed
ALLOW_MULTIPLE_SOMATA = 1 << 6, //!< Multiple somata are allowed
ALLOW_CUSTOM_ROOT_ID = 1 << 7 //!< Custom root points are allowed
};

/**
Expand All @@ -40,7 +41,8 @@ enum Warning {
ZERO_DIAMETER, //!< Zero section diameter
ROOT_BIFURCATION, //!< Bifurcation at root point
SOMA_BIFURCATION, //!< Bifurcation in soma
MULTIPLE_SOMATA //!< Multiple somata
MULTIPLE_SOMATA, //!< Multiple somata
CUSTOM_ROOT_ID //!< Custom root ID
};

enum AnnotationType {
Expand Down
2 changes: 2 additions & 0 deletions include/morphio/errorMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class ErrorMessages
std::string WARNING_SOMA_BIFURCATION() const;
/** Loading multiple somata */
std::string WARNING_MULTIPLE_SOMATA() const;
/** Loading point with root ID no equal to -1 */
std::string WARNING_CUSTOM_ROOT_ID(const Sample& sample) const;
/** Loading section with only one point */
std::string WARNING_ROOT_BIFURCATION(const Sample& sample) const;
/** Writing empty morphology warning message */
Expand Down
7 changes: 7 additions & 0 deletions src/errorMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ std::string ErrorMessages::WARNING_APPENDING_EMPTY_SECTION(
"Warning: appending empty section with id: " + std::to_string(section->id()));
}

std::string ErrorMessages::WARNING_CUSTOM_ROOT_ID(const Sample& sample) const {
return errorMsg(sample.lineNumber,
ErrorLevel::WARNING,
"Warning: root id changed from " + std::to_string(sample.parentId) +
" to -1 for point with id: " + std::to_string(sample.id));
}

std::string ErrorMessages::WARNING_ROOT_BIFURCATION(const Sample& sample) const {
return errorMsg(sample.lineNumber,
ErrorLevel::WARNING,
Expand Down
18 changes: 18 additions & 0 deletions src/readers/morphologySWC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,24 @@ class SWCBuilder
Property::Properties buildProperties(const std::string& contents, unsigned int options) {
_readSamples(contents);

if (options & ALLOW_CUSTOM_ROOT_ID) {
for (auto& sample_pair : samples) {
auto& sample = sample_pair.second;
if (sample.parentId > -1 &&
samples.count(static_cast<unsigned int>(sample.parentId)) == 0) {
printError(Warning::CUSTOM_ROOT_ID, err.WARNING_CUSTOM_ROOT_ID(sample));
// Remove the sample from the children
children[sample.parentId].erase(std::remove(children[sample.parentId].begin(),
children[sample.parentId].end(),
sample.id),
children[sample.parentId].end());
// Update the sample and add it to the proper children
sample.parentId = -1;
children[-1].push_back(sample.id);
}
}
}

for (const auto& sample_pair : samples) {
const auto& sample = sample_pair.second;
raiseIfNonConform(sample, options);
Expand Down
7 changes: 7 additions & 0 deletions tests/data/edge_cases/bad_root_point.swc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The root point is not -1
1 2 0 0 0 6 0
2 2 0 0 1 6 1
3 2 0 0 2 6 2
4 2 1 0 0 6 0
5 2 1 0 1 6 1
6 2 1 0 2 6 2
19 changes: 19 additions & 0 deletions tests/test_reader_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,22 @@ TEST_CASE("multiple somata", "[loader_edge_cases]") {
morphio::enums::Option::ALLOW_MULTIPLE_SOMATA));
}
}


TEST_CASE("bad root point", "[loader_edge_cases]") {
std::vector<std::string> exts{"swc"};
for (auto&& ext : exts) {
SECTION("testing mutable morphology with " + ext + " extension") {
// By default, root points not equal to -1 are considered as errors in SWC files
REQUIRE_THROWS(morphio::mut::Morphology("data/edge_cases/bad_root_point." + ext));

// Bad root points can optionaly be loaded from SWC files
auto mutable_morph =
morphio::mut::Morphology("data/edge_cases/bad_root_point." + ext,
morphio::enums::Option::ALLOW_CUSTOM_ROOT_ID);

REQUIRE(mutable_morph.soma()->points().size() == 0);
REQUIRE(mutable_morph.rootSections().size() == 2);
}
}
}

0 comments on commit b63ec83

Please sign in to comment.