Map<String, SAMRecord> outputReadAlignmentInfo = new HashMap<String, SAMRecord>();
for (HitInfo hitInfo : bestHits) {
SAMRecord contigRead = hitInfo.getRecord();
int position = hitInfo.getPosition() - 1;
List<ReadBlock> contigReadBlocks = ReadBlock.getReadBlocks(contigRead);
ReadPosition readPosition = new ReadPosition(origRead, position, -1);
SAMRecord updatedRead = updateReadAlignment(contigRead,
contigReadBlocks, readPosition);
if (updatedRead != null) {
if (updatedRead.getReadUnmappedFlag()) {
updatedRead.setReadUnmappedFlag(false);
}
updatedRead.setReadNegativeStrandFlag(hitInfo.isOnNegativeStrand());
// If the read's alignment info has been modified, record the original alignment.
if (origRead.getReadUnmappedFlag() ||
!origRead.getReferenceName().equals(updatedRead.getReferenceName()) ||
origRead.getAlignmentStart() != updatedRead.getAlignmentStart() ||
origRead.getReadNegativeStrandFlag() != updatedRead.getReadNegativeStrandFlag() ||
!origRead.getCigarString().equals(updatedRead.getCigarString())) {
if (SAMRecordUtils.isSoftClipEquivalent(origRead, updatedRead)) {
// Restore Cigar and position
// System.out.println("Re-setting [" + updatedRead.getSAMString() + "] --- [" + origRead.getSAMString() + "]");
updatedRead.setAlignmentStart(origRead.getAlignmentStart());
updatedRead.setCigar(origRead.getCigar());
} else {
String originalAlignment;
if (origRead.getReadUnmappedFlag()) {
originalAlignment = "N/A";
} else {
originalAlignment = origRead.getReferenceName() + ":" + origRead.getAlignmentStart() + ":" +
(origRead.getReadNegativeStrandFlag() ? "-" : "+") + ":" + origRead.getCigarString();
}
// Read's original alignment position
updatedRead.setAttribute(ORIGINAL_ALIGNMENT_TAG, originalAlignment);
}
}
// Mismatches to the contig
updatedRead.setAttribute(MISMATCHES_TO_CONTIG_TAG, hitInfo.getNumMismatches());
// Contig's mapping quality
updatedRead.setAttribute(CONTIG_QUALITY_TAG, hitInfo.getRecord().getMappingQuality());
// Contig's length
// updatedRead.setAttribute("YL", hitInfo.getRecord().getCigar().getReadLength());
// Contig Position + CIGAR
// updatedRead.setAttribute(CONTIG_ALIGNMENT_TAG, contigRead.getReferenceName() + ":" + contigRead.getAlignmentStart() +
// ":" + contigRead.getCigarString());
updatedRead.setAttribute(CONTIG_ALIGNMENT_TAG, contigRead.getStringAttribute("ZZ"));
//TODO: Check strand!!!
String readAlignmentInfo = updatedRead.getReferenceName() + "_" + updatedRead.getAlignmentStart() + "_" +
updatedRead.getCigarString();
if (!outputReadAlignmentInfo.containsKey(readAlignmentInfo)) {
outputReadAlignmentInfo.put(readAlignmentInfo, updatedRead);
}
}