-
Notifications
You must be signed in to change notification settings - Fork 16
/
pair_iterator.go
204 lines (189 loc) · 6.29 KB
/
pair_iterator.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
package bamprovider
import (
"fmt"
"runtime"
"strings"
gbam "github.com/grailbio/bio/encoding/bam"
"github.com/grailbio/hts/sam"
)
type pairIteratorSharedState struct {
provider Provider
shardChan chan gbam.Shard // for receiving shard ranges through NewShardChannel.
// "distantMates" store records whose mate are in different shards.
distantMates *concurrentMap
}
// Pair encapsulates a pair of SAM records for a pair of reads, and whether
// any error was encountered in retrieving them.
type Pair = gbam.Pair
// MissingMateError is a specific error that can be used when one or more mates
// are missing.
type MissingMateError struct {
Message string
}
func (mme MissingMateError) Error() string {
return mme.Message
}
// PairIterator reads matched pairs of records from a BAM or PAM file. Use
// NewPairIterators to create an iterator.
type PairIterator struct {
rec Pair
iter Iterator
shard gbam.Shard // Shard currently read
shared *pairIteratorSharedState
localNameToRecord map[string]*sam.Record
}
// NewPairIterators creates a set of PairIterators. A PairIterator yields pairs
// of records in the BAM or PAM data corresponding to primary alignments for
// paired reads. Records will not be included if they represent secondary or
// supplemental alignments (based on SAM flags). Pairs that have both reads
// unmapped will not be included unless includeUnmapped is true.
//
// The pairs in the BAM file will be randomly sharded across the PairIterators
// created by this function. Pairs are returned in an unspecified order, even
// within one PairIterator. (Use BoundedPairIterator instead if you want
// deterministic behavior and do not need to process distant mates.)
//
// Each PairIterator is thread-compatible. It is recommended to create one
// goroutine for each iterator.
func NewPairIterators(provider Provider, includeUnmapped bool) ([]*PairIterator, error) {
parallelism := runtime.NumCPU()
shards, err := provider.GenerateShards(GenerateShardsOpts{
Strategy: ByteBased,
// Create more shards than parallelism. This will prevent a very large shard
// from delaying completion of the job toward the end (i.e., alleviate the
// straggler problem).
NumShards: parallelism * 2,
Padding: 0,
IncludeUnmapped: includeUnmapped,
SplitUnmappedCoords: true})
if err != nil {
return nil, err
}
shared := &pairIteratorSharedState{
provider: provider,
shardChan: gbam.NewShardChannel(shards),
distantMates: newConcurrentMap(),
}
iters := make([]*PairIterator, parallelism)
for i := 0; i < parallelism; i++ {
iters[i] = &PairIterator{
shared: shared,
localNameToRecord: make(map[string]*sam.Record),
}
}
return iters, nil
}
func isPrimary(record *sam.Record) bool {
return (record.Flags&sam.Secondary) == 0 && (record.Flags&sam.Supplementary) == 0
}
// Record returns the current pair, or an error.
//
// REQUIRES: Scan() has been called and its last call returned true.
func (l *PairIterator) Record() Pair { return l.rec }
// Scan reads the next record. It returns true if a record has been read, and
// false on end of data stream.
func (l *PairIterator) Scan() bool {
for {
if l.iter == nil {
// Start reading a new shard.
var ok bool
if l.shard, ok = <-l.shared.shardChan; !ok {
break
}
l.iter = l.shared.provider.NewIterator(l.shard)
}
if l.iter.Scan() {
record := l.iter.Record()
if !isPrimary(record) {
sam.PutInFreePool(record)
continue
}
mate, ok := l.localNameToRecord[record.Name]
if ok {
// We've already seen the mate of this record.
delete(l.localNameToRecord, record.Name)
if record.Flags&sam.Read1 != 0 {
l.rec = Pair{R1: record, R2: mate}
} else {
l.rec = Pair{R1: mate, R2: record}
}
return true
}
if mateInShard(record, &l.shard) {
// Store the record for later, when we see its mate.
l.localNameToRecord[record.Name] = record
continue
}
// The reads in this pair are in different shards, so we have to synchronize
// with other goroutines.
mate = l.shared.distantMates.lookupAndDelete(record)
if mate != nil {
if record.Flags&sam.Read1 != 0 {
l.rec = Pair{R1: record, R2: mate}
} else {
l.rec = Pair{R1: mate, R2: record}
}
return true
}
continue
}
// End of shard. Report records that didn't find a mate locally.
if err := l.iter.Close(); err != nil {
l.rec = Pair{Err: err}
l.iter = nil
return true
}
l.iter = nil
var orphans []string
if len(l.localNameToRecord) > 0 {
for _, rec := range l.localNameToRecord {
orphans = append(orphans, fmt.Sprintf("%v:[%v:%d,%v:%d]", rec.Name, rec.Ref.ID(), rec.Pos, rec.MateRef.ID(), rec.MatePos))
if len(orphans) > 100 {
break
}
}
for k := range l.localNameToRecord {
delete(l.localNameToRecord, k)
}
}
if len(orphans) > 0 {
l.rec = Pair{Err: MissingMateError{fmt.Sprintf("shard %+v: didn't find expected mates for reads: %v", l.shard, strings.Join(orphans, "\n"))}}
return true
}
}
return false
}
// FinishPairIterators should be called after reading all pairs. It returns an
// error if there are some unpaired reads.
func FinishPairIterators(iters []*PairIterator) error {
if len(iters) > 0 {
// All iters have the same "shared" value, so just check the iters[0].
n := iters[0].shared.distantMates.approxSize()
if n > 0 {
return MissingMateError{Message: fmt.Sprintf("found %d unmatched mates in the global hash", n)}
}
}
return nil
}
// MateInShard checks if the mate of "record" is contained in the shard's range.
//
// If shard.StartSeq != 0 and the mate is at coordinate
// <shard.StartRef,shard.Start>, this function returns false, even though the
// real status may be true. The same holds for shard.EndSeq. This function
// still guarantees that MateInShard(r1, shard1) == MateInShard(r2, shard2) for
// any matching pair of reads r1 (found in shard1) and r2 (found in shard2) .
func mateInShard(record *sam.Record, shard *gbam.Shard) bool {
mateAddr := gbam.NewCoord(record.MateRef, record.MatePos, 0)
startAddr := gbam.NewCoord(shard.StartRef, shard.Start, 0)
endAddr := gbam.NewCoord(shard.EndRef, shard.End, 0)
if mateAddr.LT(startAddr) {
return false
}
if mateAddr.GE(endAddr) {
return false
}
if shard.StartSeq != 0 && mateAddr.EQ(startAddr) {
return false
}
return true
}