JAL-629 Open Structure Viewer from cmdline
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 14 Dec 2022 10:45:27 +0000 (10:45 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 14 Dec 2022 10:45:27 +0000 (10:45 +0000)
src/jalview/bin/ArgParser.java
src/jalview/bin/Commands.java
src/jalview/gui/StructureChooser.java

index 0f9ac2e..b02bfbc 100644 (file)
@@ -58,7 +58,8 @@ public class ArgParser
     ANNOTATION2, DISPLAY, GUI, NEWS, NOQUESTIONNAIRE, SORTBYTREE,
     USAGESTATS, OPEN, OPEN2, PROPS, QUESTIONNAIRE, SETPROP, TREE, VDOC,
     VSESS, OUTPUT, OUTPUTTYPE, SSANNOTATION, NOTEMPFAC, TEMPFAC,
-    TEMPFAC_LABEL, TEMPFAC_DESC, TEMPFAC_SHADING, TITLE, PAEMATRIX, WRAP;
+    TEMPFAC_LABEL, TEMPFAC_DESC, TEMPFAC_SHADING, TITLE, PAEMATRIX, WRAP,
+    NOSTRUCTURE, STRUCTURE;
 
     static
     {
@@ -105,6 +106,8 @@ public class ArgParser
       TEMPFAC_SHADING.setOptions(Opt.STRING, Opt.LINKED);
       TITLE.setOptions(Opt.STRING, Opt.LINKED);
       PAEMATRIX.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
+      NOSTRUCTURE.setOptions(Opt.UNARY, Opt.LINKED);
+      STRUCTURE.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI);
       WRAP.setOptions(Opt.BOOLEAN, Opt.LINKED);
     }
 
index 552dda6..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;
@@ -285,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;
   }
 
   /**
index 07eec2b..89d5c6b 100644 (file)
@@ -24,6 +24,7 @@ package jalview.gui;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
@@ -116,11 +117,19 @@ public class StructureChooser extends GStructureChooser
 
   List<SequenceI> seqsWithoutSourceDBRef = null;
 
+  private boolean noChooserGUI = false;
+
   private static StructureViewer lastTargetedView = null;
 
   public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
           AlignmentPanel ap)
   {
+    this(selectedSeqs, selectedSeq, ap, false);
+  }
+
+  public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
+          AlignmentPanel ap, boolean noChooserGUI)
+  {
     // which FTS engine to use
     data = StructureChooserQuerySource.getQuerySourceFor(selectedSeqs);
     initDialog();
@@ -129,6 +138,7 @@ public class StructureChooser extends GStructureChooser
     this.selectedSequence = selectedSeq;
     this.selectedSequences = selectedSeqs;
     this.progressIndicator = (ap == null) ? null : ap.alignFrame;
+    this.noChooserGUI = noChooserGUI;
     init();
 
   }
@@ -244,7 +254,7 @@ public class StructureChooser extends GStructureChooser
     populateFilterComboBox(isStructuresDiscovered(), cachedPDBExists);
     discoverStructureViews();
     updateProgressIndicator(null, startTime);
-    mainFrame.setVisible(true);
+    mainFrame.setVisible(noChooserGUI);
     updateCurrentView();
   }
 
@@ -1576,4 +1586,33 @@ public class StructureChooser extends GStructureChooser
   {
     return notQueriedTDBYet;
   }
+
+  /**
+   * Open a single structure file for a given sequence
+   */
+  public static void openStructureFileForSequence(AlignmentPanel ap,
+          SequenceI seq, File sFile)
+  {
+    StructureChooser sc = new StructureChooser(new SequenceI[] { seq }, seq,
+            ap, true);
+    StructureSelectionManager ssm = ap.getStructureSelectionManager();
+    PDBEntry fileEntry = null;
+    try
+    {
+      fileEntry = new AssociatePdbFileWithSeq().associatePdbWithSeq(
+              sFile.getAbsolutePath(), DataSourceType.FILE, seq, true,
+              Desktop.instance);
+    } catch (Exception e)
+    {
+      Console.error("Could not open structure file '"
+              + sFile.getAbsolutePath() + "'");
+      return;
+    }
+
+    StructureViewer sViewer = sc.launchStructureViewer(ssm,
+            new PDBEntry[]
+            { fileEntry }, ap, new SequenceI[] { seq });
+
+    sc.mainFrame.dispose();
+  }
 }