2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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/>.
19 * Jalview - A Sequence Alignment Editor and Viewer
20 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version 2
25 * of the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 import jalview.analysis.*;
43 import jalview.api.AlignCalcManagerI;
44 import jalview.api.AlignViewportI;
45 import jalview.api.AlignmentViewPanel;
46 import jalview.api.OOMHandlerI;
50 import jalview.datamodel.*;
52 import jalview.schemes.*;
53 import jalview.structure.SelectionSource;
54 import jalview.structure.StructureSelectionManager;
55 import jalview.structure.VamsasSource;
56 import jalview.viewmodel.AlignmentViewport;
57 import jalview.workers.AlignCalcManager;
58 import jalview.workers.ConsensusThread;
59 import jalview.workers.ConservationThread;
60 import jalview.workers.StrucConsensusThread;
66 * @version $Revision: 1.141 $
68 public class AlignViewport extends AlignmentViewport implements SelectionSource, VamsasSource, AlignViewportI
70 private static final int RIGHT_JUSTIFY = 1;
80 boolean showJVSuffix = true;
82 boolean showText = true;
84 boolean showColourText = false;
86 boolean showBoxes = true;
88 boolean wrapAlignment = false;
90 boolean renderGaps = true;
92 boolean showSequenceFeatures = false;
94 boolean showAnnotation = true;
96 boolean colourAppliesToAllGroups = true;
98 boolean conservationColourSelected = false;
100 boolean abovePIDThreshold = false;
106 boolean validCharWidth;
112 boolean seqNameItalics;
118 NJTree currentTree = null;
120 boolean scaleAboveWrapped = false;
122 boolean scaleLeftWrapped = true;
124 boolean scaleRightWrapped = true;
126 boolean showHiddenMarkers = true;
128 boolean cursorMode = false;
131 * Keys are the feature types which are currently visible. Note: Values are
134 Hashtable featuresDisplayed = null;
136 boolean antiAlias = false;
138 Rectangle explodedPosition;
142 boolean gatherViewsHere = false;
144 Stack historyList = new Stack();
146 Stack redoList = new Stack();
148 Hashtable sequenceColours;
150 int thresholdTextColour = 0;
152 Color textColour = Color.black;
154 Color textColour2 = Color.white;
156 boolean rightAlignIds = false;
159 * Creates a new AlignViewport object.
164 public AlignViewport(AlignmentI al)
171 * Create a new AlignViewport object with a specific sequence set ID
175 * (may be null - but potential for ambiguous constructor exception)
177 public AlignViewport(AlignmentI al, String seqsetid)
179 this(al, seqsetid, null);
182 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
184 sequenceSetID = seqsetid;
186 // TODO remove these once 2.4.VAMSAS release finished
187 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
189 Cache.log.debug("Setting viewport's sequence set id : "
192 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
194 Cache.log.debug("Setting viewport's view id : " + viewId);
201 * Create a new AlignViewport with hidden regions
205 * @param hiddenColumns
208 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
211 if (hiddenColumns != null)
213 this.colSel = hiddenColumns;
214 if (hiddenColumns.getHiddenColumns() != null
215 && hiddenColumns.getHiddenColumns().size() > 0)
217 hasHiddenColumns = true;
221 hasHiddenColumns = false;
228 * New viewport with hidden columns and an existing sequence set id
231 * @param hiddenColumns
235 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
238 this(al, hiddenColumns, seqsetid, null);
242 * New viewport with hidden columns and an existing sequence set id and viewid
245 * @param hiddenColumns
251 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
252 String seqsetid, String viewid)
254 sequenceSetID = seqsetid;
256 // TODO remove these once 2.4.VAMSAS release finished
257 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
259 Cache.log.debug("Setting viewport's sequence set id : "
262 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
264 Cache.log.debug("Setting viewport's view id : " + viewId);
267 if (hiddenColumns != null)
269 this.colSel = hiddenColumns;
270 if (hiddenColumns.getHiddenColumns() != null
271 && hiddenColumns.getHiddenColumns().size() > 0)
273 hasHiddenColumns = true;
277 hasHiddenColumns = false;
286 this.endRes = alignment.getWidth() - 1;
288 this.endSeq = alignment.getHeight() - 1;
290 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
292 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
293 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
295 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
296 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
297 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
299 setPadGaps(Cache.getDefault("PAD_GAPS", true));
300 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
301 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
303 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
304 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
305 String fontSize = Cache.getDefault("FONT_SIZE", "10");
307 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
311 if (fontStyle.equals("bold"))
315 else if (fontStyle.equals("italic"))
320 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
323 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
325 // We must set conservation and consensus before setting colour,
326 // as Blosum and Clustal require this to be done
327 if (hconsensus == null && !isDataset)
329 if (!alignment.isNucleotide())
331 showConservation=Cache.getDefault("SHOW_CONSERVATION", true);
332 showQuality=Cache.getDefault("SHOW_QUALITY", true);
333 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
336 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
338 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
339 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO", false);
340 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
341 showConsensus=Cache.getDefault("SHOW_IDENTITY", true);
342 consensus = new AlignmentAnnotation("Consensus", "PID",
343 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
344 consensus.hasText = true;
345 consensus.autoCalculated = true;
347 initAutoAnnotation();
348 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
350 globalColourScheme = ColourSchemeProperty.getColour(alignment,
351 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
353 if (globalColourScheme instanceof UserColourScheme)
355 globalColourScheme = UserDefinedColours.loadDefaultColours();
356 ((UserColourScheme) globalColourScheme).setThreshold(0,
357 getIgnoreGapsConsensus());
360 if (globalColourScheme != null)
362 globalColourScheme.setConsensus(hconsensus);
366 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
367 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
369 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
370 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
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" : ""));
444 * @return true if conservation based shading is enabled
446 public boolean getConservationSelected()
448 return conservationColourSelected;
455 * enable conservation based shading
457 public void setConservationSelected(boolean b)
459 conservationColourSelected = b;
465 * @return true if percent identity threshold is applied to shading
467 public boolean getAbovePIDThreshold()
469 return abovePIDThreshold;
477 * indicate if percent identity threshold is applied to shading
479 public void setAbovePIDThreshold(boolean b)
481 abovePIDThreshold = b;
487 * @return DOCUMENT ME!
489 public int getStartRes()
497 * @return DOCUMENT ME!
499 public int getEndRes()
507 * @return DOCUMENT ME!
509 public int getStartSeq()
520 public void setStartRes(int res)
531 public void setStartSeq(int seq)
542 public void setEndRes(int res)
544 if (res > (alignment.getWidth() - 1))
546 // log.System.out.println(" Corrected res from " + res + " to maximum " +
547 // (alignment.getWidth()-1));
548 res = alignment.getWidth() - 1;
565 public void setEndSeq(int seq)
567 if (seq > alignment.getHeight())
569 seq = alignment.getHeight();
583 * @return DOCUMENT ME!
585 public int getEndSeq()
596 public void setFont(Font f)
600 Container c = new Container();
602 java.awt.FontMetrics fm = c.getFontMetrics(font);
603 setCharHeight(fm.getHeight());
604 setCharWidth(fm.charWidth('M'));
605 validCharWidth = true;
611 * @return DOCUMENT ME!
613 public Font getFont()
624 public void setCharWidth(int w)
632 * @return DOCUMENT ME!
634 public int getCharWidth()
645 public void setCharHeight(int h)
653 * @return DOCUMENT ME!
655 public int getCharHeight()
666 public void setWrappedWidth(int w)
668 this.wrappedWidth = w;
674 * @return DOCUMENT ME!
676 public int getWrappedWidth()
684 * @return DOCUMENT ME!
686 public AlignmentI getAlignment()
697 public void setAlignment(AlignmentI align)
699 if (alignment != null && alignment.getCodonFrames() != null)
701 StructureSelectionManager.getStructureSelectionManager(
702 Desktop.instance).removeMappings(alignment.getCodonFrames());
704 this.alignment = align;
705 if (alignment!=null && alignment.getCodonFrames() != null)
707 StructureSelectionManager.getStructureSelectionManager(
708 Desktop.instance).addMappings(alignment.getCodonFrames());
718 public void setWrapAlignment(boolean state)
720 wrapAlignment = state;
729 public void setShowText(boolean state)
740 public void setRenderGaps(boolean state)
748 * @return DOCUMENT ME!
750 public boolean getColourText()
752 return showColourText;
761 public void setColourText(boolean state)
763 showColourText = state;
772 public void setShowBoxes(boolean state)
780 * @return DOCUMENT ME!
782 public boolean getWrapAlignment()
784 return wrapAlignment;
790 * @return DOCUMENT ME!
792 public boolean getShowText()
800 * @return DOCUMENT ME!
802 public boolean getShowBoxes()
810 * @return DOCUMENT ME!
812 public char getGapCharacter()
814 return getAlignment().getGapCharacter();
823 public void setGapCharacter(char gap)
825 if (getAlignment() != null)
827 getAlignment().setGapCharacter(gap);
837 public void setThreshold(int thresh)
845 * @return DOCUMENT ME!
847 public int getThreshold()
858 public void setIncrement(int inc)
866 * @return DOCUMENT ME!
868 public int getIncrement()
876 * @return DOCUMENT ME!
878 public ColumnSelection getColumnSelection()
889 public void setCurrentTree(NJTree tree)
897 * @return DOCUMENT ME!
899 public NJTree getCurrentTree()
910 public void setColourAppliesToAllGroups(boolean b)
912 colourAppliesToAllGroups = b;
918 * @return DOCUMENT ME!
920 public boolean getColourAppliesToAllGroups()
922 return colourAppliesToAllGroups;
928 * @return DOCUMENT ME!
930 public boolean getShowJVSuffix()
941 public void setShowJVSuffix(boolean b)
949 * @return DOCUMENT ME!
951 public boolean getShowAnnotation()
953 return showAnnotation;
962 public void setShowAnnotation(boolean b)
970 * @return DOCUMENT ME!
972 public boolean getScaleAboveWrapped()
974 return scaleAboveWrapped;
980 * @return DOCUMENT ME!
982 public boolean getScaleLeftWrapped()
984 return scaleLeftWrapped;
990 * @return DOCUMENT ME!
992 public boolean getScaleRightWrapped()
994 return scaleRightWrapped;
1003 public void setScaleAboveWrapped(boolean b)
1005 scaleAboveWrapped = b;
1014 public void setScaleLeftWrapped(boolean b)
1016 scaleLeftWrapped = b;
1025 public void setScaleRightWrapped(boolean b)
1027 scaleRightWrapped = b;
1031 public void setDataset(boolean b)
1036 public boolean isDataset()
1043 public boolean getShowHiddenMarkers()
1045 return showHiddenMarkers;
1048 public void setShowHiddenMarkers(boolean show)
1050 showHiddenMarkers = show;
1053 public Color getSequenceColour(SequenceI seq)
1055 if (sequenceColours == null || !sequenceColours.containsKey(seq))
1061 return (Color) sequenceColours.get(seq);
1065 public void setSequenceColour(SequenceI seq, Color col)
1067 if (sequenceColours == null)
1069 sequenceColours = new Hashtable();
1074 sequenceColours.remove(seq);
1078 sequenceColours.put(seq, col);
1083 * returns the visible column regions of the alignment
1085 * @param selectedRegionOnly
1086 * true to just return the contigs intersecting with the selected
1090 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
1092 int[] viscontigs = null;
1093 int start = 0, end = 0;
1094 if (selectedRegionOnly && selectionGroup != null)
1096 start = selectionGroup.getStartRes();
1097 end = selectionGroup.getEndRes() + 1;
1101 end = alignment.getWidth();
1103 viscontigs = colSel.getVisibleContigs(start, end);
1108 * get hash of undo and redo list for the alignment
1110 * @return long[] { historyList.hashCode, redoList.hashCode };
1112 public long[] getUndoRedoHash()
1114 if (historyList == null || redoList == null)
1118 { historyList.hashCode(), this.redoList.hashCode() };
1122 * test if a particular set of hashcodes are different to the hashcodes for
1123 * the undo and redo list.
1126 * the stored set of hashcodes as returned by getUndoRedoHash
1127 * @return true if the hashcodes differ (ie the alignment has been edited) or
1128 * the stored hashcode array differs in size
1130 public boolean isUndoRedoHashModified(long[] undoredo)
1132 if (undoredo == null)
1136 long[] cstate = getUndoRedoHash();
1137 if (cstate.length != undoredo.length)
1142 for (int i = 0; i < cstate.length; i++)
1144 if (cstate[i] != undoredo[i])
1152 public boolean getCentreColumnLabels()
1154 return centreColumnLabels;
1157 public void setCentreColumnLabels(boolean centrecolumnlabels)
1159 centreColumnLabels = centrecolumnlabels;
1162 public void updateSequenceIdColours()
1164 Vector groups = alignment.getGroups();
1165 if (sequenceColours == null)
1167 sequenceColours = new Hashtable();
1169 for (int ig = 0, igSize = groups.size(); ig < igSize; ig++)
1171 SequenceGroup sg = (SequenceGroup) groups.elementAt(ig);
1172 if (sg.idColour != null)
1174 Vector sqs = sg.getSequences(getHiddenRepSequences());
1175 for (int s = 0, sSize = sqs.size(); s < sSize; s++)
1177 sequenceColours.put(sqs.elementAt(s), sg.idColour);
1184 * enable or disable the display of Database Cross References in the sequence
1187 public void setShowDbRefs(boolean show)
1194 * @return true if Database References are to be displayed on tooltips.
1196 public boolean isShowDbRefs()
1203 * @return true if Non-positional features are to be displayed on tooltips.
1205 public boolean isShowNpFeats()
1211 * enable or disable the display of Non-Positional sequence features in the
1212 * sequence ID tooltip
1216 public void setShowNpFeats(boolean show)
1223 * @return true if view has hidden rows
1225 public boolean hasHiddenRows()
1227 return hasHiddenRows;
1232 * @return true if view has hidden columns
1234 public boolean hasHiddenColumns()
1236 return hasHiddenColumns;
1240 * when set, view will scroll to show the highlighted position
1242 public boolean followHighlight = true;
1245 * @return true if view should scroll to show the highlighted region of a
1249 public boolean getFollowHighlight()
1251 return followHighlight;
1254 public boolean followSelection = true;
1257 * @return true if view selection should always follow the selections
1258 * broadcast by other selection sources
1260 public boolean getFollowSelection()
1262 return followSelection;
1265 boolean showSeqFeaturesHeight;
1267 public void sendSelection()
1269 jalview.structure.StructureSelectionManager
1270 .getStructureSelectionManager(Desktop.instance).sendSelection(
1271 new SequenceGroup(getSelectionGroup()),
1272 new ColumnSelection(getColumnSelection()), this);
1275 public void setShowSequenceFeaturesHeight(boolean selected)
1277 showSeqFeaturesHeight = selected;
1280 public boolean getShowSequenceFeaturesHeight()
1282 return showSeqFeaturesHeight;
1286 * return the alignPanel containing the given viewport. Use this to get the
1287 * components currently handling the given viewport.
1290 * @return null or an alignPanel guaranteed to have non-null alignFrame
1293 public AlignmentPanel getAlignPanel()
1295 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1296 .getSequenceSetId());
1297 AlignmentPanel ap = null;
1298 for (int p = 0; aps != null && p < aps.length; p++)
1300 if (aps[p].av == this)
1308 public boolean getSortByTree()
1313 public void setSortByTree(boolean sort)
1319 * synthesize a column selection if none exists so it covers the given
1320 * selection group. if wholewidth is false, no column selection is made if the
1321 * selection group covers the whole alignment width.
1326 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1330 && (sgs = sg.getStartRes()) >= 0
1331 && sg.getStartRes() <= (sge = sg.getEndRes())
1332 && (colSel == null || colSel.getSelected() == null || colSel
1333 .getSelected().size() == 0))
1335 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1342 colSel = new ColumnSelection();
1344 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1346 colSel.addElement(cspos);
1351 public StructureSelectionManager getStructureSelectionManager()
1353 return StructureSelectionManager
1354 .getStructureSelectionManager(Desktop.instance);
1360 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1361 * sequence in the alignment holds a reference to it
1363 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1365 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1366 for (PDBEntry pdb : pdbEntries)
1368 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1369 for (int i = 0; i < alignment.getHeight(); i++)
1371 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1376 for (int p = 0; p < pdbs.size(); p++)
1378 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1379 if (p1.getId().equals(pdb.getId()))
1381 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1388 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1390 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1394 public boolean isNormaliseSequenceLogo()
1396 return normaliseSequenceLogo;
1399 public void setNormaliseSequenceLogo(boolean state)
1401 normaliseSequenceLogo = state;
1407 * @return true if alignment characters should be displayed
1409 public boolean isValidCharWidth()
1411 return validCharWidth;