JAL-4269 Now using --width, --height, --scale for both --image and --structureimage...
[jalview.git] / src / jalview / bin / argparser / ArgValuesMap.java
index 4aa8570..99a4836 100644 (file)
@@ -2,12 +2,17 @@ package jalview.bin.argparser;
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
+import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.bin.argparser.Arg.Opt;
+import jalview.bin.argparser.Arg.Type;
 import jalview.util.FileUtils;
 
 /**
@@ -16,6 +21,8 @@ import jalview.util.FileUtils;
  */
 public class ArgValuesMap
 {
+  private List<ArgInfo> argInfoList = new ArrayList<>();
+
   protected Map<Arg, ArgValues> m;
 
   private String linkedId;
@@ -165,7 +172,8 @@ public class ArgValuesMap
     return closestAv;
   }
 
-  public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
+  public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a,
+          boolean withinType)
   {
     // this looks for the *next* arg that *might* be referring back to
     // a thisAv. Such an arg would have no subValues (if it does it should
@@ -185,6 +193,35 @@ public class ArgValuesMap
         closestAv = av;
       }
     }
+
+    // check if withinType this closestAv doesn't belong to the next primary arg
+    // of this type
+    if (withinType && closestAv != null)
+    {
+      int nextPrimaryArgOfSameTypeIndex = Integer.MAX_VALUE;
+      for (Arg tmpA : this.getArgKeys())
+      {
+        // interested in Opt.PRIMARY args of the same type
+        if (tmpA.getType() == a.getType() && tmpA.hasOption(Opt.PRIMARY))
+        {
+          for (ArgValue tmpAv : getArgValueList(tmpA))
+          {
+            int tmpArgIndex = tmpAv.getArgIndex();
+            if (tmpArgIndex > thisArgIndex
+                    && tmpArgIndex < nextPrimaryArgOfSameTypeIndex)
+            {
+              nextPrimaryArgOfSameTypeIndex = tmpArgIndex;
+            }
+          }
+        }
+      }
+      if (nextPrimaryArgOfSameTypeIndex < closestAv.getArgIndex())
+      {
+        // looks licke closestAv actually belongs to a different primary Arg
+        return null;
+      }
+    }
+
     return closestAv;
   }
 
@@ -281,4 +318,226 @@ public class ArgValuesMap
     }
     return false;
   }
+
+  /*
+   * ArgInfo is a more straightforward list of arguments and their info
+   */
+
+  public void addArgInfo(Arg arg, String value, SubVals subVals,
+          int argIndex)
+  {
+    argInfoList.add(new ArgInfo(arg, value, subVals, argIndex));
+  }
+
+  public List<ArgInfo> getArgInfoList()
+  {
+    Collections.sort(argInfoList);
+    return argInfoList;
+  }
+
+  /**
+   * get from following Arg of type a or subval of same name (lowercase)
+   */
+  public String getValueFromSubValOrArg(ArgValue av, Arg a, SubVals sv)
+  {
+    return getFromSubValArgOrPref(av, a, sv, null, null, null);
+  }
+
+  /**
+   * get from following Arg of type a or subval key or preference pref or
+   * default def
+   */
+  public String getFromSubValArgOrPref(ArgValue av, Arg a, SubVals sv,
+          String key, String pref, String def)
+  {
+    return getFromSubValArgOrPref(a, ArgValuesMap.Position.AFTER, av, sv,
+            key, pref, def);
+  }
+
+  /**
+   * get from following(AFTER), first occurence of (FIRST) or previous (BEFORE)
+   * Arg of type a or subval key or preference pref or default def
+   */
+  public String getFromSubValArgOrPref(Arg a, ArgValuesMap.Position pos,
+          ArgValue av, SubVals sv, String key, String pref, String def)
+  {
+    return getFromSubValArgOrPrefWithSubstitutions(null, a, pos, av, sv,
+            key, pref, def);
+  }
+
+  public String getFromSubValArgOrPrefWithSubstitutions(ArgParser ap, Arg a,
+          ArgValuesMap.Position pos, ArgValue av, SubVals sv, String key,
+          String pref, String def)
+  {
+    return getFromSubValArgOrPrefWithSubstitutionsWithinType(ap, a, pos, av,
+            sv, key, pref, def, true);
+  }
+
+  public String getFromSubValArgOrPrefWithSubstitutionsWithinType(
+          ArgParser ap, Arg a, ArgValuesMap.Position pos, ArgValue av,
+          SubVals sv, String key, String pref, String def,
+          boolean withinType)
+  {
+    if (key == null)
+      key = a.getName();
+    String value = null;
+    if (sv != null && sv.has(key) && sv.get(key) != null)
+    {
+      value = ap == null ? sv.get(key)
+              : sv.getWithSubstitutions(ap, getLinkedId(), key);
+    }
+    else if (containsArg(a))
+    {
+      if (pos == ArgValuesMap.Position.FIRST && getValue(a) != null)
+        value = getValue(a);
+      else if (pos == ArgValuesMap.Position.BEFORE
+              && getClosestPreviousArgValueOfArg(av, a) != null)
+        value = getClosestPreviousArgValueOfArg(av, a).getValue();
+      else if (pos == ArgValuesMap.Position.AFTER
+              && getClosestNextArgValueOfArg(av, a, withinType) != null)
+        value = getClosestNextArgValueOfArg(av, a, withinType).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 = getArgValueOfArgWithSubValKey(a,
+                Arg.ALLSTRUCTURES.getName());
+        if (av2 != null)
+        {
+          value = av2.getValue();
+        }
+      }
+    }
+    if (value == null)
+    {
+      value = pref != null ? Cache.getDefault(pref, def) : def;
+    }
+    return value;
+  }
+
+  public boolean getBoolFromSubValOrArg(Arg a, SubVals sv)
+  {
+    return getFromSubValArgOrPref(a, sv, null, null, false);
+  }
+
+  public boolean getFromSubValArgOrPref(Arg a, SubVals sv, String key,
+          String pref, boolean def)
+  {
+    return getFromSubValArgOrPref(a, sv, key, pref, def, false);
+  }
+
+  public boolean getFromSubValArgOrPref(Arg a, SubVals sv, String key,
+          String pref, boolean def, boolean invertPref)
+  {
+    if ((key == null && a == null) || (sv == null && a == null))
+      return false;
+
+    boolean usingArgKey = false;
+    if (key == null)
+    {
+      key = a.getName();
+      usingArgKey = true;
+    }
+
+    String nokey = ArgParser.NEGATESTRING + key;
+
+    // look for key or nokey in subvals first (if using Arg check options)
+    if (sv != null)
+    {
+      // check for true boolean
+      if (sv.has(key) && sv.get(key) != null)
+      {
+        if (usingArgKey)
+        {
+          if (!(a.hasOption(Opt.BOOLEAN) || a.hasOption(Opt.UNARY)))
+          {
+            Console.debug(
+                    "Looking for boolean in subval from non-boolean/non-unary Arg "
+                            + a.getName());
+            return false;
+          }
+        }
+        return sv.get(key).toLowerCase(Locale.ROOT).equals("true");
+      }
+
+      // check for negative boolean (subval "no..." will be "true")
+      if (sv.has(nokey) && sv.get(nokey) != null)
+      {
+        if (usingArgKey)
+        {
+          if (!(a.hasOption(Opt.BOOLEAN)))
+          {
+            Console.debug(
+                    "Looking for negative boolean in subval from non-boolean Arg "
+                            + a.getName());
+            return false;
+          }
+        }
+        return !sv.get(nokey).toLowerCase(Locale.ROOT).equals("true");
+      }
+    }
+
+    // check argvalues
+    if (containsArg(a))
+      return getBoolean(a);
+
+    // return preference or default
+    boolean prefVal = pref != null ? Cache.getDefault(pref, def) : false;
+    return pref != null ? (invertPref ? !prefVal : prefVal) : def;
+  }
+
+  public class ArgInfo implements Comparable<ArgInfo>
+  {
+    private Arg arg;
+
+    private String value;
+
+    private SubVals subVals;
+
+    private int argIndex;
+
+    public ArgInfo(Arg arg, String value, SubVals subVals, int argIndex)
+    {
+      this.arg = arg;
+      this.value = value;
+      this.subVals = subVals;
+      this.argIndex = argIndex;
+    }
+
+    public Arg arg()
+    {
+      return arg;
+    }
+
+    public String value()
+    {
+      return value;
+    }
+
+    public SubVals subVals()
+    {
+      return subVals;
+    }
+
+    public int argIndex()
+    {
+      return argIndex;
+    }
+
+    @Override
+    public int compareTo(ArgInfo ai2)
+    {
+      return Integer.compare(this.argIndex(), ai2.argIndex());
+    }
+  }
+
+  public static enum Position
+  {
+    FIRST, BEFORE, AFTER
+  }
 }