static debug flag controlling output of excessive info. showfeaturegroups and hidefea...
[jalview.git] / src / jalview / bin / JalviewLite.java
index df82b5f..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,23 +198,18 @@ public class JalviewLite
   boolean embedded = false;
 
   public boolean jmolAvailable = false;
+  public static boolean debug;
 
   /**
    * init method for Jalview Applet
    */
   public void init()
   {
-    try
+    String dbg = getParameter("debug");
+    if (dbg!=null)
     {
-      Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
-
-      jmolAvailable = true;
+      debug = dbg.toLowerCase().equals("true");
     }
-    catch (java.lang.ClassNotFoundException ex)
-    {
-      System.out.println("Jmol not found - Using MCview for structures");
-    }
-
     int r = 255;
     int g = 255;
     int b = 255;
@@ -215,6 +257,9 @@ public class JalviewLite
       }
     }
 
+    LoadJmolThread jmolAvailable = new LoadJmolThread();
+    jmolAvailable.start();
+
     final JalviewLite applet = this;
     if (getParameter("embedded") != null
         && getParameter("embedded").equalsIgnoreCase("true"))
@@ -244,33 +289,6 @@ public class JalviewLite
     }
   }
 
-  public static void main(String[] args)
-  {
-    if (args.length != 1)
-    {
-      System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
-      System.exit(1);
-    }
-
-    String format = new jalview.io.IdentifyFile().Identify(args[0],
-        AppletFormatAdapter.FILE);
-
-    Alignment al = null;
-    try
-    {
-      al = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE,
-                                              format);
-    }
-    catch (java.io.IOException ex)
-    {
-      ex.printStackTrace();
-    }
-    if ( (al != null) && (al.getHeight() > 0))
-    {
-      AlignFrame af = new AlignFrame(al, null, args[0], false);
-      af.statusBar.setText("Successfully loaded file " + args[0]);
-    }
-  }
 
   /**
    * Initialises and displays a new java.awt.Frame
@@ -311,6 +329,10 @@ public class JalviewLite
         if (frame instanceof AlignFrame)
         {
           currentAlignFrame = (AlignFrame) frame;
+          if (debug)
+          {
+            System.err.println("Activated window "+frame);
+          }
         }
       }
 
@@ -343,9 +365,29 @@ public class JalviewLite
       g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
       g.drawString("Loading Data...", 50, this.getSize().height / 2);
     }
+  }
 
+
+  class LoadJmolThread extends Thread
+  {
+    public void run()
+    {
+      try
+      {
+        if (!System.getProperty("java.version").startsWith("1.1"))
+        {
+          Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
+          jmolAvailable = true;
+        }
+      }
+      catch (java.lang.ClassNotFoundException ex)
+      {
+        System.out.println("Jmol not available - Using MCview for structures");
+      }
+    }
   }
 
+
   class LoadingThread
       extends Thread
   {
@@ -357,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"))
       {
@@ -372,12 +418,25 @@ 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;
     }
 
     public void run()
     {
+      startLoading();
+    }
+
+    private void startLoading()
+    {
       Alignment al = null;
       try
       {
@@ -505,62 +564,129 @@ public class JalviewLite
           }
         }
 
+        /*
+         <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B PDB|1GAQ|1GAQ|C">
 
-        param = getParameter("PDBFILE");
-        if (param != null)
-        {
-
-          PDBEntry pdb = new PDBEntry();
+         <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
 
-          String seqstring;
-          SequenceI [] seqs = null;
+         <param name="PDBfile3" value="1q0o Q45135_9MICO">
+        */
 
-          if(param.indexOf(" ")>-1)
-          {
-            seqstring = param.substring(param.indexOf(" ")+1);
-            param = param.substring(0, param.indexOf(" "));
-            StringTokenizer st = new StringTokenizer(seqstring);
-            Vector tmp = new Vector();
-            while(st.hasMoreTokens())
-            {
-              tmp.addElement((Sequence) currentAlignFrame.
-                getAlignViewport().getAlignment().
-                findName(st.nextToken()));
-            }
 
-            seqs = new SequenceI[tmp.size()];
-            tmp.copyInto(seqs);
-          }
+        int pdbFileCount = 0;
+        do{
+          if (pdbFileCount > 0)
+            param = getParameter("PDBFILE" + pdbFileCount);
           else
-          {
-            String sequence = applet.getParameter("PDBSEQ");
-            if (sequence != null)
-              seqs = new SequenceI[]
-                  {
-                  (Sequence) currentAlignFrame.
-                  getAlignViewport().getAlignment().
-                  findName(sequence)};
-          }
+            param = getParameter("PDBFILE");
 
-          if (!inArchive(param) || jmolAvailable)
+          if (param != null)
           {
-            param = addProtocol(param);
-          }
+            PDBEntry pdb = new PDBEntry();
 
-          pdb.setFile(param);
+            String seqstring;
+            SequenceI[] seqs = null;
+            String [] chains = null;
 
+            StringTokenizer st = new StringTokenizer(param, " ");
 
-          if (jmolAvailable)
-            new jalview.appletgui.AppletJmol(pdb,
-                                             seqs,
-                                             currentAlignFrame.alignPanel,
-                                             protocol);
-          else
+            if (st.countTokens() < 2)
+            {
+              String sequence = applet.getParameter("PDBSEQ");
+              if (sequence != null)
+                seqs = new SequenceI[]
+                    {
+                    (Sequence) currentAlignFrame.
+                    getAlignViewport().getAlignment().
+                    findName(sequence)};
+
+            }
+            else
+            {
+              param = st.nextToken();
+              Vector tmp = new Vector();
+              Vector tmp2 = new Vector();
+
+              while (st.hasMoreTokens())
+              {
+                seqstring = st.nextToken();
+                StringTokenizer st2 = new StringTokenizer(seqstring,"=");
+                if(st2.countTokens()>1)
+                {
+                  //This is the chain
+                  tmp2.addElement(st2.nextToken());
+                  seqstring = st2.nextToken();
+                }
+                tmp.addElement( (Sequence) currentAlignFrame.
+                                 getAlignViewport().getAlignment().
+                                 findName(seqstring));
+              }
+
+              seqs = new SequenceI[tmp.size()];
+              tmp.copyInto(seqs);
+              if(tmp2.size()==tmp.size())
+              {
+                chains = new String[tmp2.size()];
+                tmp2.copyInto(chains);
+              }
+            }
+
+            if (inArchive(param) && !jmolAvailable)
+            {
+              protocol = AppletFormatAdapter.CLASSLOADER;
+            }
+            else
+            {
+              protocol = AppletFormatAdapter.URL;
+              param = addProtocol(param);
+            }
+
+            pdb.setFile(param);
 
-            new MCview.AppletPDBViewer(pdb,
-                                       seqs,
-                                       currentAlignFrame.alignPanel,
-                                       protocol);
+            if(seqs!=null)
+            {
+              for (int i = 0; i < seqs.length; i++)
+              {
+                ( (Sequence) seqs[i]).addPDBId(pdb);
+              }
+
+              if (jmolAvailable)
+              {
+                new jalview.appletgui.AppletJmol(pdb,
+                                                 seqs,
+                                                 chains,
+                                                 currentAlignFrame.alignPanel,
+                                                 protocol);
+                lastFrameX += 40;
+                lastFrameY+=40;
+              }
+              else
+                    new MCview.AppletPDBViewer(pdb,
+                                           seqs,
+                                           chains,
+                                           currentAlignFrame.alignPanel,
+                                           protocol);
+            }
+          }
+
+          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
@@ -601,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);
+  }
 }