JAL-1136 sometimes there are no parameters associated with a CalcId key
[jalview.git] / src / jalview / gui / Jalview2XML.java
index d9aca36..ee67b55 100644 (file)
@@ -1236,12 +1236,18 @@ public class Jalview2XML
         }
       }
     }
-    if (calcIdSet.size()>0)
+    if (calcIdSet.size() > 0)
     {
-      for (String calcId:calcIdSet)
+      for (String calcId : calcIdSet)
       {
-        if (calcId.trim().length()>0) {
-          view.addCalcIdParam(createCalcIdParam(calcId, av));
+        if (calcId.trim().length() > 0)
+        {
+          CalcIdParam cidp = createCalcIdParam(calcId, av);
+          // Some calcIds have no parameters.
+          if (cidp != null)
+          {
+            view.addCalcIdParam(cidp);
+          }
         }
       }
     }
@@ -1306,7 +1312,7 @@ public class Jalview2XML
       // recreate settings from preset 3) predefined settings provided by
       // service - or settings that can be transferred (or discarded)
       vCalcIdParam.setParameters(settings
-              .getWsParamFile());
+              .getWsParamFile().replace("\n", "|\\n|"));
       vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
       // todo - decide if updateImmediately is needed for any projects.
 
@@ -1325,7 +1331,7 @@ public class Jalview2XML
       {
         WsParamSetI parmSet=null;
         try {
-          service.getParamStore().parseServiceParameterFile(calcIdParam.getName(), calcIdParam.getDescription(), calcIdParam.getServiceURL(), calcIdParam.getParameters());
+          parmSet = service.getParamStore().parseServiceParameterFile(calcIdParam.getName(), calcIdParam.getDescription(), calcIdParam.getServiceURL(), calcIdParam.getParameters().replace("|\\n|", "\n"));
         } catch (IOException x)
         {
           warn("Couldn't parse parameter data for "+calcIdParam.getCalcId(), x);
@@ -1345,15 +1351,14 @@ public class Jalview2XML
         }
         AAConsSettings settings = new AAConsSettings(calcIdParam.isAutoUpdate(), service, parmSet, argList);
         av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings, calcIdParam.isNeedsUpdate());
+        return true;
       } else {
         warn("Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
         return false;
       }
     }
-    else
-      throw new Error("Unsupported Version for calcIdparam "
-              + calcIdParam.toString());
-    return false;
+    throw new Error("Unsupported Version for calcIdparam "
+            + calcIdParam.toString());
   }
   /**
    * External mapping between jalview objects and objects yielding a valid and
@@ -2307,24 +2312,20 @@ public class Jalview2XML
           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
                   an[i].getDescription(), anot);
         }
-        if (autoForView)
+        // register new annotation
+        if (an[i].getId() != null)
         {
-          // register new annotation
-          if (an[i].getId() != null)
-          {
-            annotationIds.put(an[i].getId(), jaa);
-            jaa.annotationId = an[i].getId();
-          }
-          // recover sequence association
-          if (an[i].getSequenceRef() != null)
+          annotationIds.put(an[i].getId(), jaa);
+          jaa.annotationId = an[i].getId();
+        }
+        // recover sequence association
+        if (an[i].getSequenceRef() != null)
+        {
+          if (al.findName(an[i].getSequenceRef()) != null)
           {
-            if (al.findName(an[i].getSequenceRef()) != null)
-            {
-              jaa.createSequenceMapping(
-                      al.findName(an[i].getSequenceRef()), 1, true);
-              al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(
-                      jaa);
-            }
+            jaa.createSequenceMapping(al.findName(an[i].getSequenceRef()),
+                    1, true);
+            al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
           }
         }
         // and make a note of any group association
@@ -3368,7 +3369,23 @@ public class Jalview2XML
                 );
       }
     }
-
+    if (view.getCalcIdParam() != null)
+    {
+      for (CalcIdParam calcIdParam : view.getCalcIdParam())
+      {
+        if (calcIdParam != null)
+        {
+          if (recoverCalcIdParam(calcIdParam, af.viewport))
+          {
+          }
+          else
+          {
+            warn("Couldn't recover parameters for "
+                    + calcIdParam.getCalcId());
+          }
+        }
+      }
+    }
     af.setMenusFromViewport(af.viewport);
     // TODO: we don't need to do this if the viewport is aready visible.
     Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
@@ -3398,21 +3415,32 @@ public class Jalview2XML
       }
       for (JvAnnotRow auan : autoAlan)
       {
-        visan.put(auan.template.label, auan);
+        visan.put(auan.template.label+(auan.template.getCalcId()==null ? "" : "\t"+auan.template.getCalcId()), auan);
       }
       int hSize = al.getAlignmentAnnotation().length;
       ArrayList<JvAnnotRow> reorder = new ArrayList<JvAnnotRow>();
+      // work through any autoCalculated annotation already on the view
+      // removing it if it should be placed in a different location on the
+      // annotation panel.
+      List<String> remains=new ArrayList(visan.keySet());
       for (int h = 0; h < hSize; h++)
       {
         jalview.datamodel.AlignmentAnnotation jalan = al
                 .getAlignmentAnnotation()[h];
         if (jalan.autoCalculated)
         {
-          JvAnnotRow valan = visan.get(jalan.label);
+          String k;
+          JvAnnotRow valan = visan.get(k=jalan.label);
+          if (jalan.getCalcId()!=null)
+          {
+            valan = visan.get(k=jalan.label+ "\t"+jalan.getCalcId());
+          }
+          
           if (valan != null)
           {
             // delete the auto calculated row from the alignment
-            al.deleteAnnotation(al.getAlignmentAnnotation()[h], false);
+            al.deleteAnnotation(jalan, false);
+            remains.remove(k);
             hSize--;
             h--;
             if (valan != nullAnnot)
@@ -3434,6 +3462,16 @@ public class Jalview2XML
           }
         }
       }
+      // Add any (possibly stale) autocalculated rows that were not appended to the view during construction
+      for (String other:remains)
+      {
+        JvAnnotRow othera=visan.get(other);
+        if (othera!=nullAnnot && othera.template.getCalcId()!=null && othera.template.getCalcId().length()>0)
+        {
+          reorder.add(othera);
+        }
+      }
+      // now put the automatic annotation in its correct place
       int s = 0, srt[] = new int[reorder.size()];
       JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
       for (JvAnnotRow jvar : reorder)