JAL-629 Added --structureimage code and formatting args/subvals. Added tests for...
[jalview.git] / src / jalview / bin / argparser / ArgParser.java
index 6d1251c..0a8b8d3 100644 (file)
@@ -121,6 +121,10 @@ public class ArgParser
   // or OPENED linkedIds
   private boolean openedLinkedIds = false;
 
+  // flag to say whether the structure arguments should be applied to all
+  // structures with this linked id
+  private boolean allStructures = false;
+
   protected static final Map<String, Arg> argMap;
 
   protected Map<String, ArgValuesMap> linkedArgs = new HashMap<>();
@@ -424,6 +428,10 @@ public class ArgParser
           openedLinkedIds = !negated;
           allLinkedIds = false;
         }
+        else if (a == Arg.ALLSTRUCTURES)
+        {
+          allStructures = !negated;
+        }
 
         if (a.hasOption(Opt.STORED))
         {
@@ -485,23 +493,26 @@ public class ArgParser
                       + arg);
             }
           }
-          else if (linkedId.contains(LINKEDIDAUTOCOUNTER))
-          {
-            // turn {n} to the autoCounter
-            autoCounterString = Integer.toString(linkedIdAutoCounter);
-            linkedId = linkedId.replace(LINKEDIDAUTOCOUNTER,
-                    autoCounterString);
-            Console.debug(
-                    "Changing linkedId to '" + linkedId + "' from " + arg);
-          }
-          else if (linkedId.contains(INCREMENTLINKEDIDAUTOCOUNTER))
+          else
           {
-            // turn {++n} to the incremented autoCounter
-            autoCounterString = Integer.toString(++linkedIdAutoCounter);
-            linkedId = linkedId.replace(INCREMENTLINKEDIDAUTOCOUNTER,
-                    autoCounterString);
-            Console.debug(
-                    "Changing linkedId to '" + linkedId + "' from " + arg);
+            if (linkedId.contains(LINKEDIDAUTOCOUNTER))
+            {
+              // turn {n} to the autoCounter
+              autoCounterString = Integer.toString(linkedIdAutoCounter);
+              linkedId = linkedId.replace(LINKEDIDAUTOCOUNTER,
+                      autoCounterString);
+              Console.debug("Changing linkedId to '" + linkedId + "' from "
+                      + arg);
+            }
+            if (linkedId.contains(INCREMENTLINKEDIDAUTOCOUNTER))
+            {
+              // turn {++n} to the incremented autoCounter
+              autoCounterString = Integer.toString(++linkedIdAutoCounter);
+              linkedId = linkedId.replace(INCREMENTLINKEDIDAUTOCOUNTER,
+                      autoCounterString);
+              Console.debug("Changing linkedId to '" + linkedId + "' from "
+                      + arg);
+            }
           }
         }
 
@@ -521,8 +532,9 @@ public class ArgParser
         }
 
         // check for unique id
-        SubVals idsv = new SubVals(val);
-        String id = idsv.get(ArgValues.ID);
+        SubVals subvals = new SubVals(val);
+        boolean addNewSubVals = false;
+        String id = subvals.get(ArgValues.ID);
         if (id != null && avm.hasId(a, id))
         {
           Console.error("Argument '" + a.argString()
@@ -530,10 +542,21 @@ public class ArgParser
           continue;
         }
 
-        /* TODO
-         * Change all avs.addValue() avs.setBoolean avs.setNegated() avs.incrementCount calls to checkfor linkedId == "*"
-         * DONE, need to check
-         */
+        // set allstructures to all non-primary structure options in this linked
+        // id if --allstructures has been set
+        if (allStructures
+                && (a.getType() == Type.STRUCTURE
+                        || a.getType() == Type.STRUCTUREIMAGE)
+                && !a.hasOption(Opt.PRIMARY))
+        {
+          if (!subvals.has(Arg.ALLSTRUCTURES.getName()))
+          // && !subvals.has("structureid"))
+          {
+            subvals.put(Arg.ALLSTRUCTURES.getName(), "true");
+            addNewSubVals = true;
+          }
+        }
+
         ArgValues avs = avm.getOrCreateArgValues(a);
 
         // store appropriate String value(s)
@@ -563,7 +586,9 @@ public class ArgParser
           }
           else
           {
-            addValue(linkedId, type, avs, val, argIndex, true);
+            // addValue(linkedId, type, avs, val, argIndex, true);
+            addValue(linkedId, type, avs, addNewSubVals ? subvals : null,
+                    val, argIndex, true);
           }
         }
         else if (a.hasOption(Opt.BOOLEAN))
@@ -925,8 +950,23 @@ public class ArgParser
       else if (pos == Position.AFTER
               && avm.getClosestNextArgValueOfArg(av, a) != null)
         value = avm.getClosestNextArgValueOfArg(av, a).getValue();
+
+      // look for allstructures subval for Type.STRUCTURE*
+      Arg arg = av.getArg();
+      if (value == null && arg.hasOption(Opt.PRIMARY)
+              && arg.getType() == Type.STRUCTURE
+              && !a.hasOption(Opt.PRIMARY) && (a.getType() == Type.STRUCTURE
+                      || a.getType() == Type.STRUCTUREIMAGE))
+      {
+        ArgValue av2 = avm.getArgValueOfArgWithSubValKey(a,
+                Arg.ALLSTRUCTURES.getName());
+        if (av2 != null)
+        {
+          value = av2.getValue();
+        }
+      }
     }
-    else
+    if (value == null)
     {
       value = pref != null ? Cache.getDefault(pref, def) : def;
     }
@@ -942,6 +982,13 @@ public class ArgParser
   public static boolean getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
           SubVals sv, String key, String pref, boolean def)
   {
+    return getFromSubValArgOrPref(avm, a, sv, key, pref, def, false);
+  }
+
+  public static boolean getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
+          SubVals sv, String key, String pref, boolean def,
+          boolean invertPref)
+  {
     if ((key == null && a == null) || (sv == null && a == null))
       return false;
 
@@ -995,7 +1042,8 @@ public class ArgParser
       return avm.getBoolean(a);
 
     // return preference or default
-    return pref != null ? Cache.getDefault(pref, def) : def;
+    boolean prefVal = pref != null ? Cache.getDefault(pref, def) : false;
+    return pref != null ? (invertPref ? !prefVal : prefVal) : def;
   }
 
   // the following methods look for the "*" linkedId and add the argvalue to all
@@ -1039,13 +1087,35 @@ public class ArgParser
     ADDVALUE, SETBOOLEAN, SETNEGATED, INCREMENTCOUNT
   }
 
-  // The following operations look for the "*" and "open*" linkedIds and add the
-  // argvalue to all appropriate linkedId ArgValues if it does.
-  // If subvals are supplied, they are inserted into all new set values.
   private void argValueOperation(Op op, String linkedId, Type type,
           ArgValues avs, SubVals sv, String v, boolean b, int argIndex,
           boolean doSubs)
   {
+    // default to merge subvals if subvals are provided
+    argValueOperation(op, linkedId, type, avs, sv, true, v, b, argIndex,
+            doSubs);
+  }
+
+  /*
+   * The following operations look for the "*" and "open*" linkedIds and add the
+   * argvalue to all appropriate linkedId ArgValues if it does.
+   * If subvals are supplied, they are inserted into all new set values.
+   * 
+   * @param op The ArgParser.Op operation
+   * @param linkedId The String linkedId from the ArgValuesMap
+   * @param type The Arg.Type to attach to this ArgValue
+   * @param avs The ArgValues for this linkedId
+   * @param sv Use these SubVals on the ArgValue
+   * @param merge Merge the SubVals with any existing on the value.  False will replace unless sv is null
+   * @param v The value of the ArgValue (may contain subvals).
+   * @param b The boolean value of the ArgValue.
+   * @param argIndex The argIndex for the ArgValue.
+   * @param doSubs Whether to perform substitutions on the subvals and value.
+   */
+  private void argValueOperation(Op op, String linkedId, Type type,
+          ArgValues avs, SubVals sv, boolean merge, String v, boolean b,
+          int argIndex, boolean doSubs)
+  {
     Arg a = avs.arg();
 
     List<String> wildcardLinkedIds = null;
@@ -1097,8 +1167,8 @@ public class ArgParser
           {
             if (doSubs)
             {
-              val = makeSubstitutions(v, id);
-              sv = new SubVals(sv, val);
+              sv = new SubVals(sv, val, merge);
+              val = makeSubstitutions(sv.getContent(), id);
             }
             tavs.addValue(sv, type, val, argIndex, true);
           }