2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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
38 import jalview.analysis.NJTree;
39 import jalview.api.AlignViewportI;
40 import jalview.bin.Cache;
41 import jalview.datamodel.AlignmentAnnotation;
42 import jalview.datamodel.AlignmentI;
43 import jalview.datamodel.Annotation;
44 import jalview.datamodel.ColumnSelection;
45 import jalview.datamodel.PDBEntry;
46 import jalview.datamodel.Sequence;
47 import jalview.datamodel.SequenceGroup;
48 import jalview.datamodel.SequenceI;
49 import jalview.schemes.ColourSchemeProperty;
50 import jalview.schemes.UserColourScheme;
51 import jalview.structure.SelectionSource;
52 import jalview.structure.StructureSelectionManager;
53 import jalview.structure.VamsasSource;
54 import jalview.viewmodel.AlignmentViewport;
55 import jalview.ws.params.AutoCalcSetting;
57 import java.awt.Color;
58 import java.awt.Container;
60 import java.awt.Rectangle;
61 import java.util.ArrayList;
62 import java.util.Hashtable;
63 import java.util.Stack;
64 import java.util.Vector;
70 * @version $Revision: 1.141 $
72 public class AlignViewport extends AlignmentViewport implements
73 SelectionSource, VamsasSource, AlignViewportI
83 boolean showJVSuffix = true;
85 boolean showText = true;
87 boolean showColourText = false;
89 boolean showBoxes = true;
91 boolean wrapAlignment = false;
93 boolean renderGaps = true;
95 boolean showSequenceFeatures = false;
97 boolean showAnnotation = true;
103 boolean validCharWidth;
109 boolean seqNameItalics;
111 NJTree currentTree = null;
113 boolean scaleAboveWrapped = false;
115 boolean scaleLeftWrapped = true;
117 boolean scaleRightWrapped = true;
119 boolean showHiddenMarkers = true;
121 boolean cursorMode = false;
124 * Keys are the feature types which are currently visible. Note: Values are
127 Hashtable featuresDisplayed = null;
129 boolean antiAlias = false;
131 Rectangle explodedPosition;
135 boolean gatherViewsHere = false;
137 Stack historyList = new Stack();
139 Stack redoList = new Stack();
141 Hashtable sequenceColours;
143 int thresholdTextColour = 0;
145 Color textColour = Color.black;
147 Color textColour2 = Color.white;
149 boolean rightAlignIds = false;
152 * Creates a new AlignViewport object.
157 public AlignViewport(AlignmentI al)
164 * Create a new AlignViewport object with a specific sequence set ID
168 * (may be null - but potential for ambiguous constructor exception)
170 public AlignViewport(AlignmentI al, String seqsetid)
172 this(al, seqsetid, null);
175 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
177 sequenceSetID = seqsetid;
179 // TODO remove these once 2.4.VAMSAS release finished
180 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
182 Cache.log.debug("Setting viewport's sequence set id : "
185 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
187 Cache.log.debug("Setting viewport's view id : " + viewId);
194 * Create a new AlignViewport with hidden regions
198 * @param hiddenColumns
201 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
204 if (hiddenColumns != null)
206 this.colSel = hiddenColumns;
207 if (hiddenColumns.getHiddenColumns() != null
208 && hiddenColumns.getHiddenColumns().size() > 0)
210 hasHiddenColumns = true;
214 hasHiddenColumns = false;
221 * New viewport with hidden columns and an existing sequence set id
224 * @param hiddenColumns
228 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
231 this(al, hiddenColumns, seqsetid, null);
235 * New viewport with hidden columns and an existing sequence set id and viewid
238 * @param hiddenColumns
244 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
245 String seqsetid, String viewid)
247 sequenceSetID = seqsetid;
249 // TODO remove these once 2.4.VAMSAS release finished
250 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
252 Cache.log.debug("Setting viewport's sequence set id : "
255 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
257 Cache.log.debug("Setting viewport's view id : " + viewId);
260 if (hiddenColumns != null)
262 this.colSel = hiddenColumns;
263 if (hiddenColumns.getHiddenColumns() != null
264 && hiddenColumns.getHiddenColumns().size() > 0)
266 hasHiddenColumns = true;
270 hasHiddenColumns = false;
279 this.endRes = alignment.getWidth() - 1;
281 this.endSeq = alignment.getHeight() - 1;
283 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
285 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
286 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
288 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
289 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
290 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
292 setPadGaps(Cache.getDefault("PAD_GAPS", true));
293 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
294 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
296 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
297 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
298 String fontSize = Cache.getDefault("FONT_SIZE", "10");
300 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
304 if (fontStyle.equals("bold"))
308 else if (fontStyle.equals("italic"))
313 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
316 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
318 // We must set conservation and consensus before setting colour,
319 // as Blosum and Clustal require this to be done
320 if (hconsensus == null && !isDataset)
322 if (!alignment.isNucleotide())
324 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
325 showQuality = Cache.getDefault("SHOW_QUALITY", true);
326 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
329 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
331 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
332 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
334 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
335 showConsensus = Cache.getDefault("SHOW_IDENTITY", true);
336 consensus = new AlignmentAnnotation("Consensus", "PID",
337 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
338 consensus.hasText = true;
339 consensus.autoCalculated = true;
341 initAutoAnnotation();
342 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
344 globalColourScheme = ColourSchemeProperty.getColour(alignment,
345 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
347 if (globalColourScheme instanceof UserColourScheme)
349 globalColourScheme = UserDefinedColours.loadDefaultColours();
350 ((UserColourScheme) globalColourScheme).setThreshold(0,
351 getIgnoreGapsConsensus());
354 if (globalColourScheme != null)
356 globalColourScheme.setConsensus(hconsensus);
360 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
361 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
363 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
364 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
372 * features are displayed if true
374 public void setShowSequenceFeatures(boolean b)
376 showSequenceFeatures = b;
379 public boolean getShowSequenceFeatures()
381 return showSequenceFeatures;
385 * centre columnar annotation labels in displayed alignment annotation TODO:
386 * add to jalviewXML and annotation display settings
388 boolean centreColumnLabels = false;
390 private boolean showdbrefs;
392 private boolean shownpfeats;
394 // --------END Structure Conservation
397 * get the consensus sequence as displayed under the PID consensus annotation
400 * @return consensus sequence as a new sequence object
402 public SequenceI getConsensusSeq()
404 if (consensus == null)
406 updateConsensus(null);
408 if (consensus == null)
412 StringBuffer seqs = new StringBuffer();
413 for (int i = 0; i < consensus.annotations.length; i++)
415 if (consensus.annotations[i] != null)
417 if (consensus.annotations[i].description.charAt(0) == '[')
419 seqs.append(consensus.annotations[i].description.charAt(1));
423 seqs.append(consensus.annotations[i].displayCharacter);
428 SequenceI sq = new Sequence("Consensus", seqs.toString());
429 sq.setDescription("Percentage Identity Consensus "
430 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
437 * @return DOCUMENT ME!
439 public int getStartRes()
447 * @return DOCUMENT ME!
449 public int getEndRes()
457 * @return DOCUMENT ME!
459 public int getStartSeq()
470 public void setStartRes(int res)
481 public void setStartSeq(int seq)
492 public void setEndRes(int res)
494 if (res > (alignment.getWidth() - 1))
496 // log.System.out.println(" Corrected res from " + res + " to maximum " +
497 // (alignment.getWidth()-1));
498 res = alignment.getWidth() - 1;
515 public void setEndSeq(int seq)
517 if (seq > alignment.getHeight())
519 seq = alignment.getHeight();
533 * @return DOCUMENT ME!
535 public int getEndSeq()
546 public void setFont(Font f)
550 Container c = new Container();
552 java.awt.FontMetrics fm = c.getFontMetrics(font);
553 setCharHeight(fm.getHeight());
554 setCharWidth(fm.charWidth('M'));
555 validCharWidth = true;
561 * @return DOCUMENT ME!
563 public Font getFont()
574 public void setCharWidth(int w)
582 * @return DOCUMENT ME!
584 public int getCharWidth()
595 public void setCharHeight(int h)
603 * @return DOCUMENT ME!
605 public int getCharHeight()
616 public void setWrappedWidth(int w)
618 this.wrappedWidth = w;
624 * @return DOCUMENT ME!
626 public int getWrappedWidth()
634 * @return DOCUMENT ME!
636 public AlignmentI getAlignment()
647 public void setAlignment(AlignmentI align)
649 if (alignment != null && alignment.getCodonFrames() != null)
651 StructureSelectionManager.getStructureSelectionManager(
652 Desktop.instance).removeMappings(alignment.getCodonFrames());
654 this.alignment = align;
655 if (alignment != null && alignment.getCodonFrames() != null)
657 StructureSelectionManager.getStructureSelectionManager(
658 Desktop.instance).addMappings(alignment.getCodonFrames());
668 public void setWrapAlignment(boolean state)
670 wrapAlignment = state;
679 public void setShowText(boolean state)
690 public void setRenderGaps(boolean state)
698 * @return DOCUMENT ME!
700 public boolean getColourText()
702 return showColourText;
711 public void setColourText(boolean state)
713 showColourText = state;
722 public void setShowBoxes(boolean state)
730 * @return DOCUMENT ME!
732 public boolean getWrapAlignment()
734 return wrapAlignment;
740 * @return DOCUMENT ME!
742 public boolean getShowText()
750 * @return DOCUMENT ME!
752 public boolean getShowBoxes()
760 * @return DOCUMENT ME!
762 public char getGapCharacter()
764 return getAlignment().getGapCharacter();
773 public void setGapCharacter(char gap)
775 if (getAlignment() != null)
777 getAlignment().setGapCharacter(gap);
784 * @return DOCUMENT ME!
786 public ColumnSelection getColumnSelection()
797 public void setCurrentTree(NJTree tree)
805 * @return DOCUMENT ME!
807 public NJTree getCurrentTree()
815 * @return DOCUMENT ME!
817 public boolean getShowJVSuffix()
828 public void setShowJVSuffix(boolean b)
836 * @return DOCUMENT ME!
838 public boolean getShowAnnotation()
840 return showAnnotation;
849 public void setShowAnnotation(boolean b)
857 * @return DOCUMENT ME!
859 public boolean getScaleAboveWrapped()
861 return scaleAboveWrapped;
867 * @return DOCUMENT ME!
869 public boolean getScaleLeftWrapped()
871 return scaleLeftWrapped;
877 * @return DOCUMENT ME!
879 public boolean getScaleRightWrapped()
881 return scaleRightWrapped;
890 public void setScaleAboveWrapped(boolean b)
892 scaleAboveWrapped = b;
901 public void setScaleLeftWrapped(boolean b)
903 scaleLeftWrapped = b;
912 public void setScaleRightWrapped(boolean b)
914 scaleRightWrapped = b;
917 public void setDataset(boolean b)
922 public boolean isDataset()
927 public boolean getShowHiddenMarkers()
929 return showHiddenMarkers;
932 public void setShowHiddenMarkers(boolean show)
934 showHiddenMarkers = show;
937 public Color getSequenceColour(SequenceI seq)
939 if (sequenceColours == null || !sequenceColours.containsKey(seq))
945 return (Color) sequenceColours.get(seq);
949 public void setSequenceColour(SequenceI seq, Color col)
951 if (sequenceColours == null)
953 sequenceColours = new Hashtable();
958 sequenceColours.remove(seq);
962 sequenceColours.put(seq, col);
967 * returns the visible column regions of the alignment
969 * @param selectedRegionOnly
970 * true to just return the contigs intersecting with the selected
974 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
976 int[] viscontigs = null;
977 int start = 0, end = 0;
978 if (selectedRegionOnly && selectionGroup != null)
980 start = selectionGroup.getStartRes();
981 end = selectionGroup.getEndRes() + 1;
985 end = alignment.getWidth();
987 viscontigs = colSel.getVisibleContigs(start, end);
992 * get hash of undo and redo list for the alignment
994 * @return long[] { historyList.hashCode, redoList.hashCode };
996 public long[] getUndoRedoHash()
999 if (historyList == null || redoList == null)
1003 { historyList.hashCode(), this.redoList.hashCode() };
1007 * test if a particular set of hashcodes are different to the hashcodes for
1008 * the undo and redo list.
1011 * the stored set of hashcodes as returned by getUndoRedoHash
1012 * @return true if the hashcodes differ (ie the alignment has been edited) or
1013 * the stored hashcode array differs in size
1015 public boolean isUndoRedoHashModified(long[] undoredo)
1017 if (undoredo == null)
1021 long[] cstate = getUndoRedoHash();
1022 if (cstate.length != undoredo.length)
1027 for (int i = 0; i < cstate.length; i++)
1029 if (cstate[i] != undoredo[i])
1037 public boolean getCentreColumnLabels()
1039 return centreColumnLabels;
1042 public void setCentreColumnLabels(boolean centrecolumnlabels)
1044 centreColumnLabels = centrecolumnlabels;
1047 public void updateSequenceIdColours()
1049 if (sequenceColours == null)
1051 sequenceColours = new Hashtable();
1053 for (SequenceGroup sg : alignment.getGroups())
1055 if (sg.idColour != null)
1057 for (SequenceI s : sg.getSequences(getHiddenRepSequences()))
1059 sequenceColours.put(s, sg.idColour);
1066 * enable or disable the display of Database Cross References in the sequence
1069 public void setShowDbRefs(boolean show)
1076 * @return true if Database References are to be displayed on tooltips.
1078 public boolean isShowDbRefs()
1085 * @return true if Non-positional features are to be displayed on tooltips.
1087 public boolean isShowNpFeats()
1093 * enable or disable the display of Non-Positional sequence features in the
1094 * sequence ID tooltip
1098 public void setShowNpFeats(boolean show)
1105 * @return true if view has hidden rows
1107 public boolean hasHiddenRows()
1109 return hasHiddenRows;
1114 * @return true if view has hidden columns
1116 public boolean hasHiddenColumns()
1118 return hasHiddenColumns;
1122 * when set, view will scroll to show the highlighted position
1124 public boolean followHighlight = true;
1127 * @return true if view should scroll to show the highlighted region of a
1131 public boolean getFollowHighlight()
1133 return followHighlight;
1136 public boolean followSelection = true;
1139 * @return true if view selection should always follow the selections
1140 * broadcast by other selection sources
1142 public boolean getFollowSelection()
1144 return followSelection;
1147 boolean showSeqFeaturesHeight;
1149 public void sendSelection()
1151 jalview.structure.StructureSelectionManager
1152 .getStructureSelectionManager(Desktop.instance).sendSelection(
1153 new SequenceGroup(getSelectionGroup()),
1154 new ColumnSelection(getColumnSelection()), this);
1157 public void setShowSequenceFeaturesHeight(boolean selected)
1159 showSeqFeaturesHeight = selected;
1162 public boolean getShowSequenceFeaturesHeight()
1164 return showSeqFeaturesHeight;
1168 * return the alignPanel containing the given viewport. Use this to get the
1169 * components currently handling the given viewport.
1172 * @return null or an alignPanel guaranteed to have non-null alignFrame
1175 public AlignmentPanel getAlignPanel()
1177 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1178 .getSequenceSetId());
1179 AlignmentPanel ap = null;
1180 for (int p = 0; aps != null && p < aps.length; p++)
1182 if (aps[p].av == this)
1190 public boolean getSortByTree()
1195 public void setSortByTree(boolean sort)
1201 * synthesize a column selection if none exists so it covers the given
1202 * selection group. if wholewidth is false, no column selection is made if the
1203 * selection group covers the whole alignment width.
1208 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1212 && (sgs = sg.getStartRes()) >= 0
1213 && sg.getStartRes() <= (sge = sg.getEndRes())
1214 && (colSel == null || colSel.getSelected() == null || colSel
1215 .getSelected().size() == 0))
1217 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1224 colSel = new ColumnSelection();
1226 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1228 colSel.addElement(cspos);
1233 public StructureSelectionManager getStructureSelectionManager()
1235 return StructureSelectionManager
1236 .getStructureSelectionManager(Desktop.instance);
1242 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1243 * sequence in the alignment holds a reference to it
1245 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1247 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1248 for (PDBEntry pdb : pdbEntries)
1250 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1251 for (int i = 0; i < alignment.getHeight(); i++)
1253 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1258 for (int p = 0; p < pdbs.size(); p++)
1260 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1261 if (p1.getId().equals(pdb.getId()))
1263 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1270 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1272 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1275 public boolean isNormaliseSequenceLogo()
1277 return normaliseSequenceLogo;
1280 public void setNormaliseSequenceLogo(boolean state)
1282 normaliseSequenceLogo = state;
1287 * @return true if alignment characters should be displayed
1289 public boolean isValidCharWidth()
1291 return validCharWidth;
1294 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1296 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1298 return calcIdParams.get(calcId);
1301 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1302 boolean needsUpdate)
1304 calcIdParams.put(calcId, settings);
1305 // TODO: create a restart list to trigger any calculations that need to be
1306 // restarted after load
1307 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1310 Cache.log.debug("trigger update for " + calcId);