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 return calculator.isWorking();
213 public boolean isCalculationInProgress(
214 AlignmentAnnotation alignmentAnnotation)
216 if (!alignmentAnnotation.autoCalculated)
218 if ((calculator.isWorking(consensusThread) && consensus==alignmentAnnotation)
219 || (calculator.isWorking(conservationThread) && (conservation==alignmentAnnotation || quality==alignmentAnnotation))
220 || (calculator.isWorking(strucConsensusThread) && strucConsensus==alignmentAnnotation)
228 public boolean isClosed()
230 // TODO: check that this isClosed is only true after panel is closed, not before it is fully constructed.
231 return alignment==null;
235 public AlignCalcManagerI getCalcManager()
241 * should conservation rows be shown for groups
243 protected boolean showGroupConservation = false;
246 * should consensus rows be shown for groups
248 protected boolean showGroupConsensus = false;
251 * should consensus profile be rendered by default
253 protected boolean showSequenceLogo = false;
255 * should consensus profile be rendered normalised to row height
257 protected boolean normaliseSequenceLogo = false;
259 * should consensus histograms be rendered by default
261 protected boolean showConsensusHistogram = true;
264 * @return the showConsensusProfile
266 public boolean isShowSequenceLogo()
268 return showSequenceLogo;
272 * @param showSequenceLogo
275 public void setShowSequenceLogo(boolean showSequenceLogo)
277 if (showSequenceLogo != this.showSequenceLogo)
279 // TODO: decouple settings setting from calculation when refactoring
280 // annotation update method from alignframe to viewport
281 this.showSequenceLogo = showSequenceLogo;
282 if (consensusThread != null)
284 consensusThread.updateAnnotation();
286 if (strucConsensusThread != null)
288 strucConsensusThread.updateAnnotation();
291 this.showSequenceLogo = showSequenceLogo;
295 * @param showConsensusHistogram
296 * the showConsensusHistogram to set
298 public void setShowConsensusHistogram(boolean showConsensusHistogram)
300 this.showConsensusHistogram = showConsensusHistogram;
304 * @return the showGroupConservation
306 public boolean isShowGroupConservation()
308 return showGroupConservation;
312 * @param showGroupConservation
313 * the showGroupConservation to set
315 public void setShowGroupConservation(boolean showGroupConservation)
317 this.showGroupConservation = showGroupConservation;
321 * @return the showGroupConsensus
323 public boolean isShowGroupConsensus()
325 return showGroupConsensus;
329 * @param showGroupConsensus
330 * the showGroupConsensus to set
332 public void setShowGroupConsensus(boolean showGroupConsensus)
334 this.showGroupConsensus = showGroupConsensus;
339 * @return flag to indicate if the consensus histogram should be rendered by
342 public boolean isShowConsensusHistogram()
344 return this.showConsensusHistogram;
348 * show non-conserved residues only
350 protected boolean showUnconserved = false;
354 * when set, updateAlignment will always ensure sequences are of equal length
356 private boolean padGaps = false;
359 * when set, alignment should be reordered according to a newly opened tree
361 public boolean sortByTree = false;
363 public boolean getShowUnconserved()
365 return showUnconserved;
368 public void setShowUnconserved(boolean showunconserved)
370 showUnconserved = showunconserved;
374 * @param showNonconserved
375 * the showUnconserved to set
377 public void setShowunconserved(boolean displayNonconserved)
379 this.showUnconserved = displayNonconserved;
385 * @return null or the currently selected sequence region
387 public SequenceGroup getSelectionGroup()
389 return selectionGroup;
393 * Set the selection group for this window.
396 * - group holding references to sequences in this alignment view
399 public void setSelectionGroup(SequenceGroup sg)
404 public void setHiddenColumns(ColumnSelection colsel)
406 this.colSel = colsel;
407 if (colSel.getHiddenColumns() != null)
409 hasHiddenColumns = true;
413 public ColumnSelection getColumnSelection()
417 public void setColumnSelection(ColumnSelection colSel)
421 public Hashtable getHiddenRepSequences()
423 return hiddenRepSequences;
425 public void setHiddenRepSequences(Hashtable hiddenRepSequences)
427 this.hiddenRepSequences = hiddenRepSequences;
429 protected boolean hasHiddenColumns = false;
431 public void updateHiddenColumns()
433 hasHiddenColumns = colSel.getHiddenColumns() != null;
436 protected boolean hasHiddenRows = false;
438 public boolean hasHiddenRows() {
439 return hasHiddenRows;
442 protected SequenceGroup selectionGroup;
444 public void setSequenceSetId(String newid)
446 if (sequenceSetID!=null)
448 System.err.println("Warning - overwriting a sequenceSetId for a viewport!");
450 sequenceSetID=new String(newid);
452 public String getSequenceSetId()
454 if (sequenceSetID == null)
456 sequenceSetID = alignment.hashCode() + "";
459 return sequenceSetID;
462 * unique viewId for synchronizing state (e.g. with stored Jalview Project)
465 protected String viewId = null;
467 public String getViewId()
471 viewId = this.getSequenceSetId() + "." + this.hashCode() + "";
475 public void setIgnoreGapsConsensus(boolean b, AlignmentViewPanel ap)
477 ignoreGapsInConsensusCalculation = b;
478 if (ap!=null) {updateConsensus(ap);
479 if (globalColourScheme != null)
481 globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
482 ignoreGapsInConsensusCalculation);
486 private long sgrouphash = -1, colselhash = -1;
489 * checks current SelectionGroup against record of last hash value, and
493 * update the record of last hash value
495 * @return true if SelectionGroup changed since last call (when b is true)
497 public boolean isSelectionGroupChanged(boolean b)
499 int hc = (selectionGroup == null || selectionGroup.getSize() == 0) ? -1
500 : selectionGroup.hashCode();
501 if (hc != -1 && hc != sgrouphash)
513 * checks current colsel against record of last hash value, and optionally
517 * update the record of last hash value
518 * @return true if colsel changed since last call (when b is true)
520 public boolean isColSelChanged(boolean b)
522 int hc = (colSel == null || colSel.size() == 0) ? -1 : colSel
524 if (hc != -1 && hc != colselhash)
535 public boolean getIgnoreGapsConsensus()
537 return ignoreGapsInConsensusCalculation;
540 /// property change stuff
542 // JBPNote Prolly only need this in the applet version.
543 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
548 * Property change listener for changes in alignment
553 public void addPropertyChangeListener(
554 java.beans.PropertyChangeListener listener)
556 changeSupport.addPropertyChangeListener(listener);
565 public void removePropertyChangeListener(
566 java.beans.PropertyChangeListener listener)
568 changeSupport.removePropertyChangeListener(listener);
572 * Property change listener for changes in alignment
581 public void firePropertyChange(String prop, Object oldvalue,
584 changeSupport.firePropertyChange(prop, oldvalue, newvalue);
587 // common hide/show column stuff
590 public void hideSelectedColumns()
592 if (colSel.size() < 1)
597 colSel.hideSelectedColumns();
598 setSelectionGroup(null);
600 hasHiddenColumns = true;
603 public void hideColumns(int start, int end)
607 colSel.hideColumns(start);
611 colSel.hideColumns(start, end);
614 hasHiddenColumns = true;
617 public void showColumn(int col)
619 colSel.revealHiddenColumns(col);
620 if (colSel.getHiddenColumns() == null)
622 hasHiddenColumns = false;
626 public void showAllHiddenColumns()
628 colSel.revealAllHiddenColumns();
629 hasHiddenColumns = false;
633 // common hide/show seq stuff
634 public void showAllHiddenSeqs()
636 if (alignment.getHiddenSequences().getSize() > 0)
638 if (selectionGroup == null)
640 selectionGroup = new SequenceGroup();
641 selectionGroup.setEndRes(alignment.getWidth() - 1);
643 Vector tmp = alignment.getHiddenSequences().showAll(
645 for (int t = 0; t < tmp.size(); t++)
647 selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
650 hasHiddenRows = false;
651 hiddenRepSequences = null;
653 firePropertyChange("alignment", null, alignment.getSequences());
654 // used to set hasHiddenRows/hiddenRepSequences here, after the property changed event
659 public void showSequence(int index)
661 Vector tmp = alignment.getHiddenSequences().showSequence(index,
665 if (selectionGroup == null)
667 selectionGroup = new SequenceGroup();
668 selectionGroup.setEndRes(alignment.getWidth() - 1);
671 for (int t = 0; t < tmp.size(); t++)
673 selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
675 // JBPNote: refactor: only update flag if we modified visiblity (used to do this regardless)
676 if (alignment.getHiddenSequences().getSize() < 1)
678 hasHiddenRows = false;
680 firePropertyChange("alignment", null, alignment.getSequences());
687 public void hideAllSelectedSeqs()
689 if (selectionGroup == null || selectionGroup.getSize() < 1)
694 SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
698 setSelectionGroup(null);
702 public void hideSequence(SequenceI[] seq)
706 for (int i = 0; i < seq.length; i++)
708 alignment.getHiddenSequences().hideSequence(seq[i]);
710 hasHiddenRows = true;
711 firePropertyChange("alignment", null, alignment.getSequences());
715 public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
717 int sSize = sg.getSize();
723 if (hiddenRepSequences == null)
725 hiddenRepSequences = new Hashtable();
728 hiddenRepSequences.put(repSequence, sg);
730 // Hide all sequences except the repSequence
731 SequenceI[] seqs = new SequenceI[sSize - 1];
733 for (int i = 0; i < sSize; i++)
735 if (sg.getSequenceAt(i) != repSequence)
737 if (index == sSize - 1)
742 seqs[index++] = sg.getSequenceAt(i);
745 sg.setSeqrep(repSequence); // note: not done in 2.7applet
746 sg.setHidereps(true); // note: not done in 2.7applet
751 public boolean isHiddenRepSequence(SequenceI seq)
753 return hiddenRepSequences != null
754 && hiddenRepSequences.containsKey(seq);
756 public SequenceGroup getRepresentedSequences(SequenceI seq)
758 return (SequenceGroup) (hiddenRepSequences == null ? null : hiddenRepSequences.get(seq));
761 public int adjustForHiddenSeqs(int alignmentIndex)
763 return alignment.getHiddenSequences().adjustForHiddenSeqs(
767 // Selection manipulation
769 * broadcast selection to any interested parties
771 public abstract void sendSelection();
774 public void invertColumnSelection()
776 colSel.invertColumnSelection(0, alignment.getWidth());
781 * This method returns an array of new SequenceI objects derived from the
782 * whole alignment or just the current selection with start and end points
785 * @note if you need references to the actual SequenceI objects in the
786 * alignment or currently selected then use getSequenceSelection()
787 * @return selection as new sequenceI objects
789 public SequenceI[] getSelectionAsNewSequence()
791 SequenceI[] sequences;
792 // JBPNote: Need to test jalviewLite.getSelectedSequencesAsAlignmentFrom - this was the only caller in the applet for this method
793 // 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!)
794 if (selectionGroup == null)
796 sequences = alignment.getSequencesArray();
797 AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
798 for (int i = 0; i < sequences.length; i++)
800 sequences[i] = new Sequence(sequences[i], annots); // construct new
808 sequences = selectionGroup.getSelectionAsNewSequences(alignment);
816 * get the currently selected sequence objects or all the sequences in the
819 * @return array of references to sequence objects
821 public SequenceI[] getSequenceSelection()
823 SequenceI[] sequences = null;
824 if (selectionGroup != null)
826 sequences = selectionGroup.getSequencesInOrder(alignment);
828 if (sequences == null)
830 sequences = alignment.getSequencesArray();
837 * This method returns the visible alignment as text, as seen on the GUI, ie
838 * if columns are hidden they will not be returned in the result. Use this for
839 * calculating trees, PCA, redundancy etc on views which contain hidden
844 public jalview.datamodel.CigarArray getViewAsCigars(
845 boolean selectedRegionOnly)
847 return new jalview.datamodel.CigarArray(alignment,
848 (hasHiddenColumns ? colSel : null),
849 (selectedRegionOnly ? selectionGroup : null));
853 * return a compact representation of the current alignment selection to pass
854 * to an analysis function
856 * @param selectedOnly
857 * boolean true to just return the selected view
858 * @return AlignmentView
860 public jalview.datamodel.AlignmentView getAlignmentView(
861 boolean selectedOnly)
863 return getAlignmentView(selectedOnly, false);
867 * return a compact representation of the current alignment selection to pass
868 * to an analysis function
870 * @param selectedOnly
871 * boolean true to just return the selected view
873 * boolean true to annotate the alignment view with groups on the
874 * alignment (and intersecting with selected region if selectedOnly
876 * @return AlignmentView
878 public jalview.datamodel.AlignmentView getAlignmentView(
879 boolean selectedOnly, boolean markGroups)
881 return new AlignmentView(alignment, colSel, selectionGroup,
882 hasHiddenColumns, selectedOnly, markGroups);
887 * This method returns the visible alignment as text, as seen on the GUI, ie
888 * if columns are hidden they will not be returned in the result. Use this for
889 * calculating trees, PCA, redundancy etc on views which contain hidden
894 public String[] getViewAsString(boolean selectedRegionOnly)
896 String[] selection = null;
897 SequenceI[] seqs = null;
899 int start = 0, end = 0;
900 if (selectedRegionOnly && selectionGroup != null)
902 iSize = selectionGroup.getSize();
903 seqs = selectionGroup.getSequencesInOrder(alignment);
904 start = selectionGroup.getStartRes();
905 end = selectionGroup.getEndRes() + 1;
909 iSize = alignment.getHeight();
910 seqs = alignment.getSequencesArray();
911 end = alignment.getWidth();
914 selection = new String[iSize];
915 if (hasHiddenColumns)
917 selection = colSel.getVisibleSequenceStrings(start, end, seqs);
921 for (i = 0; i < iSize; i++)
923 selection[i] = seqs[i].getSequenceAsString(start, end);
932 * return visible region boundaries within given column range
933 * @param min first column (inclusive, from 0)
934 * @param max last column (exclusive)
935 * @return int[][] range of {start,end} visible positions
937 public int[][] getVisibleRegionBoundaries(int min, int max)
939 Vector regions = new Vector();
945 if (hasHiddenColumns)
949 start = colSel.adjustForHiddenColumns(start);
952 end = colSel.getHiddenBoundaryRight(start);
963 regions.addElement(new int[]
966 if (hasHiddenColumns)
968 start = colSel.adjustForHiddenColumns(end);
969 start = colSel.getHiddenBoundaryLeft(start) + 1;
973 int[][] startEnd = new int[regions.size()][2];
975 regions.copyInto(startEnd);
981 * @return the padGaps
983 public boolean isPadGaps()
988 * @param padGaps the padGaps to set
990 public void setPadGaps(boolean padGaps)
992 this.padGaps = padGaps;
995 * apply any post-edit constraints and trigger any calculations needed after an edit has been performed on the alignment
998 public void alignmentChanged(AlignmentViewPanel ap)
1002 alignment.padGaps();
1004 if (autoCalculateConsensus)
1006 updateConsensus(ap);
1008 if (hconsensus != null && autoCalculateConsensus)
1010 updateConservation(ap);
1012 if (autoCalculateStrucConsensus)
1014 updateStrucConsensus(ap);
1017 // Reset endRes of groups if beyond alignment width
1018 int alWidth = alignment.getWidth();
1019 Vector groups = alignment.getGroups();
1022 for (int i = 0; i < groups.size(); i++)
1024 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
1025 if (sg.getEndRes() > alWidth)
1027 sg.setEndRes(alWidth - 1);
1032 if (selectionGroup != null && selectionGroup.getEndRes() > alWidth)
1034 selectionGroup.setEndRes(alWidth - 1);
1037 resetAllColourSchemes();
1039 // alignment.adjustSequenceAnnotations();
1044 * reset scope and do calculations for all applied colourschemes on alignment
1046 void resetAllColourSchemes()
1048 ColourSchemeI cs = globalColourScheme;
1051 if (cs instanceof ClustalxColourScheme)
1053 ((ClustalxColourScheme) cs).resetClustalX(alignment.getSequences(),
1054 alignment.getWidth());
1057 cs.setConsensus(hconsensus);
1058 if (cs.conservationApplied())
1060 cs.setConservation(Conservation.calculateConservation("All",
1061 ResidueProperties.propHash, 3, alignment.getSequences(), 0,
1062 alignment.getWidth(), false, getConsPercGaps(), false));
1066 int s, sSize = alignment.getGroups().size();
1067 for (s = 0; s < sSize; s++)
1069 SequenceGroup sg = (SequenceGroup) alignment.getGroups().elementAt(s);
1070 if (sg.cs != null && sg.cs instanceof ClustalxColourScheme)
1072 ((ClustalxColourScheme) sg.cs).resetClustalX(sg
1073 .getSequences(hiddenRepSequences), sg.getWidth());
1075 sg.recalcConservation();