3 import jalview.api.JalviewApp;
4 import jalview.api.StructureSelectionManagerProvider;
5 import jalview.datamodel.PDBEntry;
6 import jalview.datamodel.Sequence;
7 import jalview.datamodel.SequenceI;
8 import jalview.io.AnnotationFile;
9 import jalview.io.DataSourceType;
10 import jalview.io.JPredFile;
11 import jalview.io.JnetAnnotationMaker;
12 import jalview.io.NewickFile;
13 import jalview.structure.StructureSelectionManager;
14 import jalview.util.HttpUtils;
15 import jalview.util.MessageManager;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.StringTokenizer;
21 import java.util.Vector;
24 * A class to load parameters for either JalviewLite or Jalview
29 public class JalviewAppLoader
32 private JalviewApp app;
34 private boolean debug;
36 private String separator;
38 public JalviewAppLoader(boolean debug)
43 public void load(JalviewApp app)
48 String sep = app.getParameter("separator");
57 throw new Error(MessageManager
58 .getString("error.invalid_separator_parameter"));
71 * Load PDBFiles if any specified by parameter(s). Returns true if loaded,
77 protected boolean loadPdbFiles()
79 boolean result = false;
81 * <param name="alignpdbfiles" value="false/true"/> Undocumented for 2.6 -
85 boolean doAlign = app.getDefaultParameter("alignpdbfiles", false);
86 app.setAlignPdbStructures(doAlign);
88 * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
91 * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
93 * <param name="PDBfile3" value="1q0o Q45135_9MICO">
97 // Accumulate pdbs here if they are heading for the same view (if
98 // alignPdbStructures is true)
99 Vector<Object[]> pdbs = new Vector<>();
100 // create a lazy matcher if we're asked to
101 jalview.analysis.SequenceIdMatcher matcher = (app
102 .getDefaultParameter("relaxedidmatch", false))
103 ? new jalview.analysis.SequenceIdMatcher(
104 app.getViewport().getAlignment()
105 .getSequencesArray())
111 if (pdbFileCount > 0)
113 param = app.getParameter("PDBFILE" + pdbFileCount);
117 param = app.getParameter("PDBFILE");
122 PDBEntry pdb = new PDBEntry();
125 SequenceI[] seqs = null;
126 String[] chains = null;
128 StringTokenizer st = new StringTokenizer(param, " ");
130 if (st.countTokens() < 2)
132 String sequence = app.getParameter("PDBSEQ");
133 if (sequence != null)
135 seqs = new SequenceI[] { matcher == null
136 ? (Sequence) app.getViewport().getAlignment()
138 : matcher.findIdMatch(sequence) };
144 param = st.nextToken();
145 List<SequenceI> tmp = new ArrayList<>();
146 List<String> tmp2 = new ArrayList<>();
148 while (st.hasMoreTokens())
150 seqstring = st.nextToken();
151 StringTokenizer st2 = new StringTokenizer(seqstring, "=");
152 if (st2.countTokens() > 1)
155 tmp2.add(st2.nextToken());
156 seqstring = st2.nextToken();
158 tmp.add(matcher == null
159 ? (Sequence) app.getViewport().getAlignment()
161 : matcher.findIdMatch(seqstring));
164 seqs = tmp.toArray(new SequenceI[tmp.size()]);
165 if (tmp2.size() == tmp.size())
167 chains = tmp2.toArray(new String[tmp2.size()]);
171 DataSourceType protocol = resolveFileProtocol(app,
173 // TODO check JAL-357 for files in a jar (CLASSLOADER)
178 for (int i = 0; i < seqs.length; i++)
182 ((Sequence) seqs[i]).addPDBId(pdb);
183 StructureSelectionManager
184 .getStructureSelectionManager(
185 (StructureSelectionManagerProvider) app)
186 .registerPDBEntry(pdb);
192 // this may not really be a problem but we give a warning
195 "Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
203 pdbs.addElement(new Object[] { pdb, seqs, chains, protocol });
207 app.newStructureView(pdb, seqs, chains, protocol);
213 } while (param != null || pdbFileCount < 10);
216 SequenceI[][] seqs = new SequenceI[pdbs.size()][];
217 PDBEntry[] pdb = new PDBEntry[pdbs.size()];
218 String[][] chains = new String[pdbs.size()][];
219 String[] protocols = new String[pdbs.size()];
220 for (int pdbsi = 0, pdbsiSize = pdbs
221 .size(); pdbsi < pdbsiSize; pdbsi++)
223 Object[] o = pdbs.elementAt(pdbsi);
224 pdb[pdbsi] = (PDBEntry) o[0];
225 seqs[pdbsi] = (SequenceI[]) o[1];
226 chains[pdbsi] = (String[]) o[2];
227 protocols[pdbsi] = (String) o[3];
229 app.alignedStructureView(pdb, seqs, chains, protocols);
236 * Load in a Jnetfile if specified by parameter. Returns true if loaded, else
242 protected boolean loadJnetFile()
244 boolean result = false;
245 String param = app.getParameter("jnetfile");
248 // jnet became jpred around 2016
249 param = app.getParameter("jpredfile");
256 DataSourceType protocol = resolveFileProtocol(app,
258 JPredFile predictions = new JPredFile(ret[0], protocol);
259 JnetAnnotationMaker.add_annotation(predictions,
260 app.getViewport().getAlignment(), 0, false);
261 // false == do not add sequence profile from concise output
262 app.getViewport().getAlignment().setupJPredAlignment();
263 app.updateForAnnotations();
265 } catch (Exception ex)
267 ex.printStackTrace();
274 * Load annotations if specified by parameter. Returns true if loaded, else
280 protected boolean loadAnnotations()
282 boolean result = false;
283 String param = app.getParameter("annotations");
287 DataSourceType protocol = resolveFileProtocol(app,
290 if (new AnnotationFile().annotateAlignmentView(app.getViewport(),
293 app.updateForAnnotations();
299 .println("Annotations were not added from annotation file '"
307 * Load features file and view settings as specified by parameters. Returns
308 * true if features were loaded, else false.
313 protected boolean loadFeatures()
315 boolean result = false;
316 // ///////////////////////////
317 // modify display of features
318 // we do this before any features have been loaded, ensuring any hidden
319 // groups are hidden when features first displayed
321 // hide specific groups
323 String param = app.getParameter("hidefeaturegroups");
326 app.setFeatureGroupState(
327 separatorListToArray(param, separator), false);
328 // app.setFeatureGroupStateOn(newAlignFrame, param, false);
330 // show specific groups
331 param = app.getParameter("showfeaturegroups");
334 app.setFeatureGroupState(separatorListToArray(param, separator),
336 // app.setFeatureGroupStateOn(newAlignFrame, param, true);
338 // and now load features
339 param = app.getParameter("features");
343 DataSourceType protocol = resolveFileProtocol(app,
346 result = app.parseFeaturesFile(ret[0], protocol);
349 param = app.getParameter("showFeatureSettings");
350 if (param != null && param.equalsIgnoreCase("true"))
352 app.newFeatureSettings();
358 * Load a score file if specified by parameter. Returns true if file was
359 * loaded, else false.
363 protected boolean loadScoreFile()
365 boolean result = false;
366 String sScoreFile = app.getParameter("scoreFile");
367 if (sScoreFile != null && !"".equals(sScoreFile))
374 "Attempting to load T-COFFEE score file from the scoreFile parameter");
376 result = app.loadScoreFile(sScoreFile);
380 "Failed to parse T-COFFEE parameter as a valid score file ('"
381 + sScoreFile + "')");
383 } catch (Exception e)
385 System.err.printf("Cannot read score file: '%s'. Cause: %s \n",
386 sScoreFile, e.getMessage());
392 String[] ret = new String[1];
394 * Load a tree for the alignment if specified by parameter. Returns true if a
395 * tree was loaded, else false.
400 protected boolean loadTree()
402 boolean result = false;
403 String treeFile = app.getParameter("tree");
404 if (treeFile == null)
406 treeFile = app.getParameter("treeFile");
409 if (treeFile != null)
414 NewickFile fin = new NewickFile(treeFile,
415 resolveFileProtocol(app, ret));
418 if (fin.getTree() != null)
420 app.loadTree(fin, ret[0]);
424 System.out.println("Successfully imported tree.");
431 System.out.println("Tree parameter did not resolve to a valid tree.");
434 } catch (Exception ex)
436 ex.printStackTrace();
443 * form a complete URL given a path to a resource and a reference location on
447 * - an absolute path on the same server as localref or a document
448 * located relative to localref
450 * - a URL on the same server as url
451 * @return a complete URL for the resource located by url
453 public static String resolveUrlForLocalOrAbsolute(String targetPath,
456 String resolvedPath = "";
457 if (targetPath.startsWith("/"))
459 String codebase = localref.toString();
460 String localfile = localref.getFile();
461 resolvedPath = codebase.substring(0,
462 codebase.length() - localfile.length()) + targetPath;
467 * get URL path and strip off any trailing file e.g.
468 * www.jalview.org/examples/index.html#applets?a=b is trimmed to
469 * www.jalview.org/examples/
471 String urlPath = localref.toString();
472 String directoryPath = urlPath;
473 int lastSeparator = directoryPath.lastIndexOf("/");
474 if (lastSeparator > 0)
476 directoryPath = directoryPath.substring(0, lastSeparator + 1);
479 if (targetPath.startsWith("/"))
482 * construct absolute URL to a file on the server - this is not allowed?
484 // String localfile = localref.getFile();
485 // resolvedPath = urlPath.substring(0,
486 // urlPath.length() - localfile.length())
488 resolvedPath = directoryPath + targetPath.substring(1);
492 resolvedPath = directoryPath + targetPath;
494 if (JalviewLite.debug)
497 "resolveUrlForLocalOrAbsolute returning " + resolvedPath);
503 * parse the string into a list
507 * @return elements separated by separator
509 public static String[] separatorListToArray(String list, String separator)
511 // TODO use StringUtils version (slightly different...)
512 int seplen = separator.length();
513 if (list == null || list.equals("") || list.equals(separator))
517 Vector<String> jv = new Vector<>();
519 while ((pos = list.indexOf(separator, cp)) > cp)
521 jv.addElement(list.substring(cp, pos));
524 if (cp < list.length())
526 String c = list.substring(cp);
527 if (!c.equals(separator))
534 String[] v = new String[jv.size()];
535 for (int i = 0; i < v.length; i++)
537 v[i] = jv.elementAt(i);
539 jv.removeAllElements();
542 // System.err.println("Array from '" + separator
543 // + "' separated List:\n" + v.length);
544 // for (int i = 0; i < v.length; i++)
546 // System.err.println("item " + i + " '" + v[i] + "'");
553 // System.err.println(
554 // "Empty Array from '" + separator + "' separated List");
559 public static DataSourceType resolveFileProtocol(JalviewApp app,
562 String path = retPath[0];
566 if (path.startsWith("PASTE"))
568 retPath[0] = path.substring(5);
569 return DataSourceType.PASTE;
575 if (path.indexOf("://") >= 0)
577 return DataSourceType.URL;
581 * try relative to document root
583 URL documentBase = app.getDocumentBase();
584 String withDocBase = resolveUrlForLocalOrAbsolute(path,
586 if (HttpUtils.isValidUrl(withDocBase))
590 // System.err.println("Prepended document base '" + documentBase
591 // + "' to make: '" + withDocBase + "'");
593 retPath[0] = withDocBase;
594 return DataSourceType.URL;
598 * try relative to codebase (if different to document base)
600 URL codeBase = app.getCodeBase();
601 String withCodeBase = resolveUrlForLocalOrAbsolute(path,
603 if (!withCodeBase.equals(withDocBase)
604 && HttpUtils.isValidUrl(withCodeBase))
608 // System.err.println("Prepended codebase '" + codeBase
609 // + "' to make: '" + withCodeBase + "'");
611 retPath[0] = withCodeBase;
612 return DataSourceType.URL;
616 * try locating by classloader; try this last so files in the directory
617 * are resolved using document base
619 if (inArchive(app.getClass(), path))
621 return DataSourceType.CLASSLOADER;
627 * Discovers whether the given file is in the Applet Archive
633 private static boolean inArchive(Class<?> c, String f)
635 // This might throw a security exception in certain browsers
636 // Netscape Communicator for instance.
639 boolean rtn = (c.getResourceAsStream("/" + f) != null);
642 // System.err.println("Resource '" + f + "' was "
643 // + (rtn ? "" : "not ") + "located by classloader.");
646 } catch (Exception ex)
648 System.out.println("Exception checking resources: " + f + " " + ex);