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