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.api.ViewStyleI;
45 import jalview.bin.Cache;
46 import jalview.commands.CommandI;
47 import jalview.datamodel.AlignmentI;
48 import jalview.datamodel.ColumnSelection;
49 import jalview.datamodel.PDBEntry;
50 import jalview.datamodel.Sequence;
51 import jalview.datamodel.SequenceGroup;
52 import jalview.datamodel.SequenceI;
53 import jalview.schemes.ColourSchemeProperty;
54 import jalview.schemes.UserColourScheme;
55 import jalview.structure.SelectionSource;
56 import jalview.structure.StructureSelectionManager;
57 import jalview.structure.VamsasSource;
58 import jalview.viewmodel.AlignmentViewport;
59 import jalview.ws.params.AutoCalcSetting;
61 import java.awt.Container;
62 import java.awt.Dimension;
64 import java.awt.Rectangle;
65 import java.util.ArrayList;
66 import java.util.Hashtable;
67 import java.util.Stack;
68 import java.util.Vector;
74 * @version $Revision: 1.141 $
76 public class AlignViewport extends AlignmentViewport implements
77 SelectionSource, VamsasSource, AlignViewportI
88 SequenceAnnotationOrder sortAnnotationsBy = null;
92 NJTree currentTree = null;
94 boolean cursorMode = false;
96 boolean antiAlias = false;
98 Rectangle explodedPosition;
102 boolean gatherViewsHere = false;
104 Stack<CommandI> historyList = new Stack<CommandI>();
106 Stack<CommandI> redoList = new Stack<CommandI>();
108 private AnnotationColumnChooser annotationColumnSelectionState;
110 * Creates a new AlignViewport object.
115 public AlignViewport(AlignmentI al)
122 * Create a new AlignViewport object with a specific sequence set ID
126 * (may be null - but potential for ambiguous constructor exception)
128 public AlignViewport(AlignmentI al, String seqsetid)
130 this(al, seqsetid, null);
133 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
135 sequenceSetID = seqsetid;
137 // TODO remove these once 2.4.VAMSAS release finished
138 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
140 Cache.log.debug("Setting viewport's sequence set id : "
143 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
145 Cache.log.debug("Setting viewport's view id : " + viewId);
152 * Create a new AlignViewport with hidden regions
156 * @param hiddenColumns
159 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
162 if (hiddenColumns != null)
164 colSel = hiddenColumns;
170 * New viewport with hidden columns and an existing sequence set id
173 * @param hiddenColumns
177 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
180 this(al, hiddenColumns, seqsetid, null);
184 * New viewport with hidden columns and an existing sequence set id and viewid
187 * @param hiddenColumns
193 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
194 String seqsetid, String viewid)
196 sequenceSetID = seqsetid;
198 // TODO remove these once 2.4.VAMSAS release finished
199 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
201 Cache.log.debug("Setting viewport's sequence set id : "
204 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
206 Cache.log.debug("Setting viewport's view id : " + viewId);
209 if (hiddenColumns != null)
211 colSel = hiddenColumns;
216 private void applyViewProperties()
218 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
220 viewStyle.setShowJVSuffix(Cache.getDefault("SHOW_JVSUFFIX", true));
221 setShowAnnotation(Cache.getDefault("SHOW_ANNOTATIONS", true));
223 setRightAlignIds(Cache.getDefault("RIGHT_ALIGN_IDS", false));
224 setCentreColumnLabels(Cache.getDefault("CENTRE_COLUMN_LABELS", false));
225 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
227 setPadGaps(Cache.getDefault("PAD_GAPS", true));
228 setShowNPFeats(Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true));
229 setShowDBRefs(Cache.getDefault("SHOW_DBREFS_TOOLTIP", true));
230 viewStyle.setSeqNameItalics(Cache.getDefault("ID_ITALICS", true));
231 viewStyle.setWrapAlignment(Cache.getDefault("WRAP_ALIGNMENT", false));
232 viewStyle.setShowUnconserved(Cache
233 .getDefault("SHOW_UNCONSERVED", false));
234 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
235 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
236 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
237 Preferences.SORT_ANNOTATIONS,
238 SequenceAnnotationOrder.NONE.name()));
239 showAutocalculatedAbove = Cache.getDefault(
240 Preferences.SHOW_AUTOCALC_ABOVE, false);
247 this.endRes = alignment.getWidth() - 1;
249 this.endSeq = alignment.getHeight() - 1;
250 applyViewProperties();
252 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
253 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
254 String fontSize = Cache.getDefault("FONT_SIZE", "10");
258 if (fontStyle.equals("bold"))
262 else if (fontStyle.equals("italic"))
267 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
270 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
272 // We must set conservation and consensus before setting colour,
273 // as Blosum and Clustal require this to be done
274 if (hconsensus == null && !isDataset)
276 if (!alignment.isNucleotide())
278 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
279 showQuality = Cache.getDefault("SHOW_QUALITY", true);
280 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
283 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
285 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
286 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
288 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
289 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
291 initAutoAnnotation();
292 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
294 globalColourScheme = ColourSchemeProperty.getColour(alignment,
295 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
297 if (globalColourScheme instanceof UserColourScheme)
299 globalColourScheme = UserDefinedColours.loadDefaultColours();
300 ((UserColourScheme) globalColourScheme).setThreshold(0,
301 isIgnoreGapsConsensus());
304 if (globalColourScheme != null)
306 globalColourScheme.setConsensus(hconsensus);
312 * get the consensus sequence as displayed under the PID consensus annotation
315 * @return consensus sequence as a new sequence object
317 public SequenceI getConsensusSeq()
319 if (consensus == null)
321 updateConsensus(null);
323 if (consensus == null)
327 StringBuffer seqs = new StringBuffer();
328 for (int i = 0; i < consensus.annotations.length; i++)
330 if (consensus.annotations[i] != null)
332 if (consensus.annotations[i].description.charAt(0) == '[')
334 seqs.append(consensus.annotations[i].description.charAt(1));
338 seqs.append(consensus.annotations[i].displayCharacter);
343 SequenceI sq = new Sequence("Consensus", seqs.toString());
344 sq.setDescription("Percentage Identity Consensus "
345 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
352 * @return DOCUMENT ME!
354 public int getStartRes()
362 * @return DOCUMENT ME!
364 public int getEndRes()
372 * @return DOCUMENT ME!
374 public int getStartSeq()
385 public void setStartRes(int res)
396 public void setStartSeq(int seq)
407 public void setEndRes(int res)
409 if (res > (alignment.getWidth() - 1))
411 // log.System.out.println(" Corrected res from " + res + " to maximum " +
412 // (alignment.getWidth()-1));
413 res = alignment.getWidth() - 1;
430 public void setEndSeq(int seq)
432 if (seq > alignment.getHeight())
434 seq = alignment.getHeight();
448 * @return DOCUMENT ME!
450 public int getEndSeq()
455 boolean validCharWidth;
463 public void setFont(Font f)
467 Container c = new Container();
469 java.awt.FontMetrics fm = c.getFontMetrics(font);
470 int w = viewStyle.getCharWidth(), ww = fm.charWidth('M'), h = viewStyle
472 // only update width/height if the new font won't fit
473 if (h < fm.getHeight())
475 setCharHeight(fm.getHeight());
481 viewStyle.setFontName(font.getName());
482 viewStyle.setFontStyle(font.getStyle());
483 viewStyle.setFontSize(font.getSize());
485 validCharWidth = true;
489 public void setViewStyle(ViewStyleI settingsForView)
491 super.setViewStyle(settingsForView);
492 setFont(new Font(viewStyle.getFontName(), viewStyle.getFontStyle(),
493 viewStyle.getFontSize()));
499 * @return DOCUMENT ME!
501 public Font getFont()
512 public void setAlignment(AlignmentI align)
514 if (alignment != null && alignment.getCodonFrames() != null)
516 StructureSelectionManager.getStructureSelectionManager(
517 Desktop.instance).removeMappings(alignment.getCodonFrames());
519 this.alignment = align;
520 if (alignment != null && alignment.getCodonFrames() != null)
522 StructureSelectionManager.getStructureSelectionManager(
523 Desktop.instance).addMappings(alignment.getCodonFrames());
530 * @return DOCUMENT ME!
532 public char getGapCharacter()
534 return getAlignment().getGapCharacter();
543 public void setGapCharacter(char gap)
545 if (getAlignment() != null)
547 getAlignment().setGapCharacter(gap);
554 * @return DOCUMENT ME!
556 public ColumnSelection getColumnSelection()
567 public void setCurrentTree(NJTree tree)
575 * @return DOCUMENT ME!
577 public NJTree getCurrentTree()
583 * returns the visible column regions of the alignment
585 * @param selectedRegionOnly
586 * true to just return the contigs intersecting with the selected
590 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
592 int[] viscontigs = null;
593 int start = 0, end = 0;
594 if (selectedRegionOnly && selectionGroup != null)
596 start = selectionGroup.getStartRes();
597 end = selectionGroup.getEndRes() + 1;
601 end = alignment.getWidth();
603 viscontigs = colSel.getVisibleContigs(start, end);
608 * get hash of undo and redo list for the alignment
610 * @return long[] { historyList.hashCode, redoList.hashCode };
612 public long[] getUndoRedoHash()
615 if (historyList == null || redoList == null)
621 { historyList.hashCode(), this.redoList.hashCode() };
625 * test if a particular set of hashcodes are different to the hashcodes for
626 * the undo and redo list.
629 * the stored set of hashcodes as returned by getUndoRedoHash
630 * @return true if the hashcodes differ (ie the alignment has been edited) or
631 * the stored hashcode array differs in size
633 public boolean isUndoRedoHashModified(long[] undoredo)
635 if (undoredo == null)
639 long[] cstate = getUndoRedoHash();
640 if (cstate.length != undoredo.length)
645 for (int i = 0; i < cstate.length; i++)
647 if (cstate[i] != undoredo[i])
656 * when set, view will scroll to show the highlighted position
658 public boolean followHighlight = true;
661 * @return true if view should scroll to show the highlighted region of a
665 public boolean getFollowHighlight()
667 return followHighlight;
670 public boolean followSelection = true;
673 * @return true if view selection should always follow the selections
674 * broadcast by other selection sources
676 public boolean getFollowSelection()
678 return followSelection;
681 public void sendSelection()
683 jalview.structure.StructureSelectionManager
684 .getStructureSelectionManager(Desktop.instance).sendSelection(
685 new SequenceGroup(getSelectionGroup()),
686 new ColumnSelection(getColumnSelection()), this);
690 * return the alignPanel containing the given viewport. Use this to get the
691 * components currently handling the given viewport.
694 * @return null or an alignPanel guaranteed to have non-null alignFrame
697 public AlignmentPanel getAlignPanel()
699 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
700 .getSequenceSetId());
701 AlignmentPanel ap = null;
702 for (int p = 0; aps != null && p < aps.length; p++)
704 if (aps[p].av == this)
712 public boolean getSortByTree()
717 public void setSortByTree(boolean sort)
723 * synthesize a column selection if none exists so it covers the given
724 * selection group. if wholewidth is false, no column selection is made if the
725 * selection group covers the whole alignment width.
730 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
734 && (sgs = sg.getStartRes()) >= 0
735 && sg.getStartRes() <= (sge = sg.getEndRes())
736 && (colSel == null || colSel.getSelected() == null || colSel
737 .getSelected().size() == 0))
739 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
746 colSel = new ColumnSelection();
748 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
750 colSel.addElement(cspos);
755 public StructureSelectionManager getStructureSelectionManager()
757 return StructureSelectionManager
758 .getStructureSelectionManager(Desktop.instance);
764 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
765 * sequence in the alignment holds a reference to it
767 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
769 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
770 for (PDBEntry pdb : pdbEntries)
772 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
773 for (int i = 0; i < alignment.getHeight(); i++)
775 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
782 for (int p = 0; p < pdbs.size(); p++)
784 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
785 if (p1.getId().equals(pdb.getId()))
787 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
796 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
798 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
801 public boolean isNormaliseSequenceLogo()
803 return normaliseSequenceLogo;
806 public void setNormaliseSequenceLogo(boolean state)
808 normaliseSequenceLogo = state;
813 * @return true if alignment characters should be displayed
815 public boolean isValidCharWidth()
817 return validCharWidth;
820 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
822 private boolean showAutocalculatedAbove;
824 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
826 return calcIdParams.get(calcId);
829 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
832 calcIdParams.put(calcId, settings);
833 // TODO: create a restart list to trigger any calculations that need to be
834 // restarted after load
835 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
838 Cache.log.debug("trigger update for " + calcId);
842 protected SequenceAnnotationOrder getSortAnnotationsBy()
844 return sortAnnotationsBy;
847 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
849 this.sortAnnotationsBy = sortAnnotationsBy;
852 protected boolean isShowAutocalculatedAbove()
854 return showAutocalculatedAbove;
857 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
859 this.showAutocalculatedAbove = showAutocalculatedAbove;
862 public AnnotationColumnChooser getAnnotationColumnSelectionState()
864 return annotationColumnSelectionState;
867 public void setAnnotationColumnSelectionState(
868 AnnotationColumnChooser currentAnnotationColumnSelectionState)
870 this.annotationColumnSelectionState = currentAnnotationColumnSelectionState;
874 public void setIdWidth(int i)
877 AlignmentPanel ap = getAlignPanel();
880 // modify GUI elements to reflect geometry change
881 Dimension idw = getAlignPanel().getIdPanel().getIdCanvas()
884 getAlignPanel().getIdPanel().getIdCanvas().setPreferredSize(idw);