forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCigarPositionIterable.java
More file actions
341 lines (300 loc) · 10.2 KB
/
CigarPositionIterable.java
File metadata and controls
341 lines (300 loc) · 10.2 KB
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
/*
* Copyright (c) 2012 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.sequenceanalysis.api.picard;
import htsjdk.samtools.Cigar;
import htsjdk.samtools.CigarOperator;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.util.CigarUtil;
import org.labkey.sequenceanalysis.run.analysis.BamIterator;
import java.util.Iterator;
/**
* User: bbimber
* Date: 8/9/12
* Time: 9:34 PM
*/
/**
* Iterator that transverses the elements of a CIGAR string, accumulating information at each position of the alignment.
*
* @author bbimber@gmail.com
*/
public class CigarPositionIterable implements Iterable<CigarPositionIterable.PositionInfo>
{
private final SAMRecord _record;
public CigarPositionIterable(SAMRecord record)
{
_record = record;
}
@Override
public CigarIterator iterator()
{
return new CigarIterator(this);
}
public class CigarIterator implements Iterator<CigarPositionIterable.PositionInfo>
{
private final SAMRecord _record;
private Integer[] _readPositions;
private Integer[] _refPositions;
private char[] _explodedCigar;
private int _pos = 0;
/**
* Prepare to iterate the CIGAR string of this record
* @param iterable A CigarPositionIterable instance
*/
public CigarIterator(CigarPositionIterable iterable)
{
_record = iterable._record;
initializeCigar();
}
private void initializeCigar()
{
Cigar c = _record.getCigar();
_explodedCigar = CigarUtil.cigarArrayFromString(c.toString());
int readPos = 0; //0-based
int refPos = _record.getAlignmentStart() - 1; //0-based
_readPositions = new Integer[_explodedCigar.length];
_refPositions = new Integer[_explodedCigar.length];
int i = 0;
for (char el : _explodedCigar)
{
CigarOperator op = CigarOperator.characterToEnum(el);
if (op.consumesReadBases())
{
_readPositions[i] = readPos;
readPos++;
}
else
{
_readPositions[i] = -1;
}
if (op.consumesReferenceBases())
{
_refPositions[i] = refPos;
refPos++;
}
else
{
_refPositions[i] = -1;
}
i++;
}
}
@Override
public void remove()
{
throw new UnsupportedOperationException("Can not remove elements in CIGAR via an iterator!");
}
/**
* @return true if there are more positions of the CIGAR string to iterate
*/
@Override
public boolean hasNext()
{
return _pos < _readPositions.length;
}
/**
* @return The next PositionInfo in the iteration
*/
@Override
public PositionInfo next()
{
if (_pos >= _readPositions.length)
return null;
PositionInfo info = new PositionInfo(_record, _pos, _explodedCigar, _readPositions, _refPositions);
_pos++;
return info;
}
}
/**
* Describes a specific position in an alignment, including the position relative to the start of both the reference and read
* sequences.
*/
public static class PositionInfo
{
private final SAMRecord _record;
private final CigarOperator _op;
private final int _pos;
private final int _readPos;
private final int _refPos;
private int _indel = 0;
private int _lastReadPos;
private int _lastRefPos;
public PositionInfo(SAMRecord record, int pos, char[] ops, Integer[] readPos, Integer[] refPos)
{
_record = record;
_pos = pos;
_op = CigarOperator.characterToEnum(ops[pos]);
_readPos = readPos[pos];
_refPos = refPos[pos];
if (_readPos > -1)
_lastReadPos = _readPos;
else
{
int i = _pos;
while (i >= 0)
{
if (readPos[i] > -1)
{
_lastReadPos = readPos[i];
_indel = i - _pos;
break;
}
i--;
}
}
if (_refPos > -1)
_lastRefPos = _refPos;
else
{
int i = _pos;
while (i >= 0)
{
if (refPos[i] > -1)
{
_lastRefPos = refPos[i];
_indel = _pos - i;
break;
}
i--;
}
}
}
/**
* @return The zero-based position relative to the start of the reference, -1 indicates an insertion
*/
public int getRefPosition()
{
return _refPos;
}
/**
* @return The zero-based position relative to the start of the read, -1 indicates a deletion
*/
public int getReadPosition()
{
return _readPos;
}
/**
* @return This will return the length of the indel at this position: 0 for no indel, positive for an insertion relative to the reference,
* negative for a deletion relative to the reference. For example, the 1st base of an insertion is 1, the second is 2, etc.
* The first base of a deletion is -1, second is -2. This is patterned after the Bio-SamTools (perl) Bio::DB::Bam::Pileup::indel method.
*/
public int getIndel()
{
return _indel;
}
/**
* @return The length of the insertion at this position. For non-indels or deletions, it will be zero. The first position of an insertion is 0 (ie. the base overlapping with the references), the second is 1, etc.
*/
public int getInsertIndex()
{
return getIndel() <= 0 ? 0 : getIndel();
}
/**
* @return The last read position that overlapped the reference, using zero-based coordinates. This is primarily used for deletions relative to
* the reference, since those return -1 as the read position. For non-indels, this will return the same value as getReadPosition()
*/
public int getLastReadPosition()
{
return _lastReadPos;
}
/**
* @return Returns the last reference position that overlapped the read, using zero-based coordinates. Primarily used for insertions relative
* to the reference, in order to find the previous reference position. For non-indels, this will return the same value as getRefPosition()
*/
public int getLastRefPosition()
{
return _lastRefPos;
}
/**
* @param referenceBases An array representing the sequence of the reference
* @return The reference base at this position. '-' indicates an insertion.
*/
public byte getReferenceBase(byte[] referenceBases)
{
return isInsertion() ? BamIterator.INDEL_CHARACTER : referenceBases[_refPos];
}
/**
* @return The read base at this position. '-' indicates a deletion.
*/
public byte getReadBase()
{
return isDel() ? BamIterator.INDEL_CHARACTER : _record.getReadBases()[_readPos];
}
/**
* @return True if this position is skipped, meaning the CIGAR operator is a soft clip (S) or a skipped region (N)
*/
public boolean isSkipped()
{
return _op.equals(CigarOperator.SOFT_CLIP) || _op.equals(CigarOperator.HARD_CLIP) || _op.equals(CigarOperator.SKIPPED_REGION);
}
/**
* @return True if this position is an indel (insertion or deletion) relative to the reference
*/
public boolean isIndel()
{
return isDel() || isInsertion();
}
/**
* @return True if this position is an insertion relative to the reference
*/
public boolean isInsertion()
{
return _op.equals(CigarOperator.INSERTION);
}
/**
* @return True if this position is a deletion relative to the reference
*/
public boolean isDel()
{
return _op.equals(CigarOperator.DELETION);
}
/**
* @return True if this position overlaps the reference, meaning it consumes both read and references bases
*/
public boolean overlapsReference()
{
return _op.consumesReadBases() && _op.consumesReferenceBases();
}
public boolean includeInSnpCount()
{
return isIndel() || (overlapsReference() && !isSkipped());
}
public String getReferenceName()
{
return _record.getReferenceName();
}
/**
* @return The CigarOperation at this position
*/
public CigarOperator getCigarOperator()
{
return _op;
}
/**
* @return The SAMRecord associated with this alignment position
*/
public SAMRecord getRecord()
{
return _record;
}
/**
* @return The base quality at this position
*/
public int getBaseQuality()
{
return _record.getBaseQualities()[getLastReadPosition()];
}
}
}