public void testRemoveMulti() throws Exception {
    try (TestDirectory td = new TestDirectory()) {
      final File f =
          FileHelper.resourceToGzFile(
              "com/rtg/vcf/resources/vcfsubset.vcf", new File(td, "vcf.vcf.gz"));
      final File out = new File(td, "out.vcf");

      checkMainInitOk(
          "-i",
          f.getPath(),
          "-o",
          out.getPath(),
          "--remove-samples",
          "--keep-info",
          "AN",
          "--keep-info",
          "AC",
          "--keep-filter",
          "YEA",
          "-Z");

      final String content = FileHelper.fileToString(out);
      final String editedContent =
          StringUtils.grepMinusV(StringUtils.grepMinusV(content, "^##RUN-ID"), "^##CL");
      mNano.check("vcfsubset-multi.vcf", editedContent);
    }
  }
  public void testExplosion() throws Exception {
    try (TestDirectory td = new TestDirectory()) {
      final File f =
          FileHelper.resourceToGzFile(
              "com/rtg/vcf/resources/vcfsubset.vcf", new File(td, "vcf.vcf.gz"));
      final File out = new File(td, "out.vcf");

      assertEquals(
          "Records skipped due to invalid or incompatible sample fields: 1" + StringUtils.LS,
          checkMainInitWarn(
              "-i",
              f.getPath(),
              "-o",
              out.getPath(),
              "--remove-format",
              "GT",
              "--remove-format",
              "DS",
              "-Z"));

      final String content = FileHelper.fileToString(out);
      final String nonheader = StringUtils.grepMinusV(content, "^#");
      assertTrue(nonheader.startsWith("X\t60052"));
    }
  }
  public static void makeTestTaxonSdf(File dest) throws IOException {
    final String sequences =
        FileHelper.resourceToString("com/rtg/reader/resources/sequences.fasta");
    ReaderTestUtils.getReaderDNA(sequences, dest, null).close();

    // cp taxonomy files to sdf
    final File taxonomy = new File(dest, "taxonomy.tsv");
    FileHelper.resourceToFile("com/rtg/reader/resources/taxonomy.tsv", taxonomy);
    final File taxonomyLookup = new File(dest, "taxonomy_lookup.tsv");
    FileHelper.resourceToFile("com/rtg/reader/resources/taxonomy_lookup.tsv", taxonomyLookup);
  }
  public void testMissingSample() throws Exception {
    try (TestDirectory td = new TestDirectory()) {
      final File f =
          FileHelper.resourceToGzFile(
              "com/rtg/vcf/resources/vcfsubset.vcf", new File(td, "vcf.vcf.gz"));
      final File out = new File(td, "out.vcf");

      assertEquals(
          "Error: Sample fields not contained in VCF header: BL RJ" + StringUtils.LS,
          checkMainInitBadFlags(
              "-i",
              f.getPath(),
              "-o",
              out.getPath(),
              "--keep-sample",
              "HG00097",
              "--keep-sample",
              "HG00099",
              "--keep-sample",
              "BL",
              "--keep-sample",
              "RJ",
              "-Z"));
    }
  }
  public void testTaxonomySDF() throws IOException {
    try (final TestDirectory dir = new TestDirectory("sdfstats")) {

      // One without tax
      final File smallSdf = ReaderTestUtils.getDNADir(dir);
      final String err =
          checkMainInitBadFlags("-i", smallSdf.getPath(), "-o", "-", "--taxons", "41431");
      TestUtils.containsAll(err, "does not contain taxonomy");

      // One with tax
      final File fullSdf = new File(dir, "sdf_full");
      final File faOut = new File(dir, "ex.fasta.gz");
      makeTestTaxonSdf(fullSdf);

      checkMainInitOk("-i", fullSdf.getPath(), "-o", faOut.getPath(), "--taxons", "41431");

      final String faout = FileHelper.gzFileToString(faOut);
      TestUtils.containsAll(
          faout,
          "gi|218169684|gb|CP001289.1|",
          "gi|218165370|gb|CP001287.1|",
          "gi|218169729|gb|CP001290.1|",
          "gi|218169631|gb|CP001288.1|");
    }
  }
 public void testInputAsFile() throws IOException {
   final File que = File.createTempFile("p2f", "flag");
   final String err = checkMainInitBadFlags("-o", "testFile", "-i", que.getPath());
   assertTrue(
       err.contains("Error: The specified file, \"" + que.getPath() + "\", is not an SDF."));
   assertTrue(FileHelper.deleteAll(que));
 }
 public void testFullName() throws IOException {
   try (TestDirectory dir = new TestDirectory("sdf2fasta")) {
     final File sdf = ReaderTestUtils.getDNADir(FULL_NAME_DATA, new File(dir, "sdf"));
     final File fasta = new File(dir, "fs.fasta.gz");
     checkMainInitOk("-i", sdf.getPath(), "-o", fasta.getPath());
     assertEquals(FULL_NAME_DATA, FileHelper.gzFileToString(fasta));
   }
 }
Example #8
0
 public void testToString() throws InvalidParamsException, IOException {
   final AbstractKappa kappa;
   kappa = getKappa();
   // System.out.println(kappa.toString());
   final String exp =
       FileHelper.resourceToString("com/rtg/variant/bayes/complex/resources/kappamemotest0.txt");
   assertTrue(TestUtils.sameLines(exp, kappa.toString(), false));
 }
  @Override
  public void testEquals() throws IOException {
    final File dir = FileHelper.createTempDirectory(mDir);
    final File otherDir = FileHelper.createTempDirectory(mDir);
    ReaderTestUtils.getReaderDNAFastq("", dir, false).close();
    ReaderTestUtils.getReaderDNAFastq("", otherDir, false).close();

    final CompressedMemorySequencesReader cmsr =
        new CompressedMemorySequencesReader(
            dir, new IndexFile(dir), 5, true, false, new LongRange(0, 0));
    final CompressedMemorySequencesReader other =
        new CompressedMemorySequencesReader(
            otherDir, new IndexFile(dir), 5, true, false, new LongRange(0, 0));
    assertTrue(cmsr.equals(cmsr));
    assertFalse(cmsr.equals(null));
    assertFalse(cmsr.equals(other));
    assertFalse(cmsr.equals("FDSA"));
  }
 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 testKeepSamples() throws Exception {
    try (TestDirectory td = new TestDirectory()) {
      final File f =
          FileHelper.resourceToGzFile(
              "com/rtg/vcf/resources/vcfsubset.vcf", new File(td, "vcf.vcf.gz"));
      final File out = new File(td, "out.vcf");

      checkMainInitOk(
          "-i",
          f.getPath(),
          "-o",
          out.getPath(),
          "--keep-sample",
          "HG00096",
          "--keep-sample",
          "HG00100",
          "-Z");

      final String content = FileHelper.fileToString(out);
      final String editedContent =
          StringUtils.grepMinusV(StringUtils.grepMinusV(content, "^##RUN-ID"), "^##CL");
      mNano.check("vcfsubset-keepsamples.vcf", editedContent);
    }
  }
 public void testValidUse() throws Exception {
   try (TestDirectory dir = new TestDirectory("sdf2fasta")) {
     createPreread(">x" + StringUtils.LS + "actgn" + StringUtils.LS, dir);
     final String pathpr = dir.getPath();
     try {
       runCommandWithNamedOutput(JUNITOUT, pathpr, "ACTGN");
       runCommandWithNamedOutput(JUNITOUT + ".FA", pathpr, "ACTGN");
       runCommandWithNamedOutput(JUNITOUT + ".fasta", pathpr, "ACTGN");
       runCommandLineLength2(pathpr);
     } finally {
       FileHelper.deleteAll(dir);
     }
     createPrereadProtein(dir);
     runCommandWithNamedOutput(JUNITOUT, pathpr, "X*ARNDCQEGHILKMFPSTWYV");
   }
 }
 /**
  * Read the contents of a zip file and turn it into a string
  *
  * @param file to be read
  * @return contents of file
  * @throws java.io.IOException if an IO error occurs
  */
 public static String zipFileToString(final File file) throws IOException {
   try (Reader reader =
       new InputStreamReader(GzipUtils.createGzipInputStream(new FileInputStream(file)))) {
     return FileHelper.readerToString(reader);
   }
 }
Example #14
0
 @Override
 public void tearDown() {
   assertTrue(FileHelper.deleteAll(mDir));
   mDir = null;
 }
  public void testValidation() throws Exception {
    try (TestDirectory main = new TestDirectory()) {
      final File in =
          FileHelper.resourceToGzFile(
              "com/rtg/vcf/resources/vcfsubset.vcf", new File(main, "vcf.vcf.gz"));
      final File foo = new File(main, "foo");
      final File out = new File(main, "out.vcf.gz");
      final File out2 = new File(main, "out.vcf");
      String err = checkHandleFlagsErr("-i", foo.getPath(), "-o", out.getPath());
      TestUtils.containsAll(TestUtils.unwrap(err), "Given file ", " does not exist.");

      err = checkHandleFlagsErr("-i", main.getPath(), "-o", out.getPath());
      TestUtils.containsAll(TestUtils.unwrap(err), main.getPath() + "\" is a directory");

      assertTrue(out.createNewFile());
      err = checkHandleFlagsErr("-i", in.getPath(), "-o", new File(main, "out.vcf").getPath());
      TestUtils.containsAll(
          TestUtils.unwrap(err), "The file \"" + out.getPath() + "\" already exists");

      assertTrue(out2.createNewFile());
      err = checkHandleFlagsErr("-i", in.getPath(), "-o", out2.getPath(), "--no-gzip");
      TestUtils.containsAll(
          TestUtils.unwrap(err), "The file \"" + out2.getPath() + "\" already exists");

      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-infos",
              "--remove-info",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-infos, --remove-info, or --keep-info can be set");
      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-infos",
              "--keep-info",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-infos, --remove-info, or --keep-info can be set");
      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-info",
              "feh",
              "--keep-info",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-infos, --remove-info, or --keep-info can be set");

      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-filters",
              "--remove-filter",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-filters, --remove-filter, or --keep-filter can be set");
      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-filters",
              "--keep-filter",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-filters, --remove-filter, or --keep-filter can be set");
      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-filter",
              "feh",
              "--keep-filter",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-filters, --remove-filter, or --keep-filter can be set");

      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-samples",
              "--remove-sample",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-samples, --remove-sample, or --keep-sample can be set");
      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-samples",
              "--keep-sample",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-samples, --remove-sample, or --keep-sample can be set");
      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-sample",
              "feh",
              "--keep-sample",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err),
          "Only one of --remove-samples, --remove-sample, or --keep-sample can be set");

      err =
          checkHandleFlagsErr(
              "-i",
              in.getPath(),
              "-o",
              new File(main, "newout.gz").getPath(),
              "--remove-format",
              "feh",
              "--keep-format",
              "blah");
      TestUtils.containsAll(
          TestUtils.unwrap(err), "Only one of --remove-format or --keep-format can be set");
    }
  }
Example #16
0
 @Override
 public void setUp() throws IOException {
   Diagnostic.setLogStream();
   mDir = FileHelper.createTempDirectory();
 }
 /**
  * Convenience method for turning a resource into a file that can be used as input.
  *
  * @param resource name (classpath) of the resource.
  * @param file a <code>File</code> to write to
  * @return a <code>File</code> containing the string content (same as <code>file</code>).
  * @throws IOException if an error occurs
  */
 public static File resourceToFile(final String resource, final File file) throws IOException {
   try (InputStream stream = Resources.getResourceAsStream(resource)) {
     return FileHelper.streamToFile(stream, file);
   }
 }
  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");
  }
 public void testValidator() throws IOException {
   Diagnostic.setLogStream();
   final MemoryPrintStream err = new MemoryPrintStream();
   final CliDiagnosticListener listener = new CliDiagnosticListener(err.printStream());
   Diagnostic.addListener(listener);
   final File tempDir = FileHelper.createTempDirectory();
   try {
     final CFlags flags =
         new CFlags("PhyloTest", TestUtils.getNullPrintStream(), err.printStream());
     SimilarityCli.initFlags(flags);
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", "ba", "-i", "humbug"},
         err,
         "Only set one of --input or --input-list-file");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-i", "humbug"},
         err,
         "The specified SDF, \"humbug\", does not exist.");
     final File fakePaired = new File(tempDir, "fakePaired");
     assertTrue(fakePaired.mkdir());
     final File left = new File(fakePaired, "left");
     assertTrue(left.mkdir());
     assertTrue(new File(fakePaired, "right").mkdir());
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-i", fakePaired.getPath()},
         err,
         "The specified SDF, \"" + fakePaired.getPath() + "\", is a paired end SDF.");
     checkErrorMessage(
         flags, new String[] {"-o", "blah"}, err, "Must set one of --input or --input-list-file");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", "ba"},
         err,
         "The specified list file, \"ba\", does not exist.");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", fakePaired.getPath()},
         err,
         "The specified list file,",
         "\"" + fakePaired.getPath() + "\",",
         "directory.");
     final File fakeList = new File(tempDir, "fakeList.txt");
     assertTrue(fakeList.createNewFile());
     checkErrorMessage(
         flags,
         new String[] {"-o", fakePaired.getPath(), "-I", fakeList.getPath()},
         err,
         "The directory",
         "\"" + fakePaired.getPath() + "\"",
         "already exists.");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", fakeList.getPath(), "-w", "-1"},
         err,
         "The specified flag \"--word\" has invalid value \"-1\". It should be greater than or equal to \"1\".");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", fakeList.getPath(), "-w", "0"},
         err,
         "The specified flag \"--word\" has invalid value \"0\". It should be greater than or equal to \"1\".");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", fakeList.getPath(), "-w", "33"},
         err,
         "The specified flag \"--word\" has invalid value \"33\". It should be less than or equal to \"32\".");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", fakeList.getPath(), "--max-reads", "0"},
         err,
         "The --max-reads must be greater than 0");
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-I", fakeList.getPath(), "-w", "20", "-s", "20"},
         err);
     checkErrorMessage(
         flags,
         new String[] {"-o", "blah", "-i", left.getPath(), "--max-reads", "1"},
         err,
         "Only set --max-reads when using --input-list-file");
     checkErrorMessage(flags, new String[] {"-o", "blah", "-i", left.getPath()}, err);
   } finally {
     Diagnostic.removeListener(listener);
     err.close();
     assertTrue(FileHelper.deleteAll(tempDir));
   }
 }