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);
179 + (dynamic ? (DELIM + var + ((regexReplace != null) ? EQUALS
180 + regexReplace + EQUALS + DELIM : DELIM)) : "")
181 + ((urlSuffix == null) ? "" : urlSuffix);
185 * @return the regexReplace
187 public String getRegexReplace()
193 * @return the invalidMessage
195 public String getInvalidMessage()
197 return invalidMessage;
201 * Check if URL string was parsed properly.
203 * @return boolean - if false then <code>getInvalidMessage</code> returns an
206 public boolean isValid()
208 return invalidMessage == null;
213 * @return whether link is dynamic
215 public boolean isDynamic()
222 * @return whether link uses DB Accession id
224 public boolean usesDBAccession()
226 return usesDBaccession;
234 public void setLabel(String newlabel)
236 this.label = newlabel;
244 public void setTarget(String desc)
250 * return one or more URL strings by applying regex to the given idstring
253 * @param onlyIfMatches
254 * - when true url strings are only made if regex is defined and
256 * @return String[] { part of idstring substituted, full substituted url , ..
257 * next part, next url..}
259 public String[] makeUrls(String idstring, boolean onlyIfMatches)
263 if (regexReplace != null)
265 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
266 + regexReplace + "/");
267 if (rg.search(idstring))
269 int ns = rg.numSubs();
273 return new String[] { rg.stringMatched(),
274 urlPrefix + rg.stringMatched() + urlSuffix };
276 * else if (ns==1) { // take only subgroup match return new String[]
277 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
283 for (int s = 0; s <= rg.numSubs(); s++)
285 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)
286 + " : " + rg.matchedTo(s) + " : '"
287 + rg.stringMatched(s) + "'");
289 // try to collate subgroup matches
290 Vector<String> subs = new Vector<String>();
291 // have to loop through submatches, collating them at top level
296 if (s + 1 <= ns && rg.matchedTo(s) > -1
297 && rg.matchedTo(s + 1) > -1
298 && rg.matchedTo(s + 1) < rg.matchedTo(s))
300 // s is top level submatch. search for submatches enclosed by
304 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
306 if (rg.matchedFrom(r) > -1)
308 mtch += rg.stringMatched(r);
312 if (mtch.length() > 0)
314 subs.addElement(mtch);
315 subs.addElement(urlPrefix + mtch + urlSuffix);
321 if (rg.matchedFrom(s) > -1)
323 subs.addElement(rg.stringMatched(s));
324 subs.addElement(urlPrefix + rg.stringMatched(s)
331 String[] res = new String[subs.size()];
332 for (int r = 0, rs = subs.size(); r < rs; r++)
334 res[r] = subs.elementAt(r);
336 subs.removeAllElements();
345 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
346 if (idstring.indexOf(SEP) > -1)
348 idstring = idstring.substring(idstring.lastIndexOf(SEP) + 1);
351 // just return simple url substitution.
352 return new String[] { idstring, urlPrefix + idstring + urlSuffix };
356 return new String[] { "", urlPrefix };
361 public String toString()
363 return label + SEP + getUrlWithToken();
367 * @return delimited string containing label, url and target
369 public String toStringWithTarget()
371 return label + SEP + getUrlWithToken() + SEP + target;
375 * Parse the label from the link string
378 * Location of first occurrence of separator in link string
380 * Position of sequence id or name in link string
382 * Link string containing database name and url
383 * @return Position of last separator symbol prior to any regex symbols
385 protected int parseLabel(int firstSep, int psqid, String link)
392 p = link.indexOf(SEP, sep + 1);
393 } while (p > sep && p < psqid);
394 // Assuming that the URL itself does not contain any SEP symbols
395 // sep now contains last pipe symbol position prior to any regex symbols
396 label = link.substring(0, sep);
402 * Parse the target from the link string
405 * Link string containing database name and url
407 * Location of first separator symbol
409 * Location of end of any regular expression in link string
411 protected void parseTarget(String link, int sep, int endOfRegex)
413 int lastsep = link.lastIndexOf(SEP);
415 if ((lastsep != sep) && (lastsep > endOfRegex))
417 // final element in link string is the target
418 target = link.substring(lastsep + 1).trim();
425 if (target.indexOf(SEP) > -1)
427 // SEP terminated database name / www target at start of Label
428 target = target.substring(0, target.indexOf(SEP));
430 else if (target.indexOf(SPACE) > 2)
432 // space separated label - first word matches database name
433 target = target.substring(0, target.indexOf(SPACE));
438 * Parse the URL part of the link string
441 * Link string containing database name and url
443 * Name of variable in url string (e.g. SEQUENCE_ID, SEQUENCE_NAME)
445 * Position of id or name in link string
447 * Position of separator in link string
448 * @return Location of end of any regex in link string
450 protected int parseUrl(String link, String varName, int sqidPos, int sep)
452 urlPrefix = link.substring(sep + 1, sqidPos).trim();
454 // delimiter at start of regex: e.g. $SEQUENCE_ID=/
455 String startDelimiter = DELIM + varName + "=/";
457 // delimiter at end of regex: /=$
458 String endDelimiter = "/=" + DELIM;
460 int startLength = startDelimiter.length();
462 // Parse URL : Whole URL string first
463 int p = link.indexOf(endDelimiter, sqidPos + startLength);
465 if (link.indexOf(startDelimiter) == sqidPos
466 && (p > sqidPos + startLength))
468 // Extract Regex and suffix
469 urlSuffix = link.substring(p + endDelimiter.length());
470 regexReplace = link.substring(sqidPos + startLength, p);
473 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
474 + regexReplace + "/");
477 invalidMessage = "Invalid Regular Expression : '" + regexReplace
480 } catch (Exception e)
482 invalidMessage = "Invalid Regular Expression : '" + regexReplace
490 // verify format is really correct.
491 if (link.indexOf(DELIM + varName + DELIM) == sqidPos)
493 int lastsep = link.lastIndexOf(SEP);
494 if (lastsep < sqidPos + startLength - 1)
496 // the last SEP character was before the regex, ignore
497 lastsep = link.length();
499 urlSuffix = link.substring(sqidPos + startLength - 1, lastsep)
505 invalidMessage = "Warning: invalid regex structure for URL link : "
514 * Create a set of URL links for a sequence
517 * The sequence to create links for
519 * Map of links: key = id + SEP + link, value = [target, label, id,
522 public void createLinksFromSeq(final SequenceI seq,
523 Map<String, List<String>> linkset)
525 if (seq != null && dynamic)
527 createDynamicLinks(seq, linkset);
531 createStaticLink(linkset);
536 * Create a static URL link
539 * Map of links: key = id + SEP + link, value = [target, label, id,
542 protected void createStaticLink(Map<String, List<String>> linkset)
544 if (!linkset.containsKey(label + SEP + getUrlPrefix()))
546 // Add a non-dynamic link
547 linkset.put(label + SEP + getUrlPrefix(),
548 Arrays.asList(target, label, null, getUrlPrefix()));
553 * Create dynamic URL links
556 * The sequence to create links for
558 * Map of links: key = id + SEP + link, value = [target, label, id,
561 protected void createDynamicLinks(final SequenceI seq,
562 Map<String, List<String>> linkset)
564 // collect id string too
565 String id = seq.getName();
566 String descr = seq.getDescription();
567 if (descr != null && descr.length() < 1)
572 if (usesDBAccession()) // link is ID
574 // collect matching db-refs
575 DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(),
576 new String[] { target });
578 // if there are any dbrefs which match up with the link
581 for (int r = 0; r < dbr.length; r++)
583 // create Bare ID link for this URL
584 createBareURLLink(dbr[r].getAccessionId(), true, linkset);
588 else if (!usesDBAccession() && id != null) // link is name
590 // create Bare ID link for this URL
591 createBareURLLink(id, false, linkset);
594 // Create urls from description but only for URL links which are regex
596 if (descr != null && getRegexReplace() != null)
598 // create link for this URL from description where regex matches
599 createBareURLLink(descr, false, linkset);
604 * Create a bare URL Link
605 * Returns map where key = id + SEP + link, and value = [target, label, id, link]
607 protected void createBareURLLink(String id, Boolean combineLabel,
608 Map<String, List<String>> linkset)
610 String[] urls = makeUrls(id, true);
613 for (int u = 0; u < urls.length; u += 2)
615 if (!linkset.containsKey(urls[u] + SEP + urls[u + 1]))
617 String thisLabel = label;
620 // incorporate label with idstring
621 thisLabel = label + SEP + urls[u];
624 linkset.put(urls[u] + SEP + urls[u + 1],
625 Arrays.asList(target, thisLabel, urls[u], urls[u + 1]));