last version stay many bugs ..
[jalview.git] / src / jalview / datamodel / AlignmentAnnotation.java
1 /*
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
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 of the License, or (at your option) any later version.
10  * 
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.
15  * 
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  */
18 package jalview.datamodel;
19
20 import jalview.analysis.Rna;
21 import jalview.analysis.WUSSParseException;
22
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26
27 import fr.orsay.lri.varna.models.rna.RNA;
28
29 /**
30  * DOCUMENT ME!
31  * 
32  * @author $author$
33  * @version $Revision$
34  */
35 public class AlignmentAnnotation
36 {
37   /**
38    * If true, this annotations is calculated every edit, eg consensus, quality
39    * or conservation graphs
40    */
41   public boolean autoCalculated = false;
42
43   public String annotationId;
44   
45   public SequenceI sequenceRef;
46
47   /** DOCUMENT ME!! */
48   public String label;
49
50   /** DOCUMENT ME!! */
51   public String description;
52
53   /** DOCUMENT ME!! */
54   public Annotation[] annotations;
55   
56   
57
58   /**
59    * RNA secondary structure contact positions
60    */
61   public SequenceFeature[] _rnasecstr = null;
62   /**
63    * position of annotation resulting in invalid WUSS parsing or -1
64    */
65   private long invalidrnastruc=-1;
66   /**
67    * Updates the _rnasecstr field Determines the positions that base pair and
68    * the positions of helices based on secondary structure from a Stockholm file
69    * 
70    * @param RNAannot
71    */
72   private void _updateRnaSecStr(CharSequence RNAannot)
73   {
74     try {
75     _rnasecstr = Rna.GetBasePairs(RNAannot);
76     invalidrnastruc=-1;
77     }
78     catch (WUSSParseException px)
79     {
80       invalidrnastruc=px.getProblemPos();
81     }
82     if (invalidrnastruc>-1)
83     {
84       return;
85     }
86     Rna.HelixMap(_rnasecstr);
87     // setRNAStruc(RNAannot);
88     
89     if (_rnasecstr != null && _rnasecstr.length > 0)
90     {
91       // show all the RNA secondary structure annotation symbols.
92       isrna=true;
93       showAllColLabels = true;
94       scaleColLabel = true;
95     }
96     // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
97   }
98   public java.util.Hashtable sequenceMapping;
99
100   /** DOCUMENT ME!! */
101   public float graphMin;
102
103   /** DOCUMENT ME!! */
104   public float graphMax;
105
106   /**
107    * Score associated with label and description.
108    */
109   public double score = Double.NaN;
110
111   /**
112    * flag indicating if annotation has a score.
113    */
114   public boolean hasScore = false;
115
116   public GraphLine threshold;
117
118   // Graphical hints and tips
119
120   /** Can this row be edited by the user ? */
121   public boolean editable = false;
122
123   /** Indicates if annotation has a graphical symbol track */
124   public boolean hasIcons; //
125
126   /** Indicates if annotation has a text character label */
127   public boolean hasText;
128
129   /** is the row visible */
130   public boolean visible = true;
131
132   public int graphGroup = -1;
133
134   /** Displayed height of row in pixels */
135   public int height = 0;
136
137   public int graph = 0;
138
139   public int graphHeight = 40;
140
141   public boolean padGaps = false;
142
143   public static final int NO_GRAPH = 0;
144
145   public static final int BAR_GRAPH = 1;
146
147   public static final int LINE_GRAPH = 2;
148
149   public boolean belowAlignment = true;
150
151   public SequenceGroup groupRef = null;
152
153   /**
154    * display every column label, even if there is a row of identical labels
155    */
156   public boolean showAllColLabels = false;
157
158   /**
159    * scale the column label to fit within the alignment column.
160    */
161   public boolean scaleColLabel = false;
162
163   /**
164    * centre the column labels relative to the alignment column
165    */
166   public boolean centreColLabels = false;
167
168   private boolean isrna;
169
170   /*
171    * (non-Javadoc)
172    * 
173    * @see java.lang.Object#finalize()
174    */
175   protected void finalize() throws Throwable
176   {
177     sequenceRef = null;
178     groupRef = null;
179     super.finalize();
180   }
181
182   public static int getGraphValueFromString(String string)
183   {
184     if (string.equalsIgnoreCase("BAR_GRAPH"))
185     {
186       return BAR_GRAPH;
187     }
188     else if (string.equalsIgnoreCase("LINE_GRAPH"))
189     {
190       return LINE_GRAPH;
191     }
192     else
193     {
194       return NO_GRAPH;
195     }
196   }
197
198   /**
199    * Creates a new AlignmentAnnotation object.
200    * 
201    * @param label
202    *          short label shown under sequence labels
203    * @param description
204    *          text displayed on mouseover
205    * @param annotations
206    *          set of positional annotation elements
207    */
208   public AlignmentAnnotation(String label, String description,
209           Annotation[] annotations)
210   {
211     // always editable?
212     editable = true;
213     this.label = label;
214     this.description = description;
215     this.annotations = annotations;
216
217     validateRangeAndDisplay();
218   }
219
220   /**
221    * Checks if annotation labels represent secondary structures
222    * 
223    */
224   void areLabelsSecondaryStructure()
225   {
226     boolean nonSSLabel = false;
227     isrna = false;
228     StringBuffer rnastring = new StringBuffer();
229
230     char firstChar = 0;
231     for (int i = 0; i < annotations.length; i++)
232     {
233       if (annotations[i] == null)
234       {
235         continue;
236       }
237       if (annotations[i].secondaryStructure == 'H'
238               || annotations[i].secondaryStructure == 'E')
239       {
240         hasIcons |= true;
241       }
242       else
243       // Check for RNA secondary structure
244       {
245           //System.out.println(annotations[i].secondaryStructure);
246         if (annotations[i].secondaryStructure == '('
247                         || annotations[i].secondaryStructure == '['
248                         || annotations[i].secondaryStructure == '<'
249                         || annotations[i].secondaryStructure == '{'
250                         || annotations[i].secondaryStructure == 'A'
251                         || annotations[i].secondaryStructure == 'B'
252                         || annotations[i].secondaryStructure == 'C'
253                         || annotations[i].secondaryStructure == 'D'
254                         || annotations[i].secondaryStructure == '1'
255                         || annotations[i].secondaryStructure == 'F'
256                         || annotations[i].secondaryStructure == 'G'
257                         || annotations[i].secondaryStructure == '2'
258                         || annotations[i].secondaryStructure == 'I'
259                         || annotations[i].secondaryStructure == 'J'
260                         || annotations[i].secondaryStructure == 'K'
261                         || annotations[i].secondaryStructure == 'L'
262                         || annotations[i].secondaryStructure == 'M'
263                         || annotations[i].secondaryStructure == 'N'
264                         || annotations[i].secondaryStructure == 'O'
265                         || annotations[i].secondaryStructure == 'P'
266                         || annotations[i].secondaryStructure == 'Q'
267                         || annotations[i].secondaryStructure == 'R'
268                         || annotations[i].secondaryStructure == 'S'
269                         || annotations[i].secondaryStructure == 'T'
270                         || annotations[i].secondaryStructure == 'U'
271                         || annotations[i].secondaryStructure == 'V'
272                         || annotations[i].secondaryStructure == 'W'
273                         || annotations[i].secondaryStructure == 'X'
274                         || annotations[i].secondaryStructure == 'Y'
275                         || annotations[i].secondaryStructure == 'Z')
276         {
277           hasIcons |= true;
278           isrna |= true;
279         }
280       }
281
282       // System.out.println("displaychar " + annotations[i].displayCharacter);
283
284       if (annotations[i].displayCharacter == null
285               || annotations[i].displayCharacter.length() == 0)
286       {
287         rnastring.append('.');
288         continue;
289       }
290       if (annotations[i].displayCharacter.length() == 1)
291       {
292         firstChar = annotations[i].displayCharacter.charAt(0);
293         // check to see if it looks like a sequence or is secondary structure
294         // labelling.
295         if (annotations[i].secondaryStructure != ' '
296                 && !hasIcons
297                 &&
298                 // Uncomment to only catch case where
299                 // displayCharacter==secondary
300                 // Structure
301                 // to correctly redisplay SS annotation imported from Stockholm,
302                 // exported to JalviewXML and read back in again.
303                 // &&
304                 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
305                 firstChar != ' '
306                 && firstChar != 'H'
307                 && firstChar != 'E'
308                 && firstChar != '('
309                 && firstChar != '['
310                 && firstChar != '>'
311                 && firstChar != '{'
312                 && firstChar != 'A'
313                 && firstChar != 'B'
314                 && firstChar != 'C'
315                 && firstChar != 'D'
316                 && firstChar != '1'
317                 && firstChar != 'F'
318                 && firstChar != 'G'
319                 && firstChar != '2'
320                 && firstChar != 'I'
321                 && firstChar != 'J'
322                 && firstChar != 'K'
323                 && firstChar != 'L'
324                 && firstChar != 'M'
325                 && firstChar != 'N'
326                 && firstChar != 'O'
327                 && firstChar != 'P'
328                 && firstChar != 'Q'
329                 && firstChar != 'R'
330                 && firstChar != 'S'
331                 && firstChar != 'T'
332                 && firstChar != 'U'
333                 && firstChar != 'V'
334                 && firstChar != 'W'
335                 && firstChar != 'X'
336                 && firstChar != 'Y'
337                 && firstChar != 'Z'
338                 && firstChar != '-'
339                 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
340         {
341           if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
342                                                                          // parameterise
343                                                                          // to
344                                                                          // gap
345                                                                          // symbol
346                                                                          // number
347           {
348             nonSSLabel = true;
349           }
350         }
351       }
352       else
353       {
354         rnastring.append(annotations[i].displayCharacter.charAt(1));
355       }
356
357       if (annotations[i].displayCharacter.length() > 0)
358       {
359         hasText = true;
360       }
361     }
362
363     if (nonSSLabel)
364     {
365       hasIcons = false;
366       for (int j = 0; j < annotations.length; j++)
367       {
368         if (annotations[j] != null
369                 && annotations[j].secondaryStructure != ' ')
370         {
371           annotations[j].displayCharacter = String
372                   .valueOf(annotations[j].secondaryStructure);
373           annotations[j].secondaryStructure = ' ';
374         }
375
376       }
377     }
378     else
379     {
380       if (isrna)
381       {
382         _updateRnaSecStr(new AnnotCharSequence());
383       }
384     }
385
386     annotationId = this.hashCode() + "";
387   }
388   /**
389    * flyweight access to positions in the alignment annotation row for RNA processing
390    * @author jimp
391    *
392    */
393   private class AnnotCharSequence  implements CharSequence 
394   {
395     int offset=0;
396     int max=0;
397     
398     public AnnotCharSequence() {
399       this(0,annotations.length);
400     }
401     public AnnotCharSequence(int start, int end) {
402       offset=start;
403       max=end;
404     }
405     @Override
406     public CharSequence subSequence(int start, int end)
407     {
408       return new AnnotCharSequence(offset+start, offset+end);
409     }
410     
411     @Override
412     public int length()
413     {
414       return max-offset;
415     }
416     
417     @Override
418     public char charAt(int index)
419     {
420       String dc;
421       return ((index+offset<0) || (index+offset)>=max || annotations[index+offset]==null || (dc=annotations[index+offset].displayCharacter.trim()).length()<1)
422               ? '.' : dc.charAt(0);
423     }
424     public String toString()
425     {
426       char[] string=new char[max-offset];
427       int mx=annotations.length;
428         
429       for (int i=offset;i<mx;i++)
430       {
431         String dc;
432         string[i]=(annotations[i]==null || (dc=annotations[i].displayCharacter.trim()).length()<1 )? '.' : dc.charAt(0);
433       }
434       return new String(string);
435     }
436   };
437   
438   private long _lastrnaannot=-1;
439   public String getRNAStruc(){
440     if (isrna)
441     {
442       String rnastruc = new AnnotCharSequence().toString();
443       if (_lastrnaannot!=rnastruc.hashCode())
444       {
445         // ensure rna structure contacts are up to date
446         _lastrnaannot=rnastruc.hashCode();
447         _updateRnaSecStr(rnastruc);
448       }
449       return rnastruc;
450     }
451     return null;
452   }
453
454 /**
455    * Creates a new AlignmentAnnotation object.
456    * 
457    * @param label
458    *          DOCUMENT ME!
459    * @param description
460    *          DOCUMENT ME!
461    * @param annotations
462    *          DOCUMENT ME!
463    * @param min
464    *          DOCUMENT ME!
465    * @param max
466    *          DOCUMENT ME!
467    * @param winLength
468    *          DOCUMENT ME!
469    */
470   public AlignmentAnnotation(String label, String description,
471           Annotation[] annotations, float min, float max, int graphType)
472   {
473     // graphs are not editable
474     editable = graphType == 0;
475
476     this.label = label;
477     this.description = description;
478     this.annotations = annotations;
479     graph = graphType;
480     graphMin = min;
481     graphMax = max;
482     validateRangeAndDisplay();
483   }
484
485   /**
486    * checks graphMin and graphMax, secondary structure symbols, sets graphType
487    * appropriately, sets null labels to the empty string if appropriate.
488    */
489   public void validateRangeAndDisplay()
490   {
491
492     if (annotations == null)
493     {
494       visible = false; // try to prevent renderer from displaying.
495       return; // this is a non-annotation row annotation - ie a sequence score.
496     }
497
498     int graphType = graph;
499     float min = graphMin;
500     float max = graphMax;
501     boolean drawValues = true;
502
503     if (min == max)
504     {
505       min = 999999999;
506       for (int i = 0; i < annotations.length; i++)
507       {
508         if (annotations[i] == null)
509         {
510           continue;
511         }
512
513         if (drawValues && annotations[i].displayCharacter != null
514                 && annotations[i].displayCharacter.length() > 1)
515         {
516           drawValues = false;
517         }
518
519         if (annotations[i].value > max)
520         {
521           max = annotations[i].value;
522         }
523
524         if (annotations[i].value < min)
525         {
526           min = annotations[i].value;
527         }
528       }
529       // ensure zero is origin for min/max ranges on only one side of zero
530       if (min > 0)
531       {
532         min = 0;
533       }
534       else
535       {
536         if (max < 0)
537         {
538           max = 0;
539         }
540       }
541     }
542
543     graphMin = min;
544     graphMax = max;
545
546     areLabelsSecondaryStructure();
547
548     if (!drawValues && graphType != NO_GRAPH)
549     {
550       for (int i = 0; i < annotations.length; i++)
551       {
552         if (annotations[i] != null)
553         {
554           annotations[i].displayCharacter = "X";
555         }
556       }
557     }
558   }
559
560   /**
561    * Copy constructor creates a new independent annotation row with the same
562    * associated sequenceRef
563    * 
564    * @param annotation
565    */
566   public AlignmentAnnotation(AlignmentAnnotation annotation)
567   {
568     this.label = new String(annotation.label);
569     if (annotation.description != null)
570       this.description = new String(annotation.description);
571     this.graphMin = annotation.graphMin;
572     this.graphMax = annotation.graphMax;
573     this.graph = annotation.graph;
574     this.graphHeight = annotation.graphHeight;
575     this.graphGroup = annotation.graphGroup;
576     this.groupRef = annotation.groupRef;
577     this.editable = annotation.editable;
578     this.autoCalculated = annotation.autoCalculated;
579     this.hasIcons = annotation.hasIcons;
580     this.hasText = annotation.hasText;
581     this.height = annotation.height;
582     this.label = annotation.label;
583     this.padGaps = annotation.padGaps;
584     this.visible = annotation.visible;
585     this.centreColLabels=annotation.centreColLabels;
586     this.scaleColLabel=annotation.scaleColLabel;
587     this.showAllColLabels=annotation.showAllColLabels;
588     this.calcId = annotation.calcId;
589     if (this.hasScore = annotation.hasScore)
590     {
591       this.score = annotation.score;
592     }
593     if (annotation.threshold != null)
594     {
595       threshold = new GraphLine(annotation.threshold);
596     }
597     if (annotation.annotations != null)
598     {
599       Annotation[] ann = annotation.annotations;
600       this.annotations = new Annotation[ann.length];
601       for (int i = 0; i < ann.length; i++)
602       {
603         annotations[i] = new Annotation(ann[i]);
604       }
605       ;
606       if (annotation.sequenceRef != null)
607       {
608         this.sequenceRef = annotation.sequenceRef;
609         if (annotation.sequenceMapping != null)
610         {
611           Integer p = null;
612           sequenceMapping = new Hashtable();
613           Enumeration pos = annotation.sequenceMapping.keys();
614           while (pos.hasMoreElements())
615           {
616             // could optimise this!
617             p = (Integer) pos.nextElement();
618             Annotation a = (Annotation) annotation.sequenceMapping.get(p);
619             if (a == null)
620             {
621               continue;
622             }
623             for (int i = 0; i < ann.length; i++)
624             {
625               if (ann[i] == a)
626               {
627                 sequenceMapping.put(p, annotations[i]);
628               }
629             }
630           }
631         }
632         else
633         {
634           this.sequenceMapping = null;
635         }
636       }
637     }
638     // TODO: check if we need to do this: JAL-952
639     //if (this.isrna=annotation.isrna)
640     {
641       // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
642     }
643     validateRangeAndDisplay(); // construct hashcodes, etc.
644   }
645
646   /**
647    * clip the annotation to the columns given by startRes and endRes (inclusive)
648    * and prune any existing sequenceMapping to just those columns.
649    * 
650    * @param startRes
651    * @param endRes
652    */
653   public void restrict(int startRes, int endRes)
654   {
655     if (annotations == null)
656     {
657       // non-positional
658       return;
659     }
660     if (startRes < 0)
661       startRes = 0;
662     if (startRes >= annotations.length)
663       startRes = annotations.length - 1;
664     if (endRes >= annotations.length)
665       endRes = annotations.length - 1;
666     if (annotations == null)
667       return;
668     Annotation[] temp = new Annotation[endRes - startRes + 1];
669     if (startRes < annotations.length)
670     {
671       System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
672               + 1);
673     }
674     if (sequenceRef != null)
675     {
676       // Clip the mapping, if it exists.
677       int spos = sequenceRef.findPosition(startRes);
678       int epos = sequenceRef.findPosition(endRes);
679       if (sequenceMapping != null)
680       {
681         Hashtable newmapping = new Hashtable();
682         Enumeration e = sequenceMapping.keys();
683         while (e.hasMoreElements())
684         {
685           Integer pos = (Integer) e.nextElement();
686           if (pos.intValue() >= spos && pos.intValue() <= epos)
687           {
688             newmapping.put(pos, sequenceMapping.get(pos));
689           }
690         }
691         sequenceMapping.clear();
692         sequenceMapping = newmapping;
693       }
694     }
695     annotations = temp;
696   }
697
698   /**
699    * set the annotation row to be at least length Annotations
700    * 
701    * @param length
702    *          minimum number of columns required in the annotation row
703    * @return false if the annotation row is greater than length
704    */
705   public boolean padAnnotation(int length)
706   {
707     if (annotations == null)
708     {
709       return true; // annotation row is correct - null == not visible and
710       // undefined length
711     }
712     if (annotations.length < length)
713     {
714       Annotation[] na = new Annotation[length];
715       System.arraycopy(annotations, 0, na, 0, annotations.length);
716       annotations = na;
717       return true;
718     }
719     return annotations.length > length;
720
721   }
722
723   /**
724    * DOCUMENT ME!
725    * 
726    * @return DOCUMENT ME!
727    */
728   public String toString()
729   {
730     StringBuffer buffer = new StringBuffer();
731
732     for (int i = 0; i < annotations.length; i++)
733     {
734       if (annotations[i] != null)
735       {
736         if (graph != 0)
737         {
738           buffer.append(annotations[i].value);
739         }
740         else if (hasIcons)
741         {
742           buffer.append(annotations[i].secondaryStructure);
743         }
744         else
745         {
746           buffer.append(annotations[i].displayCharacter);
747         }
748       }
749
750       buffer.append(", ");
751     }
752     // TODO: remove disgusting hack for 'special' treatment of consensus line.
753     if (label.indexOf("Consensus") == 0)
754     {
755       buffer.append("\n");
756
757       for (int i = 0; i < annotations.length; i++)
758       {
759         if (annotations[i] != null)
760         {
761           buffer.append(annotations[i].description);
762         }
763
764         buffer.append(", ");
765       }
766     }
767
768     return buffer.toString();
769   }
770
771   public void setThreshold(GraphLine line)
772   {
773     threshold = line;
774   }
775
776   public GraphLine getThreshold()
777   {
778     return threshold;
779   }
780
781   /**
782    * Attach the annotation to seqRef, starting from startRes position. If
783    * alreadyMapped is true then the indices of the annotation[] array are
784    * sequence positions rather than alignment column positions.
785    * 
786    * @param seqRef
787    * @param startRes
788    * @param alreadyMapped
789    */
790   public void createSequenceMapping(SequenceI seqRef, int startRes,
791           boolean alreadyMapped)
792   {
793
794     if (seqRef == null)
795     {
796       return;
797     }
798     sequenceRef = seqRef;
799     if (annotations == null)
800     {
801       return;
802     }
803     sequenceMapping = new java.util.Hashtable();
804
805     int seqPos;
806
807     for (int i = 0; i < annotations.length; i++)
808     {
809       if (annotations[i] != null)
810       {
811         if (alreadyMapped)
812         {
813           seqPos = seqRef.findPosition(i);
814         }
815         else
816         {
817           seqPos = i + startRes;
818         }
819
820         sequenceMapping.put(new Integer(seqPos), annotations[i]);
821       }
822     }
823
824   }
825
826   public void adjustForAlignment()
827   {
828     if (sequenceRef == null)
829       return;
830
831     if (annotations == null)
832     {
833       return;
834     }
835
836     int a = 0, aSize = sequenceRef.getLength();
837
838     if (aSize == 0)
839     {
840       // Its been deleted
841       return;
842     }
843
844     int position;
845     Annotation[] temp = new Annotation[aSize];
846     Integer index;
847
848     for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
849     {
850       index = new Integer(a);
851       if (sequenceMapping.containsKey(index))
852       {
853         position = sequenceRef.findIndex(a) - 1;
854
855         temp[position] = (Annotation) sequenceMapping.get(index);
856       }
857     }
858
859     annotations = temp;
860   }
861
862   /**
863    * remove any null entries in annotation row and return the number of non-null
864    * annotation elements.
865    * 
866    * @return
867    */
868   public int compactAnnotationArray()
869   {
870     int i = 0, iSize = annotations.length;
871     while (i < iSize)
872     {
873       if (annotations[i] == null)
874       {
875         if (i + 1 < iSize)
876           System.arraycopy(annotations, i + 1, annotations, i, iSize - i
877                   - 1);
878         iSize--;
879       }
880       else
881       {
882         i++;
883       }
884     }
885     Annotation[] ann = annotations;
886     annotations = new Annotation[i];
887     System.arraycopy(ann, 0, annotations, 0, i);
888     ann = null;
889     return iSize;
890   }
891
892   /**
893    * Associate this annotion with the aligned residues of a particular sequence.
894    * sequenceMapping will be updated in the following way: null sequenceI -
895    * existing mapping will be discarded but annotations left in mapped
896    * positions. valid sequenceI not equal to current sequenceRef: mapping is
897    * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
898    * parameter to specify correspondence between current and new sequenceRef
899    * 
900    * @param sequenceI
901    */
902   public void setSequenceRef(SequenceI sequenceI)
903   {
904     if (sequenceI != null)
905     {
906       if (sequenceRef != null)
907       {
908         if (sequenceRef != sequenceI
909                 && !sequenceRef.equals(sequenceI)
910                 && sequenceRef.getDatasetSequence() != sequenceI
911                         .getDatasetSequence())
912         {
913           // if sequenceRef isn't intersecting with sequenceI
914           // throw away old mapping and reconstruct.
915           sequenceRef = null;
916           if (sequenceMapping != null)
917           {
918             sequenceMapping = null;
919             // compactAnnotationArray();
920           }
921           createSequenceMapping(sequenceI, 1, true);
922           adjustForAlignment();
923         }
924         else
925         {
926           // Mapping carried over
927           sequenceRef = sequenceI;
928         }
929       }
930       else
931       {
932         // No mapping exists
933         createSequenceMapping(sequenceI, 1, true);
934         adjustForAlignment();
935       }
936     }
937     else
938     {
939       // throw away the mapping without compacting.
940       sequenceMapping = null;
941       sequenceRef = null;
942     }
943   }
944
945   /**
946    * @return the score
947    */
948   public double getScore()
949   {
950     return score;
951   }
952
953   /**
954    * @param score
955    *          the score to set
956    */
957   public void setScore(double score)
958   {
959     hasScore = true;
960     this.score = score;
961   }
962
963   /**
964    * 
965    * @return true if annotation has an associated score
966    */
967   public boolean hasScore()
968   {
969     return hasScore || !Double.isNaN(score);
970   }
971
972   /**
973    * Score only annotation
974    * 
975    * @param label
976    * @param description
977    * @param score
978    */
979   public AlignmentAnnotation(String label, String description, double score)
980   {
981     this(label, description, null);
982     setScore(score);
983   }
984
985   /**
986    * copy constructor with edit based on the hidden columns marked in colSel
987    * 
988    * @param alignmentAnnotation
989    * @param colSel
990    */
991   public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
992           ColumnSelection colSel)
993   {
994     this(alignmentAnnotation);
995     if (annotations == null)
996     {
997       return;
998     }
999     colSel.makeVisibleAnnotation(this);
1000   }
1001
1002   public void setPadGaps(boolean padgaps, char gapchar)
1003   {
1004     this.padGaps = padgaps;
1005     if (padgaps)
1006     {
1007       hasText = true;
1008       for (int i = 0; i < annotations.length; i++)
1009       {
1010         if (annotations[i] == null)
1011           annotations[i] = new Annotation(String.valueOf(gapchar), null,
1012                   ' ', 0f,null);
1013         else if (annotations[i].displayCharacter == null
1014                 || annotations[i].displayCharacter.equals(" "))
1015           annotations[i].displayCharacter = String.valueOf(gapchar);
1016       }
1017     }
1018   }
1019
1020   /**
1021    * format description string for display
1022    * 
1023    * @param seqname
1024    * @return Get the annotation description string optionally prefixed by
1025    *         associated sequence name (if any)
1026    */
1027   public String getDescription(boolean seqname)
1028   {
1029     if (seqname && this.sequenceRef != null)
1030     {
1031       int i=description.toLowerCase().indexOf("<html>");
1032       if (i>-1)
1033       {
1034         // move the html tag to before the sequence reference.
1035         return "<html>"+sequenceRef.getName()+" : "+description.substring(i+6);
1036       }
1037       return sequenceRef.getName() + " : " + description;
1038     }
1039     return description;
1040   }
1041
1042   public boolean isValidStruc()
1043   {
1044     return invalidrnastruc==-1;
1045   }
1046   public long getInvalidStrucPos()
1047   {
1048     return invalidrnastruc;
1049   }
1050
1051   /**
1052    * machine readable ID string indicating what generated this annotation
1053    */
1054   protected String calcId="";
1055   public String getCalcId()
1056   {
1057     return calcId;
1058   }
1059
1060   public void setCalcId(String calcId)
1061   {
1062     this.calcId = calcId;
1063   }
1064   
1065  
1066   
1067 }