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;
137 boolean isDataset = false;
139 boolean antiAlias = false;
141 Rectangle explodedPosition;
145 boolean gatherViewsHere = false;
147 Stack historyList = new Stack();
149 Stack redoList = new Stack();
151 Hashtable sequenceColours;
153 int thresholdTextColour = 0;
155 Color textColour = Color.black;
157 Color textColour2 = Color.white;
159 boolean rightAlignIds = false;
162 * Creates a new AlignViewport object.
167 public AlignViewport(AlignmentI al)
174 * Create a new AlignViewport object with a specific sequence set ID
178 * (may be null - but potential for ambiguous constructor exception)
180 public AlignViewport(AlignmentI al, String seqsetid)
182 this(al, seqsetid, null);
185 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
187 sequenceSetID = seqsetid;
189 // TODO remove these once 2.4.VAMSAS release finished
190 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
192 Cache.log.debug("Setting viewport's sequence set id : "
195 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
197 Cache.log.debug("Setting viewport's view id : " + viewId);
204 * Create a new AlignViewport with hidden regions
208 * @param hiddenColumns
211 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
214 if (hiddenColumns != null)
216 this.colSel = hiddenColumns;
217 if (hiddenColumns.getHiddenColumns() != null
218 && hiddenColumns.getHiddenColumns().size() > 0)
220 hasHiddenColumns = true;
224 hasHiddenColumns = false;
231 * New viewport with hidden columns and an existing sequence set id
234 * @param hiddenColumns
238 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
241 this(al, hiddenColumns, seqsetid, null);
245 * New viewport with hidden columns and an existing sequence set id and viewid
248 * @param hiddenColumns
254 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
255 String seqsetid, String viewid)
257 sequenceSetID = seqsetid;
259 // TODO remove these once 2.4.VAMSAS release finished
260 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
262 Cache.log.debug("Setting viewport's sequence set id : "
265 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
267 Cache.log.debug("Setting viewport's view id : " + viewId);
270 if (hiddenColumns != null)
272 this.colSel = hiddenColumns;
273 if (hiddenColumns.getHiddenColumns() != null
274 && hiddenColumns.getHiddenColumns().size() > 0)
276 hasHiddenColumns = true;
280 hasHiddenColumns = false;
289 this.endRes = alignment.getWidth() - 1;
291 this.endSeq = alignment.getHeight() - 1;
293 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
295 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
296 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
298 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
299 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
300 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
302 setPadGaps(Cache.getDefault("PAD_GAPS", true));
303 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
304 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
306 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
307 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
308 String fontSize = Cache.getDefault("FONT_SIZE", "10");
310 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
314 if (fontStyle.equals("bold"))
318 else if (fontStyle.equals("italic"))
323 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
326 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
328 // We must set conservation and consensus before setting colour,
329 // as Blosum and Clustal require this to be done
330 if (hconsensus == null && !isDataset)
332 if (!alignment.isNucleotide())
334 conservation = new AlignmentAnnotation("Conservation",
335 "Conservation of total alignment less than " + getConsPercGaps()
336 + "% gaps", new Annotation[1], 0f, 11f,
337 AlignmentAnnotation.BAR_GRAPH);
338 conservation.hasText = true;
339 conservation.autoCalculated = true;
341 if (Cache.getDefault("SHOW_CONSERVATION", true))
343 alignment.addAnnotation(conservation);
346 if (Cache.getDefault("SHOW_QUALITY", true))
348 quality = new AlignmentAnnotation("Quality",
349 "Alignment Quality based on Blosum62 scores",
350 new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
351 quality.hasText = true;
352 quality.autoCalculated = true;
354 alignment.addAnnotation(quality);
356 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
363 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
365 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
366 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO", false);
367 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
368 // TODO: add menu option action that nulls or creates consensus object
369 // depending on if the user wants to see the annotation or not in a
370 // specific alignment
371 consensus = new AlignmentAnnotation("Consensus", "PID",
372 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
373 consensus.hasText = true;
374 consensus.autoCalculated = true;
376 if (alignment.isNucleotide() && alignment.hasRNAStructure())
378 strucConsensus = new AlignmentAnnotation("StrucConsensus", "PID",
379 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
380 strucConsensus.hasText = true;
381 strucConsensus.autoCalculated = true;
384 if (Cache.getDefault("SHOW_IDENTITY", true))
386 alignment.addAnnotation(consensus);
387 // TODO: Make own if for structure
388 if (alignment.isNucleotide() && alignment.hasRNAStructure())
390 alignment.addAnnotation(strucConsensus);
396 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
398 globalColourScheme = ColourSchemeProperty.getColour(alignment,
399 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
401 if (globalColourScheme instanceof UserColourScheme)
403 globalColourScheme = UserDefinedColours.loadDefaultColours();
404 ((UserColourScheme) globalColourScheme).setThreshold(0,
405 getIgnoreGapsConsensus());
408 if (globalColourScheme != null)
410 globalColourScheme.setConsensus(hconsensus);
414 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
415 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
417 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
418 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
426 * features are displayed if true
428 public void setShowSequenceFeatures(boolean b)
430 showSequenceFeatures = b;
433 public boolean getShowSequenceFeatures()
435 return showSequenceFeatures;
439 * centre columnar annotation labels in displayed alignment annotation TODO:
440 * add to jalviewXML and annotation display settings
442 boolean centreColumnLabels = false;
444 private boolean showdbrefs;
446 private boolean shownpfeats;
448 // --------END Structure Conservation
451 * get the consensus sequence as displayed under the PID consensus annotation
454 * @return consensus sequence as a new sequence object
456 public SequenceI getConsensusSeq()
458 if (consensus == null)
460 updateConsensus(null);
462 if (consensus == null)
466 StringBuffer seqs = new StringBuffer();
467 for (int i = 0; i < consensus.annotations.length; i++)
469 if (consensus.annotations[i] != null)
471 if (consensus.annotations[i].description.charAt(0) == '[')
473 seqs.append(consensus.annotations[i].description.charAt(1));
477 seqs.append(consensus.annotations[i].displayCharacter);
482 SequenceI sq = new Sequence("Consensus", seqs.toString());
483 sq.setDescription("Percentage Identity Consensus "
484 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
492 * @return true if conservation based shading is enabled
494 public boolean getConservationSelected()
496 return conservationColourSelected;
503 * enable conservation based shading
505 public void setConservationSelected(boolean b)
507 conservationColourSelected = b;
513 * @return true if percent identity threshold is applied to shading
515 public boolean getAbovePIDThreshold()
517 return abovePIDThreshold;
525 * indicate if percent identity threshold is applied to shading
527 public void setAbovePIDThreshold(boolean b)
529 abovePIDThreshold = b;
535 * @return DOCUMENT ME!
537 public int getStartRes()
545 * @return DOCUMENT ME!
547 public int getEndRes()
555 * @return DOCUMENT ME!
557 public int getStartSeq()
568 public void setStartRes(int res)
579 public void setStartSeq(int seq)
590 public void setEndRes(int res)
592 if (res > (alignment.getWidth() - 1))
594 // log.System.out.println(" Corrected res from " + res + " to maximum " +
595 // (alignment.getWidth()-1));
596 res = alignment.getWidth() - 1;
613 public void setEndSeq(int seq)
615 if (seq > alignment.getHeight())
617 seq = alignment.getHeight();
631 * @return DOCUMENT ME!
633 public int getEndSeq()
644 public void setFont(Font f)
648 Container c = new Container();
650 java.awt.FontMetrics fm = c.getFontMetrics(font);
651 setCharHeight(fm.getHeight());
652 setCharWidth(fm.charWidth('M'));
653 validCharWidth = true;
659 * @return DOCUMENT ME!
661 public Font getFont()
672 public void setCharWidth(int w)
680 * @return DOCUMENT ME!
682 public int getCharWidth()
693 public void setCharHeight(int h)
701 * @return DOCUMENT ME!
703 public int getCharHeight()
714 public void setWrappedWidth(int w)
716 this.wrappedWidth = w;
722 * @return DOCUMENT ME!
724 public int getWrappedWidth()
732 * @return DOCUMENT ME!
734 public AlignmentI getAlignment()
745 public void setAlignment(AlignmentI align)
747 if (alignment != null && alignment.getCodonFrames() != null)
749 StructureSelectionManager.getStructureSelectionManager(
750 Desktop.instance).removeMappings(alignment.getCodonFrames());
752 this.alignment = align;
753 if (alignment!=null && alignment.getCodonFrames() != null)
755 StructureSelectionManager.getStructureSelectionManager(
756 Desktop.instance).addMappings(alignment.getCodonFrames());
766 public void setWrapAlignment(boolean state)
768 wrapAlignment = state;
777 public void setShowText(boolean state)
788 public void setRenderGaps(boolean state)
796 * @return DOCUMENT ME!
798 public boolean getColourText()
800 return showColourText;
809 public void setColourText(boolean state)
811 showColourText = state;
820 public void setShowBoxes(boolean state)
828 * @return DOCUMENT ME!
830 public boolean getWrapAlignment()
832 return wrapAlignment;
838 * @return DOCUMENT ME!
840 public boolean getShowText()
848 * @return DOCUMENT ME!
850 public boolean getShowBoxes()
858 * @return DOCUMENT ME!
860 public char getGapCharacter()
862 return getAlignment().getGapCharacter();
871 public void setGapCharacter(char gap)
873 if (getAlignment() != null)
875 getAlignment().setGapCharacter(gap);
885 public void setThreshold(int thresh)
893 * @return DOCUMENT ME!
895 public int getThreshold()
906 public void setIncrement(int inc)
914 * @return DOCUMENT ME!
916 public int getIncrement()
924 * @return DOCUMENT ME!
926 public ColumnSelection getColumnSelection()
937 public void setCurrentTree(NJTree tree)
945 * @return DOCUMENT ME!
947 public NJTree getCurrentTree()
958 public void setColourAppliesToAllGroups(boolean b)
960 colourAppliesToAllGroups = b;
966 * @return DOCUMENT ME!
968 public boolean getColourAppliesToAllGroups()
970 return colourAppliesToAllGroups;
976 * @return DOCUMENT ME!
978 public boolean getShowJVSuffix()
989 public void setShowJVSuffix(boolean b)
997 * @return DOCUMENT ME!
999 public boolean getShowAnnotation()
1001 return showAnnotation;
1010 public void setShowAnnotation(boolean b)
1018 * @return DOCUMENT ME!
1020 public boolean getScaleAboveWrapped()
1022 return scaleAboveWrapped;
1028 * @return DOCUMENT ME!
1030 public boolean getScaleLeftWrapped()
1032 return scaleLeftWrapped;
1038 * @return DOCUMENT ME!
1040 public boolean getScaleRightWrapped()
1042 return scaleRightWrapped;
1051 public void setScaleAboveWrapped(boolean b)
1053 scaleAboveWrapped = b;
1062 public void setScaleLeftWrapped(boolean b)
1064 scaleLeftWrapped = b;
1073 public void setScaleRightWrapped(boolean b)
1075 scaleRightWrapped = b;
1079 public void setDataset(boolean b)
1084 public boolean isDataset()
1091 public boolean getShowHiddenMarkers()
1093 return showHiddenMarkers;
1096 public void setShowHiddenMarkers(boolean show)
1098 showHiddenMarkers = show;
1101 public Color getSequenceColour(SequenceI seq)
1103 if (sequenceColours == null || !sequenceColours.containsKey(seq))
1109 return (Color) sequenceColours.get(seq);
1113 public void setSequenceColour(SequenceI seq, Color col)
1115 if (sequenceColours == null)
1117 sequenceColours = new Hashtable();
1122 sequenceColours.remove(seq);
1126 sequenceColours.put(seq, col);
1131 * returns the visible column regions of the alignment
1133 * @param selectedRegionOnly
1134 * true to just return the contigs intersecting with the selected
1138 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
1140 int[] viscontigs = null;
1141 int start = 0, end = 0;
1142 if (selectedRegionOnly && selectionGroup != null)
1144 start = selectionGroup.getStartRes();
1145 end = selectionGroup.getEndRes() + 1;
1149 end = alignment.getWidth();
1151 viscontigs = colSel.getVisibleContigs(start, end);
1156 * get hash of undo and redo list for the alignment
1158 * @return long[] { historyList.hashCode, redoList.hashCode };
1160 public long[] getUndoRedoHash()
1162 if (historyList == null || redoList == null)
1166 { historyList.hashCode(), this.redoList.hashCode() };
1170 * test if a particular set of hashcodes are different to the hashcodes for
1171 * the undo and redo list.
1174 * the stored set of hashcodes as returned by getUndoRedoHash
1175 * @return true if the hashcodes differ (ie the alignment has been edited) or
1176 * the stored hashcode array differs in size
1178 public boolean isUndoRedoHashModified(long[] undoredo)
1180 if (undoredo == null)
1184 long[] cstate = getUndoRedoHash();
1185 if (cstate.length != undoredo.length)
1190 for (int i = 0; i < cstate.length; i++)
1192 if (cstate[i] != undoredo[i])
1200 public boolean getCentreColumnLabels()
1202 return centreColumnLabels;
1205 public void setCentreColumnLabels(boolean centrecolumnlabels)
1207 centreColumnLabels = centrecolumnlabels;
1210 public void updateSequenceIdColours()
1212 Vector groups = alignment.getGroups();
1213 if (sequenceColours == null)
1215 sequenceColours = new Hashtable();
1217 for (int ig = 0, igSize = groups.size(); ig < igSize; ig++)
1219 SequenceGroup sg = (SequenceGroup) groups.elementAt(ig);
1220 if (sg.idColour != null)
1222 Vector sqs = sg.getSequences(getHiddenRepSequences());
1223 for (int s = 0, sSize = sqs.size(); s < sSize; s++)
1225 sequenceColours.put(sqs.elementAt(s), sg.idColour);
1232 * enable or disable the display of Database Cross References in the sequence
1235 public void setShowDbRefs(boolean show)
1242 * @return true if Database References are to be displayed on tooltips.
1244 public boolean isShowDbRefs()
1251 * @return true if Non-positional features are to be displayed on tooltips.
1253 public boolean isShowNpFeats()
1259 * enable or disable the display of Non-Positional sequence features in the
1260 * sequence ID tooltip
1264 public void setShowNpFeats(boolean show)
1271 * @return true if view has hidden rows
1273 public boolean hasHiddenRows()
1275 return hasHiddenRows;
1280 * @return true if view has hidden columns
1282 public boolean hasHiddenColumns()
1284 return hasHiddenColumns;
1288 * when set, view will scroll to show the highlighted position
1290 public boolean followHighlight = true;
1293 * @return true if view should scroll to show the highlighted region of a
1297 public boolean getFollowHighlight()
1299 return followHighlight;
1302 public boolean followSelection = true;
1305 * @return true if view selection should always follow the selections
1306 * broadcast by other selection sources
1308 public boolean getFollowSelection()
1310 return followSelection;
1313 boolean showSeqFeaturesHeight;
1315 public void sendSelection()
1317 jalview.structure.StructureSelectionManager
1318 .getStructureSelectionManager(Desktop.instance).sendSelection(
1319 new SequenceGroup(getSelectionGroup()),
1320 new ColumnSelection(getColumnSelection()), this);
1323 public void setShowSequenceFeaturesHeight(boolean selected)
1325 showSeqFeaturesHeight = selected;
1328 public boolean getShowSequenceFeaturesHeight()
1330 return showSeqFeaturesHeight;
1334 * return the alignPanel containing the given viewport. Use this to get the
1335 * components currently handling the given viewport.
1338 * @return null or an alignPanel guaranteed to have non-null alignFrame
1341 public AlignmentPanel getAlignPanel()
1343 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1344 .getSequenceSetId());
1345 AlignmentPanel ap = null;
1346 for (int p = 0; aps != null && p < aps.length; p++)
1348 if (aps[p].av == this)
1356 public boolean getSortByTree()
1361 public void setSortByTree(boolean sort)
1367 * synthesize a column selection if none exists so it covers the given
1368 * selection group. if wholewidth is false, no column selection is made if the
1369 * selection group covers the whole alignment width.
1374 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1378 && (sgs = sg.getStartRes()) >= 0
1379 && sg.getStartRes() <= (sge = sg.getEndRes())
1380 && (colSel == null || colSel.getSelected() == null || colSel
1381 .getSelected().size() == 0))
1383 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1390 colSel = new ColumnSelection();
1392 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1394 colSel.addElement(cspos);
1399 public StructureSelectionManager getStructureSelectionManager()
1401 return StructureSelectionManager
1402 .getStructureSelectionManager(Desktop.instance);
1408 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1409 * sequence in the alignment holds a reference to it
1411 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1413 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1414 for (PDBEntry pdb : pdbEntries)
1416 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1417 for (int i = 0; i < alignment.getHeight(); i++)
1419 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1424 for (int p = 0; p < pdbs.size(); p++)
1426 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1427 if (p1.getId().equals(pdb.getId()))
1429 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1436 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1438 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1442 public boolean isNormaliseSequenceLogo()
1444 return normaliseSequenceLogo;
1447 public void setNormaliseSequenceLogo(boolean state)
1449 normaliseSequenceLogo = state;
1455 * @return true if alignment characters should be displayed
1457 public boolean isValidCharWidth()
1459 return validCharWidth;