/**
   * Creates the Andromeda PTM file and saves the PTM indexes in the search parameters.
   *
   * @param andromedaFolder the Andromeda installation folder
   * @param identificationParameters the identification parameters
   * @param identificationParametersFile the file where to save the search parameters
   * @throws IOException exception thrown whenever an error occurs while writing to the file.
   * @throws java.lang.ClassNotFoundException exception thrown whenever an error occurred while
   *     saving the search parameters
   */
  public static void createPtmFile(
      File andromedaFolder,
      IdentificationParameters identificationParameters,
      File identificationParametersFile)
      throws IOException, ClassNotFoundException {

    File file = new File(andromedaFolder, "conf");
    file = new File(file, "modifications.xml");
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    int index = 0;
    String date = "0001-01-01T00:00:00";

    PTMFactory ptmFactory = PTMFactory.getInstance();
    SearchParameters searchParameters = identificationParameters.getSearchParameters();
    AndromedaParameters andromedaParameters =
        (AndromedaParameters)
            searchParameters.getIdentificationAlgorithmParameter(Advocate.andromeda.getIndex());

    try {
      bw.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
      bw.newLine();
      bw.write(
          "<modifications xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
      bw.newLine();

      // add the default ptms
      for (String ptmName : ptmFactory.getDefaultModifications()) {
        PTM ptm = ptmFactory.getPTM(ptmName);
        writePtm(bw, date, ptm, index);
        andromedaParameters.setPtmIndex(ptmName, index);
        index++;
      }

      // add the user ptms
      for (String ptmName : ptmFactory.getUserModifications()) {
        PTM ptm = ptmFactory.getPTM(ptmName);
        writePtm(bw, date, ptm, index);
        andromedaParameters.setPtmIndex(ptmName, index);
        index++;
      }

      bw.write("</modifications>");
      bw.newLine();
    } finally {
      bw.close();
    }

    IdentificationParameters.saveIdentificationParameters(
        identificationParameters, identificationParametersFile);
  }
 /**
  * Returns the component of the section corresponding to the given feature.
  *
  * @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 peptideMatch the peptide match
  * @param psParameter the PeptideShaker parameters of the match
  * @param peptideFeature the peptide feature to export
  * @param validatedOnly whether only validated matches should be exported
  * @param decoys whether decoy matches should be exported as well
  * @param waitingHandler the waiting handler
  * @return the component of the section corresponding to the given feature
  * @throws IOException exception thrown whenever an error occurred while interacting with a file
  *     while mapping potential modification sites
  * @throws InterruptedException exception thrown whenever a threading issue occurred while mapping
  *     potential modification sites
  * @throws ClassNotFoundException exception thrown whenever an error occurred while deserializing
  *     an object from the ProteinTree
  * @throws SQLException exception thrown whenever an error occurred while interacting with the
  *     ProteinTree
  * @throws uk.ac.ebi.jmzml.xml.io.MzMLUnmarshallerException exception thrown whenever an error
  *     occurred while reading an mzML file
  */
 public static String getfeature(
     Identification identification,
     IdentificationFeaturesGenerator identificationFeaturesGenerator,
     IdentificationParameters identificationParameters,
     ArrayList<String> keys,
     int nSurroundingAA,
     String linePrefix,
     PeptideMatch peptideMatch,
     PSParameter psParameter,
     PsPeptideFeature peptideFeature,
     boolean validatedOnly,
     boolean decoys,
     WaitingHandler waitingHandler)
     throws IOException, IllegalArgumentException, SQLException, ClassNotFoundException,
         InterruptedException, MzMLUnmarshallerException {
   switch (peptideFeature) {
     case accessions:
       StringBuilder proteins = new StringBuilder();
       ArrayList<String> accessions =
           peptideMatch
               .getTheoreticPeptide()
               .getParentProteins(identificationParameters.getSequenceMatchingPreferences());
       Collections.sort(accessions);
       for (String accession : accessions) {
         if (proteins.length() > 0) {
           proteins.append("; ");
         }
         proteins.append(accession);
       }
       return proteins.toString();
     case protein_description:
       SequenceFactory sequenceFactory = SequenceFactory.getInstance();
       StringBuilder descriptions = new StringBuilder();
       accessions =
           peptideMatch
               .getTheoreticPeptide()
               .getParentProteins(identificationParameters.getSequenceMatchingPreferences());
       Collections.sort(accessions);
       for (String accession : accessions) {
         if (descriptions.length() > 0) {
           descriptions.append("; ");
         }
         descriptions.append(sequenceFactory.getHeader(accession).getDescription());
       }
       return descriptions.toString();
     case protein_groups:
       HashSet<String> proteinGroups =
           identification.getProteinMatches(peptideMatch.getTheoreticPeptide());
       proteins = new StringBuilder();
       ArrayList<String> proteinGroupsList = new ArrayList<String>(proteinGroups);
       Collections.sort(proteinGroupsList);
       if (proteinGroupsList.size() > 1) {
         identification.loadProteinMatchParameters(
             proteinGroupsList, psParameter, waitingHandler, false);
       }
       psParameter = new PSParameter();
       for (String proteinGroup : proteinGroupsList) {
         if (identification.getProteinIdentification().contains(proteinGroup)) {
           psParameter =
               (PSParameter) identification.getProteinMatchParameter(proteinGroup, psParameter);
           if (proteins.length() > 0) {
             proteins.append("; ");
           }
           List<String> groupAccessions = Arrays.asList(ProteinMatch.getAccessions(proteinGroup));
           Collections.sort(groupAccessions);
           boolean first = true;
           for (String accession : groupAccessions) {
             if (first) {
               first = false;
             } else {
               proteins.append(", ");
             }
             proteins.append(accession);
           }
           proteins.append(" (");
           proteins.append(psParameter.getMatchValidationLevel().getName());
           proteins.append(")");
         }
       }
       return proteins.toString();
     case best_protein_group_validation:
       MatchValidationLevel bestProteinValidationLevel = MatchValidationLevel.none;
       proteinGroups = identification.getProteinMatches(peptideMatch.getTheoreticPeptide());
       proteinGroupsList = new ArrayList<String>(proteinGroups);
       Collections.sort(proteinGroupsList);
       if (proteinGroupsList.size() > 1) {
         identification.loadProteinMatchParameters(
             proteinGroupsList, psParameter, waitingHandler, false);
       }
       psParameter = new PSParameter();
       for (String proteinGroup : proteinGroupsList) {
         if (identification.getProteinIdentification().contains(proteinGroup)) {
           psParameter =
               (PSParameter) identification.getProteinMatchParameter(proteinGroup, psParameter);
           if (psParameter.getMatchValidationLevel().getIndex()
               > bestProteinValidationLevel.getIndex()) {
             bestProteinValidationLevel = psParameter.getMatchValidationLevel();
           }
         }
       }
       return bestProteinValidationLevel.getName();
     case confidence:
       return psParameter.getPeptideConfidence() + "";
     case decoy:
       if (peptideMatch
           .getTheoreticPeptide()
           .isDecoy(identificationParameters.getSequenceMatchingPreferences())) {
         return "1";
       } else {
         return "0";
       }
     case hidden:
       if (psParameter.isHidden()) {
         return "1";
       } else {
         return "0";
       }
     case localization_confidence:
       return getPeptideModificationLocations(
               peptideMatch, identificationParameters.getSearchParameters().getPtmSettings())
           + "";
     case pi:
       return psParameter.getProteinInferenceClassAsString();
     case position:
       accessions =
           peptideMatch
               .getTheoreticPeptide()
               .getParentProteins(identificationParameters.getSequenceMatchingPreferences());
       Collections.sort(accessions);
       Peptide peptide = peptideMatch.getTheoreticPeptide();
       String start = "";
       for (String proteinAccession : accessions) {
         if (!start.equals("")) {
           start += "; ";
         }
         Protein protein = SequenceFactory.getInstance().getProtein(proteinAccession);
         ArrayList<Integer> starts =
             protein.getPeptideStart(
                 peptide.getSequence(), identificationParameters.getSequenceMatchingPreferences());
         Collections.sort(starts);
         boolean first = true;
         for (int startAa : starts) {
           if (first) {
             first = false;
           } else {
             start += ", ";
           }
           start += startAa;
         }
       }
       return start;
     case psms:
       return peptideMatch.getSpectrumCount() + "";
     case variable_ptms:
       return Peptide.getPeptideModificationsAsString(peptideMatch.getTheoreticPeptide(), true);
     case fixed_ptms:
       return Peptide.getPeptideModificationsAsString(peptideMatch.getTheoreticPeptide(), false);
     case score:
       return psParameter.getPeptideScore() + "";
     case raw_score:
       return psParameter.getPeptideProbabilityScore() + "";
     case sequence:
       return peptideMatch.getTheoreticPeptide().getSequence();
     case missed_cleavages:
       peptide = peptideMatch.getTheoreticPeptide();
       Integer nMissedCleavages =
           peptide.getNMissedCleavages(
               identificationParameters.getSearchParameters().getDigestionPreferences());
       if (nMissedCleavages == null) {
         nMissedCleavages = 0;
       }
       return nMissedCleavages + "";
     case modified_sequence:
       return peptideMatch
           .getTheoreticPeptide()
           .getTaggedModifiedSequence(
               identificationParameters.getSearchParameters().getPtmSettings(),
               false,
               false,
               true);
     case starred:
       if (psParameter.isStarred()) {
         return "1";
       } else {
         return "0";
       }
     case aaBefore:
       peptide = peptideMatch.getTheoreticPeptide();
       accessions =
           peptide.getParentProteins(identificationParameters.getSequenceMatchingPreferences());
       Collections.sort(accessions);
       String subSequence = "";
       for (String proteinAccession : accessions) {
         if (!subSequence.equals("")) {
           subSequence += "; ";
         }
         HashMap<Integer, String[]> surroundingAAs =
             SequenceFactory.getInstance()
                 .getProtein(proteinAccession)
                 .getSurroundingAA(
                     peptide.getSequence(),
                     nSurroundingAA,
                     identificationParameters.getSequenceMatchingPreferences());
         ArrayList<Integer> starts = new ArrayList<Integer>(surroundingAAs.keySet());
         Collections.sort(starts);
         boolean first = true;
         for (int startAa : starts) {
           if (first) {
             first = false;
           } else {
             subSequence += ", ";
           }
           subSequence += surroundingAAs.get(startAa)[0];
         }
       }
       return subSequence;
     case aaAfter:
       peptide = peptideMatch.getTheoreticPeptide();
       accessions =
           peptide.getParentProteins(identificationParameters.getSequenceMatchingPreferences());
       Collections.sort(accessions);
       subSequence = "";
       for (String proteinAccession : accessions) {
         if (!subSequence.equals("")) {
           subSequence += "; ";
         }
         HashMap<Integer, String[]> surroundingAAs =
             SequenceFactory.getInstance()
                 .getProtein(proteinAccession)
                 .getSurroundingAA(
                     peptide.getSequence(),
                     nSurroundingAA,
                     identificationParameters.getSequenceMatchingPreferences());
         ArrayList<Integer> starts = new ArrayList<Integer>(surroundingAAs.keySet());
         Collections.sort(starts);
         boolean first = true;
         for (int startAa : starts) {
           if (first) {
             first = false;
           } else {
             subSequence += ", ";
           }
           subSequence += surroundingAAs.get(startAa)[1];
         }
       }
       return subSequence;
     case nValidatedProteinGroups:
       peptide = peptideMatch.getTheoreticPeptide();
       return identificationFeaturesGenerator.getNValidatedProteinGroups(peptide, waitingHandler)
           + "";
     case unique_database:
       peptide = peptideMatch.getTheoreticPeptide();
       if (identification.isUniqueInDatabase(peptide)) {
         return "1";
       } else {
         return "0";
       }
     case validated:
       return psParameter.getMatchValidationLevel().toString();
     case validated_psms:
       return identificationFeaturesGenerator.getNValidatedSpectraForPeptide(peptideMatch.getKey())
           + "";
     case probabilistic_score:
       PSPtmScores ptmScores = new PSPtmScores();
       ptmScores = (PSPtmScores) peptideMatch.getUrParam(ptmScores);
       if (ptmScores != null) {
         StringBuilder result = new StringBuilder();
         ArrayList<String> modList = new ArrayList<String>(ptmScores.getScoredPTMs());
         Collections.sort(modList);
         for (String mod : modList) {
           PtmScoring ptmScoring = ptmScores.getPtmScoring(mod);
           ArrayList<Integer> sites = new ArrayList<Integer>(ptmScoring.getProbabilisticSites());
           if (!sites.isEmpty()) {
             Collections.sort(sites);
             if (result.length() > 0) {
               result.append(", ");
             }
             result.append(mod).append(" (");
             boolean firstSite = true;
             for (int site : sites) {
               if (firstSite) {
                 firstSite = false;
               } else {
                 result.append(", ");
               }
               result.append(site).append(": ").append(ptmScoring.getProbabilisticScore(site));
             }
             result.append(")");
           }
         }
         return result.toString();
       }
       return "";
     case d_score:
       StringBuilder result = new StringBuilder();
       ptmScores = new PSPtmScores();
       ptmScores = (PSPtmScores) peptideMatch.getUrParam(ptmScores);
       if (ptmScores != null) {
         ArrayList<String> modList = new ArrayList<String>(ptmScores.getScoredPTMs());
         Collections.sort(modList);
         for (String mod : modList) {
           PtmScoring ptmScoring = ptmScores.getPtmScoring(mod);
           ArrayList<Integer> sites = new ArrayList<Integer>(ptmScoring.getDSites());
           if (!sites.isEmpty()) {
             Collections.sort(sites);
             if (result.length() > 0) {
               result.append(", ");
             }
             result.append(mod).append(" (");
             boolean firstSite = true;
             for (int site : sites) {
               if (firstSite) {
                 firstSite = false;
               } else {
                 result.append(", ");
               }
               result.append(site).append(": ").append(ptmScoring.getDeltaScore(site));
             }
             result.append(")");
           }
         }
         return result.toString();
       }
       return "";
     case confident_modification_sites:
       String sequence = peptideMatch.getTheoreticPeptide().getSequence();
       return identificationFeaturesGenerator.getConfidentPtmSites(peptideMatch, sequence);
     case confident_modification_sites_number:
       return identificationFeaturesGenerator.getConfidentPtmSitesNumber(peptideMatch);
     case ambiguous_modification_sites:
       sequence = peptideMatch.getTheoreticPeptide().getSequence();
       return identificationFeaturesGenerator.getAmbiguousPtmSites(peptideMatch, sequence);
     case ambiguous_modification_sites_number:
       return identificationFeaturesGenerator.getAmbiguousPtmSiteNumber(peptideMatch);
     case confident_phosphosites:
       ArrayList<String> modifications = new ArrayList<String>();
       for (String ptm :
           identificationParameters
               .getSearchParameters()
               .getPtmSettings()
               .getAllNotFixedModifications()) {
         if (ptm.contains("Phospho")) {
           modifications.add(ptm);
         }
       }
       return identificationFeaturesGenerator.getConfidentPtmSites(
           peptideMatch, peptideMatch.getTheoreticPeptide().getSequence(), modifications);
     case confident_phosphosites_number:
       modifications = new ArrayList<String>();
       for (String ptm :
           identificationParameters
               .getSearchParameters()
               .getPtmSettings()
               .getAllNotFixedModifications()) {
         if (ptm.contains("Phospho")) {
           modifications.add(ptm);
         }
       }
       return identificationFeaturesGenerator.getConfidentPtmSitesNumber(
           peptideMatch, modifications);
     case ambiguous_phosphosites:
       modifications = new ArrayList<String>();
       for (String ptm :
           identificationParameters
               .getSearchParameters()
               .getPtmSettings()
               .getAllNotFixedModifications()) {
         if (ptm.contains("Phospho")) {
           modifications.add(ptm);
         }
       }
       return identificationFeaturesGenerator.getAmbiguousPtmSites(
           peptideMatch, peptideMatch.getTheoreticPeptide().getSequence(), modifications);
     case ambiguous_phosphosites_number:
       modifications = new ArrayList<String>();
       for (String ptm :
           identificationParameters
               .getSearchParameters()
               .getPtmSettings()
               .getAllNotFixedModifications()) {
         if (ptm.contains("Phospho")) {
           modifications.add(ptm);
         }
       }
       return identificationFeaturesGenerator.getAmbiguousPtmSiteNumber(
           peptideMatch, modifications);
     default:
       return "Not implemented";
   }
 }
  /**
   * Selects a first hit in a list of equally scoring peptide matches. The selection is made based
   * on: 1 - The occurrence of the protein detection as given in the proteinCount map 2 - The
   * sequence coverage by fragment ions 3 - The precursor mass error.
   *
   * <p>If no best hit is found, the first one sorted alphabetically is retained.
   *
   * @param spectrumKey the key of the spectrum
   * @param firstHits list of equally scoring peptide matches
   * @param proteinCount map of the number of peptides for every protein
   * @param sequenceMatchingPreferences the sequence matching preferences
   * @param shotgunProtocol the shotgun protocol
   * @param identificationParameters the identification parameters
   * @param spectrumAnnotator the spectrum annotator to use
   * @return a first hit from the list of equally scoring peptide matches
   * @throws IOException exception thrown whenever an IO exception occurred while remapping the
   *     proteins or getting the spectrum
   * @throws InterruptedException exception thrown whenever an interrupted exception occurred while
   *     remapping the proteins or getting the spectrum
   * @throws SQLException exception thrown whenever an SQL exception occurred while interacting with
   *     the protein tree
   * @throws ClassNotFoundException exception thrown whenever an exception occurred while
   *     deserializing an object
   * @throws MzMLUnmarshallerException exception thrown whenever an exception occurred while reading
   *     an mzML file
   */
  public static PeptideAssumption getBestHit(
      String spectrumKey,
      ArrayList<PeptideAssumption> firstHits,
      HashMap<String, Integer> proteinCount,
      SequenceMatchingPreferences sequenceMatchingPreferences,
      ShotgunProtocol shotgunProtocol,
      IdentificationParameters identificationParameters,
      PeptideSpectrumAnnotator spectrumAnnotator)
      throws IOException, InterruptedException, SQLException, ClassNotFoundException,
          MzMLUnmarshallerException {

    if (firstHits.size() == 1) {
      return firstHits.get(0);
    }

    Integer maxProteins = 0;
    ArrayList<PeptideAssumption> bestPeptideAssumptions =
        new ArrayList<PeptideAssumption>(firstHits.size());

    for (PeptideAssumption peptideAssumption : firstHits) {
      for (String accession :
          peptideAssumption.getPeptide().getParentProteins(sequenceMatchingPreferences)) {
        Integer count = proteinCount.get(accession);
        if (count != null) {
          if (count > maxProteins) {
            maxProteins = count;
            bestPeptideAssumptions.clear();
            bestPeptideAssumptions.add(peptideAssumption);
          } else if (count.equals(maxProteins)) {
            bestPeptideAssumptions.add(peptideAssumption);
          }
        }
      }
    }

    if (bestPeptideAssumptions.size() == 1) {
      return bestPeptideAssumptions.get(0);
    } else if (!bestPeptideAssumptions.isEmpty()) {
      firstHits = bestPeptideAssumptions;
      bestPeptideAssumptions = new ArrayList<PeptideAssumption>(firstHits.size());
    }

    MSnSpectrum spectrum = (MSnSpectrum) SpectrumFactory.getInstance().getSpectrum(spectrumKey);
    int maxCoveredAminoAcids = 0;
    AnnotationSettings annotationPreferences = identificationParameters.getAnnotationPreferences();

    for (PeptideAssumption peptideAssumption : firstHits) {
      Peptide peptide = peptideAssumption.getPeptide();
      SpecificAnnotationSettings specificAnnotationPreferences =
          annotationPreferences.getSpecificAnnotationPreferences(
              spectrum.getSpectrumKey(),
              peptideAssumption,
              identificationParameters.getSequenceMatchingPreferences(),
              identificationParameters.getPtmScoringPreferences().getSequenceMatchingPreferences());
      HashMap<Integer, ArrayList<IonMatch>> coveredAminoAcids =
          spectrumAnnotator.getCoveredAminoAcids(
              annotationPreferences,
              specificAnnotationPreferences,
              (MSnSpectrum) spectrum,
              peptide);
      int nAas = coveredAminoAcids.size();
      if (nAas > maxCoveredAminoAcids) {
        maxCoveredAminoAcids = nAas;
        bestPeptideAssumptions.clear();
        bestPeptideAssumptions.add(peptideAssumption);
      } else if (nAas == maxCoveredAminoAcids) {
        bestPeptideAssumptions.add(peptideAssumption);
      }
    }

    if (bestPeptideAssumptions.size() == 1) {
      return bestPeptideAssumptions.get(0);
    } else if (!bestPeptideAssumptions.isEmpty()) {
      firstHits = bestPeptideAssumptions;
      bestPeptideAssumptions = new ArrayList<PeptideAssumption>(firstHits.size());
    }

    double minMassError = identificationParameters.getPeptideAssumptionFilter().getMaxMzDeviation();
    if (minMassError == -1.0) {
      minMassError = identificationParameters.getSearchParameters().getPrecursorAccuracy();
    }

    for (PeptideAssumption peptideAssumption : firstHits) {
      double massError =
          Math.abs(
              peptideAssumption.getDeltaMass(
                  spectrum.getPrecursor().getMz(), shotgunProtocol.isMs1ResolutionPpm()));
      if (massError < minMassError) {
        minMassError = massError;
        bestPeptideAssumptions.clear();
        bestPeptideAssumptions.add(peptideAssumption);
      } else if (massError == minMassError) {
        bestPeptideAssumptions.add(peptideAssumption);
      }
    }

    if (bestPeptideAssumptions.size() == 1) {
      return bestPeptideAssumptions.get(0);
    } else if (bestPeptideAssumptions.isEmpty()) {
      bestPeptideAssumptions = firstHits;
    }

    HashMap<String, PeptideAssumption> sequenceToPeptideAssumptionsMap =
        new HashMap<String, PeptideAssumption>(bestPeptideAssumptions.size());
    for (PeptideAssumption peptideAssumption : bestPeptideAssumptions) {
      sequenceToPeptideAssumptionsMap.put(
          peptideAssumption.getPeptide().getSequence(), peptideAssumption);
    }

    ArrayList<String> sequences = new ArrayList<String>(sequenceToPeptideAssumptionsMap.keySet());
    Collections.sort(sequences);
    return sequenceToPeptideAssumptionsMap.get(sequences.get(0));
  }
  /**
   * Indicates whether the match designated by the match key validates the given item using the
   * given comparator and value threshold.
   *
   * @param itemName the name of the item to filter on
   * @param filterItemComparator the comparator to use
   * @param value the value to use as a threshold
   * @param spectrumKey the key of the match of interest
   * @param peptideAssumption the assumption to validate
   * @param identification the identification objects where to get identification matches from
   * @param identificationFeaturesGenerator the identification feature generator where to get
   *     identification features
   * @param identificationParameters the identification parameters used
   * @param peptideSpectrumAnnotator the annotator to use to annotate spectra when filtering on PSM
   *     or assumptions
   * @return a boolean indicating whether the match designated by the protein key validates the
   *     given item using the given comparator and value threshold.
   * @throws java.io.IOException exception thrown whenever an exception occurred while reading or
   *     writing a file
   * @throws java.lang.InterruptedException exception thrown whenever a threading issue occurred
   *     while validating that the match passes the filter
   * @throws java.lang.ClassNotFoundException exception thrown whenever an error occurred while
   *     deserilalizing a match
   * @throws java.sql.SQLException exception thrown whenever an error occurred while interacting
   *     with a database
   * @throws uk.ac.ebi.jmzml.xml.io.MzMLUnmarshallerException exception thrown whenever an error
   *     occurred while reading an mzML file
   * @throws org.apache.commons.math.MathException exception thrown whenever an error occurred while
   *     doing statistics on a distribution
   */
  public boolean isValidated(
      String itemName,
      FilterItemComparator filterItemComparator,
      Object value,
      String spectrumKey,
      PeptideAssumption peptideAssumption,
      Identification identification,
      IdentificationFeaturesGenerator identificationFeaturesGenerator,
      IdentificationParameters identificationParameters,
      PeptideSpectrumAnnotator peptideSpectrumAnnotator)
      throws IOException, InterruptedException, ClassNotFoundException, SQLException,
          MzMLUnmarshallerException, MathException {

    AssumptionFilterItem filterItem = AssumptionFilterItem.getItem(itemName);
    if (filterItem == null) {
      throw new IllegalArgumentException(
          "Filter item " + itemName + "not recognized as spectrum assumption filter item.");
    }
    String input = value.toString();
    switch (filterItem) {
      case precrusorMz:
        Precursor precursor = SpectrumFactory.getInstance().getPrecursor(spectrumKey);
        Double mz = precursor.getMz();
        return filterItemComparator.passes(input, mz.toString());
      case precrusorRT:
        precursor = SpectrumFactory.getInstance().getPrecursor(spectrumKey);
        Double rt = precursor.getRt();
        return filterItemComparator.passes(input, rt.toString());
      case precrusorCharge:
        Integer charge = peptideAssumption.getIdentificationCharge().value;
        return filterItemComparator.passes(input, charge.toString());
      case precrusorMzErrorDa:
        precursor = SpectrumFactory.getInstance().getPrecursor(spectrumKey);
        SearchParameters searchParameters = identificationParameters.getSearchParameters();
        Double mzError =
            Math.abs(
                peptideAssumption.getDeltaMass(
                    precursor.getMz(),
                    false,
                    searchParameters.getMinIsotopicCorrection(),
                    searchParameters.getMaxIsotopicCorrection()));
        return filterItemComparator.passes(input, mzError.toString());
      case precrusorMzErrorPpm:
        searchParameters = identificationParameters.getSearchParameters();
        precursor = SpectrumFactory.getInstance().getPrecursor(spectrumKey);
        mzError =
            Math.abs(
                peptideAssumption.getDeltaMass(
                    precursor.getMz(),
                    true,
                    searchParameters.getMinIsotopicCorrection(),
                    searchParameters.getMaxIsotopicCorrection()));
        return filterItemComparator.passes(input, mzError.toString());
      case precrusorMzErrorStat:
        searchParameters = identificationParameters.getSearchParameters();
        precursor = SpectrumFactory.getInstance().getPrecursor(spectrumKey);
        mzError =
            peptideAssumption.getDeltaMass(
                precursor.getMz(),
                identificationParameters.getSearchParameters().isPrecursorAccuracyTypePpm(),
                searchParameters.getMinIsotopicCorrection(),
                searchParameters.getMaxIsotopicCorrection());
        NonSymmetricalNormalDistribution precDeviationDistribution =
            identificationFeaturesGenerator.getMassErrorDistribution(
                Spectrum.getSpectrumFile(spectrumKey));
        Double p;
        if (mzError > precDeviationDistribution.getMean()) {
          p = precDeviationDistribution.getDescendingCumulativeProbabilityAt(mzError);
        } else {
          p = precDeviationDistribution.getCumulativeProbabilityAt(mzError);
        }
        return filterItemComparator.passes(input, p.toString());
      case sequenceCoverage:
        SpectrumFactory spectrumFactory = SpectrumFactory.getInstance();
        MSnSpectrum spectrum = (MSnSpectrum) spectrumFactory.getSpectrum(spectrumKey);
        Peptide peptide = peptideAssumption.getPeptide();
        AnnotationSettings annotationPreferences =
            identificationParameters.getAnnotationPreferences();
        SpecificAnnotationSettings specificAnnotationPreferences =
            annotationPreferences.getSpecificAnnotationPreferences(
                spectrum.getSpectrumKey(),
                peptideAssumption,
                identificationParameters.getSequenceMatchingPreferences(),
                identificationParameters
                    .getPtmScoringPreferences()
                    .getSequenceMatchingPreferences());
        HashMap<Integer, ArrayList<IonMatch>> matches =
            peptideSpectrumAnnotator.getCoveredAminoAcids(
                annotationPreferences,
                specificAnnotationPreferences,
                (MSnSpectrum) spectrum,
                peptide);
        double nCovered = 0;
        int nAA = peptide.getSequence().length();
        for (int i = 0; i <= nAA; i++) {
          ArrayList<IonMatch> matchesAtAa = matches.get(i);
          if (matchesAtAa != null && !matchesAtAa.isEmpty()) {
            nCovered++;
          }
        }
        Double coverage = 100.0 * nCovered / nAA;
        return filterItemComparator.passes(input, coverage.toString());
      case algorithmScore:
        Double score = peptideAssumption.getRawScore();
        if (score == null) {
          score = peptideAssumption.getScore();
        }
        return filterItemComparator.passes(input, score.toString());
      case fileNames:
        return filterItemComparator.passes(input, Spectrum.getSpectrumFile(spectrumKey));
      case confidence:
        PSParameter psParameter = new PSParameter();
        psParameter =
            (PSParameter) identification.getPeptideMatchParameter(spectrumKey, psParameter);
        Double confidence = psParameter.getProteinConfidence();
        return filterItemComparator.passes(input, confidence.toString());
      case validationStatus:
        psParameter = new PSParameter();
        psParameter =
            (PSParameter) identification.getPeptideMatchParameter(spectrumKey, psParameter);
        Integer validation = psParameter.getMatchValidationLevel().getIndex();
        return filterItemComparator.passes(input, validation.toString());
      case stared:
        psParameter = new PSParameter();
        psParameter =
            (PSParameter) identification.getPeptideMatchParameter(spectrumKey, psParameter);
        String starred;
        if (psParameter.isStarred()) {
          starred = FilterItemComparator.trueFalse[0];
        } else {
          starred = FilterItemComparator.trueFalse[1];
        }
        return filterItemComparator.passes(input, starred);
      default:
        throw new IllegalArgumentException(
            "Protein filter not implemented for item " + filterItem.name + ".");
    }
  }