From c544a7491c89cc4bb1ea1533940b3b325f4c35ec Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 19 Jun 2018 12:02:52 +0100 Subject: [PATCH] JAL-3030 first cut of JalviewJS.main entry point accepting 'file' parameter --- src/jalview/bin/JalviewJS.java | 190 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 src/jalview/bin/JalviewJS.java diff --git a/src/jalview/bin/JalviewJS.java b/src/jalview/bin/JalviewJS.java new file mode 100644 index 0000000..c20e47b --- /dev/null +++ b/src/jalview/bin/JalviewJS.java @@ -0,0 +1,190 @@ +package jalview.bin; + +import jalview.gui.AlignFrame; +import jalview.io.AppletFormatAdapter; +import jalview.io.DataSourceType; +import jalview.io.FileFormatException; +import jalview.io.FileFormatI; +import jalview.io.FileLoader; +import jalview.io.IdentifyFile; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import javax.swing.JFrame; +import javax.swing.JInternalFrame; + +/** + * Entry point for Jalview as Javascript. Expects parameter names as for the + * JalviewLite applet, formatted as for the Jalview application, for example + * + *
+ *   JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
+ *       PDBFile "/examples/pdb1.txt seq1"
+ * 
+ * + * Note that (unlike the applet) parameter names are case sensitive + */ +// TODO or format as file=/examples/uniref50.fa (etc)? +public class JalviewJS +{ + private static final String PARAM_FILE = "file"; + + private static final String PARAM_FILE2 = "file2"; + + private static final String PARAM_TREE = "tree"; + + private static final String PARAM_FEATURES = "features"; + + private static final String PARAM_ANNOTATIONS = "annotations"; + + private static final String PARAM_SHOW_ANNOTATION = "showAnnotation"; + + private static final String PARAM_PDBFILE = "PDBFile"; + + public static void main(String[] args) + { + try + { + new JalviewJS().doMain(args); + } catch (FileFormatException e) + { + e.printStackTrace(); + } + } + + private String paramFile; + + private String paramFile2; + + private String paramTree; + + private String paramFeatures; + + private String paramAnnotations; + + private String paramShowAnnotation; + + private List paramPdbFile; + + /** + * Parses parameters and shows the frame and any loaded panels + * + * @throws FileFormatException + */ + void doMain(String[] args) throws FileFormatException + { + loadParameters(args); + if (paramFile == null) + { + usage(); + } + else + { + showFrame(); + } + } + + /** + * Prints a chastising, yet helpful, error message on syserr + */ + private void usage() + { + System.err.println("Usage: JalviewJS file "); + System.err.println("See documentation for full parameter list"); + } + + /** + * Parses any supplied parameters. Note that (unlike for the applet), + * parameter names are case sensitive. + * + * @param args + * + * @see http://www.jalview.org/examples/index.html#appletParameters + */ + void loadParameters(String[] args) + { + ArgsParser parser = new ArgsParser(args); + paramFile = parser.getValue(PARAM_FILE); + paramFile2 = parser.getValue(PARAM_FILE2); + paramTree = parser.getValue(PARAM_TREE); + paramFeatures = parser.getValue(PARAM_FEATURES); + paramAnnotations = parser.getValue(PARAM_ANNOTATIONS); + paramShowAnnotation = parser.getValue(PARAM_SHOW_ANNOTATION); + paramPdbFile = loadPdbParameters(parser); + } + + /** + * Reads parameter PDBFile, PDBFile1, PDFile2, ... and saves the value(s) (if + * any) + * + * @param parser + * @return + */ + List loadPdbParameters(ArgsParser parser) + { + List values = new ArrayList<>(); + String value = parser.getValue(PARAM_PDBFILE); + if (value != null) + { + values.add(value); + } + int i = 1; + while (true) + { + value = parser.getValue(PARAM_PDBFILE + String.valueOf(i)); + if (value != null) + { + values.add(value); + } + else + { + break; + } + } + return values; + } + + /** + * Constructs and displays a JFrame containing an alignment panel (and any + * additional panels depending on parameters supplied) + * + * @throws FileFormatException + */ + void showFrame() throws FileFormatException + { + JFrame frame = new JFrame(this.paramFile); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + JInternalFrame alignFrame = createAlignFrame(); + frame.setContentPane(alignFrame.getContentPane()); + + frame.pack(); + frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); + frame.setVisible(true); + } + + /** + * Constructs and returns the frame containing the alignment and its + * annotations + * + * @return + * @throws FileFormatException + */ + JInternalFrame createAlignFrame() throws FileFormatException + { + Objects.requireNonNull(this.paramFile); + + DataSourceType protocol = AppletFormatAdapter + .checkProtocol(this.paramFile); + FileFormatI format = new IdentifyFile().identify(this.paramFile, + protocol); + FileLoader fileLoader = new FileLoader(false); + AlignFrame af = fileLoader.LoadFileWaitTillLoaded(this.paramFile, + protocol, format); + + return af; + } + +} -- 1.7.10.2