-
Notifications
You must be signed in to change notification settings - Fork 3
/
bim.go
72 lines (58 loc) · 1022 Bytes
/
bim.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
package genomisc
import (
"bufio"
"os"
"strconv"
"strings"
)
type BIM struct {
path string
file *os.File
scanner *bufio.Scanner
err error
}
func OpenBIM(path string) (*BIM, error) {
bim := &BIM{
path: path,
}
file, err := os.Open(path)
if err != nil {
return nil, err
}
bim.file = file
bim.scanner = bufio.NewScanner(file)
return bim, nil
}
func (b *BIM) Close() error {
return b.file.Close()
}
func (b *BIM) Err() error {
if b.err != nil {
return b.err
}
return b.scanner.Err()
}
func (b *BIM) Read() *BIMRow {
b.scanner.Scan()
if b.scanner.Err() != nil {
return nil
}
data := b.scanner.Text()
cols := strings.Fields(data)
if len(cols) < Allele2+1 {
return nil
}
row := &BIMRow{
Chromosome: cols[Chromosome],
VariantID: cols[VariantID],
Allele1: cols[Allele1],
Allele2: cols[Allele2],
}
coord64, err := strconv.ParseUint(cols[Coordinate], 10, 32)
if err != nil {
b.err = err
return nil
}
row.Coordinate = uint32(coord64)
return row
}