@BeforeTest
 public void setUp() {
   builder.setReadLength(READ_LENGTH);
   // Will be kept when an interval overlaps chromosome 1 in the first 151
   // bases.
   builder.addPair("mapped_pair_chr1", 0, 1, 151);
   // Will be kept when an interval overlaps chromsome 2 in the first 151
   // bases.
   builder.addPair("mapped_pair_chr2", 1, 1, 151);
   // The first read should pass and second should not, but both will
   // be kept in first test.
   builder.addPair("one_of_pair", 0, 1, 1000);
   // The second read is unmapped, but both should be kept in an
   // interval test where the interval includes chromosome four, where
   // read one will overlap.
   builder.addPair(
       "second_mate_unmapped",
       3,
       -1,
       1,
       1000,
       false,
       true,
       "151M",
       null,
       false,
       false,
       false,
       false,
       -1);
   // The first read is unmapped but both should be kept in an
   // interval test where the interval includes chromosome four, where
   // read two will overlap.
   builder.addPair(
       "first_mate_unmapped",
       -1,
       3,
       1000,
       1,
       true,
       false,
       null,
       "151M",
       false,
       false,
       false,
       false,
       -1);
   // This pair will overlap any interval that includes chromosome 1:1000
   builder.addPair("prove_one_of_pair", 0, 1000, 1000);
   // These reads are unmapped and will not map to any intervals, so they
   // are never kept. This is tested below.
   builder.addPair(
       "both_unmapped", -1, -1, 1, 1, true, true, null, null, false, false, false, false, -1);
   // Secondary alignments are never kept by the interval filter.
   builder.addFrag("mapped_pair_chr1", 0, 1, false, false, "151M", null, -1, true, false);
   // Supplementary alignment are never kept by the interval filter.
   builder.addFrag("mapped_pair_chr1", 0, 1, false, false, "151M", null, -1, false, true);
 }
 /**
  * Basic positive and negative tests for the FailsReadQualityFilter
  *
  * @param readQualityFlag The read quality flag to be tested
  * @param expectedResult The expected result (true is the sequence should match the filter,
  *     otherwise false)
  */
 @Test(dataProvider = "data")
 public void testFailsReadQualityFilter(
     final String testName, final boolean readQualityFlag, final boolean expectedResult) {
   builder.addUnmappedFragment("testfrag");
   final SAMRecord record = builder.iterator().next();
   record.setReadFailsVendorQualityCheckFlag(readQualityFlag);
   Assert.assertEquals(filter.filterOut(record), expectedResult, testName);
 }
  @Test(dataProvider = "testData")
  public void testIntervalPairFilter(
      final List<Interval> intervals, final long expectedPassingRecords) {
    final IntervalKeepPairFilter filter = new IntervalKeepPairFilter(intervals);

    long actualPassingRecords =
        StreamSupport.stream(builder.spliterator(), false)
            .filter(rec -> !filter.filterOut(rec))
            .count();

    Assert.assertEquals(actualPassingRecords, expectedPassingRecords);
  }
  /////////////////////////////////////////////////////////////////////////////
  // Used to generate the Sam Record Sets with SamRecordSetBuilder.addPair().
  // testNumber 1: runGcBiasMultiLevelTest, generates records aligning to chrM and chrO
  // testNumber 2: runWindowsComparisonTest, generates records aligning to chrM,N,O.
  /////////////////////////////////////////////////////////////////////////////
  public void setupTest1(
      final int ID,
      final String readGroupId,
      final SAMReadGroupRecord readGroupRecord,
      final String sample,
      final String library,
      final SAMFileHeader header,
      final SAMRecordSetBuilder setBuilder)
      throws IOException {

    final String separator = ":";
    final int contig1 = 0;
    final int contig2 = 1;
    readGroupRecord.setSample(sample);
    readGroupRecord.setPlatform(platform);
    readGroupRecord.setLibrary(library);
    readGroupRecord.setPlatformUnit(readGroupId);
    header.addReadGroup(readGroupRecord);
    setBuilder.setReadGroup(readGroupRecord);
    setBuilder.setUseNmFlag(true);

    setBuilder.setHeader(header);

    final int max = 800;
    final int min = 1;
    final Random rg = new Random(5);

    // add records that align to chrM and O but not N
    for (int i = 0; i < NUM_READS; i++) {
      final int start = rg.nextInt(max) + min;
      final String newReadName = READ_NAME + separator + ID + separator + i;

      if (i != NUM_READS - 1) {
        setBuilder.addPair(newReadName, contig1, start + ID, start + ID + LENGTH);
      } else {
        setBuilder.addPair(newReadName, contig2, start + ID, start + ID + LENGTH);
      }
    }
  }
  @Test
  public void testNotPrimaryReads() {
    final List<Interval> intervalList = new ArrayList<>();
    final Interval interval1 = new Interval("chr1", 1, 999);
    intervalList.add(interval1);

    final IntervalKeepPairFilter filter = new IntervalKeepPairFilter(intervalList);

    boolean notPrimary =
        StreamSupport.stream(builder.spliterator(), false)
            .filter(rec -> !filter.filterOut(rec))
            .anyMatch(
                rec -> rec.getNotPrimaryAlignmentFlag() || rec.getSupplementaryAlignmentFlag());

    Assert.assertFalse(notPrimary);
  }
  @Test
  public void testUnmappedPair() {
    final List<Interval> intervalList = new ArrayList<>();

    final Interval interval1 = new Interval("chr1", 1, 999);
    final Interval interval2 = new Interval("chr3", 1, 2);
    final Interval interval3 = new Interval("chr2", 1, 2);
    final Interval interval4 = new Interval("chr4", 1, 2);

    intervalList.addAll(CollectionUtil.makeList(interval1, interval2, interval3, interval4));

    final IntervalKeepPairFilter filter = new IntervalKeepPairFilter(intervalList);

    boolean unmappedPassed =
        StreamSupport.stream(builder.spliterator(), false)
            .filter(rec -> !filter.filterOut(rec))
            .anyMatch(rec -> rec.getReadName().equals("both_unmapped"));

    Assert.assertFalse(unmappedPassed);
  }