Converting .swc traces to ROIs from a script

Hi,

I would like, within a script, to read traces previously generated from Simple Neurite Tracer and store them as ROIs for post-tracing analysis. However, the coordinates of the ROIs generated using “File > Import > Tracings” (from Neuroanatomy site) are different from those in the .swc files. I am attaching a simplified example where the .swc trace is properly overlaid on the image when loaded from the SNT interface but not when imported, e.g. with something like

run("Tracings (Traces/(e)SWC)...", "open=C:\\trace.swc apply_spatial voxel_width=1 voxel_height=1 voxel_depth=1 unit=pixel x_offset=0 y_offset=0 z_offset=0 x_scale=1 y_scale=1 z_scale=1 render=[2D ROIs (stored in ROI Manager)]");

What am I missing? Suggestions on how to accomplish this task?

Thanks,
Giovanni

== test image ==

== results from load in SNT and import ==

== trace.swc ==

# Exported from "Simple Neurite Tracer" version 3.1.4 on 2017-06-27T11:13:02.111
# https://imagej.net/Simple_Neurite_Tracer
#
# All positions and radii in pixels
# WARNING: Usage of pixel coordinates does not respect the SWC specification
#
1 0 186.0 55.0 0.0 0.0 -1
3 0 63.0 191.0 0.0 0.0 1
1 Like

I’m pinging you again @tferr on this one! :wink: In case you have some ideas…

@gcardone,

Does the offset occur if you save the path as a .traces file (not .swc) and import it back using the “Tracings (Traces/(e)SWC)…” command?

@tferr,

there is no offset when using the .trace format. It looks like the coordinates in the .swc files are shifted in order to move the trace to the upper left corner corner of the image, which is confusing because I expected to be able to control the offset from the gui of Import Tracings.

Addendum: I gave a look at the code and it seems that things happen in calculateCanvasDimensions,called by autoLoadSWC, where the coordinates are adjusted according to a guessed offset. Is this initialization needed when asking to render traces as ROIs?

Yes and no. It shouldn’t be required in principle, but SNT was designed from scratch to always use an image as input. What the the Tracings (Traces/(e)SWC)… command tries to accomplish is to feed SNT a bunch of guessed details so that an image is no longer needed. The issue gets more convoluted with SWC files, because such files have hardly have any metadata in them. There was a closed issue for a related problem. I just reopened it, as it is obviously not fixed.

https://github.com/tferr/hIPNAT/issues/5

It would be great if you could fix this bug, as my time right now is quite limited. You could start by looking at the comments in the issue above as a starting point. I’ve probably overlooked some detail. That being said, there are plans to rewrite some parts of SNT so that input images are not requirements for operations that deal exclusively with traced data.

In the interim, my suggestions are twofold: 1) use .traces files for now, as ROIs can be extracted from them without problems, or 2) consider scripting SNT directly using its API. There are a couple of examples in the the Neuroanatomy repository, that may be of help:

If you share what you currently have, or what you have tried so far we could probably help.

2 Likes

@tferr,

thank you for the explanation and the suggestions. I have multiple tracings from different images, either stored as .traces or a .swc, and I want to overlap them to the result from thresholding to infer additional information. First I started by the examples in jython from the Neuroanatomy site in order to try to convert them to skeletons, but I could not get the .swc traces properly located: being loaded one by one, they all end up overlapping to each other. Then I tried the Tracing command, which did not work either, since it also relies on the autoLoadSWC function. Then I wrote to the forum…

At this point I will give another look at the code and see what fix I can propose.
In the meantime, if you have suggestions on how to load a bunch of .swc files from within SNT, that would be great. To my knowledge, you can only load them one by one.

Thanks again!

Hi @gcardone, sorry for the long delay. With the latest SNT (available through the Neuroanatomy update site), it is now possible to load a directory of .swc files using File > Import > Directory of SWCs… Additionally, one can simply drag a directory of .swc, .json, or .traces files onto either the SNT dialog or the Path Manager dialog, and the import sequence will begin automatically!

To follow up on @arshadic post: In addition to being able to convert traces to ROIs from the GUI (see documentation), conversion can also be tackled programmatically. Once SNT is installed, you will find an example script in the Script Editor under (Templates> Neuroanatomy> Analysis> Convert Reconstruction To ROIs). It takes an SWC file and associated image (a toy neuron bundled with SNT) and converts its center-lines, end-points and branch-points to functional ROIs. Here is the output:

Paths (blue) converted to polyline ROIs. End-points (magenta) and branch-points (orange) converted to single-point ROIs. Note that line thickness reflects node radii.

1 Like