X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fappletgui%2FAlignmentPanel.java;h=0516533063f4a4e8a4958e5cdca6ebf91cbab13f;hb=2503f4dfa319a2854d2cc8f7e07e7b979df6bff6;hp=7c4882a5c2147fed77b6d9e1885d6d8edac9a01e;hpb=ad15cff29620f960119f80176f1fd443da9f6763;p=jalview.git diff --git a/src/jalview/appletgui/AlignmentPanel.java b/src/jalview/appletgui/AlignmentPanel.java index 7c4882a..0516533 100644 --- a/src/jalview/appletgui/AlignmentPanel.java +++ b/src/jalview/appletgui/AlignmentPanel.java @@ -20,12 +20,6 @@ */ package jalview.appletgui; -import jalview.api.AlignmentViewPanel; -import jalview.datamodel.AlignmentI; -import jalview.datamodel.SearchResults; -import jalview.datamodel.SequenceI; -import jalview.structure.StructureSelectionManager; - import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; @@ -38,6 +32,16 @@ import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.util.List; + +import jalview.analysis.AnnotationSorter; +import jalview.api.AlignViewportI; +import jalview.api.AlignmentViewPanel; +import jalview.bin.JalviewLite; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.SearchResults; +import jalview.datamodel.SequenceI; +import jalview.structure.StructureSelectionManager; public class AlignmentPanel extends Panel implements AdjustmentListener, AlignmentViewPanel @@ -100,7 +104,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH); alabels = new AnnotationLabels(this); - setAnnotationVisible(av.showAnnotation); + setAnnotationVisible(av.isShowAnnotation()); idPanelHolder.add(idPanel, BorderLayout.CENTER); idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER); @@ -153,6 +157,11 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, }); } + @Override + public AlignViewportI getAlignViewport() + { + return av; + } public SequenceRenderer getSequenceRenderer() { return seqPanel.seqCanvas.sr; @@ -190,8 +199,9 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, idPanel.idCanvas.image = null; FontMetrics fm = getFontMetrics(av.getFont()); - scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent())); - idwidthAdjuster.setSize(new Dimension(10, av.charHeight + scalePanel.setSize(new Dimension(10, av.getCharHeight() + + fm.getDescent())); + idwidthAdjuster.setSize(new Dimension(10, av.getCharHeight() + fm.getDescent())); av.updateSequenceIdColours(); annotationPanel.image = null; @@ -305,7 +315,21 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, public boolean scrollToPosition(SearchResults results, boolean redrawOverview) { + return scrollToPosition(results, redrawOverview, false); + } + /** + * scroll the view to show the position of the highlighted region in results + * (if any) + * + * @param results + * @param redrawOverview + * - when set, the overview will be recalculated (takes longer) + * @return false if results were not found + */ + public boolean scrollToPosition(SearchResults results, + boolean redrawOverview, boolean centre) + { // do we need to scroll the panel? if (results != null && results.getSize() > 0) { @@ -319,7 +343,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, int[] r = results.getResults(seq, 0, alignment.getWidth()); if (r == null) { - if (av.applet.debug) + if (JalviewLite.debug) {// DEBUG System.out .println("DEBUG: scroll didn't happen - results not within alignment : " @@ -327,7 +351,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, } return false; } - if (av.applet.debug) + if (JalviewLite.debug) { // DEBUG /* @@ -339,6 +363,18 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, } int start = r[0]; int end = r[1]; + + /* + * To centre results, scroll to positions half the visible width + * left/right of the start/end positions + */ + if (centre) + { + int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1; + start = Math.max(start - offset, 0); + end = Math.min(end + offset, seq.getEnd() - 1); + } + if (start < 0) { return false; @@ -375,7 +411,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, { start = ostart; } - if (!av.wrapAlignment) + if (!av.getWrapAlignment()) { /* * int spos=av.getStartRes(),sqpos=av.getStartSeq(); if ((startv = @@ -386,33 +422,64 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, * av.getStartSeq()) > seqIndex) { setScrollValues(av.getStartRes(), * seqIndex); } else if ((ends = av.getEndSeq()) <= seqIndex) { * setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1); } - * - * /* */ - if ((av.getStartRes() > end) - || (av.getEndRes() < start) - || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex))) - { - if (start > av.getAlignment().getWidth() - hextent) - { - start = av.getAlignment().getWidth() - hextent; - if (start < 0) - { - start = 0; - } + // below is scrolling logic up to Jalview 2.8.2 + // if ((av.getStartRes() > end) + // || (av.getEndRes() < start) + // || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex))) + // { + // if (start > av.getAlignment().getWidth() - hextent) + // { + // start = av.getAlignment().getWidth() - hextent; + // if (start < 0) + // { + // start = 0; + // } + // + // } + // if (seqIndex > av.getAlignment().getHeight() - vextent) + // { + // seqIndex = av.getAlignment().getHeight() - vextent; + // if (seqIndex < 0) + // { + // seqIndex = 0; + // } + // } + // setScrollValues(start, seqIndex); + // } + // logic copied from jalview.gui.AlignmentPanel: + if ((startv = av.getStartRes()) >= start) + { + /* + * Scroll left to make start of search results visible + */ + setScrollValues(start - 1, seqIndex); } - if (seqIndex > av.getAlignment().getHeight() - vextent) + else if ((endv = av.getEndRes()) <= end) { - seqIndex = av.getAlignment().getHeight() - vextent; - if (seqIndex < 0) - { - seqIndex = 0; - } + /* + * Scroll right to make end of search results visible + */ + setScrollValues(startv + 1 + end - endv, seqIndex); } - // System.out.println("trying to scroll to: "+start+" "+seqIndex); - setScrollValues(start, seqIndex); - }/**/ + else if ((starts = av.getStartSeq()) > seqIndex) + { + /* + * Scroll up to make start of search results visible + */ + setScrollValues(av.getStartRes(), seqIndex); + } + else if ((ends = av.getEndSeq()) <= seqIndex) + { + /* + * Scroll down to make end of search results visible + */ + setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1); + } + /* + * Else results are already visible - no need to scroll + */ } else { @@ -449,7 +516,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, public void setAnnotationVisible(boolean b) { - if (!av.wrapAlignment) + if (!av.getWrapAlignment()) { annotationSpaceFillerHolder.setVisible(b); annotationPanelHolder.setVisible(b); @@ -473,7 +540,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, // this is called after loading new annotation onto alignment if (alignFrame.getSize().height == 0) { - System.out.println("NEEDS FIXING"); + System.out + .println("adjustAnnotationHeight frame size zero NEEDS FIXING"); } fontChanged(); validateAnnotationDimensions(true); @@ -566,7 +634,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, annotationPanelHolder.setVisible(false); annotationSpaceFillerHolder.setVisible(false); } - else if (av.showAnnotation) + else if (av.isShowAnnotation()) { annotationPanelHolder.setVisible(true); annotationSpaceFillerHolder.setVisible(true); @@ -648,8 +716,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, } ; - hextent = seqPanel.seqCanvas.getSize().width / av.charWidth; - vextent = seqPanel.seqCanvas.getSize().height / av.charHeight; + hextent = seqPanel.seqCanvas.getSize().width / av.getCharWidth(); + vextent = seqPanel.seqCanvas.getSize().height / av.getCharHeight(); if (hextent > width) { @@ -694,7 +762,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, av.setEndSeq(endSeq); av.setStartRes(x); - av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1); + av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av + .getCharWidth())) - 1); hscroll.setValues(x, hextent, 0, width); vscroll.setValues(y, vextent, 0, height); @@ -776,13 +845,102 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, seqPanel.seqCanvas.fastPaint(scrollX, scrollY); scalePanel.repaint(); - if (av.getShowAnnotation()) + if (av.isShowAnnotation()) { annotationPanel.fastPaint(av.getStartRes() - oldX); } } sendViewPosition(); + /* + * If there is one, scroll the (Protein/cDNA) complementary alignment to + * match, unless we are ourselves doing that. + */ + if (isFollowingComplementScroll()) + { + setFollowingComplementScroll(false); + } + else + { + AlignmentPanel ap = getComplementPanel(); + av.scrollComplementaryAlignment(ap); + } + + } + + /** + * A helper method to return the AlignmentPanel in the other (complementary) + * half of a SplitFrame view. Returns null if not in a SplitFrame. + * + * @return + */ + private AlignmentPanel getComplementPanel() + { + AlignmentPanel ap = null; + if (alignFrame != null) + { + SplitFrame sf = alignFrame.getSplitFrame(); + if (sf != null) + { + AlignFrame other = sf.getComplement(alignFrame); + if (other != null) + { + ap = other.alignPanel; + } + } + } + return ap; + } + + /** + * Follow a scrolling change in the (cDNA/Protein) complementary alignment. + * The aim is to keep the two alignments 'lined up' on their centre columns. + * + * @param sr + * holds mapped region(s) of this alignment that we are scrolling + * 'to'; may be modified for sequence offset by this method + * @param seqOffset + * the number of visible sequences to show above the mapped region + */ + protected void scrollToCentre(SearchResults sr, int seqOffset) + { + /* + * To avoid jumpy vertical scrolling (if some sequences are gapped or not + * mapped), we can make the scroll-to location a sequence above the one + * actually mapped. + */ + SequenceI mappedTo = sr.getResultSequence(0); + List seqs = av.getAlignment().getSequences(); + + /* + * This is like AlignmentI.findIndex(seq) but here we are matching the + * dataset sequence not the aligned sequence + */ + int sequenceIndex = 0; + boolean matched = false; + for (SequenceI seq : seqs) + { + if (mappedTo == seq.getDatasetSequence()) + { + matched = true; + break; + } + sequenceIndex++; + } + if (!matched) + { + return; // failsafe, shouldn't happen + } + sequenceIndex = Math.max(0, sequenceIndex - seqOffset); + sr.getResults().get(0) + .setSequence(av.getAlignment().getSequenceAt(sequenceIndex)); + + /* + * Scroll to position but centring the target residue. Also set a state flag + * to prevent adjustmentValueChanged performing this recursively. + */ + setFollowingComplementScroll(true); + scrollToPosition(sr, true, true); } private void sendViewPosition() @@ -792,8 +950,15 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, av.endSeq); } + /** + * Repaint the alignment and annotations, and, optionally, any overview window + */ public void paintAlignment(boolean updateOverview) { + final AnnotationSorter sorter = new AnnotationSorter(getAlignment(), + av.isShowAutocalculatedAbove()); + sorter.sort(getAlignment().getAlignmentAnnotation(), + av.getSortAnnotationsBy()); repaint(); if (updateOverview) @@ -818,7 +983,11 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, { invalidate(); Dimension d = idPanel.idCanvas.getSize(); - idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height); + final int canvasHeight = seqPanel.seqCanvas.getSize().height; + if (canvasHeight != d.height) + { + idPanel.idCanvas.setSize(d.width, canvasHeight); + } if (av.getWrapAlignment()) { @@ -847,9 +1016,9 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, seqPanel.seqCanvas.repaint(); idPanel.idCanvas.repaint(); - if (!av.wrapAlignment) + if (!av.getWrapAlignment()) { - if (av.showAnnotation) + if (av.isShowAnnotation()) { alabels.repaint(); annotationPanel.repaint(); @@ -867,72 +1036,58 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, protected Panel seqPanelHolder = new Panel(); - BorderLayout borderLayout1 = new BorderLayout(); - - BorderLayout borderLayout3 = new BorderLayout(); - protected Panel scalePanelHolder = new Panel(); protected Panel idPanelHolder = new Panel(); - BorderLayout borderLayout5 = new BorderLayout(); - protected Panel idSpaceFillerPanel1 = new Panel(); public Panel annotationSpaceFillerHolder = new Panel(); - BorderLayout borderLayout6 = new BorderLayout(); - - BorderLayout borderLayout7 = new BorderLayout(); - - Panel hscrollHolder = new Panel(); - - BorderLayout borderLayout10 = new BorderLayout(); - protected Panel hscrollFillerPanel = new Panel(); - BorderLayout borderLayout11 = new BorderLayout(); - - BorderLayout borderLayout4 = new BorderLayout(); - - BorderLayout borderLayout2 = new BorderLayout(); - Panel annotationPanelHolder = new Panel(); protected Scrollbar apvscroll = new Scrollbar(); - BorderLayout borderLayout12 = new BorderLayout(); + /* + * Flag set while scrolling to follow complementary cDNA/protein scroll. When + * true, suppresses invoking the same method recursively. + */ + private boolean followingComplementScroll; private void jbInit() throws Exception { // idPanelHolder.setPreferredSize(new Dimension(70, 10)); - this.setLayout(borderLayout7); + this.setLayout(new BorderLayout()); // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150)); - sequenceHolderPanel.setLayout(borderLayout3); - seqPanelHolder.setLayout(borderLayout1); + sequenceHolderPanel.setLayout(new BorderLayout()); + seqPanelHolder.setLayout(new BorderLayout()); scalePanelHolder.setBackground(Color.white); // scalePanelHolder.setPreferredSize(new Dimension(10, 30)); - scalePanelHolder.setLayout(borderLayout6); - idPanelHolder.setLayout(borderLayout5); + scalePanelHolder.setLayout(new BorderLayout()); + idPanelHolder.setLayout(new BorderLayout()); idSpaceFillerPanel1.setBackground(Color.white); // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30)); - idSpaceFillerPanel1.setLayout(borderLayout11); + idSpaceFillerPanel1.setLayout(new BorderLayout()); annotationSpaceFillerHolder.setBackground(Color.white); // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80)); - annotationSpaceFillerHolder.setLayout(borderLayout4); + annotationSpaceFillerHolder.setLayout(new BorderLayout()); hscroll.setOrientation(Scrollbar.HORIZONTAL); - hscrollHolder.setLayout(borderLayout10); + + Panel hscrollHolder = new Panel(); + hscrollHolder.setLayout(new BorderLayout()); hscrollFillerPanel.setBackground(Color.white); apvscroll.setOrientation(Scrollbar.VERTICAL); apvscroll.setVisible(true); apvscroll.addAdjustmentListener(this); annotationPanelHolder.setBackground(Color.white); - annotationPanelHolder.setLayout(borderLayout12); + annotationPanelHolder.setLayout(new BorderLayout()); annotationPanelHolder.add(apvscroll, BorderLayout.EAST); // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10)); hscrollHolder.setBackground(Color.white); @@ -1003,4 +1158,19 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, error.printStackTrace(); } + /** + * Set a flag to say we are scrolling to follow a (cDNA/protein) complement. + * + * @param b + */ + protected void setFollowingComplementScroll(boolean b) + { + this.followingComplementScroll = b; + } + + protected boolean isFollowingComplementScroll() + { + return this.followingComplementScroll; + } + }