use of com.compomics.util.parameters.identification.advanced.ModificationLocalizationParameters in project peptide-shaker by compomics.
the class ModificationLocalizationScorer method scorePTMs.
/**
* Scores PTM locations for a desired spectrum match.
*
* @param identification Identification object containing the matches.
* @param spectrumMatch The spectrum match to score.
* @param sequenceProvider The protein sequence provider to use.
* @param spectrumProvider The spectrum provider to use.
* @param modificationProvider The modification provider to use.
* @param identificationParameters The parameters used for identification.
* @param waitingHandler The waiting handler to use to display progress and
* allow canceling.
* @param peptideSpectrumAnnotator The spectrum annotator to use.
*/
public void scorePTMs(Identification identification, SpectrumMatch spectrumMatch, SequenceProvider sequenceProvider, SpectrumProvider spectrumProvider, ModificationProvider modificationProvider, IdentificationParameters identificationParameters, WaitingHandler waitingHandler, PeptideSpectrumAnnotator peptideSpectrumAnnotator) {
SequenceMatchingParameters sequenceMatchingParameters = identificationParameters.getSequenceMatchingParameters();
attachDeltaScore(identification, spectrumMatch, sequenceMatchingParameters, modificationProvider);
ModificationLocalizationParameters scoringParameters = identificationParameters.getModificationLocalizationParameters();
if (scoringParameters.isProbabilisticScoreCalculation()) {
attachProbabilisticScore(spectrumMatch, sequenceProvider, spectrumProvider, modificationProvider, identificationParameters, peptideSpectrumAnnotator, identification);
}
}
use of com.compomics.util.parameters.identification.advanced.ModificationLocalizationParameters in project peptide-shaker by compomics.
the class ModificationLocalizationScorer method attachProbabilisticScore.
/**
* Attaches the selected probabilistic modification score.
*
* @param spectrumMatch The spectrum match studied, the score will be
* calculated for the best assumption only.
* @param sequenceProvider The protein sequence provider to use.
* @param spectrumProvider The spectrum provider to use.
* @param modificationProvider The modification provider to use.
* @param identificationParameters The identification parameters.
* @param peptideSpectrumAnnotator The peptide spectrum annotator to use.
* @param identification The identification object containing the matches.
*/
private void attachProbabilisticScore(SpectrumMatch spectrumMatch, SequenceProvider sequenceProvider, SpectrumProvider spectrumProvider, ModificationProvider modificationProvider, IdentificationParameters identificationParameters, PeptideSpectrumAnnotator peptideSpectrumAnnotator, Identification identification) {
SearchParameters searchParameters = identificationParameters.getSearchParameters();
AnnotationParameters annotationParameters = identificationParameters.getAnnotationParameters();
ModificationLocalizationParameters scoringParameters = identificationParameters.getModificationLocalizationParameters();
SequenceMatchingParameters sequenceMatchingParameters = identificationParameters.getSequenceMatchingParameters();
SequenceMatchingParameters modificationSequenceMatchingParameters = scoringParameters.getSequenceMatchingParameters();
ModificationParameters modificationParameters = searchParameters.getModificationParameters();
PSModificationScores modificationScores = (PSModificationScores) spectrumMatch.getUrParam(PSModificationScores.dummy);
if (modificationScores != null) {
modificationScores = new PSModificationScores();
spectrumMatch.addUrParam(modificationScores);
}
HashMap<Double, ArrayList<Modification>> modificationsMap = new HashMap<>(1);
HashMap<Double, Integer> nMod = new HashMap<>(1);
PeptideAssumption bestPeptideAssumption = spectrumMatch.getBestPeptideAssumption();
Peptide peptide = bestPeptideAssumption.getPeptide();
for (ModificationMatch modificationMatch : peptide.getVariableModifications()) {
Modification refMod = modificationProvider.getModification(modificationMatch.getModification());
double modMass = refMod.getMass();
if (!modificationsMap.containsKey(modMass)) {
ArrayList<Modification> modifications = modificationFactory.getSameMassNotFixedModifications(modMass, searchParameters).stream().map(modification -> modificationProvider.getModification(modification)).collect(Collectors.toCollection(ArrayList::new));
modificationsMap.put(modMass, modifications);
nMod.put(modMass, 1);
} else {
nMod.put(modMass, nMod.get(modMass) + 1);
}
}
if (!modificationsMap.isEmpty()) {
String spectrumFile = spectrumMatch.getSpectrumFile();
String spectrumTitle = spectrumMatch.getSpectrumTitle();
Spectrum spectrum = spectrumProvider.getSpectrum(spectrumFile, spectrumTitle);
SpecificAnnotationParameters specificAnnotationParameters = annotationParameters.getSpecificAnnotationParameters(spectrumFile, spectrumTitle, bestPeptideAssumption, modificationParameters, sequenceProvider, modificationSequenceMatchingParameters, peptideSpectrumAnnotator);
for (double modMass : modificationsMap.keySet()) {
HashMap<Integer, Double> scores = null;
if (scoringParameters.getSelectedProbabilisticScore() == ModificationLocalizationScore.PhosphoRS) {
scores = PhosphoRS.getSequenceProbabilities(peptide, modificationsMap.get(modMass), modificationParameters, spectrum, sequenceProvider, annotationParameters, specificAnnotationParameters, scoringParameters.isProbabilisticScoreNeutralLosses(), sequenceMatchingParameters, modificationSequenceMatchingParameters, peptideSpectrumAnnotator);
if (scores == null) {
throw new IllegalArgumentException("An error occurred while scoring spectrum " + spectrumTitle + " of file " + spectrumFile + " with PhosphoRS.");
// Most likely a compatibility issue with utilities
}
}
if (scores != null) {
// remap to searched modifications
Modification mappedModification = null;
String peptideSequence = peptide.getSequence();
for (int site : scores.keySet()) {
if (site == 0) {
// N-term mod
for (Modification modification : modificationsMap.get(modMass)) {
if (modification.getModificationType().isNTerm()) {
mappedModification = modification;
break;
}
}
if (mappedModification == null) {
throw new IllegalArgumentException("Could not map the PTM of mass " + modMass + " on the N-terminus of the peptide " + peptideSequence + ".");
}
} else if (site == peptideSequence.length() + 1) {
// C-term mod
for (Modification modification : modificationsMap.get(modMass)) {
if (modification.getModificationType().isCTerm()) {
mappedModification = modification;
break;
}
}
if (mappedModification == null) {
throw new IllegalArgumentException("Could not map the PTM of mass " + modMass + " on the C-terminus of the peptide " + peptideSequence + ".");
}
} else {
for (Modification modification : modificationsMap.get(modMass)) {
mappedModification = modification;
break;
}
if (mappedModification == null) {
throw new IllegalArgumentException("Could not map the PTM of mass " + modMass + " at site " + site + " in peptide " + peptide.getSequence() + ".");
}
}
String modName = mappedModification.getName();
ModificationScoring modificationScoring = modificationScores.getModificationScoring(modName);
if (modificationScoring == null) {
modificationScoring = new ModificationScoring(modName);
modificationScores.addModificationScoring(modName, modificationScoring);
}
modificationScoring.setProbabilisticScore(site, scores.get(site));
}
}
}
identification.updateObject(spectrumMatch.getKey(), spectrumMatch);
}
}
use of com.compomics.util.parameters.identification.advanced.ModificationLocalizationParameters in project peptide-shaker by compomics.
the class ModificationLocalizationScorer method modificationSiteInference.
/**
* Infers the modification site of the best peptide assumption of the given
* spectrum match.
*
* @param spectrumMatch The spectrum match.
* @param sequenceProvider The sequence provider to use.
* @param modificationProvider The modification provider to use.
* @param identificationParameters The identification parameters.
*/
public void modificationSiteInference(SpectrumMatch spectrumMatch, SequenceProvider sequenceProvider, ModificationProvider modificationProvider, IdentificationParameters identificationParameters) {
Peptide peptide = spectrumMatch.getBestPeptideAssumption().getPeptide();
int nVariableModifications = peptide.getNVariableModifications();
if (nVariableModifications > 0) {
SearchParameters searchParameters = identificationParameters.getSearchParameters();
PSModificationScores modificationScores = (PSModificationScores) spectrumMatch.getUrParam(PSModificationScores.dummy);
HashMap<Double, ArrayList<ModificationMatch>> modMatchesMap = new HashMap<>(nVariableModifications);
HashMap<Double, HashMap<Integer, String>> possiblePositions = new HashMap<>(nVariableModifications);
HashMap<Double, HashMap<Integer, ArrayList<String>>> confidentSites = new HashMap<>(nVariableModifications);
for (ModificationMatch modificationMatch : peptide.getVariableModifications()) {
String modName = modificationMatch.getModification();
Modification modification = modificationProvider.getModification(modName);
double modMass = modification.getMass();
ArrayList<ModificationMatch> modOccurence = modMatchesMap.get(modMass);
if (modOccurence == null) {
modOccurence = new ArrayList<>(1);
modMatchesMap.put(modMass, modOccurence);
}
HashMap<Integer, String> modPossibleSites = possiblePositions.get(modMass);
if (modPossibleSites == null) {
modPossibleSites = new HashMap<>(1);
possiblePositions.put(modMass, modPossibleSites);
}
boolean maybeNotTerminal = modification.getModificationType() == ModificationType.modaa;
if (!maybeNotTerminal) {
ArrayList<String> expectedModifications = modificationFactory.getExpectedVariableModifications(searchParameters);
for (String otherModName : expectedModifications) {
if (!otherModName.equals(modName)) {
Modification otherModification = modificationProvider.getModification(otherModName);
if (otherModification.getMass() == modMass && modification.getModificationType() != otherModification.getModificationType()) {
maybeNotTerminal = true;
break;
}
}
}
}
if (maybeNotTerminal) {
modOccurence.add(modificationMatch);
for (String similarModName : modificationFactory.getSameMassNotFixedModifications(modMass, searchParameters)) {
Modification similarModification = modificationProvider.getModification(similarModName);
if (modification.getMass() == modMass) {
int[] possibleSites = ModificationUtils.getPossibleModificationSites(peptide, similarModification, sequenceProvider, identificationParameters.getModificationLocalizationParameters().getSequenceMatchingParameters());
for (int pos : possibleSites) {
modPossibleSites.put(pos, similarModName);
}
}
}
} else {
ModificationScoring modificationScoring = modificationScores.getModificationScoring(modName);
if (modificationScoring == null) {
modificationScoring = new ModificationScoring(modName);
modificationScores.addModificationScoring(modName, modificationScoring);
}
modificationScoring.setSiteConfidence(modificationMatch.getSite(), ModificationScoring.VERY_CONFIDENT);
modificationMatch.setConfident(true);
HashMap<Integer, ArrayList<String>> modificationSites = confidentSites.get(modification.getMass());
if (modificationSites == null) {
modificationSites = new HashMap<>(1);
confidentSites.put(modMass, modificationSites);
}
int site = modificationMatch.getSite();
ArrayList<String> modNames = modificationSites.get(site);
if (modNames == null) {
modNames = new ArrayList<>(1);
modificationSites.put(site, modNames);
}
modNames.add(modName);
}
}
ModificationLocalizationParameters modificationScoringParameters = identificationParameters.getModificationLocalizationParameters();
Set<Double> modMasses = modMatchesMap.keySet();
// p score -> d-score -> Map mod mass -> site -> list of modifications
TreeMap<Double, TreeMap<Double, TreeMap<Double, HashMap<Integer, HashSet<String>>>>> ambiguousScoreToSiteMap = new TreeMap<>();
HashMap<Double, Integer> nRepresentativesMap = new HashMap<>(modMasses.size());
ArrayList<ModificationMatch> assignedModifications = new ArrayList<>(peptide.getNVariableModifications());
// p-score -> d-score -> mod mass -> list of posssible sites
TreeMap<Double, TreeMap<Double, TreeMap<Double, TreeSet<Integer>>>> scoreToSiteMap = new TreeMap<>();
for (double modMass : modMasses) {
ArrayList<ModificationMatch> modificationMatches = modMatchesMap.get(modMass);
int nMods = modificationMatches.size();
HashMap<Integer, String> modificationPossibleSites = possiblePositions.get(modMass);
int nPossibleSites = modificationPossibleSites.size();
HashMap<Integer, ArrayList<String>> modificationConfidentSites = confidentSites.get(modMass);
if (modificationConfidentSites == null) {
modificationConfidentSites = new HashMap<>(1);
confidentSites.put(modMass, modificationConfidentSites);
}
if (nPossibleSites < nMods) {
throw new IllegalArgumentException("The occurence of modification of mass " + modMass + " (" + modificationMatches.size() + ") is higher than the number of possible sites (" + modificationPossibleSites.size() + ") on sequence " + peptide.getSequence() + " in spectrum " + spectrumMatch.getKey() + ".");
} else if (modificationPossibleSites.size() == modificationMatches.size()) {
for (ModificationMatch modMatch : modificationMatches) {
String modName = modMatch.getModification();
int site = modMatch.getSite();
ModificationScoring modificationScoring = modificationScores.getModificationScoring(modName);
if (modificationScoring == null) {
modificationScoring = new ModificationScoring(modName);
modificationScores.addModificationScoring(modName, modificationScoring);
}
modificationScoring.setSiteConfidence(site, ModificationScoring.VERY_CONFIDENT);
modMatch.setConfident(true);
ArrayList<String> modificationsAtAA = modificationConfidentSites.get(site);
if (modificationsAtAA == null) {
modificationsAtAA = new ArrayList<>(1);
modificationConfidentSites.put(site, modificationsAtAA);
}
modificationsAtAA.add(modName);
assignedModifications.add(modMatch);
}
} else if (!modificationScoringParameters.isProbabilisticScoreCalculation()) {
// no probabilistic score in that case
double pScore = 0;
for (ModificationMatch modificationMatch : modificationMatches) {
String modName = modificationMatch.getModification();
ModificationScoring modificationScoring = modificationScores.getModificationScoring(modName);
int site = modificationMatch.getSite();
Double dScore = modificationScoring.getDeltaScore(site);
if (dScore == 0.0) {
modificationScoring.setSiteConfidence(site, ModificationScoring.RANDOM);
modificationMatch.setConfident(false);
} else if (dScore <= 95) {
modificationScoring.setSiteConfidence(site, ModificationScoring.DOUBTFUL);
modificationMatch.setConfident(false);
} else {
modificationScoring.setSiteConfidence(site, ModificationScoring.CONFIDENT);
modificationMatch.setConfident(true);
ArrayList<String> modificationsAtAA = modificationConfidentSites.get(site);
if (modificationsAtAA == null) {
modificationsAtAA = new ArrayList<>(1);
modificationConfidentSites.put(site, modificationsAtAA);
}
modificationsAtAA.add(modName);
}
if (!modificationMatch.getConfident()) {
TreeMap<Double, TreeMap<Double, HashMap<Integer, HashSet<String>>>> pScoreAmbiguousMap = ambiguousScoreToSiteMap.get(pScore);
if (pScoreAmbiguousMap == null) {
pScoreAmbiguousMap = new TreeMap<>();
ambiguousScoreToSiteMap.put(pScore, pScoreAmbiguousMap);
}
TreeMap<Double, HashMap<Integer, HashSet<String>>> dScoreAmbiguousMap = pScoreAmbiguousMap.get(dScore);
if (dScoreAmbiguousMap == null) {
dScoreAmbiguousMap = new TreeMap<>();
pScoreAmbiguousMap.put(dScore, dScoreAmbiguousMap);
}
HashMap<Integer, HashSet<String>> massAmbiguousMap = dScoreAmbiguousMap.get(modMass);
if (massAmbiguousMap == null) {
massAmbiguousMap = new HashMap<>(nPossibleSites);
dScoreAmbiguousMap.put(modMass, massAmbiguousMap);
}
HashSet<String> modifications = massAmbiguousMap.get(site);
if (modifications == null) {
modifications = new HashSet<>(1);
massAmbiguousMap.put(site, modifications);
}
modifications.add(modName);
}
assignedModifications.add(modificationMatch);
}
} else {
for (int site : modificationPossibleSites.keySet()) {
String modName = modificationPossibleSites.get(site);
ModificationScoring modificationScoring = modificationScores.getModificationScoring(modName);
double pScore = 0;
double dScore = 0;
if (modificationScoring != null) {
pScore = modificationScoring.getProbabilisticScore(site);
dScore = modificationScoring.getDeltaScore(site);
}
TreeMap<Double, TreeMap<Double, TreeSet<Integer>>> pScoreMap = scoreToSiteMap.get(pScore);
if (pScoreMap == null) {
pScoreMap = new TreeMap<>();
scoreToSiteMap.put(pScore, pScoreMap);
}
TreeMap<Double, TreeSet<Integer>> dScoreMap = pScoreMap.get(dScore);
if (dScoreMap == null) {
dScoreMap = new TreeMap<>();
pScoreMap.put(dScore, dScoreMap);
}
TreeSet<Integer> dScoreSites = dScoreMap.get(modMass);
if (dScoreSites == null) {
dScoreSites = new TreeSet<>();
dScoreMap.put(modMass, dScoreSites);
}
dScoreSites.add(site);
}
}
}
// Assign modifications to the sites passing the threshold.
if (!scoreToSiteMap.isEmpty()) {
HashMap<Double, Integer> processedSitesMap = new HashMap<>(modMasses.size());
for (Entry<Double, TreeMap<Double, TreeMap<Double, TreeSet<Integer>>>> entry1 : scoreToSiteMap.descendingMap().entrySet()) {
double pScore = entry1.getKey();
TreeMap<Double, TreeMap<Double, TreeSet<Integer>>> pScoreMap = entry1.getValue();
for (Entry<Double, TreeMap<Double, TreeSet<Integer>>> entry2 : pScoreMap.descendingMap().entrySet()) {
double dScore = entry2.getKey();
TreeMap<Double, TreeSet<Integer>> dScoreMap = entry2.getValue();
for (Entry<Double, TreeSet<Integer>> entry : dScoreMap.entrySet()) {
double modificationMass = entry.getKey();
ArrayList<ModificationMatch> modificationMatches = modMatchesMap.get(modificationMass);
HashMap<Integer, String> modificationPossibleSites = possiblePositions.get(modificationMass);
HashMap<Integer, ArrayList<String>> modificationConfidentSites = confidentSites.get(modificationMass);
int nMods = modificationMatches.size();
int nPossibleSites = modificationPossibleSites.size();
double randomScoreThreshold = modificationScoringParameters.getSelectedProbabilisticScore().getRandomThreshold(nMods, nPossibleSites);
double confidenceThreshold = modificationScoringParameters.getProbabilisticScoreThreshold();
TreeSet<Integer> sites = dScoreMap.get(modificationMass);
Integer nAssignedSites = processedSitesMap.get(modificationMass);
if (nAssignedSites == null) {
nAssignedSites = 0;
}
for (int site : sites) {
String modName = modificationPossibleSites.get(site);
ModificationScoring modificationScoring = modificationScores.getModificationScoring(modName);
if (modificationScoring == null) {
modificationScoring = new ModificationScoring(modName);
modificationScores.addModificationScoring(modName, modificationScoring);
}
ModificationMatch modificationMatch = null;
if (nAssignedSites < nMods) {
boolean alreadyOccupied = false;
for (ModificationMatch assignedMatch : assignedModifications) {
if (assignedMatch.getSite() == site) {
alreadyOccupied = true;
break;
}
}
if (!alreadyOccupied) {
if (pScore >= confidenceThreshold) {
modificationMatch = modificationMatches.get(nAssignedSites);
modificationMatch.setSite(site);
modificationMatch.setModification(modName);
assignedModifications.add(modificationMatch);
modificationScoring.setSiteConfidence(site, ModificationScoring.VERY_CONFIDENT);
modificationMatch.setConfident(true);
ArrayList<String> modificationsAtAA = modificationConfidentSites.get(site);
if (modificationsAtAA == null) {
modificationsAtAA = new ArrayList<>(1);
modificationConfidentSites.put(site, modificationsAtAA);
}
modificationsAtAA.add(modName);
nAssignedSites++;
processedSitesMap.put(modificationMass, nAssignedSites);
} else if (pScore > randomScoreThreshold) {
modificationScoring.setSiteConfidence(site, ModificationScoring.DOUBTFUL);
} else {
modificationScoring.setSiteConfidence(site, ModificationScoring.RANDOM);
}
}
}
if (modificationMatch == null) {
TreeMap<Double, TreeMap<Double, HashMap<Integer, HashSet<String>>>> pScoreAmbiguousMap = ambiguousScoreToSiteMap.get(pScore);
if (pScoreAmbiguousMap == null) {
pScoreAmbiguousMap = new TreeMap<>();
ambiguousScoreToSiteMap.put(pScore, pScoreAmbiguousMap);
}
TreeMap<Double, HashMap<Integer, HashSet<String>>> dScoreAmbiguousMap = pScoreAmbiguousMap.get(dScore);
if (dScoreAmbiguousMap == null) {
dScoreAmbiguousMap = new TreeMap<>();
pScoreAmbiguousMap.put(dScore, dScoreAmbiguousMap);
}
HashMap<Integer, HashSet<String>> massAmbiguousMap = dScoreAmbiguousMap.get(modificationMass);
if (massAmbiguousMap == null) {
massAmbiguousMap = new HashMap<>(nPossibleSites);
dScoreAmbiguousMap.put(modificationMass, massAmbiguousMap);
}
HashSet<String> modifications = massAmbiguousMap.get(site);
if (modifications == null) {
modifications = new HashSet<>(1);
massAmbiguousMap.put(site, modifications);
}
modifications.add(modName);
}
}
}
}
}
}
// See whether there are sites that are not confidently assigned.
for (double modMass : confidentSites.keySet()) {
HashMap<Integer, ArrayList<String>> modConfidentSitesMap = confidentSites.get(modMass);
ArrayList<Integer> modConfidentSites = new ArrayList<>(modConfidentSitesMap.keySet());
int nConfident = 0;
for (int site : modConfidentSites) {
ArrayList<String> modifications = modConfidentSitesMap.get(site);
for (String modName : modifications) {
modificationScores.addConfidentModificationSite(modName, site);
}
nConfident += modifications.size();
}
modConfidentSites.size();
ArrayList<ModificationMatch> modificationMatches = modMatchesMap.get(modMass);
int nMods = modificationMatches.size();
if (nConfident < nMods) {
int nRepresentatives = nMods - nConfident;
if (nRepresentatives > 0) {
nRepresentativesMap.put(modMass, nRepresentatives);
}
}
}
// Create ambiguous site groups and assign the remaining modifications to the best scoring sites.
if (!nRepresentativesMap.isEmpty()) {
HashMap<Double, HashMap<Integer, HashMap<Integer, HashSet<String>>>> representativeToSecondaryMap = getRepresentativeToSecondaryMap(ambiguousScoreToSiteMap, nRepresentativesMap);
for (Entry<Double, HashMap<Integer, HashMap<Integer, HashSet<String>>>> entry1 : representativeToSecondaryMap.entrySet()) {
double modMass = entry1.getKey();
HashMap<Integer, HashMap<Integer, HashSet<String>>> massMap = entry1.getValue();
ArrayList<ModificationMatch> modificationMatches = modMatchesMap.get(modMass);
int nMods = modificationMatches.size();
int nRepresentativeSites = nRepresentativesMap.get(modMass);
if (nRepresentativeSites != massMap.size()) {
throw new IllegalArgumentException("Unexpected number of representative sites.");
}
int nConfident = nMods - nRepresentativeSites;
int modMatchIndex = nConfident;
for (Entry<Integer, HashMap<Integer, HashSet<String>>> entry : massMap.entrySet()) {
int representativeSite = entry.getKey();
HashMap<Integer, HashSet<String>> sites = entry.getValue();
modificationScores.addAmbiguousModificationSites(representativeSite, sites);
ModificationMatch modificationMatch = modificationMatches.get(modMatchIndex);
String modName = null;
for (String possibleModName : sites.get(representativeSite)) {
Modification possibleModification = modificationProvider.getModification(possibleModName);
if (possibleModification.getMass() == modMass) {
modName = possibleModName;
break;
}
}
if (modName == null) {
throw new IllegalArgumentException("No candidate modification of mass " + modMass + " found at site " + representativeSite + " on peptide " + peptide.getSequence() + ".");
}
modificationMatch.setModification(modName);
modificationMatch.setSite(representativeSite);
modificationMatch.setConfident(false);
modMatchIndex++;
}
}
}
}
}
use of com.compomics.util.parameters.identification.advanced.ModificationLocalizationParameters in project peptide-shaker by compomics.
the class StirRunnable method scoreModificationLocalization.
/**
* Scores the modification localization for the given peptide in the given
* spectrum match. The scores are returned in a map: modification mass to
* modification site to modification score.
*
* @param spectrumMatch The spectrum match.
* @param peptideAssumption The peptide assumption.
*
* @return The modification localization scores in a map.
*/
private TreeMap<Double, HashMap<Integer, Double>> scoreModificationLocalization(SpectrumMatch spectrumMatch, PeptideAssumption peptideAssumption) {
SearchParameters searchParameters = identificationParameters.getSearchParameters();
AnnotationParameters annotationParameters = identificationParameters.getAnnotationParameters();
SequenceMatchingParameters sequenceMatchingParameters = identificationParameters.getSequenceMatchingParameters();
ModificationParameters modificationParameters = identificationParameters.getSearchParameters().getModificationParameters();
ModificationLocalizationParameters modificationLocalizationParameters = identificationParameters.getModificationLocalizationParameters();
SequenceMatchingParameters modificationSequenceMatchingParameters = modificationLocalizationParameters.getSequenceMatchingParameters();
Peptide peptide = peptideAssumption.getPeptide();
String spectrumFile = spectrumMatch.getSpectrumFile();
String spectrumTitle = spectrumMatch.getSpectrumTitle();
Spectrum spectrum = spectrumProvider.getSpectrum(spectrumFile, spectrumTitle);
HashMap<Double, ArrayList<Modification>> modificationsMap = new HashMap<>(1);
for (ModificationMatch modificationMatch : peptide.getVariableModifications()) {
Modification refMod = modificationFactory.getModification(modificationMatch.getModification());
double modMass = refMod.getMass();
if (!modificationsMap.containsKey(modMass)) {
ArrayList<Modification> modifications = modificationFactory.getSameMassNotFixedModifications(modMass, searchParameters).stream().map(modification -> modificationFactory.getModification(modification)).collect(Collectors.toCollection(ArrayList::new));
modificationsMap.put(modMass, modifications);
}
}
SpecificAnnotationParameters specificAnnotationParameters = annotationParameters.getSpecificAnnotationParameters(spectrumFile, spectrumTitle, peptideAssumption, modificationParameters, sequenceProvider, modificationSequenceMatchingParameters, peptideSpectrumAnnotator);
return modificationsMap.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey(), entry -> PhosphoRS.getSequenceProbabilities(peptide, entry.getValue(), modificationParameters, spectrum, sequenceProvider, annotationParameters, specificAnnotationParameters, modificationLocalizationParameters.isProbabilisticScoreNeutralLosses(), sequenceMatchingParameters, modificationSequenceMatchingParameters, peptideSpectrumAnnotator), (a, b) -> a, TreeMap::new));
}
use of com.compomics.util.parameters.identification.advanced.ModificationLocalizationParameters in project peptide-shaker by compomics.
the class PeptideShaker method createProject.
/**
* Creates a PeptideShaker project.
*
* @param waitingHandler the handler displaying feedback to the user
* @param exceptionHandler handler for exceptions
* @param identificationParameters the identification parameters
* @param processingParameters the processing parameters
* @param projectType the project type
* @param spectrumCountingParameters the spectrum counting parameters
* @param spectrumProvider the spectrum provider
* @param setWaitingHandlerFinshedWhenDone if true, the waiting handler is
* set to finished when the project is created
* @param projectDetails the project details
*
* @throws java.lang.InterruptedException exception thrown if a thread gets
* interrupted
* @throws java.util.concurrent.TimeoutException exception thrown if a
* process times out
* @throws java.io.IOException if an exception occurs when parsing files
*/
public void createProject(IdentificationParameters identificationParameters, ProcessingParameters processingParameters, SpectrumCountingParameters spectrumCountingParameters, SpectrumProvider spectrumProvider, ProjectDetails projectDetails, ProjectType projectType, WaitingHandler waitingHandler, boolean setWaitingHandlerFinshedWhenDone, ExceptionHandler exceptionHandler) throws InterruptedException, TimeoutException, IOException {
identification.getObjectsDB().commit();
identificationFeaturesGenerator = new IdentificationFeaturesGenerator(identification, identificationParameters, sequenceProvider, spectrumProvider, metrics, spectrumCountingParameters);
matchesValidator = new MatchesValidator(new TargetDecoyMap(), new TargetDecoyMap(), new TargetDecoyMap());
if (waitingHandler.isRunCanceled()) {
return;
}
PsmScoringParameters psmScoringPreferences = identificationParameters.getPsmScoringParameters();
FastaParameters fastaParameters = identificationParameters.getFastaParameters();
FastaSummary fastaSummary = FastaSummary.getSummary(projectDetails.getFastaFile(), fastaParameters, waitingHandler);
// set the background species
identificationParameters.getGeneParameters().setBackgroundSpeciesFromFastaSummary(fastaSummary);
ArrayList<Integer> usedAlgorithms = projectDetails.getIdentificationAlgorithms();
if (psmScoringPreferences.isScoringNeeded(usedAlgorithms)) {
waitingHandler.appendReport("Estimating PSM scores.", true, true);
PsmScorer psmScorer = new PsmScorer(fastaParameters, sequenceProvider, spectrumProvider);
psmScorer.estimateIntermediateScores(identification, inputMap, processingParameters, identificationParameters, waitingHandler, exceptionHandler);
if (psmScoringPreferences.isTargetDecoyNeededForPsmScoring(usedAlgorithms)) {
if (fastaParameters.isTargetDecoy()) {
waitingHandler.appendReport("Estimating intermediate scores probabilities.", true, true);
psmScorer.estimateIntermediateScoreProbabilities(identification, inputMap, processingParameters, waitingHandler);
} else {
waitingHandler.appendReport("No decoy sequences found. Impossible to " + "estimate intermediate scores probabilities.", true, true);
}
}
waitingHandler.appendReport("Scoring PSMs.", true, true);
psmScorer.scorePsms(identification, inputMap, processingParameters, identificationParameters, waitingHandler);
}
identification.getObjectsDB().commit();
System.gc();
if (fastaParameters.isTargetDecoy()) {
waitingHandler.appendReport("Computing assumptions probabilities.", true, true);
} else {
waitingHandler.appendReport("Importing assumptions scores.", true, true);
}
inputMap.estimateProbabilities(waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Saving assumptions probabilities, selecting best match, scoring modification localization.", true, true);
PsmProcessor psmProcessor = new PsmProcessor(identification);
psmProcessor.processPsms(inputMap, identificationParameters, matchesValidator, modificationLocalizationScorer, sequenceProvider, spectrumProvider, modificationFactory, proteinCount, processingParameters.getnThreads(), waitingHandler, exceptionHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Computing PSM probabilities.", true, true);
matchesValidator.getPsmMap().estimateProbabilities(waitingHandler);
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
if (projectType == ProjectType.peptide || projectType == ProjectType.protein) {
PeptideInference peptideInference = new PeptideInference();
ModificationLocalizationParameters modificationScoringPreferences = identificationParameters.getModificationLocalizationParameters();
if (modificationScoringPreferences.getAlignNonConfidentModifications()) {
waitingHandler.appendReport("Resolving peptide inference issues.", true, true);
peptideInference.peptideInference(identification, identificationParameters, sequenceProvider, modificationFactory, waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
}
identification.getObjectsDB().commit();
System.gc();
}
String reportTxt = "Saving probabilities";
String waitingTitle = "Saving Probabilities.";
;
switch(projectType) {
case psm:
reportTxt += ".";
break;
case peptide:
reportTxt += ", building peptides.";
waitingTitle += " Building Peptides.";
break;
default:
reportTxt += ", building peptides and proteins.";
waitingTitle += " Building Peptides and Proteins.";
}
waitingHandler.appendReport(reportTxt, true, true);
waitingHandler.setWaitingText(waitingTitle + " Please Wait...");
attachSpectrumProbabilitiesAndBuildPeptidesAndProteins(sequenceProvider, identificationParameters.getSequenceMatchingParameters(), projectType, fastaParameters, waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
if (projectType == ProjectType.peptide || projectType == ProjectType.protein) {
waitingHandler.appendReport("Generating peptide map.", true, true);
matchesValidator.fillPeptideMaps(identification, metrics, waitingHandler, identificationParameters, sequenceProvider, spectrumProvider);
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Computing peptide probabilities.", true, true);
matchesValidator.getPeptideMap().estimateProbabilities(waitingHandler);
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Saving peptide probabilities.", true, true);
matchesValidator.attachPeptideProbabilities(identification, fastaParameters, waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
if (projectType == ProjectType.protein) {
if (identificationParameters.getProteinInferenceParameters().getSimplifyGroups()) {
waitingHandler.appendReport("Simplifying protein groups.", true, true);
GroupSimplification groupSimplification = new GroupSimplification();
groupSimplification.removeRedundantGroups(identification, identificationParameters, sequenceProvider, proteinDetailsProvider, waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
}
identification.getObjectsDB().commit();
System.gc();
ProteinInference proteinInference = new ProteinInference();
waitingHandler.appendReport("Mapping shared peptides.", true, true);
proteinInference.distributeSharedPeptides(identification, waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Generating protein map.", true, true);
matchesValidator.fillProteinMap(identification, spectrumProvider, waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Selecting leading proteins, inferring peptide and protein inference status.", true, true);
proteinInference.inferPiStatus(identification, metrics, matchesValidator.getProteinMap(), identificationParameters, sequenceProvider, proteinDetailsProvider, waitingHandler);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Computing protein probabilities.", true, true);
matchesValidator.getProteinMap().estimateProbabilities(waitingHandler);
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Saving protein probabilities.", true, true);
matchesValidator.attachProteinProbabilities(identification, sequenceProvider, fastaParameters, metrics, waitingHandler, identificationParameters.getFractionParameters());
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
}
}
if (fastaParameters.isTargetDecoy()) {
IdMatchValidationParameters idMatchValidationParameters = identificationParameters.getIdValidationParameters();
if (idMatchValidationParameters.getDefaultPsmFDR() == 1 && idMatchValidationParameters.getDefaultPeptideFDR() == 1 && idMatchValidationParameters.getDefaultProteinFDR() == 1) {
waitingHandler.appendReport("Validating identifications at 1% FDR, quality control of matches.", true, true);
} else {
waitingHandler.appendReport("Validating identifications, quality control of matches.", true, true);
}
} else {
waitingHandler.appendReport("Quality control of matches.", true, true);
}
matchesValidator.validateIdentifications(identification, metrics, inputMap, waitingHandler, exceptionHandler, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, identificationParameters, projectType, processingParameters);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
if (projectType == ProjectType.peptide || projectType == ProjectType.protein) {
waitingHandler.appendReport("Scoring PTMs in peptides.", true, true);
modificationLocalizationScorer.scorePeptidePtms(identification, modificationFactory, waitingHandler, identificationParameters);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
if (projectType == ProjectType.protein) {
waitingHandler.appendReport("Estimating spectrum counting scaling values.", true, true);
ScalingFactorsEstimators scalingFactors = new ScalingFactorsEstimators(spectrumCountingParameters);
scalingFactors.estimateScalingFactors(identification, metrics, sequenceProvider, identificationFeaturesGenerator, waitingHandler, exceptionHandler, processingParameters);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
waitingHandler.appendReport("Scoring PTMs in proteins, gathering summary metrics.", true, true);
ProteinProcessor proteinProcessor = new ProteinProcessor(identification, identificationParameters, identificationFeaturesGenerator, sequenceProvider);
proteinProcessor.processProteins(modificationLocalizationScorer, metrics, modificationFactory, waitingHandler, exceptionHandler, processingParameters);
waitingHandler.increasePrimaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
identification.getObjectsDB().commit();
System.gc();
}
}
projectCreationDuration.end();
String report = "Identification processing completed (" + projectCreationDuration.toString() + ").";
waitingHandler.appendReport(report, true, true);
waitingHandler.appendReportEndLine();
waitingHandler.appendReportEndLine();
identification.addUrParam(new PSMaps(inputMap, matchesValidator.getPsmMap(), matchesValidator.getPeptideMap(), matchesValidator.getProteinMap()));
if (setWaitingHandlerFinshedWhenDone) {
waitingHandler.setRunFinished();
}
}
Aggregations