A neuron morphology IO library
Project description
MorphIO
Table of content
Installation
Dependencies
To build MorphIO from sources, the following dependencies are required:
- cmake >= 3.2
- libhdf5-dev
- A C++11 compiler
Debian:
sudo apt install cmake libhdf5-dev
Red Hat:
sudo yum install cmake3.x86_64 hdf5-devel.x86_64
Max OS:
brew install hdf5 cmake
BB5
source /opt/rh/rh-python36/enable
module load gcc/5.4.0 nix/cmake/3.9.6
Installation instructions
Install as a C++ library
For manual installation:
git clone git@github.com:bluebrain/morphio.git --recursive
cd morphio
mkdir build && cd build
cmake ..
make install
To use the installed library:
find_package(MorphIO REQUIRED)
target_link_libraries(mylib MorphIO::morphio)
Install as a Python package
The python binding can directly be installed using pip:
pip install morphio
Introduction
MorphIO is a library for reading and writing neuron morphology files. It supports the following formats:
- SWC
- ASC (aka. neurolucida)
- H5 v1
- H5 v2
It provides 3 C++ classes that are the starting point of every morphology analysis:
-
Soma
: contains the information related to the soma. -
Section
: a section is the succession of points between two bifurcations. To the bare minimum theSection
object will contain the section type, the position and diameter of each point. -
Morphology
: the morphology object contains general information about the loaded cell but also provides accessors to the different sections.
One important concept is that MorphIO is split into a read-only part and a read/write one.
Quick summary
C++ vs Python:
- C++ accessors become Python properties.
- style: C++ functions are camel case while Python ones are snake case.
Include/imports
- C++ mutable
#include <morphio/morphology.h>
#include <morphio/section.h>
#include <morphio/soma.h>
- Python mutable
from morphio import Morphology, Section, Soma
- C++ immutable
#include <morphio/mut/morphology.h>
#include <morphio/mut/section.h>
#include <morphio/mut/soma.h>
- Python immutable
from morphio.mut import Morphology, Section, Soma
Read-only API
The read-only API aims at providing better performances as its internal data representation is contiguous in memory. All accessors return immutable objects.
Internally, in this API the morphology object is in fact where all data are stored. The Soma and Section classes are lightweight classes that provide views on the Morphology data.
For more convenience, all section data are accessed through properties, such as:
points = section.points
diameters = section.diameters
C++
In C++ the API is available under the morphio/mut
namespace:
#include <morphio/mut/morphology.h>
#include <morphio/mut/section.h>
#include <morphio/mut/soma.h>
Python
In Python the API is available under the morphio.mut
module:
from morphio.mut import Morphology, Section, Soma
Mutable Read/Write API
C++
#include <morphio/morphology.h>
#include <morphio/section.h>
int main()
{
auto m = morphio::Morphology("sample.asc");
auto roots = m.rootSections();
auto first_root = roots[0];
// iterate on sections starting at first_root
for (auto it = first_root.depth_begin(); it != first_root.depth_end(); ++it) {
const morphio::Section §ion = *it;
std::cout << "Section type: " << section.type()
<< "\nSection id: " << section.id()
<< "\nParent section id: " << section.parent().id()
<< "\nNumber of child sections: " << section.children().size()
<< "\nX - Y - Z - Diameter";
for (auto i = 0u; i < section.points().size(); ++i) {
const auto& point = section.points()[i];
std::copy(point.begin(), point.end(), std::ostream_iterator<float>(std::cout, " "));
std::cout << '\n' << section.diameters()[i] << '\n';
}
std::cout << '\n';
}
}
Python
from morphio import Morphology
m = Morphology("sample.asc")
roots = m.rootSections
first_root = roots[0]
# iterate on sections starting at first_root
for section in first_root.iter():
print("Section type: {}".format(section.type))
print("Section id: {}".format(section.id))
print("Parent section id: {}".format(section.parent.id))
print("Number of child sections: {}".format(len(section.children)))
print("X - Y - Z - Diameter")
for point, diameter in zip(section.points, section.diameters):
print('{} - {}'.format(point, diameter))
Creating morphologies with the mutable API
Here is a simple example to create a morphology from scratch and write it to disk
#include <morphio/mut/morphology.h>
int main()
{
morphio::mut::Morphology morpho;
morpho.soma()->points() = {{0, 0, 0}, {1, 1, 1}};
morpho.soma()->diameters() = {1, 1};
auto section = morpho.appendRootSection(
morphio::Property::PointLevel(
{{2, 2, 2}, {3, 3, 3}}, // x,y,z coordinates of each point
{4, 4}, // diameter of each point
{5, 5}),
morphio::SectionType::SECTION_AXON); // (optional) perimeter of each point
auto childSection = section->appendSection(
morphio::Property::PointLevel(
{{3, 3, 3}, {4, 4, 4}},
{4, 4},
{5, 5}),
morphio::SectionType::SECTION_AXON);
// Writing the file in the 3 formats
morpho.write("outfile.asc");
morpho.write("outfile.swc");
morpho.write("outfile.h5");
}
Mutable Python
Reading morphologies
from morphio.mut import Morphology
m = Morphology("sample.asc")
roots = m.root_sections
first_root = roots[0]
# iterate on sections starting at first_root
for section in m.iter(first_root):
print("Section type: {}".format(section.type))
print("Section id: {}".format(section.id))
if not m.is_root(section):
print("Parent section id: {}".format(m.parent(section)))
print("Number of child sections: {}".format(len(m.children(section))))
print("X - Y - Z - Diameter")
for point, diameter in zip(section.points, section.diameters):
print('{} - {}'.format(point, diameter))
Creating morphologies
Here is a simple example to create a morphology from scratch and writing it to disk
from morphio.mut import Morphology
from morphio import SectionType, PointLevel
morpho = Morphology()
morpho.soma.points = [[0, 0, 0], [1, 1, 1]]
morpho.soma.diameters = [1, 1]
section = morpho.append_root_section(
PointLevel(
[[2, 2, 2], [3, 3, 3]], # x, y, z coordinates of each point
[4, 4], # diameter of each point
[5, 5]),
SectionType.axon) # (optional) perimeter of each point
child_section = section.append_section(
PointLevel(
[[3, 3, 3], [4, 4, 4]],
[4, 4],
[5, 5])) # section type is omitted -> parent section type will be used
morpho.write("outfile.asc")
morpho.write("outfile.swc")
morpho.write("outfile.h5")
Opening flags
When opening the file, modifier flags can be passed to alter the morphology representation. The following flags are supported:
morphio::NO_MODIFIER
: This is the default flag, it will do nothing.morphio::TWO_POINTS_SECTIONS
: Each section gets reduce to a line made of the first and last point.morphio::SOMA_SPHERE
: The soma is reduced to a sphere which is the center of gravity of the real soma.morphio::NO_DUPLICATES
: The duplicate point are not present. It means the first point of each section is no longer the last point of the parent section.morphio::NRN_ORDER
: Neurite are reordered according to the NEURON simulator ordering
Multiple flags can be passed by using the standard bit flag manipulation (works the same way in C++ and Python):
C++:
#include <morphio/Morphology.h>
Morphology("myfile.asc", options=morphio::NO_DUPLICATES|morphio::NRN_ORDER)
Python:
from morphio import Morphology, Option
Morphology("myfile.asc", options=Option.no_duplicates|Option.nrn_order)
Mitochondria
It is also possible to read and write mitochondria from/to the h5 files (SWC and ASC are not supported). As mitochondria can be represented as trees, one can define the concept of mitochondrial section similar to neuronal section and end up with a similar API. The morphology object has a mitochondria handle method that exposes the basic methods:
root_sections
: returns the section ID of the starting mitochondrial section of each mitochondrion.section(id)
: returns a given mitochondrial sectionappend_section
: creates a new mitochondrial section _depth_begin
: a depth first iterator _breadth_begin
: a breadth first iterator _upstream_begin
: an upstream iterator
from morphio.mut import Morphology
from morphio import MitochondriaPointLevel, PointLevel, SectionType
morpho = Morphology()
# A neuronal section that will store mitochondria
section = morpho.append_root_section(
PointLevel([[2, 2, 2], [3, 3, 3]], [4, 4], [5, 5]),
SectionType.axon)
# Creating a new mitochondrion
mito_id = morpho.mitochondria.append_section(
-1,
MitochondriaPointLevel([section.id, section.id], # section id hosting the mitochondria point
[0.5, 0.6], # relative distance between the start of the section and the point
[10, 20] # mitochondria diameters
))
# Appending a new mitochondrial section to the previous one
morpho.mitochondria.append_section(
mito_id, MitochondriaPointLevel([0, 0, 0, 0],
[0.6, 0.7, 0.8, 0.9],
[20, 30, 40, 50]))
# Iteration works the same as iteration on neuronal sections
first_root = morpho.mitochondria.root_sections[0]
for section_id in morpho.mitochondria.depth_begin(first_root):
section = morpho.mitochondria.section(section_id)
print('relative_path_length - diameter')
for relative_path_length, diameter in zip(section.diameters,
section.relative_path_lengths):
print("{} - {}".format(relative_path_length, diameter))
Reading mithochondria from H5 files:
from morphio import Morphology
morpho = Morphology("file_with_mithochondria.h5")
for mitochondrial_section in morpho.mitochondria.root_sections:
print('{neurite_id}, {relative_path_lengths}, {diameters}'.format(
neurite_id=mitochondrial_section.neurite_section_ids,
relative_path_lengths=mitochondrial_section.relative_path_lengths,
diameters=mitochondrial_section.diameters))
print("Number of children: {}".format(len(mitochondrial_section.children)))
Endoplasmic reticulum
Endoplasmic reticulum can also be stored and written to H5 file. The specification is part of the BBP morphology documentation There is one endoplasmic reticulum object per morphology. It contains 4 attributes. Each attribute is an array and each line refers to the value of the attribute for a specific neuronal section.
-
section_index: Each row of this dataset represents the index of a neuronal section. Each row of the other properties (eg. volume) refer to the part of the reticulum present in the corresponding section for each row.
-
volume: One column dataset indexed by section_index. Contains volumes of the reticulum per each corresponding section it lies in.
-
surface_area: Similar to the volume dataset, this dataset represents the surface area of the reticulum in each section in the section_index dataset.
-
filament_count: This 1 column dataset is composed of integers that represent the number of filaments in the segment of the reticulum lying in the section referenced by the corresponding row in the section_index dataset.
Reading endoplasmic reticula from H5 files
from morphio import Morphology
morpho = Morphology('/my/file')
reticulum = morpho.endoplasmic_reticulum
print('{indices}, {volumes}, {areas}, {counts}'.format(
indices=reticulum.section_indices,
volumes=reticulum.volumes,
areas=reticulum.surface_areas,
counts=reticulum.filament_counts))
Writing endoplasmic reticula from H5 files
neuron = Morphology()
reticulum = neuron.endoplasmic_reticulum
reticulum.section_indices = [1, 1]
reticulum.volumes = [2, 2]
reticulum.surface_areas = [3, 3]
reticulum.filament_counts = [4, 4]
neuron.write('/my/out/file.h5') # Has to be written to h5
Tips
Maximum number of warnings
On can control the maximum number of warnings using the command:
# Will stop displaying warnings after 100 warnings
morphio.set_maximum_warnings(100)
# Will never stop displaying warnings
morphio.set_maximum_warnings(-1)
# Warnings won't be displayed
morphio.set_maximum_warnings(0)
Specification
See https://github.com/BlueBrain/MorphIO/blob/master/doc/specification.md
Contributing
If you want to improve the project or you see any issue, every contribution is welcome. Please check the contribution guidelines for more information.
License
MorphIO is licensed under the terms of the GNU Lesser General Public License version 3. Refer to COPYING.LESSER and COPYING for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file MorphIO-2.3.10-cp38-cp38-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp38-cp38-manylinux1_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40c11566ddaaa5ed7a093ae917b388a13affa197bb928448773e4dade776b9b0 |
|
MD5 | 4b39da20e2e1fa169350a6b00b582c19 |
|
BLAKE2b-256 | 25ba7d33df49fe920dd13d1f45bb3c4d61b38ee224713959032711f57766c100 |
File details
Details for the file MorphIO-2.3.10-cp38-cp38-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp38-cp38-manylinux1_i686.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bdc7ea89fe5e2983729b74a323685d37312a5a863dbb804834aad158d57bf4d |
|
MD5 | 87617128bf0fd222e34e439285baff70 |
|
BLAKE2b-256 | ebf51acabdc463e3059ad91b7d14e601751f65b4ead85102c36a819b5630ce1b |
File details
Details for the file MorphIO-2.3.10-cp38-cp38-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp38-cp38-macosx_10_13_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.8, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2665acf54f5380fffedef10e0f97c0fe2530dccec1ade4ff490c0dc1577a29db |
|
MD5 | 825a177d9169f80ae51751c404ca864d |
|
BLAKE2b-256 | 528ef1667f03564b270454a5c42fee0367ee6df7a3f85a1fb7838f047d7c66c0 |
File details
Details for the file MorphIO-2.3.10-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 2.2 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe93c7cca19517dadc0ddb68810567137433e8453f2386db95fcdbb1a403f088 |
|
MD5 | b54e0bc8b118e317d805c5e73e965b8a |
|
BLAKE2b-256 | ad1ad40650942ba9de4bc845de7799089580a91421a4f083f6906e0136916a3e |
File details
Details for the file MorphIO-2.3.10-cp37-cp37m-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp37-cp37m-manylinux1_i686.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5074d611d73e3cafc05dbcf421b233c5dac58e1725ed442a066d8b7124e4ff64 |
|
MD5 | d3ca4f18e1d59438b9ab29d50e37679e |
|
BLAKE2b-256 | 847e0b27c43f83aacc7e4088e30ad61c0751797ad2483dd6142a0bc942d152a9 |
File details
Details for the file MorphIO-2.3.10-cp37-cp37m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp37-cp37m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.7m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf6a6fa51649b02427c5406bbf1adb2690eaa3da7dc6612527fb5bccd8b85ee4 |
|
MD5 | 91eb2a3a7eed149aca1b7888598b303a |
|
BLAKE2b-256 | 1adcaefbffcd5560a4ba75eb2d4d70cb1c68aec7d31d86902228394db44189d4 |
File details
Details for the file MorphIO-2.3.10-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 2.2 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28fdad4af6dcd1a2543f56ae295f8bd5bf0e97d3cb2f0f67a20153bc7273b54f |
|
MD5 | 22496b2296017f2067293e23877ce2c9 |
|
BLAKE2b-256 | 0a346c41530bed5498b33c9afaa43b0c3dbd5807e2fce2f25c9cc6ae2439a2ca |
File details
Details for the file MorphIO-2.3.10-cp36-cp36m-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp36-cp36m-manylinux1_i686.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12b40842cc86bde024d8439987d76d22bc3cac15fde9e011e43f3915d286dbc8 |
|
MD5 | cddf07dda6ecc8d8c239a63195e95d72 |
|
BLAKE2b-256 | 215248e636cd1c4160732de4ce7fca80504b5ec45e0c42a854c93d913b565e5b |
File details
Details for the file MorphIO-2.3.10-cp36-cp36m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp36-cp36m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.6m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0822ec0de5ea2deb5991b5d3888323de5cf5d2f6299e41e9c569ad26428eaaae |
|
MD5 | 7f07c431bfe696f1ecb881c7bb6b8edc |
|
BLAKE2b-256 | 5067e893ae275901e45efdad5c8cc8a7fbe1195c6359fcea730ec7b591c3e486 |
File details
Details for the file MorphIO-2.3.10-cp27-cp27mu-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp27-cp27mu-manylinux1_x86_64.whl
- Upload date:
- Size: 2.2 MB
- Tags: CPython 2.7mu
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01905dd720d7e640a42a09a7d7336e1c758bf1a11774b51daec4cb3c66d7ee53 |
|
MD5 | 8118bbe015920f8c6b20843777ef23b9 |
|
BLAKE2b-256 | ef41299ac7c2fc032ddc738deb386a0fa7e26a9e55f058b1503e7691d7133db6 |
File details
Details for the file MorphIO-2.3.10-cp27-cp27mu-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp27-cp27mu-manylinux1_i686.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 2.7mu
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 765ee6c450cb89f836d82bb53178c184b3b2063c2aee4b6ac4c93d3544c9ce15 |
|
MD5 | ffe8e85d180523f138b2233561c0fa6d |
|
BLAKE2b-256 | 650c2a2eb68d206e8e110c7c47483dd33dae61302239f1d46b44b8c10c9d8a16 |
File details
Details for the file MorphIO-2.3.10-cp27-cp27m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.10-cp27-cp27m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 2.7m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/2.7.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6ec9aea2c94c2ab9ec01b726e880f1f7d71a682764d1a9319cff2eb2a15c871 |
|
MD5 | ffd55d914a468e957a1ec6c4fc05e512 |
|
BLAKE2b-256 | 731e5f71adea9d0690614a46bad1b3a1c0fd80fd2b71661ee71d2e505ef39a3e |