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.AlignmentAnnotation;
47 import jalview.datamodel.AlignmentI;
48 import jalview.datamodel.Annotation;
49 import jalview.datamodel.ColumnSelection;
50 import jalview.datamodel.PDBEntry;
51 import jalview.datamodel.Sequence;
52 import jalview.datamodel.SequenceGroup;
53 import jalview.datamodel.SequenceI;
54 import jalview.schemes.ColourSchemeProperty;
55 import jalview.schemes.UserColourScheme;
56 import jalview.structure.SelectionSource;
57 import jalview.structure.StructureSelectionManager;
58 import jalview.structure.VamsasSource;
59 import jalview.viewmodel.AlignmentViewport;
60 import jalview.ws.params.AutoCalcSetting;
62 import java.awt.Color;
63 import java.awt.Container;
65 import java.awt.Rectangle;
66 import java.util.ArrayList;
67 import java.util.Hashtable;
68 import java.util.Stack;
69 import java.util.Vector;
75 * @version $Revision: 1.141 $
77 public class AlignViewport extends AlignmentViewport implements
78 SelectionSource, VamsasSource, AlignViewportI
88 boolean showJVSuffix = true;
90 boolean showText = true;
92 boolean showColourText = false;
94 boolean showBoxes = true;
96 boolean wrapAlignment = false;
98 boolean renderGaps = true;
100 boolean showSequenceFeatures = false;
102 boolean showAnnotation = true;
104 SequenceAnnotationOrder sortAnnotationsBy = null;
110 boolean validCharWidth;
116 boolean seqNameItalics;
118 NJTree currentTree = null;
120 boolean scaleAboveWrapped = false;
122 boolean scaleLeftWrapped = true;
124 boolean scaleRightWrapped = true;
126 boolean showHiddenMarkers = true;
128 boolean cursorMode = false;
131 * Keys are the feature types which are currently visible. Note: Values are
134 Hashtable featuresDisplayed = null;
136 boolean antiAlias = false;
138 Rectangle explodedPosition;
142 boolean gatherViewsHere = false;
144 Stack<CommandI> historyList = new Stack<CommandI>();
146 Stack<CommandI> redoList = new Stack<CommandI>();
148 int thresholdTextColour = 0;
150 Color textColour = Color.black;
152 Color textColour2 = Color.white;
154 boolean rightAlignIds = false;
157 * Creates a new AlignViewport object.
162 public AlignViewport(AlignmentI al)
169 * Create a new AlignViewport object with a specific sequence set ID
173 * (may be null - but potential for ambiguous constructor exception)
175 public AlignViewport(AlignmentI al, String seqsetid)
177 this(al, seqsetid, null);
180 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
182 sequenceSetID = seqsetid;
184 // TODO remove these once 2.4.VAMSAS release finished
185 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
187 Cache.log.debug("Setting viewport's sequence set id : "
190 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
192 Cache.log.debug("Setting viewport's view id : " + viewId);
199 * Create a new AlignViewport with hidden regions
203 * @param hiddenColumns
206 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
209 if (hiddenColumns != null)
211 this.colSel = hiddenColumns;
212 if (hiddenColumns.getHiddenColumns() != null
213 && hiddenColumns.getHiddenColumns().size() > 0)
215 hasHiddenColumns = true;
219 hasHiddenColumns = false;
226 * New viewport with hidden columns and an existing sequence set id
229 * @param hiddenColumns
233 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
236 this(al, hiddenColumns, seqsetid, null);
240 * New viewport with hidden columns and an existing sequence set id and viewid
243 * @param hiddenColumns
249 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
250 String seqsetid, String viewid)
252 sequenceSetID = seqsetid;
254 // TODO remove these once 2.4.VAMSAS release finished
255 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
257 Cache.log.debug("Setting viewport's sequence set id : "
260 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
262 Cache.log.debug("Setting viewport's view id : " + viewId);
265 if (hiddenColumns != null)
267 this.colSel = hiddenColumns;
268 if (hiddenColumns.getHiddenColumns() != null
269 && hiddenColumns.getHiddenColumns().size() > 0)
271 hasHiddenColumns = true;
275 hasHiddenColumns = false;
284 this.endRes = alignment.getWidth() - 1;
286 this.endSeq = alignment.getHeight() - 1;
288 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
290 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
291 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
293 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
294 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
295 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
297 setPadGaps(Cache.getDefault("PAD_GAPS", true));
298 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
299 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
301 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
302 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
303 String fontSize = Cache.getDefault("FONT_SIZE", "10");
305 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
309 if (fontStyle.equals("bold"))
313 else if (fontStyle.equals("italic"))
318 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
321 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
323 // We must set conservation and consensus before setting colour,
324 // as Blosum and Clustal require this to be done
325 if (hconsensus == null && !isDataset)
327 if (!alignment.isNucleotide())
329 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
330 showQuality = Cache.getDefault("SHOW_QUALITY", true);
331 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
334 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
336 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
337 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
339 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
340 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
341 consensus = new AlignmentAnnotation("Consensus", "PID",
342 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
343 consensus.hasText = true;
344 consensus.autoCalculated = true;
346 initAutoAnnotation();
347 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
349 globalColourScheme = ColourSchemeProperty.getColour(alignment,
350 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
352 if (globalColourScheme instanceof UserColourScheme)
354 globalColourScheme = UserDefinedColours.loadDefaultColours();
355 ((UserColourScheme) globalColourScheme).setThreshold(0,
356 getIgnoreGapsConsensus());
359 if (globalColourScheme != null)
361 globalColourScheme.setConsensus(hconsensus);
365 wrapAlignment = Cache.getDefault("WRAP_ALIGNMENT", false);
366 showUnconserved = Cache.getDefault("SHOW_UNCONSERVED", false);
367 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
368 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
369 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
370 Preferences.SORT_ANNOTATIONS,
371 SequenceAnnotationOrder.NONE.name()));
372 showAutocalculatedAbove = Cache.getDefault(
373 Preferences.SHOW_AUTOCALC_ABOVE, false);
380 * features are displayed if true
382 public void setShowSequenceFeatures(boolean b)
384 showSequenceFeatures = b;
387 public boolean getShowSequenceFeatures()
389 return showSequenceFeatures;
393 * centre columnar annotation labels in displayed alignment annotation TODO:
394 * add to jalviewXML and annotation display settings
396 boolean centreColumnLabels = false;
398 private boolean showdbrefs;
400 private boolean shownpfeats;
402 // --------END Structure Conservation
405 * get the consensus sequence as displayed under the PID consensus annotation
408 * @return consensus sequence as a new sequence object
410 public SequenceI getConsensusSeq()
412 if (consensus == null)
414 updateConsensus(null);
416 if (consensus == null)
420 StringBuffer seqs = new StringBuffer();
421 for (int i = 0; i < consensus.annotations.length; i++)
423 if (consensus.annotations[i] != null)
425 if (consensus.annotations[i].description.charAt(0) == '[')
427 seqs.append(consensus.annotations[i].description.charAt(1));
431 seqs.append(consensus.annotations[i].displayCharacter);
436 SequenceI sq = new Sequence("Consensus", seqs.toString());
437 sq.setDescription("Percentage Identity Consensus "
438 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
445 * @return DOCUMENT ME!
447 public int getStartRes()
455 * @return DOCUMENT ME!
457 public int getEndRes()
465 * @return DOCUMENT ME!
467 public int getStartSeq()
478 public void setStartRes(int res)
489 public void setStartSeq(int seq)
500 public void setEndRes(int res)
502 if (res > (alignment.getWidth() - 1))
504 // log.System.out.println(" Corrected res from " + res + " to maximum " +
505 // (alignment.getWidth()-1));
506 res = alignment.getWidth() - 1;
523 public void setEndSeq(int seq)
525 if (seq > alignment.getHeight())
527 seq = alignment.getHeight();
541 * @return DOCUMENT ME!
543 public int getEndSeq()
554 public void setFont(Font f)
558 Container c = new Container();
560 java.awt.FontMetrics fm = c.getFontMetrics(font);
561 setCharHeight(fm.getHeight());
562 setCharWidth(fm.charWidth('M'));
563 validCharWidth = true;
569 * @return DOCUMENT ME!
571 public Font getFont()
582 public void setCharWidth(int w)
590 * @return DOCUMENT ME!
592 public int getCharWidth()
603 public void setCharHeight(int h)
611 * @return DOCUMENT ME!
613 public int getCharHeight()
624 public void setWrappedWidth(int w)
626 this.wrappedWidth = w;
632 * @return DOCUMENT ME!
634 public int getWrappedWidth()
642 * @return DOCUMENT ME!
644 public AlignmentI getAlignment()
655 public void setAlignment(AlignmentI align)
657 if (alignment != null && alignment.getCodonFrames() != null)
659 StructureSelectionManager.getStructureSelectionManager(
660 Desktop.instance).removeMappings(alignment.getCodonFrames());
662 this.alignment = align;
663 if (alignment != null && alignment.getCodonFrames() != null)
665 StructureSelectionManager.getStructureSelectionManager(
666 Desktop.instance).addMappings(alignment.getCodonFrames());
676 public void setWrapAlignment(boolean state)
678 wrapAlignment = state;
687 public void setShowText(boolean state)
698 public void setRenderGaps(boolean state)
706 * @return DOCUMENT ME!
708 public boolean getColourText()
710 return showColourText;
719 public void setColourText(boolean state)
721 showColourText = state;
730 public void setShowBoxes(boolean state)
738 * @return DOCUMENT ME!
740 public boolean getWrapAlignment()
742 return wrapAlignment;
748 * @return DOCUMENT ME!
750 public boolean getShowText()
758 * @return DOCUMENT ME!
760 public boolean getShowBoxes()
768 * @return DOCUMENT ME!
770 public char getGapCharacter()
772 return getAlignment().getGapCharacter();
781 public void setGapCharacter(char gap)
783 if (getAlignment() != null)
785 getAlignment().setGapCharacter(gap);
792 * @return DOCUMENT ME!
794 public ColumnSelection getColumnSelection()
805 public void setCurrentTree(NJTree tree)
813 * @return DOCUMENT ME!
815 public NJTree getCurrentTree()
823 * @return DOCUMENT ME!
825 public boolean getShowJVSuffix()
836 public void setShowJVSuffix(boolean b)
844 * @return DOCUMENT ME!
846 public boolean getShowAnnotation()
848 return showAnnotation;
857 public void setShowAnnotation(boolean b)
865 * @return DOCUMENT ME!
867 public boolean getScaleAboveWrapped()
869 return scaleAboveWrapped;
875 * @return DOCUMENT ME!
877 public boolean getScaleLeftWrapped()
879 return scaleLeftWrapped;
885 * @return DOCUMENT ME!
887 public boolean getScaleRightWrapped()
889 return scaleRightWrapped;
898 public void setScaleAboveWrapped(boolean b)
900 scaleAboveWrapped = b;
909 public void setScaleLeftWrapped(boolean b)
911 scaleLeftWrapped = b;
920 public void setScaleRightWrapped(boolean b)
922 scaleRightWrapped = b;
925 public void setDataset(boolean b)
930 public boolean isDataset()
935 public boolean getShowHiddenMarkers()
937 return showHiddenMarkers;
940 public void setShowHiddenMarkers(boolean show)
942 showHiddenMarkers = show;
946 * returns the visible column regions of the alignment
948 * @param selectedRegionOnly
949 * true to just return the contigs intersecting with the selected
953 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
955 int[] viscontigs = null;
956 int start = 0, end = 0;
957 if (selectedRegionOnly && selectionGroup != null)
959 start = selectionGroup.getStartRes();
960 end = selectionGroup.getEndRes() + 1;
964 end = alignment.getWidth();
966 viscontigs = colSel.getVisibleContigs(start, end);
971 * get hash of undo and redo list for the alignment
973 * @return long[] { historyList.hashCode, redoList.hashCode };
975 public long[] getUndoRedoHash()
978 if (historyList == null || redoList == null)
984 { historyList.hashCode(), this.redoList.hashCode() };
988 * test if a particular set of hashcodes are different to the hashcodes for
989 * the undo and redo list.
992 * the stored set of hashcodes as returned by getUndoRedoHash
993 * @return true if the hashcodes differ (ie the alignment has been edited) or
994 * the stored hashcode array differs in size
996 public boolean isUndoRedoHashModified(long[] undoredo)
998 if (undoredo == null)
1002 long[] cstate = getUndoRedoHash();
1003 if (cstate.length != undoredo.length)
1008 for (int i = 0; i < cstate.length; i++)
1010 if (cstate[i] != undoredo[i])
1018 public boolean getCentreColumnLabels()
1020 return centreColumnLabels;
1023 public void setCentreColumnLabels(boolean centrecolumnlabels)
1025 centreColumnLabels = centrecolumnlabels;
1029 * enable or disable the display of Database Cross References in the sequence
1032 public void setShowDbRefs(boolean show)
1039 * @return true if Database References are to be displayed on tooltips.
1041 public boolean isShowDbRefs()
1048 * @return true if Non-positional features are to be displayed on tooltips.
1050 public boolean isShowNpFeats()
1056 * enable or disable the display of Non-Positional sequence features in the
1057 * sequence ID tooltip
1061 public void setShowNpFeats(boolean show)
1068 * @return true if view has hidden rows
1070 public boolean hasHiddenRows()
1072 return hasHiddenRows;
1077 * @return true if view has hidden columns
1079 public boolean hasHiddenColumns()
1081 return hasHiddenColumns;
1085 * when set, view will scroll to show the highlighted position
1087 public boolean followHighlight = true;
1090 * @return true if view should scroll to show the highlighted region of a
1094 public boolean getFollowHighlight()
1096 return followHighlight;
1099 public boolean followSelection = true;
1102 * @return true if view selection should always follow the selections
1103 * broadcast by other selection sources
1105 public boolean getFollowSelection()
1107 return followSelection;
1110 boolean showSeqFeaturesHeight;
1112 public void sendSelection()
1114 jalview.structure.StructureSelectionManager
1115 .getStructureSelectionManager(Desktop.instance).sendSelection(
1116 new SequenceGroup(getSelectionGroup()),
1117 new ColumnSelection(getColumnSelection()), this);
1120 public void setShowSequenceFeaturesHeight(boolean selected)
1122 showSeqFeaturesHeight = selected;
1125 public boolean getShowSequenceFeaturesHeight()
1127 return showSeqFeaturesHeight;
1131 * return the alignPanel containing the given viewport. Use this to get the
1132 * components currently handling the given viewport.
1135 * @return null or an alignPanel guaranteed to have non-null alignFrame
1138 public AlignmentPanel getAlignPanel()
1140 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1141 .getSequenceSetId());
1142 AlignmentPanel ap = null;
1143 for (int p = 0; aps != null && p < aps.length; p++)
1145 if (aps[p].av == this)
1153 public boolean getSortByTree()
1158 public void setSortByTree(boolean sort)
1164 * synthesize a column selection if none exists so it covers the given
1165 * selection group. if wholewidth is false, no column selection is made if the
1166 * selection group covers the whole alignment width.
1171 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1175 && (sgs = sg.getStartRes()) >= 0
1176 && sg.getStartRes() <= (sge = sg.getEndRes())
1177 && (colSel == null || colSel.getSelected() == null || colSel
1178 .getSelected().size() == 0))
1180 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1187 colSel = new ColumnSelection();
1189 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1191 colSel.addElement(cspos);
1196 public StructureSelectionManager getStructureSelectionManager()
1198 return StructureSelectionManager
1199 .getStructureSelectionManager(Desktop.instance);
1205 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1206 * sequence in the alignment holds a reference to it
1208 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1210 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1211 for (PDBEntry pdb : pdbEntries)
1213 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1214 for (int i = 0; i < alignment.getHeight(); i++)
1216 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1223 for (int p = 0; p < pdbs.size(); p++)
1225 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1226 if (p1.getId().equals(pdb.getId()))
1228 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1237 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1239 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1242 public boolean isNormaliseSequenceLogo()
1244 return normaliseSequenceLogo;
1247 public void setNormaliseSequenceLogo(boolean state)
1249 normaliseSequenceLogo = state;
1254 * @return true if alignment characters should be displayed
1256 public boolean isValidCharWidth()
1258 return validCharWidth;
1261 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1263 private boolean showAutocalculatedAbove;
1265 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1267 return calcIdParams.get(calcId);
1270 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1271 boolean needsUpdate)
1273 calcIdParams.put(calcId, settings);
1274 // TODO: create a restart list to trigger any calculations that need to be
1275 // restarted after load
1276 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1279 Cache.log.debug("trigger update for " + calcId);
1283 protected SequenceAnnotationOrder getSortAnnotationsBy()
1285 return sortAnnotationsBy;
1288 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1290 this.sortAnnotationsBy = sortAnnotationsBy;
1293 protected boolean isShowAutocalculatedAbove()
1295 return showAutocalculatedAbove;
1298 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1300 this.showAutocalculatedAbove = showAutocalculatedAbove;