2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.viewmodel;
20 import jalview.analysis.Conservation;
21 import jalview.api.AlignCalcManagerI;
22 import jalview.api.AlignViewportI;
23 import jalview.api.AlignmentViewPanel;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AlignmentView;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.ClustalxColourScheme;
32 import jalview.schemes.ColourSchemeI;
33 import jalview.schemes.ResidueProperties;
34 import jalview.workers.AlignCalcManager;
35 import jalview.workers.ConsensusThread;
36 import jalview.workers.ConservationThread;
37 import jalview.workers.StrucConsensusThread;
39 import java.util.Hashtable;
40 import java.util.Vector;
43 * base class holding visualization and analysis attributes and common logic for an active alignment view displayed in the GUI
47 public abstract class AlignmentViewport implements AlignViewportI
50 * alignment displayed in the viewport. Please use get/setter
52 protected AlignmentI alignment;
54 protected String sequenceSetID;
56 private Hashtable hiddenRepSequences;
58 protected ColumnSelection colSel = new ColumnSelection();
61 public boolean autoCalculateConsensus = true;
63 protected boolean autoCalculateStrucConsensus = true;
65 protected boolean ignoreGapsInConsensusCalculation = false;
68 protected ColourSchemeI globalColourScheme = null;
71 public void setGlobalColourScheme(ColourSchemeI cs)
73 globalColourScheme = cs;
76 public ColourSchemeI getGlobalColourScheme()
78 return globalColourScheme;
82 protected AlignmentAnnotation consensus;
84 protected AlignmentAnnotation strucConsensus;
86 protected AlignmentAnnotation conservation;
88 protected AlignmentAnnotation quality;
90 protected AlignmentAnnotation[] groupConsensus;
92 protected AlignmentAnnotation[] groupConservation;
95 * results of alignment consensus analysis for visible portion of view
97 protected Hashtable[] hconsensus=null;
100 * results of secondary structure base pair consensus for visible portion of view
102 protected Hashtable[] hStrucConsensus=null;
105 * percentage gaps allowed in a column before all amino acid properties should be considered unconserved
107 int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
110 public int getConsPercGaps()
115 public void setSequenceConsensusHash(Hashtable[] hconsensus)
117 this.hconsensus=hconsensus;
122 public Hashtable[] getSequenceConsensusHash()
128 public Hashtable[] getRnaStructureConsensusHash()
130 return hStrucConsensus;
133 public void setRnaStructureConsensusHash(Hashtable[] hStrucConsensus)
135 this.hStrucConsensus=hStrucConsensus;
139 public AlignmentAnnotation getAlignmentQualityAnnot()
145 public AlignmentAnnotation getAlignmentConservationAnnotation()
150 public AlignmentAnnotation getAlignmentConsensusAnnotation()
155 public AlignmentAnnotation getAlignmentStrucConsensusAnnotation()
157 return strucConsensus;
160 protected AlignCalcManagerI calculator=new AlignCalcManager();
162 jalview.workers.ConsensusThread consensusThread;
164 StrucConsensusThread strucConsensusThread;
167 private ConservationThread conservationThread;
170 * trigger update of conservation annotation
172 public void updateConservation(final AlignmentViewPanel ap)
174 // see note in mantis : issue number 8585
175 if (alignment.isNucleotide() || conservation == null
176 || !autoCalculateConsensus)
181 calculator.startWorker(conservationThread=new jalview.workers.ConservationThread(this, ap));
185 * trigger update of consensus annotation
187 public void updateConsensus(final AlignmentViewPanel ap)
189 // see note in mantis : issue number 8585
190 if (consensus == null || !autoCalculateConsensus)
194 calculator.startWorker(consensusThread = new ConsensusThread(this, ap));
197 // --------START Structure Conservation
198 public void updateStrucConsensus(final AlignmentViewPanel ap)
200 // see note in mantis : issue number 8585
201 if (strucConsensus == null || !autoCalculateStrucConsensus)
205 calculator.startWorker(strucConsensusThread = new StrucConsensusThread(this,ap));
208 public boolean isCalcInProgress()
210 // TODO generalise to iterate over all calculators associated with av
211 return calculator.isWorking();
214 public boolean isCalculationInProgress(
215 AlignmentAnnotation alignmentAnnotation)
217 if (!alignmentAnnotation.autoCalculated)
219 if ((calculator.isWorking(consensusThread) && consensus==alignmentAnnotation)
220 || (calculator.isWorking(conservationThread) && (conservation==alignmentAnnotation || quality==alignmentAnnotation))
221 || (calculator.isWorking(strucConsensusThread) && strucConsensus==alignmentAnnotation)
229 public boolean isClosed()
231 // TODO: check that this isClosed is only true after panel is closed, not before it is fully constructed.
232 return alignment==null;
236 public AlignCalcManagerI getCalcManager()
242 * should conservation rows be shown for groups
244 protected boolean showGroupConservation = false;
247 * should consensus rows be shown for groups
249 protected boolean showGroupConsensus = false;
252 * should consensus profile be rendered by default
254 protected boolean showSequenceLogo = false;
256 * should consensus profile be rendered normalised to row height
258 protected boolean normaliseSequenceLogo = false;
260 * should consensus histograms be rendered by default
262 protected boolean showConsensusHistogram = true;
265 * @return the showConsensusProfile
267 public boolean isShowSequenceLogo()
269 return showSequenceLogo;
273 * @param showSequenceLogo
276 public void setShowSequenceLogo(boolean showSequenceLogo)
278 if (showSequenceLogo != this.showSequenceLogo)
280 // TODO: decouple settings setting from calculation when refactoring
281 // annotation update method from alignframe to viewport
282 this.showSequenceLogo = showSequenceLogo;
283 if (consensusThread != null)
285 consensusThread.updateAnnotation();
287 if (strucConsensusThread != null)
289 strucConsensusThread.updateAnnotation();
292 this.showSequenceLogo = showSequenceLogo;
296 * @param showConsensusHistogram
297 * the showConsensusHistogram to set
299 public void setShowConsensusHistogram(boolean showConsensusHistogram)
301 this.showConsensusHistogram = showConsensusHistogram;
305 * @return the showGroupConservation
307 public boolean isShowGroupConservation()
309 return showGroupConservation;
313 * @param showGroupConservation
314 * the showGroupConservation to set
316 public void setShowGroupConservation(boolean showGroupConservation)
318 this.showGroupConservation = showGroupConservation;
322 * @return the showGroupConsensus
324 public boolean isShowGroupConsensus()
326 return showGroupConsensus;
330 * @param showGroupConsensus
331 * the showGroupConsensus to set
333 public void setShowGroupConsensus(boolean showGroupConsensus)
335 this.showGroupConsensus = showGroupConsensus;
340 * @return flag to indicate if the consensus histogram should be rendered by
343 public boolean isShowConsensusHistogram()
345 return this.showConsensusHistogram;
349 * show non-conserved residues only
351 protected boolean showUnconserved = false;
355 * when set, updateAlignment will always ensure sequences are of equal length
357 private boolean padGaps = false;
360 * when set, alignment should be reordered according to a newly opened tree
362 public boolean sortByTree = false;
364 public boolean getShowUnconserved()
366 return showUnconserved;
369 public void setShowUnconserved(boolean showunconserved)
371 showUnconserved = showunconserved;
375 * @param showNonconserved
376 * the showUnconserved to set
378 public void setShowunconserved(boolean displayNonconserved)
380 this.showUnconserved = displayNonconserved;
386 * @return null or the currently selected sequence region
388 public SequenceGroup getSelectionGroup()
390 return selectionGroup;
394 * Set the selection group for this window.
397 * - group holding references to sequences in this alignment view
400 public void setSelectionGroup(SequenceGroup sg)
405 public void setHiddenColumns(ColumnSelection colsel)
407 this.colSel = colsel;
408 if (colSel.getHiddenColumns() != null)
410 hasHiddenColumns = true;
414 public ColumnSelection getColumnSelection()
418 public void setColumnSelection(ColumnSelection colSel)
422 public Hashtable getHiddenRepSequences()
424 return hiddenRepSequences;
426 public void setHiddenRepSequences(Hashtable hiddenRepSequences)
428 this.hiddenRepSequences = hiddenRepSequences;
430 protected boolean hasHiddenColumns = false;
432 public void updateHiddenColumns()
434 hasHiddenColumns = colSel.getHiddenColumns() != null;
437 protected boolean hasHiddenRows = false;
439 public boolean hasHiddenRows() {
440 return hasHiddenRows;
443 protected SequenceGroup selectionGroup;
445 public void setSequenceSetId(String newid)
447 if (sequenceSetID!=null)
449 System.err.println("Warning - overwriting a sequenceSetId for a viewport!");
451 sequenceSetID=new String(newid);
453 public String getSequenceSetId()
455 if (sequenceSetID == null)
457 sequenceSetID = alignment.hashCode() + "";
460 return sequenceSetID;
463 * unique viewId for synchronizing state (e.g. with stored Jalview Project)
466 protected String viewId = null;
468 public String getViewId()
472 viewId = this.getSequenceSetId() + "." + this.hashCode() + "";
476 public void setIgnoreGapsConsensus(boolean b, AlignmentViewPanel ap)
478 ignoreGapsInConsensusCalculation = b;
479 if (ap!=null) {updateConsensus(ap);
480 if (globalColourScheme != null)
482 globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
483 ignoreGapsInConsensusCalculation);
487 private long sgrouphash = -1, colselhash = -1;
490 * checks current SelectionGroup against record of last hash value, and
494 * update the record of last hash value
496 * @return true if SelectionGroup changed since last call (when b is true)
498 public boolean isSelectionGroupChanged(boolean b)
500 int hc = (selectionGroup == null || selectionGroup.getSize() == 0) ? -1
501 : selectionGroup.hashCode();
502 if (hc != -1 && hc != sgrouphash)
514 * checks current colsel against record of last hash value, and optionally
518 * update the record of last hash value
519 * @return true if colsel changed since last call (when b is true)
521 public boolean isColSelChanged(boolean b)
523 int hc = (colSel == null || colSel.size() == 0) ? -1 : colSel
525 if (hc != -1 && hc != colselhash)
536 public boolean getIgnoreGapsConsensus()
538 return ignoreGapsInConsensusCalculation;
541 /// property change stuff
543 // JBPNote Prolly only need this in the applet version.
544 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
549 * Property change listener for changes in alignment
554 public void addPropertyChangeListener(
555 java.beans.PropertyChangeListener listener)
557 changeSupport.addPropertyChangeListener(listener);
566 public void removePropertyChangeListener(
567 java.beans.PropertyChangeListener listener)
569 changeSupport.removePropertyChangeListener(listener);
573 * Property change listener for changes in alignment
582 public void firePropertyChange(String prop, Object oldvalue,
585 changeSupport.firePropertyChange(prop, oldvalue, newvalue);
588 // common hide/show column stuff
591 public void hideSelectedColumns()
593 if (colSel.size() < 1)
598 colSel.hideSelectedColumns();
599 setSelectionGroup(null);
601 hasHiddenColumns = true;
604 public void hideColumns(int start, int end)
608 colSel.hideColumns(start);
612 colSel.hideColumns(start, end);
615 hasHiddenColumns = true;
618 public void showColumn(int col)
620 colSel.revealHiddenColumns(col);
621 if (colSel.getHiddenColumns() == null)
623 hasHiddenColumns = false;
627 public void showAllHiddenColumns()
629 colSel.revealAllHiddenColumns();
630 hasHiddenColumns = false;
634 // common hide/show seq stuff
635 public void showAllHiddenSeqs()
637 if (alignment.getHiddenSequences().getSize() > 0)
639 if (selectionGroup == null)
641 selectionGroup = new SequenceGroup();
642 selectionGroup.setEndRes(alignment.getWidth() - 1);
644 Vector tmp = alignment.getHiddenSequences().showAll(
646 for (int t = 0; t < tmp.size(); t++)
648 selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
651 hasHiddenRows = false;
652 hiddenRepSequences = null;
654 firePropertyChange("alignment", null, alignment.getSequences());
655 // used to set hasHiddenRows/hiddenRepSequences here, after the property changed event
660 public void showSequence(int index)
662 Vector tmp = alignment.getHiddenSequences().showSequence(index,
666 if (selectionGroup == null)
668 selectionGroup = new SequenceGroup();
669 selectionGroup.setEndRes(alignment.getWidth() - 1);
672 for (int t = 0; t < tmp.size(); t++)
674 selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
676 // JBPNote: refactor: only update flag if we modified visiblity (used to do this regardless)
677 if (alignment.getHiddenSequences().getSize() < 1)
679 hasHiddenRows = false;
681 firePropertyChange("alignment", null, alignment.getSequences());
688 public void hideAllSelectedSeqs()
690 if (selectionGroup == null || selectionGroup.getSize() < 1)
695 SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
699 setSelectionGroup(null);
703 public void hideSequence(SequenceI[] seq)
707 for (int i = 0; i < seq.length; i++)
709 alignment.getHiddenSequences().hideSequence(seq[i]);
711 hasHiddenRows = true;
712 firePropertyChange("alignment", null, alignment.getSequences());
716 public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
718 int sSize = sg.getSize();
724 if (hiddenRepSequences == null)
726 hiddenRepSequences = new Hashtable();
729 hiddenRepSequences.put(repSequence, sg);
731 // Hide all sequences except the repSequence
732 SequenceI[] seqs = new SequenceI[sSize - 1];
734 for (int i = 0; i < sSize; i++)
736 if (sg.getSequenceAt(i) != repSequence)
738 if (index == sSize - 1)
743 seqs[index++] = sg.getSequenceAt(i);
746 sg.setSeqrep(repSequence); // note: not done in 2.7applet
747 sg.setHidereps(true); // note: not done in 2.7applet
752 public boolean isHiddenRepSequence(SequenceI seq)
754 return hiddenRepSequences != null
755 && hiddenRepSequences.containsKey(seq);
757 public SequenceGroup getRepresentedSequences(SequenceI seq)
759 return (SequenceGroup) (hiddenRepSequences == null ? null : hiddenRepSequences.get(seq));
762 public int adjustForHiddenSeqs(int alignmentIndex)
764 return alignment.getHiddenSequences().adjustForHiddenSeqs(
768 // Selection manipulation
770 * broadcast selection to any interested parties
772 public abstract void sendSelection();
775 public void invertColumnSelection()
777 colSel.invertColumnSelection(0, alignment.getWidth());
782 * This method returns an array of new SequenceI objects derived from the
783 * whole alignment or just the current selection with start and end points
786 * @note if you need references to the actual SequenceI objects in the
787 * alignment or currently selected then use getSequenceSelection()
788 * @return selection as new sequenceI objects
790 public SequenceI[] getSelectionAsNewSequence()
792 SequenceI[] sequences;
793 // JBPNote: Need to test jalviewLite.getSelectedSequencesAsAlignmentFrom - this was the only caller in the applet for this method
794 // JBPNote: in applet, this method returned references to the alignment sequences, and it did not honour the presence/absence of annotation attached to the alignment (probably!)
795 if (selectionGroup == null)
797 sequences = alignment.getSequencesArray();
798 AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
799 for (int i = 0; i < sequences.length; i++)
801 sequences[i] = new Sequence(sequences[i], annots); // construct new
809 sequences = selectionGroup.getSelectionAsNewSequences(alignment);
817 * get the currently selected sequence objects or all the sequences in the
820 * @return array of references to sequence objects
822 public SequenceI[] getSequenceSelection()
824 SequenceI[] sequences = null;
825 if (selectionGroup != null)
827 sequences = selectionGroup.getSequencesInOrder(alignment);
829 if (sequences == null)
831 sequences = alignment.getSequencesArray();
838 * This method returns the visible alignment as text, as seen on the GUI, ie
839 * if columns are hidden they will not be returned in the result. Use this for
840 * calculating trees, PCA, redundancy etc on views which contain hidden
845 public jalview.datamodel.CigarArray getViewAsCigars(
846 boolean selectedRegionOnly)
848 return new jalview.datamodel.CigarArray(alignment,
849 (hasHiddenColumns ? colSel : null),
850 (selectedRegionOnly ? selectionGroup : null));
854 * return a compact representation of the current alignment selection to pass
855 * to an analysis function
857 * @param selectedOnly
858 * boolean true to just return the selected view
859 * @return AlignmentView
861 public jalview.datamodel.AlignmentView getAlignmentView(
862 boolean selectedOnly)
864 return getAlignmentView(selectedOnly, false);
868 * return a compact representation of the current alignment selection to pass
869 * to an analysis function
871 * @param selectedOnly
872 * boolean true to just return the selected view
874 * boolean true to annotate the alignment view with groups on the
875 * alignment (and intersecting with selected region if selectedOnly
877 * @return AlignmentView
879 public jalview.datamodel.AlignmentView getAlignmentView(
880 boolean selectedOnly, boolean markGroups)
882 return new AlignmentView(alignment, colSel, selectionGroup,
883 hasHiddenColumns, selectedOnly, markGroups);
888 * This method returns the visible alignment as text, as seen on the GUI, ie
889 * if columns are hidden they will not be returned in the result. Use this for
890 * calculating trees, PCA, redundancy etc on views which contain hidden
895 public String[] getViewAsString(boolean selectedRegionOnly)
897 String[] selection = null;
898 SequenceI[] seqs = null;
900 int start = 0, end = 0;
901 if (selectedRegionOnly && selectionGroup != null)
903 iSize = selectionGroup.getSize();
904 seqs = selectionGroup.getSequencesInOrder(alignment);
905 start = selectionGroup.getStartRes();
906 end = selectionGroup.getEndRes() + 1;
910 iSize = alignment.getHeight();
911 seqs = alignment.getSequencesArray();
912 end = alignment.getWidth();
915 selection = new String[iSize];
916 if (hasHiddenColumns)
918 selection = colSel.getVisibleSequenceStrings(start, end, seqs);
922 for (i = 0; i < iSize; i++)
924 selection[i] = seqs[i].getSequenceAsString(start, end);
933 * return visible region boundaries within given column range
934 * @param min first column (inclusive, from 0)
935 * @param max last column (exclusive)
936 * @return int[][] range of {start,end} visible positions
938 public int[][] getVisibleRegionBoundaries(int min, int max)
940 Vector regions = new Vector();
946 if (hasHiddenColumns)
950 start = colSel.adjustForHiddenColumns(start);
953 end = colSel.getHiddenBoundaryRight(start);
964 regions.addElement(new int[]
967 if (hasHiddenColumns)
969 start = colSel.adjustForHiddenColumns(end);
970 start = colSel.getHiddenBoundaryLeft(start) + 1;
974 int[][] startEnd = new int[regions.size()][2];
976 regions.copyInto(startEnd);
982 * @return the padGaps
984 public boolean isPadGaps()
989 * @param padGaps the padGaps to set
991 public void setPadGaps(boolean padGaps)
993 this.padGaps = padGaps;
996 * apply any post-edit constraints and trigger any calculations needed after an edit has been performed on the alignment
999 public void alignmentChanged(AlignmentViewPanel ap)
1001 // TODO: test jvLite always pads, and that JVD has configurable padding.
1004 alignment.padGaps();
1006 if (autoCalculateConsensus)
1008 updateConsensus(ap);
1010 if (hconsensus != null && autoCalculateConsensus)
1012 updateConservation(ap);
1014 if (autoCalculateStrucConsensus)
1016 updateStrucConsensus(ap);
1019 // Reset endRes of groups if beyond alignment width
1020 int alWidth = alignment.getWidth();
1021 Vector groups = alignment.getGroups();
1024 for (int i = 0; i < groups.size(); i++)
1026 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
1027 if (sg.getEndRes() > alWidth)
1029 sg.setEndRes(alWidth - 1);
1034 if (selectionGroup != null && selectionGroup.getEndRes() > alWidth)
1036 selectionGroup.setEndRes(alWidth - 1);
1039 resetAllColourSchemes();
1041 // alignment.adjustSequenceAnnotations();
1046 * reset scope and do calculations for all applied colourschemes on alignment
1048 void resetAllColourSchemes()
1050 ColourSchemeI cs = globalColourScheme;
1053 if (cs instanceof ClustalxColourScheme)
1055 ((ClustalxColourScheme) cs).resetClustalX(alignment.getSequences(),
1056 alignment.getWidth());
1059 cs.setConsensus(hconsensus);
1060 if (cs.conservationApplied())
1062 cs.setConservation(Conservation.calculateConservation("All",
1063 ResidueProperties.propHash, 3, alignment.getSequences(), 0,
1064 alignment.getWidth(), false, getConsPercGaps(), false));
1068 int s, sSize = alignment.getGroups().size();
1069 for (s = 0; s < sSize; s++)
1071 SequenceGroup sg = (SequenceGroup) alignment.getGroups().elementAt(s);
1072 if (sg.cs != null && sg.cs instanceof ClustalxColourScheme)
1074 ((ClustalxColourScheme) sg.cs).resetClustalX(sg
1075 .getSequences(hiddenRepSequences), sg.getWidth());
1077 sg.recalcConservation();