group/annotation association
[jalview.git] / src / jalview / io / AnnotationFile.java
index aac48eb..01daf39 100755 (executable)
@@ -72,8 +72,11 @@ public class AnnotationFile
       this.hiddenRepSeqs = hiddenRepSeqs;\r
     }\r
   }\r
+\r
   /**\r
-   * Prepare an annotation file given a set of annotations, groups, alignment properties and views. \r
+   * Prepare an annotation file given a set of annotations, groups, alignment\r
+   * properties and views.\r
+   * \r
    * @param annotations\r
    * @param groups\r
    * @param properties\r
@@ -83,13 +86,15 @@ public class AnnotationFile
   public String printAnnotations(AlignmentAnnotation[] annotations,\r
           Vector groups, Hashtable properties, ViewDef[] views)\r
   {\r
-    // TODO: resolve views issue : annotationFile could contain visible region, or full data + hidden region specifications for a view.\r
+    // TODO: resolve views issue : annotationFile could contain visible region,\r
+    // or full data + hidden region specifications for a view.\r
     if (annotations != null)\r
     {\r
       boolean oneColour = true;\r
       AlignmentAnnotation row;\r
       String comma;\r
       SequenceI refSeq = null;\r
+      SequenceGroup refGroup = null;\r
 \r
       StringBuffer colours = new StringBuffer();\r
       StringBuffer graphLine = new StringBuffer();\r
@@ -120,11 +125,34 @@ public class AnnotationFile
           refSeq = null;\r
         }\r
 \r
-        else if (refSeq == null || refSeq != row.sequenceRef)\r
+        else\r
+        {\r
+          if (refSeq == null || refSeq != row.sequenceRef)\r
+          {\r
+            refSeq = row.sequenceRef;\r
+            text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");\r
+          }\r
+        }\r
+        // mark any group references for the row\r
+        if (row.groupRef == null)\r
+        {\r
+\r
+          if (refGroup != null)\r
+          {\r
+            text.append("\nGROUP_REF\tALIGNMENT\n");\r
+          }\r
+\r
+          refGroup = null;\r
+        }\r
+        else\r
         {\r
-          refSeq = row.sequenceRef;\r
-          text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");\r
+          if (refGroup == null || refGroup != row.groupRef)\r
+          {\r
+            refGroup = row.groupRef;\r
+            text.append("\nGROUP_REF\t" + refGroup.getName() + "\n");\r
+          }\r
         }\r
+\r
         boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;\r
         // lookahead to check what the annotation row object actually contains.\r
         for (int j = 0; row.annotations != null\r
@@ -138,8 +166,8 @@ public class AnnotationFile
                     .equals(" "));\r
             hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');\r
             hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't\r
-                                                                  // be\r
-                                                                  // rendered..\r
+            // be\r
+            // rendered..\r
             hasText |= (row.annotations[j].description != null && row.annotations[j].description\r
                     .length() > 0);\r
           }\r
@@ -210,7 +238,7 @@ public class AnnotationFile
           if (row.annotations[j] != null)\r
           {\r
             comma = "";\r
-            if (hasGlyphs) // could be also hasGlyphs || ... \r
+            if (hasGlyphs) // could be also hasGlyphs || ...\r
             {\r
 \r
               text.append(comma);\r
@@ -288,12 +316,13 @@ public class AnnotationFile
           colours.append("COLOUR\t" + row.label + "\t"\r
                   + jalview.util.Format.getHexString(color) + "\n");\r
         }\r
-        if (row.scaleColLabel || row.showAllColLabels || row.centreColLabels)\r
+        if (row.scaleColLabel || row.showAllColLabels\r
+                || row.centreColLabels)\r
         {\r
-          rowprops.append("ROWPROPERTIES\t"+row.label);\r
-          rowprops.append("\tscaletofit="+row.scaleColLabel);\r
-          rowprops.append("\tshowalllabs="+row.showAllColLabels);\r
-          rowprops.append("\tcentrelabs="+row.centreColLabels);\r
+          rowprops.append("ROWPROPERTIES\t" + row.label);\r
+          rowprops.append("\tscaletofit=" + row.scaleColLabel);\r
+          rowprops.append("\tshowalllabs=" + row.showAllColLabels);\r
+          rowprops.append("\tcentrelabs=" + row.centreColLabels);\r
           rowprops.append("\n");\r
         }\r
       }\r
@@ -433,7 +462,10 @@ public class AnnotationFile
   public boolean readAnnotationFile(AlignmentI al, String file,\r
           String protocol)\r
   {\r
-    Hashtable autoAnnots=new Hashtable();\r
+    String groupRef = null;\r
+    Hashtable groupRefRows = new Hashtable();\r
+\r
+    Hashtable autoAnnots = new Hashtable();\r
     try\r
     {\r
       BufferedReader in = null;\r
@@ -463,21 +495,28 @@ public class AnnotationFile
       int graphStyle, index;\r
       int refSeqIndex = 1;\r
       int existingAnnotations = 0;\r
-      // when true - will add new rows regardless of whether they are duplicate auto-annotation like consensus or conservation graphs\r
-      boolean overrideAutoAnnot=false; \r
+      // when true - will add new rows regardless of whether they are duplicate\r
+      // auto-annotation like consensus or conservation graphs\r
+      boolean overrideAutoAnnot = false;\r
       if (al.getAlignmentAnnotation() != null)\r
       {\r
         existingAnnotations = al.getAlignmentAnnotation().length;\r
-        if (existingAnnotations>0)\r
+        if (existingAnnotations > 0)\r
         {\r
           AlignmentAnnotation[] aa = al.getAlignmentAnnotation();\r
-          for (int aai=0;aai<aa.length;aai++)\r
+          for (int aai = 0; aai < aa.length; aai++)\r
           {\r
             if (aa[aai].autoCalculated)\r
             {\r
               // make a note of the name and description\r
-              autoAnnots.put(aa[aai].graph+"\t"+aa[aai].label+"\t"+aa[aai].description+"\t"+(aa[aai].sequenceRef!=null ? aa[aai].sequenceRef.getDisplayId(true) : "")\r
-                      ,new Integer(1));\r
+              autoAnnots.put(aa[aai].graph\r
+                      + "\t"\r
+                      + aa[aai].label\r
+                      + "\t"\r
+                      + aa[aai].description\r
+                      + "\t"\r
+                      + (aa[aai].sequenceRef != null ? aa[aai].sequenceRef\r
+                              .getDisplayId(true) : ""), new Integer(1));\r
             }\r
           }\r
         }\r
@@ -575,7 +614,28 @@ public class AnnotationFile
           }\r
           continue;\r
         }\r
-\r
+        else if (token.equalsIgnoreCase("GROUP_REF"))\r
+        {\r
+          // Group references could be forward or backwards, so they are\r
+          // resolved after the whole file is read in\r
+          groupRef = null;\r
+          if (st.hasMoreTokens())\r
+          {\r
+            groupRef = st.nextToken();\r
+            if (groupRef.length() < 1)\r
+            {\r
+              groupRef = null; // empty string\r
+            }\r
+            else\r
+            {\r
+              if (groupRefRows.get(groupRef) == null)\r
+              {\r
+                groupRefRows.put(groupRef, new Vector());\r
+              }\r
+            }\r
+          }\r
+          continue;\r
+        }\r
         else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))\r
         {\r
           addGroup(al, st);\r
@@ -599,6 +659,7 @@ public class AnnotationFile
           continue;\r
         }\r
 \r
+        // Parse out the annotation row\r
         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
         label = st.nextToken();\r
 \r
@@ -661,12 +722,21 @@ public class AnnotationFile
           }\r
 \r
         }\r
-        \r
+\r
         annotation = new AlignmentAnnotation(label, description,\r
                 (index == 0) ? null : annotations, 0, 0, graphStyle);\r
 \r
         annotation.score = score;\r
-        if (!overrideAutoAnnot && autoAnnots.containsKey(annotation.graph+"\t"+annotation.label+"\t"+annotation.description+"\t"+(refSeq!=null ? refSeq.getDisplayId(true) : "")))\r
+        if (!overrideAutoAnnot\r
+                && autoAnnots\r
+                        .containsKey(annotation.graph\r
+                                + "\t"\r
+                                + annotation.label\r
+                                + "\t"\r
+                                + annotation.description\r
+                                + "\t"\r
+                                + (refSeq != null ? refSeq\r
+                                        .getDisplayId(true) : "")))\r
         {\r
           // skip - we've already got an automatic annotation of this type.\r
           continue;\r
@@ -674,7 +744,7 @@ public class AnnotationFile
         // otherwise add it!\r
         if (refSeq != null)\r
         {\r
-          \r
+\r
           annotation.belowAlignment = false;\r
           // make a copy of refSeq so we can find other matches in the alignment\r
           SequenceI referedSeq = refSeq;\r
@@ -692,6 +762,10 @@ public class AnnotationFile
             al.setAnnotationIndex(annotation,\r
                     al.getAlignmentAnnotation().length\r
                             - existingAnnotations - 1);\r
+            if (groupRef != null)\r
+            {\r
+              ((Vector) groupRefRows.get(groupRef)).addElement(annotation);\r
+            }\r
             // and recover our virgin copy to use again if necessary.\r
             annotation = ann;\r
 \r
@@ -704,9 +778,52 @@ public class AnnotationFile
           al.setAnnotationIndex(annotation,\r
                   al.getAlignmentAnnotation().length - existingAnnotations\r
                           - 1);\r
+          if (groupRef != null)\r
+          {\r
+            ((Vector) groupRefRows.get(groupRef)).addElement(annotation);\r
+          }\r
         }\r
       }\r
+      // Finally, resolve the groupRefs\r
+      Enumeration en = groupRefRows.keys();\r
+      SequenceGroup theGroup = null;\r
 \r
+      while (en.hasMoreElements())\r
+      {\r
+        groupRef = (String) en.nextElement();\r
+        boolean matched = false;\r
+        // Resolve group: TODO: add a getGroupByName method to alignments\r
+        Vector grps = al.getGroups();\r
+        for (int g = 0, gSize = grps.size(); g < gSize; g++)\r
+        {\r
+          theGroup = (SequenceGroup) grps.elementAt(g);\r
+          if (theGroup.getName().equals(groupRef))\r
+          {\r
+            if (matched)\r
+            {\r
+              // TODO: specify and implement duplication of alignment annotation for multiple group references.\r
+              System.err\r
+                      .println("Ignoring 1:many group reference mappings for group name '"\r
+                              + groupRef+"'");\r
+            }\r
+            else\r
+            {\r
+              matched = true;\r
+              Vector rowset = (Vector) groupRefRows.get(groupRef);\r
+              if (rowset != null && rowset.size() > 0)\r
+              {\r
+                AlignmentAnnotation alan = null;\r
+                for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)\r
+                {\r
+                  alan = (AlignmentAnnotation) rowset.elementAt(elm);\r
+                  alan.groupRef = theGroup;\r
+                }\r
+              }\r
+            }\r
+          }\r
+        }\r
+        ((Vector) groupRefRows.get(groupRef)).removeAllElements();\r
+      }\r
     } catch (Exception ex)\r
     {\r
       ex.printStackTrace();\r
@@ -718,7 +835,15 @@ public class AnnotationFile
 \r
   Annotation parseAnnotation(String string, int graphStyle)\r
   {\r
-    boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't do the glyph test if we don't want secondary structure\r
+    boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't\r
+                                                                       // do the\r
+                                                                       // glyph\r
+                                                                       // test\r
+                                                                       // if we\r
+                                                                       // don't\r
+                                                                       // want\r
+                                                                       // secondary\r
+                                                                       // structure\r
     String desc = null, displayChar = null;\r
     char ss = ' '; // secondaryStructure\r
     float value = 0;\r
@@ -733,7 +858,7 @@ public class AnnotationFile
       UserColourScheme ucs = new UserColourScheme();\r
 \r
       colour = ucs.getColourFromString(string.substring(i + 1, j));\r
-      if ( i>0 && string.charAt(i-1)==',')\r
+      if (i > 0 && string.charAt(i - 1) == ',')\r
       {\r
         // clip the preceding comma as well\r
         i--;\r
@@ -743,8 +868,8 @@ public class AnnotationFile
 \r
     StringTokenizer st = new StringTokenizer(string, ",", true);\r
     String token;\r
-    boolean seenContent=false;\r
-    int pass=0;\r
+    boolean seenContent = false;\r
+    int pass = 0;\r
     while (st.hasMoreTokens())\r
     {\r
       pass++;\r
@@ -759,7 +884,9 @@ public class AnnotationFile
         }\r
         seenContent = false;\r
         continue;\r
-      } else {\r
+      }\r
+      else\r
+      {\r
         seenContent = true;\r
       }\r
 \r
@@ -767,7 +894,7 @@ public class AnnotationFile
       {\r
         try\r
         {\r
-          displayChar = token; \r
+          displayChar = token;\r
           // foo\r
           value = new Float(token).floatValue();\r
           parsedValue = true;\r
@@ -775,13 +902,17 @@ public class AnnotationFile
         } catch (NumberFormatException ex)\r
         {\r
         }\r
-      } else {\r
-        if (token.length()==1)\r
+      }\r
+      else\r
+      {\r
+        if (token.length() == 1)\r
         {\r
           displayChar = token;\r
         }\r
       }\r
-      if (hasSymbols && (token.equals("H") || token.equals("E") || token.equals(" ")))\r
+      if (hasSymbols\r
+              && (token.equals("H") || token.equals("E") || token\r
+                      .equals(" ")))\r
       {\r
         // Either this character represents a helix or sheet\r
         // or an integer which can be displayed\r
@@ -791,26 +922,29 @@ public class AnnotationFile
           displayChar = "";\r
         }\r
       }\r
-      else if (desc == null || (parsedValue && pass>2))\r
+      else if (desc == null || (parsedValue && pass > 2))\r
       {\r
         desc = token;\r
       }\r
 \r
     }\r
-//    if (!dcset && string.charAt(string.length() - 1) == ',')\r
-//    {\r
-//      displayChar = " "; // empty display char symbol.\r
-//    }\r
-    if (displayChar != null && desc!=null && desc.length()==1) \r
+    // if (!dcset && string.charAt(string.length() - 1) == ',')\r
+    // {\r
+    // displayChar = " "; // empty display char symbol.\r
+    // }\r
+    if (displayChar != null && desc != null && desc.length() == 1)\r
     {\r
       if (displayChar.length() > 1)\r
       {\r
-        // switch desc and displayChar - legacy support  \r
+        // switch desc and displayChar - legacy support\r
         String tmp = displayChar;\r
         displayChar = desc;\r
         desc = tmp;\r
-      } else {\r
-        if (displayChar.equals(desc)) {\r
+      }\r
+      else\r
+      {\r
+        if (displayChar.equals(desc))\r
+        {\r
           // duplicate label - hangover from the 'robust parser' above\r
           desc = null;\r
         }\r
@@ -999,33 +1133,38 @@ public class AnnotationFile
 \r
   void addRowProperties(AlignmentI al, StringTokenizer st)\r
   {\r
-    String label = st.nextToken(),keyValue,key,value;\r
-    boolean scaletofit=false,centerlab=false,showalllabs=false;\r
-    while (st.hasMoreTokens()) {\r
-      keyValue=st.nextToken();\r
+    String label = st.nextToken(), keyValue, key, value;\r
+    boolean scaletofit = false, centerlab = false, showalllabs = false;\r
+    while (st.hasMoreTokens())\r
+    {\r
+      keyValue = st.nextToken();\r
       key = keyValue.substring(0, keyValue.indexOf("="));\r
       value = keyValue.substring(keyValue.indexOf("=") + 1);\r
-      if (key.equalsIgnoreCase("scaletofit")) {\r
+      if (key.equalsIgnoreCase("scaletofit"))\r
+      {\r
         scaletofit = Boolean.valueOf(value).booleanValue();\r
       }\r
-        if (key.equalsIgnoreCase("showalllabs")) {\r
-          showalllabs = Boolean.valueOf(value).booleanValue();\r
-        }\r
-        if (key.equalsIgnoreCase("centrelabs")) {\r
-          centerlab = Boolean.valueOf(value).booleanValue();\r
-    }\r
-        AlignmentAnnotation[] alr = al.getAlignmentAnnotation(); \r
-        for (int i = 0; i < alr.length; i++)\r
+      if (key.equalsIgnoreCase("showalllabs"))\r
+      {\r
+        showalllabs = Boolean.valueOf(value).booleanValue();\r
+      }\r
+      if (key.equalsIgnoreCase("centrelabs"))\r
+      {\r
+        centerlab = Boolean.valueOf(value).booleanValue();\r
+      }\r
+      AlignmentAnnotation[] alr = al.getAlignmentAnnotation();\r
+      for (int i = 0; i < alr.length; i++)\r
+      {\r
+        if (alr[i].label.equalsIgnoreCase(label))\r
         {\r
-          if (alr[i].label.equalsIgnoreCase(label))\r
-          {\r
-            alr[i].centreColLabels = centerlab;\r
-            alr[i].scaleColLabel = scaletofit;\r
-            alr[i].showAllColLabels = showalllabs;\r
-          }\r
+          alr[i].centreColLabels = centerlab;\r
+          alr[i].scaleColLabel = scaletofit;\r
+          alr[i].showAllColLabels = showalllabs;\r
         }\r
+      }\r
     }\r
   }\r
+\r
   void addProperties(AlignmentI al, StringTokenizer st)\r
   {\r
 \r