JAL-1684 align added alignment to match original in split frame
[jalview.git] / src / jalview / bin / JalviewLite.java
index 8a9c54b..7112381 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.bin;
 
+import jalview.analysis.AlignmentUtils;
 import jalview.api.StructureSelectionManagerProvider;
 import jalview.appletgui.AlignFrame;
 import jalview.appletgui.AlignViewport;
@@ -91,7 +92,7 @@ public class JalviewLite extends Applet implements
   }
 
   // /////////////////////////////////////////
-  // The following public methods maybe called
+  // The following public methods may be called
   // externally, eg via javascript in HTML page
   /*
    * (non-Javadoc)
@@ -955,11 +956,16 @@ public class JalviewLite extends Applet implements
     }
   }
 
-  /*
-   * (non-Javadoc)
-   * 
+  /**
+   * Callable from javascript to register a javascript function to pass events
+   * to a structure viewer.
+   *
+   * @param listener
+   *          the name of a javascript function
+   * @param modelSet
+   *          a token separated list of PDB file names listened for
    * @see jalview.bin.JalviewLiteJsApi#setStructureListener(java.lang.String,
-   * java.lang.String)
+   *      java.lang.String)
    */
   public void setStructureListener(String listener, String modelSet)
   {
@@ -1288,7 +1294,7 @@ public class JalviewLite extends Applet implements
     {
       builddate = "unknown";
       version = "test";
-      installation = "Webstart";
+      installation = "applet";
       java.net.URL url = JalviewLite.class
               .getResource("/.build_properties");
       if (url != null)
@@ -1344,9 +1350,10 @@ public class JalviewLite extends Applet implements
   /**
    * init method for Jalview Applet
    */
+  @Override
   public void init()
   {
-    // remove any handlers that might be hanging around from an earlier instance
+    debug = TRUE.equalsIgnoreCase(getParameter("debug"));
     try
     {
       if (debug)
@@ -1369,23 +1376,17 @@ public class JalviewLite extends Applet implements
         ex.printStackTrace();
       }
     }
-    /**
-     * turn on extra applet debugging
-     */
-    debug = TRUE.equalsIgnoreCase(getParameter("debug"));
+
     if (debug)
     {
-
       System.err.println("JalviewLite Version " + getVersion());
       System.err.println("Build Date : " + getBuildDate());
       System.err.println("Installation : " + getInstallation());
-
     }
     String externalsviewer = getParameter("externalstructureviewer");
     if (externalsviewer != null)
     {
-      useXtrnalSviewer = externalsviewer.trim().toLowerCase()
-              .equals(TRUE);
+      useXtrnalSviewer = externalsviewer.trim().toLowerCase().equals(TRUE);
     }
     /**
      * if true disable the check for jmol
@@ -1411,7 +1412,9 @@ public class JalviewLite extends Applet implements
       }
       else
       {
-        throw new Error(MessageManager.getString("error.invalid_separator_parameter"));
+        throw new Error(
+                MessageManager
+                        .getString("error.invalid_separator_parameter"));
       }
     }
     int r = 255;
@@ -1458,7 +1461,7 @@ public class JalviewLite extends Applet implements
         file = data.toString();
       }
     }
-    if (TRUE.equalsIgnoreCase(getParameter("enableSplitFrame")))
+    if (getDefaultParameter("enableSplitFrame", true))
     {
       file2 = getParameter("file2");
     }
@@ -1879,15 +1882,30 @@ public class JalviewLite extends Applet implements
      */
     public void addToDisplay(AlignFrame af, AlignFrame af2)
     {
-      if (af2 == null)
-      {
-        af.addToDisplay(embedded);
-      }
-      else
+      if (af2 != null)
       {
-        SplitFrame sf = new SplitFrame(af, af2);
-        sf.addToDisplay(embedded, JalviewLite.this);
+        AlignmentI al1 = af.viewport.getAlignment();
+        AlignmentI al2 = af2.viewport.getAlignment();
+        AlignmentI cdna = al1.isNucleotide() ? al1 : al2;
+        AlignmentI prot = al1.isNucleotide() ? al2 : al1;
+        if (AlignmentUtils.mapProteinAlignmentToCdna(prot, cdna))
+        {
+          al2.alignAs(al1);
+          SplitFrame sf = new SplitFrame(af, af2);
+          sf.addToDisplay(embedded, JalviewLite.this);
+          return;
+        }
+        else
+        {
+          String msg = "Could not map any sequence in " + af2.getTitle()
+                  + " as "
+                  + (al1.isNucleotide() ? "protein product" : "cDNA")
+                  + " for " + af.getTitle();
+          System.err.println(msg);
+        }
       }
+
+      af.addToDisplay(embedded);
     }
 
     /**
@@ -1908,7 +1926,8 @@ public class JalviewLite extends Applet implements
       AlignmentI al = null;
       try
       {
-        al = new AppletFormatAdapter().readFile(resolvedFile, protocol, format);
+        al = new AppletFormatAdapter().readFile(resolvedFile, protocol,
+                format);
         if ((al != null) && (al.getHeight() > 0))
         {
           dbgMsg("Successfully loaded file.");
@@ -1926,13 +1945,13 @@ public class JalviewLite extends Applet implements
           if (protocol == AppletFormatAdapter.PASTE)
           {
             newAlignFrame.setTitle(MessageManager.formatMessage(
-                    "label.sequences_from", new Object[]
-                    { applet.getDocumentBase().toString() }));
+                    "label.sequences_from", new Object[] { applet
+                            .getDocumentBase().toString() }));
           }
 
           newAlignFrame.statusBar.setText(MessageManager.formatMessage(
-                  "label.successfully_loaded_file", new Object[]
-                  { resolvedFile }));
+                  "label.successfully_loaded_file",
+                  new Object[] { resolvedFile }));
 
           return newAlignFrame;
         }
@@ -2027,10 +2046,9 @@ public class JalviewLite extends Applet implements
             String sequence = applet.getParameter("PDBSEQ");
             if (sequence != null)
             {
-              seqs = new SequenceI[]
-              { matcher == null ? (Sequence) alignFrame.getAlignViewport()
-                      .getAlignment().findName(sequence) : matcher
-                      .findIdMatch(sequence) };
+              seqs = new SequenceI[] { matcher == null ? (Sequence) alignFrame
+                      .getAlignViewport().getAlignment().findName(sequence)
+                      : matcher.findIdMatch(sequence) };
             }
 
           }
@@ -2112,8 +2130,8 @@ public class JalviewLite extends Applet implements
             }
             else
             {
-              pdbs.addElement(new Object[]
-              { pdb, seqs, chains, new String(protocol) });
+              pdbs.addElement(new Object[] { pdb, seqs, chains,
+                  new String(protocol) });
             }
           }
         }
@@ -2338,26 +2356,26 @@ public class JalviewLite extends Applet implements
     /**
      * Discovers whether the given file is in the Applet Archive
      * 
-     * @param file
+     * @param f
      *          String
      * @return boolean
      */
-    boolean inArchive(String file)
+    boolean inArchive(String f)
     {
       // This might throw a security exception in certain browsers
       // Netscape Communicator for instance.
       try
       {
-        boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
+        boolean rtn = (getClass().getResourceAsStream("/" + f) != null);
         if (debug)
         {
-          System.err.println("Resource '" + file + "' was "
-                  + (rtn ? "" : "not") + " located by classloader.");
+          System.err.println("Resource '" + f + "' was "
+                  + (rtn ? "" : "not ") + "located by classloader.");
         }
         return rtn;
       } catch (Exception ex)
       {
-        System.out.println("Exception checking resources: " + file + " "
+        System.out.println("Exception checking resources: " + f + " "
                 + ex);
         return false;
       }
@@ -2367,47 +2385,48 @@ public class JalviewLite extends Applet implements
      * If the file is not already in URL format, tries to locate it by resolving
      * as a URL.
      * 
-     * @param file
+     * @param f
      * @return
      */
-    String addProtocol(final String file)
+    String addProtocol(final String f)
     {
-      if (file.indexOf("://") == -1)
+      if (f.indexOf("://") != -1)
       {
-        /*
-         * Try relative to document base
-         */
-        String url = applet.resolveUrlForLocalOrAbsolute(file,
-                getDocumentBase());
-        if (urlExists(url))
-        {
-          if (debug)
-          {
-            System.err.println("Prepended document base for resource: '"
-                    + file + "'");
-          }
-          return url;
-        }
+        // already has URL format
+        return f;
+      }
 
-        /*
-         * Try relative to codebase
-         */
-        url = applet.resolveUrlForLocalOrAbsolute(file, getCodeBase());
-        if (urlExists(url))
+      /*
+       * Try relative to document base
+       */
+      URL documentBase = getDocumentBase();
+      String url = applet.resolveUrlForLocalOrAbsolute(f, documentBase);
+      if (urlExists(url))
+      {
+        if (debug)
         {
-          if (debug)
-          {
-            System.err.println("Prepended codebase for resource: '" + file
-                    + "'");
-          }
-          return url;
+          System.err.println("Prepended document base '" + documentBase
+                  + "' to make: '" + url + "'");
         }
+        return url;
       }
 
       /*
-       * Not resolved, leave unchanged
+       * Try relative to codebase
        */
-      return file;
+      URL codeBase = getCodeBase();
+      url = applet.resolveUrlForLocalOrAbsolute(f, codeBase);
+      if (urlExists(url))
+      {
+        if (debug)
+        {
+          System.err.println("Prepended codebase '" + codeBase
+                  + "' to make: '" + url + "'");
+        }
+        return url;
+      }
+
+      return f;
     }
 
     /**
@@ -2497,9 +2516,9 @@ public class JalviewLite extends Applet implements
    * @param separator
    * @return elements separated by separator
    */
-  public String[] separatorListToArray(String list, String separator)
+  public static String[] separatorListToArray(String list, String separator)
   {
-    // note separator local variable intentionally masks object field
+    // TODO use StringUtils version (slightly different...)
     int seplen = separator.length();
     if (list == null || list.equals("") || list.equals(separator))
     {
@@ -2565,8 +2584,9 @@ public class JalviewLite extends Applet implements
    * @param separator
    * @return concatenated string
    */
-  public String arrayToSeparatorList(String[] list, String separator)
+  public static String arrayToSeparatorList(String[] list, String separator)
   {
+    // TODO use StringUtils version
     StringBuffer v = new StringBuffer();
     if (list != null && list.length > 0)
     {
@@ -2723,7 +2743,7 @@ public class JalviewLite extends Applet implements
     {
       return def;
     }
-    if (stn.toLowerCase().equals(TRUE))
+    if (TRUE.equalsIgnoreCase(stn))
     {
       return true;
     }
@@ -2896,14 +2916,22 @@ public class JalviewLite extends Applet implements
     String codebase = localref.toString();
     if (url.indexOf("/") == 0)
     {
-      url = codebase.substring(0, codebase.length()
-              - localref.getFile().length())
+      String localfile = localref.getFile();
+      url = codebase.substring(0, codebase.length() - localfile.length())
               + url;
     }
     else
     {
       url = localref + url;
     }
+    if (debug)
+    {
+      System.err.println("URL: " + localref.toString());
+      System.err.println("URL.getFile: " + localref.getFile());
+      System.err.println("URL.getPath: " + localref.getPath());
+      System.err.println("URL.getQuery: " + localref.getQuery());
+      System.err.println("returning " + url);
+    }
     return url;
   }