Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ergonomics improvements #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
*.png
*.svg
!example.png
7 changes: 4 additions & 3 deletions draw_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import draw_rna as d
from __future__ import absolute_import, division, print_function
from . import draw_rna as d
import argparse
import sys
import os
Expand Down Expand Up @@ -27,15 +28,15 @@ def main():
seq = next(f).strip()
secstruct = next(f).strip()
except Exception as e:
print e
print(e)
raise ValueError('improper format')

try:
col = next(f).strip()
except:
col = None

print 'drawing %s' % name
print('drawing %s' % name)
if col:
d.draw_rna(seq, secstruct, col, name, line=args.line)
else:
Expand Down
18 changes: 11 additions & 7 deletions draw_rna.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import render_rna_flip as render_rna
import svg
import inv_utils
from __future__ import absolute_import, division, print_function
from . import render_rna
from . import svg
from . import inv_utils
import argparse
import re
from matplotlib import cm
Expand Down Expand Up @@ -31,15 +32,15 @@
#"i": [0, 204, 153],
#"h": [46, 184, 46]}

def draw_rna(sequence, secstruct, colors, filename="secstruct", line=False):
def draw_rna(sequence, secstruct, colors, filename="secstruct", line=False, square=True, flipped=True, draw_pairs=True):
r = render_rna.RNARenderer()

pairmap = render_rna.get_pairmap_from_secstruct(secstruct)
pairs = []
for i in range(len(pairmap)):
if pairmap[i] > i:
pairs.append({"from":i, "to":pairmap[i], "p":1.0, "color":COLORS["e"]})
r.setup_tree(secstruct, NODE_R, PRIMARY_SPACE, PAIR_SPACE)
r.setup_tree(secstruct, NODE_R, PRIMARY_SPACE, PAIR_SPACE, False)
size = r.get_size()

cell_size = max(size) + CELL_PADDING * 2
Expand All @@ -55,8 +56,11 @@ def draw_rna(sequence, secstruct, colors, filename="secstruct", line=False):
except:
colors = [COLORS[x] for x in list(colors)]

svgobj = svg.svg("%s.svg" % filename, cell_size, cell_size)
r.draw(svgobj, CELL_PADDING, CELL_PADDING, colors, pairs, sequence, RENDER_IN_LETTERS, line)
width = cell_size if square else size[0]
height = cell_size if square else size[1]

svgobj = svg.svg("%s.svg" % filename, width, height)
r.draw(svgobj, CELL_PADDING, CELL_PADDING, colors, pairs if draw_pairs else None, sequence, RENDER_IN_LETTERS, line)

def parse_colors(color_string):
colorings = color_string.strip().split(",")
Expand Down
1 change: 1 addition & 0 deletions inv_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import, division, print_function
import os
import random, string
import re
Expand Down
78 changes: 34 additions & 44 deletions render_rna.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, division, print_function
import sys
import svg
from . import svg
import re
import random
import math
Expand Down Expand Up @@ -54,16 +55,15 @@ def get_pairmap_from_secstruct(secstruct):
pairs_array[pair_stack[ii]] = end_stack[-ii]
pairs_array[end_stack[-ii]] = pair_stack[ii]
else:
print "ERROR: pairing incorrect %s" % secstruct
print("ERROR: pairing incorrect %s" % secstruct)

return pairs_array


def add_nodes_recursive(bi_pairs, rootnode, start_index, end_index):

if(start_index > end_index) :
print ("Error occured while drawing RNA %d %d" % (start_index, end_index))
sys.exit(0)
raise RuntimeError("Error occured while drawing RNA %d %d" % (start_index, end_index))

if(bi_pairs[start_index] == end_index) :

Expand Down Expand Up @@ -91,7 +91,7 @@ def add_nodes_recursive(bi_pairs, rootnode, start_index, end_index):

rootnode.children_.append(newnode)

def setup_coords_recursive(rootnode, parentnode, start_x, start_y, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE):
def setup_coords_recursive(rootnode, parentnode, start_x, start_y, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED):

cross_x = -go_y
cross_y = go_x
Expand All @@ -106,11 +106,11 @@ def setup_coords_recursive(rootnode, parentnode, start_x, start_y, go_x, go_y, N
rootnode.y_ = start_y

if(rootnode.children_[0].is_pair_):
setup_coords_recursive(rootnode.children_[0], rootnode, start_x + go_x * PRIMARY_SPACE, start_y + go_y * PRIMARY_SPACE, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE)
setup_coords_recursive(rootnode.children_[0], rootnode, start_x + go_x * PRIMARY_SPACE, start_y + (-1 if FLIPPED else 1) * go_y * PRIMARY_SPACE, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED)
elif(rootnode.children_[0].is_pair_ == False and rootnode.children_[0].index_a_ < 0):
setup_coords_recursive(rootnode.children_[0], rootnode, start_x, start_y, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE)
setup_coords_recursive(rootnode.children_[0], rootnode, start_x, start_y, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED)
else:
setup_coords_recursive(rootnode.children_[0], rootnode, start_x + go_x * PRIMARY_SPACE, start_y + go_y * PRIMARY_SPACE, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE)
setup_coords_recursive(rootnode.children_[0], rootnode, start_x + go_x * PRIMARY_SPACE, start_y + (-1 if FLIPPED else 1) * go_y * PRIMARY_SPACE, go_x, go_y, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED)

elif(len(rootnode.children_) > 1) :

Expand All @@ -128,7 +128,7 @@ def setup_coords_recursive(rootnode, parentnode, start_x, start_y, go_x, go_y, N
rootnode.y_ = go_y * circle_radius
else :
rootnode.x_ = parentnode.x_ + go_x * circle_radius
rootnode.y_ = parentnode.y_ + go_y * circle_radius
rootnode.y_ = parentnode.y_ + (-1 if FLIPPED else 1) * go_y * circle_radius

for ii in range(0,len(rootnode.children_)):

Expand All @@ -139,13 +139,13 @@ def setup_coords_recursive(rootnode, parentnode, start_x, start_y, go_x, go_y, N

rad_angle = length_walker/circle_length * 2 * math.pi - math.pi / 2.0
child_x = rootnode.x_ + math.cos(rad_angle) * cross_x * circle_radius + math.sin(rad_angle) * go_x * circle_radius
child_y = rootnode.y_ + math.cos(rad_angle) * cross_y * circle_radius + math.sin(rad_angle) * go_y * circle_radius
child_y = rootnode.y_ + (-1 if FLIPPED else 1) * math.cos(rad_angle) * cross_y * circle_radius + (-1 if FLIPPED else 1) * math.sin(rad_angle) * go_y * circle_radius

child_go_x = child_x - rootnode.x_
child_go_y = child_y - rootnode.y_
child_go_len = math.sqrt(child_go_x * child_go_x + child_go_y * child_go_y)

setup_coords_recursive(rootnode.children_[ii], rootnode, child_x, child_y, child_go_x / child_go_len, child_go_y / child_go_len, NODE_R, PRIMARY_SPACE, PAIR_SPACE)
setup_coords_recursive(rootnode.children_[ii], rootnode, child_x, child_y, child_go_x / child_go_len, (-1 if FLIPPED else 1) * child_go_y / child_go_len, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED)

if(rootnode.children_[ii].is_pair_) :
length_walker += PAIR_SPACE / 2.0
Expand All @@ -154,22 +154,22 @@ def setup_coords_recursive(rootnode, parentnode, start_x, start_y, go_x, go_y, N
rootnode.x_ = start_x
rootnode.y_ = start_y

def get_coords_recursive(rootnode, xarray, yarray, PRIMARY_SPACE, PAIR_SPACE):
def get_coords_recursive(rootnode, xarray, yarray, PRIMARY_SPACE, PAIR_SPACE, FLIPPED):
if(rootnode.is_pair_) :
cross_x = -rootnode.go_y_
cross_y = rootnode.go_x_

xarray[rootnode.index_a_] = rootnode.x_ + cross_x * PAIR_SPACE/2.0
xarray[rootnode.index_b_] = rootnode.x_ - cross_x * PAIR_SPACE/2.0

yarray[rootnode.index_a_] = rootnode.y_ + cross_y * PAIR_SPACE/2.0
yarray[rootnode.index_b_] = rootnode.y_ - cross_y * PAIR_SPACE/2.0
yarray[rootnode.index_a_] = rootnode.y_ + (-1 if FLIPPED else 1) * cross_y * PAIR_SPACE/2.0
yarray[rootnode.index_b_] = rootnode.y_ - (-1 if FLIPPED else 1) * cross_y * PAIR_SPACE/2.0
elif(rootnode.index_a_ >= 0) :
xarray[rootnode.index_a_] = rootnode.x_
yarray[rootnode.index_a_] = rootnode.y_

for ii in range(0, len(rootnode.children_)):
get_coords_recursive(rootnode.children_[ii], xarray, yarray, PRIMARY_SPACE, PAIR_SPACE)
get_coords_recursive(rootnode.children_[ii], xarray, yarray, PRIMARY_SPACE, PAIR_SPACE, FLIPPED)



Expand All @@ -181,7 +181,7 @@ def __init__(self):
self.yarray_ = None
self.size_ = None

def setup_tree(self, secstruct, NODE_R,PRIMARY_SPACE, PAIR_SPACE):
def setup_tree(self, secstruct, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED=True):

dangling_start = 0
dangling_end = 0
Expand Down Expand Up @@ -223,8 +223,8 @@ def setup_tree(self, secstruct, NODE_R,PRIMARY_SPACE, PAIR_SPACE):
xarray.append(0.0)
yarray.append(0.0)

self.setup_coords(NODE_R,PRIMARY_SPACE,PAIR_SPACE)
self.get_coords(xarray,yarray,PRIMARY_SPACE,PAIR_SPACE)
self.setup_coords(NODE_R,PRIMARY_SPACE,PAIR_SPACE,FLIPPED)
self.get_coords(xarray,yarray,PRIMARY_SPACE,PAIR_SPACE,FLIPPED)

min_x = xarray[0] - NODE_R
min_y = yarray[0] - NODE_R
Expand Down Expand Up @@ -271,46 +271,36 @@ def draw(self,svgobj,offset_x,offset_y,colors, pairs, sequence, render_in_letter
for pair in pairs:
svgobj.line(offset_x + self.xarray_[pair['from']], offset_y + self.yarray_[pair['from']], offset_x + self.xarray_[pair['to']], offset_y + self.yarray_[pair['to']], pair['color'], self.NODE_R)

if not render_in_letter:
for ii in range(0,len(self.xarray_)):
if colors == None:
svgobj.circle(self.xarray_[ii] + offset_x,self.yarray_[ii] + offset_y, self.NODE_R, "#000000", "#000000")
else:
svgobj.circle(self.xarray_[ii] + offset_x,self.yarray_[ii] + offset_y, self.NODE_R, colors[ii], colors[ii])
for ii in range(0,len(self.xarray_)):
if colors == None:
svgobj.circle(self.xarray_[ii] + offset_x,self.yarray_[ii] + offset_y, self.NODE_R, "#000000", "#000000")
else:
svgobj.circle(self.xarray_[ii] + offset_x,self.yarray_[ii] + offset_y, self.NODE_R, colors[ii], colors[ii])

if sequence:
if sequence and render_in_letter:

for ii in range(0,len(self.xarray_)):
if not render_in_letter:
text_size = self.NODE_R * 1.5
if colors[ii] == [0,0,0]:
color = "#FFFFFF"
else:
color = "#000000"
text_offset_x = -4.0
text_offset_y = (text_size)/2.0 - 1.0
text_size = self.NODE_R * 1.5
if colors[ii] == [0,0,0]:
color = "#FFFFFF"
else:
if colors == None:
color = "#000000"
else:
color = colors[ii]
text_size = self.NODE_R * 2.0
text_offset_x = -2
text_offset_y = (text_size)/2.0
color = "#000000"
text_offset_x = -4.0
text_offset_y = (text_size)/2.0 - 1.0
svgobj.text(self.xarray_[ii] + offset_x + text_offset_x, self.yarray_[ii] + offset_y + text_offset_y, text_size, color, "center", sequence[ii])

def get_coords(self, xarray, yarray, PRIMARY_SPACE, PAIR_SPACE):
def get_coords(self, xarray, yarray, PRIMARY_SPACE, PAIR_SPACE, FLIPPED):

if(self.root_ != None) :
get_coords_recursive(self.root_, xarray, yarray, PRIMARY_SPACE, PAIR_SPACE)
get_coords_recursive(self.root_, xarray, yarray, PRIMARY_SPACE, PAIR_SPACE, FLIPPED)
else :
for ii in range(0,len(xarray)):
xarray[ii] = 0
yarray[ii] = ii * PRIMARY_SPACE

def setup_coords(self, NODE_R, PRIMARY_SPACE, PAIR_SPACE):
def setup_coords(self, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED):
if self.root_ != None:
setup_coords_recursive(self.root_, None, 0, 0, 0, 1, NODE_R, PRIMARY_SPACE, PAIR_SPACE)
setup_coords_recursive(self.root_, None, 0, 0, 0, 1, NODE_R, PRIMARY_SPACE, PAIR_SPACE, FLIPPED)



Loading