| 1 | This will serve as documentation for the parallel chromosome-sweep algorithm implemented |
| 2 | in [irelate](https://github.com/brentp/irelate). |
| 3 | |
| 4 | Implementation |
| 5 | -------------- |
| 6 | |
| 7 | `chrom-sweep` is a means of intersecting sorted sets of intervals without using an |
| 8 | interval tree or bin structure (e.g., the UCSC binning algorithm) whose memory |
| 9 | footprint scales poorly with input size. As implemented here in `irelate`, there |
| 10 | are the following steps: |
| 11 | |
| 12 | + create an iterator of parsed intervals (BED/GFF/BAM) from |
| 13 | all (sorted) interval sets (sources) |
| 14 | + merge the intervals via a priority queue, which maintains sort order |
| 15 | + request an interval from the priority queue and insert it into a cache |
| 16 | + check for overlaps with the newest interval and all items in the cache (and add |
| 17 | overlaps to a list of pointers associated with each interval). |
| 18 | + eject a given interval from the cache when it does not intersect the newest |
| 19 | interval to the cache and send that interval to the caller if it was a "query" interval. |
| 20 | |
| 21 | An assumption here is that as soon as an interval in the cache does not overlap the in-coming |
| 22 | interval, then it can not be related to any later intervals that would come in to the cache. |
| 23 | This holds true since the intervals are sorted by chromosome and then by start coordinate. |
| 24 | It also means that we can test, not only for overlaps, but for K-nearest neighbors or |
| 25 | for intervals within a certain distance. For this reason the "overlaps" function can be |
| 26 | specified by the user making the implementation quite flexible. |
| 27 | |
| 28 | Limitations |
| 29 | ----------- |
| 30 | |
| 31 | This algorithm is very fast, as evidenced by the [bedtools2 -sorted behavior](http://bedtools.readthedocs.org/en/latest/#performance) |
| 32 | but it suffers from 3 major problems: |
| 33 | |
| 34 | 1. It relies on having all chromosomes in the same order. This is especially onerous since |
| 35 | VCFs coming out of GATK have order of `1,2,...21,X,Y,MT` while most other sorted files |
| 36 | would put MT before X and Y. Of course sorting the numeric chromosomes as characters or |
| 37 | integers also result in different sort orders. |
| 38 | |
| 39 | 2. It parses many unneeded intervals. Given a sparse query, for example, variants from a few |
| 40 | target genes, and dense databases of whole-genome coverage, the chromosome sweep algorithm |
| 41 | will have to parse *every interval* in the whole-genome databases, even though the areas of |
| 42 | interest are comprised by less than 1% of the regions in the file. In short, sparse queries |
| 43 | with dense databases are a bad-case for chrom-sweep. |
| 44 | |
| 45 | 3. Due to the serial nature, it is not possible to parallelize the intersections. |
| 46 | |
| 47 | Parallel Chromosome Sweep |
| 48 | ------------------------- |
| 49 | |
| 50 | To address these shortcomings, we have developed the parallel chrom-sweep algorithm. |
| 51 | It relies on the algorithm described above, but operates on chunks of the query in |
| 52 | parallel. It does this by parsing the query into an array. It breaks off into a new |
| 53 | array and sends the current one to be processed when one of these is true: |
| 54 | |
| 55 | + the array contains the number of intervals requested by the user (the chunk-size) |
| 56 | + a chromosome change is detected |
| 57 | + a gap (current start minus previous end) of a user-specified size is seen. |
| 58 | |
| 59 | The chunk-size helps to send reasonably even amounts of work to the user in support |
| 60 | of efficient load balancing (i.e., to avoid task divergence). The gap cutoff avoids |
| 61 | putting distant query intervals together in the same array; this helps to parse fewer |
| 62 | database intervals. |
| 63 | |
| 64 | Once an array is complete, it is sent off for the chrom-sweep in parallel as a new array |
| 65 | accumulates; the bounds of the intervals it contains are determined and |
| 66 | those are the basis for a tabix request to each database (or any indexed query). Those |
| 67 | requested regions result in streams of intervals that are sent, along with the query array to |
| 68 | chrom-sweep. This means that only the query chunk is in memory and the database intervals |
| 69 | are retreived from their iterators. This parallelizes quite well up to about a dozen processes |
| 70 | because multiple chromosome-sweeps can be operating as the arrays accumulate. One difficulty |
| 71 | is that we want the output to be sorted; consequently, even though each chunk may finish in any |
| 72 | order, we must restore sorted order to send the intersections back to the caller. |
| 73 | This is likely the reason that we seem to asymptote in speed at about 10-12 processes. |
| 74 | |
| 75 | Implementation |
| 76 | -------------- |
| 77 | |
| 78 | The concepts described above, with some modifications to improve parallelization and |
| 79 | to deal with reality are implemented in the [go programming language](https://golang.org) here in [parallel.go](https://github.com/brentp/irelate/blob/master/parallel.go). |
| 80 | |
| 81 | The function signature is essentially: |
| 82 | |
| 83 | ```go |
| 84 | func PIRelate(arraySize int, maxGap int, qstream interfaces.RelatableIterator, dbs ...interfaces.Queryable) interfaces.RelatableChannel { |
| 85 | ``` |
| 86 | |
| 87 | where: |
| 88 | |
| 89 | + arraySize is the requested chunk size |
| 90 | + maxGap will start a new chunk if a gap that size is seen |
| 91 | + qstram is an iterable of query intervals |
| 92 | + dbs... is any number of databases that are "queryable" (see below). |
| 93 | + and the function returns a channel of intervals (RelatableChannel) where each interval has a list of pointers to database intervals that overlap it. |
| 94 | |
| 95 | Queryable is a golang interface. It is: |
| 96 | |
| 97 | ```go |
| 98 | // Queryable allows querying by genomic position. Anything that meets this interface |
| 99 | // can be used in irelate. |
| 100 | type Queryable interface { |
| 101 | Query(region IPosition) (RelatableIterator, error) |
| 102 | } |
| 103 | ``` |
| 104 | |
| 105 | and a `RelatableIterator` must have these methods: |
| 106 | |
| 107 | ```go |
| 108 | // RelatableIterator provides a method to iterate over Relatables |
| 109 | type RelatableIterator interface { |
| 110 | Next() (Relatable, error) |
| 111 | Close() error |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | So, we are not bound to tabix or any particular file format, we simply have to write go code |
| 116 | for any particular file format that implements those interfaces. |
| 117 | |
| 118 | Using the same `golang` concept of interfaces, we can support VCF, BAM, GFF, etc intervals as long |
| 119 | as a parser can generate `IPositions`--structs that have these methods available: |
| 120 | |
| 121 | ```go |
| 122 | type IPosition interface { |
| 123 | Chrom() string |
| 124 | Start() uint32 |
| 125 | End() uint32 |
| 126 | } |
| 127 | ``` |
| 128 | |
| 129 | It's easy to actually implement these 3 methods for any genomic interval so adding additional file |
| 130 | formats is trivial; we arent tied to any particular set. |