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
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 SelectionSource, VamsasSource, AlignViewportI
82 boolean showJVSuffix = true;
84 boolean showText = true;
86 boolean showColourText = false;
88 boolean showBoxes = true;
90 boolean wrapAlignment = false;
92 boolean renderGaps = true;
94 boolean showSequenceFeatures = false;
96 boolean showAnnotation = true;
102 boolean validCharWidth;
108 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", false);
333 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
334 showConsensus=Cache.getDefault("SHOW_IDENTITY", true);
335 consensus = new AlignmentAnnotation("Consensus", "PID",
336 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
337 consensus.hasText = true;
338 consensus.autoCalculated = true;
340 initAutoAnnotation();
341 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
343 globalColourScheme = ColourSchemeProperty.getColour(alignment,
344 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
346 if (globalColourScheme instanceof UserColourScheme)
348 globalColourScheme = UserDefinedColours.loadDefaultColours();
349 ((UserColourScheme) globalColourScheme).setThreshold(0,
350 getIgnoreGapsConsensus());
353 if (globalColourScheme != null)
355 globalColourScheme.setConsensus(hconsensus);
359 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
360 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
362 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
363 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
371 * features are displayed if true
373 public void setShowSequenceFeatures(boolean b)
375 showSequenceFeatures = b;
378 public boolean getShowSequenceFeatures()
380 return showSequenceFeatures;
384 * centre columnar annotation labels in displayed alignment annotation TODO:
385 * add to jalviewXML and annotation display settings
387 boolean centreColumnLabels = false;
389 private boolean showdbrefs;
391 private boolean shownpfeats;
393 // --------END Structure Conservation
396 * get the consensus sequence as displayed under the PID consensus annotation
399 * @return consensus sequence as a new sequence object
401 public SequenceI getConsensusSeq()
403 if (consensus == null)
405 updateConsensus(null);
407 if (consensus == null)
411 StringBuffer seqs = new StringBuffer();
412 for (int i = 0; i < consensus.annotations.length; i++)
414 if (consensus.annotations[i] != null)
416 if (consensus.annotations[i].description.charAt(0) == '[')
418 seqs.append(consensus.annotations[i].description.charAt(1));
422 seqs.append(consensus.annotations[i].displayCharacter);
427 SequenceI sq = new Sequence("Consensus", seqs.toString());
428 sq.setDescription("Percentage Identity Consensus "
429 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
436 * @return DOCUMENT ME!
438 public int getStartRes()
446 * @return DOCUMENT ME!
448 public int getEndRes()
456 * @return DOCUMENT ME!
458 public int getStartSeq()
469 public void setStartRes(int res)
480 public void setStartSeq(int seq)
491 public void setEndRes(int res)
493 if (res > (alignment.getWidth() - 1))
495 // log.System.out.println(" Corrected res from " + res + " to maximum " +
496 // (alignment.getWidth()-1));
497 res = alignment.getWidth() - 1;
514 public void setEndSeq(int seq)
516 if (seq > alignment.getHeight())
518 seq = alignment.getHeight();
532 * @return DOCUMENT ME!
534 public int getEndSeq()
545 public void setFont(Font f)
549 Container c = new Container();
551 java.awt.FontMetrics fm = c.getFontMetrics(font);
552 setCharHeight(fm.getHeight());
553 setCharWidth(fm.charWidth('M'));
554 validCharWidth = true;
560 * @return DOCUMENT ME!
562 public Font getFont()
573 public void setCharWidth(int w)
581 * @return DOCUMENT ME!
583 public int getCharWidth()
594 public void setCharHeight(int h)
602 * @return DOCUMENT ME!
604 public int getCharHeight()
615 public void setWrappedWidth(int w)
617 this.wrappedWidth = w;
623 * @return DOCUMENT ME!
625 public int getWrappedWidth()
633 * @return DOCUMENT ME!
635 public AlignmentI getAlignment()
646 public void setAlignment(AlignmentI align)
648 if (alignment != null && alignment.getCodonFrames() != null)
650 StructureSelectionManager.getStructureSelectionManager(
651 Desktop.instance).removeMappings(alignment.getCodonFrames());
653 this.alignment = align;
654 if (alignment!=null && alignment.getCodonFrames() != null)
656 StructureSelectionManager.getStructureSelectionManager(
657 Desktop.instance).addMappings(alignment.getCodonFrames());
667 public void setWrapAlignment(boolean state)
669 wrapAlignment = state;
678 public void setShowText(boolean state)
689 public void setRenderGaps(boolean state)
697 * @return DOCUMENT ME!
699 public boolean getColourText()
701 return showColourText;
710 public void setColourText(boolean state)
712 showColourText = state;
721 public void setShowBoxes(boolean state)
729 * @return DOCUMENT ME!
731 public boolean getWrapAlignment()
733 return wrapAlignment;
739 * @return DOCUMENT ME!
741 public boolean getShowText()
749 * @return DOCUMENT ME!
751 public boolean getShowBoxes()
759 * @return DOCUMENT ME!
761 public char getGapCharacter()
763 return getAlignment().getGapCharacter();
772 public void setGapCharacter(char gap)
774 if (getAlignment() != null)
776 getAlignment().setGapCharacter(gap);
783 * @return DOCUMENT ME!
785 public ColumnSelection getColumnSelection()
796 public void setCurrentTree(NJTree tree)
804 * @return DOCUMENT ME!
806 public NJTree getCurrentTree()
814 * @return DOCUMENT ME!
816 public boolean getShowJVSuffix()
827 public void setShowJVSuffix(boolean b)
835 * @return DOCUMENT ME!
837 public boolean getShowAnnotation()
839 return showAnnotation;
848 public void setShowAnnotation(boolean b)
856 * @return DOCUMENT ME!
858 public boolean getScaleAboveWrapped()
860 return scaleAboveWrapped;
866 * @return DOCUMENT ME!
868 public boolean getScaleLeftWrapped()
870 return scaleLeftWrapped;
876 * @return DOCUMENT ME!
878 public boolean getScaleRightWrapped()
880 return scaleRightWrapped;
889 public void setScaleAboveWrapped(boolean b)
891 scaleAboveWrapped = b;
900 public void setScaleLeftWrapped(boolean b)
902 scaleLeftWrapped = b;
911 public void setScaleRightWrapped(boolean b)
913 scaleRightWrapped = b;
917 public void setDataset(boolean b)
922 public boolean isDataset()
929 public boolean getShowHiddenMarkers()
931 return showHiddenMarkers;
934 public void setShowHiddenMarkers(boolean show)
936 showHiddenMarkers = show;
939 public Color getSequenceColour(SequenceI seq)
941 if (sequenceColours == null || !sequenceColours.containsKey(seq))
947 return (Color) sequenceColours.get(seq);
951 public void setSequenceColour(SequenceI seq, Color col)
953 if (sequenceColours == null)
955 sequenceColours = new Hashtable();
960 sequenceColours.remove(seq);
964 sequenceColours.put(seq, col);
969 * returns the visible column regions of the alignment
971 * @param selectedRegionOnly
972 * true to just return the contigs intersecting with the selected
976 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
978 int[] viscontigs = null;
979 int start = 0, end = 0;
980 if (selectedRegionOnly && selectionGroup != null)
982 start = selectionGroup.getStartRes();
983 end = selectionGroup.getEndRes() + 1;
987 end = alignment.getWidth();
989 viscontigs = colSel.getVisibleContigs(start, end);
994 * get hash of undo and redo list for the alignment
996 * @return long[] { historyList.hashCode, redoList.hashCode };
998 public long[] getUndoRedoHash()
1001 if (historyList == null || redoList == null)
1005 { historyList.hashCode(), this.redoList.hashCode() };
1009 * test if a particular set of hashcodes are different to the hashcodes for
1010 * the undo and redo list.
1013 * the stored set of hashcodes as returned by getUndoRedoHash
1014 * @return true if the hashcodes differ (ie the alignment has been edited) or
1015 * the stored hashcode array differs in size
1017 public boolean isUndoRedoHashModified(long[] undoredo)
1019 if (undoredo == null)
1023 long[] cstate = getUndoRedoHash();
1024 if (cstate.length != undoredo.length)
1029 for (int i = 0; i < cstate.length; i++)
1031 if (cstate[i] != undoredo[i])
1039 public boolean getCentreColumnLabels()
1041 return centreColumnLabels;
1044 public void setCentreColumnLabels(boolean centrecolumnlabels)
1046 centreColumnLabels = centrecolumnlabels;
1049 public void updateSequenceIdColours()
1051 if (sequenceColours == null)
1053 sequenceColours = new Hashtable();
1055 for (SequenceGroup sg:alignment.getGroups())
1057 if (sg.idColour != null)
1059 for (SequenceI s:sg.getSequences(getHiddenRepSequences()))
1061 sequenceColours.put(s, sg.idColour);
1068 * enable or disable the display of Database Cross References in the sequence
1071 public void setShowDbRefs(boolean show)
1078 * @return true if Database References are to be displayed on tooltips.
1080 public boolean isShowDbRefs()
1087 * @return true if Non-positional features are to be displayed on tooltips.
1089 public boolean isShowNpFeats()
1095 * enable or disable the display of Non-Positional sequence features in the
1096 * sequence ID tooltip
1100 public void setShowNpFeats(boolean show)
1107 * @return true if view has hidden rows
1109 public boolean hasHiddenRows()
1111 return hasHiddenRows;
1116 * @return true if view has hidden columns
1118 public boolean hasHiddenColumns()
1120 return hasHiddenColumns;
1124 * when set, view will scroll to show the highlighted position
1126 public boolean followHighlight = true;
1129 * @return true if view should scroll to show the highlighted region of a
1133 public boolean getFollowHighlight()
1135 return followHighlight;
1138 public boolean followSelection = true;
1141 * @return true if view selection should always follow the selections
1142 * broadcast by other selection sources
1144 public boolean getFollowSelection()
1146 return followSelection;
1149 boolean showSeqFeaturesHeight;
1151 public void sendSelection()
1153 jalview.structure.StructureSelectionManager
1154 .getStructureSelectionManager(Desktop.instance).sendSelection(
1155 new SequenceGroup(getSelectionGroup()),
1156 new ColumnSelection(getColumnSelection()), this);
1159 public void setShowSequenceFeaturesHeight(boolean selected)
1161 showSeqFeaturesHeight = selected;
1164 public boolean getShowSequenceFeaturesHeight()
1166 return showSeqFeaturesHeight;
1170 * return the alignPanel containing the given viewport. Use this to get the
1171 * components currently handling the given viewport.
1174 * @return null or an alignPanel guaranteed to have non-null alignFrame
1177 public AlignmentPanel getAlignPanel()
1179 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1180 .getSequenceSetId());
1181 AlignmentPanel ap = null;
1182 for (int p = 0; aps != null && p < aps.length; p++)
1184 if (aps[p].av == this)
1192 public boolean getSortByTree()
1197 public void setSortByTree(boolean sort)
1203 * synthesize a column selection if none exists so it covers the given
1204 * selection group. if wholewidth is false, no column selection is made if the
1205 * selection group covers the whole alignment width.
1210 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1214 && (sgs = sg.getStartRes()) >= 0
1215 && sg.getStartRes() <= (sge = sg.getEndRes())
1216 && (colSel == null || colSel.getSelected() == null || colSel
1217 .getSelected().size() == 0))
1219 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1226 colSel = new ColumnSelection();
1228 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1230 colSel.addElement(cspos);
1235 public StructureSelectionManager getStructureSelectionManager()
1237 return StructureSelectionManager
1238 .getStructureSelectionManager(Desktop.instance);
1244 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1245 * sequence in the alignment holds a reference to it
1247 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1249 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1250 for (PDBEntry pdb : pdbEntries)
1252 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1253 for (int i = 0; i < alignment.getHeight(); i++)
1255 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1260 for (int p = 0; p < pdbs.size(); p++)
1262 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1263 if (p1.getId().equals(pdb.getId()))
1265 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1272 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1274 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1278 public boolean isNormaliseSequenceLogo()
1280 return normaliseSequenceLogo;
1283 public void setNormaliseSequenceLogo(boolean state)
1285 normaliseSequenceLogo = state;
1291 * @return true if alignment characters should be displayed
1293 public boolean isValidCharWidth()
1295 return validCharWidth;
1297 private Hashtable<String, AutoCalcSetting> calcIdParams=new Hashtable<String, AutoCalcSetting>();
1298 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1300 return calcIdParams.get(calcId);
1303 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings, boolean needsUpdate)
1305 calcIdParams.put(calcId, settings);
1306 // TODO: create a restart list to trigger any calculations that need to be restarted after load
1307 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1310 Cache.log.debug("trigger update for "+calcId);