3 import jalview.analysis.AlignmentUtils;
4 import jalview.datamodel.AlignmentI;
5 import jalview.gui.AlignFrame;
6 import jalview.gui.Desktop;
7 import jalview.gui.SplitFrame;
8 import jalview.io.AppletFormatAdapter;
9 import jalview.io.DataSourceType;
10 import jalview.io.FileFormatException;
11 import jalview.io.FileFormatI;
12 import jalview.io.FileLoader;
13 import jalview.io.IdentifyFile;
14 import jalview.structure.StructureSelectionManager;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
21 import javax.swing.JFrame;
22 import javax.swing.JInternalFrame;
25 * Entry point for Jalview as Javascript. Expects parameter names as for the
26 * JalviewLite applet, formatted as for the Jalview application, for example
29 * JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
30 * PDBFile "/examples/pdb1.txt seq1"
33 * Note that (unlike the applet) parameter names are case sensitive
35 // TODO or format as file=/examples/uniref50.fa (etc)?
36 public class JalviewJS
38 private static final String PARAM_FILE = "file";
40 private static final String PARAM_FILE2 = "file2";
42 private static final String PARAM_TREE = "tree";
44 private static final String PARAM_FEATURES = "features";
46 private static final String PARAM_ANNOTATIONS = "annotations";
48 private static final String PARAM_SHOW_ANNOTATION = "showAnnotation";
50 private static final String PARAM_PDBFILE = "PDBFile";
52 private static final String PARAM_PROPS = "props";
54 private Map<String, String> params;
56 private List<String> pdbFileParams;
58 public static void main(String[] args) throws Exception
60 new JalviewJS().doMain(args);
64 * Parses parameters and shows the frame and any loaded panels
66 * @throws FileFormatException
68 void doMain(String[] args) throws FileFormatException
71 if (getParameter(PARAM_FILE) == null)
82 * Answers the value of the given runtime parameter, or null if not provided
87 private String getParameter(String paramName)
89 return params.get(paramName);
93 * Prints a chastising, yet helpful, error message on syserr
98 "Usage: JalviewJS file <alignmentFile> [features <featuresFile>]");
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 (optionally with features)
183 AlignFrame alignFrame = createAlignFrame(PARAM_FILE);
184 loadFeatures(alignFrame, getParameter(PARAM_FEATURES));
186 JInternalFrame internalFrame = alignFrame;
189 * convert to SplitFrame if a valid file2 is supplied
191 int height = AlignFrame.DEFAULT_HEIGHT;
192 AlignFrame alignFrame2 = createAlignFrame(PARAM_FILE2);
193 if (alignFrame2 != null)
195 SplitFrame splitFrame = loadSplitFrame(alignFrame, alignFrame2);
196 if (splitFrame != null)
198 internalFrame = splitFrame;
204 * move AlignFrame (or SplitFrame) menu bar and content pane to our frame
205 * TODO there may be a less obscure way to do this
207 frame.setContentPane(internalFrame.getContentPane());
208 frame.setJMenuBar(internalFrame.getJMenuBar());
210 // fudge so that dialogs can be opened with this frame as parent
211 // todo JAL-3031 also override Desktop.addInternalFrame etc
212 Desktop.parent = frame.getContentPane();
215 frame.setSize(AlignFrame.DEFAULT_WIDTH, height);
216 frame.setVisible(true);
220 * Constructs a SplitFrame if cdna-protein mappings can be made between the
221 * given alignment frames, else returns null. Any mappings made are registered
222 * with StructureSelectionManager to enable broadcast to listeners.
228 protected SplitFrame loadSplitFrame(AlignFrame alignFrame,
229 AlignFrame alignFrame2)
231 // code borrowed from AlignViewport.openLinkedAlignment
232 AlignmentI al1 = alignFrame.getViewport().getAlignment();
233 AlignmentI al2 = alignFrame2.getViewport().getAlignment();
234 boolean al1Nuc = al1.isNucleotide();
235 if (al1Nuc == al2.isNucleotide())
237 System.err.println("Can't make split frame as alignments are both "
238 + (al1Nuc ? "nucleotide" : "protein"));
241 AlignmentI protein = al1Nuc ? al2 : al1;
242 AlignmentI cdna = al1Nuc ? al1 : al2;
243 boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein,
247 System.err.println("Can't make any mappings for split frame");
252 * register sequence mappings
254 StructureSelectionManager ssm = StructureSelectionManager
255 .getStructureSelectionManager(null);
256 ssm.registerMappings(protein.getCodonFrames());
258 cdna.alignAs(protein);
260 SplitFrame splitFrame = new SplitFrame(
261 al1Nuc ? alignFrame : alignFrame2,
262 al1Nuc ? alignFrame2 : alignFrame);
268 * Loads on a features file if one was specified as a parameter
273 protected void loadFeatures(AlignFrame alignFrame, String featureFile)
275 if (featureFile != null)
277 // todo extract helper for protocol resolution from JalviewLite
278 DataSourceType sourceType = featureFile.startsWith("http")
280 : DataSourceType.FILE;
281 alignFrame.parseFeaturesFile(featureFile, sourceType);
286 * Constructs and returns the frame containing the alignment and its
287 * annotations. Returns null if the specified parameter value is not set.
292 * @throws FileFormatException
294 AlignFrame createAlignFrame(String fileParam) throws FileFormatException
296 AlignFrame af = null;
297 String file = getParameter(fileParam);
300 DataSourceType protocol = AppletFormatAdapter.checkProtocol(file);
301 FileFormatI format = new IdentifyFile().identify(file, protocol);
302 FileLoader fileLoader = new FileLoader(false);
303 af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format);