JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / bin / JalviewLite.java
index b490815..59424df 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.bin;
 
+import jalview.analysis.AlignmentUtils;
 import jalview.api.StructureSelectionManagerProvider;
 import jalview.appletgui.AlignFrame;
 import jalview.appletgui.AlignViewport;
 import jalview.appletgui.EmbmenuFrame;
 import jalview.appletgui.FeatureSettings;
+import jalview.appletgui.SplitFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
@@ -37,7 +39,9 @@ import jalview.io.AnnotationFile;
 import jalview.io.AppletFormatAdapter;
 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;
 import jalview.javascript.JsCallBack;
@@ -57,13 +61,15 @@ import java.awt.event.ActionEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
-import netscape.javascript.JSException;
 import netscape.javascript.JSObject;
 
 /**
@@ -76,6 +82,10 @@ public class JalviewLite extends Applet implements
         StructureSelectionManagerProvider, JalviewLiteJsApi
 {
 
+  private static final String TRUE = "true";
+
+  private static final String FALSE = "false";
+
   public StructureSelectionManager getStructureSelectionManager()
   {
     return StructureSelectionManager.getStructureSelectionManager(this);
@@ -447,12 +457,11 @@ public class JalviewLite extends Applet implements
         end = rs.findIndex(end);
         if (csel != null)
         {
-          Vector cs = csel.getSelected();
+          List<Integer> cs = csel.getSelected();
           csel.clear();
-          for (int csi = 0, csiS = cs.size(); csi < csiS; csi++)
+          for (Integer selectedCol : cs)
           {
-            csel.addElement(rs.findIndex(((Integer) cs.elementAt(csi))
-                    .intValue()));
+            csel.addElement(rs.findIndex(selectedCol));
           }
         }
       }
@@ -494,7 +503,7 @@ public class JalviewLite extends Applet implements
   {
     try
     {
-      boolean seqlimits = suffix.equalsIgnoreCase("true");
+      boolean seqlimits = suffix.equalsIgnoreCase(TRUE);
       if (alf.viewport.getSelectionGroup() != null)
       {
         // JBPNote: getSelectionAsNewSequence behaviour has changed - this
@@ -630,7 +639,7 @@ public class JalviewLite extends Applet implements
     final String _undoName = undoName;
     // TODO: deal with synchronization here: cannot raise any events until after
     // this has returned.
-    return alf.sortBy(aorder, _undoName) ? "true" : "";
+    return alf.sortBy(aorder, _undoName) ? TRUE : "";
   }
 
   /*
@@ -640,7 +649,7 @@ public class JalviewLite extends Applet implements
    */
   public String getAlignment(String format)
   {
-    return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
+    return getAlignmentFrom(getDefaultTargetFrame(), format, TRUE);
   }
 
   /*
@@ -652,7 +661,7 @@ public class JalviewLite extends Applet implements
    */
   public String getAlignmentFrom(AlignFrame alf, String format)
   {
-    return getAlignmentFrom(alf, format, "true");
+    return getAlignmentFrom(alf, format, TRUE);
   }
 
   /*
@@ -678,7 +687,7 @@ public class JalviewLite extends Applet implements
   {
     try
     {
-      boolean seqlimits = suffix.equalsIgnoreCase("true");
+      boolean seqlimits = suffix.equalsIgnoreCase(TRUE);
 
       String reply = new AppletFormatAdapter().formatSequences(format,
               alf.viewport.getAlignment(), seqlimits);
@@ -838,7 +847,7 @@ public class JalviewLite extends Applet implements
    */
   public AlignFrame loadAlignment(String text, String title)
   {
-    Alignment al = null;
+    AlignmentI al = null;
 
     String format = new IdentifyFile().Identify(text,
             AppletFormatAdapter.PASTE);
@@ -1233,7 +1242,10 @@ public class JalviewLite extends Applet implements
 
   String file = "No file";
 
-  Button launcher = new Button("Start Jalview");
+  String file2 = null;
+
+  Button launcher = new Button(
+          MessageManager.getString("label.start_jalview"));
 
   /**
    * The currentAlignFrame is static, it will change if and when the user
@@ -1277,7 +1289,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)
@@ -1361,11 +1373,7 @@ public class JalviewLite extends Applet implements
     /**
      * turn on extra applet debugging
      */
-    String dbg = getParameter("debug");
-    if (dbg != null)
-    {
-      debug = dbg.toLowerCase().equals("true");
-    }
+    debug = TRUE.equalsIgnoreCase(getParameter("debug"));
     if (debug)
     {
 
@@ -1377,8 +1385,7 @@ public class JalviewLite extends Applet implements
     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
@@ -1386,7 +1393,7 @@ public class JalviewLite extends Applet implements
     String chkforJmol = getParameter("nojmol");
     if (chkforJmol != null)
     {
-      checkForJmol = !chkforJmol.equals("true");
+      checkForJmol = !chkforJmol.equals(TRUE);
     }
     /**
      * get the separator parameter if present
@@ -1404,7 +1411,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;
@@ -1451,38 +1460,40 @@ public class JalviewLite extends Applet implements
         file = data.toString();
       }
     }
+    if (getDefaultParameter("enableSplitFrame", true))
+    {
+      file2 = getParameter("file2");
+    }
 
-    final JalviewLite jvapplet = this;
-    if (getParameter("embedded") != null
-            && getParameter("embedded").equalsIgnoreCase("true"))
+    embedded = TRUE.equalsIgnoreCase(getParameter("embedded"));
+    if (embedded)
     {
-      // Launch as embedded applet in page
-      embedded = true;
-      LoadingThread loader = new LoadingThread(file, jvapplet);
+      LoadingThread loader = new LoadingThread(file, file2, this);
       loader.start();
     }
     else if (file != null)
     {
-      if (getParameter("showbutton") == null
-              || !getParameter("showbutton").equalsIgnoreCase("false"))
+      /*
+       * Start the applet immediately or show a button to start it
+       */
+      if (FALSE.equalsIgnoreCase(getParameter("showbutton")))
+      {
+        LoadingThread loader = new LoadingThread(file, file2, this);
+        loader.start();
+      }
+      else
       {
-        // Add the JalviewLite 'Button' to the page
         add(launcher);
         launcher.addActionListener(new java.awt.event.ActionListener()
         {
           public void actionPerformed(ActionEvent e)
           {
-            LoadingThread loader = new LoadingThread(file, jvapplet);
+            LoadingThread loader = new LoadingThread(file, file2,
+                    JalviewLite.this);
             loader.start();
           }
         });
       }
-      else
-      {
-        // Open jalviewLite immediately.
-        LoadingThread loader = new LoadingThread(file, jvapplet);
-        loader.start();
-      }
     }
     else
     {
@@ -1523,7 +1534,7 @@ public class JalviewLite extends Applet implements
         {
           notFailed = true;
         }
-      } catch (JSException jsex)
+      } catch (Exception jsex)
       {
         System.err.println("Attempt " + tries
                 + " to access LiveConnect javascript failed.");
@@ -1756,27 +1767,19 @@ public class JalviewLite extends Applet implements
   class LoadingThread extends Thread
   {
     /**
-     * State variable: File source
-     */
-    String file;
-
-    /**
      * State variable: protocol for access to file source
      */
     String protocol;
 
-    /**
-     * State variable: format of file source
-     */
-    String format;
+    String _file; // alignment file or URL spec
 
-    String _file;
+    String _file2; // second alignment file or URL spec
 
     JalviewLite applet;
 
     private void dbgMsg(String msg)
     {
-      if (applet.debug)
+      if (JalviewLite.debug)
       {
         System.err.println(msg);
       }
@@ -1809,9 +1812,10 @@ public class JalviewLite extends Applet implements
       return file;
     }
 
-    public LoadingThread(String _file, JalviewLite _applet)
+    public LoadingThread(String file, String file2, JalviewLite _applet)
     {
-      this._file = _file;
+      this._file = file;
+      this._file2 = file2;
       applet = _applet;
     }
 
@@ -1828,25 +1832,125 @@ public class JalviewLite extends Applet implements
         } catch (Exception e)
         {
         }
-        ;
       }
       startLoading();
       // applet.callInitCallback();
     }
 
+    /**
+     * Load the alignment and any related files as specified by applet
+     * parameters
+     */
     private void startLoading()
     {
-      AlignFrame newAlignFrame;
       dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
-      file = setProtocolState(_file);
 
-      format = new jalview.io.IdentifyFile().Identify(file, protocol);
-      dbgMsg("File identified as '" + format + "'");
       dbgMsg("Loading started.");
-      Alignment al = null;
+
+      AlignFrame newAlignFrame = readAlignment(_file);
+      AlignFrame newAlignFrame2 = readAlignment(_file2);
+      if (newAlignFrame != null)
+      {
+        addToDisplay(newAlignFrame, newAlignFrame2);
+        loadTree(newAlignFrame);
+
+        loadScoreFile(newAlignFrame);
+
+        loadFeatures(newAlignFrame);
+
+        loadAnnotations(newAlignFrame);
+
+        loadJnetFile(newAlignFrame);
+
+        loadPdbFiles(newAlignFrame);
+      }
+      else
+      {
+        fileFound = false;
+        applet.remove(launcher);
+        applet.repaint();
+      }
+      callInitCallback();
+    }
+
+    /**
+     * Add an AlignFrame to the display; or if two are provided, a SplitFrame.
+     * 
+     * @param af
+     * @param af2
+     */
+    public void addToDisplay(AlignFrame af, AlignFrame af2)
+    {
+      if (af2 != null)
+      {
+        AlignmentI al1 = af.viewport.getAlignment();
+        AlignmentI al2 = af2.viewport.getAlignment();
+        if (AlignmentUtils.isMappable(al1, al2))
+        {
+          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);
+    }
+
+    /**
+     * Read the alignment file (from URL, text 'paste', or archive by
+     * classloader).
+     * 
+     * @return
+     */
+    protected AlignFrame readAlignment(String fileParam)
+    {
+      if (fileParam == null)
+      {
+        return null;
+      }
+      String resolvedFile = setProtocolState(fileParam);
+      String format = new IdentifyFile().Identify(resolvedFile, protocol);
+      dbgMsg("File identified as '" + format + "'");
+      AlignmentI al = null;
       try
       {
-        al = new AppletFormatAdapter().readFile(file, protocol, format);
+        al = new AppletFormatAdapter().readFile(resolvedFile, protocol,
+                format);
+        if ((al != null) && (al.getHeight() > 0))
+        {
+          dbgMsg("Successfully loaded file.");
+          al.setDataset(null);
+          AlignFrame newAlignFrame = new AlignFrame(al, applet,
+                  resolvedFile, embedded, false);
+          newAlignFrame.setTitle(resolvedFile);
+          if (initialAlignFrame == null)
+          {
+            initialAlignFrame = newAlignFrame;
+          }
+          // update the focus.
+          currentAlignFrame = newAlignFrame;
+
+          if (protocol == AppletFormatAdapter.PASTE)
+          {
+            newAlignFrame.setTitle(MessageManager.formatMessage(
+                    "label.sequences_from", new Object[] { applet
+                            .getDocumentBase().toString() }));
+          }
+
+          newAlignFrame.statusBar.setText(MessageManager.formatMessage(
+                  "label.successfully_loaded_file",
+                  new Object[] { resolvedFile }));
+
+          return newAlignFrame;
+        }
       } catch (java.io.IOException ex)
       {
         dbgMsg("File load exception.");
@@ -1855,9 +1959,9 @@ public class JalviewLite extends Applet implements
         {
           try
           {
-            FileParse fp = new FileParse(file, protocol);
+            FileParse fp = new FileParse(resolvedFile, protocol);
             String ln = null;
-            dbgMsg(">>>Dumping contents of '" + file + "' " + "("
+            dbgMsg(">>>Dumping contents of '" + resolvedFile + "' " + "("
                     + protocol + ")");
             while ((ln = fp.nextLine()) != null)
             {
@@ -1872,350 +1976,377 @@ public class JalviewLite extends Applet implements
           }
         }
       }
-      if ((al != null) && (al.getHeight() > 0))
+      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
       {
-        dbgMsg("Successfully loaded file.");
-        newAlignFrame = new AlignFrame(al, applet, file, embedded);
-        if (initialAlignFrame == null)
+        if (pdbFileCount > 0)
         {
-          initialAlignFrame = newAlignFrame;
+          param = applet.getParameter("PDBFILE" + pdbFileCount);
         }
-        // update the focus.
-        currentAlignFrame = newAlignFrame;
-
-        if (protocol == jalview.io.AppletFormatAdapter.PASTE)
+        else
         {
-          newAlignFrame.setTitle(MessageManager.formatMessage(
-                  "label.sequences_from", new String[]
-                  { applet.getDocumentBase().toString() }));
+          param = applet.getParameter("PDBFILE");
         }
 
-        newAlignFrame.statusBar.setText(MessageManager.formatMessage(
-                "label.successfully_loaded_file", new String[]
-                { file }));
-
-        String treeFile = applet.getParameter("tree");
-        if (treeFile == null)
+        if (param != null)
         {
-          treeFile = applet.getParameter("treeFile");
-        }
+          PDBEntry pdb = new PDBEntry();
 
-        if (treeFile != null)
-        {
-          try
+          String seqstring;
+          SequenceI[] seqs = null;
+          String[] chains = null;
+
+          StringTokenizer st = new StringTokenizer(param, " ");
+
+          if (st.countTokens() < 2)
           {
-            treeFile = setProtocolState(treeFile);
-            /*
-             * if (inArchive(treeFile)) { protocol =
-             * AppletFormatAdapter.CLASSLOADER; } else { protocol =
-             * AppletFormatAdapter.URL; treeFile = addProtocol(treeFile); }
-             */
-            jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
-                    protocol);
-
-            fin.parse();
-
-            if (fin.getTree() != null)
+            String sequence = applet.getParameter("PDBSEQ");
+            if (sequence != null)
             {
-              newAlignFrame.loadTree(fin, treeFile);
-              dbgMsg("Successfuly imported tree.");
+              seqs = new SequenceI[] { matcher == null ? (Sequence) alignFrame
+                      .getAlignViewport().getAlignment().findName(sequence)
+                      : matcher.findIdMatch(sequence) };
             }
-            else
+
+          }
+          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(matcher == null ? (Sequence) alignFrame
+                      .getAlignViewport().getAlignment()
+                      .findName(seqstring) : matcher.findIdMatch(seqstring));
+            }
+
+            seqs = new SequenceI[tmp.size()];
+            tmp.copyInto(seqs);
+            if (tmp2.size() == tmp.size())
             {
-              dbgMsg("Tree parameter did not resolve to a valid tree.");
+              chains = new String[tmp2.size()];
+              tmp2.copyInto(chains);
             }
-          } catch (Exception ex)
+          }
+          param = setProtocolState(param);
+
+          if (// !jmolAvailable
+          // &&
+          protocol == AppletFormatAdapter.CLASSLOADER && !useXtrnalSviewer)
           {
-            ex.printStackTrace();
+            // Re: JAL-357 : the bug isn't a problem if we are using an
+            // external viewer!
+            // TODO: verify this Re:
+            // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
+            // 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); //
           }
-        }
 
-        /*
-         * Try to load T-Coffee score file
-         */
-        String sScoreFile = applet.getParameter("scoreFile");
-        if (sScoreFile != null && !"".equals(sScoreFile))
-        {
-          try
+          pdb.setFile(param);
+
+          if (seqs != null)
           {
-            if (debug)
+            for (int i = 0; i < seqs.length; i++)
             {
-              System.err
-                      .println("Attempting to load T-COFFEE score file from the scoreFile parameter");
+              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 (!newAlignFrame.loadScoreFile(sScoreFile))
+
+            if (!alignPdbStructures)
             {
-              System.err
-                      .println("Failed to parse T-COFFEE parameter as a valid score file ('"
-                              + sScoreFile + "')");
+              alignFrame.newStructureView(applet, pdb, seqs, chains,
+                      protocol);
+            }
+            else
+            {
+              pdbs.addElement(new Object[] { pdb, seqs, chains,
+                  new String(protocol) });
             }
-          } catch (Exception e)
-          {
-            System.err.printf("Cannot read score file: '%s'. Cause: %s \n",
-                    sScoreFile, e.getMessage());
           }
         }
 
-        // ///////////////////////////
-        // 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)
-        {
-          newAlignFrame.setFeatureGroupState(separatorListToArray(param),
-                  false);
-          // applet.setFeatureGroupStateOn(newAlignFrame, param, false);
-        }
-        // show specific groups
-        param = applet.getParameter("showfeaturegroups");
-        if (param != null)
-        {
-          newAlignFrame.setFeatureGroupState(separatorListToArray(param),
-                  true);
-          // applet.setFeatureGroupStateOn(newAlignFrame, param, true);
+        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];
         }
-        // and now load features
-        param = applet.getParameter("features");
-        if (param != null)
+        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)
+      {
+        try
         {
           param = setProtocolState(param);
-
-          newAlignFrame.parseFeaturesFile(param, protocol);
+          JPredFile predictions = new JPredFile(param, protocol);
+          JnetAnnotationMaker.add_annotation(predictions,
+                  alignFrame.viewport.getAlignment(), 0, false);
+          // false == do not add sequence profile from concise output
+          SequenceI repseq = alignFrame.viewport.getAlignment()
+                  .getSequenceAt(0);
+          alignFrame.viewport.getAlignment().setSeqrep(repseq);
+          ColumnSelection cs = new ColumnSelection();
+          cs.hideInsertionsFor(repseq);
+          alignFrame.viewport.setColumnSelection(cs);
+          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 = setProtocolState(param);
 
-        param = applet.getParameter("showFeatureSettings");
-        if (param != null && param.equalsIgnoreCase("true"))
+        if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
+                param, protocol))
         {
-          newAlignFrame.viewport.setShowSequenceFeatures(true);
-          new FeatureSettings(newAlignFrame.alignPanel);
+          alignFrame.alignPanel.fontChanged();
+          alignFrame.alignPanel.setScrollValues(0, 0);
+          result = true;
         }
-
-        param = applet.getParameter("annotations");
-        if (param != null)
+        else
         {
-          param = setProtocolState(param);
+          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 = setProtocolState(param);
+
+        result = alignFrame.parseFeaturesFile(param, protocol);
+      }
 
-          if (new AnnotationFile().annotateAlignmentView(
-                  newAlignFrame.viewport, 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)
           {
-            newAlignFrame.alignPanel.fontChanged();
-            newAlignFrame.alignPanel.setScrollValues(0, 0);
+            System.err
+                    .println("Attempting to load T-COFFEE score file from the scoreFile parameter");
           }
-          else
+          result = alignFrame.loadScoreFile(sScoreFile);
+          if (!result)
           {
             System.err
-                    .println("Annotations were not added from annotation file '"
-                            + param + "'");
+                    .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;
+    }
 
-        param = applet.getParameter("jnetfile");
-        if (param != null)
+    /**
+     * 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
         {
-          try
-          {
-            param = setProtocolState(param);
-            jalview.io.JPredFile predictions = new jalview.io.JPredFile(
-                    param, protocol);
-            JnetAnnotationMaker.add_annotation(predictions,
-                    newAlignFrame.viewport.getAlignment(), 0, false); // false==do
-            SequenceI repseq = newAlignFrame.viewport.getAlignment()
-                    .getSequenceAt(0);
-            newAlignFrame.viewport.getAlignment().setSeqrep(repseq);
-            ColumnSelection cs = new ColumnSelection();
-            cs.hideInsertionsFor(repseq);
-            newAlignFrame.viewport.setColumnSelection(cs);
-            // not
-            // add
-            // sequence
-            // profile
-            // from
-            // concise
-            // output
-            newAlignFrame.alignPanel.fontChanged();
-            newAlignFrame.alignPanel.setScrollValues(0, 0);
-          } catch (Exception ex)
-          {
-            ex.printStackTrace();
-          }
-        }
-        /*
-         * <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(
-                newAlignFrame.getAlignViewport().getAlignment()
-                        .getSequencesArray()) : null;
-
-        do
-        {
-          if (pdbFileCount > 0)
+          treeFile = setProtocolState(treeFile);
+          NewickFile fin = new NewickFile(treeFile, protocol);
+          fin.parse();
+
+          if (fin.getTree() != null)
           {
-            param = applet.getParameter("PDBFILE" + pdbFileCount);
+            alignFrame.loadTree(fin, treeFile);
+            result = true;
+            dbgMsg("Successfully imported tree.");
           }
           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) newAlignFrame
-                        .getAlignViewport().getAlignment()
-                        .findName(sequence) : matcher.findIdMatch(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(matcher == null ? (Sequence) newAlignFrame
-                        .getAlignViewport().getAlignment()
-                        .findName(seqstring) : matcher
-                        .findIdMatch(seqstring));
-              }
-
-              seqs = new SequenceI[tmp.size()];
-              tmp.copyInto(seqs);
-              if (tmp2.size() == tmp.size())
-              {
-                chains = new String[tmp2.size()];
-                tmp2.copyInto(chains);
-              }
-            }
-            param = setProtocolState(param);
-
-            if (// !jmolAvailable
-            // &&
-            protocol == AppletFormatAdapter.CLASSLOADER
-                    && !useXtrnalSviewer)
-            {
-              // Re: JAL-357 : the bug isn't a problem if we are using an
-              // external viewer!
-              // TODO: verify this Re:
-              // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
-              // 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);
-
-            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)
-              {
-                newAlignFrame.newStructureView(applet, pdb, seqs, chains,
-                        protocol);
-              }
-              else
-              {
-                pdbs.addElement(new Object[]
-                { pdb, seqs, chains, new String(protocol) });
-              }
-            }
+            dbgMsg("Tree parameter did not resolve to a valid tree.");
           }
-
-          pdbFileCount++;
-        } while (param != null || pdbFileCount < 10);
-        if (pdbs.size() > 0)
+        } catch (Exception ex)
         {
-          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];
-          }
-          newAlignFrame.alignedStructureView(applet, pdb, seqs, chains,
-                  protocols);
-
+          ex.printStackTrace();
         }
       }
-      else
-      {
-        fileFound = false;
-        applet.remove(launcher);
-        applet.repaint();
-      }
-      callInitCallback();
+      return result;
     }
 
     /**
@@ -2246,47 +2377,88 @@ public class JalviewLite extends Applet implements
       }
     }
 
-    String addProtocol(String file)
+    /**
+     * If the file is not already in URL format, tries to locate it by resolving
+     * as a URL.
+     * 
+     * @param f
+     * @return
+     */
+    String addProtocol(final String f)
     {
-      if (file.indexOf("://") == -1)
+      if (f.indexOf("://") != -1)
       {
-        String fl = applet.resolveUrlForLocalOrAbsolute(file,
-                getDocumentBase());
-        try
+        // already has URL format
+        return f;
+      }
+
+      /*
+       * Try relative to document base
+       */
+      URL documentBase = getDocumentBase();
+      String url = applet.resolveUrlForLocalOrAbsolute(f, documentBase);
+      if (urlExists(url))
+      {
+        if (debug)
         {
-          if (new java.net.URL(fl).openStream() != null)
-          {
-            if (debug)
-            {
-              System.err.println("Prepended document base for resource: '"
-                      + file + "'");
-            }
-            return fl;
-          }
-        } catch (Exception x)
+          System.err.println("Prepended document base '" + documentBase
+                  + "' to make: '" + url + "'");
+        }
+        return url;
+      }
+
+      /*
+       * Try relative to codebase
+       */
+      URL codeBase = getCodeBase();
+      url = applet.resolveUrlForLocalOrAbsolute(f, codeBase);
+      if (urlExists(url))
+      {
+        if (debug)
         {
+          System.err.println("Prepended codebase '" + codeBase
+                  + "' to make: '" + url + "'");
         }
-        ;
-        fl = applet.resolveUrlForLocalOrAbsolute(file, getCodeBase());
-        try
+        return url;
+      }
+
+      return f;
+    }
+
+    /**
+     * Returns true if an input stream can be opened on the specified URL, else
+     * false.
+     * 
+     * @param url
+     * @return
+     */
+    private boolean urlExists(String url)
+    {
+      InputStream is = null;
+      try
+      {
+        is = new URL(url).openStream();
+        if (is != null)
+        {
+          return true;
+        }
+      } catch (Exception x)
+      {
+        // ignore
+      } finally
+      {
+        if (is != null)
         {
-          if (new java.net.URL(fl).openStream() != null)
+          try
           {
-            if (debug)
-            {
-              System.err.println("Prepended codebase for resource: '"
-                      + file + "'");
-            }
-            return fl;
+            is.close();
+          } catch (IOException e)
+          {
+            // ignore
           }
-        } catch (Exception x)
-        {
         }
-        ;
-
       }
-
-      return file;
+      return false;
     }
   }
 
@@ -2342,7 +2514,6 @@ public class JalviewLite extends Applet implements
    */
   public String[] separatorListToArray(String list, String separator)
   {
-    // note separator local variable intentionally masks object field
     int seplen = separator.length();
     if (list == null || list.equals("") || list.equals(separator))
     {
@@ -2566,7 +2737,7 @@ public class JalviewLite extends Applet implements
     {
       return def;
     }
-    if (stn.toLowerCase().equals("true"))
+    if (TRUE.equalsIgnoreCase(stn))
     {
       return true;
     }
@@ -2739,14 +2910,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;
   }