2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 * Jalview - A Sequence Alignment Editor and Viewer
23 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version 2
28 * of the License, or (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
41 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
42 import jalview.analysis.NJTree;
43 import jalview.api.AlignViewportI;
44 import jalview.bin.Cache;
45 import jalview.commands.CommandI;
46 import jalview.datamodel.AlignmentI;
47 import jalview.datamodel.ColumnSelection;
48 import jalview.datamodel.PDBEntry;
49 import jalview.datamodel.Sequence;
50 import jalview.datamodel.SequenceGroup;
51 import jalview.datamodel.SequenceI;
52 import jalview.schemes.ColourSchemeProperty;
53 import jalview.schemes.UserColourScheme;
54 import jalview.structure.SelectionSource;
55 import jalview.structure.StructureSelectionManager;
56 import jalview.structure.VamsasSource;
57 import jalview.viewmodel.AlignmentViewport;
58 import jalview.ws.params.AutoCalcSetting;
60 import java.awt.Container;
62 import java.awt.Rectangle;
63 import java.util.ArrayList;
64 import java.util.Hashtable;
65 import java.util.Stack;
66 import java.util.Vector;
72 * @version $Revision: 1.141 $
74 public class AlignViewport extends AlignmentViewport implements
75 SelectionSource, VamsasSource, AlignViewportI
86 SequenceAnnotationOrder sortAnnotationsBy = null;
90 NJTree currentTree = null;
92 boolean cursorMode = false;
94 boolean antiAlias = false;
96 Rectangle explodedPosition;
100 boolean gatherViewsHere = false;
102 Stack<CommandI> historyList = new Stack<CommandI>();
104 Stack<CommandI> redoList = new Stack<CommandI>();
106 private AnnotationColumnChooser annotationColumnSelectionState;
108 * Creates a new AlignViewport object.
113 public AlignViewport(AlignmentI al)
120 * Create a new AlignViewport object with a specific sequence set ID
124 * (may be null - but potential for ambiguous constructor exception)
126 public AlignViewport(AlignmentI al, String seqsetid)
128 this(al, seqsetid, null);
131 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
133 sequenceSetID = seqsetid;
135 // TODO remove these once 2.4.VAMSAS release finished
136 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
138 Cache.log.debug("Setting viewport's sequence set id : "
141 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
143 Cache.log.debug("Setting viewport's view id : " + viewId);
150 * Create a new AlignViewport with hidden regions
154 * @param hiddenColumns
157 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
160 if (hiddenColumns != null)
162 colSel = hiddenColumns;
168 * New viewport with hidden columns and an existing sequence set id
171 * @param hiddenColumns
175 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
178 this(al, hiddenColumns, seqsetid, null);
182 * New viewport with hidden columns and an existing sequence set id and viewid
185 * @param hiddenColumns
191 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
192 String seqsetid, String viewid)
194 sequenceSetID = seqsetid;
196 // TODO remove these once 2.4.VAMSAS release finished
197 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
199 Cache.log.debug("Setting viewport's sequence set id : "
202 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
204 Cache.log.debug("Setting viewport's view id : " + viewId);
207 if (hiddenColumns != null)
209 colSel = hiddenColumns;
214 private void applyViewProperties()
216 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
218 viewStyle.setShowJVSuffix(Cache.getDefault("SHOW_JVSUFFIX", true));
219 setShowAnnotation(Cache.getDefault("SHOW_ANNOTATIONS", true));
221 setRightAlignIds(Cache.getDefault("RIGHT_ALIGN_IDS", false));
222 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
223 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
225 setPadGaps(Cache.getDefault("PAD_GAPS", true));
226 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
227 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
228 viewStyle.setSeqNameItalics(Cache.getDefault("ID_ITALICS", true));
229 viewStyle.setWrapAlignment(Cache.getDefault("WRAP_ALIGNMENT", false));
230 viewStyle.setShowUnconserved(Cache
231 .getDefault("SHOW_UNCONSERVED", false));
232 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
233 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
234 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
235 Preferences.SORT_ANNOTATIONS,
236 SequenceAnnotationOrder.NONE.name()));
237 showAutocalculatedAbove = Cache.getDefault(
238 Preferences.SHOW_AUTOCALC_ABOVE, false);
245 this.endRes = alignment.getWidth() - 1;
247 this.endSeq = alignment.getHeight() - 1;
248 applyViewProperties();
250 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
251 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
252 String fontSize = Cache.getDefault("FONT_SIZE", "10");
256 if (fontStyle.equals("bold"))
260 else if (fontStyle.equals("italic"))
265 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
268 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
270 // We must set conservation and consensus before setting colour,
271 // as Blosum and Clustal require this to be done
272 if (hconsensus == null && !isDataset)
274 if (!alignment.isNucleotide())
276 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
277 showQuality = Cache.getDefault("SHOW_QUALITY", true);
278 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
281 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
283 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
284 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
286 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
287 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
289 initAutoAnnotation();
290 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
292 globalColourScheme = ColourSchemeProperty.getColour(alignment,
293 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
295 if (globalColourScheme instanceof UserColourScheme)
297 globalColourScheme = UserDefinedColours.loadDefaultColours();
298 ((UserColourScheme) globalColourScheme).setThreshold(0,
299 isIgnoreGapsConsensus());
302 if (globalColourScheme != null)
304 globalColourScheme.setConsensus(hconsensus);
310 * centre columnar annotation labels in displayed alignment annotation TODO:
311 * add to jalviewXML and annotation display settings
313 boolean centreColumnLabels = false;
315 private boolean showdbrefs;
317 private boolean shownpfeats;
319 // --------END Structure Conservation
322 * get the consensus sequence as displayed under the PID consensus annotation
325 * @return consensus sequence as a new sequence object
327 public SequenceI getConsensusSeq()
329 if (consensus == null)
331 updateConsensus(null);
333 if (consensus == null)
337 StringBuffer seqs = new StringBuffer();
338 for (int i = 0; i < consensus.annotations.length; i++)
340 if (consensus.annotations[i] != null)
342 if (consensus.annotations[i].description.charAt(0) == '[')
344 seqs.append(consensus.annotations[i].description.charAt(1));
348 seqs.append(consensus.annotations[i].displayCharacter);
353 SequenceI sq = new Sequence("Consensus", seqs.toString());
354 sq.setDescription("Percentage Identity Consensus "
355 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
362 * @return DOCUMENT ME!
364 public int getStartRes()
372 * @return DOCUMENT ME!
374 public int getEndRes()
382 * @return DOCUMENT ME!
384 public int getStartSeq()
395 public void setStartRes(int res)
406 public void setStartSeq(int seq)
417 public void setEndRes(int res)
419 if (res > (alignment.getWidth() - 1))
421 // log.System.out.println(" Corrected res from " + res + " to maximum " +
422 // (alignment.getWidth()-1));
423 res = alignment.getWidth() - 1;
440 public void setEndSeq(int seq)
442 if (seq > alignment.getHeight())
444 seq = alignment.getHeight();
458 * @return DOCUMENT ME!
460 public int getEndSeq()
465 boolean validCharWidth;
473 public void setFont(Font f)
477 Container c = new Container();
479 java.awt.FontMetrics fm = c.getFontMetrics(font);
480 setCharHeight(fm.getHeight());
481 setCharWidth(fm.charWidth('M'));
482 validCharWidth = true;
488 * @return DOCUMENT ME!
490 public Font getFont()
501 public void setAlignment(AlignmentI align)
503 if (alignment != null && alignment.getCodonFrames() != null)
505 StructureSelectionManager.getStructureSelectionManager(
506 Desktop.instance).removeMappings(alignment.getCodonFrames());
508 this.alignment = align;
509 if (alignment != null && alignment.getCodonFrames() != null)
511 StructureSelectionManager.getStructureSelectionManager(
512 Desktop.instance).addMappings(alignment.getCodonFrames());
519 * @return DOCUMENT ME!
521 public char getGapCharacter()
523 return getAlignment().getGapCharacter();
532 public void setGapCharacter(char gap)
534 if (getAlignment() != null)
536 getAlignment().setGapCharacter(gap);
543 * @return DOCUMENT ME!
545 public ColumnSelection getColumnSelection()
556 public void setCurrentTree(NJTree tree)
564 * @return DOCUMENT ME!
566 public NJTree getCurrentTree()
572 * returns the visible column regions of the alignment
574 * @param selectedRegionOnly
575 * true to just return the contigs intersecting with the selected
579 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
581 int[] viscontigs = null;
582 int start = 0, end = 0;
583 if (selectedRegionOnly && selectionGroup != null)
585 start = selectionGroup.getStartRes();
586 end = selectionGroup.getEndRes() + 1;
590 end = alignment.getWidth();
592 viscontigs = colSel.getVisibleContigs(start, end);
597 * get hash of undo and redo list for the alignment
599 * @return long[] { historyList.hashCode, redoList.hashCode };
601 public long[] getUndoRedoHash()
604 if (historyList == null || redoList == null)
610 { historyList.hashCode(), this.redoList.hashCode() };
614 * test if a particular set of hashcodes are different to the hashcodes for
615 * the undo and redo list.
618 * the stored set of hashcodes as returned by getUndoRedoHash
619 * @return true if the hashcodes differ (ie the alignment has been edited) or
620 * the stored hashcode array differs in size
622 public boolean isUndoRedoHashModified(long[] undoredo)
624 if (undoredo == null)
628 long[] cstate = getUndoRedoHash();
629 if (cstate.length != undoredo.length)
634 for (int i = 0; i < cstate.length; i++)
636 if (cstate[i] != undoredo[i])
644 public boolean getCentreColumnLabels()
646 return centreColumnLabels;
649 public void setCentreColumnLabels(boolean centrecolumnlabels)
651 centreColumnLabels = centrecolumnlabels;
655 * enable or disable the display of Database Cross References in the sequence
658 public void setShowDbRefs(boolean show)
665 * @return true if Database References are to be displayed on tooltips.
667 public boolean isShowDbRefs()
674 * @return true if Non-positional features are to be displayed on tooltips.
676 public boolean isShowNpFeats()
682 * enable or disable the display of Non-Positional sequence features in the
683 * sequence ID tooltip
687 public void setShowNpFeats(boolean show)
694 * when set, view will scroll to show the highlighted position
696 public boolean followHighlight = true;
699 * @return true if view should scroll to show the highlighted region of a
703 public boolean getFollowHighlight()
705 return followHighlight;
708 public boolean followSelection = true;
711 * @return true if view selection should always follow the selections
712 * broadcast by other selection sources
714 public boolean getFollowSelection()
716 return followSelection;
719 public void sendSelection()
721 jalview.structure.StructureSelectionManager
722 .getStructureSelectionManager(Desktop.instance).sendSelection(
723 new SequenceGroup(getSelectionGroup()),
724 new ColumnSelection(getColumnSelection()), this);
728 * return the alignPanel containing the given viewport. Use this to get the
729 * components currently handling the given viewport.
732 * @return null or an alignPanel guaranteed to have non-null alignFrame
735 public AlignmentPanel getAlignPanel()
737 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
738 .getSequenceSetId());
739 AlignmentPanel ap = null;
740 for (int p = 0; aps != null && p < aps.length; p++)
742 if (aps[p].av == this)
750 public boolean getSortByTree()
755 public void setSortByTree(boolean sort)
761 * synthesize a column selection if none exists so it covers the given
762 * selection group. if wholewidth is false, no column selection is made if the
763 * selection group covers the whole alignment width.
768 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
772 && (sgs = sg.getStartRes()) >= 0
773 && sg.getStartRes() <= (sge = sg.getEndRes())
774 && (colSel == null || colSel.getSelected() == null || colSel
775 .getSelected().size() == 0))
777 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
784 colSel = new ColumnSelection();
786 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
788 colSel.addElement(cspos);
793 public StructureSelectionManager getStructureSelectionManager()
795 return StructureSelectionManager
796 .getStructureSelectionManager(Desktop.instance);
802 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
803 * sequence in the alignment holds a reference to it
805 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
807 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
808 for (PDBEntry pdb : pdbEntries)
810 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
811 for (int i = 0; i < alignment.getHeight(); i++)
813 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
820 for (int p = 0; p < pdbs.size(); p++)
822 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
823 if (p1.getId().equals(pdb.getId()))
825 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
834 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
836 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
839 public boolean isNormaliseSequenceLogo()
841 return normaliseSequenceLogo;
844 public void setNormaliseSequenceLogo(boolean state)
846 normaliseSequenceLogo = state;
851 * @return true if alignment characters should be displayed
853 public boolean isValidCharWidth()
855 return validCharWidth;
858 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
860 private boolean showAutocalculatedAbove;
862 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
864 return calcIdParams.get(calcId);
867 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
870 calcIdParams.put(calcId, settings);
871 // TODO: create a restart list to trigger any calculations that need to be
872 // restarted after load
873 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
876 Cache.log.debug("trigger update for " + calcId);
880 protected SequenceAnnotationOrder getSortAnnotationsBy()
882 return sortAnnotationsBy;
885 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
887 this.sortAnnotationsBy = sortAnnotationsBy;
890 protected boolean isShowAutocalculatedAbove()
892 return showAutocalculatedAbove;
895 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
897 this.showAutocalculatedAbove = showAutocalculatedAbove;
900 public AnnotationColumnChooser getAnnotationColumnSelectionState()
902 return annotationColumnSelectionState;
905 public void setAnnotationColumnSelectionState(
906 AnnotationColumnChooser currentAnnotationColumnSelectionState)
908 this.annotationColumnSelectionState = currentAnnotationColumnSelectionState;