d161d1d83f07c1223354ffa8053129d0eeee191c
[jalview.git] / src / jalview / bin / JalviewJS.java
1 package jalview.bin;
2
3 import jalview.gui.AlignFrame;
4 import jalview.io.AppletFormatAdapter;
5 import jalview.io.DataSourceType;
6 import jalview.io.FileFormatException;
7 import jalview.io.FileFormatI;
8 import jalview.io.FileLoader;
9 import jalview.io.IdentifyFile;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Objects;
14
15 import javax.swing.JFrame;
16 import javax.swing.JInternalFrame;
17
18 /**
19  * Entry point for Jalview as Javascript. Expects parameter names as for the
20  * JalviewLite applet, formatted as for the Jalview application, for example
21  * 
22  * <pre>
23  *   JalviewJS file /examples/uniref50.fa features /examples/exampleFeatures.txt \
24  *       PDBFile "/examples/pdb1.txt seq1"
25  * </pre>
26  * 
27  * Note that (unlike the applet) parameter names are case sensitive
28  */
29 // TODO or format as file=/examples/uniref50.fa (etc)?
30 public class JalviewJS
31 {
32   private static final String PARAM_FILE = "file";
33
34   private static final String PARAM_FILE2 = "file2";
35
36   private static final String PARAM_TREE = "tree";
37
38   private static final String PARAM_FEATURES = "features";
39
40   private static final String PARAM_ANNOTATIONS = "annotations";
41
42   private static final String PARAM_SHOW_ANNOTATION = "showAnnotation";
43
44   private static final String PARAM_PDBFILE = "PDBFile";
45
46   public static void main(String[] args)
47   {
48     try
49     {
50       new JalviewJS().doMain(args);
51     } catch (FileFormatException e)
52     {
53       e.printStackTrace();
54     }
55   }
56
57   private String paramFile;
58
59   private String paramFile2;
60
61   private String paramTree;
62
63   private String paramFeatures;
64
65   private String paramAnnotations;
66
67   private String paramShowAnnotation;
68
69   private List<String> paramPdbFile;
70
71   /**
72    * Parses parameters and shows the frame and any loaded panels
73    * 
74    * @throws FileFormatException
75    */
76   void doMain(String[] args) throws FileFormatException
77   {
78     loadParameters(args);
79     if (paramFile == null)
80     {
81       usage();
82     }
83     else
84     {
85       showFrame();
86     }
87   }
88
89   /**
90    * Prints a chastising, yet helpful, error message on syserr
91    */
92   private void usage()
93   {
94     System.err.println("Usage: JalviewJS file <alignmentFileName>");
95     System.err.println("See documentation for full parameter list");
96   }
97
98   /**
99    * Parses any supplied parameters. Note that (unlike for the applet),
100    * parameter names are case sensitive.
101    * 
102    * @param args
103    * 
104    * @see http://www.jalview.org/examples/index.html#appletParameters
105    */
106   void loadParameters(String[] args)
107   {
108     ArgsParser parser = new ArgsParser(args);
109     paramFile = parser.getValue(PARAM_FILE);
110     paramFile2 = parser.getValue(PARAM_FILE2);
111     paramTree = parser.getValue(PARAM_TREE);
112     paramFeatures = parser.getValue(PARAM_FEATURES);
113     paramAnnotations = parser.getValue(PARAM_ANNOTATIONS);
114     paramShowAnnotation = parser.getValue(PARAM_SHOW_ANNOTATION);
115     paramPdbFile = loadPdbParameters(parser);
116   }
117
118   /**
119    * Reads parameter PDBFile, PDBFile1, PDFile2, ... and saves the value(s) (if
120    * any)
121    * 
122    * @param parser
123    * @return
124    */
125   List<String> loadPdbParameters(ArgsParser parser)
126   {
127     List<String> values = new ArrayList<>();
128     String value = parser.getValue(PARAM_PDBFILE);
129     if (value != null)
130     {
131       values.add(value);
132     }
133     int i = 1;
134     while (true)
135     {
136       value = parser.getValue(PARAM_PDBFILE + String.valueOf(i));
137       if (value != null)
138       {
139         values.add(value);
140       }
141       else
142       {
143         break;
144       }
145     }
146     return values;
147   }
148
149   /**
150    * Constructs and displays a JFrame containing an alignment panel (and any
151    * additional panels depending on parameters supplied)
152    * 
153    * @throws FileFormatException
154    */
155   void showFrame() throws FileFormatException
156   {
157     JFrame frame = new JFrame(this.paramFile);
158     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
159
160     JInternalFrame alignFrame = createAlignFrame();
161     frame.setContentPane(alignFrame.getContentPane());
162     frame.setJMenuBar(alignFrame.getJMenuBar());
163
164     frame.pack();
165     frame.setSize(AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
166     frame.setVisible(true);
167   }
168
169   /**
170    * Constructs and returns the frame containing the alignment and its
171    * annotations
172    * 
173    * @return
174    * @throws FileFormatException
175    */
176   JInternalFrame createAlignFrame() throws FileFormatException
177   {
178     Objects.requireNonNull(this.paramFile);
179
180     DataSourceType protocol = AppletFormatAdapter
181             .checkProtocol(this.paramFile);
182     FileFormatI format = new IdentifyFile().identify(this.paramFile,
183             protocol);
184     FileLoader fileLoader = new FileLoader(false);
185     AlignFrame af = fileLoader.LoadFileWaitTillLoaded(this.paramFile,
186             protocol, format);
187
188     return af;
189   }
190
191 }