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