Skip to content

Commit

Permalink
Multitrack support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-k committed Nov 21, 2018
1 parent 4a9ce7b commit 7a641ad
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 30 deletions.
156 changes: 128 additions & 28 deletions examples/Genetracks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from genetracks import Figure, Track, Alignment"
"from genetracks import Figure, Track, Alignment, Multitrack"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Vectorized sequence alignment diagrams"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"metadata": {
"scrolled": false
},
Expand Down Expand Up @@ -39,10 +46,10 @@
"</svg>"
],
"text/plain": [
"<drawSvg.drawing.Drawing at 0x7f0ada3253c8>"
"<drawSvg.drawing.Drawing at 0x7f2ed6a67080>"
]
},
"execution_count": 11,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -56,59 +63,152 @@
"figure.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example we pack multiple tracks onto the same row and join them to illustrate gaps:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n",
" width=\"500\" height=\"90.0\" viewBox=\"0 -90 500 90\">\n",
"<defs>\n",
"</defs>\n",
"<g transform=\"translate(0 0)\">\n",
"<path d=\"M0,-5.0 L450,-5.0\" stroke=\"lightgrey\" />\n",
"<g transform=\"translate(0 0)\">\n",
"<rect x=\"0\" y=\"-10\" width=\"150\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"75.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Read 1, forward</text>\n",
"<path d=\"M150,0 L155,-5.0 L150,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"</g>\n",
"<g transform=\"translate(0 0)\">\n",
"<rect x=\"300\" y=\"-10\" width=\"150\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"375.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Read 1, reverse</text>\n",
"<path d=\"M300,0 L295,-5.0 L300,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"</g>\n",
"</g>\n",
"<g transform=\"translate(0 -30)\">\n",
"<path d=\"M30,-5.0 L475,-5.0\" stroke=\"lightgrey\" />\n",
"<g transform=\"translate(0 0)\">\n",
"<rect x=\"30\" y=\"-10\" width=\"150\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"105.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Read 2, forward</text>\n",
"<path d=\"M180,0 L185,-5.0 L180,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"</g>\n",
"<g transform=\"translate(0 0)\">\n",
"<rect x=\"325\" y=\"-10\" width=\"150\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"400.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Read 2, reverse</text>\n",
"<path d=\"M325,0 L320,-5.0 L325,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"</g>\n",
"</g>\n",
"<g transform=\"translate(0 -60)\">\n",
"<path d=\"M60,-5.0 L500,-5.0\" stroke=\"lightgrey\" />\n",
"<g transform=\"translate(0 0)\">\n",
"<rect x=\"60\" y=\"-10\" width=\"150\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"135.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Read 3, forward</text>\n",
"<path d=\"M210,0 L215,-5.0 L210,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"</g>\n",
"<g transform=\"translate(0 0)\">\n",
"<rect x=\"350\" y=\"-10\" width=\"150\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"425.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Read 3, reverse</text>\n",
"<path d=\"M350,0 L345,-5.0 L350,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<drawSvg.drawing.Drawing at 0x7f2ed6a77b00>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"figure = Figure(height=90, size=500)\n",
"for i in range(0,3):\n",
" o = i * 30\n",
" e = i * 25\n",
" figure.add_track(Multitrack([\n",
" Track(o, 150 + o, direction='f', label='Read {}, forward'.format(i + 1)),\n",
" Track(300 + e, 450 + e, direction='r', label='Read {}, reverse'.format(i + 1))], join=True))\n",
"figure.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `Alignment` class allows us to illustrate the relationship between regions of two different tracks:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n",
" width=\"700\" height=\"100.0\" viewBox=\"0 -100 700 100\">\n",
" width=\"700\" height=\"75.0\" viewBox=\"0 -75 700 75\">\n",
"<defs>\n",
"</defs>\n",
"<g transform=\"translate(0 0)\">\n",
"<g transform=\"translate(0 0)\">\n",
"<rect x=\"50\" y=\"-10\" width=\"250\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"175.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Another sequence</text>\n",
"<path d=\"M300,0 L305,-5.0 L300,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<rect x=\"50\" y=\"-10\" width=\"50\" height=\"10\" fill=\"lightblue\" stroke=\"lightblue\" />\n",
"<path d=\"M50,0 L45,-5.0 L50,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<rect x=\"110\" y=\"-10\" width=\"190\" height=\"10\" fill=\"lightblue\" stroke=\"lightblue\" />\n",
"</g>\n",
"<g transform=\"translate(0 -50)\">\n",
"<rect x=\"110\" y=\"-10\" width=\"300\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<text x=\"260.0\" y=\"-20\" font-size=\"10.0\" text-anchor=\"middle\" transform=\"translate(0,5.0)\">Sequence 1</text>\n",
"<path d=\"M110,0 L105,-5.0 L110,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<rect x=\"150\" y=\"-10\" width=\"50\" height=\"10\" fill=\"salmon\" stroke=\"salmon\" />\n",
"<rect x=\"110\" y=\"-10\" width=\"250\" height=\"10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<path d=\"M360,0 L365,-5.0 L360,-10\" fill=\"lightgrey\" stroke=\"lightgrey\" />\n",
"<rect x=\"110\" y=\"-10\" width=\"190\" height=\"10\" fill=\"salmon\" stroke=\"salmon\" />\n",
"</g>\n",
"<path d=\"M50,-10 L150,-50\" stroke=\"black\" />\n",
"<path d=\"M50,0 L50,-10\" stroke=\"black\" />\n",
"<path d=\"M150,-50 L150,-60\" stroke=\"black\" />\n",
"<path d=\"M100,-10 L200,-50\" stroke=\"black\" />\n",
"<path d=\"M100,0 L100,-10\" stroke=\"black\" />\n",
"<path d=\"M200,-50 L200,-60\" stroke=\"black\" />\n",
"<path d=\"M110,-10 L300,-50\" stroke=\"black\" />\n",
"<path d=\"M110,0 L110,-10\" stroke=\"black\" />\n",
"<path d=\"M300,-50 L300,-60\" stroke=\"black\" />\n",
"<path d=\"M300,-10 L110,-50\" stroke=\"black\" />\n",
"<path d=\"M300,0 L300,-10\" stroke=\"black\" />\n",
"<path d=\"M110,-50 L110,-60\" stroke=\"black\" />\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<drawSvg.drawing.Drawing at 0x7f0ada3184e0>"
"<drawSvg.drawing.Drawing at 0x7f2ed6a679b0>"
]
},
"execution_count": 12,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = Figure(height=100)\n",
"f.add_alignment(Alignment(t1, t2, [(50, 150), (100, 200)]))\n",
"f.show()"
"figure = Figure(width=400)\n",
"t1 = Track(50, 300, direction='r', regions=[(110, 300, 'lightblue')])\n",
"t2 = Track(110, 360, direction='f', regions=[(110, 300, 'salmon')])\n",
"figure.add_alignment(Alignment(t1, t2, [(110, 300), (300, 110)]))\n",
"figure.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -127,10 +227,10 @@
"</svg>"
],
"text/plain": [
"<drawSvg.drawing.Drawing at 0x7f0ada3180b8>"
"<drawSvg.drawing.Drawing at 0x7f2ed6a595c0>"
]
},
"execution_count": 13,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
2 changes: 1 addition & 1 deletion genetracks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Vectorized Genetrack Renderer
"""
from genetracks.elements import Track, Figure, Alignment
from genetracks.elements import Track, Figure, Alignment, Multitrack
from genetracks.plasmid import Plasmid, Region
19 changes: 18 additions & 1 deletion genetracks/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, track1, track2, connections, text=None, style=None):
self.t2 = track2
self.connections = connections

def draw(self, x=0, y=0, h=10, w=1, gap=50):
def draw(self, x=0, y=0, h=10, gap=50):
g = draw.Group(transform="translate({} {})".format(x, y))
g.append(self.t1.draw(x=0, y=0))
g.append(self.t2.draw(x=0, y=-gap))
Expand All @@ -104,6 +104,23 @@ def draw(self, x=0, y=0, h=10, w=1, gap=50):
g.append(draw.Lines(top, gap, top, gap + h, stroke='black'))
return g

class Multitrack:
"""Pack multiple tracks onto a line
"""
def __init__(self, tracks, join=False):
self.tracks = tracks
self.join = join

def draw(self, x=0, y=0, h=10):
g = draw.Group(transform="translate({} {})".format(x, y))
if self.join:
start = min([t.a for t in self.tracks])
end = max([t.b for t in self.tracks])
g.append(draw.Lines(start, h / 2, end, h / 2, stroke='lightgrey'))
for track in self.tracks:
g.append(track.draw())

return g

class Tick:
"""
Expand Down

0 comments on commit 7a641ad

Please sign in to comment.