2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * Jalview - A Sequence Alignment Editor and Viewer
20 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version 2
25 * of the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
38 import jalview.analysis.NJTree;
39 import jalview.api.AlignViewportI;
40 import jalview.bin.Cache;
41 import jalview.datamodel.AlignmentAnnotation;
42 import jalview.datamodel.AlignmentI;
43 import jalview.datamodel.Annotation;
44 import jalview.datamodel.ColumnSelection;
45 import jalview.datamodel.PDBEntry;
46 import jalview.datamodel.Sequence;
47 import jalview.datamodel.SequenceGroup;
48 import jalview.datamodel.SequenceI;
49 import jalview.schemes.ColourSchemeProperty;
50 import jalview.schemes.UserColourScheme;
51 import jalview.structure.SelectionSource;
52 import jalview.structure.StructureSelectionManager;
53 import jalview.structure.VamsasSource;
54 import jalview.viewmodel.AlignmentViewport;
55 import jalview.ws.params.AutoCalcSetting;
57 import java.awt.Color;
58 import java.awt.Container;
60 import java.awt.Rectangle;
61 import java.util.ArrayList;
62 import java.util.Hashtable;
63 import java.util.Stack;
64 import java.util.Vector;
70 * @version $Revision: 1.141 $
72 public class AlignViewport extends AlignmentViewport implements
73 SelectionSource, VamsasSource, AlignViewportI
83 boolean showJVSuffix = true;
85 boolean showText = true;
87 boolean showColourText = false;
89 boolean showBoxes = true;
91 boolean wrapAlignment = false;
93 boolean renderGaps = true;
95 boolean showSequenceFeatures = false;
97 boolean showAnnotation = true;
103 boolean validCharWidth;
109 boolean seqNameItalics;
111 NJTree currentTree = null;
113 boolean scaleAboveWrapped = false;
115 boolean scaleLeftWrapped = true;
117 boolean scaleRightWrapped = true;
119 boolean showHiddenMarkers = true;
121 boolean cursorMode = false;
124 * Keys are the feature types which are currently visible. Note: Values are
127 Hashtable featuresDisplayed = null;
129 boolean antiAlias = false;
131 Rectangle explodedPosition;
135 boolean gatherViewsHere = false;
137 Stack historyList = new Stack();
139 Stack redoList = new Stack();
141 int thresholdTextColour = 0;
143 Color textColour = Color.black;
145 Color textColour2 = Color.white;
147 boolean rightAlignIds = false;
150 * Creates a new AlignViewport object.
155 public AlignViewport(AlignmentI al)
162 * Create a new AlignViewport object with a specific sequence set ID
166 * (may be null - but potential for ambiguous constructor exception)
168 public AlignViewport(AlignmentI al, String seqsetid)
170 this(al, seqsetid, null);
173 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
175 sequenceSetID = seqsetid;
177 // TODO remove these once 2.4.VAMSAS release finished
178 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
180 Cache.log.debug("Setting viewport's sequence set id : "
183 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
185 Cache.log.debug("Setting viewport's view id : " + viewId);
192 * Create a new AlignViewport with hidden regions
196 * @param hiddenColumns
199 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
202 if (hiddenColumns != null)
204 this.colSel = hiddenColumns;
205 if (hiddenColumns.getHiddenColumns() != null
206 && hiddenColumns.getHiddenColumns().size() > 0)
208 hasHiddenColumns = true;
212 hasHiddenColumns = false;
219 * New viewport with hidden columns and an existing sequence set id
222 * @param hiddenColumns
226 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
229 this(al, hiddenColumns, seqsetid, null);
233 * New viewport with hidden columns and an existing sequence set id and viewid
236 * @param hiddenColumns
242 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
243 String seqsetid, String viewid)
245 sequenceSetID = seqsetid;
247 // TODO remove these once 2.4.VAMSAS release finished
248 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
250 Cache.log.debug("Setting viewport's sequence set id : "
253 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
255 Cache.log.debug("Setting viewport's view id : " + viewId);
258 if (hiddenColumns != null)
260 this.colSel = hiddenColumns;
261 if (hiddenColumns.getHiddenColumns() != null
262 && hiddenColumns.getHiddenColumns().size() > 0)
264 hasHiddenColumns = true;
268 hasHiddenColumns = false;
277 this.endRes = alignment.getWidth() - 1;
279 this.endSeq = alignment.getHeight() - 1;
281 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
283 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
284 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
286 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
287 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
288 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
290 setPadGaps(Cache.getDefault("PAD_GAPS", true));
291 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
292 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
294 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
295 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
296 String fontSize = Cache.getDefault("FONT_SIZE", "10");
298 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
302 if (fontStyle.equals("bold"))
306 else if (fontStyle.equals("italic"))
311 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
314 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
316 // We must set conservation and consensus before setting colour,
317 // as Blosum and Clustal require this to be done
318 if (hconsensus == null && !isDataset)
320 if (!alignment.isNucleotide())
322 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
323 showQuality = Cache.getDefault("SHOW_QUALITY", true);
324 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
327 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
329 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
330 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
332 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
333 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
334 consensus = new AlignmentAnnotation("Consensus", "PID",
335 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
336 consensus.hasText = true;
337 consensus.autoCalculated = true;
339 initAutoAnnotation();
340 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
342 globalColourScheme = ColourSchemeProperty.getColour(alignment,
343 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
345 if (globalColourScheme instanceof UserColourScheme)
347 globalColourScheme = UserDefinedColours.loadDefaultColours();
348 ((UserColourScheme) globalColourScheme).setThreshold(0,
349 getIgnoreGapsConsensus());
352 if (globalColourScheme != null)
354 globalColourScheme.setConsensus(hconsensus);
358 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
359 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
361 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
362 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
370 * features are displayed if true
372 public void setShowSequenceFeatures(boolean b)
374 showSequenceFeatures = b;
377 public boolean getShowSequenceFeatures()
379 return showSequenceFeatures;
383 * centre columnar annotation labels in displayed alignment annotation TODO:
384 * add to jalviewXML and annotation display settings
386 boolean centreColumnLabels = false;
388 private boolean showdbrefs;
390 private boolean shownpfeats;
392 // --------END Structure Conservation
395 * get the consensus sequence as displayed under the PID consensus annotation
398 * @return consensus sequence as a new sequence object
400 public SequenceI getConsensusSeq()
402 if (consensus == null)
404 updateConsensus(null);
406 if (consensus == null)
410 StringBuffer seqs = new StringBuffer();
411 for (int i = 0; i < consensus.annotations.length; i++)
413 if (consensus.annotations[i] != null)
415 if (consensus.annotations[i].description.charAt(0) == '[')
417 seqs.append(consensus.annotations[i].description.charAt(1));
421 seqs.append(consensus.annotations[i].displayCharacter);
426 SequenceI sq = new Sequence("Consensus", seqs.toString());
427 sq.setDescription("Percentage Identity Consensus "
428 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
435 * @return DOCUMENT ME!
437 public int getStartRes()
445 * @return DOCUMENT ME!
447 public int getEndRes()
455 * @return DOCUMENT ME!
457 public int getStartSeq()
468 public void setStartRes(int res)
479 public void setStartSeq(int seq)
490 public void setEndRes(int res)
492 if (res > (alignment.getWidth() - 1))
494 // log.System.out.println(" Corrected res from " + res + " to maximum " +
495 // (alignment.getWidth()-1));
496 res = alignment.getWidth() - 1;
513 public void setEndSeq(int seq)
515 if (seq > alignment.getHeight())
517 seq = alignment.getHeight();
531 * @return DOCUMENT ME!
533 public int getEndSeq()
544 public void setFont(Font f)
548 Container c = new Container();
550 java.awt.FontMetrics fm = c.getFontMetrics(font);
551 setCharHeight(fm.getHeight());
552 setCharWidth(fm.charWidth('M'));
553 validCharWidth = true;
559 * @return DOCUMENT ME!
561 public Font getFont()
572 public void setCharWidth(int w)
580 * @return DOCUMENT ME!
582 public int getCharWidth()
593 public void setCharHeight(int h)
601 * @return DOCUMENT ME!
603 public int getCharHeight()
614 public void setWrappedWidth(int w)
616 this.wrappedWidth = w;
622 * @return DOCUMENT ME!
624 public int getWrappedWidth()
632 * @return DOCUMENT ME!
634 public AlignmentI getAlignment()
645 public void setAlignment(AlignmentI align)
647 if (alignment != null && alignment.getCodonFrames() != null)
649 StructureSelectionManager.getStructureSelectionManager(
650 Desktop.instance).removeMappings(alignment.getCodonFrames());
652 this.alignment = align;
653 if (alignment != null && alignment.getCodonFrames() != null)
655 StructureSelectionManager.getStructureSelectionManager(
656 Desktop.instance).addMappings(alignment.getCodonFrames());
666 public void setWrapAlignment(boolean state)
668 wrapAlignment = state;
677 public void setShowText(boolean state)
688 public void setRenderGaps(boolean state)
696 * @return DOCUMENT ME!
698 public boolean getColourText()
700 return showColourText;
709 public void setColourText(boolean state)
711 showColourText = state;
720 public void setShowBoxes(boolean state)
728 * @return DOCUMENT ME!
730 public boolean getWrapAlignment()
732 return wrapAlignment;
738 * @return DOCUMENT ME!
740 public boolean getShowText()
748 * @return DOCUMENT ME!
750 public boolean getShowBoxes()
758 * @return DOCUMENT ME!
760 public char getGapCharacter()
762 return getAlignment().getGapCharacter();
771 public void setGapCharacter(char gap)
773 if (getAlignment() != null)
775 getAlignment().setGapCharacter(gap);
782 * @return DOCUMENT ME!
784 public ColumnSelection getColumnSelection()
795 public void setCurrentTree(NJTree tree)
803 * @return DOCUMENT ME!
805 public NJTree getCurrentTree()
813 * @return DOCUMENT ME!
815 public boolean getShowJVSuffix()
826 public void setShowJVSuffix(boolean b)
834 * @return DOCUMENT ME!
836 public boolean getShowAnnotation()
838 return showAnnotation;
847 public void setShowAnnotation(boolean b)
855 * @return DOCUMENT ME!
857 public boolean getScaleAboveWrapped()
859 return scaleAboveWrapped;
865 * @return DOCUMENT ME!
867 public boolean getScaleLeftWrapped()
869 return scaleLeftWrapped;
875 * @return DOCUMENT ME!
877 public boolean getScaleRightWrapped()
879 return scaleRightWrapped;
888 public void setScaleAboveWrapped(boolean b)
890 scaleAboveWrapped = b;
899 public void setScaleLeftWrapped(boolean b)
901 scaleLeftWrapped = b;
910 public void setScaleRightWrapped(boolean b)
912 scaleRightWrapped = b;
915 public void setDataset(boolean b)
920 public boolean isDataset()
925 public boolean getShowHiddenMarkers()
927 return showHiddenMarkers;
930 public void setShowHiddenMarkers(boolean show)
932 showHiddenMarkers = show;
936 * returns the visible column regions of the alignment
938 * @param selectedRegionOnly
939 * true to just return the contigs intersecting with the selected
943 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
945 int[] viscontigs = null;
946 int start = 0, end = 0;
947 if (selectedRegionOnly && selectionGroup != null)
949 start = selectionGroup.getStartRes();
950 end = selectionGroup.getEndRes() + 1;
954 end = alignment.getWidth();
956 viscontigs = colSel.getVisibleContigs(start, end);
961 * get hash of undo and redo list for the alignment
963 * @return long[] { historyList.hashCode, redoList.hashCode };
965 public long[] getUndoRedoHash()
968 if (historyList == null || redoList == null)
972 { historyList.hashCode(), this.redoList.hashCode() };
976 * test if a particular set of hashcodes are different to the hashcodes for
977 * the undo and redo list.
980 * the stored set of hashcodes as returned by getUndoRedoHash
981 * @return true if the hashcodes differ (ie the alignment has been edited) or
982 * the stored hashcode array differs in size
984 public boolean isUndoRedoHashModified(long[] undoredo)
986 if (undoredo == null)
990 long[] cstate = getUndoRedoHash();
991 if (cstate.length != undoredo.length)
996 for (int i = 0; i < cstate.length; i++)
998 if (cstate[i] != undoredo[i])
1006 public boolean getCentreColumnLabels()
1008 return centreColumnLabels;
1011 public void setCentreColumnLabels(boolean centrecolumnlabels)
1013 centreColumnLabels = centrecolumnlabels;
1018 * enable or disable the display of Database Cross References in the sequence
1021 public void setShowDbRefs(boolean show)
1028 * @return true if Database References are to be displayed on tooltips.
1030 public boolean isShowDbRefs()
1037 * @return true if Non-positional features are to be displayed on tooltips.
1039 public boolean isShowNpFeats()
1045 * enable or disable the display of Non-Positional sequence features in the
1046 * sequence ID tooltip
1050 public void setShowNpFeats(boolean show)
1057 * @return true if view has hidden rows
1059 public boolean hasHiddenRows()
1061 return hasHiddenRows;
1066 * @return true if view has hidden columns
1068 public boolean hasHiddenColumns()
1070 return hasHiddenColumns;
1074 * when set, view will scroll to show the highlighted position
1076 public boolean followHighlight = true;
1079 * @return true if view should scroll to show the highlighted region of a
1083 public boolean getFollowHighlight()
1085 return followHighlight;
1088 public boolean followSelection = true;
1091 * @return true if view selection should always follow the selections
1092 * broadcast by other selection sources
1094 public boolean getFollowSelection()
1096 return followSelection;
1099 boolean showSeqFeaturesHeight;
1101 public void sendSelection()
1103 jalview.structure.StructureSelectionManager
1104 .getStructureSelectionManager(Desktop.instance).sendSelection(
1105 new SequenceGroup(getSelectionGroup()),
1106 new ColumnSelection(getColumnSelection()), this);
1109 public void setShowSequenceFeaturesHeight(boolean selected)
1111 showSeqFeaturesHeight = selected;
1114 public boolean getShowSequenceFeaturesHeight()
1116 return showSeqFeaturesHeight;
1120 * return the alignPanel containing the given viewport. Use this to get the
1121 * components currently handling the given viewport.
1124 * @return null or an alignPanel guaranteed to have non-null alignFrame
1127 public AlignmentPanel getAlignPanel()
1129 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1130 .getSequenceSetId());
1131 AlignmentPanel ap = null;
1132 for (int p = 0; aps != null && p < aps.length; p++)
1134 if (aps[p].av == this)
1142 public boolean getSortByTree()
1147 public void setSortByTree(boolean sort)
1153 * synthesize a column selection if none exists so it covers the given
1154 * selection group. if wholewidth is false, no column selection is made if the
1155 * selection group covers the whole alignment width.
1160 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1164 && (sgs = sg.getStartRes()) >= 0
1165 && sg.getStartRes() <= (sge = sg.getEndRes())
1166 && (colSel == null || colSel.getSelected() == null || colSel
1167 .getSelected().size() == 0))
1169 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1176 colSel = new ColumnSelection();
1178 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1180 colSel.addElement(cspos);
1185 public StructureSelectionManager getStructureSelectionManager()
1187 return StructureSelectionManager
1188 .getStructureSelectionManager(Desktop.instance);
1194 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1195 * sequence in the alignment holds a reference to it
1197 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1199 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1200 for (PDBEntry pdb : pdbEntries)
1202 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1203 for (int i = 0; i < alignment.getHeight(); i++)
1205 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1210 for (int p = 0; p < pdbs.size(); p++)
1212 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1213 if (p1.getId().equals(pdb.getId()))
1215 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1222 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1224 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1227 public boolean isNormaliseSequenceLogo()
1229 return normaliseSequenceLogo;
1232 public void setNormaliseSequenceLogo(boolean state)
1234 normaliseSequenceLogo = state;
1239 * @return true if alignment characters should be displayed
1241 public boolean isValidCharWidth()
1243 return validCharWidth;
1246 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1248 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1250 return calcIdParams.get(calcId);
1253 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1254 boolean needsUpdate)
1256 calcIdParams.put(calcId, settings);
1257 // TODO: create a restart list to trigger any calculations that need to be
1258 // restarted after load
1259 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1262 Cache.log.debug("trigger update for " + calcId);