package jalview.bin;
import jalview.gui.AlignFrame;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.swing.JFrame;
/**
* Entry point for Jalview as Javascript. Expects parameter names as for the
* JalviewLite applet, formatted as for the Jalview application, for example
*
*
* JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
* PDBFile "/examples/pdb1.txt seq1"
*
*
* Note that (unlike the applet) parameter names are case sensitive
*/
// TODO or format as file=/examples/uniref50.fa (etc)?
public class JalviewJS
{
private static final String PARAM_FILE = "file";
private static final String PARAM_FILE2 = "file2";
private static final String PARAM_TREE = "tree";
private static final String PARAM_FEATURES = "features";
private static final String PARAM_ANNOTATIONS = "annotations";
private static final String PARAM_SHOW_ANNOTATION = "showAnnotation";
private static final String PARAM_PDBFILE = "PDBFile";
private static final String PARAM_PROPS = "props";
private Map params;
private List pdbFileParams;
public static void main(String[] args)
{
try
{
new JalviewJS().doMain(args);
} catch (FileFormatException e)
{
e.printStackTrace();
}
}
/**
* Parses parameters and shows the frame and any loaded panels
*
* @throws FileFormatException
*/
void doMain(String[] args) throws FileFormatException
{
loadParameters(args);
if (getParameter(PARAM_FILE) == null)
{
usage();
}
else
{
showFrame();
}
}
/**
* 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("See documentation for full parameter list");
}
/**
* Parses any supplied parameters. Note that (unlike for the applet),
* parameter names are case sensitive.
*
* @param args
*
* @see http://www.jalview.org/examples/index.html#appletParameters
*/
void loadParameters(String[] args)
{
ArgsParser parser = new ArgsParser(args);
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));
}
/**
* Reads parameter PDBFile, PDBFile1, PDFile2, ... and saves the value(s) (if
* any)
*
* @param parser
* @return
*/
List loadPdbParameters(ArgsParser parser)
{
List values = new ArrayList<>();
String value = parser.getValue(PARAM_PDBFILE);
if (value != null)
{
values.add(value);
}
int i = 1;
while (true)
{
value = parser.getValue(PARAM_PDBFILE + String.valueOf(i));
if (value != null)
{
values.add(value);
}
else
{
break;
}
}
return values;
}
/**
* Constructs and displays a JFrame containing an alignment panel (and any
* additional panels depending on parameters supplied)
*
* @throws FileFormatException
*/
void showFrame() throws FileFormatException
{
JFrame frame = new JFrame(getParameter(PARAM_FILE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
* construct an AlignFrame and extract its contents and menu bar
* TODO there may be a less obscure way to do this
*/
AlignFrame alignFrame = createAlignFrame();
frame.setContentPane(alignFrame.getContentPane());
frame.setJMenuBar(alignFrame.getJMenuBar());
// fudge so that dialogs can be opened with this frame as parent
// 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
*
* @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
*
* @return
* @throws FileFormatException
*/
AlignFrame createAlignFrame() 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);
return af;
}
}