/**
   * Calculates the reference coordinate for the end of the read taking into account soft clips but
   * not hard clips.
   *
   * <p>Note: getUnclippedEnd() adds soft and hard clips, this function only adds soft clips.
   *
   * @return the unclipped end of the read taking soft clips (but not hard clips) into account
   */
  public int getSoftEnd() {
    if (softEnd == UNINITIALIZED) {
      boolean foundAlignedBase = false;
      softEnd = getAlignmentEnd();
      final List<CigarElement> cigs = getCigar().getCigarElements();
      for (int i = cigs.size() - 1; i >= 0; --i) {
        final CigarElement cig = cigs.get(i);
        final CigarOperator op = cig.getOperator();

        if (op
            == CigarOperator
                .SOFT_CLIP) // assumes the soft clip that we found is at the end of the aligned read
        softEnd += cig.getLength();
        else if (op != CigarOperator.HARD_CLIP) {
          foundAlignedBase = true;
          break;
        }
      }
      if (!foundAlignedBase) { // for example 64H14S, the soft end is actually the same as the
                               // alignment end
        softEnd = getAlignmentEnd();
      }
    }

    return softEnd;
  }