2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
20 * Jalview - A Sequence Alignment Editor and Viewer
21 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
39 import jalview.analysis.NJTree;
40 import jalview.api.AlignViewportI;
41 import jalview.bin.Cache;
42 import jalview.datamodel.AlignmentAnnotation;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.ColumnSelection;
46 import jalview.datamodel.PDBEntry;
47 import jalview.datamodel.Sequence;
48 import jalview.datamodel.SequenceGroup;
49 import jalview.datamodel.SequenceI;
50 import jalview.schemes.ColourSchemeProperty;
51 import jalview.schemes.UserColourScheme;
52 import jalview.structure.SelectionSource;
53 import jalview.structure.StructureSelectionManager;
54 import jalview.structure.VamsasSource;
55 import jalview.viewmodel.AlignmentViewport;
56 import jalview.ws.params.AutoCalcSetting;
58 import java.awt.Color;
59 import java.awt.Container;
61 import java.awt.Rectangle;
62 import java.util.ArrayList;
63 import java.util.Hashtable;
64 import java.util.Stack;
65 import java.util.Vector;
71 * @version $Revision: 1.141 $
73 public class AlignViewport extends AlignmentViewport implements
74 SelectionSource, VamsasSource, AlignViewportI
84 boolean showJVSuffix = true;
86 boolean showText = true;
88 boolean showColourText = false;
90 boolean showBoxes = true;
92 boolean wrapAlignment = false;
94 boolean renderGaps = true;
96 boolean showSequenceFeatures = false;
98 boolean showAnnotation = true;
104 boolean validCharWidth;
110 boolean seqNameItalics;
112 NJTree currentTree = null;
114 boolean scaleAboveWrapped = false;
116 boolean scaleLeftWrapped = true;
118 boolean scaleRightWrapped = true;
120 boolean showHiddenMarkers = true;
122 boolean cursorMode = false;
125 * Keys are the feature types which are currently visible. Note: Values are
128 Hashtable featuresDisplayed = null;
130 boolean antiAlias = false;
132 Rectangle explodedPosition;
136 boolean gatherViewsHere = false;
138 Stack historyList = new Stack();
140 Stack redoList = new Stack();
142 Hashtable sequenceColours;
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;
938 public Color getSequenceColour(SequenceI seq)
940 if (sequenceColours == null || !sequenceColours.containsKey(seq))
946 return (Color) sequenceColours.get(seq);
950 public void setSequenceColour(SequenceI seq, Color col)
952 if (sequenceColours == null)
954 sequenceColours = new Hashtable();
959 sequenceColours.remove(seq);
963 sequenceColours.put(seq, col);
968 * returns the visible column regions of the alignment
970 * @param selectedRegionOnly
971 * true to just return the contigs intersecting with the selected
975 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
977 int[] viscontigs = null;
978 int start = 0, end = 0;
979 if (selectedRegionOnly && selectionGroup != null)
981 start = selectionGroup.getStartRes();
982 end = selectionGroup.getEndRes() + 1;
986 end = alignment.getWidth();
988 viscontigs = colSel.getVisibleContigs(start, end);
993 * get hash of undo and redo list for the alignment
995 * @return long[] { historyList.hashCode, redoList.hashCode };
997 public long[] getUndoRedoHash()
1000 if (historyList == null || redoList == null)
1004 { historyList.hashCode(), this.redoList.hashCode() };
1008 * test if a particular set of hashcodes are different to the hashcodes for
1009 * the undo and redo list.
1012 * the stored set of hashcodes as returned by getUndoRedoHash
1013 * @return true if the hashcodes differ (ie the alignment has been edited) or
1014 * the stored hashcode array differs in size
1016 public boolean isUndoRedoHashModified(long[] undoredo)
1018 if (undoredo == null)
1022 long[] cstate = getUndoRedoHash();
1023 if (cstate.length != undoredo.length)
1028 for (int i = 0; i < cstate.length; i++)
1030 if (cstate[i] != undoredo[i])
1038 public boolean getCentreColumnLabels()
1040 return centreColumnLabels;
1043 public void setCentreColumnLabels(boolean centrecolumnlabels)
1045 centreColumnLabels = centrecolumnlabels;
1048 public void updateSequenceIdColours()
1050 if (sequenceColours == null)
1052 sequenceColours = new Hashtable();
1054 for (SequenceGroup sg : alignment.getGroups())
1056 if (sg.idColour != null)
1058 for (SequenceI s : sg.getSequences(getHiddenRepSequences()))
1060 sequenceColours.put(s, sg.idColour);
1067 * enable or disable the display of Database Cross References in the sequence
1070 public void setShowDbRefs(boolean show)
1077 * @return true if Database References are to be displayed on tooltips.
1079 public boolean isShowDbRefs()
1086 * @return true if Non-positional features are to be displayed on tooltips.
1088 public boolean isShowNpFeats()
1094 * enable or disable the display of Non-Positional sequence features in the
1095 * sequence ID tooltip
1099 public void setShowNpFeats(boolean show)
1106 * @return true if view has hidden rows
1108 public boolean hasHiddenRows()
1110 return hasHiddenRows;
1115 * @return true if view has hidden columns
1117 public boolean hasHiddenColumns()
1119 return hasHiddenColumns;
1123 * when set, view will scroll to show the highlighted position
1125 public boolean followHighlight = true;
1128 * @return true if view should scroll to show the highlighted region of a
1132 public boolean getFollowHighlight()
1134 return followHighlight;
1137 public boolean followSelection = true;
1140 * @return true if view selection should always follow the selections
1141 * broadcast by other selection sources
1143 public boolean getFollowSelection()
1145 return followSelection;
1148 boolean showSeqFeaturesHeight;
1150 public void sendSelection()
1152 jalview.structure.StructureSelectionManager
1153 .getStructureSelectionManager(Desktop.instance).sendSelection(
1154 new SequenceGroup(getSelectionGroup()),
1155 new ColumnSelection(getColumnSelection()), this);
1158 public void setShowSequenceFeaturesHeight(boolean selected)
1160 showSeqFeaturesHeight = selected;
1163 public boolean getShowSequenceFeaturesHeight()
1165 return showSeqFeaturesHeight;
1169 * return the alignPanel containing the given viewport. Use this to get the
1170 * components currently handling the given viewport.
1173 * @return null or an alignPanel guaranteed to have non-null alignFrame
1176 public AlignmentPanel getAlignPanel()
1178 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1179 .getSequenceSetId());
1180 AlignmentPanel ap = null;
1181 for (int p = 0; aps != null && p < aps.length; p++)
1183 if (aps[p].av == this)
1191 public boolean getSortByTree()
1196 public void setSortByTree(boolean sort)
1202 * synthesize a column selection if none exists so it covers the given
1203 * selection group. if wholewidth is false, no column selection is made if the
1204 * selection group covers the whole alignment width.
1209 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1213 && (sgs = sg.getStartRes()) >= 0
1214 && sg.getStartRes() <= (sge = sg.getEndRes())
1215 && (colSel == null || colSel.getSelected() == null || colSel
1216 .getSelected().size() == 0))
1218 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1225 colSel = new ColumnSelection();
1227 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1229 colSel.addElement(cspos);
1234 public StructureSelectionManager getStructureSelectionManager()
1236 return StructureSelectionManager
1237 .getStructureSelectionManager(Desktop.instance);
1243 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1244 * sequence in the alignment holds a reference to it
1246 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1248 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1249 for (PDBEntry pdb : pdbEntries)
1251 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1252 for (int i = 0; i < alignment.getHeight(); i++)
1254 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1259 for (int p = 0; p < pdbs.size(); p++)
1261 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1262 if (p1.getId().equals(pdb.getId()))
1264 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1271 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1273 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1276 public boolean isNormaliseSequenceLogo()
1278 return normaliseSequenceLogo;
1281 public void setNormaliseSequenceLogo(boolean state)
1283 normaliseSequenceLogo = state;
1288 * @return true if alignment characters should be displayed
1290 public boolean isValidCharWidth()
1292 return validCharWidth;
1295 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1297 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1299 return calcIdParams.get(calcId);
1302 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1303 boolean needsUpdate)
1305 calcIdParams.put(calcId, settings);
1306 // TODO: create a restart list to trigger any calculations that need to be
1307 // restarted after load
1308 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1311 Cache.log.debug("trigger update for " + calcId);