/**
   * Writes the desired section.
   *
   * @param identification the identification of the project
   * @param identificationFeaturesGenerator the identification features generator of the project
   * @param identificationParameters the identification parameters
   * @param keys the keys of the protein matches to output
   * @param nSurroundingAA the number of surrounding amino acids to export
   * @param linePrefix the line prefix to use.
   * @param validatedOnly whether only validated matches should be exported
   * @param decoys whether decoy matches should be exported as well
   * @param waitingHandler the waiting handler
   * @throws IOException exception thrown whenever an error occurred while interacting with a file
   * @throws SQLException thrown whenever an error occurred while interacting with the database
   * @throws ClassNotFoundException thrown whenever an error occurred while deserializing a match
   *     from the database
   * @throws InterruptedException thrown whenever a threading error occurred while interacting with
   *     the database
   * @throws MzMLUnmarshallerException thrown whenever an error occurred while reading an mzML file
   */
  public void writeSection(
      Identification identification,
      IdentificationFeaturesGenerator identificationFeaturesGenerator,
      IdentificationParameters identificationParameters,
      ArrayList<String> keys,
      int nSurroundingAA,
      String linePrefix,
      boolean validatedOnly,
      boolean decoys,
      WaitingHandler waitingHandler)
      throws IOException, SQLException, ClassNotFoundException, InterruptedException,
          MzMLUnmarshallerException {

    if (waitingHandler != null) {
      waitingHandler.setSecondaryProgressCounterIndeterminate(true);
    }

    if (header) {
      writeHeader();
    }

    if (keys == null) {
      keys = new ArrayList<String>(identification.getPeptideIdentification());
    }

    int line = 1;

    if (waitingHandler != null) {
      waitingHandler.setWaitingText("Exporting. Please Wait...");
      waitingHandler.resetSecondaryProgressCounter();
      waitingHandler.setMaxSecondaryProgressCounter(keys.size());
    }

    PSParameter psParameter = new PSParameter();
    ArrayList<UrParameter> parameters = new ArrayList<UrParameter>(1);
    parameters.add(psParameter);

    PeptideMatchesIterator peptideMatchesIterator =
        identification.getPeptideMatchesIterator(
            keys, parameters, psmSection != null, parameters, waitingHandler);

    while (peptideMatchesIterator.hasNext()) {

      if (waitingHandler != null) {
        if (waitingHandler.isRunCanceled()) {
          return;
        }
        waitingHandler.increaseSecondaryProgressCounter();
      }

      PeptideMatch peptideMatch = peptideMatchesIterator.next();
      String peptideKey = peptideMatch.getKey();
      psParameter = (PSParameter) identification.getPeptideMatchParameter(peptideKey, psParameter);

      if (!validatedOnly || psParameter.getMatchValidationLevel().isValidated()) {

        if (decoys
            || !peptideMatch
                .getTheoreticPeptide()
                .isDecoy(identificationParameters.getSequenceMatchingPreferences())) {

          boolean first = true;

          if (indexes) {
            if (linePrefix != null) {
              writer.write(linePrefix);
            }
            writer.write(line + "");
            first = false;
          }

          for (ExportFeature exportFeature : peptideFeatures) {
            if (!first) {
              writer.addSeparator();
            } else {
              first = false;
            }
            PsPeptideFeature peptideFeature = (PsPeptideFeature) exportFeature;
            writer.write(
                getfeature(
                    identification,
                    identificationFeaturesGenerator,
                    identificationParameters,
                    keys,
                    nSurroundingAA,
                    linePrefix,
                    peptideMatch,
                    psParameter,
                    peptideFeature,
                    validatedOnly,
                    decoys,
                    waitingHandler));
          }
          writer.newLine();
          if (psmSection != null) {
            String psmSectionPrefix = "";
            if (linePrefix != null) {
              psmSectionPrefix += linePrefix;
            }
            psmSectionPrefix += line + ".";
            writer.increaseDepth();
            if (waitingHandler != null) {
              waitingHandler.setDisplayProgress(false);
            }
            psmSection.writeSection(
                identification,
                identificationFeaturesGenerator,
                identificationParameters,
                peptideMatch.getSpectrumMatchesKeys(),
                psmSectionPrefix,
                nSurroundingAA,
                validatedOnly,
                decoys,
                waitingHandler);
            if (waitingHandler != null) {
              waitingHandler.setDisplayProgress(true);
            }
            writer.decreseDepth();
          }
          line++;
        }
      }
    }
  }