JAL-1503 update version in GPL header
[jalview.git] / src / jalview / ws / EnfinEnvision2OneWay.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws;
20
21 import jalview.bin.Cache;
22 import jalview.datamodel.DBRefEntry;
23 import jalview.datamodel.SequenceGroup;
24 import jalview.datamodel.SequenceI;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.Desktop;
27 import jalview.gui.JvSwingUtils;
28 import jalview.util.GroupUrlLink;
29 import jalview.util.GroupUrlLink.UrlStringTooLongException;
30
31 import java.awt.Component;
32 import java.awt.Cursor;
33 import java.awt.event.ActionEvent;
34 import java.io.UnsupportedEncodingException;
35 import java.net.URL;
36 import java.net.URLEncoder;
37 import java.util.Hashtable;
38 import java.util.Map;
39 import java.util.Vector;
40 import java.util.regex.Pattern;
41
42 import javax.swing.JMenu;
43 import javax.swing.JMenuItem;
44 import javax.swing.JOptionPane;
45 import javax.swing.event.MenuEvent;
46 import javax.swing.event.MenuListener;
47 import javax.xml.parsers.SAXParser;
48 import javax.xml.parsers.SAXParserFactory;
49
50 import org.xml.sax.Attributes;
51 import org.xml.sax.SAXException;
52 import org.xml.sax.helpers.DefaultHandler;
53
54 import com.lowagie.text.html.HtmlEncoder;
55
56 /**
57  * Lightweight runnable to discover dynamic 'one way' group URL services
58  * 
59  * as of Jalview 2.8.1 this class is mothballed and will be dropped in v3.
60  * @author JimP
61  * @deprecated
62  * 
63  */
64 public class EnfinEnvision2OneWay extends DefaultHandler implements
65         Runnable, WSMenuEntryProviderI
66 {
67   private static EnfinEnvision2OneWay groupURLLinksGatherer = null;
68
69   public static EnfinEnvision2OneWay getInstance()
70   {
71     if (groupURLLinksGatherer == null)
72     {
73       groupURLLinksGatherer = new EnfinEnvision2OneWay();
74     }
75     return groupURLLinksGatherer;
76   }
77
78   private void waitForCompletion()
79   {
80     if (groupURLLinksGatherer.isRunning())
81     {
82       // wait around and show a visual delay indicator
83       Cursor oldCursor = Desktop.instance.getCursor();
84       Desktop.instance.setCursor(new Cursor(Cursor.WAIT_CURSOR));
85       while (groupURLLinksGatherer.isRunning())
86       {
87         try
88         {
89           Thread.sleep(100);
90         } catch (InterruptedException e)
91         {
92         }
93         ;
94       }
95       Desktop.instance.setCursor(oldCursor);
96     }
97   }
98
99   public Vector getEnvisionServiceGroupURLS()
100   {
101     waitForCompletion();
102     return groupURLLinks;
103   }
104
105   /**
106    * indicate if
107    */
108   private static String BACKGROUND = "BACKGROUNDPARAM";
109
110   /**
111    * contains null strings or one of the above constants - indicate if this URL
112    * is a special case.
113    */
114   private Vector additionalPar = new Vector();
115
116   /**
117    * the enfin service URL
118    */
119   private String enfinService = null;
120
121   private String description = null;
122
123   private String wfname;
124
125   /*
126    * (non-Javadoc)
127    * 
128    * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
129    * java.lang.String, java.lang.String)
130    */
131   public void endElement(String uri, String localName, String qName)
132           throws SAXException
133   {
134
135     // System.err.println("End element: : '"+uri+" "+localName+" "+qName);
136     if (qName.equalsIgnoreCase("workflow") && description != null
137             && description.length() > 0)
138     {
139       // groupURLLinks.addElement("UNIPROT|EnVision2|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow=Default&datasetName=JalviewIDs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,");
140       // groupURLLinks.addElement("Seqs|EnVision2|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow=Default&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,");
141       System.err.println("Adding entry for " + wfname + " " + description);
142       if (wfname.toLowerCase().indexOf("funcnet") == -1)
143       {
144         description = Pattern.compile("\\s+", Pattern.MULTILINE)
145                 .matcher(description).replaceAll(" ");
146         groupURLdescr.addElement(description);
147         groupURLdescr.addElement(description);
148         String urlstub = wfname;
149         if (wfname.indexOf(" ") > -1)
150         {
151           // make the name safe!
152           try
153           {
154             urlstub = URLEncoder.encode(wfname, "utf-8");
155           } catch (UnsupportedEncodingException e)
156           {
157             // TODO Auto-generated catch block
158             e.printStackTrace();
159           }
160         }
161         groupURLLinks
162                 .addElement(wfname
163                         + "|"
164                         + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
165                         + urlstub
166                         + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,"); // #"+description+"#");
167         groupURLLinks
168                 .addElement(wfname
169                         + "|"
170                         + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
171                         + urlstub
172                         + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,"); // #"+description+"#");
173       }
174     }
175   }
176
177   /*
178    * (non-Javadoc)
179    * 
180    * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
181    */
182   public void characters(char[] ch, int start, int length)
183           throws SAXException
184   {
185     if (description != null)
186     {
187       for (int i = start; i < start + length; i++)
188       {
189         description += ch[i];
190       }
191     }
192   }
193
194   /*
195    * (non-Javadoc)
196    * 
197    * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
198    * java.lang.String, java.lang.String, org.xml.sax.Attributes)
199    */
200   public void startElement(String uri, String localName, String qName,
201           Attributes attributes) throws SAXException
202   {
203     if (qName.equalsIgnoreCase("workflow"))
204     {
205       description = null;
206       wfname = attributes.getValue("name");
207     }
208     if (qName.equalsIgnoreCase("description"))
209     {
210       description = "";
211     }
212
213     // System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
214     // super.startElement(uri,localName,qname,attributes);
215   }
216
217   private boolean started = false;
218
219   private boolean running = false;
220
221   private Vector groupURLLinks = null;
222
223   private Vector groupURLdescr = null;
224
225   private static String[] allowedDb = new String[]
226   { "UNIPROT", "EMBL", "PDB" };
227
228   public EnfinEnvision2OneWay()
229   {
230     groupURLLinks = new Vector();
231     groupURLdescr = new Vector();
232
233     enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE",
234             "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
235     new Thread(this).start();
236   }
237
238   public void run()
239   {
240     started = true;
241     running = true;
242     try
243     {
244       SAXParserFactory spf = SAXParserFactory.newInstance();
245       SAXParser sp = spf.newSAXParser();
246       sp.parse(new URL(enfinService).openStream(), this);
247     } catch (Exception e)
248     {
249       Cache.log.warn("Exception when discovering One Way services: ", e);
250     } catch (Error e)
251     {
252       Cache.log.warn("Error when discovering One Way services: ", e);
253     }
254     running = false;
255     Cache.log.debug("Finished running.");
256   }
257
258   /**
259    * have we finished running yet ?
260    * 
261    * @return false if we have been run.
262    */
263   public boolean isRunning()
264   {
265
266     // TODO Auto-generated method stub
267     return !started || running;
268   }
269
270   public static void main(String[] args)
271   {
272     Cache.initLogger();
273     EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
274     while (ow.isRunning())
275     {
276       try
277       {
278         Thread.sleep(50);
279       } catch (Exception e)
280       {
281       }
282       ;
283
284     }
285     for (int i = 0; i < ow.groupURLLinks.size(); i++)
286     {
287       System.err.println("Description" + ow.groupURLdescr.elementAt(i)
288               + "Service URL: " + ow.groupURLLinks.elementAt(i));
289     }
290   }
291
292   // / Copied from jalview.gui.PopupMenu
293   /**
294    * add a late bound URL service item to the given menu
295    * 
296    * @param linkMenu
297    * @param label
298    *          - menu label string
299    * @param urlgenerator
300    *          GroupURLLink used to generate URL
301    * @param urlstub
302    *          Object array returned from the makeUrlStubs function.
303    */
304   private void addshowLink(JMenu linkMenu, String label, String descr,
305           String dbname, final GroupUrlLink urlgenerator,
306           final Object[] urlstub)
307   {
308     Component[] jmi = linkMenu.getMenuComponents();
309     for (int i = 0; i < jmi.length; i++)
310     {
311       if (jmi[i] instanceof JMenuItem
312               && ((JMenuItem) jmi[i]).getText().equalsIgnoreCase(label))
313       {
314         // don't add this - its a repeat of an existing URL.
315         return;
316       }
317     }
318     try
319     {
320       descr = HtmlEncoder.encode(descr);
321     } catch (Exception e)
322     {
323     }
324     ;
325
326     boolean seqsorids = (urlgenerator.getGroupURLType() & urlgenerator.SEQUENCEIDS) == 0;
327     int i = urlgenerator.getNumberInvolved(urlstub);
328     JMenuItem item = new JMenuItem(label);
329     //
330     if (dbname == null || dbname.trim().length() == 0)
331     {
332       dbname = "";
333     }
334     item.setToolTipText("<html>"
335             + JvSwingUtils.wrapTooltip("Submit " + i + " " + dbname + " "
336                     + (seqsorids ? "sequence" : "sequence id")
337                     + (i > 1 ? "s" : "")
338
339                     + " to<br/>" + descr) + "</html>");
340     item.addActionListener(new java.awt.event.ActionListener()
341     {
342       public void actionPerformed(ActionEvent e)
343       {
344         new Thread(new Runnable()
345         {
346
347           public void run()
348           {
349             try
350             {
351               showLink(urlgenerator.constructFrom(urlstub));
352             } catch (UrlStringTooLongException ex)
353             {
354               Cache.log.warn("Not showing link: URL is too long!", ex);
355             }
356           }
357
358         }).start();
359       }
360     });
361
362     linkMenu.add(item);
363   }
364
365   /**
366    * open the given link in a new browser window
367    * 
368    * @param url
369    */
370   public void showLink(String url)
371   {
372     try
373     {
374       jalview.util.BrowserLauncher.openURL(url);
375     } catch (Exception ex)
376     {
377       JOptionPane
378               .showInternalMessageDialog(
379                       Desktop.desktop,
380                       "Unixers: Couldn't find default web browser."
381                               + "\nAdd the full path to your browser in Preferences.",
382                       "Web browser not found", JOptionPane.WARNING_MESSAGE);
383
384       ex.printStackTrace();
385     }
386   }
387
388   /**
389    * called by a web service menu instance when it is opened.
390    * 
391    * @param enfinServiceMenu
392    * @param alignFrame
393    */
394   private void buildGroupLinkMenu(JMenu enfinServiceMenu,
395           AlignFrame alignFrame)
396   {
397     if (running || !started)
398     {
399       return;
400     }
401     SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
402     SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
403     if (sg == null)
404     {
405       // consider visible regions here/
406     }
407     enfinServiceMenu.removeAll();
408     JMenu entries = buildGroupURLMenu(seqs, sg);
409     if (entries != null)
410     {
411       for (int i = 0, iSize = entries.getMenuComponentCount(); i < iSize; i++)
412       {
413         // transfer - menu component is removed from entries automatically
414         enfinServiceMenu.add(entries.getMenuComponent(0));
415       }
416       // entries.removeAll();
417       enfinServiceMenu.setEnabled(true);
418     }
419     else
420     {
421       enfinServiceMenu.setEnabled(false);
422     }
423   }
424
425   /**
426    * construct a dynamic enfin services menu given a sequence selection
427    * 
428    * @param seqs
429    * @param sg
430    * @param groupLinks
431    * @return
432    */
433   private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
434   {
435     if (groupURLdescr == null || groupURLLinks == null)
436       return null;
437     // TODO: usability: thread off the generation of group url content so root
438     // menu appears asap
439     // sequence only URLs
440     // ID/regex match URLs
441     JMenu groupLinksMenu = new JMenu("Group Link");
442     String[][] idandseqs = GroupUrlLink.formStrings(seqs);
443     Hashtable commonDbrefs = new Hashtable();
444     for (int sq = 0; sq < seqs.length; sq++)
445     {
446
447       int start, end;
448       if (sg != null)
449       {
450         start = seqs[sq].findPosition(sg.getStartRes());
451         end = seqs[sq].findPosition(sg.getEndRes());
452       }
453       else
454       {
455         // get total width of alignment.
456         start = seqs[sq].getStart();
457         end = seqs[sq].findPosition(seqs[sq].getLength());
458       }
459       // we skip sequences which do not have any non-gaps in the region of
460       // interest
461       if (start > end)
462       {
463         continue;
464       }
465       // just collect ids from dataset sequence
466       // TODO: check if IDs collected from selecton group intersects with the
467       // current selection, too
468       SequenceI sqi = seqs[sq];
469       while (sqi.getDatasetSequence() != null)
470       {
471         sqi = sqi.getDatasetSequence();
472       }
473       DBRefEntry[] dbr = sqi.getDBRef();
474       if (dbr != null && dbr.length > 0)
475       {
476         for (int d = 0; d < dbr.length; d++)
477         {
478           String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
479           Object[] sarray = (Object[]) commonDbrefs.get(src);
480           if (sarray == null)
481           {
482             sarray = new Object[2];
483             sarray[0] = new int[]
484             { 0 };
485             sarray[1] = new String[seqs.length];
486
487             commonDbrefs.put(src, sarray);
488           }
489
490           if (((String[]) sarray[1])[sq] == null)
491           {
492             if (!dbr[d].hasMap()
493                     || (dbr[d].getMap().locateMappedRange(start, end) != null))
494             {
495               ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
496               ((int[]) sarray[0])[0]++;
497             }
498           }
499         }
500       }
501     }
502     // now create group links for all distinct ID/sequence sets.
503     Hashtable<String, JMenu[]> gurlMenus = new Hashtable<String, JMenu[]>();
504     /**
505      * last number of sequences where URL generation failed
506      */
507     int[] nsqtype = new int[]
508     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
509     for (int i = 0; i < groupURLLinks.size(); i++)
510     {
511       String link = (String) groupURLLinks.elementAt(i);
512       String descr = (String) groupURLdescr.elementAt(i);
513
514       // boolean specialCase =
515       // additionalPar.elementAt(i).toString().equals(BACKGROUND);
516       GroupUrlLink urlLink = null;
517       try
518       {
519         urlLink = new GroupUrlLink(link);
520       } catch (Exception foo)
521       {
522         jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
523                 + "'", foo);
524         continue;
525       }
526       ;
527       if (!urlLink.isValid())
528       {
529         jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
530         continue;
531       }
532       final String label = urlLink.getLabel();
533       // create/recover the sub menus that might be populated for this link.
534       JMenu[] wflinkMenus = gurlMenus.get(label);
535       if (wflinkMenus == null)
536       {
537         // three types of url that might be
538         // created.
539         wflinkMenus = new JMenu[]
540         { null, new JMenu("IDS"), new JMenu("Sequences"),
541             new JMenu("IDS and Sequences") };
542         gurlMenus.put(label, wflinkMenus);
543       }
544
545       boolean usingNames = false;
546       // Now see which parts of the group apply for this URL
547       String ltarget;
548       String[] seqstr, ids; // input to makeUrl
549       for (int t = 0; t < allowedDb.length; t++)
550       {
551         ltarget = allowedDb[t]; // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
552         Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
553         if (idset != null)
554         {
555           int numinput = ((int[]) idset[0])[0];
556           String[] allids = ((String[]) idset[1]);
557           seqstr = new String[numinput];
558           ids = new String[numinput];
559           if (nsqtype[urlLink.getGroupURLType()] > 0
560                   && numinput >= nsqtype[urlLink.getGroupURLType()])
561           {
562             continue;
563           }
564           for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
565           {
566             if (allids[sq] != null)
567             {
568               ids[idcount] = allids[sq];
569               seqstr[idcount++] = idandseqs[1][sq];
570             }
571           }
572           try
573           {
574             createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
575                     descr, ids, seqstr);
576           } catch (UrlStringTooLongException ex)
577           {
578             nsqtype[urlLink.getGroupURLType()] = numinput;
579           }
580         }
581       }
582       // also do names only.
583       seqstr = idandseqs[1];
584       ids = idandseqs[0];
585       if (nsqtype[urlLink.getGroupURLType()] > 0
586               && idandseqs[0].length >= nsqtype[urlLink.getGroupURLType()])
587       {
588         continue;
589       }
590
591       try
592       {
593         createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
594                 ids, seqstr);
595       } catch (UrlStringTooLongException ex)
596       {
597         nsqtype[urlLink.getGroupURLType()] = idandseqs[0].length;
598       }
599     }
600     boolean anyadded = false; // indicates if there are any group links to give
601     // to user
602     for (Map.Entry<String, JMenu[]> menues : gurlMenus.entrySet())
603     {
604       JMenu grouplinkset = new JMenu(menues.getKey());
605       JMenu[] wflinkMenus = menues.getValue();
606       for (int m = 0; m < wflinkMenus.length; m++)
607       {
608         if (wflinkMenus[m] != null
609                 && wflinkMenus[m].getMenuComponentCount() > 0)
610         {
611           anyadded = true;
612           grouplinkset.add(wflinkMenus[m]);
613         }
614       }
615       groupLinksMenu.add(grouplinkset);
616     }
617     if (anyadded)
618     {
619       return groupLinksMenu;
620     }
621     return null;
622   }
623
624   private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
625           GroupUrlLink urlLink, String label, String ltarget, String descr,
626           String[] ids, String[] seqstr) throws UrlStringTooLongException
627   {
628     Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
629             + System.currentTimeMillis(), false);
630
631     if (urlset != null)
632     {
633       int type = urlLink.getGroupURLType() & 3;
634       // System.out.println(urlLink.getGroupURLType()
635       // +" "+((String[])urlset[3])[0]);
636       // first two bits ofurlLink type bitfield are sequenceids and sequences
637       // TODO: FUTURE: ensure the groupURL menu structure can be generalised
638       addshowLink(
639               linkMenus[type],
640               label
641                       + " "
642                       + (ltarget == null ? (((type & 1) == 1 ? "ID"
643                               : "Sequence") + (urlLink
644                               .getNumberInvolved(urlset) > 1 ? "s" : ""))
645                               : (usingNames ? (((type & 1) == 1) ? "(Names)"
646                                       : "")
647                                       : ("(" + ltarget + ")"))), descr,
648               usingNames ? null : label, urlLink, urlset);
649       return true;
650     }
651     return false;
652   }
653
654   // / end of stuff copied from popupmenu
655   public void attachWSMenuEntry(final JMenu wsmenu,
656           final AlignFrame alignFrame)
657   {
658     final JMenu enfinServiceMenu = new JMenu("Envision 2");
659     wsmenu.add(enfinServiceMenu);
660     enfinServiceMenu.setEnabled(false);
661     wsmenu.addMenuListener(new MenuListener()
662     {
663       // this listener remembers when the menu was first selected, and
664       // doesn't rebuild the session list until it has been cleared and
665       // reselected again.
666       boolean refresh = true;
667
668       public void menuCanceled(MenuEvent e)
669       {
670         refresh = true;
671       }
672
673       public void menuDeselected(MenuEvent e)
674       {
675         refresh = true;
676       }
677
678       public void menuSelected(MenuEvent e)
679       {
680         if (refresh && !isRunning())
681         {
682           new Thread(new Runnable()
683           {
684             public void run()
685             {
686               try
687               {
688                 buildGroupLinkMenu(enfinServiceMenu, alignFrame);
689               } catch (OutOfMemoryError ex)
690               {
691                 Cache.log
692                         .error("Out of memory when calculating the Envision2 links.",
693                                 ex);
694                 enfinServiceMenu.setEnabled(false);
695               }
696             }
697           }).start();
698           refresh = false;
699         }
700       }
701     });
702
703   }
704
705 }