Skip to content

Instantly share code, notes, and snippets.

@sdouglas
Created December 6, 2011 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdouglas/1439369 to your computer and use it in GitHub Desktop.
Save sdouglas/1439369 to your computer and use it in GitHub Desktop.
Parses a cadnano file and prints the [helix, index] for each base in all strands.
#!/usr/bin/env python
# encoding: utf-8
"""
parseJson.py
Created by Shawn M. Douglas on 2011-12-06.
"""
import sys
import os
import json
filename = "nanorobot.v2.json"
def main():
file = open(filename, 'r')
lines = file.readlines()
str = ""
for line in lines:
str += line
obj = json.loads(str)
strands = obj["vstrands"]
name = obj["name"]
# create dictionaries (keyed by virtual helix #) of
# row/col, scaf array, stap array
vhToScaf = {}
vhToStap = {}
vhNums = []
for strand in strands:
num = strand["num"]
vhNums.append(num)
# row = strand["row"]
# col = strand["col"]
scaf = strand["scaf"]
stap = strand["stap"]
vhToScaf[num] = scaf
vhToStap[num] = stap
# extract scaffold 5' endpoints
scafEnds = []
for vh in vhNums:
scaf = vhToScaf[vh]
for i in range(len(scaf)):
base = scaf[i]
if (base[1] == -1) & (base[3] != -1):
scafEnds.append([vh, i])
# extract staple 5' endpoints
stapEnds = []
for vh in vhNums:
stap = vhToStap[vh]
for i in range(len(stap)):
base = stap[i]
if (base[1] == -1) & (base[3] != -1):
stapEnds.append([vh, i])
# extract scaffold paths, starting at 5' endpoints
scafPaths = []
for scafEnd in scafEnds:
path = []
[curr_vh, curr_base] = scafEnd
[next_vh, next_base] = vhToScaf[curr_vh][curr_base][2:4]
while next_base != -1:
path.append([curr_vh,curr_base])
[curr_vh, curr_base] = [next_vh, next_base]
[next_vh, next_base] = vhToScaf[curr_vh][curr_base][2:4]
path.append([curr_vh,curr_base]) # append last base
scafPaths.append(path)
# extract staple paths, starting at 5' endpoints
stapPaths = []
for stapEnd in stapEnds:
path = []
[curr_vh, curr_base] = stapEnd
[next_vh, next_base] = vhToStap[curr_vh][curr_base][2:4]
while next_base != -1:
path.append([curr_vh,curr_base])
[curr_vh, curr_base] = [next_vh, next_base]
[next_vh, next_base] = vhToStap[curr_vh][curr_base][2:4]
path.append([curr_vh,curr_base]) # append last base
stapPaths.append(path)
print "Scaffold Strands"
for i in range(len(scafPaths)):
print i, scafPaths[i]
print "Staple Strands"
for i in range(len(stapPaths)):
print i, stapPaths[i]
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment