minor bugfix for case when PDB sequence strings are not parsed and added new javascri...
[jalview.git] / src / jalview / bin / JalviewLite.java
index 4d45f8c..c7cc4f3 100755 (executable)
@@ -44,16 +44,42 @@ public class JalviewLite
   //The following public methods maybe called
   //externally, eg via javascript in HTML page
   /**
-   * @return list of selected sequences separated by "¬"
+   * @return String list of selected sequence IDs, each terminated by "¬" (¬)
    */
   public String getSelectedSequences()
   {
     return getSelectedSequencesFrom(getDefaultTargetFrame());
   }
+  /**
+   * @param sep separator string or null for default
+   * @return String list of selected sequence IDs, each terminated by sep or ("¬" as default)
+   */
+  public String getSelectedSequences(String sep)
+  {
+    return getSelectedSequencesFrom(getDefaultTargetFrame(), sep);
+  }
+  /**
+   * @param alf alignframe containing selection
+   * @return String list of selected sequence IDs, each terminated by "¬"
+   *  
+   */
   public String getSelectedSequencesFrom(AlignFrame alf)
   {
+    return getSelectedSequencesFrom(alf, "¬");
+  }
+  /**
+   * get list of selected sequence IDs separated by given separator 
+   * @param alf window containing selection 
+   * @param sep separator string to use - default is "¬"
+   * @return String list of selected sequence IDs, each terminated by the given separator
+   */
+  public String getSelectedSequencesFrom(AlignFrame alf, String sep)
+  {
     StringBuffer result = new StringBuffer("");
-
+    if (sep==null || sep.length()==0)
+    {
+      sep = "¬";
+    }
     if (alf.viewport.getSelectionGroup() != null)
     {
       SequenceI[] seqs = alf.viewport.getSelectionGroup().
@@ -62,13 +88,52 @@ public class JalviewLite
 
       for (int i = 0; i < seqs.length; i++)
       {
-        result.append(seqs[i].getName() + "¬");
+        result.append(seqs[i].getName());
+        result.append(sep); 
       }
     }
 
     return result.toString();
   }
-  
+
+  /**
+   * get sequences selected in current alignFrame and return their alignment in format 'format' either with or without suffix 
+   * @param alf - where selection is
+   * @param format - format of alignment file
+   * @param suffix - "true" to append /start-end string to each sequence ID 
+   * @return selected sequences as flat file or empty string if there was no current selection
+   */
+  public String getSelectedSequencesAsAlignment(String format, String suffix) {
+    return getSelectedSequencesAsAlignmentFrom(currentAlignFrame, format, suffix);
+  }
+
+  /**
+   * get sequences selected in alf and return their alignment in format 'format' either with or without suffix 
+   * @param alf - where selection is
+   * @param format - format of alignment file
+   * @param suffix - "true" to append /start-end string to each sequence ID 
+   * @return selected sequences as flat file or empty string if there was no current selection
+   */
+  public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf, String format, String suffix)
+  {
+    try
+    {
+      boolean seqlimits = suffix.equalsIgnoreCase("true");
+      if (alf.viewport.getSelectionGroup()!=null)
+      {
+        String reply = new AppletFormatAdapter().formatSequences(format,
+                new Alignment(alf.viewport.getSelectionAsNewSequence()), seqlimits);
+        return reply;
+      }
+    }
+    catch (Exception ex)
+    {
+      ex.printStackTrace();
+      return "Error retrieving alignment in " + format + " format. ";
+    }
+    return "";
+  }
+
   public String getAlignment(String format)
   {
     return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
@@ -436,19 +501,34 @@ public class JalviewLite
   class LoadingThread
       extends Thread
   {
-    String file;
+    /**
+     * State variable: File source 
+     */
+    String file; 
+    /**
+     * State variable: protocol for access to file source
+     */
     String protocol;
+    /**
+     * State variable: format of file source
+     */
     String format;
     JalviewLite applet;
-
-    public LoadingThread(String _file,
-                         JalviewLite _applet)
+    private void dbgMsg(String msg)
     {
       if (applet.debug)
       {
-        System.err.println("Loading thread started with:\n>>file\n"+_file+">>endfile");
+        System.err.println(msg);
       }
-      file = _file;
+    }
+    /**
+     * update the protocol state variable for accessing the datasource
+     * located by file. 
+     * @param file
+     * @return possibly updated datasource string
+     */
+    public String setProtocolState(String file)
+    {
       if (file.startsWith("PASTE"))
       {
         file = file.substring(5);
@@ -463,15 +543,17 @@ public class JalviewLite
         file = addProtocol(file);
         protocol = AppletFormatAdapter.URL;
       }
-      if (applet.debug)
-      {
-        System.err.println("Protocol identified as '"+protocol+"'");
-      }
+      dbgMsg("Protocol identified as '"+protocol+"'");
+      return file;
+    }
+    public LoadingThread(String _file,
+                         JalviewLite _applet)
+    {
+      dbgMsg("Loading thread started with:\n>>file\n"+_file+">>endfile");
+      file = setProtocolState(_file);
+      
       format = new jalview.io.IdentifyFile().Identify(file, protocol);
-      if (applet.debug)
-      {
-        System.err.println("File identified as '"+format+"'");
-      }
+      dbgMsg("File identified as '"+format+"'");
       applet = _applet;
     }
 
@@ -482,6 +564,7 @@ public class JalviewLite
 
     private void startLoading()
     {
+      dbgMsg("Loading started.");
       Alignment al = null;
       try
       {
@@ -490,10 +573,12 @@ public class JalviewLite
       }
       catch (java.io.IOException ex)
       {
+        dbgMsg("File load exception.");
         ex.printStackTrace();
       }
       if ( (al != null) && (al.getHeight() > 0))
       {
+        dbgMsg("Successfully loaded file.");
         initialAlignFrame =  new AlignFrame(al,
                                            applet,
                                            file,
@@ -518,7 +603,8 @@ public class JalviewLite
         {
           try
           {
-            if (inArchive(treeFile))
+            treeFile = setProtocolState(treeFile);
+            /*if (inArchive(treeFile))
             {
               protocol = AppletFormatAdapter.CLASSLOADER;
             }
@@ -527,7 +613,7 @@ public class JalviewLite
               protocol = AppletFormatAdapter.URL;
               treeFile = addProtocol(treeFile);
             }
-
+             */
             jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
                 protocol);
 
@@ -536,6 +622,9 @@ public class JalviewLite
             if (fin.getTree() != null)
             {
               currentAlignFrame.loadTree(fin, treeFile);
+              dbgMsg("Successfuly imported tree.");
+            } else {
+              dbgMsg("Tree parameter did not resolve to a valid tree.");
             }
           }
           catch (Exception ex)
@@ -547,11 +636,8 @@ public class JalviewLite
         String param = getParameter("features");
         if (param != null)
         {
-          if (!inArchive(param))
-          {
-            param = addProtocol(param);
-          }
-
+          param = setProtocolState(param);
+          
           currentAlignFrame.parseFeaturesFile(param, protocol);
         }
 
@@ -564,19 +650,19 @@ public class JalviewLite
 
         param = getParameter("annotations");
         if (param != null)
-        {
-          if (!inArchive(param))
-          {
-            param = addProtocol(param);
-          }
-
-          new AnnotationFile().readAnnotationFile(
+        { 
+          param = setProtocolState(param);
+          
+          if (new AnnotationFile().readAnnotationFile(
               currentAlignFrame.viewport.getAlignment(),
               param,
-              protocol);
-
-          currentAlignFrame.alignPanel.fontChanged();
-          currentAlignFrame.alignPanel.setScrollValues(0, 0);
+              protocol))
+          {
+            currentAlignFrame.alignPanel.fontChanged();
+            currentAlignFrame.alignPanel.setScrollValues(0, 0);
+          } else {
+            System.err.println("Annotations were not added from annotation file '"+param+"'");
+          }
 
         }
 
@@ -585,21 +671,12 @@ public class JalviewLite
         {
           try
           {
-            if (inArchive(param))
-            {
-              protocol = AppletFormatAdapter.CLASSLOADER;
-            }
-            else
-            {
-              protocol = AppletFormatAdapter.URL;
-              param = addProtocol(param);
-            }
-
+            param = setProtocolState(param);
             jalview.io.JPredFile predictions = new jalview.io.JPredFile(
                 param, protocol);
-            new JnetAnnotationMaker().add_annotation(predictions,
+            JnetAnnotationMaker.add_annotation(predictions,
                 currentAlignFrame.viewport.getAlignment(),
-                0, false); // do not add sequence profile from concise output
+                0, false); // false==do not add sequence profile from concise output
             currentAlignFrame.alignPanel.fontChanged();
             currentAlignFrame.alignPanel.setScrollValues(0, 0);
           }
@@ -675,15 +752,14 @@ public class JalviewLite
                 tmp2.copyInto(chains);
               }
             }
-
-            if (inArchive(param) && !jmolAvailable)
+            param = setProtocolState(param);
+            
+            if (!jmolAvailable && protocol==AppletFormatAdapter.CLASSLOADER)
             {
-              protocol = AppletFormatAdapter.CLASSLOADER;
-            }
-            else
-            {
-              protocol = AppletFormatAdapter.URL;
-              param = addProtocol(param);
+              // TODO: pass PDB file in classloader on to Jmol
+              // This exception preserves the current behaviour where, even if the local pdb file was identified in the class loader
+              protocol = AppletFormatAdapter.URL; // this is probably NOT CORRECT!
+              param = addProtocol(param); // 
             }
 
             pdb.setFile(param);
@@ -692,7 +768,16 @@ public class JalviewLite
             {
               for (int i = 0; i < seqs.length; i++)
               {
-                ( (Sequence) seqs[i]).addPDBId(pdb);
+                if (seqs[i]!=null)
+                {
+                  ( (Sequence) seqs[i]).addPDBId(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 (jmolAvailable)
@@ -753,7 +838,11 @@ public class JalviewLite
       //Netscape Communicator for instance.
       try
       {
-        return (getClass().getResourceAsStream("/" + file) != null);
+        boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
+        if (debug)
+        {  System.err.println("Resource '"+file+"' was "+(rtn ? "" : "not") +" located by classloader.");
+        }
+        return rtn;
       }
       catch (Exception ex)
       {
@@ -767,6 +856,10 @@ public class JalviewLite
       if (file.indexOf("://") == -1)
       {
         file = getCodeBase() + file;
+        if (debug)
+        {
+          System.err.println("Prepended codebase for resource: '"+file+"'");
+        }
       }
 
       return file;