+ /**
+ * Parse the URL part of the link string
+ *
+ * @param link
+ * Link string containing database name and url
+ * @param varName
+ * Name of variable in url string (e.g. SEQUENCE_ID, SEQUENCE_NAME)
+ * @param sqidPos
+ * Position of id or name in link string
+ * @param sep
+ * Position of separator in link string
+ */
+ protected void parseUrl(String link, String varName, int sqidPos, int sep)
+ {
+ urlPrefix = link.substring(sep + 1, sqidPos).trim();
+
+ // delimiter at start of regex: e.g. $SEQUENCE_ID=/
+ String startDelimiter = DELIM + varName + "=/";
+
+ // delimiter at end of regex: /=$
+ String endDelimiter = "/=" + DELIM;
+
+ int startLength = startDelimiter.length();
+
+ // Parse URL : Whole URL string first
+ int p = link.indexOf(endDelimiter, sqidPos + startLength);
+
+ if (link.indexOf(startDelimiter) == sqidPos
+ && (p > sqidPos + startLength))
+ {
+ // Extract Regex and suffix
+ urlSuffix = link.substring(p + endDelimiter.length());
+ regexReplace = link.substring(sqidPos + startLength, p);
+ try
+ {
+ com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
+ + regexReplace + "/");
+ if (rg == null)
+ {
+ invalidMessage = "Invalid Regular Expression : '" + regexReplace
+ + "'\n";
+ }
+ } catch (Exception e)
+ {
+ invalidMessage = "Invalid Regular Expression : '" + regexReplace
+ + "'\n";
+ }
+ }
+ else
+ {
+ // no regex
+ regexReplace = null;
+ // verify format is really correct.
+ if (link.indexOf(DELIM + varName + DELIM) == sqidPos)
+ {
+ urlSuffix = link.substring(sqidPos + startLength - 1);
+ regexReplace = null;
+ }
+ else
+ {
+ invalidMessage = "Warning: invalid regex structure for URL link : "
+ + link;
+ }
+ }
+ }
+
+ /**
+ * Create a set of URL links for a sequence
+ *
+ * @param seq
+ * The sequence to create links for
+ * @param linkset
+ * Map of links: key = id + SEP + link, value = [target, label, id,
+ * link]
+ */
+ public void createLinksFromSeq(final SequenceI seq,
+ Map<String, List<String>> linkset)
+ {
+ if (seq != null && dynamic)
+ {
+ createDynamicLinks(seq, linkset);
+ }
+ else
+ {
+ createStaticLink(linkset);
+ }
+ }
+
+ /**
+ * Create a static URL link
+ *
+ * @param linkset
+ * Map of links: key = id + SEP + link, value = [target, label, id,
+ * link]
+ */
+ protected void createStaticLink(Map<String, List<String>> linkset)
+ {
+ if (!linkset.containsKey(label + SEP + getUrl_prefix()))
+ {
+ // Add a non-dynamic link
+ linkset.put(label + SEP + getUrl_prefix(),
+ Arrays.asList(target, label, null, getUrl_prefix()));
+ }
+ }
+
+ /**
+ * Create dynamic URL links
+ *
+ * @param seq
+ * The sequence to create links for
+ * @param linkset
+ * Map of links: key = id + SEP + link, value = [target, label, id,
+ * link]
+ */
+ protected void createDynamicLinks(final SequenceI seq,
+ Map<String, List<String>> linkset)
+ {
+ // collect id string too
+ String id = seq.getName();
+ String descr = seq.getDescription();
+ if (descr != null && descr.length() < 1)
+ {
+ descr = null;
+ }
+
+ if (usesDBAccession()) // link is ID
+ {
+ // collect matching db-refs
+ DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(),
+ new String[] { target });
+
+ // if there are any dbrefs which match up with the link
+ if (dbr != null)
+ {
+ for (int r = 0; r < dbr.length; r++)
+ {
+ // create Bare ID link for this URL
+ createBareURLLink(dbr[r].getAccessionId(), true, linkset);
+ }
+ }
+ }
+ else if (!usesDBAccession() && id != null) // link is name
+ {
+ // create Bare ID link for this URL
+ createBareURLLink(id, false, linkset);
+ }
+
+ // Create urls from description but only for URL links which are regex
+ // links
+ if (descr != null && getRegexReplace() != null)
+ {
+ // create link for this URL from description where regex matches
+ createBareURLLink(descr, false, linkset);
+ }
+ }
+
+ /*
+ * Create a bare URL Link
+ * Returns map where key = id + SEP + link, and value = [target, label, id, link]
+ */
+ protected void createBareURLLink(String id, Boolean combineLabel,
+ Map<String, List<String>> linkset)
+ {
+ String[] urls = makeUrls(id, true);
+ if (urls != null)
+ {
+ for (int u = 0; u < urls.length; u += 2)
+ {
+ if (!linkset.containsKey(urls[u] + SEP + urls[u + 1]))
+ {
+ String thisLabel = label;
+ if (combineLabel)
+ {
+ // incorporate label with idstring
+ thisLabel = label + SEP + urls[u];
+ }
+
+ linkset.put(urls[u] + SEP + urls[u + 1],
+ Arrays.asList(target, thisLabel, urls[u], urls[u + 1]));
+ }
+ }
+ }
+ }
+