use of com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator in project peptide-shaker by compomics.
the class QCPanel method getProteinDataset.
/**
* Returns the dataset to use for the protein QC plot.
*/
private void getProteinDataset() {
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(peptideShakerGUI.getIdentification().getProteinIdentification().size());
progressDialog.setValue(0);
IdentificationFeaturesGenerator identificationFeaturesGenerator = peptideShakerGUI.getIdentificationFeaturesGenerator();
maxValue = Double.MIN_VALUE;
validatedValues = new ArrayList<>();
validatedDoubtfulValues = new ArrayList<>();
nonValidatedValues = new ArrayList<>();
validatedDecoyValues = new ArrayList<>();
nonValidatedDecoyValues = new ArrayList<>();
ProteinMatchesIterator proteinMatchesIterator = peptideShakerGUI.getIdentification().getProteinMatchesIterator(progressDialog);
ProteinMatch proteinMatch;
while ((proteinMatch = proteinMatchesIterator.next()) != null) {
long proteinKey = proteinMatch.getKey();
if (progressDialog.isRunCanceled()) {
break;
}
double value = 0;
if (proteinNumberValidatedPeptidesJRadioButton.isSelected()) {
value = identificationFeaturesGenerator.getNValidatedPeptides(proteinKey);
} else if (proteinSpectrumCountingScoreJRadioButton.isSelected()) {
value = identificationFeaturesGenerator.getSpectrumCounting(proteinKey);
} else if (proteinSequenceCoverageJRadioButton.isSelected()) {
HashMap<Integer, Double> sequenceCoverage = peptideShakerGUI.getIdentificationFeaturesGenerator().getSequenceCoverage(proteinKey);
Double sequenceCoverageConfident = 100 * sequenceCoverage.get(MatchValidationLevel.confident.getIndex());
Double sequenceCoverageDoubtful = 100 * sequenceCoverage.get(MatchValidationLevel.doubtful.getIndex());
value = sequenceCoverageConfident + sequenceCoverageDoubtful;
} else if (proteinSequenceLengthJRadioButton.isSelected()) {
String proteinSequence = peptideShakerGUI.getSequenceProvider().getSequence(proteinMatch.getLeadingAccession());
value = proteinSequence.length();
}
PSParameter proteinParameter = (PSParameter) proteinMatch.getUrParam(PSParameter.dummy);
if (!proteinParameter.getHidden()) {
if (value > maxValue) {
maxValue = value;
}
if (!proteinMatch.isDecoy()) {
if (proteinParameter.getMatchValidationLevel().isValidated()) {
if (proteinParameter.getMatchValidationLevel() == MatchValidationLevel.confident) {
validatedValues.add(value);
} else {
validatedDoubtfulValues.add(value);
}
} else {
nonValidatedValues.add(value);
}
}
}
progressDialog.increasePrimaryProgressCounter();
}
}
use of com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator in project peptide-shaker by compomics.
the class ProteinProcessor method processProteins.
/**
* Scores the PTMs of all protein matches contained in an identification
* object, estimates spectrum counting and summary statistics.
*
* @param modificationLocalizationScorer The modification localization
* scorer to use.
* @param metrics If provided, metrics on proteins will be saved while
* iterating the matches.
* @param modificationProvider The modification provider to use.
* @param waitingHandler The handler displaying feedback to the user.
* @param exceptionHandler The exception handler to use.
* @param processingParameters The processing parameters.
*
* @throws java.lang.InterruptedException exception thrown if a thread gets
* interrupted
* @throws java.util.concurrent.TimeoutException exception thrown if the
* operation times out
*/
public void processProteins(ModificationLocalizationScorer modificationLocalizationScorer, Metrics metrics, ModificationProvider modificationProvider, WaitingHandler waitingHandler, ExceptionHandler exceptionHandler, ProcessingParameters processingParameters) throws InterruptedException, TimeoutException {
waitingHandler.setWaitingText("Scoring Protein Modification Localization. Please Wait...");
int max = identification.getProteinIdentification().size();
waitingHandler.setSecondaryProgressCounterIndeterminate(false);
waitingHandler.setMaxSecondaryProgressCounter(max);
// validate the proteins
ExecutorService pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(waitingHandler);
ArrayList<ProteinRunnable> runnables = new ArrayList<>(processingParameters.getnThreads());
for (int i = 1; i <= processingParameters.getnThreads(); i++) {
ProteinRunnable runnable = new ProteinRunnable(proteinMatchesIterator, modificationLocalizationScorer, modificationProvider, waitingHandler, exceptionHandler);
pool.submit(runnable);
runnables.add(runnable);
}
if (waitingHandler.isRunCanceled()) {
pool.shutdownNow();
}
pool.shutdown();
if (!pool.awaitTermination(identification.getProteinIdentification().size(), TimeUnit.MINUTES)) {
throw new InterruptedException("Protein matches validation timed out. Please contact the developers.");
}
waitingHandler.setSecondaryProgressCounterIndeterminate(true);
if (metrics != null) {
metrics.setMaxSpectrumCounting(runnables.stream().mapToDouble(ProteinRunnable::getMaxSpectrumCounting).sum());
metrics.setnValidatedProteins(runnables.stream().mapToInt(ProteinRunnable::getnValidatedProteins).sum());
metrics.setnConfidentProteins(runnables.stream().mapToInt(ProteinRunnable::getnConfidentProteins).sum());
metrics.setMaxNPeptides(runnables.stream().mapToInt(ProteinRunnable::getMaxPeptides).max().orElse(0));
metrics.setMaxNPsms(runnables.stream().mapToInt(ProteinRunnable::getMaxPsms).max().orElse(0));
metrics.setMaxMW(runnables.stream().mapToDouble(ProteinRunnable::getMaxMW).max().orElse(0.0));
metrics.setMaxProteinAccessionLength(runnables.stream().mapToInt(ProteinRunnable::getMaxProteinAccessionLength).max().orElse(0));
TreeMap<Double, TreeMap<Integer, TreeMap<Integer, TreeSet<Long>>>> orderMap1 = new TreeMap<>();
for (int i = 0; i < runnables.size(); i++) {
HashMap<Double, HashMap<Integer, HashMap<Integer, HashSet<Long>>>> threadMap1 = runnables.get(i).getOrderMap();
for (Entry<Double, HashMap<Integer, HashMap<Integer, HashSet<Long>>>> entry1 : threadMap1.entrySet()) {
double key1 = entry1.getKey();
HashMap<Integer, HashMap<Integer, HashSet<Long>>> threadMap2 = entry1.getValue();
TreeMap<Integer, TreeMap<Integer, TreeSet<Long>>> orderMap2 = orderMap1.get(key1);
if (orderMap2 == null) {
orderMap2 = new TreeMap<>();
orderMap1.put(key1, orderMap2);
}
for (Entry<Integer, HashMap<Integer, HashSet<Long>>> entry2 : threadMap2.entrySet()) {
int key2 = entry2.getKey();
HashMap<Integer, HashSet<Long>> threadMap3 = entry2.getValue();
TreeMap<Integer, TreeSet<Long>> orderMap3 = orderMap2.get(key2);
if (orderMap3 == null) {
orderMap3 = new TreeMap<>();
orderMap2.put(key2, orderMap3);
}
for (Entry<Integer, HashSet<Long>> entry3 : threadMap3.entrySet()) {
int key3 = entry3.getKey();
HashSet<Long> threadSet = entry3.getValue();
TreeSet<Long> orderedSet = orderMap3.get(key3);
if (orderedSet == null) {
orderedSet = new TreeSet<>();
orderMap3.put(key3, orderedSet);
}
orderedSet.addAll(threadSet);
}
}
}
}
long[] proteinKeys = orderMap1.values().stream().flatMap(map -> map.values().stream()).flatMap(map -> map.values().stream()).flatMap(set -> set.stream()).mapToLong(a -> a).toArray();
metrics.setProteinKeys(proteinKeys);
}
}
use of com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator in project peptide-shaker by compomics.
the class StarHider method starHide.
/**
* Updates the star/hide status of all identification items.
*/
public void starHide() {
progressDialog.setPrimaryProgressCounterIndeterminate(true);
progressDialog.setTitle("Hiding/Starring Matches. Please Wait...");
new Thread(new Runnable() {
public void run() {
try {
progressDialog.setVisible(true);
} catch (IndexOutOfBoundsException e) {
// ignore
}
}
}, "ProgressDialog").start();
new Thread("Star/Hide") {
@Override
public void run() {
try {
ExecutorService pool = Executors.newFixedThreadPool(nThreads);
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(identification.getProteinIdentification().size());
ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(progressDialog);
ArrayList<StarHiderRunnable> runnables = new ArrayList<>(nThreads);
for (int i = 1; i <= nThreads && !progressDialog.isRunCanceled(); i++) {
StarHiderRunnable starHiderRunnable = new StarHiderRunnable(proteinMatchesIterator, progressDialog);
pool.submit(starHiderRunnable);
runnables.add(starHiderRunnable);
}
if (progressDialog.isRunCanceled()) {
pool.shutdownNow();
return;
}
pool.shutdown();
if (!pool.awaitTermination(identification.getProteinIdentification().size(), TimeUnit.MINUTES)) {
throw new TimeoutException("Hiding/Starring matches timed out. Please contact the developers.");
}
HashMap<String, ArrayList<Double>> fractionMW = new HashMap<>();
for (StarHiderRunnable starHiderRunnable : runnables) {
HashMap<String, ArrayList<Double>> threadFractionMW = starHiderRunnable.getThreadFractionMW();
for (String fraction : threadFractionMW.keySet()) {
ArrayList<Double> mws = fractionMW.get(fraction), threadMws = threadFractionMW.get(fraction);
if (mws == null) {
fractionMW.put(fraction, threadMws);
} else {
mws.addAll(threadMws);
}
}
}
// set the observed fractional molecular weights per fraction
metrics.setObservedFractionalMassesAll(fractionMW);
progressDialog.setRunFinished();
} catch (Exception e) {
exceptionHandler.catchException(e);
}
}
}.start();
}
use of com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator in project peptide-shaker by compomics.
the class InclusionListExport method exportInclusionList.
/**
* Writes an inclusion list based on the validated PSMs of the validated
* peptides of the validated proteins.
*
* @param destinationFile the file where to write the inclusion list
* @param identification the identification object containing all matches
* and match parameters
* @param identificationFeaturesGenerator the identification features
* generator calculating identification metrics on the fly
* @param spectrumProvider the spectrum provider
* @param proteinFilters the inclusion list protein filters
* @param peptideFilters the inclusion list peptide filters
* @param exportFormat the export format
* @param searchParameters the identification parameters
* @param rtWindow the window to use for retention time
* @param waitingHandler waiting handler displaying progress to the user
* (can be null)
* @param filterPreferences the general filtering preferences of this
* project
*
* @throws IOException thrown if an error occurred while writing the file
*/
public static void exportInclusionList(File destinationFile, Identification identification, IdentificationFeaturesGenerator identificationFeaturesGenerator, SpectrumProvider spectrumProvider, ArrayList<Integer> proteinFilters, ArrayList<PeptideFilterType> peptideFilters, ExportFormat exportFormat, SearchParameters searchParameters, double rtWindow, WaitingHandler waitingHandler, FilterParameters filterPreferences) throws IOException {
if (waitingHandler != null) {
if (waitingHandler.isRunCanceled()) {
return;
}
waitingHandler.setWaitingText("Inclusion List - Writing File. Please Wait...");
waitingHandler.resetSecondaryProgressCounter();
waitingHandler.setMaxSecondaryProgressCounter(identification.getProteinIdentification().size());
}
try (SimpleFileWriter writer = new SimpleFileWriter(destinationFile, false)) {
ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(waitingHandler);
ProteinMatch proteinMatch;
while ((proteinMatch = proteinMatchesIterator.next()) != null) {
PSParameter proteinParameter = (PSParameter) proteinMatch.getUrParam(PSParameter.dummy);
if (!proteinFilters.contains(proteinParameter.getProteinInferenceGroupClass())) {
ArrayList<Long> peptideMatches = new ArrayList<>();
for (long peptideKey : proteinMatch.getPeptideMatchesKeys()) {
PeptideMatch peptideMatch = identification.getPeptideMatch(peptideKey);
PSParameter peptideParameter = (PSParameter) peptideMatch.getUrParam(PSParameter.dummy);
if (peptideParameter.getMatchValidationLevel().isValidated()) {
boolean passesFilter = true;
for (PeptideFilterType filterType : peptideFilters) {
String sequence = peptideMatch.getPeptide().getSequence();
if (filterType == PeptideFilterType.degenerated) {
if (peptideParameter.getProteinInferenceGroupClass() != PSParameter.NOT_GROUP) {
passesFilter = false;
break;
}
} else if (filterType == PeptideFilterType.miscleaved) {
Integer peptideMinMissedCleavages = null;
DigestionParameters digestionPreferences = searchParameters.getDigestionParameters();
if (digestionPreferences.getCleavageParameter() == DigestionParameters.CleavageParameter.enzyme) {
for (Enzyme enzyme : digestionPreferences.getEnzymes()) {
int tempMissedCleavages = enzyme.getNmissedCleavages(sequence);
if (peptideMinMissedCleavages == null || tempMissedCleavages < peptideMinMissedCleavages) {
peptideMinMissedCleavages = tempMissedCleavages;
}
}
}
if (peptideMinMissedCleavages != null && peptideMinMissedCleavages > 0) {
passesFilter = false;
break;
}
} else if (filterType == PeptideFilterType.reactive) {
if (sequence.contains("M") || sequence.contains("C") || sequence.contains("W") || sequence.contains("NG") || sequence.contains("DG") || sequence.contains("QG") || sequence.startsWith("N") || sequence.startsWith("Q")) {
passesFilter = false;
break;
}
}
}
if (passesFilter) {
peptideMatches.add(peptideKey);
}
}
}
if (!peptideMatches.isEmpty()) {
for (long peptideKey : peptideMatches) {
PeptideMatch peptideMatch = identification.getPeptideMatch(peptideKey);
ArrayList<SpectrumMatch> validatedPsms = new ArrayList<>(peptideMatch.getSpectrumCount());
ArrayList<Double> retentionTimes = new ArrayList<>(peptideMatch.getSpectrumCount());
for (long spectrumKey : peptideMatch.getSpectrumMatchesKeys()) {
SpectrumMatch spectrumMatch = identification.getSpectrumMatch(spectrumKey);
if (spectrumMatch.getBestPeptideAssumption() != null) {
PSParameter spectrumParameter = (PSParameter) spectrumMatch.getUrParam(PSParameter.dummy);
if (spectrumParameter.getMatchValidationLevel().isValidated()) {
validatedPsms.add(spectrumMatch);
retentionTimes.add(spectrumProvider.getPrecursorRt(spectrumMatch.getSpectrumFile(), spectrumMatch.getSpectrumTitle()));
}
}
}
if (!validatedPsms.isEmpty()) {
for (SpectrumMatch spectrumMatch : validatedPsms) {
double precursorMz = spectrumProvider.getPrecursorMz(spectrumMatch.getSpectrumFile(), spectrumMatch.getSpectrumTitle());
String line = getInclusionListLine(spectrumMatch, retentionTimes, rtWindow, precursorMz, exportFormat, searchParameters);
writer.writeLine(line);
}
}
}
}
}
if (waitingHandler != null) {
if (waitingHandler.isRunCanceled()) {
return;
}
waitingHandler.increaseSecondaryProgressCounter();
}
}
}
}
use of com.compomics.util.experiment.identification.matches_iterators.ProteinMatchesIterator in project peptide-shaker by compomics.
the class ProteoformExport method writeProteoforms.
/**
* Writes an export with all the possible proteoforms of the validated
* proteins.
*
* @param destinationFile the destination file
* @param identification the identification
* @param waitingHandler the waiting handler
*/
public static void writeProteoforms(File destinationFile, Identification identification, WaitingHandler waitingHandler) {
ModificationFactory modificationFactory = ModificationFactory.getInstance();
if (waitingHandler != null) {
waitingHandler.setWaitingText("Exporting Proteoforms. Please Wait...");
// reset the progress bar
waitingHandler.resetSecondaryProgressCounter();
waitingHandler.setMaxSecondaryProgressCounter(identification.getProteinIdentification().size());
}
ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(waitingHandler);
ProteinMatch proteinMatch;
try (SimpleFileWriter simpleFileWriter = new SimpleFileWriter(destinationFile, false)) {
while ((proteinMatch = proteinMatchesIterator.next()) != null) {
if (!proteinMatch.isDecoy()) {
for (String accession : proteinMatch.getAccessions()) {
boolean unmodified = false;
TreeSet<String> modifications = new TreeSet<>();
for (long peptideKey : proteinMatch.getPeptideMatchesKeys()) {
PeptideMatch peptideMatch = identification.getPeptideMatch(peptideKey);
Peptide peptide = peptideMatch.getPeptide();
ModificationMatch[] modificationMatches = peptide.getVariableModifications();
if (modificationMatches.length > 0) {
int[] peptideStarts = peptide.getProteinMapping().get(accession);
for (int peptideStart : peptideStarts) {
TreeMap<Integer, String> modMap = new TreeMap<>();
for (ModificationMatch modificationMatch : modificationMatches) {
String modName = modificationMatch.getModification();
Modification modification = modificationFactory.getModification(modName);
String modAccession;
// try to map to PSI-MOD or Unimod
if (modification.getPsiModCvTerm() != null) {
modAccession = modification.getPsiModCvTerm().getAccession();
// remove everything but the accession number, i.e. "MOD:00040" ends up as "00040"
modAccession = modAccession.substring(modAccession.indexOf(':') + 1);
} else if (modification.getUnimodCvTerm() != null) {
// not mapping to PSI-MOD, use the Unimod accession number instead
modAccession = modification.getUnimodCvTerm().getAccession();
} else {
// not mapping to PSI-MOD nor Unimod, use the utilties modification name...
modAccession = modName;
}
int site = modificationMatch.getSite();
if (site == 0) {
site = 1;
} else if (site == peptide.getSequence().length() + 1) {
site = peptide.getSequence().length();
}
int siteOnProtein = peptideStart + site;
modMap.put(siteOnProtein, modAccession);
}
String proteoformMods = modMap.entrySet().stream().map(entry -> String.join(":", entry.getValue(), Integer.toString(entry.getKey()))).collect(Collectors.joining(","));
modifications.add(proteoformMods);
}
} else {
unmodified = true;
}
}
if (unmodified) {
simpleFileWriter.writeLine(accession);
}
for (String modification : modifications) {
simpleFileWriter.writeLine(String.join(";", accession, modification));
}
}
}
if (waitingHandler != null) {
if (waitingHandler.isRunCanceled()) {
return;
}
waitingHandler.increaseSecondaryProgressCounter();
}
}
}
}
Aggregations