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;
126 boolean antiAlias = false;
128 Rectangle explodedPosition;
132 boolean gatherViewsHere = false;
134 Stack historyList = new Stack();
136 Stack redoList = new Stack();
138 int thresholdTextColour = 0;
140 Color textColour = Color.black;
142 Color textColour2 = Color.white;
144 boolean rightAlignIds = false;
147 * Creates a new AlignViewport object.
152 public AlignViewport(AlignmentI al)
159 * Create a new AlignViewport object with a specific sequence set ID
163 * (may be null - but potential for ambiguous constructor exception)
165 public AlignViewport(AlignmentI al, String seqsetid)
167 this(al, seqsetid, null);
170 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
172 sequenceSetID = seqsetid;
174 // TODO remove these once 2.4.VAMSAS release finished
175 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
177 Cache.log.debug("Setting viewport's sequence set id : "
180 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
182 Cache.log.debug("Setting viewport's view id : " + viewId);
189 * Create a new AlignViewport with hidden regions
193 * @param hiddenColumns
196 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
199 if (hiddenColumns != null)
201 this.colSel = hiddenColumns;
202 if (hiddenColumns.getHiddenColumns() != null
203 && hiddenColumns.getHiddenColumns().size() > 0)
205 hasHiddenColumns = true;
209 hasHiddenColumns = false;
216 * New viewport with hidden columns and an existing sequence set id
219 * @param hiddenColumns
223 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
226 this(al, hiddenColumns, seqsetid, null);
230 * New viewport with hidden columns and an existing sequence set id and viewid
233 * @param hiddenColumns
239 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
240 String seqsetid, String viewid)
242 sequenceSetID = seqsetid;
244 // TODO remove these once 2.4.VAMSAS release finished
245 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
247 Cache.log.debug("Setting viewport's sequence set id : "
250 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
252 Cache.log.debug("Setting viewport's view id : " + viewId);
255 if (hiddenColumns != null)
257 this.colSel = hiddenColumns;
258 if (hiddenColumns.getHiddenColumns() != null
259 && hiddenColumns.getHiddenColumns().size() > 0)
261 hasHiddenColumns = true;
265 hasHiddenColumns = false;
274 this.endRes = alignment.getWidth() - 1;
276 this.endSeq = alignment.getHeight() - 1;
278 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
280 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
281 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
283 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
284 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
285 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
287 setPadGaps(Cache.getDefault("PAD_GAPS", true));
288 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
289 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
291 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
292 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
293 String fontSize = Cache.getDefault("FONT_SIZE", "10");
295 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
299 if (fontStyle.equals("bold"))
303 else if (fontStyle.equals("italic"))
308 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
311 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
313 // We must set conservation and consensus before setting colour,
314 // as Blosum and Clustal require this to be done
315 if (hconsensus == null && !isDataset)
317 if (!alignment.isNucleotide())
319 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
320 showQuality = Cache.getDefault("SHOW_QUALITY", true);
321 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
324 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
326 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
327 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
329 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
330 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
331 consensus = new AlignmentAnnotation("Consensus", "PID",
332 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
333 consensus.hasText = true;
334 consensus.autoCalculated = true;
336 initAutoAnnotation();
337 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
339 globalColourScheme = ColourSchemeProperty.getColour(alignment,
340 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
342 if (globalColourScheme instanceof UserColourScheme)
344 globalColourScheme = UserDefinedColours.loadDefaultColours();
345 ((UserColourScheme) globalColourScheme).setThreshold(0,
346 getIgnoreGapsConsensus());
349 if (globalColourScheme != null)
351 globalColourScheme.setConsensus(hconsensus);
355 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
356 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
358 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
359 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
367 * features are displayed if true
369 public void setShowSequenceFeatures(boolean b)
371 showSequenceFeatures = b;
374 public boolean getShowSequenceFeatures()
376 return showSequenceFeatures;
380 * centre columnar annotation labels in displayed alignment annotation TODO:
381 * add to jalviewXML and annotation display settings
383 boolean centreColumnLabels = false;
385 private boolean showdbrefs;
387 private boolean shownpfeats;
389 // --------END Structure Conservation
392 * get the consensus sequence as displayed under the PID consensus annotation
395 * @return consensus sequence as a new sequence object
397 public SequenceI getConsensusSeq()
399 if (consensus == null)
401 updateConsensus(null);
403 if (consensus == null)
407 StringBuffer seqs = new StringBuffer();
408 for (int i = 0; i < consensus.annotations.length; i++)
410 if (consensus.annotations[i] != null)
412 if (consensus.annotations[i].description.charAt(0) == '[')
414 seqs.append(consensus.annotations[i].description.charAt(1));
418 seqs.append(consensus.annotations[i].displayCharacter);
423 SequenceI sq = new Sequence("Consensus", seqs.toString());
424 sq.setDescription("Percentage Identity Consensus "
425 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
432 * @return DOCUMENT ME!
434 public int getStartRes()
442 * @return DOCUMENT ME!
444 public int getEndRes()
452 * @return DOCUMENT ME!
454 public int getStartSeq()
465 public void setStartRes(int res)
476 public void setStartSeq(int seq)
487 public void setEndRes(int res)
489 if (res > (alignment.getWidth() - 1))
491 // log.System.out.println(" Corrected res from " + res + " to maximum " +
492 // (alignment.getWidth()-1));
493 res = alignment.getWidth() - 1;
510 public void setEndSeq(int seq)
512 if (seq > alignment.getHeight())
514 seq = alignment.getHeight();
528 * @return DOCUMENT ME!
530 public int getEndSeq()
541 public void setFont(Font f)
545 Container c = new Container();
547 java.awt.FontMetrics fm = c.getFontMetrics(font);
548 setCharHeight(fm.getHeight());
549 setCharWidth(fm.charWidth('M'));
550 validCharWidth = true;
556 * @return DOCUMENT ME!
558 public Font getFont()
569 public void setCharWidth(int w)
577 * @return DOCUMENT ME!
579 public int getCharWidth()
590 public void setCharHeight(int h)
598 * @return DOCUMENT ME!
600 public int getCharHeight()
611 public void setWrappedWidth(int w)
613 this.wrappedWidth = w;
619 * @return DOCUMENT ME!
621 public int getWrappedWidth()
629 * @return DOCUMENT ME!
631 public AlignmentI getAlignment()
642 public void setAlignment(AlignmentI align)
644 if (alignment != null && alignment.getCodonFrames() != null)
646 StructureSelectionManager.getStructureSelectionManager(
647 Desktop.instance).removeMappings(alignment.getCodonFrames());
649 this.alignment = align;
650 if (alignment != null && alignment.getCodonFrames() != null)
652 StructureSelectionManager.getStructureSelectionManager(
653 Desktop.instance).addMappings(alignment.getCodonFrames());
663 public void setWrapAlignment(boolean state)
665 wrapAlignment = state;
674 public void setShowText(boolean state)
685 public void setRenderGaps(boolean state)
693 * @return DOCUMENT ME!
695 public boolean getColourText()
697 return showColourText;
706 public void setColourText(boolean state)
708 showColourText = state;
717 public void setShowBoxes(boolean state)
725 * @return DOCUMENT ME!
727 public boolean getWrapAlignment()
729 return wrapAlignment;
735 * @return DOCUMENT ME!
737 public boolean getShowText()
745 * @return DOCUMENT ME!
747 public boolean getShowBoxes()
755 * @return DOCUMENT ME!
757 public char getGapCharacter()
759 return getAlignment().getGapCharacter();
768 public void setGapCharacter(char gap)
770 if (getAlignment() != null)
772 getAlignment().setGapCharacter(gap);
779 * @return DOCUMENT ME!
781 public ColumnSelection getColumnSelection()
792 public void setCurrentTree(NJTree tree)
800 * @return DOCUMENT ME!
802 public NJTree getCurrentTree()
810 * @return DOCUMENT ME!
812 public boolean getShowJVSuffix()
823 public void setShowJVSuffix(boolean b)
831 * @return DOCUMENT ME!
833 public boolean getShowAnnotation()
835 return showAnnotation;
844 public void setShowAnnotation(boolean b)
852 * @return DOCUMENT ME!
854 public boolean getScaleAboveWrapped()
856 return scaleAboveWrapped;
862 * @return DOCUMENT ME!
864 public boolean getScaleLeftWrapped()
866 return scaleLeftWrapped;
872 * @return DOCUMENT ME!
874 public boolean getScaleRightWrapped()
876 return scaleRightWrapped;
885 public void setScaleAboveWrapped(boolean b)
887 scaleAboveWrapped = b;
896 public void setScaleLeftWrapped(boolean b)
898 scaleLeftWrapped = b;
907 public void setScaleRightWrapped(boolean b)
909 scaleRightWrapped = b;
912 public void setDataset(boolean b)
917 public boolean isDataset()
922 public boolean getShowHiddenMarkers()
924 return showHiddenMarkers;
927 public void setShowHiddenMarkers(boolean show)
929 showHiddenMarkers = show;
933 * returns the visible column regions of the alignment
935 * @param selectedRegionOnly
936 * true to just return the contigs intersecting with the selected
940 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
942 int[] viscontigs = null;
943 int start = 0, end = 0;
944 if (selectedRegionOnly && selectionGroup != null)
946 start = selectionGroup.getStartRes();
947 end = selectionGroup.getEndRes() + 1;
951 end = alignment.getWidth();
953 viscontigs = colSel.getVisibleContigs(start, end);
958 * get hash of undo and redo list for the alignment
960 * @return long[] { historyList.hashCode, redoList.hashCode };
962 public long[] getUndoRedoHash()
965 if (historyList == null || redoList == null)
969 { historyList.hashCode(), this.redoList.hashCode() };
973 * test if a particular set of hashcodes are different to the hashcodes for
974 * the undo and redo list.
977 * the stored set of hashcodes as returned by getUndoRedoHash
978 * @return true if the hashcodes differ (ie the alignment has been edited) or
979 * the stored hashcode array differs in size
981 public boolean isUndoRedoHashModified(long[] undoredo)
983 if (undoredo == null)
987 long[] cstate = getUndoRedoHash();
988 if (cstate.length != undoredo.length)
993 for (int i = 0; i < cstate.length; i++)
995 if (cstate[i] != undoredo[i])
1003 public boolean getCentreColumnLabels()
1005 return centreColumnLabels;
1008 public void setCentreColumnLabels(boolean centrecolumnlabels)
1010 centreColumnLabels = centrecolumnlabels;
1014 * enable or disable the display of Database Cross References in the sequence
1017 public void setShowDbRefs(boolean show)
1024 * @return true if Database References are to be displayed on tooltips.
1026 public boolean isShowDbRefs()
1033 * @return true if Non-positional features are to be displayed on tooltips.
1035 public boolean isShowNpFeats()
1041 * enable or disable the display of Non-Positional sequence features in the
1042 * sequence ID tooltip
1046 public void setShowNpFeats(boolean show)
1053 * @return true if view has hidden rows
1055 public boolean hasHiddenRows()
1057 return hasHiddenRows;
1062 * @return true if view has hidden columns
1064 public boolean hasHiddenColumns()
1066 return hasHiddenColumns;
1070 * when set, view will scroll to show the highlighted position
1072 public boolean followHighlight = true;
1075 * @return true if view should scroll to show the highlighted region of a
1079 public boolean getFollowHighlight()
1081 return followHighlight;
1084 public boolean followSelection = true;
1087 * @return true if view selection should always follow the selections
1088 * broadcast by other selection sources
1090 public boolean getFollowSelection()
1092 return followSelection;
1095 boolean showSeqFeaturesHeight;
1097 public void sendSelection()
1099 jalview.structure.StructureSelectionManager
1100 .getStructureSelectionManager(Desktop.instance).sendSelection(
1101 new SequenceGroup(getSelectionGroup()),
1102 new ColumnSelection(getColumnSelection()), this);
1105 public void setShowSequenceFeaturesHeight(boolean selected)
1107 showSeqFeaturesHeight = selected;
1110 public boolean getShowSequenceFeaturesHeight()
1112 return showSeqFeaturesHeight;
1116 * return the alignPanel containing the given viewport. Use this to get the
1117 * components currently handling the given viewport.
1120 * @return null or an alignPanel guaranteed to have non-null alignFrame
1123 public AlignmentPanel getAlignPanel()
1125 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1126 .getSequenceSetId());
1127 AlignmentPanel ap = null;
1128 for (int p = 0; aps != null && p < aps.length; p++)
1130 if (aps[p].av == this)
1138 public boolean getSortByTree()
1143 public void setSortByTree(boolean sort)
1149 * synthesize a column selection if none exists so it covers the given
1150 * selection group. if wholewidth is false, no column selection is made if the
1151 * selection group covers the whole alignment width.
1156 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1160 && (sgs = sg.getStartRes()) >= 0
1161 && sg.getStartRes() <= (sge = sg.getEndRes())
1162 && (colSel == null || colSel.getSelected() == null || colSel
1163 .getSelected().size() == 0))
1165 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1172 colSel = new ColumnSelection();
1174 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1176 colSel.addElement(cspos);
1181 public StructureSelectionManager getStructureSelectionManager()
1183 return StructureSelectionManager
1184 .getStructureSelectionManager(Desktop.instance);
1190 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1191 * sequence in the alignment holds a reference to it
1193 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1195 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1196 for (PDBEntry pdb : pdbEntries)
1198 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1199 for (int i = 0; i < alignment.getHeight(); i++)
1201 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1206 for (int p = 0; p < pdbs.size(); p++)
1208 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1209 if (p1.getId().equals(pdb.getId()))
1211 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1218 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1220 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1223 public boolean isNormaliseSequenceLogo()
1225 return normaliseSequenceLogo;
1228 public void setNormaliseSequenceLogo(boolean state)
1230 normaliseSequenceLogo = state;
1235 * @return true if alignment characters should be displayed
1237 public boolean isValidCharWidth()
1239 return validCharWidth;
1242 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1244 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1246 return calcIdParams.get(calcId);
1249 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1250 boolean needsUpdate)
1252 calcIdParams.put(calcId, settings);
1253 // TODO: create a restart list to trigger any calculations that need to be
1254 // restarted after load
1255 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1258 Cache.log.debug("trigger update for " + calcId);