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 showSequenceFeatures = false;
100 boolean showAnnotation = true;
106 boolean validCharWidth;
112 boolean seqNameItalics;
114 NJTree currentTree = null;
116 boolean scaleAboveWrapped = false;
118 boolean scaleLeftWrapped = true;
120 boolean scaleRightWrapped = true;
122 boolean showHiddenMarkers = true;
124 boolean cursorMode = false;
127 * Keys are the feature types which are currently visible. Note: Values are
130 Hashtable featuresDisplayed = null;
132 boolean antiAlias = false;
134 Rectangle explodedPosition;
138 boolean gatherViewsHere = false;
140 Stack historyList = new Stack();
142 Stack redoList = new Stack();
144 int thresholdTextColour = 0;
146 Color textColour = Color.black;
148 Color textColour2 = Color.white;
150 boolean rightAlignIds = false;
153 * Creates a new AlignViewport object.
158 public AlignViewport(AlignmentI al)
165 * Create a new AlignViewport object with a specific sequence set ID
169 * (may be null - but potential for ambiguous constructor exception)
171 public AlignViewport(AlignmentI al, String seqsetid)
173 this(al, seqsetid, null);
176 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
178 sequenceSetID = seqsetid;
180 // TODO remove these once 2.4.VAMSAS release finished
181 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
183 Cache.log.debug("Setting viewport's sequence set id : "
186 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
188 Cache.log.debug("Setting viewport's view id : " + viewId);
195 * Create a new AlignViewport with hidden regions
199 * @param hiddenColumns
202 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
205 if (hiddenColumns != null)
207 this.colSel = hiddenColumns;
208 if (hiddenColumns.getHiddenColumns() != null
209 && hiddenColumns.getHiddenColumns().size() > 0)
211 hasHiddenColumns = true;
215 hasHiddenColumns = false;
222 * New viewport with hidden columns and an existing sequence set id
225 * @param hiddenColumns
229 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
232 this(al, hiddenColumns, seqsetid, null);
236 * New viewport with hidden columns and an existing sequence set id and viewid
239 * @param hiddenColumns
245 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
246 String seqsetid, String viewid)
248 sequenceSetID = seqsetid;
250 // TODO remove these once 2.4.VAMSAS release finished
251 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
253 Cache.log.debug("Setting viewport's sequence set id : "
256 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
258 Cache.log.debug("Setting viewport's view id : " + viewId);
261 if (hiddenColumns != null)
263 this.colSel = hiddenColumns;
264 if (hiddenColumns.getHiddenColumns() != null
265 && hiddenColumns.getHiddenColumns().size() > 0)
267 hasHiddenColumns = true;
271 hasHiddenColumns = false;
280 this.endRes = alignment.getWidth() - 1;
282 this.endSeq = alignment.getHeight() - 1;
284 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
286 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
287 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
289 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
290 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
291 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
293 setPadGaps(Cache.getDefault("PAD_GAPS", true));
294 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
295 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
297 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
298 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
299 String fontSize = Cache.getDefault("FONT_SIZE", "10");
301 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
305 if (fontStyle.equals("bold"))
309 else if (fontStyle.equals("italic"))
314 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
317 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
319 // We must set conservation and consensus before setting colour,
320 // as Blosum and Clustal require this to be done
321 if (hconsensus == null && !isDataset)
323 if (!alignment.isNucleotide())
325 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
326 showQuality = Cache.getDefault("SHOW_QUALITY", true);
327 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
330 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
332 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
333 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
335 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
336 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
337 consensus = new AlignmentAnnotation("Consensus", "PID",
338 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
339 consensus.hasText = true;
340 consensus.autoCalculated = true;
342 initAutoAnnotation();
343 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
345 globalColourScheme = ColourSchemeProperty.getColour(alignment,
346 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
348 if (globalColourScheme instanceof UserColourScheme)
350 globalColourScheme = UserDefinedColours.loadDefaultColours();
351 ((UserColourScheme) globalColourScheme).setThreshold(0,
352 getIgnoreGapsConsensus());
355 if (globalColourScheme != null)
357 globalColourScheme.setConsensus(hconsensus);
361 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
362 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
364 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
365 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
373 * features are displayed if true
375 public void setShowSequenceFeatures(boolean b)
377 showSequenceFeatures = b;
380 public boolean getShowSequenceFeatures()
382 return showSequenceFeatures;
386 * centre columnar annotation labels in displayed alignment annotation TODO:
387 * add to jalviewXML and annotation display settings
389 boolean centreColumnLabels = false;
391 private boolean showdbrefs;
393 private boolean shownpfeats;
395 // --------END Structure Conservation
398 * get the consensus sequence as displayed under the PID consensus annotation
401 * @return consensus sequence as a new sequence object
403 public SequenceI getConsensusSeq()
405 if (consensus == null)
407 updateConsensus(null);
409 if (consensus == null)
413 StringBuffer seqs = new StringBuffer();
414 for (int i = 0; i < consensus.annotations.length; i++)
416 if (consensus.annotations[i] != null)
418 if (consensus.annotations[i].description.charAt(0) == '[')
420 seqs.append(consensus.annotations[i].description.charAt(1));
424 seqs.append(consensus.annotations[i].displayCharacter);
429 SequenceI sq = new Sequence("Consensus", seqs.toString());
430 sq.setDescription("Percentage Identity Consensus "
431 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
438 * @return DOCUMENT ME!
440 public int getStartRes()
448 * @return DOCUMENT ME!
450 public int getEndRes()
458 * @return DOCUMENT ME!
460 public int getStartSeq()
471 public void setStartRes(int res)
482 public void setStartSeq(int seq)
493 public void setEndRes(int res)
495 if (res > (alignment.getWidth() - 1))
497 // log.System.out.println(" Corrected res from " + res + " to maximum " +
498 // (alignment.getWidth()-1));
499 res = alignment.getWidth() - 1;
516 public void setEndSeq(int seq)
518 if (seq > alignment.getHeight())
520 seq = alignment.getHeight();
534 * @return DOCUMENT ME!
536 public int getEndSeq()
547 public void setFont(Font f)
551 Container c = new Container();
553 java.awt.FontMetrics fm = c.getFontMetrics(font);
554 setCharHeight(fm.getHeight());
555 setCharWidth(fm.charWidth('M'));
556 validCharWidth = true;
562 * @return DOCUMENT ME!
564 public Font getFont()
575 public void setCharWidth(int w)
583 * @return DOCUMENT ME!
585 public int getCharWidth()
596 public void setCharHeight(int h)
604 * @return DOCUMENT ME!
606 public int getCharHeight()
617 public void setWrappedWidth(int w)
619 this.wrappedWidth = w;
625 * @return DOCUMENT ME!
627 public int getWrappedWidth()
635 * @return DOCUMENT ME!
637 public AlignmentI getAlignment()
648 public void setAlignment(AlignmentI align)
650 if (alignment != null && alignment.getCodonFrames() != null)
652 StructureSelectionManager.getStructureSelectionManager(
653 Desktop.instance).removeMappings(alignment.getCodonFrames());
655 this.alignment = align;
656 if (alignment != null && alignment.getCodonFrames() != null)
658 StructureSelectionManager.getStructureSelectionManager(
659 Desktop.instance).addMappings(alignment.getCodonFrames());
669 public void setWrapAlignment(boolean state)
671 wrapAlignment = state;
680 public void setShowText(boolean state)
691 public void setRenderGaps(boolean state)
699 * @return DOCUMENT ME!
701 public boolean getColourText()
703 return showColourText;
712 public void setColourText(boolean state)
714 showColourText = state;
723 public void setShowBoxes(boolean state)
731 * @return DOCUMENT ME!
733 public boolean getWrapAlignment()
735 return wrapAlignment;
741 * @return DOCUMENT ME!
743 public boolean getShowText()
751 * @return DOCUMENT ME!
753 public boolean getShowBoxes()
761 * @return DOCUMENT ME!
763 public char getGapCharacter()
765 return getAlignment().getGapCharacter();
774 public void setGapCharacter(char gap)
776 if (getAlignment() != null)
778 getAlignment().setGapCharacter(gap);
785 * @return DOCUMENT ME!
787 public ColumnSelection getColumnSelection()
798 public void setCurrentTree(NJTree tree)
806 * @return DOCUMENT ME!
808 public NJTree getCurrentTree()
816 * @return DOCUMENT ME!
818 public boolean getShowJVSuffix()
829 public void setShowJVSuffix(boolean b)
837 * @return DOCUMENT ME!
839 public boolean getShowAnnotation()
841 return showAnnotation;
850 public void setShowAnnotation(boolean b)
858 * @return DOCUMENT ME!
860 public boolean getScaleAboveWrapped()
862 return scaleAboveWrapped;
868 * @return DOCUMENT ME!
870 public boolean getScaleLeftWrapped()
872 return scaleLeftWrapped;
878 * @return DOCUMENT ME!
880 public boolean getScaleRightWrapped()
882 return scaleRightWrapped;
891 public void setScaleAboveWrapped(boolean b)
893 scaleAboveWrapped = b;
902 public void setScaleLeftWrapped(boolean b)
904 scaleLeftWrapped = b;
913 public void setScaleRightWrapped(boolean b)
915 scaleRightWrapped = b;
918 public void setDataset(boolean b)
923 public boolean isDataset()
928 public boolean getShowHiddenMarkers()
930 return showHiddenMarkers;
933 public void setShowHiddenMarkers(boolean show)
935 showHiddenMarkers = show;
939 * returns the visible column regions of the alignment
941 * @param selectedRegionOnly
942 * true to just return the contigs intersecting with the selected
946 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
948 int[] viscontigs = null;
949 int start = 0, end = 0;
950 if (selectedRegionOnly && selectionGroup != null)
952 start = selectionGroup.getStartRes();
953 end = selectionGroup.getEndRes() + 1;
957 end = alignment.getWidth();
959 viscontigs = colSel.getVisibleContigs(start, end);
964 * get hash of undo and redo list for the alignment
966 * @return long[] { historyList.hashCode, redoList.hashCode };
968 public long[] getUndoRedoHash()
971 if (historyList == null || redoList == null)
975 { historyList.hashCode(), this.redoList.hashCode() };
979 * test if a particular set of hashcodes are different to the hashcodes for
980 * the undo and redo list.
983 * the stored set of hashcodes as returned by getUndoRedoHash
984 * @return true if the hashcodes differ (ie the alignment has been edited) or
985 * the stored hashcode array differs in size
987 public boolean isUndoRedoHashModified(long[] undoredo)
989 if (undoredo == null)
993 long[] cstate = getUndoRedoHash();
994 if (cstate.length != undoredo.length)
999 for (int i = 0; i < cstate.length; i++)
1001 if (cstate[i] != undoredo[i])
1009 public boolean getCentreColumnLabels()
1011 return centreColumnLabels;
1014 public void setCentreColumnLabels(boolean centrecolumnlabels)
1016 centreColumnLabels = centrecolumnlabels;
1020 * enable or disable the display of Database Cross References in the sequence
1023 public void setShowDbRefs(boolean show)
1030 * @return true if Database References are to be displayed on tooltips.
1032 public boolean isShowDbRefs()
1039 * @return true if Non-positional features are to be displayed on tooltips.
1041 public boolean isShowNpFeats()
1047 * enable or disable the display of Non-Positional sequence features in the
1048 * sequence ID tooltip
1052 public void setShowNpFeats(boolean show)
1059 * @return true if view has hidden rows
1061 public boolean hasHiddenRows()
1063 return hasHiddenRows;
1068 * @return true if view has hidden columns
1070 public boolean hasHiddenColumns()
1072 return hasHiddenColumns;
1076 * when set, view will scroll to show the highlighted position
1078 public boolean followHighlight = true;
1081 * @return true if view should scroll to show the highlighted region of a
1085 public boolean getFollowHighlight()
1087 return followHighlight;
1090 public boolean followSelection = true;
1093 * @return true if view selection should always follow the selections
1094 * broadcast by other selection sources
1096 public boolean getFollowSelection()
1098 return followSelection;
1101 boolean showSeqFeaturesHeight;
1103 public void sendSelection()
1105 jalview.structure.StructureSelectionManager
1106 .getStructureSelectionManager(Desktop.instance).sendSelection(
1107 new SequenceGroup(getSelectionGroup()),
1108 new ColumnSelection(getColumnSelection()), this);
1111 public void setShowSequenceFeaturesHeight(boolean selected)
1113 showSeqFeaturesHeight = selected;
1116 public boolean getShowSequenceFeaturesHeight()
1118 return showSeqFeaturesHeight;
1122 * return the alignPanel containing the given viewport. Use this to get the
1123 * components currently handling the given viewport.
1126 * @return null or an alignPanel guaranteed to have non-null alignFrame
1129 public AlignmentPanel getAlignPanel()
1131 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1132 .getSequenceSetId());
1133 AlignmentPanel ap = null;
1134 for (int p = 0; aps != null && p < aps.length; p++)
1136 if (aps[p].av == this)
1144 public boolean getSortByTree()
1149 public void setSortByTree(boolean sort)
1155 * synthesize a column selection if none exists so it covers the given
1156 * selection group. if wholewidth is false, no column selection is made if the
1157 * selection group covers the whole alignment width.
1162 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1166 && (sgs = sg.getStartRes()) >= 0
1167 && sg.getStartRes() <= (sge = sg.getEndRes())
1168 && (colSel == null || colSel.getSelected() == null || colSel
1169 .getSelected().size() == 0))
1171 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1178 colSel = new ColumnSelection();
1180 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1182 colSel.addElement(cspos);
1187 public StructureSelectionManager getStructureSelectionManager()
1189 return StructureSelectionManager
1190 .getStructureSelectionManager(Desktop.instance);
1196 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1197 * sequence in the alignment holds a reference to it
1199 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1201 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1202 for (PDBEntry pdb : pdbEntries)
1204 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1205 for (int i = 0; i < alignment.getHeight(); i++)
1207 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1212 for (int p = 0; p < pdbs.size(); p++)
1214 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1215 if (p1.getId().equals(pdb.getId()))
1217 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1224 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1226 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1229 public boolean isNormaliseSequenceLogo()
1231 return normaliseSequenceLogo;
1234 public void setNormaliseSequenceLogo(boolean state)
1236 normaliseSequenceLogo = state;
1241 * @return true if alignment characters should be displayed
1243 public boolean isValidCharWidth()
1245 return validCharWidth;
1248 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1250 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1252 return calcIdParams.get(calcId);
1255 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1256 boolean needsUpdate)
1258 calcIdParams.put(calcId, settings);
1259 // TODO: create a restart list to trigger any calculations that need to be
1260 // restarted after load
1261 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1264 Cache.log.debug("trigger update for " + calcId);