update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / AnnotationFile.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
10  * \r
11  * Jalview is distributed in the hope that it will be useful, but \r
12  * WITHOUT ANY WARRANTY; without even the implied warranty \r
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
14  * PURPOSE.  See the GNU General Public License for more details.\r
15  * \r
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
17  */\r
18 package jalview.io;\r
19 \r
20 import java.io.*;\r
21 import java.net.*;\r
22 import java.util.*;\r
23 \r
24 import jalview.analysis.*;\r
25 import jalview.datamodel.*;\r
26 import jalview.schemes.*;\r
27 \r
28 public class AnnotationFile\r
29 {\r
30   public AnnotationFile()\r
31   {\r
32     init();\r
33   }\r
34 \r
35   /**\r
36    * character used to write newlines\r
37    */\r
38   protected String newline = System.getProperty("line.separator");\r
39 \r
40   /**\r
41    * set new line string and reset the output buffer\r
42    * \r
43    * @param nl\r
44    */\r
45   public void setNewlineString(String nl)\r
46   {\r
47     newline = nl;\r
48     init();\r
49   }\r
50 \r
51   public String getNewlineString()\r
52   {\r
53     return newline;\r
54   }\r
55 \r
56   StringBuffer text;\r
57 \r
58   private void init()\r
59   {\r
60     text = new StringBuffer("JALVIEW_ANNOTATION"+newline + "# Created: "\r
61             + new java.util.Date() + newline + newline);\r
62     refSeq = null;\r
63     refSeqId = null;\r
64   }\r
65 \r
66   /**\r
67    * convenience method for pre-2.4 feature files which have no view, hidden\r
68    * columns or hidden row keywords.\r
69    * \r
70    * @param annotations\r
71    * @param groups\r
72    * @param properties\r
73    * @return feature file as a string.\r
74    */\r
75   public String printAnnotations(AlignmentAnnotation[] annotations,\r
76           Vector groups, Hashtable properties)\r
77   {\r
78     return printAnnotations(annotations, groups, properties, null);\r
79 \r
80   }\r
81 \r
82   /**\r
83    * hold all the information about a particular view definition read from or\r
84    * written out in an annotations file.\r
85    */\r
86   public class ViewDef\r
87   {\r
88     public String viewname;\r
89 \r
90     public HiddenSequences hidseqs;\r
91 \r
92     public ColumnSelection hiddencols;\r
93 \r
94     public Vector visibleGroups;\r
95 \r
96     public Hashtable hiddenRepSeqs;\r
97 \r
98     public ViewDef(String viewname, HiddenSequences hidseqs,\r
99             ColumnSelection hiddencols, Hashtable hiddenRepSeqs)\r
100     {\r
101       this.viewname = viewname;\r
102       this.hidseqs = hidseqs;\r
103       this.hiddencols = hiddencols;\r
104       this.hiddenRepSeqs = hiddenRepSeqs;\r
105     }\r
106   }\r
107 \r
108   /**\r
109    * Prepare an annotation file given a set of annotations, groups, alignment\r
110    * properties and views.\r
111    * \r
112    * @param annotations\r
113    * @param groups\r
114    * @param properties\r
115    * @param views\r
116    * @return annotation file\r
117    */\r
118   public String printAnnotations(AlignmentAnnotation[] annotations,\r
119           Vector groups, Hashtable properties, ViewDef[] views)\r
120   {\r
121     // TODO: resolve views issue : annotationFile could contain visible region,\r
122     // or full data + hidden region specifications for a view.\r
123     if (annotations != null)\r
124     {\r
125       boolean oneColour = true;\r
126       AlignmentAnnotation row;\r
127       String comma;\r
128       SequenceI refSeq = null;\r
129       SequenceGroup refGroup = null;\r
130 \r
131       StringBuffer colours = new StringBuffer();\r
132       StringBuffer graphLine = new StringBuffer();\r
133       StringBuffer rowprops = new StringBuffer();\r
134       Hashtable graphGroup = new Hashtable();\r
135 \r
136       java.awt.Color color;\r
137 \r
138       for (int i = 0; i < annotations.length; i++)\r
139       {\r
140         row = annotations[i];\r
141 \r
142         if (!row.visible && !row.hasScore())\r
143         {\r
144           continue;\r
145         }\r
146 \r
147         color = null;\r
148         oneColour = true;\r
149 \r
150         if (row.sequenceRef == null)\r
151         {\r
152           if (refSeq != null)\r
153           {\r
154             text.append(newline);\r
155             text.append("SEQUENCE_REF\tALIGNMENT");\r
156             text.append(newline);\r
157           }\r
158 \r
159           refSeq = null;\r
160         }\r
161 \r
162         else\r
163         {\r
164           if (refSeq == null || refSeq != row.sequenceRef)\r
165           {\r
166             refSeq = row.sequenceRef;\r
167             text.append(newline);\r
168             text.append("SEQUENCE_REF\t");\r
169             text.append(refSeq.getName());\r
170             text.append(newline);\r
171           }\r
172         }\r
173         // mark any group references for the row\r
174         if (row.groupRef == null)\r
175         {\r
176 \r
177           if (refGroup != null)\r
178           {\r
179             text.append(newline);\r
180             text.append("GROUP_REF\tALIGNMENT");\r
181             text.append(newline);\r
182           }\r
183 \r
184           refGroup = null;\r
185         }\r
186         else\r
187         {\r
188           if (refGroup == null || refGroup != row.groupRef)\r
189           {\r
190             refGroup = row.groupRef;\r
191             text.append(newline);\r
192             text.append("GROUP_REF\t");\r
193             text.append(refGroup.getName());\r
194             text.append(newline);\r
195           }\r
196         }\r
197 \r
198         boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;\r
199         // lookahead to check what the annotation row object actually contains.\r
200         for (int j = 0; row.annotations != null\r
201                 && j < row.annotations.length\r
202                 && (!hasGlyphs || !hasLabels || !hasValues); j++)\r
203         {\r
204           if (row.annotations[j] != null)\r
205           {\r
206             hasLabels |= (row.annotations[j].displayCharacter != null\r
207                     && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter\r
208                     .equals(" "));\r
209             hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');\r
210             hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't\r
211             // be\r
212             // rendered..\r
213             hasText |= (row.annotations[j].description != null && row.annotations[j].description\r
214                     .length() > 0);\r
215           }\r
216         }\r
217 \r
218         if (row.graph == AlignmentAnnotation.NO_GRAPH)\r
219         {\r
220           text.append("NO_GRAPH\t");\r
221           hasValues = false; // only secondary structure\r
222           // hasLabels = false; // and annotation description string.\r
223         }\r
224         else\r
225         {\r
226           if (row.graph == AlignmentAnnotation.BAR_GRAPH)\r
227           {\r
228             text.append("BAR_GRAPH\t");\r
229             hasGlyphs = false; // no secondary structure\r
230 \r
231           }\r
232           else if (row.graph == AlignmentAnnotation.LINE_GRAPH)\r
233           {\r
234             hasGlyphs = false; // no secondary structure\r
235             text.append("LINE_GRAPH\t");\r
236           }\r
237 \r
238           if (row.getThreshold() != null)\r
239           {\r
240             graphLine.append("GRAPHLINE\t");\r
241             graphLine.append(row.label);\r
242             graphLine.append("\t");\r
243             graphLine.append(row.getThreshold().value);\r
244             graphLine.append("\t");\r
245             graphLine.append(row.getThreshold().label);\r
246             graphLine.append("\t");\r
247             graphLine.append(jalview.util.Format.getHexString(row\r
248                     .getThreshold().colour));\r
249             graphLine.append(newline);\r
250           }\r
251 \r
252           if (row.graphGroup > -1)\r
253           {\r
254             String key = String.valueOf(row.graphGroup);\r
255             if (graphGroup.containsKey(key))\r
256             {\r
257               graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);\r
258             }\r
259             else\r
260             {\r
261               graphGroup.put(key, row.label);\r
262             }\r
263           }\r
264         }\r
265 \r
266         text.append(row.label + "\t");\r
267         if (row.description != null)\r
268         {\r
269           text.append(row.description + "\t");\r
270         }\r
271         for (int j = 0; row.annotations != null\r
272                 && j < row.annotations.length; j++)\r
273         {\r
274           if (refSeq != null\r
275                   && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))\r
276           {\r
277             continue;\r
278           }\r
279 \r
280           if (row.annotations[j] != null)\r
281           {\r
282             comma = "";\r
283             if (hasGlyphs) // could be also hasGlyphs || ...\r
284             {\r
285 \r
286               text.append(comma);\r
287               if (row.annotations[j].secondaryStructure != ' ')\r
288               {\r
289                 // only write out the field if its not whitespace.\r
290                 text.append(row.annotations[j].secondaryStructure);\r
291               }\r
292               comma = ",";\r
293             }\r
294             if (hasValues)\r
295             {\r
296               if (row.annotations[j].value != Float.NaN)\r
297               {\r
298                 text.append(comma + row.annotations[j].value);\r
299               }\r
300               else\r
301               {\r
302                 System.err.println("Skipping NaN - not valid value.");\r
303                 text.append(comma + 0f);// row.annotations[j].value);\r
304               }\r
305               comma = ",";\r
306             }\r
307             if (hasLabels)\r
308             {\r
309               // TODO: labels are emitted after values for bar graphs.\r
310               if // empty labels are allowed, so\r
311               (row.annotations[j].displayCharacter != null\r
312                       && row.annotations[j].displayCharacter.length() > 0\r
313                       && !row.annotations[j].displayCharacter.equals(" "))\r
314               {\r
315                 text.append(comma + row.annotations[j].displayCharacter);\r
316                 comma = ",";\r
317               }\r
318             }\r
319             if (hasText)\r
320             {\r
321               if (row.annotations[j].description != null\r
322                       && row.annotations[j].description.length() > 0\r
323                       && !row.annotations[j].description\r
324                               .equals(row.annotations[j].displayCharacter))\r
325               {\r
326                 text.append(comma + row.annotations[j].description);\r
327                 comma = ",";\r
328               }\r
329             }\r
330             if (color != null && !color.equals(row.annotations[j].colour))\r
331             {\r
332               oneColour = false;\r
333             }\r
334 \r
335             color = row.annotations[j].colour;\r
336 \r
337             if (row.annotations[j].colour != null\r
338                     && row.annotations[j].colour != java.awt.Color.black)\r
339             {\r
340               text.append(comma\r
341                       + "["\r
342                       + jalview.util.Format\r
343                               .getHexString(row.annotations[j].colour)\r
344                       + "]");\r
345               comma = ",";\r
346             }\r
347           }\r
348           text.append("|");\r
349         }\r
350 \r
351         if (row.hasScore())\r
352           text.append("\t" + row.score);\r
353 \r
354         text.append(newline);\r
355 \r
356         if (color != null && color != java.awt.Color.black && oneColour)\r
357         {\r
358           colours.append("COLOUR\t");\r
359           colours.append(row.label);\r
360           colours.append("\t");\r
361           colours.append(jalview.util.Format.getHexString(color));\r
362           colours.append(newline);\r
363         }\r
364         if (row.scaleColLabel || row.showAllColLabels\r
365                 || row.centreColLabels)\r
366         {\r
367           rowprops.append("ROWPROPERTIES\t");\r
368           rowprops.append(row.label);\r
369           rowprops.append("\tscaletofit=");\r
370           rowprops.append(row.scaleColLabel);\r
371           rowprops.append("\tshowalllabs=");\r
372           rowprops.append(row.showAllColLabels);\r
373           rowprops.append("\tcentrelabs=");\r
374           rowprops.append(row.centreColLabels);\r
375           rowprops.append(newline);\r
376         }\r
377       }\r
378 \r
379       text.append(newline);\r
380 \r
381       text.append(colours.toString());\r
382       text.append(graphLine.toString());\r
383       if (graphGroup.size() > 0)\r
384       {\r
385         text.append("COMBINE\t");\r
386         Enumeration en = graphGroup.elements();\r
387         while (en.hasMoreElements())\r
388         {\r
389           text.append(en.nextElement());\r
390           text.append(newline);\r
391         }\r
392       }\r
393       text.append(rowprops.toString());\r
394     }\r
395 \r
396     if (groups != null)\r
397     {\r
398       printGroups(groups);\r
399     }\r
400 \r
401     if (properties != null)\r
402     {\r
403       text.append(newline);\r
404       text.append(newline);\r
405       text.append("ALIGNMENT");\r
406       Enumeration en = properties.keys();\r
407       while (en.hasMoreElements())\r
408       {\r
409         String key = en.nextElement().toString();\r
410         text.append("\t");\r
411         text.append(key);\r
412         text.append("=");\r
413         text.append(properties.get(key));\r
414       }\r
415       // TODO: output alignment visualization settings here if required\r
416 \r
417     }\r
418 \r
419     return text.toString();\r
420   }\r
421 \r
422   public void printGroups(Vector sequenceGroups)\r
423   {\r
424     SequenceGroup sg;\r
425     SequenceI seqrep = null;\r
426     for (int i = 0; i < sequenceGroups.size(); i++)\r
427     {\r
428       sg = (SequenceGroup) sequenceGroups.elementAt(i);\r
429       if (!sg.hasSeqrep())\r
430       {\r
431         text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"\r
432                 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)\r
433                 + "\t" + "-1\t");\r
434         seqrep = null;\r
435       }\r
436       else\r
437       {\r
438         seqrep = sg.getSeqrep();\r
439         text.append("SEQUENCE_REF\t");\r
440         text.append(seqrep.getName());\r
441         text.append(newline);\r
442         text.append("SEQUENCE_GROUP\t");\r
443         text.append(sg.getName());\r
444         text.append("\t");\r
445         text.append((seqrep.findPosition(sg.getStartRes())));\r
446         text.append("\t");\r
447         text.append((seqrep.findPosition(sg.getEndRes())));\r
448         text.append("\t");\r
449         text.append("-1\t");\r
450       }\r
451       for (int s = 0; s < sg.getSize(); s++)\r
452       {\r
453         text.append(sg.getSequenceAt(s).getName());\r
454         text.append("\t");\r
455       }\r
456       text.append(newline);\r
457       text.append("PROPERTIES\t");\r
458       text.append(sg.getName());\r
459       text.append("\t");\r
460 \r
461       if (sg.getDescription() != null)\r
462       {\r
463         text.append("description=");\r
464         text.append(sg.getDescription());\r
465         text.append("\t");\r
466       }\r
467       if (sg.cs != null)\r
468       {\r
469         text.append("colour=");\r
470         text.append(ColourSchemeProperty.getColourName(sg.cs));\r
471         text.append("\t");\r
472         if (sg.cs.getThreshold() != 0)\r
473         {\r
474           text.append("pidThreshold=");\r
475           text.append(sg.cs.getThreshold());\r
476         }\r
477         if (sg.cs.conservationApplied())\r
478         {\r
479           text.append("consThreshold=");\r
480           text.append(sg.cs.getConservationInc());\r
481           text.append("\t");\r
482         }\r
483       }\r
484       text.append("outlineColour=");\r
485       text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));\r
486       text.append("\t");\r
487 \r
488       text.append("displayBoxes=");\r
489       text.append(sg.getDisplayBoxes());\r
490       text.append("\t");\r
491       text.append("displayText=");\r
492       text.append(sg.getDisplayText());\r
493       text.append("\t");\r
494       text.append("colourText=");\r
495       text.append(sg.getColourText());\r
496       text.append("\t");\r
497       text.append("showUnconserved=");\r
498       text.append(sg.getShowNonconserved());\r
499       text.append("\t");\r
500       if (sg.textColour != java.awt.Color.black)\r
501       {\r
502         text.append("textCol1=");\r
503         text.append(jalview.util.Format.getHexString(sg.textColour));\r
504         text.append("\t");\r
505       }\r
506       if (sg.textColour2 != java.awt.Color.white)\r
507       {\r
508         text.append("textCol2=");\r
509         text.append(jalview.util.Format.getHexString(sg.textColour2));\r
510         text.append("\t");\r
511       }\r
512       if (sg.thresholdTextColour != 0)\r
513       {\r
514         text.append("textColThreshold=");\r
515         text.append(sg.thresholdTextColour);\r
516         text.append("\t");\r
517       }\r
518       if (sg.idColour != null)\r
519       {\r
520         text.append("idColour=");\r
521         text.append(jalview.util.Format.getHexString(sg.idColour));\r
522         text.append("\t");\r
523       }\r
524       if (sg.isHidereps())\r
525       {\r
526         text.append("hide=true\t");\r
527       }\r
528       if (sg.isHideCols())\r
529       {\r
530         text.append("hidecols=true\t");\r
531       }\r
532       if (seqrep != null)\r
533       {\r
534         // terminate the last line and clear the sequence ref for the group\r
535         text.append(newline);\r
536         text.append("SEQUENCE_REF");\r
537       }\r
538       text.append(newline);\r
539       text.append(newline);\r
540 \r
541     }\r
542   }\r
543 \r
544   SequenceI refSeq = null;\r
545 \r
546   String refSeqId = null;\r
547 \r
548   public boolean readAnnotationFile(AlignmentI al, String file,\r
549           String protocol)\r
550   {\r
551     BufferedReader in = null;\r
552     try\r
553     {\r
554       if (protocol.equals(AppletFormatAdapter.FILE))\r
555       {\r
556         in = new BufferedReader(new FileReader(file));\r
557       }\r
558       else if (protocol.equals(AppletFormatAdapter.URL))\r
559       {\r
560         URL url = new URL(file);\r
561         in = new BufferedReader(new InputStreamReader(url.openStream()));\r
562       }\r
563       else if (protocol.equals(AppletFormatAdapter.PASTE))\r
564       {\r
565         in = new BufferedReader(new StringReader(file));\r
566       }\r
567       else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))\r
568       {\r
569         java.io.InputStream is = getClass().getResourceAsStream("/" + file);\r
570         if (is != null)\r
571         {\r
572           in = new BufferedReader(new java.io.InputStreamReader(is));\r
573         }\r
574       }\r
575       if (in != null)\r
576       {\r
577         return parseAnnotationFrom(al, in);\r
578       }\r
579 \r
580     } catch (Exception ex)\r
581     {\r
582       ex.printStackTrace();\r
583       System.out.println("Problem reading annotation file: " + ex);\r
584       return false;\r
585     }\r
586     return false;\r
587   }\r
588 \r
589   public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)\r
590           throws Exception\r
591   {\r
592     boolean modified = false;\r
593     String groupRef = null;\r
594     Hashtable groupRefRows = new Hashtable();\r
595 \r
596     Hashtable autoAnnots = new Hashtable();\r
597     {\r
598       String line, label, description, token;\r
599       int graphStyle, index;\r
600       int refSeqIndex = 1;\r
601       int existingAnnotations = 0;\r
602       // when true - will add new rows regardless of whether they are duplicate\r
603       // auto-annotation like consensus or conservation graphs\r
604       boolean overrideAutoAnnot = false;\r
605       if (al.getAlignmentAnnotation() != null)\r
606       {\r
607         existingAnnotations = al.getAlignmentAnnotation().length;\r
608         if (existingAnnotations > 0)\r
609         {\r
610           AlignmentAnnotation[] aa = al.getAlignmentAnnotation();\r
611           for (int aai = 0; aai < aa.length; aai++)\r
612           {\r
613             if (aa[aai].autoCalculated)\r
614             {\r
615               // make a note of the name and description\r
616               autoAnnots.put(\r
617                       autoAnnotsKey(aa[aai], aa[aai].sequenceRef,\r
618                               (aa[aai].groupRef == null ? null\r
619                                       : aa[aai].groupRef.getName())),\r
620                       new Integer(1));\r
621             }\r
622           }\r
623         }\r
624       }\r
625 \r
626       int alWidth = al.getWidth();\r
627 \r
628       StringTokenizer st;\r
629       Annotation[] annotations;\r
630       AlignmentAnnotation annotation = null;\r
631 \r
632       // First confirm this is an Annotation file\r
633       boolean jvAnnotationFile = false;\r
634       while ((line = in.readLine()) != null)\r
635       {\r
636         if (line.indexOf("#") == 0)\r
637         {\r
638           continue;\r
639         }\r
640 \r
641         if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
642         {\r
643           jvAnnotationFile = true;\r
644           break;\r
645         }\r
646       }\r
647 \r
648       if (!jvAnnotationFile)\r
649       {\r
650         in.close();\r
651         return false;\r
652       }\r
653 \r
654       while ((line = in.readLine()) != null)\r
655       {\r
656         if (line.indexOf("#") == 0\r
657                 || line.indexOf("JALVIEW_ANNOTATION") > -1\r
658                 || line.length() == 0)\r
659         {\r
660           continue;\r
661         }\r
662 \r
663         st = new StringTokenizer(line, "\t");\r
664         token = st.nextToken();\r
665         if (token.equalsIgnoreCase("COLOUR"))\r
666         {\r
667           // TODO: use graduated colour def'n here too\r
668           colourAnnotations(al, st.nextToken(), st.nextToken());\r
669           modified = true;\r
670           continue;\r
671         }\r
672 \r
673         else if (token.equalsIgnoreCase("COMBINE"))\r
674         {\r
675           combineAnnotations(al, st);\r
676           modified = true;\r
677           continue;\r
678         }\r
679         else if (token.equalsIgnoreCase("ROWPROPERTIES"))\r
680         {\r
681           addRowProperties(al, st);\r
682           modified = true;\r
683           continue;\r
684         }\r
685         else if (token.equalsIgnoreCase("GRAPHLINE"))\r
686         {\r
687           addLine(al, st);\r
688           modified = true;\r
689           continue;\r
690         }\r
691 \r
692         else if (token.equalsIgnoreCase("SEQUENCE_REF"))\r
693         {\r
694           if (st.hasMoreTokens())\r
695           {\r
696             refSeq = al.findName(refSeqId = st.nextToken());\r
697             if (refSeq == null)\r
698             {\r
699               refSeqId = null;\r
700             }\r
701             try\r
702             {\r
703               refSeqIndex = Integer.parseInt(st.nextToken());\r
704               if (refSeqIndex < 1)\r
705               {\r
706                 refSeqIndex = 1;\r
707                 System.out\r
708                         .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");\r
709               }\r
710             } catch (Exception ex)\r
711             {\r
712               refSeqIndex = 1;\r
713             }\r
714           }\r
715           else\r
716           {\r
717             refSeq = null;\r
718             refSeqId = null;\r
719           }\r
720           continue;\r
721         }\r
722         else if (token.equalsIgnoreCase("GROUP_REF"))\r
723         {\r
724           // Group references could be forward or backwards, so they are\r
725           // resolved after the whole file is read in\r
726           groupRef = null;\r
727           if (st.hasMoreTokens())\r
728           {\r
729             groupRef = st.nextToken();\r
730             if (groupRef.length() < 1)\r
731             {\r
732               groupRef = null; // empty string\r
733             }\r
734             else\r
735             {\r
736               if (groupRefRows.get(groupRef) == null)\r
737               {\r
738                 groupRefRows.put(groupRef, new Vector());\r
739               }\r
740             }\r
741           }\r
742           continue;\r
743         }\r
744         else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))\r
745         {\r
746           addGroup(al, st);\r
747           continue;\r
748         }\r
749 \r
750         else if (token.equalsIgnoreCase("PROPERTIES"))\r
751         {\r
752           addProperties(al, st);\r
753           modified = true;\r
754           continue;\r
755         }\r
756 \r
757         else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))\r
758         {\r
759           setBelowAlignment(al, st);\r
760           modified = true;\r
761           continue;\r
762         }\r
763         else if (token.equalsIgnoreCase("ALIGNMENT"))\r
764         {\r
765           addAlignmentDetails(al, st);\r
766           modified = true;\r
767           continue;\r
768         }\r
769 \r
770         // Parse out the annotation row\r
771         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
772         label = st.nextToken();\r
773 \r
774         index = 0;\r
775         annotations = new Annotation[alWidth];\r
776         description = null;\r
777         float score = Float.NaN;\r
778 \r
779         if (st.hasMoreTokens())\r
780         {\r
781           line = st.nextToken();\r
782 \r
783           if (line.indexOf("|") == -1)\r
784           {\r
785             description = line;\r
786             if (st.hasMoreTokens())\r
787               line = st.nextToken();\r
788           }\r
789 \r
790           if (st.hasMoreTokens())\r
791           {\r
792             // This must be the score\r
793             score = Float.valueOf(st.nextToken()).floatValue();\r
794           }\r
795 \r
796           st = new StringTokenizer(line, "|", true);\r
797 \r
798           boolean emptyColumn = true;\r
799           boolean onlyOneElement = (st.countTokens() == 1);\r
800 \r
801           while (st.hasMoreElements() && index < alWidth)\r
802           {\r
803             token = st.nextToken().trim();\r
804 \r
805             if (onlyOneElement)\r
806             {\r
807               try\r
808               {\r
809                 score = Float.valueOf(token).floatValue();\r
810                 break;\r
811               } catch (NumberFormatException ex)\r
812               {\r
813               }\r
814             }\r
815 \r
816             if (token.equals("|"))\r
817             {\r
818               if (emptyColumn)\r
819               {\r
820                 index++;\r
821               }\r
822 \r
823               emptyColumn = true;\r
824             }\r
825             else\r
826             {\r
827               annotations[index++] = parseAnnotation(token, graphStyle);\r
828               emptyColumn = false;\r
829             }\r
830           }\r
831 \r
832         }\r
833 \r
834         annotation = new AlignmentAnnotation(label, description,\r
835                 (index == 0) ? null : annotations, 0, 0, graphStyle);\r
836 \r
837         annotation.score = score;\r
838         if (!overrideAutoAnnot\r
839                 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,\r
840                         groupRef)))\r
841         {\r
842           // skip - we've already got an automatic annotation of this type.\r
843           continue;\r
844         }\r
845         // otherwise add it!\r
846         if (refSeq != null)\r
847         {\r
848 \r
849           annotation.belowAlignment = false;\r
850           // make a copy of refSeq so we can find other matches in the alignment\r
851           SequenceI referedSeq = refSeq;\r
852           do\r
853           {\r
854             // copy before we do any mapping business.\r
855             // TODO: verify that undo/redo with 1:many sequence associated\r
856             // annotations can be undone correctly\r
857             AlignmentAnnotation ann = new AlignmentAnnotation(annotation);\r
858             annotation\r
859                     .createSequenceMapping(referedSeq, refSeqIndex, false);\r
860             annotation.adjustForAlignment();\r
861             referedSeq.addAlignmentAnnotation(annotation);\r
862             al.addAnnotation(annotation);\r
863             al.setAnnotationIndex(annotation,\r
864                     al.getAlignmentAnnotation().length\r
865                             - existingAnnotations - 1);\r
866             if (groupRef != null)\r
867             {\r
868               ((Vector) groupRefRows.get(groupRef)).addElement(annotation);\r
869             }\r
870             // and recover our virgin copy to use again if necessary.\r
871             annotation = ann;\r
872 \r
873           } while (refSeqId != null\r
874                   && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);\r
875         }\r
876         else\r
877         {\r
878           al.addAnnotation(annotation);\r
879           al.setAnnotationIndex(annotation,\r
880                   al.getAlignmentAnnotation().length - existingAnnotations\r
881                           - 1);\r
882           if (groupRef != null)\r
883           {\r
884             ((Vector) groupRefRows.get(groupRef)).addElement(annotation);\r
885           }\r
886         }\r
887         // and set modification flag\r
888         modified = true;\r
889       }\r
890       // Finally, resolve the groupRefs\r
891       Enumeration en = groupRefRows.keys();\r
892       SequenceGroup theGroup = null;\r
893 \r
894       while (en.hasMoreElements())\r
895       {\r
896         groupRef = (String) en.nextElement();\r
897         boolean matched = false;\r
898         // Resolve group: TODO: add a getGroupByName method to alignments\r
899         Vector grps = al.getGroups();\r
900         for (int g = 0, gSize = grps.size(); g < gSize; g++)\r
901         {\r
902           theGroup = (SequenceGroup) grps.elementAt(g);\r
903           if (theGroup.getName().equals(groupRef))\r
904           {\r
905             if (matched)\r
906             {\r
907               // TODO: specify and implement duplication of alignment annotation\r
908               // for multiple group references.\r
909               System.err\r
910                       .println("Ignoring 1:many group reference mappings for group name '"\r
911                               + groupRef + "'");\r
912             }\r
913             else\r
914             {\r
915               matched = true;\r
916               Vector rowset = (Vector) groupRefRows.get(groupRef);\r
917               if (rowset != null && rowset.size() > 0)\r
918               {\r
919                 AlignmentAnnotation alan = null;\r
920                 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)\r
921                 {\r
922                   alan = (AlignmentAnnotation) rowset.elementAt(elm);\r
923                   alan.groupRef = theGroup;\r
924                 }\r
925               }\r
926             }\r
927           }\r
928         }\r
929         ((Vector) groupRefRows.get(groupRef)).removeAllElements();\r
930       }\r
931     }\r
932     return modified;\r
933   }\r
934 \r
935   private Object autoAnnotsKey(AlignmentAnnotation annotation,\r
936           SequenceI refSeq, String groupRef)\r
937   {\r
938     return annotation.graph + "\t" + annotation.label + "\t"\r
939             + annotation.description + "\t"\r
940             + (refSeq != null ? refSeq.getDisplayId(true) : "");\r
941   }\r
942 \r
943   Annotation parseAnnotation(String string, int graphStyle)\r
944   {\r
945     boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't\r
946     // do the\r
947     // glyph\r
948     // test\r
949     // if we\r
950     // don't\r
951     // want\r
952     // secondary\r
953     // structure\r
954     String desc = null, displayChar = null;\r
955     char ss = ' '; // secondaryStructure\r
956     float value = 0;\r
957     boolean parsedValue = false, dcset = false;\r
958 \r
959     // find colour here\r
960     java.awt.Color colour = null;\r
961     int i = string.indexOf("[");\r
962     int j = string.indexOf("]");\r
963     if (i > -1 && j > -1)\r
964     {\r
965       UserColourScheme ucs = new UserColourScheme();\r
966 \r
967       colour = ucs.getColourFromString(string.substring(i + 1, j));\r
968       if (i > 0 && string.charAt(i - 1) == ',')\r
969       {\r
970         // clip the preceding comma as well\r
971         i--;\r
972       }\r
973       string = string.substring(0, i) + string.substring(j + 1);\r
974     }\r
975 \r
976     StringTokenizer st = new StringTokenizer(string, ",", true);\r
977     String token;\r
978     boolean seenContent = false;\r
979     int pass = 0;\r
980     while (st.hasMoreTokens())\r
981     {\r
982       pass++;\r
983       token = st.nextToken().trim();\r
984       if (token.equals(","))\r
985       {\r
986         if (!seenContent && parsedValue && !dcset)\r
987         {\r
988           // allow the value below the bar/line to be empty\r
989           dcset = true;\r
990           displayChar = " ";\r
991         }\r
992         seenContent = false;\r
993         continue;\r
994       }\r
995       else\r
996       {\r
997         seenContent = true;\r
998       }\r
999 \r
1000       if (!parsedValue)\r
1001       {\r
1002         try\r
1003         {\r
1004           displayChar = token;\r
1005           // foo\r
1006           value = new Float(token).floatValue();\r
1007           parsedValue = true;\r
1008           continue;\r
1009         } catch (NumberFormatException ex)\r
1010         {\r
1011         }\r
1012       }\r
1013       else\r
1014       {\r
1015         if (token.length() == 1)\r
1016         {\r
1017           displayChar = token;\r
1018         }\r
1019       }\r
1020       if (hasSymbols\r
1021               && (token.equals("H") || token.equals("E") || token.equals("S") || token\r
1022                       .equals(" ")))\r
1023       {\r
1024         // Either this character represents a helix or sheet\r
1025         // or an integer which can be displayed\r
1026         ss = token.charAt(0);\r
1027         if (displayChar.equals(token.substring(0, 1)))\r
1028         {\r
1029           displayChar = "";\r
1030         }\r
1031       }\r
1032       else if (desc == null || (parsedValue && pass > 2))\r
1033       {\r
1034         desc = token;\r
1035       }\r
1036 \r
1037     }\r
1038     // if (!dcset && string.charAt(string.length() - 1) == ',')\r
1039     // {\r
1040     // displayChar = " "; // empty display char symbol.\r
1041     // }\r
1042     if (displayChar != null && desc != null && desc.length() == 1)\r
1043     {\r
1044       if (displayChar.length() > 1)\r
1045       {\r
1046         // switch desc and displayChar - legacy support\r
1047         String tmp = displayChar;\r
1048         displayChar = desc;\r
1049         desc = tmp;\r
1050       }\r
1051       else\r
1052       {\r
1053         if (displayChar.equals(desc))\r
1054         {\r
1055           // duplicate label - hangover from the 'robust parser' above\r
1056           desc = null;\r
1057         }\r
1058       }\r
1059     }\r
1060     Annotation anot = new Annotation(displayChar, desc, ss, value);\r
1061 \r
1062     anot.colour = colour;\r
1063 \r
1064     return anot;\r
1065   }\r
1066 \r
1067   void colourAnnotations(AlignmentI al, String label, String colour)\r
1068   {\r
1069     UserColourScheme ucs = new UserColourScheme(colour);\r
1070     Annotation[] annotations;\r
1071     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1072     {\r
1073       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
1074       {\r
1075         annotations = al.getAlignmentAnnotation()[i].annotations;\r
1076         for (int j = 0; j < annotations.length; j++)\r
1077         {\r
1078           if (annotations[j] != null)\r
1079           {\r
1080             annotations[j].colour = ucs.findColour('A');\r
1081           }\r
1082         }\r
1083       }\r
1084     }\r
1085   }\r
1086 \r
1087   void combineAnnotations(AlignmentI al, StringTokenizer st)\r
1088   {\r
1089     int graphGroup = -1;\r
1090     String group = st.nextToken();\r
1091     // First make sure we are not overwriting the graphIndex\r
1092     if (al.getAlignmentAnnotation() != null)\r
1093     {\r
1094       for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1095       {\r
1096         if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
1097         {\r
1098           graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;\r
1099           al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
1100           break;\r
1101         }\r
1102       }\r
1103 \r
1104       // Now update groups\r
1105       while (st.hasMoreTokens())\r
1106       {\r
1107         group = st.nextToken();\r
1108         for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1109         {\r
1110           if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
1111           {\r
1112             al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
1113             break;\r
1114           }\r
1115         }\r
1116       }\r
1117     }\r
1118     else\r
1119     {\r
1120       System.err\r
1121               .println("Couldn't combine annotations. None are added to alignment yet!");\r
1122     }\r
1123   }\r
1124 \r
1125   void addLine(AlignmentI al, StringTokenizer st)\r
1126   {\r
1127     String group = st.nextToken();\r
1128     AlignmentAnnotation annotation = null, alannot[] = al\r
1129             .getAlignmentAnnotation();\r
1130     if (alannot != null)\r
1131     {\r
1132       for (int i = 0; i < alannot.length; i++)\r
1133       {\r
1134         if (alannot[i].label.equalsIgnoreCase(group))\r
1135         {\r
1136           annotation = alannot[i];\r
1137           break;\r
1138         }\r
1139       }\r
1140     }\r
1141     if (annotation == null)\r
1142     {\r
1143       return;\r
1144     }\r
1145     float value = new Float(st.nextToken()).floatValue();\r
1146     String label = st.hasMoreTokens() ? st.nextToken() : null;\r
1147     java.awt.Color colour = null;\r
1148     if (st.hasMoreTokens())\r
1149     {\r
1150       UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
1151       colour = ucs.findColour('A');\r
1152     }\r
1153 \r
1154     annotation.setThreshold(new GraphLine(value, label, colour));\r
1155   }\r
1156 \r
1157   void addGroup(AlignmentI al, StringTokenizer st)\r
1158   {\r
1159     SequenceGroup sg = new SequenceGroup();\r
1160     sg.setName(st.nextToken());\r
1161     String rng = "";\r
1162     try\r
1163     {\r
1164       rng = st.nextToken();\r
1165       if (rng.length() > 0 && !rng.startsWith("*"))\r
1166       {\r
1167         sg.setStartRes(Integer.parseInt(rng) - 1);\r
1168       }\r
1169       else\r
1170       {\r
1171         sg.setStartRes(0);\r
1172       }\r
1173       rng = st.nextToken();\r
1174       if (rng.length() > 0 && !rng.startsWith("*"))\r
1175       {\r
1176         sg.setEndRes(Integer.parseInt(rng) - 1);\r
1177       }\r
1178       else\r
1179       {\r
1180         sg.setEndRes(al.getWidth() - 1);\r
1181       }\r
1182     } catch (Exception e)\r
1183     {\r
1184       System.err\r
1185               .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"\r
1186                       + rng + "' - assuming alignment width for group.");\r
1187       // assume group is full width\r
1188       sg.setStartRes(0);\r
1189       sg.setEndRes(al.getWidth() - 1);\r
1190     }\r
1191 \r
1192     String index = st.nextToken();\r
1193     if (index.equals("-1"))\r
1194     {\r
1195       while (st.hasMoreElements())\r
1196       {\r
1197         sg.addSequence(al.findName(st.nextToken()), false);\r
1198       }\r
1199     }\r
1200     else\r
1201     {\r
1202       StringTokenizer st2 = new StringTokenizer(index, ",");\r
1203 \r
1204       while (st2.hasMoreTokens())\r
1205       {\r
1206         String tmp = st2.nextToken();\r
1207         if (tmp.equals("*"))\r
1208         {\r
1209           for (int i = 0; i < al.getHeight(); i++)\r
1210           {\r
1211             sg.addSequence(al.getSequenceAt(i), false);\r
1212           }\r
1213         }\r
1214         else if (tmp.indexOf("-") >= 0)\r
1215         {\r
1216           StringTokenizer st3 = new StringTokenizer(tmp, "-");\r
1217 \r
1218           int start = (Integer.parseInt(st3.nextToken()));\r
1219           int end = (Integer.parseInt(st3.nextToken()));\r
1220 \r
1221           if (end > start)\r
1222           {\r
1223             for (int i = start; i <= end; i++)\r
1224             {\r
1225               sg.addSequence(al.getSequenceAt(i - 1), false);\r
1226             }\r
1227           }\r
1228         }\r
1229         else\r
1230         {\r
1231           sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);\r
1232         }\r
1233       }\r
1234     }\r
1235 \r
1236     if (refSeq != null)\r
1237     {\r
1238       sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);\r
1239       sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);\r
1240       sg.setSeqrep(refSeq);\r
1241     }\r
1242 \r
1243     if (sg.getSize() > 0)\r
1244     {\r
1245       al.addGroup(sg);\r
1246     }\r
1247   }\r
1248 \r
1249   void addRowProperties(AlignmentI al, StringTokenizer st)\r
1250   {\r
1251     String label = st.nextToken(), keyValue, key, value;\r
1252     boolean scaletofit = false, centerlab = false, showalllabs = false;\r
1253     while (st.hasMoreTokens())\r
1254     {\r
1255       keyValue = st.nextToken();\r
1256       key = keyValue.substring(0, keyValue.indexOf("="));\r
1257       value = keyValue.substring(keyValue.indexOf("=") + 1);\r
1258       if (key.equalsIgnoreCase("scaletofit"))\r
1259       {\r
1260         scaletofit = Boolean.valueOf(value).booleanValue();\r
1261       }\r
1262       if (key.equalsIgnoreCase("showalllabs"))\r
1263       {\r
1264         showalllabs = Boolean.valueOf(value).booleanValue();\r
1265       }\r
1266       if (key.equalsIgnoreCase("centrelabs"))\r
1267       {\r
1268         centerlab = Boolean.valueOf(value).booleanValue();\r
1269       }\r
1270       AlignmentAnnotation[] alr = al.getAlignmentAnnotation();\r
1271       if (alr != null)\r
1272       {\r
1273         for (int i = 0; i < alr.length; i++)\r
1274         {\r
1275           if (alr[i].label.equalsIgnoreCase(label))\r
1276           {\r
1277             alr[i].centreColLabels = centerlab;\r
1278             alr[i].scaleColLabel = scaletofit;\r
1279             alr[i].showAllColLabels = showalllabs;\r
1280           }\r
1281         }\r
1282       }\r
1283     }\r
1284   }\r
1285 \r
1286   void addProperties(AlignmentI al, StringTokenizer st)\r
1287   {\r
1288 \r
1289     // So far we have only added groups to the annotationHash,\r
1290     // the idea is in the future properties can be added to\r
1291     // alignments, other annotations etc\r
1292     if (al.getGroups() == null)\r
1293     {\r
1294       return;\r
1295     }\r
1296     SequenceGroup sg = null;\r
1297 \r
1298     String name = st.nextToken();\r
1299 \r
1300     Vector groups = al.getGroups();\r
1301     for (int i = 0; i < groups.size(); i++)\r
1302     {\r
1303       sg = (SequenceGroup) groups.elementAt(i);\r
1304       if (sg.getName().equals(name))\r
1305       {\r
1306         break;\r
1307       }\r
1308       else\r
1309       {\r
1310         sg = null;\r
1311       }\r
1312     }\r
1313 \r
1314     if (sg != null)\r
1315     {\r
1316       String keyValue, key, value;\r
1317       ColourSchemeI def = sg.cs;\r
1318       sg.cs = null;\r
1319       while (st.hasMoreTokens())\r
1320       {\r
1321         keyValue = st.nextToken();\r
1322         key = keyValue.substring(0, keyValue.indexOf("="));\r
1323         value = keyValue.substring(keyValue.indexOf("=") + 1);\r
1324 \r
1325         if (key.equalsIgnoreCase("description"))\r
1326         {\r
1327           sg.setDescription(value);\r
1328         }\r
1329         else if (key.equalsIgnoreCase("colour"))\r
1330         {\r
1331           sg.cs = ColourSchemeProperty.getColour(al, value);\r
1332         }\r
1333         else if (key.equalsIgnoreCase("pidThreshold"))\r
1334         {\r
1335           sg.cs.setThreshold(Integer.parseInt(value), true);\r
1336 \r
1337         }\r
1338         else if (key.equalsIgnoreCase("consThreshold"))\r
1339         {\r
1340           sg.cs.setConservationInc(Integer.parseInt(value));\r
1341           Conservation c = new Conservation("Group",\r
1342                   ResidueProperties.propHash, 3, sg.getSequences(null),\r
1343                   sg.getStartRes(), sg.getEndRes() + 1);\r
1344 \r
1345           c.calculate();\r
1346           c.verdict(false, 25);\r
1347 \r
1348           sg.cs.setConservation(c);\r
1349 \r
1350         }\r
1351         else if (key.equalsIgnoreCase("outlineColour"))\r
1352         {\r
1353           sg.setOutlineColour(new UserColourScheme(value).findColour('A'));\r
1354         }\r
1355         else if (key.equalsIgnoreCase("displayBoxes"))\r
1356         {\r
1357           sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());\r
1358         }\r
1359         else if (key.equalsIgnoreCase("showUnconserved"))\r
1360         {\r
1361           sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());\r
1362         }\r
1363         else if (key.equalsIgnoreCase("displayText"))\r
1364         {\r
1365           sg.setDisplayText(Boolean.valueOf(value).booleanValue());\r
1366         }\r
1367         else if (key.equalsIgnoreCase("colourText"))\r
1368         {\r
1369           sg.setColourText(Boolean.valueOf(value).booleanValue());\r
1370         }\r
1371         else if (key.equalsIgnoreCase("textCol1"))\r
1372         {\r
1373           sg.textColour = new UserColourScheme(value).findColour('A');\r
1374         }\r
1375         else if (key.equalsIgnoreCase("textCol2"))\r
1376         {\r
1377           sg.textColour2 = new UserColourScheme(value).findColour('A');\r
1378         }\r
1379         else if (key.equalsIgnoreCase("textColThreshold"))\r
1380         {\r
1381           sg.thresholdTextColour = Integer.parseInt(value);\r
1382         }\r
1383         else if (key.equalsIgnoreCase("idColour"))\r
1384         {\r
1385           // consider warning if colour doesn't resolve to a real colour\r
1386           sg.setIdColour((def = new UserColourScheme(value))\r
1387                   .findColour('A'));\r
1388         }\r
1389         else if (key.equalsIgnoreCase("hide"))\r
1390         {\r
1391           // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847\r
1392           sg.setHidereps(true);\r
1393         }\r
1394         else if (key.equalsIgnoreCase("hidecols"))\r
1395         {\r
1396           // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847\r
1397           sg.setHideCols(true);\r
1398         }\r
1399         sg.recalcConservation();\r
1400       }\r
1401       if (sg.cs == null)\r
1402       {\r
1403         sg.cs = def;\r
1404       }\r
1405     }\r
1406   }\r
1407 \r
1408   void setBelowAlignment(AlignmentI al, StringTokenizer st)\r
1409   {\r
1410     String token;\r
1411     AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();\r
1412     if (ala == null)\r
1413     {\r
1414       System.err\r
1415               .print("Warning - no annotation to set below for sequence associated annotation:");\r
1416     }\r
1417     while (st.hasMoreTokens())\r
1418     {\r
1419       token = st.nextToken();\r
1420       if (ala == null)\r
1421       {\r
1422         System.err.print(" " + token);\r
1423       }\r
1424       else\r
1425       {\r
1426         for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1427         {\r
1428           aa = al.getAlignmentAnnotation()[i];\r
1429           if (aa.sequenceRef == refSeq && aa.label.equals(token))\r
1430           {\r
1431             aa.belowAlignment = true;\r
1432           }\r
1433         }\r
1434       }\r
1435     }\r
1436     if (ala == null)\r
1437     {\r
1438       System.err.print("\n");\r
1439     }\r
1440   }\r
1441 \r
1442   void addAlignmentDetails(AlignmentI al, StringTokenizer st)\r
1443   {\r
1444     String keyValue, key, value;\r
1445     while (st.hasMoreTokens())\r
1446     {\r
1447       keyValue = st.nextToken();\r
1448       key = keyValue.substring(0, keyValue.indexOf("="));\r
1449       value = keyValue.substring(keyValue.indexOf("=") + 1);\r
1450       al.setProperty(key, value);\r
1451     }\r
1452   }\r
1453 \r
1454   /**\r
1455    * Write annotations as a CSV file of the form 'label, value, value, ...' for\r
1456    * each row.\r
1457    * \r
1458    * @param annotations\r
1459    * @return CSV file as a string.\r
1460    */\r
1461   public String printCSVAnnotations(AlignmentAnnotation[] annotations)\r
1462   {\r
1463     StringBuffer sp = new StringBuffer();\r
1464     for (int i = 0; i < annotations.length; i++)\r
1465     {\r
1466       String atos = annotations[i].toString();\r
1467       int p = 0;\r
1468       do\r
1469       {\r
1470         int cp = atos.indexOf("\n", p);\r
1471         sp.append(annotations[i].label);\r
1472         sp.append(",");\r
1473         if (cp > p)\r
1474         {\r
1475           sp.append(atos.substring(p, cp + 1));\r
1476         }\r
1477         else\r
1478         {\r
1479           sp.append(atos.substring(p));\r
1480           sp.append(newline);\r
1481         }\r
1482         p = cp + 1;\r
1483       } while (p > 0);\r
1484     }\r
1485     return sp.toString();\r
1486   }\r
1487 }\r