use of org.jfree.chart.event.ChartChangeListener 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
}
}
Aggregations