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
78 boolean showJVSuffix = true;
80 boolean showText = true;
82 boolean showColourText = false;
84 boolean showBoxes = true;
86 boolean wrapAlignment = false;
88 boolean renderGaps = true;
90 boolean showSequenceFeatures = false;
92 boolean showAnnotation = true;
98 boolean validCharWidth;
104 boolean seqNameItalics;
107 NJTree currentTree = null;
109 boolean scaleAboveWrapped = false;
111 boolean scaleLeftWrapped = true;
113 boolean scaleRightWrapped = true;
115 boolean showHiddenMarkers = true;
117 boolean cursorMode = false;
120 * Keys are the feature types which are currently visible. Note: Values are
123 Hashtable featuresDisplayed = null;
125 boolean antiAlias = false;
127 Rectangle explodedPosition;
131 boolean gatherViewsHere = false;
133 Stack historyList = new Stack();
135 Stack redoList = new Stack();
137 Hashtable sequenceColours;
139 int thresholdTextColour = 0;
141 Color textColour = Color.black;
143 Color textColour2 = Color.white;
145 boolean rightAlignIds = false;
148 * Creates a new AlignViewport object.
153 public AlignViewport(AlignmentI al)
160 * Create a new AlignViewport object with a specific sequence set ID
164 * (may be null - but potential for ambiguous constructor exception)
166 public AlignViewport(AlignmentI al, String seqsetid)
168 this(al, seqsetid, null);
171 public AlignViewport(AlignmentI al, String seqsetid, String viewid)
173 sequenceSetID = seqsetid;
175 // TODO remove these once 2.4.VAMSAS release finished
176 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
178 Cache.log.debug("Setting viewport's sequence set id : "
181 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
183 Cache.log.debug("Setting viewport's view id : " + viewId);
190 * Create a new AlignViewport with hidden regions
194 * @param hiddenColumns
197 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns)
200 if (hiddenColumns != null)
202 this.colSel = hiddenColumns;
203 if (hiddenColumns.getHiddenColumns() != null
204 && hiddenColumns.getHiddenColumns().size() > 0)
206 hasHiddenColumns = true;
210 hasHiddenColumns = false;
217 * New viewport with hidden columns and an existing sequence set id
220 * @param hiddenColumns
224 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
227 this(al, hiddenColumns, seqsetid, null);
231 * New viewport with hidden columns and an existing sequence set id and viewid
234 * @param hiddenColumns
240 public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns,
241 String seqsetid, String viewid)
243 sequenceSetID = seqsetid;
245 // TODO remove these once 2.4.VAMSAS release finished
246 if (Cache.log != null && Cache.log.isDebugEnabled() && seqsetid != null)
248 Cache.log.debug("Setting viewport's sequence set id : "
251 if (Cache.log != null && Cache.log.isDebugEnabled() && viewId != null)
253 Cache.log.debug("Setting viewport's view id : " + viewId);
256 if (hiddenColumns != null)
258 this.colSel = hiddenColumns;
259 if (hiddenColumns.getHiddenColumns() != null
260 && hiddenColumns.getHiddenColumns().size() > 0)
262 hasHiddenColumns = true;
266 hasHiddenColumns = false;
275 this.endRes = alignment.getWidth() - 1;
277 this.endSeq = alignment.getHeight() - 1;
279 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
281 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
282 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
284 rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
285 centreColumnLabels = Cache.getDefault("CENTRE_COLUMN_LABELS", false);
286 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
288 setPadGaps(Cache.getDefault("PAD_GAPS", true));
289 shownpfeats = Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true);
290 showdbrefs = Cache.getDefault("SHOW_DBREFS_TOOLTIP", true);
292 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
293 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "");
294 String fontSize = Cache.getDefault("FONT_SIZE", "10");
296 seqNameItalics = Cache.getDefault("ID_ITALICS", true);
300 if (fontStyle.equals("bold"))
304 else if (fontStyle.equals("italic"))
309 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
312 .setGapCharacter(Cache.getDefault("GAP_SYMBOL", "-").charAt(0));
314 // We must set conservation and consensus before setting colour,
315 // as Blosum and Clustal require this to be done
316 if (hconsensus == null && !isDataset)
318 if (!alignment.isNucleotide())
320 showConservation=Cache.getDefault("SHOW_CONSERVATION", true);
321 showQuality=Cache.getDefault("SHOW_QUALITY", true);
322 showGroupConservation = Cache.getDefault("SHOW_GROUP_CONSERVATION",
325 showConsensusHistogram = Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM",
327 showSequenceLogo = Cache.getDefault("SHOW_CONSENSUS_LOGO", false);
328 normaliseSequenceLogo = Cache.getDefault("NORMALISE_CONSENSUS_LOGO", false);
329 showGroupConsensus = Cache.getDefault("SHOW_GROUP_CONSENSUS", false);
330 showConsensus=Cache.getDefault("SHOW_IDENTITY", true);
331 consensus = new AlignmentAnnotation("Consensus", "PID",
332 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
333 consensus.hasText = true;
334 consensus.autoCalculated = true;
336 initAutoAnnotation();
337 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
339 globalColourScheme = ColourSchemeProperty.getColour(alignment,
340 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
342 if (globalColourScheme instanceof UserColourScheme)
344 globalColourScheme = UserDefinedColours.loadDefaultColours();
345 ((UserColourScheme) globalColourScheme).setThreshold(0,
346 getIgnoreGapsConsensus());
349 if (globalColourScheme != null)
351 globalColourScheme.setConsensus(hconsensus);
355 wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
356 showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
358 sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
359 followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
367 * features are displayed if true
369 public void setShowSequenceFeatures(boolean b)
371 showSequenceFeatures = b;
374 public boolean getShowSequenceFeatures()
376 return showSequenceFeatures;
380 * centre columnar annotation labels in displayed alignment annotation TODO:
381 * add to jalviewXML and annotation display settings
383 boolean centreColumnLabels = false;
385 private boolean showdbrefs;
387 private boolean shownpfeats;
389 // --------END Structure Conservation
392 * get the consensus sequence as displayed under the PID consensus annotation
395 * @return consensus sequence as a new sequence object
397 public SequenceI getConsensusSeq()
399 if (consensus == null)
401 updateConsensus(null);
403 if (consensus == null)
407 StringBuffer seqs = new StringBuffer();
408 for (int i = 0; i < consensus.annotations.length; i++)
410 if (consensus.annotations[i] != null)
412 if (consensus.annotations[i].description.charAt(0) == '[')
414 seqs.append(consensus.annotations[i].description.charAt(1));
418 seqs.append(consensus.annotations[i].displayCharacter);
423 SequenceI sq = new Sequence("Consensus", seqs.toString());
424 sq.setDescription("Percentage Identity Consensus "
425 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
432 * @return DOCUMENT ME!
434 public int getStartRes()
442 * @return DOCUMENT ME!
444 public int getEndRes()
452 * @return DOCUMENT ME!
454 public int getStartSeq()
465 public void setStartRes(int res)
476 public void setStartSeq(int seq)
487 public void setEndRes(int res)
489 if (res > (alignment.getWidth() - 1))
491 // log.System.out.println(" Corrected res from " + res + " to maximum " +
492 // (alignment.getWidth()-1));
493 res = alignment.getWidth() - 1;
510 public void setEndSeq(int seq)
512 if (seq > alignment.getHeight())
514 seq = alignment.getHeight();
528 * @return DOCUMENT ME!
530 public int getEndSeq()
541 public void setFont(Font f)
545 Container c = new Container();
547 java.awt.FontMetrics fm = c.getFontMetrics(font);
548 setCharHeight(fm.getHeight());
549 setCharWidth(fm.charWidth('M'));
550 validCharWidth = true;
556 * @return DOCUMENT ME!
558 public Font getFont()
569 public void setCharWidth(int w)
577 * @return DOCUMENT ME!
579 public int getCharWidth()
590 public void setCharHeight(int h)
598 * @return DOCUMENT ME!
600 public int getCharHeight()
611 public void setWrappedWidth(int w)
613 this.wrappedWidth = w;
619 * @return DOCUMENT ME!
621 public int getWrappedWidth()
629 * @return DOCUMENT ME!
631 public AlignmentI getAlignment()
642 public void setAlignment(AlignmentI align)
644 if (alignment != null && alignment.getCodonFrames() != null)
646 StructureSelectionManager.getStructureSelectionManager(
647 Desktop.instance).removeMappings(alignment.getCodonFrames());
649 this.alignment = align;
650 if (alignment!=null && alignment.getCodonFrames() != null)
652 StructureSelectionManager.getStructureSelectionManager(
653 Desktop.instance).addMappings(alignment.getCodonFrames());
663 public void setWrapAlignment(boolean state)
665 wrapAlignment = state;
674 public void setShowText(boolean state)
685 public void setRenderGaps(boolean state)
693 * @return DOCUMENT ME!
695 public boolean getColourText()
697 return showColourText;
706 public void setColourText(boolean state)
708 showColourText = state;
717 public void setShowBoxes(boolean state)
725 * @return DOCUMENT ME!
727 public boolean getWrapAlignment()
729 return wrapAlignment;
735 * @return DOCUMENT ME!
737 public boolean getShowText()
745 * @return DOCUMENT ME!
747 public boolean getShowBoxes()
755 * @return DOCUMENT ME!
757 public char getGapCharacter()
759 return getAlignment().getGapCharacter();
768 public void setGapCharacter(char gap)
770 if (getAlignment() != null)
772 getAlignment().setGapCharacter(gap);
779 * @return DOCUMENT ME!
781 public ColumnSelection getColumnSelection()
792 public void setCurrentTree(NJTree tree)
800 * @return DOCUMENT ME!
802 public NJTree getCurrentTree()
810 * @return DOCUMENT ME!
812 public boolean getShowJVSuffix()
823 public void setShowJVSuffix(boolean b)
831 * @return DOCUMENT ME!
833 public boolean getShowAnnotation()
835 return showAnnotation;
844 public void setShowAnnotation(boolean b)
852 * @return DOCUMENT ME!
854 public boolean getScaleAboveWrapped()
856 return scaleAboveWrapped;
862 * @return DOCUMENT ME!
864 public boolean getScaleLeftWrapped()
866 return scaleLeftWrapped;
872 * @return DOCUMENT ME!
874 public boolean getScaleRightWrapped()
876 return scaleRightWrapped;
885 public void setScaleAboveWrapped(boolean b)
887 scaleAboveWrapped = b;
896 public void setScaleLeftWrapped(boolean b)
898 scaleLeftWrapped = b;
907 public void setScaleRightWrapped(boolean b)
909 scaleRightWrapped = b;
913 public void setDataset(boolean b)
918 public boolean isDataset()
925 public boolean getShowHiddenMarkers()
927 return showHiddenMarkers;
930 public void setShowHiddenMarkers(boolean show)
932 showHiddenMarkers = show;
935 public Color getSequenceColour(SequenceI seq)
937 if (sequenceColours == null || !sequenceColours.containsKey(seq))
943 return (Color) sequenceColours.get(seq);
947 public void setSequenceColour(SequenceI seq, Color col)
949 if (sequenceColours == null)
951 sequenceColours = new Hashtable();
956 sequenceColours.remove(seq);
960 sequenceColours.put(seq, col);
965 * returns the visible column regions of the alignment
967 * @param selectedRegionOnly
968 * true to just return the contigs intersecting with the selected
972 public int[] getViewAsVisibleContigs(boolean selectedRegionOnly)
974 int[] viscontigs = null;
975 int start = 0, end = 0;
976 if (selectedRegionOnly && selectionGroup != null)
978 start = selectionGroup.getStartRes();
979 end = selectionGroup.getEndRes() + 1;
983 end = alignment.getWidth();
985 viscontigs = colSel.getVisibleContigs(start, end);
990 * get hash of undo and redo list for the alignment
992 * @return long[] { historyList.hashCode, redoList.hashCode };
994 public long[] getUndoRedoHash()
997 if (historyList == null || redoList == null)
1001 { historyList.hashCode(), this.redoList.hashCode() };
1005 * test if a particular set of hashcodes are different to the hashcodes for
1006 * the undo and redo list.
1009 * the stored set of hashcodes as returned by getUndoRedoHash
1010 * @return true if the hashcodes differ (ie the alignment has been edited) or
1011 * the stored hashcode array differs in size
1013 public boolean isUndoRedoHashModified(long[] undoredo)
1015 if (undoredo == null)
1019 long[] cstate = getUndoRedoHash();
1020 if (cstate.length != undoredo.length)
1025 for (int i = 0; i < cstate.length; i++)
1027 if (cstate[i] != undoredo[i])
1035 public boolean getCentreColumnLabels()
1037 return centreColumnLabels;
1040 public void setCentreColumnLabels(boolean centrecolumnlabels)
1042 centreColumnLabels = centrecolumnlabels;
1045 public void updateSequenceIdColours()
1047 if (sequenceColours == null)
1049 sequenceColours = new Hashtable();
1051 for (SequenceGroup sg:alignment.getGroups())
1053 if (sg.idColour != null)
1055 for (SequenceI s:sg.getSequences(getHiddenRepSequences()))
1057 sequenceColours.put(s, sg.idColour);
1064 * enable or disable the display of Database Cross References in the sequence
1067 public void setShowDbRefs(boolean show)
1074 * @return true if Database References are to be displayed on tooltips.
1076 public boolean isShowDbRefs()
1083 * @return true if Non-positional features are to be displayed on tooltips.
1085 public boolean isShowNpFeats()
1091 * enable or disable the display of Non-Positional sequence features in the
1092 * sequence ID tooltip
1096 public void setShowNpFeats(boolean show)
1103 * @return true if view has hidden rows
1105 public boolean hasHiddenRows()
1107 return hasHiddenRows;
1112 * @return true if view has hidden columns
1114 public boolean hasHiddenColumns()
1116 return hasHiddenColumns;
1120 * when set, view will scroll to show the highlighted position
1122 public boolean followHighlight = true;
1125 * @return true if view should scroll to show the highlighted region of a
1129 public boolean getFollowHighlight()
1131 return followHighlight;
1134 public boolean followSelection = true;
1137 * @return true if view selection should always follow the selections
1138 * broadcast by other selection sources
1140 public boolean getFollowSelection()
1142 return followSelection;
1145 boolean showSeqFeaturesHeight;
1147 public void sendSelection()
1149 jalview.structure.StructureSelectionManager
1150 .getStructureSelectionManager(Desktop.instance).sendSelection(
1151 new SequenceGroup(getSelectionGroup()),
1152 new ColumnSelection(getColumnSelection()), this);
1155 public void setShowSequenceFeaturesHeight(boolean selected)
1157 showSeqFeaturesHeight = selected;
1160 public boolean getShowSequenceFeaturesHeight()
1162 return showSeqFeaturesHeight;
1166 * return the alignPanel containing the given viewport. Use this to get the
1167 * components currently handling the given viewport.
1170 * @return null or an alignPanel guaranteed to have non-null alignFrame
1173 public AlignmentPanel getAlignPanel()
1175 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(this
1176 .getSequenceSetId());
1177 AlignmentPanel ap = null;
1178 for (int p = 0; aps != null && p < aps.length; p++)
1180 if (aps[p].av == this)
1188 public boolean getSortByTree()
1193 public void setSortByTree(boolean sort)
1199 * synthesize a column selection if none exists so it covers the given
1200 * selection group. if wholewidth is false, no column selection is made if the
1201 * selection group covers the whole alignment width.
1206 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
1210 && (sgs = sg.getStartRes()) >= 0
1211 && sg.getStartRes() <= (sge = sg.getEndRes())
1212 && (colSel == null || colSel.getSelected() == null || colSel
1213 .getSelected().size() == 0))
1215 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
1222 colSel = new ColumnSelection();
1224 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
1226 colSel.addElement(cspos);
1231 public StructureSelectionManager getStructureSelectionManager()
1233 return StructureSelectionManager
1234 .getStructureSelectionManager(Desktop.instance);
1240 * @return a series of SequenceI arrays, one for each PDBEntry, listing which
1241 * sequence in the alignment holds a reference to it
1243 public SequenceI[][] collateForPDB(PDBEntry[] pdbEntries)
1245 ArrayList<SequenceI[]> seqvectors = new ArrayList<SequenceI[]>();
1246 for (PDBEntry pdb : pdbEntries)
1248 ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
1249 for (int i = 0; i < alignment.getHeight(); i++)
1251 Vector pdbs = alignment.getSequenceAt(i).getDatasetSequence()
1256 for (int p = 0; p < pdbs.size(); p++)
1258 PDBEntry p1 = (PDBEntry) pdbs.elementAt(p);
1259 if (p1.getId().equals(pdb.getId()))
1261 if (!seqs.contains(sq = alignment.getSequenceAt(i)))
1268 seqvectors.add(seqs.toArray(new SequenceI[seqs.size()]));
1270 return seqvectors.toArray(new SequenceI[seqvectors.size()][]);
1274 public boolean isNormaliseSequenceLogo()
1276 return normaliseSequenceLogo;
1279 public void setNormaliseSequenceLogo(boolean state)
1281 normaliseSequenceLogo = state;
1287 * @return true if alignment characters should be displayed
1289 public boolean isValidCharWidth()
1291 return validCharWidth;
1293 private jalview.ws.jws2.dm.AAConsSettings preferredAAConsSettings;
1295 public jalview.ws.jws2.dm.AAConsSettings getPreferredAAConsSettings()
1297 return preferredAAConsSettings;
1299 public void setPreferredAAConsSettings(jalview.ws.jws2.dm.AAConsSettings aaConsSettings)
1301 preferredAAConsSettings=aaConsSettings;