JAL-629 Tests for FileUtils. Tidying counter substitutions. Fixing PAE opening.
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 5 Apr 2023 15:29:31 +0000 (16:29 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 5 Apr 2023 15:29:31 +0000 (16:29 +0100)
examples/test_fab41.result/argfile.txt
src/jalview/bin/Commands.java
src/jalview/bin/argparser/ArgParser.java
src/jalview/bin/argparser/SubVals.java
test/jalview/bin/CommandsTest.java
test/jalview/util/FileUtilsTest.java [new file with mode: 0644]

index ed450dd..a3540cf 100644 (file)
@@ -1,14 +1,16 @@
+--nonews
+--nosplash
 --substitutions
 --open={argfiledirname}/sample.a2m
 --colour=gecos:flower
---structure={argfiledirname}/test_fab41_unrelaxed_rank_1_model_3.pdb
---paematrix=[label=pAE R1-M3]{argfiledirname}/test_fab41_unrelaxed_rank_1_model_3_scores.json
---structure={argfiledirname}/test_fab41_unrelaxed_rank_2_model_4.pdb
---paematrix=[label=pAE R2-M4]{argfiledirname}/test_fab41_unrelaxed_rank_2_model_4_scores.json
---structure={argfiledirname}/test_fab41_unrelaxed_rank_3_model_2.pdb
---paematrix=[label=pAE R3-M2]{argfiledirname}/test_fab41_unrelaxed_rank_3_model_2_scores.json
---structure={argfiledirname}/test_fab41_unrelaxed_rank_4_model_5.pdb
---paematrix=[label=pAE R4-M5]{argfiledirname}/test_fab41_unrelaxed_rank_4_model_5_scores.json
---structure={argfiledirname}/test_fab41_unrelaxed_rank_5_model_1.pdb
---paematrix=[label=pAE R5-M1]{argfiledirname}/test_fab41_unrelaxed_rank_5_model_1_scores.json
+--structure={dirname}/test_fab41_unrelaxed_rank_1_model_3.pdb
+--paematrix="[label=PAE R1-M3]{dirname}/test_fab41_unrelaxed_rank_1_model_3_scores.json"
+--structure={dirname}/test_fab41_unrelaxed_rank_2_model_4.pdb
+--paematrix="[label=PAE R2-M4]{dirname}/test_fab41_unrelaxed_rank_2_model_4_scores.json"
+--structure={dirname}/test_fab41_unrelaxed_rank_3_model_2.pdb
+--paematrix="[label=PAE R3-M2]{dirname}/test_fab41_unrelaxed_rank_3_model_2_scores.json"
+--structure={dirname}/test_fab41_unrelaxed_rank_4_model_5.pdb
+--paematrix="[label=PAE R4-M5]{dirname}/test_fab41_unrelaxed_rank_4_model_5_scores.json"
+--structure={dirname}/test_fab41_unrelaxed_rank_5_model_1.pdb
+--paematrix="[label=PAE R5-M1]{dirname}/test_fab41_unrelaxed_rank_5_model_1_scores.json"
 --image={dirname}/{basename}.html
index e4be614..9ff95e3 100644 (file)
@@ -403,6 +403,14 @@ public class Commands
           SequenceI seq = getSpecifiedSequence(af, subId);
           if (seq == null)
           {
+            // Could not find sequence from subId, let's assume the first
+            // sequence in the alignframe
+            AlignmentI al = af.getCurrentView().getAlignment();
+            seq = al.getSequenceAt(0);
+          }
+
+          if (seq == null)
+          {
             Console.warn("Could not find sequence for argument "
                     + Arg.STRUCTURE.argString() + "=" + val);
             // you probably want to continue here, not break
@@ -417,7 +425,6 @@ public class Commands
             Console.debug("Using structure file (from argument) '"
                     + structureFile.getAbsolutePath() + "'");
           }
-
           // TRY THIS
           /*
            * PDBEntry fileEntry = new AssociatePdbFileWithSeq()
@@ -428,7 +435,6 @@ public class Commands
            * SequenceI[] { selectedSequence });
            * 
            */
-
           /* THIS DOESN'T WORK */
           else if (seq.getAllPDBEntries() != null
                   && seq.getAllPDBEntries().size() > 0)
@@ -479,7 +485,7 @@ public class Commands
       }
     }
 
-    // load a pAE file if given
+    // load a PAE file if given
     if (avm.containsArg(Arg.PAEMATRIX))
     {
       AlignFrame af = afMap.get(id);
@@ -503,7 +509,7 @@ public class Commands
           }
           String structid = null;
           String structfile = null;
-          String seqid = null;
+          int seqindex = SubVals.NOTSET;
           if (subVals.notSet())
           {
             ArgValue likelyStructure = avm
@@ -518,6 +524,9 @@ public class Commands
               else
               {
                 structfile = likelyStructure.getValue();
+                Console.debug(
+                        "##### Using closest previous structure argument '"
+                                + structfile + "'");
               }
             }
           }
@@ -532,27 +541,26 @@ public class Commands
           if (structfile != null)
           {
             Console.info("##### Attaching paeFile '" + paePath + "' to "
-                    + "structfile=" + subVals.get("structfile"));
+                    + "structfile=" + structfile);
             EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
-                    paeFile, subVals.getIndex(), subVals.get("structfile"),
-                    true, false, paeLabel);
+                    paeFile, seqindex, structfile, true, false, paeLabel);
           }
           else if (structid != null)
           {
             Console.info("##### Attaching paeFile '" + paePath + "' to "
-                    + "structid=" + subVals.get("structid"));
+                    + "structid=" + structid);
             EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
-                    paeFile, subVals.getIndex(), subVals.get("structid"),
-                    true, true, paeLabel);
+                    paeFile, seqindex, subVals.get("structid"), true, true,
+                    paeLabel);
           }
           else
           {
+            seqindex = subVals.getIndex();
             Console.debug("##### Attaching paeFile '" + paePath
-                    + "' to sequence index " + subVals.getIndex());
+                    + "' to sequence index " + seqindex);
             EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
-                    paeFile, subVals.getIndex(), null, false, false,
-                    paeLabel);
-            // required to readjust the height and position of the pAE
+                    paeFile, seqindex, null, false, false, paeLabel);
+            // required to readjust the height and position of the PAE
             // annotation
           }
           for (AlignmentViewPanel ap : af.getAlignPanels())
@@ -646,15 +654,17 @@ public class Commands
 
   private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
   {
+    if (subId == null)
+      return null;
     AlignmentI al = af.getCurrentView().getAlignment();
-    if (-1 < subId.getIndex()
-            && subId.getIndex() < al.getSequences().size())
+    if (subId.has("seqid"))
     {
-      return al.getSequenceAt(subId.getIndex());
+      return al.findName(subId.get("seqid"));
     }
-    else if (subId.has("seqid"))
+    else if (-1 < subId.getIndex()
+            && subId.getIndex() < al.getSequences().size())
     {
-      return al.findName(subId.get("seqid"));
+      return al.getSequenceAt(subId.getIndex());
     }
     return null;
   }
index ff238da..f3306d6 100644 (file)
@@ -50,18 +50,24 @@ public class ArgParser
   // the counter added to the default linked id prefix
   private int defaultLinkedIdCounter = 0;
 
-  // the linked id prefix used for --opennew files
-  protected static final String OPENNEWLINKEDIDPREFIX = "OPENNEW:";
+  // the substitution string used to use the defaultLinkedIdCounter
+  private static final String DEFAULTLINKEDIDCOUNTER = "{}";
 
   // the counter added to the default linked id prefix
   private int opennewLinkedIdCounter = 0;
 
+  // the linked id prefix used for --opennew files
+  protected static final String OPENNEWLINKEDIDPREFIX = "OPENNEW:";
+
+  // the counter used for {n} substitutions
+  private int linkedIdAutoCounter = 0;
+
   // the linked id substitution string used to increment the idCounter (and use
   // the incremented value)
-  private static final String INCREMENTAUTOCOUNTERLINKEDID = "{++n}";
+  private static final String INCREMENTLINKEDIDAUTOCOUNTER = "{++n}";
 
   // the linked id substitution string used to use the idCounter
-  private static final String AUTOCOUNTERLINKEDID = "{n}";
+  private static final String LINKEDIDAUTOCOUNTER = "{n}";
 
   // the linked id substitution string used to use the base filename of --open
   // or --opennew
@@ -82,8 +88,6 @@ public class ArgParser
   // --argfile name
   private static final String ARGFILEDIRNAME = "{argfiledirname}";
 
-  private int linkedIdAutoCounter = 0;
-
   // flag to say whether {n} subtitutions in output filenames should be made.
   // Turn on and off with --subs and --nosubs
   private boolean substitutions = false;
@@ -369,21 +373,21 @@ public class ArgParser
                       + arg);
             }
           }
-          else if (linkedId.contains(AUTOCOUNTERLINKEDID))
+          else if (linkedId.contains(LINKEDIDAUTOCOUNTER))
           {
             // turn {n} to the autoCounter
             autoCounterString = Integer.toString(linkedIdAutoCounter);
-            linkedId = linkedId.replace(AUTOCOUNTERLINKEDID,
+            linkedId = linkedId.replace(LINKEDIDAUTOCOUNTER,
                     autoCounterString);
             usingAutoCounterLinkedId = true;
             Console.debug(
                     "Changing linkedId to '" + linkedId + "' from " + arg);
           }
-          else if (linkedId.contains(INCREMENTAUTOCOUNTERLINKEDID))
+          else if (linkedId.contains(INCREMENTLINKEDIDAUTOCOUNTER))
           {
             // turn {++n} to the incremented autoCounter
             autoCounterString = Integer.toString(++linkedIdAutoCounter);
-            linkedId = linkedId.replace(INCREMENTAUTOCOUNTERLINKEDID,
+            linkedId = linkedId.replace(INCREMENTLINKEDIDAUTOCOUNTER,
                     autoCounterString);
             usingAutoCounterLinkedId = true;
             Console.debug(
@@ -494,14 +498,15 @@ public class ArgParser
       subvals = "";
       rest = val;
     }
-    if (rest.contains(AUTOCOUNTERLINKEDID))
-      rest = rest.replace(AUTOCOUNTERLINKEDID,
+    if (rest.contains(LINKEDIDAUTOCOUNTER))
+      rest = rest.replace(LINKEDIDAUTOCOUNTER,
               String.valueOf(linkedIdAutoCounter));
-    if (rest.contains(INCREMENTAUTOCOUNTERLINKEDID))
-      rest = rest.replace(INCREMENTAUTOCOUNTERLINKEDID,
+    if (rest.contains(INCREMENTLINKEDIDAUTOCOUNTER))
+      rest = rest.replace(INCREMENTLINKEDIDAUTOCOUNTER,
               String.valueOf(++linkedIdAutoCounter));
-    if (rest.contains("{}"))
-      rest = rest.replace("{}", String.valueOf(defaultLinkedIdCounter));
+    if (rest.contains(DEFAULTLINKEDIDCOUNTER))
+      rest = rest.replace(DEFAULTLINKEDIDCOUNTER,
+              String.valueOf(defaultLinkedIdCounter));
     ArgValuesMap avm = linkedArgs.get(linkedId);
     if (avm != null)
     {
index dd20ca6..3c7ba7c 100644 (file)
@@ -15,7 +15,7 @@ import jalview.bin.Console;
  */
 public class SubVals
 {
-  private static int NOTSET = -1;
+  public static int NOTSET = -1;
 
   private int index = NOTSET;
 
index 30c5e4e..cb4ac49 100644 (file)
@@ -19,7 +19,7 @@ import jalview.gui.Desktop;
 import jalview.gui.JvOptionPane;
 import jalview.util.ArrayUtils;
 
-@Test(singleThreaded = true)
+@Test
 public class CommandsTest
 {
   private static final String testfiles = "test/jalview/bin/argparser/testfiles";
diff --git a/test/jalview/util/FileUtilsTest.java b/test/jalview/util/FileUtilsTest.java
new file mode 100644 (file)
index 0000000..8b52577
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.util;
+
+import java.io.File;
+import java.util.List;
+
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+@Test
+public class FileUtilsTest
+{
+  @Test(groups = "Functional", dataProvider = "patternsAndMinNumFiles")
+  public void testJavaFileGlob(String pattern, int atLeast, int atMost)
+  {
+    List<File> files = FileUtils.getFilesFromGlob(pattern);
+    if (atLeast != -1)
+    {
+      Assert.assertTrue(files.size() > atLeast,
+              "Did not find more than " + atLeast + " files with " + pattern
+                      + " (found " + files.size() + ")");
+    }
+    if (atLeast != -1)
+    {
+      Assert.assertTrue(files.size() > atLeast,
+              "Did not find more than " + atLeast + " files with " + pattern
+                      + " (found " + files.size() + ")");
+    }
+    if (atMost != -1)
+    {
+      Assert.assertTrue(files.size() < atMost,
+              "Did not find fewer than " + atMost + " files with " + pattern
+                      + " (found " + files.size() + ")");
+    }
+  }
+
+  @Test(groups = "Functional", dataProvider = "dirnamesAndBasenames")
+  public void testDirnamesAndBasenames(String filename, int where,
+          String dirname, String basename)
+  {
+    File file = new File(filename);
+    String d = FileUtils.getDirname(file);
+    String b = FileUtils.getBasename(file);
+    Assert.assertEquals(b, basename);
+    if (where == 0)
+      Assert.assertEquals(d, dirname);
+    else if (where < 0)
+      Assert.assertTrue(d.startsWith(dirname),
+              "getDirname(" + file.getPath() + ")=" + d
+                      + " didn't start with '" + d + "'");
+    else if (where > 0)
+      Assert.assertTrue(d.endsWith(dirname), "getDirname(" + file.getPath()
+              + ")=" + d + " didn't end with '" + d + "'");
+  }
+
+  @DataProvider(name = "patternsAndMinNumFiles")
+  public Object[][] patternsAndMinNumFiles()
+  {
+    return new Object[][] { { "src/**/*.java", 900, 100000 },
+        { "src/**.java", 900, 100000 },
+        { "test/**/*.java", 250, 2500 },
+        { "test/**.java", 250, 2500 },
+        { "help/**/*.html", 100, 1000 },
+        { "test/**/F*.java", 15, 150 },
+        { "test/jalview/*/F*.java", 10, 15 }, // 12 at time of writing
+        { "test/jalview/**/F*.java", 18, 30 }, // 20 at time of writing
+        { "test/jalview/util/F**.java", 1, 5 }, // 2 at time of writing
+        { "src/jalview/b*/*.java", 14, 19 }, // 15 at time of writing
+        { "src/jalview/b**/*.java", 20, 25 }, // 22 at time of writing
+    };
+  }
+
+  @DataProvider(name = "dirnamesAndBasenames")
+  public Object[][] dirnamesAndBasenames()
+  {
+    return new Object[][] { // -1=startsWith, 0=equals, 1=endsWith
+        { "~/hello/sailor", -1, System.getProperty("user.home"), "sailor" }, //
+        { "~/hello/sailor", 1, "/hello", "sailor" }, //
+        { "./examples/uniref50.fa", -1, "/", "uniref50" }, //
+        { "./examples/uniref50.fa", 1, "/examples", "uniref50" }, //
+        { "examples/uniref50.fa", 1, "/examples", "uniref50" }, //
+    };
+  }
+}