Debian Codegraph @

README.md

raw symbols SCIP
1 irelate.go
2 ==========
3
4 Streaming relation (overlap, distance, KNN) testing of (any number of) sorted files of intervals.
5
6 Currently supports BED, BAM, GFF, VCF.
7
8 [![GoDoc] (https://godoc.org/github.com/brentp/irelate?status.png)](https://godoc.org/github.com/brentp/irelate)
9 [![Build Status](https://travis-ci.org/brentp/irelate.svg?branch=master)](https://travis-ci.org/brentp/irelate)
10 [![Coverage Status](https://coveralls.io/repos/brentp/irelate/badge.svg?branch=master)](https://coveralls.io/r/brentp/irelate?branch=master)
11
12 Motivation
13 ----------
14
15 We want to relate (e.g. intersect or by distance) sets of intervals. For example, we may want
16 to report the nearest gene to a set of ChIP-Seq peaks. BEDTools does this extremely well, *irelate*
17 is an attempt to provide an API so that users can write their own tools with little effort in
18 [go](https://golang.org).
19
20 Design
21 ------
22
23 + data-sources must support the *Relatable* Interface. (we provide parsers for common formats).
24 + a user-defined function returns true if 2 *Relatable*'s are related. (only a small number of interval-pairs
25 are sent to be tested--this is handled automatically by `IRelate`.). We provide `CheckRelatedByOverlap`
26 to perform overlap testing.
27 + i.Related() gives access to all of the related intervals (after they are added internally by `IRelate`)
28 + the "API" is a for loop
29 + A parallel chrom-sweep algorithm is used that avoids problems with chromosome order and parallelizes nicely
30 up to about a dozen CPUs.
31
32 Example
33 -------
34
35 (also see [main/main.go](https://github.com/brentp/irelate/blob/master/main/main.go) which
36 is similar to bedtools intersect -sorted -sortout -c)
37
38 print the number of `b` alignments that overlap an interval in `a`
39
40 ```go
41
42 // CheckRelatedByOverlap returns true if Relatables overlap.
43 func CheckRelatedByOverlap(a Relatable, b Relatable) bool {
44 // note with distance == 0 this just overlap.
45 return (b.Start() < a.End()) && (b.Chrom() == a.Chrom())
46 }
47
48 // determine ordering of Relatables.
49 func Less(a Relatable, b Relatable) bool {
50 if a.Chrom() != b.Chrom() {
51 return a.Chrom() < b.Chrom()
52 }
53 return a.Start() < b.Start() // || (a.Start() == b.Start() && a.End() < b.End())
54 }
55
56
57
58 // a and b are channels that send Relatables.
59 a, _ := bix.New('intervals.bed.gz')
60 b, _ := bix.New('some.vcf.gz')
61 for interval := range IRelate(CheckRelatedByOverlap, 0, Less, a, b) {
62 fmt.Fprintf("%s\t%d\t%d\t%d\n", interval.Chrom(), interval.Start(), interval.End(), len(interval.Related()))
63 }
64 ```
65
66 The 2nd argument determines the *query* set of intervals. So,
67 only intervals from `a` (the 0th) source will be sent from IRelate. If this is set to -1, then
68 all intervals from all sources will be sent. After this, any number of interval streams
69 can be passed to `IRelate`
70
71 If we only want to count variants with a given mapping quality, the loop becomes:
72
73 ```go
74 for interval := range IRelate(CheckRelatedByOverlap, 0, Less, a, b) {
75 n := 0
76 for _, b := range interval.Related() {
77 // cast to a bam to ge the mapping quality.
78 if int(b.(*Variant).Score()) > 20 {
79 n += 1
80 }
81 }
82 fmt.Fprintf("%s\t%d\t%d\t%d\n", interval.Chrom(), interval.Start(), interval.End(), n))
83 }
84
85
86 ```
87
88 *note* that *any number* of interval sources are supported even though the example is with 2.
89 We can see the source of each interval with: `interval.Source()`. That value is set automatically inside
90 of irelate.
91
92
93 This is a very simple example, but the point of this is that since the interface is a simple function (as in
94 CheckRelatedByOverlap) and a for loop, it is easy to create custom applications.
95
96 For example, here is the function to relate all intervals within 2KB:
97 ```go
98 // CheckRelatedBy2KB returns true if intervals are within 2KB.
99 func CheckRelatedBy2KB(a Relatable, b Relatable) bool {
100 distance := uint32(2000)
101 // note with distance == 0 this just overlap.
102 return (b.Start()-distance < a.End()) && (b.Chrom() == a.Chrom())
103 }
104
105 ```
106
107 Note that we are guaranteed that b.Start() >= a.Start() so the check is quite
108 simple.
109
110 Relatable
111 ---------
112
113 a key interface in *irelated* is:
114
115 ```go
116 // Relatable provides all the methods for irelate to function.
117 // See Interval in interval.go for a class that satisfies this interface.
118 // Related() likely returns and AddRelated() likely appends to a slice of
119 // relatables. Note that for performance reasons, Relatable should be implemented
120 // as a pointer to your data-structure (see Interval).
121 type Relatable interface {
122 Chrom() string
123 Start() uint32
124 End() uint32
125 Related() []Relatable // A slice of related Relatable's filled by IRelate
126 AddRelated(Relatable) // Adds to the slice of relatables
127 SetSource() uint32 // Internally marks the source (file/stream) of the Relatable
128 }
129 ```
130
131 Performance
132 -----------
133
134 irelate is quite fast, but use PIRelate for parallel intersection. It is less flexible
135 than irelate, but skips parsing of database intervals for sparse regions in the query.
136 In addition, it has very good (automatic) parallelization.