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