Applet has hidden regions
[jalview.git] / src / jalview / appletgui / AlignViewport.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 \r
26 import jalview.analysis.*;\r
27 import jalview.bin.*;\r
28 import jalview.datamodel.*;\r
29 import jalview.schemes.*;\r
30 \r
31 public class AlignViewport\r
32 {\r
33   int startRes;\r
34   int endRes;\r
35 \r
36   int startSeq;\r
37   int endSeq;\r
38 \r
39 \r
40   boolean cursorMode = false;\r
41 \r
42   boolean showJVSuffix = true;\r
43   boolean showText = true;\r
44   boolean showColourText = false;\r
45   boolean showBoxes = true;\r
46   boolean wrapAlignment = false;\r
47   boolean renderGaps = true;\r
48   boolean showSequenceFeatures = false;\r
49   boolean showAnnotation = true;\r
50   boolean showConservation = true;\r
51   boolean showQuality = true;\r
52   boolean showConsensus = true;\r
53 \r
54   boolean colourAppliesToAllGroups = true;\r
55   ColourSchemeI globalColourScheme = null;\r
56   boolean conservationColourSelected = false;\r
57   boolean abovePIDThreshold = false;\r
58 \r
59   SequenceGroup selectionGroup;\r
60 \r
61   int charHeight;\r
62   int charWidth;\r
63   int wrappedWidth;\r
64 \r
65   Font font = new Font("SansSerif", Font.PLAIN, 10);\r
66   boolean validCharWidth = true;\r
67   AlignmentI alignment;\r
68 \r
69   ColumnSelection colSel = new ColumnSelection();\r
70 \r
71   int threshold;\r
72   int increment;\r
73 \r
74   NJTree currentTree = null;\r
75 \r
76   boolean scaleAboveWrapped = true;\r
77   boolean scaleLeftWrapped = true;\r
78   boolean scaleRightWrapped = true;\r
79 \r
80   // The following vector holds the features which are\r
81  // currently visible, in the correct order or rendering\r
82   Hashtable featuresDisplayed;\r
83 \r
84   boolean hasHiddenColumns = false;\r
85   boolean hasHiddenRows = false;\r
86   boolean showHiddenMarkers = true;\r
87 \r
88 \r
89   public Vector vconsensus;\r
90   AlignmentAnnotation consensus;\r
91   AlignmentAnnotation conservation;\r
92   AlignmentAnnotation quality;\r
93 \r
94   boolean autocalculateConsensus = true;\r
95 \r
96   public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!\r
97 \r
98   private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(this);\r
99 \r
100   boolean ignoreGapsInConsensusCalculation = false;\r
101 \r
102   jalview.bin.JalviewLite applet;\r
103 \r
104   public AlignViewport(AlignmentI al, JalviewLite applet)\r
105   {\r
106     this.applet = applet;\r
107     setAlignment(al);\r
108     this.startRes = 0;\r
109     this.endRes = al.getWidth() - 1;\r
110     this.startSeq = 0;\r
111     this.endSeq = al.getHeight() - 1;\r
112     setFont(font);\r
113 \r
114     if (applet != null)\r
115     {\r
116       String param = applet.getParameter("showFullId");\r
117       if (param != null)\r
118       {\r
119         showJVSuffix = Boolean.valueOf(param).booleanValue();\r
120       }\r
121 \r
122       param = applet.getParameter("showAnnotation");\r
123       if (param != null)\r
124       {\r
125         showAnnotation = Boolean.valueOf(param).booleanValue();\r
126       }\r
127 \r
128       param = applet.getParameter("showConservation");\r
129       if (param != null)\r
130       {\r
131         showConservation = Boolean.valueOf(param).booleanValue();\r
132       }\r
133 \r
134       param = applet.getParameter("showQuality");\r
135       if (param != null)\r
136       {\r
137         showQuality = Boolean.valueOf(param).booleanValue();\r
138       }\r
139 \r
140       param = applet.getParameter("showConsensus");\r
141       if (param != null)\r
142       {\r
143         showConsensus = Boolean.valueOf(param).booleanValue();\r
144       }\r
145     }\r
146     // We must set conservation and consensus before setting colour,\r
147     // as Blosum and Clustal require this to be done\r
148     updateConservation();\r
149     updateConsensus();\r
150 \r
151 \r
152     if (applet != null)\r
153     {\r
154       String colour = applet.getParameter("defaultColour");\r
155 \r
156       if(colour == null)\r
157       {\r
158         colour = applet.getParameter("userDefinedColour");\r
159         if(colour !=null)\r
160           colour = "User Defined";\r
161       }\r
162 \r
163       if(colour != null)\r
164       {\r
165         globalColourScheme = ColourSchemeProperty.getColour(alignment, colour);\r
166         if (globalColourScheme != null)\r
167         {\r
168           globalColourScheme.setConsensus(vconsensus);\r
169         }\r
170       }\r
171 \r
172       if(applet.getParameter("userDefinedColour")!=null)\r
173       {\r
174         ((UserColourScheme)globalColourScheme).parseAppletParameter(\r
175             applet.getParameter("userDefinedColour"));\r
176       }\r
177 \r
178 \r
179     }\r
180   }\r
181 \r
182   public void showSequenceFeatures(boolean b)\r
183   {\r
184     showSequenceFeatures = b;\r
185   }\r
186 \r
187   public boolean getShowSequenceFeatures()\r
188   {\r
189     return showSequenceFeatures;\r
190   }\r
191 \r
192 \r
193   public void updateConservation()\r
194   {\r
195     if(alignment.isNucleotide())\r
196           return;\r
197 \r
198     Conservation cons = new jalview.analysis.Conservation("All",\r
199         jalview.schemes.ResidueProperties.propHash, 3,\r
200         alignment.getSequences(), 0,\r
201         alignment.getWidth() - 1);\r
202     cons.calculate();\r
203     cons.verdict(false, ConsPercGaps);\r
204     cons.findQuality();\r
205     int alWidth = alignment.getWidth();\r
206     Annotation[] annotations = new Annotation[alWidth];\r
207     Annotation[] qannotations = new Annotation[alWidth];\r
208     String sequence = cons.getConsSequence().getSequence();\r
209     float minR, minG, minB, maxR, maxG, maxB;\r
210     minR = 0.3f;\r
211     minG = 0.0f;\r
212     minB = 0f;\r
213     maxR = 1.0f - minR;\r
214     maxG = 0.9f - minG;\r
215     maxB = 0f - minB; // scalable range for colouring both Conservation and Quality\r
216     float min = 0f;\r
217     float max = 11f;\r
218     float qmin = cons.qualityRange[0].floatValue();\r
219     float qmax = cons.qualityRange[1].floatValue();\r
220 \r
221     for (int i = 0; i < alWidth; i++)\r
222     {\r
223       float value = 0;\r
224       try\r
225       {\r
226         value = Integer.parseInt(sequence.charAt(i) + "");\r
227       }\r
228       catch (Exception ex)\r
229       {\r
230         if (sequence.charAt(i) == '*')\r
231         {\r
232           value = 11;\r
233         }\r
234         if (sequence.charAt(i) == '+')\r
235         {\r
236           value = 10;\r
237         }\r
238       }\r
239       float vprop = value - min;\r
240       vprop /= max;\r
241 \r
242       annotations[i] = new Annotation(sequence.charAt(i) + "",\r
243                                       "", ' ', value,\r
244                                       new Color(minR + maxR * vprop,\r
245                                                 minG + maxG * vprop,\r
246                                                 minB + maxB * vprop));\r
247       // Quality calc\r
248       value = ( (Double) cons.quality.elementAt(i)).floatValue();\r
249       vprop = value - qmin;\r
250       vprop /= qmax;\r
251       qannotations[i] = new Annotation(" ",\r
252                                        String.valueOf(value), ' ', value,\r
253                                        new\r
254                                        Color(minR + maxR * vprop,\r
255                                              minG + maxG * vprop,\r
256                                              minB + maxB * vprop));\r
257     }\r
258 \r
259     if (conservation == null)\r
260     {\r
261       conservation = new AlignmentAnnotation("Conservation",\r
262                                              "Conservation of total alignment less than " +\r
263                                              ConsPercGaps + "% gaps",\r
264                                              annotations,\r
265                                              0f, // cons.qualityRange[0].floatValue(),\r
266                                              11f, // cons.qualityRange[1].floatValue()\r
267                                              AlignmentAnnotation.BAR_GRAPH);\r
268       if (showConservation)\r
269       {\r
270         alignment.addAnnotation(conservation);\r
271       }\r
272       quality = new AlignmentAnnotation("Quality",\r
273                                         "Alignment Quality based on Blosum62 scores",\r
274                                         qannotations,\r
275                                         cons.qualityRange[0].floatValue(),\r
276                                         cons.qualityRange[1].floatValue(),\r
277                                         AlignmentAnnotation.BAR_GRAPH);\r
278       if (showQuality)\r
279       {\r
280         alignment.addAnnotation(quality);\r
281       }\r
282     }\r
283     else\r
284     {\r
285       conservation.annotations = annotations;\r
286       quality.annotations = qannotations;\r
287       quality.graphMax = cons.qualityRange[1].floatValue();\r
288     }\r
289 \r
290   }\r
291 \r
292   public void updateConsensus()\r
293   {\r
294     Annotation[] annotations = new Annotation[alignment.getWidth()];\r
295 \r
296     // this routine prevents vconsensus becoming a new object each time\r
297     // consenus is calculated. Important for speed of Blosum62\r
298     // and PID colouring of alignment\r
299     if (vconsensus == null)\r
300     {\r
301       vconsensus = alignment.getAAFrequency();\r
302     }\r
303     else\r
304     {\r
305       Vector temp = alignment.getAAFrequency();\r
306       vconsensus.removeAllElements();\r
307       Enumeration e = temp.elements();\r
308       while (e.hasMoreElements())\r
309       {\r
310         vconsensus.addElement(e.nextElement());\r
311       }\r
312     }\r
313     Hashtable hash = null;\r
314     for (int i = 0; i < alignment.getWidth(); i++)\r
315     {\r
316       hash = (Hashtable) vconsensus.elementAt(i);\r
317       float value = 0;\r
318       if(ignoreGapsInConsensusCalculation)\r
319         value = ((Float)hash.get("pid_nogaps")).floatValue();\r
320       else\r
321         value = ((Float)hash.get("pid_gaps")).floatValue();\r
322 \r
323       String maxRes = hash.get("maxResidue").toString();\r
324       String mouseOver = hash.get("maxResidue") + " ";\r
325       if (maxRes.length() > 1)\r
326       {\r
327         mouseOver = "[" + maxRes + "] ";\r
328         maxRes = "+";\r
329       }\r
330 \r
331 \r
332       mouseOver += (int) value + "%";\r
333       annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);\r
334 \r
335     }\r
336 \r
337     if (consensus == null)\r
338     {\r
339       consensus = new AlignmentAnnotation("Consensus",\r
340                                           "PID", annotations, 0f, 100f, AlignmentAnnotation.BAR_GRAPH);\r
341       if (showConsensus)\r
342       {\r
343         alignment.addAnnotation(consensus);\r
344       }\r
345     }\r
346     else\r
347     {\r
348       consensus.annotations = annotations;\r
349     }\r
350 \r
351     if(globalColourScheme!=null)\r
352           globalColourScheme.setConsensus(vconsensus);\r
353 \r
354   }\r
355 \r
356   public SequenceGroup getSelectionGroup()\r
357   {\r
358     return selectionGroup;\r
359   }\r
360 \r
361   public void setSelectionGroup(SequenceGroup sg)\r
362   {\r
363     selectionGroup = sg;\r
364   }\r
365 \r
366   public boolean getConservationSelected()\r
367   {\r
368     return conservationColourSelected;\r
369   }\r
370 \r
371   public void setConservationSelected(boolean b)\r
372   {\r
373     conservationColourSelected = b;\r
374   }\r
375 \r
376   public boolean getAbovePIDThreshold()\r
377   {\r
378     return abovePIDThreshold;\r
379   }\r
380 \r
381   public void setAbovePIDThreshold(boolean b)\r
382   {\r
383     abovePIDThreshold = b;\r
384   }\r
385 \r
386   public int getStartRes()\r
387   {\r
388     return startRes;\r
389   }\r
390 \r
391   public int getEndRes()\r
392   {\r
393     return endRes;\r
394   }\r
395 \r
396   public int getStartSeq()\r
397   {\r
398     return startSeq;\r
399   }\r
400 \r
401   public void setGlobalColourScheme(ColourSchemeI cs)\r
402   {\r
403     globalColourScheme = cs;\r
404   }\r
405 \r
406   public ColourSchemeI getGlobalColourScheme()\r
407   {\r
408     return globalColourScheme;\r
409   }\r
410 \r
411   public void setStartRes(int res)\r
412   {\r
413     this.startRes = res;\r
414   }\r
415 \r
416   public void setStartSeq(int seq)\r
417   {\r
418     this.startSeq = seq;\r
419   }\r
420 \r
421   public void setEndRes(int res)\r
422   {\r
423     if (res > alignment.getWidth() - 1)\r
424     {\r
425       // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
426       res = alignment.getWidth() - 1;\r
427     }\r
428     if (res < 0)\r
429     {\r
430       res = 0;\r
431     }\r
432     this.endRes = res;\r
433   }\r
434 \r
435   public void setEndSeq(int seq)\r
436   {\r
437     if (seq > alignment.getHeight())\r
438     {\r
439       seq = alignment.getHeight();\r
440     }\r
441     if (seq < 0)\r
442     {\r
443       seq = 0;\r
444     }\r
445     this.endSeq = seq;\r
446   }\r
447 \r
448   public int getEndSeq()\r
449   {\r
450     return endSeq;\r
451   }\r
452 \r
453   java.awt.Frame nullFrame;\r
454   public void setFont(Font f)\r
455   {\r
456     font = f;\r
457     if(nullFrame == null)\r
458     {\r
459       nullFrame = new java.awt.Frame();\r
460       nullFrame.addNotify();\r
461     }\r
462 \r
463     java.awt.FontMetrics fm = nullFrame.getGraphics().getFontMetrics(font);\r
464     setCharHeight(fm.getHeight());\r
465     setCharWidth(fm.charWidth('M'));\r
466   }\r
467 \r
468   public Font getFont()\r
469   {\r
470     return font;\r
471   }\r
472 \r
473   public void setCharWidth(int w)\r
474   {\r
475     this.charWidth = w;\r
476   }\r
477 \r
478   public int getCharWidth()\r
479   {\r
480     return charWidth;\r
481   }\r
482 \r
483   public void setCharHeight(int h)\r
484   {\r
485     this.charHeight = h;\r
486   }\r
487 \r
488   public int getCharHeight()\r
489   {\r
490     return charHeight;\r
491   }\r
492 \r
493   public void setWrappedWidth(int w)\r
494   {\r
495     this.wrappedWidth = w;\r
496   }\r
497 \r
498   public int getwrappedWidth()\r
499   {\r
500     return wrappedWidth;\r
501   }\r
502 \r
503   public AlignmentI getAlignment()\r
504   {\r
505     return alignment;\r
506   }\r
507 \r
508   public void setAlignment(AlignmentI align)\r
509   {\r
510     this.alignment = align;\r
511   }\r
512 \r
513   public void setWrapAlignment(boolean state)\r
514   {\r
515     wrapAlignment = state;\r
516   }\r
517 \r
518   public void setShowText(boolean state)\r
519   {\r
520     showText = state;\r
521   }\r
522 \r
523   public void setRenderGaps(boolean state)\r
524   {\r
525     renderGaps = state;\r
526   }\r
527 \r
528   public boolean getColourText()\r
529   {\r
530     return showColourText;\r
531   }\r
532 \r
533   public void setColourText(boolean state)\r
534   {\r
535     showColourText = state;\r
536   }\r
537 \r
538   public void setShowBoxes(boolean state)\r
539   {\r
540     showBoxes = state;\r
541   }\r
542 \r
543   public boolean getWrapAlignment()\r
544   {\r
545     return wrapAlignment;\r
546   }\r
547 \r
548   public boolean getShowText()\r
549   {\r
550     return showText;\r
551   }\r
552 \r
553   public boolean getShowBoxes()\r
554   {\r
555     return showBoxes;\r
556   }\r
557 \r
558   public char getGapCharacter()\r
559   {\r
560     return getAlignment().getGapCharacter();\r
561   }\r
562 \r
563   public void setGapCharacter(char gap)\r
564   {\r
565     if (getAlignment() != null)\r
566     {\r
567       getAlignment().setGapCharacter(gap);\r
568     }\r
569   }\r
570 \r
571   public void setThreshold(int thresh)\r
572   {\r
573     threshold = thresh;\r
574   }\r
575 \r
576   public int getThreshold()\r
577   {\r
578     return threshold;\r
579   }\r
580 \r
581   public void setIncrement(int inc)\r
582   {\r
583     increment = inc;\r
584   }\r
585 \r
586   public int getIncrement()\r
587   {\r
588     return increment;\r
589   }\r
590 \r
591   public void setHiddenColumns(ColumnSelection colsel)\r
592   {\r
593     this.colSel = colsel;\r
594     if(colSel.getHiddenColumns()!=null)\r
595       hasHiddenColumns = true;\r
596   }\r
597 \r
598   public ColumnSelection getColumnSelection()\r
599   {\r
600     return colSel;\r
601   }\r
602 \r
603   public void resetSeqLimits(int height)\r
604   {\r
605     setEndSeq(height / getCharHeight());\r
606   }\r
607 \r
608   public void setCurrentTree(NJTree tree)\r
609   {\r
610     currentTree = tree;\r
611   }\r
612 \r
613   public NJTree getCurrentTree()\r
614   {\r
615     return currentTree;\r
616   }\r
617 \r
618   public void setColourAppliesToAllGroups(boolean b)\r
619   {\r
620     colourAppliesToAllGroups = b;\r
621   }\r
622 \r
623   public boolean getColourAppliesToAllGroups()\r
624   {\r
625     return colourAppliesToAllGroups;\r
626   }\r
627 \r
628   public boolean getShowJVSuffix()\r
629   {\r
630     return showJVSuffix;\r
631   }\r
632 \r
633   public void setShowJVSuffix(boolean b)\r
634   {\r
635     showJVSuffix = b;\r
636   }\r
637 \r
638   public boolean getShowAnnotation()\r
639   {\r
640     return showAnnotation;\r
641   }\r
642 \r
643   public void setShowAnnotation(boolean b)\r
644   {\r
645     showAnnotation = b;\r
646   }\r
647 \r
648   public boolean getScaleAboveWrapped()\r
649   {\r
650     return scaleAboveWrapped;\r
651   }\r
652 \r
653   public boolean getScaleLeftWrapped()\r
654   {\r
655     return scaleLeftWrapped;\r
656   }\r
657 \r
658   public boolean getScaleRightWrapped()\r
659   {\r
660     return scaleRightWrapped;\r
661   }\r
662 \r
663   public void setScaleAboveWrapped(boolean b)\r
664   {\r
665     scaleAboveWrapped = b;\r
666   }\r
667 \r
668   public void setScaleLeftWrapped(boolean b)\r
669   {\r
670     scaleLeftWrapped = b;\r
671   }\r
672 \r
673   public void setScaleRightWrapped(boolean b)\r
674   {\r
675     scaleRightWrapped = b;\r
676   }\r
677 \r
678   public void setIgnoreGapsConsensus(boolean b)\r
679   {\r
680     ignoreGapsInConsensusCalculation = b;\r
681     updateConsensus();\r
682     if (globalColourScheme!=null)\r
683     {\r
684       globalColourScheme.setThreshold(globalColourScheme.getThreshold(),\r
685           ignoreGapsInConsensusCalculation);\r
686 \r
687     }\r
688   }\r
689 \r
690   /**\r
691    * Property change listener for changes in alignment\r
692    *\r
693    * @param listener DOCUMENT ME!\r
694    */\r
695   public void addPropertyChangeListener(\r
696       java.beans.PropertyChangeListener listener)\r
697   {\r
698       changeSupport.addPropertyChangeListener(listener);\r
699   }\r
700 \r
701   /**\r
702    * DOCUMENT ME!\r
703    *\r
704    * @param listener DOCUMENT ME!\r
705    */\r
706   public void removePropertyChangeListener(\r
707       java.beans.PropertyChangeListener listener)\r
708   {\r
709       changeSupport.removePropertyChangeListener(listener);\r
710   }\r
711 \r
712   /**\r
713    * Property change listener for changes in alignment\r
714    *\r
715    * @param prop DOCUMENT ME!\r
716    * @param oldvalue DOCUMENT ME!\r
717    * @param newvalue DOCUMENT ME!\r
718    */\r
719   public void firePropertyChange(String prop, Object oldvalue, Object newvalue)\r
720   {\r
721       changeSupport.firePropertyChange(prop, oldvalue, newvalue);\r
722   }\r
723 \r
724 \r
725 \r
726   public boolean getIgnoreGapsConsensus()\r
727   {\r
728     return ignoreGapsInConsensusCalculation;\r
729   }\r
730   public void hideSelectedColumns()\r
731   {\r
732     if (colSel.size() < 1)\r
733       return;\r
734 \r
735     colSel.hideSelectedColumns();\r
736     setSelectionGroup(null);\r
737 \r
738     hasHiddenColumns = true;\r
739   }\r
740 \r
741 \r
742   public void hideColumns(int start, int end)\r
743   {\r
744     if(start==end)\r
745       colSel.hideColumns(start);\r
746     else\r
747       colSel.hideColumns(start, end);\r
748     setSelectionGroup(null);\r
749     hasHiddenColumns = true;\r
750   }\r
751 \r
752   public void hideSequence(SequenceI seq)\r
753   {\r
754     if(seq!=null)\r
755     {\r
756       alignment.getHiddenSequences().hideSequence(seq);\r
757       hasHiddenRows = true;\r
758     }\r
759   }\r
760 \r
761   public void showSequence(int index)\r
762   {\r
763     alignment.getHiddenSequences().showSequence(index);\r
764 \r
765     if(alignment.getHiddenSequences().getSize()<1)\r
766       hasHiddenRows = false;\r
767   }\r
768 \r
769   public void showColumn(int col)\r
770   {\r
771     colSel.revealHiddenColumns(col);\r
772     if(colSel.getHiddenColumns()==null)\r
773       hasHiddenColumns = false;\r
774   }\r
775 \r
776   public void showAllHiddenColumns()\r
777   {\r
778     colSel.revealAllHiddenColumns();\r
779     hasHiddenColumns = false;\r
780   }\r
781 \r
782   public void showAllHiddenSeqs()\r
783   {\r
784     if(alignment.getHiddenSequences().getSize()>0)\r
785     {\r
786       alignment.getHiddenSequences().showAll();\r
787       hasHiddenRows = false;\r
788     }\r
789   }\r
790 \r
791   public int adjustForHiddenSeqs(int alignmentIndex)\r
792   {\r
793     return alignment.getHiddenSequences().adjustForHiddenSeqs(alignmentIndex);\r
794   }\r
795 \r
796   /**\r
797    * This method returns the a new SequenceI [] with\r
798    * the selection sequence and start and end points adjusted\r
799    * @return String[]\r
800    */\r
801   public SequenceI[] getSelectionAsNewSequence()\r
802   {\r
803     SequenceI[] sequences;\r
804 \r
805     if (selectionGroup == null)\r
806       sequences = alignment.getSequencesArray();\r
807     else\r
808       sequences = selectionGroup.getSelectionAsNewSequences(alignment);\r
809 \r
810     return sequences;\r
811   }\r
812 \r
813   /**\r
814    * This method returns the visible alignment as text, as\r
815    * seen on the GUI, ie if columns are hidden they will not\r
816    * be returned in the result.\r
817    * Use this for calculating trees, PCA, redundancy etc on views\r
818    * which contain hidden columns.\r
819    * @return String[]\r
820    */\r
821   public jalview.datamodel.CigarArray getViewAsCigars(boolean selectedRegionOnly)\r
822   {\r
823     CigarArray selection=null;\r
824     SequenceI [] seqs= null;\r
825     int i, iSize;\r
826     int start = 0, end = 0;\r
827     if(selectedRegionOnly && selectionGroup!=null)\r
828     {\r
829       iSize = selectionGroup.getSize(false);\r
830       seqs = selectionGroup.getSequencesInOrder(alignment);\r
831       start = selectionGroup.getStartRes();\r
832       end = selectionGroup.getEndRes(); // inclusive for start and end in SeqCigar constructor\r
833     }\r
834     else\r
835     {\r
836       iSize = alignment.getHeight();\r
837       seqs = alignment.getSequencesArray();\r
838       end = alignment.getWidth()-1;\r
839     }\r
840     SeqCigar[] selseqs = new SeqCigar[iSize];\r
841     for(i=0; i<iSize; i++)\r
842     {\r
843       selseqs[i] = new SeqCigar(seqs[i], start, end);\r
844     }\r
845     selection=new CigarArray(selseqs);\r
846     // now construct the CigarArray operations\r
847     if (hasHiddenColumns) {\r
848       Vector regions = colSel.getHiddenColumns();\r
849       int [] region;\r
850       int hideStart, hideEnd;\r
851       int last=start;\r
852       for (int j = 0; last<end & j < regions.size(); j++)\r
853       {\r
854         region = (int[]) regions.elementAt(j);\r
855         hideStart = region[0];\r
856         hideEnd = region[1];\r
857         // edit hidden regions to selection range\r
858         if(hideStart<last) {\r
859           if (hideEnd > last)\r
860           {\r
861             hideStart = last;\r
862           } else\r
863             continue;\r
864         }\r
865 \r
866         if (hideStart>end)\r
867           break;\r
868 \r
869         if (hideEnd>end)\r
870           hideEnd=end;\r
871 \r
872         if (hideStart>hideEnd)\r
873           break;\r
874         /**\r
875          * form operations...\r
876          */\r
877         if (last<hideStart)\r
878           selection.addOperation(CigarArray.M, hideStart-last);\r
879         selection.addOperation(CigarArray.D, 1+hideEnd-hideStart);\r
880         last = hideEnd+1;\r
881       }\r
882       // Final match if necessary.\r
883       if (last<end)\r
884         selection.addOperation(CigarArray.M, end-last);\r
885     } else {\r
886       selection.addOperation(CigarArray.M, end-start);\r
887     }\r
888     return selection;\r
889   }\r
890   /**\r
891    * return a compact representation of the current alignment selection to\r
892    * pass to an analysis function\r
893    * @param selectedOnly boolean true to just return the selected view\r
894    * @return AlignmentView\r
895    */\r
896   jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly) {\r
897     // JBPNote:\r
898     // this is here because the AlignmentView constructor modifies the CigarArray\r
899     // object. Refactoring of Cigar and alignment view representation should\r
900     // be done to remove redundancy.\r
901     CigarArray aligview = getViewAsCigars(selectedOnly);\r
902     if (aligview!=null)\r
903       return new AlignmentView(aligview);\r
904     return null;\r
905   }\r
906   /**\r
907    * This method returns the visible alignment as text, as\r
908    * seen on the GUI, ie if columns are hidden they will not\r
909    * be returned in the result.\r
910    * Use this for calculating trees, PCA, redundancy etc on views\r
911    * which contain hidden columns.\r
912    * @return String[]\r
913    */\r
914   public String [] getViewAsString(boolean selectedRegionOnly)\r
915   {\r
916     String [] selection = null;\r
917     SequenceI [] seqs= null;\r
918     int i, iSize;\r
919     int start = 0, end = 0;\r
920     if(selectedRegionOnly && selectionGroup!=null)\r
921     {\r
922       iSize = selectionGroup.getSize(false);\r
923       seqs = selectionGroup.getSequencesInOrder(alignment);\r
924       start = selectionGroup.getStartRes();\r
925       end = selectionGroup.getEndRes()+1;\r
926     }\r
927     else\r
928     {\r
929       iSize = alignment.getHeight();\r
930       seqs = alignment.getSequencesArray();\r
931       end = alignment.getWidth();\r
932     }\r
933 \r
934     selection = new String[iSize];\r
935 \r
936 \r
937     for(i=0; i<iSize; i++)\r
938     {\r
939       if (hasHiddenColumns)\r
940       {\r
941            StringBuffer visibleSeq = new StringBuffer();\r
942            Vector regions = colSel.getHiddenColumns();\r
943 \r
944            int blockStart = start, blockEnd=end;\r
945            int [] region;\r
946            int hideStart, hideEnd;\r
947 \r
948            for (int j = 0; j < regions.size(); j++)\r
949            {\r
950              region = (int[]) regions.elementAt(j);\r
951              hideStart = region[0];\r
952              hideEnd = region[1];\r
953 \r
954              if(hideStart < start)\r
955              {\r
956                continue;\r
957              }\r
958 \r
959              blockStart = Math.min(blockStart, hideEnd+1);\r
960              blockEnd = Math.min(blockEnd, hideStart);\r
961 \r
962              if(blockStart>blockEnd)\r
963              {\r
964                 break;\r
965              }\r
966 \r
967 \r
968              visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));\r
969 \r
970              blockStart = hideEnd+1;\r
971              blockEnd = end;\r
972            }\r
973 \r
974            if(end>blockStart)\r
975              visibleSeq.append(seqs[i].getSequence(blockStart, end));\r
976 \r
977            selection[i] = visibleSeq.toString();\r
978       }\r
979       else\r
980       {\r
981         selection[i] = seqs[i].getSequence(start, end);\r
982       }\r
983     }\r
984 \r
985     return selection;\r
986   }\r
987 \r
988   public boolean getShowHiddenMarkers()\r
989   {\r
990     return showHiddenMarkers;\r
991   }\r
992 \r
993   public void setShowHiddenMarkers(boolean show)\r
994   {\r
995     showHiddenMarkers = show;\r
996   }\r
997 \r
998 \r
999 }\r