-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgranges_gtf.go
356 lines (337 loc) · 10.1 KB
/
granges_gtf.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* 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 "compress/gzip"
import "os"
import "io"
import "strconv"
import "strings"
import "unicode"
/* i/o
* -------------------------------------------------------------------------- */
type gtfOptional map[string]interface{}
type gtfTypeMap map[string]string
type gtfDefaults map[string]interface{}
func readGTFParseOptional(fields []string, gtfOpt gtfOptional, typeMap gtfTypeMap, gtfDef gtfDefaults, length int) (gtfOptional, error) {
if len(fields) % 2 == 1 {
return nil, fmt.Errorf("ReadGTF(): invalid file format!")
}
// loop through list
for i := 0; i < len(fields); i += 2 {
name := fields[i]
valueStr := fields[i+1]
if _, ok := typeMap[name]; ok {
// get the data vector
switch v := gtfOpt[name].(type) {
case []int:
if value, err := strconv.ParseInt(valueStr, 10, 64); err != nil {
return nil, err
} else {
v = append(v, int(value))
}
gtfOpt[name] = v
case []float64:
if value, err := strconv.ParseFloat(valueStr, 64); err != nil {
return nil, err
} else {
v = append(v, value)
}
gtfOpt[name] = v
case []string:
v = append(v, valueStr)
gtfOpt[name] = v
}
}
}
// check that all optional fields are available
for name, values := range gtfOpt {
thisLength := 0
switch v := values.(type) {
case []int : thisLength = len(v)
case []float64: thisLength = len(v)
case []string : thisLength = len(v)
}
if thisLength < length {
if defVal, ok := gtfDef[name]; ok {
switch v := values.(type) {
case []int : v = append(v, defVal.(int)) ; gtfOpt[name] = v
case []float64: v = append(v, defVal.(float64)) ; gtfOpt[name] = v
case []string : v = append(v, defVal.(string)) ; gtfOpt[name] = v
}
} else {
return nil, fmt.Errorf("optional field `%s' is missing at line `%d' with no default", name, length+1)
}
}
}
return gtfOpt, nil
}
func readGTFParseLine(line string) []string {
// if quoted
q := false
f := func(r rune) bool {
if r == '"' {
q = !q
}
// A quote is treated as a white space so that it is removed from the
// line. Otherwise a white space is removed only if q (quote) is false.
return r == '"' || ((unicode.IsSpace(r) || r == ';') && q == false)
}
return strings.FieldsFunc(line, f)
}
// Parse expression data from a GTF file (gene transfer format). The data
// is added as a meta column named "expr" to the gene list. Parameters:
// geneIdName: Name of the optional field containing the gene id
// exprIdName: Name of the optional field containing the expression data
// genes: List of query genes
func (granges *GRanges) ReadGTF(r io.Reader, optNames, optTypes []string, defaults []interface{}) error {
scanner := bufio.NewScanner(r)
if len(optNames) != len(optTypes) {
return fmt.Errorf("ReadGTF(): invalid arguments")
}
if len(defaults) != 0 && len(defaults) != len(optNames) {
return fmt.Errorf("ReadGTF(): invalid number of default values")
}
// construct type map
seqname := []string{}
source := []string{}
feature := []string{}
start := []int{}
end := []int{}
score := []float64{}
strand := []byte{}
frame := []int{}
gtfOpt := make(gtfOptional)
gtfDef := make(gtfDefaults)
typeMap := make(gtfTypeMap)
for i := 0; i < len(optNames); i++ {
typeMap[optNames[i]] = optTypes[i]
if len(defaults) != 0 {
gtfDef[optNames[i]] = defaults[i]
}
}
for name, typeStr := range typeMap {
switch typeStr {
case "[]int" : gtfOpt[name] = []int{}
case "[]float64": gtfOpt[name] = []float64{}
case "[]string" : gtfOpt[name] = []string{}
default:
return fmt.Errorf("ReadGTF(): invalid type `%s' for optional field `%s'", typeStr, name)
}
}
for i := 0; scanner.Scan(); i++ {
if err := scanner.Err(); err != nil {
return err
}
fields := readGTFParseLine(scanner.Text())
if len(fields) == 0 {
continue
}
if len(fields) < 8 {
return fmt.Errorf("file must have at least eight columns")
}
seqname = append(seqname, fields[0])
source = append(source, fields[1])
feature = append(feature, fields[2])
if v, err := strconv.ParseInt(fields[3], 10, 64); err != nil {
return err
} else {
start = append(start, int(v))
}
if v, err := strconv.ParseInt(fields[4], 10, 64); err != nil {
return err
} else {
end = append(end, int(v))
}
if fields[5] == "." {
score = append(score, 0)
} else {
if v, err := strconv.ParseFloat(fields[5], 64); err != nil {
return err
} else {
score = append(score, v)
}
}
if fields[6] == "." {
strand = append(strand, '*')
} else {
strand = append(strand, fields[6][0])
}
if fields[7] == "." {
frame = append(frame, -1)
} else {
if v, err := strconv.ParseInt(fields[7], 10, 64); err != nil {
return err
} else {
frame = append(frame, int(v))
}
}
// parse optional fields
if tmp, err := readGTFParseOptional(fields[8:len(fields)], gtfOpt, typeMap, gtfDef, i+1); err != nil {
return err
} else {
gtfOpt = tmp
}
}
// create new granges object
*granges = NewGRanges(seqname, start, end, strand)
// add meta columns
granges.AddMeta("source", source)
granges.AddMeta("feature", feature)
granges.AddMeta("score", score)
granges.AddMeta("frame", frame)
// add optional fields as meta columns
for name, values := range gtfOpt {
granges.AddMeta(name, values)
}
return nil
}
func (granges *GRanges) ImportGTF(filename string, optNames, optTypes []string, optDef []interface{}) 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 granges.ReadGTF(r, optNames, optTypes, optDef)
}
/* -------------------------------------------------------------------------- */
// Export GRanges as GTF file. Required GTF fields should be provided
// as meta columns named sources, features, scores, and frames. All other
// meta columns are exported as optional fields.
func (granges GRanges) WriteGTF(w_ io.Writer) error {
w := bufio.NewWriter(w_)
defer w.Flush()
sources := granges.GetMetaStr("sources")
features := granges.GetMetaStr("features")
scores := granges.GetMetaFloat("scores")
frames := granges.GetMetaInt("frames")
for i := 0; i < granges.Length(); i++ {
fmt.Fprintf(w, "%s", granges.Seqnames[i])
if len(sources) > 0 {
fmt.Fprintf(w, "\t%s", sources[i])
} else {
fmt.Fprintf(w, "\t%s", ".")
}
if len(features) > 0 {
fmt.Fprintf(w, "\t%s", features[i])
} else {
fmt.Fprintf(w, "\t%s", ".")
}
fmt.Fprintf(w, "\t%d", granges.Ranges[i].From)
fmt.Fprintf(w, "\t%d", granges.Ranges[i].To)
if len(scores) > 0 {
fmt.Fprintf(w, "\t%f", scores[i])
} else {
fmt.Fprintf(w, "\t%s", ".")
}
if len(granges.Strand) > 0 && granges.Strand[i] != '*' {
fmt.Fprintf(w, "\t%c", granges.Strand[i])
} else {
fmt.Fprintf(w, "\t%c", '.')
}
if len(frames) > 0 {
fmt.Fprintf(w, "\t%d", frames[i])
} else {
fmt.Fprintf(w, "\t%s", ".")
}
if granges.MetaLength() != 0 {
printedTab := false
for k := 0; k < granges.MetaLength(); k++ {
if granges.MetaName[k] == "sources" {
continue
}
if granges.MetaName[k] == "features" {
continue
}
if granges.MetaName[k] == "scores" {
continue
}
if granges.MetaName[k] == "frames" {
continue
}
if printedTab {
w.WriteString(" ")
} else {
w.WriteString("\t")
printedTab = true
}
// print name of the meta data
fmt.Fprintf(w, "%s ", granges.MetaName[k])
// print data
switch v := granges.MetaData[k].(type) {
case []string : fmt.Fprintf(w, "\"%s\"", v[i])
case []float64: fmt.Fprintf(w, "\"%f\"", v[i])
case []int : fmt.Fprintf(w, "\"%d\"", v[i])
case [][]string:
w.WriteString("\"")
for j := 0; j < len(v[i]); j++ {
if j != 0 {
w.WriteString(" ")
}
fmt.Fprintf(w, "%s", v[i][j])
}
w.WriteString("\"")
case [][]float64:
w.WriteString("\"")
for j := 0; j < len(v[i]); j++ {
if j != 0 {
w.WriteString(" ")
}
fmt.Fprintf(w, "%f", v[i][j])
}
w.WriteString("\"")
case [][]int:
w.WriteString("\"")
for j := 0; j < len(v[i]); j++ {
if j != 0 {
w.WriteString(" ")
}
fmt.Fprintf(w, "%d", v[i][j])
}
w.WriteString("\"")
}
w.WriteString(";")
}
}
w.WriteString("\n")
}
return nil
}
func (granges GRanges) ExportGTF(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
defer w.Flush()
return granges.WriteGTF(f)
}