2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
20 * Jalview - A Sequence Alignment Editor and Viewer
21 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
39 import jalview.analysis.NJTree;
40 import jalview.api.AlignViewportI;
41 import jalview.bin.Cache;
42 import jalview.datamodel.AlignmentAnnotation;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.ColumnSelection;
46 import jalview.datamodel.PDBEntry;
47 import jalview.datamodel.Sequence;
48 import jalview.datamodel.SequenceGroup;
49 import jalview.datamodel.SequenceI;
50 import jalview.schemes.ColourSchemeProperty;
51 import jalview.schemes.UserColourScheme;
52 import jalview.structure.SelectionSource;
53 import jalview.structure.StructureSelectionManager;
54 import jalview.structure.VamsasSource;
55 import jalview.viewmodel.AlignmentViewport;
56 import jalview.ws.params.AutoCalcSetting;
58 import java.awt.Color;
59 import java.awt.Container;
61 import java.awt.Rectangle;
62 import java.util.ArrayList;
63 import java.util.Hashtable;
64 import java.util.Stack;
65 import java.util.Vector;
71 * @version $Revision: 1.141 $
73 public class AlignViewport extends AlignmentViewport implements
74 SelectionSource, VamsasSource, AlignViewportI
84 boolean showJVSuffix = true;
86 boolean showText = true;
88 boolean showColourText = false;
90 boolean showBoxes = true;
92 boolean wrapAlignment = false;
94 boolean renderGaps = true;
96 boolean showSequenceFeatures = false;
98 boolean showAnnotation = true;
104 boolean validCharWidth;
110 boolean seqNameItalics;
112 NJTree currentTree = null;
114 boolean scaleAboveWrapped = false;
116 boolean scaleLeftWrapped = true;
118 boolean scaleRightWrapped = true;
120 boolean showHiddenMarkers = true;
122 boolean cursorMode = false;
125 * Keys are the feature types which are currently visible. Note: Values are
128 Hashtable featuresDisplayed = null;
130 boolean antiAlias = false;
132 Rectangle explodedPosition;
136 boolean gatherViewsHere = false;
138 Stack historyList = new Stack();
140 Stack redoList = new Stack();
142 int thresholdTextColour = 0;
144 Color textColour = Color.black;
146 Color textColour2 = Color.white;
148 boolean rightAlignIds = false;
151 * Creates a new AlignViewport object.
156 public AlignViewport(AlignmentI al)
163 * Create a new AlignViewport object with a specific sequence set ID
167 * (may be null - but potential for ambiguous constructor exception)
169 public AlignViewport(AlignmentI al, String seqsetid)
171 this(al, seqsetid, null);
174 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
176 sequenceSetID = seqsetid;
178 // TODO remove these once 2.4.VAMSAS release finished
179 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
181 Cache.log.debug("Setting viewport's sequence set id : "
184 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
186 Cache.log.debug("Setting viewport's view id : " + viewId);
193 * Create a new AlignViewport with hidden regions
197 * @param hiddenColumns
200 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
203 if (hiddenColumns != null)
205 this.colSel = hiddenColumns;
206 if (hiddenColumns.getHiddenColumns() != null
207 && hiddenColumns.getHiddenColumns().size() > 0)
209 hasHiddenColumns = true;
213 hasHiddenColumns = false;
220 * New viewport with hidden columns and an existing sequence set id
223 * @param hiddenColumns
227 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
230 this(al, hiddenColumns, seqsetid, null);
234 * New viewport with hidden columns and an existing sequence set id and viewid
237 * @param hiddenColumns
243 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
244 String seqsetid, String viewid)
246 sequenceSetID = seqsetid;
248 // TODO remove these once 2.4.VAMSAS release finished
249 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
251 Cache.log.debug("Setting viewport's sequence set id : "
254 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
256 Cache.log.debug("Setting viewport's view id : " + viewId);
259 if (hiddenColumns != null)
261 this.colSel = hiddenColumns;
262 if (hiddenColumns.getHiddenColumns() != null
263 && hiddenColumns.getHiddenColumns().size() > 0)
265 hasHiddenColumns = true;
269 hasHiddenColumns = false;
278 this.endRes = alignment.getWidth() - 1;
280 this.endSeq = alignment.getHeight() - 1;
282 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
284 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
285 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
287 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
288 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
289 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
291 setPadGaps(Cache.getDefault("PAD_GAPS", true));
292 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
293 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
295 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
296 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
297 String fontSize = Cache.getDefault("FONT_SIZE", "10");
299 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
303 if (fontStyle.equals("bold"))
307 else if (fontStyle.equals("italic"))
312 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
315 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
317 // We must set conservation and consensus before setting colour,
318 // as Blosum and Clustal require this to be done
319 if (hconsensus == null && !isDataset)
321 if (!alignment.isNucleotide())
323 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
324 showQuality = Cache.getDefault("SHOW_QUALITY", true);
325 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
328 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
330 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
331 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO",
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;
916 public void setDataset(boolean b)
921 public boolean isDataset()
926 public boolean getShowHiddenMarkers()
928 return showHiddenMarkers;
931 public void setShowHiddenMarkers(boolean show)
933 showHiddenMarkers = show;
937 * returns the visible column regions of the alignment
939 * @param selectedRegionOnly
940 * true to just return the contigs intersecting with the selected
944 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
946 int[] viscontigs = null;
947 int start = 0, end = 0;
948 if (selectedRegionOnly && selectionGroup != null)
950 start = selectionGroup.getStartRes();
951 end = selectionGroup.getEndRes() + 1;
955 end = alignment.getWidth();
957 viscontigs = colSel.getVisibleContigs(start, end);
962 * get hash of undo and redo list for the alignment
964 * @return long[] { historyList.hashCode, redoList.hashCode };
966 public long[] getUndoRedoHash()
969 if (historyList == null || redoList == null)
973 { historyList.hashCode(), this.redoList.hashCode() };
977 * test if a particular set of hashcodes are different to the hashcodes for
978 * the undo and redo list.
981 * the stored set of hashcodes as returned by getUndoRedoHash
982 * @return true if the hashcodes differ (ie the alignment has been edited) or
983 * the stored hashcode array differs in size
985 public boolean isUndoRedoHashModified(long[] undoredo)
987 if (undoredo == null)
991 long[] cstate = getUndoRedoHash();
992 if (cstate.length != undoredo.length)
997 for (int i = 0; i < cstate.length; i++)
999 if (cstate[i] != undoredo[i])
1007 public boolean getCentreColumnLabels()
1009 return centreColumnLabels;
1012 public void setCentreColumnLabels(boolean centrecolumnlabels)
1014 centreColumnLabels = centrecolumnlabels;
1019 * enable or disable the display of Database Cross References in the sequence
1022 public void setShowDbRefs(boolean show)
1029 * @return true if Database References are to be displayed on tooltips.
1031 public boolean isShowDbRefs()
1038 * @return true if Non-positional features are to be displayed on tooltips.
1040 public boolean isShowNpFeats()
1046 * enable or disable the display of Non-Positional sequence features in the
1047 * sequence ID tooltip
1051 public void setShowNpFeats(boolean show)
1058 * @return true if view has hidden rows
1060 public boolean hasHiddenRows()
1062 return hasHiddenRows;
1067 * @return true if view has hidden columns
1069 public boolean hasHiddenColumns()
1071 return hasHiddenColumns;
1075 * when set, view will scroll to show the highlighted position
1077 public boolean followHighlight = true;
1080 * @return true if view should scroll to show the highlighted region of a
1084 public boolean getFollowHighlight()
1086 return followHighlight;
1089 public boolean followSelection = true;
1092 * @return true if view selection should always follow the selections
1093 * broadcast by other selection sources
1095 public boolean getFollowSelection()
1097 return followSelection;
1100 boolean showSeqFeaturesHeight;
1102 public void sendSelection()
1104 jalview.structure.StructureSelectionManager
1105 .getStructureSelectionManager(Desktop.instance).sendSelection(
1106 new SequenceGroup(getSelectionGroup()),
1107 new ColumnSelection(getColumnSelection()), this);
1110 public void setShowSequenceFeaturesHeight(boolean selected)
1112 showSeqFeaturesHeight = selected;
1115 public boolean getShowSequenceFeaturesHeight()
1117 return showSeqFeaturesHeight;
1121 * return the alignPanel containing the given viewport. Use this to get the
1122 * components currently handling the given viewport.
1125 * @return null or an alignPanel guaranteed to have non-null alignFrame
1128 public AlignmentPanel getAlignPanel()
1130 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1131 .getSequenceSetId());
1132 AlignmentPanel ap = null;
1133 for (int p = 0; aps != null && p < aps.length; p++)
1135 if (aps[p].av == this)
1143 public boolean getSortByTree()
1148 public void setSortByTree(boolean sort)
1154 * synthesize a column selection if none exists so it covers the given
1155 * selection group. if wholewidth is false, no column selection is made if the
1156 * selection group covers the whole alignment width.
1161 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1165 && (sgs = sg.getStartRes()) >= 0
1166 && sg.getStartRes() <= (sge = sg.getEndRes())
1167 && (colSel == null || colSel.getSelected() == null || colSel
1168 .getSelected().size() == 0))
1170 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1177 colSel = new ColumnSelection();
1179 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1181 colSel.addElement(cspos);
1186 public StructureSelectionManager getStructureSelectionManager()
1188 return StructureSelectionManager
1189 .getStructureSelectionManager(Desktop.instance);
1195 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1196 * sequence in the alignment holds a reference to it
1198 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1200 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1201 for (PDBEntry pdb : pdbEntries)
1203 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1204 for (int i = 0; i < alignment.getHeight(); i++)
1206 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1211 for (int p = 0; p < pdbs.size(); p++)
1213 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1214 if (p1.getId().equals(pdb.getId()))
1216 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1223 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1225 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1228 public boolean isNormaliseSequenceLogo()
1230 return normaliseSequenceLogo;
1233 public void setNormaliseSequenceLogo(boolean state)
1235 normaliseSequenceLogo = state;
1240 * @return true if alignment characters should be displayed
1242 public boolean isValidCharWidth()
1244 return validCharWidth;
1247 private Hashtable<String, AutoCalcSetting> calcIdParams = new Hashtable<String, AutoCalcSetting>();
1249 public AutoCalcSetting getCalcIdSettingsFor(String calcId)
1251 return calcIdParams.get(calcId);
1254 public void setCalcIdSettingsFor(String calcId, AutoCalcSetting settings,
1255 boolean needsUpdate)
1257 calcIdParams.put(calcId, settings);
1258 // TODO: create a restart list to trigger any calculations that need to be
1259 // restarted after load
1260 // calculator.getRegisteredWorkersOfClass(settings.getWorkerClass())
1263 Cache.log.debug("trigger update for " + calcId);