2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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;
28 import jalview.util.GroupUrlLink.UrlStringTooLongException;
30 import java.awt.Component;
31 import java.awt.Cursor;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.ItemEvent;
35 import java.awt.event.ItemListener;
36 import java.io.BufferedReader;
37 import java.io.InputStreamReader;
39 import java.util.Hashtable;
41 import java.util.Vector;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuItem;
45 import javax.swing.JOptionPane;
46 import javax.swing.event.MenuEvent;
47 import javax.swing.event.MenuListener;
48 import javax.xml.parsers.SAXParser;
49 import javax.xml.parsers.SAXParserFactory;
51 import org.xml.sax.Attributes;
52 import org.xml.sax.SAXException;
53 import org.xml.sax.helpers.DefaultHandler;
56 * Lightweight runnable to discover dynamic 'one way' group URL services
61 public class EnfinEnvision2OneWay extends DefaultHandler implements
62 Runnable, WSMenuEntryProviderI
64 private static EnfinEnvision2OneWay groupURLLinksGatherer = null;
66 public static EnfinEnvision2OneWay getInstance()
68 if (groupURLLinksGatherer == null)
70 groupURLLinksGatherer = new EnfinEnvision2OneWay();
72 return groupURLLinksGatherer;
75 private void waitForCompletion()
77 if (groupURLLinksGatherer.isRunning())
79 // wait around and show a visual delay indicator
80 Cursor oldCursor = Desktop.instance.getCursor();
81 Desktop.instance.setCursor(new Cursor(Cursor.WAIT_CURSOR));
82 while (groupURLLinksGatherer.isRunning())
87 } catch (InterruptedException e)
92 Desktop.instance.setCursor(oldCursor);
96 public Vector getEnvisionServiceGroupURLS()
105 private static String BACKGROUND = "BACKGROUNDPARAM";
108 * contains null strings or one of the above constants - indicate if this URL
111 private Vector additionalPar = new Vector();
114 * the enfin service URL
116 private String enfinService = null;
118 private String description = null;
120 private String wfname;
125 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
126 * java.lang.String, java.lang.String)
128 public void endElement(String uri, String localName, String qName)
132 // System.err.println("End element: : '"+uri+" "+localName+" "+qName);
133 if (qName.equalsIgnoreCase("workflow") && description != null
134 && description.length() > 0)
136 // 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|,");
137 // 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|,");
138 System.err.println("Adding entry for " + wfname + " " + description);
139 if (wfname.toLowerCase().indexOf("funcnet") == -1)
141 groupURLdescr.addElement(description);
142 groupURLdescr.addElement(description);
146 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
148 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,"); // #"+description+"#");
152 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
154 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,"); // #"+description+"#");
162 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
164 public void characters(char[] ch, int start, int length)
167 if (description != null)
169 for (int i = start; i < start + length; i++)
171 description += ch[i];
179 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
180 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
182 public void startElement(String uri, String localName, String qName,
183 Attributes attributes) throws SAXException
185 if (qName.equalsIgnoreCase("workflow"))
188 wfname = attributes.getValue("name");
190 if (qName.equalsIgnoreCase("description"))
195 // System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
196 // super.startElement(uri,localName,qname,attributes);
199 private boolean started = false;
201 private boolean running = false;
203 private Vector groupURLLinks = null;
205 private Vector groupURLdescr = null;
207 private static String[] allowedDb = new String[]
208 { "UNIPROT", "EMBL", "PDB" };
210 public EnfinEnvision2OneWay()
212 groupURLLinks = new Vector();
213 groupURLdescr = new Vector();
215 enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE",
216 "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
217 new Thread(this).start();
226 SAXParserFactory spf = SAXParserFactory.newInstance();
227 SAXParser sp = spf.newSAXParser();
228 sp.parse(new URL(enfinService).openStream(), this);
229 } catch (Exception e)
231 Cache.log.warn("Exception when discovering One Way services: ", e);
234 Cache.log.warn("Error when discovering One Way services: ", e);
237 Cache.log.debug("Finished running.");
241 * have we finished running yet ?
243 * @return false if we have been run.
245 public boolean isRunning()
248 // TODO Auto-generated method stub
249 return !started || running;
252 public static void main(String[] args)
255 EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
256 while (ow.isRunning())
261 } catch (Exception e)
267 for (int i = 0; i < ow.groupURLLinks.size(); i++)
269 System.err.println("Description" + ow.groupURLdescr.elementAt(i)
270 + "Service URL: " + ow.groupURLLinks.elementAt(i));
274 // / Copied from jalview.gui.PopupMenu
276 * add a late bound URL service item to the given menu
280 * - menu label string
281 * @param urlgenerator
282 * GroupURLLink used to generate URL
284 * Object array returned from the makeUrlStubs function.
286 private void addshowLink(JMenu linkMenu, String label, String descr,
287 String dbname, final GroupUrlLink urlgenerator,
288 final Object[] urlstub)
290 Component[] jmi = linkMenu.getMenuComponents();
291 for (int i = 0; i < jmi.length; i++)
293 if (jmi[i] instanceof JMenuItem
294 && ((JMenuItem) jmi[i]).getText().equalsIgnoreCase(label))
296 // don't add this - its a repeat of an existing URL.
300 boolean seqsorids = (urlgenerator.getGroupURLType() & urlgenerator.SEQUENCEIDS) == 0;
301 int i = urlgenerator.getNumberInvolved(urlstub);
302 JMenuItem item = new JMenuItem(label);
304 if (dbname == null || dbname.trim().length() == 0)
308 item.setToolTipText("<html>"
309 + JvSwingUtils.wrapTooltip("Submit " + i + " " + dbname + " "
310 + (seqsorids ? "sequence" : "sequence id")
313 + " to<br/>" + descr) + "</html>");
314 item.addActionListener(new java.awt.event.ActionListener()
316 public void actionPerformed(ActionEvent e)
318 new Thread(new Runnable()
325 showLink(urlgenerator.constructFrom(urlstub));
326 } catch (UrlStringTooLongException ex)
328 Cache.log.warn("Not showing link: URL is too long!", ex);
340 * open the given link in a new browser window
344 public void showLink(String url)
348 jalview.util.BrowserLauncher.openURL(url);
349 } catch (Exception ex)
352 .showInternalMessageDialog(
354 "Unixers: Couldn't find default web browser."
355 + "\nAdd the full path to your browser in Preferences.",
356 "Web browser not found", JOptionPane.WARNING_MESSAGE);
358 ex.printStackTrace();
363 * called by a web service menu instance when it is opened.
365 * @param enfinServiceMenu
368 private void buildGroupLinkMenu(JMenu enfinServiceMenu,
369 AlignFrame alignFrame)
371 if (running || !started)
375 SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
376 SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
379 // consider visible regions here/
381 enfinServiceMenu.removeAll();
382 JMenu entries = buildGroupURLMenu(seqs, sg);
385 for (int i = 0, iSize = entries.getMenuComponentCount(); i < iSize; i++)
387 // transfer - menu component is removed from entries automatically
388 enfinServiceMenu.add(entries.getMenuComponent(0));
390 // entries.removeAll();
391 enfinServiceMenu.setEnabled(true);
395 enfinServiceMenu.setEnabled(false);
400 * construct a dynamic enfin services menu given a sequence selection
407 private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
409 if (groupURLdescr == null || groupURLLinks == null)
411 // TODO: usability: thread off the generation of group url content so root
413 // sequence only URLs
414 // ID/regex match URLs
415 JMenu groupLinksMenu = new JMenu("Group Link");
416 String[][] idandseqs = GroupUrlLink.formStrings(seqs);
417 Hashtable commonDbrefs = new Hashtable();
418 for (int sq = 0; sq < seqs.length; sq++)
424 start = seqs[sq].findPosition(sg.getStartRes());
425 end = seqs[sq].findPosition(sg.getEndRes());
429 // get total width of alignment.
430 start = seqs[sq].getStart();
431 end = seqs[sq].findPosition(seqs[sq].getLength());
433 // we skip sequences which do not have any non-gaps in the region of
439 // just collect ids from dataset sequence
440 // TODO: check if IDs collected from selecton group intersects with the
441 // current selection, too
442 SequenceI sqi = seqs[sq];
443 while (sqi.getDatasetSequence() != null)
445 sqi = sqi.getDatasetSequence();
447 DBRefEntry[] dbr = sqi.getDBRef();
448 if (dbr != null && dbr.length > 0)
450 for (int d = 0; d < dbr.length; d++)
452 String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
453 Object[] sarray = (Object[]) commonDbrefs.get(src);
456 sarray = new Object[2];
457 sarray[0] = new int[]
459 sarray[1] = new String[seqs.length];
461 commonDbrefs.put(src, sarray);
464 if (((String[]) sarray[1])[sq] == null)
467 || (dbr[d].getMap().locateMappedRange(start, end) != null))
469 ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
470 ((int[]) sarray[0])[0]++;
476 // now create group links for all distinct ID/sequence sets.
477 Hashtable<String, JMenu[]> gurlMenus = new Hashtable<String, JMenu[]>();
479 * last number of sequences where URL generation failed
481 int[] nsqtype = new int[]
482 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
483 for (int i = 0; i < groupURLLinks.size(); i++)
485 String link = (String) groupURLLinks.elementAt(i);
486 String descr = (String) groupURLdescr.elementAt(i);
488 // boolean specialCase =
489 // additionalPar.elementAt(i).toString().equals(BACKGROUND);
490 GroupUrlLink urlLink = null;
493 urlLink = new GroupUrlLink(link);
494 } catch (Exception foo)
496 jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
501 if (!urlLink.isValid())
503 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
506 final String label = urlLink.getLabel();
507 // create/recover the sub menus that might be populated for this link.
508 JMenu[] wflinkMenus = gurlMenus.get(label);
509 if (wflinkMenus == null)
511 // three types of url that might be
513 wflinkMenus = new JMenu[]
514 { null, new JMenu("IDS"), new JMenu("Sequences"),
515 new JMenu("IDS and Sequences") };
516 gurlMenus.put(label, wflinkMenus);
519 boolean usingNames = false;
520 // Now see which parts of the group apply for this URL
522 String[] seqstr, ids; // input to makeUrl
523 for (int t = 0; t < allowedDb.length; t++)
525 ltarget = allowedDb[t]; // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
526 Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
529 int numinput = ((int[]) idset[0])[0];
530 String[] allids = ((String[]) idset[1]);
531 seqstr = new String[numinput];
532 ids = new String[numinput];
533 if (nsqtype[urlLink.getGroupURLType()] > 0
534 && numinput >= nsqtype[urlLink.getGroupURLType()])
538 for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
540 if (allids[sq] != null)
542 ids[idcount] = allids[sq];
543 seqstr[idcount++] = idandseqs[1][sq];
548 createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
550 } catch (UrlStringTooLongException ex)
552 nsqtype[urlLink.getGroupURLType()] = numinput;
556 // also do names only.
557 seqstr = idandseqs[1];
559 if (nsqtype[urlLink.getGroupURLType()] > 0
560 && idandseqs[0].length >= nsqtype[urlLink.getGroupURLType()])
567 createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
569 } catch (UrlStringTooLongException ex)
571 nsqtype[urlLink.getGroupURLType()] = idandseqs[0].length;
574 boolean anyadded = false; // indicates if there are any group links to give
576 for (Map.Entry<String, JMenu[]> menues : gurlMenus.entrySet())
578 JMenu grouplinkset = new JMenu(menues.getKey());
579 JMenu[] wflinkMenus = menues.getValue();
580 for (int m = 0; m < wflinkMenus.length; m++)
582 if (wflinkMenus[m] != null
583 && wflinkMenus[m].getMenuComponentCount() > 0)
586 grouplinkset.add(wflinkMenus[m]);
589 groupLinksMenu.add(grouplinkset);
593 return groupLinksMenu;
598 private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
599 GroupUrlLink urlLink, String label, String ltarget, String descr,
600 String[] ids, String[] seqstr) throws UrlStringTooLongException
602 Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
603 + System.currentTimeMillis(), false);
607 int type = urlLink.getGroupURLType() & 3;
608 // System.out.println(urlLink.getGroupURLType()
609 // +" "+((String[])urlset[3])[0]);
610 // first two bits ofurlLink type bitfield are sequenceids and sequences
611 // TODO: FUTURE: ensure the groupURL menu structure can be generalised
616 + (ltarget == null ? (((type & 1) == 1 ? "ID"
617 : "Sequence") + (urlLink
618 .getNumberInvolved(urlset) > 1 ? "s" : ""))
619 : (usingNames ? (((type & 1) == 1) ? "(Names)"
621 : ("(" + ltarget + ")"))), descr,
622 usingNames ? null : label, urlLink, urlset);
628 // / end of stuff copied from popupmenu
629 public void attachWSMenuEntry(final JMenu wsmenu,
630 final AlignFrame alignFrame)
632 final JMenu enfinServiceMenu = new JMenu("Envision 2");
633 wsmenu.add(enfinServiceMenu);
634 enfinServiceMenu.setEnabled(false);
635 wsmenu.addMenuListener(new MenuListener()
637 // this listener remembers when the menu was first selected, and
638 // doesn't rebuild the session list until it has been cleared and
640 boolean refresh = true;
642 public void menuCanceled(MenuEvent e)
647 public void menuDeselected(MenuEvent e)
652 public void menuSelected(MenuEvent e)
654 if (refresh && !isRunning())
656 new Thread(new Runnable()
662 buildGroupLinkMenu(enfinServiceMenu, alignFrame);
663 } catch (OutOfMemoryError ex)
666 .error("Out of memory when calculating the Envision2 links.",
668 enfinServiceMenu.setEnabled(false);