Last active
February 15, 2018 05:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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))) |
Checkerboard overview:
- 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.
- 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.
- 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.
- Click on the "Render" button to render a plot.
- In the "Figure" column, click "Export" to export a plot to an SVG file. This can be imported into Adobe Illustrator or Inkscape.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example of a tab-delimited matrix, showing a grid of pairwise metrics (0 <= s[ij] <= 1) for some set of samples (cell types, etc.):
Here's the result of converting this matrix to JSON:
This string can be cut-and-paste into the Checkerboard application at: https://tools.altiusinstitute.org/checkerboard/