-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrack_simple_bedGraph.go
100 lines (93 loc) · 2.53 KB
/
track_simple_bedGraph.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
/* Copyright (C) 2017 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 "bufio"
import "compress/gzip"
import "fmt"
import "io"
import "os"
import "strconv"
import "strings"
/* -------------------------------------------------------------------------- */
// Import data from wiggle files.
func (track *SimpleTrack) ReadBedGraph(reader io.Reader) error {
fields := []string{}
binsize := track.BinSize
scanner := bufio.NewScanner(reader)
if !scanner.Scan() {
return nil
}
// current sequence and name
cur_seq := []float64{}
cur_name := ""
for scanner.Scan() {
fields = strings.Fields(scanner.Text())
if len(fields) == 0 {
break
}
if len(fields) != 4 {
return fmt.Errorf("ReadBedGraph(): bedGraph file must have four columns!")
}
t1, err := strconv.ParseInt(fields[1], 10, 64); if err != nil {
return err
}
t2, err := strconv.ParseInt(fields[2], 10, 64); if err != nil {
return err
}
t3, err := strconv.ParseFloat(fields[3], 64); if err != nil {
return err
}
if name := fields[0]; name != cur_name {
cur_seq = track.Data[name]
cur_name = name
}
from := int(t1)
to := int(t2)
value := float64(t3)
if from < 0 || to < 0 {
continue
}
if from/binsize >= len(cur_seq) || to/binsize >= len(cur_seq) {
continue
}
for i := from/binsize; i < to/binsize; i++ {
cur_seq[i] = value
}
}
return nil
}
func (track *SimpleTrack) ImportBedGrah(filename string) error {
var r io.Reader
// open file
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
// check if file is gzipped
if isGzip(filename) {
g, err := gzip.NewReader(f)
if err != nil {
return err
}
defer g.Close()
r = g
} else {
r = f
}
return track.ReadBedGraph(r)
}