private void attachProbabilisticScore(Identification identification, SpectrumMatch spectrumMatch, SearchParameters searchParameters,
AnnotationPreferences annotationPreferences, PTMScoringPreferences scoringPreferences, SequenceMatchingPreferences sequenceMatchingPreferences) throws Exception {
ModificationProfile ptmProfile = searchParameters.getModificationProfile();
PSPtmScores ptmScores = new PSPtmScores();
if (spectrumMatch.getUrParam(new PSPtmScores()) != null) {
ptmScores = (PSPtmScores) spectrumMatch.getUrParam(new PSPtmScores());
}
HashMap<Double, ArrayList<PTM>> modifications = new HashMap<Double, ArrayList<PTM>>();
HashMap<Double, Integer> nMod = new HashMap<Double, Integer>();
HashMap<Double, ModificationMatch> modificationMatches = new HashMap<Double, ModificationMatch>();
Peptide peptide = spectrumMatch.getBestPeptideAssumption().getPeptide();
for (ModificationMatch modificationMatch : peptide.getModificationMatches()) {
if (modificationMatch.isVariable()) {
PTM refPTM = ptmFactory.getPTM(modificationMatch.getTheoreticPtm());
double ptmMass = refPTM.getMass();
if (!modifications.containsKey(ptmMass)) {
ArrayList<PTM> ptms = new ArrayList<PTM>();
for (String ptm : ptmProfile.getSimilarNotFixedModifications(ptmMass)) {
ptms.add(ptmFactory.getPTM(ptm));
}
modifications.put(ptmMass, ptms);
nMod.put(ptmMass, 1);
} else {
nMod.put(ptmMass, nMod.get(ptmMass) + 1);
}
modificationMatches.put(ptmMass, modificationMatch);
}
}
if (!modifications.isEmpty()) {
MSnSpectrum spectrum = (MSnSpectrum) spectrumFactory.getSpectrum(spectrumMatch.getKey());
annotationPreferences.setCurrentSettings(spectrumMatch.getBestPeptideAssumption(), true, sequenceMatchingPreferences);
for (Double ptmMass : modifications.keySet()) {
HashMap<Integer, Double> scores = null;
if (scoringPreferences.getSelectedProbabilisticScore() == PtmScore.AScore && nMod.get(ptmMass) == 1) {
scores = AScore.getAScore(peptide, modifications.get(ptmMass), spectrum, annotationPreferences.getIonTypes(),
annotationPreferences.getNeutralLosses(), annotationPreferences.getValidatedCharges(),
spectrumMatch.getBestPeptideAssumption().getIdentificationCharge().value,
searchParameters.getFragmentIonAccuracy(), scoringPreferences.isProbabilisticScoreNeutralLosses(), sequenceMatchingPreferences, peptideSpectrumAnnotator, roundingDecimal);
} else if (scoringPreferences.getSelectedProbabilisticScore() == PtmScore.PhosphoRS) {
scores = PhosphoRS.getSequenceProbabilities(peptide, modifications.get(ptmMass), spectrum, annotationPreferences.getIonTypes(),
annotationPreferences.getNeutralLosses(), annotationPreferences.getValidatedCharges(),
spectrumMatch.getBestPeptideAssumption().getIdentificationCharge().value,
searchParameters.getFragmentIonAccuracy(), scoringPreferences.isProbabilisticScoreNeutralLosses(),
sequenceMatchingPreferences, peptideSpectrumAnnotator, roundingDecimal);
}
if (scores != null) {
// remap to searched PTMs
PTM mappedModification = null;
String peptideSequence = peptide.getSequence();
for (int site : scores.keySet()) {
if (site == 0) {
// N-term ptm
for (PTM ptm : modifications.get(ptmMass)) {
if (ptm.isNTerm() && peptide.getPotentialModificationSites(ptm, sequenceMatchingPreferences).contains(1)) {
mappedModification = ptm;
break;
}
}
if (mappedModification == null) {
throw new IllegalArgumentException("Could not map the PTM of mass " + ptmMass + " on the N-terminus of the peptide " + peptideSequence + ".");
}
} else if (site == peptideSequence.length() + 1) {
// C-term ptm
for (PTM ptm : modifications.get(ptmMass)) {
if (ptm.isCTerm() && peptide.getPotentialModificationSites(ptm, sequenceMatchingPreferences).contains(peptideSequence.length())) {
mappedModification = ptm;
break;
}
}
if (mappedModification == null) {
throw new IllegalArgumentException("Could not map the PTM of mass " + ptmMass + " on the C-terminus of the peptide " + peptideSequence + ".");
}
} else {
for (PTM ptm : modifications.get(ptmMass)) {
if (peptide.getPotentialModificationSites(ptm, sequenceMatchingPreferences).contains(site)) {
mappedModification = ptm;
break;
}
}
if (mappedModification == null) {
throw new IllegalArgumentException("Could not map the PTM of mass " + ptmMass + " at site " + site + " in peptide " + peptide.getSequence() + ".");
}
}
String ptmName = mappedModification.getName();
PtmScoring ptmScoring = ptmScores.getPtmScoring(ptmName);
if (ptmScoring == null) {
ptmScoring = new PtmScoring(ptmName);
ptmScores.addPtmScoring(ptmName, ptmScoring);
}
ptmScoring.setProbabilisticScore(site, scores.get(site));
}
}