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

alexpreynolds commented May 11, 2017

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.):

$ more matrix.txt
        NCI-H187        NCI-H1930       NCI-H2029       NCI-H2081       NCI-H446        NCI-H510A       NCI-H524        NCI-H526        SHP-77
NCI-H187        1       0.490257        0.483636        0.502391        0.390506        0.54837 0.40433 0.474518        0.505722
NCI-H1930       0.490257        1       0.423023        0.552522        0.399871        0.452782        0.376301        0.470975        0.519897
NCI-H2029       0.483636        0.423023        1       0.511018        0.378581        0.563623        0.337071        0.39593 0.518829
NCI-H2081       0.502391        0.552522        0.511018        1       0.435448        0.501634        0.385037        0.458029        0.534584
NCI-H446        0.390506        0.399871        0.378581        0.435448        1       0.367828        0.402817        0.36178 0.415591
NCI-H510A       0.54837 0.452782        0.563623        0.501634        0.367828        1       0.374224        0.442611        0.553995
NCI-H524        0.40433 0.376301        0.337071        0.385037        0.402817        0.374224        1       0.360606        0.406875
NCI-H526        0.474518        0.470975        0.39593 0.458029        0.36178 0.442611        0.360606        1       0.459958
SHP-77  0.505722        0.519897        0.518829        0.534584        0.415591        0.553995        0.406875        0.459958        1

Here's the result of converting this matrix to JSON:

$ ./mtx2json.py < ./matrix.txt 
{"SHP-77": {"NCI-H1930": 0.519897, "NCI-H524": 0.406875, "NCI-H446": 0.415591, "NCI-H526": 0.459958, "NCI-H2081": 0.534584, "NCI-H2029": 0.518829, "NCI-H510A": 0.553995, "NCI-H187": 0.505722}, "NCI-H1930": {"NCI-H1930": 1.0, "NCI-H524": 0.376301, "NCI-H446": 0.399871, "NCI-H526": 0.470975, "NCI-H2081": 0.552522, "NCI-H2029": 0.423023, "NCI-H510A": 0.452782, "NCI-H187": 0.490257}, "NCI-H524": {"NCI-H1930": 0.376301, "NCI-H524": 1.0, "NCI-H446": 0.402817, "NCI-H526": 0.360606, "NCI-H2081": 0.385037, "NCI-H2029": 0.337071, "NCI-H510A": 0.374224, "NCI-H187": 0.40433}, "NCI-H446": {"NCI-H1930": 0.399871, "NCI-H524": 0.402817, "NCI-H446": 1.0, "NCI-H526": 0.36178, "NCI-H2081": 0.435448, "NCI-H2029": 0.378581, "NCI-H510A": 0.367828, "NCI-H187": 0.390506}, "NCI-H526": {"NCI-H1930": 0.470975, "NCI-H524": 0.360606, "NCI-H446": 0.36178, "NCI-H526": 1.0, "NCI-H2081": 0.458029, "NCI-H2029": 0.39593, "NCI-H510A": 0.442611, "NCI-H187": 0.474518}, "NCI-H2081": {"NCI-H1930": 0.552522, "NCI-H524": 0.385037, "NCI-H446": 0.435448, "NCI-H526": 0.458029, "NCI-H2081": 1.0, "NCI-H2029": 0.511018, "NCI-H510A": 0.501634, "NCI-H187": 0.502391}, "NCI-H2029": {"NCI-H1930": 0.423023, "NCI-H524": 0.337071, "NCI-H446": 0.378581, "NCI-H526": 0.39593, "NCI-H2081": 0.511018, "NCI-H2029": 1.0, "NCI-H510A": 0.563623, "NCI-H187": 0.483636}, "NCI-H510A": {"NCI-H1930": 0.452782, "NCI-H524": 0.374224, "NCI-H446": 0.367828, "NCI-H526": 0.442611, "NCI-H2081": 0.501634, "NCI-H2029": 0.563623, "NCI-H510A": 1.0, "NCI-H187": 0.54837}, "NCI-H187": {"NCI-H1930": 0.490257, "NCI-H524": 0.40433, "NCI-H446": 0.390506, "NCI-H526": 0.474518, "NCI-H2081": 0.502391, "NCI-H2029": 0.483636, "NCI-H510A": 0.54837, "NCI-H187": 1.0}}

This string can be cut-and-paste into the Checkerboard application at: https://tools.altiusinstitute.org/checkerboard/

@alexpreynolds
Copy link
Author

alexpreynolds commented May 11, 2017

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
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