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