HashMap<String, HashMap<Double, ArrayList<String>>> notConfidentPeptideInference = new HashMap<String, HashMap<Double, ArrayList<String>>>();
for (String spectrumFileName : identification.getSpectrumFiles()) {
identification.loadSpectrumMatches(spectrumFileName, null);
for (String spectrumKey : identification.getSpectrumIdentification(spectrumFileName)) {
SpectrumMatch spectrumMatch = identification.getSpectrumMatch(spectrumKey);
if (spectrumMatch.getBestPeptideAssumption() != null) {
boolean variableAA = false;
for (ModificationMatch modificationMatch : spectrumMatch.getBestPeptideAssumption().getPeptide().getModificationMatches()) {
if (modificationMatch.isVariable()) {
String modName = modificationMatch.getTheoreticPtm();
PTM ptm = ptmFactory.getPTM(modName);
if (ptm.getType() == PTM.MODAA) {
variableAA = true;
break;
}
}
}
if (variableAA) {
ptmSiteInference(spectrumMatch, ptmScoringPreferences, searchParameters, sequenceMatchingPreferences);
boolean confident = true;
for (ModificationMatch modMatch : spectrumMatch.getBestPeptideAssumption().getPeptide().getModificationMatches()) {
if (modMatch.isVariable()) {
String modName = modMatch.getTheoreticPtm();
PTM ptm = ptmFactory.getPTM(modName);
double ptmMass = ptm.getMass();
if (ptm.getType() == PTM.MODAA) {
if (!modMatch.isConfident()) {
HashMap<Double, ArrayList<String>> fileMap = notConfidentPeptideInference.get(spectrumFileName);
if (fileMap == null) {
fileMap = new HashMap<Double, ArrayList<String>>();
notConfidentPeptideInference.put(spectrumFileName, fileMap);
}
ArrayList<String> spectra = fileMap.get(ptmMass);
if (spectra == null) {
spectra = new ArrayList<String>();
fileMap.put(ptmMass, spectra);
}
spectra.add(spectrumKey);
confident = false;
} else {
HashMap<String, ArrayList<String>> modMap = confidentPeptideInference.get(ptmMass);
if (modMap == null) {
modMap = new HashMap<String, ArrayList<String>>();
confidentPeptideInference.put(ptmMass, modMap);
}
String sequence = spectrumMatch.getBestPeptideAssumption().getPeptide().getSequence();
ArrayList<String> spectra = modMap.get(sequence);
if (spectra == null) {
spectra = new ArrayList<String>();
modMap.put(sequence, spectra);
}
spectra.add(spectrumKey);
}
}
}
}
identification.updateSpectrumMatch(spectrumMatch);
if (confident) {
waitingHandler.increaseSecondaryProgressCounter();
}
if (waitingHandler.isRunCanceled()) {
return;
}
} else {
waitingHandler.increaseSecondaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
}
}
}
}
// try to infer the modification site based on any related peptide
for (String spectrumFile : notConfidentPeptideInference.keySet()) {
HashSet<String> progress = new HashSet<String>();
HashMap<Double, ArrayList<String>> peptidesOfFile = notConfidentPeptideInference.get(spectrumFile);
for (Double ptmMass : peptidesOfFile.keySet()) {
ArrayList<String> spectrumKeys = peptidesOfFile.get(ptmMass);
identification.loadSpectrumMatches(spectrumKeys, null);
for (String spectrumKey : spectrumKeys) {
SpectrumMatch spectrumMatch = identification.getSpectrumMatch(spectrumKey);
Peptide peptide = spectrumMatch.getBestPeptideAssumption().getPeptide();
String sequence = peptide.getSequence();
String notConfidentKey = peptide.getMatchingKey(sequenceMatchingPreferences);
int nMod = Peptide.getModificationCount(notConfidentKey, ptmMass);
ArrayList<Integer> tempLocalizations, oldLocalizations = Peptide.getNModificationLocalized(notConfidentKey, ptmMass);
ArrayList<Integer> newLocalizationCandidates = new ArrayList<Integer>();
HashMap<String, ArrayList<String>> ptmConfidentPeptides = confidentPeptideInference.get(ptmMass);
if (ptmConfidentPeptides != null) {
// See if we can explain this peptide by another already identified peptide with the same number of modifications (the two peptides will be merged)
ArrayList<String> keys = ptmConfidentPeptides.get(sequence);
if (keys != null) {
for (String tempKey : keys) {
SpectrumMatch secondaryMatch = identification.getSpectrumMatch(tempKey);
String secondaryKey = secondaryMatch.getBestPeptideAssumption().getPeptide().getMatchingKey(sequenceMatchingPreferences);
if (Peptide.getModificationCount(secondaryKey, ptmMass) == nMod) {
tempLocalizations = Peptide.getNModificationLocalized(secondaryKey, ptmMass);
for (int localization : tempLocalizations) {
if (!oldLocalizations.contains(localization) && !newLocalizationCandidates.contains(localization)) {
newLocalizationCandidates.add(localization);
}
}
}
}
if (oldLocalizations.size() + newLocalizationCandidates.size() < nMod) {
// we cannot merge this peptide, see whether we can explain the remaining modifications using peptides with the same sequence but other modification profile
for (String tempKey : keys) {
SpectrumMatch secondaryMatch = identification.getSpectrumMatch(tempKey);
String secondaryKey = secondaryMatch.getBestPeptideAssumption().getPeptide().getMatchingKey(sequenceMatchingPreferences);
tempLocalizations = Peptide.getNModificationLocalized(secondaryKey, ptmMass);
for (int localization : tempLocalizations) {
if (!oldLocalizations.contains(localization) && !newLocalizationCandidates.contains(localization)) {
newLocalizationCandidates.add(localization);
}
}
}
}
}
if (oldLocalizations.size() + newLocalizationCandidates.size() < nMod) {
// There are still unexplained sites, let's see if we find a related peptide which can help.
HashMap<String, ArrayList<String>> confidentAtMass = confidentPeptideInference.get(ptmMass);
for (String otherSequence : confidentAtMass.keySet()) {
if (!sequence.equals(otherSequence) && sequence.contains(otherSequence)) {
for (String tempKey : confidentAtMass.get(otherSequence)) {
SpectrumMatch secondaryMatch = identification.getSpectrumMatch(tempKey);
String secondaryKey = secondaryMatch.getBestPeptideAssumption().getPeptide().getMatchingKey(sequenceMatchingPreferences);
tempLocalizations = Peptide.getNModificationLocalized(secondaryKey, ptmMass);
int tempIndex, ref = 0;
String tempSequence = sequence;
while ((tempIndex = tempSequence.indexOf(otherSequence)) >= 0) {
ref += tempIndex;
for (int localization : tempLocalizations) {
int shiftedLocalization = ref + localization;
if (!oldLocalizations.contains(shiftedLocalization) && !newLocalizationCandidates.contains(shiftedLocalization)) {
boolean candidatePtm = false;
for (String ptmName : searchParameters.getModificationProfile().getAllNotFixedModifications()) {
PTM ptm = ptmFactory.getPTM(ptmName);
if (ptm.getType() == PTM.MODAA && ptm.getMass() == ptm.getMass() && peptide.getPotentialModificationSites(ptm, sequenceMatchingPreferences).contains(shiftedLocalization)) {
candidatePtm = true;
break;
}
}
if (candidatePtm) {
newLocalizationCandidates.add(shiftedLocalization);
}
}
}
tempSequence = tempSequence.substring(tempIndex + 1);
ref++;
}
}
} else if (!sequence.equals(otherSequence) && otherSequence.contains(sequence)) {
for (String tempKey : confidentAtMass.get(otherSequence)) {
SpectrumMatch secondaryMatch = identification.getSpectrumMatch(tempKey);
String secondaryKey = secondaryMatch.getBestPeptideAssumption().getPeptide().getMatchingKey(sequenceMatchingPreferences);
tempLocalizations = Peptide.getNModificationLocalized(secondaryKey, ptmMass);
int tempIndex, ref = 0;
String tempSequence = otherSequence;
while ((tempIndex = tempSequence.indexOf(sequence)) >= 0) {
ref += tempIndex;