From abb67d70ddbcaf52374cc4d611959317d18e012e Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 21 Jun 2018 12:37:33 +0100 Subject: [PATCH] JAL-3030 try to open a split frame if file and file2 are supplied --- src/jalview/bin/JalviewJS.java | 114 +++++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 20 deletions(-) diff --git a/src/jalview/bin/JalviewJS.java b/src/jalview/bin/JalviewJS.java index 1187bcf..18be43b 100644 --- a/src/jalview/bin/JalviewJS.java +++ b/src/jalview/bin/JalviewJS.java @@ -1,20 +1,24 @@ 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.Map; -import java.util.Objects; import javax.swing.JFrame; +import javax.swing.JInternalFrame; /** * Entry point for Jalview as Javascript. Expects parameter names as for the @@ -55,7 +59,7 @@ public class JalviewJS try { new JalviewJS().doMain(args); - } catch (FileFormatException e) + } catch (Throwable e) { e.printStackTrace(); } @@ -95,7 +99,8 @@ public class JalviewJS */ 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"); } @@ -178,28 +183,94 @@ public class JalviewJS frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* - * construct an AlignFrame and extract its contents and menu bar + * 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 */ - AlignFrame alignFrame = createAlignFrame(); - frame.setContentPane(alignFrame.getContentPane()); - frame.setJMenuBar(alignFrame.getJMenuBar()); + 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(); - loadFeatures(alignFrame, getParameter(PARAM_FEATURES)); - frame.pack(); frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); frame.setVisible(true); } /** - * Load on a features file if one was specified as a parameter + * 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) @@ -216,21 +287,24 @@ public class JalviewJS /** * 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 */ - AlignFrame createAlignFrame() throws FileFormatException + AlignFrame createAlignFrame(String fileParam) throws FileFormatException { - String file = getParameter(PARAM_FILE); - Objects.requireNonNull(file); - - DataSourceType protocol = AppletFormatAdapter.checkProtocol(file); - FileFormatI format = new IdentifyFile().identify(file, protocol); - FileLoader fileLoader = new FileLoader(false); - AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, 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; } -- 1.7.10.2