/**
   * 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;
    }
  }
  /**
   * Opens a file chooser where the user can select the database file.
   *
   * @param evt
   */
  private void browseDatabaseSettingsActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_browseDatabaseSettingsActionPerformed

    LastSelectedFolder lastSelectedFolder = peptideShakerGUI.getLastSelectedFolder();
    File startLocation = null;
    File utilitiesDbFolder = peptideShakerGUI.getUtilitiesUserPreferences().getDbFolder();
    if (utilitiesDbFolder != null && utilitiesDbFolder.exists()) {
      startLocation = utilitiesDbFolder;
    }
    if (startLocation == null) {
      startLocation = new File(getLastSelectedFolder(lastSelectedFolder));
    }

    UtilitiesUserPreferences utilitiesUserPreferences =
        UtilitiesUserPreferences.loadUserPreferences();
    if (utilitiesUserPreferences.getDbFolder() != null
        && utilitiesUserPreferences.getDbFolder().exists()) {
      startLocation = utilitiesUserPreferences.getDbFolder();
    }

    // First check whether a file has already been selected.
    // If so, start from that file's parent.
    if (databaseSettingsTxt.getText() != null && new File(databaseSettingsTxt.getText()).exists()) {
      File temp = new File(databaseSettingsTxt.getText());
      startLocation = temp.getParentFile();
    }

    JFileChooser fc = new JFileChooser(startLocation);
    FileFilter filter =
        new FileFilter() {
          @Override
          public boolean accept(File myFile) {
            return myFile.getName().toLowerCase().endsWith("fasta")
                || myFile.getName().toLowerCase().endsWith("fas")
                || myFile.isDirectory();
          }

          @Override
          public String getDescription() {
            return "Supported formats: FASTA (.fasta or .fas)";
          }
        };

    fc.setFileFilter(filter);
    int result = fc.showOpenDialog(this);

    if (result == JFileChooser.APPROVE_OPTION) {
      File file = fc.getSelectedFile();

      if (file.getName().contains(" ")) {
        renameFastaFileName(file);
      } else {
        databaseSettingsTxt.setText(file.getAbsolutePath());
        databaseSettingsTxt.setText(file.getAbsolutePath());
      }

      lastSelectedFolder.setLastSelectedFolder(
          SequenceDbDetailsDialog.lastFolderKey, file.getAbsolutePath());
      targetDecoySettingsButton.setEnabled(true);

      // check if the database contains decoys
      if (!file.getAbsolutePath()
          .endsWith(
              peptideShakerGUI.getUtilitiesUserPreferences().getTargetDecoyFileNameSuffix()
                  + ".fasta")) {

        int value =
            JOptionPane.showConfirmDialog(
                this,
                "The selected FASTA file does not seem to contain decoy sequences.\n"
                    + "Decoys are required by PeptideShaker. Add decoys?",
                "Add Decoy Sequences?",
                JOptionPane.YES_NO_OPTION);

        if (value == JOptionPane.NO_OPTION) {
          // do nothing
        } else if (value == JOptionPane.YES_OPTION) {
          targetDecoySettingsButtonActionPerformed(null);
        }
      }

      validateParametersInput(false);
    }
  } // GEN-LAST:event_browseDatabaseSettingsActionPerformed