JAL-1807 update
[jalviewjs.git] / unused / appletgui / AlignViewport.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.analysis.NJTree;
24 import jalview.api.AlignViewportI;
25 import jalview.bin.JalviewLite;
26 import jalview.commands.CommandI;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.SearchResults;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.schemes.ColourSchemeProperty;
34 import jalview.schemes.UserColourScheme;
35 import jalview.structure.CommandListener;
36 import jalview.structure.SelectionSource;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.structure.VamsasSource;
39 import jalview.util.Platform;
40 import jalview.viewmodel.AlignmentViewport;
41 import jalview.workers.AlignCalcManager;
42
43 import java.awt.Font;
44
45 public class AlignViewport extends AlignmentViewport implements
46         SelectionSource, VamsasSource, CommandListener
47 {
48   boolean cursorMode = false;
49
50   Font font = new Font("SansSerif", Font.PLAIN, 10);
51
52   boolean validCharWidth = true;
53
54   NJTree currentTree = null;
55
56   public JalviewLite applet;
57
58   boolean MAC = false;
59
60   private AnnotationColumnChooser annotationColumnSelectionState;
61
62   public void finalize()
63   {
64     applet = null;
65     quality = null;
66     alignment = null;
67     colSel = null;
68   }
69
70   public AlignViewport(AlignmentI al, JalviewLite applet)
71   {
72     super();
73     calculator = new AlignCalcManager();
74     this.applet = applet;
75     alignment = al;
76     // we always pad gaps
77     this.setPadGaps(true);
78     this.startRes = 0;
79     this.endRes = al.getWidth() - 1;
80     this.startSeq = 0;
81     this.endSeq = al.getHeight() - 1;
82     if (applet != null)
83     {
84       // get the width and height scaling factors if they were specified
85       String param = applet.getParameter("widthScale");
86       if (param != null)
87       {
88         try
89         {
90           widthScale = new Float(param).floatValue();
91         } catch (Exception e)
92         {
93         }
94         if (widthScale <= 1.0)
95         {
96           System.err
97                   .println("Invalid alignment character width scaling factor ("
98                           + widthScale + "). Ignoring.");
99           widthScale = 1;
100         }
101         if (JalviewLite.debug)
102         {
103           System.err
104                   .println("Alignment character width scaling factor is now "
105                           + widthScale);
106         }
107       }
108       param = applet.getParameter("heightScale");
109       if (param != null)
110       {
111         try
112         {
113           heightScale = new Float(param).floatValue();
114         } catch (Exception e)
115         {
116         }
117         if (heightScale <= 1.0)
118         {
119           System.err
120                   .println("Invalid alignment character height scaling factor ("
121                           + heightScale + "). Ignoring.");
122           heightScale = 1;
123         }
124         if (JalviewLite.debug)
125         {
126           System.err
127                   .println("Alignment character height scaling factor is now "
128                           + heightScale);
129         }
130       }
131     }
132     setFont(font);
133
134     MAC = Platform.isAMac();
135
136     if (applet != null)
137     {
138       setShowJVSuffix(applet.getDefaultParameter("showFullId",
139               getShowJVSuffix()));
140
141       setShowAnnotation(applet.getDefaultParameter("showAnnotation",
142               isShowAnnotation()));
143
144       showConservation = applet.getDefaultParameter("showConservation",
145               showConservation);
146
147       showQuality = applet.getDefaultParameter("showQuality", showQuality);
148
149       showConsensus = applet.getDefaultParameter("showConsensus",
150               showConsensus);
151
152       setShowUnconserved(applet.getDefaultParameter("showUnconserved",
153               getShowUnconserved()));
154
155       setScaleProteinAsCdna(applet.getDefaultParameter(
156               "scaleProteinAsCdna", isScaleProteinAsCdna()));
157
158       String param = applet.getParameter("upperCase");
159       if (param != null)
160       {
161         if (param.equalsIgnoreCase("bold"))
162         {
163           setUpperCasebold(true);
164         }
165       }
166       sortByTree = applet.getDefaultParameter("sortByTree", sortByTree);
167
168       setFollowHighlight(applet.getDefaultParameter("automaticScrolling",
169               isFollowHighlight()));
170       followSelection = isFollowHighlight();
171
172       showSequenceLogo = applet.getDefaultParameter("showSequenceLogo",
173               showSequenceLogo);
174
175       normaliseSequenceLogo = applet.getDefaultParameter(
176               "normaliseSequenceLogo", applet.getDefaultParameter(
177                       "normaliseLogo", normaliseSequenceLogo));
178
179       showGroupConsensus = applet.getDefaultParameter("showGroupConsensus",
180               showGroupConsensus);
181
182       showGroupConservation = applet.getDefaultParameter(
183               "showGroupConservation", showGroupConservation);
184
185       showConsensusHistogram = applet.getDefaultParameter(
186               "showConsensusHistogram", showConsensusHistogram);
187
188     }
189
190     if (applet != null)
191     {
192       String colour = applet.getParameter("defaultColour");
193
194       if (colour == null)
195       {
196         colour = applet.getParameter("userDefinedColour");
197         if (colour != null)
198         {
199           colour = "User Defined";
200         }
201       }
202
203       if (colour != null)
204       {
205         globalColourScheme = ColourSchemeProperty.getColour(alignment,
206                 colour);
207         if (globalColourScheme != null)
208         {
209           globalColourScheme.setConsensus(hconsensus);
210         }
211       }
212
213       if (applet.getParameter("userDefinedColour") != null)
214       {
215         ((UserColourScheme) globalColourScheme).parseAppletParameter(applet
216                 .getParameter("userDefinedColour"));
217       }
218     }
219     initAutoAnnotation();
220
221   }
222
223   /**
224    * get the consensus sequence as displayed under the PID consensus annotation
225    * row.
226    * 
227    * @return consensus sequence as a new sequence object
228    */
229   public SequenceI getConsensusSeq()
230   {
231     if (consensus == null)
232     {
233       updateConsensus(null);
234     }
235     if (consensus == null)
236     {
237       return null;
238     }
239     StringBuilder seqs = new StringBuilder(consensus.annotations.length);
240     for (int i = 0; i < consensus.annotations.length; i++)
241     {
242       if (consensus.annotations[i] != null)
243       {
244         if (consensus.annotations[i].description.charAt(0) == '[')
245         {
246           seqs.append(consensus.annotations[i].description.charAt(1));
247         }
248         else
249         {
250           seqs.append(consensus.annotations[i].displayCharacter);
251         }
252       }
253     }
254     SequenceI sq = new Sequence("Consensus", seqs.toString());
255     sq.setDescription("Percentage Identity Consensus "
256             + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
257     return sq;
258   }
259
260   java.awt.Frame nullFrame;
261
262   protected FeatureSettings featureSettings = null;
263
264   private float heightScale = 1, widthScale = 1;
265
266   public void setFont(Font f)
267   {
268     font = f;
269     if (nullFrame == null)
270     {
271       nullFrame = new java.awt.Frame();
272       nullFrame.addNotify();
273     }
274
275     java.awt.FontMetrics fm = nullFrame.getGraphics().getFontMetrics(font);
276     setCharHeight((int) (heightScale * fm.getHeight()));
277     setCharWidth((int) (widthScale * fm.charWidth('M')));
278
279     if (isUpperCasebold())
280     {
281       Font f2 = new Font(f.getName(), Font.BOLD, f.getSize());
282       fm = nullFrame.getGraphics().getFontMetrics(f2);
283       setCharWidth((int) (widthScale * (fm.stringWidth("MMMMMMMMMMM") / 10)));
284     }
285   }
286
287   public Font getFont()
288   {
289     return font;
290   }
291
292
293   public void resetSeqLimits(int height)
294   {
295     setEndSeq(height / getCharHeight());
296   }
297
298   public void setCurrentTree(NJTree tree)
299   {
300     currentTree = tree;
301   }
302
303   public NJTree getCurrentTree()
304   {
305     return currentTree;
306   }
307
308
309   boolean centreColumnLabels;
310
311   public boolean getCentreColumnLabels()
312   {
313     return centreColumnLabels;
314   }
315
316   public boolean followSelection = true;
317
318   /**
319    * @return true if view selection should always follow the selections
320    *         broadcast by other selection sources
321    */
322   public boolean getFollowSelection()
323   {
324     return followSelection;
325   }
326
327   public void sendSelection()
328   {
329     getStructureSelectionManager().sendSelection(
330                     new SequenceGroup(getSelectionGroup()),
331                     new ColumnSelection(getColumnSelection()), this);
332   }
333
334   /**
335    * Returns an instance of the StructureSelectionManager scoped to this applet
336    * instance.
337    * 
338    * @return
339    */
340   @Override
341   public StructureSelectionManager getStructureSelectionManager()
342   {
343     return StructureSelectionManager.getStructureSelectionManager(applet);
344   }
345
346   /**
347    * synthesize a column selection if none exists so it covers the given
348    * selection group. if wholewidth is false, no column selection is made if the
349    * selection group covers the whole alignment width.
350    * 
351    * @param sg
352    * @param wholewidth
353    */
354   public void expandColSelection(SequenceGroup sg, boolean wholewidth)
355   {
356     int sgs, sge;
357     if (sg != null
358             && (sgs = sg.getStartRes()) >= 0
359             && sg.getStartRes() <= (sge = sg.getEndRes())
360             && (colSel == null || colSel.getSelected() == null || colSel
361                     .getSelected().size() == 0))
362     {
363       if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
364       {
365         // do nothing
366         return;
367       }
368       if (colSel == null)
369       {
370         colSel = new ColumnSelection();
371       }
372       for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
373       {
374         colSel.addElement(cspos);
375       }
376     }
377   }
378
379   public boolean isNormaliseSequenceLogo()
380   {
381     return normaliseSequenceLogo;
382   }
383
384   public void setNormaliseSequenceLogo(boolean state)
385   {
386     normaliseSequenceLogo = state;
387   }
388
389   /**
390    * 
391    * @return true if alignment characters should be displayed
392    */
393   public boolean isValidCharWidth()
394   {
395     return validCharWidth;
396   }
397
398   public AnnotationColumnChooser getAnnotationColumnSelectionState()
399   {
400     return annotationColumnSelectionState;
401   }
402
403   public void setAnnotationColumnSelectionState(
404           AnnotationColumnChooser annotationColumnSelectionState)
405   {
406     this.annotationColumnSelectionState = annotationColumnSelectionState;
407   }
408
409   @Override
410   public void mirrorCommand(CommandI command, boolean undo,
411           StructureSelectionManager ssm, VamsasSource source)
412   {
413     // TODO refactor so this can be pulled up to superclass or controller
414     /*
415      * Do nothing unless we are a 'complement' of the source. May replace this
416      * with direct calls not via SSM.
417      */
418     if (source instanceof AlignViewportI
419             && ((AlignViewportI) source).getCodingComplement() == this)
420     {
421       // ok to continue;
422     }
423     else
424     {
425       return;
426     }
427
428     CommandI mappedCommand = ssm.mapCommand(command, undo, getAlignment(),
429             getGapCharacter());
430     if (mappedCommand != null)
431     {
432       mappedCommand.doCommand(null);
433       firePropertyChange("alignment", null, getAlignment().getSequences());
434
435       // ap.scalePanelHolder.repaint();
436       // ap.repaint();
437     }
438   }
439
440   @Override
441   public VamsasSource getVamsasSource()
442   {
443     return this;
444   }
445
446   /**
447    * If this viewport has a (Protein/cDNA) complement, then scroll the
448    * complementary alignment to match this one.
449    */
450   public void scrollComplementaryAlignment(AlignmentPanel complementPanel)
451   {
452     if (complementPanel == null)
453     {
454       return;
455     }
456
457     /*
458      * Populate a SearchResults object with the mapped location to scroll to. If
459      * there is no complement, or it is not following highlights, or no mapping
460      * is found, the result will be empty.
461      */
462     SearchResults sr = new SearchResults();
463     int seqOffset = findComplementScrollTarget(sr);
464     if (!sr.isEmpty())
465     {
466       complementPanel.setFollowingComplementScroll(true);
467       complementPanel.scrollToCentre(sr, seqOffset);
468     }
469   }
470
471
472 }