2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 import java.util.List;
25 import java.awt.event.*;
28 import javax.swing.tree.DefaultMutableTreeNode;
30 import com.stevesoft.pat.Regex;
32 import jalview.datamodel.*;
34 import jalview.util.DBRefUtils;
35 import jalview.util.MessageManager;
36 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
37 import jalview.ws.seqfetcher.DbSourceProxy;
38 import java.awt.BorderLayout;
40 public class SequenceFetcher extends JPanel implements Runnable
42 // ASequenceFetcher sfetch;
45 IProgressIndicator guiWindow;
47 AlignFrame alignFrame;
51 final String noDbSelected = "-- Select Database --";
53 private static jalview.ws.SequenceFetcher sfetch = null;
55 private static long lastDasSourceRegistry = -3;
57 private static DasSourceRegistryI dasRegistry = null;
59 private static boolean _initingFetcher = false;
61 private static Thread initingThread = null;
64 * Blocking method that initialises and returns the shared instance of the
65 * SequenceFetcher client
68 * - where the initialisation delay message should be shown
69 * @return the singleton instance of the sequence fetcher client
71 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
72 final IProgressIndicator guiWindow)
74 if (_initingFetcher && initingThread != null && initingThread.isAlive())
76 if (guiWindow != null)
78 guiWindow.setProgressBar(
79 "Waiting for Sequence Database Fetchers to initialise",
80 Thread.currentThread().hashCode());
82 // initting happening on another thread - so wait around to see if it
84 while (_initingFetcher && initingThread != null
85 && initingThread.isAlive())
95 if (guiWindow != null)
97 guiWindow.setProgressBar(
98 "Waiting for Sequence Database Fetchers to initialise",
99 Thread.currentThread().hashCode());
103 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
104 || lastDasSourceRegistry != (jalview.bin.Cache
105 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
106 .getDasSourceRegistry().getLocalSourceString())
109 _initingFetcher = true;
110 initingThread = Thread.currentThread();
112 * give a visual indication that sequence fetcher construction is occuring
114 if (guiWindow != null)
116 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
117 Thread.currentThread().hashCode());
119 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
120 dasRegistry.refreshSources();
122 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
123 if (guiWindow != null)
125 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
126 Thread.currentThread().hashCode());
128 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
129 .getLocalSourceString()).hashCode();
131 _initingFetcher = false;
132 initingThread = null;
137 public SequenceFetcher(IProgressIndicator guiIndic)
139 final IProgressIndicator guiWindow = guiIndic;
140 final SequenceFetcher us = this;
141 // launch initialiser thread
142 Thread sf = new Thread(new Runnable()
148 if (getSequenceFetcherSingleton(guiWindow) != null)
150 us.initGui(guiWindow);
154 javax.swing.SwingUtilities.invokeLater(new Runnable()
160 .showInternalMessageDialog(
162 "Could not create the sequence fetcher client. Check error logs for details.",
163 "Couldn't create SequenceFetcher",
164 JOptionPane.ERROR_MESSAGE);
168 // raise warning dialog
175 private class DatabaseAuthority extends DefaultMutableTreeNode
180 private class DatabaseSource extends DefaultMutableTreeNode
186 * called by thread spawned by constructor
190 private void initGui(IProgressIndicator guiWindow)
192 this.guiWindow = guiWindow;
193 if (guiWindow instanceof AlignFrame)
195 alignFrame = (AlignFrame) guiWindow;
197 database = new JDatabaseTree(sfetch);
201 } catch (Exception ex)
203 ex.printStackTrace();
206 frame = new JInternalFrame();
207 frame.setContentPane(this);
208 if (new jalview.util.Platform().isAMac())
210 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
214 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
218 private String getFrameTitle()
220 return ((alignFrame == null) ? "New " : "Additional ")
221 + "Sequence Fetcher";
224 private void jbInit() throws Exception
226 this.setLayout(borderLayout2);
228 database.setFont(JvSwingUtils.getLabelFont());
229 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
230 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
231 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
232 jLabel1.setText(MessageManager.getString("label.separate_multiple_accession_ids"));
234 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
236 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
237 replacePunctuation.setText(MessageManager.getString("label.replace_commas_semicolons"));
238 ok.setText(MessageManager.getString("action.ok"));
239 ok.addActionListener(new ActionListener()
242 public void actionPerformed(ActionEvent e)
244 ok_actionPerformed();
247 clear.setText(MessageManager.getString("action.clear"));
248 clear.addActionListener(new ActionListener()
251 public void actionPerformed(ActionEvent e)
253 clear_actionPerformed();
257 example.setText(MessageManager.getString("label.example"));
258 example.addActionListener(new ActionListener()
261 public void actionPerformed(ActionEvent e)
263 example_actionPerformed();
266 close.setText(MessageManager.getString("action.close"));
267 close.addActionListener(new ActionListener()
270 public void actionPerformed(ActionEvent e)
272 close_actionPerformed(e);
275 textArea.setFont(JvSwingUtils.getLabelFont());
276 textArea.setLineWrap(true);
277 textArea.addKeyListener(new KeyAdapter()
280 public void keyPressed(KeyEvent e)
282 if (e.getKeyCode() == KeyEvent.VK_ENTER)
283 ok_actionPerformed();
286 jPanel3.setLayout(borderLayout1);
287 borderLayout1.setVgap(5);
289 jPanel1.add(example);
292 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
293 jPanel2.setLayout(borderLayout3);
294 databaseButt = database.getDatabaseSelectorButton();
295 databaseButt.setFont(JvSwingUtils.getLabelFont());
296 database.addActionListener(new ActionListener()
300 public void actionPerformed(ActionEvent e)
304 databaseButt.setText(database.getSelectedItem()
305 + (database.getSelectedSources().size() > 1 ? " (and "
306 + database.getSelectedSources().size()
308 String eq = database.getExampleQueries();
309 dbeg.setText(MessageManager.formatMessage("label.example_query_param", new String[]{eq}));
310 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
311 for (DbSourceProxy dbs : database.getSelectedSources())
313 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
319 replacePunctuation.setEnabled(enablePunct);
321 } catch (Exception ex)
324 replacePunctuation.setEnabled(true);
330 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
331 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
332 JPanel jPanel2a = new JPanel(new BorderLayout());
333 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
334 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
335 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
336 // jPanel2.setPreferredSize(new Dimension())
337 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
338 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
339 this.add(jPanel3, java.awt.BorderLayout.CENTER);
340 this.add(jPanel2, java.awt.BorderLayout.NORTH);
341 jScrollPane1.getViewport().add(textArea);
345 protected void example_actionPerformed()
347 DbSourceProxy db = null;
350 textArea.setText(database.getExampleQueries());
351 } catch (Exception ex)
357 protected void clear_actionPerformed()
359 textArea.setText("");
363 JLabel dbeg = new JLabel();
365 JDatabaseTree database;
367 JButton databaseButt;
369 JLabel jLabel1 = new JLabel();
371 JCheckBox replacePunctuation = new JCheckBox();
373 JButton ok = new JButton();
375 JButton clear = new JButton();
377 JButton example = new JButton();
379 JButton close = new JButton();
381 JPanel jPanel1 = new JPanel();
383 JTextArea textArea = new JTextArea();
385 JScrollPane jScrollPane1 = new JScrollPane();
387 JPanel jPanel2 = new JPanel();
389 JPanel jPanel3 = new JPanel();
391 JPanel jPanel4 = new JPanel();
393 BorderLayout borderLayout1 = new BorderLayout();
395 BorderLayout borderLayout2 = new BorderLayout();
397 BorderLayout borderLayout3 = new BorderLayout();
399 public void close_actionPerformed(ActionEvent e)
403 frame.setClosed(true);
404 } catch (Exception ex)
409 public void ok_actionPerformed()
411 databaseButt.setEnabled(false);
412 example.setEnabled(false);
413 textArea.setEnabled(false);
414 ok.setEnabled(false);
415 close.setEnabled(false);
417 Thread worker = new Thread(this);
421 private void resetDialog()
423 databaseButt.setEnabled(true);
424 example.setEnabled(true);
425 textArea.setEnabled(true);
427 close.setEnabled(true);
434 if (!database.hasSelection())
436 error += "Please select the source database\n";
438 // TODO: make this transformation more configurable
439 com.stevesoft.pat.Regex empty;
440 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
442 empty = new com.stevesoft.pat.Regex(
443 // replace commas and spaces with a semicolon
444 "(\\s|[,; ])+", ";");
448 // just turn spaces and semicolons into single semicolons
449 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
451 textArea.setText(empty.replaceAll(textArea.getText()));
452 // see if there's anthing to search with
453 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
456 error += "Please enter a (semi-colon separated list of) database id(s)";
458 if (error.length() > 0)
460 showErrorMessage(error);
464 // indicate if successive sources should be merged into one alignment.
465 boolean addToLast = false;
466 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
467 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
468 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
471 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
473 Iterator<String> en = Arrays.asList(new String[0]).iterator();
474 int nqueries = qries.length;
475 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
477 if (!en.hasNext() && nextfetch.size() > 0)
479 en = nextfetch.iterator();
480 nqueries = nextfetch.size();
481 // save the remaining queries in the original array
482 qries = nextfetch.toArray(new String[nqueries]);
483 nextfetch = new ArrayList<String>();
486 DbSourceProxy proxy = proxies.next();
487 boolean isAliSource = false;
491 guiWindow.setProgressBar("Fetching " + nqueries
492 + " sequence queries from " + proxy.getDbName(), Thread
493 .currentThread().hashCode());
494 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
495 if (proxy.getAccessionSeparator() == null)
499 String item = en.next();
506 // give the server a chance to breathe
508 } catch (Exception e)
515 AlignmentI indres = null;
518 indres = proxy.getSequenceRecords(item);
519 } catch (OutOfMemoryError oome)
521 new OOMWarning("fetching " + item + " from "
522 + proxy.getDbName(), oome, this);
533 } catch (Exception e)
535 jalview.bin.Cache.log.info("Error retrieving " + item
536 + " from " + proxy.getDbName(), e);
543 StringBuffer multiacc = new StringBuffer();
544 ArrayList<String> tosend = new ArrayList<String>();
547 String nel = en.next();
549 multiacc.append(nel);
552 multiacc.append(proxy.getAccessionSeparator());
559 List<String> nores = new ArrayList<String>();
560 rslt = proxy.getSequenceRecords(multiacc.toString());
561 if (rslt == null || rslt.getHeight() == 0)
563 // no results - pass on all queries to next source
564 nextfetch.addAll(tosend);
568 aresultq.add(multiacc.toString());
571 rs = rslt.getSequencesArray();
572 // search for each query in the dbrefs associated with each
575 // ones we do not find will be used to query next source (if any)
576 for (String q : tosend)
578 DBRefEntry dbr = new DBRefEntry(), found[] = null;
579 dbr.setSource(proxy.getDbSource());
580 dbr.setVersion(null);
581 if (proxy.getAccessionValidator() != null)
583 Regex vgr = proxy.getAccessionValidator();
585 if (vgr.numSubs() > 0)
587 dbr.setAccessionId(vgr.stringMatched(1));
591 dbr.setAccessionId(vgr.stringMatched());
596 dbr.setAccessionId(q);
598 boolean rfound = false;
599 for (int r = 0; r < rs.length; r++)
602 && (found = DBRefUtils.searchRefs(
603 rs[r].getDBRef(), dbr)) != null
617 } catch (OutOfMemoryError oome)
619 new OOMWarning("fetching " + multiacc + " from "
620 + database.getSelectedItem(), oome, this);
624 } catch (Exception e)
626 showErrorMessage("Error retrieving " + textArea.getText()
627 + " from " + database.getSelectedItem());
629 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
630 System.err.println("Retrieval failed for source ='"
631 + database.getSelectedItem() + "' and query\n'"
632 + textArea.getText() + "'\n");
634 } catch (OutOfMemoryError e)
636 // resets dialog box - so we don't use OOMwarning here.
637 showErrorMessage("Out of Memory when retrieving "
640 + database.getSelectedItem()
641 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
645 showErrorMessage("Serious Error retrieving " + textArea.getText()
646 + " from " + database.getSelectedItem());
649 // Stack results ready for opening in alignment windows
650 if (aresult != null && aresult.size() > 0)
652 AlignmentI ar = null;
656 // new window for each result
657 while (aresult.size() > 0)
659 presult.add(aresult.remove(0));
660 presultTitle.add(aresultq.remove(0) + " "
661 + getDefaultRetrievalTitle());
667 if (addToLast && presult.size() > 0)
669 ar = presult.remove(presult.size() - 1);
670 titl = presultTitle.remove(presultTitle.size() - 1);
672 // concatenate all results in one window
673 while (aresult.size() > 0)
677 ar = aresult.remove(0);
681 ar.append(aresult.remove(0));
687 presultTitle.add(titl);
690 guiWindow.setProgressBar("Finished querying", Thread.currentThread()
693 guiWindow.setProgressBar((presult.size() > 0) ? "Parsing results."
694 : "Processing ..", Thread.currentThread().hashCode());
696 while (presult.size() > 0)
698 parseResult(presult.remove(0), presultTitle.remove(0), null);
700 // only remove visual delay after we finished parsing.
701 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
702 if (nextfetch.size() > 0)
704 StringBuffer sb = new StringBuffer();
705 sb.append("Didn't retrieve the following "
706 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
707 + " queries") + ": \n");
708 int l = sb.length(), lr = 0;
709 for (String s : nextfetch)
711 if (l != sb.length())
715 if (lr - sb.length() > 40)
721 showErrorMessage(sb.toString());
726 AlignmentI parseResult(String result, String title)
728 String format = new IdentifyFile().Identify(result, "Paste");
729 Alignment sequences = null;
730 if (FormatAdapter.isValidFormat(format))
735 sequences = new FormatAdapter().readFile(result.toString(),
737 } catch (Exception ex)
741 if (sequences != null)
743 return parseResult(sequences, title, format);
748 showErrorMessage("Error retrieving " + textArea.getText() + " from "
749 + database.getSelectedItem());
757 * @return a standard title for any results retrieved using the currently
758 * selected source and settings
760 public String getDefaultRetrievalTitle()
762 return "Retrieved from " + database.getSelectedItem();
765 AlignmentI parseResult(AlignmentI al, String title,
766 String currentFileFormat)
769 if (al != null && al.getHeight() > 0)
771 if (alignFrame == null)
773 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
774 AlignFrame.DEFAULT_HEIGHT);
775 if (currentFileFormat != null)
777 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
779 // NON-FormatAdapter Sourced
785 title = getDefaultRetrievalTitle();
787 SequenceFeature[] sfs = null;
788 List<SequenceI> alsqs;
789 synchronized (alsqs = al.getSequences())
791 for (SequenceI sq : alsqs)
793 if ((sfs = (sq).getDatasetSequence().getSequenceFeatures()) != null)
797 af.setShowSeqFeatures(true);
804 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
805 AlignFrame.DEFAULT_HEIGHT);
807 af.statusBar.setText(MessageManager.getString("label.successfully_pasted_alignment_file"));
811 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
813 } catch (Exception ex)
819 for (int i = 0; i < al.getHeight(); i++)
821 alignFrame.viewport.getAlignment().addSequence(
822 al.getSequenceAt(i)); // this
829 alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
831 alignFrame.viewport.getAlignment().getWidth();
832 alignFrame.viewport.firePropertyChange("alignment", null,
833 alignFrame.viewport.getAlignment().getSequences());
839 void showErrorMessage(final String error)
842 javax.swing.SwingUtilities.invokeLater(new Runnable()
847 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
848 MessageManager.getString("label.error_retrieving_data"), JOptionPane.WARNING_MESSAGE);