From 55deb271c9f6fa106548013a9790db38d518cf6c Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 5 Nov 2018 14:50:48 +0000 Subject: [PATCH] JAL-3148 SequenceRenderer, ResidueColourFinder overloads and refactorings for colour structure by viewport colour schem --- .../api/structures/JalviewStructureDisplayI.java | 18 +-- src/jalview/appletgui/AppletJmol.java | 33 +++-- src/jalview/appletgui/AppletJmolBinding.java | 7 - src/jalview/ext/jmol/JalviewJmolBinding.java | 40 +++--- .../ext/rbvi/chimera/JalviewChimeraBinding.java | 19 +-- src/jalview/gui/AppJmol.java | 7 +- src/jalview/gui/AppJmolBinding.java | 27 +--- src/jalview/gui/ChimeraViewFrame.java | 7 +- src/jalview/gui/Jalview2XML.java | 4 +- src/jalview/gui/JalviewChimeraBindingModel.java | 27 ---- src/jalview/gui/OverviewCanvas.java | 6 - src/jalview/gui/SeqCanvas.java | 7 +- src/jalview/gui/SequenceRenderer.java | 101 ++++++-------- src/jalview/gui/StructureViewerBase.java | 34 +++-- src/jalview/renderer/ResidueColourFinder.java | 31 +++-- .../structures/models/AAStructureBindingModel.java | 142 +++++++++++++++----- .../models/AAStructureBindingModelTest.java | 44 +----- 17 files changed, 261 insertions(+), 293 deletions(-) diff --git a/src/jalview/api/structures/JalviewStructureDisplayI.java b/src/jalview/api/structures/JalviewStructureDisplayI.java index 8f778f7..7c51068 100644 --- a/src/jalview/api/structures/JalviewStructureDisplayI.java +++ b/src/jalview/api/structures/JalviewStructureDisplayI.java @@ -23,7 +23,7 @@ package jalview.api.structures; import jalview.api.AlignmentViewPanel; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; -import jalview.schemes.ColourSchemeI; +import jalview.gui.AlignmentPanel; import jalview.structures.models.AAStructureBindingModel; public interface JalviewStructureDisplayI @@ -59,13 +59,6 @@ public interface JalviewStructureDisplayI void closeViewer(boolean closeExternalViewer); /** - * apply a colourscheme to the structures in the viewer - * - * @param colourScheme - */ - void setJalviewColourScheme(ColourSchemeI colourScheme); - - /** * * @return true if all background sequence/structure binding threads have * completed for this viewer instance @@ -125,4 +118,13 @@ public interface JalviewStructureDisplayI */ void raiseViewer(); + /** + * Answers true if the argument is a view used as the basis for structure + * colouring + * + * @param o + * @return + */ + boolean isUsedForColourBy(Object o); + } diff --git a/src/jalview/appletgui/AppletJmol.java b/src/jalview/appletgui/AppletJmol.java index 3d1442d..22c467d 100644 --- a/src/jalview/appletgui/AppletJmol.java +++ b/src/jalview/appletgui/AppletJmol.java @@ -28,6 +28,7 @@ import jalview.io.DataSourceType; import jalview.io.FileParse; import jalview.io.StructureFile; import jalview.schemes.BuriedColourScheme; +import jalview.schemes.ColourSchemeI; import jalview.schemes.HelixColourScheme; import jalview.schemes.HydrophobicColourScheme; import jalview.schemes.PurinePyrimidineColourScheme; @@ -37,6 +38,7 @@ import jalview.schemes.TurnColourScheme; import jalview.schemes.UserColourScheme; import jalview.schemes.ZappoColourScheme; import jalview.structure.StructureSelectionManager; +import jalview.structures.models.AAStructureBindingModel.ColourBy; import jalview.util.MessageManager; import java.awt.BorderLayout; @@ -185,7 +187,7 @@ public class AppletJmol extends EmbmenuFrame implements jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(), new PDBEntry[] { pdbentry }, new SequenceI[][] { seq }, protocol); - jmb.setColourBySequence(true); + jmb.setColourBy(ColourBy.Sequence); if (pdbentry.getId() == null || pdbentry.getId().length() < 1) { if (protocol == DataSourceType.PASTE) @@ -455,41 +457,41 @@ public class AppletJmol extends EmbmenuFrame implements else if (evt.getSource() == zappo) { setEnabled(zappo); - jmb.setJalviewColourScheme(new ZappoColourScheme()); + setColourScheme(new ZappoColourScheme()); } else if (evt.getSource() == taylor) { setEnabled(taylor); - jmb.setJalviewColourScheme(new TaylorColourScheme()); + setColourScheme(new TaylorColourScheme()); } else if (evt.getSource() == hydro) { setEnabled(hydro); - jmb.setJalviewColourScheme(new HydrophobicColourScheme()); + setColourScheme(new HydrophobicColourScheme()); } else if (evt.getSource() == helix) { setEnabled(helix); - jmb.setJalviewColourScheme(new HelixColourScheme()); + setColourScheme(new HelixColourScheme()); } else if (evt.getSource() == strand) { setEnabled(strand); - jmb.setJalviewColourScheme(new StrandColourScheme()); + setColourScheme(new StrandColourScheme()); } else if (evt.getSource() == turn) { setEnabled(turn); - jmb.setJalviewColourScheme(new TurnColourScheme()); + setColourScheme(new TurnColourScheme()); } else if (evt.getSource() == buried) { setEnabled(buried); - jmb.setJalviewColourScheme(new BuriedColourScheme()); + setColourScheme(new BuriedColourScheme()); } else if (evt.getSource() == purinepyrimidine) { - jmb.setJalviewColourScheme(new PurinePyrimidineColourScheme()); + setColourScheme(new PurinePyrimidineColourScheme()); } else if (evt.getSource() == user) { @@ -524,7 +526,12 @@ public class AppletJmol extends EmbmenuFrame implements } } - /** + private void setColourScheme(ColourSchemeI cs) + { + jmb.setJalviewColourScheme(cs, ap); + } + +/** * tick or untick the seqColour menu entry or jmoColour entry depending upon * if it was selected or not. * @@ -534,7 +541,7 @@ public class AppletJmol extends EmbmenuFrame implements { jmolColour.setState(itm == jmolColour); seqColour.setState(itm == seqColour); - jmb.setColourBySequence(itm == seqColour); + jmb.setColourBy(itm == seqColour ? ColourBy.Sequence : ColourBy.Viewer); } @Override @@ -543,7 +550,7 @@ public class AppletJmol extends EmbmenuFrame implements if (evt.getSource() == jmolColour) { setEnabled(jmolColour); - jmb.setColourBySequence(false); + jmb.setColourBy(ColourBy.Viewer); } else if (evt.getSource() == seqColour) { @@ -695,7 +702,7 @@ public class AppletJmol extends EmbmenuFrame implements */ public void setJalviewColourScheme(UserColourScheme ucs) { - jmb.setJalviewColourScheme(ucs); + setColourScheme(ucs); } public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment) diff --git a/src/jalview/appletgui/AppletJmolBinding.java b/src/jalview/appletgui/AppletJmolBinding.java index 2f61b24..636d2e0 100644 --- a/src/jalview/appletgui/AppletJmolBinding.java +++ b/src/jalview/appletgui/AppletJmolBinding.java @@ -59,13 +59,6 @@ class AppletJmolBinding extends JalviewJmolBinding } @Override - public jalview.api.SequenceRenderer getSequenceRenderer( - AlignmentViewPanel alignment) - { - return new SequenceRenderer(((AlignmentPanel) alignment).av); - } - - @Override public void sendConsoleEcho(String strEcho) { if (appletJmolBinding.scriptWindow == null) diff --git a/src/jalview/ext/jmol/JalviewJmolBinding.java b/src/jalview/ext/jmol/JalviewJmolBinding.java index 8832278..7a8bc9c 100644 --- a/src/jalview/ext/jmol/JalviewJmolBinding.java +++ b/src/jalview/ext/jmol/JalviewJmolBinding.java @@ -22,7 +22,7 @@ package jalview.ext.jmol; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureRenderer; -import jalview.api.SequenceRenderer; +import jalview.api.SequenceRendererI; import jalview.datamodel.AlignmentI; import jalview.datamodel.HiddenColumns; import jalview.datamodel.PDBEntry; @@ -175,7 +175,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel @Override public void colourByChain() { - colourBySequence = false; + super.colourByChain(); // TODO: colour by chain should colour each chain distinctly across all // visible models // TODO: http://issues.jalview.org/browse/JAL-628 @@ -185,7 +185,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel @Override public void colourByCharge() { - colourBySequence = false; + super.colourByCharge(); evalStateCommand("select *;color white;select ASP,GLU;color red;" + "select LYS,ARG;color blue;select CYS;color yellow"); } @@ -518,7 +518,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel */ @Override protected StructureMappingcommandSet[] getColourBySequenceCommands( - String[] files, SequenceRenderer sr, AlignmentViewPanel viewPanel) + String[] files, SequenceRendererI sr, AlignmentViewPanel viewPanel) { return JmolCommands.getColourBySequenceCommand(getSsm(), files, getSequence(), sr, viewPanel); @@ -1296,29 +1296,23 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel } @Override - public void setJalviewColourScheme(ColourSchemeI cs) + public void setSimpleColourScheme(ColourSchemeI cs) { - colourBySequence = false; - - if (cs == null) - { - return; - } - jmolHistory(false); StringBuilder command = new StringBuilder(128); command.append("select *;color white;"); - List residueSet = ResidueProperties.getResidues(isNucleotide(), - false); - for (String resName : residueSet) - { - char res = resName.length() == 3 - ? ResidueProperties.getSingleCharacterCode(resName) - : resName.charAt(0); - Color col = cs.findColour(res, 0, null, null, 0f); - command.append("select " + resName + ";color[" + col.getRed() + "," - + col.getGreen() + "," + col.getBlue() + "];"); - } + if (cs != null) + { + List residueSet = ResidueProperties.getResidues(isNucleotide(), false); + for (String resName : residueSet) + { + char res = resName.length() == 3 ? ResidueProperties.getSingleCharacterCode(resName) + : resName.charAt(0); + Color col = cs.findColour(res); + command.append("select " + resName + ";color[" + col.getRed() + "," + col.getGreen() + "," + + col.getBlue() + "];"); + } + } evalStateCommand(command.toString()); jmolHistory(true); diff --git a/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java b/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java index 00446f2..844f718 100644 --- a/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java +++ b/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java @@ -21,7 +21,7 @@ package jalview.ext.rbvi.chimera; import jalview.api.AlignmentViewPanel; -import jalview.api.SequenceRenderer; +import jalview.api.SequenceRendererI; import jalview.api.structures.JalviewStructureDisplayI; import jalview.bin.Cache; import jalview.datamodel.AlignmentI; @@ -312,7 +312,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel @Override public void colourByChain() { - colourBySequence = false; + super.colourByChain(); sendAsynchronousCommand("rainbow chain", COLOURING_CHIMERA); } @@ -328,7 +328,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel @Override public void colourByCharge() { - colourBySequence = false; + super.colourByCharge(); String command = "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS"; sendAsynchronousCommand(command, COLOURING_CHIMERA); } @@ -696,7 +696,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel */ @Override protected StructureMappingcommandSet[] getColourBySequenceCommands( - String[] files, SequenceRenderer sr, AlignmentViewPanel viewPanel) + String[] files, SequenceRendererI sr, AlignmentViewPanel viewPanel) { return ChimeraCommands.getColourBySequenceCommand(getSsm(), files, getSequence(), sr, viewPanel); @@ -915,15 +915,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel } @Override - public void setJalviewColourScheme(ColourSchemeI cs) + public void setSimpleColourScheme(ColourSchemeI cs) { - colourBySequence = false; - - if (cs == null) - { - return; - } - // Chimera expects RBG values in the range 0-1 final double normalise = 255D; viewerCommandHistory(false); @@ -936,7 +929,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel char res = resName.length() == 3 ? ResidueProperties.getSingleCharacterCode(resName) : resName.charAt(0); - Color col = cs.findColour(res, 0, null, null, 0f); + Color col = cs == null ? Color.WHITE : cs.findColour(res); command.append("color " + col.getRed() / normalise + "," + col.getGreen() / normalise + "," + col.getBlue() / normalise + " ::" + resName + ";"); diff --git a/src/jalview/gui/AppJmol.java b/src/jalview/gui/AppJmol.java index 6c934c8..12bb21d 100644 --- a/src/jalview/gui/AppJmol.java +++ b/src/jalview/gui/AppJmol.java @@ -26,6 +26,7 @@ import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; import jalview.gui.StructureViewer.ViewerType; import jalview.structures.models.AAStructureBindingModel; +import jalview.structures.models.AAStructureBindingModel.ColourBy; import jalview.util.BrowserLauncher; import jalview.util.MessageManager; import jalview.util.Platform; @@ -113,14 +114,14 @@ public class AppJmol extends StructureViewerBase initMenus(); if (leaveColouringToJmol || !usetoColour) { - jmb.setColourBySequence(false); + jmb.setColourBy(ColourBy.Viewer); seqColour.setSelected(false); viewerColour.setSelected(true); } else if (usetoColour) { useAlignmentPanelForColourbyseq(ap); - jmb.setColourBySequence(true); + jmb.setColourBy(ColourBy.Sequence); seqColour.setSelected(true); viewerColour.setSelected(false); } @@ -193,7 +194,7 @@ public class AppJmol extends StructureViewerBase alignAddedStructures = alignAdded; useAlignmentPanelForSuperposition(ap); - jmb.setColourBySequence(true); + jmb.setColourBy(ColourBy.Sequence); setSize(400, 400); // probably should be a configurable/dynamic default here initMenus(); addingStructures = false; diff --git a/src/jalview/gui/AppJmolBinding.java b/src/jalview/gui/AppJmolBinding.java index 724cec1..a287fb2 100644 --- a/src/jalview/gui/AppJmolBinding.java +++ b/src/jalview/gui/AppJmolBinding.java @@ -21,6 +21,7 @@ package jalview.gui; import jalview.api.AlignmentViewPanel; +import jalview.api.SequenceRendererI; import jalview.api.structures.JalviewStructureDisplayI; import jalview.bin.Cache; import jalview.datamodel.PDBEntry; @@ -55,12 +56,6 @@ public class AppJmolBinding extends JalviewJmolBinding } @Override - public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment) - { - return new SequenceRenderer(((AlignmentPanel) alignment).av); - } - - @Override public void sendConsoleEcho(String strEcho) { if (console != null) @@ -109,21 +104,6 @@ public class AppJmolBinding extends JalviewJmolBinding } @Override - public void updateColours(Object source) - { - AlignmentPanel ap = (AlignmentPanel) source; - // ignore events from panels not used to colour this view - if (!appJmolWindow.isUsedforcolourby(ap)) - { - return; - } - if (!isLoadingFromArchive()) - { - colourBySequence(ap); - } - } - - @Override public void notifyScriptTermination(String strStatus, int msWalltime) { // todo - script termination doesn't happen ? @@ -147,15 +127,11 @@ public class AppJmolBinding extends JalviewJmolBinding @Override public void selectionChanged(BS arg0) { - // TODO Auto-generated method stub - } @Override public void refreshPdbEntries() { - // TODO Auto-generated method stub - } @Override @@ -191,7 +167,6 @@ public class AppJmolBinding extends JalviewJmolBinding @Override public Map getJSpecViewProperty(String arg0) { - // TODO Auto-generated method stub return null; } diff --git a/src/jalview/gui/ChimeraViewFrame.java b/src/jalview/gui/ChimeraViewFrame.java index d07a7c2..a662105 100644 --- a/src/jalview/gui/ChimeraViewFrame.java +++ b/src/jalview/gui/ChimeraViewFrame.java @@ -31,6 +31,7 @@ import jalview.gui.StructureViewer.ViewerType; import jalview.io.DataSourceType; import jalview.io.StructureFile; import jalview.structures.models.AAStructureBindingModel; +import jalview.structures.models.AAStructureBindingModel.ColourBy; import jalview.util.BrowserLauncher; import jalview.util.MessageManager; import jalview.util.Platform; @@ -236,7 +237,7 @@ public class ChimeraViewFrame extends StructureViewerBase { useAlignmentPanelForSuperposition(ap); } - jmb.setColourBySequence(true); + jmb.setColourBy(ColourBy.Sequence); setSize(myWidth, myHeight); initMenus(); @@ -279,13 +280,13 @@ public class ChimeraViewFrame extends StructureViewerBase openNewChimera(alignPanel, pdbArray, seqsArray); if (colourByChimera) { - jmb.setColourBySequence(false); + jmb.setColourBy(ColourBy.Viewer); seqColour.setSelected(false); viewerColour.setSelected(true); } else if (colourBySequence) { - jmb.setColourBySequence(true); + jmb.setColourBy(ColourBy.Sequence); seqColour.setSelected(true); viewerColour.setSelected(false); } diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index 9285754..39755f9 100644 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -1732,8 +1732,8 @@ public class Jalview2XML state.setHeight(viewFrame.getHeight()); final String viewId = viewFrame.getViewId(); state.setViewId(viewId); - state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap)); - state.setColourwithAlignPanel(viewFrame.isUsedforcolourby(ap)); + state.setAlignwithAlignPanel(viewFrame.isUsedForAligment(ap)); + state.setColourwithAlignPanel(viewFrame.isUsedForColourBy(ap)); state.setColourByJmol(viewFrame.isColouredByViewer()); state.setType(viewFrame.getViewerType().toString()); pdb.addStructureState(state); diff --git a/src/jalview/gui/JalviewChimeraBindingModel.java b/src/jalview/gui/JalviewChimeraBindingModel.java index 2f11c30..f41021e 100644 --- a/src/jalview/gui/JalviewChimeraBindingModel.java +++ b/src/jalview/gui/JalviewChimeraBindingModel.java @@ -56,13 +56,6 @@ public class JalviewChimeraBindingModel extends JalviewChimeraBinding } @Override - public jalview.api.SequenceRenderer getSequenceRenderer( - AlignmentViewPanel alignment) - { - return new SequenceRenderer(((AlignmentPanel) alignment).av); - } - - @Override public void refreshGUI() { javax.swing.SwingUtilities.invokeLater(new Runnable() @@ -77,31 +70,11 @@ public class JalviewChimeraBindingModel extends JalviewChimeraBinding } @Override - public void updateColours(Object source) - { - AlignmentPanel ap = (AlignmentPanel) source; - // ignore events from panels not used to colour this view - if (!cvf.isUsedforcolourby(ap)) - { - return; - } - if (!isLoadingFromArchive()) - { - colourBySequence(ap); - } - } - - @Override public void releaseReferences(Object svl) { } @Override - protected void releaseUIResources() - { - } - - @Override public void refreshPdbEntries() { } diff --git a/src/jalview/gui/OverviewCanvas.java b/src/jalview/gui/OverviewCanvas.java index cc361a5..1468cf8 100644 --- a/src/jalview/gui/OverviewCanvas.java +++ b/src/jalview/gui/OverviewCanvas.java @@ -49,10 +49,6 @@ public class OverviewCanvas extends JComponent private BufferedImage lastMiniMe = null; - // Can set different properties in this seqCanvas than - // main visible SeqCanvas - private SequenceRenderer sr; - private jalview.renderer.seqfeatures.FeatureRenderer fr; private OverviewDimensions od; @@ -72,8 +68,6 @@ public class OverviewCanvas extends JComponent av = alignvp; progressPanel = pp; - sr = new SequenceRenderer(av); - sr.renderGaps = false; fr = new jalview.renderer.seqfeatures.FeatureRenderer(av); boolean useLegacy = Cache.getDefault(Preferences.USE_LEGACY_GAP, false); diff --git a/src/jalview/gui/SeqCanvas.java b/src/jalview/gui/SeqCanvas.java index 5c404f0..76ff5f2 100755 --- a/src/jalview/gui/SeqCanvas.java +++ b/src/jalview/gui/SeqCanvas.java @@ -1007,12 +1007,13 @@ public class SeqCanvas extends JComponent implements ViewportListenerI int charWidth = av.getCharWidth(); g.setFont(av.getFont()); - seqRdr.prepare(g, av.isRenderGaps()); + seqRdr.setGraphics(g); SequenceI nextSeq; // / First draw the sequences // /////////////////////////// + boolean drawGaps = av.isRenderGaps(); for (int i = startSeq; i <= endSeq; i++) { nextSeq = av.getAlignment().getSequenceAt(i); @@ -1022,8 +1023,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI // empty continue; } - seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq), - startRes, endRes, offset + ((i - startSeq) * charHeight)); + seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq), + startRes, endRes, offset + ((i - startSeq) * charHeight), drawGaps); if (av.isShowSequenceFeatures()) { diff --git a/src/jalview/gui/SequenceRenderer.java b/src/jalview/gui/SequenceRenderer.java index 81b394b..90d068b 100755 --- a/src/jalview/gui/SequenceRenderer.java +++ b/src/jalview/gui/SequenceRenderer.java @@ -25,30 +25,28 @@ import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.renderer.ResidueColourFinder; import jalview.renderer.seqfeatures.FeatureColourFinder; +import jalview.schemes.ColourSchemeI; +import jalview.util.Comparison; import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; -public class SequenceRenderer implements jalview.api.SequenceRenderer +public class SequenceRenderer implements jalview.api.SequenceRendererI { final static int CHAR_TO_UPPER = 'A' - 'a'; - AlignViewportI av; + private AlignViewportI av; - FontMetrics fm; + private FontMetrics fm; - boolean renderGaps = true; + private SequenceGroup[] allGroups = null; - SequenceGroup[] allGroups = null; + private Graphics graphics; - // Color resBoxColour; + private boolean monospacedFont; - Graphics graphics; - - boolean monospacedFont; - - ResidueColourFinder resColourFinder; + private ResidueColourFinder resColourFinder; /** * Creates a new SequenceRenderer object @@ -62,12 +60,23 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer } /** - * DOCUMENT ME! + * Constructor given a choice of colour scheme. May be used to find colours + * for a structure that has a viewport colour scheme other than 'by sequence' * - * @param b - * DOCUMENT ME! + * @param cs + */ + public SequenceRenderer(AlignViewportI viewport, ColourSchemeI cs) + { + this.av = viewport; + resColourFinder = new ResidueColourFinder(viewport, cs); + } + +/** + * Sets the Graphics context to draw on, and also guesses whether we are using a monospaced font + * + * @param g */ - public void prepare(Graphics g, boolean renderGaps) + public void setGraphics(Graphics g) { graphics = g; fm = g.getFontMetrics(); @@ -77,8 +86,6 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av.getCharWidth() == dwidth); - - this.renderGaps = renderGaps; } /** @@ -103,29 +110,19 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer } /** - * DOCUMENT ME! + * Draws the sequence (box colour and residues) over the given range, at the + * specified y-offset on the Graphics context * - * @param g - * DOCUMENT ME! * @param seq - * DOCUMENT ME! - * @param sg - * DOCUMENT ME! + * @param sg groups (if any) that the sequence is a member of (may have + * distinct group colouring) * @param start - * DOCUMENT ME! * @param end - * DOCUMENT ME! - * @param x1 - * DOCUMENT ME! * @param y1 - * DOCUMENT ME! - * @param width - * DOCUMENT ME! - * @param height - * DOCUMENT ME! + * @param drawGaps */ public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start, - int end, int y1) + int end, int y1, boolean drawGaps) { allGroups = sg; @@ -133,27 +130,18 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer if (av.isValidCharWidth()) { - drawText(seq, start, end, y1); + drawText(seq, start, end, y1, drawGaps); } } /** - * DOCUMENT ME! + * Draws box colours on the given sequence residue range, at the specified + * y-offset on the Graphics context * * @param seq - * DOCUMENT ME! * @param start - * DOCUMENT ME! * @param end - * DOCUMENT ME! - * @param x1 - * DOCUMENT ME! * @param y1 - * DOCUMENT ME! - * @param width - * DOCUMENT ME! - * @param height - * DOCUMENT ME! */ public synchronized void drawBoxes(SequenceI seq, int start, int end, int y1) @@ -225,24 +213,16 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer } /** - * DOCUMENT ME! + * Draws residue letters on the given sequence residue range, at the specified + * y-offset on the Graphics context * * @param seq - * DOCUMENT ME! * @param start - * DOCUMENT ME! * @param end - * DOCUMENT ME! - * @param x1 - * DOCUMENT ME! * @param y1 - * DOCUMENT ME! - * @param width - * DOCUMENT ME! - * @param height - * DOCUMENT ME! + * @param drawGaps */ - public void drawText(SequenceI seq, int start, int end, int y1) + public void drawText(SequenceI seq, int start, int end, int y1, boolean drawGaps) { y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady int charOffset = 0; @@ -283,7 +263,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer getboxColour = false; s = seq.getCharAt(i); - if (!renderGaps && jalview.util.Comparison.isGap(s)) + if (!drawGaps && Comparison.isGap(s)) { continue; } @@ -333,9 +313,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer { // todo - use sequence group consensus s = getDisplayChar(srep, i, s, '.', currentSequenceGroup); - } - } else { @@ -377,15 +355,12 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer if (!isarep && av.getShowUnconserved()) { s = getDisplayChar(srep, i, s, '.', null); - } - } charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2; graphics.drawString(String.valueOf(s), charOffset + av.getCharWidth() * (i - start), y1); - } } } diff --git a/src/jalview/gui/StructureViewerBase.java b/src/jalview/gui/StructureViewerBase.java index 72b0bcc..05a163b 100644 --- a/src/jalview/gui/StructureViewerBase.java +++ b/src/jalview/gui/StructureViewerBase.java @@ -37,6 +37,7 @@ import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemes; import jalview.structure.StructureMapping; import jalview.structures.models.AAStructureBindingModel; +import jalview.structures.models.AAStructureBindingModel.ColourBy; import jalview.util.MessageManager; import java.awt.Color; @@ -159,15 +160,15 @@ public abstract class StructureViewerBase extends GStructureViewer return _aps.contains(ap2.av.getSequenceSetId()); } - public boolean isUsedforaligment(AlignmentPanel ap2) + public boolean isUsedForAligment(AlignmentPanel ap2) { return (_alignwith != null) && _alignwith.contains(ap2); } - public boolean isUsedforcolourby(AlignmentPanel ap2) + public boolean isUsedForColourBy(Object o) { - return (_colourwith != null) && _colourwith.contains(ap2); + return (_colourwith != null) && _colourwith.contains(o); } /** @@ -315,9 +316,10 @@ public abstract class StructureViewerBase extends GStructureViewer boolean enableColourBySeq) { useAlignmentPanelForColourbyseq(nap); - getBinding().setColourBySequence(enableColourBySeq); seqColour.setSelected(enableColourBySeq); viewerColour.setSelected(!enableColourBySeq); + + getBinding().setColourBy(enableColourBySeq ? ColourBy.Sequence : ColourBy.Viewer); } public void useAlignmentPanelForColourbyseq(AlignmentPanel nap) @@ -565,7 +567,7 @@ public abstract class StructureViewerBase extends GStructureViewer AlignmentI al = getAlignmentPanel().av.getAlignment(); ColourSchemeI cs = ColourSchemes.getInstance() .getColourScheme(colourSchemeName, al, null); - getBinding().setJalviewColourScheme(cs); + getBinding().setJalviewColourScheme(cs, getAlignmentPanel()); } /** @@ -576,6 +578,8 @@ public abstract class StructureViewerBase extends GStructureViewer colourMenu.removeAll(); AlignmentI al = getAlignmentPanel().av.getAlignment(); + colourMenu.addSeparator(); + /* * add colour by sequence, by chain, by charge and cysteine */ @@ -584,11 +588,11 @@ public abstract class StructureViewerBase extends GStructureViewer colourMenu.add(chargeColour); chargeColour.setEnabled(!al.isNucleotide()); - /* - * add all 'simple' (per-residue) colour schemes registered to Jalview - */ + /* + * add all colour schemes registered to Jalview + */ ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this, - al, true); + al); /* * add 'colour by viewer' (menu item text is set in subclasses) @@ -746,12 +750,6 @@ public abstract class StructureViewerBase extends GStructureViewer buildColourMenu(); } - @Override - public void setJalviewColourScheme(ColourSchemeI cs) - { - getBinding().setJalviewColourScheme(cs); - } - /** * Sends commands to the structure viewer to superimpose structures based on * currently associated alignments. May optionally return an error message for @@ -827,7 +825,7 @@ public abstract class StructureViewerBase extends GStructureViewer if (viewerColour.isSelected()) { // disable automatic sequence colouring. - getBinding().setColourBySequence(false); + getBinding().setColourBy(ColourBy.Viewer); } } @@ -849,12 +847,12 @@ public abstract class StructureViewerBase extends GStructureViewer public void seqColour_actionPerformed(ActionEvent actionEvent) { AAStructureBindingModel binding = getBinding(); - binding.setColourBySequence(seqColour.isSelected()); + boolean colourBySequence = seqColour.isSelected(); if (_colourwith == null) { _colourwith = new Vector<>(); } - if (binding.isColourBySequence()) + if (colourBySequence) { if (!binding.isLoadingFromArchive()) { diff --git a/src/jalview/renderer/ResidueColourFinder.java b/src/jalview/renderer/ResidueColourFinder.java index 2da7233..0b197b9 100644 --- a/src/jalview/renderer/ResidueColourFinder.java +++ b/src/jalview/renderer/ResidueColourFinder.java @@ -20,19 +20,30 @@ */ package jalview.renderer; +import jalview.api.AlignViewportI; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.renderer.seqfeatures.FeatureColourFinder; +import jalview.schemes.ColourSchemeI; import java.awt.Color; public class ResidueColourFinder { + private ResidueShaderI shader; + public ResidueColourFinder() { } - /** + public ResidueColourFinder(AlignViewportI viewport, ColourSchemeI cs) + { + shader = new ResidueShader(cs); + // TODO what if consensus is still calculating - or changes? + shader.setConsensus(viewport.getSequenceConsensusHash()); + } + +/** * Get the colour of a residue in a sequence * * @param showBoxes @@ -49,11 +60,18 @@ public class ResidueColourFinder * FeatureColourFinder for the viewport * @return colour of the residue */ - public Color getResidueColour(boolean showBoxes, ResidueShaderI shader, + public Color getResidueColour(boolean showBoxes, ResidueShaderI colourScheme, SequenceGroup[] allGroups, final SequenceI seq, int position, FeatureColourFinder finder) { - Color col = getResidueBoxColour(showBoxes, shader, allGroups, seq, + /* + * override alignment colour scheme if one set on construction of this + */ + if (shader != null) + { + colourScheme = shader; + } + Color col = getResidueBoxColour(showBoxes, colourScheme, allGroups, seq, position); // if there's a FeatureColourFinder we might override the residue colour @@ -145,12 +163,7 @@ public class ResidueColourFinder */ public Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i) { - Color resBoxColour = Color.white; - if (shader.getColourScheme() != null) - { - resBoxColour = shader.findColour(seq.getCharAt(i), i, seq); - } - return resBoxColour; + return shader.findColour(seq.getCharAt(i), i, seq); } } diff --git a/src/jalview/structures/models/AAStructureBindingModel.java b/src/jalview/structures/models/AAStructureBindingModel.java index 2528286..fc82827 100644 --- a/src/jalview/structures/models/AAStructureBindingModel.java +++ b/src/jalview/structures/models/AAStructureBindingModel.java @@ -21,13 +21,15 @@ package jalview.structures.models; import jalview.api.AlignmentViewPanel; -import jalview.api.SequenceRenderer; +import jalview.api.SequenceRendererI; import jalview.api.StructureSelectionManagerProvider; import jalview.api.structures.JalviewStructureDisplayI; import jalview.datamodel.AlignmentI; import jalview.datamodel.HiddenColumns; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; +import jalview.gui.AlignmentPanel; +import jalview.gui.SequenceRenderer; import jalview.io.DataSourceType; import jalview.schemes.ColourSchemeI; import jalview.structure.AtomSpec; @@ -57,7 +59,21 @@ public abstract class AAStructureBindingModel extends SequenceStructureBindingModel implements StructureListener, StructureSelectionManagerProvider { + public enum ColourBy + { + Sequence, Chain, ChargeAndCysteine, Jalview, Viewer + } + + /* + * selected colour menu option + */ + private ColourBy colourBy = ColourBy.Sequence; + /* + * selected colour scheme, if a Jalview colour scheme is selected + */ + private ColourSchemeI colourScheme; + private StructureSelectionManager ssm; /* @@ -80,9 +96,7 @@ public abstract class AAStructureBindingModel * datasource protocol for access to PDBEntrylatest */ DataSourceType protocol = null; - - protected boolean colourBySequence = true; - + private boolean nucleotide; private boolean finishedInit = false; @@ -334,14 +348,19 @@ public abstract class AAStructureBindingModel } + public void setColourBy(ColourBy option) + { + colourBy = option; + } + public boolean isColourBySequence() { - return colourBySequence; + return colourBy == ColourBy.Sequence; } - - public void setColourBySequence(boolean colourBySequence) + + protected boolean isJalviewColourScheme() { - this.colourBySequence = colourBySequence; + return colourBy == ColourBy.Jalview; } protected void addSequenceAndChain(int pe, SequenceI[] seq, @@ -748,9 +767,37 @@ public abstract class AAStructureBindingModel return null; } - public abstract void setJalviewColourScheme(ColourSchemeI cs); + /** + * Sets the selected colour scheme, possibly with reference to the given + * alignment view + * + * @param cs + * @param ap + */ + public void setJalviewColourScheme(ColourSchemeI cs, AlignmentViewPanel ap) + { + colourBy = ColourBy.Jalview; + colourScheme = cs; + + if (cs == null || cs.isSimple()) + { + setSimpleColourScheme(cs); + } + else + { + colourBySequence(ap, new SequenceRenderer(ap.getAlignViewport(), cs)); + } + } /** + * Sets a colour scheme which is determined solely by the residue at each + * position + * + * @param cs + */ + protected abstract void setSimpleColourScheme(ColourSchemeI cs); + + /** * Constructs and sends a command to align structures against a reference * structure, based on one or more sequence alignments. May optionally return * an error or warning message for the alignment command. @@ -772,34 +819,33 @@ public abstract class AAStructureBindingModel public abstract void setBackgroundColour(Color col); protected abstract StructureMappingcommandSet[] getColourBySequenceCommands( - String[] files, SequenceRenderer sr, AlignmentViewPanel avp); - - /** - * returns the current sequenceRenderer that should be used to colour the - * structures - * - * @param alignment - * - * @return - */ - public abstract SequenceRenderer getSequenceRenderer( - AlignmentViewPanel alignment); + String[] files, SequenceRendererI sr, AlignmentViewPanel avp); protected abstract void colourBySequence( StructureMappingcommandSet[] colourBySequenceCommands); - public abstract void colourByChain(); + public void colourByChain() + { + colourBy = ColourBy.Chain; + } + + public void colourByCharge() + { + colourBy = ColourBy.ChargeAndCysteine; + } - public abstract void colourByCharge(); + public void colourBySequence(AlignmentViewPanel alignmentv) + { + colourBySequence(alignmentv, alignmentv.getSequenceRenderer()); + } /** - * colour any structures associated with sequences in the given alignment - * using the getFeatureRenderer() and getSequenceRenderer() renderers but only - * if colourBySequence is enabled. + * Colours any structures associated with sequences in the given alignment view + * using the getFeatureRenderer() and getSequenceRenderer() renderers */ - public void colourBySequence(AlignmentViewPanel alignmentv) + public void colourBySequence(AlignmentViewPanel alignmentv, SequenceRendererI sr) { - if (!colourBySequence || !isLoadingFinished()) + if (!isLoadingFinished()) { return; } @@ -809,8 +855,6 @@ public abstract class AAStructureBindingModel } String[] files = getStructureFiles(); - SequenceRenderer sr = getSequenceRenderer(alignmentv); - StructureMappingcommandSet[] colourBySequenceCommands = getColourBySequenceCommands( files, sr, alignmentv); colourBySequence(colourBySequenceCommands); @@ -823,4 +867,42 @@ public abstract class AAStructureBindingModel public abstract jalview.api.FeatureRenderer getFeatureRenderer( AlignmentViewPanel alignment); + +@Override +public void updateColours(Object source) { + AlignmentPanel ap = (AlignmentPanel) source; + + /* + * ignore events from panels not used to colour this view + */ + if (!getViewer().isUsedForColourBy(ap)) + { + return; + } + + /* + * no need to update colours if structure colouring is not + * viewport dependent + */ + if (!isColourBySequence() && !isJalviewColourScheme()) + { + return; + } + if (colourScheme == null || colourScheme.isSimple()) + { + return; + } + + if (!isLoadingFromArchive()) + { + if (isColourBySequence()) + { + colourBySequence(ap, new SequenceRenderer(ap.getAlignViewport())); + } + else + { + colourBySequence(ap, new SequenceRenderer(ap.getAlignViewport(), colourScheme)); + } + } + } } diff --git a/test/jalview/structures/models/AAStructureBindingModelTest.java b/test/jalview/structures/models/AAStructureBindingModelTest.java index af02d5e..1b5045a 100644 --- a/test/jalview/structures/models/AAStructureBindingModelTest.java +++ b/test/jalview/structures/models/AAStructureBindingModelTest.java @@ -26,7 +26,7 @@ import static org.testng.AssertJUnit.assertTrue; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureRenderer; -import jalview.api.SequenceRenderer; +import jalview.api.SequenceRendererI; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; import jalview.datamodel.HiddenColumns; @@ -139,21 +139,16 @@ public class AAStructureBindingModelTest @Override public void updateColours(Object source) { - // TODO Auto-generated method stub - } @Override public void releaseReferences(Object svl) { - // TODO Auto-generated method stub - } @Override public String[] getStructureFiles() { - // TODO Auto-generated method stub return null; } @@ -161,57 +156,41 @@ public class AAStructureBindingModelTest public String superposeStructures(AlignmentI[] alignments, int[] structureIndices, HiddenColumns[] hiddenCols) { - // TODO Auto-generated method stub return null; } @Override - public void setJalviewColourScheme(ColourSchemeI cs) + public void setSimpleColourScheme(ColourSchemeI cs) { - // TODO Auto-generated method stub - } @Override public void setBackgroundColour(Color col) { - // TODO Auto-generated method stub } @Override public void highlightAtoms(List atoms) { - // TODO Auto-generated method stub - - } - - @Override - public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment) - { - // TODO Auto-generated method stub - return null; } @Override public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment) { - // TODO Auto-generated method stub return null; } @Override protected StructureMappingcommandSet[] getColourBySequenceCommands( - String[] files, SequenceRenderer sr, AlignmentViewPanel avp) + String[] files, SequenceRendererI sr, AlignmentViewPanel avp) { - // TODO Auto-generated method stub return null; } @Override public List getChainNames() { - // TODO Auto-generated method stub return null; } @@ -219,22 +198,16 @@ public class AAStructureBindingModelTest protected void colourBySequence( StructureMappingcommandSet[] colourBySequenceCommands) { - // TODO Auto-generated method stub - } @Override public void colourByCharge() { - // TODO Auto-generated method stub - } @Override public void colourByChain() { - // TODO Auto-generated method stub - } }; String[][] chains = binder.getChains(); @@ -311,7 +284,7 @@ public class AAStructureBindingModelTest } @Override - public void setJalviewColourScheme(ColourSchemeI cs) + public void setSimpleColourScheme(ColourSchemeI cs) { } @@ -329,14 +302,7 @@ public class AAStructureBindingModelTest @Override protected StructureMappingcommandSet[] getColourBySequenceCommands( - String[] files, SequenceRenderer sr, AlignmentViewPanel avp) - { - return null; - } - - @Override - public SequenceRenderer getSequenceRenderer( - AlignmentViewPanel alignment) + String[] files, SequenceRendererI sr, AlignmentViewPanel avp) { return null; } -- 1.7.10.2