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
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 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 bifurcation. To the bare minimum the Section object will contain the section type, the position and diameter of each point.
-
Morphology: the morphology object will contain general information about the loaded cell but will also provide accessors to the different section.
One important concept is that MorphIO is splitted 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
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() << std::endl;
std::cout << "Section id: " << section.id() << std::endl;
std::cout << "Parent section id: " << section.parent().id() << std::endl;
std::cout << "Number of child sections: " << section.children().size() << std::endl;
std::cout << "X - Y - Z - Diameter" << std::endl;
for(int i = 0; i<section.points().size(); ++i) {
std::cout <<
section.points()[i][0] << ' ' <<
section.points()[i][1] << ' ' <<
section.points()[i][2] << ' ' <<
section.diameters()[i] << std::endl;
}
std::cout << std::endl;
}
}
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 writing 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};
std::shared_ptr<morphio::mut::Section> 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
std::shared_ptr<morphio::mut::Section> 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 section
- append_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)))
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
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.2-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78960e1ea42e2aba749822303be1fbc4357706f2ed737a4298924dbfccb14894 |
|
MD5 | 4dee355871fa607745c7e4f004bfa6be |
|
BLAKE2b-256 | b7545d0385d13e7b672a19c7cbd141af7362fe341ecf56fba48cfcaef57e0678 |
File details
Details for the file MorphIO-2.3.2-cp37-cp37m-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp37-cp37m-manylinux1_i686.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87a6ed21b8f12e39515004ac2526e169b69c2bccc7cb568336184a573cb9ad19 |
|
MD5 | c91812eeb63514a4145d2c58de4d0fd3 |
|
BLAKE2b-256 | 578329dc0addf8443980e2faeaae3d729914bf522bc6b07b03617cc391a8d972 |
File details
Details for the file MorphIO-2.3.2-cp37-cp37m-macosx_10_6_intel.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp37-cp37m-macosx_10_6_intel.whl
- Upload date:
- Size: 494.9 kB
- Tags: CPython 3.7m, macOS 10.6+ intel
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b535efefa3478b189f276e102828a4676895bfa5c3d4f93d091abb54edd8adae |
|
MD5 | 73dc5bf4b34e4ff68bd8f8ae738e2ad6 |
|
BLAKE2b-256 | 9fdae1f73c8957f71c4fd9136c16332803e698d77385fb6161caf2a94240c87d |
File details
Details for the file MorphIO-2.3.2-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6209b260c0a33c838575b2037267d0424b5ddeef062010649a7a78e8cc655313 |
|
MD5 | 967f6d30e2410844454084862566388c |
|
BLAKE2b-256 | 942275788e8b3f517b70cbf291f805579e3285a77b4a069c4251e883a73779b2 |
File details
Details for the file MorphIO-2.3.2-cp36-cp36m-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp36-cp36m-manylinux1_i686.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d52ed7714e4754861346d4c6341a26a692ed2a275de302f50b8c5d6a4bc271f |
|
MD5 | c56304cbd41a8e42b6529caef3c44966 |
|
BLAKE2b-256 | b752294082296632f71dcbe72f7c2600e40a085fded24971d74662a29aa2fe39 |
File details
Details for the file MorphIO-2.3.2-cp36-cp36m-macosx_10_6_intel.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp36-cp36m-macosx_10_6_intel.whl
- Upload date:
- Size: 494.9 kB
- Tags: CPython 3.6m, macOS 10.6+ intel
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4debc50a9b909e296bcde97a08ff3c2e477faf81ec4c0ed746d7f8bcab0854b |
|
MD5 | 7ef534ed4e5aff449ee38856d0c46f32 |
|
BLAKE2b-256 | 53d90b6b4e2687e6bda838ff91b61d8bb21ce86b52d5c1c618065bf37121a3db |
File details
Details for the file MorphIO-2.3.2-cp27-cp27mu-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp27-cp27mu-manylinux1_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 2.7mu
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa00afa308051fcfed23e52707604fec6768bca7e13939f1a7bf819fe24a3e54 |
|
MD5 | 3cec56aeacbf411407e1dd70753e59ca |
|
BLAKE2b-256 | 6c9e7f7e05bf446b7014f226a7cdb9b7ffadab56389ed6e683c6e39275f7d4e2 |
File details
Details for the file MorphIO-2.3.2-cp27-cp27mu-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp27-cp27mu-manylinux1_i686.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 2.7mu
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d63165165ca0efb729c432e1a5e340a75f01592efba93d0bdfd561e714f97fd |
|
MD5 | 18624f8fc8435038745c8fd39a21fb22 |
|
BLAKE2b-256 | 961a9ca5f75859cc149acd714aaa1106d6360e4d74b97a9981bee922977b8c7b |
File details
Details for the file MorphIO-2.3.2-cp27-cp27m-manylinux1_x86_64.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp27-cp27m-manylinux1_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 2.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb376517634d326cd8e4ab56428b71563b1a4b7cf640ad2271fa88e7dcc88233 |
|
MD5 | e4d658ca295f3d40ba349c8e70189d62 |
|
BLAKE2b-256 | 7b6fa2f78d9eb733fa32e7e8c3d666c395a41d8f47c3646f27f3bf5c9e89e1cf |
File details
Details for the file MorphIO-2.3.2-cp27-cp27m-manylinux1_i686.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp27-cp27m-manylinux1_i686.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 2.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4f2dad4d954dac6430a60c4bf08a374890398e22b457c821404f25344b9c990 |
|
MD5 | ae2c0dff0f1d4e164c3d64ac12b25a10 |
|
BLAKE2b-256 | 453bb4a2d8ce3f538f69ae5c06fa154c64eeac2716333c8bcec04e10fadbe04a |
File details
Details for the file MorphIO-2.3.2-cp27-cp27m-macosx_10_6_intel.whl
.
File metadata
- Download URL: MorphIO-2.3.2-cp27-cp27m-macosx_10_6_intel.whl
- Upload date:
- Size: 498.3 kB
- Tags: CPython 2.7m, macOS 10.6+ intel
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/2.7.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a8ed5ca9a677cc1c17caa2336efbc0b6915e2ae8c0654af48f5ef95861be4d6 |
|
MD5 | 0c04a56e6cb86b5f3462d38de2fca7d8 |
|
BLAKE2b-256 | dacdaa064ce325dafc7dbd63a375fc59ed938d7b76087bab34a3e8162df827bc |