/** Sets the path configuration. */
 private void setPathConfiguration() throws IOException {
   File pathConfigurationFile =
       new File(PeptideShaker.getJarFilePath(), UtilitiesPathPreferences.configurationFileName);
   if (pathConfigurationFile.exists()) {
     PeptideShakerPathPreferences.loadPathPreferencesFromFile(pathConfigurationFile);
   }
 }
 /** Loads the species from the species file into the species factory. */
 private void loadSpecies() {
   try {
     SpeciesFactory speciesFactory = SpeciesFactory.getInstance();
     speciesFactory.initiate(PeptideShaker.getJarFilePath());
   } catch (Exception e) {
     System.out.println("An error occurred while loading the species.");
     e.printStackTrace();
   }
 }
  /**
   * Calling this method will run the configured PeptideShaker process.
   *
   * @return returns 1 if the process was canceled
   */
  public Object call() {

    PathSettingsCLIInputBean pathSettingsCLIInputBean =
        followUpCLIInputBean.getPathSettingsCLIInputBean();

    if (pathSettingsCLIInputBean.getLogFolder() != null) {
      redirectErrorStream(pathSettingsCLIInputBean.getLogFolder());
    }

    if (pathSettingsCLIInputBean.hasInput()) {
      PathSettingsCLI pathSettingsCLI = new PathSettingsCLI(pathSettingsCLIInputBean);
      pathSettingsCLI.setPathSettings();
    } else {
      try {
        setPathConfiguration();
      } catch (Exception e) {
        System.out.println(
            "An error occurred when the setting path configurations. Default paths will be used.");
        e.printStackTrace();
      }
    }

    setDbFolder(PeptideShaker.getMatchesFolder());

    try {
      ArrayList<PathKey> errorKeys = PeptideShakerPathPreferences.getErrorKeys();
      if (!errorKeys.isEmpty()) {
        System.out.println(
            "Unable to write in the following configuration folders. Please use a temporary folder, "
                + "the path configuration command line, or edit the configuration paths from the graphical interface.");
        for (PathKey pathKey : errorKeys) {
          System.out.println(pathKey.getId() + ": " + pathKey.getDescription());
        }
      }
    } catch (Exception e) {
      System.out.println("Unable to load the path configurations. Default paths will be used.");
      e.printStackTrace();
    }

    // Load user preferences
    utilitiesUserPreferences = UtilitiesUserPreferences.loadUserPreferences();

    // Instantiate factories
    PeptideShaker.instantiateFacories(utilitiesUserPreferences);
    ptmFactory = PTMFactory.getInstance();
    enzymeFactory = EnzymeFactory.getInstance();

    // Load resources files
    loadSpecies();

    waitingHandler = new WaitingHandlerCLIImpl();

    String inputFilePath = null;

    try {
      if (followUpCLIInputBean.getZipFile() != null) {
        inputFilePath = followUpCLIInputBean.getZipFile().getAbsolutePath();
        loadCpsFromZipFile(
            followUpCLIInputBean.getZipFile(), PeptideShaker.getMatchesFolder(), waitingHandler);
      } else if (followUpCLIInputBean.getCpsFile() != null) {
        inputFilePath = followUpCLIInputBean.getCpsFile().getAbsolutePath();
        cpsFile = followUpCLIInputBean.getCpsFile();
        loadCpsFile(PeptideShaker.getMatchesFolder(), waitingHandler);
      } else {
        waitingHandler.appendReport("PeptideShaker project input missing.", true, true);
        return 1;
      }
    } catch (SQLException e) {
      waitingHandler.appendReport(
          "An error occurred while reading: "
              + inputFilePath
              + ". "
              + "It looks like another instance of PeptideShaker is still connected to the file. "
              + "Please close all instances of PeptideShaker and try again.",
          true,
          true);
      e.printStackTrace();
    } catch (Exception e) {
      waitingHandler.appendReport(
          "An error occurred while reading: " + inputFilePath + ".", true, true);
      e.printStackTrace();
      try {
        PeptideShakerCLI.closePeptideShaker(identification);
      } catch (Exception e2) {
        waitingHandler.appendReport("An error occurred while closing PeptideShaker.", true, true);
        e2.printStackTrace();
      }
      return 1;
    }

    // load fasta file
    try {
      if (!loadFastaFile(waitingHandler)) {
        waitingHandler.appendReport(
            "The FASTA file was not found. Please provide its location in the command line parameters.",
            true,
            true);
        try {
          PeptideShakerCLI.closePeptideShaker(identification);
        } catch (Exception e2) {
          waitingHandler.appendReport("An error occurred while closing PeptideShaker.", true, true);
          e2.printStackTrace();
        }
        return 1;
      }
      waitingHandler.appendReport(
          "Protein database "
              + identificationParameters
                  .getProteinInferencePreferences()
                  .getProteinSequenceDatabase()
                  .getName()
              + ".",
          true,
          true);
    } catch (Exception e) {
      waitingHandler.appendReport("An error occurred while loading the fasta file.", true, true);
      e.printStackTrace();
      try {
        PeptideShakerCLI.closePeptideShaker(identification);
      } catch (Exception e2) {
        waitingHandler.appendReport("An error occurred while closing PeptideShaker.", true, true);
        e2.printStackTrace();
      }
      return 1;
    }

    // load the spectrum files
    try {
      if (!loadSpectrumFiles(waitingHandler)) {
        if (identification.getSpectrumFiles().size() > 1) {
          waitingHandler.appendReport(
              "The spectrum files were not found. Please provide their location in the command line parameters.",
              true,
              true);
        } else {
          waitingHandler.appendReport(
              "The spectrum file was not found. Please provide its location in the command line parameters",
              true,
              true);
        }
        try {
          PeptideShakerCLI.closePeptideShaker(identification);
        } catch (Exception e2) {
          waitingHandler.appendReport("An error occurred while closing PeptideShaker.", true, true);
          e2.printStackTrace();
        }
        return 1;
      }
    } catch (Exception e) {
      waitingHandler.appendReport(
          "An error occurred while loading the spectrum file(s).", true, true);
      e.printStackTrace();
      try {
        PeptideShakerCLI.closePeptideShaker(identification);
      } catch (Exception e2) {
        waitingHandler.appendReport("An error occurred while closing PeptideShaker.", true, true);
        e2.printStackTrace();
      }
      return 1;
    }

    // Load project specific PTMs
    String error =
        PeptideShaker.loadModifications(getIdentificationParameters().getSearchParameters());
    if (error != null) {
      System.out.println(error);
    }

    // recalibrate spectra
    if (followUpCLIInputBean.recalibrationNeeded()) {
      try {
        CLIExportMethods.recalibrateSpectra(
            followUpCLIInputBean, identification, identificationParameters, waitingHandler);
        waitingHandler.appendReport("Recalibration process completed.", true, true);
      } catch (Exception e) {
        waitingHandler.appendReport(
            "An error occurred while recalibrating the spectra.", true, true);
        e.printStackTrace();
        waitingHandler.setRunCanceled();
      }
    }

    // export spectra
    if (followUpCLIInputBean.spectrumExportNeeded()) {
      try {
        CLIExportMethods.exportSpectra(
            followUpCLIInputBean,
            identification,
            waitingHandler,
            identificationParameters.getSequenceMatchingPreferences());
        waitingHandler.appendReport("Spectrum export completed.", true, true);
      } catch (Exception e) {
        waitingHandler.appendReport("An error occurred while exporting the spectra.", true, true);
        e.printStackTrace();
        waitingHandler.setRunCanceled();
      }
    }

    // export protein accessions
    if (followUpCLIInputBean.accessionExportNeeded()) {
      try {
        CLIExportMethods.exportAccessions(
            followUpCLIInputBean,
            identification,
            identificationFeaturesGenerator,
            waitingHandler,
            filterPreferences);
        waitingHandler.appendReport("Protein accessions export completed.", true, true);
      } catch (Exception e) {
        waitingHandler.appendReport(
            "An error occurred while exporting the protein accessions.", true, true);
        e.printStackTrace();
        waitingHandler.setRunCanceled();
      }
    }

    // export protein details
    if (followUpCLIInputBean.fastaExportNeeded()) {
      try {
        CLIExportMethods.exportFasta(
            followUpCLIInputBean,
            identification,
            identificationFeaturesGenerator,
            waitingHandler,
            filterPreferences);
        waitingHandler.appendReport("Protein details export completed.", true, true);
      } catch (Exception e) {
        waitingHandler.appendReport(
            "An error occurred while exporting the protein details.", true, true);
        e.printStackTrace();
        waitingHandler.setRunCanceled();
      }
    }

    // progenesis export
    if (followUpCLIInputBean.progenesisExportNeeded()) {
      try {
        CLIExportMethods.exportProgenesis(
            followUpCLIInputBean,
            identification,
            waitingHandler,
            identificationParameters.getSequenceMatchingPreferences());
        waitingHandler.appendReport("Progenesis export completed.", true, true);
      } catch (Exception e) {
        waitingHandler.appendReport(
            "An error occurred while exporting the Progenesis file.", true, true);
        e.printStackTrace();
        waitingHandler.setRunCanceled();
      }
    }

    // PepNovo training export
    if (followUpCLIInputBean.pepnovoTrainingExportNeeded()) {
      try {
        CLIExportMethods.exportPepnovoTrainingFiles(
            followUpCLIInputBean, identification, identificationParameters, waitingHandler);
        waitingHandler.appendReport("PepNovo training export completed.", true, true);
      } catch (Exception e) {
        waitingHandler.appendReport(
            "An error occurred while exporting the PepNovo training file.", true, true);
        e.printStackTrace();
        waitingHandler.setRunCanceled();
      }
    }

    // inclusion list export
    if (followUpCLIInputBean.inclusionListNeeded()) {
      try {
        CLIExportMethods.exportInclusionList(
            followUpCLIInputBean,
            identification,
            identificationFeaturesGenerator,
            identificationParameters.getSearchParameters(),
            waitingHandler,
            filterPreferences);
      } catch (Exception e) {
        waitingHandler.appendReport(
            "An error occurred while generating the inclusion list.", true, true);
        e.printStackTrace();
        waitingHandler.setRunCanceled();
      }
    }

    try {
      PeptideShakerCLI.closePeptideShaker(identification);
    } catch (Exception e2) {
      waitingHandler.appendReport("An error occurred while closing PeptideShaker.", true, true);
      e2.printStackTrace();
      waitingHandler.setRunCanceled();
    }

    if (!waitingHandler.isRunCanceled()) {
      waitingHandler.appendReport("Follow-up export completed.", true, true);
      System.exit(
          0); // @TODO: Find other ways of cancelling the process? If not cancelled searchgui will
              // not stop.
      // Note that if a different solution is found, the DummyFrame has to be closed similar to the
      // setVisible method in the WelcomeDialog!!
      return 0;
    } else {
      System.exit(
          1); // @TODO: Find other ways of cancelling the process? If not cancelled searchgui will
              // not stop.
      // Note that if a different solution is found, the DummyFrame has to be closed similar to the
      // setVisible method in the WelcomeDialog!!
      return 1;
    }
  }
 /**
  * Updates the search parameters.
  *
  * @param searchParameters the new search parameters
  */
 public void setSearchParameters(SearchParameters searchParameters) {
   this.searchParameters = searchParameters;
   PeptideShaker.loadModifications(searchParameters);
 }