| Andrew Cooke | Contents | Latest | RSS | Twitter | Previous | Next


Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Lepl parser for Python.

Colorless Green.

Photography around Santiago.

SVG experiment.

Professional Portfolio

Calibration of seismometers.

Data access via web services.

Cache rewrite.

Extending OpenSSH.

C-ORM: docs, API.

Last 100 entries

And Smugness; McCloskey Economics Trilogy; cmocka - Mocks for C; Concept Creep (Americans); Futhark - OpenCL Language; Moved / Gone; Fan and USB issues; Burgers in Santiago; The Origin of Icosahedral Symmetry in Viruses; autoenum on PyPI; Jars Explains; Tomato Chutney v3; REST; US Elections and Gender: 24 Point Swing; PPPoE on OpenSuse Leap 42.1; SuperMicro X10SDV-TLN4F/F with Opensuse Leap 42.1; Big Data AI Could Be Very Bad Indeed....; Cornering; Postcapitalism (Paul Mason); Black Science Fiction; Git is not a CDN; Mining of Massive Data Sets; Rachel Kaadzi Ghansah; How great republics meet their end; Raspberry, Strawberry and Banana Jam; Interesting Dead Areas of Math; Later Taste; For Sale; Death By Bean; It's Good!; Tomato Chutney v2; Time ATAC MX 2 Pedals - First Impressions; Online Chilean Crafts; Intellectual Variety; Taste + Texture; Time Invariance and Gauge Symmetry; Jodorowsky; Tomato Chutney; Analysis of Support for Trump; Indian SF; TP-Link TL-WR841N DNS TCP Bug; TP-Link TL-WR841N as Wireless Bridge; Sending Email On Time; Maybe run a command; Sterile Neutrinos; Strawberry and Banana Jam; The Best Of All Possible Worlds; Kenzaburo Oe: The Changeling; Peach Jam; Taste Test; Strawberry and Raspberry Jam; flac to mp3 on OpenSuse 42.1; Also, Sebald; Kenzaburo Oe Interview; Otake (Kitani Minoru) move Black 121; Is free speech in British universities under threat?; I am actually good at computers; Was This Mansplaining?; WebFaction / LetsEncrypt / General Disappointment; Sensible Philosophy of Science; George Ellis; Misplaced Intuition and Online Communities; More Reading About Japan; Visibilty / Public Comments / Domestic Violence; Ferias de Santiago; More (Clearly Deliberate); Deleted Obit Post; And then a 50 yo male posts this...; We Have Both Kinds Of Contributors; Free Springer Books; Books on Religion; Books on Linguistics; Palestinan Electronica; Books In Anthropology; Taylor Expansions of Spacetime; Info on Juniper; Efficient Stream Processing; The Moral Character of Crypto; Hearing Aid Info; Small Success With Go!; Re: Quick message - This link is broken; Adding Reverb To The Echo Chamber; Sox Audio Tools; Would This Have Been OK?; Honesty only important economically before institutions develop; Stegangraphy via PS4; OpenCL Mess; More Book Recommendations; Good Explanation of Difference Between Majority + Minority; Musical Chairs - Who's The Privileged White Guy; I can see straight men watching this conversation and laffing; Meta Thread Defending POC Causes POC To Close Account; Indigenous People Of Chile; Curry Recipe; Interesting Link On Marginality; A Nuclear Launch Ordered, 1962; More Book Recs (Better Person); It's Nuanced, And I Tried, So Back Off; Marx; The Negative Of Positive; Jenny Holzer Rocks

© 2006-2015 Andrew Cooke (site) / post authors (content).

Matching DNA Update - Faster Java Code

From: "andrew cooke" <andrew@...>

Date: Sat, 20 Sep 2008 18:54:39 -0400 (CLT)

I have just finished implementing the main core of the algorithm outlined
here - http://www.acooke.org/cute/Identifyin0.html - directly in Java and
it runs in about 8 seconds!

There were two main problems.  First, inferring how Postgres did an
efficient search and, second, implementing that without using too much
memory (my first attempt exhausted the heap so I now have a slight 
tradeoff, which uses a sort to avoid creating more memory structures and
so adds a log term to the big-O).  It's easiest to describe both together,
by outlining the final solution, but in practice I the development had two
distinct steps.

So, as in the prototype code, I generate candidate pairs by matching small
fragments of the DNA.  More exactly: I take 25 fragments, each 8 bits,
from each individual and I categorise two individuals as a candidate pair
if they have at least 3 fragments in common.

So, in psuedocode, I do the following:

 generate a table of fragments[individual_idx][fragment_idx]
 generate a table of counts[individual1_idx][individual2_idx] = 0

 for each column of fragments in turn:
   sort the table column containing the fragments;
   scan the sorted column:
     for all fragments with the same value:
       increment the counts associated with the pairs of individuals
               that share that fragment value;
     if any count == 3:
       if the "bit distance" between the pair is < 3000:
         add the pair for that count to the graph;

And I need to repeat this 6 times with different sets of fragments (the
number of identified pairs after each set is 8116, 9623, 9935, 9988, 9998,

Instead of sorting each column of fragments the scan could be direct, but
you would need to have a separate memory structure to record which
individuals were associated with which values (for this amount of data I
suspect the log pays for itself in the simplification (reduced constant
cost) that the sorted data introduces).

Also, Java has no direct support for sorting bytes (the fragments) with
keys.  I could have wrapped everything in objects, but it was more compact
(and probably faster) to bit-pack the DNA fragment and the individual
index together in a single integer (obviously the DNA has to occupy the
more significant bits for the sorting to give the corrected order).

I am going to look for a graph library now to finish this off.

8 seconds is pretty good.  When I started out I was looking at many hours;
even the optimized Python/SQL code took 30 min...


PS  My initial attempt at searching the hashes was to do a depth first
search trying to find common fragments for each pair in turn.  While this
would have fitted well within a constraint programming framework (see my
posts here over the last week or two when I was looking at Choco and
Gecode) it was, in retrospect, completely stupid - a huge amount of time
is spent exhaustively searching irrelevant pairs.  The direct scan
described above is much more efficient, but it's not yet clear to me how
the two approaches are related.  Is there some way in which the direct
scan with counting is a dual of the search?  Or does some kind of
optimisation of the search eventually reduce it to the scan?  I don't see
how either of those pan out, but haven't looked at the CP techniques in
any detail yet.

Core Routine

From: "andrew cooke" <andrew@...>

Date: Sat, 20 Sep 2008 19:07:14 -0400 (CLT)

public int search()
  byte[] counts = new byte[GenomePair.hashSize(population.size())];
  int[] scratch = new int[population.size()];
  // for each fragment in turn:
  for (int column = 0; column < nHashes; ++column) {
    // pack into an integer
    for (int row = 0; row < population.size(); ++row) {
      scratch[row] = pack(hashes[row][column], row);
    // group individuals with the same hash are together
    // for each group
    for (int row = 0; row < population.size();) {
      // get the hash for the group
      byte hash = unpackHash(scratch[row]);
      Set<Integer> allMatching = new HashSet<Integer>();
      // note the first individual
      // for each additional individual
      while (++row < population.size() &&
          unpackHash(scratch[row]) == hash) {
        int higher = unpackRow(scratch[row]);
        // for each pair
        for (int lower: allMatching) {
          GenomePair pair = new GenomePair(lower, higher);
          // if we have sufficient hits, check the distance
          if (++counts[pair.hashCode()] == nMatches
              && population.connected(pair, cutoff)) {
        // extend the current set so that we generate all pairs
  // this should tend to to population.size()-1
  return graph.size();

Perfect Hash

From: "andrew cooke" <andrew@...>

Date: Sat, 20 Sep 2008 19:10:22 -0400 (CLT)

I should explain that I am abusing GenomePair.hashCode() - the
implementation returns a continguous index from 0 over all possible pairs.
 So all pairs are distinct and there are no gaps.  The total number of
values is given by hashSize().

At some point I'll change the name.  Originally I was using HashMaps of


Same Results

From: "andrew cooke" <andrew@...>

Date: Sun, 21 Sep 2008 20:05:00 -0400 (CLT)

I added the final graph code (using JGraphT, which seems quite capable)
and the results are, as expected, identical to the earlier work.  I also
tried some variations on the numbers of matches and hashes (but not the
fragment size, which is hard coded at 8 bits (ie bytes) in this version) -
the code is much more stable than the Python/SQL implementation to these
changes (I now suspect Postgres was switching algorithms depending on
predicted memory usage), and the values chosen aren't particularly

I'm considering sending it off to the company that posted the problem, but
they only accept submissions that are employment applications, so it seems
a bit silly (I'm not looking for a job, and won't move to Boston...).


Comment on this post