X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2FJalviewJS.java;h=2f8e61bf4442c65c65c8f8a4aab07a8eff67bb65;hb=4cc5a82175c051403f3838b08b9b52bd8b2ebc73;hp=c20e47b0dddbd59edb78acd5c736686a41604a7e;hpb=c544a7491c89cc4bb1ea1533940b3b325f4c35ec;p=jalview.git diff --git a/src/jalview/bin/JalviewJS.java b/src/jalview/bin/JalviewJS.java index c20e47b..2f8e61b 100644 --- a/src/jalview/bin/JalviewJS.java +++ b/src/jalview/bin/JalviewJS.java @@ -1,16 +1,21 @@ package jalview.bin; +import jalview.analysis.AlignmentUtils; +import jalview.datamodel.AlignmentI; import jalview.gui.AlignFrame; +import jalview.gui.SplitFrame; import jalview.io.AppletFormatAdapter; import jalview.io.DataSourceType; import jalview.io.FileFormatException; import jalview.io.FileFormatI; import jalview.io.FileLoader; import jalview.io.IdentifyFile; +import jalview.structure.StructureSelectionManager; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.Objects; +import java.util.Map; import javax.swing.JFrame; import javax.swing.JInternalFrame; @@ -43,30 +48,16 @@ public class JalviewJS private static final String PARAM_PDBFILE = "PDBFile"; - public static void main(String[] args) - { - try - { - new JalviewJS().doMain(args); - } catch (FileFormatException e) - { - e.printStackTrace(); - } - } - - private String paramFile; - - private String paramFile2; - - private String paramTree; + private static final String PARAM_PROPS = "props"; - private String paramFeatures; + private Map params; - private String paramAnnotations; + private List pdbFileParams; - private String paramShowAnnotation; - - private List paramPdbFile; + public static void main(String[] args) throws Exception + { + new JalviewJS().doMain(args); + } /** * Parses parameters and shows the frame and any loaded panels @@ -76,7 +67,7 @@ public class JalviewJS void doMain(String[] args) throws FileFormatException { loadParameters(args); - if (paramFile == null) + if (getParameter(PARAM_FILE) == null) { usage(); } @@ -87,11 +78,23 @@ public class JalviewJS } /** + * Answers the value of the given runtime parameter, or null if not provided + * + * @param paramName + * @return + */ + private String getParameter(String paramName) + { + return params.get(paramName); + } + + /** * Prints a chastising, yet helpful, error message on syserr */ private void usage() { - System.err.println("Usage: JalviewJS file "); + System.err.println( + "Usage: JalviewJS file [features ]"); System.err.println("See documentation for full parameter list"); } @@ -106,13 +109,29 @@ public class JalviewJS void loadParameters(String[] args) { ArgsParser parser = new ArgsParser(args); - paramFile = parser.getValue(PARAM_FILE); - paramFile2 = parser.getValue(PARAM_FILE2); - paramTree = parser.getValue(PARAM_TREE); - paramFeatures = parser.getValue(PARAM_FEATURES); - paramAnnotations = parser.getValue(PARAM_ANNOTATIONS); - paramShowAnnotation = parser.getValue(PARAM_SHOW_ANNOTATION); - paramPdbFile = loadPdbParameters(parser); + params = new HashMap<>(); + + // TODO javascript-friendly source of properties + Cache.loadProperties(parser.getValue(PARAM_PROPS)); + loadParameter(parser, PARAM_FILE); + loadParameter(parser, PARAM_FILE2); + loadParameter(parser, PARAM_TREE); + loadParameter(parser, PARAM_FEATURES); + loadParameter(parser, PARAM_ANNOTATIONS); + loadParameter(parser, PARAM_SHOW_ANNOTATION); + pdbFileParams = loadPdbParameters(parser); + } + + /** + * Reads one command line parameter value and saves it against the parameter + * name. Note the saved value is null if the parameter is not present. + * + * @param parser + * @param param + */ + protected void loadParameter(ArgsParser parser, String param) + { + params.put(param, parser.getValue(param)); } /** @@ -154,11 +173,40 @@ public class JalviewJS */ void showFrame() throws FileFormatException { - JFrame frame = new JFrame(this.paramFile); + JFrame frame = new JFrame(getParameter(PARAM_FILE)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - JInternalFrame alignFrame = createAlignFrame(); - frame.setContentPane(alignFrame.getContentPane()); + /* + * construct an AlignFrame (optionally with features) + */ + AlignFrame alignFrame = createAlignFrame(PARAM_FILE); + loadFeatures(alignFrame, getParameter(PARAM_FEATURES)); + + JInternalFrame internalFrame = alignFrame; + + /* + * convert to SplitFrame if a valid file2 is supplied + */ + AlignFrame alignFrame2 = createAlignFrame(PARAM_FILE2); + if (alignFrame2 != null) + { + SplitFrame splitFrame = loadSplitFrame(alignFrame, alignFrame2); + if (splitFrame != null) + { + internalFrame = splitFrame; + } + } + + /* + * move AlignFrame (or SplitFrame) menu bar and content pane to our frame + * TODO there may be a less obscure way to do this + */ + frame.setContentPane(internalFrame.getContentPane()); + frame.setJMenuBar(internalFrame.getJMenuBar()); + + // fudge so that dialogs can be opened with this frame as parent + // todo JAL-3031 also override Desktop.addInternalFrame etc + // Desktop.parent = frame.getContentPane(); frame.pack(); frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); @@ -166,23 +214,91 @@ public class JalviewJS } /** + * Constructs a SplitFrame if cdna-protein mappings can be made between the + * given alignment frames, else returns null. Any mappings made are registered + * with StructureSelectionManager to enable broadcast to listeners. + * + * @param alignFrame + * @param alignFrame2 + * @return + */ + protected SplitFrame loadSplitFrame(AlignFrame alignFrame, + AlignFrame alignFrame2) + { + // code borrowed from AlignViewport.openLinkedAlignment + AlignmentI al1 = alignFrame.getViewport().getAlignment(); + AlignmentI al2 = alignFrame2.getViewport().getAlignment(); + boolean al1Nuc = al1.isNucleotide(); + if (al1Nuc == al2.isNucleotide()) + { + System.err.println("Can't make split frame as alignments are both " + + (al1Nuc ? "nucleotide" : "protein")); + return null; + } + AlignmentI protein = al1Nuc ? al2 : al1; + AlignmentI cdna = al1Nuc ? al1 : al2; + boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein, + cdna); + if (!mapped) + { + System.err.println("Can't make any mappings for split frame"); + return null; + } + + /* + * register sequence mappings + */ + StructureSelectionManager ssm = StructureSelectionManager + .getStructureSelectionManager(null); + ssm.registerMappings(protein.getCodonFrames()); + + cdna.alignAs(protein); + + SplitFrame splitFrame = new SplitFrame( + al1Nuc ? alignFrame : alignFrame2, + al1Nuc ? alignFrame2 : alignFrame); + + return splitFrame; + } + + /** + * Loads on a features file if one was specified as a parameter + * + * @param alignFrame + * @param featureFile + */ + protected void loadFeatures(AlignFrame alignFrame, String featureFile) + { + if (featureFile != null) + { + // todo extract helper for protocol resolution from JalviewLite + DataSourceType sourceType = featureFile.startsWith("http") + ? DataSourceType.URL + : DataSourceType.FILE; + alignFrame.parseFeaturesFile(featureFile, sourceType); + } + } + + /** * Constructs and returns the frame containing the alignment and its - * annotations + * annotations. Returns null if the specified parameter value is not set. + * + * @param fileParam * * @return * @throws FileFormatException */ - JInternalFrame createAlignFrame() throws FileFormatException + AlignFrame createAlignFrame(String fileParam) throws FileFormatException { - Objects.requireNonNull(this.paramFile); - - DataSourceType protocol = AppletFormatAdapter - .checkProtocol(this.paramFile); - FileFormatI format = new IdentifyFile().identify(this.paramFile, - protocol); - FileLoader fileLoader = new FileLoader(false); - AlignFrame af = fileLoader.LoadFileWaitTillLoaded(this.paramFile, - protocol, format); + AlignFrame af = null; + String file = getParameter(fileParam); + if (file != null) + { + DataSourceType protocol = AppletFormatAdapter.checkProtocol(file); + FileFormatI format = new IdentifyFile().identify(file, protocol); + FileLoader fileLoader = new FileLoader(false); + af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format); + } return af; }