Created
May 23, 2009 19:27
-
-
Save brentp/116734 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
sys.stdout = sys.stderr | |
from cgi import parse_qsl | |
import os | |
os.environ[ 'HOME' ] = '/tmp/' | |
import matplotlib | |
matplotlib.interactive(0) | |
matplotlib.rcParams['path.simplify'] = True | |
from matplotlib.figure import Figure | |
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas | |
from cStringIO import StringIO | |
import numpy as np | |
from numpy.lib import format | |
import simplejson | |
path = os.path.dirname(__file__) | |
seqids = {} | |
posns = {} | |
for ichr in range(1, 6): | |
schr = str(ichr) | |
# memmap these | |
seqids[schr] = format.open_memmap(os.path.join(path, \ | |
'at.tome.data.%s.npy' % (schr,)), mode='r') | |
# read these into memory. | |
posns[schr] = np.load(os.path.join(path, 'at.tome.posn.%s.npy' % schr)) | |
COLORS = [(102, 102, 0), (255, 51, 0), (204, 153, 204), (51, 51, 255), | |
(102, 102, 51), (102, 204, 153), (0, 102, 255), (255, 51, 51), | |
(255, 102, 0)] | |
COLORS = [map(lambda x: x/255., list(c)) for c in COLORS] | |
COLS = 5 | |
def application(env, start_response): | |
p = dict(parse_qsl(env['QUERY_STRING'])) | |
xmin = max(1, int(p['xmin'])) | |
xmax = int(p['xmax']) | |
if xmax < 0: return [''] | |
seqid = p['seqid'] | |
tissue = p.get('tissue') | |
aspng = p.get('width') | |
minidx = posns[seqid].searchsorted(xmin) | |
maxidx = posns[seqid].searchsorted(xmax) | |
data = seqids[seqid][minidx: maxidx] | |
data_idx = posns[seqid][minidx: maxidx] | |
if aspng: | |
start_response("200 OK", [("Content-type", "image/png")]) | |
io = StringIO() | |
height = int(p.get('height', 300)) | |
f = Figure(frameon=False) | |
f.canvas = FigureCanvas(f) | |
dpi = 128. | |
f.set_size_inches(int(p['width'])/dpi, height/dpi) | |
ax = f.add_axes((0, 0, 1, 1), frameon=False, xticks=(), yticks=()) | |
ax.set_autoscale_on(0) | |
ax.set_xlim(xmin - 1,xmax) | |
ax.set_ylim(0, 1) | |
else: | |
start_response("200 OK", [("Content-type", "text/plain")]) | |
if tissue is None: | |
if not aspng: | |
if not p.get('summary'): | |
d = {'+': zip(data_idx.tolist(), data[:,:COLS].tolist()), | |
'-': zip(data_idx.tolist(), data[:,COLS:].tolist()) | |
} | |
else: | |
d = {'+': data[:,:COLS].mean(axis=0).tolist(), '-': | |
data[:,COLS:].mean(axis=0).tolist()} | |
return [simplejson.dumps(d)] | |
div = 1. | |
plus_tot = np.zeros((data.shape[0], )) + 0.5 | |
minus_tot = plus_tot.copy() | |
for i in range(COLS): | |
ax.vlines(data_idx, plus_tot, plus_tot + data[:, i + COLS]/div, color=COLORS[i]) | |
ax.vlines(data_idx, minus_tot, minus_tot - data[:, i]/div, color=COLORS[i]) | |
plus_tot += data[:, i + COLS]/div | |
minus_tot -= data[:, i]/div | |
else: | |
tissue = int(tissue) | |
if not aspng: | |
if not p.get('summary'): | |
d = {'+': zip(data_idx.tolist(), data[:,tissue].tolist()), | |
'-': zip(data_idx.tolist(), data[:,tissue + COLS].tolist()) | |
} | |
else: | |
d = {'+': data[:,tissue].mean(), '-': data[:,tissue + COLS].mean()} | |
if p.get('summary') == 'both': | |
return [str(d['+']) + "," + str(d['-'])] | |
return [simplejson.dumps(d)] | |
ax.vlines(data_idx, 0.5, 0.5 + data[:, tissue] * COLS, color=COLORS[tissue]) | |
ax.vlines(data_idx, 0.5, 0.5 - data[:, tissue + COLS] * COLS, color=COLORS[tissue]) | |
ax.axhline(y=0.5) | |
f.savefig(io, format='png', dpi=dpi) | |
io.seek(0) | |
return [io.read()] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment