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