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 boolean showSequenceFeatures = false;
100 boolean showAnnotation = true;
102 SequenceAnnotationOrder sortAnnotationsBy = null;
108 boolean validCharWidth;
114 boolean seqNameItalics;
116 NJTree currentTree = null;
118 boolean scaleAboveWrapped = false;
120 boolean scaleLeftWrapped = true;
122 boolean scaleRightWrapped = true;
124 boolean showHiddenMarkers = true;
126 boolean cursorMode = false;
129 * Keys are the feature types which are currently visible. Note: Values are
132 Hashtable featuresDisplayed = null;
134 boolean antiAlias = false;
136 Rectangle explodedPosition;
140 boolean gatherViewsHere = false;
142 Stack<CommandI> historyList = new Stack<CommandI>();
144 Stack<CommandI> redoList = new Stack<CommandI>();
146 int thresholdTextColour = 0;
148 Color textColour = Color.black;
150 Color textColour2 = Color.white;
152 boolean rightAlignIds = false;
155 * Creates a new AlignViewport object.
160 public AlignViewport(AlignmentI al)
167 * Create a new AlignViewport object with a specific sequence set ID
171 * (may be null - but potential for ambiguous constructor exception)
173 public AlignViewport(AlignmentI al, String seqsetid)
175 this(al, seqsetid, null);
178 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
180 sequenceSetID = seqsetid;
182 // TODO remove these once 2.4.VAMSAS release finished
183 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
185 Cache.log.debug("Setting viewport's sequence set id : "
188 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
190 Cache.log.debug("Setting viewport's view id : " + viewId);
197 * Create a new AlignViewport with hidden regions
201 * @param hiddenColumns
204 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
207 if (hiddenColumns != null)
209 this.colSel = hiddenColumns;
210 if (hiddenColumns.getHiddenColumns() != null
211 && hiddenColumns.getHiddenColumns().size() > 0)
213 hasHiddenColumns = true;
217 hasHiddenColumns = false;
224 * New viewport with hidden columns and an existing sequence set id
227 * @param hiddenColumns
231 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
234 this(al, hiddenColumns, seqsetid, null);
238 * New viewport with hidden columns and an existing sequence set id and viewid
241 * @param hiddenColumns
247 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
248 String seqsetid, String viewid)
250 sequenceSetID = seqsetid;
252 // TODO remove these once 2.4.VAMSAS release finished
253 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
255 Cache.log.debug("Setting viewport's sequence set id : "
258 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
260 Cache.log.debug("Setting viewport's view id : " + viewId);
263 if (hiddenColumns != null)
265 this.colSel = hiddenColumns;
266 if (hiddenColumns.getHiddenColumns() != null
267 && hiddenColumns.getHiddenColumns().size() > 0)
269 hasHiddenColumns = true;
273 hasHiddenColumns = false;
282 this.endRes = alignment.getWidth() - 1;
284 this.endSeq = alignment.getHeight() - 1;
286 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
288 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
289 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
291 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
292 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
293 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
295 setPadGaps(Cache.getDefault("PAD_GAPS", true));
296 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
297 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
299 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
300 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
301 String fontSize = Cache.getDefault("FONT_SIZE", "10");
303 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
307 if (fontStyle.equals("bold"))
311 else if (fontStyle.equals("italic"))
316 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
319 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
321 // We must set conservation and consensus before setting colour,
322 // as Blosum and Clustal require this to be done
323 if (hconsensus == null && !isDataset)
325 if (!alignment.isNucleotide())
327 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
328 showQuality = Cache.getDefault("SHOW_QUALITY", true);
329 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
332 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
334 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
335 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
337 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
338 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
340 initAutoAnnotation();
341 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
343 globalColourScheme = ColourSchemeProperty.getColour(alignment,
344 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
346 if (globalColourScheme instanceof UserColourScheme)
348 globalColourScheme = UserDefinedColours.loadDefaultColours();
349 ((UserColourScheme) globalColourScheme).setThreshold(0,
350 getIgnoreGapsConsensus());
353 if (globalColourScheme != null)
355 globalColourScheme.setConsensus(hconsensus);
359 wrapAlignment = Cache.getDefault("WRAP_ALIGNMENT", false);
360 showUnconserved = Cache.getDefault("SHOW_UNCONSERVED", false);
361 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
362 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
363 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
364 Preferences.SORT_ANNOTATIONS,
365 SequenceAnnotationOrder.NONE.name()));
366 showAutocalculatedAbove = Cache.getDefault(
367 Preferences.SHOW_AUTOCALC_ABOVE, false);
374 * features are displayed if true
376 public void setShowSequenceFeatures(boolean b)
378 showSequenceFeatures = b;
381 public boolean getShowSequenceFeatures()
383 return showSequenceFeatures;
387 * centre columnar annotation labels in displayed alignment annotation TODO:
388 * add to jalviewXML and annotation display settings
390 boolean centreColumnLabels = false;
392 private boolean showdbrefs;
394 private boolean shownpfeats;
396 // --------END Structure Conservation
399 * get the consensus sequence as displayed under the PID consensus annotation
402 * @return consensus sequence as a new sequence object
404 public SequenceI getConsensusSeq()
406 if (consensus == null)
408 updateConsensus(null);
410 if (consensus == null)
414 StringBuffer seqs = new StringBuffer();
415 for (int i = 0; i < consensus.annotations.length; i++)
417 if (consensus.annotations[i] != null)
419 if (consensus.annotations[i].description.charAt(0) == '[')
421 seqs.append(consensus.annotations[i].description.charAt(1));
425 seqs.append(consensus.annotations[i].displayCharacter);
430 SequenceI sq = new Sequence("Consensus", seqs.toString());
431 sq.setDescription("Percentage Identity Consensus "
432 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
439 * @return DOCUMENT ME!
441 public int getStartRes()
449 * @return DOCUMENT ME!
451 public int getEndRes()
459 * @return DOCUMENT ME!
461 public int getStartSeq()
472 public void setStartRes(int res)
483 public void setStartSeq(int seq)
494 public void setEndRes(int res)
496 if (res > (alignment.getWidth() - 1))
498 // log.System.out.println(" Corrected res from " + res + " to maximum " +
499 // (alignment.getWidth()-1));
500 res = alignment.getWidth() - 1;
517 public void setEndSeq(int seq)
519 if (seq > alignment.getHeight())
521 seq = alignment.getHeight();
535 * @return DOCUMENT ME!
537 public int getEndSeq()
548 public void setFont(Font f)
552 Container c = new Container();
554 java.awt.FontMetrics fm = c.getFontMetrics(font);
555 setCharHeight(fm.getHeight());
556 setCharWidth(fm.charWidth('M'));
557 validCharWidth = true;
563 * @return DOCUMENT ME!
565 public Font getFont()
576 public void setCharWidth(int w)
584 * @return DOCUMENT ME!
586 public int getCharWidth()
597 public void setCharHeight(int h)
605 * @return DOCUMENT ME!
607 public int getCharHeight()
618 public void setWrappedWidth(int w)
620 this.wrappedWidth = w;
626 * @return DOCUMENT ME!
628 public int getWrappedWidth()
636 * @return DOCUMENT ME!
638 public AlignmentI getAlignment()
649 public void setAlignment(AlignmentI align)
651 if (alignment != null && alignment.getCodonFrames() != null)
653 StructureSelectionManager.getStructureSelectionManager(
654 Desktop.instance).removeMappings(alignment.getCodonFrames());
656 this.alignment = align;
657 if (alignment != null && alignment.getCodonFrames() != null)
659 StructureSelectionManager.getStructureSelectionManager(
660 Desktop.instance).addMappings(alignment.getCodonFrames());
670 public void setWrapAlignment(boolean state)
672 wrapAlignment = state;
681 public void setShowText(boolean state)
692 public void setRenderGaps(boolean state)
700 * @return DOCUMENT ME!
702 public boolean getColourText()
704 return showColourText;
713 public void setColourText(boolean state)
715 showColourText = state;
724 public void setShowBoxes(boolean state)
732 * @return DOCUMENT ME!
734 public boolean getWrapAlignment()
736 return wrapAlignment;
742 * @return DOCUMENT ME!
744 public boolean getShowText()
752 * @return DOCUMENT ME!
754 public boolean getShowBoxes()
762 * @return DOCUMENT ME!
764 public char getGapCharacter()
766 return getAlignment().getGapCharacter();
775 public void setGapCharacter(char gap)
777 if (getAlignment() != null)
779 getAlignment().setGapCharacter(gap);
786 * @return DOCUMENT ME!
788 public ColumnSelection getColumnSelection()
799 public void setCurrentTree(NJTree tree)
807 * @return DOCUMENT ME!
809 public NJTree getCurrentTree()
817 * @return DOCUMENT ME!
819 public boolean getShowJVSuffix()
830 public void setShowJVSuffix(boolean b)
838 * @return DOCUMENT ME!
840 public boolean getShowAnnotation()
842 return showAnnotation;
851 public void setShowAnnotation(boolean b)
859 * @return DOCUMENT ME!
861 public boolean getScaleAboveWrapped()
863 return scaleAboveWrapped;
869 * @return DOCUMENT ME!
871 public boolean getScaleLeftWrapped()
873 return scaleLeftWrapped;
879 * @return DOCUMENT ME!
881 public boolean getScaleRightWrapped()
883 return scaleRightWrapped;
892 public void setScaleAboveWrapped(boolean b)
894 scaleAboveWrapped = b;
903 public void setScaleLeftWrapped(boolean b)
905 scaleLeftWrapped = b;
914 public void setScaleRightWrapped(boolean b)
916 scaleRightWrapped = b;
919 public void setDataset(boolean b)
924 public boolean isDataset()
929 public boolean getShowHiddenMarkers()
931 return showHiddenMarkers;
934 public void setShowHiddenMarkers(boolean show)
936 showHiddenMarkers = show;
940 * returns the visible column regions of the alignment
942 * @param selectedRegionOnly
943 * true to just return the contigs intersecting with the selected
947 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
949 int[] viscontigs = null;
950 int start = 0, end = 0;
951 if (selectedRegionOnly && selectionGroup != null)
953 start = selectionGroup.getStartRes();
954 end = selectionGroup.getEndRes() + 1;
958 end = alignment.getWidth();
960 viscontigs = colSel.getVisibleContigs(start, end);
965 * get hash of undo and redo list for the alignment
967 * @return long[] { historyList.hashCode, redoList.hashCode };
969 public long[] getUndoRedoHash()
972 if (historyList == null || redoList == null)
978 { historyList.hashCode(), this.redoList.hashCode() };
982 * test if a particular set of hashcodes are different to the hashcodes for
983 * the undo and redo list.
986 * the stored set of hashcodes as returned by getUndoRedoHash
987 * @return true if the hashcodes differ (ie the alignment has been edited) or
988 * the stored hashcode array differs in size
990 public boolean isUndoRedoHashModified(long[] undoredo)
992 if (undoredo == null)
996 long[] cstate = getUndoRedoHash();
997 if (cstate.length != undoredo.length)
1002 for (int i = 0; i < cstate.length; i++)
1004 if (cstate[i] != undoredo[i])
1012 public boolean getCentreColumnLabels()
1014 return centreColumnLabels;
1017 public void setCentreColumnLabels(boolean centrecolumnlabels)
1019 centreColumnLabels = centrecolumnlabels;
1023 * enable or disable the display of Database Cross References in the sequence
1026 public void setShowDbRefs(boolean show)
1033 * @return true if Database References are to be displayed on tooltips.
1035 public boolean isShowDbRefs()
1042 * @return true if Non-positional features are to be displayed on tooltips.
1044 public boolean isShowNpFeats()
1050 * enable or disable the display of Non-Positional sequence features in the
1051 * sequence ID tooltip
1055 public void setShowNpFeats(boolean show)
1062 * @return true if view has hidden rows
1064 public boolean hasHiddenRows()
1066 return hasHiddenRows;
1071 * @return true if view has hidden columns
1073 public boolean hasHiddenColumns()
1075 return hasHiddenColumns;
1079 * when set, view will scroll to show the highlighted position
1081 public boolean followHighlight = true;
1084 * @return true if view should scroll to show the highlighted region of a
1088 public boolean getFollowHighlight()
1090 return followHighlight;
1093 public boolean followSelection = true;
1096 * @return true if view selection should always follow the selections
1097 * broadcast by other selection sources
1099 public boolean getFollowSelection()
1101 return followSelection;
1104 boolean showSeqFeaturesHeight;
1106 public void sendSelection()
1108 jalview.structure.StructureSelectionManager
1109 .getStructureSelectionManager(Desktop.instance).sendSelection(
1110 new SequenceGroup(getSelectionGroup()),
1111 new ColumnSelection(getColumnSelection()), this);
1114 public void setShowSequenceFeaturesHeight(boolean selected)
1116 showSeqFeaturesHeight = selected;
1119 public boolean getShowSequenceFeaturesHeight()
1121 return showSeqFeaturesHeight;
1125 * return the alignPanel containing the given viewport. Use this to get the
1126 * components currently handling the given viewport.
1129 * @return null or an alignPanel guaranteed to have non-null alignFrame
1132 public AlignmentPanel getAlignPanel()
1134 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1135 .getSequenceSetId());
1136 AlignmentPanel ap = null;
1137 for (int p = 0; aps != null && p < aps.length; p++)
1139 if (aps[p].av == this)
1147 public boolean getSortByTree()
1152 public void setSortByTree(boolean sort)
1158 * synthesize a column selection if none exists so it covers the given
1159 * selection group. if wholewidth is false, no column selection is made if the
1160 * selection group covers the whole alignment width.
1165 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1169 && (sgs = sg.getStartRes()) >= 0
1170 && sg.getStartRes() <= (sge = sg.getEndRes())
1171 && (colSel == null || colSel.getSelected() == null || colSel
1172 .getSelected().size() == 0))
1174 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1181 colSel = new ColumnSelection();
1183 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1185 colSel.addElement(cspos);
1190 public StructureSelectionManager getStructureSelectionManager()
1192 return StructureSelectionManager
1193 .getStructureSelectionManager(Desktop.instance);
1199 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1200 * sequence in the alignment holds a reference to it
1202 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1204 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1205 for (PDBEntry pdb : pdbEntries)
1207 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1208 for (int i = 0; i < alignment.getHeight(); i++)
1210 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1217 for (int p = 0; p < pdbs.size(); p++)
1219 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1220 if (p1.getId().equals(pdb.getId()))
1222 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1231 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1233 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1236 public boolean isNormaliseSequenceLogo()
1238 return normaliseSequenceLogo;
1241 public void setNormaliseSequenceLogo(boolean state)
1243 normaliseSequenceLogo = state;
1248 * @return true if alignment characters should be displayed
1250 public boolean isValidCharWidth()
1252 return validCharWidth;
1255 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1257 private boolean showAutocalculatedAbove;
1259 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1261 return calcIdParams.get(calcId);
1264 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1265 boolean needsUpdate)
1267 calcIdParams.put(calcId, settings);
1268 // TODO: create a restart list to trigger any calculations that need to be
1269 // restarted after load
1270 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1273 Cache.log.debug("trigger update for " + calcId);
1277 protected SequenceAnnotationOrder getSortAnnotationsBy()
1279 return sortAnnotationsBy;
1282 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1284 this.sortAnnotationsBy = sortAnnotationsBy;
1287 protected boolean isShowAutocalculatedAbove()
1289 return showAutocalculatedAbove;
1292 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1294 this.showAutocalculatedAbove = showAutocalculatedAbove;