static debug flag controlling output of excessive info. showfeaturegroups and hidefea...
[jalview.git] / src / jalview / bin / JalviewLite.java
index e0f0910..c9977fd 100755 (executable)
@@ -43,16 +43,22 @@ public class JalviewLite
   ///////////////////////////////////////////
   //The following public methods maybe called
   //externally, eg via javascript in HTML page
-
+  /**
+   * @return list of selected sequences separated by "¬"
+   */
   public String getSelectedSequences()
   {
+    return getSelectedSequences(currentAlignFrame);
+  }
+  public String getSelectedSequences(AlignFrame alf)
+  {
     StringBuffer result = new StringBuffer("");
 
-    if (initialAlignFrame.viewport.getSelectionGroup() != null)
+    if (alf.viewport.getSelectionGroup() != null)
     {
-      SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
+      SequenceI[] seqs = alf.viewport.getSelectionGroup().
           getSequencesInOrder(
-              initialAlignFrame.viewport.getAlignment());
+                  alf.viewport.getAlignment());
 
       for (int i = 0; i < seqs.length; i++)
       {
@@ -62,20 +68,27 @@ public class JalviewLite
 
     return result.toString();
   }
-
+  
   public String getAlignment(String format)
   {
-    return getAlignment(format, "true");
+    return getAlignment(currentAlignFrame, format, "true");
+  }
+  public String getAlignment(AlignFrame alf, String format)
+  {
+    return getAlignment(alf, format, "true");
   }
-
   public String getAlignment(String format, String suffix)
   {
+    return getAlignment(currentAlignFrame, format, suffix);
+  }
+  public String getAlignment(AlignFrame alf, String format, String suffix)
+  {
     try
     {
       boolean seqlimits = suffix.equalsIgnoreCase("true");
 
       String reply = new AppletFormatAdapter().formatSequences(format,
-          currentAlignFrame.viewport.getAlignment(), seqlimits);
+          alf.viewport.getAlignment(), seqlimits);
       return reply;
     }
     catch (Exception ex)
@@ -87,30 +100,63 @@ public class JalviewLite
 
   public void loadAnnotation(String annotation)
   {
+    loadAnnotation(currentAlignFrame, annotation);
+  }
+  public void loadAnnotation(AlignFrame alf, String annotation)
+  {
     if (new AnnotationFile().readAnnotationFile(
-        currentAlignFrame.getAlignViewport().getAlignment(), annotation,
+        alf.getAlignViewport().getAlignment(), annotation,
         AppletFormatAdapter.PASTE))
     {
-      currentAlignFrame.alignPanel.fontChanged();
-      currentAlignFrame.alignPanel.setScrollValues(0, 0);
+      alf.alignPanel.fontChanged();
+      alf.alignPanel.setScrollValues(0, 0);
     }
     else
     {
-      currentAlignFrame.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
+      alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
     }
   }
 
   public String getFeatures(String format)
   {
-    return currentAlignFrame.outputFeatures(false, format);
+    return getFeatures(currentAlignFrame, format);
+  }
+  public String getFeatures(AlignFrame alf, String format)
+  {
+    return alf.outputFeatures(false, format);
   }
-
   public String getAnnotation()
   {
-    return currentAlignFrame.outputAnnotations(false);
+    return getAnnotation(currentAlignFrame);
+  }
+  public String getAnnotation(AlignFrame alf)
+  {
+    return alf.outputAnnotations(false);
+  }
+  public AlignFrame newView()
+  {
+    return newView(currentAlignFrame);
+  }
+  public AlignFrame newView(String name)
+  {
+    return newView(currentAlignFrame, name);
   }
 
-  public void loadAlignment(String text, String title)
+  public AlignFrame newView(AlignFrame alf)
+  {
+    return alf.newView(null);
+  }
+  public AlignFrame newView(AlignFrame alf, String name)
+  {
+    return alf.newView(name);
+  }
+  /**
+   * 
+   * @param text alignment file as a string
+   * @param title window title 
+   * @return null or new alignment frame
+   */
+  public AlignFrame loadAlignment(String text, String title)
   {
     Alignment al = null;
     String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
@@ -121,13 +167,14 @@ public class JalviewLite
                                               format);
       if (al.getHeight() > 0)
       {
-        new AlignFrame(al, this, title, false);
+        return new AlignFrame(al, this, title, false);
       }
     }
     catch (java.io.IOException ex)
     {
       ex.printStackTrace();
     }
+    return null;
   }
 
   ////////////////////////////////////////////////
@@ -151,12 +198,18 @@ public class JalviewLite
   boolean embedded = false;
 
   public boolean jmolAvailable = false;
+  public static boolean debug;
 
   /**
    * init method for Jalview Applet
    */
   public void init()
   {
+    String dbg = getParameter("debug");
+    if (dbg!=null)
+    {
+      debug = dbg.toLowerCase().equals("true");
+    }
     int r = 255;
     int g = 255;
     int b = 255;
@@ -276,6 +329,10 @@ public class JalviewLite
         if (frame instanceof AlignFrame)
         {
           currentAlignFrame = (AlignFrame) frame;
+          if (debug)
+          {
+            System.err.println("Activated window "+frame);
+          }
         }
       }
 
@@ -321,7 +378,6 @@ public class JalviewLite
         {
           Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
           jmolAvailable = true;
-          System.out.println("JMOL FOUND");
         }
       }
       catch (java.lang.ClassNotFoundException ex)
@@ -343,6 +399,10 @@ public class JalviewLite
     public LoadingThread(String _file,
                          JalviewLite _applet)
     {
+      if (applet.debug)
+      {
+        System.err.println("Loading thread started with:\n>>file\n"+_file+">>endfile");
+      }
       file = _file;
       if (file.startsWith("PASTE"))
       {
@@ -358,7 +418,15 @@ public class JalviewLite
         file = addProtocol(file);
         protocol = AppletFormatAdapter.URL;
       }
+      if (applet.debug)
+      {
+        System.err.println("Protocol identified as '"+protocol+"'");
+      }
       format = new jalview.io.IdentifyFile().Identify(file, protocol);
+      if (applet.debug)
+      {
+        System.err.println("File identified as '"+format+"'");
+      }
       applet = _applet;
     }
 
@@ -563,7 +631,16 @@ public class JalviewLite
               }
             }
 
-            param = addProtocol(param);
+            if (inArchive(param) && !jmolAvailable)
+            {
+              protocol = AppletFormatAdapter.CLASSLOADER;
+            }
+            else
+            {
+              protocol = AppletFormatAdapter.URL;
+              param = addProtocol(param);
+            }
+
             pdb.setFile(param);
 
             if(seqs!=null)
@@ -582,8 +659,6 @@ public class JalviewLite
                                                  protocol);
                 lastFrameX += 40;
                 lastFrameY+=40;
-
-                System.out.println(lastFrameX+" "+lastFrameY);
               }
               else
                     new MCview.AppletPDBViewer(pdb,
@@ -597,7 +672,22 @@ public class JalviewLite
           pdbFileCount++;
         }
         while(pdbFileCount < 10);
-
+        
+        /////////////////////////////
+        // modify display of features
+        //
+        // hide specific groups
+        param = getParameter("hidefeaturegroups");
+        if (param != null)
+        {
+          applet.setFeatureGroupState(param, false);
+        }
+        // show specific groups
+        param = getParameter("showfeaturegroups");
+        if (param != null)
+        {
+          applet.setFeatureGroupState(param, true);
+        }
       }
       else
       {
@@ -637,4 +727,98 @@ public class JalviewLite
       return file;
     }
   }
+
+  public String[] tabbedListToArray(String list)
+  {
+    if (list==null || list.equals(""))
+      return null;
+    java.util.Vector jv = new Vector();
+    int cp=0,pos;
+    while ((pos=list.indexOf("\t",cp))>cp)
+    {
+      jv.addElement(list.substring(cp,pos));
+      cp = pos+1;
+    }
+    if (cp<list.length())
+    {
+      jv.addElement(list.substring(cp));
+    }
+    if (jv.size()>0)
+    { String[] v = new String[jv.size()];
+      jv.copyInto(v);
+      jv.removeAllElements();
+      if (debug)
+      {
+        System.err.println("Array from Tabbed List:\n"+v.length+"\n"+v.toString());
+      }
+      return v;
+    }
+    if (debug)
+    {
+      System.err.println("Empty Array from Tabbed List");
+    }
+    return null;
+  }
+
+  public String ArraytotabbedList(String[] list)
+  {
+    StringBuffer v = new StringBuffer();
+    if (list!=null)
+    {
+      for (int i=0,iSize=list.length-1;i<iSize;i++)
+      { 
+        if (list[i]!=null)
+        {  
+          v.append(list[i]); 
+        }
+        v.append("\t");
+      }
+      if (list[list.length-1]!=null)
+      { v.append(list[list.length-1]);
+      }
+      if (debug)
+      {
+        System.err.println("Tabbed List:\n"+v.toString());
+      }
+      return v.toString();
+    }
+    if (debug)
+    {
+      System.err.println("Empty Tabbed List\n");
+    }
+    return "";
+  }
+  /**
+   * @return
+   * @see jalview.appletgui.AlignFrame#getFeatureGroups()
+   */
+  public String getFeatureGroups()
+  {
+    String lst = ArraytotabbedList(currentAlignFrame.getFeatureGroups());
+    return lst;
+  }
+
+  /**
+   * @param visible
+   * @return
+   * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
+   */
+  public String getFeatureGroupsOfState(boolean visible)
+  {
+    return ArraytotabbedList(currentAlignFrame.getFeatureGroupsOfState(visible));
+  }
+  /**
+   * @param groups tab separated list of group names 
+   * @param state true or false
+   * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[], boolean)
+   */
+  public void setFeatureGroupState(AlignFrame alf, String groups, boolean state)
+  {
+    boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
+    alf.setFeatureGroupState(tabbedListToArray(groups), st);
+  }
+  public void setFeatureGroupState(String groups, boolean state)
+  {
+    setFeatureGroupState(currentAlignFrame, groups, state);
+  }
 }