-
Notifications
You must be signed in to change notification settings - Fork 1
/
peaks.go
162 lines (138 loc) · 4.54 KB
/
peaks.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
/* 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 "bufio"
import "os"
import "io"
import "strconv"
import "strings"
/* -------------------------------------------------------------------------- */
type GPeaks struct {
GRanges
}
/* constructors
* -------------------------------------------------------------------------- */
func NewGPeaks(seqnames []string, from, to, absSummit []int, pileup, pvalue, foldEnrichment, qvalue []float64) GPeaks {
r := NewGRanges(seqnames, from, to, []byte{})
r.AddMeta("abs_summit", absSummit)
r.AddMeta("pileup", pileup)
r.AddMeta("-log10(pvalue)", pvalue)
r.AddMeta("fold_enrichment", foldEnrichment)
r.AddMeta("-log10(qvalue)", qvalue)
return GPeaks{r}
}
/* -------------------------------------------------------------------------- */
func (p GPeaks) AbsSummit() []int {
return p.GetMetaInt("abs_summit")
}
func (p GPeaks) Pileup() []float64 {
return p.GetMetaFloat("pileup")
}
func (p GPeaks) Pvalue() []float64 {
return p.GetMetaFloat("-log10(pvalue)")
}
func (p GPeaks) FoldEnrichment() []float64 {
return p.GetMetaFloat("fold_enrichment")
}
func (p GPeaks) Qvalue() []float64 {
return p.GetMetaFloat("-log10(qvalue)")
}
/* i/o
* -------------------------------------------------------------------------- */
func ReadXlsPeaks(r io.Reader) (GPeaks, error) {
var gpeaks GPeaks
// check if we already saw the header
header := false
seqnames := []string{}
from := []int{}
to := []int{}
absSummit := []int{}
pileup := []float64{}
pvalue := []float64{}
foldEnrichment := []float64{}
qvalue := []float64{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) == 0 {
continue
}
if fields[0] == "#" {
continue
}
if len(fields) != 10 {
return gpeaks, fmt.Errorf("invalid peaks xls file")
}
if header == false {
// first line is the header
if fields[0] != "chr" || fields[1] != "start" || fields[2] != "end" ||
(fields[4] != "abs_summit" || fields[5] != "pileup" || fields[6] != "-log10(pvalue)") ||
(fields[7] != "fold_enrichment" || fields[8] != "-log10(qvalue)") {
return gpeaks, fmt.Errorf("Invalid Xls header!")
}
header = true
continue
}
t1, e := strconv.ParseInt(fields[1], 10, 64) // from
if e != nil {
return gpeaks, e
}
t2, e := strconv.ParseInt(fields[2], 10, 64) // to
if e != nil {
return gpeaks, e
}
t3, e := strconv.ParseInt(fields[4], 10, 64) // abs_summit
if e != nil {
return gpeaks, e
}
t4, e := strconv.ParseFloat(fields[5], 64) // pileup
if e != nil {
return gpeaks, e
}
t5, e := strconv.ParseFloat(fields[6], 64) // pvalue
if e != nil {
return gpeaks, e
}
t6, e := strconv.ParseFloat(fields[7], 64) // fold_enrichment
if e != nil {
return gpeaks, e
}
t7, e := strconv.ParseFloat(fields[8], 64) // qvalue
if e != nil {
return gpeaks, e
}
seqnames = append(seqnames, fields[0])
from = append(from, int(t1))
to = append(to, int(t2)+1)
absSummit = append(absSummit, int(t3))
pileup = append(pileup, float64(t4))
pvalue = append(pvalue, float64(t5))
foldEnrichment = append(foldEnrichment, float64(t6))
qvalue = append(qvalue, float64(t7))
}
gpeaks = NewGPeaks(seqnames, from, to, absSummit, pileup, pvalue, foldEnrichment, qvalue)
return gpeaks, nil
}
func ImportXlsPeaks(filename string) (GPeaks, error) {
f, err := os.Open(filename)
if err != nil {
return GPeaks{}, err
}
defer f.Close()
return ReadXlsPeaks(f)
}