2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Cache;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.AlignFrame;
28 import jalview.gui.Desktop;
29 import jalview.gui.JvSwingUtils;
30 import jalview.util.GroupUrlLink;
31 import jalview.util.GroupUrlLink.UrlStringTooLongException;
33 import java.awt.Component;
34 import java.awt.Cursor;
35 import java.awt.event.ActionEvent;
36 import java.io.UnsupportedEncodingException;
38 import java.net.URLEncoder;
39 import java.util.Hashtable;
41 import java.util.Vector;
42 import java.util.regex.Pattern;
44 import javax.swing.JMenu;
45 import javax.swing.JMenuItem;
46 import javax.swing.JOptionPane;
47 import javax.swing.event.MenuEvent;
48 import javax.swing.event.MenuListener;
49 import javax.xml.parsers.SAXParser;
50 import javax.xml.parsers.SAXParserFactory;
52 import org.xml.sax.Attributes;
53 import org.xml.sax.SAXException;
54 import org.xml.sax.helpers.DefaultHandler;
56 import com.lowagie.text.html.HtmlEncoder;
59 * Lightweight runnable to discover dynamic 'one way' group URL services
61 * as of Jalview 2.8.1 this class is mothballed and will be dropped in v3.
67 public class EnfinEnvision2OneWay extends DefaultHandler implements
68 Runnable, WSMenuEntryProviderI
70 private static EnfinEnvision2OneWay groupURLLinksGatherer = null;
72 public static EnfinEnvision2OneWay getInstance()
74 if (groupURLLinksGatherer == null)
76 groupURLLinksGatherer = new EnfinEnvision2OneWay();
78 return groupURLLinksGatherer;
81 private void waitForCompletion()
83 if (groupURLLinksGatherer.isRunning())
85 // wait around and show a visual delay indicator
86 Cursor oldCursor = Desktop.instance.getCursor();
87 Desktop.instance.setCursor(new Cursor(Cursor.WAIT_CURSOR));
88 while (groupURLLinksGatherer.isRunning())
93 } catch (InterruptedException e)
98 Desktop.instance.setCursor(oldCursor);
102 public Vector getEnvisionServiceGroupURLS()
105 return groupURLLinks;
111 private static String BACKGROUND = "BACKGROUNDPARAM";
114 * contains null strings or one of the above constants - indicate if this URL
117 private Vector additionalPar = new Vector();
120 * the enfin service URL
122 private String enfinService = null;
124 private String description = null;
126 private String wfname;
131 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
132 * java.lang.String, java.lang.String)
134 public void endElement(String uri, String localName, String qName)
138 // System.err.println("End element: : '"+uri+" "+localName+" "+qName);
139 if (qName.equalsIgnoreCase("workflow") && description != null
140 && description.length() > 0)
142 // 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|,");
143 // 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|,");
144 System.err.println("Adding entry for " + wfname + " " + description);
145 if (wfname.toLowerCase().indexOf("funcnet") == -1)
147 description = Pattern.compile("\\s+", Pattern.MULTILINE)
148 .matcher(description).replaceAll(" ");
149 groupURLdescr.addElement(description);
150 groupURLdescr.addElement(description);
151 String urlstub = wfname;
152 if (wfname.indexOf(" ") > -1)
154 // make the name safe!
157 urlstub = URLEncoder.encode(wfname, "utf-8");
158 } catch (UnsupportedEncodingException e)
160 // TODO Auto-generated catch block
167 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
169 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,"); // #"+description+"#");
173 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
175 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,"); // #"+description+"#");
183 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
185 public void characters(char[] ch, int start, int length)
188 if (description != null)
190 for (int i = start; i < start + length; i++)
192 description += ch[i];
200 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
201 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
203 public void startElement(String uri, String localName, String qName,
204 Attributes attributes) throws SAXException
206 if (qName.equalsIgnoreCase("workflow"))
209 wfname = attributes.getValue("name");
211 if (qName.equalsIgnoreCase("description"))
216 // System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
217 // super.startElement(uri,localName,qname,attributes);
220 private boolean started = false;
222 private boolean running = false;
224 private Vector groupURLLinks = null;
226 private Vector groupURLdescr = null;
228 private static String[] allowedDb = new String[]
229 { "UNIPROT", "EMBL", "PDB" };
231 public EnfinEnvision2OneWay()
233 groupURLLinks = new Vector();
234 groupURLdescr = new Vector();
236 enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE",
237 "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
238 new Thread(this).start();
247 SAXParserFactory spf = SAXParserFactory.newInstance();
248 SAXParser sp = spf.newSAXParser();
249 sp.parse(new URL(enfinService).openStream(), this);
250 } catch (Exception e)
252 Cache.log.warn("Exception when discovering One Way services: ", e);
255 Cache.log.warn("Error when discovering One Way services: ", e);
258 Cache.log.debug("Finished running.");
262 * have we finished running yet ?
264 * @return false if we have been run.
266 public boolean isRunning()
269 // TODO Auto-generated method stub
270 return !started || running;
273 public static void main(String[] args)
276 EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
277 while (ow.isRunning())
282 } catch (Exception e)
288 for (int i = 0; i < ow.groupURLLinks.size(); i++)
290 System.err.println("Description" + ow.groupURLdescr.elementAt(i)
291 + "Service URL: " + ow.groupURLLinks.elementAt(i));
295 // / Copied from jalview.gui.PopupMenu
297 * add a late bound URL service item to the given menu
301 * - menu label string
302 * @param urlgenerator
303 * GroupURLLink used to generate URL
305 * Object array returned from the makeUrlStubs function.
307 private void addshowLink(JMenu linkMenu, String label, String descr,
308 String dbname, final GroupUrlLink urlgenerator,
309 final Object[] urlstub)
311 Component[] jmi = linkMenu.getMenuComponents();
312 for (int i = 0; i < jmi.length; i++)
314 if (jmi[i] instanceof JMenuItem
315 && ((JMenuItem) jmi[i]).getText().equalsIgnoreCase(label))
317 // don't add this - its a repeat of an existing URL.
323 descr = HtmlEncoder.encode(descr);
324 } catch (Exception e)
329 boolean seqsorids = (urlgenerator.getGroupURLType() & urlgenerator.SEQUENCEIDS) == 0;
330 int i = urlgenerator.getNumberInvolved(urlstub);
331 JMenuItem item = new JMenuItem(label);
333 if (dbname == null || dbname.trim().length() == 0)
337 item.setToolTipText("<html>"
338 + JvSwingUtils.wrapTooltip("Submit " + i + " " + dbname + " "
339 + (seqsorids ? "sequence" : "sequence id")
342 + " to<br/>" + descr) + "</html>");
343 item.addActionListener(new java.awt.event.ActionListener()
345 public void actionPerformed(ActionEvent e)
347 new Thread(new Runnable()
354 showLink(urlgenerator.constructFrom(urlstub));
355 } catch (UrlStringTooLongException ex)
357 Cache.log.warn("Not showing link: URL is too long!", ex);
369 * open the given link in a new browser window
373 public void showLink(String url)
377 jalview.util.BrowserLauncher.openURL(url);
378 } catch (Exception ex)
381 .showInternalMessageDialog(
383 "Unixers: Couldn't find default web browser."
384 + "\nAdd the full path to your browser in Preferences.",
385 "Web browser not found", JOptionPane.WARNING_MESSAGE);
387 ex.printStackTrace();
392 * called by a web service menu instance when it is opened.
394 * @param enfinServiceMenu
397 private void buildGroupLinkMenu(JMenu enfinServiceMenu,
398 AlignFrame alignFrame)
400 if (running || !started)
404 SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
405 SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
408 // consider visible regions here/
410 enfinServiceMenu.removeAll();
411 JMenu entries = buildGroupURLMenu(seqs, sg);
414 for (int i = 0, iSize = entries.getMenuComponentCount(); i < iSize; i++)
416 // transfer - menu component is removed from entries automatically
417 enfinServiceMenu.add(entries.getMenuComponent(0));
419 // entries.removeAll();
420 enfinServiceMenu.setEnabled(true);
424 enfinServiceMenu.setEnabled(false);
429 * construct a dynamic enfin services menu given a sequence selection
436 private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
438 if (groupURLdescr == null || groupURLLinks == null)
440 // TODO: usability: thread off the generation of group url content so root
442 // sequence only URLs
443 // ID/regex match URLs
444 JMenu groupLinksMenu = new JMenu("Group Link");
445 String[][] idandseqs = GroupUrlLink.formStrings(seqs);
446 Hashtable commonDbrefs = new Hashtable();
447 for (int sq = 0; sq < seqs.length; sq++)
453 start = seqs[sq].findPosition(sg.getStartRes());
454 end = seqs[sq].findPosition(sg.getEndRes());
458 // get total width of alignment.
459 start = seqs[sq].getStart();
460 end = seqs[sq].findPosition(seqs[sq].getLength());
462 // we skip sequences which do not have any non-gaps in the region of
468 // just collect ids from dataset sequence
469 // TODO: check if IDs collected from selecton group intersects with the
470 // current selection, too
471 SequenceI sqi = seqs[sq];
472 while (sqi.getDatasetSequence() != null)
474 sqi = sqi.getDatasetSequence();
476 DBRefEntry[] dbr = sqi.getDBRef();
477 if (dbr != null && dbr.length > 0)
479 for (int d = 0; d < dbr.length; d++)
481 String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
482 Object[] sarray = (Object[]) commonDbrefs.get(src);
485 sarray = new Object[2];
486 sarray[0] = new int[]
488 sarray[1] = new String[seqs.length];
490 commonDbrefs.put(src, sarray);
493 if (((String[]) sarray[1])[sq] == null)
496 || (dbr[d].getMap().locateMappedRange(start, end) != null))
498 ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
499 ((int[]) sarray[0])[0]++;
505 // now create group links for all distinct ID/sequence sets.
506 Hashtable<String, JMenu[]> gurlMenus = new Hashtable<String, JMenu[]>();
508 * last number of sequences where URL generation failed
510 int[] nsqtype = new int[]
511 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
512 for (int i = 0; i < groupURLLinks.size(); i++)
514 String link = (String) groupURLLinks.elementAt(i);
515 String descr = (String) groupURLdescr.elementAt(i);
517 // boolean specialCase =
518 // additionalPar.elementAt(i).toString().equals(BACKGROUND);
519 GroupUrlLink urlLink = null;
522 urlLink = new GroupUrlLink(link);
523 } catch (Exception foo)
525 jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
530 if (!urlLink.isValid())
532 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
535 final String label = urlLink.getLabel();
536 // create/recover the sub menus that might be populated for this link.
537 JMenu[] wflinkMenus = gurlMenus.get(label);
538 if (wflinkMenus == null)
540 // three types of url that might be
542 wflinkMenus = new JMenu[]
543 { null, new JMenu("IDS"), new JMenu("Sequences"),
544 new JMenu("IDS and Sequences") };
545 gurlMenus.put(label, wflinkMenus);
548 boolean usingNames = false;
549 // Now see which parts of the group apply for this URL
551 String[] seqstr, ids; // input to makeUrl
552 for (int t = 0; t < allowedDb.length; t++)
554 ltarget = allowedDb[t]; // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
555 Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
558 int numinput = ((int[]) idset[0])[0];
559 String[] allids = ((String[]) idset[1]);
560 seqstr = new String[numinput];
561 ids = new String[numinput];
562 if (nsqtype[urlLink.getGroupURLType()] > 0
563 && numinput >= nsqtype[urlLink.getGroupURLType()])
567 for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
569 if (allids[sq] != null)
571 ids[idcount] = allids[sq];
572 seqstr[idcount++] = idandseqs[1][sq];
577 createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
579 } catch (UrlStringTooLongException ex)
581 nsqtype[urlLink.getGroupURLType()] = numinput;
585 // also do names only.
586 seqstr = idandseqs[1];
588 if (nsqtype[urlLink.getGroupURLType()] > 0
589 && idandseqs[0].length >= nsqtype[urlLink.getGroupURLType()])
596 createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
598 } catch (UrlStringTooLongException ex)
600 nsqtype[urlLink.getGroupURLType()] = idandseqs[0].length;
603 boolean anyadded = false; // indicates if there are any group links to give
605 for (Map.Entry<String, JMenu[]> menues : gurlMenus.entrySet())
607 JMenu grouplinkset = new JMenu(menues.getKey());
608 JMenu[] wflinkMenus = menues.getValue();
609 for (int m = 0; m < wflinkMenus.length; m++)
611 if (wflinkMenus[m] != null
612 && wflinkMenus[m].getMenuComponentCount() > 0)
615 grouplinkset.add(wflinkMenus[m]);
618 groupLinksMenu.add(grouplinkset);
622 return groupLinksMenu;
627 private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
628 GroupUrlLink urlLink, String label, String ltarget, String descr,
629 String[] ids, String[] seqstr) throws UrlStringTooLongException
631 Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
632 + System.currentTimeMillis(), false);
636 int type = urlLink.getGroupURLType() & 3;
637 // System.out.println(urlLink.getGroupURLType()
638 // +" "+((String[])urlset[3])[0]);
639 // first two bits ofurlLink type bitfield are sequenceids and sequences
640 // TODO: FUTURE: ensure the groupURL menu structure can be generalised
645 + (ltarget == null ? (((type & 1) == 1 ? "ID"
646 : "Sequence") + (urlLink
647 .getNumberInvolved(urlset) > 1 ? "s" : ""))
648 : (usingNames ? (((type & 1) == 1) ? "(Names)"
650 : ("(" + ltarget + ")"))), descr,
651 usingNames ? null : label, urlLink, urlset);
657 // / end of stuff copied from popupmenu
658 public void attachWSMenuEntry(final JMenu wsmenu,
659 final AlignFrame alignFrame)
661 final JMenu enfinServiceMenu = new JMenu("Envision 2");
662 wsmenu.add(enfinServiceMenu);
663 enfinServiceMenu.setEnabled(false);
664 wsmenu.addMenuListener(new MenuListener()
666 // this listener remembers when the menu was first selected, and
667 // doesn't rebuild the session list until it has been cleared and
669 boolean refresh = true;
671 public void menuCanceled(MenuEvent e)
676 public void menuDeselected(MenuEvent e)
681 public void menuSelected(MenuEvent e)
683 if (refresh && !isRunning())
685 new Thread(new Runnable()
691 buildGroupLinkMenu(enfinServiceMenu, alignFrame);
692 } catch (OutOfMemoryError ex)
695 .error("Out of memory when calculating the Envision2 links.",
697 enfinServiceMenu.setEnabled(false);