-
Notifications
You must be signed in to change notification settings - Fork 1
/
granges_findOverlaps.go
177 lines (153 loc) · 4.54 KB
/
granges_findOverlaps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright (C) 2016 Philipp Benner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gonetics
/* -------------------------------------------------------------------------- */
import "fmt"
import "sort"
/* -------------------------------------------------------------------------- */
type endPoint struct {
position int
start *endPoint
end *endPoint
srcIdx int
isQuery bool
}
func (obj *endPoint) isStart() bool {
return obj.start == nil
}
func (obj *endPoint) isEnd() bool {
return obj.end == nil
}
func (obj *endPoint) getStart() int {
if obj.start == nil {
return obj.position
} else {
return obj.start.position
}
}
func (obj *endPoint) getEnd() int {
if obj.end == nil {
return obj.position
} else {
return obj.end.position
}
}
func (obj *endPoint) String() string {
return fmt.Sprintf("<%d,%d>", obj.getStart(), obj.getEnd())
}
/* endPointList
* -------------------------------------------------------------------------- */
type endPointList []endPoint
func NewEndPointList() endPointList {
r := []endPoint{}
return r
}
func (r endPointList) Len() int {
return len(r)
}
func (r endPointList) Less(i, j int) bool {
if r[i].position != r[j].position {
return r[i].position < r[j].position
}
// if the positions equal, give priority to the beginning of a range
if r[i].start == nil && r[j].start != nil {
return true
} else {
return false
}
}
func (r endPointList) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (s *endPointList) Append(r endPoint) {
(*s) = append(*s, r)
}
func (s *endPointList) Remove(r endPoint) {
for i := 0; i < len(*s); i++ {
if (*s)[i] == r {
(*s) = append((*s)[0:i], (*s)[i+1:len(*s)]...)
}
}
}
/* FindOverlaps
* -------------------------------------------------------------------------- */
func findOverlapsEntry(queryHits, subjectHits []int, entry endPointList) ([]int, []int) {
queryList := NewEndPointList()
subjectList := NewEndPointList()
for _, r := range entry {
if r.isQuery {
if r.start == nil {
queryList.Append(r)
// record overlaps (all elements in subjectList overlap with this
// position)
for i := 0; i < len(subjectList); i++ {
queryHits = append( queryHits, r.srcIdx)
subjectHits = append(subjectHits, subjectList[i].srcIdx)
}
} else {
// remove start position from stack
queryList.Remove(*r.start)
}
} else {
if r.start == nil {
subjectList.Append(r)
// record overlaps (all elements in queryList overlap with this
// position)
for i := 0; i < len(queryList); i++ {
queryHits = append( queryHits, queryList[i].srcIdx)
subjectHits = append(subjectHits, r.srcIdx)
}
} else {
// remove start position from stack
subjectList.Remove(*r.start)
}
}
}
return queryHits, subjectHits
}
func FindOverlaps(query, subject GRanges) ([]int, []int) {
n := query.Length()
m := subject.Length()
queryHits := []int{}
subjectHits := []int{}
rmap := make(map[string]endPointList)
// fill map
for i := 0; i < n; i++ {
start := endPoint{query.Ranges[i].From, nil, nil, i, true}
end := endPoint{query.Ranges[i].To-1, &start, nil, i, true}
entry := rmap[query.Seqnames[i]]
entry = append(entry, start)
entry = append(entry, end)
rmap[query.Seqnames[i]] = entry
}
for i := 0; i < m; i++ {
start := endPoint{subject.Ranges[i].From, nil, nil, i, false}
end := endPoint{subject.Ranges[i].To-1, &start, nil, i, false}
entry := rmap[subject.Seqnames[i]]
entry = append(entry, start)
entry = append(entry, end)
rmap[subject.Seqnames[i]] = entry
}
// sort map entries
for _, entry := range rmap {
sort.Sort(entry)
}
// find overlaps
for _, entry := range rmap {
queryHits, subjectHits = findOverlapsEntry(queryHits, subjectHits, entry)
}
return queryHits, subjectHits
}