JAL-1064 - refactor to iterate over many sources associated with a database authority
[jalview.git] / src / jalview / ws / SequenceFetcher.java
index d5b3d11..077ce14 100644 (file)
@@ -72,20 +72,23 @@ public class SequenceFetcher extends ASequenceFetcher
     ArrayList dassrc = new ArrayList(), nondas = new ArrayList();\r
     for (int i = 0; i < srcs.length; i++)\r
     {\r
-      String nm = getSourceProxy(srcs[i]).getDbName();\r
-      if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)\r
+      for (DbSourceProxy dbs : getSourceProxy(srcs[i]))\r
       {\r
-        if (nm.startsWith("das:"))\r
+        String nm = dbs.getDbName();\r
+        if (getSourceProxy(srcs[i]) instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)\r
         {\r
-          nm = nm.substring(4);\r
+          if (nm.startsWith("das:"))\r
+          {\r
+            nm = nm.substring(4);\r
+          }\r
+          dassrc.add(new String[]\r
+          { srcs[i], nm.toUpperCase() });\r
+        }\r
+        else\r
+        {\r
+          nondas.add(new String[]\r
+          { srcs[i], nm.toUpperCase() });\r
         }\r
-        dassrc.add(new String[]\r
-        { srcs[i], nm.toUpperCase() });\r
-      }\r
-      else\r
-      {\r
-        nondas.add(new String[]\r
-        { srcs[i], nm.toUpperCase() });\r
       }\r
     }\r
     Object[] sorted = nondas.toArray();\r
@@ -97,6 +100,8 @@ public class SequenceFetcher extends ASequenceFetcher
     }\r
     jalview.util.QuickSort.sort(tosort, sorted);\r
     int i = 0;\r
+    // construct array with all sources listed\r
+    srcs = new String[sorted.length + dassrc.size()];\r
     for (int j = sorted.length - 1; j >= 0; j--, i++)\r
     {\r
       srcs[i] = ((String[]) sorted[j])[0];\r
@@ -133,27 +138,32 @@ public class SequenceFetcher extends ASequenceFetcher
             + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";\r
     if (argv != null && argv.length > 0)\r
     {\r
-      DbSourceProxy sp = new SequenceFetcher().getSourceProxy(argv[0]);\r
-      if (sp != null)\r
+      List<DbSourceProxy> sps = new SequenceFetcher()\r
+              .getSourceProxy(argv[0]);\r
+\r
+      if (sps != null)\r
       {\r
-        AlignmentI al = null;\r
-        try\r
-        {\r
-          al = sp.getSequenceRecords(argv[1]);\r
-        } catch (Exception e)\r
+        for (DbSourceProxy sp : sps)\r
         {\r
-          e.printStackTrace();\r
-          System.err.println("Error when retrieving " + argv[1] + " from "\r
-                  + argv[0] + "\nUsage: " + usage);\r
-        }\r
-        SequenceI[] prod = al.getSequencesArray();\r
-        if (al != null)\r
-        {\r
-          for (int p = 0; p < prod.length; p++)\r
+          AlignmentI al = null;\r
+          try\r
+          {\r
+            al = sp.getSequenceRecords(argv[1]);\r
+          } catch (Exception e)\r
           {\r
-            System.out.println("Prod " + p + ": "\r
-                    + prod[p].getDisplayId(true) + " : "\r
-                    + prod[p].getDescription());\r
+            e.printStackTrace();\r
+            System.err.println("Error when retrieving " + argv[1]\r
+                    + " from " + argv[0] + "\nUsage: " + usage);\r
+          }\r
+          SequenceI[] prod = al.getSequencesArray();\r
+          if (al != null)\r
+          {\r
+            for (int p = 0; p < prod.length; p++)\r
+            {\r
+              System.out.println("Prod " + p + ": "\r
+                      + prod[p].getDisplayId(true) + " : "\r
+                      + prod[p].getDescription());\r
+            }\r
           }\r
         }\r
         return;\r
@@ -165,6 +175,7 @@ public class SequenceFetcher extends ASequenceFetcher
                 + new SequenceFetcher().getSupportedDb());\r
       }\r
       System.out.println(usage);\r
+      return;\r
     }\r
     ASequenceFetcher sfetcher = new SequenceFetcher();\r
     String[] dbSources = sfetcher.getSupportedDb();\r
@@ -174,135 +185,141 @@ public class SequenceFetcher extends ASequenceFetcher
       // skip me\r
       if (db.equals(DBRefSource.PDB))\r
         continue;\r
-      DbSourceProxy sp = sfetcher.getSourceProxy(db);\r
-      System.out.println("Source: " + sp.getDbName() + " (" + db\r
-              + "): retrieving test:" + sp.getTestQuery());\r
-      AlignmentI al = null;\r
-      try\r
+      for (DbSourceProxy sp : sfetcher.getSourceProxy(db))\r
       {\r
-        al = sp.getSequenceRecords(sp.getTestQuery());\r
-        if (al != null && al.getHeight() > 0\r
-                && sp.getDbSourceProperties() != null)\r
+        System.out.println("Source: " + sp.getDbName() + " (" + db\r
+                + "): retrieving test:" + sp.getTestQuery());\r
+        AlignmentI al = null;\r
+        try\r
         {\r
-          boolean dna = sp.getDbSourceProperties().containsKey(\r
-                  DBRefSource.DNACODINGSEQDB)\r
-                  || sp.getDbSourceProperties().containsKey(\r
-                          DBRefSource.DNASEQDB)\r
-                  || sp.getDbSourceProperties().containsKey(\r
-                          DBRefSource.CODINGSEQDB);\r
-          // try and find products\r
-          String types[] = jalview.analysis.CrossRef.findSequenceXrefTypes(\r
-                  dna, al.getSequencesArray());\r
-          if (types != null)\r
+          al = sp.getSequenceRecords(sp.getTestQuery());\r
+          if (al != null && al.getHeight() > 0\r
+                  && sp.getDbSourceProperties() != null)\r
           {\r
-            System.out.println("Xref Types for: " + (dna ? "dna" : "prot"));\r
-            for (int t = 0; t < types.length; t++)\r
+            boolean dna = sp.getDbSourceProperties().containsKey(\r
+                    DBRefSource.DNACODINGSEQDB)\r
+                    || sp.getDbSourceProperties().containsKey(\r
+                            DBRefSource.DNASEQDB)\r
+                    || sp.getDbSourceProperties().containsKey(\r
+                            DBRefSource.CODINGSEQDB);\r
+            // try and find products\r
+            String types[] = jalview.analysis.CrossRef\r
+                    .findSequenceXrefTypes(dna, al.getSequencesArray());\r
+            if (types != null)\r
             {\r
-              System.out.println("Type: " + types[t]);\r
-              SequenceI[] prod = jalview.analysis.CrossRef\r
-                      .findXrefSequences(al.getSequencesArray(), dna,\r
-                              types[t]).getSequencesArray();\r
-              System.out.println("Found "\r
-                      + ((prod == null) ? "no" : "" + prod.length)\r
-                      + " products");\r
-              if (prod != null)\r
+              System.out.println("Xref Types for: "\r
+                      + (dna ? "dna" : "prot"));\r
+              for (int t = 0; t < types.length; t++)\r
               {\r
-                for (int p = 0; p < prod.length; p++)\r
+                System.out.println("Type: " + types[t]);\r
+                SequenceI[] prod = jalview.analysis.CrossRef\r
+                        .findXrefSequences(al.getSequencesArray(), dna,\r
+                                types[t]).getSequencesArray();\r
+                System.out.println("Found "\r
+                        + ((prod == null) ? "no" : "" + prod.length)\r
+                        + " products");\r
+                if (prod != null)\r
                 {\r
-                  System.out.println("Prod " + p + ": "\r
-                          + prod[p].getDisplayId(true));\r
+                  for (int p = 0; p < prod.length; p++)\r
+                  {\r
+                    System.out.println("Prod " + p + ": "\r
+                            + prod[p].getDisplayId(true));\r
+                  }\r
                 }\r
               }\r
             }\r
+            else\r
+            {\r
+              noProds.addElement((dna ? new Object[]\r
+              { al, al } : new Object[]\r
+              { al }));\r
+            }\r
+\r
           }\r
-          else\r
-          {\r
-            noProds.addElement((dna ? new Object[]\r
-            { al, al } : new Object[]\r
-            { al }));\r
-          }\r
+        } catch (Exception ex)\r
+        {\r
+          System.out.println("ERROR:Failed to retrieve test query.");\r
+          ex.printStackTrace(System.out);\r
+        }\r
 \r
+        if (al == null)\r
+        {\r
+          System.out.println("ERROR:No alignment retrieved.");\r
+          StringBuffer raw = sp.getRawRecords();\r
+          if (raw != null)\r
+            System.out.println(raw.toString());\r
+          else\r
+            System.out.println("ERROR:No Raw results.");\r
         }\r
-      } catch (Exception ex)\r
-      {\r
-        System.out.println("ERROR:Failed to retrieve test query.");\r
-        ex.printStackTrace(System.out);\r
-      }\r
-      if (al == null)\r
-      {\r
-        System.out.println("ERROR:No alignment retrieved.");\r
-        StringBuffer raw = sp.getRawRecords();\r
-        if (raw != null)\r
-          System.out.println(raw.toString());\r
         else\r
-          System.out.println("ERROR:No Raw results.");\r
-      }\r
-      else\r
-      {\r
-        System.out.println("Retrieved " + al.getHeight() + " sequences.");\r
-        for (int s = 0; s < al.getHeight(); s++)\r
         {\r
-          SequenceI sq = al.getSequenceAt(s);\r
-          while (sq.getDatasetSequence() != null)\r
+          System.out.println("Retrieved " + al.getHeight() + " sequences.");\r
+          for (int s = 0; s < al.getHeight(); s++)\r
           {\r
-            sq = sq.getDatasetSequence();\r
+            SequenceI sq = al.getSequenceAt(s);\r
+            while (sq.getDatasetSequence() != null)\r
+            {\r
+              sq = sq.getDatasetSequence();\r
 \r
-          }\r
-          if (ds == null)\r
-          {\r
-            ds = new Alignment(new SequenceI[]\r
-            { sq });\r
+            }\r
+            if (ds == null)\r
+            {\r
+              ds = new Alignment(new SequenceI[]\r
+              { sq });\r
 \r
-          }\r
-          else\r
-          {\r
-            ds.addSequence(sq);\r
+            }\r
+            else\r
+            {\r
+              ds.addSequence(sq);\r
+            }\r
           }\r
         }\r
-      }\r
-      System.out.flush();\r
-      System.err.flush();\r
-\r
-    }\r
-    if (noProds.size() > 0)\r
-    {\r
-      Enumeration ts = noProds.elements();\r
-      while (ts.hasMoreElements())\r
+        System.out.flush();\r
+        System.err.flush();\r
 \r
+      }\r
+      if (noProds.size() > 0)\r
       {\r
-        Object[] typeSq = (Object[]) ts.nextElement();\r
-        boolean dna = (typeSq.length > 1);\r
-        AlignmentI al = (AlignmentI) typeSq[0];\r
-        System.out.println("Trying getProducts for "\r
-                + al.getSequenceAt(0).getDisplayId(true));\r
-        System.out.println("Search DS Xref for: " + (dna ? "dna" : "prot"));\r
-        // have a bash at finding the products amongst all the retrieved\r
-        // sequences.\r
-        SequenceI[] seqs = al.getSequencesArray();\r
-        Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(\r
-                seqs, dna, null, ds);\r
-        System.out.println("Found "\r
-                + ((prodal == null) ? "no" : "" + prodal.getHeight())\r
-                + " products");\r
-        if (prodal != null)\r
+        Enumeration ts = noProds.elements();\r
+        while (ts.hasMoreElements())\r
+\r
         {\r
-          SequenceI[] prod = prodal.getSequencesArray(); // note\r
-          // should\r
-          // test\r
-          // rather\r
-          // than\r
-          // throw\r
-          // away\r
-          // codon\r
-          // mapping\r
-          // (if\r
-          // present)\r
-          for (int p = 0; p < prod.length; p++)\r
+          Object[] typeSq = (Object[]) ts.nextElement();\r
+          boolean dna = (typeSq.length > 1);\r
+          AlignmentI al = (AlignmentI) typeSq[0];\r
+          System.out.println("Trying getProducts for "\r
+                  + al.getSequenceAt(0).getDisplayId(true));\r
+          System.out.println("Search DS Xref for: "\r
+                  + (dna ? "dna" : "prot"));\r
+          // have a bash at finding the products amongst all the retrieved\r
+          // sequences.\r
+          SequenceI[] seqs = al.getSequencesArray();\r
+          Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(\r
+                  seqs, dna, null, ds);\r
+          System.out.println("Found "\r
+                  + ((prodal == null) ? "no" : "" + prodal.getHeight())\r
+                  + " products");\r
+          if (prodal != null)\r
           {\r
-            System.out.println("Prod " + p + ": "\r
-                    + prod[p].getDisplayId(true));\r
+            SequenceI[] prod = prodal.getSequencesArray(); // note\r
+            // should\r
+            // test\r
+            // rather\r
+            // than\r
+            // throw\r
+            // away\r
+            // codon\r
+            // mapping\r
+            // (if\r
+            // present)\r
+            for (int p = 0; p < prod.length; p++)\r
+            {\r
+              System.out.println("Prod " + p + ": "\r
+                      + prod[p].getDisplayId(true));\r
+            }\r
           }\r
         }\r
+\r
       }\r
 \r
     }\r
@@ -317,7 +334,8 @@ public class SequenceFetcher extends ASequenceFetcher
   {\r
     // TODO: define a context as a registry provider (either desktop,\r
     // jalview.bin.cache, or something else).\r
-    for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry().getSources())\r
+    for (jalviewSourceI source : jalview.bin.Cache.getDasSourceRegistry()\r
+            .getSources())\r
     {\r
       if (source.isSequenceSource())\r
       {\r