b3b2111540fecc0a369da12f8d9dce01630bf5d1
[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 list\r
72    * @param properties\r
73    * @return feature file as a string.\r
74    */\r
75   public String printAnnotations(AlignmentAnnotation[] annotations,\r
76           List<SequenceGroup> list, Hashtable properties)\r
77   {\r
78     return printAnnotations(annotations, list, 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 list\r
114    * @param properties\r
115    * @param views\r
116    * @return annotation file\r
117    */\r
118   public String printAnnotations(AlignmentAnnotation[] annotations,\r
119           List<SequenceGroup> list, 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 (list != null)\r
397     {\r
398       printGroups(list);\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(List<SequenceGroup> list)\r
423   {\r
424     SequenceI seqrep = null;\r
425     for (SequenceGroup sg:list)\r
426     {\r
427       if (!sg.hasSeqrep())\r
428       {\r
429         text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"\r
430                 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)\r
431                 + "\t" + "-1\t");\r
432         seqrep = null;\r
433       }\r
434       else\r
435       {\r
436         seqrep = sg.getSeqrep();\r
437         text.append("SEQUENCE_REF\t");\r
438         text.append(seqrep.getName());\r
439         text.append(newline);\r
440         text.append("SEQUENCE_GROUP\t");\r
441         text.append(sg.getName());\r
442         text.append("\t");\r
443         text.append((seqrep.findPosition(sg.getStartRes())));\r
444         text.append("\t");\r
445         text.append((seqrep.findPosition(sg.getEndRes())));\r
446         text.append("\t");\r
447         text.append("-1\t");\r
448       }\r
449       for (int s = 0; s < sg.getSize(); s++)\r
450       {\r
451         text.append(sg.getSequenceAt(s).getName());\r
452         text.append("\t");\r
453       }\r
454       text.append(newline);\r
455       text.append("PROPERTIES\t");\r
456       text.append(sg.getName());\r
457       text.append("\t");\r
458 \r
459       if (sg.getDescription() != null)\r
460       {\r
461         text.append("description=");\r
462         text.append(sg.getDescription());\r
463         text.append("\t");\r
464       }\r
465       if (sg.cs != null)\r
466       {\r
467         text.append("colour=");\r
468         text.append(ColourSchemeProperty.getColourName(sg.cs));\r
469         text.append("\t");\r
470         if (sg.cs.getThreshold() != 0)\r
471         {\r
472           text.append("pidThreshold=");\r
473           text.append(sg.cs.getThreshold());\r
474         }\r
475         if (sg.cs.conservationApplied())\r
476         {\r
477           text.append("consThreshold=");\r
478           text.append(sg.cs.getConservationInc());\r
479           text.append("\t");\r
480         }\r
481       }\r
482       text.append("outlineColour=");\r
483       text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));\r
484       text.append("\t");\r
485 \r
486       text.append("displayBoxes=");\r
487       text.append(sg.getDisplayBoxes());\r
488       text.append("\t");\r
489       text.append("displayText=");\r
490       text.append(sg.getDisplayText());\r
491       text.append("\t");\r
492       text.append("colourText=");\r
493       text.append(sg.getColourText());\r
494       text.append("\t");\r
495       text.append("showUnconserved=");\r
496       text.append(sg.getShowNonconserved());\r
497       text.append("\t");\r
498       if (sg.textColour != java.awt.Color.black)\r
499       {\r
500         text.append("textCol1=");\r
501         text.append(jalview.util.Format.getHexString(sg.textColour));\r
502         text.append("\t");\r
503       }\r
504       if (sg.textColour2 != java.awt.Color.white)\r
505       {\r
506         text.append("textCol2=");\r
507         text.append(jalview.util.Format.getHexString(sg.textColour2));\r
508         text.append("\t");\r
509       }\r
510       if (sg.thresholdTextColour != 0)\r
511       {\r
512         text.append("textColThreshold=");\r
513         text.append(sg.thresholdTextColour);\r
514         text.append("\t");\r
515       }\r
516       if (sg.idColour != null)\r
517       {\r
518         text.append("idColour=");\r
519         text.append(jalview.util.Format.getHexString(sg.idColour));\r
520         text.append("\t");\r
521       }\r
522       if (sg.isHidereps())\r
523       {\r
524         text.append("hide=true\t");\r
525       }\r
526       if (sg.isHideCols())\r
527       {\r
528         text.append("hidecols=true\t");\r
529       }\r
530       if (seqrep != null)\r
531       {\r
532         // terminate the last line and clear the sequence ref for the group\r
533         text.append(newline);\r
534         text.append("SEQUENCE_REF");\r
535       }\r
536       text.append(newline);\r
537       text.append(newline);\r
538 \r
539     }\r
540   }\r
541 \r
542   SequenceI refSeq = null;\r
543 \r
544   String refSeqId = null;\r
545 \r
546   public boolean readAnnotationFile(AlignmentI al, String file,\r
547           String protocol)\r
548   {\r
549     BufferedReader in = null;\r
550     try\r
551     {\r
552       if (protocol.equals(AppletFormatAdapter.FILE))\r
553       {\r
554         in = new BufferedReader(new FileReader(file));\r
555       }\r
556       else if (protocol.equals(AppletFormatAdapter.URL))\r
557       {\r
558         URL url = new URL(file);\r
559         in = new BufferedReader(new InputStreamReader(url.openStream()));\r
560       }\r
561       else if (protocol.equals(AppletFormatAdapter.PASTE))\r
562       {\r
563         in = new BufferedReader(new StringReader(file));\r
564       }\r
565       else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))\r
566       {\r
567         java.io.InputStream is = getClass().getResourceAsStream("/" + file);\r
568         if (is != null)\r
569         {\r
570           in = new BufferedReader(new java.io.InputStreamReader(is));\r
571         }\r
572       }\r
573       if (in != null)\r
574       {\r
575         return parseAnnotationFrom(al, in);\r
576       }\r
577 \r
578     } catch (Exception ex)\r
579     {\r
580       ex.printStackTrace();\r
581       System.out.println("Problem reading annotation file: " + ex);\r
582       return false;\r
583     }\r
584     return false;\r
585   }\r
586 \r
587   public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)\r
588           throws Exception\r
589   {\r
590     boolean modified = false;\r
591     String groupRef = null;\r
592     Hashtable groupRefRows = new Hashtable();\r
593 \r
594     Hashtable autoAnnots = new Hashtable();\r
595     {\r
596       String line, label, description, token;\r
597       int graphStyle, index;\r
598       int refSeqIndex = 1;\r
599       int existingAnnotations = 0;\r
600       // when true - will add new rows regardless of whether they are duplicate\r
601       // auto-annotation like consensus or conservation graphs\r
602       boolean overrideAutoAnnot = false;\r
603       if (al.getAlignmentAnnotation() != null)\r
604       {\r
605         existingAnnotations = al.getAlignmentAnnotation().length;\r
606         if (existingAnnotations > 0)\r
607         {\r
608           AlignmentAnnotation[] aa = al.getAlignmentAnnotation();\r
609           for (int aai = 0; aai < aa.length; aai++)\r
610           {\r
611             if (aa[aai].autoCalculated)\r
612             {\r
613               // make a note of the name and description\r
614               autoAnnots.put(\r
615                       autoAnnotsKey(aa[aai], aa[aai].sequenceRef,\r
616                               (aa[aai].groupRef == null ? null\r
617                                       : aa[aai].groupRef.getName())),\r
618                       new Integer(1));\r
619             }\r
620           }\r
621         }\r
622       }\r
623 \r
624       int alWidth = al.getWidth();\r
625 \r
626       StringTokenizer st;\r
627       Annotation[] annotations;\r
628       AlignmentAnnotation annotation = null;\r
629 \r
630       // First confirm this is an Annotation file\r
631       boolean jvAnnotationFile = false;\r
632       while ((line = in.readLine()) != null)\r
633       {\r
634         if (line.indexOf("#") == 0)\r
635         {\r
636           continue;\r
637         }\r
638 \r
639         if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
640         {\r
641           jvAnnotationFile = true;\r
642           break;\r
643         }\r
644       }\r
645 \r
646       if (!jvAnnotationFile)\r
647       {\r
648         in.close();\r
649         return false;\r
650       }\r
651 \r
652       while ((line = in.readLine()) != null)\r
653       {\r
654         if (line.indexOf("#") == 0\r
655                 || line.indexOf("JALVIEW_ANNOTATION") > -1\r
656                 || line.length() == 0)\r
657         {\r
658           continue;\r
659         }\r
660 \r
661         st = new StringTokenizer(line, "\t");\r
662         token = st.nextToken();\r
663         if (token.equalsIgnoreCase("COLOUR"))\r
664         {\r
665           // TODO: use graduated colour def'n here too\r
666           colourAnnotations(al, st.nextToken(), st.nextToken());\r
667           modified = true;\r
668           continue;\r
669         }\r
670 \r
671         else if (token.equalsIgnoreCase("COMBINE"))\r
672         {\r
673           combineAnnotations(al, st);\r
674           modified = true;\r
675           continue;\r
676         }\r
677         else if (token.equalsIgnoreCase("ROWPROPERTIES"))\r
678         {\r
679           addRowProperties(al, st);\r
680           modified = true;\r
681           continue;\r
682         }\r
683         else if (token.equalsIgnoreCase("GRAPHLINE"))\r
684         {\r
685           addLine(al, st);\r
686           modified = true;\r
687           continue;\r
688         }\r
689 \r
690         else if (token.equalsIgnoreCase("SEQUENCE_REF"))\r
691         {\r
692           if (st.hasMoreTokens())\r
693           {\r
694             refSeq = al.findName(refSeqId = st.nextToken());\r
695             if (refSeq == null)\r
696             {\r
697               refSeqId = null;\r
698             }\r
699             try\r
700             {\r
701               refSeqIndex = Integer.parseInt(st.nextToken());\r
702               if (refSeqIndex < 1)\r
703               {\r
704                 refSeqIndex = 1;\r
705                 System.out\r
706                         .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");\r
707               }\r
708             } catch (Exception ex)\r
709             {\r
710               refSeqIndex = 1;\r
711             }\r
712           }\r
713           else\r
714           {\r
715             refSeq = null;\r
716             refSeqId = null;\r
717           }\r
718           continue;\r
719         }\r
720         else if (token.equalsIgnoreCase("GROUP_REF"))\r
721         {\r
722           // Group references could be forward or backwards, so they are\r
723           // resolved after the whole file is read in\r
724           groupRef = null;\r
725           if (st.hasMoreTokens())\r
726           {\r
727             groupRef = st.nextToken();\r
728             if (groupRef.length() < 1)\r
729             {\r
730               groupRef = null; // empty string\r
731             }\r
732             else\r
733             {\r
734               if (groupRefRows.get(groupRef) == null)\r
735               {\r
736                 groupRefRows.put(groupRef, new Vector());\r
737               }\r
738             }\r
739           }\r
740           continue;\r
741         }\r
742         else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))\r
743         {\r
744           addGroup(al, st);\r
745           continue;\r
746         }\r
747 \r
748         else if (token.equalsIgnoreCase("PROPERTIES"))\r
749         {\r
750           addProperties(al, st);\r
751           modified = true;\r
752           continue;\r
753         }\r
754 \r
755         else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))\r
756         {\r
757           setBelowAlignment(al, st);\r
758           modified = true;\r
759           continue;\r
760         }\r
761         else if (token.equalsIgnoreCase("ALIGNMENT"))\r
762         {\r
763           addAlignmentDetails(al, st);\r
764           modified = true;\r
765           continue;\r
766         }\r
767 \r
768         // Parse out the annotation row\r
769         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
770         label = st.nextToken();\r
771 \r
772         index = 0;\r
773         annotations = new Annotation[alWidth];\r
774         description = null;\r
775         float score = Float.NaN;\r
776 \r
777         if (st.hasMoreTokens())\r
778         {\r
779           line = st.nextToken();\r
780 \r
781           if (line.indexOf("|") == -1)\r
782           {\r
783             description = line;\r
784             if (st.hasMoreTokens())\r
785               line = st.nextToken();\r
786           }\r
787 \r
788           if (st.hasMoreTokens())\r
789           {\r
790             // This must be the score\r
791             score = Float.valueOf(st.nextToken()).floatValue();\r
792           }\r
793 \r
794           st = new StringTokenizer(line, "|", true);\r
795 \r
796           boolean emptyColumn = true;\r
797           boolean onlyOneElement = (st.countTokens() == 1);\r
798 \r
799           while (st.hasMoreElements() && index < alWidth)\r
800           {\r
801             token = st.nextToken().trim();\r
802 \r
803             if (onlyOneElement)\r
804             {\r
805               try\r
806               {\r
807                 score = Float.valueOf(token).floatValue();\r
808                 break;\r
809               } catch (NumberFormatException ex)\r
810               {\r
811               }\r
812             }\r
813 \r
814             if (token.equals("|"))\r
815             {\r
816               if (emptyColumn)\r
817               {\r
818                 index++;\r
819               }\r
820 \r
821               emptyColumn = true;\r
822             }\r
823             else\r
824             {\r
825               annotations[index++] = parseAnnotation(token, graphStyle);\r
826               emptyColumn = false;\r
827             }\r
828           }\r
829 \r
830         }\r
831 \r
832         annotation = new AlignmentAnnotation(label, description,\r
833                 (index == 0) ? null : annotations, 0, 0, graphStyle);\r
834 \r
835         annotation.score = score;\r
836         if (!overrideAutoAnnot\r
837                 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,\r
838                         groupRef)))\r
839         {\r
840           // skip - we've already got an automatic annotation of this type.\r
841           continue;\r
842         }\r
843         // otherwise add it!\r
844         if (refSeq != null)\r
845         {\r
846 \r
847           annotation.belowAlignment = false;\r
848           // make a copy of refSeq so we can find other matches in the alignment\r
849           SequenceI referedSeq = refSeq;\r
850           do\r
851           {\r
852             // copy before we do any mapping business.\r
853             // TODO: verify that undo/redo with 1:many sequence associated\r
854             // annotations can be undone correctly\r
855             AlignmentAnnotation ann = new AlignmentAnnotation(annotation);\r
856             annotation\r
857                     .createSequenceMapping(referedSeq, refSeqIndex, false);\r
858             annotation.adjustForAlignment();\r
859             referedSeq.addAlignmentAnnotation(annotation);\r
860             al.addAnnotation(annotation);\r
861             al.setAnnotationIndex(annotation,\r
862                     al.getAlignmentAnnotation().length\r
863                             - existingAnnotations - 1);\r
864             if (groupRef != null)\r
865             {\r
866               ((Vector) groupRefRows.get(groupRef)).addElement(annotation);\r
867             }\r
868             // and recover our virgin copy to use again if necessary.\r
869             annotation = ann;\r
870 \r
871           } while (refSeqId != null\r
872                   && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);\r
873         }\r
874         else\r
875         {\r
876           al.addAnnotation(annotation);\r
877           al.setAnnotationIndex(annotation,\r
878                   al.getAlignmentAnnotation().length - existingAnnotations\r
879                           - 1);\r
880           if (groupRef != null)\r
881           {\r
882             ((Vector) groupRefRows.get(groupRef)).addElement(annotation);\r
883           }\r
884         }\r
885         // and set modification flag\r
886         modified = true;\r
887       }\r
888       // Finally, resolve the groupRefs\r
889       Enumeration en = groupRefRows.keys();\r
890       \r
891       while (en.hasMoreElements())\r
892       {\r
893         groupRef = (String) en.nextElement();\r
894         boolean matched = false;\r
895         // Resolve group: TODO: add a getGroupByName method to alignments\r
896         for (SequenceGroup theGroup : al.getGroups())\r
897         {\r
898           if (theGroup.getName().equals(groupRef))\r
899           {\r
900             if (matched)\r
901             {\r
902               // TODO: specify and implement duplication of alignment annotation\r
903               // for multiple group references.\r
904               System.err\r
905                       .println("Ignoring 1:many group reference mappings for group name '"\r
906                               + groupRef + "'");\r
907             }\r
908             else\r
909             {\r
910               matched = true;\r
911               Vector rowset = (Vector) groupRefRows.get(groupRef);\r
912               if (rowset != null && rowset.size() > 0)\r
913               {\r
914                 AlignmentAnnotation alan = null;\r
915                 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)\r
916                 {\r
917                   alan = (AlignmentAnnotation) rowset.elementAt(elm);\r
918                   alan.groupRef = theGroup;\r
919                 }\r
920               }\r
921             }\r
922           }\r
923         }\r
924         ((Vector) groupRefRows.get(groupRef)).removeAllElements();\r
925       }\r
926     }\r
927     return modified;\r
928   }\r
929 \r
930   private Object autoAnnotsKey(AlignmentAnnotation annotation,\r
931           SequenceI refSeq, String groupRef)\r
932   {\r
933     return annotation.graph + "\t" + annotation.label + "\t"\r
934             + annotation.description + "\t"\r
935             + (refSeq != null ? refSeq.getDisplayId(true) : "");\r
936   }\r
937 \r
938   Annotation parseAnnotation(String string, int graphStyle)\r
939   {\r
940     boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't\r
941     // do the\r
942     // glyph\r
943     // test\r
944     // if we\r
945     // don't\r
946     // want\r
947     // secondary\r
948     // structure\r
949     String desc = null, displayChar = null;\r
950     char ss = ' '; // secondaryStructure\r
951     float value = 0;\r
952     boolean parsedValue = false, dcset = false;\r
953 \r
954     // find colour here\r
955     java.awt.Color colour = null;\r
956     int i = string.indexOf("[");\r
957     int j = string.indexOf("]");\r
958     if (i > -1 && j > -1)\r
959     {\r
960       UserColourScheme ucs = new UserColourScheme();\r
961 \r
962       colour = ucs.getColourFromString(string.substring(i + 1, j));\r
963       if (i > 0 && string.charAt(i - 1) == ',')\r
964       {\r
965         // clip the preceding comma as well\r
966         i--;\r
967       }\r
968       string = string.substring(0, i) + string.substring(j + 1);\r
969     }\r
970 \r
971     StringTokenizer st = new StringTokenizer(string, ",", true);\r
972     String token;\r
973     boolean seenContent = false;\r
974     int pass = 0;\r
975     while (st.hasMoreTokens())\r
976     {\r
977       pass++;\r
978       token = st.nextToken().trim();\r
979       if (token.equals(","))\r
980       {\r
981         if (!seenContent && parsedValue && !dcset)\r
982         {\r
983           // allow the value below the bar/line to be empty\r
984           dcset = true;\r
985           displayChar = " ";\r
986         }\r
987         seenContent = false;\r
988         continue;\r
989       }\r
990       else\r
991       {\r
992         seenContent = true;\r
993       }\r
994 \r
995       if (!parsedValue)\r
996       {\r
997         try\r
998         {\r
999           displayChar = token;\r
1000           // foo\r
1001           value = new Float(token).floatValue();\r
1002           parsedValue = true;\r
1003           continue;\r
1004         } catch (NumberFormatException ex)\r
1005         {\r
1006         }\r
1007       }\r
1008       else\r
1009       {\r
1010         if (token.length() == 1)\r
1011         {\r
1012           displayChar = token;\r
1013         }\r
1014       }\r
1015       if (hasSymbols\r
1016               && (token.equals("H") || token.equals("E") || token.equals("S") || token\r
1017                       .equals(" ")))\r
1018       {\r
1019         // Either this character represents a helix or sheet\r
1020         // or an integer which can be displayed\r
1021         ss = token.charAt(0);\r
1022         if (displayChar.equals(token.substring(0, 1)))\r
1023         {\r
1024           displayChar = "";\r
1025         }\r
1026       }\r
1027       else if (desc == null || (parsedValue && pass > 2))\r
1028       {\r
1029         desc = token;\r
1030       }\r
1031 \r
1032     }\r
1033     // if (!dcset && string.charAt(string.length() - 1) == ',')\r
1034     // {\r
1035     // displayChar = " "; // empty display char symbol.\r
1036     // }\r
1037     if (displayChar != null && desc != null && desc.length() == 1)\r
1038     {\r
1039       if (displayChar.length() > 1)\r
1040       {\r
1041         // switch desc and displayChar - legacy support\r
1042         String tmp = displayChar;\r
1043         displayChar = desc;\r
1044         desc = tmp;\r
1045       }\r
1046       else\r
1047       {\r
1048         if (displayChar.equals(desc))\r
1049         {\r
1050           // duplicate label - hangover from the 'robust parser' above\r
1051           desc = null;\r
1052         }\r
1053       }\r
1054     }\r
1055     Annotation anot = new Annotation(displayChar, desc, ss, value);\r
1056 \r
1057     anot.colour = colour;\r
1058 \r
1059     return anot;\r
1060   }\r
1061 \r
1062   void colourAnnotations(AlignmentI al, String label, String colour)\r
1063   {\r
1064     UserColourScheme ucs = new UserColourScheme(colour);\r
1065     Annotation[] annotations;\r
1066     for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1067     {\r
1068       if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
1069       {\r
1070         annotations = al.getAlignmentAnnotation()[i].annotations;\r
1071         for (int j = 0; j < annotations.length; j++)\r
1072         {\r
1073           if (annotations[j] != null)\r
1074           {\r
1075             annotations[j].colour = ucs.findColour('A');\r
1076           }\r
1077         }\r
1078       }\r
1079     }\r
1080   }\r
1081 \r
1082   void combineAnnotations(AlignmentI al, StringTokenizer st)\r
1083   {\r
1084     int graphGroup = -1;\r
1085     String group = st.nextToken();\r
1086     // First make sure we are not overwriting the graphIndex\r
1087     if (al.getAlignmentAnnotation() != null)\r
1088     {\r
1089       for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1090       {\r
1091         if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
1092         {\r
1093           graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;\r
1094           al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
1095           break;\r
1096         }\r
1097       }\r
1098 \r
1099       // Now update groups\r
1100       while (st.hasMoreTokens())\r
1101       {\r
1102         group = st.nextToken();\r
1103         for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1104         {\r
1105           if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
1106           {\r
1107             al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
1108             break;\r
1109           }\r
1110         }\r
1111       }\r
1112     }\r
1113     else\r
1114     {\r
1115       System.err\r
1116               .println("Couldn't combine annotations. None are added to alignment yet!");\r
1117     }\r
1118   }\r
1119 \r
1120   void addLine(AlignmentI al, StringTokenizer st)\r
1121   {\r
1122     String group = st.nextToken();\r
1123     AlignmentAnnotation annotation = null, alannot[] = al\r
1124             .getAlignmentAnnotation();\r
1125     if (alannot != null)\r
1126     {\r
1127       for (int i = 0; i < alannot.length; i++)\r
1128       {\r
1129         if (alannot[i].label.equalsIgnoreCase(group))\r
1130         {\r
1131           annotation = alannot[i];\r
1132           break;\r
1133         }\r
1134       }\r
1135     }\r
1136     if (annotation == null)\r
1137     {\r
1138       return;\r
1139     }\r
1140     float value = new Float(st.nextToken()).floatValue();\r
1141     String label = st.hasMoreTokens() ? st.nextToken() : null;\r
1142     java.awt.Color colour = null;\r
1143     if (st.hasMoreTokens())\r
1144     {\r
1145       UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
1146       colour = ucs.findColour('A');\r
1147     }\r
1148 \r
1149     annotation.setThreshold(new GraphLine(value, label, colour));\r
1150   }\r
1151 \r
1152   void addGroup(AlignmentI al, StringTokenizer st)\r
1153   {\r
1154     SequenceGroup sg = new SequenceGroup();\r
1155     sg.setName(st.nextToken());\r
1156     String rng = "";\r
1157     try\r
1158     {\r
1159       rng = st.nextToken();\r
1160       if (rng.length() > 0 && !rng.startsWith("*"))\r
1161       {\r
1162         sg.setStartRes(Integer.parseInt(rng) - 1);\r
1163       }\r
1164       else\r
1165       {\r
1166         sg.setStartRes(0);\r
1167       }\r
1168       rng = st.nextToken();\r
1169       if (rng.length() > 0 && !rng.startsWith("*"))\r
1170       {\r
1171         sg.setEndRes(Integer.parseInt(rng) - 1);\r
1172       }\r
1173       else\r
1174       {\r
1175         sg.setEndRes(al.getWidth() - 1);\r
1176       }\r
1177     } catch (Exception e)\r
1178     {\r
1179       System.err\r
1180               .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"\r
1181                       + rng + "' - assuming alignment width for group.");\r
1182       // assume group is full width\r
1183       sg.setStartRes(0);\r
1184       sg.setEndRes(al.getWidth() - 1);\r
1185     }\r
1186 \r
1187     String index = st.nextToken();\r
1188     if (index.equals("-1"))\r
1189     {\r
1190       while (st.hasMoreElements())\r
1191       {\r
1192         sg.addSequence(al.findName(st.nextToken()), false);\r
1193       }\r
1194     }\r
1195     else\r
1196     {\r
1197       StringTokenizer st2 = new StringTokenizer(index, ",");\r
1198 \r
1199       while (st2.hasMoreTokens())\r
1200       {\r
1201         String tmp = st2.nextToken();\r
1202         if (tmp.equals("*"))\r
1203         {\r
1204           for (int i = 0; i < al.getHeight(); i++)\r
1205           {\r
1206             sg.addSequence(al.getSequenceAt(i), false);\r
1207           }\r
1208         }\r
1209         else if (tmp.indexOf("-") >= 0)\r
1210         {\r
1211           StringTokenizer st3 = new StringTokenizer(tmp, "-");\r
1212 \r
1213           int start = (Integer.parseInt(st3.nextToken()));\r
1214           int end = (Integer.parseInt(st3.nextToken()));\r
1215 \r
1216           if (end > start)\r
1217           {\r
1218             for (int i = start; i <= end; i++)\r
1219             {\r
1220               sg.addSequence(al.getSequenceAt(i - 1), false);\r
1221             }\r
1222           }\r
1223         }\r
1224         else\r
1225         {\r
1226           sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);\r
1227         }\r
1228       }\r
1229     }\r
1230 \r
1231     if (refSeq != null)\r
1232     {\r
1233       sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);\r
1234       sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);\r
1235       sg.setSeqrep(refSeq);\r
1236     }\r
1237 \r
1238     if (sg.getSize() > 0)\r
1239     {\r
1240       al.addGroup(sg);\r
1241     }\r
1242   }\r
1243 \r
1244   void addRowProperties(AlignmentI al, StringTokenizer st)\r
1245   {\r
1246     String label = st.nextToken(), keyValue, key, value;\r
1247     boolean scaletofit = false, centerlab = false, showalllabs = false;\r
1248     while (st.hasMoreTokens())\r
1249     {\r
1250       keyValue = st.nextToken();\r
1251       key = keyValue.substring(0, keyValue.indexOf("="));\r
1252       value = keyValue.substring(keyValue.indexOf("=") + 1);\r
1253       if (key.equalsIgnoreCase("scaletofit"))\r
1254       {\r
1255         scaletofit = Boolean.valueOf(value).booleanValue();\r
1256       }\r
1257       if (key.equalsIgnoreCase("showalllabs"))\r
1258       {\r
1259         showalllabs = Boolean.valueOf(value).booleanValue();\r
1260       }\r
1261       if (key.equalsIgnoreCase("centrelabs"))\r
1262       {\r
1263         centerlab = Boolean.valueOf(value).booleanValue();\r
1264       }\r
1265       AlignmentAnnotation[] alr = al.getAlignmentAnnotation();\r
1266       if (alr != null)\r
1267       {\r
1268         for (int i = 0; i < alr.length; i++)\r
1269         {\r
1270           if (alr[i].label.equalsIgnoreCase(label))\r
1271           {\r
1272             alr[i].centreColLabels = centerlab;\r
1273             alr[i].scaleColLabel = scaletofit;\r
1274             alr[i].showAllColLabels = showalllabs;\r
1275           }\r
1276         }\r
1277       }\r
1278     }\r
1279   }\r
1280 \r
1281   void addProperties(AlignmentI al, StringTokenizer st)\r
1282   {\r
1283 \r
1284     // So far we have only added groups to the annotationHash,\r
1285     // the idea is in the future properties can be added to\r
1286     // alignments, other annotations etc\r
1287     if (al.getGroups() == null)\r
1288     {\r
1289       return;\r
1290     }\r
1291     \r
1292     String name = st.nextToken();\r
1293     SequenceGroup sg=null;\r
1294     for (SequenceGroup _sg:al.getGroups())\r
1295     {\r
1296       if ((sg=_sg).getName().equals(name))\r
1297       {\r
1298         break;\r
1299       }\r
1300       else\r
1301       {\r
1302         sg = null;\r
1303       }\r
1304     }\r
1305 \r
1306     if (sg != null)\r
1307     {\r
1308       String keyValue, key, value;\r
1309       ColourSchemeI def = sg.cs;\r
1310       sg.cs = null;\r
1311       while (st.hasMoreTokens())\r
1312       {\r
1313         keyValue = st.nextToken();\r
1314         key = keyValue.substring(0, keyValue.indexOf("="));\r
1315         value = keyValue.substring(keyValue.indexOf("=") + 1);\r
1316 \r
1317         if (key.equalsIgnoreCase("description"))\r
1318         {\r
1319           sg.setDescription(value);\r
1320         }\r
1321         else if (key.equalsIgnoreCase("colour"))\r
1322         {\r
1323           sg.cs = ColourSchemeProperty.getColour(al, value);\r
1324         }\r
1325         else if (key.equalsIgnoreCase("pidThreshold"))\r
1326         {\r
1327           sg.cs.setThreshold(Integer.parseInt(value), true);\r
1328 \r
1329         }\r
1330         else if (key.equalsIgnoreCase("consThreshold"))\r
1331         {\r
1332           sg.cs.setConservationInc(Integer.parseInt(value));\r
1333           Conservation c = new Conservation("Group",\r
1334                   ResidueProperties.propHash, 3, sg.getSequences(null),\r
1335                   sg.getStartRes(), sg.getEndRes() + 1);\r
1336 \r
1337           c.calculate();\r
1338           c.verdict(false, 25); // TODO: refer to conservation percent threshold\r
1339 \r
1340           sg.cs.setConservation(c);\r
1341 \r
1342         }\r
1343         else if (key.equalsIgnoreCase("outlineColour"))\r
1344         {\r
1345           sg.setOutlineColour(new UserColourScheme(value).findColour('A'));\r
1346         }\r
1347         else if (key.equalsIgnoreCase("displayBoxes"))\r
1348         {\r
1349           sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());\r
1350         }\r
1351         else if (key.equalsIgnoreCase("showUnconserved"))\r
1352         {\r
1353           sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());\r
1354         }\r
1355         else if (key.equalsIgnoreCase("displayText"))\r
1356         {\r
1357           sg.setDisplayText(Boolean.valueOf(value).booleanValue());\r
1358         }\r
1359         else if (key.equalsIgnoreCase("colourText"))\r
1360         {\r
1361           sg.setColourText(Boolean.valueOf(value).booleanValue());\r
1362         }\r
1363         else if (key.equalsIgnoreCase("textCol1"))\r
1364         {\r
1365           sg.textColour = new UserColourScheme(value).findColour('A');\r
1366         }\r
1367         else if (key.equalsIgnoreCase("textCol2"))\r
1368         {\r
1369           sg.textColour2 = new UserColourScheme(value).findColour('A');\r
1370         }\r
1371         else if (key.equalsIgnoreCase("textColThreshold"))\r
1372         {\r
1373           sg.thresholdTextColour = Integer.parseInt(value);\r
1374         }\r
1375         else if (key.equalsIgnoreCase("idColour"))\r
1376         {\r
1377           // consider warning if colour doesn't resolve to a real colour\r
1378           sg.setIdColour((def = new UserColourScheme(value))\r
1379                   .findColour('A'));\r
1380         }\r
1381         else if (key.equalsIgnoreCase("hide"))\r
1382         {\r
1383           // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847\r
1384           sg.setHidereps(true);\r
1385         }\r
1386         else if (key.equalsIgnoreCase("hidecols"))\r
1387         {\r
1388           // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847\r
1389           sg.setHideCols(true);\r
1390         }\r
1391         sg.recalcConservation();\r
1392       }\r
1393       if (sg.cs == null)\r
1394       {\r
1395         sg.cs = def;\r
1396       }\r
1397     }\r
1398   }\r
1399 \r
1400   void setBelowAlignment(AlignmentI al, StringTokenizer st)\r
1401   {\r
1402     String token;\r
1403     AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();\r
1404     if (ala == null)\r
1405     {\r
1406       System.err\r
1407               .print("Warning - no annotation to set below for sequence associated annotation:");\r
1408     }\r
1409     while (st.hasMoreTokens())\r
1410     {\r
1411       token = st.nextToken();\r
1412       if (ala == null)\r
1413       {\r
1414         System.err.print(" " + token);\r
1415       }\r
1416       else\r
1417       {\r
1418         for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
1419         {\r
1420           aa = al.getAlignmentAnnotation()[i];\r
1421           if (aa.sequenceRef == refSeq && aa.label.equals(token))\r
1422           {\r
1423             aa.belowAlignment = true;\r
1424           }\r
1425         }\r
1426       }\r
1427     }\r
1428     if (ala == null)\r
1429     {\r
1430       System.err.print("\n");\r
1431     }\r
1432   }\r
1433 \r
1434   void addAlignmentDetails(AlignmentI al, StringTokenizer st)\r
1435   {\r
1436     String keyValue, key, value;\r
1437     while (st.hasMoreTokens())\r
1438     {\r
1439       keyValue = st.nextToken();\r
1440       key = keyValue.substring(0, keyValue.indexOf("="));\r
1441       value = keyValue.substring(keyValue.indexOf("=") + 1);\r
1442       al.setProperty(key, value);\r
1443     }\r
1444   }\r
1445 \r
1446   /**\r
1447    * Write annotations as a CSV file of the form 'label, value, value, ...' for\r
1448    * each row.\r
1449    * \r
1450    * @param annotations\r
1451    * @return CSV file as a string.\r
1452    */\r
1453   public String printCSVAnnotations(AlignmentAnnotation[] annotations)\r
1454   {\r
1455     StringBuffer sp = new StringBuffer();\r
1456     for (int i = 0; i < annotations.length; i++)\r
1457     {\r
1458       String atos = annotations[i].toString();\r
1459       int p = 0;\r
1460       do\r
1461       {\r
1462         int cp = atos.indexOf("\n", p);\r
1463         sp.append(annotations[i].label);\r
1464         sp.append(",");\r
1465         if (cp > p)\r
1466         {\r
1467           sp.append(atos.substring(p, cp + 1));\r
1468         }\r
1469         else\r
1470         {\r
1471           sp.append(atos.substring(p));\r
1472           sp.append(newline);\r
1473         }\r
1474         p = cp + 1;\r
1475       } while (p > 0);\r
1476     }\r
1477     return sp.toString();\r
1478   }\r
1479 }\r