e8af4cd7e2e8bb54117e6a9583ba3147203694a0
[jalview.git] / src / jalview / util / GroupUrlLink.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.util;
22
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceI;
25
26 import java.util.Hashtable;
27 import java.util.Vector;
28
29 public class GroupUrlLink
30 {
31   public class UrlStringTooLongException extends Exception
32   {
33     public UrlStringTooLongException(int lng)
34     {
35       urlLength = lng;
36     }
37
38     public int urlLength;
39
40     public String toString()
41     {
42       return "Generated url is estimated to be too long (" + urlLength
43               + ")";
44     }
45   }
46
47   /**
48    * Helper class based on the UrlLink class which enables URLs to be
49    * constructed from sequences or IDs associated with a group of sequences. URL
50    * definitions consist of a pipe separated string containing a <label>|<url
51    * construct>|<separator character>[|<sequence separator character>]. The url
52    * construct includes regex qualified tokens which are replaced with seuqence
53    * IDs ($SEQUENCE_IDS$) and/or seuqence regions ($SEQUENCES$) that are
54    * extracted from the group. See <code>UrlLink</code> for more information
55    * about the approach, and the original implementation. Documentation to come.
56    * Note - groupUrls can be very big!
57    */
58   private String url_prefix, target, label;
59
60   /**
61    * these are all filled in order of the occurence of each token in the url
62    * string template
63    */
64   private String url_suffix[], separators[], regexReplace[];
65
66   private String invalidMessage = null;
67
68   /**
69    * tokens that can be replaced in the URL.
70    */
71   private static String[] tokens;
72
73   /**
74    * position of each token (which can appear once only) in the url
75    */
76   private int[] segs;
77
78   /**
79    * contains tokens in the order they appear in the URL template.
80    */
81   private String[] mtch;
82   static
83   {
84     if (tokens == null)
85     {
86       tokens = new String[]
87       { "SEQUENCEIDS", "SEQUENCES", "DATASETID" };
88     }
89   }
90
91   /**
92    * test for GroupURLType bitfield (with default tokens)
93    */
94   public static final int SEQUENCEIDS = 1;
95
96   /**
97    * test for GroupURLType bitfield (with default tokens)
98    */
99   public static final int SEQUENCES = 2;
100
101   /**
102    * test for GroupURLType bitfield (with default tokens)
103    */
104   public static final int DATASETID = 4;
105
106   // private int idseg = -1, seqseg = -1;
107
108   /**
109    * parse the given linkString of the form '<label>|<url>|separator
110    * char[|optional sequence separator char]' into parts. url may contain a
111    * string $SEQUENCEIDS<=optional regex=>$ where <=optional regex=> must be of
112    * the form =/<perl style regex>/=$ or $SEQUENCES<=optional regex=>$ or
113    * $SEQUENCES<=optional regex=>$.
114    * 
115    * @param link
116    */
117   public GroupUrlLink(String link)
118   {
119     int sep = link.indexOf("|");
120     segs = new int[tokens.length];
121     int ntoks = 0;
122     for (int i = 0; i < segs.length; i++)
123     {
124       if ((segs[i] = link.indexOf("$" + tokens[i])) > -1)
125       {
126         ntoks++;
127       }
128     }
129     // expect at least one token
130     if (ntoks == 0)
131     {
132       invalidMessage = "Group URL string must contain at least one of ";
133       for (int i = 0; i < segs.length; i++)
134       {
135         invalidMessage += " '$" + tokens[i] + "[=/regex=/]$'";
136       }
137       return;
138     }
139
140     int[] ptok = new int[ntoks + 1];
141     String[] tmtch = new String[ntoks + 1];
142     mtch = new String[ntoks];
143     for (int i = 0, t = 0; i < segs.length; i++)
144     {
145       if (segs[i] > -1)
146       {
147         ptok[t] = segs[i];
148         tmtch[t++] = tokens[i];
149       }
150     }
151     ptok[ntoks] = link.length();
152     tmtch[ntoks] = "$$$$$$$$$";
153     jalview.util.QuickSort.sort(ptok, tmtch);
154     for (int i = 0; i < ntoks; i++)
155     {
156       mtch[i] = tmtch[i]; // TODO: check order is ascending
157     }
158     /*
159      * replaces the specific code below {}; if (psqids > -1 && pseqs > -1) { if
160      * (psqids > pseqs) { idseg = 1; seqseg = 0;
161      * 
162      * ptok = new int[] { pseqs, psqids, link.length() }; mtch = new String[] {
163      * "$SEQUENCES", "$SEQUENCEIDS" }; } else { idseg = 0; seqseg = 1; ptok =
164      * new int[] { psqids, pseqs, link.length() }; mtch = new String[] {
165      * "$SEQUENCEIDS", "$SEQUENCES" }; } } else { if (psqids != -1) { idseg = 0;
166      * ptok = new int[] { psqids, link.length() }; mtch = new String[] {
167      * "$SEQUENCEIDS" }; } else { seqseg = 0; ptok = new int[] { pseqs,
168      * link.length() }; mtch = new String[] { "$SEQUENCES" }; } }
169      */
170
171     int p = sep;
172     // first get the label and target part before the first |
173     do
174     {
175       sep = p;
176       p = link.indexOf("|", sep + 1);
177     } while (p > sep && p < ptok[0]);
178     // Assuming that the URL itself does not contain any '|' symbols
179     // sep now contains last pipe symbol position prior to any regex symbols
180     label = link.substring(0, sep);
181     if (label.indexOf("|") > -1)
182     {
183       // | terminated database name / www target at start of Label
184       target = label.substring(0, label.indexOf("|"));
185     }
186     else if (label.indexOf(" ") > 2)
187     {
188       // space separated Label - matches database name
189       target = label.substring(0, label.indexOf(" "));
190     }
191     else
192     {
193       target = label;
194     }
195     // Now Parse URL : Whole URL string first
196     url_prefix = link.substring(sep + 1, ptok[0]);
197     url_suffix = new String[mtch.length];
198     regexReplace = new String[mtch.length];
199     // and loop through tokens
200     for (int pass = 0; pass < mtch.length; pass++)
201     {
202       int mlength = 3 + mtch[pass].length();
203       if (link.indexOf("$" + mtch[pass] + "=/") == ptok[pass]
204               && (p = link.indexOf("/=$", ptok[pass] + mlength)) > ptok[pass]
205                       + mlength)
206       {
207         // Extract Regex and suffix
208         if (ptok[pass + 1] < p + 3)
209         {
210           // tokens are not allowed inside other tokens - e.g. inserting a
211           // $sequences$ into the regex match for the sequenceid
212           invalidMessage = "Token regexes cannot contain other regexes (did you terminate the $"
213                   + mtch[pass] + " regex with a '/=$' ?";
214           return;
215         }
216         url_suffix[pass] = link.substring(p + 3, ptok[pass + 1]);
217         regexReplace[pass] = link.substring(ptok[pass] + mlength, p);
218         try
219         {
220           com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
221                   + regexReplace[pass] + "/");
222           if (rg == null)
223           {
224             invalidMessage = "Invalid Regular Expression : '"
225                     + regexReplace[pass] + "'\n";
226           }
227         } catch (Exception e)
228         {
229           invalidMessage = "Invalid Regular Expression : '"
230                   + regexReplace[pass] + "'\n";
231         }
232       }
233       else
234       {
235         regexReplace[pass] = null;
236         // verify format is really correct.
237         if ((p = link.indexOf("$" + mtch[pass] + "$")) == ptok[pass])
238         {
239           url_suffix[pass] = link.substring(p + mtch[pass].length() + 2,
240                   ptok[pass + 1]);
241         }
242         else
243         {
244           invalidMessage = "Warning: invalid regex structure (after '"
245                   + mtch[0] + "') for URL link : " + link;
246         }
247       }
248     }
249     int pass = 0;
250     separators = new String[url_suffix.length];
251     String suffices = url_suffix[url_suffix.length - 1], lastsep = ",";
252     // have a look in the last suffix for any more separators.
253     while ((p = suffices.indexOf('|')) > -1)
254     {
255       separators[pass] = suffices.substring(p + 1);
256       if (pass == 0)
257       {
258         // trim the original suffix string
259         url_suffix[url_suffix.length - 1] = suffices.substring(0, p);
260       }
261       else
262       {
263         lastsep = (separators[pass - 1] = separators[pass - 1].substring(0,
264                 p));
265       }
266       suffices = separators[pass];
267       pass++;
268     }
269     if (pass > 0)
270     {
271       lastsep = separators[pass - 1];
272     }
273     // last separator is always used for all the remaining separators
274     while (pass < separators.length)
275     {
276       separators[pass++] = lastsep;
277     }
278   }
279
280   /**
281    * @return the url_suffix
282    */
283   public String getUrl_suffix()
284   {
285     return url_suffix[url_suffix.length - 1];
286   }
287
288   /**
289    * @return the url_prefix
290    */
291   public String getUrl_prefix()
292   {
293     return url_prefix;
294   }
295
296   /**
297    * @return the target
298    */
299   public String getTarget()
300   {
301     return target;
302   }
303
304   /**
305    * @return the label
306    */
307   public String getLabel()
308   {
309     return label;
310   }
311
312   /**
313    * @return the sequence ID regexReplace
314    */
315   public String getIDRegexReplace()
316   {
317     return _replaceFor(tokens[0]);
318   }
319
320   private String _replaceFor(String token)
321   {
322     for (int i = 0; i < mtch.length; i++)
323       if (segs[i] > -1 && mtch[i].equals(token))
324       {
325         return regexReplace[i];
326       }
327     return null;
328   }
329
330   /**
331    * @return the sequence ID regexReplace
332    */
333   public String getSeqRegexReplace()
334   {
335     return _replaceFor(tokens[1]);
336   }
337
338   /**
339    * @return the invalidMessage
340    */
341   public String getInvalidMessage()
342   {
343     return invalidMessage;
344   }
345
346   /**
347    * Check if URL string was parsed properly.
348    * 
349    * @return boolean - if false then <code>getInvalidMessage</code> returns an
350    *         error message
351    */
352   public boolean isValid()
353   {
354     return invalidMessage == null;
355   }
356
357   /**
358    * return one or more URL strings by applying regex to the given idstring
359    * 
360    * @param idstrings
361    *          array of id strings to pass to service
362    * @param seqstrings
363    *          array of seq strings to pass to service
364    * @param onlyIfMatches
365    *          - when true url strings are only made if regex is defined and
366    *          matches for all qualified tokens in groupURL - TODO: consider if
367    *          onlyIfMatches is really a useful parameter!
368    * @return null or Object[] { int[] { number of seqs substituted},boolean[] {
369    *         which seqs were substituted }, StringBuffer[] { substituted lists
370    *         for each token }, String[] { url } }
371    * @throws UrlStringTooLongException
372    */
373   public Object[] makeUrls(String[] idstrings, String[] seqstrings,
374           String dsstring, boolean onlyIfMatches)
375           throws UrlStringTooLongException
376   {
377     Hashtable rstrings = replacementArgs(idstrings, seqstrings, dsstring);
378     return makeUrls(rstrings, onlyIfMatches);
379   }
380
381   /**
382    * gathers input into a hashtable
383    * 
384    * @param idstrings
385    * @param seqstrings
386    * @param dsstring
387    * @return
388    */
389   private Hashtable replacementArgs(String[] idstrings,
390           String[] seqstrings, String dsstring)
391   {
392     Hashtable rstrings = new Hashtable();
393     rstrings.put(tokens[0], idstrings);
394     rstrings.put(tokens[1], seqstrings);
395     rstrings.put(tokens[2], new String[]
396     { dsstring });
397     if (idstrings.length != seqstrings.length)
398     {
399       throw new Error(MessageManager.getString("error.idstring_seqstrings_only_one_per_sequence"));
400     }
401     return rstrings;
402   }
403
404   public Object[] makeUrls(Hashtable repstrings, boolean onlyIfMatches)
405           throws UrlStringTooLongException
406   {
407     return makeUrlsIf(true, repstrings, onlyIfMatches);
408   }
409
410   /**
411    * 
412    * @param ids
413    * @param seqstr
414    * @param string
415    * @param b
416    * @return URL stub objects ready to pass to constructFrom
417    * @throws UrlStringTooLongException
418    */
419   public Object[] makeUrlStubs(String[] ids, String[] seqstr,
420           String string, boolean b) throws UrlStringTooLongException
421   {
422     Hashtable rstrings = replacementArgs(ids, seqstr, string);
423     Object[] stubs = makeUrlsIf(false, rstrings, b);
424     if (stubs != null)
425     {
426       return new Object[]
427       { stubs[0], stubs[1], rstrings, new boolean[]
428       { b } };
429     }
430     // TODO Auto-generated method stub
431     return null;
432   }
433
434   /**
435    * generate the URL for the given URL stub object array returned from
436    * makeUrlStubs
437    * 
438    * @param stubs
439    * @return URL string.
440    * @throws UrlStringTooLongException
441    */
442   public String constructFrom(Object[] stubs)
443           throws UrlStringTooLongException
444   {
445     Object[] results = makeUrlsIf(true, (Hashtable) stubs[2],
446             ((boolean[]) stubs[3])[0]);
447     return ((String[]) results[3])[0];
448   }
449
450   /**
451    * conditionally generate urls or stubs for a given input.
452    * 
453    * @param createFullUrl
454    *          set to false if you only want to test if URLs would be generated.
455    * @param repstrings
456    * @param onlyIfMatches
457    * @return null if no url is generated. Object[] { int[] { number of matches
458    *         seqs }, boolean[] { which matched }, (if createFullUrl also has
459    *         StringBuffer[] { segment generated from inputs that is used in URL
460    *         }, String[] { url })}
461    * @throws UrlStringTooLongException
462    */
463   protected Object[] makeUrlsIf(boolean createFullUrl,
464           Hashtable repstrings, boolean onlyIfMatches)
465           throws UrlStringTooLongException
466   {
467     int pass = 0;
468
469     // prepare string arrays in correct order to be assembled into URL input
470     String[][] idseq = new String[mtch.length][]; // indexed by pass
471     int mins = 0, maxs = 0; // allowed two values, 1 or n-sequences.
472     for (int i = 0; i < mtch.length; i++)
473     {
474       idseq[i] = (String[]) repstrings.get(mtch[i]);
475       if (idseq[i].length >= 1)
476       {
477         if (mins == 0 && idseq[i].length == 1)
478         {
479           mins = 1;
480         }
481         if (maxs < 2)
482         {
483           maxs = idseq[i].length;
484         }
485         else
486         {
487           if (maxs != idseq[i].length)
488           {
489             throw new Error(MessageManager.formatMessage("error.cannot_have_mixed_length_replacement_vectors",
490                                 new String[]{(mtch[i]), Integer.valueOf(idseq[i].length).toString(),Integer.valueOf(maxs).toString()}));
491           }
492         }
493       }
494       else
495       {
496         throw new Error(MessageManager.getString("error.cannot_have_zero_length_vector_replacement_strings"));
497       }
498     }
499     // iterate through input, collating segments to be inserted into url
500     StringBuffer matched[] = new StringBuffer[idseq.length];
501     // and precompile regexes
502     com.stevesoft.pat.Regex[] rgxs = new com.stevesoft.pat.Regex[matched.length];
503     for (pass = 0; pass < matched.length; pass++)
504     {
505       matched[pass] = new StringBuffer();
506       if (regexReplace[pass] != null)
507       {
508         rgxs[pass] = com.stevesoft.pat.Regex.perlCode("/"
509                 + regexReplace[pass] + "/");
510       }
511       else
512       {
513         rgxs[pass] = null;
514       }
515     }
516     // tot up the invariant lengths for this url
517     int urllength = url_prefix.length();
518     for (pass = 0; pass < matched.length; pass++)
519     {
520       urllength += url_suffix[pass].length();
521     }
522
523     // flags to record which of the input sequences were actually used to
524     // generate the
525     // url
526     boolean[] thismatched = new boolean[maxs];
527     int seqsmatched = 0;
528     for (int sq = 0; sq < maxs; sq++)
529     {
530       // initialise flag for match
531       thismatched[sq] = false;
532       StringBuffer[] thematches = new StringBuffer[rgxs.length];
533       for (pass = 0; pass < rgxs.length; pass++)
534       {
535         thematches[pass] = new StringBuffer(); // initialise - in case there are
536                                                // no more
537         // matches.
538         // if a regex is provided, then it must match for all sequences in all
539         // tokens for it to be considered.
540         if (idseq[pass].length <= sq)
541         {
542           // no more replacement strings to try for this token
543           continue;
544         }
545         if (rgxs[pass] != null)
546         {
547           com.stevesoft.pat.Regex rg = rgxs[pass];
548           int rematchat = 0;
549           // concatenate all matches of re in the given string!
550           while (rg.searchFrom(idseq[pass][sq], rematchat))
551           {
552             rematchat = rg.matchedTo();
553             thismatched[sq] |= true;
554             urllength += rg.charsMatched(); // count length
555             if ((urllength + 32) > Platform.getMaxCommandLineLength())
556             {
557               throw new UrlStringTooLongException(urllength);
558             }
559
560             if (!createFullUrl)
561             {
562               continue; // don't bother making the URL replacement text.
563             }
564             // do we take the cartesian products of the substituents ?
565             int ns = rg.numSubs();
566             if (ns == 0)
567             {
568               thematches[pass].append(rg.stringMatched());// take whole regex
569             }
570             /*
571              * else if (ns==1) { // take only subgroup match return new String[]
572              * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
573              * }; }
574              */
575             // deal with multiple submatch case - for moment we do the simplest
576             // - concatenate the matched regions, instead of creating a complete
577             // list for each alternate match over all sequences.
578             // TODO: specify a 'replace pattern' - next refinement
579             else
580             {
581               // debug
582               /*
583                * for (int s = 0; s <= rg.numSubs(); s++) {
584                * System.err.println("Sub " + s + " : " + rg.matchedFrom(s) +
585                * " : " + rg.matchedTo(s) + " : '" + rg.stringMatched(s) + "'");
586                * }
587                */
588               // try to collate subgroup matches
589               StringBuffer subs = new StringBuffer();
590               // have to loop through submatches, collating them at top level
591               // match
592               int s = 0; // 1;
593               while (s <= ns)
594               {
595                 if (s + 1 <= ns && rg.matchedTo(s) > -1
596                         && rg.matchedTo(s + 1) > -1
597                         && rg.matchedTo(s + 1) < rg.matchedTo(s))
598                 {
599                   // s is top level submatch. search for submatches enclosed by
600                   // this one
601                   int r = s + 1;
602                   StringBuffer rmtch = new StringBuffer();
603                   while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
604                   {
605                     if (rg.matchedFrom(r) > -1)
606                     {
607                       rmtch.append(rg.stringMatched(r));
608                     }
609                     r++;
610                   }
611                   if (rmtch.length() > 0)
612                   {
613                     subs.append(rmtch); // simply concatenate
614                   }
615                   s = r;
616                 }
617                 else
618                 {
619                   if (rg.matchedFrom(s) > -1)
620                   {
621                     subs.append(rg.stringMatched(s)); // concatenate
622                   }
623                   s++;
624                 }
625               }
626               thematches[pass].append(subs);
627             }
628           }
629         }
630         else
631         {
632           // are we only supposed to take regex matches ?
633           if (!onlyIfMatches)
634           {
635             thismatched[sq] |= true;
636             urllength += idseq[pass][sq].length(); // tot up length
637             if (createFullUrl)
638             {
639               thematches[pass] = new StringBuffer(idseq[pass][sq]); // take
640                                                                     // whole
641                                                                     // string -
642               // regardless - probably not a
643               // good idea!
644               /*
645                * TODO: do some boilerplate trimming of the fields to make them
646                * sensible e.g. trim off any 'prefix' in the id string (see
647                * UrlLink for the below) - pre 2.4 Jalview behaviour if
648                * (idstring.indexOf("|") > -1) { idstring =
649                * idstring.substring(idstring.lastIndexOf("|") + 1); }
650                */
651             }
652
653           }
654         }
655       }
656
657       // check if we are going to add this sequence's results ? all token
658       // replacements must be valid for this to happen!
659       // (including single value replacements - eg. dataset name)
660       if (thismatched[sq])
661       {
662         if (createFullUrl)
663         {
664           for (pass = 0; pass < matched.length; pass++)
665           {
666             if (idseq[pass].length > 1 && matched[pass].length() > 0)
667             {
668               matched[pass].append(separators[pass]);
669             }
670             matched[pass].append(thematches[pass]);
671           }
672         }
673         seqsmatched++;
674       }
675     }
676     // finally, if any sequences matched, then form the URL and return
677     if (seqsmatched == 0 || (createFullUrl && matched[0].length() == 0))
678     {
679       // no matches - no url generated
680       return null;
681     }
682     // check if we are beyond the feasible command line string limit for this
683     // platform
684     if ((urllength + 32) > Platform.getMaxCommandLineLength())
685     {
686       throw new UrlStringTooLongException(urllength);
687     }
688     if (!createFullUrl)
689     {
690       // just return the essential info about what the URL would be generated
691       // from
692       return new Object[]
693       { new int[]
694       { seqsmatched }, thismatched };
695     }
696     // otherwise, create the URL completely.
697
698     StringBuffer submiturl = new StringBuffer();
699     submiturl.append(url_prefix);
700     for (pass = 0; pass < matched.length; pass++)
701     {
702       submiturl.append(matched[pass]);
703       if (url_suffix[pass] != null)
704       {
705         submiturl.append(url_suffix[pass]);
706       }
707     }
708
709     return new Object[]
710     { new int[]
711     { seqsmatched }, thismatched, matched, new String[]
712     { submiturl.toString() } };
713   }
714
715   /**
716    * 
717    * @param urlstub
718    * @return number of distinct sequence (id or seuqence) replacements predicted
719    *         for this stub
720    */
721   public int getNumberInvolved(Object[] urlstub)
722   {
723     return ((int[]) urlstub[0])[0]; // returns seqsmatched from
724                                     // makeUrlsIf(false,...)
725   }
726
727   /**
728    * get token types present in this url as a bitfield indicating presence of
729    * each token from tokens (LSB->MSB).
730    * 
731    * @return groupURL class as integer
732    */
733   public int getGroupURLType()
734   {
735     int r = 0;
736     for (int pass = 0; pass < tokens.length; pass++)
737     {
738       for (int i = 0; i < mtch.length; i++)
739       {
740         if (mtch[i].equals(tokens[pass]))
741         {
742           r += 1 << pass;
743         }
744       }
745     }
746     return r;
747   }
748
749   public String toString()
750   {
751     StringBuffer result = new StringBuffer();
752     result.append(label + "|" + url_prefix);
753     int r;
754     for (r = 0; r < url_suffix.length; r++)
755     {
756       result.append("$");
757       result.append(mtch[r]);
758       if (regexReplace[r] != null)
759       {
760         result.append("=/");
761         result.append(regexReplace[r]);
762         result.append("/=");
763       }
764       result.append("$");
765       result.append(url_suffix[r]);
766     }
767     for (r = 0; r < separators.length; r++)
768     {
769       result.append("|");
770       result.append(separators[r]);
771     }
772     return result.toString();
773   }
774
775   /**
776    * report stats about the generated url string given an input set
777    * 
778    * @param ul
779    * @param idstring
780    * @param url
781    */
782   private static void testUrls(GroupUrlLink ul, String[][] idstring,
783           Object[] url)
784   {
785
786     if (url == null)
787     {
788       System.out.println("Created NO urls.");
789     }
790     else
791     {
792       System.out.println("Created a url from " + ((int[]) url[0])[0]
793               + "out of " + idstring[0].length + " sequences.");
794       System.out.println("Sequences that did not match:");
795       for (int sq = 0; sq < idstring[0].length; sq++)
796       {
797         if (!((boolean[]) url[1])[sq])
798         {
799           System.out.println("Seq " + sq + ": " + idstring[0][sq] + "\t: "
800                   + idstring[1][sq]);
801         }
802       }
803       System.out.println("Sequences that DID match:");
804       for (int sq = 0; sq < idstring[0].length; sq++)
805       {
806         if (((boolean[]) url[1])[sq])
807         {
808           System.out.println("Seq " + sq + ": " + idstring[0][sq] + "\t: "
809                   + idstring[1][sq]);
810         }
811       }
812       System.out.println("The generated URL:");
813       System.out.println(((String[]) url[3])[0]);
814     }
815   }
816
817   public static void main(String argv[])
818   {
819     // note - JAL-1383 - these services are all dead
820     String[] links = new String[]
821     {
822         "EnVision2|IDS|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=linkInDatasetFromJalview&input=$SEQUENCEIDS$&inputType=0|,",
823         "EnVision2|Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=linkInDatasetFromJalview&input=$SEQUENCES$&inputType=1|,",
824         "EnVision2|IDS|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,",
825         "EnVision2|Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=Enfin%20Default%20Workflow&datasetName=$DATASETID$&input=$SEQUENCES$&inputType=1|,",
826         "EnVision2|IDS|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=$SEQUENCEIDS$&datasetName=linkInDatasetFromJalview&input=$SEQUENCEIDS$&inputType=0|,",
827         "EnVision2|Seqs|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?workflow=$SEQUENCEIDS$&datasetName=$DATASETID$&input=$SEQUENCES$&inputType=1|,",
828         "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|,",
829         "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|,"
830     /*
831      * http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?input=P38389,P38398
832      * &inputType=0&workflow=Enfin%20Default%20Workflow&datasetName=
833      * linkInDatasetFromPRIDE
834      */
835     };
836
837     SequenceI[] seqs = new SequenceI[]
838     { new Sequence("StupidLabel:gi|9234|pdb|102L|A",
839             "asdiasdpasdpadpwpadasdpaspdw"), };
840     String[][] seqsandids = formStrings(seqs);
841     for (int i = 0; i < links.length; i++)
842     {
843       GroupUrlLink ul = new GroupUrlLink(links[i]);
844       if (ul.isValid())
845       {
846         System.out.println("\n\n\n");
847         System.out.println("Link " + i + " " + links[i] + " : "
848                 + ul.toString());
849         System.out.println(" pref : " + ul.getUrl_prefix());
850         System.out.println(" IdReplace : " + ul.getIDRegexReplace());
851         System.out.println(" SeqReplace : " + ul.getSeqRegexReplace());
852         System.out.println(" Suffixes : " + ul.getUrl_suffix());
853
854         System.out
855                 .println("<insert input id and sequence strings here> Without onlyIfMatches:");
856         Object[] urls;
857         try
858         {
859           urls = ul.makeUrls(seqsandids[0], seqsandids[1], "mydataset",
860                   false);
861           testUrls(ul, seqsandids, urls);
862         } catch (UrlStringTooLongException ex)
863         {
864           System.out.println("too long exception " + ex);
865         }
866         System.out
867                 .println("<insert input id and sequence strings here> With onlyIfMatches set:");
868         try
869         {
870           urls = ul.makeUrls(seqsandids[0], seqsandids[1], "mydataset",
871                   true);
872           testUrls(ul, seqsandids, urls);
873         } catch (UrlStringTooLongException ex)
874         {
875           System.out.println("too long exception " + ex);
876         }
877       }
878       else
879       {
880         System.err.println("Invalid URLLink : " + links[i] + " : "
881                 + ul.getInvalidMessage());
882       }
883     }
884   }
885
886   /**
887    * covenience method to generate the id and sequence string vector from a set
888    * of seuqences using each sequence's getName() and getSequenceAsString()
889    * method
890    * 
891    * @param seqs
892    * @return String[][] {{sequence ids},{sequence strings}}
893    */
894   public static String[][] formStrings(SequenceI[] seqs)
895   {
896     String[][] idset = new String[2][seqs.length];
897     for (int i = 0; i < seqs.length; i++)
898     {
899       idset[0][i] = seqs[i].getName();
900       idset[1][i] = seqs[i].getSequenceAsString();
901     }
902     return idset;
903   }
904
905   public void setLabel(String newlabel)
906   {
907     this.label = newlabel;
908   }
909
910 }