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;
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;
22 import java.util.Hashtable;
23 import java.util.Vector;
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;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
35 import org.xml.sax.helpers.DefaultHandler;
38 * Lightweight runnable to discover dynamic 'one way' group URL services
42 public class EnfinEnvision2OneWay extends DefaultHandler implements Runnable,WSMenuEntryProviderI
44 private static EnfinEnvision2OneWay groupURLLinksGatherer=null;
45 public static EnfinEnvision2OneWay getInstance() {
46 if (groupURLLinksGatherer==null) {
47 groupURLLinksGatherer = new EnfinEnvision2OneWay();
49 return groupURLLinksGatherer;
51 private void waitForCompletion() {
52 if (groupURLLinksGatherer.isRunning())
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())
61 } catch (InterruptedException e ){};
63 Desktop.instance.setCursor(oldCursor);
66 public Vector getEnvisionServiceGroupURLS() {
73 private static String BACKGROUND="BACKGROUNDPARAM";
75 * contains null strings or one of the above constants - indicate if this URL is a special case.
77 private Vector additionalPar = new Vector();
79 * the enfin service URL
81 private String enfinService=null;
82 private String description=null;
83 private String wfname;
85 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
87 public void endElement(String uri, String localName, String qName)
91 // System.err.println("End element: : '"+uri+" "+localName+" "+qName);
92 if (qName.equalsIgnoreCase("workflow") && description!=null && description.length()>0)
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)
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+"#");
108 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
110 public void characters(char[] ch, int start, int length)
113 if (description!=null) {
114 for (int i=start; i<start+length; i++) {
121 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
123 public void startElement(String uri, String localName, String qName,
124 Attributes attributes) throws SAXException
126 if (qName.equalsIgnoreCase("workflow"))
129 wfname = attributes.getValue("name");
131 if (qName.equalsIgnoreCase("description"))
137 //System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
138 // super.startElement(uri,localName,qname,attributes);
141 private boolean started=false;
142 private boolean running=false;
143 private Vector groupURLLinks = null;
144 private Vector groupURLdescr = null;
146 public EnfinEnvision2OneWay() {
147 groupURLLinks = new Vector();
148 groupURLdescr = new Vector();
150 enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE", "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
151 new Thread(this).start();
159 SAXParserFactory spf = SAXParserFactory.newInstance();
160 SAXParser sp = spf.newSAXParser();
161 sp.parse(new URL(enfinService).openStream(), this);
162 } catch (Exception e)
164 Cache.log.warn("Exception when discovering One Way services: ",e);
168 Cache.log.warn("Error when discovering One Way services: ",e);
171 Cache.log.debug("Finished running.");
175 * have we finished running yet ?
176 * @return false if we have been run.
178 public boolean isRunning()
181 // TODO Auto-generated method stub
182 return !started || running;
184 public static void main(String[] args){
186 EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
187 while (ow.isRunning())
191 } catch (Exception e){};
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));
198 /// Copied from jalview.gui.PopupMenu
200 * add a late bound URL service item to the given menu
205 * @param urlgenerator GroupURLLink used to generate URL
206 * @param urlstub Object array returned from the makeUrlStubs function.
208 private void addshowLink(JMenu linkMenu, String label, String descr, final GroupUrlLink urlgenerator, final Object[] urlstub)
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()
214 public void actionPerformed(ActionEvent e)
216 new Thread(new Runnable()
221 showLink(urlgenerator.constructFrom(urlstub));
232 * open the given link in a new browser window
235 public void showLink(String url)
239 jalview.util.BrowserLauncher.openURL(url);
240 } catch (Exception ex)
243 .showInternalMessageDialog(
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);
249 ex.printStackTrace();
253 * called by a web service menu instance when it is opened.
254 * @param enfinServiceMenu
257 private void buildGroupLinkMenu(JMenu enfinServiceMenu,
258 AlignFrame alignFrame)
260 SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
261 SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
263 // consider visible regions here/
265 enfinServiceMenu.removeAll();
266 JMenu entries = buildGroupURLMenu(seqs,sg);
269 for (int i=0,iSize=entries.getMenuComponentCount();i<iSize;i++)
271 // transfer - menu component is removed from entries automatically
272 enfinServiceMenu.add(entries.getMenuComponent(0));
274 // entries.removeAll();
275 enfinServiceMenu.setEnabled(true);
277 enfinServiceMenu.setEnabled(false);
282 * construct a dynamic enfin services menu given a sequence selection
288 private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
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++) {
302 start = seqs[sq].findPosition(sg.getStartRes());
303 end=seqs[sq].findPosition(sg.getEndRes());
305 // get total width of alignment.
306 start = seqs[sq].getStart();
307 end = seqs[sq].findPosition(seqs[sq].getLength());
309 // we skip sequences which do not have any non-gaps in the region of interest
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)
322 for (int d=0;d<dbr.length;d++)
324 String src =dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
325 Object[] sarray = (Object[]) commonDbrefs.get(src);
328 sarray = new Object[2];
329 sarray[0] = new int[] { 0 };
330 sarray[1] = new String[seqs.length];
332 commonDbrefs.put(src,sarray);
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]++;
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;
353 urlLink = new GroupUrlLink(link);
354 } catch (Exception foo)
356 jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
361 if (!urlLink.isValid())
363 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
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
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++)
380 if (allids[sq]!=null) {
381 ids[idcount] = allids[sq];
382 seqstr[idcount++] = idandseqs[1][sq];
386 // just use the id/seq set
387 seqstr = idandseqs[1];
391 // and try and make the groupURL!
393 Object[] urlset = urlLink.makeUrlStubs(ids,seqstr, "FromJalview"+System.currentTimeMillis(),false);
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);
406 groupLinksMenu = new JMenu("Group Links");
407 for (int m=0;m<linkMenus.length; m++)
409 if (linkMenus[m]!=null && linkMenus[m].getMenuComponentCount()>0)
411 groupLinksMenu.add(linkMenus[m]);
415 return groupLinksMenu;
420 /// end of stuff copied from popupmenu
421 public void attachWSMenuEntry(final JMenu wsmenu, final AlignFrame alignFrame)
423 final JMenu enfinServiceMenu = new JMenu("Envision 2");
424 wsmenu.add(enfinServiceMenu);
425 enfinServiceMenu.setEnabled(false);
426 wsmenu.addMenuListener(new MenuListener()
428 // this listener remembers when the menu was first selected, and
429 // doesn't rebuild the session list until it has been cleared and
431 boolean refresh = true;
433 public void menuCanceled(MenuEvent e)
438 public void menuDeselected(MenuEvent e)
443 public void menuSelected(MenuEvent e)
448 buildGroupLinkMenu(enfinServiceMenu,alignFrame);
449 } catch (OutOfMemoryError ex)
451 Cache.log.error("Out of memory when calculating the Envision2 links.",ex);
452 enfinServiceMenu.setEnabled(false);