refactored copy of groupURL mechanism into new Envision service discovery class.
[jalview.git] / src / jalview / ws / EnfinEnvision2OneWay.java
1 /**
2  * 
3  */
4 package jalview.ws;
5
6 import jalview.bin.Cache;
7 import jalview.datamodel.DBRefEntry;
8 import jalview.datamodel.SequenceGroup;
9 import jalview.datamodel.SequenceI;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.Desktop;
12 import jalview.util.GroupUrlLink;
13
14 import java.awt.Cursor;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.event.ItemEvent;
18 import java.awt.event.ItemListener;
19 import java.io.BufferedReader;
20 import java.io.InputStreamReader;
21 import java.net.URL;
22 import java.util.Hashtable;
23 import java.util.Vector;
24
25 import javax.swing.JMenu;
26 import javax.swing.JMenuItem;
27 import javax.swing.JOptionPane;
28 import javax.swing.event.MenuEvent;
29 import javax.swing.event.MenuListener;
30 import javax.xml.parsers.SAXParser;
31 import javax.xml.parsers.SAXParserFactory;
32
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
35 import org.xml.sax.helpers.DefaultHandler;
36
37 /**
38  * Lightweight runnable to discover dynamic 'one way' group URL services
39  * @author JimP
40  *
41  */
42 public class EnfinEnvision2OneWay extends DefaultHandler implements Runnable,WSMenuEntryProviderI 
43 {
44   private static EnfinEnvision2OneWay groupURLLinksGatherer=null;
45   public static EnfinEnvision2OneWay getInstance() {
46     if (groupURLLinksGatherer==null) {
47       groupURLLinksGatherer = new EnfinEnvision2OneWay();
48     }
49     return groupURLLinksGatherer;
50   }
51   private void waitForCompletion() {
52     if (groupURLLinksGatherer.isRunning())
53     {
54     // wait around and show a visual delay indicator  
55     Cursor oldCursor = Desktop.instance.getCursor();
56     Desktop.instance.setCursor(new Cursor(Cursor.WAIT_CURSOR));
57     while (groupURLLinksGatherer.isRunning())
58     {
59       try {
60         Thread.sleep(100);
61       } catch (InterruptedException e ){};
62     }
63     Desktop.instance.setCursor(oldCursor);
64     } 
65   }
66   public Vector getEnvisionServiceGroupURLS() {
67     waitForCompletion();
68     return groupURLLinks;
69   }
70   /**
71    * indicate if 
72    */
73   private static String BACKGROUND="BACKGROUNDPARAM";
74   /**
75    * contains null strings or one of the above constants - indicate if this URL is a special case.
76    */
77   private Vector additionalPar = new Vector();
78   /**
79    * the enfin service URL
80    */
81   private String enfinService=null;
82   private String description=null;
83   private String wfname;
84   /* (non-Javadoc)
85    * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
86    */
87   public void endElement(String uri, String localName, String qName)
88           throws SAXException
89   {
90     
91 //  System.err.println("End element: : '"+uri+" "+localName+" "+qName);
92     if (qName.equalsIgnoreCase("workflow") && description!=null && description.length()>0)
93     {
94       //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|,");
95       //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|,");
96       System.err.println("Adding entry for "+wfname+" "+description);
97       if (wfname.toLowerCase().indexOf("funcnet")==-1)
98       {
99         groupURLdescr.addElement(description);
100         groupURLdescr.addElement(description);
101         groupURLLinks.addElement(wfname+"|"+"http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="+wfname+"&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,"); // #"+description+"#");
102         groupURLLinks.addElement(wfname+"|"+"http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="+wfname+"&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,"); // #"+description+"#");
103       }
104     }
105   }
106
107   /* (non-Javadoc)
108    * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
109    */
110   public void characters(char[] ch, int start, int length)
111           throws SAXException
112   {
113     if (description!=null) {
114       for (int i=start; i<start+length; i++) {
115       description+=ch[i];
116       }
117     }
118   }
119
120   /* (non-Javadoc)
121    * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
122    */
123   public void startElement(String uri, String localName, String qName,
124           Attributes attributes) throws SAXException
125   {
126     if (qName.equalsIgnoreCase("workflow"))
127     {
128       description = null;
129       wfname = attributes.getValue("name");
130     }
131     if (qName.equalsIgnoreCase("description"))
132     {
133       description = "";
134     } 
135     
136     
137     //System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
138     // super.startElement(uri,localName,qname,attributes);
139   }
140
141   private boolean started=false;
142   private boolean running=false;
143   private Vector groupURLLinks = null;
144   private Vector groupURLdescr = null;
145   
146   public EnfinEnvision2OneWay() {
147     groupURLLinks = new Vector();
148     groupURLdescr = new Vector();
149     
150     enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE", "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
151     new Thread(this).start();
152   }
153
154   public void run()
155   {
156     started = true;
157     running = true;
158     try {
159       SAXParserFactory spf = SAXParserFactory.newInstance();
160       SAXParser sp = spf.newSAXParser();
161       sp.parse(new URL(enfinService).openStream(), this);      
162     } catch (Exception e)
163     {
164       Cache.log.warn("Exception when discovering One Way services: ",e);      
165     }
166     catch (Error e)
167     {
168       Cache.log.warn("Error when discovering One Way services: ",e);
169     }
170     running = false;
171     Cache.log.debug("Finished running.");
172   }
173
174   /**
175    * have we finished running yet ?
176    * @return false if we have been run.
177    */
178   public boolean isRunning()
179   {
180     
181     // TODO Auto-generated method stub
182     return !started || running;
183   }
184   public static void main(String[] args){
185     Cache.initLogger();
186     EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
187     while (ow.isRunning())
188     {
189       try {
190         Thread.sleep(50);
191       } catch (Exception e){};
192      
193     }
194     for (int i=0;i<ow.groupURLLinks.size();i++) {
195       System.err.println("Description"+ow.groupURLdescr.elementAt(i)+"Service URL: "+ow.groupURLLinks.elementAt(i));
196     }
197   }
198   /// Copied from jalview.gui.PopupMenu
199   /**
200    * add a late bound URL service item to the given menu
201    * 
202    * @param linkMenu
203    * @param label -
204    *                menu label string
205    * @param urlgenerator GroupURLLink used to generate URL  
206    * @param urlstub Object array returned from the makeUrlStubs function.
207    */
208   private void addshowLink(JMenu linkMenu, String label, String descr, final GroupUrlLink urlgenerator, final Object[] urlstub)
209   {
210     JMenuItem item = new JMenuItem(label);
211     item.setToolTipText("Submit ("+urlgenerator.getNumberInvolved(urlstub)+" seqs) to workflow: "+descr);
212     item.addActionListener(new java.awt.event.ActionListener()
213     {
214       public void actionPerformed(ActionEvent e)
215       {
216         new Thread(new Runnable()
217         {
218
219           public void run()
220           {
221             showLink(urlgenerator.constructFrom(urlstub));
222           }
223
224         }).start();
225       }
226     });
227
228     linkMenu.add(item);
229   }
230
231   /**
232    * open the given link in a new browser window
233    * @param url
234    */
235   public void showLink(String url)
236   {
237     try
238     {
239       jalview.util.BrowserLauncher.openURL(url);
240     } catch (Exception ex)
241     {
242       JOptionPane
243               .showInternalMessageDialog(
244                       Desktop.desktop,
245                       "Unixers: Couldn't find default web browser."
246                               + "\nAdd the full path to your browser in Preferences.",
247                       "Web browser not found", JOptionPane.WARNING_MESSAGE);
248
249       ex.printStackTrace();
250     }
251   }
252   /**
253    * called by a web service menu instance when it is opened.
254    * @param enfinServiceMenu
255    * @param alignFrame
256    */
257   private void buildGroupLinkMenu(JMenu enfinServiceMenu,
258           AlignFrame alignFrame)
259   {
260     SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
261     SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
262     if (sg==null) {
263       // consider visible regions here/
264     }
265     enfinServiceMenu.removeAll();
266     JMenu entries = buildGroupURLMenu(seqs,sg);
267     if (entries!=null)
268     {
269       for (int i=0,iSize=entries.getMenuComponentCount();i<iSize;i++)
270       {
271         // transfer - menu component is removed from entries automatically
272         enfinServiceMenu.add(entries.getMenuComponent(0));
273       }
274       // entries.removeAll();
275       enfinServiceMenu.setEnabled(true);
276     } else {
277       enfinServiceMenu.setEnabled(false);
278     }
279   }
280
281   /**
282    * construct a dynamic enfin services menu given a sequence selection
283    * @param seqs
284    * @param sg
285    * @param groupLinks
286    * @return
287    */
288   private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
289   {
290     
291     // TODO: usability: thread off the generation of group url content so root menu appears asap
292     // sequence only URLs
293     // ID/regex match URLs
294     JMenu groupLinksMenu = new JMenu("Group Link");
295     JMenu[] linkMenus = new JMenu[] { null, new JMenu("IDS"), new JMenu("Sequences"), new JMenu("IDS and Sequences")}; // three types of url that might be created.
296     String[][] idandseqs = GroupUrlLink.formStrings(seqs);
297     Hashtable commonDbrefs = new Hashtable();
298     for (int sq = 0; sq<seqs.length;sq++) {
299
300       int start,end;
301       if (sg!=null) {
302         start = seqs[sq].findPosition(sg.getStartRes());
303         end=seqs[sq].findPosition(sg.getEndRes()); 
304       } else {
305         // get total width of alignment.
306         start = seqs[sq].getStart();
307         end = seqs[sq].findPosition(seqs[sq].getLength());
308       }
309       // we skip sequences which do not have any non-gaps in the region of interest
310       if (start>end)
311       {
312         continue;
313       }
314       // just collect ids from dataset sequence
315       // TODO: check if IDs collected from selecton group intersects with the current selection, too
316       SequenceI sqi = seqs[sq];
317       while (sqi.getDatasetSequence()!=null) {
318         sqi = sqi.getDatasetSequence(); }
319       DBRefEntry[] dbr = sqi.getDBRef();
320       if (dbr!=null && dbr.length>0)
321       {
322         for (int d=0;d<dbr.length;d++)
323         {
324           String src =dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
325           Object[] sarray = (Object[]) commonDbrefs.get(src);
326           if (sarray==null)
327           {
328             sarray = new Object[2];
329             sarray[0] = new int[] { 0 };
330             sarray[1] = new String[seqs.length];
331             
332             commonDbrefs.put(src,sarray);
333           }
334           
335           if (((String[])sarray[1])[sq]==null) {
336             if (!dbr[d].hasMap() || (dbr[d].getMap().locateMappedRange(start, end)!=null)) {
337               ((String[])sarray[1])[sq] = dbr[d].getAccessionId();
338               ((int[])sarray[0])[0]++;
339             }
340           }
341         }
342       }
343     }
344     // now create group links for all distinct ID/sequence sets.
345     boolean addMenu = false; // indicates if there are any group links to give to user  
346     for (int i = 0; i < groupURLLinks.size(); i++) {
347         String link = groupURLLinks.elementAt(i).toString();
348         String descr = groupURLdescr.elementAt(i).toString();
349 //        boolean specialCase = additionalPar.elementAt(i).toString().equals(BACKGROUND);
350         GroupUrlLink urlLink = null;
351         try
352         {
353           urlLink = new GroupUrlLink(link);
354         } catch (Exception foo)
355         {
356           jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
357                   + "'", foo);
358           continue;
359         }
360         ;
361         if (!urlLink.isValid())
362         {
363           jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
364           continue;
365         }
366         final String label = urlLink.getLabel();
367         boolean usingNames = false;
368         // Now see which parts of the group apply for this URL
369         String ltarget = urlLink.getTarget(); // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
370         Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
371         String[] seqstr,ids; // input to makeUrl
372         if (idset!=null)
373         {
374           int numinput = ((int[])idset[0])[0];
375           String[] allids = ((String[])idset[1]);
376           seqstr = new String[numinput];
377           ids = new String[numinput];
378           for (int sq=0,idcount=0;sq<seqs.length;sq++)
379           {
380             if (allids[sq]!=null) {
381               ids[idcount] = allids[sq];
382               seqstr[idcount++] = idandseqs[1][sq];
383             }
384           }
385         } else {
386           // just use the id/seq set
387           seqstr = idandseqs[1];
388           ids = idandseqs[0];
389           usingNames=true;
390         }
391         // and try and make the groupURL!
392         
393         Object[] urlset = urlLink.makeUrlStubs(ids,seqstr, "FromJalview"+System.currentTimeMillis(),false);
394         if (urlset!=null)
395         {
396           int type = urlLink.getGroupURLType() & 3;
397           //System.out.println(urlLink.getGroupURLType() +" "+((String[])urlset[3])[0]);
398           // first two bits ofurlLink type bitfield are sequenceids and sequences
399           // TODO: FUTURE: ensure the groupURL menu structure can be generalised
400           addshowLink(linkMenus[type], label + (((type & 1)==1) ? ("("+(usingNames ? "Names" : ltarget)+")") : ""), descr, urlLink, urlset);
401           addMenu = true;
402         }
403       }
404     if (addMenu)
405     {
406       groupLinksMenu = new JMenu("Group Links");
407       for (int m=0;m<linkMenus.length; m++)
408       {
409         if (linkMenus[m]!=null && linkMenus[m].getMenuComponentCount()>0)
410         {
411           groupLinksMenu.add(linkMenus[m]);
412         }
413       }
414       
415       return groupLinksMenu;
416     }
417     return null;
418   }
419
420   /// end of stuff copied from popupmenu
421   public void attachWSMenuEntry(final JMenu wsmenu, final AlignFrame alignFrame)
422   {
423     final JMenu enfinServiceMenu = new JMenu("Envision 2");
424     wsmenu.add(enfinServiceMenu);
425     enfinServiceMenu.setEnabled(false);
426     wsmenu.addMenuListener(new MenuListener()
427       {
428         // this listener remembers when the menu was first selected, and
429         // doesn't rebuild the session list until it has been cleared and
430         // reselected again.
431         boolean refresh = true;
432
433         public void menuCanceled(MenuEvent e)
434         {
435           refresh = true;
436         }
437
438         public void menuDeselected(MenuEvent e)
439         {
440           refresh = true;
441         }
442
443         public void menuSelected(MenuEvent e)
444         {
445           if (refresh)
446           {
447             try {
448               buildGroupLinkMenu(enfinServiceMenu,alignFrame);
449             } catch (OutOfMemoryError ex)
450             {
451               Cache.log.error("Out of memory when calculating the Envision2 links.",ex);
452               enfinServiceMenu.setEnabled(false);
453             }
454             refresh = false;
455           }
456         }
457      });
458     
459     
460   }
461
462 }