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