/** @{inheritDoc} */
  @Override
  public SAMRecordIterator query(
      final String sequence, final int start, final int end, final boolean contained) {
    GenomeLoc region = genomeLocParser.createGenomeLoc(sequence, start, end);
    List<SAMRecord> coveredSubset = new ArrayList<SAMRecord>();

    for (SAMRecord read : reads) {
      GenomeLoc readPosition = genomeLocParser.createGenomeLoc(read);
      if (contained && region.containsP(readPosition)) coveredSubset.add(read);
      else if (!contained && readPosition.overlapsP(region)) coveredSubset.add(read);
    }

    final Iterator<SAMRecord> iterator = coveredSubset.iterator();
    return new SAMRecordIterator() {
      public boolean hasNext() {
        return iterator.hasNext();
      }

      public SAMRecord next() {
        return iterator.next();
      }

      public void close() {}

      public void remove() {
        iterator.remove();
      }

      public SAMRecordIterator assertSorted(SAMFileHeader.SortOrder sortOrder) {
        return this;
      }
    };
  }
  private void writeDifferences(
      final List<VariantContext> source1Alleles, final List<VariantContext> source2Alleles) {
    int currentIndex1 = 0, currentIndex2 = 0;
    final int size1 = source1Alleles.size(), size2 = source2Alleles.size();
    VariantContext current1 = source1Alleles.get(0);
    VariantContext current2 = source2Alleles.get(0);

    while (currentIndex1 < size1 || currentIndex2 < size2) {
      if (current1 == null) {
        writeOne(current2, source2, null);
        currentIndex2++;
        current2 = (currentIndex2 < size2 ? source2Alleles.get(currentIndex2) : null);
      } else if (current2 == null) {
        writeOne(current1, source1, null);
        currentIndex1++;
        current1 = (currentIndex1 < size1 ? source1Alleles.get(currentIndex1) : null);
      } else {

        final GenomeLoc loc1 = getToolkit().getGenomeLocParser().createGenomeLoc(current1);
        final GenomeLoc loc2 = getToolkit().getGenomeLocParser().createGenomeLoc(current2);

        if (loc1.getStart() == loc2.getStart() || loc1.overlapsP(loc2)) {
          String status;
          if (loc1.getStart() == loc2.getStart()) {
            final String allele1 = current1.getAlternateAllele(0).getBaseString();
            final String allele2 = current2.getAlternateAllele(0).getBaseString();
            if (allele1.indexOf(allele2) != -1 || allele2.indexOf(allele1) != -1)
              status = ONE_ALLELE_SUBSET_OF_OTHER_STATUS;
            else status = SAME_START_DIFFERENT_ALLELES_STATUS;
          } else {
            status = OVERLAPPING_EVENTS_STATUS;
          }

          writeOne(current1, INTERSECTION_SET, status);
          currentIndex1++;
          currentIndex2++;
          current1 = (currentIndex1 < size1 ? source1Alleles.get(currentIndex1) : null);
          current2 = (currentIndex2 < size2 ? source2Alleles.get(currentIndex2) : null);
        } else if (loc1.isBefore(loc2)) {
          writeOne(current1, source1, null);
          currentIndex1++;
          current1 = (currentIndex1 < size1 ? source1Alleles.get(currentIndex1) : null);
        } else {
          writeOne(current2, source2, null);
          currentIndex2++;
          current2 = (currentIndex2 < size2 ? source2Alleles.get(currentIndex2) : null);
        }
      }
    }
  }