fixed sequence associated annotation reading bug where only first annotation is assoc...
[jalview.git] / src / jalview / io / AnnotationFile.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
3  * Copyright (C) 2008 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 package jalview.io;\r
20 \r
21 import java.io.*;\r
22 import java.net.*;\r
23 import java.util.*;\r
24 \r
25 import jalview.analysis.*;\r
26 import jalview.datamodel.*;\r
27 import jalview.schemes.*;\r
28 \r
29 public class AnnotationFile\r
30 {\r
31   StringBuffer text = new StringBuffer("JALVIEW_ANNOTATION\n"\r
32           + "# Created: " + new java.util.Date() + "\n\n");\r
33 \r
34   /**\r
35    * convenience method for pre-2.4 feature files which have no view, hidden\r
36    * columns or hidden row keywords.\r
37    * \r
38    * @param annotations\r
39    * @param groups\r
40    * @param properties\r
41    * @return feature file as a string.\r
42    */\r
43   public String printAnnotations(AlignmentAnnotation[] annotations,\r
44           Vector groups, Hashtable properties)\r
45   {\r
46     return printAnnotations(annotations, groups, properties, null);\r
47 \r
48   }\r
49 \r
50   /**\r
51    * hold all the information about a particular view definition read from or\r
52    * written out in an annotations file.\r
53    */\r
54   public class ViewDef\r
55   {\r
56     public String viewname;\r
57 \r
58     public HiddenSequences hidseqs;\r
59 \r
60     public ColumnSelection hiddencols;\r
61 \r
62     public Vector visibleGroups;\r
63 \r
64     public ViewDef(String viewname, HiddenSequences hidseqs,\r
65             ColumnSelection hiddencols)\r
66     {\r
67       this.viewname = viewname;\r
68       this.hidseqs = hidseqs;\r
69       this.hiddencols = hiddencols;\r
70     }\r
71   }\r
72 \r
73   public String printAnnotations(AlignmentAnnotation[] annotations,\r
74           Vector groups, Hashtable properties, ViewDef[] views)\r
75   {\r
76     if (annotations != null)\r
77     {\r
78       boolean oneColour = true;\r
79       AlignmentAnnotation row;\r
80       String comma;\r
81       SequenceI refSeq = null;\r
82 \r
83       StringBuffer colours = new StringBuffer();\r
84       StringBuffer graphLine = new StringBuffer();\r
85 \r
86       Hashtable graphGroup = new Hashtable();\r
87 \r
88       java.awt.Color color;\r
89 \r
90       for (int i = 0; i < annotations.length; i++)\r
91       {\r
92         row = annotations[i];\r
93 \r
94         if (!row.visible && !row.hasScore())\r
95         {\r
96           continue;\r
97         }\r
98 \r
99         color = null;\r
100         oneColour = true;\r
101 \r
102         if (row.sequenceRef == null)\r
103         {\r
104           if (refSeq != null)\r
105           {\r
106             text.append("\nSEQUENCE_REF\tALIGNMENT\n");\r
107           }\r
108 \r
109           refSeq = null;\r
110         }\r
111 \r
112         else if (refSeq == null || refSeq != row.sequenceRef)\r
113         {\r
114           refSeq = row.sequenceRef;\r
115           text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");\r
116         }\r
117 \r
118         if (row.graph == AlignmentAnnotation.NO_GRAPH)\r
119         {\r
120           text.append("NO_GRAPH\t");\r
121         }\r
122         else\r
123         {\r
124           if (row.graph == AlignmentAnnotation.BAR_GRAPH)\r
125           {\r
126             text.append("BAR_GRAPH\t");\r
127           }\r
128           else if (row.graph == AlignmentAnnotation.LINE_GRAPH)\r
129           {\r
130             text.append("LINE_GRAPH\t");\r
131           }\r
132 \r
133           if (row.getThreshold() != null)\r
134           {\r
135             graphLine\r
136                     .append("GRAPHLINE\t"\r
137                             + row.label\r
138                             + "\t"\r
139                             + row.getThreshold().value\r
140                             + "\t"\r
141                             + row.getThreshold().label\r
142                             + "\t"\r
143                             + jalview.util.Format.getHexString(row\r
144                                     .getThreshold().colour) + "\n");\r
145           }\r
146 \r
147           if (row.graphGroup > -1)\r
148           {\r
149             String key = String.valueOf(row.graphGroup);\r
150             if (graphGroup.containsKey(key))\r
151             {\r
152               graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);\r
153             }\r
154             else\r
155             {\r
156               graphGroup.put(key, row.label);\r
157             }\r
158           }\r
159         }\r
160 \r
161         text.append(row.label + "\t");\r
162         if (row.description != null)\r
163         {\r
164           text.append(row.description + "\t");\r
165         }\r
166 \r
167         for (int j = 0; row.annotations != null\r
168                 && j < row.annotations.length; j++)\r
169         {\r
170           if (refSeq != null\r
171                   && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))\r
172           {\r
173             continue;\r
174           }\r
175 \r
176           if (row.annotations[j] != null)\r
177           {\r
178             comma = "";\r
179             if (row.annotations[j].secondaryStructure != ' ')\r
180             {\r
181               text.append(comma + row.annotations[j].secondaryStructure);\r
182               comma = ",";\r
183             }\r
184             if (row.annotations[j].displayCharacter != null\r
185                     && row.annotations[j].displayCharacter.length() > 0\r
186                     && !row.annotations[j].displayCharacter.equals(" "))\r
187             {\r
188               text.append(comma + row.annotations[j].displayCharacter);\r
189               comma = ",";\r
190             }\r
191 \r
192             if (row.annotations[j] != null)\r
193             {\r
194               if (color != null && !color.equals(row.annotations[j].colour))\r
195               {\r
196                 oneColour = false;\r
197               }\r
198 \r
199               color = row.annotations[j].colour;\r
200               if (row.annotations[j].value != 0f\r
201                       && row.annotations[j].value != Float.NaN)\r
202               {\r
203                 text.append(comma + row.annotations[j].value);\r
204               }\r
205             }\r
206 \r
207             if (row.annotations[j].colour != null\r
208                     && row.annotations[j].colour != java.awt.Color.black)\r
209             {\r
210               text.append(comma\r
211                       + "["\r
212                       + jalview.util.Format\r
213                               .getHexString(row.annotations[j].colour)\r
214                       + "]");\r
215             }\r
216           }\r
217           text.append("|");\r
218         }\r
219 \r
220         if (row.hasScore())\r
221           text.append("\t" + row.score);\r
222 \r
223         text.append("\n");\r
224 \r
225         if (color != null && color != java.awt.Color.black && oneColour)\r
226         {\r
227           colours.append("COLOUR\t" + row.label + "\t"\r
228                   + jalview.util.Format.getHexString(color) + "\n");\r
229         }\r
230 \r
231       }\r
232 \r
233       text.append("\n");\r
234 \r
235       text.append(colours.toString());\r
236       text.append(graphLine.toString());\r
237       if (graphGroup.size() > 0)\r
238       {\r
239         text.append("COMBINE\t");\r
240         Enumeration en = graphGroup.elements();\r
241         while (en.hasMoreElements())\r
242         {\r
243           text.append(en.nextElement() + "\n");\r
244         }\r
245       }\r
246     }\r
247 \r
248     if (groups != null)\r
249     {\r
250       printGroups(groups);\r
251     }\r
252 \r
253     if (properties != null)\r
254     {\r
255       text.append("\n\nALIGNMENT");\r
256       Enumeration en = properties.keys();\r
257       while (en.hasMoreElements())\r
258       {\r
259         String key = en.nextElement().toString();\r
260         text.append("\t" + key + "=" + properties.get(key));\r
261       }\r
262 \r
263     }\r
264 \r
265     return text.toString();\r
266   }\r
267 \r
268   public void printGroups(Vector sequenceGroups)\r
269   {\r
270     SequenceGroup sg;\r
271     for (int i = 0; i < sequenceGroups.size(); i++)\r
272     {\r
273       sg = (SequenceGroup) sequenceGroups.elementAt(i);\r
274       text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"\r
275               + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1) + "\t"\r
276               + "-1\t");\r
277       for (int s = 0; s < sg.getSize(); s++)\r
278       {\r
279         text.append(sg.getSequenceAt(s).getName() + "\t");\r
280       }\r
281 \r
282       text.append("\nPROPERTIES\t" + sg.getName() + "\t");\r
283 \r
284       if (sg.getDescription() != null)\r
285       {\r
286         text.append("description=" + sg.getDescription() + "\t");\r
287       }\r
288       if (sg.cs != null)\r
289       {\r
290         text.append("colour=" + ColourSchemeProperty.getColourName(sg.cs)\r
291                 + "\t");\r
292         if (sg.cs.getThreshold() != 0)\r
293         {\r
294           text.append("pidThreshold=" + sg.cs.getThreshold());\r
295         }\r
296         if (sg.cs.conservationApplied())\r
297         {\r
298           text.append("consThreshold=" + sg.cs.getConservationInc() + "\t");\r
299         }\r
300       }\r
301       text.append("outlineColour="\r
302               + jalview.util.Format.getHexString(sg.getOutlineColour())\r
303               + "\t");\r
304 \r
305       text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t");\r
306       text.append("displayText=" + sg.getDisplayText() + "\t");\r
307       text.append("colourText=" + sg.getColourText() + "\t");\r
308 \r
309       if (sg.textColour != java.awt.Color.black)\r
310       {\r
311         text.append("textCol1="\r
312                 + jalview.util.Format.getHexString(sg.textColour) + "\t");\r
313       }\r
314       if (sg.textColour2 != java.awt.Color.white)\r
315       {\r
316         text.append("textCol2="\r
317                 + jalview.util.Format.getHexString(sg.textColour2) + "\t");\r
318       }\r
319       if (sg.thresholdTextColour != 0)\r
320       {\r
321         text.append("textColThreshold=" + sg.thresholdTextColour);\r
322       }\r
323       if (sg.idColour != null)\r
324       {\r
325         text.append("idColour="\r
326                 + jalview.util.Format.getHexString(sg.idColour) + "\t");\r
327       }\r
328 \r
329       text.append("\n\n");\r
330 \r
331     }\r
332   }\r
333 \r
334   SequenceI refSeq = null;\r
335 \r
336   String refSeqId = null;\r
337 \r
338   public boolean readAnnotationFile(AlignmentI al, String file,\r
339           String protocol)\r
340   {\r
341     try\r
342     {\r
343       BufferedReader in = null;\r
344       if (protocol.equals(AppletFormatAdapter.FILE))\r
345       {\r
346         in = new BufferedReader(new FileReader(file));\r
347       }\r
348       else if (protocol.equals(AppletFormatAdapter.URL))\r
349       {\r
350         URL url = new URL(file);\r
351         in = new BufferedReader(new InputStreamReader(url.openStream()));\r
352       }\r
353       else if (protocol.equals(AppletFormatAdapter.PASTE))\r
354       {\r
355         in = new BufferedReader(new StringReader(file));\r
356       }\r
357       else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))\r
358       {\r
359         java.io.InputStream is = getClass().getResourceAsStream("/" + file);\r
360         if (is != null)\r
361         {\r
362           in = new BufferedReader(new java.io.InputStreamReader(is));\r
363         }\r
364       }\r
365 \r
366       String line, label, description, token;\r
367       int graphStyle, index;\r
368       int refSeqIndex = 1;\r
369       int existingAnnotations = 0;\r
370       if (al.getAlignmentAnnotation() != null)\r
371       {\r
372         existingAnnotations = al.getAlignmentAnnotation().length;\r
373       }\r
374 \r
375       int alWidth = al.getWidth();\r
376 \r
377       StringTokenizer st;\r
378       Annotation[] annotations;\r
379       AlignmentAnnotation annotation = null;\r
380 \r
381       // First confirm this is an Annotation file\r
382       boolean jvAnnotationFile = false;\r
383       while ((line = in.readLine()) != null)\r
384       {\r
385         if (line.indexOf("#") == 0)\r
386         {\r
387           continue;\r
388         }\r
389 \r
390         if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
391         {\r
392           jvAnnotationFile = true;\r
393           break;\r
394         }\r
395       }\r
396 \r
397       if (!jvAnnotationFile)\r
398       {\r
399         in.close();\r
400         return false;\r
401       }\r
402 \r
403       while ((line = in.readLine()) != null)\r
404       {\r
405         if (line.indexOf("#") == 0\r
406                 || line.indexOf("JALVIEW_ANNOTATION") > -1\r
407                 || line.length() == 0)\r
408         {\r
409           continue;\r
410         }\r
411 \r
412         st = new StringTokenizer(line, "\t");\r
413         token = st.nextToken();\r
414         if (token.equalsIgnoreCase("COLOUR"))\r
415         {\r
416           colourAnnotations(al, st.nextToken(), st.nextToken());\r
417           continue;\r
418         }\r
419 \r
420         else if (token.equalsIgnoreCase("COMBINE"))\r
421         {\r
422           combineAnnotations(al, st);\r
423           continue;\r
424         }\r
425 \r
426         else if (token.equalsIgnoreCase("GRAPHLINE"))\r
427         {\r
428           addLine(al, st);\r
429           continue;\r
430         }\r
431 \r
432         else if (token.equalsIgnoreCase("SEQUENCE_REF"))\r
433         {\r
434           if (st.hasMoreTokens())\r
435           {\r
436             refSeq = al.findName(refSeqId = st.nextToken());\r
437             if (refSeq == null)\r
438             {\r
439               refSeqId = null;\r
440             }\r
441             try\r
442             {\r
443               refSeqIndex = Integer.parseInt(st.nextToken());\r
444               if (refSeqIndex < 1)\r
445               {\r
446                 refSeqIndex = 1;\r
447                 System.out\r
448                         .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");\r
449               }\r
450             } catch (Exception ex)\r
451             {\r
452               refSeqIndex = 1;\r
453             }\r
454           }\r
455           else\r
456           {\r
457             refSeq = null;\r
458             refSeqId = null;\r
459           }\r
460           continue;\r
461         }\r
462 \r
463         else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))\r
464         {\r
465           addGroup(al, st);\r
466           continue;\r
467         }\r
468 \r
469         else if (token.equalsIgnoreCase("PROPERTIES"))\r
470         {\r
471           addProperties(al, st);\r
472           continue;\r
473         }\r
474 \r
475         else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))\r
476         {\r
477           setBelowAlignment(al, st);\r
478           continue;\r
479         }\r
480         else if (token.equalsIgnoreCase("ALIGNMENT"))\r
481         {\r
482           addAlignmentDetails(al, st);\r
483           continue;\r
484         }\r
485 \r
486         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
487         label = st.nextToken();\r
488 \r
489         index = 0;\r
490         annotations = new Annotation[alWidth];\r
491         description = null;\r
492         float score = Float.NaN;\r
493 \r
494         if (st.hasMoreTokens())\r
495         {\r
496           line = st.nextToken();\r
497 \r
498           if (line.indexOf("|") == -1)\r
499           {\r
500             description = line;\r
501             if (st.hasMoreTokens())\r
502               line = st.nextToken();\r
503           }\r
504 \r
505           if (st.hasMoreTokens())\r
506           {\r
507             // This must be the score\r
508             score = Float.valueOf(st.nextToken()).floatValue();\r
509           }\r
510 \r
511           st = new StringTokenizer(line, "|", true);\r
512 \r
513           boolean emptyColumn = true;\r
514           boolean onlyOneElement = (st.countTokens() == 1);\r
515 \r
516           while (st.hasMoreElements() && index < alWidth)\r
517           {\r
518             token = st.nextToken().trim();\r
519 \r
520             if (onlyOneElement)\r
521             {\r
522               try\r
523               {\r
524                 score = Float.valueOf(token).floatValue();\r
525                 break;\r
526               } catch (NumberFormatException ex)\r
527               {\r
528               }\r
529             }\r
530 \r
531             if (token.equals("|"))\r
532             {\r
533               if (emptyColumn)\r
534               {\r
535                 index++;\r
536               }\r
537 \r
538               emptyColumn = true;\r
539             }\r
540             else\r
541             {\r
542               annotations[index++] = parseAnnotation(token);\r
543               emptyColumn = false;\r
544             }\r
545           }\r
546 \r
547         }\r
548 \r
549         annotation = new AlignmentAnnotation(label, description,\r
550                 (index == 0) ? null : annotations, 0, 0, graphStyle);\r
551 \r
552         annotation.score = score;\r
553 \r
554         if (refSeq != null)\r
555         {\r
556           annotation.belowAlignment = false;\r
557           // make a copy of refSeq so we can find other matches in the alignment\r
558           SequenceI referedSeq = refSeq;\r
559           do\r
560           {\r
561             // copy before we do any mapping business.\r
562             // TODO: verify that undo/redo with 1:many sequence associated\r
563             // annotations can be undone correctly\r
564             AlignmentAnnotation ann = new AlignmentAnnotation(annotation);\r
565             annotation.createSequenceMapping(referedSeq, refSeqIndex, false);\r
566             annotation.adjustForAlignment();\r
567             referedSeq.addAlignmentAnnotation(annotation);\r
568             al.addAnnotation(annotation);\r
569             al.setAnnotationIndex(annotation,\r
570                     al.getAlignmentAnnotation().length\r
571                             - existingAnnotations - 1);\r
572             // and recover our virgin copy to use again if necessary.\r
573             annotation = ann;\r
574 \r
575           } while (refSeqId != null\r
576                   && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);\r
577         }\r
578         else\r
579         {\r
580           al.addAnnotation(annotation);\r
581           al.setAnnotationIndex(annotation,\r
582                   al.getAlignmentAnnotation().length - existingAnnotations\r
583                           - 1);\r
584         }\r
585       }\r
586 \r
587     } catch (Exception ex)\r
588     {\r
589       ex.printStackTrace();\r
590       System.out.println("Problem reading annotation file: " + ex);\r
591       return false;\r
592     }\r
593     return true;\r
594   }\r
595 \r
596   Annotation parseAnnotation(String string)\r
597   {\r
598     String desc = null, displayChar = null;\r
599     char ss = ' '; // secondaryStructure\r
600     float value = 0;\r
601     boolean parsedValue = false;\r
602 \r
603     // find colour here\r
604     java.awt.Color colour = null;\r
605     int i = string.indexOf("[");\r
606     int j = string.indexOf("]");\r
607     if (i > -1 && j > -1)\r
608     {\r
609       UserColourScheme ucs = new UserColourScheme();\r
610 \r
611       colour = ucs.getColourFromString(string.substring(i + 1, j));\r
612 \r
613       string = string.substring(0, i) + string.substring(j + 1);\r
614     }\r
615 \r
616     StringTokenizer st = new StringTokenizer(string, ",");\r
617     String token;\r
618     while (st.hasMoreTokens())\r
619     {\r
620       token = st.nextToken().trim();\r
621       if (token.length() == 0)\r
622       {\r
623         continue;\r
624       }\r
625 \r
626       if (!parsedValue)\r
627       {\r
628         try\r
629         {\r
630           displayChar = token;\r
631           value = new Float(token).floatValue();\r
632           parsedValue = true;\r
633           continue;\r
634         } catch (NumberFormatException ex)\r
635         {\r
636         }\r
637       }\r
638 \r
639       if (token.equals("H") || token.equals("E"))\r
640       {\r
641         // Either this character represents a helix or sheet\r
642         // or an integer which can be displayed\r
643         ss = token.charAt(0);\r
644         if (displayChar.equals(token.substring(0, 1)))\r
645         {\r
646           displayChar = "";\r
647         }\r
648       }\r
649       else if (desc == null)\r
650       {\r
651         desc = token;\r
652       }\r
653 \r
654     }\r
655 \r
656     if (displayChar != null && displayChar.length() > 1 && desc != null\r
657             && desc.length() == 1)\r
658     {\r
659       String tmp = displayChar;\r
660       displayChar = desc;\r
661       desc = tmp;\r
662     }\r
663     /*\r
664      * In principle, this code will ensure that the Annotation element generated\r
665      * is renderable by any of the applet or application rendering code but\r
666      * instead we check for null strings when the display character is rendered.\r
667      * if (displayChar==null) { displayChar=""; }\r
668      */\r
669     Annotation anot = new Annotation(displayChar, desc, ss, value);\r
670 \r
671     anot.colour = colour;\r
672 \r
673     return anot;\r
674   }\r
675 \r
676   void colourAnnotations(AlignmentI al, String label, String colour)\r
677   {\r
678     UserColourScheme ucs = new UserColourScheme(colour);\r
679     Annotation[] annotations;\r
680     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
681     {\r
682       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
683       {\r
684         annotations = al.getAlignmentAnnotation()[i].annotations;\r
685         for (int j = 0; j < annotations.length; j++)\r
686         {\r
687           if (annotations[j] != null)\r
688           {\r
689             annotations[j].colour = ucs.findColour('A');\r
690           }\r
691         }\r
692       }\r
693     }\r
694   }\r
695 \r
696   void combineAnnotations(AlignmentI al, StringTokenizer st)\r
697   {\r
698     int graphGroup = -1;\r
699     String group = st.nextToken();\r
700     // First make sure we are not overwriting the graphIndex\r
701     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
702     {\r
703       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
704       {\r
705         graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;\r
706         al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
707         break;\r
708       }\r
709     }\r
710 \r
711     // Now update groups\r
712     while (st.hasMoreTokens())\r
713     {\r
714       group = st.nextToken();\r
715       for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
716       {\r
717         if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
718         {\r
719           al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
720           break;\r
721         }\r
722       }\r
723     }\r
724   }\r
725 \r
726   void addLine(AlignmentI al, StringTokenizer st)\r
727   {\r
728     String group = st.nextToken();\r
729     AlignmentAnnotation annotation = null;\r
730 \r
731     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
732     {\r
733       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
734       {\r
735         annotation = al.getAlignmentAnnotation()[i];\r
736         break;\r
737       }\r
738     }\r
739 \r
740     if (annotation == null)\r
741     {\r
742       return;\r
743     }\r
744     float value = new Float(st.nextToken()).floatValue();\r
745     String label = st.hasMoreTokens() ? st.nextToken() : null;\r
746     java.awt.Color colour = null;\r
747     if (st.hasMoreTokens())\r
748     {\r
749       UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
750       colour = ucs.findColour('A');\r
751     }\r
752 \r
753     annotation.setThreshold(new GraphLine(value, label, colour));\r
754   }\r
755 \r
756   void addGroup(AlignmentI al, StringTokenizer st)\r
757   {\r
758     SequenceGroup sg = new SequenceGroup();\r
759     sg.setName(st.nextToken());\r
760     sg.setStartRes(Integer.parseInt(st.nextToken()) - 1);\r
761     sg.setEndRes(Integer.parseInt(st.nextToken()) - 1);\r
762 \r
763     String index = st.nextToken();\r
764     if (index.equals("-1"))\r
765     {\r
766       while (st.hasMoreElements())\r
767       {\r
768         sg.addSequence(al.findName(st.nextToken()), false);\r
769       }\r
770     }\r
771     else\r
772     {\r
773       StringTokenizer st2 = new StringTokenizer(index, ",");\r
774 \r
775       while (st2.hasMoreTokens())\r
776       {\r
777         String tmp = st2.nextToken();\r
778         if (tmp.equals("*"))\r
779         {\r
780           for (int i = 0; i < al.getHeight(); i++)\r
781           {\r
782             sg.addSequence(al.getSequenceAt(i), false);\r
783           }\r
784         }\r
785         else if (tmp.indexOf("-") >= 0)\r
786         {\r
787           StringTokenizer st3 = new StringTokenizer(tmp, "-");\r
788 \r
789           int start = (Integer.parseInt(st3.nextToken()));\r
790           int end = (Integer.parseInt(st3.nextToken()));\r
791 \r
792           if (end > start)\r
793           {\r
794             for (int i = start; i <= end; i++)\r
795             {\r
796               sg.addSequence(al.getSequenceAt(i - 1), false);\r
797             }\r
798           }\r
799         }\r
800         else\r
801         {\r
802           sg\r
803                   .addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1),\r
804                           false);\r
805         }\r
806       }\r
807     }\r
808 \r
809     if (refSeq != null)\r
810     {\r
811       sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);\r
812       sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);\r
813     }\r
814 \r
815     if (sg.getSize() > 0)\r
816     {\r
817       al.addGroup(sg);\r
818     }\r
819   }\r
820 \r
821   void addProperties(AlignmentI al, StringTokenizer st)\r
822   {\r
823 \r
824     // So far we have only added groups to the annotationHash,\r
825     // the idea is in the future properties can be added to\r
826     // alignments, other annotations etc\r
827     if (al.getGroups() == null)\r
828     {\r
829       return;\r
830     }\r
831     SequenceGroup sg = null;\r
832 \r
833     String name = st.nextToken();\r
834 \r
835     Vector groups = al.getGroups();\r
836     for (int i = 0; i < groups.size(); i++)\r
837     {\r
838       sg = (SequenceGroup) groups.elementAt(i);\r
839       if (sg.getName().equals(name))\r
840       {\r
841         break;\r
842       }\r
843       else\r
844       {\r
845         sg = null;\r
846       }\r
847     }\r
848 \r
849     if (sg != null)\r
850     {\r
851       String keyValue, key, value;\r
852       ColourSchemeI def = sg.cs;\r
853       sg.cs = null;\r
854       while (st.hasMoreTokens())\r
855       {\r
856         keyValue = st.nextToken();\r
857         key = keyValue.substring(0, keyValue.indexOf("="));\r
858         value = keyValue.substring(keyValue.indexOf("=") + 1);\r
859 \r
860         if (key.equalsIgnoreCase("description"))\r
861         {\r
862           sg.setDescription(value);\r
863         }\r
864         else if (key.equalsIgnoreCase("colour"))\r
865         {\r
866           sg.cs = ColourSchemeProperty.getColour(al, value);\r
867         }\r
868         else if (key.equalsIgnoreCase("pidThreshold"))\r
869         {\r
870           sg.cs.setThreshold(Integer.parseInt(value), true);\r
871 \r
872         }\r
873         else if (key.equalsIgnoreCase("consThreshold"))\r
874         {\r
875           sg.cs.setConservationInc(Integer.parseInt(value));\r
876           Conservation c = new Conservation("Group",\r
877                   ResidueProperties.propHash, 3, sg.getSequences(null), sg\r
878                           .getStartRes(), sg.getEndRes() + 1);\r
879 \r
880           c.calculate();\r
881           c.verdict(false, 25);\r
882 \r
883           sg.cs.setConservation(c);\r
884 \r
885         }\r
886         else if (key.equalsIgnoreCase("outlineColour"))\r
887         {\r
888           sg.setOutlineColour(new UserColourScheme(value).findColour('A'));\r
889         }\r
890         else if (key.equalsIgnoreCase("displayBoxes"))\r
891         {\r
892           sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());\r
893         }\r
894         else if (key.equalsIgnoreCase("displayText"))\r
895         {\r
896           sg.setDisplayText(Boolean.valueOf(value).booleanValue());\r
897         }\r
898         else if (key.equalsIgnoreCase("colourText"))\r
899         {\r
900           sg.setColourText(Boolean.valueOf(value).booleanValue());\r
901         }\r
902         else if (key.equalsIgnoreCase("textCol1"))\r
903         {\r
904           sg.textColour = new UserColourScheme(value).findColour('A');\r
905         }\r
906         else if (key.equalsIgnoreCase("textCol2"))\r
907         {\r
908           sg.textColour2 = new UserColourScheme(value).findColour('A');\r
909         }\r
910         else if (key.equalsIgnoreCase("textColThreshold"))\r
911         {\r
912           sg.thresholdTextColour = Integer.parseInt(value);\r
913         }\r
914         else if (key.equalsIgnoreCase("idColour"))\r
915         {\r
916           // consider warning if colour doesn't resolve to a real colour\r
917           sg.setIdColour((def = new UserColourScheme(value))\r
918                   .findColour('A'));\r
919         }\r
920         sg.recalcConservation();\r
921       }\r
922       if (sg.cs == null)\r
923       {\r
924         sg.cs = def;\r
925       }\r
926     }\r
927   }\r
928 \r
929   void setBelowAlignment(AlignmentI al, StringTokenizer st)\r
930   {\r
931     String token;\r
932     AlignmentAnnotation aa;\r
933     while (st.hasMoreTokens())\r
934     {\r
935       token = st.nextToken();\r
936       for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
937       {\r
938         aa = al.getAlignmentAnnotation()[i];\r
939         if (aa.sequenceRef == refSeq && aa.label.equals(token))\r
940         {\r
941           aa.belowAlignment = true;\r
942         }\r
943       }\r
944     }\r
945   }\r
946 \r
947   void addAlignmentDetails(AlignmentI al, StringTokenizer st)\r
948   {\r
949     String keyValue, key, value;\r
950     while (st.hasMoreTokens())\r
951     {\r
952       keyValue = st.nextToken();\r
953       key = keyValue.substring(0, keyValue.indexOf("="));\r
954       value = keyValue.substring(keyValue.indexOf("=") + 1);\r
955       al.setProperty(key, value);\r
956     }\r
957   }\r
958 \r
959   /**\r
960    * Write annotations as a CSV file of the form 'label, value, value, ...' for\r
961    * each row.\r
962    * \r
963    * @param annotations\r
964    * @return CSV file as a string.\r
965    */\r
966   public String printCSVAnnotations(AlignmentAnnotation[] annotations)\r
967   {\r
968     StringBuffer sp = new StringBuffer();\r
969     for (int i = 0; i < annotations.length; i++)\r
970     {\r
971       String atos = annotations[i].toString();\r
972       int p = 0;\r
973       do\r
974       {\r
975         int cp = atos.indexOf("\n", p);\r
976         sp.append(annotations[i].label);\r
977         sp.append(",");\r
978         if (cp > p)\r
979         {\r
980           sp.append(atos.substring(p, cp + 1));\r
981         }\r
982         else\r
983         {\r
984           sp.append(atos.substring(p));\r
985           sp.append("\n");\r
986         }\r
987         p = cp + 1;\r
988       } while (p > 0);\r
989     }\r
990     return sp.toString();\r
991   }\r
992 }\r