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