From 14672e0318e176af295cd213fe5cd846edfce631 Mon Sep 17 00:00:00 2001 From: Syed Hussain Ather Date: Fri, 14 Jun 2024 15:52:35 -0400 Subject: [PATCH] Add SWC to NeuroML converter and update documentation --- converters/swc_to_nml.py | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 converters/swc_to_nml.py diff --git a/converters/swc_to_nml.py b/converters/swc_to_nml.py new file mode 100644 index 00000000..e5e97595 --- /dev/null +++ b/converters/swc_to_nml.py @@ -0,0 +1,46 @@ +import neuroml +from neuroml import Segment, SegmentGroup, Morphology, Cell +import neuroml.writers as writers + +def parse_swc(file_path): + with open(file_path, 'r') as file: + lines = file.readlines() + + segments = [] + for line in lines: + if not line.startswith('#'): + parts = line.strip().split() + if len(parts) == 7: + id, type, x, y, z, radius, parent = map(float, parts) + segment = Segment( + id=int(id), + parent=int(parent) if int(parent) != -1 else None, + proximal=None, + distal=None, + name=f"type_{int(type)}", + group=f"custom_{int(type)}" if int(type) > 3 else None + ) + segments.append(segment) + + return segments + +def create_neuroml(segments, output_path): + cell = Cell(id="swc_cell") + morphology = Morphology(id="morph") + cell.morphology = morphology + + for segment in segments: + morphology.segments.append(segment) + + doc = neuroml.NeuroMLDocument(id="swc_to_nml") + doc.cells.append(cell) + + writers.NeuroMLWriter.write(doc, output_path) + print(f"NeuroML file written to {output_path}") + +if __name__ == "__main__": + swc_file = "path/to/your/input.swc" + nml_file = "path/to/your/output.nml" + segments = parse_swc(swc_file) + create_neuroml(segments, nml_file) +