3 import jalview.analysis.AlignmentUtils;
4 import jalview.datamodel.AlignmentI;
5 import jalview.gui.AlignFrame;
6 import jalview.gui.SplitFrame;
7 import jalview.io.AppletFormatAdapter;
8 import jalview.io.DataSourceType;
9 import jalview.io.FileFormatException;
10 import jalview.io.FileFormatI;
11 import jalview.io.FileLoader;
12 import jalview.io.IdentifyFile;
13 import jalview.structure.StructureSelectionManager;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
20 import javax.swing.JFrame;
21 import javax.swing.JInternalFrame;
24 * Entry point for Jalview as Javascript. Expects parameter names as for the
25 * JalviewLite applet, formatted as for the Jalview application, for example
28 * JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
29 * PDBFile "/examples/pdb1.txt seq1"
32 * Note that (unlike the applet) parameter names are case sensitive
34 // TODO or format as file=/examples/uniref50.fa (etc)?
35 public class JalviewJS
37 private static final String PARAM_FILE = "file";
39 private static final String PARAM_FILE2 = "file2";
41 private static final String PARAM_TREE = "tree";
43 private static final String PARAM_FEATURES = "features";
45 private static final String PARAM_ANNOTATIONS = "annotations";
47 private static final String PARAM_SHOW_ANNOTATION = "showAnnotation";
49 private static final String PARAM_PDBFILE = "PDBFile";
51 private static final String PARAM_PROPS = "props";
53 private Map<String, String> params;
55 private List<String> pdbFileParams;
57 public static void main(String[] args)
61 new JalviewJS().doMain(args);
69 * Parses parameters and shows the frame and any loaded panels
71 * @throws FileFormatException
73 void doMain(String[] args) throws FileFormatException
76 if (getParameter(PARAM_FILE) == null)
87 * Answers the value of the given runtime parameter, or null if not provided
92 private String getParameter(String paramName)
94 return params.get(paramName);
98 * Prints a chastising, yet helpful, error message on syserr
103 "Usage: JalviewJS file <alignmentFile> [features <featuresFile>]");
104 System.err.println("See documentation for full parameter list");
108 * Parses any supplied parameters. Note that (unlike for the applet),
109 * parameter names are case sensitive.
113 * @see http://www.jalview.org/examples/index.html#appletParameters
115 void loadParameters(String[] args)
117 ArgsParser parser = new ArgsParser(args);
118 params = new HashMap<>();
120 // TODO javascript-friendly source of properties
121 Cache.loadProperties(parser.getValue(PARAM_PROPS));
122 loadParameter(parser, PARAM_FILE);
123 loadParameter(parser, PARAM_FILE2);
124 loadParameter(parser, PARAM_TREE);
125 loadParameter(parser, PARAM_FEATURES);
126 loadParameter(parser, PARAM_ANNOTATIONS);
127 loadParameter(parser, PARAM_SHOW_ANNOTATION);
128 pdbFileParams = loadPdbParameters(parser);
132 * Reads one command line parameter value and saves it against the parameter
133 * name. Note the saved value is null if the parameter is not present.
138 protected void loadParameter(ArgsParser parser, String param)
140 params.put(param, parser.getValue(param));
144 * Reads parameter PDBFile, PDBFile1, PDFile2, ... and saves the value(s) (if
150 List<String> loadPdbParameters(ArgsParser parser)
152 List<String> values = new ArrayList<>();
153 String value = parser.getValue(PARAM_PDBFILE);
161 value = parser.getValue(PARAM_PDBFILE + String.valueOf(i));
175 * Constructs and displays a JFrame containing an alignment panel (and any
176 * additional panels depending on parameters supplied)
178 * @throws FileFormatException
180 void showFrame() throws FileFormatException
182 JFrame frame = new JFrame(getParameter(PARAM_FILE));
183 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
186 * construct an AlignFrame (optionally with features)
188 AlignFrame alignFrame = createAlignFrame(PARAM_FILE);
189 loadFeatures(alignFrame, getParameter(PARAM_FEATURES));
191 JInternalFrame internalFrame = alignFrame;
194 * convert to SplitFrame if a valid file2 is supplied
196 AlignFrame alignFrame2 = createAlignFrame(PARAM_FILE2);
197 if (alignFrame2 != null)
199 SplitFrame splitFrame = loadSplitFrame(alignFrame, alignFrame2);
200 if (splitFrame != null)
202 internalFrame = splitFrame;
207 * move AlignFrame (or SplitFrame) menu bar and content pane to our frame
208 * TODO there may be a less obscure way to do this
210 frame.setContentPane(internalFrame.getContentPane());
211 frame.setJMenuBar(internalFrame.getJMenuBar());
213 // fudge so that dialogs can be opened with this frame as parent
214 // todo JAL-3031 also override Desktop.addInternalFrame etc
215 // Desktop.parent = frame.getContentPane();
218 frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
219 frame.setVisible(true);
223 * Constructs a SplitFrame if cdna-protein mappings can be made between the
224 * given alignment frames, else returns null. Any mappings made are registered
225 * with StructureSelectionManager to enable broadcast to listeners.
231 protected SplitFrame loadSplitFrame(AlignFrame alignFrame,
232 AlignFrame alignFrame2)
234 // code borrowed from AlignViewport.openLinkedAlignment
235 AlignmentI al1 = alignFrame.getViewport().getAlignment();
236 AlignmentI al2 = alignFrame2.getViewport().getAlignment();
237 boolean al1Nuc = al1.isNucleotide();
238 if (al1Nuc == al2.isNucleotide())
240 System.err.println("Can't make split frame as alignments are both "
241 + (al1Nuc ? "nucleotide" : "protein"));
244 AlignmentI protein = al1Nuc ? al2 : al1;
245 AlignmentI cdna = al1Nuc ? al1 : al2;
246 boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein,
250 System.err.println("Can't make any mappings for split frame");
255 * register sequence mappings
257 StructureSelectionManager ssm = StructureSelectionManager
258 .getStructureSelectionManager(null);
259 ssm.registerMappings(protein.getCodonFrames());
261 cdna.alignAs(protein);
263 SplitFrame splitFrame = new SplitFrame(
264 al1Nuc ? alignFrame : alignFrame2,
265 al1Nuc ? alignFrame2 : alignFrame);
271 * Loads on a features file if one was specified as a parameter
276 protected void loadFeatures(AlignFrame alignFrame, String featureFile)
278 if (featureFile != null)
280 // todo extract helper for protocol resolution from JalviewLite
281 DataSourceType sourceType = featureFile.startsWith("http")
283 : DataSourceType.FILE;
284 alignFrame.parseFeaturesFile(featureFile, sourceType);
289 * Constructs and returns the frame containing the alignment and its
290 * annotations. Returns null if the specified parameter value is not set.
295 * @throws FileFormatException
297 AlignFrame createAlignFrame(String fileParam) throws FileFormatException
299 AlignFrame af = null;
300 String file = getParameter(fileParam);
303 DataSourceType protocol = AppletFormatAdapter.checkProtocol(file);
304 FileFormatI format = new IdentifyFile().identify(file, protocol);
305 FileLoader fileLoader = new FileLoader(false);
306 af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format);