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;
141 private boolean rightAlignIds = false;
143 private AnnotationColumnChooser annotationColumnSelectionState;
145 * Creates a new AlignViewport object.
150 public AlignViewport(AlignmentI al)
157 * Create a new AlignViewport object with a specific sequence set ID
161 * (may be null - but potential for ambiguous constructor exception)
163 public AlignViewport(AlignmentI al, String seqsetid)
165 this(al, seqsetid, null);
168 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
170 sequenceSetID = seqsetid;
172 // TODO remove these once 2.4.VAMSAS release finished
173 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
175 Cache.log.debug("Setting viewport's sequence set id : "
178 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
180 Cache.log.debug("Setting viewport's view id : " + viewId);
187 * Create a new AlignViewport with hidden regions
191 * @param hiddenColumns
194 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
197 if (hiddenColumns != null)
199 this.colSel = hiddenColumns;
200 if (hiddenColumns.getHiddenColumns() != null
201 && hiddenColumns.getHiddenColumns().size() > 0)
203 hasHiddenColumns = true;
207 hasHiddenColumns = false;
214 * New viewport with hidden columns and an existing sequence set id
217 * @param hiddenColumns
221 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
224 this(al, hiddenColumns, seqsetid, null);
228 * New viewport with hidden columns and an existing sequence set id and viewid
231 * @param hiddenColumns
237 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
238 String seqsetid, String viewid)
240 sequenceSetID = seqsetid;
242 // TODO remove these once 2.4.VAMSAS release finished
243 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
245 Cache.log.debug("Setting viewport's sequence set id : "
248 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
250 Cache.log.debug("Setting viewport's view id : " + viewId);
253 if (hiddenColumns != null)
255 this.colSel = hiddenColumns;
256 if (hiddenColumns.getHiddenColumns() != null
257 && hiddenColumns.getHiddenColumns().size() > 0)
259 hasHiddenColumns = true;
263 hasHiddenColumns = false;
272 this.endRes = alignment.getWidth() - 1;
274 this.endSeq = alignment.getHeight() - 1;
276 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
278 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
279 setShowAnnotation(Cache.getDefault("SHOW_ANNOTATIONS", true));
281 setRightAlignIds(Cache.getDefault("RIGHT_ALIGN_IDS", false));
282 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
283 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
285 setPadGaps(Cache.getDefault("PAD_GAPS", true));
286 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
287 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
289 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
290 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
291 String fontSize = Cache.getDefault("FONT_SIZE", "10");
293 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
297 if (fontStyle.equals("bold"))
301 else if (fontStyle.equals("italic"))
306 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
309 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
311 // We must set conservation and consensus before setting colour,
312 // as Blosum and Clustal require this to be done
313 if (hconsensus == null && !isDataset)
315 if (!alignment.isNucleotide())
317 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
318 showQuality = Cache.getDefault("SHOW_QUALITY", true);
319 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
322 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
324 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
325 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
327 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
328 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
330 initAutoAnnotation();
331 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
333 globalColourScheme = ColourSchemeProperty.getColour(alignment,
334 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
336 if (globalColourScheme instanceof UserColourScheme)
338 globalColourScheme = UserDefinedColours.loadDefaultColours();
339 ((UserColourScheme) globalColourScheme).setThreshold(0,
340 getIgnoreGapsConsensus());
343 if (globalColourScheme != null)
345 globalColourScheme.setConsensus(hconsensus);
349 wrapAlignment = Cache.getDefault("WRAP_ALIGNMENT", false);
350 showUnconserved = Cache.getDefault("SHOW_UNCONSERVED", false);
351 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
352 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
353 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
354 Preferences.SORT_ANNOTATIONS,
355 SequenceAnnotationOrder.NONE.name()));
356 showAutocalculatedAbove = Cache.getDefault(
357 Preferences.SHOW_AUTOCALC_ABOVE, false);
361 * centre columnar annotation labels in displayed alignment annotation TODO:
362 * add to jalviewXML and annotation display settings
364 boolean centreColumnLabels = false;
366 private boolean showdbrefs;
368 private boolean shownpfeats;
370 // --------END Structure Conservation
373 * get the consensus sequence as displayed under the PID consensus annotation
376 * @return consensus sequence as a new sequence object
378 public SequenceI getConsensusSeq()
380 if (consensus == null)
382 updateConsensus(null);
384 if (consensus == null)
388 StringBuffer seqs = new StringBuffer();
389 for (int i = 0; i < consensus.annotations.length; i++)
391 if (consensus.annotations[i] != null)
393 if (consensus.annotations[i].description.charAt(0) == '[')
395 seqs.append(consensus.annotations[i].description.charAt(1));
399 seqs.append(consensus.annotations[i].displayCharacter);
404 SequenceI sq = new Sequence("Consensus", seqs.toString());
405 sq.setDescription("Percentage Identity Consensus "
406 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
413 * @return DOCUMENT ME!
415 public int getStartRes()
423 * @return DOCUMENT ME!
425 public int getEndRes()
433 * @return DOCUMENT ME!
435 public int getStartSeq()
446 public void setStartRes(int res)
457 public void setStartSeq(int seq)
468 public void setEndRes(int res)
470 if (res > (alignment.getWidth() - 1))
472 // log.System.out.println(" Corrected res from " + res + " to maximum " +
473 // (alignment.getWidth()-1));
474 res = alignment.getWidth() - 1;
491 public void setEndSeq(int seq)
493 if (seq > alignment.getHeight())
495 seq = alignment.getHeight();
509 * @return DOCUMENT ME!
511 public int getEndSeq()
522 public void setFont(Font f)
526 Container c = new Container();
528 java.awt.FontMetrics fm = c.getFontMetrics(font);
529 setCharHeight(fm.getHeight());
530 setCharWidth(fm.charWidth('M'));
531 validCharWidth = true;
537 * @return DOCUMENT ME!
539 public Font getFont()
550 public void setCharWidth(int w)
558 * @return DOCUMENT ME!
560 public int getCharWidth()
571 public void setCharHeight(int h)
579 * @return DOCUMENT ME!
581 public int getCharHeight()
592 public void setWrappedWidth(int w)
594 this.wrappedWidth = w;
600 * @return DOCUMENT ME!
602 public int getWrappedWidth()
610 * @return DOCUMENT ME!
612 public AlignmentI getAlignment()
623 public void setAlignment(AlignmentI align)
625 if (alignment != null && alignment.getCodonFrames() != null)
627 StructureSelectionManager.getStructureSelectionManager(
628 Desktop.instance).removeMappings(alignment.getCodonFrames());
630 this.alignment = align;
631 if (alignment != null && alignment.getCodonFrames() != null)
633 StructureSelectionManager.getStructureSelectionManager(
634 Desktop.instance).addMappings(alignment.getCodonFrames());
644 public void setWrapAlignment(boolean state)
646 wrapAlignment = state;
655 public void setShowText(boolean state)
666 public void setRenderGaps(boolean state)
674 * @return DOCUMENT ME!
676 public boolean getColourText()
678 return showColourText;
687 public void setColourText(boolean state)
689 showColourText = state;
698 public void setShowBoxes(boolean state)
706 * @return DOCUMENT ME!
708 public boolean getWrapAlignment()
710 return wrapAlignment;
716 * @return DOCUMENT ME!
718 public boolean getShowText()
726 * @return DOCUMENT ME!
728 public boolean getShowBoxes()
736 * @return DOCUMENT ME!
738 public char getGapCharacter()
740 return getAlignment().getGapCharacter();
749 public void setGapCharacter(char gap)
751 if (getAlignment() != null)
753 getAlignment().setGapCharacter(gap);
760 * @return DOCUMENT ME!
762 public ColumnSelection getColumnSelection()
773 public void setCurrentTree(NJTree tree)
781 * @return DOCUMENT ME!
783 public NJTree getCurrentTree()
791 * @return DOCUMENT ME!
793 public boolean getShowJVSuffix()
804 public void setShowJVSuffix(boolean b)
812 * @return DOCUMENT ME!
814 public boolean getScaleAboveWrapped()
816 return scaleAboveWrapped;
822 * @return DOCUMENT ME!
824 public boolean getScaleLeftWrapped()
826 return scaleLeftWrapped;
832 * @return DOCUMENT ME!
834 public boolean getScaleRightWrapped()
836 return scaleRightWrapped;
845 public void setScaleAboveWrapped(boolean b)
847 scaleAboveWrapped = b;
856 public void setScaleLeftWrapped(boolean b)
858 scaleLeftWrapped = b;
867 public void setScaleRightWrapped(boolean b)
869 scaleRightWrapped = b;
872 public void setDataset(boolean b)
877 public boolean isDataset()
882 public boolean getShowHiddenMarkers()
884 return showHiddenMarkers;
887 public void setShowHiddenMarkers(boolean show)
889 showHiddenMarkers = show;
893 * returns the visible column regions of the alignment
895 * @param selectedRegionOnly
896 * true to just return the contigs intersecting with the selected
900 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
902 int[] viscontigs = null;
903 int start = 0, end = 0;
904 if (selectedRegionOnly && selectionGroup != null)
906 start = selectionGroup.getStartRes();
907 end = selectionGroup.getEndRes() + 1;
911 end = alignment.getWidth();
913 viscontigs = colSel.getVisibleContigs(start, end);
918 * get hash of undo and redo list for the alignment
920 * @return long[] { historyList.hashCode, redoList.hashCode };
922 public long[] getUndoRedoHash()
925 if (historyList == null || redoList == null)
931 { historyList.hashCode(), this.redoList.hashCode() };
935 * test if a particular set of hashcodes are different to the hashcodes for
936 * the undo and redo list.
939 * the stored set of hashcodes as returned by getUndoRedoHash
940 * @return true if the hashcodes differ (ie the alignment has been edited) or
941 * the stored hashcode array differs in size
943 public boolean isUndoRedoHashModified(long[] undoredo)
945 if (undoredo == null)
949 long[] cstate = getUndoRedoHash();
950 if (cstate.length != undoredo.length)
955 for (int i = 0; i < cstate.length; i++)
957 if (cstate[i] != undoredo[i])
965 public boolean getCentreColumnLabels()
967 return centreColumnLabels;
970 public void setCentreColumnLabels(boolean centrecolumnlabels)
972 centreColumnLabels = centrecolumnlabels;
976 * enable or disable the display of Database Cross References in the sequence
979 public void setShowDbRefs(boolean show)
986 * @return true if Database References are to be displayed on tooltips.
988 public boolean isShowDbRefs()
995 * @return true if Non-positional features are to be displayed on tooltips.
997 public boolean isShowNpFeats()
1003 * enable or disable the display of Non-Positional sequence features in the
1004 * sequence ID tooltip
1008 public void setShowNpFeats(boolean show)
1015 * @return true if view has hidden rows
1017 public boolean hasHiddenRows()
1019 return hasHiddenRows;
1024 * @return true if view has hidden columns
1026 public boolean hasHiddenColumns()
1028 return hasHiddenColumns;
1032 * when set, view will scroll to show the highlighted position
1034 public boolean followHighlight = true;
1037 * @return true if view should scroll to show the highlighted region of a
1041 public boolean getFollowHighlight()
1043 return followHighlight;
1046 public boolean followSelection = true;
1049 * @return true if view selection should always follow the selections
1050 * broadcast by other selection sources
1052 public boolean getFollowSelection()
1054 return followSelection;
1057 public void sendSelection()
1059 jalview.structure.StructureSelectionManager
1060 .getStructureSelectionManager(Desktop.instance).sendSelection(
1061 new SequenceGroup(getSelectionGroup()),
1062 new ColumnSelection(getColumnSelection()), this);
1066 * return the alignPanel containing the given viewport. Use this to get the
1067 * components currently handling the given viewport.
1070 * @return null or an alignPanel guaranteed to have non-null alignFrame
1073 public AlignmentPanel getAlignPanel()
1075 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1076 .getSequenceSetId());
1077 AlignmentPanel ap = null;
1078 for (int p = 0; aps != null && p < aps.length; p++)
1080 if (aps[p].av == this)
1088 public boolean getSortByTree()
1093 public void setSortByTree(boolean sort)
1099 * synthesize a column selection if none exists so it covers the given
1100 * selection group. if wholewidth is false, no column selection is made if the
1101 * selection group covers the whole alignment width.
1106 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1110 && (sgs = sg.getStartRes()) >= 0
1111 && sg.getStartRes() <= (sge = sg.getEndRes())
1112 && (colSel == null || colSel.getSelected() == null || colSel
1113 .getSelected().size() == 0))
1115 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1122 colSel = new ColumnSelection();
1124 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1126 colSel.addElement(cspos);
1131 public StructureSelectionManager getStructureSelectionManager()
1133 return StructureSelectionManager
1134 .getStructureSelectionManager(Desktop.instance);
1140 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1141 * sequence in the alignment holds a reference to it
1143 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1145 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1146 for (PDBEntry pdb : pdbEntries)
1148 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1149 for (int i = 0; i < alignment.getHeight(); i++)
1151 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1158 for (int p = 0; p < pdbs.size(); p++)
1160 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1161 if (p1.getId().equals(pdb.getId()))
1163 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1172 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1174 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1177 public boolean isNormaliseSequenceLogo()
1179 return normaliseSequenceLogo;
1182 public void setNormaliseSequenceLogo(boolean state)
1184 normaliseSequenceLogo = state;
1189 * @return true if alignment characters should be displayed
1191 public boolean isValidCharWidth()
1193 return validCharWidth;
1196 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1198 private boolean showAutocalculatedAbove;
1200 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1202 return calcIdParams.get(calcId);
1205 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1206 boolean needsUpdate)
1208 calcIdParams.put(calcId, settings);
1209 // TODO: create a restart list to trigger any calculations that need to be
1210 // restarted after load
1211 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1214 Cache.log.debug("trigger update for " + calcId);
1218 protected SequenceAnnotationOrder getSortAnnotationsBy()
1220 return sortAnnotationsBy;
1223 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1225 this.sortAnnotationsBy = sortAnnotationsBy;
1228 protected boolean isShowAutocalculatedAbove()
1230 return showAutocalculatedAbove;
1233 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1235 this.showAutocalculatedAbove = showAutocalculatedAbove;
1239 public boolean isShowAnnotation()
1241 return super.isShowAnnotation();
1244 public boolean isRightAlignIds()
1246 return rightAlignIds;
1249 public void setRightAlignIds(boolean rightAlignIds)
1251 this.rightAlignIds = rightAlignIds;
1254 public AnnotationColumnChooser getAnnotationColumnSelectionState()
1256 return annotationColumnSelectionState;
1259 public void setAnnotationColumnSelectionState(
1260 AnnotationColumnChooser currentAnnotationColumnSelectionState)
1262 this.annotationColumnSelectionState = currentAnnotationColumnSelectionState;