-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrange.go
59 lines (48 loc) · 1.82 KB
/
range.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
/* 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 "log"
/* -------------------------------------------------------------------------- */
type Range struct {
From, To int
}
/* constructors
* -------------------------------------------------------------------------- */
// Range object used to identify a genomic subsequence. By convention the first
// position in a sequence is numbered 0. The arguments from, to are interpreted
// as the interval [from, to).
func NewRange(from, to int) Range {
if from > to {
log.Fatalf("NewRange(): invalid range, i.e. from > to (from=%d, to=%d)\n", from, to)
}
return Range{from, to}
}
/* -------------------------------------------------------------------------- */
func (r Range) Intersection(s Range) Range {
from := iMax(r.From, s.From)
to := iMin(r.To, s.To)
// this shouldn't happen if r and s overlap
if to < from {
to = from
}
return NewRange(from, to)
}
/* -------------------------------------------------------------------------- */
func (r Range) String() string {
return fmt.Sprintf("[%d %d)", r.From, r.To)
}