basic implementation of profile display and todos
[jalview.git] / src / jalview / bin / Jalview.java
index 36f4ff6..805d098 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
- * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
+ * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -26,6 +26,11 @@ import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.lang.reflect.Constructor;
 import java.net.URL;
+import java.security.AllPermission;
+import java.security.CodeSource;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.security.Policy;
 import java.util.*;
 
 import javax.swing.*;
@@ -43,7 +48,19 @@ import jalview.io.AppletFormatAdapter;
  */
 public class Jalview
 {
-
+  static {
+  // grab all the rights we can the JVM
+    Policy.setPolicy( new Policy() {
+    public PermissionCollection
+    getPermissions(CodeSource codesource) {
+    Permissions perms = new Permissions();
+    perms.add(new AllPermission());
+    return(perms);
+    }
+    public void refresh(){
+    }
+    });
+  }
   /**
    * main class for Jalview application
    * 
@@ -80,19 +97,38 @@ public class Jalview
                       + "-jalview FILE\tCreate alignment file FILE in Jalview format.\n"
                       + "-png FILE\tCreate PNG image FILE from alignment.\n"
                       + "-imgMap FILE\tCreate HTML file FILE with image map of PNG image.\n"
-                      + "-eps FILE\tCreate EPS file FILE from alignment."
-                      + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires."
-                      + "-noquestionnaire\tTurn off questionnaire check."
-                      + "-dasserver nickname=URL\tAdd and enable a das server with given nickname (alphanumeric or underscores only) for retrieval of features for all alignments."
-                      + "-fetchfrom nickname\tQuery nickname for features for the alignments and display them."
-                      + "-groovy FILE\tExecute groovy script in FILE, after all other arguments have been processed (if FILE is the text 'STDIN' then the file will be read from STDIN)"
-                      + "\n\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n");
+                      + "-eps FILE\tCreate EPS file FILE from alignment.\n"
+                      + "-questionnaire URL\tQueries the given URL for information about any Jalview user questionnaires.\n"
+                      + "-noquestionnaire\tTurn off questionnaire check.\n"
+                      + "-nousagestats\tTurn off google analytics tracking for this session.\n"
+                      + "-sortbytree OR -nosortbytree\tEnable or disable sorting of the given alignment by the given tree\n"
+//                      + "-setprop PROPERTY=VALUE\tSet the given Jalview property, after all other properties files have been read\n\t (quote the 'PROPERTY=VALUE' pair to ensure spaces are passed in correctly)"
+                      + "-dasserver nickname=URL\tAdd and enable a das server with given nickname\n\t\t\t(alphanumeric or underscores only) for retrieval of features for all alignments.\n"
+                      +"\t\t\tSources that also support the sequence command may be specified by prepending the URL with sequence:\n"
+                      +"\t\t\t e.g. sequence:http://localdas.somewhere.org/das/source)\n"
+                      + "-fetchfrom nickname\tQuery nickname for features for the alignments and display them.\n"
+//                      + "-vdoc vamsas-document\tImport vamsas document into new session or join existing session with same URN\n"
+//                      + "-vses vamsas-session\tJoin session with given URN\n"
+                      + "-groovy FILE\tExecute groovy script in FILE, after all other arguments have been processed (if FILE is the text 'STDIN' then the file will be read from STDIN)\n"
+                      + "\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n");
       System.exit(0);
     }
-
     Cache.loadProperties(aparser.getValue("props")); // must do this before
     // anything else!
-
+    String defs = aparser.getValue("setprop");
+    while (defs!=null)
+    {
+      int p = defs.indexOf('=');
+      if ( p==-1 )
+      {
+        System.err.println("Ignoring invalid setprop argument : "+defs);
+      } else {
+        System.out.println("Executing setprop argument: "+defs);
+        // DISABLED FOR SECURITY REASONS
+       // Cache.setProperty(defs.substring(0,p), defs.substring(p+1));
+      }
+      defs = aparser.getValue("setprop");
+    }
     if (aparser.contains("nodisplay"))
     {
       System.setProperty("java.awt.headless", "true");
@@ -126,6 +162,7 @@ public class Jalview
               // "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
               // "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
 
+              
               );
     } catch (Exception ex)
     {
@@ -135,6 +172,13 @@ public class Jalview
       desktop = new Desktop();
       desktop.setVisible(true);
       desktop.discoverer.start();
+      if (!aparser.contains("nousagestats") && Cache.getDefault("USAGESTATS", true)) {
+        Cache.log.info("Initialising googletracker for usage stats.");
+        Cache.initGoogleTracker();
+        Cache.log.debug("Tracking enabled.");
+      } else {
+        Cache.log.info("Not enabling Google Tracking.");
+      }
       if (!aparser.contains("noquestionnaire"))
       {
         String url = aparser.getValue("questionnaire");
@@ -180,7 +224,62 @@ public class Jalview
       System.out.println("No files to open!");
       System.exit(1);
     }
-
+    String vamsasImport=aparser.getValue("vdoc"),vamsasSession=aparser.getValue("vsess");
+    if (vamsasImport!=null || vamsasSession!=null)
+    {
+      if (desktop==null || headless)
+      {
+        System.out.println("Headless vamsas sessions not yet supported. Sorry.");
+        System.exit(1);
+      }
+      // if we have a file, start a new session and import it.
+      boolean inSession = false;
+      if (vamsasImport!=null)
+      {
+          try {
+            String viprotocol = Jalview.checkProtocol(vamsasImport);
+            if (viprotocol == jalview.io.FormatAdapter.FILE) {
+              inSession = desktop.vamsasImport(new File(vamsasImport));
+            } else if (viprotocol == jalview.io.FormatAdapter.URL) {
+              inSession = desktop.vamsasImport(new URL(vamsasImport));
+            }
+          
+          } catch (Exception e)
+          {
+            System.err.println("Exeption when importing "+vamsasImport+" as a vamsas document.");
+            e.printStackTrace();
+          }
+          if (!inSession) {
+            System.err.println("Failed to import "+vamsasImport+" as a vamsas document.");
+          } else {
+            System.out.println("Imported Successfully into new session "+desktop.getVamsasApplication().getCurrentSession());
+          }
+      }
+      if (vamsasSession!=null) {
+        if (vamsasImport!=null) {
+       // close the newly imported session and import the Jalview specific remnants into the new session later on.
+          desktop.vamsasStop_actionPerformed(null);
+        }
+        // now join the new session
+        try {
+          if (desktop.joinVamsasSession(vamsasSession)) {
+            System.out.println("Successfully joined vamsas session "+vamsasSession);
+          } else {
+            System.err.println("WARNING: Failed to join vamsas session "+vamsasSession);
+          }
+        } catch (Exception e)
+        {
+          System.err.println("ERROR: Failed to join vamsas session "+vamsasSession);
+          e.printStackTrace();
+        }
+        if (vamsasImport!=null) {
+          // the Jalview specific remnants can now be imported into the new session at the user's leisure.
+          Cache.log.info("Skipping Push for import of data into existing vamsas session."); // TODO: enable this when debugged
+          // desktop.getVamsasApplication().push_update();
+        }
+      }
+    }
+    // Finally, deal with the remaining input data.
     if (file != null)
     {
       System.out.println("Opening file: " + file);
@@ -203,7 +302,6 @@ public class Jalview
 
       AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol,
               format);
-
       if (af == null)
       {
         System.out.println("error");
@@ -250,6 +348,15 @@ public class Jalview
         af.loadJalviewDataFile(data);
         System.out.println("Added " + data);
       }
+      // set or clear the sortbytree flag.
+      if (aparser.contains("sortbytree"))
+      {
+        af.getViewport().setSortByTree(true);
+      }
+      if (aparser.contains("nosortbytree"))
+      {
+        af.getViewport().setSortByTree(false);
+      }
       data = aparser.getValue("tree");
       if (data != null)
       {
@@ -351,7 +458,7 @@ public class Jalview
     // We'll only open the default file if the desktop is visible.
     // And the user
     // ////////////////////
-    if (!headless && file == null
+    if (!headless && file == null && vamsasImport==null
             && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true))
     {
       file = jalview.bin.Cache.getDefault("STARTUP_FILE",
@@ -532,13 +639,15 @@ public class Jalview
     {
       String nickname = null;
       String url = null;
+      boolean seq=false,feat=true;
       int pos = data.indexOf('=');
+      // determine capabilities
       if (pos > 0)
       {
         nickname = data.substring(0, pos);
       }
       url = data.substring(pos + 1);
-      if (url != null && url.startsWith("http:"))
+      if (url != null && (url.startsWith("http:") || url.startsWith("sequence:http:")))
       {
         if (nickname == null)
         {