public void testOk1() throws Exception {
    final File map = File.createTempFile("testok1", "coverageParams");
    try {
      FileUtils.stringToFile(SharedSamConstants.SAM9, map);
      assertTrue(map.isFile());

      final CoverageParams ccp;
      final File outFile = new File(mDir, TEST_OUTPUT);
      assertTrue(outFile.mkdir());

      final List<File> mapped = new ArrayList<>();
      mapped.add(map);

      ccp = getCoverageParams(outFile, mapped);

      assertFalse(ccp.errorRates());
      assertNotNull(ccp.filterParams());
      assertEquals(1, ccp.ioThreads());
      assertEquals(3, ccp.minimumCoverageForBreadth());
      final String ccs = ccp.toString();
      // System.err.println(ccs);
      TestUtils.containsAll(ccs, BASE_PARAMS);
      ccp.close();
    } finally {
      assertTrue(map.delete());
    }
  }
 /**
  * Creates test serialized form for current serial version
  *
  * @param args directory in which test data should be saved
  * @throws IOException it happens
  */
 public static void main(String[] args) throws IOException {
   final File dir = new File(args[0]);
   final File output =
       new File(dir, "testZeroRVersion_" + ZeroRBuilder.ZeroRClassifier.SERIAL_VERSION);
   try (DataOutputStream dos = new DataOutputStream(FileUtils.createOutputStream(output))) {
     createTestZeroR().save(dos, null);
   }
 }
 /**
  * Creates a GZIP file copying the string <code>content</code> to it.
  *
  * @param content a <code>String</code>
  * @param file a non-null <code>File</code> to write to
  * @return a <code>File</code> containing the string content (same as <code>file</code>).
  * @exception IOException if an error occurs.
  * @exception NullPointerException if the content is null
  */
 public static File stringToGzFile(final String content, final File file) throws IOException {
   if (content == null) {
     throw new NullPointerException("null string given");
   }
   try (OutputStream out = FileUtils.createOutputStream(file, true, false, true)) {
     out.write(content.getBytes());
   }
   return file;
 }
 /**
  * Gets the contents of the given resource as a string.
  *
  * @param resource name (classpath) of the resource.
  * @return a String containing the contents of the stream
  * @exception IOException if an error occurs.
  */
 public static String resourceToString(final String resource) throws IOException {
   final InputStream str = Resources.getResourceAsStream(resource);
   if (str == null) {
     throw new RuntimeException("Unable to find resource:" + resource);
   }
   final String res = FileUtils.streamToString(str);
   str.close();
   return res;
 }
 private ReaderParams makeGenome() throws IOException {
   final File subjectsDir = FileUtils.createTempDir("test", "coverageparams", mDir);
   ReaderTestUtils.getReaderDNA(">t\nacgt", subjectsDir, null).close();
   return SequenceParams.builder()
       .directory(subjectsDir)
       .mode(SequenceMode.UNIDIRECTIONAL)
       .create()
       .readerParams();
 }
  public void writeTempFile(File out) throws IOException {
    final TempRecordWriter trw =
        new TempRecordWriterNio(FileUtils.createOutputStream(out, true, false));
    try {
      final BinaryTempFileRecord bar = new BinaryTempFileRecord(false, false, false, false);

      //      + "3\t16\t0\t28734\t30\t35M\t=\t0\t0\tACCT\t<<<<\tAS:i:0\tNM:i:1\n"
      bar.setAlignmentScore(0);
      bar.setCigarString("35M".getBytes());
      bar.setMdString(new byte[0]);
      bar.setNumberMismatches(0);
      bar.setReadId(3);
      bar.setReferenceId(0);
      bar.setSamFlags((byte) 16);
      bar.setStartPosition(28734);
      trw.writeRecord(bar);

      //      + "1\t0\t0\t28833\t20\t4M5M\t=\t0\t0\tAGCT\t<<<<\tNM:i:1\tAS:i:3\n"
      bar.setAlignmentScore(3);
      bar.setCigarString("4M5M".getBytes());
      bar.setNumberMismatches(1);
      bar.setReadId(1);
      bar.setSamFlags((byte) 0);
      bar.setStartPosition(28833);
      trw.writeRecord(bar);

      //      + "3\t16\t0\t28834\t30\t35M\t=\t0\t0\tACCT\t<<<<\tMF:i:18\tAS:i:1\n"
      bar.setAlignmentScore(1);
      bar.setCigarString("35M".getBytes());
      bar.setNumberMismatches(0);
      bar.setReadId(3);
      bar.setSamFlags((byte) 16);
      bar.setStartPosition(28834);
      trw.writeRecord(bar);

      //      + "20\t0\t0\t28934\t30\t35M\t=\t0\t0\tACCT\t<<<<\tMF:i:18\tAS:i:4\n"
      bar.setAlignmentScore(4);
      bar.setReadId(20);
      bar.setSamFlags((byte) 0);
      bar.setStartPosition(28934);
      trw.writeRecord(bar);

      //      + "66\t0\t0\t28934\t30\t35M\t=\t0\t0\tACCT\t<<<<\tMF:i:18\tAS:i:4\n";
      bar.setReadId(66);
      trw.writeRecord(bar);

      bar.setSentinelRecord();
      trw.writeRecord(bar);
    } finally {
      trw.close();
    }
  }
 /**
  * Read the contents of a reader and turn it into a string.
  *
  * @param fileReader where to get the string.
  * @return the contents of the file.
  * @throws IOException if an I/O error occurs.
  */
 public static String readerToString(final Reader fileReader) throws IOException {
   final StringBuilder sb = new StringBuilder();
   try (BufferedReader br = new BufferedReader(fileReader)) {
     final char[] buffer = FileUtils.makeBuffer();
     final int eof = -1;
     for (int len = br.read(buffer); len > eof; len = br.read(buffer)) {
       for (int i = 0; i < len; i++) {
         sb.append(buffer[i]);
       }
     }
   }
   return sb.toString();
 }
 public void testFencePost() throws IOException {
   final File dir = FileUtils.createTempDir("cmsrt", "fencepost");
   try {
     randomSDF(dir, 256, 8200, 2L * 1024 * 1024);
     final CompressedMemorySequencesReader reader =
         (CompressedMemorySequencesReader)
             CompressedMemorySequencesReader.createSequencesReader(
                 dir, true, false, LongRange.NONE);
     assertTrue(reader.checkChecksums());
   } finally {
     assertTrue(FileHelper.deleteAll(dir));
   }
 }
 public void testDefaultParams() throws IOException {
   final File outDir = new File(mDir, "output");
   final CoverageParams cp =
       CoverageParams.builder()
           .outputParams(new OutputParams(outDir, false, false))
           .genome(makeGenome())
           .create();
   assertFalse(cp.tsvOutput());
   assertTrue(cp.bedOutput());
   assertFalse(cp.blockCompressed());
   assertFalse(cp.onlyMappedRegions());
   assertFalse(cp.errorRates());
   assertEquals(0, cp.smoothing());
   assertEquals(1, cp.minimumCoverageForBreadth());
   assertEquals(outDir, cp.directory());
   assertEquals(cp.outFile(), cp.file("coverage.bed"));
   assertEquals("coverage.bed", cp.outFile().getName());
   final OutputStream out = cp.bedStream();
   out.write("test".getBytes());
   out.close();
   assertTrue(cp.outFile().exists());
   assertEquals("test", FileUtils.fileToString(cp.outFile()));
 }
  public void testFilterUnmated() throws IOException {
    final ByteArrayOutputStream log = new ByteArrayOutputStream();
    try (PrintStream prLog = new PrintStream(log)) {
      Diagnostic.setLogStream(prLog);
      final int numReads = 100;
      final MapQScoringReadBlocker blocker = new MapQScoringReadBlocker(numReads, 2);
      blocker.increment(1, 3);
      blocker.increment(20, 4);
      blocker.increment(20, 4);
      blocker.increment(20, 4); // read 20 is blocked for score=4
      blocker.increment(3, 1);
      blocker.increment(3, 1);
      blocker.increment(3, 1); // read 3 is blocked for score=1
      blocker.increment(3, 0);
      blocker.increment(3, 0); // read 3 is just not blocked for score=0
      final File dir = FileUtils.createTempDir("test", "unmatedSamFilter");
      OutputStream out = null;
      try {
        final File in1 = File.createTempFile("sam", "_1.gz", dir);
        writeTempFile(in1);
        final File outFile = File.createTempFile("out", ".gz", dir);
        out = new GZIPOutputStream(new FileOutputStream(outFile));

        final StatusListener listener = new StatusListener(numReads);
        final ReadBlocker freqBlocker = new ReadBlocker(numReads, 2);
        freqBlocker.increment(66);
        freqBlocker.increment(66);

        final MockSequencesReader msr =
            new MockSequencesReader(SequenceType.DNA) {
              @Override
              public PrereadType getPrereadType() {
                return PrereadType.UNKNOWN;
              }

              @Override
              public boolean hasQualityData() {
                return true;
              }

              @Override
              public int read(long sequenceIndex, byte[] dataOut) {
                dataOut[0] = 1;
                if (sequenceIndex == 3) {
                  dataOut[1] = 3;
                  dataOut[2] = 3;
                } else {
                  dataOut[1] = 3;
                  dataOut[2] = 2;
                }
                dataOut[3] = 4;
                return 4;
              }

              @Override
              public int readQuality(final long sequenceIndex, final byte[] dest) {
                dest[0] = dest[1] = dest[2] = dest[3] = '<' - 33;
                return 4;
              }
            };

        final SingleEndSamResultsFilter filter =
            new SingleEndSamResultsFilter(blocker, freqBlocker, listener, 0, msr, null, false);
        assertEquals("Alignment", filter.getName());
        filter.filterConcat(makeHeader(), out, null, null, mTemplateReader, false, in1);
        out.close();
        final String contents = FileHelper.gzFileToString(outFile);
        // System.out.println("contents=" + contents);
        assertTrue(
            TestUtils.sameLines(SAM_UNMATED_EXPECTED, TestUtils.stripSAMHeader(contents), false));

        // now check that the listener has been updated correctly.
        for (int read = 0; read < numReads; read++) {
          final int expect;
          switch (read) {
            case 1:
            case 3:
              expect = ReadStatusTracker.UNMATED_FIRST;
              break;
            default:
              expect = 0;
              break;
          }
          assertEquals("readId=" + read, expect, listener.getStatus(read));
        }
      } finally {
        if (out != null) {
          out.close();
        }
        assertTrue(FileHelper.deleteAll(dir));
      }
    } finally {
      Diagnostic.setLogStream();
    }
    final String logString = log.toString();
    // System.err.println(logString);
    TestUtils.containsAll(logString, "Alignment SAM filter outputs 2/5 records");
  }
 /**
  * Creates an empty directory in the unit test temporary-file directory, automatically generating
  * its name.
  *
  * @return an empty temporary directory
  * @exception IOException if a file could not be created
  */
 public static File createTempDirectory() throws IOException {
   return FileUtils.createTempDir(PREFIX, SUFFIX);
 }
 /**
  * Constructor
  *
  * @param outputFile compressed file to write
  * @param bits number of bits used per entry
  * @throws IOException If an IO error occurs
  */
 public FileBitwiseOutputStream(File outputFile, int bits) throws IOException {
   mStream = new DataOutputStream(FileUtils.createOutputStream(outputFile, false));
   mBits = bits;
   // mRawBuffer = new byte[1024 * 1024];
   mBuffer = new long[1024 * 1024 / 8];
 }
 private void compareToFile(final String str, final File f) throws IOException {
   final String main = FileUtils.fileToString(f);
   assertEquals(str, main);
 }