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