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