From b65330720abd1329eac5fddbed9516b8302abe52 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 19 Oct 2011 18:34:14 +0100 Subject: [PATCH] Refactored alignment viewport to use common base, extended viewport API with getters for calculation manager and calc parameters, and refactored conservation to use AlignWorker interface. --- src/jalview/api/AlignViewportI.java | 21 +++ src/jalview/appletgui/APopupMenu.java | 11 +- src/jalview/appletgui/AlignFrame.java | 4 +- src/jalview/appletgui/AlignViewport.java | 195 ++++++--------------------- src/jalview/appletgui/TreeCanvas.java | 2 +- src/jalview/gui/AlignFrame.java | 4 +- src/jalview/gui/AlignViewport.java | 59 +++++--- src/jalview/gui/ConservationThread.java | 103 -------------- src/jalview/gui/PopupMenu.java | 2 +- src/jalview/gui/TreeCanvas.java | 2 +- src/jalview/workers/ConservationThread.java | 100 ++++++++++++++ 11 files changed, 211 insertions(+), 292 deletions(-) delete mode 100644 src/jalview/gui/ConservationThread.java create mode 100644 src/jalview/workers/ConservationThread.java diff --git a/src/jalview/api/AlignViewportI.java b/src/jalview/api/AlignViewportI.java index 11fa615..5189e70 100644 --- a/src/jalview/api/AlignViewportI.java +++ b/src/jalview/api/AlignViewportI.java @@ -48,4 +48,25 @@ public interface AlignViewportI boolean getCentreColumnLabels(); boolean isCalculationInProgress(AlignmentAnnotation alignmentAnnotation); + + AlignmentAnnotation getAlignmentQualityAnnot(); + + AlignmentAnnotation getAlignmentConservationAnnotation(); + + /** + * Test to see if viewport is still open and active + * @return true indicates that all references to viewport should be dropped + */ + boolean isClosed(); + /** + * get the associated calculation thread manager for the view + * @return + */ + AlignCalcManagerI getCalcManager(); + + /** + * get the percentage gaps allowed in a conservation calculation + * + */ + public int getConsPercGaps(); } diff --git a/src/jalview/appletgui/APopupMenu.java b/src/jalview/appletgui/APopupMenu.java index 6ca4ea1..17bc65f 100755 --- a/src/jalview/appletgui/APopupMenu.java +++ b/src/jalview/appletgui/APopupMenu.java @@ -962,16 +962,11 @@ public class APopupMenu extends java.awt.PopupMenu implements if (conservationMenuItem.getState()) { - Conservation c = new Conservation("Group", + sg.cs.setConservation(Conservation.calculateConservation("Group", ResidueProperties.propHash, 3, sg.getSequences(ap.av.hiddenRepSequences), 0, - ap.av.alignment.getWidth()); - - c.calculate(); - c.verdict(false, ap.av.ConsPercGaps); - - sg.cs.setConservation(c); - + ap.av.alignment.getWidth(), + false, ap.av.getConsPercGaps(),false)); SliderPanel.setConservationSlider(ap, sg.cs, sg.getName()); SliderPanel.showConservationSlider(); } diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index 798a882..b24488f 100644 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -2229,7 +2229,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, al.getWidth() - 1); c.calculate(); - c.verdict(false, viewport.ConsPercGaps); + c.verdict(false, viewport.getConsPercGaps()); cs.setConservation(c); @@ -2298,7 +2298,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, sg.getSequences(viewport.hiddenRepSequences), 0, viewport.alignment.getWidth() - 1); c.calculate(); - c.verdict(false, viewport.ConsPercGaps); + c.verdict(false, viewport.getConsPercGaps()); sg.cs.setConservation(c); } else diff --git a/src/jalview/appletgui/AlignViewport.java b/src/jalview/appletgui/AlignViewport.java index 9af5ac2..e48c0ea 100644 --- a/src/jalview/appletgui/AlignViewport.java +++ b/src/jalview/appletgui/AlignViewport.java @@ -22,14 +22,16 @@ import java.util.*; import java.awt.*; import jalview.analysis.*; +import jalview.api.AlignCalcManagerI; import jalview.api.AlignViewportI; import jalview.bin.*; import jalview.datamodel.*; import jalview.schemes.*; import jalview.structure.SelectionSource; import jalview.structure.VamsasSource; +import jalview.workers.ConservationThread; -public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSource +public class AlignViewport extends AlignmentViewport implements AlignViewportI, SelectionSource, VamsasSource { int startRes; @@ -125,8 +127,6 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou boolean autocalculateConsensus = true; - public int ConsPercGaps = 25; // JBPNote : This should be a scalable property! - private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport( this); @@ -146,6 +146,8 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou Hashtable hiddenRepSequences; + AlignCalcManagerI calculator=new jalview.workers.AlignCalcManager(); + public void finalize() { applet=null; quality=null; @@ -288,7 +290,7 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou if (!alignment.isNucleotide()) { conservation = new AlignmentAnnotation("Conservation", - "Conservation of total alignment less than " + ConsPercGaps + "Conservation of total alignment less than " + getConsPercGaps() + "% gaps", new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH); conservation.hasText = true; @@ -334,149 +336,6 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou return showSequenceFeatures; } - class ConservationThread extends Thread - { - AlignmentPanel ap; - - public ConservationThread(AlignmentPanel ap) - { - this.ap = ap; - } - - public void run() - { - try - { - updatingConservation = true; - - while (UPDATING_CONSERVATION) - { - try - { - if (ap != null) - { - ap.paintAlignment(false); - } - Thread.sleep(200); - } catch (Exception ex) - { - ex.printStackTrace(); - } - } - - UPDATING_CONSERVATION = true; - - int alWidth = (alignment==null) ? -1 : alignment.getWidth(); - if (alWidth < 0) - { - updatingConservation = false; - UPDATING_CONSERVATION = false; - return; - } - - Conservation cons = new jalview.analysis.Conservation("All", - jalview.schemes.ResidueProperties.propHash, 3, - alignment.getSequences(), 0, alWidth - 1); - - cons.calculate(); - cons.verdict(false, ConsPercGaps); - - if (quality != null) - { - cons.findQuality(); - } - - char[] sequence = cons.getConsSequence().getSequence(); - float minR; - float minG; - float minB; - float maxR; - float maxG; - float maxB; - minR = 0.3f; - minG = 0.0f; - minB = 0f; - maxR = 1.0f - minR; - maxG = 0.9f - minG; - maxB = 0f - minB; // scalable range for colouring both Conservation and - // Quality - - float min = 0f; - float max = 11f; - float qmin = 0f; - float qmax = 0f; - - char c; - - conservation.annotations = new Annotation[alWidth]; - - if (quality != null) - { - quality.graphMax = cons.qualityRange[1].floatValue(); - quality.annotations = new Annotation[alWidth]; - qmin = cons.qualityRange[0].floatValue(); - qmax = cons.qualityRange[1].floatValue(); - } - - for (int i = 0; i < alWidth; i++) - { - float value = 0; - - c = sequence[i]; - - if (Character.isDigit(c)) - { - value = (int) (c - '0'); - } - else if (c == '*') - { - value = 11; - } - else if (c == '+') - { - value = 10; - } - // TODO - refactor to use a graduatedColorScheme to calculate the - // histogram colors. - float vprop = value - min; - vprop /= max; - conservation.annotations[i] = new Annotation(String.valueOf(c), - String.valueOf(value), ' ', value, new Color(minR - + (maxR * vprop), minG + (maxG * vprop), minB - + (maxB * vprop))); - - // Quality calc - if (quality != null) - { - value = ((Double) cons.quality.elementAt(i)).floatValue(); - vprop = value - qmin; - vprop /= qmax; - quality.annotations[i] = new Annotation(" ", - String.valueOf(value), ' ', value, new Color(minR - + (maxR * vprop), minG + (maxG * vprop), minB - + (maxB * vprop))); - } - } - } catch (OutOfMemoryError error) - { - System.out.println("Out of memory calculating conservation!!"); - conservation = null; - quality = null; - System.gc(); - } - - UPDATING_CONSERVATION = false; - updatingConservation = false; - - if (ap != null) - { - ap.paintAlignment(true); - } - - } - } - - ConservationThread conservationThread; ConsensusThread consensusThread; @@ -507,8 +366,7 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou return; } - conservationThread = new ConservationThread(ap); - conservationThread.start(); + calculator.startWorker(new ConservationThread(this, ap)); } /** @@ -1496,14 +1354,9 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou cs.setConsensus(hconsensus); if (cs.conservationApplied()) { - Alignment al = (Alignment) alignment; - Conservation c = new Conservation("All", - ResidueProperties.propHash, 3, al.getSequences(), 0, - al.getWidth() - 1); - c.calculate(); - c.verdict(false, ConsPercGaps); - - cs.setConservation(c); + cs.setConservation(Conservation.calculateConservation("All", + ResidueProperties.propHash, 3, alignment.getSequences(), 0, + alignment.getWidth(), false, getConsPercGaps(), false)); } } @@ -1829,4 +1682,32 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou { return null; // TODO: JAL-891 port to jvlite : refactor and introduce hStrucConsensus; } + + @Override + public AlignmentAnnotation getAlignmentQualityAnnot() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public AlignmentAnnotation getAlignmentConservationAnnotation() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean isClosed() + { + System.err.println("Alignment is "+alignment==null ? "closed" : "open"); + return alignment==null; + } + + @Override + public AlignCalcManagerI getCalcManager() + { + // TODO Auto-generated method stub + return null; + } } diff --git a/src/jalview/appletgui/TreeCanvas.java b/src/jalview/appletgui/TreeCanvas.java index 06b92ca..2ab7bf7 100755 --- a/src/jalview/appletgui/TreeCanvas.java +++ b/src/jalview/appletgui/TreeCanvas.java @@ -644,7 +644,7 @@ public class TreeCanvas extends Panel implements MouseListener, sg.getStartRes(), sg.getEndRes()); c.calculate(); - c.verdict(false, av.ConsPercGaps); + c.verdict(false, av.getConsPercGaps()); cs.setConservation(c); sg.cs = cs; diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 94a7848..850450b 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -3057,7 +3057,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, al.getWidth() - 1); c.calculate(); - c.verdict(false, viewport.ConsPercGaps); + c.verdict(false, viewport.getConsPercGaps()); cs.setConservation(c); @@ -3130,7 +3130,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, sg.getSequences(viewport.hiddenRepSequences), sg.getStartRes(), sg.getEndRes() + 1); c.calculate(); - c.verdict(false, viewport.ConsPercGaps); + c.verdict(false, viewport.getConsPercGaps()); sg.cs.setConservation(c); } else diff --git a/src/jalview/gui/AlignViewport.java b/src/jalview/gui/AlignViewport.java index 1c6a94e..1b39869 100644 --- a/src/jalview/gui/AlignViewport.java +++ b/src/jalview/gui/AlignViewport.java @@ -40,7 +40,10 @@ import java.util.*; import java.awt.*; import jalview.analysis.*; +import jalview.api.AlignCalcManagerI; import jalview.api.AlignViewportI; +import jalview.api.AlignmentViewPanel; +import jalview.api.OOMHandlerI; import jalview.bin.*; @@ -50,6 +53,8 @@ import jalview.schemes.*; import jalview.structure.SelectionSource; import jalview.structure.StructureSelectionManager; import jalview.structure.VamsasSource; +import jalview.workers.AlignCalcManager; +import jalview.workers.ConservationThread; /** * DOCUMENT ME! @@ -57,7 +62,7 @@ import jalview.structure.VamsasSource; * @author $author$ * @version $Revision: 1.141 $ */ -public class AlignViewport implements SelectionSource, VamsasSource, AlignViewportI +public class AlignViewport extends AlignmentViewport implements SelectionSource, VamsasSource, AlignViewportI { private static final int RIGHT_JUSTIFY = 1; @@ -158,9 +163,6 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo boolean autoCalculateStrucConsensus = true; - /** DOCUMENT ME!! */ - public int ConsPercGaps = 25; // JBPNote : This should be a scalable property! - // JBPNote Prolly only need this in the applet version. private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport( this); @@ -373,7 +375,7 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo if (!alignment.isNucleotide()) { conservation = new AlignmentAnnotation("Conservation", - "Conservation of total alignment less than " + ConsPercGaps + "Conservation of total alignment less than " + getConsPercGaps() + "% gaps", new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH); conservation.hasText = true; @@ -475,8 +477,8 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo { return showSequenceFeatures; } - - ConservationThread conservationThread; + + AlignCalcManagerI calculator=new AlignCalcManager(); ConsensusThread consensusThread; @@ -509,7 +511,7 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo /** * trigger update of conservation annotation */ - public void updateConservation(final AlignmentPanel ap) + public void updateConservation(final AlignmentViewPanel ap) { // see note in mantis : issue number 8585 if (alignment.isNucleotide() || conservation == null @@ -517,9 +519,8 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo { return; } - - conservationThread = new ConservationThread(this, ap); - conservationThread.start(); + + calculator.startWorker(new jalview.workers.ConservationThread(this, ap)); } /** @@ -538,8 +539,7 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo class ConsensusThread extends Thread { - AlignmentPanel ap; - + AlignmentViewPanel ap; public ConsensusThread(AlignmentPanel ap) { this.ap = ap; @@ -596,8 +596,8 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo alignment.deleteAnnotation(consensus); consensus = null; - hconsensus = null; - new OOMWarning("calculating consensus", error); + hconsensus = null; + ap.raiseOOMWarning("calculating consensus", error); } UPDATING_CONSENSUS = false; updatingConsensus = false; @@ -1939,12 +1939,12 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo cs.setConsensus(hconsensus); if (cs.conservationApplied()) { - Alignment al = (Alignment) alignment; + AlignmentI al = (Alignment) alignment; Conservation c = new Conservation("All", ResidueProperties.propHash, 3, al.getSequences(), 0, al .getWidth() - 1); c.calculate(); - c.verdict(false, ConsPercGaps); + c.verdict(false, getConsPercGaps()); cs.setConservation(c); } @@ -2523,4 +2523,29 @@ public class AlignViewport implements SelectionSource, VamsasSource, AlignViewpo { return hStrucConsensus; } + + @Override + public AlignmentAnnotation getAlignmentQualityAnnot() + { + return quality; + } + + @Override + public AlignmentAnnotation getAlignmentConservationAnnotation() + { + return conservation; + } + + @Override + public boolean isClosed() + { + // TODO: check that this isClosed is only true after panel is closed, not before it is fully constructed. + return alignment==null; + } + + @Override + public AlignCalcManagerI getCalcManager() + { + return calculator; + } } diff --git a/src/jalview/gui/ConservationThread.java b/src/jalview/gui/ConservationThread.java deleted file mode 100644 index 15ec239..0000000 --- a/src/jalview/gui/ConservationThread.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7) - * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle - * - * This file is part of Jalview. - * - * Jalview is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * Jalview is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with Jalview. If not, see . - */ -package jalview.gui; - -import jalview.analysis.Conservation; -import jalview.datamodel.Annotation; - -import java.awt.Color; - -class ConservationThread extends Thread -{ - /** - * - */ - private AlignViewport alignViewport; - - AlignmentPanel ap; - - public ConservationThread(AlignViewport alignViewport, AlignmentPanel ap) - { - this.alignViewport = alignViewport; - this.ap = ap; - } - - public void run() - { - try - { - this.alignViewport.updatingConservation = true; - - while (AlignViewport.UPDATING_CONSERVATION) - { - try - { - if (ap != null) - { - ap.paintAlignment(false); - } - Thread.sleep(200); - } catch (Exception ex) - { - ex.printStackTrace(); - } - } - - AlignViewport.UPDATING_CONSERVATION = true; - - int alWidth; - - if (alignViewport==null || alignViewport.alignment==null || (alWidth=alignViewport.alignment.getWidth())< 0) - { - this.alignViewport.updatingConservation = false; - AlignViewport.UPDATING_CONSERVATION = false; - return; - } - - Conservation cons = new jalview.analysis.Conservation("All", - jalview.schemes.ResidueProperties.propHash, 3, - this.alignViewport.alignment.getSequences(), 0, alWidth - 1); - - cons.calculate(); - cons.verdict(false, this.alignViewport.ConsPercGaps); - - if (this.alignViewport.quality != null) - { - cons.findQuality(); - } - cons.completeAnnotations(alignViewport.conservation, - alignViewport.quality, 0, alWidth); - } catch (OutOfMemoryError error) - { - new OOMWarning("calculating conservation", error); - - this.alignViewport.conservation = null; - this.alignViewport.quality = null; - - } - - AlignViewport.UPDATING_CONSERVATION = false; - this.alignViewport.updatingConservation = false; - - if (ap != null) - { - ap.paintAlignment(true); - } - - } -} diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index e812530..d87a62b 100644 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -1529,7 +1529,7 @@ public class PopupMenu extends JPopupMenu sg.getEndRes() + 1); c.calculate(); - c.verdict(false, ap.av.ConsPercGaps); + c.verdict(false, ap.av.getConsPercGaps()); sg.cs.setConservation(c); diff --git a/src/jalview/gui/TreeCanvas.java b/src/jalview/gui/TreeCanvas.java index 42a09aa..b62a1c7 100755 --- a/src/jalview/gui/TreeCanvas.java +++ b/src/jalview/gui/TreeCanvas.java @@ -928,7 +928,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, sg.getStartRes(), sg.getEndRes()); c.calculate(); - c.verdict(false, aps[a].av.ConsPercGaps); + c.verdict(false, aps[a].av.getConsPercGaps()); sg.cs.setConservation(c); } diff --git a/src/jalview/workers/ConservationThread.java b/src/jalview/workers/ConservationThread.java new file mode 100644 index 0000000..4afaea7 --- /dev/null +++ b/src/jalview/workers/ConservationThread.java @@ -0,0 +1,100 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7) + * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Jalview. If not, see . + */ +package jalview.workers; + +import jalview.analysis.Conservation; +import jalview.api.AlignCalcWorkerI; +import jalview.api.AlignmentViewPanel; +import jalview.api.AlignViewportI; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentI; + +public class ConservationThread extends AlignCalcWorker implements AlignCalcWorkerI +{ + + private int ConsPercGaps = 25; // JBPNote : This should be a configurable property! + + public ConservationThread(AlignViewportI alignViewport, AlignmentViewPanel alignPanel) + { + super(alignViewport, alignPanel); + ConsPercGaps = alignViewport.getConsPercGaps(); + } + + public void run() + { + try + { + calcMan.notifyStart(this); // updatingConservation = true; + + while (calcMan.alreadyDoing(this)) //UPDATING_CONSERVATION) + { + try + { + if (ap != null) + { + ap.paintAlignment(false); + } + Thread.sleep(200); + } catch (Exception ex) + { + ex.printStackTrace(); + } + } + calcMan.notifyWorking(this); + if (alignViewport.isClosed()) { + abortAndDestroy(); + } + + AlignmentI alignment=alignViewport.getAlignment(); + AlignmentAnnotation conservation=alignViewport.getAlignmentConservationAnnotation(); + AlignmentAnnotation quality=alignViewport.getAlignmentQualityAnnot(); + // AlignViewport.UPDATING_CONSERVATION = true; + + int alWidth; + + if (alignment==null || (alWidth=alignment.getWidth())< 0) + { + calcMan.workerComplete(this); + //.updatingConservation = false; + //AlignViewport.UPDATING_CONSERVATION = false; + + return; + } + + Conservation cons = Conservation.calculateConservation("All", + jalview.schemes.ResidueProperties.propHash, 3, + alignment.getSequences(), 0, alWidth - 1, false, ConsPercGaps, quality!=null); + cons.completeAnnotations(conservation, + quality, 0, alWidth); + } catch (OutOfMemoryError error) + { + ap.raiseOOMWarning("calculating conservation", error); + calcMan.workerCannotRun(this); + // alignViewport.conservation = null; + // this.alignViewport.quality = null; + + } + calcMan.workerComplete(this); + + if (ap != null) + { + ap.paintAlignment(true); + } + + } +} -- 1.7.10.2