2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.BufferedOutputStream;
37 import java.io.BufferedReader;
38 import java.io.InputStreamReader;
39 import java.io.OutputStreamWriter;
40 import java.io.PrintWriter;
41 import java.io.StringWriter;
42 import java.io.UnsupportedEncodingException;
44 import java.net.URLEncoder;
45 import java.nio.charset.Charset;
46 import java.nio.charset.CharsetEncoder;
47 import java.util.Hashtable;
49 import java.util.Vector;
50 import java.util.regex.Pattern;
52 import javax.swing.JMenu;
53 import javax.swing.JMenuItem;
54 import javax.swing.JOptionPane;
55 import javax.swing.event.MenuEvent;
56 import javax.swing.event.MenuListener;
57 import javax.xml.parsers.SAXParser;
58 import javax.xml.parsers.SAXParserFactory;
60 import org.apache.axis.utils.StringUtils;
61 import org.apache.http.client.utils.URLEncodedUtils;
62 import org.apache.http.entity.StringEntity;
63 import org.xml.sax.Attributes;
64 import org.xml.sax.SAXException;
65 import org.xml.sax.helpers.DefaultHandler;
67 import com.lowagie.text.html.HtmlEncoder;
70 * Lightweight runnable to discover dynamic 'one way' group URL services
75 public class EnfinEnvision2OneWay extends DefaultHandler implements
76 Runnable, WSMenuEntryProviderI
78 private static EnfinEnvision2OneWay groupURLLinksGatherer = null;
80 public static EnfinEnvision2OneWay getInstance()
82 if (groupURLLinksGatherer == null)
84 groupURLLinksGatherer = new EnfinEnvision2OneWay();
86 return groupURLLinksGatherer;
89 private void waitForCompletion()
91 if (groupURLLinksGatherer.isRunning())
93 // wait around and show a visual delay indicator
94 Cursor oldCursor = Desktop.instance.getCursor();
95 Desktop.instance.setCursor(new Cursor(Cursor.WAIT_CURSOR));
96 while (groupURLLinksGatherer.isRunning())
101 } catch (InterruptedException e)
106 Desktop.instance.setCursor(oldCursor);
110 public Vector getEnvisionServiceGroupURLS()
113 return groupURLLinks;
119 private static String BACKGROUND = "BACKGROUNDPARAM";
122 * contains null strings or one of the above constants - indicate if this URL
125 private Vector additionalPar = new Vector();
128 * the enfin service URL
130 private String enfinService = null;
132 private String description = null;
134 private String wfname;
139 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
140 * java.lang.String, java.lang.String)
142 public void endElement(String uri, String localName, String qName)
146 // System.err.println("End element: : '"+uri+" "+localName+" "+qName);
147 if (qName.equalsIgnoreCase("workflow") && description != null
148 && description.length() > 0)
150 // 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|,");
151 // 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|,");
152 System.err.println("Adding entry for " + wfname + " " + description);
153 if (wfname.toLowerCase().indexOf("funcnet") == -1)
155 description = Pattern.compile("\\s+",Pattern.MULTILINE).matcher(description).replaceAll(" ");
156 groupURLdescr.addElement(description);
157 groupURLdescr.addElement(description);
158 String urlstub=wfname;
159 if (wfname.indexOf(" ")>-1)
161 // make the name safe!
163 urlstub = URLEncoder.encode(wfname,"utf-8");
164 } catch (UnsupportedEncodingException e) {
165 // TODO Auto-generated catch block
172 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
174 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,"); // #"+description+"#");
178 + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
180 + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,"); // #"+description+"#");
188 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
190 public void characters(char[] ch, int start, int length)
193 if (description != null)
195 for (int i = start; i < start + length; i++)
197 description += ch[i];
205 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
206 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
208 public void startElement(String uri, String localName, String qName,
209 Attributes attributes) throws SAXException
211 if (qName.equalsIgnoreCase("workflow"))
214 wfname = attributes.getValue("name");
216 if (qName.equalsIgnoreCase("description"))
221 // System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
222 // super.startElement(uri,localName,qname,attributes);
225 private boolean started = false;
227 private boolean running = false;
229 private Vector groupURLLinks = null;
231 private Vector groupURLdescr = null;
233 private static String[] allowedDb = new String[]
234 { "UNIPROT", "EMBL", "PDB" };
236 public EnfinEnvision2OneWay()
238 groupURLLinks = new Vector();
239 groupURLdescr = new Vector();
241 enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE",
242 "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
243 new Thread(this).start();
252 SAXParserFactory spf = SAXParserFactory.newInstance();
253 SAXParser sp = spf.newSAXParser();
254 sp.parse(new URL(enfinService).openStream(), this);
255 } catch (Exception e)
257 Cache.log.warn("Exception when discovering One Way services: ", e);
260 Cache.log.warn("Error when discovering One Way services: ", e);
263 Cache.log.debug("Finished running.");
267 * have we finished running yet ?
269 * @return false if we have been run.
271 public boolean isRunning()
274 // TODO Auto-generated method stub
275 return !started || running;
278 public static void main(String[] args)
281 EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
282 while (ow.isRunning())
287 } catch (Exception e)
293 for (int i = 0; i < ow.groupURLLinks.size(); i++)
295 System.err.println("Description" + ow.groupURLdescr.elementAt(i)
296 + "Service URL: " + ow.groupURLLinks.elementAt(i));
300 // / Copied from jalview.gui.PopupMenu
302 * add a late bound URL service item to the given menu
306 * - menu label string
307 * @param urlgenerator
308 * GroupURLLink used to generate URL
310 * Object array returned from the makeUrlStubs function.
312 private void addshowLink(JMenu linkMenu, String label, String descr,
313 String dbname, final GroupUrlLink urlgenerator,
314 final Object[] urlstub)
316 Component[] jmi = linkMenu.getMenuComponents();
317 for (int i = 0; i < jmi.length; i++)
319 if (jmi[i] instanceof JMenuItem
320 && ((JMenuItem) jmi[i]).getText().equalsIgnoreCase(label))
322 // don't add this - its a repeat of an existing URL.
327 descr = HtmlEncoder.encode(descr);
328 } catch (Exception e){};
330 boolean seqsorids = (urlgenerator.getGroupURLType() & urlgenerator.SEQUENCEIDS) == 0;
331 int i = urlgenerator.getNumberInvolved(urlstub);
332 JMenuItem item = new JMenuItem(label);
334 if (dbname == null || dbname.trim().length() == 0)
338 item.setToolTipText("<html>"
339 + JvSwingUtils.wrapTooltip("Submit " + i + " " + dbname + " "
340 + (seqsorids ? "sequence" : "sequence id")
343 + " to<br/>" + descr) + "</html>");
344 item.addActionListener(new java.awt.event.ActionListener()
346 public void actionPerformed(ActionEvent e)
348 new Thread(new Runnable()
355 showLink(urlgenerator.constructFrom(urlstub));
356 } catch (UrlStringTooLongException ex)
358 Cache.log.warn("Not showing link: URL is too long!", ex);
370 * open the given link in a new browser window
374 public void showLink(String url)
378 jalview.util.BrowserLauncher.openURL(url);
379 } catch (Exception ex)
382 .showInternalMessageDialog(
384 "Unixers: Couldn't find default web browser."
385 + "\nAdd the full path to your browser in Preferences.",
386 "Web browser not found", JOptionPane.WARNING_MESSAGE);
388 ex.printStackTrace();
393 * called by a web service menu instance when it is opened.
395 * @param enfinServiceMenu
398 private void buildGroupLinkMenu(JMenu enfinServiceMenu,
399 AlignFrame alignFrame)
401 if (running || !started)
405 SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
406 SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
409 // consider visible regions here/
411 enfinServiceMenu.removeAll();
412 JMenu entries = buildGroupURLMenu(seqs, sg);
415 for (int i = 0, iSize = entries.getMenuComponentCount(); i < iSize; i++)
417 // transfer - menu component is removed from entries automatically
418 enfinServiceMenu.add(entries.getMenuComponent(0));
420 // entries.removeAll();
421 enfinServiceMenu.setEnabled(true);
425 enfinServiceMenu.setEnabled(false);
430 * construct a dynamic enfin services menu given a sequence selection
437 private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
439 if (groupURLdescr == null || groupURLLinks == null)
441 // TODO: usability: thread off the generation of group url content so root
443 // sequence only URLs
444 // ID/regex match URLs
445 JMenu groupLinksMenu = new JMenu("Group Link");
446 String[][] idandseqs = GroupUrlLink.formStrings(seqs);
447 Hashtable commonDbrefs = new Hashtable();
448 for (int sq = 0; sq < seqs.length; sq++)
454 start = seqs[sq].findPosition(sg.getStartRes());
455 end = seqs[sq].findPosition(sg.getEndRes());
459 // get total width of alignment.
460 start = seqs[sq].getStart();
461 end = seqs[sq].findPosition(seqs[sq].getLength());
463 // we skip sequences which do not have any non-gaps in the region of
469 // just collect ids from dataset sequence
470 // TODO: check if IDs collected from selecton group intersects with the
471 // current selection, too
472 SequenceI sqi = seqs[sq];
473 while (sqi.getDatasetSequence() != null)
475 sqi = sqi.getDatasetSequence();
477 DBRefEntry[] dbr = sqi.getDBRef();
478 if (dbr != null && dbr.length > 0)
480 for (int d = 0; d < dbr.length; d++)
482 String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
483 Object[] sarray = (Object[]) commonDbrefs.get(src);
486 sarray = new Object[2];
487 sarray[0] = new int[]
489 sarray[1] = new String[seqs.length];
491 commonDbrefs.put(src, sarray);
494 if (((String[]) sarray[1])[sq] == null)
497 || (dbr[d].getMap().locateMappedRange(start, end) != null))
499 ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
500 ((int[]) sarray[0])[0]++;
506 // now create group links for all distinct ID/sequence sets.
507 Hashtable<String, JMenu[]> gurlMenus = new Hashtable<String, JMenu[]>();
509 * last number of sequences where URL generation failed
511 int[] nsqtype = new int[]
512 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
513 for (int i = 0; i < groupURLLinks.size(); i++)
515 String link = (String) groupURLLinks.elementAt(i);
516 String descr = (String) groupURLdescr.elementAt(i);
518 // boolean specialCase =
519 // additionalPar.elementAt(i).toString().equals(BACKGROUND);
520 GroupUrlLink urlLink = null;
523 urlLink = new GroupUrlLink(link);
524 } catch (Exception foo)
526 jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
531 if (!urlLink.isValid())
533 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
536 final String label = urlLink.getLabel();
537 // create/recover the sub menus that might be populated for this link.
538 JMenu[] wflinkMenus = gurlMenus.get(label);
539 if (wflinkMenus == null)
541 // three types of url that might be
543 wflinkMenus = new JMenu[]
544 { null, new JMenu("IDS"), new JMenu("Sequences"),
545 new JMenu("IDS and Sequences") };
546 gurlMenus.put(label, wflinkMenus);
549 boolean usingNames = false;
550 // Now see which parts of the group apply for this URL
552 String[] seqstr, ids; // input to makeUrl
553 for (int t = 0; t < allowedDb.length; t++)
555 ltarget = allowedDb[t]; // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
556 Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
559 int numinput = ((int[]) idset[0])[0];
560 String[] allids = ((String[]) idset[1]);
561 seqstr = new String[numinput];
562 ids = new String[numinput];
563 if (nsqtype[urlLink.getGroupURLType()] > 0
564 && numinput >= nsqtype[urlLink.getGroupURLType()])
568 for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
570 if (allids[sq] != null)
572 ids[idcount] = allids[sq];
573 seqstr[idcount++] = idandseqs[1][sq];
578 createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
580 } catch (UrlStringTooLongException ex)
582 nsqtype[urlLink.getGroupURLType()] = numinput;
586 // also do names only.
587 seqstr = idandseqs[1];
589 if (nsqtype[urlLink.getGroupURLType()] > 0
590 && idandseqs[0].length >= nsqtype[urlLink.getGroupURLType()])
597 createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
599 } catch (UrlStringTooLongException ex)
601 nsqtype[urlLink.getGroupURLType()] = idandseqs[0].length;
604 boolean anyadded = false; // indicates if there are any group links to give
606 for (Map.Entry<String, JMenu[]> menues : gurlMenus.entrySet())
608 JMenu grouplinkset = new JMenu(menues.getKey());
609 JMenu[] wflinkMenus = menues.getValue();
610 for (int m = 0; m < wflinkMenus.length; m++)
612 if (wflinkMenus[m] != null
613 && wflinkMenus[m].getMenuComponentCount() > 0)
616 grouplinkset.add(wflinkMenus[m]);
619 groupLinksMenu.add(grouplinkset);
623 return groupLinksMenu;
628 private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
629 GroupUrlLink urlLink, String label, String ltarget, String descr,
630 String[] ids, String[] seqstr) throws UrlStringTooLongException
632 Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
633 + System.currentTimeMillis(), false);
637 int type = urlLink.getGroupURLType() & 3;
638 // System.out.println(urlLink.getGroupURLType()
639 // +" "+((String[])urlset[3])[0]);
640 // first two bits ofurlLink type bitfield are sequenceids and sequences
641 // TODO: FUTURE: ensure the groupURL menu structure can be generalised
646 + (ltarget == null ? (((type & 1) == 1 ? "ID"
647 : "Sequence") + (urlLink
648 .getNumberInvolved(urlset) > 1 ? "s" : ""))
649 : (usingNames ? (((type & 1) == 1) ? "(Names)"
651 : ("(" + ltarget + ")"))), descr,
652 usingNames ? null : label, urlLink, urlset);
658 // / end of stuff copied from popupmenu
659 public void attachWSMenuEntry(final JMenu wsmenu,
660 final AlignFrame alignFrame)
662 final JMenu enfinServiceMenu = new JMenu("Envision 2");
663 wsmenu.add(enfinServiceMenu);
664 enfinServiceMenu.setEnabled(false);
665 wsmenu.addMenuListener(new MenuListener()
667 // this listener remembers when the menu was first selected, and
668 // doesn't rebuild the session list until it has been cleared and
670 boolean refresh = true;
672 public void menuCanceled(MenuEvent e)
677 public void menuDeselected(MenuEvent e)
682 public void menuSelected(MenuEvent e)
684 if (refresh && !isRunning())
686 new Thread(new Runnable()
692 buildGroupLinkMenu(enfinServiceMenu, alignFrame);
693 } catch (OutOfMemoryError ex)
696 .error("Out of memory when calculating the Envision2 links.",
698 enfinServiceMenu.setEnabled(false);