diff --git a/examples/Genetracks.ipynb b/examples/Genetracks.ipynb index bb095fa..adecc3d 100644 --- a/examples/Genetracks.ipynb +++ b/examples/Genetracks.ipynb @@ -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 }, @@ -39,10 +46,10 @@ "" ], "text/plain": [ - "" + "" ] }, - "execution_count": 11, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -56,9 +63,97 @@ "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": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "Read 1, forward\n", + "\n", + "\n", + "\n", + "\n", + "Read 1, reverse\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "Read 2, forward\n", + "\n", + "\n", + "\n", + "\n", + "Read 2, reverse\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "Read 3, forward\n", + "\n", + "\n", + "\n", + "\n", + "Read 3, reverse\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "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": [ { @@ -66,49 +161,54 @@ "image/svg+xml": [ "\n", "\n", + " width=\"700\" height=\"75.0\" viewBox=\"0 -75 700 75\">\n", "\n", "\n", "\n", "\n", "\n", - "Another sequence\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", - "\n", - "Sequence 1\n", - "\n", - "\n", + "\n", + "\n", + "\n", "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", "\n", "" ], "text/plain": [ - "" + "" ] }, - "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": [ { @@ -127,10 +227,10 @@ "" ], "text/plain": [ - "" + "" ] }, - "execution_count": 13, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } diff --git a/genetracks/__init__.py b/genetracks/__init__.py index e0edc50..f1c3db7 100644 --- a/genetracks/__init__.py +++ b/genetracks/__init__.py @@ -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 diff --git a/genetracks/elements.py b/genetracks/elements.py index b8969c4..925be9b 100644 --- a/genetracks/elements.py +++ b/genetracks/elements.py @@ -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)) @@ -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: """