3 import jalview.analysis.AlignmentUtils;
4 import jalview.datamodel.AlignmentI;
5 import jalview.gui.AlignFrame;
6 import jalview.gui.SplitFrame;
7 import jalview.io.DataSourceType;
8 import jalview.io.FileFormatException;
9 import jalview.io.FileFormatI;
10 import jalview.io.FileLoader;
11 import jalview.io.IdentifyFile;
12 import jalview.structure.StructureSelectionManager;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
19 import javax.swing.JFrame;
20 import javax.swing.JInternalFrame;
24 * early work -- deprecated
26 * Entry point for Jalview as Javascript. Expects parameter names as for the
27 * JalviewLite applet, formatted as for the Jalview application, for example
30 * JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
31 * PDBFile "/examples/pdb1.txt seq1"
34 * Note that (unlike the applet) parameter names are case sensitive
36 // TODO or format as file=/examples/uniref50.fa (etc)?
37 public class JalviewJS
45 * thisApplet.__Info.args =
46 * ["file","examples/uniref50.fa","features",
47 * "examples/exampleFeatures.txt",
48 * "props","/Users/gmcarstairs/.jalview_properties"];
53 private static final String PARAM_FILE = "file";
55 private static final String PARAM_FILE2 = "file2";
57 private static final String PARAM_TREE = "tree";
59 private static final String PARAM_FEATURES = "features";
61 private static final String PARAM_ANNOTATIONS = "annotations";
63 private static final String PARAM_SHOW_ANNOTATION = "showAnnotation";
65 private static final String PARAM_PDBFILE = "PDBFile";
67 private static final String PARAM_PROPS = "props";
69 private Map<String, String> params;
71 private List<String> pdbFileParams;
73 public static void main(String[] args) throws Exception
75 new JalviewJS().doMain(args);
79 * Parses parameters and shows the frame and any loaded panels
81 * @throws FileFormatException
83 void doMain(String[] args) throws FileFormatException
86 if (getParameter(PARAM_FILE) == null)
97 * Answers the value of the given runtime parameter, or null if not provided
102 private String getParameter(String paramName)
104 return params.get(paramName);
108 * Prints a chastising, yet helpful, error message on syserr
113 "Usage: JalviewJS file <alignmentFile> [features <featuresFile>]");
114 System.err.println("See documentation for full parameter list");
118 * Parses any supplied parameters. Note that (unlike for the applet),
119 * parameter names are case sensitive.
123 * @see http://www.jalview.org/examples/index.html#appletParameters
125 void loadParameters(String[] args)
127 ArgsParser parser = new ArgsParser(args);
128 params = new HashMap<>();
130 // TODO javascript-friendly source of properties
131 Cache.loadProperties(parser.getValue(PARAM_PROPS));
132 loadParameter(parser, PARAM_FILE);
133 loadParameter(parser, PARAM_FILE2);
134 loadParameter(parser, PARAM_TREE);
135 loadParameter(parser, PARAM_FEATURES);
136 loadParameter(parser, PARAM_ANNOTATIONS);
137 loadParameter(parser, PARAM_SHOW_ANNOTATION);
138 pdbFileParams = loadPdbParameters(parser);
142 * Reads one command line parameter value and saves it against the parameter
143 * name. Note the saved value is null if the parameter is not present.
148 protected void loadParameter(ArgsParser parser, String param)
150 params.put(param, parser.getValue(param));
154 * Reads parameter PDBFile, PDBFile1, PDFile2, ... and saves the value(s) (if
160 List<String> loadPdbParameters(ArgsParser parser)
162 List<String> values = new ArrayList<>();
163 String value = parser.getValue(PARAM_PDBFILE);
171 value = parser.getValue(PARAM_PDBFILE + String.valueOf(i));
185 * Constructs and displays a JFrame containing an alignment panel (and any
186 * additional panels depending on parameters supplied)
188 * @throws FileFormatException
190 void showFrame() throws FileFormatException
192 JFrame frame = new JFrame(getParameter(PARAM_FILE));
193 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
196 * construct an AlignFrame (optionally with features)
198 AlignFrame alignFrame = createAlignFrame(PARAM_FILE);
199 loadFeatures(alignFrame, getParameter(PARAM_FEATURES));
201 JInternalFrame internalFrame = alignFrame;
204 * convert to SplitFrame if a valid file2 is supplied
206 AlignFrame alignFrame2 = createAlignFrame(PARAM_FILE2);
207 if (alignFrame2 != null)
209 SplitFrame splitFrame = loadSplitFrame(alignFrame, alignFrame2);
210 if (splitFrame != null)
212 internalFrame = splitFrame;
217 * move AlignFrame (or SplitFrame) menu bar and content pane to our frame
218 * TODO there may be a less obscure way to do this
220 frame.setContentPane(internalFrame.getContentPane());
221 frame.setJMenuBar(internalFrame.getJMenuBar());
223 // fudge so that dialogs can be opened with this frame as parent
224 // todo JAL-3031 also override Desktop.addInternalFrame etc
225 // Desktop.parent = frame.getContentPane();
228 frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
229 frame.setVisible(true);
233 * Constructs a SplitFrame if cdna-protein mappings can be made between the
234 * given alignment frames, else returns null. Any mappings made are registered
235 * with StructureSelectionManager to enable broadcast to listeners.
241 protected SplitFrame loadSplitFrame(AlignFrame alignFrame,
242 AlignFrame alignFrame2)
244 // code borrowed from AlignViewport.openLinkedAlignment
245 AlignmentI al1 = alignFrame.getViewport().getAlignment();
246 AlignmentI al2 = alignFrame2.getViewport().getAlignment();
247 boolean al1Nuc = al1.isNucleotide();
248 if (al1Nuc == al2.isNucleotide())
250 System.err.println("Can't make split frame as alignments are both "
251 + (al1Nuc ? "nucleotide" : "protein"));
254 AlignmentI protein = al1Nuc ? al2 : al1;
255 AlignmentI cdna = al1Nuc ? al1 : al2;
256 boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein,
260 System.err.println("Can't make any mappings for split frame");
265 * register sequence mappings
267 StructureSelectionManager ssm = StructureSelectionManager
268 .getStructureSelectionManager(null);
269 ssm.registerMappings(protein.getCodonFrames());
271 cdna.alignAs(protein);
273 SplitFrame splitFrame = new SplitFrame(
274 al1Nuc ? alignFrame : alignFrame2,
275 al1Nuc ? alignFrame2 : alignFrame);
281 * Loads on a features file if one was specified as a parameter
286 protected void loadFeatures(AlignFrame alignFrame, String featureFile)
288 if (featureFile != null)
290 // todo extract helper for protocol resolution from JalviewLite
291 DataSourceType sourceType = featureFile.startsWith("http")
293 : DataSourceType.RELATIVE_URL;
294 alignFrame.parseFeaturesFile(featureFile, sourceType);
299 * Constructs and returns the frame containing the alignment and its
300 * annotations. Returns null if the specified parameter value is not set.
305 * @throws FileFormatException
307 AlignFrame createAlignFrame(String fileParam) throws FileFormatException
309 AlignFrame af = null;
310 String file = getParameter(fileParam);
313 DataSourceType protocol = file.startsWith("http") ? DataSourceType.URL
314 : DataSourceType.RELATIVE_URL;
315 // DataSourceType protocol = AppletFormatAdapter.checkProtocol(file);
316 FileFormatI format = new IdentifyFile().identify(file, protocol);
317 FileLoader fileLoader = new FileLoader(false);
318 af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format);