2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.datamodel.Sequence;
21 import jalview.datamodel.SequenceI;
23 import java.util.Hashtable;
24 import java.util.Vector;
26 public class GroupUrlLink
29 * Helper class based on the UrlLink class which enables URLs to be
30 * constructed from sequences or IDs associated with a group of sequences. URL
31 * definitions consist of a pipe separated string containing a <label>|<url
32 * construct>|<separator character>[|<sequence separator character>]. The url
33 * construct includes regex qualified tokens which are replaced with seuqence
34 * IDs ($SEQUENCE_IDS$) and/or seuqence regions ($SEQUENCES$) that are
35 * extracted from the group. See <code>UrlLink</code> for more information
36 * about the approach, and the original implementation. Documentation to come.
37 * Note - groupUrls can be very big!
39 private String url_prefix, target, label;
42 * these are all filled in order of the occurence of each token in the url
45 private String url_suffix[], separators[], regexReplace[];
47 private String invalidMessage = null;
50 * tokens that can be replaced in the URL.
52 private static String[] tokens;
55 * position of each token (which can appear once only) in the url
60 * contains tokens in the order they appear in the URL template.
62 private String[] mtch;
68 { "SEQUENCEIDS", "SEQUENCES", "DATASETID" };
72 // private int idseg = -1, seqseg = -1;
75 * parse the given linkString of the form '<label>|<url>|separator
76 * char[|optional sequence separator char]' into parts. url may contain a
77 * string $SEQUENCEIDS<=optional regex=>$ where <=optional regex=> must be of
78 * the form =/<perl style regex>/=$ or $SEQUENCES<=optional regex=>$ or
79 * $SEQUENCES<=optional regex=>$.
83 public GroupUrlLink(String link)
85 int sep = link.indexOf("|");
86 segs = new int[tokens.length];
88 for (int i = 0; i < segs.length; i++)
90 if ((segs[i] = link.indexOf("$" + tokens[i])) > -1)
95 // expect at least one token
98 invalidMessage = "Group URL string must contain at least one of ";
99 for (int i = 0; i < segs.length; i++)
101 invalidMessage += " '$" + tokens[i] + "[=/regex=/]$'";
106 int[] ptok = new int[ntoks + 1];
107 String[] tmtch = new String[ntoks + 1];
108 mtch = new String[ntoks];
109 for (int i = 0, t = 0; i < segs.length; i++)
114 tmtch[t++] = tokens[i];
117 ptok[ntoks] = link.length();
118 tmtch[ntoks] = "$$$$$$$$$";
119 jalview.util.QuickSort.sort(ptok, tmtch);
120 for (int i = 0; i < ntoks; i++)
122 mtch[i] = tmtch[i]; // TODO: check order is ascending
125 * replaces the specific code below {}; if (psqids > -1 && pseqs > -1) { if
126 * (psqids > pseqs) { idseg = 1; seqseg = 0;
128 * ptok = new int[] { pseqs, psqids, link.length() }; mtch = new String[] {
129 * "$SEQUENCES", "$SEQUENCEIDS" }; } else { idseg = 0; seqseg = 1; ptok =
130 * new int[] { psqids, pseqs, link.length() }; mtch = new String[] {
131 * "$SEQUENCEIDS", "$SEQUENCES" }; } } else { if (psqids != -1) { idseg = 0;
132 * ptok = new int[] { psqids, link.length() }; mtch = new String[] {
133 * "$SEQUENCEIDS" }; } else { seqseg = 0; ptok = new int[] { pseqs,
134 * link.length() }; mtch = new String[] { "$SEQUENCES" }; } }
138 // first get the label and target part before the first |
142 p = link.indexOf("|", sep + 1);
143 } while (p > sep && p < ptok[0]);
144 // Assuming that the URL itself does not contain any '|' symbols
145 // sep now contains last pipe symbol position prior to any regex symbols
146 label = link.substring(0, sep);
147 if (label.indexOf("|") > -1)
149 // | terminated database name / www target at start of Label
150 target = label.substring(0, label.indexOf("|"));
152 else if (label.indexOf(" ") > 2)
154 // space separated Label - matches database name
155 target = label.substring(0, label.indexOf(" "));
161 // Now Parse URL : Whole URL string first
162 url_prefix = link.substring(sep + 1, ptok[0]);
163 url_suffix = new String[mtch.length];
164 regexReplace = new String[mtch.length];
165 // and loop through tokens
166 for (int pass = 0; pass < mtch.length; pass++)
168 int mlength = 3 + mtch[pass].length();
169 if (link.indexOf("$" + mtch[pass] + "=/") == ptok[pass]
170 && (p = link.indexOf("/=$", ptok[pass] + mlength)) > ptok[pass]
173 // Extract Regex and suffix
174 if (ptok[pass + 1] < p + 3)
176 // tokens are not allowed inside other tokens - e.g. inserting a
177 // $sequences$ into the regex match for the sequenceid
178 invalidMessage = "Token regexes cannot contain other regexes (did you terminate the $"
179 + mtch[pass] + " regex with a '/=$' ?";
182 url_suffix[pass] = link.substring(p + 3, ptok[pass + 1]);
183 regexReplace[pass] = link.substring(ptok[pass] + mlength, p);
186 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
187 + regexReplace[pass] + "/");
190 invalidMessage = "Invalid Regular Expression : '"
191 + regexReplace[pass] + "'\n";
193 } catch (Exception e)
195 invalidMessage = "Invalid Regular Expression : '"
196 + regexReplace[pass] + "'\n";
201 regexReplace[pass] = null;
202 // verify format is really correct.
203 if ((p = link.indexOf("$" + mtch[pass] + "$")) == ptok[pass])
205 url_suffix[pass] = link.substring(p + mtch[pass].length() + 2,
210 invalidMessage = "Warning: invalid regex structure (after '"
211 + mtch[0] + "') for URL link : " + link;
216 separators = new String[url_suffix.length];
217 String suffices = url_suffix[url_suffix.length - 1], lastsep = ",";
218 // have a look in the last suffix for any more separators.
219 while ((p = suffices.indexOf('|')) > -1)
221 separators[pass] = suffices.substring(p + 1);
224 // trim the original suffix string
225 url_suffix[url_suffix.length - 1] = suffices.substring(0, p);
229 lastsep = (separators[pass - 1] = separators[pass - 1].substring(0,
232 suffices = separators[pass];
237 lastsep = separators[pass - 1];
239 // last separator is always used for all the remaining separators
240 while (pass < separators.length)
242 separators[pass++] = lastsep;
247 * @return the url_suffix
249 public String getUrl_suffix()
251 return url_suffix[url_suffix.length - 1];
255 * @return the url_prefix
257 public String getUrl_prefix()
265 public String getTarget()
273 public String getLabel()
279 * @return the sequence ID regexReplace
281 public String getIDRegexReplace()
283 return _replaceFor(tokens[0]);
286 private String _replaceFor(String token)
288 for (int i = 0; i < mtch.length; i++)
289 if (segs[i] > -1 && mtch[i].equals(token))
291 return regexReplace[i];
297 * @return the sequence ID regexReplace
299 public String getSeqRegexReplace()
301 return _replaceFor(tokens[1]);
305 * @return the invalidMessage
307 public String getInvalidMessage()
309 return invalidMessage;
313 * Check if URL string was parsed properly.
315 * @return boolean - if false then <code>getInvalidMessage</code> returns an
318 public boolean isValid()
320 return invalidMessage == null;
324 * return one or more URL strings by applying regex to the given idstring
327 * array of id strings to pass to service
329 * array of seq strings to pass to service
330 * @param onlyIfMatches
331 * - when true url strings are only made if regex is defined and
332 * matches for all qualified tokens in groupURL - TODO: consider if
333 * onlyIfMatches is really a useful parameter!
334 * @return null or Object[] { int[] { number of seqs substituted},boolean[] {
335 * which seqs were substituted }, StringBuffer[] { substituted lists
336 * for each token }, String[] { url } }
338 public Object[] makeUrls(String[] idstrings, String[] seqstrings,
339 String dsstring, boolean onlyIfMatches)
341 Hashtable rstrings = replacementArgs(idstrings, seqstrings, dsstring);
342 return makeUrls(rstrings, onlyIfMatches);
346 * gathers input into a hashtable
353 private Hashtable replacementArgs(String[] idstrings,
354 String[] seqstrings, String dsstring)
356 Hashtable rstrings = new Hashtable();
357 rstrings.put(tokens[0], idstrings);
358 rstrings.put(tokens[1], seqstrings);
359 rstrings.put(tokens[2], new String[]
361 if (idstrings.length != seqstrings.length)
364 "idstrings and seqstrings contain one string each per sequence.");
369 public Object[] makeUrls(Hashtable repstrings, boolean onlyIfMatches)
371 return makeUrlsIf(true, repstrings, onlyIfMatches);
380 * @return URL stub objects ready to pass to constructFrom
382 public Object[] makeUrlStubs(String[] ids, String[] seqstr,
383 String string, boolean b)
385 Hashtable rstrings = replacementArgs(ids, seqstr, string);
386 Object[] stubs = makeUrlsIf(false, rstrings, b);
390 { stubs[0], stubs[1], rstrings, new boolean[]
393 // TODO Auto-generated method stub
398 * generate the URL for the given URL stub object array returned from
402 * @return URL string.
404 public String constructFrom(Object[] stubs)
406 Object[] results = makeUrlsIf(true, (Hashtable) stubs[2],
407 ((boolean[]) stubs[3])[0]);
408 return ((String[]) results[3])[0];
412 * conditionally generate urls or stubs for a given input.
414 * @param createFullUrl
415 * set to false if you only want to test if URLs would be generated.
417 * @param onlyIfMatches
418 * @return null if no url is generated. Object[] { int[] { number of matches
419 * seqs }, boolean[] { which matched }, (if createFullUrl also has
420 * StringBuffer[] { segment generated from inputs that is used in URL
421 * }, String[] { url })}
423 protected Object[] makeUrlsIf(boolean createFullUrl,
424 Hashtable repstrings, boolean onlyIfMatches)
428 // prepare string arrays in correct order to be assembled into URL input
429 String[][] idseq = new String[mtch.length][]; // indexed by pass
430 int mins = 0, maxs = 0; // allowed two values, 1 or n-sequences.
431 for (int i = 0; i < mtch.length; i++)
433 idseq[i] = (String[]) repstrings.get(mtch[i]);
434 if (idseq[i].length >= 1)
436 if (mins == 0 && idseq[i].length == 1)
442 maxs = idseq[i].length;
446 if (maxs != idseq[i].length)
449 "Cannot have mixed length replacement vectors. Replacement vector for "
450 + (mtch[i]) + " is " + idseq[i].length
451 + " strings long, and have already seen a "
452 + maxs + " length vector.");
459 "Cannot have zero length vector of replacement strings - either 1 value or n values.");
462 // iterate through input, collating segments to be inserted into url
463 StringBuffer matched[] = new StringBuffer[idseq.length];
464 // and precompile regexes
465 com.stevesoft.pat.Regex[] rgxs = new com.stevesoft.pat.Regex[matched.length];
466 for (pass = 0; pass < matched.length; pass++)
468 matched[pass] = new StringBuffer();
469 if (regexReplace[pass] != null)
471 rgxs[pass] = com.stevesoft.pat.Regex.perlCode("/"
472 + regexReplace[pass] + "/");
479 // tot up the invariant lengths for this url
480 int urllength = url_prefix.length();
481 for (pass = 0; pass < matched.length; pass++)
483 urllength += url_suffix[pass].length();
486 // flags to record which of the input sequences were actually used to
489 boolean[] thismatched = new boolean[maxs];
491 for (int sq = 0; sq < maxs; sq++)
493 // initialise flag for match
494 thismatched[sq] = false;
495 StringBuffer[] thematches = new StringBuffer[rgxs.length];
496 for (pass = 0; pass < rgxs.length; pass++)
498 thematches[pass] = new StringBuffer(); // initialise - in case there are
501 // if a regex is provided, then it must match for all sequences in all
502 // tokens for it to be considered.
503 if (idseq[pass].length <= sq)
505 // no more replacement strings to try for this token
508 if (rgxs[pass] != null)
510 com.stevesoft.pat.Regex rg = rgxs[pass];
512 // concatenate all matches of re in the given string!
513 while (rg.searchFrom(idseq[pass][sq], rematchat))
515 rematchat = rg.matchedTo();
516 thismatched[sq] |= true;
517 urllength += rg.charsMatched(); // count length
520 continue; // don't bother making the URL replacement text.
522 // do we take the cartesian products of the substituents ?
523 int ns = rg.numSubs();
526 thematches[pass].append(rg.stringMatched());// take whole regex
529 * else if (ns==1) { // take only subgroup match return new String[]
530 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
533 // deal with multiple submatch case - for moment we do the simplest
534 // - concatenate the matched regions, instead of creating a complete
535 // list for each alternate match over all sequences.
536 // TODO: specify a 'replace pattern' - next refinement
541 * for (int s = 0; s <= rg.numSubs(); s++) {
542 * System.err.println("Sub " + s + " : " + rg.matchedFrom(s) +
543 * " : " + rg.matchedTo(s) + " : '" + rg.stringMatched(s) + "'");
546 // try to collate subgroup matches
547 StringBuffer subs = new StringBuffer();
548 // have to loop through submatches, collating them at top level
553 if (s + 1 <= ns && rg.matchedTo(s) > -1
554 && rg.matchedTo(s + 1) > -1
555 && rg.matchedTo(s + 1) < rg.matchedTo(s))
557 // s is top level submatch. search for submatches enclosed by
560 StringBuffer rmtch = new StringBuffer();
561 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
563 if (rg.matchedFrom(r) > -1)
565 rmtch.append(rg.stringMatched(r));
569 if (rmtch.length() > 0)
571 subs.append(rmtch); // simply concatenate
577 if (rg.matchedFrom(s) > -1)
579 subs.append(rg.stringMatched(s)); // concatenate
584 thematches[pass].append(subs);
590 // are we only supposed to take regex matches ?
593 thismatched[sq] |= true;
594 urllength += idseq[pass][sq].length(); // tot up length
597 thematches[pass] = new StringBuffer(idseq[pass][sq]); // take
600 // regardless - probably not a
603 * TODO: do some boilerplate trimming of the fields to make them
604 * sensible e.g. trim off any 'prefix' in the id string (see
605 * UrlLink for the below) - pre 2.4 Jalview behaviour if
606 * (idstring.indexOf("|") > -1) { idstring =
607 * idstring.substring(idstring.lastIndexOf("|") + 1); }
615 // check if we are going to add this sequence's results ? all token
616 // replacements must be valid for this to happen!
617 // (including single value replacements - eg. dataset name)
622 for (pass = 0; pass < matched.length; pass++)
624 if (idseq[pass].length > 1 && matched[pass].length() > 0)
626 matched[pass].append(separators[pass]);
628 matched[pass].append(thematches[pass]);
634 // finally, if any sequences matched, then form the URL and return
635 if (seqsmatched == 0 || (createFullUrl && matched[0].length() == 0))
637 // no matches - no url generated
640 // check if we are beyond the feasible command line string limit for this
642 if ((urllength + 32) > Platform.getMaxCommandLineLength())
644 System.err.println("URL estimated to be too long " + urllength);
649 // just return the essential info about what the URL would be generated
653 { seqsmatched }, thismatched };
655 // otherwise, create the URL completely.
657 StringBuffer submiturl = new StringBuffer();
658 submiturl.append(url_prefix);
659 for (pass = 0; pass < matched.length; pass++)
661 submiturl.append(matched[pass]);
662 if (url_suffix[pass] != null)
664 submiturl.append(url_suffix[pass]);
670 { seqsmatched }, thismatched, matched, new String[]
671 { submiturl.toString() } };
677 * @return number of distinct sequence (id or seuqence) replacements predicted
680 public int getNumberInvolved(Object[] urlstub)
682 return ((int[]) urlstub[0])[0]; // returns seqsmatched from
683 // makeUrlsIf(false,...)
687 * get token types present in this url as a bitfield indicating presence of
688 * each token from tokens (LSB->MSB).
690 * @return groupURL class as integer
692 public int getGroupURLType()
695 for (int pass = 0; pass < tokens.length; pass++)
697 for (int i = 0; i < mtch.length; i++)
699 if (mtch[i].equals(tokens[pass]))
708 public String toString()
710 StringBuffer result = new StringBuffer();
711 result.append(label + "|" + url_prefix);
713 for (r = 0; r < url_suffix.length; r++)
716 result.append(mtch[r]);
717 if (regexReplace[r] != null)
720 result.append(regexReplace[r]);
724 result.append(url_suffix[r]);
726 for (r = 0; r < separators.length; r++)
729 result.append(separators[r]);
731 return result.toString();
735 * report stats about the generated url string given an input set
741 private static void testUrls(GroupUrlLink ul, String[][] idstring,
747 System.out.println("Created NO urls.");
751 System.out.println("Created a url from " + ((int[]) url[0])[0]
752 + "out of " + idstring[0].length + " sequences.");
753 System.out.println("Sequences that did not match:");
754 for (int sq = 0; sq < idstring[0].length; sq++)
756 if (!((boolean[]) url[1])[sq])
758 System.out.println("Seq " + sq + ": " + idstring[0][sq] + "\t: "
762 System.out.println("Sequences that DID match:");
763 for (int sq = 0; sq < idstring[0].length; sq++)
765 if (((boolean[]) url[1])[sq])
767 System.out.println("Seq " + sq + ": " + idstring[0][sq] + "\t: "
771 System.out.println("The generated URL:");
772 System.out.println(((String[]) url[3])[0]);
776 public static void main(String argv[])
778 String[] links = new String[]
780 "EnVision2|IDS|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=linkInDatasetFromJalview&input=$SEQUENCEIDS$&inputType=0|,",
781 "EnVision2|Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=linkInDatasetFromJalview&input=$SEQUENCES$&inputType=1|,",
782 "EnVision2|IDS|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,",
783 "EnVision2|Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=$DATASETID$&input=$SEQUENCES$&inputType=1|,",
784 "EnVision2|IDS|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=$SEQUENCEIDS$&datasetName=linkInDatasetFromJalview&input=$SEQUENCEIDS$&inputType=0|,",
785 "EnVision2|Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=$SEQUENCEIDS$&datasetName=$DATASETID$&input=$SEQUENCES$&inputType=1|,",
786 "EnVision2 Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Default&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([a-zA-Z]+)/=$&inputType=1|,",
787 "EnVision2 Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Default&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/[A-Za-z]+/=$&inputType=1|,"
789 * http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?input=P38389,P38398
790 * &inputType=0&workflow=Enfin%20Default%20Workflow&datasetName=
791 * linkInDatasetFromPRIDE
795 SequenceI[] seqs = new SequenceI[]
796 { new Sequence("StupidLabel:gi|9234|pdb|102L|A",
797 "asdiasdpasdpadpwpadasdpaspdw"), };
798 String[][] seqsandids = formStrings(seqs);
799 for (int i = 0; i < links.length; i++)
801 GroupUrlLink ul = new GroupUrlLink(links[i]);
804 System.out.println("\n\n\n");
805 System.out.println("Link " + i + " " + links[i] + " : "
807 System.out.println(" pref : " + ul.getUrl_prefix());
808 System.out.println(" IdReplace : " + ul.getIDRegexReplace());
809 System.out.println(" SeqReplace : " + ul.getSeqRegexReplace());
810 System.out.println(" Suffixes : " + ul.getUrl_suffix());
813 .println("<insert input id and sequence strings here> Without onlyIfMatches:");
814 Object[] urls = ul.makeUrls(seqsandids[0], seqsandids[1],
816 testUrls(ul, seqsandids, urls);
818 .println("<insert input id and sequence strings here> With onlyIfMatches set:");
819 urls = ul.makeUrls(seqsandids[0], seqsandids[1], "mydataset", true);
820 testUrls(ul, seqsandids, urls);
824 System.err.println("Invalid URLLink : " + links[i] + " : "
825 + ul.getInvalidMessage());
831 * covenience method to generate the id and sequence string vector from a set
832 * of seuqences using each sequence's getName() and getSequenceAsString()
836 * @return String[][] {{sequence ids},{sequence strings}}
838 public static String[][] formStrings(SequenceI[] seqs)
840 String[][] idset = new String[2][seqs.length];
841 for (int i = 0; i < seqs.length; i++)
843 idset[0][i] = seqs[i].getName();
844 idset[1][i] = seqs[i].getSequenceAsString();
849 public void setLabel(String newlabel)
851 this.label = newlabel;