reinstate submission of database Ids as well as sequence Id strings.
[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   private static String[] allowedDb=new String[] {"UNIPROT","EMBL","PDB"} ;
146   
147   public EnfinEnvision2OneWay() {
148     groupURLLinks = new Vector();
149     groupURLdescr = new Vector();
150     
151     enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE", "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
152     new Thread(this).start();
153   }
154
155   public void run()
156   {
157     started = true;
158     running = true;
159     try {
160       SAXParserFactory spf = SAXParserFactory.newInstance();
161       SAXParser sp = spf.newSAXParser();
162       sp.parse(new URL(enfinService).openStream(), this);      
163     } catch (Exception e)
164     {
165       Cache.log.warn("Exception when discovering One Way services: ",e);      
166     }
167     catch (Error e)
168     {
169       Cache.log.warn("Error when discovering One Way services: ",e);
170     }
171     running = false;
172     Cache.log.debug("Finished running.");
173   }
174
175   /**
176    * have we finished running yet ?
177    * @return false if we have been run.
178    */
179   public boolean isRunning()
180   {
181     
182     // TODO Auto-generated method stub
183     return !started || running;
184   }
185   public static void main(String[] args){
186     Cache.initLogger();
187     EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
188     while (ow.isRunning())
189     {
190       try {
191         Thread.sleep(50);
192       } catch (Exception e){};
193      
194     }
195     for (int i=0;i<ow.groupURLLinks.size();i++) {
196       System.err.println("Description"+ow.groupURLdescr.elementAt(i)+"Service URL: "+ow.groupURLLinks.elementAt(i));
197     }
198   }
199   /// Copied from jalview.gui.PopupMenu
200   /**
201    * add a late bound URL service item to the given menu
202    * 
203    * @param linkMenu
204    * @param label -
205    *                menu label string
206    * @param urlgenerator GroupURLLink used to generate URL  
207    * @param urlstub Object array returned from the makeUrlStubs function.
208    */
209   private void addshowLink(JMenu linkMenu, String label, String descr, final GroupUrlLink urlgenerator, final Object[] urlstub)
210   {
211     JMenuItem item = new JMenuItem(label);
212     item.setToolTipText("Submit ("+urlgenerator.getNumberInvolved(urlstub)+" seqs) to workflow: "+descr);
213     item.addActionListener(new java.awt.event.ActionListener()
214     {
215       public void actionPerformed(ActionEvent e)
216       {
217         new Thread(new Runnable()
218         {
219
220           public void run()
221           {
222             showLink(urlgenerator.constructFrom(urlstub));
223           }
224
225         }).start();
226       }
227     });
228
229     linkMenu.add(item);
230   }
231
232   /**
233    * open the given link in a new browser window
234    * @param url
235    */
236   public void showLink(String url)
237   {
238     try
239     {
240       jalview.util.BrowserLauncher.openURL(url);
241     } catch (Exception ex)
242     {
243       JOptionPane
244               .showInternalMessageDialog(
245                       Desktop.desktop,
246                       "Unixers: Couldn't find default web browser."
247                               + "\nAdd the full path to your browser in Preferences.",
248                       "Web browser not found", JOptionPane.WARNING_MESSAGE);
249
250       ex.printStackTrace();
251     }
252   }
253   /**
254    * called by a web service menu instance when it is opened.
255    * @param enfinServiceMenu
256    * @param alignFrame
257    */
258   private void buildGroupLinkMenu(JMenu enfinServiceMenu,
259           AlignFrame alignFrame)
260   {
261     SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
262     SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
263     if (sg==null) {
264       // consider visible regions here/
265     }
266     enfinServiceMenu.removeAll();
267     JMenu entries = buildGroupURLMenu(seqs,sg);
268     if (entries!=null)
269     {
270       for (int i=0,iSize=entries.getMenuComponentCount();i<iSize;i++)
271       {
272         // transfer - menu component is removed from entries automatically
273         enfinServiceMenu.add(entries.getMenuComponent(0));
274       }
275       // entries.removeAll();
276       enfinServiceMenu.setEnabled(true);
277     } else {
278       enfinServiceMenu.setEnabled(false);
279     }
280   }
281
282   /**
283    * construct a dynamic enfin services menu given a sequence selection
284    * @param seqs
285    * @param sg
286    * @param groupLinks
287    * @return
288    */
289   private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
290   {
291     
292     // TODO: usability: thread off the generation of group url content so root menu appears asap
293     // sequence only URLs
294     // ID/regex match URLs
295     JMenu groupLinksMenu = new JMenu("Group Link");
296     JMenu[] linkMenus = new JMenu[] { null, new JMenu("IDS"), new JMenu("Sequences"), new JMenu("IDS and Sequences")}; // three types of url that might be created.
297     String[][] idandseqs = GroupUrlLink.formStrings(seqs);
298     Hashtable commonDbrefs = new Hashtable();
299     for (int sq = 0; sq<seqs.length;sq++) {
300
301       int start,end;
302       if (sg!=null) {
303         start = seqs[sq].findPosition(sg.getStartRes());
304         end=seqs[sq].findPosition(sg.getEndRes()); 
305       } else {
306         // get total width of alignment.
307         start = seqs[sq].getStart();
308         end = seqs[sq].findPosition(seqs[sq].getLength());
309       }
310       // we skip sequences which do not have any non-gaps in the region of interest
311       if (start>end)
312       {
313         continue;
314       }
315       // just collect ids from dataset sequence
316       // TODO: check if IDs collected from selecton group intersects with the current selection, too
317       SequenceI sqi = seqs[sq];
318       while (sqi.getDatasetSequence()!=null) {
319         sqi = sqi.getDatasetSequence(); }
320       DBRefEntry[] dbr = sqi.getDBRef();
321       if (dbr!=null && dbr.length>0)
322       {
323         for (int d=0;d<dbr.length;d++)
324         {
325           String src =dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
326           Object[] sarray = (Object[]) commonDbrefs.get(src);
327           if (sarray==null)
328           {
329             sarray = new Object[2];
330             sarray[0] = new int[] { 0 };
331             sarray[1] = new String[seqs.length];
332             
333             commonDbrefs.put(src,sarray);
334           }
335           
336           if (((String[])sarray[1])[sq]==null) {
337             if (!dbr[d].hasMap() || (dbr[d].getMap().locateMappedRange(start, end)!=null)) {
338               ((String[])sarray[1])[sq] = dbr[d].getAccessionId();
339               ((int[])sarray[0])[0]++;
340             }
341           }
342         }
343       }
344     }
345     // now create group links for all distinct ID/sequence sets.
346     boolean addMenu = false; // indicates if there are any group links to give to user  
347     for (int i = 0; i < groupURLLinks.size(); i++) {
348         String link = groupURLLinks.elementAt(i).toString();
349         String descr = groupURLdescr.elementAt(i).toString();
350 //        boolean specialCase = additionalPar.elementAt(i).toString().equals(BACKGROUND);
351         GroupUrlLink urlLink = null;
352         try
353         {
354           urlLink = new GroupUrlLink(link);
355         } catch (Exception foo)
356         {
357           jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
358                   + "'", foo);
359           continue;
360         }
361         ;
362         if (!urlLink.isValid())
363         {
364           jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
365           continue;
366         }
367         final String label = urlLink.getLabel();
368         boolean usingNames = false;
369         // Now see which parts of the group apply for this URL
370         String ltarget;
371         String[] seqstr,ids; // input to makeUrl
372         for (int t=0;t<allowedDb.length; t++) {
373           ltarget = allowedDb[t]; // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
374           Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
375           if (idset!=null)
376           {
377             int numinput = ((int[])idset[0])[0];
378             String[] allids = ((String[])idset[1]);
379             seqstr = new String[numinput];
380             ids = new String[numinput];
381             for (int sq=0,idcount=0;sq<seqs.length;sq++)
382             {
383               if (allids[sq]!=null) {
384                 ids[idcount] = allids[sq];
385                 seqstr[idcount++] = idandseqs[1][sq];
386               }
387             }
388             addMenu = addMenu | createAndAddLinks(linkMenus,false,urlLink,label,ltarget,descr, ids,seqstr);
389           }
390         }
391         // also do names only.
392         seqstr = idandseqs[1];
393         ids = idandseqs[0];
394         addMenu = addMenu | createAndAddLinks(linkMenus,true,urlLink,label,"Names",descr, ids,seqstr);
395       }
396     if (addMenu)
397     {
398       groupLinksMenu = new JMenu("Group Links");
399       for (int m=0;m<linkMenus.length; m++)
400       {
401         if (linkMenus[m]!=null && linkMenus[m].getMenuComponentCount()>0)
402         {
403           groupLinksMenu.add(linkMenus[m]);
404         }
405       }
406       
407       return groupLinksMenu;
408     }
409     return null;
410   }
411
412   private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
413           GroupUrlLink urlLink, String label, String ltarget, String descr, String[] ids,
414           String[] seqstr)
415   {
416     Object[] urlset = urlLink.makeUrlStubs(ids,seqstr, "FromJalview"+System.currentTimeMillis(),false);
417     if (urlset!=null)
418     {
419       int type = urlLink.getGroupURLType() & 3;
420       //System.out.println(urlLink.getGroupURLType() +" "+((String[])urlset[3])[0]);
421       // first two bits ofurlLink type bitfield are sequenceids and sequences
422       // TODO: FUTURE: ensure the groupURL menu structure can be generalised
423       addshowLink(linkMenus[type], label + (((type & 1)==1) ? ("("+(usingNames ? "Names" : ltarget)+")") : ""), descr, urlLink, urlset);
424       return true;
425     }
426     return false;
427   }
428   /// end of stuff copied from popupmenu
429   public void attachWSMenuEntry(final JMenu wsmenu, final AlignFrame alignFrame)
430   {
431     final JMenu enfinServiceMenu = new JMenu("Envision 2");
432     wsmenu.add(enfinServiceMenu);
433     enfinServiceMenu.setEnabled(false);
434     wsmenu.addMenuListener(new MenuListener()
435       {
436         // this listener remembers when the menu was first selected, and
437         // doesn't rebuild the session list until it has been cleared and
438         // reselected again.
439         boolean refresh = true;
440
441         public void menuCanceled(MenuEvent e)
442         {
443           refresh = true;
444         }
445
446         public void menuDeselected(MenuEvent e)
447         {
448           refresh = true;
449         }
450
451         public void menuSelected(MenuEvent e)
452         {
453           if (refresh)
454           {
455             try {
456               buildGroupLinkMenu(enfinServiceMenu,alignFrame);
457             } catch (OutOfMemoryError ex)
458             {
459               Cache.log.error("Out of memory when calculating the Envision2 links.",ex);
460               enfinServiceMenu.setEnabled(false);
461             }
462             refresh = false;
463           }
464         }
465      });
466     
467     
468   }
469
470 }