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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example of a figure from the JSON string posted above: