2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
21 import java.util.List;
24 import java.awt.event.*;
27 import javax.swing.tree.DefaultMutableTreeNode;
29 import com.stevesoft.pat.Regex;
31 import jalview.datamodel.*;
33 import jalview.util.DBRefUtils;
34 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
35 import jalview.ws.seqfetcher.DbSourceProxy;
36 import java.awt.BorderLayout;
38 public class SequenceFetcher extends JPanel implements Runnable
40 // ASequenceFetcher sfetch;
43 IProgressIndicator guiWindow;
45 AlignFrame alignFrame;
49 final String noDbSelected = "-- Select Database --";
51 private static jalview.ws.SequenceFetcher sfetch = null;
53 private static long lastDasSourceRegistry = -3;
55 private static DasSourceRegistryI dasRegistry = null;
57 private static boolean _initingFetcher = false;
59 private static Thread initingThread = null;
62 * Blocking method that initialises and returns the shared instance of the
63 * SequenceFetcher client
66 * - where the initialisation delay message should be shown
67 * @return the singleton instance of the sequence fetcher client
69 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
70 final IProgressIndicator guiWindow)
72 if (_initingFetcher && initingThread != null && initingThread.isAlive())
74 if (guiWindow != null)
76 guiWindow.setProgressBar(
77 "Waiting for Sequence Database Fetchers to initialise",
78 Thread.currentThread().hashCode());
80 // initting happening on another thread - so wait around to see if it
82 while (_initingFetcher && initingThread != null
83 && initingThread.isAlive())
93 if (guiWindow != null)
95 guiWindow.setProgressBar(
96 "Waiting for Sequence Database Fetchers to initialise",
97 Thread.currentThread().hashCode());
101 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
102 || lastDasSourceRegistry != (jalview.bin.Cache
103 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
104 .getDasSourceRegistry().getLocalSourceString())
107 _initingFetcher = true;
108 initingThread = Thread.currentThread();
110 * give a visual indication that sequence fetcher construction is occuring
112 if (guiWindow != null)
114 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
115 Thread.currentThread().hashCode());
117 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
118 dasRegistry.refreshSources();
120 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
121 if (guiWindow != null)
123 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
124 Thread.currentThread().hashCode());
126 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
127 .getLocalSourceString()).hashCode();
129 _initingFetcher = false;
130 initingThread = null;
135 public SequenceFetcher(IProgressIndicator guiIndic)
137 final IProgressIndicator guiWindow = guiIndic;
138 final SequenceFetcher us = this;
139 // launch initialiser thread
140 Thread sf = new Thread(new Runnable()
146 if (getSequenceFetcherSingleton(guiWindow) != null)
148 us.initGui(guiWindow);
152 javax.swing.SwingUtilities.invokeLater(new Runnable()
158 .showInternalMessageDialog(
160 "Could not create the sequence fetcher client. Check error logs for details.",
161 "Couldn't create SequenceFetcher",
162 JOptionPane.ERROR_MESSAGE);
166 // raise warning dialog
173 private class DatabaseAuthority extends DefaultMutableTreeNode
178 private class DatabaseSource extends DefaultMutableTreeNode
184 * called by thread spawned by constructor
188 private void initGui(IProgressIndicator guiWindow)
190 this.guiWindow = guiWindow;
191 if (guiWindow instanceof AlignFrame)
193 alignFrame = (AlignFrame) guiWindow;
195 database = new JDatabaseTree(sfetch);
199 } catch (Exception ex)
201 ex.printStackTrace();
204 frame = new JInternalFrame();
205 frame.setContentPane(this);
206 if (new jalview.util.Platform().isAMac())
208 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
212 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
216 private String getFrameTitle()
218 return ((alignFrame == null) ? "New " : "Additional ")
219 + "Sequence Fetcher";
222 private void jbInit() throws Exception
224 this.setLayout(borderLayout2);
226 database.setFont(JvSwingUtils.getLabelFont());
227 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
228 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
229 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
230 jLabel1.setText("Separate multiple accession ids with semi colon \";\"");
232 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
234 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
235 replacePunctuation.setText("Replace commas with semi-colons");
237 ok.addActionListener(new ActionListener()
240 public void actionPerformed(ActionEvent e)
242 ok_actionPerformed();
245 clear.setText("Clear");
246 clear.addActionListener(new ActionListener()
249 public void actionPerformed(ActionEvent e)
251 clear_actionPerformed();
255 example.setText("Example");
256 example.addActionListener(new ActionListener()
259 public void actionPerformed(ActionEvent e)
261 example_actionPerformed();
264 close.setText("Close");
265 close.addActionListener(new ActionListener()
268 public void actionPerformed(ActionEvent e)
270 close_actionPerformed(e);
273 textArea.setFont(JvSwingUtils.getLabelFont());
274 textArea.setLineWrap(true);
275 textArea.addKeyListener(new KeyAdapter()
278 public void keyPressed(KeyEvent e)
280 if (e.getKeyCode() == KeyEvent.VK_ENTER)
281 ok_actionPerformed();
284 jPanel3.setLayout(borderLayout1);
285 borderLayout1.setVgap(5);
287 jPanel1.add(example);
290 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
291 jPanel2.setLayout(borderLayout3);
292 databaseButt = database.getDatabaseSelectorButton();
293 databaseButt.setFont(JvSwingUtils.getLabelFont());
294 database.addActionListener(new ActionListener()
298 public void actionPerformed(ActionEvent e)
302 databaseButt.setText(database.getSelectedItem()
303 + (database.getSelectedSources().size() > 1 ? " (and "
304 + database.getSelectedSources().size()
306 String eq = database.getExampleQueries();
307 dbeg.setText("Example query: " + eq);
308 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
309 for (DbSourceProxy dbs : database.getSelectedSources())
311 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
317 replacePunctuation.setEnabled(enablePunct);
319 } catch (Exception ex)
322 replacePunctuation.setEnabled(true);
328 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
329 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
330 JPanel jPanel2a = new JPanel(new BorderLayout());
331 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
332 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
333 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
334 // jPanel2.setPreferredSize(new Dimension())
335 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
336 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
337 this.add(jPanel3, java.awt.BorderLayout.CENTER);
338 this.add(jPanel2, java.awt.BorderLayout.NORTH);
339 jScrollPane1.getViewport().add(textArea);
343 protected void example_actionPerformed()
345 DbSourceProxy db = null;
348 textArea.setText(database.getExampleQueries());
349 } catch (Exception ex)
355 protected void clear_actionPerformed()
357 textArea.setText("");
361 JLabel dbeg = new JLabel();
363 JDatabaseTree database;
365 JButton databaseButt;
367 JLabel jLabel1 = new JLabel();
369 JCheckBox replacePunctuation = new JCheckBox();
371 JButton ok = new JButton();
373 JButton clear = new JButton();
375 JButton example = new JButton();
377 JButton close = new JButton();
379 JPanel jPanel1 = new JPanel();
381 JTextArea textArea = new JTextArea();
383 JScrollPane jScrollPane1 = new JScrollPane();
385 JPanel jPanel2 = new JPanel();
387 JPanel jPanel3 = new JPanel();
389 JPanel jPanel4 = new JPanel();
391 BorderLayout borderLayout1 = new BorderLayout();
393 BorderLayout borderLayout2 = new BorderLayout();
395 BorderLayout borderLayout3 = new BorderLayout();
397 public void close_actionPerformed(ActionEvent e)
401 frame.setClosed(true);
402 } catch (Exception ex)
407 public void ok_actionPerformed()
409 databaseButt.setEnabled(false);
410 example.setEnabled(false);
411 textArea.setEnabled(false);
412 ok.setEnabled(false);
413 close.setEnabled(false);
415 Thread worker = new Thread(this);
419 private void resetDialog()
421 databaseButt.setEnabled(true);
422 example.setEnabled(true);
423 textArea.setEnabled(true);
425 close.setEnabled(true);
432 if (!database.hasSelection())
434 error += "Please select the source database\n";
436 // TODO: make this transformation more configurable
437 com.stevesoft.pat.Regex empty;
438 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
440 empty = new com.stevesoft.pat.Regex(
441 // replace commas and spaces with a semicolon
442 "(\\s|[,; ])+", ";");
446 // just turn spaces and semicolons into single semicolons
447 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
449 textArea.setText(empty.replaceAll(textArea.getText()));
450 // see if there's anthing to search with
451 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
454 error += "Please enter a (semi-colon separated list of) database id(s)";
456 if (error.length() > 0)
458 showErrorMessage(error);
462 // indicate if successive sources should be merged into one alignment.
463 boolean addToLast = false;
464 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
465 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
466 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
469 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
471 Iterator<String> en = Arrays.asList(new String[0]).iterator();
472 int nqueries = qries.length;
473 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
475 if (!en.hasNext() && nextfetch.size() > 0)
477 en = nextfetch.iterator();
478 nqueries = nextfetch.size();
479 // save the remaining queries in the original array
480 qries = nextfetch.toArray(new String[nqueries]);
481 nextfetch = new ArrayList<String>();
484 DbSourceProxy proxy = proxies.next();
485 boolean isAliSource = false;
489 guiWindow.setProgressBar("Fetching " + nqueries
490 + " sequence queries from " + proxy.getDbName(), Thread
491 .currentThread().hashCode());
492 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
493 if (proxy.getAccessionSeparator() == null)
497 String item = en.next();
504 // give the server a chance to breathe
506 } catch (Exception e)
513 AlignmentI indres = null;
516 indres = proxy.getSequenceRecords(item);
517 } catch (OutOfMemoryError oome)
519 new OOMWarning("fetching " + item + " from "
520 + proxy.getDbName(), oome, this);
531 } catch (Exception e)
533 jalview.bin.Cache.log.info("Error retrieving " + item
534 + " from " + proxy.getDbName(), e);
541 StringBuffer multiacc = new StringBuffer();
542 ArrayList<String> tosend = new ArrayList<String>();
545 String nel = en.next();
547 multiacc.append(nel);
550 multiacc.append(proxy.getAccessionSeparator());
557 List<String> nores = new ArrayList<String>();
558 rslt = proxy.getSequenceRecords(multiacc.toString());
559 if (rslt == null || rslt.getHeight() == 0)
561 // no results - pass on all queries to next source
562 nextfetch.addAll(tosend);
566 aresultq.add(multiacc.toString());
569 rs = rslt.getSequencesArray();
570 // search for each query in the dbrefs associated with each
573 // ones we do not find will be used to query next source (if any)
574 for (String q : tosend)
576 DBRefEntry dbr = new DBRefEntry(), found[] = null;
577 dbr.setSource(proxy.getDbSource());
578 dbr.setVersion(null);
579 if (proxy.getAccessionValidator() != null)
581 Regex vgr = proxy.getAccessionValidator();
583 if (vgr.numSubs() > 0)
585 dbr.setAccessionId(vgr.stringMatched(1));
589 dbr.setAccessionId(vgr.stringMatched());
594 dbr.setAccessionId(q);
596 boolean rfound = false;
597 for (int r = 0; r < rs.length; r++)
600 && (found = DBRefUtils.searchRefs(
601 rs[r].getDBRef(), dbr)) != null
615 } catch (OutOfMemoryError oome)
617 new OOMWarning("fetching " + multiacc + " from "
618 + database.getSelectedItem(), oome, this);
622 } catch (Exception e)
624 showErrorMessage("Error retrieving " + textArea.getText()
625 + " from " + database.getSelectedItem());
627 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
628 System.err.println("Retrieval failed for source ='"
629 + database.getSelectedItem() + "' and query\n'"
630 + textArea.getText() + "'\n");
632 } catch (OutOfMemoryError e)
634 // resets dialog box - so we don't use OOMwarning here.
635 showErrorMessage("Out of Memory when retrieving "
638 + database.getSelectedItem()
639 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
643 showErrorMessage("Serious Error retrieving " + textArea.getText()
644 + " from " + database.getSelectedItem());
647 // Stack results ready for opening in alignment windows
648 if (aresult != null && aresult.size() > 0)
650 AlignmentI ar = null;
654 // new window for each result
655 while (aresult.size() > 0)
657 presult.add(aresult.remove(0));
658 presultTitle.add(aresultq.remove(0) + " "
659 + getDefaultRetrievalTitle());
665 if (addToLast && presult.size() > 0)
667 ar = presult.remove(presult.size() - 1);
668 titl = presultTitle.remove(presultTitle.size() - 1);
670 // concatenate all results in one window
671 while (aresult.size() > 0)
675 ar = aresult.remove(0);
679 ar.append(aresult.remove(0));
685 presultTitle.add(titl);
688 guiWindow.setProgressBar("Finished querying", Thread.currentThread()
691 guiWindow.setProgressBar((presult.size() > 0) ? "Parsing results."
692 : "Processing ..", Thread.currentThread().hashCode());
694 while (presult.size() > 0)
696 parseResult(presult.remove(0), presultTitle.remove(0), null);
698 // only remove visual delay after we finished parsing.
699 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
700 if (nextfetch.size() > 0)
702 StringBuffer sb = new StringBuffer();
703 sb.append("Didn't retrieve the following "
704 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
705 + " queries") + ": \n");
706 int l = sb.length(), lr = 0;
707 for (String s : nextfetch)
709 if (l != sb.length())
713 if (lr - sb.length() > 40)
719 showErrorMessage(sb.toString());
724 AlignmentI parseResult(String result, String title)
726 String format = new IdentifyFile().Identify(result, "Paste");
727 Alignment sequences = null;
728 if (FormatAdapter.isValidFormat(format))
733 sequences = new FormatAdapter().readFile(result.toString(),
735 } catch (Exception ex)
739 if (sequences != null)
741 return parseResult(sequences, title, format);
746 showErrorMessage("Error retrieving " + textArea.getText() + " from "
747 + database.getSelectedItem());
755 * @return a standard title for any results retrieved using the currently
756 * selected source and settings
758 public String getDefaultRetrievalTitle()
760 return "Retrieved from " + database.getSelectedItem();
763 AlignmentI parseResult(AlignmentI al, String title,
764 String currentFileFormat)
767 if (al != null && al.getHeight() > 0)
769 if (alignFrame == null)
771 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
772 AlignFrame.DEFAULT_HEIGHT);
773 if (currentFileFormat != null)
775 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
777 // NON-FormatAdapter Sourced
783 title = getDefaultRetrievalTitle();
785 SequenceFeature[] sfs = null;
786 List<SequenceI> alsqs;
787 synchronized (alsqs = al.getSequences())
789 for (SequenceI sq : alsqs)
791 if ((sfs = (sq).getDatasetSequence().getSequenceFeatures()) != null)
795 af.setShowSeqFeatures(true);
802 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
803 AlignFrame.DEFAULT_HEIGHT);
805 af.statusBar.setText("Successfully pasted alignment file");
809 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
811 } catch (Exception ex)
817 for (int i = 0; i < al.getHeight(); i++)
819 alignFrame.viewport.getAlignment().addSequence(
820 al.getSequenceAt(i)); // this
827 alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
829 alignFrame.viewport.getAlignment().getWidth();
830 alignFrame.viewport.firePropertyChange("alignment", null,
831 alignFrame.viewport.getAlignment().getSequences());
837 void showErrorMessage(final String error)
840 javax.swing.SwingUtilities.invokeLater(new Runnable()
845 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
846 "Error Retrieving Data", JOptionPane.WARNING_MESSAGE);