private static final Cigar getCigar2(Collection<ReadFeature> features,
int readLength) {
if (features == null || features.isEmpty()) {
CigarElement ce = new CigarElement(readLength, CigarOperator.M);
return new Cigar(Arrays.asList(ce));
}
List<CigarElement> list = new ArrayList<CigarElement>();
int totalOpLen = 1;
CigarElement ce;
CigarOperator lastOperator = CigarOperator.MATCH_OR_MISMATCH;
int lastOpLen = 0;
int lastOpPos = 1;
CigarOperator co = null;
int rfLen = 0;
for (ReadFeature f : features) {
int gap = f.getPosition() - (lastOpPos + lastOpLen);
if (gap > 0) {
if (lastOperator != CigarOperator.MATCH_OR_MISMATCH) {
list.add(new CigarElement(lastOpLen, lastOperator));
lastOpPos += lastOpLen;
totalOpLen += lastOpLen;
lastOpLen = gap;
} else {
lastOpLen += gap;
}
lastOperator = CigarOperator.MATCH_OR_MISMATCH;
}
switch (f.getOperator()) {
case Insertion.operator:
co = CigarOperator.INSERTION;
rfLen = ((Insertion) f).getSequence().length;
break;
case SoftClip.operator:
co = CigarOperator.SOFT_CLIP;
rfLen = ((SoftClip) f).getSequence().length;
break;
case HardClip.operator:
co = CigarOperator.HARD_CLIP ;
rfLen = ((HardClip) f).getSequence().length;
break;
case InsertBase.operator:
co = CigarOperator.INSERTION;
rfLen = 1;
break;
case Deletion.operator:
co = CigarOperator.DELETION;
rfLen = ((Deletion) f).getLength();
break;
case RefSkip.operator:
co = CigarOperator.SKIPPED_REGION;
rfLen = ((RefSkip) f).getLength();
break;
case Padding.operator:
co = CigarOperator.PADDING ;
rfLen = ((Padding) f).getLength();
break;
case Substitution.operator:
case ReadBase.operator:
co = CigarOperator.MATCH_OR_MISMATCH;
rfLen = 1;
break;
default:
continue;
}
if (lastOperator != co) {
// add last feature
if (lastOpLen > 0) {
list.add(new CigarElement(lastOpLen, lastOperator));
totalOpLen += lastOpLen;
}
lastOperator = co;
lastOpLen = rfLen;
lastOpPos = f.getPosition();
} else
lastOpLen += rfLen;
if (!co.consumesReadBases())
lastOpPos -= rfLen;
}
if (lastOperator != null) {
if (lastOperator != CigarOperator.M) {
list.add(new CigarElement(lastOpLen, lastOperator));
if (readLength >= lastOpPos + lastOpLen) {
ce = new CigarElement(readLength - (lastOpLen + lastOpPos)
+ 1, CigarOperator.M);
list.add(ce);
}
} else if (readLength > lastOpPos - 1) {
ce = new CigarElement(readLength - lastOpPos + 1,
CigarOperator.M);
list.add(ce);
}
}
if (list.isEmpty()) {
ce = new CigarElement(readLength, CigarOperator.M);
return new Cigar(Arrays.asList(ce));
}
return new Cigar(list);
}