fixed deadlock
[jalview.git] / src / jalview / io / AnnotationFile.java
index 2053fef..e54ae71 100755 (executable)
@@ -35,13 +35,15 @@ public class AnnotationFile
       + new java.util.Date() + "\n\n");\r
 \r
   public String printAnnotations(AlignmentAnnotation[] annotations,\r
-                                 Vector groups)\r
+                                 Vector groups,\r
+                                 Hashtable properties)\r
   {\r
     if (annotations != null)\r
     {\r
+      boolean oneColour = true;\r
       AlignmentAnnotation row;\r
       String comma;\r
-      SequenceI seqref = null;\r
+      SequenceI refSeq = null;\r
 \r
       StringBuffer colours = new StringBuffer();\r
       StringBuffer graphLine = new StringBuffer();\r
@@ -60,21 +62,22 @@ public class AnnotationFile
         }\r
 \r
         color = null;\r
+        oneColour = true;\r
 \r
         if (row.sequenceRef == null)\r
         {\r
-          if (seqref != null)\r
+          if (refSeq != null)\r
           {\r
             text.append("\nSEQUENCE_REF\tALIGNMENT\n");\r
           }\r
 \r
-          seqref = null;\r
+          refSeq = null;\r
         }\r
 \r
-        else if (seqref == null || seqref != row.sequenceRef)\r
+        else if (refSeq == null || refSeq != row.sequenceRef)\r
         {\r
-          seqref = row.sequenceRef;\r
-          text.append("\nSEQUENCE_REF\t" + seqref.getName() + "\n");\r
+          refSeq = row.sequenceRef;\r
+          text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");\r
         }\r
 \r
         if (row.graph == AlignmentAnnotation.NO_GRAPH)\r
@@ -126,8 +129,8 @@ public class AnnotationFile
 \r
         for (int j = 0; j < row.annotations.length; j++)\r
         {\r
-          if (seqref != null &&\r
-              jalview.util.Comparison.isGap(seqref.getCharAt(j)))\r
+          if (refSeq != null &&\r
+              jalview.util.Comparison.isGap(refSeq.getCharAt(j)))\r
           {\r
             continue;\r
           }\r
@@ -140,7 +143,8 @@ public class AnnotationFile
               text.append(comma + row.annotations[j].secondaryStructure);\r
               comma = ",";\r
             }\r
-            if (row.annotations[j].displayCharacter.length() > 0\r
+            if (row.annotations[j].displayCharacter!=null\r
+                && row.annotations[j].displayCharacter.length() > 0\r
                 && !row.annotations[j].displayCharacter.equals(" "))\r
             {\r
               text.append(comma + row.annotations[j].displayCharacter);\r
@@ -149,19 +153,35 @@ public class AnnotationFile
 \r
             if (row.annotations[j] != null)\r
             {\r
+              if(color!=null && !color.equals(row.annotations[j].colour))\r
+              {\r
+                oneColour = false;\r
+              }\r
+\r
               color = row.annotations[j].colour;\r
               if (row.annotations[j].value != 0f)\r
               {\r
                 text.append(comma + row.annotations[j].value);\r
               }\r
             }\r
+\r
+            if(row.annotations[j].colour!=null\r
+               && row.annotations[j].colour!=java.awt.Color.black)\r
+            {\r
+              text.append(comma+"["+\r
+                          jalview.util.Format.getHexString(\r
+                          row.annotations[j].colour)+"]");\r
+            }\r
           }\r
           text.append("|");\r
         }\r
 \r
+        if(row.hasScore())\r
+          text.append("\t"+row.score);\r
+\r
         text.append("\n");\r
 \r
-        if (color != null && color != java.awt.Color.black)\r
+        if (color != null && color != java.awt.Color.black && oneColour)\r
         {\r
           colours.append("COLOUR\t"\r
                          + row.label + "\t"\r
@@ -190,6 +210,18 @@ public class AnnotationFile
       printGroups(groups);\r
     }\r
 \r
+    if(properties!=null)\r
+    {\r
+      text.append("\n\nALIGNMENT");\r
+      Enumeration en = properties.keys();\r
+      while(en.hasMoreElements())\r
+      {\r
+        String key = en.nextElement().toString();\r
+        text.append("\t"+key+"="+properties.get(key));\r
+      }\r
+\r
+    }\r
+\r
     return text.toString();\r
   }\r
 \r
@@ -367,7 +399,6 @@ public class AnnotationFile
           catch (Exception ex)\r
           {\r
             refSeqIndex = 1;\r
-            refSeq=null;\r
           }\r
 \r
           continue ;\r
@@ -385,43 +416,79 @@ public class AnnotationFile
           continue;\r
         }\r
 \r
-        graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
-        label = st.nextToken();\r
-\r
-        if (st.countTokens() > 1)\r
+        else if( token.equalsIgnoreCase("BELOW_ALIGNMENT"))\r
         {\r
-          description = st.nextToken();\r
+          setBelowAlignment(al, st);\r
+          continue;\r
         }\r
-        else\r
+        else if( token.equalsIgnoreCase("ALIGNMENT"))\r
         {\r
-          description = null;\r
+          addAlignmentDetails(al, st);\r
+          continue;\r
         }\r
 \r
-        line = st.nextToken();\r
+        graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
+        label = st.nextToken();\r
 \r
-        st = new StringTokenizer(line, "|", true);\r
-        annotations = new Annotation[alWidth];\r
 \r
         index = 0;\r
-        boolean emptyColumn = true;\r
+        annotations = new Annotation[alWidth];\r
+        description = null;\r
+        float score = Float.NaN;\r
 \r
-        while (st.hasMoreElements() && index < alWidth)\r
+        if(st.hasMoreTokens())\r
         {\r
-          token = st.nextToken().trim();\r
-          if (token.equals("|"))\r
+          line = st.nextToken();\r
+\r
+          if (line.indexOf("|") ==-1)\r
           {\r
-            if (emptyColumn)\r
-            {\r
-              index++;\r
-            }\r
+            description = line;\r
+            if (st.hasMoreTokens())\r
+              line = st.nextToken();\r
+          }\r
 \r
-            emptyColumn = true;\r
+          if(st.hasMoreTokens())\r
+          {\r
+            //This must be the score\r
+            score = Float.valueOf(st.nextToken()).floatValue();\r
           }\r
-          else\r
+\r
+          st = new StringTokenizer(line, "|", true);\r
+\r
+\r
+          boolean emptyColumn = true;\r
+          boolean onlyOneElement = (st.countTokens()==1);\r
+\r
+          while (st.hasMoreElements() && index < alWidth)\r
           {\r
-            annotations[index++] = parseAnnotation(token);\r
-            emptyColumn = false;\r
+            token = st.nextToken().trim();\r
+\r
+            if(onlyOneElement)\r
+            {\r
+              try\r
+              {\r
+                score = Float.valueOf(token).floatValue();\r
+                break;\r
+              }\r
+              catch(NumberFormatException ex){}\r
+            }\r
+\r
+            if (token.equals("|"))\r
+            {\r
+              if (emptyColumn)\r
+              {\r
+                index++;\r
+              }\r
+\r
+              emptyColumn = true;\r
+            }\r
+            else\r
+            {\r
+              annotations[index++] = parseAnnotation(token);\r
+              emptyColumn = false;\r
+            }\r
           }\r
+\r
         }\r
 \r
         annotation = new AlignmentAnnotation(label,\r
@@ -431,8 +498,11 @@ public class AnnotationFile
                                              0,\r
                                              graphStyle);\r
 \r
+        annotation.score = score;\r
+\r
         if (refSeq != null)\r
         {\r
+          annotation.belowAlignment=false;\r
           annotation.createSequenceMapping(refSeq, refSeqIndex, false);\r
           annotation.adjustForAlignment();\r
           refSeq.addAlignmentAnnotation(annotation);\r
@@ -457,10 +527,24 @@ public class AnnotationFile
 \r
   Annotation parseAnnotation(String string)\r
   {\r
-    String desc = null, displayChar = "";\r
+    String desc = null, displayChar = null;\r
     char ss = ' '; // secondaryStructure\r
     float value = 0;\r
     boolean parsedValue = false;\r
+\r
+    //find colour here\r
+    java.awt.Color colour = null;\r
+    int i=string.indexOf("[");\r
+    int j=string.indexOf("]");\r
+    if(i>-1 && j>-1)\r
+    {\r
+      UserColourScheme ucs = new UserColourScheme();\r
+\r
+      colour = ucs.getColourFromString(string.substring(i+1,j));\r
+\r
+      string = string.substring(0,i)+string.substring(j+1);\r
+    }\r
+\r
     StringTokenizer st = new StringTokenizer(string, ",");\r
     String token;\r
     while (st.hasMoreTokens())\r
@@ -501,19 +585,21 @@ public class AnnotationFile
 \r
     }\r
 \r
-    if (desc == null)\r
-    {\r
-      desc = value + "";\r
-    }\r
-\r
-    if (displayChar.length() > 1 && desc.length() == 1)\r
+    if (displayChar!=null\r
+        && displayChar.length() > 1\r
+        &&  desc!=null\r
+        && desc.length() == 1)\r
     {\r
       String tmp = displayChar;\r
       displayChar = desc;\r
       desc = tmp;\r
     }\r
 \r
-    return new Annotation(displayChar, desc, ss, value);\r
+    Annotation anot = new Annotation(displayChar, desc, ss, value);\r
+\r
+    anot.colour = colour;\r
+\r
+    return anot;\r
   }\r
 \r
   void colourAnnotations(AlignmentI al, String label, String colour)\r
@@ -647,6 +733,8 @@ public class AnnotationFile
       }\r
     }\r
 \r
+\r
+\r
     if (refSeq != null)\r
     {\r
       sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);\r
@@ -757,4 +845,34 @@ public class AnnotationFile
       }\r
     }\r
   }\r
+\r
+  void setBelowAlignment(AlignmentI al, StringTokenizer st)\r
+  {\r
+    String token;\r
+    AlignmentAnnotation aa;\r
+    while(st.hasMoreTokens())\r
+    {\r
+      token = st.nextToken();\r
+      for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
+      {\r
+        aa = al.getAlignmentAnnotation()[i];\r
+        if(aa.sequenceRef==refSeq && aa.label.equals(token))\r
+        {\r
+          aa.belowAlignment = true;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  void addAlignmentDetails(AlignmentI al, StringTokenizer st)\r
+  {\r
+    String keyValue, key, value;\r
+    while (st.hasMoreTokens())\r
+    {\r
+      keyValue = st.nextToken();\r
+      key = keyValue.substring(0, keyValue.indexOf("="));\r
+      value = keyValue.substring(keyValue.indexOf("=") + 1);\r
+      al.setProperty(key,value);\r
+    }\r
+  }\r
 }\r