Skip to content

Instantly share code, notes, and snippets.

@alexpreynolds
Last active February 15, 2018 05:21
Show Gist options
  • Select an option

  • Save alexpreynolds/4e22e012d121e2c7832967649f5a2a1e to your computer and use it in GitHub Desktop.

Select an option

Save alexpreynolds/4e22e012d121e2c7832967649f5a2a1e to your computer and use it in GitHub Desktop.
Convert a symmetric matrix to JSON for use with the Checkerboard web application
#!/usr/bin/env python
import sys
import json
ct = {}
ct_a = []
ctr = 0
for line in sys.stdin:
if ctr == 0:
ct_a = line.strip().split('\t')
else:
elems = line.strip().split('\t')
ct_k = elems[0]
ct_vs = elems[1:len(elems)]
ct_v_ctr = 0
for ct_vi, ct_v in enumerate(ct_vs):
if ct_v_ctr == 0:
ct[ct_k] = {}
try:
ct[ct_k][ct_a[ct_vi]] = float(ct_v)
if ct_a[ct_vi] not in ct:
ct[ct_a[ct_vi]] = {}
ct[ct_a[ct_vi]][ct_k] = float(ct_v)
except ValueError as ve:
if ct_v == 'NA':
ct[ct_k][ct_a[ct_vi]] = None # encode NAs as 'null' in JSON output
ct_v_ctr +=1
ctr += 1
sys.stdout.write("%s\n" % (json.dumps(ct)))
@alexpreynolds

alexpreynolds commented May 11, 2017

Copy link
Copy Markdown
Author

Checkerboard overview:

  1. Once you paste the JSON string into the Import column of the Checkerboard app, click on the "Import" button to bring it into the application.
  2. In the Categories column, click on the "Select All" button to select all the cell types. Deselect any you don't want by clicking on their checkboxes.
  3. In the Ordering column, drag-and-drop the "hamburger" symbol next to each cell type up or down, to re-order the cell-types as desired.
  4. Click on the "Render" button to render a plot.
  5. In the "Figure" column, click "Export" to export a plot to an SVG file. This can be imported into Adobe Illustrator or Inkscape.

@alexpreynolds

Copy link
Copy Markdown
Author

Here's an example of a figure from the JSON string posted above:

checkerboard_fig_2017-5-11_0 1 55 101

@alexpreynolds

Copy link
Copy Markdown
Author

An example of what the Checkerboard application looks like when adding data, selecting cell types (or samples), and ordering them before rendering a plot:

checkerboard_screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment