Brian Naughton // Sun 14 August 2016 // Filed under data // Tags data genomics statistics pymc3 nanopore

Oxford Nanopore (ONT) sells an amazing, inexpensive sequencer called the MinION. It's an unusual device in that the sequencing "flowcells" use protein pores. Unlike a silicon chip, the pores are sensitive to their environment (especially heat) and can get damaged or degraded and stop working.

When you receive a flowcell from ONT, only a fraction of the possible 2048 pores are active, perhaps 800–1500. Because of how the flowcell works, you start a run using zero or one pores from each of the 512 "channels" (each of which contains 4 pores).

As the pores interact with the DNA and other stuff in your solution, they can get gummed up and stop working. It's not unusual to start a run with 400 pores and end a few hours later with half that many still active. It's also not unusual to put a flowcell in the fridge and find it has 20% fewer pores when you take it out. (Conversely, pores can come back to life after a bit of a rest.)

All told, this means it's quite difficult to tell how much sequence you can expect from a run. If you want to sequence a plasmid at 100X, and you are starting with 400 pores, will you get enough data in two hours? That's the kind of question we want an answer to.

import pymc3 as pm
import numpy as np
import scipy as sp
import theano.tensor as T

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

from functools import partial
from IPython.display import display, HTML, Image

For testing purposes — and because I don't have nearly enough real data — I will simulate some data.

In this toy example, DNA of length 1000+/-50 bases is sequenced for 1000, 20000 or 40000 seconds at 250bp/s. After 100 reads, a pore gets blocked and can no longer sequence. The device I am simulating has only 3 pores (where a real MinION would have 512).

datatypes = [
    ('total_num_reads',,        ('total_dna_read',,
    ('num_minutes', np.float),          ('num_active_pores_start',,
    ('num_active_pores_end',,   ('mean_input_dna_len_estimate', np.float)]


data = np.array([
  ( MAX_PORES*9,   MAX_PORES*  9*1000*.95,    1000.0/60,  MAX_PORES,  MAX_PORES,  1000.0),
  ( MAX_PORES*10,  MAX_PORES* 10*1000*1.05,   1000.0/60,  MAX_PORES,  MAX_PORES,  1000.0),
  ( MAX_PORES*10,  MAX_PORES* 10*1000*.95,    1000.0/60,  MAX_PORES,  MAX_PORES,  1000.0),
  ( MAX_PORES*11,  MAX_PORES* 11*1000*1.05,   1000.0/60,  MAX_PORES,  MAX_PORES,  1000.0),
  ( MAX_PORES*100, MAX_PORES*100*1000*.95,   20000.0/60,  MAX_PORES,          0,  1000.0),
  ( MAX_PORES*100, MAX_PORES*100*1000*1.05,  20000.0/60,  MAX_PORES,          0,  1000.0),
  ( MAX_PORES*100, MAX_PORES*100*1000*.95,   40000.0/60,  MAX_PORES,          0,  1000.0),
  ( MAX_PORES*100, MAX_PORES*100*1000*1.05,  40000.0/60,  MAX_PORES,          0,  1000.0),
], dtype=datatypes)

MinION Simulator

To help figure out how long to leave our sequencer running, I made a MinION simulator in a jupyter notebook. I then turned the notebook into an app using, an awesome new service that can turn jupyter notebooks into apps running on the cloud (including AWS lambda). Here is the dappled nanopore simulator app / notebook.

The model is simple: a pore reads DNA until it's completely read, then with low probability it gets blocked and can no longer sequence. I've been fiddling with the parameters to give a reasonable result for our runs, but we don't have too much data yet, so it's not terribly accurate. (The model itself may also be inaccurate, since how the pore gets blocked is not really discussed by ONT.)

Nevertheless, the simulator is a useful tool for ballparking how much sequence we should expect from a run.

Hierarchical Bayesian Model

Here's where things get more complicated...

In theory, given some data from real MinION runs, we should be able to learn the parameters for a model that would enable us to predict how much data we would get from a new run. Like many problems, this is a good fit for Bayesian analysis, since we have data and we want to learn the most appropriate model given the data.

For each run, I need to know:

  • what length DNA I think I started with
  • how long the run was in minutes
  • how many reads I got
  • how much sequence I got
  • the number of active pores at the start and at the end of a run

I'll use PyMC3 for this problem. First, I need to specify the model.

Input DNA

The input DNA depends a lot on its preparation. I believe the distribution of input DNA lengths could be:

  • exponential if it is genomic DNA breaking randomly
  • normal with a small variance if it is from a plasmid
  • normal but sharply truncated if it is cut out of a gel

The length of input DNA is different to the length of reads produced by the sequencer, which is affected by breakage, capture biases, secondary structure, etc. The relationship between input DNA length and read length could be learned. We could get arbitrarily complex here: for example, given enough data, we could include a mixture model for different DNA types (genomic vs plasmid).

A simple distribution with small variance but fat tails seems reasonable here. I don't have much idea what the standard deviation should be, so I'll make it a fraction of the mean.

with pm.Model() as model:
    mean_input_dna_len = pm.StudentT('mean_input_dna_len', nu=3, mu=data['mean_input_dna_len_estimate'],
                                     sd=data['mean_input_dna_len_estimate']/5, shape=data.shape[0])

This is the first PyMC3 code, so here are some notes on what's going on:

  • the context must always specify the pymc3 model
  • Absent better ideas, I'll generally be using a T distribution instead of a normal, as MacKay recommends. This distribution should be truncated at zero, but I can't do this for multidimensional data because of what I think is a bug in pymc3.
  • I am not modeling the input_dna_len here, just the mean of that distribution. As I mention above, the distribution of DNA lengths could be modeled several ways, but I currently only need the mean in my model.

the shape parameter

In this experiment, We will encounter three different kinds of distribution shape:

  • shape=1: this is a scalar value. For example, we might think the speed of the nanopore machine is the same for all runs.
  • shape=data.shape[0]: data.shape[0] is the number of runs. For example, the length of DNA in solution varies from run to run.
  • shape=(data.shape[0],MAX_NUM_PORES): we estimate a value for every pore in every run. For example, how many reads until a pore gets blocked? This varies by run, but needs to be modeled per pore too.

R9 Read Speed

I know that the R9 pore is supposed to read at 250 bases per second. I believe the mean could be a little bit more or less than that, and I believe that all flowcells and devices should be about the same speed, therefore all runs will sample from the same distribution of mean_read_speed.

Because this is a scalar, pymc3 lets me set a lower bound of 0 bases per second. (Interestingly, unlike DNA length, the speed could technically be negative, since the voltage across the pore can be positive or negative, as exploited by Read Until...)

Truncated0T1D = pm.Bound(pm.StudentT, lower=0)
with pm.Model() as model:
    mean_read_speed = Truncated0T1D('mean_read_speed', nu=3, mu=250, sd=10, shape=data.shape)

Capturing DNA

DNA is flopping around in solution, randomly entering pores now and then. How long will a pore have to wait for DNA? I have a very vague idea that it should take around a minute but this is basically an unknown that the sampler should figure out.

Note again that I am using the mean time only, not the distribution of times. The actual distribution of times to capture DNA would likely be distributed by an exponential distribution (waiting time for a Poisson process).

Hierarchical model

This is the first time I am using a hierarchical/multilevel model. For more on what this means, see this example from pymc3 or Andrew Gelman's books.

There are three options for modeling mean_time_to_capture_dna: (a) it's the same for each run (e.g., everybody us using the same recommended DNA concentration) (b) it's independent for each run (c) each run has a different mean, but the means are drawn from the same distribution (and probably should not be too different).


Diagram taken from PyMC3 dev twiecki's blog

with pm.Model() as model:
    prior_mean_time_to_capture_dna = Truncated0T1D('prior_mean_time_to_capture_dna', nu=3, mu=60, sd=30)
    mean_time_to_capture_dna = pm.StudentT('mean_time_to_capture_dna', nu=3, mu=prior_mean_time_to_capture_dna,
                                           sd=prior_mean_time_to_capture_dna/10, shape=data.shape)

Reading DNA

I can use pymc3's Deterministic type to calculate how long a pore spends reading a chunk of DNA, on average. That's just a division, but note it uses theano's true_div function instead of a regular division. This is because neither value is a number; they are random variables. Theano will calculate this as it's being sampled. (A simple "a/b" also works, but I like to keep in mind what's being done by theano if possible.)

with pm.Model() as model:
    mean_time_to_read_dna = pm.Deterministic('mean_time_to_read_dna', T.true_div(mean_input_dna_len, mean_read_speed))

Then each pore can do how many reads in this run? I have to be a bit careful to specify that I mean the number of reads possible per pore.

with pm.Model() as model:
    num_reads_possible_in_time_per_pore = pm.Deterministic('num_reads_possible_in_time_per_pore',
        T.true_div(num_seconds_run, T.add(mean_time_to_capture_dna, mean_time_to_read_dna)))

Blocking Pores

In my model, after each read ends, a pore can get blocked, and once it's blocked it does not become unblocked. I believe about one in a hundred times, a pore will be blocked after if finishes sequencing DNA. If it were more than that, sequencing would be difficult, but it could be much less.

We can think of the Beta distribution as modeling coin-flips where the probability of heads (pore getting blocked) is 1/100.

with pm.Model() as model:
    prior_p_pore_blocked_a, prior_p_pore_blocked_b, = 1, 99
    p_pore_blocked = pm.Beta('p_pore_blocked', alpha=prior_p_pore_blocked_a, beta=prior_p_pore_blocked_b)

Then, per pore, the number of reads before blockage is distributed as a geometric distribution (the distribution of the number of tails you will flip before you flip a head). I have to approximate this with an exponential distribution — the continuous version of a geometric — because ADVI (see below) requires continuous distributions. I don't think it makes an appreciable difference.

Here the shape parameter is the number of runs x number of pores since here I need a value for every pore.

with pm.Model() as model:
    num_reads_before_blockage = pm.Exponential('num_reads_before_blockage', lam=p_pore_blocked,
        shape=(data.shape[0], MAX_PORES))


Here things get a little more complicated. It is not possible to have two random variables x and y, set x + y = data, and sample values of x and y.

testdata = np.random.normal(loc=10,size=100) + np.random.normal(loc=1,size=100)
with pm.Model() as testmodel:
    x = pm.Normal("x", mu=0, sd=10)
    y = pm.Normal("y", mu=0, sd=1)
    # This does not work
    #z = pm.Deterministic(x + y, observed=data)
    # This does work
    z = pm.Normal('x+y', mu=x+y, sd=1, observed=testdata)
    trace = pm.sample(1000, pm.Metropolis())

I was surprised by this at first but it makes sense. This process is more like a regression where you minimize error than a perfectly fixed constraint. The smaller the standard deviation, the more you penalize deviations from the constraint. However, you need some slack so that it's always possible to estimate a logp for the data given the model. If the standard deviation goes too low, you end up with numerical problems (e.g., nans). Unfortunately, I do not know of a reasonable way to set this value.

I encode constraints in my model in a similar way: First I define a Laplace distribution in theano to act as my likelihood. Why Laplace instead of Normal? It returned nans less often...

Using your own DensityDist allows you to use any function as a likelihood. I use DensityDist just to have more control over the likelihood, and to differentiate from a true Normal/Laplacian distribution. This can be finicky, so I've had to spend a bunch of time tweaking this.

def T_Laplace(val, obs, b=1):
    return T.log(T.mul(1/(2*b), T.exp(T.neg(T.true_div(T.abs_(T.sub(val, obs)), b)))))

Constraint #1

The total DNA I have read must be the product of mean_input_dna_len and total_num_reads. mean_input_dna_len can be different to my estimate.

This is a bit redundant since I know total_num_reads and total_dna_read exactly. In a slightly more complex model we would have different mean_read_length and mean_input_dna_len. Here it amounts to just calculating mean_input_dna_len as total_dna_read / total_num_reads.

def apply_mul_constraint(f1, f2, observed):
    b = 1 # 1 fails with NUTS (logp=-inf), 10/100 fails with NUTS too (not positive definite)
    return T_Laplace(T.mul(f1,f2), observed, b)

with pm.Model() as model:
    total_dna_read_constraint = partial(apply_mul_constraint, mean_input_dna_len, data['total_num_reads'])
    constrain_total_dna_read = pm.DensityDist('constrain_total_dna_read', total_dna_read_constraint, observed=data['total_dna_read'])

Constraint #2

The number of reads per pore is whichever is lower:

  • the number of reads the pore could manage in the length of the run
  • the number of reads it manages before getting blocked

To calculate this, I compare num_reads_before_blockage and num_reads_possible_in_time_per_pore.

First, I use T.tile to replicate num_reads_possible_in_time_per_pore for all pores. That turns it from an array of length #runs to a matrix of shape #runs x #pores (this should use broadcasting but I wasn't sure it was working properly...)

Then I take the minimum value of these two arrays ( and if the minimum value is the number of reads before blockage (T.switch(,f2),0,1)) then that pore is blocked, otherwise it is active. I sum these 0/1s over all pores (axis=AXIS_PORES) to get a count of the number of active pores for each run.

The value of this count is constrained to be equal to data['num_active_pores_end'].

def apply_count_constraint(num_reads_before_blockage, num_reads_possible_in_time_broadcast, observed):
    b = 1
    num_active_pores = T.sum(T.switch(, num_reads_possible_in_time_broadcast),0,1),axis=AXIS_PORES)
    return T_Laplace(num_active_pores, observed, b)

with pm.Model() as model:
    num_reads_possible_in_time_broadcast = T.tile(num_reads_possible_in_time_per_pore, (MAX_PORES,1)).T
    num_active_pores_end_constraint = partial(apply_count_constraint, num_reads_before_blockage, num_reads_possible_in_time_broadcast)
    constrain_num_active_pores_end = pm.DensityDist('constrain_num_active_pores_end', num_active_pores_end_constraint, observed=data['num_active_pores_end'])

Constraint #3

Using the same matrix num_reads_possible_in_time_broadcast this time I sum the total number of reads from each pore in a run. I simply sum the minimum value from each pore: either the number before blockage occurs or the total number possible.

def apply_minsum_constraint(num_reads_before_blockage, num_reads_possible_in_time_broadcast, observed):
    b = 1 # b=1 fails with ADVI and >100 pores (nan)
    min_reads_per_run = T.sum(T.minimum(num_reads_before_blockage, num_reads_possible_in_time_broadcast),axis=AXIS_PORES)
    return T_Laplace(min_reads_per_run, observed, b)

with pm.Model() as model:
    total_num_reads_constraint = partial(apply_minsum_constraint, num_reads_before_blockage, num_reads_possible_in_time_broadcast)
    constrain_total_num_reads = pm.DensityDist('constrain_total_num_reads', total_num_reads_constraint, observed=data['total_num_reads'])


There are three principal ways to sample in PyMC3:

  • Metropolis-Hastings: the simplest sampler, generally works fine on simpler problems
  • NUTS: more efficient than Metropolis, but I've found it to be slow and tricky to get to work
  • ADVI: this is "variational inference", the new, fast way to estimate a posterior. This seems to work great, though as I mention above, it needs continuous distributions only.

I used ADVI most of the time in this project, since NUTS was too slow and had more numerical issues than ADVI, and the ADVI results seemed more sensible than Metropolis.

with pm.Model() as model:
    v_params = pm.variational.advi(n=500000, random_seed=1)
    trace = pm.variational.sample_vp(v_params, draws=5000)


In my toy model: there are 8 runs in total; each device has 3 pores; it takes about 4 seconds to sequence a DNA molecule (~1000 bases / 250 bases per second) and 96 seconds to capture a DNA molecule (so 100 seconds in total to read one DNA molecule).

In my 8 runs, I expect that [10, 10, 10, 10, 200, 200, 400, 400] reads are possible. However, I expect that runs 4, 5, 6, 7 will have a blockage at 100 reads.

Finally, I expect there to be 3 pores remaining in the first four runs, and 0 pores remaining in the last four.

ADVI Results

mean_input_dna_len__0                    950.0 (+/- 0.1) # All the input_dna_lens are ok.
mean_input_dna_len__1                   1050.0 (+/- 0.1) # This is a simple calculation since I know
mean_input_dna_len__2                    950.0 (+/- 0.1) # how many reads there are and how long the reads are
mean_input_dna_len__3                   1050.0 (+/- 0.1)
mean_input_dna_len__4                    950.0 (+/- 0.1)
mean_input_dna_len__5                   1049.9 (+/- 0.1)
mean_input_dna_len__6                    950.0 (+/- 0.1)
mean_input_dna_len__7                   1050.0 (+/- 0.1)
num_reads_possible_in_time_per_pore__0    10.1 (+/- 1) # The number of reads possible is also a simple calculation
num_reads_possible_in_time_per_pore__1    10.3 (+/- 1) # It depends on the speed of the sequencer and the length of DNA
num_reads_possible_in_time_per_pore__2    10.2 (+/- 1)
num_reads_possible_in_time_per_pore__3    10.5 (+/- 1)
num_reads_possible_in_time_per_pore__4   210.9 (+/- 36)
num_reads_possible_in_time_per_pore__5   207.4 (+/- 35)
num_reads_possible_in_time_per_pore__6   419.8 (+/- 67)
num_reads_possible_in_time_per_pore__7   413.2 (+/- 66)
num_reads_before_blockage_per_run__0     501.3 (+/- 557) # The total number of reads before blockage per run
num_reads_before_blockage_per_run__1     509.8 (+/- 543) # is highly variable when there is no blockage (runs 0-3).
num_reads_before_blockage_per_run__2     501.9 (+/- 512)
num_reads_before_blockage_per_run__3     502.4 (+/- 591)
num_reads_before_blockage_per_run__4     297.2 (+/- 39)  # When there is blockage (runs 4-7), then it's much less variable
num_reads_before_blockage_per_run__5     298.7 (+/- 38)
num_reads_before_blockage_per_run__6     299.6 (+/- 38)
num_reads_before_blockage_per_run__7     301.2 (+/- 38)
num_active_pores_per_run__0                2.8 (+/- 0.4) # The number of active pores per run is estimated correctly
num_active_pores_per_run__1                2.8 (+/- 0.4) # as we expect for a value we've inputted
num_active_pores_per_run__2                2.8 (+/- 0.4)
num_active_pores_per_run__3                2.8 (+/- 0.3)
num_active_pores_per_run__4                0.0 (+/- 0.1)
num_active_pores_per_run__5                0.0 (+/- 0.1)
num_active_pores_per_run__6                0.0 (+/- 0.0)
num_active_pores_per_run__7                0.0 (+/- 0.0)

ADVI Plots

We can plot the values above too. No real surprises here, though num_active_pores_per_run doesn't show the full distribution for some reason.

with pm.Model() as model:
                 lines={k: v['mean'] for k, v in pm.df_summary(trace[-1000:]).iterrows()})


It's a pretty complex model. Is it actually useful? I think it's almost useful, but in its current form it suffers from a lack of robustness.

The results I get are sensitive to changes in several independent areas:

  • the sampler/inference method: for example, Metropolis returns different answers to ADVI
  • the constraint parameters: changing the "b" parameter can lead to numerical problems
  • the priors I selected: changes in my priors that I do not perceive as important can lead to different results (more data would help here)

A big problem with my current model is that scaling it up to 512 pores seems to be difficult numerically. Metropolis just fails, I think because it can't sample efficiently; NUTS fails for reasons I don't understand (it throws an error about a positive definite matrix); ADVI works best, but starts to get nans as the number of pores grows, unless I loosen the constraints. It's possible that I should need to begin with loose constraints and tighten them over time.

Finally, the model currently expects the same number of pores to be available for every run. I haven't addressed that yet, though I think it should be pretty straightforward. There may be nice theano trick I am overlooking.

More Conclusions

Without ADVI, I think I would have failed to get an answer here. I'm pretty ignorant of how it works, but it seems like a significant advance and I'll definitely use it again. In fact, it would be interesting to try applying Edward, a variational inference toolkit with PyMC3 support, to this problem (this would mean swapping theano for tensorflow).

The process of modeling your data with a PyMC3/Stan/Edward model forces you to think a lot about what is really going on with your data and model (and even after spending quite a bit of time on it, my model still needs quite a bit of work to be more than a toy...) When your model has computational problems, as I had several times, it often means the model wasn't described correctly (Gelman's folk theorem).

Although it is still a difficult, technical process, I'm excited about this method of doing science. It seems like the right way to tackle a lot of problems. Maybe with advances like ADVI and theano/tensorflow; groups like the Blei and Gelman labs developing modeling tools like PyMC3, Stan and Edward; and experts like Kruschke and the Stan team creating robust models for us to copy, it will become more common.

Brian Naughton // Sun 10 April 2016 // Filed under data // Tags data machine learning neural nets deep learning threejs webgl

Convnetjs is a lovely javascript-based neural net / deep learning library from @karpathy. Of course, it's slow since it's javascript and doesn't use the GPU, but the demos are terrific.

Convnetjs implements deep reinforcement learning, which has famously been used to teach computers to play video games just from looking at the pixels. Google's DeepMind, the folks responsible for AlphaGo, did the original work.

The deep reinforcement learning demo is a simple game where an agent wanders around a room, trying to pick up red balls and avoid green balls. The agent has nine antennae that register touches with walls and balls. The input to the neural net is simply the output of the nine antennae.

Convnetjs deep reinforcement demo: the agent's antennae sense two green balls

I wanted to see what the net would do if given some "raw" pixels instead of a sanitized input. How magical are deep nets anyway? I made a three.js canvas where I render a representation of what the agent is seeing. Then I pull the pixel color values out of the canvas and pass them into the neural net, replacing the antennae's sensor values.

3D view, as rendered by three.js, and below, the pixel values that get fed into the neural net.

3D view of the play area, from above (agent at the top right)

The new pixel-based version works too, but it does take forever to learn anything — at least compared to the original. I optimistically expected it to just work without much tweaking. I probably need to understand the parameters a lot better to make it work better.

The three.js version of the demo is here (and the source here).

Brian Naughton // Thu 16 July 2015 // Filed under data // Tags python ipython notebook data

IPython notebook is awesome, but it's still pretty immature in many ways. These are a few functions that I've found help make it a more pleasant experience.

Use retina displays

It's great to be able to make use of a retina display, if you have one. The difference in figure quality is very noticable. To use retina, just set this flag at the top of your notebook (you probably want inline plots too).

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

More prominent section breaks

IPython notebooks can look messy and disorienting. I like to include large section breaks using IPython.display.HTML.

from IPython.display import HTML

def new_section(title):
    style = "text-align:center;background:#66aa33;padding:120px;color:#ffffff;font-size:3em;"
    return HTML('<div style="{}">{}</div>'.format(style, title))

new_section("New Section")
New Section

Nicer image display

The IPython.display.Image function is very useful, but sometimes it's nice to use more of the horizontal space available. I extended the Image function to Images to allow for a table-based layout.

from IPython.display import display, HTML, Image

def Images(images, header=None, width="100%"): # to match Image syntax
    if type(width)==type(1): width = "{}px".format(width)
    html = ["<table style='width:{}'><tr>".format(width)]
    if header is not None:
        html += ["<th>{}</th>".format(h) for h in header] + ["</tr><tr>"]

    for image in images:
        html.append("<td><img src='{}' /></td>".format(image))

       header=["Hanahaus", "Drug approvals"], width="60%")
HanahausDrug approvals

Running command-line tools

What do you do if you want to run a command-line tool from within IPython notebook? It can get very messy, since you may need to see stdout, but it may include a lot of unformatted text, which can make your notebook very unreadable.

To address this, I created my own wrapper around subprocess. I first have to set some global CSS for the notebook. There are no really good ways to do this yet, so I just call a HTML function at the top of the notebook. I then create a do_call function that runs the command-line tool (subprocess) and captures the output. The output is hidden but can be expanded using a CSS trick. It also keeps track of how long each subprocess took to run. This function also includes your environment variables from os.environ, which is often important.

I also include a @contextmanager-based function that allows me to run command-line tools from within specific directories. This comes up a lot.

This system was very useful for me when I was making a "pipeline" notebook, chaining together different command-line tools.

from IPython.display import HTML

    .showhide_label { display:block; cursor:pointer; }
    .showhide { position: absolute; left: -999em; }
    .showhide + div { display: none; }
    .showhide:checked + div { display: block; }
    .shown_or_hidden { font-size:85%; }
import time, os, random
from IPython.display import display, HTML
from subprocess import Popen, PIPE
from contextlib import contextmanager

def do_call(cmd, stdin=None, stdout=None, stderr=None, env=None, base_dir=None):
    """Help call subprocess with some niceties. Output to html."""

    assert type(cmd)==type([])
    MAX_OUT = 30000 # characters to output to >stdout and >stderr

    def with_div(text, style="none", label=None):
        random_id = ''.join(random.choice("01234567890ABCDEF") for _ in range(16))
        div = {
            "time":"<div style='color:#953'>{}</div>".format(text),
            "title":"<div style='font-size:125%;padding:5px;color:#6a3;'>{}</div>".format(text),
            "main":"<div style='border:2px solid #6a3;padding:5px;color:#555;'>{}</div>".format(text),
            "mono":"<div style='font-family:monospace;padding:5px;'><pre>{}</pre></div>".format(text),
            "hide":"""<label class="showhide_label" for="showhide_{}">▸{}</label>
                      <input type="checkbox" id="showhide_{}" class="showhide"/>
                      <div class="shown_or_hidden"><pre>{}</pre></div>""".format(random_id, label, random_id, text),

        return div[style]

    # Keep track of the amount of time spent in the process
    start_time = time.time()

    # Treat Nones as empty
    cmd = [c for c in cmd if c is not None] # ignore Nones, which otherwise would be ""

    # Optionally, make the command more readable by pretending base_dir is an env
    # To actually make an env requires shell=True and seems worse since this is only cosmetic
    cmdstr = ' '.join(cmd).replace(base_dir, "$BASE") if base_dir is not None else ' '.join(cmd)

    # Use custom environment variables. os.environ variables are overwritten if duplicated
    process_env = dict(os.environ, **env) if env is not None else os.environ

    # Use Popen instead of to get stdout, stderr
    p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=process_env)
    p_out, p_err = p.communicate(stdin)
    p_rc = p.returncode
    p_out = p_out if p_out != "" else "[No stdout]"
    p_err = p_err if p_err != "" else "[No stderr]"

    if stdout is not None: stdout.write(p_out)
    if stderr is not None: stderr.write(p_err)

    p_out_fmt = p_out if len(p_out) <= MAX_OUT else "{}\n{}".format(p_out[:MAX_OUT], "[TRUNCATED]")
    p_err_fmt = p_err if len(p_err) <= MAX_OUT else "{}\n{}".format(p_err[:MAX_OUT], "[TRUNCATED]")

    # Output a nicely formatted HTML block
    html = [with_div("Running subprocess", "title")]
    html += [with_div(cmdstr, "mono")]
    html += [with_div(p_out_fmt, "hide", "stdout ({})".format(p_out_fmt.count("\n")))]
    html += [with_div(p_err_fmt, "hide", "stderr ({})".format(p_err_fmt.count("\n")))]
    html += [with_div("subprocess time : {:.2f}s".format(time.time() - start_time), "time")]
    display(HTML(with_div(''.join(html), "main")))

def using_dir(path):
    old_dir = os.getcwd()

# The same thing, two ways
with using_dir("/Users/briann/anaconda"):
    do_call(["ls", "."])

do_call(["ls", "/Users/briann/anaconda"])
Running subprocess
ls .
[No stderr]
subprocess time : 0.01s
Running subprocess
ls /Users/briann/anaconda
[No stderr]
subprocess time : 0.01s

Nicer printing

Sometimes the standard print function doesn't cut it. I have a few helper functions that make printing text a bit more flexible.

I also generally use "from __future__ import print_function" everywhere, since print should be a function anyway. I use "from __future__ import division" since we'll all be on Python 3 soon enough.

from __future__ import print_function, division

from IPython.display import display, HTML
from itertools import count
import yaml

def uprint(text):
    print("{}\n".format(text) + "-"*len(text))

def hprint(text):

def tprint(rows, header=True):
    html = ["<table>"]
    html_row = "</td><td>".join(k for k in rows[0])
    html.append("<tr style='font-weight:{}'><td>{}</td></tr>".format('bold' if header is True else 'normal', html_row))
    for row in rows[1:]:
        html_row = "</td><td>".join(r for r in row)
        html.append("<tr style='font-family:monospace;'><td>{:}</td></tr>".format(html_row))

def jprint(dict_or_json, do_print=True, numbered=False):
    text = yaml.safe_dump(dict_or_json, indent=2, default_flow_style=False)
    if numbered:
        cnt = count(1)
        text = re.sub("^\-", lambda x: str(, text, 0, re.MULTILINE)
    if do_print:
        return text

uprint("Some text")
print("Normal text")

hprint("HTML table")
tprint([[random.choice("ACGT")*3 for _ in range(4)] for _ in range(4)])

uprint("Some JSON or a dict")
jprint({"a":{"b":"c"}, "d":"e", "f":{"g":{"i":"k"}}})
Some text
Normal text
HTML table
Some JSON or a dict
  b: c
d: e
    i: k

Use SVG for plotting

I really like using SVG for custom plots, since it is extremely flexible. For example, you can combine images/photos and data easily. Doing this does require learning some SVG, but it's not so complicated — in many ways matplotlib etc. are much more complex since you often can't get it to do exactly what you want (at least I can't.)

from IPython.display import display, SVG

def show_svg(svgs, width=1000, height=1000):
    SVG_HEAD = '''<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "">'''
    SVG_START = '''<svg width="{w:}px" height="{h:}px" version="1.1" xmlns="" xmlns:xlink= "">'''
    SVG_END = '</svg>'
    return display(SVG(SVG_HEAD + SVG_START.format(w=width, h=height) + svgs + SVG_END))
from random import random
w, h = 500, 390

def box(xy,wh,rgba=(50,50,50,1)):
    return '''<rect x="{}" y="{}" width="{}" height="{}" fill="rgba({:d},{:d},{:d},{:f})" stroke="rgb(0,0,0)" />
        '''.format(xy[0],xy[1], wh[0],wh[1], rgba[0],rgba[1],rgba[2],rgba[3])

svgs = ['<image xlink:href="static/biospace-news-biogen-idec-2.png" x="0" y="0" width="{:d}px" height="{:d}px"/>'.format(w,h)]
svgs += [box((100,140),(200,180), rgba=(0,255,255,.35))]
svgs += ['''<text x="{x:d}" y="{y:d}" text-anchor="middle" font-size="28" fill="rgba(0,255,255,.95)">
         IMPORTANT!</text>'''.format(x=200, y=120)]

show_svg(''.join(svgs), width=w, height=h)
Brian Naughton // Thu 18 June 2015 // Filed under data // Tags virtual biotech cloud labs data

Transforming an E. coli with luciferase on Transcriptic and measuring the result

Read More
Brian Naughton // Sat 09 May 2015 // Filed under data // Tags data analysis bayesian ipython regression mcmc

Comparing some linear regression methods using MCMC

Read More
Brian Naughton // Sun 19 April 2015 // Filed under data // Tags data analysis bayesian ipython notebook jupyter kruschke pymc3

Kruschke's Doing Bayesian Data Analysis with PyMC3 in an IPython notebook

Read More
Brian Naughton // Tue 14 April 2015 // Filed under data // Tags biobanks uk biobank big data data science nih

News about UK Biobank and NIH's new big data site

Read More
Brian Naughton // Tue 24 March 2015 // Filed under data // Tags data analysis bayesian ipython alzheimer biogen drugs

An IPython notebook exploring some Phase Ib data from Biogen on their Alzheimer's drug.

Read More
Brian Naughton // Tue 06 January 2015 // Filed under data // Tags biobank

Biobanks: applying for data and samples

Read More
Brian Naughton // Mon 05 January 2015 // Filed under data // Tags statistics bayesian

BEST is a Bayesian t-test

Read More

Boolean Biotech © Brian Naughton Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More