Refactoring of Jalview, JalviewAppLoader, JalviewApp, various
[jalview.git] / src / jalview / bin / AppletParams.java
1
2 package jalview.bin;
3
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import jalview.gui.Preferences;
9
10 /**
11  * Collection of all known applet tags from JalviewLite
12  * 
13  * @author hansonr
14  *
15  */
16 @SuppressWarnings("serial")
17 public class AppletParams extends HashMap<String, Object>
18 {
19
20   private final static String[] params = { "alignpdbfiles",
21       Preferences.ANNOTATIONCOLOUR_MAX, Preferences.ANNOTATIONCOLOUR_MIN,
22       "annotations",
23       "APPLICATION_URL", "automaticScrolling", "centrecolumnlabels",
24       "debug", "defaultColour", "defaultColourNuc", "defaultColourProt",
25       "embedded", "enableSplitFrame", "externalstructureviewer", "features",
26       "file", "file2", "format", "heightScale", "hidefeaturegroups",
27       "jalviewhelpurl", "jnetfile", "jpredfile", "label", "linkLabel_",
28       "linkLabel_1", "linkURL_", "nojmol", "normaliseLogo",
29       "normaliseSequenceLogo", "oninit", "PDBFILE", "PDBSEQ",
30       "relaxedidmatch", "resolvetocodebase", "RGB", "scaleProteinAsCdna",
31       "scoreFile", "separator", "sequence", "showAnnotation", "showbutton",
32       "showConsensus", "showConsensusHistogram", "showConservation",
33       "showfeaturegroups", "showFeatureSettings", "showFullId",
34       "showGroupConsensus", "showGroupConservation", "showOccupancy",
35       "showQuality", "showSequenceLogo", "showTreeBootstraps",
36       "showTreeDistances", "showUnconserved", "showUnlinkedTreeNodes",
37       "sortBy", "sortByTree", "tree", "treeFile", "upperCase",
38       "userDefinedColour", "widthScale", "windowHeight", "windowWidth",
39       "wrap", };
40
41   public String getParam(String param, String def)
42   {
43     Object val = get(param);
44     return (val != null ? val.toString() : def);
45   }
46
47   // <applet
48   // code="jalview.bin.JalviewLite" width="140" height="35"
49   // archive="jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar">
50   // <param name="permissions" value="sandbox"/>
51   // <param name="file" value="uniref50.fa"/>
52   // <param name="treeFile" value="ferredoxin.nw"/>
53   // <param name="userDefinedColour" value="C=yellow; R,K,H=FF5555;
54   // D,E=5555FF"/>
55   // <param name="sortByTree" value="True"/>
56   // <param name="showSequenceLogo" value="true"/>
57   // <param name="showGroupConsensus" value="true"/>
58   // <param name="showFullId" value="false"/>
59   // <param name="linkLabel_1" value="Uniprot"/>
60   // <param name="linkUrl_1"
61   // value="http://www.uniprot.org/uniprot/$SEQUENCE_ID$"/>
62   // <param name="linkLabel_2" value="EMBL-EBI Search"/>
63   // <param name="linkUrl_2"
64   // value="http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$"/>
65   // <param name="APPLICATION_URL"
66   // value="http://www.jalview.org/services/launchApp"/>
67   // </applet>
68   //
69   public AppletParams(String outerHTML)
70   {
71     String[] tokens = outerHTML.split("<param");
72     outerHTML = tokens[0];
73     String code = getAttr(outerHTML, "code");
74     if (!code.equals("jalview.bin.JalviewLite"))
75     {
76       return;
77     }
78     for (int i = tokens.length; --i > 0;)
79     {
80       String param = tokens[i];
81       String key = getAttr(param, "name");
82       if (key != null)
83       {
84         String value = getAttr(param, "value");
85         System.out.println("AppletParams " + key + " = \"" + value + "\"");
86         put(key, value);
87       }
88     }
89     put("_width", getAttr(outerHTML, "width"));
90     put("_height", getAttr(outerHTML, "height"));
91     put("_id", getAttr(outerHTML, "id"));
92     put("_name", getAttr(outerHTML, "name"));
93     put("_archive", getAttr(outerHTML, "archive"));
94     put("_code", code);
95   }
96
97   public AppletParams()
98   {
99   }
100
101   public static AppletParams getAppletParams(Map<String, Object> map,
102           List<String> vargs)
103   {
104     AppletParams appletParams = new AppletParams();
105     String resourcePath = getString(map, "resourcePath");
106     if (resourcePath == null)
107       resourcePath = "";
108     if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
109     {
110       resourcePath += "/";
111     }
112     for (int i = params.length; --i >= 0;)
113     {
114       String prefName = params[i];
115       Object value = map.get(prefName);
116       if (value != null)
117         addParam(vargs, prefName, value, appletParams, resourcePath);
118     }
119     return appletParams;
120   }
121
122   private static String getString(Map<String, Object> map, String key)
123   {
124     Object o = map.get(key);  
125     return (o == null ? null : o.toString());
126   }
127
128   public static AppletParams getAppletParams(String[] args,
129           List<String> vargs)
130   {
131     AppletParams appletParams = new AppletParams();
132     String resourcePath = "";
133     for (int i = args.length; --i > 0;) // > 0 is correct, not >=0
134     {
135       if (args[i].startsWith("name=\"Info.resourcePath\""))
136       {
137         resourcePath = getAttr(args[i], "value");
138         if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
139         {
140           resourcePath += "/";
141         }
142         break;
143       }
144     }
145     for (int i = 1; i < args.length; i++)
146     {
147       String arg = args[i].trim();
148       if (arg.startsWith("name="))
149       {
150         String prefName = getAttr(arg, "name");
151         String value = getAttr(arg, "value");
152         addParam(vargs, prefName, value, appletParams, resourcePath);
153       }
154     }
155     return appletParams;
156   }
157
158   private static void addParam(List<String> vargs, String prefName,
159           Object value, AppletParams appletParams, String resourcePath)
160   {
161
162     // note that Application arguments ARE case-sensitive, but
163     // Applet.getParameter() is not.
164
165     String appletName = prefName.toLowerCase();
166     String argName = prefName;
167     switch (appletName)
168     {
169
170     case "file":
171       argName = "open";
172       appletName = null;
173       value = resourcePath + value;
174       break;
175     case "file2":
176       argName = "open2";
177       prefName = null;
178       value = resourcePath + value;
179       break;
180     case "features":
181     case "jnetfile":
182     case "jpredfile":
183     case "pdbfile":
184     case "scorefile":
185     case "sequence":
186       // setting argName to null indicates that we want
187       // JalviewAppLoader to take care of this.
188       prefName = argName = null;
189       value = resourcePath + value;
190       break;
191     case "tree":
192     case "treefile":
193       // setting appletName to null indicates that we want
194       // Jalview.doMain to taken care of this as Jalview args
195       argName = "tree";
196       appletName = null;
197       value = resourcePath + value;
198       break;
199
200     // non-loading preferences
201
202     case "defaultcolour":
203       prefName = Preferences.DEFAULT_COLOUR;
204       break;
205     case "defaultcolournuc":
206       prefName = Preferences.DEFAULT_COLOUR_NUC;
207       break;
208     case "defaultcolourprot":
209       prefName = Preferences.DEFAULT_COLOUR_PROT;
210       break;
211     case "annotationcolour_max":
212       prefName = Preferences.ANNOTATIONCOLOUR_MAX;
213       break;
214     case "annotationcolour_min":
215       prefName = Preferences.ANNOTATIONCOLOUR_MIN;
216       break;
217     case "enablesplitframe":
218       prefName = Preferences.ENABLE_SPLIT_FRAME;
219       break;
220     case "centrecolumnlabels":
221       prefName = Preferences.CENTRE_COLUMN_LABELS;
222       break;
223     case "sortby":
224       prefName = Preferences.SORT_ALIGNMENT; // id, etc.
225       break;
226     case "normalisesequencelogo":
227       prefName = Preferences.NORMALISE_CONSENSUS_LOGO;
228       break;
229     case "relaxedidmatch":
230       prefName = Preferences.RELAXEDSEQIDMATCHING;
231       break;
232     case "scaleproteinascdna":
233       prefName = Preferences.SCALE_PROTEIN_TO_CDNA;
234       break;
235     case "userdefinedcolour":
236       argName = "colour";
237       prefName = Preferences.USER_DEFINED_COLOURS;
238       break;
239     case "wrap":
240       prefName = Preferences.WRAP_ALIGNMENT;
241       break;
242
243     // implemented; not tested:
244
245     case "oninit":
246       prefName = null;
247       break;
248     case "annotations":
249       value = resourcePath + value;
250       argName = null;
251       break;
252     case "hidefeaturegroups":
253       // TODO
254       break;
255     case "pdbseq":
256       argName = prefName = null;
257       break;
258     case "sortbytree":
259       prefName = Preferences.SORT_BY_TREE;
260       value = checkTF(value);
261       appletName = null; // taken care of by Jalview
262       break;
263     case "format":
264       break;
265     case "alignpdbfiles":
266       argName = prefName = null;
267       break;
268     case "separator":
269       break;
270
271     // TODO: probably not relevant?
272
273     case "rgb":
274       prefName = null; // TODO no background for application?
275       break;
276     case "externalstructureviewer":
277       break;
278     case "application_url":
279       break;
280     case "automaticscrolling":
281       break;
282     case "heightscale":
283       break;
284     case "jalviewhelpurl":
285       break;
286     case "label":
287       break;
288     case "linklabel_":
289       prefName = "linkLabel_";
290       break;
291     case "linklabel_1":
292       prefName = "linkLabel_1";
293       break;
294     case "linkurl_":
295       prefName = "linkURL_";
296       break;
297
298     // unknown:
299
300     case "nojmol":
301     case "normaliselogo":
302     case "resolvetocodebase":
303     case "uppercase":
304     case "widthscale":
305     case "windowheight":
306     case "windowwidth":
307       argName = prefName = null;
308       break;
309
310     // TRUE/FALSE
311
312     case "debug":
313       value = checkTF(value);
314       break;
315     case "embedded":
316       value = checkTF(value);
317       break;
318     case "showbutton":
319       value = checkTF(value);
320       break;
321     case "showannotation":
322       prefName = Preferences.SHOW_ANNOTATIONS;
323       value = checkTF(value);
324       break;
325     case "showconsensus":
326       prefName = Preferences.SHOW_CONSENSUS_LOGO;
327       value = checkTF(value);
328       break;
329     case "showconsensushistogram":
330       prefName = Preferences.SHOW_CONSENSUS_HISTOGRAM;
331       value = checkTF(value);
332       break;
333     case "showconservation":
334       prefName = Preferences.SHOW_CONSERVATION;
335       value = checkTF(value);
336       break;
337     case "showgroupconsensus":
338       prefName = Preferences.SHOW_GROUP_CONSENSUS;
339       value = checkTF(value);
340       break;
341     case "showgroupconservation":
342       prefName = Preferences.SHOW_GROUP_CONSERVATION;
343       value = checkTF(value);
344       break;
345     case "showoccupancy":
346       prefName = Preferences.SHOW_OCCUPANCY;
347       value = checkTF(value);
348       break;
349     case "showquality":
350       prefName = Preferences.SHOW_QUALITY;
351       value = checkTF(value);
352       break;
353     case "showsequencelogo":
354       prefName = Preferences.SHOW_CONSENSUS_LOGO;
355       value = checkTF(value);
356       break;
357     case "showfeaturegroups":
358       value = checkTF(value);
359       break;
360     case "showfeaturesettings":
361       value = checkTF(value);
362       break;
363     case "showfullid":
364       value = checkTF(value);
365       break;
366     case "showtreebootstraps":
367       value = checkTF(value);
368       break;
369     case "showtreedistances":
370       value = checkTF(value);
371       break;
372     case "showunconserved":
373       prefName = Preferences.SHOW_UNCONSERVED;
374       value = checkTF(value);
375       break;
376     case "showunlinkedtreenodes":
377       value = checkTF(value);
378       break;
379     default:
380       if (appletName.startsWith("pdbfile")
381               || appletName.startsWith("sequence") && Character
382                       .isDigit(appletName.charAt(appletName.length() - 1)))
383       {
384         // could be pdbFile2, for example
385         prefName = argName = null;
386         value = resourcePath + value;
387         break;
388       }
389       // or one of the app preference names
390       break;
391     }
392
393     // put name and value into application args
394     if (value != null && argName != null)
395     {
396       vargs.add(argName);
397       if (value != "true")
398       {
399         vargs.add(value.toString());
400       }
401     }
402     if (value == null)
403     {
404       value = "false";
405     }
406     System.out.println("AppletParams propName=" + prefName + " argName="
407             + argName + " appletName=" + appletName + " value=" + value);
408     if (appletName != null)
409     {
410       appletParams.put(appletName, value);
411     }
412     if (prefName != null)
413     {
414       Cache.setPropertyNoSave(prefName, value.toString());
415     }
416   }
417
418   /**
419    * Check for a single-argument option.
420    * 
421    * @param value
422    * @return "true" or null
423    */
424   private static String checkTF(Object value)
425   {
426     return (("" + value).toLowerCase() == "true" ? "true" : null);
427   }
428
429   /**
430    * Crude applet innerHTML parser
431    * 
432    * @param tag
433    * @param attr
434    * @return
435    */
436   private static String getAttr(String tag, String attr)
437   {
438     int pt = tag.indexOf(attr + "=\"");
439     if (pt < 0)
440     {
441       System.out
442               .println("AppletParams did not read " + attr + " in " + tag);
443       return null;
444     }
445     // <param name="sortByTree" value="True"/>
446     int pt1 = pt + attr.length() + 2;
447     int pt2 = tag.indexOf("\"", pt1);
448     return (pt < 0 ? null : tag.substring(pt1, pt2));
449   }
450
451   public static void main(String[] args)
452   {
453     new AppletParams("<applet\r\n"
454             + "    code=\"jalview.bin.JalviewLite\" width=\"140\" height=\"35\"\r\n"
455             + "    archive=\"jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar\">  \r\n"
456             + "  <param name=\"permissions\" value=\"sandbox\"/>\r\n"
457             + "  <param name=\"file\" value=\"uniref50.fa\"/>\r\n"
458             + "  <param name=\"treeFile\" value=\"ferredoxin.nw\"/>\r\n"
459             + "  <param name=\"userDefinedColour\" value=\"C=yellow; R,K,H=FF5555; D,E=5555FF\"/>\r\n"
460             + "  <param name=\"sortByTree\" value=\"True\"/>\r\n"
461             + "  <param name=\"showSequenceLogo\" value=\"true\"/>\r\n"
462             + "  <param name=\"showGroupConsensus\" value=\"true\"/>\r\n"
463             + "  <param name=\"showFullId\" value=\"false\"/>\r\n"
464             + "    <param name=\"linkLabel_1\" value=\"Uniprot\"/>\r\n"
465             + "    <param name=\"linkUrl_1\" value=\"http://www.uniprot.org/uniprot/$SEQUENCE_ID$\"/>\r\n"
466             + "    <param name=\"linkLabel_2\" value=\"EMBL-EBI Search\"/>\r\n"
467             + "    <param name=\"linkUrl_2\" value=\"http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$\"/>\r\n"
468             + "    <param name=\"APPLICATION_URL\" value=\"http://www.jalview.org/services/launchApp\"/>\r\n"
469             + "     </applet>");
470   }
471
472 }