Example #1
0
 /**
  * Logs a message to the main log stream. All attached logTargets will also receive this message.
  * If the given log-level is higher than the given debug-level in the main config file, no logging
  * will be done.
  *
  * <p>The exception's stacktrace will be appended to the log-stream
  *
  * @param level log level of the message.
  * @param message text to be logged.
  * @param e the exception, which should be logged.
  */
 public void log(final int level, final Object message, final Exception e) {
   if (this.contextPrefix != null) {
     Log.getInstance().doLog(level, new Log.SimpleMessage(this.contextPrefix, ":", message), e);
   } else {
     Log.getInstance().doLog(level, message, e);
   }
 }
Example #2
0
 // Resets the configuration of this plugin.
 @Override
 public final void resetConfig() {
   Log.getInstance()
       .write(Log.LOGLEVEL_TRACE, "resetConfig: resetConfig received from Plugin Manager.");
   Log.getInstance().SetLogLevel(Log.LOGLEVEL_WARN);
   Configuration.SetServerProperty(PROPERTY_DEFAULT_MAX, DEFAULT_MAX_STRING);
   Configuration.SetServerProperty(PROPERTY_REDUCE_TO_MAX, "false");
   Configuration.SetServerProperty(PROPERTY_KEEP_OLDEST, "true");
   showInFocus = null;
 }
Example #3
0
  // Sets a configuration value for this plugin.
  @Override
  public void setConfigValue(String setting, String value) {
    Log.getInstance()
        .write(
            Log.LOGLEVEL_ALL,
            "PlugIn: setConfigValue received from Plugin Manager. Setting = "
                + setting
                + ":"
                + value);

    if (setting.startsWith(SETTING_LOGLEVEL)) {
      if (value.startsWith("None")) Log.getInstance().SetLogLevel(Log.LOGLEVEL_NONE);
      else if (value.startsWith("Error")) Log.getInstance().SetLogLevel(Log.LOGLEVEL_ERROR);
      else if (value.startsWith("Warn")) Log.getInstance().SetLogLevel(Log.LOGLEVEL_WARN);
      else if (value.startsWith("Trace")) Log.getInstance().SetLogLevel(Log.LOGLEVEL_TRACE);
      else if (value.startsWith("Verbose")) Log.getInstance().SetLogLevel(Log.LOGLEVEL_VERBOSE);
      else if (value.startsWith("Maximum")) Log.getInstance().SetLogLevel(Log.LOGLEVEL_ALL);
      else Log.getInstance().SetLogLevel(Log.LOGLEVEL_ERROR);
    } else if (setting.startsWith(SETTING_DEFAULT_MAX)) {
      if (value.equalsIgnoreCase("5309")) {
        System.out.println("LIR:: Deleting all user records.");
        UserRecordAPI.DeleteAllUserRecords(DataStore.STORE);
        showInFocus = null;
      } else Configuration.SetServerProperty(PROPERTY_DEFAULT_MAX, verifyMax(value));
    } else if (setting.startsWith(SETTING_REDUCE_TO_MAX)) {
      Configuration.SetServerProperty(PROPERTY_REDUCE_TO_MAX, value);
    } else if (setting.startsWith(SETTING_KEEP_OLDEST)) {
      Configuration.SetServerProperty(PROPERTY_KEEP_OLDEST, value);
    } else if (setting.startsWith(SETTING_PICK_SHOW)) {

      // The user just selected a show.  Put it in focus.
      showInFocus = Util.removeNumberMax(value);

    } else if (setting.startsWith(SETTING_HAVE_SHOW)) {

      // The user just selected a different show.  Put it in focus.
      showInFocus = Util.removeNumberMax(value);

    } else if (setting.startsWith(SETTING_SHOW_MAX)) {

      // The user just entered a new max for this show. If it's non null add it.
      if (value == null || value.trim().length() == 0) return;

      try {
        Integer.parseInt(value);
      } catch (NumberFormatException e) {
        return;
      }

      DataStore store = new DataStore(showInFocus);
      store.addRecord(showInFocus);
      store.setMax(verifyMax(value));
    } else if (setting.startsWith(SETTING_RESET_SHOW)) {

      // The user wants to reset this show so just delete the User Record.
      DataStore store = new DataStore(showInFocus);
      if (store.deleteRecord()) showInFocus = null;
      else Log.getInstance().write(Log.LOGLEVEL_WARN, "Plugin: Could not delete the User Record.");
    }
  }
Example #4
0
  // This method is called when the plugin should shutdown.
  @Override
  public void stop() {
    Log.getInstance().write(Log.LOGLEVEL_TRACE, "stop: Stop received from Plugin Manager.");

    if (Global.IsClient()) {
      Log.getInstance().write(Log.LOGLEVEL_WARN, "stop: Running in Client mode.");
      return;
    }

    // registry.eventUnsubscribe(listener, "RecordingCompleted");
    registry.eventUnsubscribe(listener, "RecordingStopped");
    showInFocus = null;
  }
Example #5
0
  // This method is called after plugin shutdown to free any resources
  // used by the plugin.
  @Override
  public void destroy() {
    if (Global.IsClient()) {
      Log.getInstance().write(Log.LOGLEVEL_WARN, "destroy: Running in Client mode.");
      Log.getInstance().destroy();
      return;
    }

    Log.getInstance().write(Log.LOGLEVEL_TRACE, "destroy: Destroy received from Plugin Manager.");
    Log.getInstance().destroy();
    registry = null;
    listener = null;
  }
 public String getTitle() {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: getTitle.");
   if (CurrentlyRecording == null) {
     return null;
   }
   return CurrentlyRecording.getRSSItemTitle();
 }
 /*
  * Constructor.
  */
 public DownloadThread() {
   RecordingMaps = new LinkedBlockingQueue<RecordingEpisode>();
   stop = false;
   AbortCurrent = false;
   CurrentlyRecording = null;
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: Constructor completed.");
 }
Example #8
0
  @Override
  public void start() {
    System.out.println("LIR: Plugin: Starting. Version = " + VERSION);

    // Set the loglevel to what's in the .properties file.
    Integer DefaultLevel = Log.LOGLEVEL_WARN;
    String CurrentLevel =
        Configuration.GetServerProperty(Log.PROPERTY_LOGLEVEL, DefaultLevel.toString());
    Integer SetLevel = Integer.decode(CurrentLevel);
    Log.getInstance().SetLogLevel(SetLevel);

    switch (Log.getInstance().GetLogLevel()) {
      case Log.LOGLEVEL_ALL:
        System.out.println("LIR: Plugin: LogLevel = Maximum.");
        break;
      case Log.LOGLEVEL_ERROR:
        System.out.println("LIR: Plugin: LogLevel = Error.");
        break;
      case Log.LOGLEVEL_NONE:
        System.out.println("LIR: Plugin: LogLevel = None.");
        break;
      case Log.LOGLEVEL_TRACE:
        System.out.println("LIR: Plugin: LogLevel = Trace.");
        break;
      case Log.LOGLEVEL_VERBOSE:
        System.out.println("LIR: Plugin: LogLevel = Verbose.");
        break;
      case Log.LOGLEVEL_WARN:
        System.out.println("LIR: Plugin: LogLevel = Warn.");
        break;
      default:
        System.out.println("LIR: Plugin: Error.  Unknown LogLevel.");
        break;
    }

    // If we're running on a client we are done.
    if (Global.IsClient()) {
      Log.getInstance().write(Log.LOGLEVEL_WARN, "start: Running in Client mode.");
      return;
    }

    // Subscribe to what we need.
    Log.getInstance().write(Log.LOGLEVEL_TRACE, "start: Subscribing to events.");
    // registry.eventSubscribe(listener, "RecordingCompleted");
    registry.eventSubscribe(listener, "RecordingStopped");
  }
 public Long getCurrentDownloadSize() {
   Log.getInstance().write(Log.LOGLEVEL_VERBOSE, "DT: getCurrentDownloadSize.");
   if (CurrentlyRecording == null) {
     return 0L;
   } else {
     return CurrentlyRecording.getBlocksRecorded() * RecordingEpisode.BLOCK_SIZE;
   }
 }
Example #10
0
 // Returns the current value of the specified setting for this plugin.
 @Override
 public String getConfigValue(String setting) {
   Log.getInstance()
       .write(
           Log.LOGLEVEL_ALL,
           "PlugIn: setConfigValue received from Plugin Manager. Setting = " + setting);
   if (setting.startsWith(SETTING_LOGLEVEL)) {
     switch (Log.getInstance().GetLogLevel()) {
       case Log.LOGLEVEL_ALL:
         return "Maximum";
       case Log.LOGLEVEL_ERROR:
         return "Error";
       case Log.LOGLEVEL_NONE:
         return "None";
       case Log.LOGLEVEL_TRACE:
         return "Trace";
       case Log.LOGLEVEL_VERBOSE:
         return "Verbose";
       case Log.LOGLEVEL_WARN:
         return "Warn";
       default:
         return "Unknown";
     }
   } else if (setting.startsWith(SETTING_DEFAULT_MAX)) {
     return Configuration.GetServerProperty(PROPERTY_DEFAULT_MAX, DEFAULT_MAX_STRING);
   } else if (setting.startsWith(SETTING_REDUCE_TO_MAX)) {
     return Configuration.GetServerProperty(PROPERTY_REDUCE_TO_MAX, "false");
   } else if (setting.startsWith(SETTING_KEEP_OLDEST)) {
     return Configuration.GetServerProperty(PROPERTY_KEEP_OLDEST, "true");
   } else if (setting.startsWith(SETTING_PICK_SHOW)) {
     return "Select";
   } else if (setting.startsWith(SETTING_HAVE_SHOW)) {
     return showInFocus;
   } else if (setting.startsWith(SETTING_SHOW_MAX)) {
     DataStore store = new DataStore(showInFocus);
     return (store.isMonitored() ? store.getMaxString() : "");
   } else if (setting.startsWith(SETTING_RESET_SHOW)) {
     return "Reset Now";
   } else return null;
 }
Example #11
0
 // Returns one of the constants above that indicates what type of value
 // is used for a specific settings.
 @Override
 public int getConfigType(String setting) {
   Log.getInstance()
       .write(
           Log.LOGLEVEL_ALL,
           "PlugIn: getConfigType received from Plugin Manager. Setting = " + setting);
   if (setting.startsWith(SETTING_LOGLEVEL)) return CONFIG_CHOICE;
   else if (setting.startsWith(SETTING_DEFAULT_MAX)) return CONFIG_TEXT;
   else if (setting.startsWith(SETTING_REDUCE_TO_MAX)) return CONFIG_BOOL;
   else if (setting.startsWith(SETTING_KEEP_OLDEST)) return CONFIG_BOOL;
   else if (setting.startsWith(SETTING_PICK_SHOW)) return CONFIG_CHOICE;
   else if (setting.startsWith(SETTING_HAVE_SHOW)) return CONFIG_CHOICE;
   else if (setting.startsWith(SETTING_SHOW_MAX)) return CONFIG_TEXT;
   else if (setting.startsWith(SETTING_RESET_SHOW)) return CONFIG_BUTTON;
   else return 0;
 }
Example #12
0
  // For CONFIG_CHOICE settings; this returns the list of choices.
  @Override
  public String[] getConfigOptions(String setting) {
    Log.getInstance()
        .write(
            Log.LOGLEVEL_ALL,
            "PlugIn: getConfigOptions received from Plugin Manager. Setting = " + setting);
    if (setting.startsWith(SETTING_LOGLEVEL)) {
      String[] values = {"None", "Error", "Warn", "Trace", "Verbose", "Maximum"};
      return values;
    } else if (setting.startsWith(SETTING_PICK_SHOW) || setting.startsWith(SETTING_HAVE_SHOW)) {

      // The user wants to select a show to put in focus.  The options are all
      // shows that are intelligent recordings.
      return Util.getAllIntelligentRecordingTitlesAndMax();
    } else {
      return null;
    }
  }
  /**
   * O(n^2) N - total length of collection M - length of mark (M<N) S - size of collection (S<N) n -
   * average length of collection member (eq N/S) m - estimated number of marks in collection (m<N)
   */
  @Override
  public void findAndReplace(
      List<String> collection, String mark, String newValue) { // O(MNmnS) = O(MNNm)
    if (newValue.contains(mark)) return; // O(M)

    String fullString = glueCollection(collection, ""); // O(S)
    int position = fullString.indexOf(mark); // O(NM)

    for (; -1 != position; ) { // O(m)
      int[] border = formBorders(collection, ""); // // O(S) left border
      int foundPosIndex = -1;
      int foundEndIndex = -1;
      for (int i = 0; i < collection.size(); i++) { // O(N)
        if (-1 == foundPosIndex && border[i] <= position && border[i + 1] > position) {
          foundPosIndex = i;
        }

        if (-1 == foundEndIndex
            && border[i] <= (position + mark.length() - 1)
            && border[i + 1] > (position + mark.length() - 1)) {
          foundEndIndex = i;
        }
        if (-1 != foundPosIndex && -1 != foundEndIndex) {
          break;
        }
      }
      for (int i = foundPosIndex; i <= foundEndIndex; i++) { // O(n)

        String prefix = collection.get(i).substring(0, Math.max(0, position - border[i]));
        String modifiedString = prefix.concat(i == foundPosIndex ? newValue : "");
        int endIndex = Math.min(position + mark.length() - border[i], (border[i + 1] - border[i]));
        String suffix = collection.get(i).substring(endIndex);
        modifiedString = modifiedString.concat(suffix);
        collection.set(i, modifiedString);
      }

      fullString = glueCollection(collection, ""); // O(S)
      position = fullString.indexOf(mark); // O(NM)
    }
    Log.getInstance().debug("fullString " + glueCollection(collection, "|"));
  }
Example #14
0
  // Returns the label used to present this setting to the user.
  @Override
  public String getConfigLabel(String setting) {

    if (setting.startsWith(SETTING_LOGLEVEL)) {
      return "Debug Logging Level";
    } else if (setting.startsWith(SETTING_DEFAULT_MAX)) {
      return "Global Maximum to Keep";
    } else if (setting.startsWith(SETTING_REDUCE_TO_MAX)) {
      return "Delete Excess Recordings";
    } else if (setting.startsWith(SETTING_KEEP_OLDEST)) {
      return "Keep the Oldest Recordings";
    } else if (setting.startsWith(SETTING_PICK_SHOW)) {
      return "Choose a Show";
    } else if (setting.startsWith(SETTING_HAVE_SHOW)) {
      return "Limit This Show";
    } else if (setting.startsWith(SETTING_SHOW_MAX)) {
      return "Max to Keep for This Show";
    } else if (setting.startsWith(SETTING_RESET_SHOW)) {
      return "Use Default for This Show";
    } else {
      Log.getInstance().write(Log.LOGLEVEL_WARN, "getConfigLabel: Unknown setting = " + setting);
      return null;
    }
  }
Example #15
0
 public void removeItem(RecordingEpisode episode) {
   if (!RecordingMaps.remove(episode)) {
     Log.getInstance()
         .write(Log.LOGLEVEL_ERROR, "DT: Failed to remove episode from RecordingMaps.");
   }
 }
Example #16
0
 public void setAbortCurrent(boolean state) {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: setAbortCurrent.");
   AbortCurrent = state;
 }
Example #17
0
 /**
  * Set the "Stop" flag to to value specified.
  *
  * <p>
  *
  * @param state The new value of the Stop flag.
  */
 public void setStop(boolean state) {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: setStop.");
   stop = state;
   if (stop) this.interrupt();
 }
Example #18
0
 public void abortCurrentDownload() {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: Aborting current download.");
   if (CurrentlyRecording != null) CurrentlyRecording.abortCurrentDownload();
 }
Example #19
0
public class SamReaderFactoryTest {
  private static final File TEST_DATA_DIR = new File("src/test/resources/htsjdk/samtools");

  private static final Log LOG = Log.getInstance(SamReaderFactoryTest.class);

  @Test(dataProvider = "variousFormatReaderTestCases")
  public void variousFormatReaderTest(final String inputFile) throws IOException {
    final File input = new File(TEST_DATA_DIR, inputFile);
    final SamReader reader = SamReaderFactory.makeDefault().open(input);
    for (final SAMRecord ignored : reader) {}
    reader.close();
  }

  private int countRecordsInQueryInterval(final SamReader reader, final QueryInterval query) {
    final SAMRecordIterator iter = reader.queryOverlapping(new QueryInterval[] {query});
    int count = 0;
    while (iter.hasNext()) {
      iter.next();
      count++;
    }
    iter.close();
    return count;
  }

  private int countRecords(final SamReader reader) {
    int count = 0;
    try (final SAMRecordIterator iter = reader.iterator()) {
      while (iter.hasNext()) {
        iter.next();
        count++;
      }
    }
    return count;
  }

  private static SeekableByteChannel addHeader(SeekableByteChannel input) {
    try {
      int total = (int) input.size();
      final String comment =
          "@HD\tVN:1.0  SO:unsorted\n"
              + "@SQ\tSN:chr1\tLN:101\n"
              + "@SQ\tSN:chr2\tLN:101\n"
              + "@SQ\tSN:chr3\tLN:101\n"
              + "@RG\tID:0\tSM:JP was here\n";

      byte[] commentBuf = comment.getBytes();
      ByteBuffer buf = ByteBuffer.allocate(total + commentBuf.length);
      buf.put(commentBuf);
      input.position(0);
      while (input.read(buf) > 0) {
        // read until EOF
      }
      buf.flip();
      return new SeekableByteChannelFromBuffer(buf);
    } catch (IOException x) {
      throw new RuntimeException(x);
    }
  }

  @Test
  public void testWrap() throws IOException {
    final Path input = Paths.get(TEST_DATA_DIR.getPath(), "noheader.sam");
    final SamReader wrappedReader =
        SamReaderFactory.makeDefault().setPathWrapper(SamReaderFactoryTest::addHeader).open(input);
    int records = countRecords(wrappedReader);
    Assert.assertEquals(10, records);
  }

  // See https://github.com/samtools/htsjdk/issues/76
  @Test(dataProvider = "queryIntervalIssue76TestCases")
  public void queryIntervalIssue76(
      final String sequenceName, final int start, final int end, final int expectedCount)
      throws IOException {
    final File input = new File(TEST_DATA_DIR, "issue76.bam");
    final SamReader reader = SamReaderFactory.makeDefault().open(input);
    final QueryInterval interval =
        new QueryInterval(
            reader.getFileHeader().getSequence(sequenceName).getSequenceIndex(), start, end);
    Assert.assertEquals(countRecordsInQueryInterval(reader, interval), expectedCount);
    reader.close();
  }

  @DataProvider(name = "queryIntervalIssue76TestCases")
  public Object[][] queryIntervalIssue76TestCases() {
    return new Object[][] {
      {"1", 11966, 11966, 2},
      {"1", 11966, 11967, 2},
      {"1", 11967, 11967, 1}
    };
  }

  @DataProvider(name = "variousFormatReaderTestCases")
  public Object[][] variousFormatReaderTestCases() {
    return new Object[][] {
      {"block_compressed.sam.gz"},
      {"uncompressed.sam"},
      {"compressed.sam.gz"},
      {"compressed.bam"},
      {"unsorted.sam"}
    };
  }

  // Tests for the SAMRecordFactory usage
  class SAMRecordFactoryTester extends DefaultSAMRecordFactory {
    int samRecordsCreated;
    int bamRecordsCreated;

    public SAMRecord createSAMRecord(final SAMFileHeader header) {
      ++samRecordsCreated;
      return super.createSAMRecord(header);
    }

    public BAMRecord createBAMRecord(
        final SAMFileHeader header,
        final int referenceSequenceIndex,
        final int alignmentStart,
        final short readNameLength,
        final short mappingQuality,
        final int indexingBin,
        final int cigarLen,
        final int flags,
        final int readLen,
        final int mateReferenceSequenceIndex,
        final int mateAlignmentStart,
        final int insertSize,
        final byte[] variableLengthBlock) {
      ++bamRecordsCreated;
      return super.createBAMRecord(
          header,
          referenceSequenceIndex,
          alignmentStart,
          readNameLength,
          mappingQuality,
          indexingBin,
          cigarLen,
          flags,
          readLen,
          mateReferenceSequenceIndex,
          mateAlignmentStart,
          insertSize,
          variableLengthBlock);
    }
  }

  @Test(dataProvider = "variousFormatReaderTestCases")
  public void samRecordFactoryTest(final String inputFile) throws IOException {
    final File input = new File(TEST_DATA_DIR, inputFile);

    final SAMRecordFactoryTester recordFactory = new SAMRecordFactoryTester();
    final SamReaderFactory readerFactory =
        SamReaderFactory.makeDefault().samRecordFactory(recordFactory);
    final SamReader reader = readerFactory.open(input);

    int i = 0;
    for (final SAMRecord ignored : reader) {
      ++i;
    }
    reader.close();

    Assert.assertTrue(i > 0);
    if (inputFile.endsWith(".sam") || inputFile.endsWith(".sam.gz"))
      Assert.assertEquals(recordFactory.samRecordsCreated, i);
    else if (inputFile.endsWith(".bam")) Assert.assertEquals(recordFactory.bamRecordsCreated, i);
  }

  @Test(expectedExceptions = IllegalStateException.class)
  public void samRecordFactoryNullHeaderBAMTest() {
    final SAMRecordFactory recordFactory = new DefaultSAMRecordFactory();
    recordFactory.createBAMRecord(
        null, // null header
        0, 0, (short) 0, (short) 0, 0, 0, 0, 0, 0, 0, 0, null);
  }

  /**
   * Unit tests for asserting all permutations of data and index sources read the same records and
   * header.
   */
  final File localBam =
      new File("src/test/resources/htsjdk/samtools/BAMFileIndexTest/index_test.bam");

  final File localBamIndex =
      new File("src/test/resources/htsjdk/samtools/BAMFileIndexTest/index_test.bam.bai");

  final URL bamUrl, bamIndexUrl;

  {
    try {
      bamUrl = new URL(TestUtil.BASE_URL_FOR_HTTP_TESTS + "index_test.bam");
      bamIndexUrl = new URL(TestUtil.BASE_URL_FOR_HTTP_TESTS + "index_test.bam.bai");
    } catch (final MalformedURLException e) {
      throw new RuntimeException(e);
    }
  }

  @DataProvider
  public Object[][] composeAllPermutationsOfSamInputResource() {
    final List<SamInputResource> sources = new ArrayList<SamInputResource>();
    for (final InputResource.Type dataType : InputResource.Type.values()) {
      if (dataType.equals(InputResource.Type.SRA_ACCESSION)) continue;

      sources.add(new SamInputResource(composeInputResourceForType(dataType, false)));
      for (final InputResource.Type indexType : InputResource.Type.values()) {
        if (indexType.equals(InputResource.Type.SRA_ACCESSION)) continue;

        sources.add(
            new SamInputResource(
                composeInputResourceForType(dataType, false),
                composeInputResourceForType(indexType, true)));
      }
    }
    final Object[][] data = new Object[sources.size()][];
    for (final SamInputResource source : sources) {
      data[sources.indexOf(source)] = new Object[] {source};
    }

    return data;
  }

  private InputResource composeInputResourceForType(
      final InputResource.Type type, final boolean forIndex) {
    final File f = forIndex ? localBamIndex : localBam;
    final URL url = forIndex ? bamIndexUrl : bamUrl;
    switch (type) {
      case FILE:
        return new FileInputResource(f);
      case PATH:
        return new PathInputResource(f.toPath(), Function.identity());
      case URL:
        return new UrlInputResource(url);
      case SEEKABLE_STREAM:
        return new SeekableStreamInputResource(new SeekableHTTPStream(url));
      case INPUT_STREAM:
        try {
          return new InputStreamInputResource(new FileInputStream(f));
        } catch (final FileNotFoundException e) {
          throw new RuntimeIOException(e);
        }
      default:
        throw new IllegalStateException();
    }
  }

  final Set<SAMFileHeader> observedHeaders = new HashSet<SAMFileHeader>();
  final Set<List<SAMRecord>> observedRecordOrdering = new HashSet<List<SAMRecord>>();

  @Test(dataProvider = "composeAllPermutationsOfSamInputResource")
  public void exhaustInputResourcePermutation(final SamInputResource resource) throws IOException {
    final SamReader reader = SamReaderFactory.makeDefault().open(resource);
    LOG.info(String.format("Reading from %s ...", resource));
    final List<SAMRecord> slurped = Iterables.slurp(reader);
    final SAMFileHeader fileHeader = reader.getFileHeader();
    reader.hasIndex();
    reader.indexing().hasBrowseableIndex();
    reader.close();

    /* Ensure all tests have read the same records in the same order or, if this is the first test, set it as the template. */
    observedHeaders.add(fileHeader);
    observedRecordOrdering.add(slurped);
    Assert.assertEquals(observedHeaders.size(), 1, "read different headers than other testcases");
    Assert.assertEquals(
        observedRecordOrdering.size(), 1, "read different records than other testcases");
  }

  @Test
  public void openPath() throws IOException {
    final Path path = localBam.toPath();
    final List<SAMRecord> records;
    final SAMFileHeader fileHeader;
    try (final SamReader reader = SamReaderFactory.makeDefault().open(path)) {
      LOG.info(String.format("Reading from %s ...", path));
      records = Iterables.slurp(reader);
      fileHeader = reader.getFileHeader();
      reader.close();
    }

    try (final SamReader fileReader = SamReaderFactory.makeDefault().open(localBam)) {
      final List<SAMRecord> expectedRecords = Iterables.slurp(fileReader);
      final SAMFileHeader expectedFileHeader = fileReader.getFileHeader();
      Assert.assertEquals(records, expectedRecords);
      Assert.assertEquals(fileHeader, expectedFileHeader);
    }
  }

  final Set<List<SAMRecord>> observedRecordOrdering1 = new HashSet<List<SAMRecord>>();
  final Set<List<SAMRecord>> observedRecordOrdering3 = new HashSet<List<SAMRecord>>();
  final Set<List<SAMRecord>> observedRecordOrdering20 = new HashSet<List<SAMRecord>>();

  @Test(dataProvider = "composeAllPermutationsOfSamInputResource")
  public void queryInputResourcePermutation(final SamInputResource resource) throws IOException {
    final SamReader reader = SamReaderFactory.makeDefault().open(resource);
    LOG.info(String.format("Query from %s ...", resource));
    if (reader.hasIndex()) {
      final StopWatch stopWatch = new StopWatch();
      stopWatch.start();
      final SAMRecordIterator q1 = reader.query("chr1", 500000, 100000000, true);
      observedRecordOrdering1.add(Iterables.slurp(q1));
      q1.close();
      final SAMRecordIterator q20 = reader.query("chr20", 1, 1000000, true);
      observedRecordOrdering20.add(Iterables.slurp(q20));
      q20.close();
      final SAMRecordIterator q3 = reader.query("chr3", 1, 10000000, true);
      observedRecordOrdering3.add(Iterables.slurp(q3));
      q3.close();
      stopWatch.stop();
      LOG.info(String.format("Finished queries in %sms", stopWatch.getElapsedTime()));

      Assert.assertEquals(
          observedRecordOrdering1.size(), 1, "read different records for chromosome 1");
      Assert.assertEquals(
          observedRecordOrdering20.size(), 1, "read different records for chromosome 20");
      Assert.assertEquals(
          observedRecordOrdering3.size(), 1, "read different records for chromosome 3");
    } else if (resource.indexMaybe() != null) {
      LOG.warn("Resource has an index source, but is not indexed: " + resource);
    } else {
      LOG.info("Skipping query operation: no index.");
    }
    reader.close();
  }

  /**
   * A path that pretends it's not based upon a file. This helps in cases where we want to test
   * branches that apply to non-file based paths without actually having to use non-file based
   * resources (like cloud urls)
   */
  private static class NeverFilePathInputResource extends PathInputResource {
    public NeverFilePathInputResource(Path pathResource) {
      super(pathResource);
    }

    @Override
    public File asFile() {
      return null;
    }
  }

  @Test
  public void checkHasIndexForStreamingPathBamWithFileIndex() throws IOException {
    InputResource bam = new NeverFilePathInputResource(localBam.toPath());
    InputResource index = new FileInputResource(localBamIndex);

    // ensure that the index is being used, not checked in queryInputResourcePermutation
    try (final SamReader reader =
        SamReaderFactory.makeDefault().open(new SamInputResource(bam, index))) {
      Assert.assertTrue(reader.hasIndex());
    }
  }

  @Test
  public void queryStreamingPathBamWithFileIndex() throws IOException {
    InputResource bam = new NeverFilePathInputResource(localBam.toPath());
    InputResource index = new FileInputResource(localBamIndex);

    final SamInputResource resource = new SamInputResource(bam, index);
    queryInputResourcePermutation(new SamInputResource(bam, index));
  }

  @Test
  public void customReaderFactoryTest() throws IOException {
    try {
      CustomReaderFactory.setInstance(
          new CustomReaderFactory(
              "https://www.googleapis.com/genomics/v1beta/reads/,"
                  + "htsjdk.samtools.SamReaderFactoryTest$TestReaderFactory"));
      final SamReader reader =
          SamReaderFactory.makeDefault()
              .open(
                  SamInputResource.of(
                      "https://www.googleapis.com/genomics/v1beta/reads/?uncompressed.sam"));
      int i = 0;
      for (@SuppressWarnings("unused") final SAMRecord ignored : reader) {
        ++i;
      }
      reader.close();

      Assert.assertTrue(i > 0);
    } finally {
      CustomReaderFactory.resetToDefaultInstance();
    }
  }

  public static class TestReaderFactory implements CustomReaderFactory.ICustomReaderFactory {
    @Override
    public SamReader open(URL url) {
      final File file = new File(TEST_DATA_DIR, url.getQuery());
      LOG.info("Opening customr reader for " + file.toString());
      return SamReaderFactory.makeDefault().open(file);
    }
  }

  @Test
  public void inputResourceFromStringTest() throws IOException {
    Assert.assertEquals(
        SamInputResource.of("http://test.url").data().type(), InputResource.Type.URL);
    Assert.assertEquals(
        SamInputResource.of("https://test.url").data().type(), InputResource.Type.URL);
    Assert.assertEquals(
        SamInputResource.of("ftp://test.url").data().type(), InputResource.Type.URL);
    Assert.assertEquals(SamInputResource.of("/a/b/c").data().type(), InputResource.Type.FILE);
  }

  @Test
  public void testCRAMReaderFromURL() throws IOException {
    // get a CRAM reader with an index from a URL-backed resource
    getCRAMReaderFromInputResource(
        (cramURL, indexURL) -> {
          return SamInputResource.of(cramURL).index(indexURL);
        },
        true,
        3);
  }

  @Test
  public void testCRAMReaderFromURLStream() throws IOException {
    // get a CRAM reader with an index from a stream-backed resource created from a URL
    getCRAMReaderFromInputResource(
        (cramURL, indexURL) -> {
          try {
            ISeekableStreamFactory streamFactory = SeekableStreamFactory.getInstance();
            return SamInputResource.of(streamFactory.getStreamFor(cramURL))
                .index(streamFactory.getStreamFor(indexURL));
          } catch (IOException e) {
            throw new RuntimeIOException(e);
          }
        },
        true,
        3);
  }

  @Test
  public void testCRAMReaderFromURLNoIndexFile() throws IOException {
    // get just a CRAM reader (no index) from an URL-backed resource
    getCRAMReaderFromInputResource(
        (cramURL, indexURL) -> {
          return SamInputResource.of(cramURL);
        },
        false,
        11);
  }

  @Test(expectedExceptions = RuntimeIOException.class)
  public void testCRAMReaderFromURLBadIndexFile() throws IOException {
    // deliberately specify a bad index file to ensure we get an IOException
    getCRAMReaderFromInputResource(
        (cramURL, indexURL) -> {
          return SamInputResource.of(cramURL).index(new File("nonexistent.bai"));
        },
        true,
        3);
  }

  private void getCRAMReaderFromInputResource(
      final BiFunction<URL, URL, SamInputResource> getInputResource,
      final boolean hasIndex,
      final int expectedCount)
      throws IOException {
    final String cramFilePath =
        new File(TEST_DATA_DIR, "cram_with_bai_index.cram").getAbsolutePath();
    final String cramIndexPath =
        new File(TEST_DATA_DIR, "cram_with_bai_index.cram.bai").getAbsolutePath();
    final URL cramURL = new URL("file://" + cramFilePath);
    final URL indexURL = new URL("file://" + cramIndexPath);

    final SamReaderFactory factory =
        SamReaderFactory.makeDefault()
            .referenceSource(new ReferenceSource(new File(TEST_DATA_DIR, "hg19mini.fasta")))
            .validationStringency(ValidationStringency.SILENT);
    final SamReader reader = factory.open(getInputResource.apply(cramURL, indexURL));

    int count =
        hasIndex
            ? countRecordsInQueryInterval(reader, new QueryInterval(1, 10, 1000))
            : countRecords(reader);
    Assert.assertEquals(count, expectedCount);
  }

  @Test
  public void testSamReaderFromSeekableStream() throws IOException {
    // even though a SAM isn't indexable, make sure we can open one
    // using a seekable stream
    final File samFile = new File(TEST_DATA_DIR, "unsorted.sam");
    final SamReaderFactory factory =
        SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT);
    final SamReader reader = factory.open(SamInputResource.of(new SeekableFileStream(samFile)));
    Assert.assertEquals(countRecords(reader), 10);
  }

  @Test
  public void testSamReaderFromURL() throws IOException {
    final String samFilePath = new File(TEST_DATA_DIR, "unsorted.sam").getAbsolutePath();
    final URL samURL = new URL("file://" + samFilePath);
    final SamReaderFactory factory =
        SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT);
    final SamReader reader = factory.open(SamInputResource.of(samURL));
    Assert.assertEquals(countRecords(reader), 10);
  }

  @Test(expectedExceptions = SAMFormatException.class)
  public void testSamReaderFromMalformedSeekableStream() throws IOException {
    // use a bogus (.bai file) to force SamReaderFactory to fall through to the
    // fallback code that assumes a SAM File when it can't determine the
    // format of the input, to ensure that it results in a SAMFormatException
    final File samFile = new File(TEST_DATA_DIR, "cram_with_bai_index.cram.bai");
    final SamReaderFactory factory =
        SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT);
    final SamReader reader = factory.open(SamInputResource.of(new SeekableFileStream(samFile)));
    countRecords(reader);
  }
}
Example #20
0
  /** The main thread that does all of the downloading. */
  @Override
  public void run() {
    Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: Starting.");

    Thread.currentThread().setName("DownloadThread");

    while (!stop) {

      // Get the first item in the queue and then remove it from the queue.
      try {
        CurrentlyRecording = RecordingMaps.take();
      } catch (InterruptedException e) {
        Log.getInstance().write(Log.LOGLEVEL_WARN, "DT: Interrupted.  Terminating.");
        return;
      }

      Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: Have work to do.");

      showCurrentlyRecording(CurrentlyRecording);

      // Make sure we have enough parameters.
      if (!CurrentlyRecording.isComplete()) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: Not enough parameters.");
        CurrentlyRecording.failed();
        continue;
      }

      DownloadManager.getInstance().setCurrentlyRecordingID(CurrentlyRecording.getRequestID());

      // Get all of the RSSItems for the Feed Context.
      List<RSSItem> RSSItems = CurrentlyRecording.getRSSItems();
      if (RSSItems == null) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: null RSSItems.");
        CurrentlyRecording.failed();
        continue;
      }

      Log.getInstance()
          .write(Log.LOGLEVEL_TRACE, "DT: Found episodes for podcast = " + RSSItems.size());

      if (RSSItems.isEmpty()) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: No RSSItems.");
        CurrentlyRecording.failed();
        continue;
      }

      // Get the one ChanItem (RSSItem) we are interested in.
      RSSItem ChanItem =
          CurrentlyRecording.getItemForID(RSSItems, CurrentlyRecording.getEpisodeID());
      if (ChanItem == null) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: null ChanItem.");
        CurrentlyRecording.failed();
        continue;
      }

      // Set the ChanItem.
      CurrentlyRecording.setChanItem(ChanItem);

      // Set the fileExt instance variable.
      CurrentlyRecording.setFileExt();

      // Create the tempfile where the episode will be downloaded to.
      if (!CurrentlyRecording.setTempFile()) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: Failed to setTempFile.");
        CurrentlyRecording.failed();
        continue;
      }

      // Download the episode to the tempfile.
      if (!CurrentlyRecording.download()) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: download failed.");
        CurrentlyRecording.failed();
        continue;
      }

      // Check for 0 size download.
      if (CurrentlyRecording.isZeroSizeDownload()) {
        Log.getInstance().write(Log.LOGLEVEL_WARN, "DT: File is 0 bytes long.");
        CurrentlyRecording.failed();
        continue;
      }

      // Move the tempfile to the final location and rename it to the final name.
      if (!CurrentlyRecording.moveToFinalLocation()) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: moveToFinalLocation failed.");
        CurrentlyRecording.failed();
        continue;
      }

      // Import the episode into the Sage database as an imported media file.
      if (CurrentlyRecording.importAsAiring() == null) {
        Log.getInstance().write(Log.LOGLEVEL_ERROR, "DT: importAsMediaFile failed.");
        CurrentlyRecording.failed();
        continue;
      }

      // Force Episode to update it's Airing information.
      int AiringID = CurrentlyRecording.getAiringID();

      // It worked.
      Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: Completed successfully.");
      CurrentlyRecording.completed();
      CurrentlyRecording = null;
    } // While !stop

    Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: Fatal error. Ending.");
  } // Run
Example #21
0
/**
 * Computes a number of metrics that are useful for evaluating coverage and performance of whole
 * genome sequencing experiments.
 *
 * @author tfennell
 */
@CommandLineProgramProperties(
    usage =
        "Computes a number of metrics that are useful for evaluating coverage and performance of "
            + "whole genome sequencing experiments.",
    usageShort = "Writes whole genome sequencing-related metrics for a SAM or BAM file",
    programGroup = Metrics.class)
public class CollectWgsMetrics extends CommandLineProgram {

  @Option(shortName = StandardOptionDefinitions.INPUT_SHORT_NAME, doc = "Input SAM or BAM file.")
  public File INPUT;

  @Option(shortName = StandardOptionDefinitions.OUTPUT_SHORT_NAME, doc = "Output metrics file.")
  public File OUTPUT;

  @Option(
      shortName = StandardOptionDefinitions.REFERENCE_SHORT_NAME,
      doc = "The reference sequence fasta aligned to.")
  public File REFERENCE_SEQUENCE;

  @Option(
      shortName = "MQ",
      doc = "Minimum mapping quality for a read to contribute coverage.",
      overridable = true)
  public int MINIMUM_MAPPING_QUALITY = 20;

  @Option(
      shortName = "Q",
      doc = "Minimum base quality for a base to contribute coverage.",
      overridable = true)
  public int MINIMUM_BASE_QUALITY = 20;

  @Option(
      shortName = "CAP",
      doc = "Treat bases with coverage exceeding this value as if they had coverage at this value.",
      overridable = true)
  public int COVERAGE_CAP = 250;

  @Option(doc = "For debugging purposes, stop after processing this many genomic bases.")
  public long STOP_AFTER = -1;

  @Option(doc = "Determines whether to include the base quality histogram in the metrics file.")
  public boolean INCLUDE_BQ_HISTOGRAM = false;

  @Option(doc = "If true, count unpaired reads, and paired reads with one end unmapped")
  public boolean COUNT_UNPAIRED = false;

  private final Log log = Log.getInstance(CollectWgsMetrics.class);

  /** Metrics for evaluating the performance of whole genome sequencing experiments. */
  public static class WgsMetrics extends MetricBase {
    /** The number of non-N bases in the genome reference over which coverage will be evaluated. */
    public long GENOME_TERRITORY;
    /** The mean coverage in bases of the genome territory, after all filters are applied. */
    public double MEAN_COVERAGE;
    /** The standard deviation of coverage of the genome after all filters are applied. */
    public double SD_COVERAGE;
    /** The median coverage in bases of the genome territory, after all filters are applied. */
    public double MEDIAN_COVERAGE;
    /** The median absolute deviation of coverage of the genome after all filters are applied. */
    public double MAD_COVERAGE;

    /**
     * The fraction of aligned bases that were filtered out because they were in reads with low
     * mapping quality (default is < 20).
     */
    public double PCT_EXC_MAPQ;
    /**
     * The fraction of aligned bases that were filtered out because they were in reads marked as
     * duplicates.
     */
    public double PCT_EXC_DUPE;
    /**
     * The fraction of aligned bases that were filtered out because they were in reads without a
     * mapped mate pair.
     */
    public double PCT_EXC_UNPAIRED;
    /**
     * The fraction of aligned bases that were filtered out because they were of low base quality
     * (default is < 20).
     */
    public double PCT_EXC_BASEQ;
    /**
     * The fraction of aligned bases that were filtered out because they were the second observation
     * from an insert with overlapping reads.
     */
    public double PCT_EXC_OVERLAP;
    /**
     * The fraction of aligned bases that were filtered out because they would have raised coverage
     * above the capped value (default cap = 250x).
     */
    public double PCT_EXC_CAPPED;
    /** The total fraction of aligned bases excluded due to all filters. */
    public double PCT_EXC_TOTAL;

    /**
     * The fraction of bases that attained at least 1X sequence coverage in post-filtering bases.
     */
    public double PCT_1X;
    /**
     * The fraction of bases that attained at least 5X sequence coverage in post-filtering bases.
     */
    public double PCT_5X;
    /**
     * The fraction of bases that attained at least 10X sequence coverage in post-filtering bases.
     */
    public double PCT_10X;
    /**
     * The fraction of bases that attained at least 15X sequence coverage in post-filtering bases.
     */
    public double PCT_15X;
    /**
     * The fraction of bases that attained at least 20X sequence coverage in post-filtering bases.
     */
    public double PCT_20X;
    /**
     * The fraction of bases that attained at least 25X sequence coverage in post-filtering bases.
     */
    public double PCT_25X;
    /**
     * The fraction of bases that attained at least 30X sequence coverage in post-filtering bases.
     */
    public double PCT_30X;
    /**
     * The fraction of bases that attained at least 40X sequence coverage in post-filtering bases.
     */
    public double PCT_40X;
    /**
     * The fraction of bases that attained at least 50X sequence coverage in post-filtering bases.
     */
    public double PCT_50X;
    /**
     * The fraction of bases that attained at least 60X sequence coverage in post-filtering bases.
     */
    public double PCT_60X;
    /**
     * The fraction of bases that attained at least 70X sequence coverage in post-filtering bases.
     */
    public double PCT_70X;
    /**
     * The fraction of bases that attained at least 80X sequence coverage in post-filtering bases.
     */
    public double PCT_80X;
    /**
     * The fraction of bases that attained at least 90X sequence coverage in post-filtering bases.
     */
    public double PCT_90X;
    /**
     * The fraction of bases that attained at least 100X sequence coverage in post-filtering bases.
     */
    public double PCT_100X;
  }

  public static void main(final String[] args) {
    new CollectWgsMetrics().instanceMainWithExit(args);
  }

  @Override
  protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(OUTPUT);
    IOUtil.assertFileIsReadable(REFERENCE_SEQUENCE);

    // Setup all the inputs
    final ProgressLogger progress = new ProgressLogger(log, 10000000, "Processed", "loci");
    final ReferenceSequenceFileWalker refWalker =
        new ReferenceSequenceFileWalker(REFERENCE_SEQUENCE);
    final SamReader in =
        SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    final SamLocusIterator iterator = getLocusIterator(in);

    final List<SamRecordFilter> filters = new ArrayList<SamRecordFilter>();
    final CountingFilter dupeFilter = new CountingDuplicateFilter();
    final CountingFilter mapqFilter = new CountingMapQFilter(MINIMUM_MAPPING_QUALITY);
    final CountingPairedFilter pairFilter = new CountingPairedFilter();
    filters.add(mapqFilter);
    filters.add(dupeFilter);
    if (!COUNT_UNPAIRED) {
      filters.add(pairFilter);
    }
    filters.add(
        new SecondaryAlignmentFilter()); // Not a counting filter because we never want to count
                                         // reads twice
    iterator.setSamFilters(filters);
    iterator.setEmitUncoveredLoci(true);
    iterator.setMappingQualityScoreCutoff(0); // Handled separately because we want to count bases
    iterator.setQualityScoreCutoff(0); // Handled separately because we want to count bases
    iterator.setIncludeNonPfReads(false);

    final int max = COVERAGE_CAP;
    final long[] HistogramArray = new long[max + 1];
    final long[] baseQHistogramArray = new long[Byte.MAX_VALUE];
    final boolean usingStopAfter = STOP_AFTER > 0;
    final long stopAfter = STOP_AFTER - 1;
    long counter = 0;

    long basesExcludedByBaseq = 0;
    long basesExcludedByOverlap = 0;
    long basesExcludedByCapping = 0;

    // Loop through all the loci
    while (iterator.hasNext()) {
      final SamLocusIterator.LocusInfo info = iterator.next();

      // Check that the reference is not N
      final ReferenceSequence ref = refWalker.get(info.getSequenceIndex());
      final byte base = ref.getBases()[info.getPosition() - 1];
      if (base == 'N') continue;

      // Figure out the coverage while not counting overlapping reads twice, and excluding various
      // things
      final HashSet<String> readNames = new HashSet<String>(info.getRecordAndPositions().size());
      int pileupSize = 0;
      for (final SamLocusIterator.RecordAndOffset recs : info.getRecordAndPositions()) {

        if (recs.getBaseQuality() < MINIMUM_BASE_QUALITY) {
          ++basesExcludedByBaseq;
          continue;
        }
        if (!readNames.add(recs.getRecord().getReadName())) {
          ++basesExcludedByOverlap;
          continue;
        }
        pileupSize++;
        if (pileupSize <= max) {
          baseQHistogramArray[recs.getRecord().getBaseQualities()[recs.getOffset()]]++;
        }
      }

      final int depth = Math.min(readNames.size(), max);
      if (depth < readNames.size()) basesExcludedByCapping += readNames.size() - max;
      HistogramArray[depth]++;

      // Record progress and perhaps stop
      progress.record(info.getSequenceName(), info.getPosition());
      if (usingStopAfter && ++counter > stopAfter) break;
    }

    // Construct and write the outputs
    final Histogram<Integer> histo = new Histogram<Integer>("coverage", "count");
    for (int i = 0; i < HistogramArray.length; ++i) {
      histo.increment(i, HistogramArray[i]);
    }

    // Construct and write the outputs
    final Histogram<Integer> baseQHisto = new Histogram<Integer>("value", "baseq_count");
    for (int i = 0; i < baseQHistogramArray.length; ++i) {
      baseQHisto.increment(i, baseQHistogramArray[i]);
    }

    final WgsMetrics metrics = generateWgsMetrics();
    metrics.GENOME_TERRITORY = (long) histo.getSumOfValues();
    metrics.MEAN_COVERAGE = histo.getMean();
    metrics.SD_COVERAGE = histo.getStandardDeviation();
    metrics.MEDIAN_COVERAGE = histo.getMedian();
    metrics.MAD_COVERAGE = histo.getMedianAbsoluteDeviation();

    final long basesExcludedByDupes = getBasesExcludedBy(dupeFilter);
    final long basesExcludedByMapq = getBasesExcludedBy(mapqFilter);
    final long basesExcludedByPairing = getBasesExcludedBy(pairFilter);
    final double total = histo.getSum();
    final double totalWithExcludes =
        total
            + basesExcludedByDupes
            + basesExcludedByMapq
            + basesExcludedByPairing
            + basesExcludedByBaseq
            + basesExcludedByOverlap
            + basesExcludedByCapping;
    metrics.PCT_EXC_DUPE = basesExcludedByDupes / totalWithExcludes;
    metrics.PCT_EXC_MAPQ = basesExcludedByMapq / totalWithExcludes;
    metrics.PCT_EXC_UNPAIRED = basesExcludedByPairing / totalWithExcludes;
    metrics.PCT_EXC_BASEQ = basesExcludedByBaseq / totalWithExcludes;
    metrics.PCT_EXC_OVERLAP = basesExcludedByOverlap / totalWithExcludes;
    metrics.PCT_EXC_CAPPED = basesExcludedByCapping / totalWithExcludes;
    metrics.PCT_EXC_TOTAL = (totalWithExcludes - total) / totalWithExcludes;

    metrics.PCT_1X =
        MathUtil.sum(HistogramArray, 1, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_5X =
        MathUtil.sum(HistogramArray, 5, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_10X =
        MathUtil.sum(HistogramArray, 10, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_15X =
        MathUtil.sum(HistogramArray, 15, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_20X =
        MathUtil.sum(HistogramArray, 20, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_25X =
        MathUtil.sum(HistogramArray, 25, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_30X =
        MathUtil.sum(HistogramArray, 30, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_40X =
        MathUtil.sum(HistogramArray, 40, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_50X =
        MathUtil.sum(HistogramArray, 50, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_60X =
        MathUtil.sum(HistogramArray, 60, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_70X =
        MathUtil.sum(HistogramArray, 70, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_80X =
        MathUtil.sum(HistogramArray, 80, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_90X =
        MathUtil.sum(HistogramArray, 90, HistogramArray.length) / (double) metrics.GENOME_TERRITORY;
    metrics.PCT_100X =
        MathUtil.sum(HistogramArray, 100, HistogramArray.length)
            / (double) metrics.GENOME_TERRITORY;

    final MetricsFile<WgsMetrics, Integer> out = getMetricsFile();
    out.addMetric(metrics);
    out.addHistogram(histo);
    if (INCLUDE_BQ_HISTOGRAM) {
      out.addHistogram(baseQHisto);
    }
    out.write(OUTPUT);

    return 0;
  }

  protected WgsMetrics generateWgsMetrics() {
    return new WgsMetrics();
  }

  protected long getBasesExcludedBy(final CountingFilter filter) {
    return filter.getFilteredBases();
  }

  protected SamLocusIterator getLocusIterator(final SamReader in) {
    return new SamLocusIterator(in);
  }
}
Example #22
0
 /**
  * Returns the number of items in the download queue. Does NOT include the currently downloading
  * item, if any.
  *
  * <p>
  *
  * @return The number of items in the download queue.
  */
 public Integer getNumberOfQueuedItems() {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: getNumberOfItems.");
   return RecordingMaps.size();
 }
Example #23
0
 /**
  * Add an item to be downloaded. Details to follow....
  *
  * <p>
  *
  * @param info an array of strings ....
  * @return true if it succeeded, false otherwise.
  */
 public boolean addItem(RecordingEpisode episode) {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: addItem.");
   return RecordingMaps.add(episode);
 }
Example #24
0
  /**
   * Interface definition for implementation classes that listen for events from the SageTV core
   *
   * <p>Variable types are in brackets[] after the var name unless they are the same as the var name
   * itself. List of known core events:
   *
   * <p>MediaFileImported - vars: MediaFile ImportingStarted ImportingCompleted RecordingCompleted
   * (called when a complete recording is done) vars: MediaFile RecordingStarted (called when any
   * kind of recording is started) vars: MediaFile RecordingStopped (called whenever a recording is
   * stopped for any reason) vars: MediaFile AllPluginsLoaded RecordingScheduleChanged
   * ConflictStatusChanged SystemMessagePosted vars: SystemMessage EPGUpdateCompleted
   * MediaFileRemoved vars: MediaFile PlaybackStopped (called when the file is closed) vars:
   * MediaFile, UIContext[String], Duration[Long], MediaTime[Long], ChapterNum[Integer],
   * TitleNum[Integer] PlaybackFinished (called at the EOF) vars: MediaFile, UIContext[String],
   * Duration[Long], MediaTime[Long], ChapterNum[Integer], TitleNum[Integer] PlaybackStarted vars:
   * MediaFile, UIContext[String], Duration[Long], MediaTime[Long], ChapterNum[Integer],
   * TitleNum[Integer] FavoriteAdded vars: Favorite FavoriteModified vars: Favorite FavoriteRemoved
   * vars: Favorite PlaylistAdded vars: Playlist, UIContext[String] PlaylistModified vars: Playlist,
   * UIContext[String] PlaylistRemoved vars: Playlist, UIContext[String] ClientConnected vars:
   * IPAddress[String], MACAddress[String] (if its a placeshifter/extender, MACAddress is null
   * otherwise) ClientDisconnected vars: IPAddress[String], MACAddress[String] (if its a
   * placeshifter/extender, MACAddress is null otherwise)
   *
   * <p>This is a callback method invoked from the SageTV core for any events the listener has
   * subscribed to. See the sage.SageTVPluginRegistry interface definition for details regarding
   * subscribing and unsubscribing to events. The eventName will be a predefined String which
   * indicates the event type. The eventVars will be a Map of variables specific to the event
   * information. This Map should NOT be modified. The keys to the eventVars Map will generally be
   * Strings; but this may change in the future and plugins that submit events are not required to
   * follow that rule.
   */
  @Override
  public synchronized void sageEvent(String eventName, java.util.Map eventVars) {

    Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: Event received = " + eventName);

    // Check that we have the right event.
    if (!(eventName.startsWith("RecordingCompleted") || eventName.startsWith("RecordingStopped"))) {
      Log.getInstance()
          .write(Log.LOGLEVEL_WARN, "sageEvent: Unexpected event received = " + eventName);
      return;
    }

    // Check that we have a valid MediaFile.
    Object MediaFile = eventVars.get("MediaFile");

    if (MediaFile == null) {
      Log.getInstance().write(Log.LOGLEVEL_WARN, "sageEvent: null MediaFile");
      return;
    }

    Log.getInstance()
        .write(
            Log.LOGLEVEL_TRACE,
            "sageEvent: Finished recording "
                + AiringAPI.GetAiringTitle(MediaFile)
                + " - "
                + ShowAPI.GetShowEpisode(MediaFile));

    // If it's a Manual, Favorite, or TimedRecord (manual) we do not need to worry about it.
    if (AiringAPI.IsFavorite(MediaFile) || AiringAPI.IsManualRecord(MediaFile)) {
      Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: Is not an Intelligent Recording.");
      return;
    }

    // Create the DataStore which will allow us to access the data for this MediaFile.
    DataStore store = new DataStore(MediaFile);

    int maxToKeep;

    // If it's monitored keep the number specified. If it's not monitored use the
    // global default.
    if (store.isMonitored()) {
      Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: Using max for this show.");
      maxToKeep = store.getMax();
    } else {
      Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: Using global max.");
      maxToKeep = Util.GetIntProperty(PROPERTY_DEFAULT_MAX, DEFAULT_MAX_STRING);
    }

    Log.getInstance()
        .write(
            Log.LOGLEVEL_TRACE,
            "sageEvent: Max to keep = " + (maxToKeep == DEFAULT_MAX ? "unlimited" : maxToKeep));

    // See how many are already recorded.
    int numberRecorded = Util.getNumberRecorded(MediaFile);
    Log.getInstance()
        .write(Log.LOGLEVEL_TRACE, "sageEvent: Number already recorded = " + numberRecorded);

    // If it's unlimited or below the threshhold don't worry about it.
    if (maxToKeep == UNLIMITED || numberRecorded <= maxToKeep) {
      Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: Below threshhold.");
      return;
    }

    Log.getInstance()
        .write(
            Log.LOGLEVEL_TRACE,
            "sageEvent: Threshhold exceeded. Deleting one or more "
                + AiringAPI.GetAiringTitle(MediaFile));

    // Get the direction to sort.
    boolean keepOldest =
        Configuration.GetServerProperty(PROPERTY_KEEP_OLDEST, "true").equalsIgnoreCase("true");
    Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: Keep oldest = " + keepOldest);

    // Get all of the recordings in the proper order. Recordings at the beginning of the
    // List will be deleted first.
    List<Object> allRecorded = Util.getAllRecorded(MediaFile, "GetAiringStartTime", keepOldest);

    Log.getInstance()
        .write(Log.LOGLEVEL_TRACE, "sageEvent: Sorted list size = " + allRecorded.size());

    if (Log.getInstance().GetLogLevel() <= Log.LOGLEVEL_VERBOSE) {
      for (Object MF : allRecorded)
        Log.getInstance()
            .write(
                Log.LOGLEVEL_VERBOSE,
                "sageEvent: Date recorded = "
                    + Utility.PrintDateLong(AiringAPI.GetAiringStartTime(MF))
                    + " : "
                    + Utility.PrintTimeLong(AiringAPI.GetAiringStartTime(MF))
                    + " - "
                    + AiringAPI.GetAiringTitle(MF)
                    + " - "
                    + ShowAPI.GetShowEpisode(MF));
    }

    boolean reduceToMax =
        Configuration.GetServerProperty(PROPERTY_REDUCE_TO_MAX, "false").equalsIgnoreCase("true");

    // Calculate how many to delete.
    int numberToDelete = (reduceToMax ? numberRecorded - maxToKeep : 1);

    Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: Need to delete " + numberToDelete);

    // Sanity check.
    if (allRecorded == null || allRecorded.size() < numberToDelete || numberToDelete < 1) {
      Log.getInstance()
          .write(
              Log.LOGLEVEL_WARN,
              "sageEvent: Internal error. numberToDelete exceeds allRecorded. Deleting this MediaFile.");
      MediaFileAPI.DeleteFile(MediaFile);
      return;
    }

    for (int i = 0; i < numberToDelete; i++) {
      Object MF = allRecorded.get(i);

      // Log.getInstance().write(Log.LOGLEVEL_TRACE, "sageEvent: TESTMODE. Would have deleted " +
      // AiringAPI.GetAiringTitle(MF) + " - " + ShowAPI.GetShowEpisode(MF));
      if (MediaFileAPI.DeleteFile(MF))
        Log.getInstance()
            .write(
                Log.LOGLEVEL_TRACE,
                "sageEvent: Deleted "
                    + AiringAPI.GetAiringTitle(MF)
                    + " - "
                    + ShowAPI.GetShowEpisode(MF));
      else
        Log.getInstance()
            .write(
                Log.LOGLEVEL_WARN,
                "sageEvent: Failed to delete "
                    + AiringAPI.GetAiringTitle(MF)
                    + " - "
                    + ShowAPI.GetShowEpisode(MF));
    }
  }
Example #25
0
 public RecordingEpisode getCurrentlyRecording() {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: getCurrentlyRecording.");
   return CurrentlyRecording;
 }
Example #26
0
 public boolean removeAllItems() {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: removeAllItems.");
   RecordingMaps = new LinkedBlockingQueue<RecordingEpisode>();
   return true;
 }
Example #27
0
 /**
  * Gets the status of the "Stop" flag. If Stop is set to true the DownloadThread will exit
  * normally after finishing any download that is in progress.
  *
  * <p>
  *
  * @return The status of the Stop flag.
  */
 public boolean getStop() {
   Log.getInstance().write(Log.LOGLEVEL_TRACE, "DT: getStop.");
   return stop;
 }
/**
 * Class containing methods for working with properties
 *
 * @see SetupReader - setup reader allows more advanced access to properties
 * @author bastafidli
 */
public final class PropertyUtils extends OSSObject {
  // Attributes ///////////////////////////////////////////////////////////////

  /**
   * Map storing previous values of the configuration settings to eliminate excessive log output.
   * Use Hashtable because it is synchronized.
   */
  private static Map<String, String> s_previousValues = new ConcurrentHashMap<>();

  // Cached values ////////////////////////////////////////////////////////////

  /** Logger for this class */
  private static Logger s_logger = Log.getInstance(PropertyUtils.class);

  // Constructors /////////////////////////////////////////////////////////////

  /** Private constructor since this class cannot be instantiated */
  private PropertyUtils() {
    // Do nothing
  }

  // Logic ////////////////////////////////////////////////////////////////////

  /**
   * Save the configuration to a file.
   *
   * @param fileConfig - file to save the properties to
   * @param prpSettings - configuration settings to store to the file
   * @throws IOException - there was a problem saving configuration file.
   * @throws FileNotFoundException - file cannot be found
   */
  public static void save(File fileConfig, Properties prpSettings)
      throws IOException, FileNotFoundException {
    // Open the file
    OutputStream osConfigFile = null;

    try {
      // Open the file
      osConfigFile = new FileOutputStream(fileConfig);

      BufferedOutputStream bosConfigFile = null;

      // Load the properties
      try {
        bosConfigFile = new BufferedOutputStream(osConfigFile);

        // TODO: Improve: Once this is invoked, all the comments from
        // the original file are lost and the properties are in random
        // order. Figure out how to save it so we don't mess up the
        // comments and order/grouping of properties
        prpSettings.store(bosConfigFile, "DO NOT MODIFY THIS FILE DIRECTLY.");
      } finally {
        // Close the file
        try {
          if (bosConfigFile != null) {
            bosConfigFile.close();
          }
        } catch (IOException ioeExc) {
          // Ignore this
          s_logger.log(
              Level.WARNING,
              "Failed to close buffer for configuration file " + fileConfig.getCanonicalPath(),
              ioeExc);
        }
      }
    } finally {
      try {
        if (osConfigFile != null) {
          osConfigFile.close();
        }
      } catch (IOException ioeExc) {
        // Ignore this
        s_logger.log(
            Level.WARNING,
            "Failed to close configuration file " + fileConfig.getCanonicalPath(),
            ioeExc);
      }
    }
  }

  /**
   * Retrieve integer property value of which should existing within a specified range.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param iDefaultValue - default value to use if a valid value is not specified
   * @param strDisplayName - user friendly name of the property
   * @param iMinValue - inclusive minimal value of the range
   * @param iMaxValue - inclusive maximal value of the range
   * @return int - value of the property or default value if the value is not specified
   */
  public static int getIntPropertyInRange(
      Properties prpSettings,
      String strProperty,
      int iDefaultValue,
      String strDisplayName,
      int iMinValue,
      int iMaxValue) {
    String strParam;
    int iValue;

    // Read the property, use the name of the property as default value to
    // detect if property is not set
    strParam = prpSettings.getProperty(strProperty, strProperty);
    if ((strParam.length() == 0) || (strParam.equals(strProperty))) {
      printConfigMessage(
          strProperty,
          Integer.toString(iDefaultValue),
          strDisplayName
              + " is not set in property "
              + strProperty
              + ", using default value "
              + iDefaultValue);
      iValue = iDefaultValue;
    } else {
      try {
        iValue = Integer.parseInt(strParam);
        if ((iValue < iMinValue) || (iValue > iMaxValue)) {
          printConfigMessage(
              strProperty,
              Integer.toString(iDefaultValue),
              "Value of "
                  + strProperty
                  + " property is outside of valid range ("
                  + iMinValue
                  + " - "
                  + iMaxValue
                  + "), using default value "
                  + iDefaultValue);
          iValue = iDefaultValue;
        } else {
          printConfigMessage(strProperty, Integer.toString(iValue), null);
        }
      } catch (NumberFormatException nfeExc) {
        printConfigMessage(
            strProperty,
            Integer.toString(iDefaultValue),
            "Value of "
                + strProperty
                + " property is incorrect ("
                + strParam
                + ", valid range is "
                + iMinValue
                + " - "
                + iMaxValue
                + "), using default value "
                + iDefaultValue);
        iValue = iDefaultValue;
      }
    }

    return iValue;
  }

  /**
   * Retrieve string property value and if the value is not specified or it is empty throw an
   * exception.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param strDisplayName - user friendly name of the property
   * @return String - value of the property
   * @throws OSSConfigException - value for the requested property is not specified
   */
  public static String getStringProperty(
      Properties prpSettings, String strProperty, String strDisplayName) throws OSSConfigException {
    return getStringProperty(prpSettings, strProperty, strDisplayName, false);
  }

  /**
   * Retrieve string property value and if the value is not specified throw an exception.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param strDisplayName - user friendly name of the property
   * @param bAllowEmpty - if true then empty value is allowed
   * @return String - value of the property
   * @throws OSSConfigException - value for the requested property is not specified
   */
  public static String getStringProperty(
      Properties prpSettings, String strProperty, String strDisplayName, boolean bAllowEmpty)
      throws OSSConfigException {
    String strParam;
    String strValue;

    strParam = prpSettings.getProperty(strProperty, strProperty);
    if ((strParam.equals(strProperty)) || ((!bAllowEmpty) && (strParam.length() == 0))) {
      throw new OSSConfigException(strDisplayName + " is not set in property " + strProperty);
    } else {
      strValue = strParam;
    }
    printConfigMessage(strProperty, strValue, null);

    return strValue;
  }

  /**
   * Retrieve string property value and if the property is not specified or it is specified as an
   * empty value, use the default value instead.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param strDefaultValue - default value to use if a valid value is not specified
   * @param strDisplayName - user friendly name of the property
   * @return String - value of the property or default value if the value is not specified
   */
  public static String getStringProperty(
      Properties prpSettings, String strProperty, String strDefaultValue, String strDisplayName) {
    return getStringProperty(prpSettings, strProperty, strDefaultValue, strDisplayName, false);
  }

  /**
   * Retrieve string property value.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param strDefaultValue - default value to use if a valid value is not specified. If null is
   *     specified as a default value and no value is found then no config message will be printed
   *     into log
   * @param strDisplayName - user friendly name of the property
   * @param bAllowEmpty - if true then empty value is allowed
   * @return String - value of the property or default value if the value is not specified
   */
  public static String getStringProperty(
      Properties prpSettings,
      String strProperty,
      String strDefaultValue,
      String strDisplayName,
      boolean bAllowEmpty) {
    return getStringProperty(
        prpSettings, strProperty, strDefaultValue, strDisplayName, bAllowEmpty, true);
  }

  /**
   * Retrieve string property value.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param strDefaultValue - default value to use if a valid value is not specified. If null is
   *     specified as a default value and no value is found then no config message will be printed
   *     into log
   * @param strDisplayName - user friendly name of the property
   * @param bAllowEmpty - if true then empty value is allowed
   * @param bPrintMessage - if true then message about what value was read will be printed into the
   *     log, if false nothing will be
   * @return String - value of the property or default value if the value is not specified
   */
  public static String getStringProperty(
      Properties prpSettings,
      String strProperty,
      String strDefaultValue,
      String strDisplayName,
      boolean bAllowEmpty,
      boolean bPrintMessage) {
    String strParam;
    String strValue;

    // Read the property, use the name of the property as default value to
    // detect if property is not set
    strParam = prpSettings.getProperty(strProperty, strProperty);
    if ((strParam.equals(strProperty)) || ((!bAllowEmpty) && (strParam.length() == 0))) {
      if ((strDefaultValue != null) && (bPrintMessage)) {
        printConfigMessage(
            strProperty,
            strDefaultValue,
            strDisplayName
                + " is not set in property "
                + strProperty
                + ", using default value "
                + strDefaultValue);
      }
      strValue = strDefaultValue;
    } else {
      strValue = strParam;
    }

    if ((strValue != null) && (bPrintMessage)) {
      printConfigMessage(strProperty, strValue, null);
    }

    return strValue;
  }

  /**
   * Retrieve boolean property value and if the value is not specified throw an exception.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param strDisplayName - user friendly name of the property
   * @return boolean - value of the property
   * @throws OSSConfigException - value for the requested property is not specified
   */
  public static Boolean getBooleanProperty(
      Properties prpSettings, String strProperty, String strDisplayName) throws OSSConfigException {
    String strParam;
    Boolean bValue;

    strParam = prpSettings.getProperty(strProperty, strProperty);
    if ((strParam.equals(strProperty)) || (strParam.length() == 0)) {
      throw new OSSConfigException(strDisplayName + " is not set in property " + strProperty);
    } else {
      bValue = GlobalConstants.isTrue(strParam) ? Boolean.TRUE : Boolean.FALSE;
    }
    printConfigMessage(strProperty, bValue.toString(), null);

    return bValue;
  }

  /**
   * Retrieve boolean property value and if the value is not specified return the default value.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param bDefaultValue - default value to use if a valid value is not specified. If null is
   *     specified as a default value and no value is found then no config message will be printed
   *     into log
   * @param strDisplayName - user friendly name of the property
   * @return boolean - value of the property or default value if the value is not specified
   */
  public static Boolean getBooleanProperty(
      Properties prpSettings, String strProperty, Boolean bDefaultValue, String strDisplayName) {
    String strParam;
    Boolean bValue;

    strParam = prpSettings.getProperty(strProperty, strProperty);
    if ((strParam.equals(strProperty)) || (strParam.length() == 0)) {
      if (bDefaultValue != null) {
        printConfigMessage(
            strProperty,
            bDefaultValue.toString(),
            strDisplayName
                + " is not set in property "
                + strProperty
                + ", using default value "
                + bDefaultValue);
      }
      bValue = bDefaultValue;
    } else {
      bValue = GlobalConstants.isTrue(strParam) ? Boolean.TRUE : Boolean.FALSE;
    }
    if (bValue != null) {
      printConfigMessage(strProperty, bValue.toString(), null);
    }

    return bValue;
  }

  /**
   * Retrieve boolean property value as a string representation of Boolean.TRUE or Boolean.FALSE.
   *
   * @param prpSettings - properties to retrieve the setting from
   * @param strProperty - name of the property to retrieve
   * @param bDefaultValue - default value to use if a valid value is not specified. If null is
   *     specified as a default value and no value is found then no config message will be printed
   *     into log
   * @param strDisplayName - user friendly name of the property
   * @return String - value of the property or default value if the value is not specified. If
   *     default value is null then empty string is returned.
   */
  public static String getBooleanPropertyAsString(
      Properties prpSettings, String strProperty, Boolean bDefaultValue, String strDisplayName) {
    String strReturn = "";
    Boolean bReturn;

    bReturn = getBooleanProperty(prpSettings, strProperty, bDefaultValue, strDisplayName);
    if (bReturn != null) {
      strReturn = bReturn.toString();
    }

    return strReturn;
  }

  /**
   * Log configuration message to the logger if the value of the property is read for the first time
   * or has changed.
   *
   * @param strProperty - property value of which is read
   * @param strValue - value of the property
   * @param strMessage - message to log, if null a default message will be logged
   */
  public static void printConfigMessage(String strProperty, String strValue, String strMessage) {
    String strPrevious;

    if (strValue == null) {
      strValue = "null";
    }

    // No need to synchronize because s_previousValues is synchronized
    strPrevious = s_previousValues.get(strProperty);
    if ((strPrevious == null) || (!strPrevious.equals(strValue))) {
      s_previousValues.put(strProperty, strValue);
      if (strMessage != null) {
        s_logger.config(strMessage);
      } else {
        s_logger.log(Level.CONFIG, "{0} = {1}", new Object[] {strProperty, strValue});
      }
    }
  }
}
Example #29
0
/** @author <a href="mailto:herzog@raffael.ch">Raffael Herzog</a> */
public final class ContractsContext {

  public static final String ROOT_NAME = "*";

  private static final ContractsContext ROOT = new ContractsContext("");
  private static final Log LOG = Log.getInstance();

  private static final Map<String, ContractsContext> CONTEXTS =
      new HashMap<String, ContractsContext>();

  private final String name;
  private final LinkedList<ContractsContext> children = new LinkedList<ContractsContext>();
  private volatile boolean enabled;

  private ContractsContext(@NotNull String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "ContractsPolicy[" + name + "]";
  }

  @NotNull
  public static ContractsContext getContext(@NotNull String name) {
    ContractsContext context;
    if (name.equals(ROOT_NAME)) {
      return ROOT;
    }
    synchronized (CONTEXTS) {
      context = CONTEXTS.get(name);
      if (context != null) {
        return context;
      }
      if (!ROOT_NAME.equals(name)) {
        boolean firstChar = true;
        for (int i = 0; i < name.length(); i++) {
          char c = name.charAt(i);
          if (firstChar) {
            if (!Character.isJavaIdentifierStart(c)) {
              throw new IllegalArgumentException("Illegal policy name: '" + name + "'");
            }
            firstChar = false;
          } else {
            if (c == '.') {
              firstChar = true;
            } else if (!Character.isJavaIdentifierPart(c)) {
              throw new IllegalArgumentException("Illegal policy name: '" + name + "'");
            }
          }
        }
        if (firstChar) {
          throw new IllegalArgumentException("Illegal policy name: '" + name + "'");
        }
      }
      return getContext0(name);
    }
  }

  @NotNull
  private static ContractsContext getContext0(@NotNull String name) {
    ContractsContext context;
    context = CONTEXTS.get(name);
    if (context == null) {
      context = new ContractsContext(name);
      ContractsContext parent;
      int pos = name.lastIndexOf('.');
      if (pos < 0) {
        parent = ROOT;
      } else {
        parent = getContext0(name.substring(0, pos));
      }
      parent.children.add(context);
      CONTEXTS.put(name, context);
    }
    return context;
  }

  @NotNull
  public static ContractsContext getContext(@NotNull Class<?> clazz) {
    Class<?> outer = clazz;
    while (outer.getEnclosingClass() != null) {
      outer = outer.getEnclosingClass();
    }
    synchronized (CONTEXTS) {
      return getContext0(outer.getName());
    }
  }

  @NotNull
  public static ContractsContext getContext(@NotNull Package pkg) {
    synchronized (CONTEXTS) {
      return getContext0(pkg.getName());
    }
  }

  public boolean isEnabled() {
    return enabled;
  }

  public void enable() {
    synchronized (CONTEXTS) {
      LOG.info("Enabling contracts for %s", name);
      enable0();
    }
  }

  private void enable0() {
    enabled = true;
    for (ContractsContext child : children) {
      child.enable0();
    }
  }

  public void disable() {
    synchronized (CONTEXTS) {
      LOG.info("Disabling contracts for %s", name);
      disable0();
    }
  }

  private void disable0() {
    enabled = false;
    for (ContractsContext child : children) {
      child.disable0();
    }
  }

  public void violation(ContractViolationError violation) {
    throw violation;
  }
}