use of com.compomics.util.experiment.biology.modifications.ModificationFactory in project peptide-shaker by compomics.
the class QCPanel method getPeptideModificationEnrichmentSpecificityDataset.
/**
* Returns the dataset for the peptide modification rate QC plot.
*
* @return the dataset for the peptide modification rate QC plot
*/
private DefaultCategoryDataset getPeptideModificationEnrichmentSpecificityDataset() {
ModificationFactory modificationFactory = ModificationFactory.getInstance();
Identification identification = peptideShakerGUI.getIdentification();
SequenceProvider sequenceProvider = peptideShakerGUI.getSequenceProvider();
IdentificationParameters identificationParameters = peptideShakerGUI.getIdentificationParameters();
ModificationParameters modificationParameters = identificationParameters.getSearchParameters().getModificationParameters();
SequenceMatchingParameters modificationSequenceMatchingParameters = identificationParameters.getModificationLocalizationParameters().getSequenceMatchingParameters();
ArrayList<String> modNames = modificationParameters.getAllNotFixedModifications();
HashMap<String, Integer> modifiedPeptidesMap = new HashMap<>(modNames.size());
HashMap<String, Integer> possiblyModifiedPeptidesMap = new HashMap<>(modNames.size());
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(identification.getPeptideIdentification().size());
progressDialog.setValue(0);
PeptideMatchesIterator peptideMatchesIterator = identification.getPeptideMatchesIterator(progressDialog);
PeptideMatch peptideMatch;
while ((peptideMatch = peptideMatchesIterator.next()) != null) {
PSParameter psParameter = (PSParameter) peptideMatch.getUrParam(PSParameter.dummy);
if (psParameter.getMatchValidationLevel().isValidated()) {
Peptide peptide = peptideMatch.getPeptide();
for (String modName : modificationParameters.getAllNotFixedModifications()) {
Modification modification = modificationFactory.getModification(modName);
int[] possibleSites = ModificationUtils.getPossibleModificationSites(peptide, modification, sequenceProvider, modificationSequenceMatchingParameters);
if (possibleSites.length != 0) {
Integer nPossiblePeptides = possiblyModifiedPeptidesMap.get(modName);
if (nPossiblePeptides == null) {
possiblyModifiedPeptidesMap.put(modName, 1);
} else {
possiblyModifiedPeptidesMap.put(modName, nPossiblePeptides + 1);
}
boolean modified = false;
for (ModificationMatch modificationMatch : peptide.getVariableModifications()) {
if (modificationMatch.getModification().equals(modName)) {
modified = true;
break;
}
}
if (modified) {
Integer nModifiedPeptides = modifiedPeptidesMap.get(modName);
if (nModifiedPeptides == null) {
modifiedPeptidesMap.put(modName, 1);
} else {
modifiedPeptidesMap.put(modName, nModifiedPeptides + 1);
}
}
}
}
}
if (progressDialog.isRunCanceled()) {
break;
}
progressDialog.increaseSecondaryProgressCounter();
}
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (String modName : modificationParameters.getAllNotFixedModifications()) {
Integer nFound = modifiedPeptidesMap.get(modName);
if (nFound == null) {
nFound = 0;
}
Integer nPossible = possiblyModifiedPeptidesMap.get(modName);
Double rate = 0.0;
if (nPossible != null) {
rate = (100.0 * nFound) / nPossible;
}
dataset.addValue(rate, "Modified", modName);
double rest = 0.0;
if (nPossible != null) {
rest = 100 - rate;
}
dataset.addValue(rest, "Not Modified", modName);
}
return dataset;
}
use of com.compomics.util.experiment.biology.modifications.ModificationFactory in project peptide-shaker by compomics.
the class QCPanel method getPeptideModificationEfficiencyDataset.
/**
* Returns the dataset for the peptide modification efficiency QC plot.
*
* @return the dataset for the peptide modification efficiency QC plot
*/
private DefaultCategoryDataset getPeptideModificationEfficiencyDataset() {
ModificationFactory modificationFactory = ModificationFactory.getInstance();
Identification identification = peptideShakerGUI.getIdentification();
SequenceProvider sequenceProvider = peptideShakerGUI.getSequenceProvider();
IdentificationParameters identificationParameters = peptideShakerGUI.getIdentificationParameters();
ModificationParameters modificationParameters = identificationParameters.getSearchParameters().getModificationParameters();
SequenceMatchingParameters modificationSequenceMatchingPreferences = identificationParameters.getModificationLocalizationParameters().getSequenceMatchingParameters();
ArrayList<String> modNames = modificationParameters.getAllNotFixedModifications();
HashMap<String, Integer> modifiedSitesMap = new HashMap<>(modNames.size());
HashMap<String, Integer> possibleSitesMap = new HashMap<>(modNames.size());
PSParameter psParameter = new PSParameter();
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(identification.getPeptideIdentification().size());
progressDialog.setValue(0);
PeptideMatchesIterator peptideMatchesIterator = identification.getPeptideMatchesIterator(progressDialog);
PeptideMatch peptideMatch;
while ((peptideMatch = peptideMatchesIterator.next()) != null) {
psParameter = (PSParameter) peptideMatch.getUrParam(psParameter);
if (psParameter.getMatchValidationLevel().isValidated()) {
Peptide peptide = peptideMatch.getPeptide();
HashMap<String, Integer> peptideModificationsMap = new HashMap<>(peptide.getVariableModifications().length);
for (ModificationMatch modificationMatch : peptide.getVariableModifications()) {
String modName = modificationMatch.getModification();
Integer occurrence = peptideModificationsMap.get(modName);
if (occurrence == null) {
peptideModificationsMap.put(modName, 1);
} else {
peptideModificationsMap.put(modName, occurrence + 1);
}
}
for (String modName : modificationParameters.getAllNotFixedModifications()) {
Modification modification = modificationFactory.getModification(modName);
int[] possibleSites = ModificationUtils.getPossibleModificationSites(peptide, modification, sequenceProvider, modificationSequenceMatchingPreferences);
if (possibleSites.length != 0) {
Integer occurrencePeptide = peptideModificationsMap.get(modName);
if (occurrencePeptide != null) {
Integer occurrenceDataset = modifiedSitesMap.get(modName);
if (occurrenceDataset == null) {
modifiedSitesMap.put(modName, occurrencePeptide);
} else {
modifiedSitesMap.put(modName, occurrenceDataset + occurrencePeptide);
}
}
Integer possibleSitesDataset = possibleSitesMap.get(modName);
if (possibleSitesDataset == null) {
possibleSitesMap.put(modName, possibleSites.length);
} else {
possibleSitesMap.put(modName, possibleSitesDataset + possibleSites.length);
}
}
}
}
if (progressDialog.isRunCanceled()) {
break;
}
progressDialog.increaseSecondaryProgressCounter();
}
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (String modName : modificationParameters.getAllNotFixedModifications()) {
Integer nFound = modifiedSitesMap.get(modName);
if (nFound == null) {
nFound = 0;
}
Integer nPossible = possibleSitesMap.get(modName);
Double rate = 0.0;
if (nPossible != null) {
rate = (100.0 * nFound) / nPossible;
}
dataset.addValue(rate, "Modified", modName);
double rest = 100 - rate;
dataset.addValue(rest, "Not Modified", modName);
}
return dataset;
}
use of com.compomics.util.experiment.biology.modifications.ModificationFactory in project peptide-shaker by compomics.
the class PsmImportRunnable method importAssumptions.
/**
* Import the assumptions. Maps algorithm specific modifications to the
* generic objects. Relocates aberrant modifications and removes assumptions
* where not all modifications are mapped. Verifies whether there is a best
* match for the spectrum according to the search engine score.
*
* @param spectrumMatch the spectrum match to import
* @param assumptions the assumptions to import
* @param peptideSpectrumAnnotator the spectrum annotator to use to annotate
* spectra
* @param waitingHandler waiting handler to display progress and allow
* canceling the import
*/
private void importAssumptions(SpectrumMatch spectrumMatch) {
PeptideAssumptionFilter peptideAssumptionFilter = identificationParameters.getPeptideAssumptionFilter();
SequenceMatchingParameters sequenceMatchingParameters = identificationParameters.getSequenceMatchingParameters();
SequenceMatchingParameters modificationSequenceMatchingParameters = identificationParameters.getModificationLocalizationParameters().getSequenceMatchingParameters();
SearchParameters searchParameters = identificationParameters.getSearchParameters();
HashMap<Integer, TreeMap<Double, ArrayList<PeptideAssumption>>> peptideAssumptions = spectrumMatch.getPeptideAssumptionsMap();
for (Map.Entry<Integer, TreeMap<Double, ArrayList<PeptideAssumption>>> entry : peptideAssumptions.entrySet()) {
int advocateId = entry.getKey();
if (advocateId == Advocate.xtandem.getIndex()) {
PsmImporter.verifyXTandemModifications(identificationParameters);
}
TreeMap<Double, ArrayList<PeptideAssumption>> assumptionsForAdvocate = entry.getValue();
TreeSet<Double> scores = new TreeSet<>(assumptionsForAdvocate.keySet());
for (double score : scores) {
ArrayList<PeptideAssumption> oldAssumptions = assumptionsForAdvocate.get(score);
ArrayList<PeptideAssumption> newAssumptions = new ArrayList<>(oldAssumptions.size());
nPeptideAssumptionsTotal += oldAssumptions.size();
for (PeptideAssumption peptideAssumption : oldAssumptions) {
Peptide peptide = peptideAssumption.getPeptide();
String peptideSequence = peptide.getSequence();
// Ignore peptides that are too long or too short
if (peptideSequence.length() >= peptideAssumptionFilter.getMinPepLength() && peptideSequence.length() <= peptideAssumptionFilter.getMaxPepLength()) {
// Map peptide to protein
proteinMapping(peptide);
// map the algorithm-specific modifications to utilities modifications
// If there are not enough sites to put them all on the sequence, add an unknown modification
// Note: this needs to be done for tag based assumptions as well since the protein mapping can
// return erroneous modifications for some pattern based modifications
ModificationParameters modificationParameters = searchParameters.getModificationParameters();
ModificationMatch[] modificationMatches = peptide.getVariableModifications();
HashMap<Integer, ArrayList<String>> expectedNames = new HashMap<>(modificationMatches.length);
HashMap<ModificationMatch, ArrayList<String>> modNames = new HashMap<>(modificationMatches.length);
for (ModificationMatch modMatch : modificationMatches) {
HashMap<Integer, HashSet<String>> tempNames = ModificationNameMapper.getPossibleModificationNames(peptide, modMatch, fileReader, searchParameters, modificationSequenceMatchingParameters, sequenceProvider, modificationFactory);
HashSet<String> allNames = tempNames.values().stream().flatMap(nameList -> nameList.stream()).collect(Collectors.toCollection(HashSet::new));
modNames.put(modMatch, new ArrayList<>(allNames));
for (int pos : tempNames.keySet()) {
ArrayList<String> namesAtPosition = expectedNames.get(pos);
if (namesAtPosition == null) {
namesAtPosition = new ArrayList<>(2);
expectedNames.put(pos, namesAtPosition);
}
for (String modName : tempNames.get(pos)) {
if (!namesAtPosition.contains(modName)) {
namesAtPosition.add(modName);
}
}
}
}
if (peptide.getVariableModifications().length > 0) {
ModificationLocalizationMapper.modificationLocalization(peptide, expectedNames, modNames, identificationParameters, fileReader, modificationFactory);
}
if (peptideAssumptionFilter.validateModifications(peptide, sequenceMatchingParameters, modificationSequenceMatchingParameters, searchParameters.getModificationParameters())) {
// Set peptide key
peptide.setKey(Peptide.getKey(peptide.getSequence(), peptide.getVariableModifications()));
// Estimate mass
peptide.getMass(modificationParameters, sequenceProvider, modificationSequenceMatchingParameters);
// Add new assumption
newAssumptions.add(peptideAssumption);
// Get protein count
for (String protein : peptide.getProteinMapping().navigableKeySet()) {
Integer count = proteinCount.get(protein);
if (count != null) {
proteinCount.put(protein, count + 1);
} else {
proteinCount.put(protein, 1);
}
}
} else {
modificationIssue++;
}
} else {
peptideIssue++;
}
}
if (!newAssumptions.isEmpty()) {
assumptionsForAdvocate.put(score, newAssumptions);
} else {
assumptionsForAdvocate.remove(score);
}
}
}
}
use of com.compomics.util.experiment.biology.modifications.ModificationFactory in project peptide-shaker by compomics.
the class DeepLcExport method deepLcExport.
/**
* Exports a DeepLC training file for the given spectrum file.
*
* @param destinationFile The file where to write the export.
* @param confidentHitsDestinationFile The file where to write the export
* for confident hits.
* @param keys The keys of the spectrum matches.
* @param identification The identification object containing the matches.
* @param modificationParameters The modification parameters.
* @param sequenceMatchingParameters The sequence matching parameters.
* @param sequenceProvider The sequence provider.
* @param spectrumProvider The spectrum provider.
* @param waitingHandler The waiting handler.
*/
public static void deepLcExport(File destinationFile, File confidentHitsDestinationFile, HashSet<Long> keys, Identification identification, ModificationParameters modificationParameters, SequenceMatchingParameters sequenceMatchingParameters, SequenceProvider sequenceProvider, SpectrumProvider spectrumProvider, WaitingHandler waitingHandler) {
ModificationFactory modificationFactory = ModificationFactory.getInstance();
HashSet<Long> processedPeptideKeys = new HashSet<>();
SimpleSemaphore writingSemaphore = new SimpleSemaphore(1);
try (SimpleFileWriter writer = new SimpleFileWriter(destinationFile, true)) {
writer.writeLine("seq,modifications,tr");
try (SimpleFileWriter writerConfident = new SimpleFileWriter(confidentHitsDestinationFile, true)) {
writerConfident.writeLine("seq,modifications,tr");
long[] spectrumKeys = keys.stream().mapToLong(a -> a).toArray();
SpectrumMatchesIterator spectrumMatchesIterator = identification.getSpectrumMatchesIterator(spectrumKeys, waitingHandler);
SpectrumMatch spectrumMatch;
while ((spectrumMatch = spectrumMatchesIterator.next()) != null) {
// Display progress
if (waitingHandler != null) {
waitingHandler.increaseSecondaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
}
// Measured retention time
String spectrumFile = spectrumMatch.getSpectrumFile();
String spectrumTitle = spectrumMatch.getSpectrumTitle();
Precursor precursor = spectrumProvider.getPrecursor(spectrumFile, spectrumTitle);
double retentionTime = precursor.rt;
// Export all candidate peptides
spectrumMatch.getAllPeptideAssumptions().parallel().forEach(peptideAssumption -> writePeptideCandidate(peptideAssumption, retentionTime, modificationParameters, sequenceProvider, sequenceMatchingParameters, modificationFactory, processedPeptideKeys, writingSemaphore, writer));
// Check whether the spectrum yielded a confident peptide
if (spectrumMatch.getBestPeptideAssumption() != null && ((PSParameter) spectrumMatch.getUrParam(PSParameter.dummy)).getMatchValidationLevel().isValidated()) {
// Export the confident peptide to the confident peptides file
writePeptideCandidate(spectrumMatch.getBestPeptideAssumption(), retentionTime, modificationParameters, sequenceProvider, sequenceMatchingParameters, modificationFactory, processedPeptideKeys, writingSemaphore, writerConfident);
}
}
}
}
}
use of com.compomics.util.experiment.biology.modifications.ModificationFactory in project peptide-shaker by compomics.
the class Ms2PipExport method ms2pipExport.
/**
* Exports a ms2pip training file for the given spectrum file.
*
* @param peprecFile The file where to write the export.
* @param identification The identification object containing the matches.
* @param modificationParameters The modification parameters.
* @param sequenceMatchingParameters The sequence matching parameters.
* @param sequenceProvider The sequence provider.
* @param spectrumProvider The spectrum provider.
* @param waitingHandler The waiting handler.
*/
public static void ms2pipExport(File peprecFile, Identification identification, ModificationParameters modificationParameters, SequenceMatchingParameters sequenceMatchingParameters, SequenceProvider sequenceProvider, SpectrumProvider spectrumProvider, WaitingHandler waitingHandler) {
ModificationFactory modificationFactory = ModificationFactory.getInstance();
try (SimpleFileWriter writer = new SimpleFileWriter(peprecFile, true)) {
writer.writeLine("spec_id modifications peptide charge");
SpectrumMatchesIterator spectrumMatchesIterator = identification.getSpectrumMatchesIterator(waitingHandler);
HashSet<Long> processedPeptideKeys = new HashSet<>();
SimpleSemaphore writingSemaphore = new SimpleSemaphore(1);
SpectrumMatch spectrumMatch;
while ((spectrumMatch = spectrumMatchesIterator.next()) != null) {
// Display progress
if (waitingHandler != null) {
waitingHandler.increaseSecondaryProgressCounter();
if (waitingHandler.isRunCanceled()) {
return;
}
}
// Export all candidate peptides
spectrumMatch.getAllPeptideAssumptions().parallel().forEach(peptideAssumption -> writePeptideCandidate(peptideAssumption, modificationParameters, sequenceProvider, sequenceMatchingParameters, modificationFactory, processedPeptideKeys, writingSemaphore, writer));
}
}
}
Aggregations