use of com.compomics.util.math.statistics.distributions.NonSymmetricalNormalDistribution in project peptide-shaker by compomics.
the class OverviewPanel method updateSequenceCoverage.
/**
* Updates the sequence coverage panel.
*
* @param proteinAccession the protein accession
* @param updateProtein if true, force a complete recreation of the plot
*/
private void updateSequenceCoverage(long proteinKey, String proteinAccession, boolean updateProtein) {
// @TODO: should be in a separate thread that is possible to cancel if the selection changes
try {
// only need to redo this if the protein changes
if (updateProtein || !proteinAccession.equalsIgnoreCase(currentProteinAccession) || coverage == null) {
updateProteinSequenceCoveragePanelTitle(proteinAccession);
updatePtmCoveragePlot(proteinAccession);
updatePeptideVariationsCoveragePlot(proteinAccession);
}
currentProteinAccession = proteinAccession;
SearchParameters searchParameters = peptideShakerGUI.getIdentificationParameters().getSearchParameters();
ArrayList<Integer> selectedPeptideStart = new ArrayList<>();
int selectionLength = 0;
if (peptideTable.getSelectedRow() != -1) {
SelfUpdatingTableModel tableModel = (SelfUpdatingTableModel) peptideTable.getModel();
int peptideIndex = tableModel.getViewIndex(peptideTable.getSelectedRow());
long peptideKey = peptideKeys[peptideIndex];
PeptideMatch peptideMatch = peptideShakerGUI.getIdentification().getPeptideMatch(peptideKey);
String peptideSequence = peptideMatch.getPeptide().getSequence();
selectionLength = peptideSequence.length();
try {
for (int startIndex : peptideMatch.getPeptide().getProteinMapping().get(currentProteinAccession)) {
selectedPeptideStart.add(startIndex);
}
} catch (Exception e) {
// ignore errors due to the user switching too quickly between rows. seems to solve themselves anyway
}
}
IdentificationFeaturesGenerator identificationFeaturesGenerator = peptideShakerGUI.getIdentificationFeaturesGenerator();
int[] validationCoverage;
if (coverageShowAllPeptidesJRadioButtonMenuItem.isSelected()) {
validationCoverage = identificationFeaturesGenerator.getAACoverage(proteinKey);
} else {
validationCoverage = identificationFeaturesGenerator.estimateAACoverage(proteinKey, coverageShowEnzymaticPeptidesOnlyJRadioButtonMenuItem.isSelected());
}
double minHeight = 0.2, maxHeight = 1;
NonSymmetricalNormalDistribution peptideLengthDistribution = peptideShakerGUI.getMetrics().getPeptideLengthDistribution();
if (peptideLengthDistribution != null) {
double medianLength = peptideLengthDistribution.getMean();
maxHeight = (1 - minHeight) * peptideLengthDistribution.getProbabilityAt(medianLength);
}
double[] coverageLikelihood = identificationFeaturesGenerator.getCoverableAA(proteinKey);
double[] coverageHeight = new double[coverageLikelihood.length];
for (int i = 0; i < coverageLikelihood.length; i++) {
double p = coverageLikelihood[i];
coverageHeight[i] = minHeight + p / maxHeight;
}
HashMap<Integer, Color> colors = new HashMap<>();
colors.put(MatchValidationLevel.confident.getIndex(), peptideShakerGUI.getSparklineColor());
colors.put(MatchValidationLevel.doubtful.getIndex(), peptideShakerGUI.getUtilitiesUserParameters().getSparklineColorDoubtful());
colors.put(MatchValidationLevel.not_validated.getIndex(), peptideShakerGUI.getSparklineColorNonValidated());
colors.put(MatchValidationLevel.none.getIndex(), peptideShakerGUI.getSparklineColorNotFound());
int userSelectionIndex = 0;
while (colors.containsKey(userSelectionIndex)) {
userSelectionIndex++;
}
// @TODO: use non hard coded value
colors.put(userSelectionIndex, Color.blue);
int[] coverageColor = validationCoverage.clone();
for (int aaStart : selectedPeptideStart) {
for (int aa = aaStart; aa < aaStart + selectionLength; aa++) {
coverageColor[aa] = userSelectionIndex;
}
}
// Dirty fix until the width of the sparkline can change
int transparentIndex = userSelectionIndex + 1;
colors.put(userSelectionIndex + 1, new Color(0, 0, 0, 0));
for (int aa = 0; aa < coverageHeight.length; aa++) {
if (coverageColor[aa] == MatchValidationLevel.none.getIndex()) {
if (coverageLikelihood[aa] < 0.01 || !coverageShowPossiblePeptidesJCheckBoxMenuItem.isSelected()) {
// NOTE: if the fix is removed, make sure that this line is kept!!!
coverageColor[aa] = transparentIndex;
}
}
}
// create the coverage plot
ArrayList<JSparklinesDataSeries> sparkLineDataSeriesCoverage = ProteinSequencePanel.getSparkLineDataSeriesCoverage(coverageHeight, coverageColor, colors);
HashMap<Integer, ArrayList<ResidueAnnotation>> proteinTooltips = peptideShakerGUI.getDisplayFeaturesGenerator().getResidueAnnotation(proteinKey, peptideShakerGUI.getIdentificationParameters().getSequenceMatchingParameters(), identificationFeaturesGenerator, peptideShakerGUI.getMetrics(), peptideShakerGUI.getIdentification(), coverageShowAllPeptidesJRadioButtonMenuItem.isSelected(), searchParameters, coverageShowEnzymaticPeptidesOnlyJRadioButtonMenuItem.isSelected());
// Dirty fix for a bloc-level annotation
HashMap<Integer, ArrayList<ResidueAnnotation>> blocTooltips = new HashMap<>();
int aaCpt = 0, blocCpt = 0;
for (JSparklinesDataSeries jSparklinesDataSeries : sparkLineDataSeriesCoverage) {
double sparkLineLength = jSparklinesDataSeries.getData().get(0);
ArrayList<ResidueAnnotation> blocAnnotation = new ArrayList<>();
for (int j = 0; j < sparkLineLength; j++, aaCpt++) {
ArrayList<ResidueAnnotation> aaAnnotation = proteinTooltips.get(aaCpt);
if (aaAnnotation != null) {
for (ResidueAnnotation residueAnnotation : aaAnnotation) {
if (!blocAnnotation.contains(residueAnnotation)) {
blocAnnotation.add(residueAnnotation);
}
}
}
}
blocTooltips.put(blocCpt, blocAnnotation);
blocCpt++;
}
proteinSequencePanel = new ProteinSequencePanel(Color.WHITE);
coverageChart = proteinSequencePanel.getSequencePlot(this, new JSparklinesDataset(sparkLineDataSeriesCoverage), blocTooltips, true, true);
// make sure that the range is the same for all the sequence annotation charts
coverageChart.getChart().addChangeListener(new ChartChangeListener() {
@Override
public void chartChanged(ChartChangeEvent cce) {
if (ptmChart != null) {
Range range = ((CategoryPlot) coverageChart.getChart().getPlot()).getRangeAxis().getRange();
((CategoryPlot) ptmChart.getChart().getPlot()).getRangeAxis().setRange(range);
ptmChart.revalidate();
ptmChart.repaint();
}
if (peptideVariationsChart != null) {
Range range = ((CategoryPlot) coverageChart.getChart().getPlot()).getRangeAxis().getRange();
((CategoryPlot) peptideVariationsChart.getChart().getPlot()).getRangeAxis().setRange(range);
peptideVariationsChart.revalidate();
peptideVariationsChart.repaint();
}
}
});
sequenceCoverageInnerPanel.removeAll();
sequenceCoverageInnerPanel.add(coverageChart);
sequenceCoverageInnerPanel.revalidate();
sequenceCoverageInnerPanel.repaint();
} catch (ClassCastException e) {
// ignore @TODO: this should not happen, but can happen if the table does not update fast enough for the filtering
}
}
use of com.compomics.util.math.statistics.distributions.NonSymmetricalNormalDistribution in project peptide-shaker by compomics.
the class MatchesValidator method validateIdentifications.
/**
* This method validates the identification matches of an identification
* object. Target Decoy thresholds must be set.
*
* @param identification The identification class containing the matches to
* validate.
* @param metrics If provided, metrics on fractions will be saved while
* iterating the matches.
* @param geneMaps The gene maps.
* @param inputMap The target decoy map of all search engine scores.
* @param waitingHandler The waiting handler displaying progress to the user
* and allowing canceling the process.
* @param exceptionHandler The handler for exceptions.
* @param identificationFeaturesGenerator The identification features
* generator computing information about the identification matches.
* @param sequenceProvider The protein sequence provider.
* @param proteinDetailsProvider The protein details provider.
* @param spectrumProvider The spectrum provider.
* @param identificationParameters The identification parameters.
* @param projectType The project type.
* @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 validateIdentifications(Identification identification, Metrics metrics, InputMap inputMap, WaitingHandler waitingHandler, ExceptionHandler exceptionHandler, IdentificationFeaturesGenerator identificationFeaturesGenerator, SequenceProvider sequenceProvider, ProteinDetailsProvider proteinDetailsProvider, SpectrumProvider spectrumProvider, GeneMaps geneMaps, IdentificationParameters identificationParameters, ProjectType projectType, ProcessingParameters processingParameters) throws InterruptedException, TimeoutException {
IdMatchValidationParameters validationParameters = identificationParameters.getIdValidationParameters();
waitingHandler.setWaitingText("Finding FDR Thresholds. Please Wait...");
for (int algorithm : inputMap.getInputAlgorithms()) {
TargetDecoyMap targetDecoyMap = inputMap.getTargetDecoyMap(algorithm);
TargetDecoyResults currentResults = targetDecoyMap.getTargetDecoyResults();
currentResults.setInputType(1);
currentResults.setUserInput(validationParameters.getDefaultPsmFDR());
currentResults.setFdrLimit(validationParameters.getDefaultPsmFDR());
targetDecoyMap.getTargetDecoySeries().getFDRResults(currentResults);
}
TargetDecoyResults currentResults = psmMap.getTargetDecoyResults();
currentResults.setInputType(1);
currentResults.setUserInput(validationParameters.getDefaultPsmFDR());
currentResults.setFdrLimit(validationParameters.getDefaultPsmFDR());
psmMap.getTargetDecoySeries().getFDRResults(currentResults);
waitingHandler.setSecondaryProgressCounterIndeterminate(false);
currentResults = peptideMap.getTargetDecoyResults();
currentResults.setInputType(1);
currentResults.setUserInput(validationParameters.getDefaultPeptideFDR());
currentResults.setFdrLimit(validationParameters.getDefaultPeptideFDR());
peptideMap.getTargetDecoySeries().getFDRResults(currentResults);
currentResults = proteinMap.getTargetDecoyResults();
currentResults.setInputType(1);
currentResults.setUserInput(validationParameters.getDefaultProteinFDR());
currentResults.setFdrLimit(validationParameters.getDefaultProteinFDR());
proteinMap.getTargetDecoySeries().getFDRResults(currentResults);
ValidationQcParameters validationQCParameters = validationParameters.getValidationQCParameters();
waitingHandler.setWaitingText("Match Validation and Quality Control. Please Wait...");
waitingHandler.setSecondaryProgressCounterIndeterminate(false);
waitingHandler.resetSecondaryProgressCounter();
waitingHandler.setMaxSecondaryProgressCounter(identification.getProteinIdentification().size() + identification.getPeptideIdentification().size() + 2 * identification.getSpectrumIdentificationSize());
// validate the spectrum matches
if (inputMap != null) {
inputMap.resetAdvocateContributions();
}
AnnotationParameters annotationParameters = identificationParameters.getAnnotationParameters();
double intensityLimit = annotationParameters.getAnnotationIntensityLimit();
annotationParameters.setIntensityLimit(0);
ExecutorService pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
SpectrumMatchesIterator psmIterator = identification.getSpectrumMatchesIterator(waitingHandler);
ArrayList<PsmValidatorRunnable> psmRunnables = new ArrayList<>(processingParameters.getnThreads());
for (int i = 1; i <= processingParameters.getnThreads(); i++) {
PsmValidatorRunnable runnable = new PsmValidatorRunnable(psmIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, identificationParameters, waitingHandler, exceptionHandler, inputMap, false, true);
pool.submit(runnable);
psmRunnables.add(runnable);
}
if (waitingHandler.isRunCanceled()) {
pool.shutdownNow();
return;
}
pool.shutdown();
if (!pool.awaitTermination(identification.getSpectrumIdentificationSize(), TimeUnit.MINUTES)) {
throw new TimeoutException("Spectrum matches validation timed out. Please contact the developers.");
}
// combine the precursor mz deviations from the different threads into one map
HashMap<String, ArrayList<Double>> precursorMzDeviations = new HashMap<>(identification.getSpectrumIdentification().size());
for (PsmValidatorRunnable runnable : psmRunnables) {
for (String spectrumFileName : runnable.getThreadPrecursorMzDeviations().keySet()) {
ArrayList<Double> threadPrecursorMzDeviations = runnable.getThreadPrecursorMzDeviations().get(spectrumFileName);
ArrayList<Double> filePrecursorMzDeviations = precursorMzDeviations.get(spectrumFileName);
if (filePrecursorMzDeviations != null) {
filePrecursorMzDeviations.addAll(threadPrecursorMzDeviations);
} else {
precursorMzDeviations.put(spectrumFileName, threadPrecursorMzDeviations);
}
}
}
for (String spectrumFileName : precursorMzDeviations.keySet()) {
double[] precursorMzDeviationsFile = precursorMzDeviations.get(spectrumFileName).stream().mapToDouble(a -> a).toArray();
if (precursorMzDeviationsFile.length >= 100) {
Arrays.sort(precursorMzDeviationsFile);
identificationFeaturesGenerator.setMassErrorDistribution(spectrumFileName, precursorMzDeviationsFile);
} else {
// There are not enough precursors, disable probabilistic precursor filter
if (validationQCParameters.getPsmFilters() != null) {
for (Filter filter : validationQCParameters.getPsmFilters()) {
PsmFilter psmFilter = (PsmFilter) filter;
if (psmFilter.getItemsNames().contains(AssumptionFilterItem.precrusorMzErrorStat.name)) {
psmFilter.removeFilterItem(AssumptionFilterItem.precrusorMzErrorStat.name);
SearchParameters searchParameters = identificationParameters.getSearchParameters();
if (searchParameters.isPrecursorAccuracyTypePpm()) {
psmFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorPpm.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
} else {
psmFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorDa.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
}
}
AssumptionFilter assumptionFilter = psmFilter.getAssumptionFilter();
if (assumptionFilter.getItemsNames().contains(AssumptionFilterItem.precrusorMzErrorStat.name)) {
assumptionFilter.removeFilterItem(AssumptionFilterItem.precrusorMzErrorStat.name);
SearchParameters searchParameters = identificationParameters.getSearchParameters();
if (searchParameters.isPrecursorAccuracyTypePpm()) {
assumptionFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorPpm.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
} else {
assumptionFilter.setFilterItem(AssumptionFilterItem.precrusorMzErrorDa.name, FilterItemComparator.lowerOrEqual, searchParameters.getPrecursorAccuracy());
}
}
}
}
}
}
pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
psmIterator = identification.getSpectrumMatchesIterator(waitingHandler);
for (int i = 1; i <= processingParameters.getnThreads(); i++) {
PsmValidatorRunnable runnable = new PsmValidatorRunnable(psmIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, identificationParameters, waitingHandler, exceptionHandler, inputMap, true, false);
pool.submit(runnable);
}
if (waitingHandler.isRunCanceled()) {
pool.shutdownNow();
return;
}
pool.shutdown();
if (!pool.awaitTermination(identification.getSpectrumIdentificationSize(), TimeUnit.MINUTES)) {
throw new TimeoutException("Spectrum matches validation timed out. Please contact the developers.");
}
annotationParameters.setIntensityLimit(intensityLimit);
if (projectType == ProjectType.peptide || projectType == ProjectType.protein) {
// validate the peptides
pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
ArrayList<PeptideValidatorRunnable> peptideRunnables = new ArrayList<>(processingParameters.getnThreads());
PeptideMatchesIterator peptideMatchesIterator = identification.getPeptideMatchesIterator(waitingHandler);
for (int i = 1; i <= processingParameters.getnThreads(); i++) {
PeptideValidatorRunnable runnable = new PeptideValidatorRunnable(peptideMatchesIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, identificationParameters, waitingHandler, exceptionHandler, metrics);
pool.submit(runnable);
peptideRunnables.add(runnable);
}
if (waitingHandler.isRunCanceled()) {
pool.shutdownNow();
return;
}
pool.shutdown();
if (!pool.awaitTermination(identification.getPeptideIdentification().size(), TimeUnit.MINUTES)) {
throw new InterruptedException("Peptide matches validation timed out. Please contact the developers.");
}
HashMap<String, Integer> validatedTotalPeptidesPerFraction = new HashMap<>();
ArrayList<Double> validatedPeptideLengths = new ArrayList<>();
for (PeptideValidatorRunnable runnable : peptideRunnables) {
HashMap<String, Integer> threadValidatedTotalPeptidesPerFraction = runnable.getValidatedTotalPeptidesPerFraction();
for (String fraction : threadValidatedTotalPeptidesPerFraction.keySet()) {
Integer nValidated = validatedTotalPeptidesPerFraction.get(fraction);
if (nValidated == null) {
nValidated = 0;
}
nValidated += threadValidatedTotalPeptidesPerFraction.get(fraction);
validatedTotalPeptidesPerFraction.put(fraction, nValidated);
}
validatedPeptideLengths.addAll(runnable.getValidatedPeptideLengths());
}
if (validatedPeptideLengths.size() >= 100) {
NonSymmetricalNormalDistribution lengthDistribution = NonSymmetricalNormalDistribution.getRobustNonSymmetricalNormalDistribution(validatedPeptideLengths);
metrics.setPeptideLengthDistribution(lengthDistribution);
}
metrics.setTotalPeptidesPerFraction(validatedTotalPeptidesPerFraction);
if (projectType == ProjectType.protein) {
// validate the proteins
pool = Executors.newFixedThreadPool(processingParameters.getnThreads());
ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(waitingHandler);
ArrayList<ProteinValidatorRunnable> proteinRunnables = new ArrayList<>(processingParameters.getnThreads());
for (int i = 1; i <= processingParameters.getnThreads(); i++) {
ProteinValidatorRunnable runnable = new ProteinValidatorRunnable(proteinMatchesIterator, identification, identificationFeaturesGenerator, sequenceProvider, proteinDetailsProvider, spectrumProvider, geneMaps, metrics, identificationParameters, waitingHandler, exceptionHandler);
pool.submit(runnable);
proteinRunnables.add(runnable);
}
if (waitingHandler.isRunCanceled()) {
pool.shutdownNow();
return;
}
pool.shutdown();
if (!pool.awaitTermination(identification.getProteinIdentification().size(), TimeUnit.MINUTES)) {
throw new InterruptedException("Protein matches validation timed out. Please contact the developers.");
}
long[] validatedTargetProteinKeys = proteinRunnables.stream().flatMap(runnable -> runnable.getValidatedProteinMatches().stream()).mapToLong(a -> a).toArray();
metrics.setValidatedTargetProteinKeys(validatedTargetProteinKeys);
}
}
}
Aggregations