Exemplo n.º 1
0
  /** Initializes a new instance of <code>Plugin</code>. */
  public Plugin(CySwingApplication swingApp) {
    try {
      // Initiate default visual settings
      SettingsSerializer.initVisualSettings();
      // If initialization fails, the following lines are not executed:

    } catch (SecurityException ex) {
      Utils.showErrorBox(swingApp.getJFrame(), Messages.DT_SECERROR, Messages.SM_SECERROR1);
      System.err.println(Messages.SM_SECERROR1);
    } catch (InnerException ex) {
      final ByteArrayOutputStream os = new ByteArrayOutputStream();
      final PrintStream ps = new PrintStream(os);
      ex.printStackTrace(ps);
      ps.flush();
      if (os.toString().contains("NoClassDefFoundError:")) {
        // Library is missing
        logger.error(Messages.SM_LOGERROR, ex);
      } else {
        // NetworkAnalyzer internal error
        logger.error(Messages.SM_LOGERROR, ex);
      }
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see cytoscape.util.SwingWorker#construct()
   */
  @Override
  public Object construct() {
    progress = 0;
    for (final File inputFile : inputFiles) {

      // Make a new network in cytoscape from a filename in
      // the network-directory
      CyNetwork network = null;
      try {
        write(Messages.SM_LOADING + inputFile.getName() + " ... ");
        if (!inputFile.isFile()) {
          throw new RuntimeException();
        }
        CyNetworkReader reader =
            cyNetworkViewReaderMgr.getReader(inputFile.toURI(), inputFile.getName());
        try {
          // TODO Use the Task's task monitor
          reader.run(new SampleTaskMonitor());
        } catch (Exception ex) {
          return null;
        }
        network = reader.getNetworks()[0];
        network.getRow(network).set(CyNetwork.NAME, inputFile.getName());
      } catch (RuntimeException e) {
        writeLine(Messages.SM_READERROR);
        reports.add(new NetworkAnalysisReport(inputFile, null, AnalysisError.NETWORK_NOT_OPENED));
        progress += PROGRESS_PER_NET;
        continue;
      }

      // Get all possible interpretations for the network
      NetworkInspection inspection = null;
      try {
        inspection = CyNetworkUtils.inspectNetwork(network);
      } catch (IllegalArgumentException e) {
        writeLine(Messages.SM_DONE);
        reports.add(new NetworkAnalysisReport(inputFile, null, AnalysisError.NETWORK_EMPTY));
        unloadNetwork(inputFile, network);
        continue;
      } catch (NullPointerException e) {
        reports.add(new NetworkAnalysisReport(inputFile, null, AnalysisError.NETWORK_FILE_INVALID));
        progress += PROGRESS_PER_NET;
        continue;
      }

      final NetworkInterpretation[] interprs =
          filterInterpretations(getInterpretations(inspection));
      final int intCount = interprs.length;
      final int advance = PROGRESS_PER_NET / intCount;

      // Run NetworkAnalyzer on all accepted interpretations
      writeLine(Messages.SM_DONE);
      for (int j = 0; j < intCount; progress += advance, ++j) {
        if (cancelled) {
          writeLine(Messages.SM_ANALYSISC);
          return null;
        }

        // Run the analysis for an interpretation
        final NetworkInterpretation interpretation = interprs[j];
        try {
          if (interpretation.isDirected()) {
            analyzer = new DirNetworkAnalyzer(network, null, interpretation);
          } else {
            analyzer = new UndirNetworkAnalyzer(network, null, interpretation);
          }
          writeLine(
              Messages.DI_ANALYZINGINTERP1 + (j + 1) + Messages.DI_ANALYZINGINTERP2 + intCount);
          final int maxProgress = analyzer.getMaxProgress();
          scale = (double) advance / (double) maxProgress;
          analyzing = true;
          subProgress = 0;
          analyzer.computeAll();
          analyzing = false;
          if (cancelled) {
            writeLine(Messages.SM_ANALYSISC);
            return null;
          }
          final NetworkStats stats = analyzer.getStats();
          synchronized (this) {
            analyzer = null;
          }

          final String networkName = network.getRow(network).get("name", String.class);
          stats.setTitle(networkName + interpretation.getInterpretSuffix());
          final String extendedName = networkName + createID(interpretation);
          try {
            if (SettingsSerializer.getPluginSettings().getUseNodeAttributes()) {
              if (!saveNodeAttributes(
                  network, interpretation.isDirected(), outputDir, extendedName)) {
                writeLine(Messages.SM_ATTRIBUTESNOTSAVED);
              }
            }
            File netstatFile = new File(outputDir, extendedName + ".netstats");
            StatsSerializer.save(stats, netstatFile);
            writeLine(Messages.SM_RESULTSSAVED);
            reports.add(new NetworkAnalysisReport(inputFile, interpretation, netstatFile));
          } catch (SecurityException ex) {
            writeError(Messages.SM_SAVEERROR);
            reports.add(
                new NetworkAnalysisReport(
                    inputFile, interpretation, AnalysisError.OUTPUT_NOT_CREATED));
          } catch (FileNotFoundException ex) {
            writeError(Messages.SM_SAVEERROR);
            reports.add(
                new NetworkAnalysisReport(
                    inputFile, interpretation, AnalysisError.OUTPUT_NOT_CREATED));
          } catch (IOException e) {
            writeError(Messages.SM_SAVEERROR);
            reports.add(
                new NetworkAnalysisReport(
                    inputFile, interpretation, AnalysisError.OUTPUT_IO_ERROR));
          }

          if (cancelled) {
            writeLine(Messages.SM_ANALYSISC);
            return null;
          }
        } catch (Exception e) {
          reports.add(
              new NetworkAnalysisReport(inputFile, interpretation, AnalysisError.INTERNAL_ERROR));
        }
      }

      unloadNetwork(inputFile, network);
    }
    return null;
  }