Home / Project Blogs /Swanson Data Exporter

Goal

Discuss a script I wrote that runs inside Adobe Illustrator and exports individual svg (cell bodies) and png (fibers) files.

Why

The reason it was necessary to develop the script is tied to Vanessa's paper. When we first started exploring the isopleth contours to visualize cell body and fiber densities, I made the mistake of suggesting we do the analysis on the cropped-out frames that were being used in the artboards of the final paper. I suggested this because at the time it felt like we were going to have to do more work if I ran the analysis on the large map and later had to crop each isopleth map to fit the artboard figures. Because of this, we exported all 500+ data files (SVGs and PNGs) with the cropped-out dimensions. I did notice that in the cropped-out AI files, some data (cell bodies and fiber tracings) fell outside of the cropped-out artboard, though it was not an issue since the density estimation was going to have a negligible difference.

Later, when we started to discuss the choropleth maps, we noticed that the brain regions also fell outside the artboard. This was an issue because we wanted to report data coverage divided by brain region area. Since my program only ran an analysis on what was inside the cropped-out artboard, the areal reports for some brain regions were inaccurate. To fix this, Dr. Khan and I brainstormed on our options and concluded that we would redo the three levels (out of 7). This meant I would have to export all data files (SVGs and PNGs) for those levels which amounted to about 200 individual files.

A bit overwhelmed with the task, I scoured the interwebs to find a solution, maybe a way I can automate the process without me having to save/name each individual file. I was aware of the existence of scripts in Adobe Illustrator, I have used scripts in other projects, but I had never written scripts myself. Nonetheless, I figured developing the script to automate the process would be the best investment of time and that's how the script came to fruition. Kind of surprised I figured it out.

The Script: How To

You can find the script on GitHub. I will first explain the programmatic logic behind the script and then explain how one would use the script.

Logic


The script expects a specific layout/hierarchy of the layers inside Adobe Illustrator, organized as follows:

1) Case number w peptide acronym like “18 016 aMCH nNOS”

a) Peptide Acronym like “aMCH”

i) “Fibers”

ii) “Cell Bodies”

iii) Any other data layer such as “Appositions”

b) Peptide Acronym like “nNOS”

i) “Fibers”

ii) “Cell Bodies”

iii) Any other data layer such as “Appositions”

In this example, “a)” is a sublayer inside layer “1)” and so on. Layers “i)” – “iii)” contain individual vector objects of the class (eg. circles for cell bodies and paths for fibers).


Currently, the script will first search for peptide acronyms from a list of hard-coded acronyms in the name of all the highest layers or layers of rank 1. Hard coding, or fixing parameters (the acronyms) in a program in such a way that they cannot be altered without modifying the program, is not good practice when developing software, but for now, it does the trick. I don’t worry too much about this because many things can change which would get rid of hard coding. Currently the acronyms it looks for are "aMSH", "HO", "nNOS", "MCH", "Copeptin" since they are the ones used in Vanessa’s paper. These are case-sensitive.


Then the program will look for the acronyms again in the names of the sublayers or layers of the second rank in the layer hierarchy. If it finds a match it will look for a sublayer, or layer of the third tank, named “fibers” or “cell bodies” (not case sensitive), toggle on the visibility of that sublayer, and save the artboard as PNG or SVG depending on if it’s “fibers” or “cell bodies” respectively. The added benefit is that the file will be saved under the name "caseNumber_peptide_lvl##.png" automatically which is the file naming convention expected by the program I wrote that does the isopleth maps and so on.


This sequence repeats until all first layer names have been scanned for the list of peptide acronyms.


I need to meet with Dr. Khan to go over the naming convention which could change the logic of the program a bit. I feel we can come up with a more general solution that will work for this and future datasets.


How To


As it stands, the script will only work if layers of rank 1, 2, and 3 (i.e. “1)”, “a)”, and “i)”) are made invisible, meaning the little eye is toggled off, and layers of rank 4 (all the cell body and fiber objects) are made visible. Once this is done, we can proceed to run the script.


Running scripts in Adobe Illustrator is pretty straightforward. Once you download the script from GitHub, you navigate in Adobe Illustrator to File>Scripts>Other Scripts... and when prompted, you will search and select the script file you downloaded. That's it. Alternatively, you can install the script so that it will be in the list of scripts when you navigate to File>Scripts, but that is beyond the scope of this blog.


I will post a tutorial video here once Dr. Khan and I rough out the edges of the process.

How To Update

Sept 13 2021 - There have been significant changes to the workflow since the previous blog post, including some modifications to the "Swanson Data Exporter" and the addition of a new script "Swanson Shape Exporter"

Logic


The logic for the data export script remains identical:

1) Case number w peptide acronym like “18 016 aMCH nNOS”

a) Peptide Acronym like “aMCH”

i) “Fibers”

ii) “Cell Bodies”

iii) Any other data layer such as “Appositions”

b) Peptide Acronym like “nNOS”

i) “Fibers”

ii) “Cell Bodies”

iii) Any other data layer such as “Appositions”

In this example, “a)” is a sublayer inside layer “1)” and so on. Layers “i)” – “iii)” contain individual vector objects of the class (eg. circles for cell bodies and paths for fibers). Finally, I'm only using a numbered list to differentiate among the sublayers but there is no particular order inside any given sublayer (eg. "Fibers" can go after "Cell Bodies" or vise versa).


How To


  1. Create a folder on your computer for the project. In it, create a folder to store all the master Illustrator files, one folder to store all the scripts, and then folders for each type of data you'll be exporting (e.g. fibers, cells, appositions, etc).

  2. Download the master Illustrator files containing the data. Make sure the AI files follow the logic mentioned above and that you include empty layers for cases where "no data" is significant for the project. For example, if your project is a study across levels 20-30 and you map cell bodies in levels 20 to 27 in one replicate of the study, you would still include layers (empty) in the Illustrator templates for level 28-30.

  3. Download the SwansonDataExporter.js file from GitHub. The script currently exports cell bodies fibers and appositions into folders named "cells", "fibers", and "appositions" respectively. These names need to match with the names of the folders you created in step one.

  4. Move files to their respective folders you created in step one.

  5. Open an Illustrator file.

  6. With AI open, run the SwansonDataExporter.js script. Navigate to File>Scripts>Other Scripts... and when prompted, search and select the script file inside your scripts folder. Alternatively, you can install the script so that it will be in the list of scripts when you navigate to File>Scripts.

  7. Repeat steps 5 and 6 until finished.


I will post a tutorial video here once Dr. Khan and I rough out the edges of the process.