-
Notifications
You must be signed in to change notification settings - Fork 1
/
track_lazy.go
146 lines (121 loc) · 4.01 KB
/
track_lazy.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
/* 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 "io"
import "os"
/* -------------------------------------------------------------------------- */
type LazyTrack struct {
Name string
BinSize int
BinOverlap int
Bwr BigWigReader
BinSumStat BinSummaryStatistics
Init float64
}
/* constructor
* -------------------------------------------------------------------------- */
func NewLazyTrack(reader io.ReadSeeker, name string, f BinSummaryStatistics, binSize, binOverlap int, init float64) (LazyTrack, error) {
bwr, err := NewBigWigReader(reader); if err != nil {
return LazyTrack{}, err
}
if binSize == 0 {
if b, err := bwr.GetBinSize(); err != nil {
return LazyTrack{}, err
} else {
binSize = b
}
}
return LazyTrack{name, binSize, binOverlap, *bwr, f, init}, nil
}
/* -------------------------------------------------------------------------- */
func (track LazyTrack) Clone() LazyTrack {
r := track
return r
}
func (track LazyTrack) CloneTrack() Track {
return track.Clone()
}
/* access methods
* -------------------------------------------------------------------------- */
func (track LazyTrack) GetBinSize() int {
return track.BinSize
}
func (track LazyTrack) GetName() string {
return track.Name
}
func (track LazyTrack) GetSeqNames() []string {
return track.Bwr.Genome.Seqnames
}
func (track LazyTrack) GetGenome() Genome {
return track.Bwr.Genome
}
func (track LazyTrack) GetSequence(query string) (TrackSequence, error) {
if seq, binSize, err := track.Bwr.QuerySequence(query, track.BinSumStat, track.BinSize, track.BinOverlap, track.Init); err != nil {
return TrackSequence{}, err
} else {
return TrackSequence{seq, binSize}, nil
}
}
func (track LazyTrack) GetSlice(r GRangesRow) ([]float64, error) {
binFrom := r.Range.From/track.BinSize
binTo := r.Range.To /track.BinSize
seq := make([]float64, binTo-binFrom)
for s := range track.Bwr.Query(r.Seqname, r.Range.From, r.Range.To, track.BinSize) {
if s.Error != nil {
return nil, s.Error
}
for i := s.From; i < s.To; i += track.BinSize {
if j := (i-r.Range.From)/track.BinSize; j < len(seq) {
seq[j] = s.Sum/s.Valid
}
}
}
return seq, nil
}
func (track *LazyTrack) FilterGenome(f func(name string, length int) bool) {
track.Bwr.Genome = track.Bwr.Genome.Filter(f)
}
/* -------------------------------------------------------------------------- */
func (track *LazyTrack) ReadBigWig(reader io.ReadSeeker, name string, f BinSummaryStatistics, binSize, binOverlap int, init float64) error {
if tmp, err := NewLazyTrack(reader, name, f, binSize, binOverlap, init); err != nil {
return err
} else {
*track = tmp
}
return nil
}
/* -------------------------------------------------------------------------- */
type LazyTrackFile struct {
LazyTrack
f *os.File
}
func (obj *LazyTrackFile) ImportBigWig(filename, name string, s BinSummaryStatistics, binSize, binOverlap int, init float64) error {
f, err := OpenBigWigFile(filename)
if err != nil {
return err
}
if tmp, err := NewLazyTrack(f, name, s, binSize, binOverlap, init); err != nil {
return err
} else {
obj.LazyTrack = tmp
}
return nil
}
func (obj *LazyTrackFile) Close() error {
return obj.f.Close()
}