use of no.uib.jsparklines.data.ValueAndBooleanDataPoint in project peptide-shaker by compomics.
the class GOEAPanel method updateGoPlots.
/**
* Update the GO plots.
*/
private void updateGoPlots() {
this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
DefaultCategoryDataset frquencyPlotDataset = new DefaultCategoryDataset();
DefaultCategoryDataset significancePlotDataset = new DefaultCategoryDataset();
ArrayList<Color> significanceColors = new ArrayList<>();
Double maxLog2Diff = 0.0;
for (int i = 0; i < goMappingsTable.getRowCount(); i++) {
boolean selected = (Boolean) goMappingsTable.getValueAt(i, goMappingsTable.getColumn(" ").getModelIndex());
boolean significant = ((ValueAndBooleanDataPoint) goMappingsTable.getValueAt(i, goMappingsTable.getColumn("Log2 Diff").getModelIndex())).isSignificant();
if (selected) {
String goTerm = (String) goMappingsTable.getValueAt(i, goMappingsTable.getColumn("GO Term").getModelIndex());
Double percentAll = (Double) goMappingsTable.getValueAt(i, goMappingsTable.getColumn("Frequency All (%)").getModelIndex());
Double percentDataset = (Double) goMappingsTable.getValueAt(i, goMappingsTable.getColumn("Frequency Dataset (%)").getModelIndex());
Double log2Diff = ((ValueAndBooleanDataPoint) goMappingsTable.getValueAt(i, goMappingsTable.getColumn("Log2 Diff").getModelIndex())).getValue();
frquencyPlotDataset.addValue(percentAll, "All", goTerm);
frquencyPlotDataset.addValue(percentDataset, "Dataset", goTerm);
if (!log2Diff.isInfinite()) {
significancePlotDataset.addValue(log2Diff, "Difference", goTerm);
} else {
significancePlotDataset.addValue(0, "Difference", goTerm);
}
if (significant) {
if (log2Diff > 0) {
significanceColors.add(peptideShakerGUI.getSparklineColor());
} else {
significanceColors.add(new Color(255, 51, 51));
}
} else {
significanceColors.add(Color.lightGray);
}
if (!log2Diff.isInfinite() && Math.abs(log2Diff) > maxLog2Diff) {
maxLog2Diff = Math.abs(log2Diff);
}
}
}
maxLog2Diff = Math.ceil(maxLog2Diff);
JFreeChart distributionChart = ChartFactory.createBarChart(null, "GO Terms", "Frequency (%)", frquencyPlotDataset, PlotOrientation.VERTICAL, false, true, true);
distributionChartPanel = new ChartPanel(distributionChart);
((CategoryPlot) distributionChartPanel.getChart().getPlot()).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
BarRenderer3D renderer = new BarRenderer3D(0, 0);
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesPaint(1, peptideShakerGUI.getSparklineColor());
distributionChart.getCategoryPlot().setRenderer(renderer);
// add mouse listener
distributionChartPanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent cme) {
if (cme.getEntity() instanceof CategoryItemEntity) {
CategoryItemEntity categoryItem = (CategoryItemEntity) cme.getEntity();
String columnKey = (String) categoryItem.getColumnKey();
// select and highlight category
boolean categoryFound = false;
for (int i = 0; i < goMappingsTable.getRowCount() && !categoryFound; i++) {
if (((String) goMappingsTable.getValueAt(i, goMappingsTable.getColumn("GO Term").getModelIndex())).equalsIgnoreCase(columnKey)) {
goMappingsTable.setRowSelectionInterval(i, i);
goMappingsTable.scrollRectToVisible(goMappingsTable.getCellRect(i, 0, false));
goMappingsTableMouseReleased(null);
}
}
}
}
@Override
public void chartMouseMoved(ChartMouseEvent cme) {
// do nothing
}
});
// set background color
distributionChart.getPlot().setBackgroundPaint(Color.WHITE);
distributionChart.setBackgroundPaint(Color.WHITE);
distributionChartPanel.setBackground(Color.WHITE);
// hide the outline
distributionChart.getPlot().setOutlineVisible(false);
goFrequencyPlotPanel.removeAll();
goFrequencyPlotPanel.add(distributionChartPanel);
goFrequencyPlotPanel.revalidate();
goFrequencyPlotPanel.repaint();
JFreeChart significanceChart = ChartFactory.createBarChart(null, "GO Terms", "Log2 Difference", significancePlotDataset, PlotOrientation.VERTICAL, false, true, true);
signChartPanel = new ChartPanel(significanceChart);
((CategoryPlot) signChartPanel.getChart().getPlot()).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
((CategoryPlot) signChartPanel.getChart().getPlot()).getRangeAxis().setUpperBound(maxLog2Diff);
((CategoryPlot) signChartPanel.getChart().getPlot()).getRangeAxis().setLowerBound(-maxLog2Diff);
BarChartColorRenderer signRenderer = new BarChartColorRenderer(significanceColors);
signRenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
significanceChart.getCategoryPlot().setRenderer(signRenderer);
// add mouse listener
signChartPanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent cme) {
if (cme.getEntity() instanceof CategoryItemEntity) {
CategoryItemEntity categoryItem = (CategoryItemEntity) cme.getEntity();
String columnKey = (String) categoryItem.getColumnKey();
// select and highlight category
boolean categoryFound = false;
for (int i = 0; i < goMappingsTable.getRowCount() && !categoryFound; i++) {
if (((String) goMappingsTable.getValueAt(i, goMappingsTable.getColumn("GO Term").getModelIndex())).equalsIgnoreCase(columnKey)) {
goMappingsTable.setRowSelectionInterval(i, i);
goMappingsTable.scrollRectToVisible(goMappingsTable.getCellRect(i, 0, false));
goMappingsTableMouseReleased(null);
}
}
}
}
@Override
public void chartMouseMoved(ChartMouseEvent cme) {
// do nothing
}
});
// set background color
significanceChart.getPlot().setBackgroundPaint(Color.WHITE);
significanceChart.setBackgroundPaint(Color.WHITE);
signChartPanel.setBackground(Color.WHITE);
// hide the outline
significanceChart.getPlot().setOutlineVisible(false);
goSignificancePlotPanel.removeAll();
goSignificancePlotPanel.add(signChartPanel);
goSignificancePlotPanel.revalidate();
goSignificancePlotPanel.repaint();
updatePlotMarkers();
this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}
use of no.uib.jsparklines.data.ValueAndBooleanDataPoint in project peptide-shaker by compomics.
the class GOEAPanel method displayResults.
/**
* Update the GO mappings.
*/
public void displayResults() {
if (peptideShakerGUI.getIdentification() != null) {
GeneMaps geneMaps = peptideShakerGUI.getGeneMaps();
if (geneMaps.hasGoMappings()) {
progressDialog = new ProgressDialogX(peptideShakerGUI, Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/peptide-shaker.gif")), Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/peptide-shaker-orange.gif")), true);
progressDialog.setTitle("Getting GO Mappings. Please Wait...");
progressDialog.setPrimaryProgressCounterIndeterminate(true);
new Thread(new Runnable() {
public void run() {
try {
progressDialog.setVisible(true);
} catch (IndexOutOfBoundsException e) {
// ignore
}
}
}, "ProgressDialog").start();
new Thread("GoThread") {
@Override
public void run() {
// clear old table
DefaultTableModel dm = (DefaultTableModel) goMappingsTable.getModel();
dm.getDataVector().removeAllElements();
dm.fireTableDataChanged();
TreeMap<String, Integer> datasetGoTermUsage = new TreeMap<>();
try {
progressDialog.setTitle("Importing GO (1/3). Please Wait...");
GoMapping backgroundGoMapping = new GoMapping();
Integer taxon = null;
IdentificationParameters identificationParameters = peptideShakerGUI.getIdentificationParameters();
GeneParameters genePreferences = identificationParameters.getGeneParameters();
if (genePreferences != null) {
taxon = genePreferences.getBackgroundSpecies();
}
if (taxon == null) {
FastaSummary fastaSummary = FastaSummary.getSummary(peptideShakerGUI.getProjectDetails().getFastaFile(), identificationParameters.getFastaParameters(), progressDialog);
BackgroundSpeciesDialog backgroundSpeciesDialog = new BackgroundSpeciesDialog(peptideShakerGUI, genePreferences, fastaSummary);
if (!backgroundSpeciesDialog.isCanceled()) {
genePreferences = backgroundSpeciesDialog.getGeneParameters();
identificationParameters.setGeneParameters(genePreferences);
taxon = genePreferences.getBackgroundSpecies();
}
}
if (taxon != null) {
SpeciesFactory speciesFactory = SpeciesFactory.getInstance();
String ensemblDatasetName = speciesFactory.getEnsemblDataset(taxon);
File goMappingFile = ProteinGeneDetailsProvider.getGoMappingFile(ensemblDatasetName);
backgroundGoMapping.loadMappingsFromFile(goMappingFile, progressDialog);
GoDomains goDomains = new GoDomains();
File goDomainsFile = ProteinGeneDetailsProvider.getGoDomainsFile();
goDomains.laodMappingFromFile(goDomainsFile, progressDialog);
Identification identification = peptideShakerGUI.getIdentification();
progressDialog.setTitle("Getting GO Mappings (2/3). Please Wait...");
progressDialog.setPrimaryProgressCounterIndeterminate(false);
progressDialog.setMaxPrimaryProgressCounter(identification.getProteinIdentification().size());
progressDialog.setValue(0);
int totalNumberOfGoMappedProteinsInProject = 0;
ProteinMatchesIterator proteinMatchesIterator = identification.getProteinMatchesIterator(progressDialog);
ProteinMatch proteinMatch;
while ((proteinMatch = proteinMatchesIterator.next()) != null) {
PSParameter psParameter = (PSParameter) proteinMatch.getUrParam(PSParameter.dummy);
if (psParameter.getMatchValidationLevel().isValidated() && !proteinMatch.isDecoy() && !psParameter.getHidden()) {
String mainMatch = proteinMatch.getLeadingAccession();
HashSet<String> goTerms = backgroundGoMapping.getGoAccessions(mainMatch);
if (goTerms != null && !goTerms.isEmpty()) {
totalNumberOfGoMappedProteinsInProject++;
for (String goTerm : goTerms) {
Integer usage = datasetGoTermUsage.get(goTerm);
if (usage == null) {
usage = 0;
}
datasetGoTermUsage.put(goTerm, usage + 1);
}
}
}
if (progressDialog.isRunCanceled()) {
return;
}
progressDialog.increasePrimaryProgressCounter();
}
ArrayList<String> termNamesMapped = backgroundGoMapping.getSortedTermNames();
int nBackgroundProteins = backgroundGoMapping.getProteinToGoMap().size();
progressDialog.setTitle("Creating GO Plots (3/3). Please Wait...");
progressDialog.setValue(0);
progressDialog.setMaxPrimaryProgressCounter(termNamesMapped.size());
// update the table
Double maxLog2Diff = 0.0;
ArrayList<Integer> indexes = new ArrayList<>();
ArrayList<Double> pValues = new ArrayList<>();
// display the number of go mapped proteins
goProteinCountLabel.setText("[GO Proteins: Ensembl: " + nBackgroundProteins + ", Project: " + totalNumberOfGoMappedProteinsInProject + "]");
boolean goDomainChanged = false;
for (String goTermName : termNamesMapped) {
if (progressDialog.isRunCanceled()) {
break;
}
String goAccession = backgroundGoMapping.getTermAccession(goTermName);
Integer frequencyBackground = backgroundGoMapping.getProteinAccessions(goAccession).size();
Integer frequencyDataset = 0;
Double percentDataset = 0.0;
if (datasetGoTermUsage.get(goAccession) != null) {
frequencyDataset = datasetGoTermUsage.get(goAccession);
percentDataset = ((double) frequencyDataset) * 100 / totalNumberOfGoMappedProteinsInProject;
}
Double percentAll = ((double) frequencyBackground) * 100 / nBackgroundProteins;
Double pValue = new HypergeometricDistributionImpl(// population size
nBackgroundProteins, // number of successes
frequencyBackground, // sample size
totalNumberOfGoMappedProteinsInProject).probability(frequencyDataset);
Double log2Diff = Math.log(percentDataset / percentAll) / Math.log(2);
if (!log2Diff.isInfinite() && Math.abs(log2Diff) > maxLog2Diff) {
maxLog2Diff = Math.abs(log2Diff);
}
String goDomain = goDomains.getTermDomain(goAccession);
if (goDomain == null) {
// URL to the JSON file for the given GO term
URL u = new URL("https://www.ebi.ac.uk/QuickGO/services/ontology/go/terms/" + goAccession);
JsonMarshaller jsonMarshaller = new JsonMarshaller();
QuickGoTerm result = (QuickGoTerm) jsonMarshaller.fromJson(QuickGoTerm.class, u);
// get the domain
for (DummyResults tempResult : result.results) {
goDomain = tempResult.aspect;
}
// add the domain to the list
goDomains.addDomain(goAccession, goDomain);
goDomainChanged = true;
}
// add the data points for the first data series
ArrayList<Double> dataAll = new ArrayList<>();
dataAll.add(percentAll);
ArrayList<Double> dataDataset = new ArrayList<>();
dataDataset.add(percentDataset);
// create a JSparklineDataSeries
JSparklinesDataSeries sparklineDataseriesAll = new JSparklinesDataSeries(dataAll, Color.RED, "All");
JSparklinesDataSeries sparklineDataseriesDataset = new JSparklinesDataSeries(dataDataset, peptideShakerGUI.getSparklineColor(), "Dataset");
// add the data series to JSparklineDataset
ArrayList<JSparklinesDataSeries> sparkLineDataSeries = new ArrayList<>();
sparkLineDataSeries.add(sparklineDataseriesAll);
sparkLineDataSeries.add(sparklineDataseriesDataset);
JSparklinesDataset dataset = new JSparklinesDataset(sparkLineDataSeries);
pValues.add(pValue);
indexes.add(goMappingsTable.getRowCount());
((DefaultTableModel) goMappingsTable.getModel()).addRow(new Object[] { goMappingsTable.getRowCount() + 1, peptideShakerGUI.getDisplayFeaturesGenerator().addGoLink(goAccession), goTermName, goDomain, percentAll, percentDataset, dataset, new ValueAndBooleanDataPoint(log2Diff, false), pValue, true });
progressDialog.increasePrimaryProgressCounter();
}
if (indexes.isEmpty()) {
progressDialog.setRunCanceled();
}
int significantCounter = 0;
double significanceLevel = 0.05;
if (onePercentRadioButton.isSelected()) {
significanceLevel = 0.01;
}
if (!progressDialog.isRunCanceled()) {
((DefaultTableModel) goMappingsTable.getModel()).fireTableDataChanged();
// correct the p-values for multiple testing using benjamini-hochberg
sortPValues(pValues, indexes);
((ValueAndBooleanDataPoint) ((DefaultTableModel) goMappingsTable.getModel()).getValueAt(indexes.get(0), goMappingsTable.getColumn("Log2 Diff").getModelIndex())).setSignificant(pValues.get(0) < significanceLevel);
((DefaultTableModel) goMappingsTable.getModel()).setValueAt(new XYDataPoint(pValues.get(0), pValues.get(0)), indexes.get(0), goMappingsTable.getColumn("p-value").getModelIndex());
if (pValues.get(0) < significanceLevel) {
significantCounter++;
}
for (int i = 1; i < pValues.size(); i++) {
if (progressDialog.isRunCanceled()) {
break;
}
double tempPvalue = pValues.get(i) * pValues.size() / (pValues.size() - i);
// have to check if the correction results in a p-value bigger than 1
if (tempPvalue > 1) {
tempPvalue = 1;
}
((ValueAndBooleanDataPoint) ((DefaultTableModel) goMappingsTable.getModel()).getValueAt(indexes.get(i), goMappingsTable.getColumn("Log2 Diff").getModelIndex())).setSignificant(tempPvalue < significanceLevel);
((DefaultTableModel) goMappingsTable.getModel()).setValueAt(new XYDataPoint(tempPvalue, tempPvalue), indexes.get(i), goMappingsTable.getColumn("p-value").getModelIndex());
if (tempPvalue < significanceLevel) {
significantCounter++;
}
}
}
if (!progressDialog.isRunCanceled()) {
((TitledBorder) mappingsPanel.getBorder()).setTitle(PeptideShakerGUI.TITLED_BORDER_HORIZONTAL_PADDING + "Gene Ontology Mappings (" + significantCounter + "/" + goMappingsTable.getRowCount() + ")" + PeptideShakerGUI.TITLED_BORDER_HORIZONTAL_PADDING);
mappingsPanel.repaint();
progressDialog.setPrimaryProgressCounterIndeterminate(true);
// set the preferred size of the accession column
Integer width = ProteinTableModel.getPreferredAccessionColumnWidth(goMappingsTable, goMappingsTable.getColumn("GO Accession").getModelIndex(), 6, peptideShakerGUI.getMetrics().getMaxProteinAccessionLength());
if (width != null) {
goMappingsTable.getColumn("GO Accession").setMinWidth(width);
goMappingsTable.getColumn("GO Accession").setMaxWidth(width);
} else {
goMappingsTable.getColumn("GO Accession").setMinWidth(15);
goMappingsTable.getColumn("GO Accession").setMaxWidth(Integer.MAX_VALUE);
}
maxLog2Diff = Math.ceil(maxLog2Diff);
goMappingsTable.getColumn("Log2 Diff").setCellRenderer(new JSparklinesBarChartTableCellRenderer(PlotOrientation.HORIZONTAL, -maxLog2Diff, maxLog2Diff, Color.RED, peptideShakerGUI.getSparklineColor(), Color.lightGray, 0));
((JSparklinesBarChartTableCellRenderer) goMappingsTable.getColumn("Log2 Diff").getCellRenderer()).showNumberAndChart(true, TableProperties.getLabelWidth());
// update the plots
updateGoPlots();
// enable the contextual export options
exportMappingsJButton.setEnabled(true);
exportPlotsJButton.setEnabled(true);
peptideShakerGUI.setUpdated(PeptideShakerGUI.GO_ANALYSIS_TAB_INDEX, true);
}
if (goDomainChanged && goDomainsFile.exists()) {
goDomains.saveMapping(goDomainsFile);
}
progressDialog.setRunFinished();
}
} catch (Exception e) {
progressDialog.setRunFinished();
peptideShakerGUI.catchException(e);
}
}
}.start();
}
}
}
Aggregations