File: index_read.go

package info (click to toggle)
golang-github-biogo-hts 1.1.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,660 kB
  • sloc: makefile: 3
file content (188 lines) | stat: -rw-r--r-- 4,796 bytes parent folder | download | duplicates (3)
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
// Copyright ©2014 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package internal

import (
	"encoding/binary"
	"fmt"
	"io"
	"sort"

	"github.com/biogo/hts/bgzf"
)

// ReadIndex reads the Index from the given io.Reader.
func ReadIndex(r io.Reader, n int32, typ string) (Index, error) {
	var (
		idx Index
		err error
	)
	idx.Refs, err = readIndices(r, n, typ)
	if err != nil {
		return idx, err
	}
	var nUnmapped uint64
	err = binary.Read(r, binary.LittleEndian, &nUnmapped)
	if err == nil {
		idx.Unmapped = &nUnmapped
	} else if err != io.EOF {
		return idx, err
	}
	idx.IsSorted = true

	// Set the index of the last record to max int to
	// prevent addition of records out of order. This
	// means that the only way to append to an index is
	// to re-index and add to that created index.
	// TODO(kortschak) See if index appending is feasible
	// and needed.
	idx.LastRecord = int(^uint(0) >> 1)

	return idx, nil
}

func readIndices(r io.Reader, n int32, typ string) ([]RefIndex, error) {
	var err error
	idx := make([]RefIndex, n)
	for i := range idx {
		idx[i].Bins, idx[i].Stats, err = readBins(r, typ)
		if err != nil {
			return nil, err
		}
		idx[i].Intervals, err = readIntervals(r, typ)
		if err != nil {
			return nil, err
		}
	}
	return idx, nil
}

func readBins(r io.Reader, typ string) ([]Bin, *ReferenceStats, error) {
	var n int32
	err := binary.Read(r, binary.LittleEndian, &n)
	if err != nil {
		return nil, nil, err
	}
	if n == 0 {
		return nil, nil, nil
	}
	var stats *ReferenceStats
	bins := make([]Bin, n)
	for i := 0; i < len(bins); i++ {
		err = binary.Read(r, binary.LittleEndian, &bins[i].Bin)
		if err != nil {
			return nil, nil, fmt.Errorf("%s: failed to read bin number: %v", typ, err)
		}
		err = binary.Read(r, binary.LittleEndian, &n)
		if err != nil {
			return nil, nil, fmt.Errorf("%s: failed to read bin count: %v", typ, err)
		}
		if bins[i].Bin == StatsDummyBin {
			if n != 2 {
				return nil, nil, fmt.Errorf("%s: malformed dummy bin header", typ)
			}
			stats, err = readStats(r, typ)
			if err != nil {
				return nil, nil, err
			}
			bins = bins[:len(bins)-1]
			i--
			continue
		}
		bins[i].Chunks, err = readChunks(r, n, typ)
		if err != nil {
			return nil, nil, err
		}
	}
	if !sort.IsSorted(byBinNumber(bins)) {
		sort.Sort(byBinNumber(bins))
	}
	return bins, stats, nil
}

func readChunks(r io.Reader, n int32, typ string) ([]bgzf.Chunk, error) {
	if n == 0 {
		return nil, nil
	}
	chunks := make([]bgzf.Chunk, n)
	var buf [16]byte
	for i := range chunks {
		// Get the begin and end offset in a single read.
		_, err := io.ReadFull(r, buf[:])
		if err != nil {
			return nil, fmt.Errorf("%s: failed to read chunk virtual offset: %v", typ, err)
		}
		chunks[i].Begin = makeOffset(binary.LittleEndian.Uint64(buf[:8]))
		chunks[i].End = makeOffset(binary.LittleEndian.Uint64(buf[8:]))
	}
	if !sort.IsSorted(byBeginOffset(chunks)) {
		sort.Sort(byBeginOffset(chunks))
	}
	return chunks, nil
}

func readStats(r io.Reader, typ string) (*ReferenceStats, error) {
	var (
		vOff  uint64
		stats ReferenceStats
		err   error
	)
	err = binary.Read(r, binary.LittleEndian, &vOff)
	if err != nil {
		return nil, fmt.Errorf("%s: failed to read index stats chunk begin virtual offset: %v", typ, err)
	}
	stats.Chunk.Begin = makeOffset(vOff)
	err = binary.Read(r, binary.LittleEndian, &vOff)
	if err != nil {
		return nil, fmt.Errorf("%s: failed to read index stats chunk end virtual offset: %v", typ, err)
	}
	stats.Chunk.End = makeOffset(vOff)
	err = binary.Read(r, binary.LittleEndian, &stats.Mapped)
	if err != nil {
		return nil, fmt.Errorf("%s: failed to read index stats mapped count: %v", typ, err)
	}
	err = binary.Read(r, binary.LittleEndian, &stats.Unmapped)
	if err != nil {
		return nil, fmt.Errorf("%s: failed to read index stats unmapped count: %v", typ, err)
	}
	return &stats, nil
}

func readIntervals(r io.Reader, typ string) ([]bgzf.Offset, error) {
	var n int32
	err := binary.Read(r, binary.LittleEndian, &n)
	if err != nil {
		return nil, err
	}
	if n == 0 {
		return nil, nil
	}
	offsets := make([]bgzf.Offset, n)
	// chunkSize determines the number of offsets consumed by each binary.Read.
	const chunkSize = 512
	var vOffs [chunkSize]uint64
	for i := 0; i < int(n); i += chunkSize {
		l := min(int(n)-i, len(vOffs))
		err = binary.Read(r, binary.LittleEndian, vOffs[:l])
		if err != nil {
			return nil, fmt.Errorf("%s: failed to read tile interval virtual offset: %v", typ, err)
		}
		for k := 0; k < l; k++ {
			offsets[i+k] = makeOffset(vOffs[k])
		}
	}

	if !sort.IsSorted(byVirtOffset(offsets)) {
		sort.Sort(byVirtOffset(offsets))
	}
	return offsets, nil
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}