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 "SORT_ANNOTATIONS", SequenceAnnotationOrder.SEQUENCE_AND_LABEL.name()));
370 showAutocalculatedAbove = Cache
371 .getDefault("SHOW_AUTOCALC_ABOVE", false);
378 * features are displayed if true
380 public void setShowSequenceFeatures(boolean b)
382 showSequenceFeatures = b;
385 public boolean getShowSequenceFeatures()
387 return showSequenceFeatures;
391 * centre columnar annotation labels in displayed alignment annotation TODO:
392 * add to jalviewXML and annotation display settings
394 boolean centreColumnLabels = false;
396 private boolean showdbrefs;
398 private boolean shownpfeats;
400 // --------END Structure Conservation
403 * get the consensus sequence as displayed under the PID consensus annotation
406 * @return consensus sequence as a new sequence object
408 public SequenceI getConsensusSeq()
410 if (consensus == null)
412 updateConsensus(null);
414 if (consensus == null)
418 StringBuffer seqs = new StringBuffer();
419 for (int i = 0; i < consensus.annotations.length; i++)
421 if (consensus.annotations[i] != null)
423 if (consensus.annotations[i].description.charAt(0) == '[')
425 seqs.append(consensus.annotations[i].description.charAt(1));
429 seqs.append(consensus.annotations[i].displayCharacter);
434 SequenceI sq = new Sequence("Consensus", seqs.toString());
435 sq.setDescription("Percentage Identity Consensus "
436 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
443 * @return DOCUMENT ME!
445 public int getStartRes()
453 * @return DOCUMENT ME!
455 public int getEndRes()
463 * @return DOCUMENT ME!
465 public int getStartSeq()
476 public void setStartRes(int res)
487 public void setStartSeq(int seq)
498 public void setEndRes(int res)
500 if (res > (alignment.getWidth() - 1))
502 // log.System.out.println(" Corrected res from " + res + " to maximum " +
503 // (alignment.getWidth()-1));
504 res = alignment.getWidth() - 1;
521 public void setEndSeq(int seq)
523 if (seq > alignment.getHeight())
525 seq = alignment.getHeight();
539 * @return DOCUMENT ME!
541 public int getEndSeq()
552 public void setFont(Font f)
556 Container c = new Container();
558 java.awt.FontMetrics fm = c.getFontMetrics(font);
559 setCharHeight(fm.getHeight());
560 setCharWidth(fm.charWidth('M'));
561 validCharWidth = true;
567 * @return DOCUMENT ME!
569 public Font getFont()
580 public void setCharWidth(int w)
588 * @return DOCUMENT ME!
590 public int getCharWidth()
601 public void setCharHeight(int h)
609 * @return DOCUMENT ME!
611 public int getCharHeight()
622 public void setWrappedWidth(int w)
624 this.wrappedWidth = w;
630 * @return DOCUMENT ME!
632 public int getWrappedWidth()
640 * @return DOCUMENT ME!
642 public AlignmentI getAlignment()
653 public void setAlignment(AlignmentI align)
655 if (alignment != null && alignment.getCodonFrames() != null)
657 StructureSelectionManager.getStructureSelectionManager(
658 Desktop.instance).removeMappings(alignment.getCodonFrames());
660 this.alignment = align;
661 if (alignment != null && alignment.getCodonFrames() != null)
663 StructureSelectionManager.getStructureSelectionManager(
664 Desktop.instance).addMappings(alignment.getCodonFrames());
674 public void setWrapAlignment(boolean state)
676 wrapAlignment = state;
685 public void setShowText(boolean state)
696 public void setRenderGaps(boolean state)
704 * @return DOCUMENT ME!
706 public boolean getColourText()
708 return showColourText;
717 public void setColourText(boolean state)
719 showColourText = state;
728 public void setShowBoxes(boolean state)
736 * @return DOCUMENT ME!
738 public boolean getWrapAlignment()
740 return wrapAlignment;
746 * @return DOCUMENT ME!
748 public boolean getShowText()
756 * @return DOCUMENT ME!
758 public boolean getShowBoxes()
766 * @return DOCUMENT ME!
768 public char getGapCharacter()
770 return getAlignment().getGapCharacter();
779 public void setGapCharacter(char gap)
781 if (getAlignment() != null)
783 getAlignment().setGapCharacter(gap);
790 * @return DOCUMENT ME!
792 public ColumnSelection getColumnSelection()
803 public void setCurrentTree(NJTree tree)
811 * @return DOCUMENT ME!
813 public NJTree getCurrentTree()
821 * @return DOCUMENT ME!
823 public boolean getShowJVSuffix()
834 public void setShowJVSuffix(boolean b)
842 * @return DOCUMENT ME!
844 public boolean getShowAnnotation()
846 return showAnnotation;
855 public void setShowAnnotation(boolean b)
863 * @return DOCUMENT ME!
865 public boolean getScaleAboveWrapped()
867 return scaleAboveWrapped;
873 * @return DOCUMENT ME!
875 public boolean getScaleLeftWrapped()
877 return scaleLeftWrapped;
883 * @return DOCUMENT ME!
885 public boolean getScaleRightWrapped()
887 return scaleRightWrapped;
896 public void setScaleAboveWrapped(boolean b)
898 scaleAboveWrapped = b;
907 public void setScaleLeftWrapped(boolean b)
909 scaleLeftWrapped = b;
918 public void setScaleRightWrapped(boolean b)
920 scaleRightWrapped = b;
923 public void setDataset(boolean b)
928 public boolean isDataset()
933 public boolean getShowHiddenMarkers()
935 return showHiddenMarkers;
938 public void setShowHiddenMarkers(boolean show)
940 showHiddenMarkers = show;
944 * returns the visible column regions of the alignment
946 * @param selectedRegionOnly
947 * true to just return the contigs intersecting with the selected
951 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
953 int[] viscontigs = null;
954 int start = 0, end = 0;
955 if (selectedRegionOnly && selectionGroup != null)
957 start = selectionGroup.getStartRes();
958 end = selectionGroup.getEndRes() + 1;
962 end = alignment.getWidth();
964 viscontigs = colSel.getVisibleContigs(start, end);
969 * get hash of undo and redo list for the alignment
971 * @return long[] { historyList.hashCode, redoList.hashCode };
973 public long[] getUndoRedoHash()
976 if (historyList == null || redoList == null)
982 { historyList.hashCode(), this.redoList.hashCode() };
986 * test if a particular set of hashcodes are different to the hashcodes for
987 * the undo and redo list.
990 * the stored set of hashcodes as returned by getUndoRedoHash
991 * @return true if the hashcodes differ (ie the alignment has been edited) or
992 * the stored hashcode array differs in size
994 public boolean isUndoRedoHashModified(long[] undoredo)
996 if (undoredo == null)
1000 long[] cstate = getUndoRedoHash();
1001 if (cstate.length != undoredo.length)
1006 for (int i = 0; i < cstate.length; i++)
1008 if (cstate[i] != undoredo[i])
1016 public boolean getCentreColumnLabels()
1018 return centreColumnLabels;
1021 public void setCentreColumnLabels(boolean centrecolumnlabels)
1023 centreColumnLabels = centrecolumnlabels;
1027 * enable or disable the display of Database Cross References in the sequence
1030 public void setShowDbRefs(boolean show)
1037 * @return true if Database References are to be displayed on tooltips.
1039 public boolean isShowDbRefs()
1046 * @return true if Non-positional features are to be displayed on tooltips.
1048 public boolean isShowNpFeats()
1054 * enable or disable the display of Non-Positional sequence features in the
1055 * sequence ID tooltip
1059 public void setShowNpFeats(boolean show)
1066 * @return true if view has hidden rows
1068 public boolean hasHiddenRows()
1070 return hasHiddenRows;
1075 * @return true if view has hidden columns
1077 public boolean hasHiddenColumns()
1079 return hasHiddenColumns;
1083 * when set, view will scroll to show the highlighted position
1085 public boolean followHighlight = true;
1088 * @return true if view should scroll to show the highlighted region of a
1092 public boolean getFollowHighlight()
1094 return followHighlight;
1097 public boolean followSelection = true;
1100 * @return true if view selection should always follow the selections
1101 * broadcast by other selection sources
1103 public boolean getFollowSelection()
1105 return followSelection;
1108 boolean showSeqFeaturesHeight;
1110 public void sendSelection()
1112 jalview.structure.StructureSelectionManager
1113 .getStructureSelectionManager(Desktop.instance).sendSelection(
1114 new SequenceGroup(getSelectionGroup()),
1115 new ColumnSelection(getColumnSelection()), this);
1118 public void setShowSequenceFeaturesHeight(boolean selected)
1120 showSeqFeaturesHeight = selected;
1123 public boolean getShowSequenceFeaturesHeight()
1125 return showSeqFeaturesHeight;
1129 * return the alignPanel containing the given viewport. Use this to get the
1130 * components currently handling the given viewport.
1133 * @return null or an alignPanel guaranteed to have non-null alignFrame
1136 public AlignmentPanel getAlignPanel()
1138 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1139 .getSequenceSetId());
1140 AlignmentPanel ap = null;
1141 for (int p = 0; aps != null && p < aps.length; p++)
1143 if (aps[p].av == this)
1151 public boolean getSortByTree()
1156 public void setSortByTree(boolean sort)
1162 * synthesize a column selection if none exists so it covers the given
1163 * selection group. if wholewidth is false, no column selection is made if the
1164 * selection group covers the whole alignment width.
1169 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1173 && (sgs = sg.getStartRes()) >= 0
1174 && sg.getStartRes() <= (sge = sg.getEndRes())
1175 && (colSel == null || colSel.getSelected() == null || colSel
1176 .getSelected().size() == 0))
1178 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1185 colSel = new ColumnSelection();
1187 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1189 colSel.addElement(cspos);
1194 public StructureSelectionManager getStructureSelectionManager()
1196 return StructureSelectionManager
1197 .getStructureSelectionManager(Desktop.instance);
1203 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1204 * sequence in the alignment holds a reference to it
1206 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1208 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1209 for (PDBEntry pdb : pdbEntries)
1211 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1212 for (int i = 0; i < alignment.getHeight(); i++)
1214 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1221 for (int p = 0; p < pdbs.size(); p++)
1223 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1224 if (p1.getId().equals(pdb.getId()))
1226 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1235 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1237 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1240 public boolean isNormaliseSequenceLogo()
1242 return normaliseSequenceLogo;
1245 public void setNormaliseSequenceLogo(boolean state)
1247 normaliseSequenceLogo = state;
1252 * @return true if alignment characters should be displayed
1254 public boolean isValidCharWidth()
1256 return validCharWidth;
1259 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1261 private boolean showAutocalculatedAbove;
1263 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1265 return calcIdParams.get(calcId);
1268 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1269 boolean needsUpdate)
1271 calcIdParams.put(calcId, settings);
1272 // TODO: create a restart list to trigger any calculations that need to be
1273 // restarted after load
1274 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1277 Cache.log.debug("trigger update for " + calcId);
1281 protected SequenceAnnotationOrder getSortAnnotationsBy()
1283 return sortAnnotationsBy;
1286 protected void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
1288 this.sortAnnotationsBy = sortAnnotationsBy;
1291 protected boolean isShowAutocalculatedAbove()
1293 return showAutocalculatedAbove;
1296 protected void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
1298 this.showAutocalculatedAbove = showAutocalculatedAbove;