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 * Creates a new AlignViewport object.
147 public AlignViewport(AlignmentI al)
154 * Create a new AlignViewport object with a specific sequence set ID
158 * (may be null - but potential for ambiguous constructor exception)
160 public AlignViewport(AlignmentI al, String seqsetid)
162 this(al, seqsetid, null);
165 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
167 sequenceSetID = seqsetid;
169 // TODO remove these once 2.4.VAMSAS release finished
170 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
172 Cache.log.debug("Setting viewport's sequence set id : "
175 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
177 Cache.log.debug("Setting viewport's view id : " + viewId);
184 * Create a new AlignViewport with hidden regions
188 * @param hiddenColumns
191 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
194 if (hiddenColumns != null)
196 this.colSel = hiddenColumns;
197 if (hiddenColumns.getHiddenColumns() != null
198 && hiddenColumns.getHiddenColumns().size() > 0)
200 hasHiddenColumns = true;
204 hasHiddenColumns = false;
211 * New viewport with hidden columns and an existing sequence set id
214 * @param hiddenColumns
218 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
221 this(al, hiddenColumns, seqsetid, null);
225 * New viewport with hidden columns and an existing sequence set id and viewid
228 * @param hiddenColumns
234 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
235 String seqsetid, String viewid)
237 sequenceSetID = seqsetid;
239 // TODO remove these once 2.4.VAMSAS release finished
240 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
242 Cache.log.debug("Setting viewport's sequence set id : "
245 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
247 Cache.log.debug("Setting viewport's view id : " + viewId);
250 if (hiddenColumns != null)
252 this.colSel = hiddenColumns;
253 if (hiddenColumns.getHiddenColumns() != null
254 && hiddenColumns.getHiddenColumns().size() > 0)
256 hasHiddenColumns = true;
260 hasHiddenColumns = false;
269 this.endRes = alignment.getWidth() - 1;
271 this.endSeq = alignment.getHeight() - 1;
273 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
275 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
276 setShowAnnotation(Cache.getDefault("SHOW_ANNOTATIONS", true));
278 setRightAlignIds(Cache.getDefault("RIGHT_ALIGN_IDS", false));
279 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
280 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
282 setPadGaps(Cache.getDefault("PAD_GAPS", true));
283 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
284 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
286 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
287 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
288 String fontSize = Cache.getDefault("FONT_SIZE", "10");
290 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
294 if (fontStyle.equals("bold"))
298 else if (fontStyle.equals("italic"))
303 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
306 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
308 // We must set conservation and consensus before setting colour,
309 // as Blosum and Clustal require this to be done
310 if (hconsensus == null && !isDataset)
312 if (!alignment.isNucleotide())
314 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
315 showQuality = Cache.getDefault("SHOW_QUALITY", true);
316 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
319 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
321 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
322 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
324 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
325 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
327 initAutoAnnotation();
328 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
330 globalColourScheme = ColourSchemeProperty.getColour(alignment,
331 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
333 if (globalColourScheme instanceof UserColourScheme)
335 globalColourScheme = UserDefinedColours.loadDefaultColours();
336 ((UserColourScheme) globalColourScheme).setThreshold(0,
337 getIgnoreGapsConsensus());
340 if (globalColourScheme != null)
342 globalColourScheme.setConsensus(hconsensus);
346 wrapAlignment = Cache.getDefault("WRAP_ALIGNMENT", false);
347 showUnconserved = Cache.getDefault("SHOW_UNCONSERVED", false);
348 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
349 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
350 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
351 Preferences.SORT_ANNOTATIONS,
352 SequenceAnnotationOrder.NONE.name()));
353 showAutocalculatedAbove = Cache.getDefault(
354 Preferences.SHOW_AUTOCALC_ABOVE, false);
358 * centre columnar annotation labels in displayed alignment annotation TODO:
359 * add to jalviewXML and annotation display settings
361 boolean centreColumnLabels = false;
363 private boolean showdbrefs;
365 private boolean shownpfeats;
367 // --------END Structure Conservation
370 * get the consensus sequence as displayed under the PID consensus annotation
373 * @return consensus sequence as a new sequence object
375 public SequenceI getConsensusSeq()
377 if (consensus == null)
379 updateConsensus(null);
381 if (consensus == null)
385 StringBuffer seqs = new StringBuffer();
386 for (int i = 0; i < consensus.annotations.length; i++)
388 if (consensus.annotations[i] != null)
390 if (consensus.annotations[i].description.charAt(0) == '[')
392 seqs.append(consensus.annotations[i].description.charAt(1));
396 seqs.append(consensus.annotations[i].displayCharacter);
401 SequenceI sq = new Sequence("Consensus", seqs.toString());
402 sq.setDescription("Percentage Identity Consensus "
403 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
410 * @return DOCUMENT ME!
412 public int getStartRes()
420 * @return DOCUMENT ME!
422 public int getEndRes()
430 * @return DOCUMENT ME!
432 public int getStartSeq()
443 public void setStartRes(int res)
454 public void setStartSeq(int seq)
465 public void setEndRes(int res)
467 if (res > (alignment.getWidth() - 1))
469 // log.System.out.println(" Corrected res from " + res + " to maximum " +
470 // (alignment.getWidth()-1));
471 res = alignment.getWidth() - 1;
488 public void setEndSeq(int seq)
490 if (seq > alignment.getHeight())
492 seq = alignment.getHeight();
506 * @return DOCUMENT ME!
508 public int getEndSeq()
519 public void setFont(Font f)
523 Container c = new Container();
525 java.awt.FontMetrics fm = c.getFontMetrics(font);
526 setCharHeight(fm.getHeight());
527 setCharWidth(fm.charWidth('M'));
528 validCharWidth = true;
534 * @return DOCUMENT ME!
536 public Font getFont()
547 public void setCharWidth(int w)
555 * @return DOCUMENT ME!
557 public int getCharWidth()
568 public void setCharHeight(int h)
576 * @return DOCUMENT ME!
578 public int getCharHeight()
589 public void setWrappedWidth(int w)
591 this.wrappedWidth = w;
597 * @return DOCUMENT ME!
599 public int getWrappedWidth()
607 * @return DOCUMENT ME!
609 public AlignmentI getAlignment()
620 public void setAlignment(AlignmentI align)
622 if (alignment != null && alignment.getCodonFrames() != null)
624 StructureSelectionManager.getStructureSelectionManager(
625 Desktop.instance).removeMappings(alignment.getCodonFrames());
627 this.alignment = align;
628 if (alignment != null && alignment.getCodonFrames() != null)
630 StructureSelectionManager.getStructureSelectionManager(
631 Desktop.instance).addMappings(alignment.getCodonFrames());
641 public void setWrapAlignment(boolean state)
643 wrapAlignment = state;
652 public void setShowText(boolean state)
663 public void setRenderGaps(boolean state)
671 * @return DOCUMENT ME!
673 public boolean getColourText()
675 return showColourText;
684 public void setColourText(boolean state)
686 showColourText = state;
695 public void setShowBoxes(boolean state)
703 * @return DOCUMENT ME!
705 public boolean getWrapAlignment()
707 return wrapAlignment;
713 * @return DOCUMENT ME!
715 public boolean getShowText()
723 * @return DOCUMENT ME!
725 public boolean getShowBoxes()
733 * @return DOCUMENT ME!
735 public char getGapCharacter()
737 return getAlignment().getGapCharacter();
746 public void setGapCharacter(char gap)
748 if (getAlignment() != null)
750 getAlignment().setGapCharacter(gap);
757 * @return DOCUMENT ME!
759 public ColumnSelection getColumnSelection()
770 public void setCurrentTree(NJTree tree)
778 * @return DOCUMENT ME!
780 public NJTree getCurrentTree()
788 * @return DOCUMENT ME!
790 public boolean getShowJVSuffix()
801 public void setShowJVSuffix(boolean b)
809 * @return DOCUMENT ME!
811 public boolean getScaleAboveWrapped()
813 return scaleAboveWrapped;
819 * @return DOCUMENT ME!
821 public boolean getScaleLeftWrapped()
823 return scaleLeftWrapped;
829 * @return DOCUMENT ME!
831 public boolean getScaleRightWrapped()
833 return scaleRightWrapped;
842 public void setScaleAboveWrapped(boolean b)
844 scaleAboveWrapped = b;
853 public void setScaleLeftWrapped(boolean b)
855 scaleLeftWrapped = b;
864 public void setScaleRightWrapped(boolean b)
866 scaleRightWrapped = b;
869 public void setDataset(boolean b)
874 public boolean isDataset()
879 public boolean getShowHiddenMarkers()
881 return showHiddenMarkers;
884 public void setShowHiddenMarkers(boolean show)
886 showHiddenMarkers = show;
890 * returns the visible column regions of the alignment
892 * @param selectedRegionOnly
893 * true to just return the contigs intersecting with the selected
897 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
899 int[] viscontigs = null;
900 int start = 0, end = 0;
901 if (selectedRegionOnly && selectionGroup != null)
903 start = selectionGroup.getStartRes();
904 end = selectionGroup.getEndRes() + 1;
908 end = alignment.getWidth();
910 viscontigs = colSel.getVisibleContigs(start, end);
915 * get hash of undo and redo list for the alignment
917 * @return long[] { historyList.hashCode, redoList.hashCode };
919 public long[] getUndoRedoHash()
922 if (historyList == null || redoList == null)
928 { historyList.hashCode(), this.redoList.hashCode() };
932 * test if a particular set of hashcodes are different to the hashcodes for
933 * the undo and redo list.
936 * the stored set of hashcodes as returned by getUndoRedoHash
937 * @return true if the hashcodes differ (ie the alignment has been edited) or
938 * the stored hashcode array differs in size
940 public boolean isUndoRedoHashModified(long[] undoredo)
942 if (undoredo == null)
946 long[] cstate = getUndoRedoHash();
947 if (cstate.length != undoredo.length)
952 for (int i = 0; i < cstate.length; i++)
954 if (cstate[i] != undoredo[i])
962 public boolean getCentreColumnLabels()
964 return centreColumnLabels;
967 public void setCentreColumnLabels(boolean centrecolumnlabels)
969 centreColumnLabels = centrecolumnlabels;
973 * enable or disable the display of Database Cross References in the sequence
976 public void setShowDbRefs(boolean show)
983 * @return true if Database References are to be displayed on tooltips.
985 public boolean isShowDbRefs()
992 * @return true if Non-positional features are to be displayed on tooltips.
994 public boolean isShowNpFeats()
1000 * enable or disable the display of Non-Positional sequence features in the
1001 * sequence ID tooltip
1005 public void setShowNpFeats(boolean show)
1012 * @return true if view has hidden rows
1014 public boolean hasHiddenRows()
1016 return hasHiddenRows;
1021 * @return true if view has hidden columns
1023 public boolean hasHiddenColumns()
1025 return hasHiddenColumns;
1029 * when set, view will scroll to show the highlighted position
1031 public boolean followHighlight = true;
1034 * @return true if view should scroll to show the highlighted region of a
1038 public boolean getFollowHighlight()
1040 return followHighlight;
1043 public boolean followSelection = true;
1046 * @return true if view selection should always follow the selections
1047 * broadcast by other selection sources
1049 public boolean getFollowSelection()
1051 return followSelection;
1054 public void sendSelection()
1056 jalview.structure.StructureSelectionManager
1057 .getStructureSelectionManager(Desktop.instance).sendSelection(
1058 new SequenceGroup(getSelectionGroup()),
1059 new ColumnSelection(getColumnSelection()), this);
1063 * return the alignPanel containing the given viewport. Use this to get the
1064 * components currently handling the given viewport.
1067 * @return null or an alignPanel guaranteed to have non-null alignFrame
1070 public AlignmentPanel getAlignPanel()
1072 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1073 .getSequenceSetId());
1074 AlignmentPanel ap = null;
1075 for (int p = 0; aps != null && p < aps.length; p++)
1077 if (aps[p].av == this)
1085 public boolean getSortByTree()
1090 public void setSortByTree(boolean sort)
1096 * synthesize a column selection if none exists so it covers the given
1097 * selection group. if wholewidth is false, no column selection is made if the
1098 * selection group covers the whole alignment width.
1103 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1107 && (sgs = sg.getStartRes()) >= 0
1108 && sg.getStartRes() <= (sge = sg.getEndRes())
1109 && (colSel == null || colSel.getSelected() == null || colSel
1110 .getSelected().size() == 0))
1112 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1119 colSel = new ColumnSelection();
1121 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1123 colSel.addElement(cspos);
1128 public StructureSelectionManager getStructureSelectionManager()
1130 return StructureSelectionManager
1131 .getStructureSelectionManager(Desktop.instance);
1137 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1138 * sequence in the alignment holds a reference to it
1140 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1142 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1143 for (PDBEntry pdb : pdbEntries)
1145 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1146 for (int i = 0; i < alignment.getHeight(); i++)
1148 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1155 for (int p = 0; p < pdbs.size(); p++)
1157 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1158 if (p1.getId().equals(pdb.getId()))
1160 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1169 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1171 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1174 public boolean isNormaliseSequenceLogo()
1176 return normaliseSequenceLogo;
1179 public void setNormaliseSequenceLogo(boolean state)
1181 normaliseSequenceLogo = state;
1186 * @return true if alignment characters should be displayed
1188 public boolean isValidCharWidth()
1190 return validCharWidth;
1193 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1195 private boolean showAutocalculatedAbove;
1197 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1199 return calcIdParams.get(calcId);
1202 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1203 boolean needsUpdate)
1205 calcIdParams.put(calcId, settings);
1206 // TODO: create a restart list to trigger any calculations that need to be
1207 // restarted after load
1208 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1211 Cache.log.debug("trigger update for " + calcId);
1215 protected SequenceAnnotationOrder getSortAnnotationsBy()
1217 return sortAnnotationsBy;
1220 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1222 this.sortAnnotationsBy = sortAnnotationsBy;
1225 protected boolean isShowAutocalculatedAbove()
1227 return showAutocalculatedAbove;
1230 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1232 this.showAutocalculatedAbove = showAutocalculatedAbove;