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.bin.Cache;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.FormatAdapter;
30 import jalview.io.IdentifyFile;
31 import jalview.util.DBRefUtils;
32 import jalview.util.MessageManager;
33 import jalview.util.Platform;
34 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
35 import jalview.ws.dbsources.das.datamodel.DasSequenceSource;
36 import jalview.ws.seqfetcher.DbSourceProxy;
38 import java.awt.BorderLayout;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.KeyAdapter;
43 import java.awt.event.KeyEvent;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Iterator;
47 import java.util.List;
49 import javax.swing.JButton;
50 import javax.swing.JCheckBox;
51 import javax.swing.JInternalFrame;
52 import javax.swing.JLabel;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.JScrollPane;
56 import javax.swing.JTextArea;
57 import javax.swing.SwingConstants;
58 import javax.swing.tree.DefaultMutableTreeNode;
60 import com.stevesoft.pat.Regex;
62 public class SequenceFetcher extends JPanel implements Runnable
64 JLabel dbeg = new JLabel();
66 JDatabaseTree database;
70 JLabel jLabel1 = new JLabel();
72 JCheckBox replacePunctuation = new JCheckBox();
74 JButton ok = new JButton();
76 JButton clear = new JButton();
78 JButton example = new JButton();
80 JButton close = new JButton();
82 JPanel jPanel1 = new JPanel();
84 JTextArea textArea = new JTextArea();
86 JScrollPane jScrollPane1 = new JScrollPane();
88 JPanel jPanel2 = new JPanel();
90 JPanel jPanel3 = new JPanel();
92 JPanel jPanel4 = new JPanel();
94 BorderLayout borderLayout1 = new BorderLayout();
96 BorderLayout borderLayout2 = new BorderLayout();
98 BorderLayout borderLayout3 = new BorderLayout();
100 JInternalFrame frame;
102 IProgressIndicator guiWindow;
104 AlignFrame alignFrame;
108 final String noDbSelected = "-- Select Database --";
110 private static jalview.ws.SequenceFetcher sfetch = null;
112 private static long lastDasSourceRegistry = -3;
114 private static DasSourceRegistryI dasRegistry = null;
116 private static boolean _initingFetcher = false;
118 private static Thread initingThread = null;
120 int debounceTrap = 0;
122 * Blocking method that initialises and returns the shared instance of the
123 * SequenceFetcher client
126 * - where the initialisation delay message should be shown
127 * @return the singleton instance of the sequence fetcher client
129 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
130 final IProgressIndicator guiWindow)
132 if (_initingFetcher && initingThread != null && initingThread.isAlive())
134 if (guiWindow != null)
136 guiWindow.setProgressBar(
137 MessageManager.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)
155 guiWindow.setProgressBar(
156 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
157 Thread.currentThread().hashCode());
161 || dasRegistry != Cache.getDasSourceRegistry()
162 || lastDasSourceRegistry != (Cache
163 .getDasSourceRegistry().getDasRegistryURL() + Cache
164 .getDasSourceRegistry().getLocalSourceString())
167 _initingFetcher = true;
168 initingThread = Thread.currentThread();
170 * give a visual indication that sequence fetcher construction is occuring
172 if (guiWindow != null)
174 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
175 Thread.currentThread().hashCode());
177 dasRegistry = Cache.getDasSourceRegistry();
178 dasRegistry.refreshSources();
180 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
181 if (guiWindow != null)
183 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
185 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
186 .getLocalSourceString()).hashCode();
188 _initingFetcher = false;
189 initingThread = null;
194 private IProgressIndicator progressIndicator;
195 public SequenceFetcher(IProgressIndicator guiIndic)
197 this.progressIndicator = guiIndic;
198 final SequenceFetcher us = this;
199 // launch initialiser thread
200 Thread sf = new Thread(new Runnable()
206 if (getSequenceFetcherSingleton(progressIndicator) != null)
208 us.initGui(progressIndicator);
212 javax.swing.SwingUtilities.invokeLater(new Runnable()
218 .showInternalMessageDialog(
220 MessageManager.getString("warn.couldnt_create_sequence_fetcher_client"),
221 MessageManager.getString("label.couldnt_create_sequence_fetcher"),
222 JOptionPane.ERROR_MESSAGE);
226 // raise warning dialog
233 private class DatabaseAuthority extends DefaultMutableTreeNode
238 private class DatabaseSource extends DefaultMutableTreeNode
244 * called by thread spawned by constructor
248 private void initGui(IProgressIndicator guiWindow)
250 this.guiWindow = guiWindow;
251 if (guiWindow instanceof AlignFrame)
253 alignFrame = (AlignFrame) guiWindow;
255 database = new JDatabaseTree(sfetch);
259 } catch (Exception ex)
261 ex.printStackTrace();
264 frame = new JInternalFrame();
265 frame.setContentPane(this);
266 if (Platform.isAMac())
268 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
272 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
276 private String getFrameTitle()
278 return ((alignFrame == null) ? MessageManager.getString("label.new_sequence_fetcher") : MessageManager.getString("label.additional_sequence_fetcher"));
281 private void jbInit() throws Exception
283 this.setLayout(borderLayout2);
285 database.setFont(JvSwingUtils.getLabelFont());
286 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
287 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
288 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
289 jLabel1.setText(MessageManager
290 .getString("label.separate_multiple_accession_ids"));
292 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
294 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
295 replacePunctuation.setText(MessageManager
296 .getString("label.replace_commas_semicolons"));
297 ok.setText(MessageManager.getString("action.ok"));
298 ok.addActionListener(new ActionListener()
301 public void actionPerformed(ActionEvent e)
303 ok_actionPerformed();
306 clear.setText(MessageManager.getString("action.clear"));
307 clear.addActionListener(new ActionListener()
310 public void actionPerformed(ActionEvent e)
312 clear_actionPerformed();
316 example.setText(MessageManager.getString("label.example"));
317 example.addActionListener(new ActionListener()
320 public void actionPerformed(ActionEvent e)
322 example_actionPerformed();
325 close.setText(MessageManager.getString("action.close"));
326 close.addActionListener(new ActionListener()
329 public void actionPerformed(ActionEvent e)
331 close_actionPerformed(e);
334 textArea.setFont(JvSwingUtils.getLabelFont());
335 textArea.setLineWrap(true);
336 textArea.addKeyListener(new KeyAdapter()
339 public void keyPressed(KeyEvent e)
341 if (e.getKeyCode() == KeyEvent.VK_ENTER)
343 ok_actionPerformed();
347 jPanel3.setLayout(borderLayout1);
348 borderLayout1.setVgap(5);
350 jPanel1.add(example);
353 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
354 jPanel2.setLayout(borderLayout3);
355 databaseButt = database.getDatabaseSelectorButton();
356 databaseButt.setFont(JvSwingUtils.getLabelFont());
357 database.addActionListener(new ActionListener()
360 public void actionPerformed(ActionEvent e)
363 String currentSelection = database.getSelectedItem();
365 if (!currentSelection.equalsIgnoreCase("pdb"))
369 if (currentSelection.equalsIgnoreCase("pdb") && ((debounceTrap % 2) == 0))
377 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
378 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
379 JPanel jPanel2a = new JPanel(new BorderLayout());
380 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
381 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
382 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
383 // jPanel2.setPreferredSize(new Dimension())
384 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
385 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
386 this.add(jPanel3, java.awt.BorderLayout.CENTER);
387 this.add(jPanel2, java.awt.BorderLayout.NORTH);
388 jScrollPane1.getViewport().add(textArea);
392 private void pdbSourceAction()
394 databaseButt.setText(database.getSelectedItem());
395 new PDBSearchPanel(this);
399 private void otherSourceAction()
403 databaseButt.setText(database.getSelectedItem()
404 + (database.getSelectedSources().size() > 1 ? " (and "
405 + database.getSelectedSources().size() + " others)"
407 String eq = database.getExampleQueries();
408 dbeg.setText(MessageManager.formatMessage(
409 "label.example_query_param", new String[]
411 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
412 for (DbSourceProxy dbs : database.getSelectedSources())
414 if (dbs instanceof DasSequenceSource)
420 replacePunctuation.setEnabled(enablePunct);
422 } catch (Exception ex)
425 replacePunctuation.setEnabled(true);
430 protected void example_actionPerformed()
432 DbSourceProxy db = null;
435 textArea.setText(database.getExampleQueries());
436 } catch (Exception ex)
442 protected void clear_actionPerformed()
444 textArea.setText("");
450 public void close_actionPerformed(ActionEvent e)
454 frame.setClosed(true);
455 } catch (Exception ex)
460 public void ok_actionPerformed()
462 databaseButt.setEnabled(false);
463 example.setEnabled(false);
464 textArea.setEnabled(false);
465 ok.setEnabled(false);
466 close.setEnabled(false);
468 Thread worker = new Thread(this);
472 private void resetDialog()
474 databaseButt.setEnabled(true);
475 example.setEnabled(true);
476 textArea.setEnabled(true);
478 close.setEnabled(true);
485 if (!database.hasSelection())
487 error += "Please select the source database\n";
489 // TODO: make this transformation more configurable
491 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
494 // replace commas and spaces with a semicolon
495 "(\\s|[,; ])+", ";");
499 // just turn spaces and semicolons into single semicolons
500 empty = new Regex("(\\s|[; ])+", ";");
502 textArea.setText(empty.replaceAll(textArea.getText()));
503 // see if there's anthing to search with
504 if (!new Regex("[A-Za-z0-9_.]").search(textArea
507 error += "Please enter a (semi-colon separated list of) database id(s)";
509 if (error.length() > 0)
511 showErrorMessage(error);
515 // indicate if successive sources should be merged into one alignment.
516 boolean addToLast = false;
517 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
518 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
519 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
522 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
524 Iterator<String> en = Arrays.asList(new String[0]).iterator();
525 int nqueries = qries.length;
526 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
528 if (!en.hasNext() && nextfetch.size() > 0)
530 en = nextfetch.iterator();
531 nqueries = nextfetch.size();
532 // save the remaining queries in the original array
533 qries = nextfetch.toArray(new String[nqueries]);
534 nextfetch = new ArrayList<String>();
537 DbSourceProxy proxy = proxies.next();
538 boolean isAliSource = false;
542 guiWindow.setProgressBar(MessageManager.formatMessage("status.fetching_sequence_queries_from", new String[]{Integer.valueOf(nqueries).toString(),proxy.getDbName()}), Thread
543 .currentThread().hashCode());
544 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
545 if (proxy.getAccessionSeparator() == null)
549 String item = en.next();
556 // give the server a chance to breathe
558 } catch (Exception e)
565 AlignmentI indres = null;
568 indres = proxy.getSequenceRecords(item);
569 } catch (OutOfMemoryError oome)
571 new OOMWarning("fetching " + item + " from "
572 + proxy.getDbName(), oome, this);
583 } catch (Exception e)
585 Cache.log.info("Error retrieving " + item
586 + " from " + proxy.getDbName(), e);
593 StringBuffer multiacc = new StringBuffer();
594 ArrayList<String> tosend = new ArrayList<String>();
597 String nel = en.next();
599 multiacc.append(nel);
602 multiacc.append(proxy.getAccessionSeparator());
609 List<String> nores = new ArrayList<String>();
610 rslt = proxy.getSequenceRecords(multiacc.toString());
611 if (rslt == null || rslt.getHeight() == 0)
613 // no results - pass on all queries to next source
614 nextfetch.addAll(tosend);
618 aresultq.add(multiacc.toString());
621 rs = rslt.getSequencesArray();
622 // search for each query in the dbrefs associated with each
625 // ones we do not find will be used to query next source (if any)
626 for (String q : tosend)
628 DBRefEntry dbr = new DBRefEntry(), found[] = null;
629 dbr.setSource(proxy.getDbSource());
630 dbr.setVersion(null);
631 if (proxy.getAccessionValidator() != null)
633 Regex vgr = proxy.getAccessionValidator();
635 if (vgr.numSubs() > 0)
637 dbr.setAccessionId(vgr.stringMatched(1));
641 dbr.setAccessionId(vgr.stringMatched());
646 dbr.setAccessionId(q);
648 boolean rfound = false;
649 for (int r = 0; r < rs.length; r++)
652 && (found = DBRefUtils.searchRefs(
653 rs[r].getDBRef(), dbr)) != null
667 } catch (OutOfMemoryError oome)
669 new OOMWarning("fetching " + multiacc + " from "
670 + database.getSelectedItem(), oome, this);
674 } catch (Exception e)
676 showErrorMessage("Error retrieving " + textArea.getText()
677 + " from " + database.getSelectedItem());
679 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
680 System.err.println("Retrieval failed for source ='"
681 + database.getSelectedItem() + "' and query\n'"
682 + textArea.getText() + "'\n");
684 } catch (OutOfMemoryError e)
686 // resets dialog box - so we don't use OOMwarning here.
687 showErrorMessage("Out of Memory when retrieving "
690 + database.getSelectedItem()
691 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
695 showErrorMessage("Serious Error retrieving " + textArea.getText()
696 + " from " + database.getSelectedItem());
699 // Stack results ready for opening in alignment windows
700 if (aresult != null && aresult.size() > 0)
702 AlignmentI ar = null;
706 // new window for each result
707 while (aresult.size() > 0)
709 presult.add(aresult.remove(0));
710 presultTitle.add(aresultq.remove(0) + " "
711 + getDefaultRetrievalTitle());
717 if (addToLast && presult.size() > 0)
719 ar = presult.remove(presult.size() - 1);
720 titl = presultTitle.remove(presultTitle.size() - 1);
722 // concatenate all results in one window
723 while (aresult.size() > 0)
727 ar = aresult.remove(0);
731 ar.append(aresult.remove(0));
737 presultTitle.add(titl);
740 guiWindow.setProgressBar(MessageManager.getString("status.finshed_querying"), Thread.currentThread()
743 guiWindow.setProgressBar((presult.size() > 0) ? MessageManager.getString("status.parsing_results")
744 : MessageManager.getString("status.processing"), Thread.currentThread().hashCode());
746 while (presult.size() > 0)
748 parseResult(presult.remove(0), presultTitle.remove(0), null);
750 // only remove visual delay after we finished parsing.
751 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
752 if (nextfetch.size() > 0)
754 StringBuffer sb = new StringBuffer();
755 sb.append("Didn't retrieve the following "
756 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
757 + " queries") + ": \n");
758 int l = sb.length(), lr = 0;
759 for (String s : nextfetch)
761 if (l != sb.length())
765 if (lr - sb.length() > 40)
771 showErrorMessage(sb.toString());
776 AlignmentI parseResult(String result, String title)
778 String format = new IdentifyFile().Identify(result, "Paste");
779 AlignmentI sequences = null;
780 if (FormatAdapter.isValidFormat(format))
785 sequences = new FormatAdapter().readFile(result.toString(),
787 } catch (Exception ex)
791 if (sequences != null)
793 return parseResult(sequences, title, format);
798 showErrorMessage("Error retrieving " + textArea.getText() + " from "
799 + database.getSelectedItem());
807 * @return a standard title for any results retrieved using the currently
808 * selected source and settings
810 public String getDefaultRetrievalTitle()
812 return "Retrieved from " + database.getSelectedItem();
815 AlignmentI parseResult(AlignmentI al, String title,
816 String currentFileFormat)
819 if (al != null && al.getHeight() > 0)
823 title = getDefaultRetrievalTitle();
825 if (alignFrame == null)
827 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
828 AlignFrame.DEFAULT_HEIGHT);
829 if (currentFileFormat != null)
831 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
833 // NON-FormatAdapter Sourced
837 SequenceFeature[] sfs = null;
838 List<SequenceI> alsqs;
839 synchronized (alsqs = al.getSequences())
841 for (SequenceI sq : alsqs)
843 if ((sfs = sq.getSequenceFeatures()) != null)
847 af.setShowSeqFeatures(true);
854 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
855 AlignFrame.DEFAULT_HEIGHT);
857 af.statusBar.setText(MessageManager
858 .getString("label.successfully_pasted_alignment_file"));
862 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN",
864 } catch (Exception ex)
870 alignFrame.viewport.addAlignment(al, title);
876 void showErrorMessage(final String error)
879 javax.swing.SwingUtilities.invokeLater(new Runnable()
884 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
885 MessageManager.getString("label.error_retrieving_data"),
886 JOptionPane.WARNING_MESSAGE);
891 public IProgressIndicator getProgressIndicator()
893 return progressIndicator;
896 public void setProgressIndicator(IProgressIndicator progressIndicator)
898 this.progressIndicator = progressIndicator;