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.Annotation;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.schemes.ClustalxColourScheme;
33 import jalview.schemes.ColourSchemeI;
34 import jalview.schemes.ResidueProperties;
35 import jalview.workers.AlignCalcManager;
36 import jalview.workers.ConsensusThread;
37 import jalview.workers.ConservationThread;
38 import jalview.workers.StrucConsensusThread;
40 import java.util.Hashtable;
41 import java.util.Vector;
44 * base class holding visualization and analysis attributes and common logic for an active alignment view displayed in the GUI
48 public abstract class AlignmentViewport implements AlignViewportI
51 * alignment displayed in the viewport. Please use get/setter
53 protected AlignmentI alignment;
55 protected String sequenceSetID;
58 * probably unused indicator that view is of a dataset rather than an alignment
60 protected boolean isDataset = false;
62 private Hashtable hiddenRepSequences;
64 protected ColumnSelection colSel = new ColumnSelection();
67 public boolean autoCalculateConsensus = true;
69 protected boolean autoCalculateStrucConsensus = true;
71 protected boolean ignoreGapsInConsensusCalculation = false;
74 protected ColourSchemeI globalColourScheme = null;
77 public void setGlobalColourScheme(ColourSchemeI cs)
79 globalColourScheme = cs;
82 public ColourSchemeI getGlobalColourScheme()
84 return globalColourScheme;
88 protected AlignmentAnnotation consensus;
90 protected AlignmentAnnotation strucConsensus;
92 protected AlignmentAnnotation conservation;
94 protected AlignmentAnnotation quality;
96 protected AlignmentAnnotation[] groupConsensus;
98 protected AlignmentAnnotation[] groupConservation;
101 * results of alignment consensus analysis for visible portion of view
103 protected Hashtable[] hconsensus=null;
106 * results of secondary structure base pair consensus for visible portion of view
108 protected Hashtable[] hStrucConsensus=null;
111 * percentage gaps allowed in a column before all amino acid properties should be considered unconserved
113 int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
116 public int getConsPercGaps()
121 public void setSequenceConsensusHash(Hashtable[] hconsensus)
123 this.hconsensus=hconsensus;
128 public Hashtable[] getSequenceConsensusHash()
134 public Hashtable[] getRnaStructureConsensusHash()
136 return hStrucConsensus;
139 public void setRnaStructureConsensusHash(Hashtable[] hStrucConsensus)
141 this.hStrucConsensus=hStrucConsensus;
145 public AlignmentAnnotation getAlignmentQualityAnnot()
151 public AlignmentAnnotation getAlignmentConservationAnnotation()
156 public AlignmentAnnotation getAlignmentConsensusAnnotation()
161 public AlignmentAnnotation getAlignmentStrucConsensusAnnotation()
163 return strucConsensus;
166 protected AlignCalcManagerI calculator=new AlignCalcManager();
168 jalview.workers.ConsensusThread consensusThread;
170 StrucConsensusThread strucConsensusThread;
173 private ConservationThread conservationThread;
176 * trigger update of conservation annotation
178 public void updateConservation(final AlignmentViewPanel ap)
180 // see note in mantis : issue number 8585
181 if (alignment.isNucleotide() || conservation == null
182 || !autoCalculateConsensus)
187 calculator.startWorker(conservationThread=new jalview.workers.ConservationThread(this, ap));
191 * trigger update of consensus annotation
193 public void updateConsensus(final AlignmentViewPanel ap)
195 // see note in mantis : issue number 8585
196 if (consensus == null || !autoCalculateConsensus)
200 calculator.startWorker(consensusThread = new ConsensusThread(this, ap));
203 // --------START Structure Conservation
204 public void updateStrucConsensus(final AlignmentViewPanel ap)
206 if (autoCalculateStrucConsensus && strucConsensus==null && alignment.isNucleotide() && alignment.hasRNAStructure())
211 // see note in mantis : issue number 8585
212 if (strucConsensus == null || !autoCalculateStrucConsensus)
216 calculator.startWorker(strucConsensusThread = new StrucConsensusThread(this,ap));
219 public boolean isCalcInProgress()
221 return calculator.isWorking();
224 public boolean isCalculationInProgress(
225 AlignmentAnnotation alignmentAnnotation)
227 if (!alignmentAnnotation.autoCalculated)
229 if ((calculator.isWorking(consensusThread) && consensus==alignmentAnnotation)
230 || (calculator.isWorking(conservationThread) && (conservation==alignmentAnnotation || quality==alignmentAnnotation))
231 || (calculator.isWorking(strucConsensusThread) && strucConsensus==alignmentAnnotation)
239 public boolean isClosed()
241 // TODO: check that this isClosed is only true after panel is closed, not before it is fully constructed.
242 return alignment==null;
246 public AlignCalcManagerI getCalcManager()
252 * should conservation rows be shown for groups
254 protected boolean showGroupConservation = false;
257 * should consensus rows be shown for groups
259 protected boolean showGroupConsensus = false;
262 * should consensus profile be rendered by default
264 protected boolean showSequenceLogo = false;
266 * should consensus profile be rendered normalised to row height
268 protected boolean normaliseSequenceLogo = false;
270 * should consensus histograms be rendered by default
272 protected boolean showConsensusHistogram = true;
275 * @return the showConsensusProfile
277 public boolean isShowSequenceLogo()
279 return showSequenceLogo;
283 * @param showSequenceLogo
286 public void setShowSequenceLogo(boolean showSequenceLogo)
288 if (showSequenceLogo != this.showSequenceLogo)
290 // TODO: decouple settings setting from calculation when refactoring
291 // annotation update method from alignframe to viewport
292 this.showSequenceLogo = showSequenceLogo;
293 if (consensusThread != null)
295 consensusThread.updateAnnotation();
297 if (strucConsensusThread != null)
299 strucConsensusThread.updateAnnotation();
302 this.showSequenceLogo = showSequenceLogo;
306 * @param showConsensusHistogram
307 * the showConsensusHistogram to set
309 public void setShowConsensusHistogram(boolean showConsensusHistogram)
311 this.showConsensusHistogram = showConsensusHistogram;
315 * @return the showGroupConservation
317 public boolean isShowGroupConservation()
319 return showGroupConservation;
323 * @param showGroupConservation
324 * the showGroupConservation to set
326 public void setShowGroupConservation(boolean showGroupConservation)
328 this.showGroupConservation = showGroupConservation;
332 * @return the showGroupConsensus
334 public boolean isShowGroupConsensus()
336 return showGroupConsensus;
340 * @param showGroupConsensus
341 * the showGroupConsensus to set
343 public void setShowGroupConsensus(boolean showGroupConsensus)
345 this.showGroupConsensus = showGroupConsensus;
350 * @return flag to indicate if the consensus histogram should be rendered by
353 public boolean isShowConsensusHistogram()
355 return this.showConsensusHistogram;
359 * show non-conserved residues only
361 protected boolean showUnconserved = false;
365 * when set, updateAlignment will always ensure sequences are of equal length
367 private boolean padGaps = false;
370 * when set, alignment should be reordered according to a newly opened tree
372 public boolean sortByTree = false;
374 public boolean getShowUnconserved()
376 return showUnconserved;
379 public void setShowUnconserved(boolean showunconserved)
381 showUnconserved = showunconserved;
385 * @param showNonconserved
386 * the showUnconserved to set
388 public void setShowunconserved(boolean displayNonconserved)
390 this.showUnconserved = displayNonconserved;
396 * @return null or the currently selected sequence region
398 public SequenceGroup getSelectionGroup()
400 return selectionGroup;
404 * Set the selection group for this window.
407 * - group holding references to sequences in this alignment view
410 public void setSelectionGroup(SequenceGroup sg)
415 public void setHiddenColumns(ColumnSelection colsel)
417 this.colSel = colsel;
418 if (colSel.getHiddenColumns() != null)
420 hasHiddenColumns = true;
424 public ColumnSelection getColumnSelection()
428 public void setColumnSelection(ColumnSelection colSel)
432 public Hashtable getHiddenRepSequences()
434 return hiddenRepSequences;
436 public void setHiddenRepSequences(Hashtable hiddenRepSequences)
438 this.hiddenRepSequences = hiddenRepSequences;
440 protected boolean hasHiddenColumns = false;
442 public void updateHiddenColumns()
444 hasHiddenColumns = colSel.getHiddenColumns() != null;
447 protected boolean hasHiddenRows = false;
449 public boolean hasHiddenRows() {
450 return hasHiddenRows;
453 protected SequenceGroup selectionGroup;
455 public void setSequenceSetId(String newid)
457 if (sequenceSetID!=null)
459 System.err.println("Warning - overwriting a sequenceSetId for a viewport!");
461 sequenceSetID=new String(newid);
463 public String getSequenceSetId()
465 if (sequenceSetID == null)
467 sequenceSetID = alignment.hashCode() + "";
470 return sequenceSetID;
473 * unique viewId for synchronizing state (e.g. with stored Jalview Project)
476 protected String viewId = null;
478 public String getViewId()
482 viewId = this.getSequenceSetId() + "." + this.hashCode() + "";
486 public void setIgnoreGapsConsensus(boolean b, AlignmentViewPanel ap)
488 ignoreGapsInConsensusCalculation = b;
489 if (ap!=null) {updateConsensus(ap);
490 if (globalColourScheme != null)
492 globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
493 ignoreGapsInConsensusCalculation);
497 private long sgrouphash = -1, colselhash = -1;
500 * checks current SelectionGroup against record of last hash value, and
504 * update the record of last hash value
506 * @return true if SelectionGroup changed since last call (when b is true)
508 public boolean isSelectionGroupChanged(boolean b)
510 int hc = (selectionGroup == null || selectionGroup.getSize() == 0) ? -1
511 : selectionGroup.hashCode();
512 if (hc != -1 && hc != sgrouphash)
524 * checks current colsel against record of last hash value, and optionally
528 * update the record of last hash value
529 * @return true if colsel changed since last call (when b is true)
531 public boolean isColSelChanged(boolean b)
533 int hc = (colSel == null || colSel.size() == 0) ? -1 : colSel
535 if (hc != -1 && hc != colselhash)
546 public boolean getIgnoreGapsConsensus()
548 return ignoreGapsInConsensusCalculation;
551 /// property change stuff
553 // JBPNote Prolly only need this in the applet version.
554 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
557 protected boolean showConservation = true;
559 protected boolean showQuality = true;
561 protected boolean showConsensus = true;
565 * Property change listener for changes in alignment
570 public void addPropertyChangeListener(
571 java.beans.PropertyChangeListener listener)
573 changeSupport.addPropertyChangeListener(listener);
582 public void removePropertyChangeListener(
583 java.beans.PropertyChangeListener listener)
585 changeSupport.removePropertyChangeListener(listener);
589 * Property change listener for changes in alignment
598 public void firePropertyChange(String prop, Object oldvalue,
601 changeSupport.firePropertyChange(prop, oldvalue, newvalue);
604 // common hide/show column stuff
607 public void hideSelectedColumns()
609 if (colSel.size() < 1)
614 colSel.hideSelectedColumns();
615 setSelectionGroup(null);
617 hasHiddenColumns = true;
620 public void hideColumns(int start, int end)
624 colSel.hideColumns(start);
628 colSel.hideColumns(start, end);
631 hasHiddenColumns = true;
634 public void showColumn(int col)
636 colSel.revealHiddenColumns(col);
637 if (colSel.getHiddenColumns() == null)
639 hasHiddenColumns = false;
643 public void showAllHiddenColumns()
645 colSel.revealAllHiddenColumns();
646 hasHiddenColumns = false;
650 // common hide/show seq stuff
651 public void showAllHiddenSeqs()
653 if (alignment.getHiddenSequences().getSize() > 0)
655 if (selectionGroup == null)
657 selectionGroup = new SequenceGroup();
658 selectionGroup.setEndRes(alignment.getWidth() - 1);
660 Vector tmp = alignment.getHiddenSequences().showAll(
662 for (int t = 0; t < tmp.size(); t++)
664 selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
667 hasHiddenRows = false;
668 hiddenRepSequences = null;
670 firePropertyChange("alignment", null, alignment.getSequences());
671 // used to set hasHiddenRows/hiddenRepSequences here, after the property changed event
676 public void showSequence(int index)
678 Vector tmp = alignment.getHiddenSequences().showSequence(index,
682 if (selectionGroup == null)
684 selectionGroup = new SequenceGroup();
685 selectionGroup.setEndRes(alignment.getWidth() - 1);
688 for (int t = 0; t < tmp.size(); t++)
690 selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
692 // JBPNote: refactor: only update flag if we modified visiblity (used to do this regardless)
693 if (alignment.getHiddenSequences().getSize() < 1)
695 hasHiddenRows = false;
697 firePropertyChange("alignment", null, alignment.getSequences());
704 public void hideAllSelectedSeqs()
706 if (selectionGroup == null || selectionGroup.getSize() < 1)
711 SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
715 setSelectionGroup(null);
719 public void hideSequence(SequenceI[] seq)
723 for (int i = 0; i < seq.length; i++)
725 alignment.getHiddenSequences().hideSequence(seq[i]);
727 hasHiddenRows = true;
728 firePropertyChange("alignment", null, alignment.getSequences());
732 public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
734 int sSize = sg.getSize();
740 if (hiddenRepSequences == null)
742 hiddenRepSequences = new Hashtable();
745 hiddenRepSequences.put(repSequence, sg);
747 // Hide all sequences except the repSequence
748 SequenceI[] seqs = new SequenceI[sSize - 1];
750 for (int i = 0; i < sSize; i++)
752 if (sg.getSequenceAt(i) != repSequence)
754 if (index == sSize - 1)
759 seqs[index++] = sg.getSequenceAt(i);
762 sg.setSeqrep(repSequence); // note: not done in 2.7applet
763 sg.setHidereps(true); // note: not done in 2.7applet
768 public boolean isHiddenRepSequence(SequenceI seq)
770 return hiddenRepSequences != null
771 && hiddenRepSequences.containsKey(seq);
773 public SequenceGroup getRepresentedSequences(SequenceI seq)
775 return (SequenceGroup) (hiddenRepSequences == null ? null : hiddenRepSequences.get(seq));
778 public int adjustForHiddenSeqs(int alignmentIndex)
780 return alignment.getHiddenSequences().adjustForHiddenSeqs(
784 // Selection manipulation
786 * broadcast selection to any interested parties
788 public abstract void sendSelection();
791 public void invertColumnSelection()
793 colSel.invertColumnSelection(0, alignment.getWidth());
798 * This method returns an array of new SequenceI objects derived from the
799 * whole alignment or just the current selection with start and end points
802 * @note if you need references to the actual SequenceI objects in the
803 * alignment or currently selected then use getSequenceSelection()
804 * @return selection as new sequenceI objects
806 public SequenceI[] getSelectionAsNewSequence()
808 SequenceI[] sequences;
809 // JBPNote: Need to test jalviewLite.getSelectedSequencesAsAlignmentFrom - this was the only caller in the applet for this method
810 // 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!)
811 if (selectionGroup == null)
813 sequences = alignment.getSequencesArray();
814 AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
815 for (int i = 0; i < sequences.length; i++)
817 sequences[i] = new Sequence(sequences[i], annots); // construct new
825 sequences = selectionGroup.getSelectionAsNewSequences(alignment);
833 * get the currently selected sequence objects or all the sequences in the
836 * @return array of references to sequence objects
838 public SequenceI[] getSequenceSelection()
840 SequenceI[] sequences = null;
841 if (selectionGroup != null)
843 sequences = selectionGroup.getSequencesInOrder(alignment);
845 if (sequences == null)
847 sequences = alignment.getSequencesArray();
854 * This method returns the visible alignment as text, as seen on the GUI, ie
855 * if columns are hidden they will not be returned in the result. Use this for
856 * calculating trees, PCA, redundancy etc on views which contain hidden
861 public jalview.datamodel.CigarArray getViewAsCigars(
862 boolean selectedRegionOnly)
864 return new jalview.datamodel.CigarArray(alignment,
865 (hasHiddenColumns ? colSel : null),
866 (selectedRegionOnly ? selectionGroup : null));
870 * return a compact representation of the current alignment selection to pass
871 * to an analysis function
873 * @param selectedOnly
874 * boolean true to just return the selected view
875 * @return AlignmentView
877 public jalview.datamodel.AlignmentView getAlignmentView(
878 boolean selectedOnly)
880 return getAlignmentView(selectedOnly, false);
884 * return a compact representation of the current alignment selection to pass
885 * to an analysis function
887 * @param selectedOnly
888 * boolean true to just return the selected view
890 * boolean true to annotate the alignment view with groups on the
891 * alignment (and intersecting with selected region if selectedOnly
893 * @return AlignmentView
895 public jalview.datamodel.AlignmentView getAlignmentView(
896 boolean selectedOnly, boolean markGroups)
898 return new AlignmentView(alignment, colSel, selectionGroup,
899 hasHiddenColumns, selectedOnly, markGroups);
904 * This method returns the visible alignment as text, as seen on the GUI, ie
905 * if columns are hidden they will not be returned in the result. Use this for
906 * calculating trees, PCA, redundancy etc on views which contain hidden
911 public String[] getViewAsString(boolean selectedRegionOnly)
913 String[] selection = null;
914 SequenceI[] seqs = null;
916 int start = 0, end = 0;
917 if (selectedRegionOnly && selectionGroup != null)
919 iSize = selectionGroup.getSize();
920 seqs = selectionGroup.getSequencesInOrder(alignment);
921 start = selectionGroup.getStartRes();
922 end = selectionGroup.getEndRes() + 1;
926 iSize = alignment.getHeight();
927 seqs = alignment.getSequencesArray();
928 end = alignment.getWidth();
931 selection = new String[iSize];
932 if (hasHiddenColumns)
934 selection = colSel.getVisibleSequenceStrings(start, end, seqs);
938 for (i = 0; i < iSize; i++)
940 selection[i] = seqs[i].getSequenceAsString(start, end);
949 * return visible region boundaries within given column range
950 * @param min first column (inclusive, from 0)
951 * @param max last column (exclusive)
952 * @return int[][] range of {start,end} visible positions
954 public int[][] getVisibleRegionBoundaries(int min, int max)
956 Vector regions = new Vector();
962 if (hasHiddenColumns)
966 start = colSel.adjustForHiddenColumns(start);
969 end = colSel.getHiddenBoundaryRight(start);
980 regions.addElement(new int[]
983 if (hasHiddenColumns)
985 start = colSel.adjustForHiddenColumns(end);
986 start = colSel.getHiddenBoundaryLeft(start) + 1;
990 int[][] startEnd = new int[regions.size()][2];
992 regions.copyInto(startEnd);
998 * @return the padGaps
1000 public boolean isPadGaps()
1005 * @param padGaps the padGaps to set
1007 public void setPadGaps(boolean padGaps)
1009 this.padGaps = padGaps;
1012 * apply any post-edit constraints and trigger any calculations needed after an edit has been performed on the alignment
1015 public void alignmentChanged(AlignmentViewPanel ap)
1019 alignment.padGaps();
1021 if (autoCalculateConsensus)
1023 updateConsensus(ap);
1025 if (hconsensus != null && autoCalculateConsensus)
1027 updateConservation(ap);
1029 if (autoCalculateStrucConsensus)
1031 updateStrucConsensus(ap);
1034 // Reset endRes of groups if beyond alignment width
1035 int alWidth = alignment.getWidth();
1036 Vector groups = alignment.getGroups();
1039 for (int i = 0; i < groups.size(); i++)
1041 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
1042 if (sg.getEndRes() > alWidth)
1044 sg.setEndRes(alWidth - 1);
1049 if (selectionGroup != null && selectionGroup.getEndRes() > alWidth)
1051 selectionGroup.setEndRes(alWidth - 1);
1054 resetAllColourSchemes();
1056 // alignment.adjustSequenceAnnotations();
1061 * reset scope and do calculations for all applied colourschemes on alignment
1063 void resetAllColourSchemes()
1065 ColourSchemeI cs = globalColourScheme;
1068 if (cs instanceof ClustalxColourScheme)
1070 ((ClustalxColourScheme) cs).resetClustalX(alignment.getSequences(),
1071 alignment.getWidth());
1074 cs.setConsensus(hconsensus);
1075 if (cs.conservationApplied())
1077 cs.setConservation(Conservation.calculateConservation("All",
1078 ResidueProperties.propHash, 3, alignment.getSequences(), 0,
1079 alignment.getWidth(), false, getConsPercGaps(), false));
1083 int s, sSize = alignment.getGroups().size();
1084 for (s = 0; s < sSize; s++)
1086 SequenceGroup sg = (SequenceGroup) alignment.getGroups().elementAt(s);
1087 if (sg.cs != null && sg.cs instanceof ClustalxColourScheme)
1089 ((ClustalxColourScheme) sg.cs).resetClustalX(sg
1090 .getSequences(hiddenRepSequences), sg.getWidth());
1092 sg.recalcConservation();
1096 protected void initAutoAnnotation()
1098 // TODO: add menu option action that nulls or creates consensus object
1099 // depending on if the user wants to see the annotation or not in a
1100 // specific alignment
1102 if (hconsensus == null && !isDataset)
1104 if (!alignment.isNucleotide())
1106 if (showConservation)
1108 if (conservation==null)
1110 conservation = new AlignmentAnnotation("Conservation",
1111 "Conservation of total alignment less than " + getConsPercGaps()
1112 + "% gaps", new Annotation[1], 0f, 11f,
1113 AlignmentAnnotation.BAR_GRAPH);
1114 conservation.hasText = true;
1115 conservation.autoCalculated = true;
1116 alignment.addAnnotation(conservation);
1123 quality = new AlignmentAnnotation("Quality",
1124 "Alignment Quality based on Blosum62 scores",
1125 new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
1126 quality.hasText = true;
1127 quality.autoCalculated = true;
1128 alignment.addAnnotation(quality);
1132 if (alignment.hasRNAStructure())
1134 strucConsensus = new AlignmentAnnotation("StrucConsensus", "PID",
1135 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
1136 strucConsensus.hasText = true;
1137 strucConsensus.autoCalculated = true;
1141 consensus = new AlignmentAnnotation("Consensus", "PID",
1142 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
1143 consensus.hasText = true;
1144 consensus.autoCalculated = true;
1148 alignment.addAnnotation(consensus);
1149 if (strucConsensus!=null)
1151 alignment.addAnnotation(strucConsensus);