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.Color;
61 import java.awt.Container;
63 import java.awt.Rectangle;
64 import java.util.ArrayList;
65 import java.util.Hashtable;
66 import java.util.Stack;
67 import java.util.Vector;
73 * @version $Revision: 1.141 $
75 public class AlignViewport extends AlignmentViewport implements
76 SelectionSource, VamsasSource, AlignViewportI
86 boolean showJVSuffix = true;
88 boolean showText = true;
90 boolean showColourText = false;
92 boolean showBoxes = true;
94 boolean wrapAlignment = false;
96 boolean renderGaps = true;
98 SequenceAnnotationOrder sortAnnotationsBy = null;
104 boolean validCharWidth;
110 boolean seqNameItalics;
112 NJTree currentTree = null;
114 boolean scaleAboveWrapped = false;
116 boolean scaleLeftWrapped = true;
118 boolean scaleRightWrapped = true;
120 boolean showHiddenMarkers = true;
122 boolean cursorMode = false;
124 boolean antiAlias = false;
126 Rectangle explodedPosition;
130 boolean gatherViewsHere = false;
132 Stack<CommandI> historyList = new Stack<CommandI>();
134 Stack<CommandI> redoList = new Stack<CommandI>();
136 int thresholdTextColour = 0;
138 Color textColour = Color.black;
140 Color textColour2 = Color.white;
142 private AnnotationColumnChooser annotationColumnSelectionState;
144 * Creates a new AlignViewport object.
149 public AlignViewport(AlignmentI al)
156 * Create a new AlignViewport object with a specific sequence set ID
160 * (may be null - but potential for ambiguous constructor exception)
162 public AlignViewport(AlignmentI al, String seqsetid)
164 this(al, seqsetid, null);
167 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
169 sequenceSetID = seqsetid;
171 // TODO remove these once 2.4.VAMSAS release finished
172 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
174 Cache.log.debug("Setting viewport's sequence set id : "
177 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
179 Cache.log.debug("Setting viewport's view id : " + viewId);
186 * Create a new AlignViewport with hidden regions
190 * @param hiddenColumns
193 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
196 if (hiddenColumns != null)
198 colSel = hiddenColumns;
204 * New viewport with hidden columns and an existing sequence set id
207 * @param hiddenColumns
211 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
214 this(al, hiddenColumns, seqsetid, null);
218 * New viewport with hidden columns and an existing sequence set id and viewid
221 * @param hiddenColumns
227 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
228 String seqsetid, String viewid)
230 sequenceSetID = seqsetid;
232 // TODO remove these once 2.4.VAMSAS release finished
233 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
235 Cache.log.debug("Setting viewport's sequence set id : "
238 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
240 Cache.log.debug("Setting viewport's view id : " + viewId);
243 if (hiddenColumns != null)
245 colSel = hiddenColumns;
253 this.endRes = alignment.getWidth() - 1;
255 this.endSeq = alignment.getHeight() - 1;
257 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
259 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
260 setShowAnnotation(Cache.getDefault("SHOW_ANNOTATIONS", true));
262 setRightAlignIds(Cache.getDefault("RIGHT_ALIGN_IDS", false));
263 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
264 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
266 setPadGaps(Cache.getDefault("PAD_GAPS", true));
267 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
268 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
270 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
271 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
272 String fontSize = Cache.getDefault("FONT_SIZE", "10");
274 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
278 if (fontStyle.equals("bold"))
282 else if (fontStyle.equals("italic"))
287 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
290 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
292 // We must set conservation and consensus before setting colour,
293 // as Blosum and Clustal require this to be done
294 if (hconsensus == null && !isDataset)
296 if (!alignment.isNucleotide())
298 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
299 showQuality = Cache.getDefault("SHOW_QUALITY", true);
300 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
303 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
305 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
306 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
308 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
309 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
311 initAutoAnnotation();
312 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
314 globalColourScheme = ColourSchemeProperty.getColour(alignment,
315 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
317 if (globalColourScheme instanceof UserColourScheme)
319 globalColourScheme = UserDefinedColours.loadDefaultColours();
320 ((UserColourScheme) globalColourScheme).setThreshold(0,
321 getIgnoreGapsConsensus());
324 if (globalColourScheme != null)
326 globalColourScheme.setConsensus(hconsensus);
330 wrapAlignment = Cache.getDefault("WRAP_ALIGNMENT", false);
331 showUnconserved = Cache.getDefault("SHOW_UNCONSERVED", false);
332 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
333 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
334 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
335 Preferences.SORT_ANNOTATIONS,
336 SequenceAnnotationOrder.NONE.name()));
337 showAutocalculatedAbove = Cache.getDefault(
338 Preferences.SHOW_AUTOCALC_ABOVE, false);
342 * centre columnar annotation labels in displayed alignment annotation TODO:
343 * add to jalviewXML and annotation display settings
345 boolean centreColumnLabels = false;
347 private boolean showdbrefs;
349 private boolean shownpfeats;
351 // --------END Structure Conservation
354 * get the consensus sequence as displayed under the PID consensus annotation
357 * @return consensus sequence as a new sequence object
359 public SequenceI getConsensusSeq()
361 if (consensus == null)
363 updateConsensus(null);
365 if (consensus == null)
369 StringBuffer seqs = new StringBuffer();
370 for (int i = 0; i < consensus.annotations.length; i++)
372 if (consensus.annotations[i] != null)
374 if (consensus.annotations[i].description.charAt(0) == '[')
376 seqs.append(consensus.annotations[i].description.charAt(1));
380 seqs.append(consensus.annotations[i].displayCharacter);
385 SequenceI sq = new Sequence("Consensus", seqs.toString());
386 sq.setDescription("Percentage Identity Consensus "
387 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
394 * @return DOCUMENT ME!
396 public int getStartRes()
404 * @return DOCUMENT ME!
406 public int getEndRes()
414 * @return DOCUMENT ME!
416 public int getStartSeq()
427 public void setStartRes(int res)
438 public void setStartSeq(int seq)
449 public void setEndRes(int res)
451 if (res > (alignment.getWidth() - 1))
453 // log.System.out.println(" Corrected res from " + res + " to maximum " +
454 // (alignment.getWidth()-1));
455 res = alignment.getWidth() - 1;
472 public void setEndSeq(int seq)
474 if (seq > alignment.getHeight())
476 seq = alignment.getHeight();
490 * @return DOCUMENT ME!
492 public int getEndSeq()
503 public void setFont(Font f)
507 Container c = new Container();
509 java.awt.FontMetrics fm = c.getFontMetrics(font);
510 setCharHeight(fm.getHeight());
511 setCharWidth(fm.charWidth('M'));
512 validCharWidth = true;
518 * @return DOCUMENT ME!
520 public Font getFont()
531 public void setCharWidth(int w)
539 * @return DOCUMENT ME!
541 public int getCharWidth()
552 public void setCharHeight(int h)
560 * @return DOCUMENT ME!
562 public int getCharHeight()
573 public void setWrappedWidth(int w)
575 this.wrappedWidth = w;
581 * @return DOCUMENT ME!
583 public int getWrappedWidth()
591 * @return DOCUMENT ME!
593 public AlignmentI getAlignment()
604 public void setAlignment(AlignmentI align)
606 if (alignment != null && alignment.getCodonFrames() != null)
608 StructureSelectionManager.getStructureSelectionManager(
609 Desktop.instance).removeMappings(alignment.getCodonFrames());
611 this.alignment = align;
612 if (alignment != null && alignment.getCodonFrames() != null)
614 StructureSelectionManager.getStructureSelectionManager(
615 Desktop.instance).addMappings(alignment.getCodonFrames());
625 public void setWrapAlignment(boolean state)
627 wrapAlignment = state;
636 public void setShowText(boolean state)
647 public void setRenderGaps(boolean state)
655 * @return DOCUMENT ME!
657 public boolean getColourText()
659 return showColourText;
668 public void setColourText(boolean state)
670 showColourText = state;
679 public void setShowBoxes(boolean state)
687 * @return DOCUMENT ME!
689 public boolean getWrapAlignment()
691 return wrapAlignment;
697 * @return DOCUMENT ME!
699 public boolean getShowText()
707 * @return DOCUMENT ME!
709 public boolean getShowBoxes()
717 * @return DOCUMENT ME!
719 public char getGapCharacter()
721 return getAlignment().getGapCharacter();
730 public void setGapCharacter(char gap)
732 if (getAlignment() != null)
734 getAlignment().setGapCharacter(gap);
741 * @return DOCUMENT ME!
743 public ColumnSelection getColumnSelection()
754 public void setCurrentTree(NJTree tree)
762 * @return DOCUMENT ME!
764 public NJTree getCurrentTree()
772 * @return DOCUMENT ME!
774 public boolean getShowJVSuffix()
785 public void setShowJVSuffix(boolean b)
793 * @return DOCUMENT ME!
795 public boolean getScaleAboveWrapped()
797 return scaleAboveWrapped;
803 * @return DOCUMENT ME!
805 public boolean getScaleLeftWrapped()
807 return scaleLeftWrapped;
813 * @return DOCUMENT ME!
815 public boolean getScaleRightWrapped()
817 return scaleRightWrapped;
826 public void setScaleAboveWrapped(boolean b)
828 scaleAboveWrapped = b;
837 public void setScaleLeftWrapped(boolean b)
839 scaleLeftWrapped = b;
848 public void setScaleRightWrapped(boolean b)
850 scaleRightWrapped = b;
853 public void setDataset(boolean b)
858 public boolean isDataset()
863 public boolean getShowHiddenMarkers()
865 return showHiddenMarkers;
868 public void setShowHiddenMarkers(boolean show)
870 showHiddenMarkers = show;
874 * returns the visible column regions of the alignment
876 * @param selectedRegionOnly
877 * true to just return the contigs intersecting with the selected
881 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
883 int[] viscontigs = null;
884 int start = 0, end = 0;
885 if (selectedRegionOnly && selectionGroup != null)
887 start = selectionGroup.getStartRes();
888 end = selectionGroup.getEndRes() + 1;
892 end = alignment.getWidth();
894 viscontigs = colSel.getVisibleContigs(start, end);
899 * get hash of undo and redo list for the alignment
901 * @return long[] { historyList.hashCode, redoList.hashCode };
903 public long[] getUndoRedoHash()
906 if (historyList == null || redoList == null)
912 { historyList.hashCode(), this.redoList.hashCode() };
916 * test if a particular set of hashcodes are different to the hashcodes for
917 * the undo and redo list.
920 * the stored set of hashcodes as returned by getUndoRedoHash
921 * @return true if the hashcodes differ (ie the alignment has been edited) or
922 * the stored hashcode array differs in size
924 public boolean isUndoRedoHashModified(long[] undoredo)
926 if (undoredo == null)
930 long[] cstate = getUndoRedoHash();
931 if (cstate.length != undoredo.length)
936 for (int i = 0; i < cstate.length; i++)
938 if (cstate[i] != undoredo[i])
946 public boolean getCentreColumnLabels()
948 return centreColumnLabels;
951 public void setCentreColumnLabels(boolean centrecolumnlabels)
953 centreColumnLabels = centrecolumnlabels;
957 * enable or disable the display of Database Cross References in the sequence
960 public void setShowDbRefs(boolean show)
967 * @return true if Database References are to be displayed on tooltips.
969 public boolean isShowDbRefs()
976 * @return true if Non-positional features are to be displayed on tooltips.
978 public boolean isShowNpFeats()
984 * enable or disable the display of Non-Positional sequence features in the
985 * sequence ID tooltip
989 public void setShowNpFeats(boolean show)
996 * when set, view will scroll to show the highlighted position
998 public boolean followHighlight = true;
1001 * @return true if view should scroll to show the highlighted region of a
1005 public boolean getFollowHighlight()
1007 return followHighlight;
1010 public boolean followSelection = true;
1013 * @return true if view selection should always follow the selections
1014 * broadcast by other selection sources
1016 public boolean getFollowSelection()
1018 return followSelection;
1021 public void sendSelection()
1023 jalview.structure.StructureSelectionManager
1024 .getStructureSelectionManager(Desktop.instance).sendSelection(
1025 new SequenceGroup(getSelectionGroup()),
1026 new ColumnSelection(getColumnSelection()), this);
1030 * return the alignPanel containing the given viewport. Use this to get the
1031 * components currently handling the given viewport.
1034 * @return null or an alignPanel guaranteed to have non-null alignFrame
1037 public AlignmentPanel getAlignPanel()
1039 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1040 .getSequenceSetId());
1041 AlignmentPanel ap = null;
1042 for (int p = 0; aps != null && p < aps.length; p++)
1044 if (aps[p].av == this)
1052 public boolean getSortByTree()
1057 public void setSortByTree(boolean sort)
1063 * synthesize a column selection if none exists so it covers the given
1064 * selection group. if wholewidth is false, no column selection is made if the
1065 * selection group covers the whole alignment width.
1070 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1074 && (sgs = sg.getStartRes()) >= 0
1075 && sg.getStartRes() <= (sge = sg.getEndRes())
1076 && (colSel == null || colSel.getSelected() == null || colSel
1077 .getSelected().size() == 0))
1079 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1086 colSel = new ColumnSelection();
1088 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1090 colSel.addElement(cspos);
1095 public StructureSelectionManager getStructureSelectionManager()
1097 return StructureSelectionManager
1098 .getStructureSelectionManager(Desktop.instance);
1104 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1105 * sequence in the alignment holds a reference to it
1107 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1109 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1110 for (PDBEntry pdb : pdbEntries)
1112 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1113 for (int i = 0; i < alignment.getHeight(); i++)
1115 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1122 for (int p = 0; p < pdbs.size(); p++)
1124 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1125 if (p1.getId().equals(pdb.getId()))
1127 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1136 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1138 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1141 public boolean isNormaliseSequenceLogo()
1143 return normaliseSequenceLogo;
1146 public void setNormaliseSequenceLogo(boolean state)
1148 normaliseSequenceLogo = state;
1153 * @return true if alignment characters should be displayed
1155 public boolean isValidCharWidth()
1157 return validCharWidth;
1160 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1162 private boolean showAutocalculatedAbove;
1164 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1166 return calcIdParams.get(calcId);
1169 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1170 boolean needsUpdate)
1172 calcIdParams.put(calcId, settings);
1173 // TODO: create a restart list to trigger any calculations that need to be
1174 // restarted after load
1175 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1178 Cache.log.debug("trigger update for " + calcId);
1182 protected SequenceAnnotationOrder getSortAnnotationsBy()
1184 return sortAnnotationsBy;
1187 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1189 this.sortAnnotationsBy = sortAnnotationsBy;
1192 protected boolean isShowAutocalculatedAbove()
1194 return showAutocalculatedAbove;
1197 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1199 this.showAutocalculatedAbove = showAutocalculatedAbove;
1202 public AnnotationColumnChooser getAnnotationColumnSelectionState()
1204 return annotationColumnSelectionState;
1207 public void setAnnotationColumnSelectionState(
1208 AnnotationColumnChooser currentAnnotationColumnSelectionState)
1210 this.annotationColumnSelectionState = currentAnnotationColumnSelectionState;