2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.bin.Cache;
21 import jalview.datamodel.DBRefEntry;
22 import jalview.datamodel.SequenceGroup;
23 import jalview.datamodel.SequenceI;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.Desktop;
26 import jalview.gui.JvSwingUtils;
27 import jalview.util.GroupUrlLink;
29 import java.awt.Component;
30 import java.awt.Cursor;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.ItemEvent;
34 import java.awt.event.ItemListener;
35 import java.io.BufferedReader;
36 import java.io.InputStreamReader;
38 import java.util.Hashtable;
40 import java.util.Vector;
42 import javax.swing.JMenu;
43 import javax.swing.JMenuItem;
44 import javax.swing.JOptionPane;
45 import javax.swing.event.MenuEvent;
46 import javax.swing.event.MenuListener;
47 import javax.xml.parsers.SAXParser;
48 import javax.xml.parsers.SAXParserFactory;
50 import org.xml.sax.Attributes;
51 import org.xml.sax.SAXException;
52 import org.xml.sax.helpers.DefaultHandler;
55 * Lightweight runnable to discover dynamic 'one way' group URL services
60 public class EnfinEnvision2OneWay extends DefaultHandler implements
61 Runnable, WSMenuEntryProviderI
63 private static EnfinEnvision2OneWay groupURLLinksGatherer = null;
65 public static EnfinEnvision2OneWay getInstance()
67 if (groupURLLinksGatherer == null)
69 groupURLLinksGatherer = new EnfinEnvision2OneWay();
71 return groupURLLinksGatherer;
74 private void waitForCompletion()
76 if (groupURLLinksGatherer.isRunning())
78 // wait around and show a visual delay indicator
79 Cursor oldCursor = Desktop.instance.getCursor();
80 Desktop.instance.setCursor(new Cursor(Cursor.WAIT_CURSOR));
81 while (groupURLLinksGatherer.isRunning())
86 } catch (InterruptedException e)
91 Desktop.instance.setCursor(oldCursor);
95 public Vector getEnvisionServiceGroupURLS()
104 private static String BACKGROUND = "BACKGROUNDPARAM";
107 * contains null strings or one of the above constants - indicate if this URL
110 private Vector additionalPar = new Vector();
113 * the enfin service URL
115 private String enfinService = null;
117 private String description = null;
119 private String wfname;
124 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
125 * java.lang.String, java.lang.String)
127 public void endElement(String uri, String localName, String qName)
131 // System.err.println("End element: : '"+uri+" "+localName+" "+qName);
132 if (qName.equalsIgnoreCase("workflow") && description != null
133 && description.length() > 0)
135 // 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|,");
136 // 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|,");
137 System.err.println("Adding entry for " + wfname + " " + description);
138 if (wfname.toLowerCase().indexOf("funcnet") == -1)
140 groupURLdescr.addElement(description);
141 groupURLdescr.addElement(description);
145 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
147 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,"); // #"+description+"#");
151 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
153 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,"); // #"+description+"#");
161 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
163 public void characters(char[] ch, int start, int length)
166 if (description != null)
168 for (int i = start; i < start + length; i++)
170 description += ch[i];
178 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
179 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
181 public void startElement(String uri, String localName, String qName,
182 Attributes attributes) throws SAXException
184 if (qName.equalsIgnoreCase("workflow"))
187 wfname = attributes.getValue("name");
189 if (qName.equalsIgnoreCase("description"))
194 // System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
195 // super.startElement(uri,localName,qname,attributes);
198 private boolean started = false;
200 private boolean running = false;
202 private Vector groupURLLinks = null;
204 private Vector groupURLdescr = null;
206 private static String[] allowedDb = new String[]
207 { "UNIPROT", "EMBL", "PDB" };
209 public EnfinEnvision2OneWay()
211 groupURLLinks = new Vector();
212 groupURLdescr = new Vector();
214 enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE",
215 "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
216 new Thread(this).start();
225 SAXParserFactory spf = SAXParserFactory.newInstance();
226 SAXParser sp = spf.newSAXParser();
227 sp.parse(new URL(enfinService).openStream(), this);
228 } catch (Exception e)
230 Cache.log.warn("Exception when discovering One Way services: ", e);
233 Cache.log.warn("Error when discovering One Way services: ", e);
236 Cache.log.debug("Finished running.");
240 * have we finished running yet ?
242 * @return false if we have been run.
244 public boolean isRunning()
247 // TODO Auto-generated method stub
248 return !started || running;
251 public static void main(String[] args)
254 EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
255 while (ow.isRunning())
260 } catch (Exception e)
266 for (int i = 0; i < ow.groupURLLinks.size(); i++)
268 System.err.println("Description" + ow.groupURLdescr.elementAt(i)
269 + "Service URL: " + ow.groupURLLinks.elementAt(i));
273 // / Copied from jalview.gui.PopupMenu
275 * add a late bound URL service item to the given menu
279 * - menu label string
280 * @param urlgenerator
281 * GroupURLLink used to generate URL
283 * Object array returned from the makeUrlStubs function.
285 private void addshowLink(JMenu linkMenu, String label, String descr,
286 String dbname, final GroupUrlLink urlgenerator, final Object[] urlstub)
288 Component[] jmi = linkMenu.getMenuComponents();
289 for (int i = 0; i < jmi.length; i++)
291 if (jmi[i] instanceof JMenuItem
292 && ((JMenuItem) jmi[i]).getText().equalsIgnoreCase(label))
294 // don't add this - its a repeat of an existing URL.
298 boolean seqsorids = (urlgenerator.getGroupURLType() & urlgenerator.SEQUENCEIDS) == 0;
299 int i = urlgenerator.getNumberInvolved(urlstub);
300 JMenuItem item = new JMenuItem(label);
302 if (dbname==null || dbname.trim().length()==0)
306 item.setToolTipText("<html>"
307 + JvSwingUtils.wrapTooltip("Submit " + i + " " +
309 + (seqsorids ? "sequence" : "sequence id") + (i > 1 ? "s" : "")
311 + " to<br/>" + descr) + "</html>");
312 item.addActionListener(new java.awt.event.ActionListener()
314 public void actionPerformed(ActionEvent e)
316 new Thread(new Runnable()
321 showLink(urlgenerator.constructFrom(urlstub));
332 * open the given link in a new browser window
336 public void showLink(String url)
340 jalview.util.BrowserLauncher.openURL(url);
341 } catch (Exception ex)
344 .showInternalMessageDialog(
346 "Unixers: Couldn't find default web browser."
347 + "\nAdd the full path to your browser in Preferences.",
348 "Web browser not found", JOptionPane.WARNING_MESSAGE);
350 ex.printStackTrace();
355 * called by a web service menu instance when it is opened.
357 * @param enfinServiceMenu
360 private void buildGroupLinkMenu(JMenu enfinServiceMenu,
361 AlignFrame alignFrame)
363 SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
364 SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
367 // consider visible regions here/
369 enfinServiceMenu.removeAll();
370 JMenu entries = buildGroupURLMenu(seqs, sg);
373 for (int i = 0, iSize = entries.getMenuComponentCount(); i < iSize; i++)
375 // transfer - menu component is removed from entries automatically
376 enfinServiceMenu.add(entries.getMenuComponent(0));
378 // entries.removeAll();
379 enfinServiceMenu.setEnabled(true);
383 enfinServiceMenu.setEnabled(false);
388 * construct a dynamic enfin services menu given a sequence selection
395 private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
398 // TODO: usability: thread off the generation of group url content so root
400 // sequence only URLs
401 // ID/regex match URLs
402 JMenu groupLinksMenu = new JMenu("Group Link");
403 String[][] idandseqs = GroupUrlLink.formStrings(seqs);
404 Hashtable commonDbrefs = new Hashtable();
405 for (int sq = 0; sq < seqs.length; sq++)
411 start = seqs[sq].findPosition(sg.getStartRes());
412 end = seqs[sq].findPosition(sg.getEndRes());
416 // get total width of alignment.
417 start = seqs[sq].getStart();
418 end = seqs[sq].findPosition(seqs[sq].getLength());
420 // we skip sequences which do not have any non-gaps in the region of
426 // just collect ids from dataset sequence
427 // TODO: check if IDs collected from selecton group intersects with the
428 // current selection, too
429 SequenceI sqi = seqs[sq];
430 while (sqi.getDatasetSequence() != null)
432 sqi = sqi.getDatasetSequence();
434 DBRefEntry[] dbr = sqi.getDBRef();
435 if (dbr != null && dbr.length > 0)
437 for (int d = 0; d < dbr.length; d++)
439 String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
440 Object[] sarray = (Object[]) commonDbrefs.get(src);
443 sarray = new Object[2];
444 sarray[0] = new int[]
446 sarray[1] = new String[seqs.length];
448 commonDbrefs.put(src, sarray);
451 if (((String[]) sarray[1])[sq] == null)
454 || (dbr[d].getMap().locateMappedRange(start, end) != null))
456 ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
457 ((int[]) sarray[0])[0]++;
463 // now create group links for all distinct ID/sequence sets.
464 Hashtable<String, JMenu[]> gurlMenus = new Hashtable<String, JMenu[]>();
465 for (int i = 0; i < groupURLLinks.size(); i++)
467 String link = groupURLLinks.elementAt(i).toString();
468 String descr = groupURLdescr.elementAt(i).toString();
470 // boolean specialCase =
471 // additionalPar.elementAt(i).toString().equals(BACKGROUND);
472 GroupUrlLink urlLink = null;
475 urlLink = new GroupUrlLink(link);
476 } catch (Exception foo)
478 jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
483 if (!urlLink.isValid())
485 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
488 final String label = urlLink.getLabel();
489 // create/recover the sub menus that might be populated for this link.
490 JMenu[] wflinkMenus = gurlMenus.get(label);
491 if (wflinkMenus == null)
493 // three types of url that might be
495 wflinkMenus = new JMenu[]
496 { null, new JMenu("IDS"), new JMenu("Sequences"),
497 new JMenu("IDS and Sequences") };
498 gurlMenus.put(label, wflinkMenus);
501 boolean usingNames = false;
502 // Now see which parts of the group apply for this URL
504 String[] seqstr, ids; // input to makeUrl
505 for (int t = 0; t < allowedDb.length; t++)
507 ltarget = allowedDb[t]; // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
508 Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
511 int numinput = ((int[]) idset[0])[0];
512 String[] allids = ((String[]) idset[1]);
513 seqstr = new String[numinput];
514 ids = new String[numinput];
515 for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
517 if (allids[sq] != null)
519 ids[idcount] = allids[sq];
520 seqstr[idcount++] = idandseqs[1][sq];
523 createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
527 // also do names only.
528 seqstr = idandseqs[1];
530 createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
533 boolean anyadded = false; // indicates if there are any group links to give
535 for (Map.Entry<String, JMenu[]> menues : gurlMenus.entrySet())
537 JMenu grouplinkset = new JMenu(menues.getKey());
538 JMenu[] wflinkMenus = menues.getValue();
539 for (int m = 0; m < wflinkMenus.length; m++)
541 if (wflinkMenus[m] != null
542 && wflinkMenus[m].getMenuComponentCount() > 0)
545 grouplinkset.add(wflinkMenus[m]);
548 groupLinksMenu.add(grouplinkset);
552 return groupLinksMenu;
557 private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
558 GroupUrlLink urlLink, String label, String ltarget, String descr,
559 String[] ids, String[] seqstr)
561 Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
562 + System.currentTimeMillis(), false);
565 int type = urlLink.getGroupURLType() & 3;
566 // System.out.println(urlLink.getGroupURLType()
567 // +" "+((String[])urlset[3])[0]);
568 // first two bits ofurlLink type bitfield are sequenceids and sequences
569 // TODO: FUTURE: ensure the groupURL menu structure can be generalised
574 + (ltarget == null ? (((type & 1) == 1 ? "ID"
575 : "Sequence") + (urlLink
576 .getNumberInvolved(urlset) > 1 ? "s" : ""))
577 : (usingNames ? (((type & 1) == 1) ? "(Names)"
579 : ("(" + ltarget + ")"))), descr,
580 usingNames ? null : label, urlLink, urlset);
586 // / end of stuff copied from popupmenu
587 public void attachWSMenuEntry(final JMenu wsmenu,
588 final AlignFrame alignFrame)
590 final JMenu enfinServiceMenu = new JMenu("Envision 2");
591 wsmenu.add(enfinServiceMenu);
592 enfinServiceMenu.setEnabled(false);
593 wsmenu.addMenuListener(new MenuListener()
595 // this listener remembers when the menu was first selected, and
596 // doesn't rebuild the session list until it has been cleared and
598 boolean refresh = true;
600 public void menuCanceled(MenuEvent e)
605 public void menuDeselected(MenuEvent e)
610 public void menuSelected(MenuEvent e)
616 buildGroupLinkMenu(enfinServiceMenu, alignFrame);
617 } catch (OutOfMemoryError ex)
620 "Out of memory when calculating the Envision2 links.",
622 enfinServiceMenu.setEnabled(false);