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.NJTree;
42 import jalview.api.AlignViewportI;
43 import jalview.bin.Cache;
44 import jalview.datamodel.AlignmentAnnotation;
45 import jalview.datamodel.AlignmentI;
46 import jalview.datamodel.Annotation;
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 showAnnotation = true;
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 historyList = new Stack();
134 Stack redoList = new Stack();
136 int thresholdTextColour = 0;
138 Color textColour = Color.black;
140 Color textColour2 = Color.white;
142 boolean rightAlignIds = false;
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 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
281 rightAlignIds = 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);
329 consensus = new AlignmentAnnotation("Consensus", "PID",
330 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
331 consensus.hasText = true;
332 consensus.autoCalculated = true;
334 initAutoAnnotation();
335 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
337 globalColourScheme = ColourSchemeProperty.getColour(alignment,
338 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
340 if (globalColourScheme instanceof UserColourScheme)
342 globalColourScheme = UserDefinedColours.loadDefaultColours();
343 ((UserColourScheme) globalColourScheme).setThreshold(0,
344 getIgnoreGapsConsensus());
347 if (globalColourScheme != null)
349 globalColourScheme.setConsensus(hconsensus);
353 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
354 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
356 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
357 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
362 * centre columnar annotation labels in displayed alignment annotation TODO:
363 * add to jalviewXML and annotation display settings
365 boolean centreColumnLabels = false;
367 private boolean showdbrefs;
369 private boolean shownpfeats;
371 // --------END Structure Conservation
374 * get the consensus sequence as displayed under the PID consensus annotation
377 * @return consensus sequence as a new sequence object
379 public SequenceI getConsensusSeq()
381 if (consensus == null)
383 updateConsensus(null);
385 if (consensus == null)
389 StringBuffer seqs = new StringBuffer();
390 for (int i = 0; i < consensus.annotations.length; i++)
392 if (consensus.annotations[i] != null)
394 if (consensus.annotations[i].description.charAt(0) == '[')
396 seqs.append(consensus.annotations[i].description.charAt(1));
400 seqs.append(consensus.annotations[i].displayCharacter);
405 SequenceI sq = new Sequence("Consensus", seqs.toString());
406 sq.setDescription("Percentage Identity Consensus "
407 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
414 * @return DOCUMENT ME!
416 public int getStartRes()
424 * @return DOCUMENT ME!
426 public int getEndRes()
434 * @return DOCUMENT ME!
436 public int getStartSeq()
447 public void setStartRes(int res)
458 public void setStartSeq(int seq)
469 public void setEndRes(int res)
471 if (res > (alignment.getWidth() - 1))
473 // log.System.out.println(" Corrected res from " + res + " to maximum " +
474 // (alignment.getWidth()-1));
475 res = alignment.getWidth() - 1;
492 public void setEndSeq(int seq)
494 if (seq > alignment.getHeight())
496 seq = alignment.getHeight();
510 * @return DOCUMENT ME!
512 public int getEndSeq()
523 public void setFont(Font f)
527 Container c = new Container();
529 java.awt.FontMetrics fm = c.getFontMetrics(font);
530 setCharHeight(fm.getHeight());
531 setCharWidth(fm.charWidth('M'));
532 validCharWidth = true;
538 * @return DOCUMENT ME!
540 public Font getFont()
551 public void setCharWidth(int w)
559 * @return DOCUMENT ME!
561 public int getCharWidth()
572 public void setCharHeight(int h)
580 * @return DOCUMENT ME!
582 public int getCharHeight()
593 public void setWrappedWidth(int w)
595 this.wrappedWidth = w;
601 * @return DOCUMENT ME!
603 public int getWrappedWidth()
611 * @return DOCUMENT ME!
613 public AlignmentI getAlignment()
624 public void setAlignment(AlignmentI align)
626 if (alignment != null && alignment.getCodonFrames() != null)
628 StructureSelectionManager.getStructureSelectionManager(
629 Desktop.instance).removeMappings(alignment.getCodonFrames());
631 this.alignment = align;
632 if (alignment != null && alignment.getCodonFrames() != null)
634 StructureSelectionManager.getStructureSelectionManager(
635 Desktop.instance).addMappings(alignment.getCodonFrames());
645 public void setWrapAlignment(boolean state)
647 wrapAlignment = state;
656 public void setShowText(boolean state)
667 public void setRenderGaps(boolean state)
675 * @return DOCUMENT ME!
677 public boolean getColourText()
679 return showColourText;
688 public void setColourText(boolean state)
690 showColourText = state;
699 public void setShowBoxes(boolean state)
707 * @return DOCUMENT ME!
709 public boolean getWrapAlignment()
711 return wrapAlignment;
717 * @return DOCUMENT ME!
719 public boolean getShowText()
727 * @return DOCUMENT ME!
729 public boolean getShowBoxes()
737 * @return DOCUMENT ME!
739 public char getGapCharacter()
741 return getAlignment().getGapCharacter();
750 public void setGapCharacter(char gap)
752 if (getAlignment() != null)
754 getAlignment().setGapCharacter(gap);
761 * @return DOCUMENT ME!
763 public ColumnSelection getColumnSelection()
774 public void setCurrentTree(NJTree tree)
782 * @return DOCUMENT ME!
784 public NJTree getCurrentTree()
792 * @return DOCUMENT ME!
794 public boolean getShowJVSuffix()
805 public void setShowJVSuffix(boolean b)
813 * @return DOCUMENT ME!
815 public boolean getShowAnnotation()
817 return showAnnotation;
826 public void setShowAnnotation(boolean b)
834 * @return DOCUMENT ME!
836 public boolean getScaleAboveWrapped()
838 return scaleAboveWrapped;
844 * @return DOCUMENT ME!
846 public boolean getScaleLeftWrapped()
848 return scaleLeftWrapped;
854 * @return DOCUMENT ME!
856 public boolean getScaleRightWrapped()
858 return scaleRightWrapped;
867 public void setScaleAboveWrapped(boolean b)
869 scaleAboveWrapped = b;
878 public void setScaleLeftWrapped(boolean b)
880 scaleLeftWrapped = b;
889 public void setScaleRightWrapped(boolean b)
891 scaleRightWrapped = b;
894 public void setDataset(boolean b)
899 public boolean isDataset()
904 public boolean getShowHiddenMarkers()
906 return showHiddenMarkers;
909 public void setShowHiddenMarkers(boolean show)
911 showHiddenMarkers = show;
915 * returns the visible column regions of the alignment
917 * @param selectedRegionOnly
918 * true to just return the contigs intersecting with the selected
922 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
924 int[] viscontigs = null;
925 int start = 0, end = 0;
926 if (selectedRegionOnly && selectionGroup != null)
928 start = selectionGroup.getStartRes();
929 end = selectionGroup.getEndRes() + 1;
933 end = alignment.getWidth();
935 viscontigs = colSel.getVisibleContigs(start, end);
940 * get hash of undo and redo list for the alignment
942 * @return long[] { historyList.hashCode, redoList.hashCode };
944 public long[] getUndoRedoHash()
947 if (historyList == null || redoList == null)
951 { historyList.hashCode(), this.redoList.hashCode() };
955 * test if a particular set of hashcodes are different to the hashcodes for
956 * the undo and redo list.
959 * the stored set of hashcodes as returned by getUndoRedoHash
960 * @return true if the hashcodes differ (ie the alignment has been edited) or
961 * the stored hashcode array differs in size
963 public boolean isUndoRedoHashModified(long[] undoredo)
965 if (undoredo == null)
969 long[] cstate = getUndoRedoHash();
970 if (cstate.length != undoredo.length)
975 for (int i = 0; i < cstate.length; i++)
977 if (cstate[i] != undoredo[i])
985 public boolean getCentreColumnLabels()
987 return centreColumnLabels;
990 public void setCentreColumnLabels(boolean centrecolumnlabels)
992 centreColumnLabels = centrecolumnlabels;
996 * enable or disable the display of Database Cross References in the sequence
999 public void setShowDbRefs(boolean show)
1006 * @return true if Database References are to be displayed on tooltips.
1008 public boolean isShowDbRefs()
1015 * @return true if Non-positional features are to be displayed on tooltips.
1017 public boolean isShowNpFeats()
1023 * enable or disable the display of Non-Positional sequence features in the
1024 * sequence ID tooltip
1028 public void setShowNpFeats(boolean show)
1035 * @return true if view has hidden rows
1037 public boolean hasHiddenRows()
1039 return hasHiddenRows;
1044 * @return true if view has hidden columns
1046 public boolean hasHiddenColumns()
1048 return hasHiddenColumns;
1052 * when set, view will scroll to show the highlighted position
1054 public boolean followHighlight = true;
1057 * @return true if view should scroll to show the highlighted region of a
1061 public boolean getFollowHighlight()
1063 return followHighlight;
1066 public boolean followSelection = true;
1069 * @return true if view selection should always follow the selections
1070 * broadcast by other selection sources
1072 public boolean getFollowSelection()
1074 return followSelection;
1077 public void sendSelection()
1079 jalview.structure.StructureSelectionManager
1080 .getStructureSelectionManager(Desktop.instance).sendSelection(
1081 new SequenceGroup(getSelectionGroup()),
1082 new ColumnSelection(getColumnSelection()), this);
1086 * return the alignPanel containing the given viewport. Use this to get the
1087 * components currently handling the given viewport.
1090 * @return null or an alignPanel guaranteed to have non-null alignFrame
1093 public AlignmentPanel getAlignPanel()
1095 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1096 .getSequenceSetId());
1097 AlignmentPanel ap = null;
1098 for (int p = 0; aps != null && p < aps.length; p++)
1100 if (aps[p].av == this)
1108 public boolean getSortByTree()
1113 public void setSortByTree(boolean sort)
1119 * synthesize a column selection if none exists so it covers the given
1120 * selection group. if wholewidth is false, no column selection is made if the
1121 * selection group covers the whole alignment width.
1126 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1130 && (sgs = sg.getStartRes()) >= 0
1131 && sg.getStartRes() <= (sge = sg.getEndRes())
1132 && (colSel == null || colSel.getSelected() == null || colSel
1133 .getSelected().size() == 0))
1135 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1142 colSel = new ColumnSelection();
1144 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1146 colSel.addElement(cspos);
1151 public StructureSelectionManager getStructureSelectionManager()
1153 return StructureSelectionManager
1154 .getStructureSelectionManager(Desktop.instance);
1160 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1161 * sequence in the alignment holds a reference to it
1163 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1165 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1166 for (PDBEntry pdb : pdbEntries)
1168 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1169 for (int i = 0; i < alignment.getHeight(); i++)
1171 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1176 for (int p = 0; p < pdbs.size(); p++)
1178 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1179 if (p1.getId().equals(pdb.getId()))
1181 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1188 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1190 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1193 public boolean isNormaliseSequenceLogo()
1195 return normaliseSequenceLogo;
1198 public void setNormaliseSequenceLogo(boolean state)
1200 normaliseSequenceLogo = state;
1205 * @return true if alignment characters should be displayed
1207 public boolean isValidCharWidth()
1209 return validCharWidth;
1212 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1214 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1216 return calcIdParams.get(calcId);
1219 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1220 boolean needsUpdate)
1222 calcIdParams.put(calcId, settings);
1223 // TODO: create a restart list to trigger any calculations that need to be
1224 // restarted after load
1225 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1228 Cache.log.debug("trigger update for " + calcId);