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 import com.stevesoft.pat.Regex;
59 public class SequenceFetcher extends JPanel implements Runnable
61 JLabel dbeg = new JLabel();
63 JDatabaseTree database;
67 JLabel jLabel1 = new JLabel();
69 JCheckBox replacePunctuation = new JCheckBox();
71 JButton ok = new JButton();
73 JButton clear = new JButton();
75 JButton example = new JButton();
77 JButton close = new JButton();
79 JPanel jPanel1 = new JPanel();
81 JTextArea textArea = new JTextArea();
83 JScrollPane jScrollPane1 = new JScrollPane();
85 JPanel jPanel2 = new JPanel();
87 JPanel jPanel3 = new JPanel();
89 JPanel jPanel4 = new JPanel();
91 BorderLayout borderLayout1 = new BorderLayout();
93 BorderLayout borderLayout2 = new BorderLayout();
95 BorderLayout borderLayout3 = new BorderLayout();
99 IProgressIndicator guiWindow;
101 AlignFrame alignFrame;
105 final String noDbSelected = "-- Select Database --";
107 private static jalview.ws.SequenceFetcher sfetch = null;
109 private static long lastDasSourceRegistry = -3;
111 private static DasSourceRegistryI dasRegistry = null;
113 private static boolean _initingFetcher = false;
115 private static Thread initingThread = null;
117 int debounceTrap = 0;
120 * Blocking method that initialises and returns the shared instance of the
121 * SequenceFetcher client
124 * - where the initialisation delay message should be shown
125 * @return the singleton instance of the sequence fetcher client
127 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
128 final IProgressIndicator guiWindow)
130 if (_initingFetcher && initingThread != null && initingThread.isAlive())
132 if (guiWindow != null)
137 .getString("status.waiting_sequence_database_fetchers_init"),
138 Thread.currentThread().hashCode());
140 // initting happening on another thread - so wait around to see if it
142 while (_initingFetcher && initingThread != null
143 && initingThread.isAlive())
148 } catch (Exception e)
153 if (guiWindow != null)
158 .getString("status.waiting_sequence_database_fetchers_init"),
159 Thread.currentThread().hashCode());
163 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
164 || lastDasSourceRegistry != (jalview.bin.Cache
165 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
166 .getDasSourceRegistry().getLocalSourceString())
169 _initingFetcher = true;
170 initingThread = Thread.currentThread();
172 * give a visual indication that sequence fetcher construction is occuring
174 if (guiWindow != null)
176 guiWindow.setProgressBar(MessageManager
177 .getString("status.init_sequence_database_fetchers"),
178 Thread.currentThread().hashCode());
180 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
181 dasRegistry.refreshSources();
183 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
184 if (guiWindow != null)
186 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
188 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
189 .getLocalSourceString()).hashCode();
191 _initingFetcher = false;
192 initingThread = null;
197 private IProgressIndicator progressIndicator;
199 public SequenceFetcher(IProgressIndicator guiIndic)
201 this.progressIndicator = guiIndic;
202 final SequenceFetcher us = this;
203 // launch initialiser thread
204 Thread sf = new Thread(new Runnable()
210 if (getSequenceFetcherSingleton(progressIndicator) != null)
212 us.initGui(progressIndicator);
216 javax.swing.SwingUtilities.invokeLater(new Runnable()
222 .showInternalMessageDialog(
225 .getString("warn.couldnt_create_sequence_fetcher_client"),
227 .getString("label.couldnt_create_sequence_fetcher"),
228 JOptionPane.ERROR_MESSAGE);
232 // raise warning dialog
239 private class DatabaseAuthority extends DefaultMutableTreeNode
244 private class DatabaseSource extends DefaultMutableTreeNode
250 * called by thread spawned by constructor
254 private void initGui(IProgressIndicator guiWindow)
256 this.guiWindow = guiWindow;
257 if (guiWindow instanceof AlignFrame)
259 alignFrame = (AlignFrame) guiWindow;
261 database = new JDatabaseTree(sfetch);
265 } catch (Exception ex)
267 ex.printStackTrace();
270 frame = new JInternalFrame();
271 frame.setContentPane(this);
272 if (new jalview.util.Platform().isAMac())
274 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
278 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
282 private String getFrameTitle()
284 return ((alignFrame == null) ? MessageManager
285 .getString("label.new_sequence_fetcher") : MessageManager
286 .getString("label.additional_sequence_fetcher"));
289 private void jbInit() throws Exception
291 this.setLayout(borderLayout2);
293 database.setFont(JvSwingUtils.getLabelFont());
294 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
295 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
296 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
297 jLabel1.setText(MessageManager
298 .getString("label.separate_multiple_accession_ids"));
300 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
302 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
303 replacePunctuation.setText(MessageManager
304 .getString("label.replace_commas_semicolons"));
305 ok.setText(MessageManager.getString("action.ok"));
306 ok.addActionListener(new ActionListener()
309 public void actionPerformed(ActionEvent e)
311 ok_actionPerformed();
314 clear.setText(MessageManager.getString("action.clear"));
315 clear.addActionListener(new ActionListener()
318 public void actionPerformed(ActionEvent e)
320 clear_actionPerformed();
324 example.setText(MessageManager.getString("label.example"));
325 example.addActionListener(new ActionListener()
328 public void actionPerformed(ActionEvent e)
330 example_actionPerformed();
333 close.setText(MessageManager.getString("action.close"));
334 close.addActionListener(new ActionListener()
337 public void actionPerformed(ActionEvent e)
339 close_actionPerformed(e);
342 textArea.setFont(JvSwingUtils.getLabelFont());
343 textArea.setLineWrap(true);
344 textArea.addKeyListener(new KeyAdapter()
347 public void keyPressed(KeyEvent e)
349 if (e.getKeyCode() == KeyEvent.VK_ENTER)
351 ok_actionPerformed();
355 jPanel3.setLayout(borderLayout1);
356 borderLayout1.setVgap(5);
358 jPanel1.add(example);
361 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
362 jPanel2.setLayout(borderLayout3);
363 databaseButt = database.getDatabaseSelectorButton();
364 databaseButt.setFont(JvSwingUtils.getLabelFont());
365 database.addActionListener(new ActionListener()
368 public void actionPerformed(ActionEvent e)
371 String currentSelection = database.getSelectedItem();
372 if (!currentSelection.equalsIgnoreCase("pdb"))
376 if (currentSelection.equalsIgnoreCase("pdb")
377 && (database.action == KeyEvent.VK_ENTER || ((debounceTrap % 2) == 0)))
381 database.action = -1;
386 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
387 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
388 JPanel jPanel2a = new JPanel(new BorderLayout());
389 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
390 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
391 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
392 // jPanel2.setPreferredSize(new Dimension())
393 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
394 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
395 this.add(jPanel3, java.awt.BorderLayout.CENTER);
396 this.add(jPanel2, java.awt.BorderLayout.NORTH);
397 jScrollPane1.getViewport().add(textArea);
401 private void pdbSourceAction()
403 databaseButt.setText(database.getSelectedItem());
404 new PDBSearchPanel(this);
408 private void otherSourceAction()
412 databaseButt.setText(database.getSelectedItem()
413 + (database.getSelectedSources().size() > 1 ? " (and "
414 + database.getSelectedSources().size() + " others)"
416 String eq = database.getExampleQueries();
417 dbeg.setText(MessageManager.formatMessage(
418 "label.example_query_param", new String[] { eq }));
419 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
420 for (DbSourceProxy dbs : database.getSelectedSources())
422 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
428 replacePunctuation.setEnabled(enablePunct);
430 } catch (Exception ex)
433 replacePunctuation.setEnabled(true);
438 protected void example_actionPerformed()
440 DbSourceProxy db = null;
443 textArea.setText(database.getExampleQueries());
444 } catch (Exception ex)
450 protected void clear_actionPerformed()
452 textArea.setText("");
456 public void close_actionPerformed(ActionEvent e)
460 frame.setClosed(true);
461 } catch (Exception ex)
466 public void ok_actionPerformed()
468 databaseButt.setEnabled(false);
469 example.setEnabled(false);
470 textArea.setEnabled(false);
471 ok.setEnabled(false);
472 close.setEnabled(false);
474 Thread worker = new Thread(this);
478 private void resetDialog()
480 databaseButt.setEnabled(true);
481 example.setEnabled(true);
482 textArea.setEnabled(true);
484 close.setEnabled(true);
491 if (!database.hasSelection())
493 error += "Please select the source database\n";
495 // TODO: make this transformation more configurable
496 com.stevesoft.pat.Regex empty;
497 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
499 empty = new com.stevesoft.pat.Regex(
500 // replace commas and spaces with a semicolon
501 "(\\s|[,; ])+", ";");
505 // just turn spaces and semicolons into single semicolons
506 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
508 textArea.setText(empty.replaceAll(textArea.getText()));
509 // see if there's anthing to search with
510 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
513 error += "Please enter a (semi-colon separated list of) database id(s)";
515 if (error.length() > 0)
517 showErrorMessage(error);
521 // indicate if successive sources should be merged into one alignment.
522 boolean addToLast = false;
523 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
524 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
525 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
528 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
530 Iterator<String> en = Arrays.asList(new String[0]).iterator();
531 int nqueries = qries.length;
532 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
534 if (!en.hasNext() && nextfetch.size() > 0)
536 en = nextfetch.iterator();
537 nqueries = nextfetch.size();
538 // save the remaining queries in the original array
539 qries = nextfetch.toArray(new String[nqueries]);
540 nextfetch = new ArrayList<String>();
543 DbSourceProxy proxy = proxies.next();
544 boolean isAliSource = false;
549 .setProgressBar(MessageManager.formatMessage(
550 "status.fetching_sequence_queries_from",
552 Integer.valueOf(nqueries).toString(),
553 proxy.getDbName() }), Thread.currentThread()
555 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
556 if (proxy.getAccessionSeparator() == null)
560 String item = en.next();
567 // give the server a chance to breathe
569 } catch (Exception e)
576 AlignmentI indres = null;
579 indres = proxy.getSequenceRecords(item);
580 } catch (OutOfMemoryError oome)
582 new OOMWarning("fetching " + item + " from "
583 + proxy.getDbName(), oome, this);
594 } catch (Exception e)
596 jalview.bin.Cache.log.info("Error retrieving " + item
597 + " from " + proxy.getDbName(), e);
604 StringBuffer multiacc = new StringBuffer();
605 ArrayList<String> tosend = new ArrayList<String>();
608 String nel = en.next();
610 multiacc.append(nel);
613 multiacc.append(proxy.getAccessionSeparator());
620 List<String> nores = new ArrayList<String>();
621 rslt = proxy.getSequenceRecords(multiacc.toString());
622 if (rslt == null || rslt.getHeight() == 0)
624 // no results - pass on all queries to next source
625 nextfetch.addAll(tosend);
629 aresultq.add(multiacc.toString());
632 rs = rslt.getSequencesArray();
633 // search for each query in the dbrefs associated with each
636 // ones we do not find will be used to query next source (if any)
637 for (String q : tosend)
639 DBRefEntry dbr = new DBRefEntry(), found[] = null;
640 dbr.setSource(proxy.getDbSource());
641 dbr.setVersion(null);
642 if (proxy.getAccessionValidator() != null)
644 Regex vgr = proxy.getAccessionValidator();
646 if (vgr.numSubs() > 0)
648 dbr.setAccessionId(vgr.stringMatched(1));
652 dbr.setAccessionId(vgr.stringMatched());
657 dbr.setAccessionId(q);
659 boolean rfound = false;
660 for (int r = 0; r < rs.length; r++)
663 && (found = DBRefUtils.searchRefs(
664 rs[r].getDBRef(), dbr)) != null
678 } catch (OutOfMemoryError oome)
680 new OOMWarning("fetching " + multiacc + " from "
681 + database.getSelectedItem(), oome, this);
685 } catch (Exception e)
687 showErrorMessage("Error retrieving " + textArea.getText()
688 + " from " + database.getSelectedItem());
690 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
691 System.err.println("Retrieval failed for source ='"
692 + database.getSelectedItem() + "' and query\n'"
693 + textArea.getText() + "'\n");
695 } catch (OutOfMemoryError e)
697 // resets dialog box - so we don't use OOMwarning here.
698 showErrorMessage("Out of Memory when retrieving "
701 + database.getSelectedItem()
702 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
706 showErrorMessage("Serious Error retrieving " + textArea.getText()
707 + " from " + database.getSelectedItem());
710 // Stack results ready for opening in alignment windows
711 if (aresult != null && aresult.size() > 0)
713 AlignmentI ar = null;
717 // new window for each result
718 while (aresult.size() > 0)
720 presult.add(aresult.remove(0));
721 presultTitle.add(aresultq.remove(0) + " "
722 + getDefaultRetrievalTitle());
728 if (addToLast && presult.size() > 0)
730 ar = presult.remove(presult.size() - 1);
731 titl = presultTitle.remove(presultTitle.size() - 1);
733 // concatenate all results in one window
734 while (aresult.size() > 0)
738 ar = aresult.remove(0);
742 ar.append(aresult.remove(0));
748 presultTitle.add(titl);
751 guiWindow.setProgressBar(MessageManager
752 .getString("status.finshed_querying"), Thread.currentThread()
755 guiWindow.setProgressBar(
756 (presult.size() > 0) ? MessageManager
757 .getString("status.parsing_results") : MessageManager
758 .getString("status.processing"), Thread.currentThread()
761 while (presult.size() > 0)
763 parseResult(presult.remove(0), presultTitle.remove(0), null);
765 // only remove visual delay after we finished parsing.
766 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
767 if (nextfetch.size() > 0)
769 StringBuffer sb = new StringBuffer();
770 sb.append("Didn't retrieve the following "
771 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
772 + " queries") + ": \n");
773 int l = sb.length(), lr = 0;
774 for (String s : nextfetch)
776 if (l != sb.length())
780 if (lr - sb.length() > 40)
786 showErrorMessage(sb.toString());
791 AlignmentI parseResult(String result, String title)
793 String format = new IdentifyFile().Identify(result, "Paste");
794 AlignmentI sequences = null;
795 if (FormatAdapter.isValidFormat(format))
800 sequences = new FormatAdapter().readFile(result.toString(),
802 } catch (Exception ex)
806 if (sequences != null)
808 return parseResult(sequences, title, format);
813 showErrorMessage("Error retrieving " + textArea.getText() + " from "
814 + database.getSelectedItem());
822 * @return a standard title for any results retrieved using the currently
823 * selected source and settings
825 public String getDefaultRetrievalTitle()
827 return "Retrieved from " + database.getSelectedItem();
830 AlignmentI parseResult(AlignmentI al, String title,
831 String currentFileFormat)
834 if (al != null && al.getHeight() > 0)
838 title = getDefaultRetrievalTitle();
840 if (alignFrame == null)
842 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
843 AlignFrame.DEFAULT_HEIGHT);
844 if (currentFileFormat != null)
846 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
848 // NON-FormatAdapter Sourced
852 SequenceFeature[] sfs = null;
853 List<SequenceI> alsqs;
854 synchronized (alsqs = al.getSequences())
856 for (SequenceI sq : alsqs)
858 if ((sfs = sq.getSequenceFeatures()) != null)
862 af.setShowSeqFeatures(true);
869 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
870 AlignFrame.DEFAULT_HEIGHT);
872 af.statusBar.setText(MessageManager
873 .getString("label.successfully_pasted_alignment_file"));
877 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
879 } catch (Exception ex)
885 alignFrame.viewport.addAlignment(al, title);
891 void showErrorMessage(final String error)
894 javax.swing.SwingUtilities.invokeLater(new Runnable()
899 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
900 MessageManager.getString("label.error_retrieving_data"),
901 JOptionPane.WARNING_MESSAGE);
906 public IProgressIndicator getProgressIndicator()
908 return progressIndicator;
911 public void setProgressIndicator(IProgressIndicator progressIndicator)
913 this.progressIndicator = progressIndicator;