private void replaceHardWithSoftClips(final SAMRecord rec) {
if (rec.getReadUnmappedFlag()) return;
if (rec.getCigar().isEmpty()) return;
List<CigarElement> elements = rec.getCigar().getCigarElements();
final CigarElement first = elements.get(0);
final CigarElement last = elements.size() == 1 ? null : elements.get(elements.size()-1);
final int startHardClip = first.getOperator() == CigarOperator.H ? first.getLength() : 0;
final int endHardClip = (last != null && last.getOperator() == CigarOperator.H) ? last.getLength() : 0;
if (startHardClip + endHardClip > 0) {
final int len = rec.getReadBases().length + startHardClip + endHardClip;
// Fix the basecalls
final byte[] bases = new byte[len];
Arrays.fill(bases, (byte) 'N');
System.arraycopy(rec.getReadBases(), 0, bases, startHardClip, rec.getReadBases().length);
// Fix the quality scores
final byte[] quals = new byte[len];
Arrays.fill(quals, (byte) 2 );
System.arraycopy(rec.getBaseQualities(), 0, quals, startHardClip, rec.getBaseQualities().length);
// Fix the cigar!
elements = new ArrayList<CigarElement>(elements); // make it modifiable
if (startHardClip > 0) elements.set(0, new CigarElement(first.getLength(), CigarOperator.S));
if (endHardClip > 0) elements.set(elements.size()-1, new CigarElement(last.getLength(), CigarOperator.S));
// Set the update structures on the new record
rec.setReadBases(bases);
rec.setBaseQualities(quals);
rec.setCigar(new Cigar(elements));