3 import jalview.gui.AlignFrame;
4 import jalview.io.AppletFormatAdapter;
5 import jalview.io.DataSourceType;
6 import jalview.io.FileFormatException;
7 import jalview.io.FileFormatI;
8 import jalview.io.FileLoader;
9 import jalview.io.IdentifyFile;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Objects;
17 import javax.swing.JFrame;
20 * Entry point for Jalview as Javascript. Expects parameter names as for the
21 * JalviewLite applet, formatted as for the Jalview application, for example
24 * JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
25 * PDBFile "/examples/pdb1.txt seq1"
28 * Note that (unlike the applet) parameter names are case sensitive
30 // TODO or format as file=/examples/uniref50.fa (etc)?
31 public class JalviewJS
33 private static final String PARAM_FILE = "file";
35 private static final String PARAM_FILE2 = "file2";
37 private static final String PARAM_TREE = "tree";
39 private static final String PARAM_FEATURES = "features";
41 private static final String PARAM_ANNOTATIONS = "annotations";
43 private static final String PARAM_SHOW_ANNOTATION = "showAnnotation";
45 private static final String PARAM_PDBFILE = "PDBFile";
47 private static final String PARAM_PROPS = "props";
49 private Map<String, String> params;
51 private List<String> pdbFileParams;
53 public static void main(String[] args)
57 new JalviewJS().doMain(args);
58 } catch (FileFormatException e)
65 * Parses parameters and shows the frame and any loaded panels
67 * @throws FileFormatException
69 void doMain(String[] args) throws FileFormatException
72 if (getParameter(PARAM_FILE) == null)
83 * Answers the value of the given runtime parameter, or null if not provided
88 private String getParameter(String paramName)
90 return params.get(paramName);
94 * Prints a chastising, yet helpful, error message on syserr
98 System.err.println("Usage: JalviewJS file <alignmentFile>");
99 System.err.println("See documentation for full parameter list");
103 * Parses any supplied parameters. Note that (unlike for the applet),
104 * parameter names are case sensitive.
108 * @see http://www.jalview.org/examples/index.html#appletParameters
110 void loadParameters(String[] args)
112 ArgsParser parser = new ArgsParser(args);
113 params = new HashMap<>();
115 // TODO javascript-friendly source of properties
116 Cache.loadProperties(parser.getValue(PARAM_PROPS));
117 loadParameter(parser, PARAM_FILE);
118 loadParameter(parser, PARAM_FILE2);
119 loadParameter(parser, PARAM_TREE);
120 loadParameter(parser, PARAM_FEATURES);
121 loadParameter(parser, PARAM_ANNOTATIONS);
122 loadParameter(parser, PARAM_SHOW_ANNOTATION);
123 pdbFileParams = loadPdbParameters(parser);
127 * Reads one command line parameter value and saves it against the parameter
128 * name. Note the saved value is null if the parameter is not present.
133 protected void loadParameter(ArgsParser parser, String param)
135 params.put(param, parser.getValue(param));
139 * Reads parameter PDBFile, PDBFile1, PDFile2, ... and saves the value(s) (if
145 List<String> loadPdbParameters(ArgsParser parser)
147 List<String> values = new ArrayList<>();
148 String value = parser.getValue(PARAM_PDBFILE);
156 value = parser.getValue(PARAM_PDBFILE + String.valueOf(i));
170 * Constructs and displays a JFrame containing an alignment panel (and any
171 * additional panels depending on parameters supplied)
173 * @throws FileFormatException
175 void showFrame() throws FileFormatException
177 JFrame frame = new JFrame(getParameter(PARAM_FILE));
178 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
181 * construct an AlignFrame and extract its contents and menu bar
182 * TODO there may be a less obscure way to do this
184 AlignFrame alignFrame = createAlignFrame();
185 frame.setContentPane(alignFrame.getContentPane());
186 frame.setJMenuBar(alignFrame.getJMenuBar());
188 // fudge so that dialogs can be opened with this frame as parent
189 // Desktop.parent = frame.getContentPane();
191 loadFeatures(alignFrame, getParameter(PARAM_FEATURES));
194 frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
195 frame.setVisible(true);
199 * Load on a features file if one was specified as a parameter
205 protected void loadFeatures(AlignFrame alignFrame, String featureFile)
207 if (featureFile != null)
209 // todo extract helper for protocol resolution from JalviewLite
210 DataSourceType sourceType = featureFile.startsWith("http")
212 : DataSourceType.FILE;
213 alignFrame.parseFeaturesFile(featureFile, sourceType);
218 * Constructs and returns the frame containing the alignment and its
222 * @throws FileFormatException
224 AlignFrame createAlignFrame() throws FileFormatException
226 String file = getParameter(PARAM_FILE);
227 Objects.requireNonNull(file);
229 DataSourceType protocol = AppletFormatAdapter.checkProtocol(file);
230 FileFormatI format = new IdentifyFile().identify(file, protocol);
231 FileLoader fileLoader = new FileLoader(false);
232 AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol,