This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ReadUtils.java
597 lines (541 loc) · 23.8 KB
/
ReadUtils.java
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Copyright (C) 2014 Google Inc.
*
* 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 com.google.cloud.genomics.utils.grpc;
import com.google.common.base.Function;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import com.google.genomics.v1.CigarUnit;
import com.google.genomics.v1.LinearAlignment;
import com.google.genomics.v1.Position;
import com.google.genomics.v1.Read;
import com.google.genomics.v1.ReadGroup;
import com.google.genomics.v1.ReadGroup.Program;
import com.google.genomics.v1.ReadGroupSet;
import com.google.genomics.v1.Reference;
import com.google.protobuf.ListValue;
import com.google.protobuf.Value;
import htsjdk.samtools.CigarElement;
import htsjdk.samtools.CigarOperator;
import htsjdk.samtools.SAMException;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SAMProgramRecord;
import htsjdk.samtools.SAMReadGroupRecord;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.SAMSequenceDictionary;
import htsjdk.samtools.SAMSequenceRecord;
import htsjdk.samtools.SAMTextHeaderCodec;
import htsjdk.samtools.SamFileHeaderMerger;
import htsjdk.samtools.TagValueAndUnsignedArrayFlag;
import htsjdk.samtools.TextTagCodec;
import htsjdk.samtools.ValidationStringency;
import htsjdk.samtools.util.SequenceUtil;
import htsjdk.samtools.util.StringLineReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadUtils extends com.google.cloud.genomics.utils.ReadUtils {
protected static BiMap<String, CigarUnit.Operation> CIGAR_OPERATIONS_INV_GRPC;
static {
CIGAR_OPERATIONS_INV_GRPC = HashBiMap.create();
CIGAR_OPERATIONS_INV_GRPC.put("M", CigarUnit.Operation.ALIGNMENT_MATCH);
CIGAR_OPERATIONS_INV_GRPC.put("H", CigarUnit.Operation.CLIP_HARD);
CIGAR_OPERATIONS_INV_GRPC.put("S", CigarUnit.Operation.CLIP_SOFT);
CIGAR_OPERATIONS_INV_GRPC.put("D", CigarUnit.Operation.DELETE);
CIGAR_OPERATIONS_INV_GRPC.put("I", CigarUnit.Operation.INSERT);
CIGAR_OPERATIONS_INV_GRPC.put("P", CigarUnit.Operation.PAD);
CIGAR_OPERATIONS_INV_GRPC.put("=", CigarUnit.Operation.SEQUENCE_MATCH);
CIGAR_OPERATIONS_INV_GRPC.put("X", CigarUnit.Operation.SEQUENCE_MISMATCH);
CIGAR_OPERATIONS_INV_GRPC.put("N", CigarUnit.Operation.SKIP);
}
public static String getCigarString(Read read) {
List<CigarUnit> cigar = read.getAlignment() == null ? null : read.getAlignment().getCigarList();
if (cigar != null && cigar.size() > 0) {
StringBuilder cigarString = new StringBuilder();
for (CigarUnit unit : cigar) {
cigarString.append(String.valueOf(unit.getOperationLength()));
cigarString.append(CIGAR_OPERATIONS.get(unit.getOperation().toString()));
}
return cigarString.toString();
}
return null;
}
private static boolean isUnmapped(Position position) {
return position == null;
}
private static boolean isReverseStrand(Position position) {
return position != null && Boolean.TRUE.equals(position.getReverseStrand());
}
public static int getFlags(Read read) {
Position position = !read.hasAlignment() || !read.getAlignment().hasPosition() ?
null : read.getAlignment().getPosition();
Position nextMatePosition = !read.hasNextMatePosition() ? null : read.getNextMatePosition();
int flags = 0;
boolean paired = Integer.valueOf(2).equals(read.getNumberReads());
flags += paired ? 1 : 0; // read_paired
flags += Boolean.TRUE.equals(read.getProperPlacement()) ? 2 : 0; // read_proper_pair
flags += isUnmapped(position) ? 4 : 0; // read_unmapped
flags += paired && isUnmapped(nextMatePosition) ? 8 : 0; // mate_unmapped
flags += isReverseStrand(position) ? 16 : 0 ; // read_reverse_strand
flags += isReverseStrand(nextMatePosition) ? 32 : 0; // mate_reverse_strand
flags += read.getNumberReads() > 0 && Integer.valueOf(0).equals(read.getReadNumber()) ? 64 : 0; // first_in_pair
flags += Integer.valueOf(1).equals(read.getReadNumber()) ? 128 : 0; // second_in_pair
flags += Boolean.TRUE.equals(read.getSecondaryAlignment()) ? 256 : 0; // secondary_alignment
flags += Boolean.TRUE.equals(read.getFailedVendorQualityChecks()) ? 512 : 0; // failed_quality
flags += Boolean.TRUE.equals(read.getDuplicateFragment()) ? 1024 : 0; // duplicate_read
flags += Boolean.TRUE.equals(read.getSupplementaryAlignment()) ? 2048 : 0; // supplementary
return flags;
}
static String safeStr(String s) {
if (s == null) {
return "";
}
return s;
}
/**
* Generates a Read from a SAMRecord.
*/
public static final Read makeReadGrpc(final SAMRecord record) {
Read.Builder read = Read.newBuilder();
read.setId(safeStr(record.getReadName())); // TODO: make more unique
read.setFragmentName(safeStr(record.getReadName()));
read.setReadGroupId(safeStr(getAttr(record, "RG")));
read.setNumberReads(record.getReadPairedFlag() ? 2 : 1);
read.setProperPlacement(record.getReadPairedFlag() && record.getProperPairFlag());
if (!record.getReadUnmappedFlag() && record.getAlignmentStart() > 0) {
LinearAlignment.Builder alignment = LinearAlignment.newBuilder();
Position.Builder position = Position.newBuilder();
position.setPosition((long) record.getAlignmentStart() - 1);
position.setReferenceName(safeStr(record.getReferenceName()));
position.setReverseStrand(record.getReadNegativeStrandFlag());
alignment.setPosition(position);
alignment.setMappingQuality(record.getMappingQuality());
final String referenceSequence = (record.getAttribute("MD") != null) ? new String(
SequenceUtil.makeReferenceFromAlignment(record, true))
: null;
List<CigarUnit> cigar = Lists.transform(record.getCigar().getCigarElements(),
new Function<CigarElement, CigarUnit>() {
@Override
public CigarUnit apply(CigarElement c) {
CigarUnit.Builder u = CigarUnit.newBuilder();
CigarOperator o = c.getOperator();
u.setOperation(CIGAR_OPERATIONS_INV_GRPC.get(o.toString()));
u.setOperationLength(c.getLength());
if (referenceSequence != null && (u.getOperation().equals("SEQUENCE_MISMATCH")
|| u.getOperation().equals("DELETE"))) {
u.setReferenceSequence(referenceSequence);
}
return u.build();
}
});
alignment.addAllCigar(cigar);
read.setAlignment(alignment);
}
read.setDuplicateFragment(record.getDuplicateReadFlag());
read.setFragmentLength(record.getInferredInsertSize());
if (record.getReadPairedFlag()) {
if (record.getFirstOfPairFlag()) {
read.setReadNumber(0);
} else if (record.getSecondOfPairFlag()) {
read.setReadNumber(1);
}
if (!record.getMateUnmappedFlag()) {
Position.Builder matePosition = Position.newBuilder();
matePosition.setPosition((long) record.getMateAlignmentStart() - 1);
matePosition.setReferenceName(record.getMateReferenceName());
matePosition.setReverseStrand(record.getMateNegativeStrandFlag());
read.setNextMatePosition(matePosition);
}
}
read.setFailedVendorQualityChecks(record.getReadFailsVendorQualityCheckFlag());
read.setSecondaryAlignment(record.getNotPrimaryAlignmentFlag());
read.setSupplementaryAlignment(record.getSupplementaryAlignmentFlag());
read.setAlignedSequence(safeStr(record.getReadString()));
byte[] baseQualities = record.getBaseQualities();
if (baseQualities.length > 0) {
List<Integer> readBaseQualities = new ArrayList<>(baseQualities.length);
for (byte b : baseQualities) {
readBaseQualities.add(new Integer(b));
}
read.addAllAlignedQuality(readBaseQualities);
}
Map<String, ListValue> attributes = read.getMutableInfo();
for( SAMRecord.SAMTagAndValue tagAndValue: record.getAttributes()) {
if (tagAndValue.value == null) {
continue;
}
final String tag = tagAndValue.tag;
String value;
if (tagAndValue.value instanceof byte[]) {
// It's possible for client code of SamRecord to pass byte[]
// to setAttribute. toString is not defined for byte[], so
// it produces garbage. The solution to create a string directly.
value = new String(((byte[]) tagAndValue.value));
} else {
value = tagAndValue.value.toString();
}
ListValue listValue = attributes.get(tag);
ListValue.Builder listValueBuilder = listValue == null ?
ListValue.newBuilder() :
ListValue.newBuilder(listValue);
listValueBuilder.addValues(Value.newBuilder().setStringValue(value));
attributes.put(tag, listValueBuilder.build());
}
return read.build();
}
public static String getAttr(SAMRecord record, String attributeName) {
try {
return record.getStringAttribute(attributeName);
} catch (SAMException ex) {
return "";
}
}
/** Returns SAM Tag type. If not a known tag - defaults to "Z". */
public static String getTagType(String tagName) {
final String result = SAM_TAGS.get(tagName);
return result != null ? result : "Z";
}
public static final SAMRecord makeSAMRecord(Read read, SAMFileHeader header) {
SAMRecord record = new SAMRecord(header);
if (read.getFragmentName() != null) {
record.setReadName(read.getFragmentName());
}
// Set flags, as advised in http://google-genomics.readthedocs.org/en/latest/migrating_tips.html
int flags = getFlags(read);
record.setFlags(flags);
String referenceName = null;
Long alignmentStart = null;
if (read.hasAlignment()) {
if (read.getAlignment().hasPosition()) {
referenceName = read.getAlignment().getPosition().getReferenceName();
if (referenceName != null && !referenceName.isEmpty()) {
record.setReferenceName(referenceName);
}
alignmentStart = read.getAlignment().getPosition().getPosition();
if (alignmentStart != null) {
// API positions are 0-based and SAMRecord is 1-based.
record.setAlignmentStart(alignmentStart.intValue() + 1);
}
}
Integer mappingQuality = read.getAlignment().getMappingQuality();
if (mappingQuality != null) {
record.setMappingQuality(mappingQuality);
}
List<CigarUnit> cigar = read.getAlignment().getCigarList();
if (cigar != null && cigar.size() > 0) {
StringBuffer cigarString = new StringBuffer(cigar.size());
for (CigarUnit unit : cigar) {
cigarString.append(String.valueOf(unit.getOperationLength()));
cigarString.append(CIGAR_OPERATIONS.get(unit.getOperation().toString()));
}
record.setCigarString(cigarString.toString());
}
}
if (read.hasNextMatePosition()) {
String mateReferenceName = read.getNextMatePosition().getReferenceName();
if (mateReferenceName != null && !mateReferenceName.isEmpty()) {
record.setMateReferenceName(mateReferenceName);
}
Long matePosition = read.getNextMatePosition().getPosition();
if (matePosition != null) {
// API positions are 0-based and SAMRecord is 1-based.
record.setMateAlignmentStart(matePosition.intValue() + 1);
}
}
record.setInferredInsertSize(read.getFragmentLength());
if (read.getAlignedSequence() != null) {
record.setReadString(read.getAlignedSequence());
}
List<Integer> baseQuality = read.getAlignedQualityList();
if (baseQuality != null && baseQuality.size() > 0) {
byte[] qualityArray = new byte[baseQuality.size()];
int idx = 0;
for (Integer i : baseQuality) {
qualityArray[idx++] = i.byteValue();
}
record.setBaseQualities(qualityArray);
}
TextTagCodec textTagCodec = new TextTagCodec();
Map<String, ListValue> tags = read.getInfo();
if (tags != null) {
for (String tag : tags.keySet()) {
ListValue values = tags.get(tag);
if (values != null) {
for (Value value : values.getValuesList()) {
Object attrValue = textTagCodec.decode(
tag + ":" + getTagType(tag) + ":" + value.getStringValue())
.getValue();
if (attrValue instanceof TagValueAndUnsignedArrayFlag) {
record.setUnsignedArrayAttribute(tag,
((TagValueAndUnsignedArrayFlag)attrValue).value);
} else {
record.setAttribute(tag, attrValue);
}
}
}
}
}
return record;
}
public static final SAMRecord makeSAMRecord(Read read,
ReadGroupSet readGroupSet, List<Reference> references,
boolean forceSetMatePositionToThisPosition) {
return makeSAMRecord(read, makeSAMFileHeader(readGroupSet, references));
}
public static final SAMRecord makeSAMRecord(Read read,
boolean forceSetMatePositionToThisPosition) {
return makeSAMRecord(read, new SAMFileHeader());
}
/**
* Generates a SAMFileHeader from a ReadGroupSet and Reference metadata
*/
public static final SAMFileHeader makeSAMFileHeader(ReadGroupSet readGroupSet,
List<Reference> references) {
List<SAMFileHeader> samHeaders = new ArrayList<SAMFileHeader>(2);
SAMFileHeader samHeader = new SAMFileHeader();
samHeaders.add(samHeader);
// Reads are always returned in coordinate order form the API.
samHeader.setSortOrder(SAMFileHeader.SortOrder.coordinate);
if (references != null && references.size() > 0) {
SAMSequenceDictionary dict = new SAMSequenceDictionary();
for (Reference reference : references) {
if (reference.getName() != null && reference.getLength() != 0) {
SAMSequenceRecord sequence = new SAMSequenceRecord(reference.getName(),
(int)reference.getLength());
dict.addSequence(sequence);
}
}
samHeader.setSequenceDictionary(dict);
}
List<SAMProgramRecord> programs = null;
if (readGroupSet.getReadGroupsCount() != 0) {
List<SAMReadGroupRecord> readgroups = Lists.newArrayList();
for (ReadGroup RG : readGroupSet.getReadGroupsList()) {
if (RG.getId() != null && RG.getName() != null) {
String readGroupName = RG.getName();
if (readGroupName == null || readGroupName.isEmpty()) {
// We have to set the name to something, so if for some reason the proper
// SAM tag for name was missing, we will use the generated id.
readGroupName = RG.getId();
}
SAMReadGroupRecord readgroup = new SAMReadGroupRecord(readGroupName);
if (RG.getDescription() != null && !RG.getDescription().isEmpty()) {
readgroup.setDescription(RG.getDescription());
}
readgroup.setPredictedMedianInsertSize(RG.getPredictedInsertSize());
if (RG.getSampleId() != null) {
readgroup.setSample(RG.getSampleId());
}
if (RG.getExperiment() != null) {
if (RG.getExperiment().getLibraryId() != null && !RG.getExperiment().getLibraryId().isEmpty()) {
readgroup.setLibrary(RG.getExperiment().getLibraryId());
}
if (RG.getExperiment().getSequencingCenter() != null && !RG.getExperiment().getSequencingCenter().isEmpty()) {
readgroup.setSequencingCenter(RG.getExperiment().getSequencingCenter());
}
if (RG.getExperiment().getInstrumentModel() != null && !RG.getExperiment().getInstrumentModel().isEmpty()) {
readgroup.setPlatform(RG.getExperiment().getInstrumentModel());
}
if (RG.getExperiment().getPlatformUnit() != null && !RG.getExperiment().getPlatformUnit().isEmpty()) {
readgroup.setPlatformUnit(RG.getExperiment().getPlatformUnit());
}
}
readgroups.add(readgroup);
}
if (RG.getProgramsCount() > 0) {
if (programs == null) {
programs = Lists.newArrayList();
}
for (Program PG : RG.getProgramsList()) {
SAMProgramRecord program = new SAMProgramRecord(PG.getId());
if (PG.getCommandLine() != null && !PG.getCommandLine().isEmpty()) {
program.setCommandLine(PG.getCommandLine());
}
if (PG.getName() != null && !PG.getName().isEmpty()) {
program.setProgramName(PG.getName());
}
if (PG.getPrevProgramId() != null && !PG.getPrevProgramId().isEmpty()) {
program.setPreviousProgramGroupId(PG.getPrevProgramId());
}
if (PG.getVersion() != null && !PG.getVersion().isEmpty()) {
program.setProgramVersion(PG.getVersion());
}
programs.add(program);
}
}
}
samHeader.setReadGroups(readgroups);
if (programs != null) {
samHeader.setProgramRecords(programs);
}
}
// If BAM file is imported with non standard reference, the SQ tags
// are preserved in the info key/value array.
// Attempt to read them form there.
if (references == null || references.size() <= 0) {
@SuppressWarnings("unchecked")
Map<String, ListValue> tags = readGroupSet.getInfo();
if (tags != null) {
LOG.info("Getting @SQ header data from readgroupset info");
StringBuffer buf = new StringBuffer();
for (String tag : tags.keySet()) {
if (!tag.startsWith(HEADER_SAM_TAG_INFO_KEY_PREFIX)) {
continue;
}
final String headerName = tag.substring(HEADER_SAM_TAG_INFO_KEY_PREFIX.length());
ListValue values = tags.get(tag);
if (values == null) {
continue;
}
for (Value value : values.getValuesList()) {
buf.append(headerName);
buf.append("\t");
buf.append(value.getStringValue());
buf.append("\r\n");
}
final String headerString = buf.toString();
final SAMTextHeaderCodec codec = new SAMTextHeaderCodec();
codec.setValidationStringency(ValidationStringency.STRICT);
final SAMFileHeader parsedHeader = codec.decode(
new StringLineReader(headerString), null);
samHeaders.add(parsedHeader);
}
}
}
final SAMFileHeader finalHeader =
(new SamFileHeaderMerger(
SAMFileHeader.SortOrder.coordinate, samHeaders, true))
.getMergedHeader();
return finalHeader;
}
/*
* Regular expression for MD tag.
*
* \G = end of previous match.
* (?:[0-9]+) Number of bases in which read matches reference.
* - or -
* Single reference base for case in which reference differs from read.
* - or -
* ^one or more reference bases that are deleted in read.
*
*/
private static final Pattern mdPattern =
Pattern.compile("\\G(?:([0-9]+)|([ACTGNactgn])|(\\^[ACTGNactgn]+))");
/**
* Grab the value of the provided SAM tag if it exists (returns null otherwise)
* @param read the read whose SAM tags need to be searched
* @param tag the desired tag
* @return string corresponding to SAM tag or null if it doesn't exist
*/
public static String getSamTag(Read read, String tag) {
ListValue value = read.getInfo().get(tag);
if (value == null) {
return null;
}
return value.getValues(0).getStringValue();
}
/**
* Create the overlapping reference sequence by parsing the read and it's MD tag. This function
* will return null if the read is unaligned or doesn't contain a usable MD tag. If there is an
* insertion in the read the reference contains '-'. If the read is soft-clipped the reference
* contains '0'.
* @param read the read to parse
* @return overlapping reference string or null if not possible to compute
*/
public static String inferReferenceSequenceByParsingMdFlag(Read read) {
String mdTag = getSamTag(read, "MD");
// Make sure this read has a valid alignment with Cigar Units and usable MD tag
if (!read.hasAlignment() || (read.getAlignment().getCigarCount() == 0) || mdTag == null) {
return null;
}
String readSeq = read.getAlignedSequence();
StringBuilder refSeqBuilder = new StringBuilder();
Matcher match = mdPattern.matcher(mdTag);
int curReadPos = 0;
int numSavedBases = 0;
for (CigarUnit unit : read.getAlignment().getCigarList()) {
CigarUnit.Operation cigarOp = unit.getOperation();
int cigarOpLength = (int) unit.getOperationLength();
switch(cigarOp) {
case ALIGNMENT_MATCH:
case SEQUENCE_MISMATCH:
case SEQUENCE_MATCH:
case DELETE:
// these operators consume both read and reference bases so we need to parse the MD tag
int numMatchedBases = 0;
// Do we have any saved matched bases from a previous cigar operator?
while ((numSavedBases > 0) && (numMatchedBases < cigarOpLength)) {
refSeqBuilder.append(readSeq.charAt(curReadPos++));
numSavedBases--;
numMatchedBases++;
}
while (numMatchedBases < cigarOpLength) {
boolean matched = match.find();
if (matched) {
// need to use the regular expression to parse the MD tag
String mg1 = match.group(1);
String mg2 = match.group(2);
String mg3 = match.group(3);
if (mg1 != null && mg1.length() > 0) {
// this token is a number which means a series of matches
int numMatches = Integer.parseInt(mg1);
for (int i = 0; i < numMatches; i++) {
if (numMatchedBases < cigarOpLength) {
refSeqBuilder.append(readSeq.charAt(curReadPos++));
} else {
numSavedBases++;
}
numMatchedBases++;
}
} else if (mg2 != null && mg2.length() > 0) {
// this token is a single nucleotide which means a mismatching base
refSeqBuilder.append(mg2.charAt(0));
curReadPos++;
numMatchedBases++;
} else if (mg3 != null && mg3.length() > 0) {
// this token starts with a caret which means deleted bases
numMatchedBases += mg3.length() - 1; // don't include the caret
} else {
matched = false;
}
}
if (!matched) {
throw new IllegalStateException("Unusable or malformatted MD tag found: " + mdTag);
}
}
break;
case CLIP_SOFT:
case INSERT:
// these operators consume read bases but not reference bases
for (int i = 0; i < unit.getOperationLength(); i++) {
refSeqBuilder.append(cigarOp.equals(CigarUnit.Operation.CLIP_SOFT) ? '0' : '-');
curReadPos++;
}
break;
case SKIP:
case PAD:
case CLIP_HARD:
default:
// these operators don't consume aligned read bases and aren't included in the
// reference sequence so just skip them
break;
}
}
return refSeqBuilder.toString();
}
}