JalviewAppLoader for moving code from JalviewLite relating to applet
[jalview.git] / src / jalview / bin / JalviewLite.java
index e483fce..5cb599f 100644 (file)
@@ -21,6 +21,8 @@
 package jalview.bin;
 
 import jalview.analysis.AlignmentUtils;
+import jalview.api.AlignViewportI;
+import jalview.api.JalviewApp;
 import jalview.api.StructureSelectionManagerProvider;
 import jalview.appletgui.AlignFrame;
 import jalview.appletgui.AlignViewport;
@@ -32,7 +34,6 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.PDBEntry;
-import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.io.AnnotationFile;
@@ -42,8 +43,6 @@ import jalview.io.FileFormatI;
 import jalview.io.FileFormats;
 import jalview.io.FileParse;
 import jalview.io.IdentifyFile;
-import jalview.io.JPredFile;
-import jalview.io.JnetAnnotationMaker;
 import jalview.io.NewickFile;
 import jalview.javascript.JSFunctionExec;
 import jalview.javascript.JalviewLiteJsApi;
@@ -52,7 +51,6 @@ import jalview.javascript.MouseOverStructureListener;
 import jalview.structure.SelectionListener;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.ColorUtils;
-import jalview.util.HttpUtils;
 import jalview.util.MessageManager;
 
 import java.applet.Applet;
@@ -73,7 +71,6 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
-import java.util.StringTokenizer;
 import java.util.Vector;
 
 import netscape.javascript.JSObject;
@@ -85,9 +82,14 @@ import netscape.javascript.JSObject;
  * @version $Revision: 1.92 $
  */
 public class JalviewLite extends Applet
-        implements StructureSelectionManagerProvider, JalviewLiteJsApi
+        implements StructureSelectionManagerProvider, JalviewLiteJsApi,
+        JalviewApp
 {
 
+  public JalviewLite()
+  {
+    appLoader = new JalviewAppLoader(debug);
+  }
   private static final String TRUE = "true";
 
   private static final String FALSE = "false";
@@ -294,8 +296,8 @@ public class JalviewLite extends Applet
       }
     }
     // deparse fields
-    String[] ids = separatorListToArray(sequenceIds, sep);
-    String[] cols = separatorListToArray(columns, sep);
+    String[] ids = JalviewAppLoader.separatorListToArray(sequenceIds, sep);
+    String[] cols = JalviewAppLoader.separatorListToArray(columns, sep);
     final SequenceGroup sel = new SequenceGroup();
     final ColumnSelection csel = new ColumnSelection();
     AlignmentI al = alf.viewport.getAlignment();
@@ -618,7 +620,7 @@ public class JalviewLite extends Applet
   public String orderAlignmentBy(AlignFrame alf, String order,
           String undoName, String sep)
   {
-    String[] ids = separatorListToArray(order, sep);
+    String[] ids = JalviewAppLoader.separatorListToArray(order, sep);
     SequenceI[] sqs = null;
     if (ids != null && ids.length > 0)
     {
@@ -1338,6 +1340,10 @@ public class JalviewLite extends Applet
    */
   public boolean useXtrnalSviewer = false;
 
+  public JalviewAppLoader appLoader;
+
+  public AlignFrame loaderFrame;
+
   public static boolean debug = false;
 
   static String builddate = null, version = null, installation = null;
@@ -1836,7 +1842,7 @@ public class JalviewLite extends Applet
 
     JalviewLite applet;
 
-    private void dbgMsg(String msg)
+    public void dbgMsg(String msg)
     {
       if (JalviewLite.debug)
       {
@@ -1853,67 +1859,10 @@ public class JalviewLite extends Applet
      */
     public String resolveFileProtocol(String path)
     {
-      /*
-       * is it paste data?
-       */
-      if (path.startsWith("PASTE"))
-      {
-        protocol = DataSourceType.PASTE;
-        return path.substring(5);
-      }
-
-      /*
-       * is it a URL?
-       */
-      if (path.indexOf("://") != -1)
-      {
-        protocol = DataSourceType.URL;
-        return path;
-      }
-
-      /*
-       * try relative to document root
-       */
-      URL documentBase = getDocumentBase();
-      String withDocBase = resolveUrlForLocalOrAbsolute(path, documentBase);
-      if (HttpUtils.isValidUrl(withDocBase))
-      {
-        if (debug)
-        {
-          System.err.println("Prepended document base '" + documentBase
-                  + "' to make: '" + withDocBase + "'");
-        }
-        protocol = DataSourceType.URL;
-        return withDocBase;
-      }
-
-      /*
-       * try relative to codebase (if different to document base)
-       */
-      URL codeBase = getCodeBase();
-      String withCodeBase = applet.resolveUrlForLocalOrAbsolute(path,
-              codeBase);
-      if (!withCodeBase.equals(withDocBase)
-              && HttpUtils.isValidUrl(withCodeBase))
-      {
-        protocol = DataSourceType.URL;
-        if (debug)
-        {
-          System.err.println("Prepended codebase '" + codeBase
-                  + "' to make: '" + withCodeBase + "'");
-        }
-        return withCodeBase;
-      }
 
-      /*
-       * try locating by classloader; try this last so files in the directory
-       * are resolved using document base
-       */
-      if (inArchive(path))
-      {
-        protocol = DataSourceType.CLASSLOADER;
-      }
-      return path;
+      String[] ret = new String[] { path };
+      protocol = JalviewAppLoader.resolveFileProtocol(applet, ret);
+      return ret[0];
     }
 
     public LoadingThread(String file, String file2, JalviewLite _applet)
@@ -1958,17 +1907,8 @@ public class JalviewLite extends Applet
       if (newAlignFrame != null)
       {
         addToDisplay(newAlignFrame, newAlignFrame2);
-        loadTree(newAlignFrame);
-
-        loadScoreFile(newAlignFrame);
-
-        loadFeatures(newAlignFrame);
-
-        loadAnnotations(newAlignFrame);
-
-        loadJnetFile(newAlignFrame);
-
-        loadPdbFiles(newAlignFrame);
+        applet.loaderFrame = newAlignFrame;
+        appLoader.load(applet);
       }
       else
       {
@@ -2089,392 +2029,6 @@ public class JalviewLite extends Applet
       return null;
     }
 
-    /**
-     * Load PDBFiles if any specified by parameter(s). Returns true if loaded,
-     * else false.
-     * 
-     * @param alignFrame
-     * @return
-     */
-    protected boolean loadPdbFiles(AlignFrame alignFrame)
-    {
-      boolean result = false;
-      /*
-       * <param name="alignpdbfiles" value="false/true"/> Undocumented for 2.6 -
-       * related to JAL-434
-       */
-
-      applet.setAlignPdbStructures(
-              getDefaultParameter("alignpdbfiles", false));
-      /*
-       * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
-       * PDB|1GAQ|1GAQ|C">
-       * 
-       * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
-       * 
-       * <param name="PDBfile3" value="1q0o Q45135_9MICO">
-       */
-
-      int pdbFileCount = 0;
-      // Accumulate pdbs here if they are heading for the same view (if
-      // alignPdbStructures is true)
-      Vector pdbs = new Vector();
-      // create a lazy matcher if we're asked to
-      jalview.analysis.SequenceIdMatcher matcher = (applet
-              .getDefaultParameter("relaxedidmatch", false))
-                      ? new jalview.analysis.SequenceIdMatcher(
-                              alignFrame.getAlignViewport().getAlignment()
-                                      .getSequencesArray())
-                      : null;
-
-      String param;
-      do
-      {
-        if (pdbFileCount > 0)
-        {
-          param = applet.getParameter("PDBFILE" + pdbFileCount);
-        }
-        else
-        {
-          param = applet.getParameter("PDBFILE");
-        }
-
-        if (param != null)
-        {
-          PDBEntry pdb = new PDBEntry();
-
-          String seqstring;
-          SequenceI[] seqs = null;
-          String[] chains = null;
-
-          StringTokenizer st = new StringTokenizer(param, " ");
-
-          if (st.countTokens() < 2)
-          {
-            String sequence = applet.getParameter("PDBSEQ");
-            if (sequence != null)
-            {
-              seqs = new SequenceI[] { matcher == null
-                      ? (Sequence) alignFrame.getAlignViewport()
-                              .getAlignment().findName(sequence)
-                      : matcher.findIdMatch(sequence) };
-            }
-
-          }
-          else
-          {
-            param = st.nextToken();
-            List<SequenceI> tmp = new ArrayList<>();
-            List<String> tmp2 = new ArrayList<>();
-
-            while (st.hasMoreTokens())
-            {
-              seqstring = st.nextToken();
-              StringTokenizer st2 = new StringTokenizer(seqstring, "=");
-              if (st2.countTokens() > 1)
-              {
-                // This is the chain
-                tmp2.add(st2.nextToken());
-                seqstring = st2.nextToken();
-              }
-              tmp.add(matcher == null
-                      ? (Sequence) alignFrame.getAlignViewport()
-                              .getAlignment().findName(seqstring)
-                      : matcher.findIdMatch(seqstring));
-            }
-
-            seqs = tmp.toArray(new SequenceI[tmp.size()]);
-            if (tmp2.size() == tmp.size())
-            {
-              chains = tmp2.toArray(new String[tmp2.size()]);
-            }
-          }
-          param = resolveFileProtocol(param);
-          // TODO check JAL-357 for files in a jar (CLASSLOADER)
-          pdb.setFile(param);
-
-          if (seqs != null)
-          {
-            for (int i = 0; i < seqs.length; i++)
-            {
-              if (seqs[i] != null)
-              {
-                ((Sequence) seqs[i]).addPDBId(pdb);
-                StructureSelectionManager
-                        .getStructureSelectionManager(applet)
-                        .registerPDBEntry(pdb);
-              }
-              else
-              {
-                if (JalviewLite.debug)
-                {
-                  // this may not really be a problem but we give a warning
-                  // anyway
-                  System.err.println(
-                          "Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
-                                  + i + ")");
-                }
-              }
-            }
-
-            if (!alignPdbStructures)
-            {
-              alignFrame.newStructureView(applet, pdb, seqs, chains,
-                      protocol);
-            }
-            else
-            {
-              pdbs.addElement(new Object[] { pdb, seqs, chains, protocol });
-            }
-          }
-        }
-
-        pdbFileCount++;
-      } while (param != null || pdbFileCount < 10);
-      if (pdbs.size() > 0)
-      {
-        SequenceI[][] seqs = new SequenceI[pdbs.size()][];
-        PDBEntry[] pdb = new PDBEntry[pdbs.size()];
-        String[][] chains = new String[pdbs.size()][];
-        String[] protocols = new String[pdbs.size()];
-        for (int pdbsi = 0, pdbsiSize = pdbs
-                .size(); pdbsi < pdbsiSize; pdbsi++)
-        {
-          Object[] o = (Object[]) pdbs.elementAt(pdbsi);
-          pdb[pdbsi] = (PDBEntry) o[0];
-          seqs[pdbsi] = (SequenceI[]) o[1];
-          chains[pdbsi] = (String[]) o[2];
-          protocols[pdbsi] = (String) o[3];
-        }
-        alignFrame.alignedStructureView(applet, pdb, seqs, chains,
-                protocols);
-        result = true;
-      }
-      return result;
-    }
-
-    /**
-     * Load in a Jnetfile if specified by parameter. Returns true if loaded,
-     * else false.
-     * 
-     * @param alignFrame
-     * @return
-     */
-    protected boolean loadJnetFile(AlignFrame alignFrame)
-    {
-      boolean result = false;
-      String param = applet.getParameter("jnetfile");
-      if (param == null)
-      {
-        // jnet became jpred around 2016
-        param = applet.getParameter("jpredfile");
-      }
-      if (param != null)
-      {
-        try
-        {
-          param = resolveFileProtocol(param);
-          JPredFile predictions = new JPredFile(param, protocol);
-          JnetAnnotationMaker.add_annotation(predictions,
-                  alignFrame.viewport.getAlignment(), 0, false);
-          // false == do not add sequence profile from concise output
-
-          alignFrame.viewport.getAlignment().setupJPredAlignment();
-
-          alignFrame.alignPanel.fontChanged();
-          alignFrame.alignPanel.setScrollValues(0, 0);
-          result = true;
-        } catch (Exception ex)
-        {
-          ex.printStackTrace();
-        }
-      }
-      return result;
-    }
-
-    /**
-     * Load annotations if specified by parameter. Returns true if loaded, else
-     * false.
-     * 
-     * @param alignFrame
-     * @return
-     */
-    protected boolean loadAnnotations(AlignFrame alignFrame)
-    {
-      boolean result = false;
-      String param = applet.getParameter("annotations");
-      if (param != null)
-      {
-        param = resolveFileProtocol(param);
-
-        if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
-                param, protocol))
-        {
-          alignFrame.alignPanel.fontChanged();
-          alignFrame.alignPanel.setScrollValues(0, 0);
-          result = true;
-        }
-        else
-        {
-          System.err.println(
-                  "Annotations were not added from annotation file '"
-                          + param + "'");
-        }
-      }
-      return result;
-    }
-
-    /**
-     * Load features file and view settings as specified by parameters. Returns
-     * true if features were loaded, else false.
-     * 
-     * @param alignFrame
-     * @return
-     */
-    protected boolean loadFeatures(AlignFrame alignFrame)
-    {
-      boolean result = false;
-      // ///////////////////////////
-      // modify display of features
-      // we do this before any features have been loaded, ensuring any hidden
-      // groups are hidden when features first displayed
-      //
-      // hide specific groups
-      //
-      String param = applet.getParameter("hidefeaturegroups");
-      if (param != null)
-      {
-        alignFrame.setFeatureGroupState(separatorListToArray(param), false);
-        // applet.setFeatureGroupStateOn(newAlignFrame, param, false);
-      }
-      // show specific groups
-      param = applet.getParameter("showfeaturegroups");
-      if (param != null)
-      {
-        alignFrame.setFeatureGroupState(separatorListToArray(param), true);
-        // applet.setFeatureGroupStateOn(newAlignFrame, param, true);
-      }
-      // and now load features
-      param = applet.getParameter("features");
-      if (param != null)
-      {
-        param = resolveFileProtocol(param);
-
-        result = alignFrame.parseFeaturesFile(param, protocol);
-      }
-
-      param = applet.getParameter("showFeatureSettings");
-      if (param != null && param.equalsIgnoreCase(TRUE))
-      {
-        alignFrame.viewport.setShowSequenceFeatures(true);
-        new FeatureSettings(alignFrame.alignPanel);
-      }
-      return result;
-    }
-
-    /**
-     * Load a score file if specified by parameter. Returns true if file was
-     * loaded, else false.
-     * 
-     * @param alignFrame
-     */
-    protected boolean loadScoreFile(AlignFrame alignFrame)
-    {
-      boolean result = false;
-      String sScoreFile = applet.getParameter("scoreFile");
-      if (sScoreFile != null && !"".equals(sScoreFile))
-      {
-        try
-        {
-          if (debug)
-          {
-            System.err.println(
-                    "Attempting to load T-COFFEE score file from the scoreFile parameter");
-          }
-          result = alignFrame.loadScoreFile(sScoreFile);
-          if (!result)
-          {
-            System.err.println(
-                    "Failed to parse T-COFFEE parameter as a valid score file ('"
-                            + sScoreFile + "')");
-          }
-        } catch (Exception e)
-        {
-          System.err.printf("Cannot read score file: '%s'. Cause: %s \n",
-                  sScoreFile, e.getMessage());
-        }
-      }
-      return result;
-    }
-
-    /**
-     * Load a tree for the alignment if specified by parameter. Returns true if
-     * a tree was loaded, else false.
-     * 
-     * @param alignFrame
-     * @return
-     */
-    protected boolean loadTree(AlignFrame alignFrame)
-    {
-      boolean result = false;
-      String treeFile = applet.getParameter("tree");
-      if (treeFile == null)
-      {
-        treeFile = applet.getParameter("treeFile");
-      }
-
-      if (treeFile != null)
-      {
-        try
-        {
-          treeFile = resolveFileProtocol(treeFile);
-          NewickFile fin = new NewickFile(treeFile, protocol);
-          fin.parse();
-
-          if (fin.getTree() != null)
-          {
-            alignFrame.loadTree(fin, treeFile);
-            result = true;
-            dbgMsg("Successfully imported tree.");
-          }
-          else
-          {
-            dbgMsg("Tree parameter did not resolve to a valid tree.");
-          }
-        } catch (Exception ex)
-        {
-          ex.printStackTrace();
-        }
-      }
-      return result;
-    }
-
-    /**
-     * Discovers whether the given file is in the Applet Archive
-     * 
-     * @param f
-     *          String
-     * @return boolean
-     */
-    boolean inArchive(String f)
-    {
-      // This might throw a security exception in certain browsers
-      // Netscape Communicator for instance.
-      try
-      {
-        boolean rtn = (getClass().getResourceAsStream("/" + f) != null);
-        if (debug)
-        {
-          System.err.println("Resource '" + f + "' was "
-                  + (rtn ? "" : "not ") + "located by classloader.");
-        }
-        return rtn;
-      } catch (Exception ex)
-      {
-        System.out.println("Exception checking resources: " + f + " " + ex);
-        return false;
-      }
-    }
   }
 
   /**
@@ -2517,64 +2071,7 @@ public class JalviewLite extends Applet
    */
   public String[] separatorListToArray(String list)
   {
-    return separatorListToArray(list, separator);
-  }
-
-  /**
-   * parse the string into a list
-   * 
-   * @param list
-   * @param separator
-   * @return elements separated by separator
-   */
-  public static String[] separatorListToArray(String list, String separator)
-  {
-    // TODO use StringUtils version (slightly different...)
-    int seplen = separator.length();
-    if (list == null || list.equals("") || list.equals(separator))
-    {
-      return null;
-    }
-    java.util.Vector jv = new Vector();
-    int cp = 0, pos;
-    while ((pos = list.indexOf(separator, cp)) > cp)
-    {
-      jv.addElement(list.substring(cp, pos));
-      cp = pos + seplen;
-    }
-    if (cp < list.length())
-    {
-      String c = list.substring(cp);
-      if (!c.equals(separator))
-      {
-        jv.addElement(c);
-      }
-    }
-    if (jv.size() > 0)
-    {
-      String[] v = new String[jv.size()];
-      for (int i = 0; i < v.length; i++)
-      {
-        v[i] = (String) jv.elementAt(i);
-      }
-      jv.removeAllElements();
-      if (debug)
-      {
-        System.err.println("Array from '" + separator
-                + "' separated List:\n" + v.length);
-        for (int i = 0; i < v.length; i++)
-        {
-          System.err.println("item " + i + " '" + v[i] + "'");
-        }
-      }
-      return v;
-    }
-    if (debug)
-    {
-      System.err.println(
-              "Empty Array from '" + separator + "' separated List");
-    }
-    return null;
+    return JalviewAppLoader.separatorListToArray(list, separator);
   }
 
   /**
@@ -2755,6 +2252,7 @@ public class JalviewLite extends Applet
    *          the value to return otherwise
    * @return true or false
    */
+  @Override
   public boolean getDefaultParameter(String name, boolean def)
   {
     String stn;
@@ -2782,7 +2280,8 @@ public class JalviewLite extends Applet
     return alFrame.addPdbFile(sequenceId, pdbEntryString, pdbFile);
   }
 
-  protected void setAlignPdbStructures(boolean alignPdbStructures)
+  @Override
+  public void setAlignPdbStructures(boolean alignPdbStructures)
   {
     this.alignPdbStructures = alignPdbStructures;
   }
@@ -2914,66 +2413,6 @@ public class JalviewLite extends Applet
   }
 
   /**
-   * form a complete URL given a path to a resource and a reference location on
-   * the same server
-   * 
-   * @param targetPath
-   *          - an absolute path on the same server as localref or a document
-   *          located relative to localref
-   * @param localref
-   *          - a URL on the same server as url
-   * @return a complete URL for the resource located by url
-   */
-  private String resolveUrlForLocalOrAbsolute(String targetPath,
-          URL localref)
-  {
-    String resolvedPath = "";
-    if (targetPath.startsWith("/"))
-    {
-      String codebase = localref.toString();
-      String localfile = localref.getFile();
-      resolvedPath = codebase.substring(0,
-              codebase.length() - localfile.length()) + targetPath;
-      return resolvedPath;
-    }
-
-    /*
-     * get URL path and strip off any trailing file e.g.
-     * www.jalview.org/examples/index.html#applets?a=b is trimmed to
-     * www.jalview.org/examples/
-     */
-    String urlPath = localref.toString();
-    String directoryPath = urlPath;
-    int lastSeparator = directoryPath.lastIndexOf("/");
-    if (lastSeparator > 0)
-    {
-      directoryPath = directoryPath.substring(0, lastSeparator + 1);
-    }
-
-    if (targetPath.startsWith("/"))
-    {
-      /*
-       * construct absolute URL to a file on the server - this is not allowed?
-       */
-      // String localfile = localref.getFile();
-      // resolvedPath = urlPath.substring(0,
-      // urlPath.length() - localfile.length())
-      // + targetPath;
-      resolvedPath = directoryPath + targetPath.substring(1);
-    }
-    else
-    {
-      resolvedPath = directoryPath + targetPath;
-    }
-    if (debug)
-    {
-      System.err.println(
-              "resolveUrlForLocalOrAbsolute returning " + resolvedPath);
-    }
-    return resolvedPath;
-  }
-
-  /**
    * open a URL in the browser - resolving it according to relative refs and
    * coping with javascript: protocol if necessary.
    * 
@@ -2990,7 +2429,7 @@ public class JalviewLite extends Applet
         // form valid URL
         // Should really use docbase, not codebase.
         URL prepend;
-        url = resolveUrlForLocalOrAbsolute(url,
+        url = JalviewAppLoader.resolveUrlForLocalOrAbsolute(url,
                 prepend = getDefaultParameter("resolvetocodebase", false)
                         ? getCodeBase()
                         : getDocumentBase());
@@ -3023,6 +2462,65 @@ public class JalviewLite extends Applet
     }
   }
 
+  @Override
+  public AlignViewportI getViewport()
+  {
+    return loaderFrame.getAlignViewport();
+  }
+
+  @Override
+  public void newStructureView(PDBEntry pdb, SequenceI[] seqs,
+          String[] chains, DataSourceType protocol)
+  {
+    loaderFrame.newStructureView(this, pdb, seqs, chains,
+            protocol);
+  }
+
+  @Override
+  public void alignedStructureView(PDBEntry[] pdb, SequenceI[][] seqs,
+          String[][] chains, String[] protocols)
+  {
+    loaderFrame.alignedStructureView(this, pdb, seqs, chains, protocols);
+  }
+
+  @Override
+  public void updateForLoader()
+  {
+    loaderFrame.alignPanel.fontChanged();
+    loaderFrame.alignPanel.setScrollValues(0, 0);
+  }
+
+  @Override
+  public void setFeatureGroupState(String[] groups, boolean state)
+  {
+    loaderFrame.setFeatureGroupState(groups, state);
+  }
+
+  @Override
+  public boolean parseFeaturesFile(String param, DataSourceType protocol)
+  {
+    return loaderFrame.parseFeaturesFile(param, protocol);
+  }
+
+  @Override
+  public void newFeatureSettings()
+  {
+    getViewport().setShowSequenceFeatures(true);
+    new FeatureSettings(loaderFrame.alignPanel);
+  }
+
+  @Override
+  public boolean loadScoreFile(String sScoreFile) throws IOException
+  {
+    return loaderFrame.loadScoreFile(sScoreFile);
+  }
+
+  @Override
+  public void loadTree(NewickFile tree, String treeFile) throws IOException
+  {
+    loaderFrame.loadTree(tree, treeFile);
+  }
+
   /**
    * bind structures in a viewer to any matching sequences in an alignFrame (use
    * sequenceIds to limit scope of search to specific sequences)