HashMap<Double, ArrayList<TagAssumption>> tagAssumptions = new HashMap<Double, ArrayList<TagAssumption>>();
PSParameter psParameter = new PSParameter();
ArrayList<String> identifications = new ArrayList<String>();
SpectrumMatch spectrumMatch = identification.getSpectrumMatch(spectrumKey);
for (int searchEngine1 : spectrumMatch.getAdvocates()) {
double bestEvalue = Collections.min(spectrumMatch.getAllAssumptions(searchEngine1).keySet());
for (SpectrumIdentificationAssumption assumption1 : spectrumMatch.getAllAssumptions(searchEngine1).get(bestEvalue)) {
if (assumption1 instanceof PeptideAssumption) {
PeptideAssumption peptideAssumption1 = (PeptideAssumption) assumption1;
String id = peptideAssumption1.getPeptide().getKey();
if (!identifications.contains(id)) {
psParameter = (PSParameter) peptideAssumption1.getUrParam(psParameter);
double p;
if (multiSE && sequenceFactory.concatenatedTargetDecoy()) {
p = psParameter.getSearchEngineProbability();
} else {
p = peptideAssumption1.getScore();
}
int nSE = 1;
int proteinMax = 1;
for (String protein : peptideAssumption1.getPeptide().getParentProteins(sequenceMatchingPreferences)) {
Integer tempCount = proteinCount.get(protein);
if (tempCount != null && tempCount > proteinMax) {
proteinMax = tempCount;
}
}
for (int searchEngine2 : spectrumMatch.getAdvocates()) {
if (searchEngine1 != searchEngine2) {
boolean found = false;
ArrayList<Double> eValues2 = new ArrayList<Double>(spectrumMatch.getAllAssumptions(searchEngine2).keySet());
Collections.sort(eValues2);
for (double eValue2 : eValues2) {
for (SpectrumIdentificationAssumption assumption2 : spectrumMatch.getAllAssumptions(searchEngine2).get(eValue2)) {
if (assumption2 instanceof PeptideAssumption) {
PeptideAssumption peptideAssumption2 = (PeptideAssumption) assumption2;
if (peptideAssumption1.getPeptide().isSameSequenceAndModificationStatus(peptideAssumption2.getPeptide(),
sequenceMatchingPreferences)) {
PSParameter psParameter2 = (PSParameter) peptideAssumption2.getUrParam(psParameter);
p = p * psParameter2.getSearchEngineProbability();
nSE++;
found = true;
break;
}
}
}
if (found) {
break;
}
}
}
}
identifications.add(id);
if (!peptideAssumptions.containsKey(p)) {
peptideAssumptions.put(p, new HashMap<Integer, HashMap<Integer, HashMap<Double, HashMap<Double, ArrayList<PeptideAssumption>>>>>());
}
if (!peptideAssumptions.get(p).containsKey(proteinMax)) {
peptideAssumptions.get(p).put(proteinMax, new HashMap<Integer, HashMap<Double, HashMap<Double, ArrayList<PeptideAssumption>>>>());
}
if (!peptideAssumptions.get(p).get(proteinMax).containsKey(nSE)) {
ArrayList<PeptideAssumption> assumptions = new ArrayList<PeptideAssumption>();
assumptions.add(peptideAssumption1);
peptideAssumptions.get(p).get(proteinMax).put(nSE, new HashMap<Double, HashMap<Double, ArrayList<PeptideAssumption>>>());
peptideAssumptions.get(p).get(proteinMax).get(nSE).put(-1.0, new HashMap<Double, ArrayList<PeptideAssumption>>());
peptideAssumptions.get(p).get(proteinMax).get(nSE).get(-1.0).put(-1.0, assumptions);
} else {
HashMap<Ion.IonType, HashSet<Integer>> iontypes = annotationPreferences.getIonTypes();
NeutralLossesMap neutralLosses = annotationPreferences.getNeutralLosses();
ArrayList<Integer> charges = annotationPreferences.getValidatedCharges();
MSnSpectrum spectrum = (MSnSpectrum) spectrumFactory.getSpectrum(spectrumKey);
double mzTolerance = searchParameters.getFragmentIonAccuracy();
boolean isPpm = false; //@TODO change this as soon as search engine support fragment ion tolerance in ppm
if (peptideAssumptions.get(p).get(proteinMax).get(nSE).containsKey(-1.0)) {
ArrayList<PeptideAssumption> assumptions = peptideAssumptions.get(p).get(proteinMax).get(nSE).get(-1.0).get(-1.0);
PeptideAssumption tempAssumption = assumptions.get(0);
Peptide peptide = tempAssumption.getPeptide();
int precursorCharge = tempAssumption.getIdentificationCharge().value;
annotationPreferences.setCurrentSettings(tempAssumption, true, sequenceMatchingPreferences);
double nIons = spectrumAnnotator.getCoveredAminoAcids(iontypes, neutralLosses, charges, precursorCharge,
spectrum, peptide, 0, mzTolerance, isPpm, annotationPreferences.isHighResolutionAnnotation()).keySet().size();
double coverage = nIons / peptide.getSequence().length();
peptideAssumptions.get(p).get(proteinMax).get(nSE).put(coverage, new HashMap<Double, ArrayList<PeptideAssumption>>());
peptideAssumptions.get(p).get(proteinMax).get(nSE).get(coverage).put(-1.0, assumptions);
peptideAssumptions.get(p).get(proteinMax).get(nSE).remove(-1.0);
}
Peptide peptide = peptideAssumption1.getPeptide();
int precursorCharge = peptideAssumption1.getIdentificationCharge().value;
annotationPreferences.setCurrentSettings(peptideAssumption1, true, sequenceMatchingPreferences);
double nIons = spectrumAnnotator.getCoveredAminoAcids(iontypes, neutralLosses, charges, precursorCharge,
spectrum, peptide, 0, mzTolerance, isPpm, annotationPreferences.isHighResolutionAnnotation()).keySet().size();
double coverage = nIons / peptide.getSequence().length();
HashMap<Double, ArrayList<PeptideAssumption>> coverageMap = peptideAssumptions.get(p).get(proteinMax).get(nSE).get(coverage);
if (coverageMap == null) {
coverageMap = new HashMap<Double, ArrayList<PeptideAssumption>>();
ArrayList<PeptideAssumption> assumptions = new ArrayList<PeptideAssumption>();
assumptions.add(peptideAssumption1);
coverageMap.put(-1.0, assumptions);
peptideAssumptions.get(p).get(proteinMax).get(nSE).put(coverage, coverageMap);
} else {
ArrayList<PeptideAssumption> assumptions = coverageMap.get(-1.0);
if (assumptions != null) {
PeptideAssumption tempAssumption = assumptions.get(0);
double massError = Math.abs(tempAssumption.getDeltaMass(spectrum.getPrecursor().getMz(), searchParameters.isPrecursorAccuracyTypePpm()));
peptideAssumptions.get(p).get(proteinMax).get(nSE).get(coverage).put(massError, assumptions);
peptideAssumptions.get(p).get(proteinMax).get(nSE).get(coverage).remove(-1.0);
}
double massError = Math.abs(peptideAssumption1.getDeltaMass(spectrum.getPrecursor().getMz(), searchParameters.isPrecursorAccuracyTypePpm()));
assumptions = coverageMap.get(massError);
if (assumptions == null) {
assumptions = new ArrayList<PeptideAssumption>();
coverageMap.put(massError, assumptions);
}
assumptions.add(peptideAssumption1);
}
}
}
} else if (assumption1 instanceof TagAssumption) {
TagAssumption tagAssumption = (TagAssumption) assumption1;
ArrayList<TagAssumption> assumptions = tagAssumptions.get(bestEvalue);
if (assumptions == null) {
assumptions = new ArrayList<TagAssumption>();
tagAssumptions.put(bestEvalue, assumptions);
}
assumptions.add(tagAssumption);
}
}
}
if (!peptideAssumptions.isEmpty()) {
PeptideAssumption bestPeptideAssumption = null;
ArrayList<Double> ps = new ArrayList<Double>(peptideAssumptions.keySet());
Collections.sort(ps);
double retainedP = 0;
for (double p : ps) {
retainedP = p;
ArrayList<Integer> proteinMaxs = new ArrayList<Integer>(peptideAssumptions.get(p).keySet());
Collections.sort(proteinMaxs, Collections.reverseOrder());
for (int proteinMax : proteinMaxs) {
ArrayList<Integer> nSEs = new ArrayList<Integer>(peptideAssumptions.get(p).get(proteinMax).keySet());
Collections.sort(nSEs, Collections.reverseOrder());
for (int nSE : nSEs) {
ArrayList<Double> coverages = new ArrayList<Double>(peptideAssumptions.get(p).get(proteinMax).get(nSE).keySet());
Collections.sort(coverages);
for (double coverage : coverages) {
ArrayList<Double> minErrors = new ArrayList<Double>(peptideAssumptions.get(p).get(proteinMax).get(nSE).get(coverage).keySet());
Collections.sort(minErrors);
for (double minError : minErrors) {
for (PeptideAssumption peptideAssumption : peptideAssumptions.get(p).get(proteinMax).get(nSE).get(coverage).get(minError)) {
if (idFilter.validateProteins(peptideAssumption.getPeptide(), sequenceMatchingPreferences)) {
bestPeptideAssumption = peptideAssumption;
break;
}
}
if (bestPeptideAssumption != null) {
break;
}
}
if (bestPeptideAssumption != null) {
break;
}
}
if (bestPeptideAssumption != null) {
break;
}
}
if (bestPeptideAssumption != null) {
break;
}
}
if (bestPeptideAssumption != null) {
break;
}
}
if (bestPeptideAssumption != null) {
if (multiSE) {
// try to find the most likely modification localization based on the search engine results
HashMap<PeptideAssumption, ArrayList<Double>> assumptions = new HashMap<PeptideAssumption, ArrayList<Double>>();
String bestAssumptionKey = bestPeptideAssumption.getPeptide().getMatchingKey(sequenceMatchingPreferences);
for (int searchEngine1 : spectrumMatch.getAdvocates()) {
boolean found = false;
ArrayList<Double> eValues2 = new ArrayList<Double>(spectrumMatch.getAllAssumptions(searchEngine1).keySet());
Collections.sort(eValues2);
for (double eValue : eValues2) {
for (SpectrumIdentificationAssumption assumption : spectrumMatch.getAllAssumptions(searchEngine1).get(eValue)) {
if (assumption instanceof PeptideAssumption) {
PeptideAssumption peptideAssumption = (PeptideAssumption) assumption;
if (peptideAssumption.getPeptide().getMatchingKey(sequenceMatchingPreferences).equals(bestAssumptionKey)) {
found = true;
boolean found2 = false;
for (PeptideAssumption assumption1 : assumptions.keySet()) {
if (assumption1.getPeptide().sameModificationsAs(peptideAssumption.getPeptide())) {
found2 = true;
psParameter = (PSParameter) assumption.getUrParam(psParameter);
assumptions.get(assumption1).add(psParameter.getSearchEngineProbability());
break;
}
}
if (!found2) {
assumptions.put(peptideAssumption, new ArrayList<Double>());
psParameter = (PSParameter) assumption.getUrParam(psParameter);
assumptions.get(peptideAssumption).add(psParameter.getSearchEngineProbability());
}
}
}
}
if (found) {
break;
}
}
}
Double bestSeP = null;
int nSe = -1;
for (PeptideAssumption peptideAssumption : assumptions.keySet()) {
Double sep = Collections.min(assumptions.get(peptideAssumption));
if (bestSeP == null || bestSeP > sep) {
bestSeP = sep;
nSe = assumptions.get(peptideAssumption).size();
bestPeptideAssumption = peptideAssumption;
} else if (assumptions.get(peptideAssumption).size() > nSe) {
if (sep != null && bestSeP != null && (Math.abs(sep - bestSeP) <= 1e-10)) {
nSe = assumptions.get(peptideAssumption).size();
bestPeptideAssumption = peptideAssumption;
}
}
}
}
// create a PeptideShaker match based on the best search engine match
Peptide sePeptide = bestPeptideAssumption.getPeptide();
ArrayList<String> psProteins = new ArrayList<String>(sePeptide.getParentProteins(sequenceMatchingPreferences));
ArrayList<ModificationMatch> psModificationMatches = new ArrayList<ModificationMatch>();
for (ModificationMatch seModMatch : sePeptide.getModificationMatches()) {
psModificationMatches.add(new ModificationMatch(seModMatch.getTheoreticPtm(), seModMatch.isVariable(), seModMatch.getModificationSite()));
}
Peptide psPeptide = new Peptide(sePeptide.getSequence(), psModificationMatches);
psPeptide.setParentProteins(psProteins);
PeptideAssumption psAssumption = new PeptideAssumption(psPeptide, 1, Advocate.peptideShaker.getIndex(), bestPeptideAssumption.getIdentificationCharge(), retainedP);
spectrumMatch.setBestPeptideAssumption(psAssumption);
if (orderedPsmMap != null) {
String peptideKey = psPeptide.getMatchingKey(sequenceMatchingPreferences);
ArrayList<String> spectrumKeys = keysMap.get(peptideKey);
if (spectrumKeys == null) {
spectrumKeys = new ArrayList<String>();
keysMap.put(peptideKey, spectrumKeys);
}
spectrumKeys.add(spectrumKey);
}
psParameter = new PSParameter();
psParameter.setSpectrumProbabilityScore(retainedP);
PSParameter matchParameter = (PSParameter) bestPeptideAssumption.getUrParam(psParameter);
psParameter.setSearchEngineProbability(matchParameter.getSearchEngineProbability());
psParameter.setAlgorithmDeltaPEP(matchParameter.getAlgorithmDeltaPEP());
psParameter.setDeltaPEP(matchParameter.getDeltaPEP());
matchesValidator.getPsmMap().addPoint(retainedP, spectrumMatch, sequenceMatchingPreferences);
psParameter.setSpecificMapKey(spectrumMatch.getBestPeptideAssumption().getIdentificationCharge().value + "");
identification.addSpectrumMatchParameter(spectrumKey, psParameter);
identification.updateSpectrumMatch(spectrumMatch);
} else {
throw new IllegalArgumentException("No best assumption found for spectrum " + spectrumKey + ".");
}
}
if (!tagAssumptions.isEmpty()) {
ArrayList<Double> evalues = new ArrayList<Double>(tagAssumptions.keySet());
Double bestEvalue = Collections.min(evalues);
TagAssumption bestAssumption = tagAssumptions.get(bestEvalue).get(0);
spectrumMatch.setBestTagAssumption(bestAssumption);
identification.updateSpectrumMatch(spectrumMatch);
if (spectrumMatch.getBestPeptideAssumption() == null) {
psParameter = new PSParameter();
if (!multiSE) {
psParameter.setSpectrumProbabilityScore(bestEvalue);
}
PSParameter matchParameter = (PSParameter) bestAssumption.getUrParam(psParameter);
psParameter.setSearchEngineProbability(matchParameter.getSearchEngineProbability());
psParameter.setAlgorithmDeltaPEP(matchParameter.getAlgorithmDeltaPEP());
psParameter.setDeltaPEP(matchParameter.getDeltaPEP());
psParameter.setSpecificMapKey(spectrumMatch.getBestTagAssumption().getIdentificationCharge().value + "");
identification.addSpectrumMatchParameter(spectrumKey, psParameter);
}
}
waitingHandler.increaseSecondaryProgressCounter();
if (waitingHandler.isRunCanceled()) {