Skip to content

Instantly share code, notes, and snippets.

@alexpreynolds
Last active February 15, 2018 05:21
Show Gist options
  • Save alexpreynolds/4e22e012d121e2c7832967649f5a2a1e to your computer and use it in GitHub Desktop.
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
Copy link
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
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