JAL-2110 refactor sequenceFetcherTest to allow verifying cross-reference retrieval...
authorJim Procter <jprocter@issues.jalview.org>
Thu, 23 Jun 2016 13:04:47 +0000 (14:04 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 23 Jun 2016 13:04:47 +0000 (14:04 +0100)
test/jalview/ws/SequenceFetcherTest.java

index 4e08430..94bf979 100644 (file)
@@ -25,8 +25,6 @@ public class SequenceFetcherTest
     // TODO: extracted from SequenceFetcher - convert to proper unit test with
     // assertions
 
-    AlignmentI ds = null;
-    Vector<Object[]> noProds = new Vector<Object[]>();
     String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
             + "With no arguments, all DbSources will be queried with their test Accession number.\n"
             + "With one argument, the argument will be resolved to one or more db sources and each will be queried with their test accession only.\n"
@@ -45,7 +43,7 @@ public class SequenceFetcherTest
     {
       List<DbSourceProxy> sps = new SequenceFetcher(withDas)
               .getSourceProxy(argv[0]);
-  
+
       if (sps != null)
       {
         for (DbSourceProxy sp : sps)
@@ -53,7 +51,8 @@ public class SequenceFetcherTest
           AlignmentI al = null;
           try
           {
-            al = sp.getSequenceRecords(argv.length > 1 ? argv[1] : sp
+            testRetrieval(argv[0], sp,
+                    argv.length > 1 ? argv[1] : sp
                     .getTestQuery());
           } catch (Exception e)
           {
@@ -62,16 +61,6 @@ public class SequenceFetcherTest
                     + (argv.length > 1 ? argv[1] : sp.getTestQuery())
                     + " from " + argv[0] + "\nUsage: " + usage);
           }
-          SequenceI[] prod = al.getSequencesArray();
-          if (al != null)
-          {
-            for (int p = 0; p < prod.length; p++)
-            {
-              System.out.println("Prod " + p + ": "
-                      + prod[p].getDisplayId(true) + " : "
-                      + prod[p].getDescription());
-            }
-          }
         }
         return;
       }
@@ -96,138 +85,135 @@ public class SequenceFetcherTest
       }
       for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
       {
-        System.out.println("Source: " + sp.getDbName() + " (" + db
-                + "): retrieving test:" + sp.getTestQuery());
-        AlignmentI al = null;
-        try
+        testRetrieval(db, sp, sp.getTestQuery());
+      }
+    }
+
+  }
+
+  private static void testRetrieval(String db, DbSourceProxy sp,
+          String testQuery)
+  {
+    AlignmentI ds = null;
+    Vector<Object[]> noProds = new Vector<Object[]>();
+    System.out.println("Source: " + sp.getDbName() + " (" + db
+            + "): retrieving test:" + sp.getTestQuery());
+    {
+      AlignmentI al = null;
+      try
+      {
+        al = sp.getSequenceRecords(testQuery);
+        if (al != null && al.getHeight() > 0)
         {
-          al = sp.getSequenceRecords(sp.getTestQuery());
-          if (al != null && al.getHeight() > 0)
+          boolean dna = sp.isDnaCoding();
+          al.setDataset(null);
+          AlignmentI alds = al.getDataset();
+          // try and find products
+          CrossRef crossRef = new CrossRef(al.getSequencesArray(), alds);
+          List<String> types = crossRef.findXrefSourcesForSequences(dna);
+          if (types != null)
           {
-            boolean dna = sp.isDnaCoding();
-            // try and find products
-            CrossRef crossRef = new CrossRef(al.getSequencesArray(),
-                    al);
-            List<String> types = crossRef.findXrefSourcesForSequences(dna);
-            if (types != null)
+            System.out.println("Xref Types for: " + (dna ? "dna" : "prot"));
+            for (String source : types)
             {
-              System.out.println("Xref Types for: "
-                      + (dna ? "dna" : "prot"));
-              for (String source : types)
+              System.out.println("Type: " + source);
+              SequenceI[] prod = crossRef.findXrefSequences(source, dna)
+                      .getSequencesArray();
+              System.out.println("Found "
+                      + ((prod == null) ? "no" : "" + prod.length)
+                      + " products");
+              if (prod != null)
               {
-                System.out.println("Type: " + source);
-                SequenceI[] prod = crossRef.findXrefSequences(source, dna)
-                        .getSequencesArray();
-                System.out.println("Found "
-                        + ((prod == null) ? "no" : "" + prod.length)
-                        + " products");
-                if (prod != null)
+                for (int p = 0; p < prod.length; p++)
                 {
-                  for (int p = 0; p < prod.length; p++)
-                  {
-                    System.out.println("Prod " + p + ": "
-                            + prod[p].getDisplayId(true));
-                  }
+                  System.out.println("Prod " + p + ": "
+                          + prod[p].getDisplayId(true));
                 }
               }
             }
-            else
-            {
-              noProds.addElement((dna ? new Object[] { al, al }
-                      : new Object[] { al }));
-            }
-  
-          }
-        } catch (Exception ex)
-        {
-          System.out.println("ERROR:Failed to retrieve test query.");
-          ex.printStackTrace(System.out);
-        }
-  
-        if (al == null)
-        {
-          System.out.println("ERROR:No alignment retrieved.");
-          StringBuffer raw = sp.getRawRecords();
-          if (raw != null)
-          {
-            System.out.println(raw.toString());
           }
           else
           {
-            System.out.println("ERROR:No Raw results.");
+            noProds.addElement((dna ? new Object[] { al, al }
+                    : new Object[] { al }));
           }
+
+        }
+      } catch (Exception ex)
+      {
+        System.out.println("ERROR:Failed to retrieve test query.");
+        ex.printStackTrace(System.out);
+      }
+
+      if (al == null)
+      {
+        System.out.println("ERROR:No alignment retrieved.");
+        StringBuffer raw = sp.getRawRecords();
+        if (raw != null)
+        {
+          System.out.println(raw.toString());
         }
         else
         {
-          System.out.println("Retrieved " + al.getHeight() + " sequences.");
-          for (int s = 0; s < al.getHeight(); s++)
-          {
-            SequenceI sq = al.getSequenceAt(s);
-            while (sq.getDatasetSequence() != null)
-            {
-              sq = sq.getDatasetSequence();
-  
-            }
-            if (ds == null)
-            {
-              ds = new Alignment(new SequenceI[] { sq });
-  
-            }
-            else
-            {
-              ds.addSequence(sq);
-            }
-          }
+          System.out.println("ERROR:No Raw results.");
+        }
+      }
+      else
+      {
+        System.out.println("Retrieved " + al.getHeight() + " sequences.");
+        if (ds == null)
+        {
+          ds = al.getDataset();
+        }
+        else
+        {
+          ds.append(al.getDataset());
+          al.setDataset(ds);
         }
-        System.out.flush();
-        System.err.flush();
-  
       }
-      if (noProds.size() > 0)
+      System.out.flush();
+      System.err.flush();
+    }
+    if (noProds.size() > 0)
+    {
+      Enumeration<Object[]> ts = noProds.elements();
+      while (ts.hasMoreElements())
+
       {
-        Enumeration<Object[]> ts = noProds.elements();
-        while (ts.hasMoreElements())
-  
+        Object[] typeSq = ts.nextElement();
+        boolean dna = (typeSq.length > 1);
+        AlignmentI al = (AlignmentI) typeSq[0];
+        System.out.println("Trying getProducts for "
+                + al.getSequenceAt(0).getDisplayId(true));
+        System.out.println("Search DS Xref for: " + (dna ? "dna" : "prot"));
+        // have a bash at finding the products amongst all the retrieved
+        // sequences.
+        SequenceI[] seqs = al.getSequencesArray();
+        Alignment prodal = new CrossRef(seqs, ds).findXrefSequences(null,
+                dna);
+        System.out.println("Found "
+                + ((prodal == null) ? "no" : "" + prodal.getHeight())
+                + " products");
+        if (prodal != null)
         {
-          Object[] typeSq = ts.nextElement();
-          boolean dna = (typeSq.length > 1);
-          AlignmentI al = (AlignmentI) typeSq[0];
-          System.out.println("Trying getProducts for "
-                  + al.getSequenceAt(0).getDisplayId(true));
-          System.out.println("Search DS Xref for: "
-                  + (dna ? "dna" : "prot"));
-          // have a bash at finding the products amongst all the retrieved
-          // sequences.
-          SequenceI[] seqs = al.getSequencesArray();
-          Alignment prodal = new CrossRef(seqs, ds).findXrefSequences(null,
-                  dna);
-          System.out.println("Found "
-                  + ((prodal == null) ? "no" : "" + prodal.getHeight())
-                  + " products");
-          if (prodal != null)
+          SequenceI[] prod = prodal.getSequencesArray(); // note
+          // should
+          // test
+          // rather
+          // than
+          // throw
+          // away
+          // codon
+          // mapping
+          // (if
+          // present)
+          for (int p = 0; p < prod.length; p++)
           {
-            SequenceI[] prod = prodal.getSequencesArray(); // note
-            // should
-            // test
-            // rather
-            // than
-            // throw
-            // away
-            // codon
-            // mapping
-            // (if
-            // present)
-            for (int p = 0; p < prod.length; p++)
-            {
-              System.out.println("Prod " + p + ": "
-                      + prod[p].getDisplayId(true));
-            }
+            System.out.println("Prod " + p + ": "
+                    + prod[p].getDisplayId(true));
           }
         }
-  
       }
-  
     }
   }
-
 }