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