| 1 | // Streaming relation (overlap, distance, KNN) testing of (any number of) sorted files of intervals. |
| 2 | package irelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ |
| 3 | |
| 4 | import ( |
| 5 | "container/heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 7 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 8 | "logscip-go gomod github.com/golang/go/src go1.23.0 log/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.23.0 os/" |
| 10 | |
| 11 | . "github.com/brentp/irelate/interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/" |
| 12 | ) |
| 13 | |
| 14 | // Set relativeTo so SelfRelations constant to allow reporting overlaps within a stream |
| 15 | const SelfRelationsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/SelfRelations. = -2 |
| 16 | |
| 17 | func relatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relate().(a Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, relativeTo int) { |
| 18 | if relativeTo == SelfRelationsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/SelfRelations. { |
| 19 | a.AddRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#AddRelated.(b) |
| 20 | b.AddRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#AddRelated.(a) |
| 21 | return |
| 22 | } |
| 23 | if a.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.() != b.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.() { |
| 24 | if relativeTo == -1 { |
| 25 | a.AddRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#AddRelated.(b) |
| 26 | b.AddRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#AddRelated.(a) |
| 27 | } else { |
| 28 | if uint32(relativeTo) == a.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.() { |
| 29 | a.AddRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#AddRelated.(b) |
| 30 | } |
| 31 | if uint32(relativeTo) == b.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.() { |
| 32 | b.AddRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#AddRelated.(a) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func Lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/Less().(a Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 39 | if a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.() != b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.() { |
| 40 | return a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.() < b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.() |
| 41 | } |
| 42 | return a.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() // || (a.Start() == b.Start() && a.End() < b.End()) |
| 43 | } |
| 44 | |
| 45 | // 1, 2, 3 ... 9, 10, 11... |
| 46 | func NaturalLessPrefixscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/NaturalLessPrefix().(a Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 47 | if !SameChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/SameChrom().(a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.(), b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) { |
| 48 | return NaturalLessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/NaturalLess().(StripChrscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/StripChr().(a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()), StripChrscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/StripChr().(b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.())) |
| 49 | } |
| 50 | return a.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() || (a.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() == b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() && a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() < b.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.()) |
| 51 | |
| 52 | } |
| 53 | |
| 54 | // 1, 10, 11... 19, 2, 20, 21 ... |
| 55 | func LessPrefixscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/LessPrefix().(a Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 56 | if !SameChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/SameChrom().(a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.(), b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) { |
| 57 | return StripChrscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/StripChr().(a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) < StripChrscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/StripChr().(b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) |
| 58 | } |
| 59 | return a.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() || (a.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() == b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() && a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() < b.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.()) |
| 60 | } |
| 61 | |
| 62 | // CheckRelatedByOverlap returns true if Relatables overlap. |
| 63 | func CheckRelatedByOverlapscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/CheckRelatedByOverlap().(a Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 64 | return (b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.()) && (b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.() == a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) |
| 65 | // note with distance == 0 this just overlap. |
| 66 | //distance := uint32(0) |
| 67 | //return (b.Start()-distance < a.End()) && (b.Chrom() == a.Chrom()) |
| 68 | } |
| 69 | |
| 70 | // handles chromomomes like 'chr1' from one org and '1' from another. |
| 71 | func CheckOverlapPrefixscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/CheckOverlapPrefix().(a Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 72 | if b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() { |
| 73 | return SameChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/SameChrom().(a.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.(), b.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) |
| 74 | } |
| 75 | return false |
| 76 | } |
| 77 | |
| 78 | // CheckKNN relates an interval to its k-nearest neighbors. |
| 79 | // The reporting function will have to do some filtering since this is only |
| 80 | // guaranteed to associate *at least* k neighbors, but it could be returning extra. |
| 81 | func CheckKNNscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/CheckKNN().(a Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 82 | // the first n checked would be the n_closest, but need to consider ties |
| 83 | // the report function can decide what to do with them. |
| 84 | k := 4 |
| 85 | r := a.Relatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Related.() |
| 86 | if len(r) >= k { |
| 87 | // TODO: double-check this. |
| 88 | return r[len(r)-1].Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.()-a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() < b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.()-a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() |
| 89 | } |
| 90 | return true |
| 91 | } |
| 92 | |
| 93 | // filter rewrites the input-slice to remove nils. |
| 94 | func filterscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/filter().(s []Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, nils int) []Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable# { |
| 95 | j := 0 |
| 96 | if len(s) != nils { |
| 97 | |
| 98 | for _, v := range s { |
| 99 | if v != nil { |
| 100 | s[j] = v |
| 101 | j++ |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | for k := j; k < len(s); k++ { |
| 106 | s[k] = nil |
| 107 | } |
| 108 | return s[:j] |
| 109 | } |
| 110 | |
| 111 | type irelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate# struct { |
| 112 | checkRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#checkRelated. func(a, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool |
| 113 | // relativeTo indicates which stream is the query stream. A value of -1 means |
| 114 | // all vs all. A value of -2 reports overlaps even within the same stream. |
| 115 | relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#relativeTo. int |
| 116 | lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#less. func(a, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool |
| 117 | // cache holds the set of Relatables we must test for overlap. A Relatable |
| 118 | // is ejected from the cache when it is not related to the interval that's |
| 119 | // about to be added. |
| 120 | cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache. []Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable# |
| 121 | // an item eject from the cache gets put on the sendQ if it's from the query |
| 122 | // stream. |
| 123 | sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ. *relatableQueuescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue# |
| 124 | // mergeStream creates a single (sorted) stream of all incoming intervals. |
| 125 | mergeStreamscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#mergeStream. RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator# |
| 126 | //merger RelatableChannel |
| 127 | nilsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#nils. int |
| 128 | } |
| 129 | |
| 130 | // IRelate provides the basis for flexible overlap/proximity/k-nearest neighbor |
| 131 | // testing. IRelate receives merged, ordered Relatables via stream and takes |
| 132 | // function that checks if they are related (see CheckRelatedByOverlap). |
| 133 | // It is guaranteed that !Less(b, a) is true (we can't guarantee that Less(a, b) |
| 134 | // is true since they may have the same start). Once checkRelated returns false, |
| 135 | // it is assumed that no other `b` Relatables could possibly be related to `a` |
| 136 | // and so `a` is sent to the returnQ. |
| 137 | // streams are a variable number of iterators that send intervals. |
| 138 | func IRelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/IRelate().(checkRelated func(a, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool, |
| 139 | relativeTo int, |
| 140 | less func(a, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool, |
| 141 | streams ...RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#) RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator# { |
| 142 | |
| 143 | mergeStream := newMergerscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/newMerger().(less, relativeTo, streams...) |
| 144 | |
| 145 | ir := &irelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#{checkRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#checkRelated.: checkRelated, relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#relativeTo.: relativeTo, |
| 146 | mergeStreamscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#mergeStream.: mergeStream, |
| 147 | cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache.: make([]Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 0, 1024), sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ.: &relatableQueuescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue#{make([]Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 0, 1024), less}, |
| 148 | lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#less.: less} |
| 149 | return ir |
| 150 | } |
| 151 | |
| 152 | func (ir *irelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#) Closescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#Close().() error { |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | func (ir *irelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#) Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#Next().() (Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, error) { |
| 157 | |
| 158 | for { |
| 159 | interval, err := ir.mergeStreamscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#mergeStream..Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Next.() |
| 160 | if err == ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. { |
| 161 | break |
| 162 | } |
| 163 | // check the interval against everything in the cache. |
| 164 | for i, c := range ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache. { |
| 165 | if c == nil { |
| 166 | continue |
| 167 | } |
| 168 | if ir.checkRelatedscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#checkRelated.(c, interval) { |
| 169 | relatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relate().(c, interval, ir.relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#relativeTo.) |
| 170 | } else { |
| 171 | // if it's not related, we remove it from the cache |
| 172 | // if it's a query interval, we push it onto the sendQ. |
| 173 | if ir.relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#relativeTo. < 0 || int(c.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.()) == ir.relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#relativeTo. { |
| 174 | heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/.Pushscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/Push().(ir.sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ., c) |
| 175 | } |
| 176 | ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache.[i] = nil |
| 177 | ir.nilsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#nils.++ |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // only do this when we have a lot of nils as it's expensive to create a new slice. |
| 182 | // nils are spaces that we've removed from the cache. |
| 183 | if ir.nilsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#nils. < 2 { |
| 184 | ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache. = append(ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache., interval) |
| 185 | continue |
| 186 | } |
| 187 | // remove nils from the cache (must do this before sending) |
| 188 | ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache., ir.nilsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#nils. = filterscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/filter().(ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache., ir.nilsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#nils.), 0 |
| 189 | var o Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable# |
| 190 | if len(ir.sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ..relsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue#rels.) > 0 { |
| 191 | o = ir.sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ..relsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue#rels.[0] |
| 192 | } |
| 193 | // if the first thing in the sendQ is less than the first thing in the cache |
| 194 | // then we can send Pop the lowest thing off the sendQ. |
| 195 | // otherwise, we continue to read from the stream. |
| 196 | if o != nil && (len(ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache.) == 0 || ir.lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#less.(o, ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache.[0])) { |
| 197 | ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache. = append(ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache., interval) |
| 198 | return heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/.Popscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/Pop().(ir.sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ.).(Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#), nil |
| 199 | } |
| 200 | ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache. = append(ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache., interval) |
| 201 | } |
| 202 | // stream is done so we empty the cache by pushing onto the sendQ |
| 203 | if len(ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache.) > 0 { |
| 204 | ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache., ir.nilsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#nils. = filterscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/filter().(ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache., ir.nilsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#nils.), 0 |
| 205 | for _, c := range ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache. { |
| 206 | if ir.relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#relativeTo. < 0 || int(c.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.()) == ir.relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#relativeTo. { |
| 207 | heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/.Pushscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/Push().(ir.sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ., c) |
| 208 | } |
| 209 | } |
| 210 | ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache. = ir.cachescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#cache.[:0] |
| 211 | } |
| 212 | // ... then we clear the sendQ |
| 213 | if len(ir.sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ..relsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue#rels.) > 0 { |
| 214 | return heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/.Popscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/Pop().(ir.sendQscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/irelate#sendQ.).(Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#), nil |
| 215 | } |
| 216 | return nil, ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. |
| 217 | } |
| 218 | |
| 219 | type mergerscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger# struct { |
| 220 | lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#less. func(a, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool |
| 221 | relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#relativeTo. int |
| 222 | streamsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#streams. []RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator# |
| 223 | qscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#q. relatableQueuescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue# |
| 224 | seenscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#seen. map[string]struct{} |
| 225 | jscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#j. int |
| 226 | lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom. string |
| 227 | verbosescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#verbose. bool |
| 228 | } |
| 229 | |
| 230 | func newMergerscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/newMerger().(less func(a, b Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool, relativeTo int, streams ...RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#) *mergerscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger# { |
| 231 | q := relatableQueuescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue#{make([]Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 0, len(streams)), less} |
| 232 | verbose := osscip-go gomod github.com/golang/go/src go1.23.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.23.0 os/Getenv().("IRELATE_VERBOSE") == "TRUE" |
| 233 | |
| 234 | for i, stream := range streams { |
| 235 | interval, err := stream.Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Next.() |
| 236 | if err != nil && err != ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. { |
| 237 | panic(err) |
| 238 | } |
| 239 | if interval != nil { |
| 240 | interval.SetSourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#SetSource.(uint32(i)) |
| 241 | heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/.Pushscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/Push().(&q, interval) |
| 242 | } |
| 243 | if err == ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. { |
| 244 | stream.Closescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Close.() |
| 245 | } |
| 246 | } |
| 247 | m := &mergerscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#{lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#less.: less, relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#relativeTo.: relativeTo, streamsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#streams.: streams, qscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#q.: q, seenscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#seen.: make(map[string]struct{}), jscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#j.: -1000, lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom.: "", verbosescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#verbose.: verbose} |
| 248 | |
| 249 | return m |
| 250 | } |
| 251 | |
| 252 | func (m *mergerscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#) Closescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#Close().() error { |
| 253 | return nil |
| 254 | } |
| 255 | |
| 256 | func (m *mergerscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#) Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#Next().() (Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, error) { |
| 257 | if len(m.qscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#q..relsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/relatableQueue#rels.) == 0 { |
| 258 | return nil, ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. |
| 259 | } |
| 260 | interval := heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/.Popscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/Pop().(&m.qscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#q.).(Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) |
| 261 | source := interval.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.() |
| 262 | if !SameChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/SameChrom().(interval.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.(), m.lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom.) { |
| 263 | if m.verbosescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#verbose. && m.lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom. != "" { |
| 264 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Printfscip-go gomod github.com/golang/go/src go1.23.0 log/Printf().("on chromosome: %s\n", m.lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom.) |
| 265 | } |
| 266 | m.lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom. = StripChrscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/StripChr().(interval.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) |
| 267 | if _, ok := m.seenscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#seen.[m.lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom.]; ok { |
| 268 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Printlnscip-go gomod github.com/golang/go/src go1.23.0 log/Println().("warning: chromosomes must be in different order between files or the chromosome sort order is not as expected.") |
| 269 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Printfscip-go gomod github.com/golang/go/src go1.23.0 log/Printf().("warning: overlaps will likely be missed after this chrom: %s from source: %d\n", m.lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom., interval.Sourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#Source.()) |
| 270 | } |
| 271 | m.seenscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#seen.[m.lastChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#lastChrom.] = struct{}{} |
| 272 | } |
| 273 | // pull the next interval from the same source. |
| 274 | next_interval, err := m.streamsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#streams.[source].Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Next.() |
| 275 | if err == nil { |
| 276 | if next_interval.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < interval.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() { |
| 277 | if SameChromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/SameChrom().(next_interval.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.(), interval.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.()) { |
| 278 | panic(fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Sprintf().("intervals out of order within file: starts at: %d and %d from source: %d", interval.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.(), next_interval.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.(), source)) |
| 279 | } |
| 280 | } |
| 281 | next_interval.SetSourcescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#SetSource.(source) |
| 282 | heapscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/.Pushscip-go gomod github.com/golang/go/src go1.23.0 `container/heap`/Push().(&m.qscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#q., next_interval) |
| 283 | m.jscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#j.-- |
| 284 | if m.jscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#j. == 0 { |
| 285 | return nil, ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. |
| 286 | } |
| 287 | } else { |
| 288 | if int(source) == m.relativeToscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#relativeTo. { |
| 289 | // we pull in 200K more records and then stop. to make sure we get anything that might |
| 290 | // relate to last query |
| 291 | m.jscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#j. = 200000 |
| 292 | } |
| 293 | } |
| 294 | if err == ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. { |
| 295 | m.streamsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/merger#streams.[source].Closescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Close.() |
| 296 | } |
| 297 | return interval, nil |
| 298 | } |