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