2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static jalview.util.UrlConstants.DB_ACCESSION;
24 import static jalview.util.UrlConstants.DELIM;
25 import static jalview.util.UrlConstants.SEP;
26 import static jalview.util.UrlConstants.SEQUENCE_ID;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.SequenceI;
31 import java.util.Arrays;
32 import java.util.List;
34 import java.util.Vector;
39 * helper class to parse URL Link strings taken from applet parameters or
40 * jalview properties file using the com.stevesoft.pat.Regex implementation.
41 * Jalview 2.4 extension allows regular expressions to be used to parse ID
42 * strings and replace the result in the URL. Regex's operate on the whole ID
43 * string given to the matchURL method, if no regex is supplied, then only
44 * text following the first pipe symbol will be substituted. Usage
48 private static final String EQUALS = "=";
50 private static final String SPACE = " ";
52 private String urlSuffix;
54 private String urlPrefix;
56 private String target;
60 private String dbname;
62 private String regexReplace;
64 private boolean dynamic = false;
66 private boolean usesDBaccession = false;
68 private String invalidMessage = null;
71 * parse the given linkString of the form '<label>SEP<url>' into parts url may
72 * contain a string $SEQUENCE_ID<=optional regex=>$ where <=optional regex=>
73 * must be of the form =/<perl style regex>/=$
77 public UrlLink(String link)
79 int sep = link.indexOf(SEP);
80 int psqid = link.indexOf(DELIM + DB_ACCESSION);
81 int nsqid = link.indexOf(DELIM + SEQUENCE_ID);
85 usesDBaccession = true;
87 sep = parseLabel(sep, psqid, link);
89 int endOfRegex = parseUrl(link, DB_ACCESSION, psqid, sep);
90 parseTarget(link, sep, endOfRegex);
95 sep = parseLabel(sep, nsqid, link);
97 int endOfRegex = parseUrl(link, SEQUENCE_ID, nsqid, sep);
99 parseTarget(link, sep, endOfRegex);
103 label = link.substring(0, sep).trim();
105 // if there's a third element in the url link string
106 // it is the target name, otherwise target=label
107 int lastsep = link.lastIndexOf(SEP);
110 urlPrefix = link.substring(sep + 1, lastsep).trim();
111 target = link.substring(lastsep + 1).trim();
115 urlPrefix = link.substring(sep + 1).trim();
119 regexReplace = null; // implies we trim any prefix if necessary //
123 label = label.trim();
124 target = target.trim();
128 * Alternative constructor for separate name, link and description
131 * The string used to match the link to a DB reference id
135 * The description of the associated target DB
137 public UrlLink(String name, String url, String desc)
139 this(name + SEP + url + SEP + desc);
143 * @return the url_suffix
145 public String getUrlSuffix()
151 * @return the url_prefix
153 public String getUrlPrefix()
161 public String getTarget()
169 public String getLabel()
174 public String getUrlWithToken()
176 String var = (usesDBaccession ? DB_ACCESSION : SEQUENCE_ID);
181 + ((regexReplace != null)
182 ? EQUALS + regexReplace + EQUALS + DELIM
185 + ((urlSuffix == null) ? "" : urlSuffix);
189 * @return the regexReplace
191 public String getRegexReplace()
197 * @return the invalidMessage
199 public String getInvalidMessage()
201 return invalidMessage;
205 * Check if URL string was parsed properly.
207 * @return boolean - if false then <code>getInvalidMessage</code> returns an
210 public boolean isValid()
212 return invalidMessage == null;
217 * @return whether link is dynamic
219 public boolean isDynamic()
226 * @return whether link uses DB Accession id
228 public boolean usesDBAccession()
230 return usesDBaccession;
238 public void setLabel(String newlabel)
240 this.label = newlabel;
248 public void setTarget(String desc)
254 * return one or more URL strings by applying regex to the given idstring
257 * @param onlyIfMatches
258 * - when true url strings are only made if regex is defined and
260 * @return String[] { part of idstring substituted, full substituted url , ..
261 * next part, next url..}
263 public String[] makeUrls(String idstring, boolean onlyIfMatches)
267 if (regexReplace != null)
269 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex
270 .perlCode("/" + regexReplace + "/");
271 if (rg.search(idstring))
273 int ns = rg.numSubs();
277 return new String[] { rg.stringMatched(),
278 urlPrefix + rg.stringMatched() + urlSuffix };
280 * else if (ns==1) { // take only subgroup match return new String[]
281 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
287 for (int s = 0; s <= rg.numSubs(); s++)
289 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)
290 + " : " + rg.matchedTo(s) + " : '"
291 + rg.stringMatched(s) + "'");
293 // try to collate subgroup matches
294 Vector<String> subs = new Vector<String>();
295 // have to loop through submatches, collating them at top level
300 if (s + 1 <= ns && rg.matchedTo(s) > -1
301 && rg.matchedTo(s + 1) > -1
302 && rg.matchedTo(s + 1) < rg.matchedTo(s))
304 // s is top level submatch. search for submatches enclosed by
308 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
310 if (rg.matchedFrom(r) > -1)
312 mtch += rg.stringMatched(r);
316 if (mtch.length() > 0)
318 subs.addElement(mtch);
319 subs.addElement(urlPrefix + mtch + urlSuffix);
325 if (rg.matchedFrom(s) > -1)
327 subs.addElement(rg.stringMatched(s));
329 urlPrefix + rg.stringMatched(s) + urlSuffix);
335 String[] res = new String[subs.size()];
336 for (int r = 0, rs = subs.size(); r < rs; r++)
338 res[r] = subs.elementAt(r);
340 subs.removeAllElements();
349 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
350 if (idstring.indexOf(SEP) > -1)
352 idstring = idstring.substring(idstring.lastIndexOf(SEP) + 1);
355 // just return simple url substitution.
356 return new String[] { idstring, urlPrefix + idstring + urlSuffix };
360 return new String[] { "", urlPrefix };
365 public String toString()
367 return label + SEP + getUrlWithToken();
371 * @return delimited string containing label, url and target
373 public String toStringWithTarget()
375 return label + SEP + getUrlWithToken() + SEP + target;
379 * Parse the label from the link string
382 * Location of first occurrence of separator in link string
384 * Position of sequence id or name in link string
386 * Link string containing database name and url
387 * @return Position of last separator symbol prior to any regex symbols
389 protected int parseLabel(int firstSep, int psqid, String link)
396 p = link.indexOf(SEP, sep + 1);
397 } while (p > sep && p < psqid);
398 // Assuming that the URL itself does not contain any SEP symbols
399 // sep now contains last pipe symbol position prior to any regex symbols
400 label = link.substring(0, sep);
406 * Parse the target from the link string
409 * Link string containing database name and url
411 * Location of first separator symbol
413 * Location of end of any regular expression in link string
415 protected void parseTarget(String link, int sep, int endOfRegex)
417 int lastsep = link.lastIndexOf(SEP);
419 if ((lastsep != sep) && (lastsep > endOfRegex))
421 // final element in link string is the target
422 target = link.substring(lastsep + 1).trim();
429 if (target.indexOf(SEP) > -1)
431 // SEP terminated database name / www target at start of Label
432 target = target.substring(0, target.indexOf(SEP));
434 else if (target.indexOf(SPACE) > 2)
436 // space separated label - first word matches database name
437 target = target.substring(0, target.indexOf(SPACE));
442 * Parse the URL part of the link string
445 * Link string containing database name and url
447 * Name of variable in url string (e.g. SEQUENCE_ID, SEQUENCE_NAME)
449 * Position of id or name in link string
451 * Position of separator in link string
452 * @return Location of end of any regex in link string
454 protected int parseUrl(String link, String varName, int sqidPos, int sep)
456 urlPrefix = link.substring(sep + 1, sqidPos).trim();
458 // delimiter at start of regex: e.g. $SEQUENCE_ID=/
459 String startDelimiter = DELIM + varName + "=/";
461 // delimiter at end of regex: /=$
462 String endDelimiter = "/=" + DELIM;
464 int startLength = startDelimiter.length();
466 // Parse URL : Whole URL string first
467 int p = link.indexOf(endDelimiter, sqidPos + startLength);
469 if (link.indexOf(startDelimiter) == sqidPos
470 && (p > sqidPos + startLength))
472 // Extract Regex and suffix
473 urlSuffix = link.substring(p + endDelimiter.length());
474 regexReplace = link.substring(sqidPos + startLength, p);
477 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex
478 .perlCode("/" + regexReplace + "/");
481 invalidMessage = "Invalid Regular Expression : '" + regexReplace
484 } catch (Exception e)
486 invalidMessage = "Invalid Regular Expression : '" + regexReplace
494 // verify format is really correct.
495 if (link.indexOf(DELIM + varName + DELIM) == sqidPos)
497 int lastsep = link.lastIndexOf(SEP);
498 if (lastsep < sqidPos + startLength - 1)
500 // the last SEP character was before the regex, ignore
501 lastsep = link.length();
503 urlSuffix = link.substring(sqidPos + startLength - 1, lastsep)
509 invalidMessage = "Warning: invalid regex structure for URL link : "
518 * Create a set of URL links for a sequence
521 * The sequence to create links for
523 * Map of links: key = id + SEP + link, value = [target, label, id,
526 public void createLinksFromSeq(final SequenceI seq,
527 Map<String, List<String>> linkset)
529 if (seq != null && dynamic)
531 createDynamicLinks(seq, linkset);
535 createStaticLink(linkset);
540 * Create a static URL link
543 * Map of links: key = id + SEP + link, value = [target, label, id,
546 protected void createStaticLink(Map<String, List<String>> linkset)
548 if (!linkset.containsKey(label + SEP + getUrlPrefix()))
550 // Add a non-dynamic link
551 linkset.put(label + SEP + getUrlPrefix(),
552 Arrays.asList(target, label, null, getUrlPrefix()));
557 * Create dynamic URL links
560 * The sequence to create links for
562 * Map of links: key = id + SEP + link, value = [target, label, id,
565 protected void createDynamicLinks(final SequenceI seq,
566 Map<String, List<String>> linkset)
568 // collect id string too
569 String id = seq.getName();
570 String descr = seq.getDescription();
571 if (descr != null && descr.length() < 1)
576 if (usesDBAccession()) // link is ID
578 // collect matching db-refs
579 DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(),
583 // if there are any dbrefs which match up with the link
586 for (int r = 0; r < dbr.length; r++)
588 // create Bare ID link for this URL
589 createBareURLLink(dbr[r].getAccessionId(), true, linkset);
593 else if (!usesDBAccession() && id != null) // link is name
595 // create Bare ID link for this URL
596 createBareURLLink(id, false, linkset);
599 // Create urls from description but only for URL links which are regex
601 if (descr != null && getRegexReplace() != null)
603 // create link for this URL from description where regex matches
604 createBareURLLink(descr, false, linkset);
609 * Create a bare URL Link
610 * Returns map where key = id + SEP + link, and value = [target, label, id, link]
612 protected void createBareURLLink(String id, Boolean combineLabel,
613 Map<String, List<String>> linkset)
615 String[] urls = makeUrls(id, true);
618 for (int u = 0; u < urls.length; u += 2)
620 if (!linkset.containsKey(urls[u] + SEP + urls[u + 1]))
622 String thisLabel = label;
625 // incorporate label with idstring
626 thisLabel = label + SEP + urls[u];
629 linkset.put(urls[u] + SEP + urls[u + 1],
630 Arrays.asList(target, thisLabel, urls[u], urls[u + 1]));