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.datamodel.AlignmentAnnotation;
46 import jalview.datamodel.AlignmentI;
47 import jalview.datamodel.Annotation;
48 import jalview.datamodel.ColumnSelection;
49 import jalview.datamodel.PDBEntry;
50 import jalview.datamodel.Sequence;
51 import jalview.datamodel.SequenceGroup;
52 import jalview.datamodel.SequenceI;
53 import jalview.schemes.ColourSchemeProperty;
54 import jalview.schemes.UserColourScheme;
55 import jalview.structure.SelectionSource;
56 import jalview.structure.StructureSelectionManager;
57 import jalview.structure.VamsasSource;
58 import jalview.viewmodel.AlignmentViewport;
59 import jalview.ws.params.AutoCalcSetting;
61 import java.awt.Color;
62 import java.awt.Container;
64 import java.awt.Rectangle;
65 import java.util.ArrayList;
66 import java.util.Hashtable;
67 import java.util.Stack;
68 import java.util.Vector;
74 * @version $Revision: 1.141 $
76 public class AlignViewport extends AlignmentViewport implements
77 SelectionSource, VamsasSource, AlignViewportI
87 boolean showJVSuffix = true;
89 boolean showText = true;
91 boolean showColourText = false;
93 boolean showBoxes = true;
95 boolean wrapAlignment = false;
97 boolean renderGaps = true;
99 boolean showSequenceFeatures = false;
101 boolean showAnnotation = true;
103 SequenceAnnotationOrder sortAnnotationsBy = null;
109 boolean validCharWidth;
115 boolean seqNameItalics;
117 NJTree currentTree = null;
119 boolean scaleAboveWrapped = false;
121 boolean scaleLeftWrapped = true;
123 boolean scaleRightWrapped = true;
125 boolean showHiddenMarkers = true;
127 boolean cursorMode = false;
130 * Keys are the feature types which are currently visible. Note: Values are
133 Hashtable featuresDisplayed = null;
135 boolean antiAlias = false;
137 Rectangle explodedPosition;
141 boolean gatherViewsHere = false;
143 Stack historyList = new Stack();
145 Stack redoList = new Stack();
147 int thresholdTextColour = 0;
149 Color textColour = Color.black;
151 Color textColour2 = Color.white;
153 boolean rightAlignIds = false;
156 * Creates a new AlignViewport object.
161 public AlignViewport(AlignmentI al)
168 * Create a new AlignViewport object with a specific sequence set ID
172 * (may be null - but potential for ambiguous constructor exception)
174 public AlignViewport(AlignmentI al, String seqsetid)
176 this(al, seqsetid, null);
179 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
181 sequenceSetID = seqsetid;
183 // TODO remove these once 2.4.VAMSAS release finished
184 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
186 Cache.log.debug("Setting viewport's sequence set id : "
189 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
191 Cache.log.debug("Setting viewport's view id : " + viewId);
198 * Create a new AlignViewport with hidden regions
202 * @param hiddenColumns
205 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
208 if (hiddenColumns != null)
210 this.colSel = hiddenColumns;
211 if (hiddenColumns.getHiddenColumns() != null
212 && hiddenColumns.getHiddenColumns().size() > 0)
214 hasHiddenColumns = true;
218 hasHiddenColumns = false;
225 * New viewport with hidden columns and an existing sequence set id
228 * @param hiddenColumns
232 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
235 this(al, hiddenColumns, seqsetid, null);
239 * New viewport with hidden columns and an existing sequence set id and viewid
242 * @param hiddenColumns
248 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
249 String seqsetid, String viewid)
251 sequenceSetID = seqsetid;
253 // TODO remove these once 2.4.VAMSAS release finished
254 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
256 Cache.log.debug("Setting viewport's sequence set id : "
259 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
261 Cache.log.debug("Setting viewport's view id : " + viewId);
264 if (hiddenColumns != null)
266 this.colSel = hiddenColumns;
267 if (hiddenColumns.getHiddenColumns() != null
268 && hiddenColumns.getHiddenColumns().size() > 0)
270 hasHiddenColumns = true;
274 hasHiddenColumns = false;
283 this.endRes = alignment.getWidth() - 1;
285 this.endSeq = alignment.getHeight() - 1;
287 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
289 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
290 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
292 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
293 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
294 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
296 setPadGaps(Cache.getDefault("PAD_GAPS", true));
297 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
298 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
300 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
301 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
302 String fontSize = Cache.getDefault("FONT_SIZE", "10");
304 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
308 if (fontStyle.equals("bold"))
312 else if (fontStyle.equals("italic"))
317 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
320 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
322 // We must set conservation and consensus before setting colour,
323 // as Blosum and Clustal require this to be done
324 if (hconsensus == null && !isDataset)
326 if (!alignment.isNucleotide())
328 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
329 showQuality = Cache.getDefault("SHOW_QUALITY", true);
330 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
333 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
335 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
336 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
338 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
339 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
340 consensus = new AlignmentAnnotation("Consensus", "PID",
341 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
342 consensus.hasText = true;
343 consensus.autoCalculated = true;
345 initAutoAnnotation();
346 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
348 globalColourScheme = ColourSchemeProperty.getColour(alignment,
349 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
351 if (globalColourScheme instanceof UserColourScheme)
353 globalColourScheme = UserDefinedColours.loadDefaultColours();
354 ((UserColourScheme) globalColourScheme).setThreshold(0,
355 getIgnoreGapsConsensus());
358 if (globalColourScheme != null)
360 globalColourScheme.setConsensus(hconsensus);
364 wrapAlignment = Cache.getDefault("WRAP_ALIGNMENT", false);
365 showUnconserved = Cache.getDefault("SHOW_UNCONSERVED", false);
366 sortByTree = Cache.getDefault("SORT_BY_TREE", false);
367 followSelection = Cache.getDefault("FOLLOW_SELECTIONS", true);
368 sortAnnotationsBy = SequenceAnnotationOrder.valueOf(Cache.getDefault(
369 Preferences.SORT_ANNOTATIONS,
370 SequenceAnnotationOrder.NONE.name()));
371 showAutocalculatedAbove = Cache.getDefault(
372 Preferences.SHOW_AUTOCALC_ABOVE, false);
379 * features are displayed if true
381 public void setShowSequenceFeatures(boolean b)
383 showSequenceFeatures = b;
386 public boolean getShowSequenceFeatures()
388 return showSequenceFeatures;
392 * centre columnar annotation labels in displayed alignment annotation TODO:
393 * add to jalviewXML and annotation display settings
395 boolean centreColumnLabels = false;
397 private boolean showdbrefs;
399 private boolean shownpfeats;
401 // --------END Structure Conservation
404 * get the consensus sequence as displayed under the PID consensus annotation
407 * @return consensus sequence as a new sequence object
409 public SequenceI getConsensusSeq()
411 if (consensus == null)
413 updateConsensus(null);
415 if (consensus == null)
419 StringBuffer seqs = new StringBuffer();
420 for (int i = 0; i < consensus.annotations.length; i++)
422 if (consensus.annotations[i] != null)
424 if (consensus.annotations[i].description.charAt(0) == '[')
426 seqs.append(consensus.annotations[i].description.charAt(1));
430 seqs.append(consensus.annotations[i].displayCharacter);
435 SequenceI sq = new Sequence("Consensus", seqs.toString());
436 sq.setDescription("Percentage Identity Consensus "
437 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
444 * @return DOCUMENT ME!
446 public int getStartRes()
454 * @return DOCUMENT ME!
456 public int getEndRes()
464 * @return DOCUMENT ME!
466 public int getStartSeq()
477 public void setStartRes(int res)
488 public void setStartSeq(int seq)
499 public void setEndRes(int res)
501 if (res > (alignment.getWidth() - 1))
503 // log.System.out.println(" Corrected res from " + res + " to maximum " +
504 // (alignment.getWidth()-1));
505 res = alignment.getWidth() - 1;
522 public void setEndSeq(int seq)
524 if (seq > alignment.getHeight())
526 seq = alignment.getHeight();
540 * @return DOCUMENT ME!
542 public int getEndSeq()
553 public void setFont(Font f)
557 Container c = new Container();
559 java.awt.FontMetrics fm = c.getFontMetrics(font);
560 setCharHeight(fm.getHeight());
561 setCharWidth(fm.charWidth('M'));
562 validCharWidth = true;
568 * @return DOCUMENT ME!
570 public Font getFont()
581 public void setCharWidth(int w)
589 * @return DOCUMENT ME!
591 public int getCharWidth()
602 public void setCharHeight(int h)
610 * @return DOCUMENT ME!
612 public int getCharHeight()
623 public void setWrappedWidth(int w)
625 this.wrappedWidth = w;
631 * @return DOCUMENT ME!
633 public int getWrappedWidth()
641 * @return DOCUMENT ME!
643 public AlignmentI getAlignment()
654 public void setAlignment(AlignmentI align)
656 if (alignment != null && alignment.getCodonFrames() != null)
658 StructureSelectionManager.getStructureSelectionManager(
659 Desktop.instance).removeMappings(alignment.getCodonFrames());
661 this.alignment = align;
662 if (alignment != null && alignment.getCodonFrames() != null)
664 StructureSelectionManager.getStructureSelectionManager(
665 Desktop.instance).addMappings(alignment.getCodonFrames());
675 public void setWrapAlignment(boolean state)
677 wrapAlignment = state;
686 public void setShowText(boolean state)
697 public void setRenderGaps(boolean state)
705 * @return DOCUMENT ME!
707 public boolean getColourText()
709 return showColourText;
718 public void setColourText(boolean state)
720 showColourText = state;
729 public void setShowBoxes(boolean state)
737 * @return DOCUMENT ME!
739 public boolean getWrapAlignment()
741 return wrapAlignment;
747 * @return DOCUMENT ME!
749 public boolean getShowText()
757 * @return DOCUMENT ME!
759 public boolean getShowBoxes()
767 * @return DOCUMENT ME!
769 public char getGapCharacter()
771 return getAlignment().getGapCharacter();
780 public void setGapCharacter(char gap)
782 if (getAlignment() != null)
784 getAlignment().setGapCharacter(gap);
791 * @return DOCUMENT ME!
793 public ColumnSelection getColumnSelection()
804 public void setCurrentTree(NJTree tree)
812 * @return DOCUMENT ME!
814 public NJTree getCurrentTree()
822 * @return DOCUMENT ME!
824 public boolean getShowJVSuffix()
835 public void setShowJVSuffix(boolean b)
843 * @return DOCUMENT ME!
845 public boolean getShowAnnotation()
847 return showAnnotation;
856 public void setShowAnnotation(boolean b)
864 * @return DOCUMENT ME!
866 public boolean getScaleAboveWrapped()
868 return scaleAboveWrapped;
874 * @return DOCUMENT ME!
876 public boolean getScaleLeftWrapped()
878 return scaleLeftWrapped;
884 * @return DOCUMENT ME!
886 public boolean getScaleRightWrapped()
888 return scaleRightWrapped;
897 public void setScaleAboveWrapped(boolean b)
899 scaleAboveWrapped = b;
908 public void setScaleLeftWrapped(boolean b)
910 scaleLeftWrapped = b;
919 public void setScaleRightWrapped(boolean b)
921 scaleRightWrapped = b;
924 public void setDataset(boolean b)
929 public boolean isDataset()
934 public boolean getShowHiddenMarkers()
936 return showHiddenMarkers;
939 public void setShowHiddenMarkers(boolean show)
941 showHiddenMarkers = show;
945 * returns the visible column regions of the alignment
947 * @param selectedRegionOnly
948 * true to just return the contigs intersecting with the selected
952 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
954 int[] viscontigs = null;
955 int start = 0, end = 0;
956 if (selectedRegionOnly && selectionGroup != null)
958 start = selectionGroup.getStartRes();
959 end = selectionGroup.getEndRes() + 1;
963 end = alignment.getWidth();
965 viscontigs = colSel.getVisibleContigs(start, end);
970 * get hash of undo and redo list for the alignment
972 * @return long[] { historyList.hashCode, redoList.hashCode };
974 public long[] getUndoRedoHash()
977 if (historyList == null || redoList == null)
983 { historyList.hashCode(), this.redoList.hashCode() };
987 * test if a particular set of hashcodes are different to the hashcodes for
988 * the undo and redo list.
991 * the stored set of hashcodes as returned by getUndoRedoHash
992 * @return true if the hashcodes differ (ie the alignment has been edited) or
993 * the stored hashcode array differs in size
995 public boolean isUndoRedoHashModified(long[] undoredo)
997 if (undoredo == null)
1001 long[] cstate = getUndoRedoHash();
1002 if (cstate.length != undoredo.length)
1007 for (int i = 0; i < cstate.length; i++)
1009 if (cstate[i] != undoredo[i])
1017 public boolean getCentreColumnLabels()
1019 return centreColumnLabels;
1022 public void setCentreColumnLabels(boolean centrecolumnlabels)
1024 centreColumnLabels = centrecolumnlabels;
1028 * enable or disable the display of Database Cross References in the sequence
1031 public void setShowDbRefs(boolean show)
1038 * @return true if Database References are to be displayed on tooltips.
1040 public boolean isShowDbRefs()
1047 * @return true if Non-positional features are to be displayed on tooltips.
1049 public boolean isShowNpFeats()
1055 * enable or disable the display of Non-Positional sequence features in the
1056 * sequence ID tooltip
1060 public void setShowNpFeats(boolean show)
1067 * @return true if view has hidden rows
1069 public boolean hasHiddenRows()
1071 return hasHiddenRows;
1076 * @return true if view has hidden columns
1078 public boolean hasHiddenColumns()
1080 return hasHiddenColumns;
1084 * when set, view will scroll to show the highlighted position
1086 public boolean followHighlight = true;
1089 * @return true if view should scroll to show the highlighted region of a
1093 public boolean getFollowHighlight()
1095 return followHighlight;
1098 public boolean followSelection = true;
1101 * @return true if view selection should always follow the selections
1102 * broadcast by other selection sources
1104 public boolean getFollowSelection()
1106 return followSelection;
1109 boolean showSeqFeaturesHeight;
1111 public void sendSelection()
1113 jalview.structure.StructureSelectionManager
1114 .getStructureSelectionManager(Desktop.instance).sendSelection(
1115 new SequenceGroup(getSelectionGroup()),
1116 new ColumnSelection(getColumnSelection()), this);
1119 public void setShowSequenceFeaturesHeight(boolean selected)
1121 showSeqFeaturesHeight = selected;
1124 public boolean getShowSequenceFeaturesHeight()
1126 return showSeqFeaturesHeight;
1130 * return the alignPanel containing the given viewport. Use this to get the
1131 * components currently handling the given viewport.
1134 * @return null or an alignPanel guaranteed to have non-null alignFrame
1137 public AlignmentPanel getAlignPanel()
1139 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1140 .getSequenceSetId());
1141 AlignmentPanel ap = null;
1142 for (int p = 0; aps != null && p < aps.length; p++)
1144 if (aps[p].av == this)
1152 public boolean getSortByTree()
1157 public void setSortByTree(boolean sort)
1163 * synthesize a column selection if none exists so it covers the given
1164 * selection group. if wholewidth is false, no column selection is made if the
1165 * selection group covers the whole alignment width.
1170 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1174 && (sgs = sg.getStartRes()) >= 0
1175 && sg.getStartRes() <= (sge = sg.getEndRes())
1176 && (colSel == null || colSel.getSelected() == null || colSel
1177 .getSelected().size() == 0))
1179 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1186 colSel = new ColumnSelection();
1188 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1190 colSel.addElement(cspos);
1195 public StructureSelectionManager getStructureSelectionManager()
1197 return StructureSelectionManager
1198 .getStructureSelectionManager(Desktop.instance);
1204 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1205 * sequence in the alignment holds a reference to it
1207 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1209 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1210 for (PDBEntry pdb : pdbEntries)
1212 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1213 for (int i = 0; i < alignment.getHeight(); i++)
1215 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1222 for (int p = 0; p < pdbs.size(); p++)
1224 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1225 if (p1.getId().equals(pdb.getId()))
1227 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1236 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1238 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1241 public boolean isNormaliseSequenceLogo()
1243 return normaliseSequenceLogo;
1246 public void setNormaliseSequenceLogo(boolean state)
1248 normaliseSequenceLogo = state;
1253 * @return true if alignment characters should be displayed
1255 public boolean isValidCharWidth()
1257 return validCharWidth;
1260 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1262 private boolean showAutocalculatedAbove;
1264 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1266 return calcIdParams.get(calcId);
1269 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1270 boolean needsUpdate)
1272 calcIdParams.put(calcId, settings);
1273 // TODO: create a restart list to trigger any calculations that need to be
1274 // restarted after load
1275 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1278 Cache.log.debug("trigger update for " + calcId);
1282 protected SequenceAnnotationOrder getSortAnnotationsBy()
1284 return sortAnnotationsBy;
1287 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1289 this.sortAnnotationsBy = sortAnnotationsBy;
1292 protected boolean isShowAutocalculatedAbove()
1294 return showAutocalculatedAbove;
1297 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1299 this.showAutocalculatedAbove = showAutocalculatedAbove;