JAL-629 Open Structure Viewer from cmdline
[jalview.git] / src / jalview / bin / Commands.java
index 4239ec7..0cdc541 100644 (file)
@@ -15,8 +15,11 @@ import jalview.api.AlignmentViewPanel;
 import jalview.bin.ArgParser.Arg;
 import jalview.bin.ArgParser.ArgValues;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
+import jalview.gui.AlignmentPanel;
 import jalview.gui.Desktop;
+import jalview.gui.StructureChooser;
 import jalview.io.AppletFormatAdapter;
 import jalview.io.DataSourceType;
 import jalview.io.FileFormatException;
@@ -42,16 +45,19 @@ public class Commands
   {
     argParser = ap;
     headless = h;
-    for (String id : argParser.linkedIds())
+    if (argParser != null && argParser.linkedIds() != null)
     {
-      Commands cmds = new Commands();
-      if (id == null)
+      for (String id : argParser.linkedIds())
       {
-        cmds.processUnlinked(id);
-      }
-      else
-      {
-        cmds.processLinked(id);
+        Commands cmds = new Commands();
+        if (id == null)
+        {
+          cmds.processUnlinked(id);
+        }
+        else
+        {
+          cmds.processLinked(id);
+        }
       }
     }
   }
@@ -282,6 +288,87 @@ public class Commands
         }
       }
     }
+
+    // open the structure (from same PDB file or given PDBfile)
+    if (m.get(Arg.NOSTRUCTURE) == null
+            || !m.get(Arg.NOQUESTIONNAIRE).getBoolean())
+    {
+      AlignFrame af = afMap.get(id);
+      if (m.get(Arg.STRUCTURE) != null)
+      {
+        STRUCTURE: for (String val : m.get(Arg.STRUCTURE).getValues())
+        {
+          SubId subId = new SubId(val);
+          SequenceI seq = getSpecifiedSequence(af, subId);
+          if (seq == null)
+          {
+            Console.warn("Could not find sequence for argument --"
+                    + Arg.STRUCTURE + "=" + val);
+            break STRUCTURE;
+          }
+          File structureFile = null;
+          if (subId.content != null && subId.content.length() != 0)
+          {
+            structureFile = new File(subId.content);
+            Console.debug("Using structure file (from argument) '"
+                    + structureFile.getAbsolutePath() + "'");
+          }
+          /* THIS DOESN'T WORK */
+          else if (seq.getAllPDBEntries() != null
+                  && seq.getAllPDBEntries().size() > 0)
+          {
+            structureFile = new File(
+                    seq.getAllPDBEntries().elementAt(0).getFile());
+            Console.debug("Using structure file (from sequence) '"
+                    + structureFile.getAbsolutePath() + "'");
+          }
+
+          if (structureFile == null)
+          {
+            Console.warn("Not provided structure file with '" + val + "'");
+            continue STRUCTURE;
+          }
+
+          if (!structureFile.exists())
+          {
+            Console.warn("Structure file '"
+                    + structureFile.getAbsoluteFile() + "' not found.");
+            continue STRUCTURE;
+          }
+
+          Console.debug("Using structure file "
+                  + structureFile.getAbsolutePath());
+
+          // open structure view
+          AlignmentPanel ap = af.alignPanel;
+          StructureChooser.openStructureFileForSequence(ap, seq,
+                  structureFile);
+        }
+      }
+    }
+  }
+
+  private SequenceI getSpecifiedSequence(AlignFrame af, SubId subId)
+  {
+    SequenceI seq = null;
+    SequenceI[] sequences = af.getCurrentView().getAlignment()
+            .getSequencesArray();
+    if (-1 < subId.index && subId.index < sequences.length)
+    {
+      seq = sequences[subId.index];
+    }
+    else if ("id".equals(subId.keyName))
+    {
+      for (SequenceI s : sequences)
+      {
+        if (s.getDisplayId(false).equals(subId.keyValue))
+        {
+          seq = s;
+          break;
+        }
+      }
+    }
+    return seq;
   }
 
   /**