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 this.colSel = hiddenColumns;
199 if (hiddenColumns.getHiddenColumns() != null
200 && hiddenColumns.getHiddenColumns().size() > 0)
202 hasHiddenColumns = true;
206 hasHiddenColumns = false;
213 * New viewport with hidden columns and an existing sequence set id
216 * @param hiddenColumns
220 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
223 this(al, hiddenColumns, seqsetid, null);
227 * New viewport with hidden columns and an existing sequence set id and viewid
230 * @param hiddenColumns
236 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
237 String seqsetid, String viewid)
239 sequenceSetID = seqsetid;
241 // TODO remove these once 2.4.VAMSAS release finished
242 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
244 Cache.log.debug("Setting viewport's sequence set id : "
247 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
249 Cache.log.debug("Setting viewport's view id : " + viewId);
252 if (hiddenColumns != null)
254 this.colSel = hiddenColumns;
255 if (hiddenColumns.getHiddenColumns() != null
256 && hiddenColumns.getHiddenColumns().size() > 0)
258 hasHiddenColumns = true;
262 hasHiddenColumns = false;
271 this.endRes = alignment.getWidth() - 1;
273 this.endSeq = alignment.getHeight() - 1;
275 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
277 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
278 setShowAnnotation(Cache.getDefault("SHOW_ANNOTATIONS", true));
280 setRightAlignIds(Cache.getDefault("RIGHT_ALIGN_IDS", false));
281 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
282 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
284 setPadGaps(Cache.getDefault("PAD_GAPS", true));
285 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
286 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
288 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
289 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
290 String fontSize = Cache.getDefault("FONT_SIZE", "10");
292 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
296 if (fontStyle.equals("bold"))
300 else if (fontStyle.equals("italic"))
305 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
308 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
310 // We must set conservation and consensus before setting colour,
311 // as Blosum and Clustal require this to be done
312 if (hconsensus == null && !isDataset)
314 if (!alignment.isNucleotide())
316 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
317 showQuality = Cache.getDefault("SHOW_QUALITY", true);
318 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
321 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
323 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
324 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
326 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
327 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
329 initAutoAnnotation();
330 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
332 globalColourScheme = ColourSchemeProperty.getColour(alignment,
333 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
335 if (globalColourScheme instanceof UserColourScheme)
337 globalColourScheme = UserDefinedColours.loadDefaultColours();
338 ((UserColourScheme) globalColourScheme).setThreshold(0,
339 getIgnoreGapsConsensus());
342 if (globalColourScheme != null)
344 globalColourScheme.setConsensus(hconsensus);
348 wrapAlignment = Cache.getDefault("WRAP_ALIGNMENT", false);
349 showUnconserved = Cache.getDefault("SHOW_UNCONSERVED", false);
350 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
351 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
352 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
353 Preferences.SORT_ANNOTATIONS,
354 SequenceAnnotationOrder.NONE.name()));
355 showAutocalculatedAbove = Cache.getDefault(
356 Preferences.SHOW_AUTOCALC_ABOVE, false);
360 * centre columnar annotation labels in displayed alignment annotation TODO:
361 * add to jalviewXML and annotation display settings
363 boolean centreColumnLabels = false;
365 private boolean showdbrefs;
367 private boolean shownpfeats;
369 // --------END Structure Conservation
372 * get the consensus sequence as displayed under the PID consensus annotation
375 * @return consensus sequence as a new sequence object
377 public SequenceI getConsensusSeq()
379 if (consensus == null)
381 updateConsensus(null);
383 if (consensus == null)
387 StringBuffer seqs = new StringBuffer();
388 for (int i = 0; i < consensus.annotations.length; i++)
390 if (consensus.annotations[i] != null)
392 if (consensus.annotations[i].description.charAt(0) == '[')
394 seqs.append(consensus.annotations[i].description.charAt(1));
398 seqs.append(consensus.annotations[i].displayCharacter);
403 SequenceI sq = new Sequence("Consensus", seqs.toString());
404 sq.setDescription("Percentage Identity Consensus "
405 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
412 * @return DOCUMENT ME!
414 public int getStartRes()
422 * @return DOCUMENT ME!
424 public int getEndRes()
432 * @return DOCUMENT ME!
434 public int getStartSeq()
445 public void setStartRes(int res)
456 public void setStartSeq(int seq)
467 public void setEndRes(int res)
469 if (res > (alignment.getWidth() - 1))
471 // log.System.out.println(" Corrected res from " + res + " to maximum " +
472 // (alignment.getWidth()-1));
473 res = alignment.getWidth() - 1;
490 public void setEndSeq(int seq)
492 if (seq > alignment.getHeight())
494 seq = alignment.getHeight();
508 * @return DOCUMENT ME!
510 public int getEndSeq()
521 public void setFont(Font f)
525 Container c = new Container();
527 java.awt.FontMetrics fm = c.getFontMetrics(font);
528 setCharHeight(fm.getHeight());
529 setCharWidth(fm.charWidth('M'));
530 validCharWidth = true;
536 * @return DOCUMENT ME!
538 public Font getFont()
549 public void setCharWidth(int w)
557 * @return DOCUMENT ME!
559 public int getCharWidth()
570 public void setCharHeight(int h)
578 * @return DOCUMENT ME!
580 public int getCharHeight()
591 public void setWrappedWidth(int w)
593 this.wrappedWidth = w;
599 * @return DOCUMENT ME!
601 public int getWrappedWidth()
609 * @return DOCUMENT ME!
611 public AlignmentI getAlignment()
622 public void setAlignment(AlignmentI align)
624 if (alignment != null && alignment.getCodonFrames() != null)
626 StructureSelectionManager.getStructureSelectionManager(
627 Desktop.instance).removeMappings(alignment.getCodonFrames());
629 this.alignment = align;
630 if (alignment != null && alignment.getCodonFrames() != null)
632 StructureSelectionManager.getStructureSelectionManager(
633 Desktop.instance).addMappings(alignment.getCodonFrames());
643 public void setWrapAlignment(boolean state)
645 wrapAlignment = state;
654 public void setShowText(boolean state)
665 public void setRenderGaps(boolean state)
673 * @return DOCUMENT ME!
675 public boolean getColourText()
677 return showColourText;
686 public void setColourText(boolean state)
688 showColourText = state;
697 public void setShowBoxes(boolean state)
705 * @return DOCUMENT ME!
707 public boolean getWrapAlignment()
709 return wrapAlignment;
715 * @return DOCUMENT ME!
717 public boolean getShowText()
725 * @return DOCUMENT ME!
727 public boolean getShowBoxes()
735 * @return DOCUMENT ME!
737 public char getGapCharacter()
739 return getAlignment().getGapCharacter();
748 public void setGapCharacter(char gap)
750 if (getAlignment() != null)
752 getAlignment().setGapCharacter(gap);
759 * @return DOCUMENT ME!
761 public ColumnSelection getColumnSelection()
772 public void setCurrentTree(NJTree tree)
780 * @return DOCUMENT ME!
782 public NJTree getCurrentTree()
790 * @return DOCUMENT ME!
792 public boolean getShowJVSuffix()
803 public void setShowJVSuffix(boolean b)
811 * @return DOCUMENT ME!
813 public boolean getScaleAboveWrapped()
815 return scaleAboveWrapped;
821 * @return DOCUMENT ME!
823 public boolean getScaleLeftWrapped()
825 return scaleLeftWrapped;
831 * @return DOCUMENT ME!
833 public boolean getScaleRightWrapped()
835 return scaleRightWrapped;
844 public void setScaleAboveWrapped(boolean b)
846 scaleAboveWrapped = b;
855 public void setScaleLeftWrapped(boolean b)
857 scaleLeftWrapped = b;
866 public void setScaleRightWrapped(boolean b)
868 scaleRightWrapped = b;
871 public void setDataset(boolean b)
876 public boolean isDataset()
881 public boolean getShowHiddenMarkers()
883 return showHiddenMarkers;
886 public void setShowHiddenMarkers(boolean show)
888 showHiddenMarkers = show;
892 * returns the visible column regions of the alignment
894 * @param selectedRegionOnly
895 * true to just return the contigs intersecting with the selected
899 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
901 int[] viscontigs = null;
902 int start = 0, end = 0;
903 if (selectedRegionOnly && selectionGroup != null)
905 start = selectionGroup.getStartRes();
906 end = selectionGroup.getEndRes() + 1;
910 end = alignment.getWidth();
912 viscontigs = colSel.getVisibleContigs(start, end);
917 * get hash of undo and redo list for the alignment
919 * @return long[] { historyList.hashCode, redoList.hashCode };
921 public long[] getUndoRedoHash()
924 if (historyList == null || redoList == null)
930 { historyList.hashCode(), this.redoList.hashCode() };
934 * test if a particular set of hashcodes are different to the hashcodes for
935 * the undo and redo list.
938 * the stored set of hashcodes as returned by getUndoRedoHash
939 * @return true if the hashcodes differ (ie the alignment has been edited) or
940 * the stored hashcode array differs in size
942 public boolean isUndoRedoHashModified(long[] undoredo)
944 if (undoredo == null)
948 long[] cstate = getUndoRedoHash();
949 if (cstate.length != undoredo.length)
954 for (int i = 0; i < cstate.length; i++)
956 if (cstate[i] != undoredo[i])
964 public boolean getCentreColumnLabels()
966 return centreColumnLabels;
969 public void setCentreColumnLabels(boolean centrecolumnlabels)
971 centreColumnLabels = centrecolumnlabels;
975 * enable or disable the display of Database Cross References in the sequence
978 public void setShowDbRefs(boolean show)
985 * @return true if Database References are to be displayed on tooltips.
987 public boolean isShowDbRefs()
994 * @return true if Non-positional features are to be displayed on tooltips.
996 public boolean isShowNpFeats()
1002 * enable or disable the display of Non-Positional sequence features in the
1003 * sequence ID tooltip
1007 public void setShowNpFeats(boolean show)
1014 * @return true if view has hidden rows
1016 public boolean hasHiddenRows()
1018 return hasHiddenRows;
1023 * @return true if view has hidden columns
1025 public boolean hasHiddenColumns()
1027 return hasHiddenColumns;
1031 * when set, view will scroll to show the highlighted position
1033 public boolean followHighlight = true;
1036 * @return true if view should scroll to show the highlighted region of a
1040 public boolean getFollowHighlight()
1042 return followHighlight;
1045 public boolean followSelection = true;
1048 * @return true if view selection should always follow the selections
1049 * broadcast by other selection sources
1051 public boolean getFollowSelection()
1053 return followSelection;
1056 public void sendSelection()
1058 jalview.structure.StructureSelectionManager
1059 .getStructureSelectionManager(Desktop.instance).sendSelection(
1060 new SequenceGroup(getSelectionGroup()),
1061 new ColumnSelection(getColumnSelection()), this);
1065 * return the alignPanel containing the given viewport. Use this to get the
1066 * components currently handling the given viewport.
1069 * @return null or an alignPanel guaranteed to have non-null alignFrame
1072 public AlignmentPanel getAlignPanel()
1074 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1075 .getSequenceSetId());
1076 AlignmentPanel ap = null;
1077 for (int p = 0; aps != null && p < aps.length; p++)
1079 if (aps[p].av == this)
1087 public boolean getSortByTree()
1092 public void setSortByTree(boolean sort)
1098 * synthesize a column selection if none exists so it covers the given
1099 * selection group. if wholewidth is false, no column selection is made if the
1100 * selection group covers the whole alignment width.
1105 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1109 && (sgs = sg.getStartRes()) >= 0
1110 && sg.getStartRes() <= (sge = sg.getEndRes())
1111 && (colSel == null || colSel.getSelected() == null || colSel
1112 .getSelected().size() == 0))
1114 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1121 colSel = new ColumnSelection();
1123 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1125 colSel.addElement(cspos);
1130 public StructureSelectionManager getStructureSelectionManager()
1132 return StructureSelectionManager
1133 .getStructureSelectionManager(Desktop.instance);
1139 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1140 * sequence in the alignment holds a reference to it
1142 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1144 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1145 for (PDBEntry pdb : pdbEntries)
1147 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1148 for (int i = 0; i < alignment.getHeight(); i++)
1150 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1157 for (int p = 0; p < pdbs.size(); p++)
1159 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1160 if (p1.getId().equals(pdb.getId()))
1162 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1171 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1173 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1176 public boolean isNormaliseSequenceLogo()
1178 return normaliseSequenceLogo;
1181 public void setNormaliseSequenceLogo(boolean state)
1183 normaliseSequenceLogo = state;
1188 * @return true if alignment characters should be displayed
1190 public boolean isValidCharWidth()
1192 return validCharWidth;
1195 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1197 private boolean showAutocalculatedAbove;
1199 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1201 return calcIdParams.get(calcId);
1204 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1205 boolean needsUpdate)
1207 calcIdParams.put(calcId, settings);
1208 // TODO: create a restart list to trigger any calculations that need to be
1209 // restarted after load
1210 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1213 Cache.log.debug("trigger update for " + calcId);
1217 protected SequenceAnnotationOrder getSortAnnotationsBy()
1219 return sortAnnotationsBy;
1222 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1224 this.sortAnnotationsBy = sortAnnotationsBy;
1227 protected boolean isShowAutocalculatedAbove()
1229 return showAutocalculatedAbove;
1232 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1234 this.showAutocalculatedAbove = showAutocalculatedAbove;
1237 public AnnotationColumnChooser getAnnotationColumnSelectionState()
1239 return annotationColumnSelectionState;
1242 public void setAnnotationColumnSelectionState(
1243 AnnotationColumnChooser currentAnnotationColumnSelectionState)
1245 this.annotationColumnSelectionState = currentAnnotationColumnSelectionState;