2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.datamodel.AlignmentI;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.datamodel.DBRefSource;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.io.FormatAdapter;
29 import jalview.io.IdentifyFile;
30 import jalview.util.DBRefUtils;
31 import jalview.util.MessageManager;
32 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
33 import jalview.ws.seqfetcher.DbSourceProxy;
35 import java.awt.BorderLayout;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.KeyAdapter;
40 import java.awt.event.KeyEvent;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Iterator;
44 import java.util.List;
46 import javax.swing.JButton;
47 import javax.swing.JCheckBox;
48 import javax.swing.JInternalFrame;
49 import javax.swing.JLabel;
50 import javax.swing.JOptionPane;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollPane;
53 import javax.swing.JTextArea;
54 import javax.swing.SwingConstants;
55 import javax.swing.tree.DefaultMutableTreeNode;
57 public class SequenceFetcher extends JPanel implements Runnable
59 JLabel dbeg = new JLabel();
61 JDatabaseTree database;
65 JLabel jLabel1 = new JLabel();
67 JCheckBox replacePunctuation = new JCheckBox();
69 JButton ok = new JButton();
71 JButton clear = new JButton();
73 JButton example = new JButton();
75 JButton close = new JButton();
77 JPanel jPanel1 = new JPanel();
79 JTextArea textArea = new JTextArea();
81 JScrollPane jScrollPane1 = new JScrollPane();
83 JPanel jPanel2 = new JPanel();
85 JPanel jPanel3 = new JPanel();
87 JPanel jPanel4 = new JPanel();
89 BorderLayout borderLayout1 = new BorderLayout();
91 BorderLayout borderLayout2 = new BorderLayout();
93 BorderLayout borderLayout3 = new BorderLayout();
97 IProgressIndicator guiWindow;
99 AlignFrame alignFrame;
103 final String noDbSelected = "-- Select Database --";
105 private static jalview.ws.SequenceFetcher sfetch = null;
107 private static long lastDasSourceRegistry = -3;
109 private static DasSourceRegistryI dasRegistry = null;
111 private static boolean _initingFetcher = false;
113 private static Thread initingThread = null;
115 int debounceTrap = 0;
117 * Blocking method that initialises and returns the shared instance of the
118 * SequenceFetcher client
121 * - where the initialisation delay message should be shown
122 * @return the singleton instance of the sequence fetcher client
124 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
125 final IProgressIndicator guiWindow)
127 if (_initingFetcher && initingThread != null && initingThread.isAlive())
129 if (guiWindow != null)
131 guiWindow.setProgressBar(
132 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
133 Thread.currentThread().hashCode());
135 // initting happening on another thread - so wait around to see if it
137 while (_initingFetcher && initingThread != null
138 && initingThread.isAlive())
143 } catch (Exception e)
148 if (guiWindow != null)
150 guiWindow.setProgressBar(
151 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
152 Thread.currentThread().hashCode());
156 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
157 || lastDasSourceRegistry != (jalview.bin.Cache
158 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
159 .getDasSourceRegistry().getLocalSourceString())
162 _initingFetcher = true;
163 initingThread = Thread.currentThread();
165 * give a visual indication that sequence fetcher construction is occuring
167 if (guiWindow != null)
169 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
170 Thread.currentThread().hashCode());
172 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
173 dasRegistry.refreshSources();
175 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
176 if (guiWindow != null)
178 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
180 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
181 .getLocalSourceString()).hashCode();
183 _initingFetcher = false;
184 initingThread = null;
189 private IProgressIndicator progressIndicator;
190 public SequenceFetcher(IProgressIndicator guiIndic)
192 this.progressIndicator = guiIndic;
193 final SequenceFetcher us = this;
194 // launch initialiser thread
195 Thread sf = new Thread(new Runnable()
201 if (getSequenceFetcherSingleton(progressIndicator) != null)
203 us.initGui(progressIndicator);
207 javax.swing.SwingUtilities.invokeLater(new Runnable()
213 .showInternalMessageDialog(
215 MessageManager.getString("warn.couldnt_create_sequence_fetcher_client"),
216 MessageManager.getString("label.couldnt_create_sequence_fetcher"),
217 JOptionPane.ERROR_MESSAGE);
221 // raise warning dialog
228 private class DatabaseAuthority extends DefaultMutableTreeNode
233 private class DatabaseSource extends DefaultMutableTreeNode
239 * called by thread spawned by constructor
243 private void initGui(IProgressIndicator guiWindow)
245 this.guiWindow = guiWindow;
246 if (guiWindow instanceof AlignFrame)
248 alignFrame = (AlignFrame) guiWindow;
250 database = new JDatabaseTree(sfetch);
254 } catch (Exception ex)
256 ex.printStackTrace();
259 frame = new JInternalFrame();
260 frame.setContentPane(this);
261 if (new jalview.util.Platform().isAMac())
263 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
267 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
271 private String getFrameTitle()
273 return ((alignFrame == null) ? MessageManager.getString("label.new_sequence_fetcher") : MessageManager.getString("label.additional_sequence_fetcher"));
276 private void jbInit() throws Exception
278 this.setLayout(borderLayout2);
280 database.setFont(JvSwingUtils.getLabelFont());
281 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
282 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
283 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
284 jLabel1.setText(MessageManager
285 .getString("label.separate_multiple_accession_ids"));
287 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
289 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
290 replacePunctuation.setText(MessageManager
291 .getString("label.replace_commas_semicolons"));
292 ok.setText(MessageManager.getString("action.ok"));
293 ok.addActionListener(new ActionListener()
296 public void actionPerformed(ActionEvent e)
298 ok_actionPerformed();
301 clear.setText(MessageManager.getString("action.clear"));
302 clear.addActionListener(new ActionListener()
305 public void actionPerformed(ActionEvent e)
307 clear_actionPerformed();
311 example.setText(MessageManager.getString("label.example"));
312 example.addActionListener(new ActionListener()
315 public void actionPerformed(ActionEvent e)
317 example_actionPerformed();
320 close.setText(MessageManager.getString("action.close"));
321 close.addActionListener(new ActionListener()
324 public void actionPerformed(ActionEvent e)
326 close_actionPerformed(e);
329 textArea.setFont(JvSwingUtils.getLabelFont());
330 textArea.setLineWrap(true);
331 textArea.addKeyListener(new KeyAdapter()
334 public void keyPressed(KeyEvent e)
336 if (e.getKeyCode() == KeyEvent.VK_ENTER)
338 ok_actionPerformed();
342 jPanel3.setLayout(borderLayout1);
343 borderLayout1.setVgap(5);
345 jPanel1.add(example);
348 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
349 jPanel2.setLayout(borderLayout3);
350 databaseButt = database.getDatabaseSelectorButton();
351 databaseButt.setFont(JvSwingUtils.getLabelFont());
352 database.addActionListener(new ActionListener()
355 public void actionPerformed(ActionEvent e)
358 String currentSelection = database.getSelectedItem();
360 if (!currentSelection.equalsIgnoreCase("pdb"))
364 if (currentSelection.equalsIgnoreCase("pdb") && ((debounceTrap % 2) == 0))
372 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
373 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
374 JPanel jPanel2a = new JPanel(new BorderLayout());
375 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
376 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
377 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
378 // jPanel2.setPreferredSize(new Dimension())
379 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
380 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
381 this.add(jPanel3, java.awt.BorderLayout.CENTER);
382 this.add(jPanel2, java.awt.BorderLayout.NORTH);
383 jScrollPane1.getViewport().add(textArea);
387 private void pdbSourceAction()
389 databaseButt.setText(database.getSelectedItem());
390 new PDBSearchPanel(this);
394 private void otherSourceAction()
398 databaseButt.setText(database.getSelectedItem()
399 + (database.getSelectedSources().size() > 1 ? " (and "
400 + database.getSelectedSources().size() + " others)"
402 String eq = database.getExampleQueries();
403 dbeg.setText(MessageManager.formatMessage(
404 "label.example_query_param", new String[]
406 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
407 for (DbSourceProxy dbs : database.getSelectedSources())
409 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
415 replacePunctuation.setEnabled(enablePunct);
417 } catch (Exception ex)
420 replacePunctuation.setEnabled(true);
425 protected void example_actionPerformed()
427 DbSourceProxy db = null;
430 textArea.setText(database.getExampleQueries());
431 } catch (Exception ex)
437 protected void clear_actionPerformed()
439 textArea.setText("");
445 public void close_actionPerformed(ActionEvent e)
449 frame.setClosed(true);
450 } catch (Exception ex)
455 public void ok_actionPerformed()
457 databaseButt.setEnabled(false);
458 example.setEnabled(false);
459 textArea.setEnabled(false);
460 ok.setEnabled(false);
461 close.setEnabled(false);
463 Thread worker = new Thread(this);
467 private void resetDialog()
469 databaseButt.setEnabled(true);
470 example.setEnabled(true);
471 textArea.setEnabled(true);
473 close.setEnabled(true);
480 if (!database.hasSelection())
482 error += "Please select the source database\n";
484 // TODO: make this transformation more configurable
485 com.stevesoft.pat.Regex empty;
486 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
488 empty = new com.stevesoft.pat.Regex(
489 // replace commas and spaces with a semicolon
490 "(\\s|[,; ])+", ";");
494 // just turn spaces and semicolons into single semicolons
495 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
497 textArea.setText(empty.replaceAll(textArea.getText()));
498 // see if there's anthing to search with
499 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
502 error += "Please enter a (semi-colon separated list of) database id(s)";
504 if (error.length() > 0)
506 showErrorMessage(error);
510 // TODO: Refactor to GUI independent code and write tests.
511 // indicate if successive sources should be merged into one alignment.
512 boolean addToLast = false;
513 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
514 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
515 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
518 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
520 Iterator<String> en = Arrays.asList(new String[0]).iterator();
521 int nqueries = qries.length;
522 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
524 if (!en.hasNext() && nextfetch.size() > 0)
526 en = nextfetch.iterator();
527 nqueries = nextfetch.size();
528 // save the remaining queries in the original array
529 qries = nextfetch.toArray(new String[nqueries]);
530 nextfetch = new ArrayList<String>();
533 DbSourceProxy proxy = proxies.next();
534 boolean isAliSource = false;
538 guiWindow.setProgressBar(MessageManager.formatMessage("status.fetching_sequence_queries_from", new String[]{Integer.valueOf(nqueries).toString(),proxy.getDbName()}), Thread
539 .currentThread().hashCode());
540 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
541 if (proxy.getAccessionSeparator() == null)
545 String item = en.next();
552 // give the server a chance to breathe
554 } catch (Exception e)
561 AlignmentI indres = null;
564 indres = proxy.getSequenceRecords(item);
565 } catch (OutOfMemoryError oome)
567 new OOMWarning("fetching " + item + " from "
568 + proxy.getDbName(), oome, this);
579 } catch (Exception e)
581 jalview.bin.Cache.log.info("Error retrieving " + item
582 + " from " + proxy.getDbName(), e);
589 StringBuffer multiacc = new StringBuffer();
590 ArrayList<String> tosend = new ArrayList<String>();
593 String nel = en.next();
595 multiacc.append(nel);
598 multiacc.append(proxy.getAccessionSeparator());
605 List<String> nores = new ArrayList<String>();
606 rslt = proxy.getSequenceRecords(multiacc.toString());
607 if (rslt == null || rslt.getHeight() == 0)
609 // no results - pass on all queries to next source
610 nextfetch.addAll(tosend);
614 aresultq.add(multiacc.toString());
617 rs = rslt.getSequencesArray();
618 // search for each query in the dbrefs associated with each
621 // ones we do not find will be used to query next source (if any)
622 for (String q : tosend)
624 DBRefEntry dbr = new DBRefEntry(), found[] = null;
625 dbr.setSource(proxy.getDbSource());
626 dbr.setVersion(null);
627 String accId = DBRefUtils.processQueryToAccessionFor(proxy,
629 dbr.setAccessionId(accId);
630 boolean rfound = false;
631 for (int r = 0; r < rs.length; r++)
634 && (found = DBRefUtils.searchRefs(
635 rs[r].getDBRef(), dbr)) != null
649 } catch (OutOfMemoryError oome)
651 new OOMWarning("fetching " + multiacc + " from "
652 + database.getSelectedItem(), oome, this);
656 } catch (Exception e)
658 showErrorMessage("Error retrieving " + textArea.getText()
659 + " from " + database.getSelectedItem());
661 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
662 System.err.println("Retrieval failed for source ='"
663 + database.getSelectedItem() + "' and query\n'"
664 + textArea.getText() + "'\n");
666 } catch (OutOfMemoryError e)
668 // resets dialog box - so we don't use OOMwarning here.
669 showErrorMessage("Out of Memory when retrieving "
672 + database.getSelectedItem()
673 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
677 showErrorMessage("Serious Error retrieving " + textArea.getText()
678 + " from " + database.getSelectedItem());
681 // Stack results ready for opening in alignment windows
682 if (aresult != null && aresult.size() > 0)
684 AlignmentI ar = null;
688 // new window for each result
689 while (aresult.size() > 0)
691 presult.add(aresult.remove(0));
692 presultTitle.add(aresultq.remove(0) + " "
693 + getDefaultRetrievalTitle());
699 if (addToLast && presult.size() > 0)
701 ar = presult.remove(presult.size() - 1);
702 titl = presultTitle.remove(presultTitle.size() - 1);
704 // concatenate all results in one window
705 while (aresult.size() > 0)
709 ar = aresult.remove(0);
713 ar.append(aresult.remove(0));
719 presultTitle.add(titl);
722 guiWindow.setProgressBar(MessageManager.getString("status.finshed_querying"), Thread.currentThread()
725 guiWindow.setProgressBar((presult.size() > 0) ? MessageManager.getString("status.parsing_results")
726 : MessageManager.getString("status.processing"), Thread.currentThread().hashCode());
728 while (presult.size() > 0)
730 parseResult(presult.remove(0), presultTitle.remove(0), null);
732 // only remove visual delay after we finished parsing.
733 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
734 if (nextfetch.size() > 0)
736 StringBuffer sb = new StringBuffer();
737 sb.append("Didn't retrieve the following "
738 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
739 + " queries") + ": \n");
740 int l = sb.length(), lr = 0;
741 for (String s : nextfetch)
743 if (l != sb.length())
747 if (lr - sb.length() > 40)
753 showErrorMessage(sb.toString());
758 AlignmentI parseResult(String result, String title)
760 String format = new IdentifyFile().Identify(result, "Paste");
761 AlignmentI sequences = null;
762 if (FormatAdapter.isValidFormat(format))
767 sequences = new FormatAdapter().readFile(result.toString(),
769 } catch (Exception ex)
773 if (sequences != null)
775 return parseResult(sequences, title, format);
780 showErrorMessage("Error retrieving " + textArea.getText() + " from "
781 + database.getSelectedItem());
789 * @return a standard title for any results retrieved using the currently
790 * selected source and settings
792 public String getDefaultRetrievalTitle()
794 return "Retrieved from " + database.getSelectedItem();
797 AlignmentI parseResult(AlignmentI al, String title,
798 String currentFileFormat)
801 if (al != null && al.getHeight() > 0)
805 title = getDefaultRetrievalTitle();
807 if (alignFrame == null)
809 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
810 AlignFrame.DEFAULT_HEIGHT);
811 if (currentFileFormat != null)
813 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
815 // NON-FormatAdapter Sourced
819 SequenceFeature[] sfs = null;
820 List<SequenceI> alsqs;
821 synchronized (alsqs = al.getSequences())
823 for (SequenceI sq : alsqs)
825 if ((sfs = sq.getSequenceFeatures()) != null)
829 af.setShowSeqFeatures(true);
836 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
837 AlignFrame.DEFAULT_HEIGHT);
839 af.statusBar.setText(MessageManager
840 .getString("label.successfully_pasted_alignment_file"));
844 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
846 } catch (Exception ex)
852 alignFrame.viewport.addAlignment(al, title);
858 void showErrorMessage(final String error)
861 javax.swing.SwingUtilities.invokeLater(new Runnable()
866 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
867 MessageManager.getString("label.error_retrieving_data"),
868 JOptionPane.WARNING_MESSAGE);
873 public IProgressIndicator getProgressIndicator()
875 return progressIndicator;
878 public void setProgressIndicator(IProgressIndicator progressIndicator)
880 this.progressIndicator = progressIndicator;