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.Alignment;
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.ws.dbsources.das.api.DasSourceRegistryI;
34 import jalview.ws.seqfetcher.DbSourceProxy;
36 import java.awt.BorderLayout;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.KeyAdapter;
41 import java.awt.event.KeyEvent;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Iterator;
45 import java.util.List;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JInternalFrame;
50 import javax.swing.JLabel;
51 import javax.swing.JOptionPane;
52 import javax.swing.JPanel;
53 import javax.swing.JScrollPane;
54 import javax.swing.JTextArea;
55 import javax.swing.SwingConstants;
56 import javax.swing.tree.DefaultMutableTreeNode;
58 import com.stevesoft.pat.Regex;
60 public class SequenceFetcher extends JPanel implements Runnable
62 JLabel dbeg = new JLabel();
64 JDatabaseTree database;
68 JLabel jLabel1 = new JLabel();
70 JCheckBox replacePunctuation = new JCheckBox();
72 JButton ok = new JButton();
74 JButton clear = new JButton();
76 JButton example = new JButton();
78 JButton close = new JButton();
80 JPanel jPanel1 = new JPanel();
82 JTextArea textArea = new JTextArea();
84 JScrollPane jScrollPane1 = new JScrollPane();
86 JPanel jPanel2 = new JPanel();
88 JPanel jPanel3 = new JPanel();
90 JPanel jPanel4 = new JPanel();
92 BorderLayout borderLayout1 = new BorderLayout();
94 BorderLayout borderLayout2 = new BorderLayout();
96 BorderLayout borderLayout3 = new BorderLayout();
100 IProgressIndicator guiWindow;
102 AlignFrame alignFrame;
106 final String noDbSelected = "-- Select Database --";
108 private static jalview.ws.SequenceFetcher sfetch = null;
110 private static long lastDasSourceRegistry = -3;
112 private static DasSourceRegistryI dasRegistry = null;
114 private static boolean _initingFetcher = false;
116 private static Thread initingThread = null;
118 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)
134 guiWindow.setProgressBar(
135 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
136 Thread.currentThread().hashCode());
138 // initting happening on another thread - so wait around to see if it
140 while (_initingFetcher && initingThread != null
141 && initingThread.isAlive())
146 } catch (Exception e)
151 if (guiWindow != null)
153 guiWindow.setProgressBar(
154 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
155 Thread.currentThread().hashCode());
159 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
160 || lastDasSourceRegistry != (jalview.bin.Cache
161 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
162 .getDasSourceRegistry().getLocalSourceString())
165 _initingFetcher = true;
166 initingThread = Thread.currentThread();
168 * give a visual indication that sequence fetcher construction is occuring
170 if (guiWindow != null)
172 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
173 Thread.currentThread().hashCode());
175 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
176 dasRegistry.refreshSources();
178 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
179 if (guiWindow != null)
181 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
182 Thread.currentThread().hashCode());
184 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
185 .getLocalSourceString()).hashCode();
187 _initingFetcher = false;
188 initingThread = null;
193 private IProgressIndicator progressIndicator;
194 public SequenceFetcher(IProgressIndicator guiIndic)
196 this.progressIndicator = guiIndic;
197 final SequenceFetcher us = this;
198 // launch initialiser thread
199 Thread sf = new Thread(new Runnable()
205 if (getSequenceFetcherSingleton(progressIndicator) != null)
207 us.initGui(progressIndicator);
211 javax.swing.SwingUtilities.invokeLater(new Runnable()
217 .showInternalMessageDialog(
219 MessageManager.getString("warn.couldnt_create_sequence_fetcher_client"),
220 MessageManager.getString("label.couldnt_create_sequence_fetcher"),
221 JOptionPane.ERROR_MESSAGE);
225 // raise warning dialog
232 private class DatabaseAuthority extends DefaultMutableTreeNode
237 private class DatabaseSource extends DefaultMutableTreeNode
243 * called by thread spawned by constructor
247 private void initGui(IProgressIndicator guiWindow)
249 this.guiWindow = guiWindow;
250 if (guiWindow instanceof AlignFrame)
252 alignFrame = (AlignFrame) guiWindow;
254 database = new JDatabaseTree(sfetch);
258 } catch (Exception ex)
260 ex.printStackTrace();
263 frame = new JInternalFrame();
264 frame.setContentPane(this);
265 if (new jalview.util.Platform().isAMac())
267 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
271 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
275 private String getFrameTitle()
277 return ((alignFrame == null) ? MessageManager.getString("label.new_sequence_fetcher") : MessageManager.getString("label.additional_sequence_fetcher"));
280 private void jbInit() throws Exception
282 this.setLayout(borderLayout2);
284 database.setFont(JvSwingUtils.getLabelFont());
285 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
286 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
287 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
288 jLabel1.setText(MessageManager
289 .getString("label.separate_multiple_accession_ids"));
291 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
293 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
294 replacePunctuation.setText(MessageManager
295 .getString("label.replace_commas_semicolons"));
296 ok.setText(MessageManager.getString("action.ok"));
297 ok.addActionListener(new ActionListener()
300 public void actionPerformed(ActionEvent e)
302 ok_actionPerformed();
305 clear.setText(MessageManager.getString("action.clear"));
306 clear.addActionListener(new ActionListener()
309 public void actionPerformed(ActionEvent e)
311 clear_actionPerformed();
315 example.setText(MessageManager.getString("label.example"));
316 example.addActionListener(new ActionListener()
319 public void actionPerformed(ActionEvent e)
321 example_actionPerformed();
324 close.setText(MessageManager.getString("action.close"));
325 close.addActionListener(new ActionListener()
328 public void actionPerformed(ActionEvent e)
330 close_actionPerformed(e);
333 textArea.setFont(JvSwingUtils.getLabelFont());
334 textArea.setLineWrap(true);
335 textArea.addKeyListener(new KeyAdapter()
338 public void keyPressed(KeyEvent e)
340 if (e.getKeyCode() == KeyEvent.VK_ENTER)
342 ok_actionPerformed();
346 jPanel3.setLayout(borderLayout1);
347 borderLayout1.setVgap(5);
349 jPanel1.add(example);
352 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
353 jPanel2.setLayout(borderLayout3);
354 databaseButt = database.getDatabaseSelectorButton();
355 databaseButt.setFont(JvSwingUtils.getLabelFont());
356 database.addActionListener(new ActionListener()
359 public void actionPerformed(ActionEvent e)
362 String currentSelection = database.getSelectedItem();
364 if (!currentSelection.equalsIgnoreCase("pdb"))
368 if (currentSelection.equalsIgnoreCase("pdb") && ((debounceTrap % 2) == 0))
376 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
377 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
378 JPanel jPanel2a = new JPanel(new BorderLayout());
379 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
380 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
381 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
382 // jPanel2.setPreferredSize(new Dimension())
383 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
384 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
385 this.add(jPanel3, java.awt.BorderLayout.CENTER);
386 this.add(jPanel2, java.awt.BorderLayout.NORTH);
387 jScrollPane1.getViewport().add(textArea);
391 private void pdbSourceAction()
393 databaseButt.setText(database.getSelectedItem());
394 new PDBSearchPanel(this);
398 private void otherSourceAction()
402 databaseButt.setText(database.getSelectedItem()
403 + (database.getSelectedSources().size() > 1 ? " (and "
404 + database.getSelectedSources().size() + " others)"
406 String eq = database.getExampleQueries();
407 dbeg.setText(MessageManager.formatMessage(
408 "label.example_query_param", new String[]
410 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
411 for (DbSourceProxy dbs : database.getSelectedSources())
413 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
419 replacePunctuation.setEnabled(enablePunct);
421 } catch (Exception ex)
424 replacePunctuation.setEnabled(true);
429 protected void example_actionPerformed()
431 DbSourceProxy db = null;
434 textArea.setText(database.getExampleQueries());
435 } catch (Exception ex)
441 protected void clear_actionPerformed()
443 textArea.setText("");
449 public void close_actionPerformed(ActionEvent e)
453 frame.setClosed(true);
454 } catch (Exception ex)
459 public void ok_actionPerformed()
461 databaseButt.setEnabled(false);
462 example.setEnabled(false);
463 textArea.setEnabled(false);
464 ok.setEnabled(false);
465 close.setEnabled(false);
467 Thread worker = new Thread(this);
471 private void resetDialog()
473 databaseButt.setEnabled(true);
474 example.setEnabled(true);
475 textArea.setEnabled(true);
477 close.setEnabled(true);
484 if (!database.hasSelection())
486 error += "Please select the source database\n";
488 // TODO: make this transformation more configurable
489 com.stevesoft.pat.Regex empty;
490 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
492 empty = new com.stevesoft.pat.Regex(
493 // replace commas and spaces with a semicolon
494 "(\\s|[,; ])+", ";");
498 // just turn spaces and semicolons into single semicolons
499 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
501 textArea.setText(empty.replaceAll(textArea.getText()));
502 // see if there's anthing to search with
503 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
506 error += "Please enter a (semi-colon separated list of) database id(s)";
508 if (error.length() > 0)
510 showErrorMessage(error);
514 // indicate if successive sources should be merged into one alignment.
515 boolean addToLast = false;
516 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
517 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
518 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
521 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
523 Iterator<String> en = Arrays.asList(new String[0]).iterator();
524 int nqueries = qries.length;
525 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
527 if (!en.hasNext() && nextfetch.size() > 0)
529 en = nextfetch.iterator();
530 nqueries = nextfetch.size();
531 // save the remaining queries in the original array
532 qries = nextfetch.toArray(new String[nqueries]);
533 nextfetch = new ArrayList<String>();
536 DbSourceProxy proxy = proxies.next();
537 boolean isAliSource = false;
541 guiWindow.setProgressBar(MessageManager.formatMessage("status.fetching_sequence_queries_from", new String[]{Integer.valueOf(nqueries).toString(),proxy.getDbName()}), Thread
542 .currentThread().hashCode());
543 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
544 if (proxy.getAccessionSeparator() == null)
548 String item = en.next();
555 // give the server a chance to breathe
557 } catch (Exception e)
564 AlignmentI indres = null;
567 indres = proxy.getSequenceRecords(item);
568 } catch (OutOfMemoryError oome)
570 new OOMWarning("fetching " + item + " from "
571 + proxy.getDbName(), oome, this);
582 } catch (Exception e)
584 jalview.bin.Cache.log.info("Error retrieving " + item
585 + " from " + proxy.getDbName(), e);
592 StringBuffer multiacc = new StringBuffer();
593 ArrayList<String> tosend = new ArrayList<String>();
596 String nel = en.next();
598 multiacc.append(nel);
601 multiacc.append(proxy.getAccessionSeparator());
608 List<String> nores = new ArrayList<String>();
609 rslt = proxy.getSequenceRecords(multiacc.toString());
610 if (rslt == null || rslt.getHeight() == 0)
612 // no results - pass on all queries to next source
613 nextfetch.addAll(tosend);
617 aresultq.add(multiacc.toString());
620 rs = rslt.getSequencesArray();
621 // search for each query in the dbrefs associated with each
624 // ones we do not find will be used to query next source (if any)
625 for (String q : tosend)
627 DBRefEntry dbr = new DBRefEntry(), found[] = null;
628 dbr.setSource(proxy.getDbSource());
629 dbr.setVersion(null);
630 if (proxy.getAccessionValidator() != null)
632 Regex vgr = proxy.getAccessionValidator();
634 if (vgr.numSubs() > 0)
636 dbr.setAccessionId(vgr.stringMatched(1));
640 dbr.setAccessionId(vgr.stringMatched());
645 dbr.setAccessionId(q);
647 boolean rfound = false;
648 for (int r = 0; r < rs.length; r++)
651 && (found = DBRefUtils.searchRefs(
652 rs[r].getDBRef(), dbr)) != null
666 } catch (OutOfMemoryError oome)
668 new OOMWarning("fetching " + multiacc + " from "
669 + database.getSelectedItem(), oome, this);
673 } catch (Exception e)
675 showErrorMessage("Error retrieving " + textArea.getText()
676 + " from " + database.getSelectedItem());
678 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
679 System.err.println("Retrieval failed for source ='"
680 + database.getSelectedItem() + "' and query\n'"
681 + textArea.getText() + "'\n");
683 } catch (OutOfMemoryError e)
685 // resets dialog box - so we don't use OOMwarning here.
686 showErrorMessage("Out of Memory when retrieving "
689 + database.getSelectedItem()
690 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
694 showErrorMessage("Serious Error retrieving " + textArea.getText()
695 + " from " + database.getSelectedItem());
698 // Stack results ready for opening in alignment windows
699 if (aresult != null && aresult.size() > 0)
701 AlignmentI ar = null;
705 // new window for each result
706 while (aresult.size() > 0)
708 presult.add(aresult.remove(0));
709 presultTitle.add(aresultq.remove(0) + " "
710 + getDefaultRetrievalTitle());
716 if (addToLast && presult.size() > 0)
718 ar = presult.remove(presult.size() - 1);
719 titl = presultTitle.remove(presultTitle.size() - 1);
721 // concatenate all results in one window
722 while (aresult.size() > 0)
726 ar = aresult.remove(0);
730 ar.append(aresult.remove(0));
736 presultTitle.add(titl);
739 guiWindow.setProgressBar(MessageManager.getString("status.finshed_querying"), Thread.currentThread()
742 guiWindow.setProgressBar((presult.size() > 0) ? MessageManager.getString("status.parsing_results")
743 : MessageManager.getString("status.processing"), Thread.currentThread().hashCode());
745 while (presult.size() > 0)
747 parseResult(presult.remove(0), presultTitle.remove(0), null);
749 // only remove visual delay after we finished parsing.
750 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
751 if (nextfetch.size() > 0)
753 StringBuffer sb = new StringBuffer();
754 sb.append("Didn't retrieve the following "
755 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
756 + " queries") + ": \n");
757 int l = sb.length(), lr = 0;
758 for (String s : nextfetch)
760 if (l != sb.length())
764 if (lr - sb.length() > 40)
770 showErrorMessage(sb.toString());
775 AlignmentI parseResult(String result, String title)
777 String format = new IdentifyFile().Identify(result, "Paste");
778 Alignment sequences = null;
779 if (FormatAdapter.isValidFormat(format))
784 sequences = new FormatAdapter().readFile(result.toString(),
786 } catch (Exception ex)
790 if (sequences != null)
792 return parseResult(sequences, title, format);
797 showErrorMessage("Error retrieving " + textArea.getText() + " from "
798 + database.getSelectedItem());
806 * @return a standard title for any results retrieved using the currently
807 * selected source and settings
809 public String getDefaultRetrievalTitle()
811 return "Retrieved from " + database.getSelectedItem();
814 AlignmentI parseResult(AlignmentI al, String title,
815 String currentFileFormat)
818 if (al != null && al.getHeight() > 0)
822 title = getDefaultRetrievalTitle();
824 if (alignFrame == null)
826 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
827 AlignFrame.DEFAULT_HEIGHT);
828 if (currentFileFormat != null)
830 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
832 // NON-FormatAdapter Sourced
836 SequenceFeature[] sfs = null;
837 List<SequenceI> alsqs;
838 synchronized (alsqs = al.getSequences())
840 for (SequenceI sq : alsqs)
842 if ((sfs = sq.getSequenceFeatures()) != null)
846 af.setShowSeqFeatures(true);
853 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
854 AlignFrame.DEFAULT_HEIGHT);
856 af.statusBar.setText(MessageManager
857 .getString("label.successfully_pasted_alignment_file"));
861 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
863 } catch (Exception ex)
869 alignFrame.viewport.addAlignment(al, title);
875 void showErrorMessage(final String error)
878 javax.swing.SwingUtilities.invokeLater(new Runnable()
883 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
884 MessageManager.getString("label.error_retrieving_data"),
885 JOptionPane.WARNING_MESSAGE);
890 public IProgressIndicator getProgressIndicator()
892 return progressIndicator;
895 public void setProgressIndicator(IProgressIndicator progressIndicator)
897 this.progressIndicator = progressIndicator;