3 import jalview.gui.AlignFrame;
4 import jalview.gui.Desktop;
5 import jalview.io.AppletFormatAdapter;
6 import jalview.io.DataSourceType;
7 import jalview.io.FileFormatException;
8 import jalview.io.FileFormatI;
9 import jalview.io.FileLoader;
10 import jalview.io.IdentifyFile;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Objects;
18 import javax.swing.JFrame;
21 * Entry point for Jalview as Javascript. Expects parameter names as for the
22 * JalviewLite applet, formatted as for the Jalview application, for example
25 * JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
26 * PDBFile "/examples/pdb1.txt seq1"
29 * Note that (unlike the applet) parameter names are case sensitive
31 // TODO or format as file=/examples/uniref50.fa (etc)?
32 public class JalviewJS
34 private static final String PARAM_FILE = "file";
36 private static final String PARAM_FILE2 = "file2";
38 private static final String PARAM_TREE = "tree";
40 private static final String PARAM_FEATURES = "features";
42 private static final String PARAM_ANNOTATIONS = "annotations";
44 private static final String PARAM_SHOW_ANNOTATION = "showAnnotation";
46 private static final String PARAM_PDBFILE = "PDBFile";
48 private static final String PARAM_PROPS = "props";
50 private Map<String, String> params;
52 private List<String> pdbFileParams;
54 public static void main(String[] args)
58 new JalviewJS().doMain(args);
59 } catch (FileFormatException e)
66 * Parses parameters and shows the frame and any loaded panels
68 * @throws FileFormatException
70 void doMain(String[] args) throws FileFormatException
73 if (getParameter(PARAM_FILE) == null)
84 * Answers the value of the given runtime parameter, or null if not provided
89 private String getParameter(String paramName)
91 return params.get(paramName);
95 * Prints a chastising, yet helpful, error message on syserr
99 System.err.println("Usage: JalviewJS file <alignmentFile>");
100 System.err.println("See documentation for full parameter list");
104 * Parses any supplied parameters. Note that (unlike for the applet),
105 * parameter names are case sensitive.
109 * @see http://www.jalview.org/examples/index.html#appletParameters
111 void loadParameters(String[] args)
113 ArgsParser parser = new ArgsParser(args);
114 params = new HashMap<>();
116 // TODO javascript-friendly source of properties
117 Cache.loadProperties(parser.getValue(PARAM_PROPS));
118 loadParameter(parser, PARAM_FILE);
119 loadParameter(parser, PARAM_FILE2);
120 loadParameter(parser, PARAM_TREE);
121 loadParameter(parser, PARAM_FEATURES);
122 loadParameter(parser, PARAM_ANNOTATIONS);
123 loadParameter(parser, PARAM_SHOW_ANNOTATION);
124 pdbFileParams = loadPdbParameters(parser);
128 * Reads one command line parameter value and saves it against the parameter
129 * name. Note the saved value is null if the parameter is not present.
134 protected void loadParameter(ArgsParser parser, String param)
136 params.put(param, parser.getValue(param));
140 * Reads parameter PDBFile, PDBFile1, PDFile2, ... and saves the value(s) (if
146 List<String> loadPdbParameters(ArgsParser parser)
148 List<String> values = new ArrayList<>();
149 String value = parser.getValue(PARAM_PDBFILE);
157 value = parser.getValue(PARAM_PDBFILE + String.valueOf(i));
171 * Constructs and displays a JFrame containing an alignment panel (and any
172 * additional panels depending on parameters supplied)
174 * @throws FileFormatException
176 void showFrame() throws FileFormatException
178 JFrame frame = new JFrame(getParameter(PARAM_FILE));
179 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
182 * construct an AlignFrame and extract its contents and menu bar
183 * TODO there may be a less obscure way to do this
185 AlignFrame alignFrame = createAlignFrame();
186 frame.setContentPane(alignFrame.getContentPane());
187 frame.setJMenuBar(alignFrame.getJMenuBar());
189 // fudge so that dialogs can be opened with this frame as parent
190 Desktop.parent = frame.getContentPane();
192 loadFeatures(alignFrame, getParameter(PARAM_FEATURES));
195 frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
196 frame.setVisible(true);
200 * Load on a features file if one was specified as a parameter
206 protected void loadFeatures(AlignFrame alignFrame, String featureFile)
208 if (featureFile != null)
210 // todo extract helper for protocol resolution from JalviewLite
211 DataSourceType sourceType = featureFile.startsWith("http")
213 : DataSourceType.FILE;
214 alignFrame.parseFeaturesFile(featureFile, sourceType);
219 * Constructs and returns the frame containing the alignment and its
223 * @throws FileFormatException
225 AlignFrame createAlignFrame() throws FileFormatException
227 String file = getParameter(PARAM_FILE);
228 Objects.requireNonNull(file);
230 DataSourceType protocol = AppletFormatAdapter.checkProtocol(file);
231 FileFormatI format = new IdentifyFile().identify(file, protocol);
232 FileLoader fileLoader = new FileLoader(false);
233 AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol,