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;
61 import jalview.ws.jws2.dm.AAConsSettings;
62 import jalview.ws.params.AutoCalcSetting;
68 * @version $Revision: 1.141 $
70 public class AlignViewport extends AlignmentViewport implements SelectionSource, VamsasSource, AlignViewportI
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;
100 boolean validCharWidth;
106 boolean seqNameItalics;
109 NJTree currentTree = null;
111 boolean scaleAboveWrapped = false;
113 boolean scaleLeftWrapped = true;
115 boolean scaleRightWrapped = true;
117 boolean showHiddenMarkers = true;
119 boolean cursorMode = false;
122 * Keys are the feature types which are currently visible. Note: Values are
125 Hashtable featuresDisplayed = null;
127 boolean antiAlias = false;
129 Rectangle explodedPosition;
133 boolean gatherViewsHere = false;
135 Stack historyList = new Stack();
137 Stack redoList = new Stack();
139 Hashtable sequenceColours;
141 int thresholdTextColour = 0;
143 Color textColour = Color.black;
145 Color textColour2 = Color.white;
147 boolean rightAlignIds = false;
150 * Creates a new AlignViewport object.
155 public AlignViewport(AlignmentI al)
162 * Create a new AlignViewport object with a specific sequence set ID
166 * (may be null - but potential for ambiguous constructor exception)
168 public AlignViewport(AlignmentI al, String seqsetid)
170 this(al, seqsetid, null);
173 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
175 sequenceSetID = seqsetid;
177 // TODO remove these once 2.4.VAMSAS release finished
178 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
180 Cache.log.debug("Setting viewport's sequence set id : "
183 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
185 Cache.log.debug("Setting viewport's view id : " + viewId);
192 * Create a new AlignViewport with hidden regions
196 * @param hiddenColumns
199 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
202 if (hiddenColumns != null)
204 this.colSel = hiddenColumns;
205 if (hiddenColumns.getHiddenColumns() != null
206 && hiddenColumns.getHiddenColumns().size() > 0)
208 hasHiddenColumns = true;
212 hasHiddenColumns = false;
219 * New viewport with hidden columns and an existing sequence set id
222 * @param hiddenColumns
226 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
229 this(al, hiddenColumns, seqsetid, null);
233 * New viewport with hidden columns and an existing sequence set id and viewid
236 * @param hiddenColumns
242 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
243 String seqsetid, String viewid)
245 sequenceSetID = seqsetid;
247 // TODO remove these once 2.4.VAMSAS release finished
248 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
250 Cache.log.debug("Setting viewport's sequence set id : "
253 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
255 Cache.log.debug("Setting viewport's view id : " + viewId);
258 if (hiddenColumns != null)
260 this.colSel = hiddenColumns;
261 if (hiddenColumns.getHiddenColumns() != null
262 && hiddenColumns.getHiddenColumns().size() > 0)
264 hasHiddenColumns = true;
268 hasHiddenColumns = false;
277 this.endRes = alignment.getWidth() - 1;
279 this.endSeq = alignment.getHeight() - 1;
281 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
283 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
284 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
286 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
287 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
288 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
290 setPadGaps(Cache.getDefault("PAD_GAPS", true));
291 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
292 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
294 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
295 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
296 String fontSize = Cache.getDefault("FONT_SIZE", "10");
298 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
302 if (fontStyle.equals("bold"))
306 else if (fontStyle.equals("italic"))
311 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
314 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
316 // We must set conservation and consensus before setting colour,
317 // as Blosum and Clustal require this to be done
318 if (hconsensus == null && !isDataset)
320 if (!alignment.isNucleotide())
322 showConservation=Cache.getDefault("SHOW_CONSERVATION", true);
323 showQuality=Cache.getDefault("SHOW_QUALITY", true);
324 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
327 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
329 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
330 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO", false);
331 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
332 showConsensus=Cache.getDefault("SHOW_IDENTITY", true);
333 consensus = new AlignmentAnnotation("Consensus", "PID",
334 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
335 consensus.hasText = true;
336 consensus.autoCalculated = true;
338 initAutoAnnotation();
339 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
341 globalColourScheme = ColourSchemeProperty.getColour(alignment,
342 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
344 if (globalColourScheme instanceof UserColourScheme)
346 globalColourScheme = UserDefinedColours.loadDefaultColours();
347 ((UserColourScheme) globalColourScheme).setThreshold(0,
348 getIgnoreGapsConsensus());
351 if (globalColourScheme != null)
353 globalColourScheme.setConsensus(hconsensus);
357 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
358 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
360 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
361 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
369 * features are displayed if true
371 public void setShowSequenceFeatures(boolean b)
373 showSequenceFeatures = b;
376 public boolean getShowSequenceFeatures()
378 return showSequenceFeatures;
382 * centre columnar annotation labels in displayed alignment annotation TODO:
383 * add to jalviewXML and annotation display settings
385 boolean centreColumnLabels = false;
387 private boolean showdbrefs;
389 private boolean shownpfeats;
391 // --------END Structure Conservation
394 * get the consensus sequence as displayed under the PID consensus annotation
397 * @return consensus sequence as a new sequence object
399 public SequenceI getConsensusSeq()
401 if (consensus == null)
403 updateConsensus(null);
405 if (consensus == null)
409 StringBuffer seqs = new StringBuffer();
410 for (int i = 0; i < consensus.annotations.length; i++)
412 if (consensus.annotations[i] != null)
414 if (consensus.annotations[i].description.charAt(0) == '[')
416 seqs.append(consensus.annotations[i].description.charAt(1));
420 seqs.append(consensus.annotations[i].displayCharacter);
425 SequenceI sq = new Sequence("Consensus", seqs.toString());
426 sq.setDescription("Percentage Identity Consensus "
427 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
434 * @return DOCUMENT ME!
436 public int getStartRes()
444 * @return DOCUMENT ME!
446 public int getEndRes()
454 * @return DOCUMENT ME!
456 public int getStartSeq()
467 public void setStartRes(int res)
478 public void setStartSeq(int seq)
489 public void setEndRes(int res)
491 if (res > (alignment.getWidth() - 1))
493 // log.System.out.println(" Corrected res from " + res + " to maximum " +
494 // (alignment.getWidth()-1));
495 res = alignment.getWidth() - 1;
512 public void setEndSeq(int seq)
514 if (seq > alignment.getHeight())
516 seq = alignment.getHeight();
530 * @return DOCUMENT ME!
532 public int getEndSeq()
543 public void setFont(Font f)
547 Container c = new Container();
549 java.awt.FontMetrics fm = c.getFontMetrics(font);
550 setCharHeight(fm.getHeight());
551 setCharWidth(fm.charWidth('M'));
552 validCharWidth = true;
558 * @return DOCUMENT ME!
560 public Font getFont()
571 public void setCharWidth(int w)
579 * @return DOCUMENT ME!
581 public int getCharWidth()
592 public void setCharHeight(int h)
600 * @return DOCUMENT ME!
602 public int getCharHeight()
613 public void setWrappedWidth(int w)
615 this.wrappedWidth = w;
621 * @return DOCUMENT ME!
623 public int getWrappedWidth()
631 * @return DOCUMENT ME!
633 public AlignmentI getAlignment()
644 public void setAlignment(AlignmentI align)
646 if (alignment != null && alignment.getCodonFrames() != null)
648 StructureSelectionManager.getStructureSelectionManager(
649 Desktop.instance).removeMappings(alignment.getCodonFrames());
651 this.alignment = align;
652 if (alignment!=null && alignment.getCodonFrames() != null)
654 StructureSelectionManager.getStructureSelectionManager(
655 Desktop.instance).addMappings(alignment.getCodonFrames());
665 public void setWrapAlignment(boolean state)
667 wrapAlignment = state;
676 public void setShowText(boolean state)
687 public void setRenderGaps(boolean state)
695 * @return DOCUMENT ME!
697 public boolean getColourText()
699 return showColourText;
708 public void setColourText(boolean state)
710 showColourText = state;
719 public void setShowBoxes(boolean state)
727 * @return DOCUMENT ME!
729 public boolean getWrapAlignment()
731 return wrapAlignment;
737 * @return DOCUMENT ME!
739 public boolean getShowText()
747 * @return DOCUMENT ME!
749 public boolean getShowBoxes()
757 * @return DOCUMENT ME!
759 public char getGapCharacter()
761 return getAlignment().getGapCharacter();
770 public void setGapCharacter(char gap)
772 if (getAlignment() != null)
774 getAlignment().setGapCharacter(gap);
781 * @return DOCUMENT ME!
783 public ColumnSelection getColumnSelection()
794 public void setCurrentTree(NJTree tree)
802 * @return DOCUMENT ME!
804 public NJTree getCurrentTree()
812 * @return DOCUMENT ME!
814 public boolean getShowJVSuffix()
825 public void setShowJVSuffix(boolean b)
833 * @return DOCUMENT ME!
835 public boolean getShowAnnotation()
837 return showAnnotation;
846 public void setShowAnnotation(boolean b)
854 * @return DOCUMENT ME!
856 public boolean getScaleAboveWrapped()
858 return scaleAboveWrapped;
864 * @return DOCUMENT ME!
866 public boolean getScaleLeftWrapped()
868 return scaleLeftWrapped;
874 * @return DOCUMENT ME!
876 public boolean getScaleRightWrapped()
878 return scaleRightWrapped;
887 public void setScaleAboveWrapped(boolean b)
889 scaleAboveWrapped = b;
898 public void setScaleLeftWrapped(boolean b)
900 scaleLeftWrapped = b;
909 public void setScaleRightWrapped(boolean b)
911 scaleRightWrapped = b;
915 public void setDataset(boolean b)
920 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()][]);
1276 public boolean isNormaliseSequenceLogo()
1278 return normaliseSequenceLogo;
1281 public void setNormaliseSequenceLogo(boolean state)
1283 normaliseSequenceLogo = state;
1289 * @return true if alignment characters should be displayed
1291 public boolean isValidCharWidth()
1293 return validCharWidth;
1295 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, boolean needsUpdate)
1303 calcIdParams.put(calcId, settings);
1304 // TODO: create a restart list to trigger any calculations that need to be restarted after load
1305 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())