Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SWC to NeuroML converter and update documentation #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions converters/swc_to_nml.py
Original file line number Diff line number Diff line change
@@ -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)