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.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.FormatAdapter;
28 import jalview.io.IdentifyFile;
29 import jalview.util.DBRefUtils;
30 import jalview.util.MessageManager;
31 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
32 import jalview.ws.seqfetcher.DbSourceProxy;
34 import java.awt.BorderLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.KeyAdapter;
39 import java.awt.event.KeyEvent;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Iterator;
43 import java.util.List;
45 import javax.swing.JButton;
46 import javax.swing.JCheckBox;
47 import javax.swing.JInternalFrame;
48 import javax.swing.JLabel;
49 import javax.swing.JOptionPane;
50 import javax.swing.JPanel;
51 import javax.swing.JScrollPane;
52 import javax.swing.JTextArea;
53 import javax.swing.SwingConstants;
54 import javax.swing.tree.DefaultMutableTreeNode;
56 public class SequenceFetcher extends JPanel implements Runnable
58 JLabel dbeg = new JLabel();
60 JDatabaseTree database;
64 JLabel jLabel1 = new JLabel();
66 JCheckBox replacePunctuation = new JCheckBox();
68 JButton ok = new JButton();
70 JButton clear = new JButton();
72 JButton example = new JButton();
74 JButton close = new JButton();
76 JPanel jPanel1 = new JPanel();
78 JTextArea textArea = new JTextArea();
80 JScrollPane jScrollPane1 = new JScrollPane();
82 JPanel jPanel2 = new JPanel();
84 JPanel jPanel3 = new JPanel();
86 JPanel jPanel4 = new JPanel();
88 BorderLayout borderLayout1 = new BorderLayout();
90 BorderLayout borderLayout2 = new BorderLayout();
92 BorderLayout borderLayout3 = new BorderLayout();
96 IProgressIndicator guiWindow;
98 AlignFrame alignFrame;
102 final String noDbSelected = "-- Select Database --";
104 private static jalview.ws.SequenceFetcher sfetch = null;
106 private static long lastDasSourceRegistry = -3;
108 private static DasSourceRegistryI dasRegistry = null;
110 private static boolean _initingFetcher = false;
112 private static Thread initingThread = null;
114 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)
134 .getString("status.waiting_sequence_database_fetchers_init"),
135 Thread.currentThread().hashCode());
137 // initting happening on another thread - so wait around to see if it
139 while (_initingFetcher && initingThread != null
140 && initingThread.isAlive())
145 } catch (Exception e)
150 if (guiWindow != null)
155 .getString("status.waiting_sequence_database_fetchers_init"),
156 Thread.currentThread().hashCode());
160 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
161 || lastDasSourceRegistry != (jalview.bin.Cache
162 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
163 .getDasSourceRegistry().getLocalSourceString())
166 _initingFetcher = true;
167 initingThread = Thread.currentThread();
169 * give a visual indication that sequence fetcher construction is occuring
171 if (guiWindow != null)
173 guiWindow.setProgressBar(MessageManager
174 .getString("status.init_sequence_database_fetchers"),
175 Thread.currentThread().hashCode());
177 dasRegistry = jalview.bin.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;
196 public SequenceFetcher(IProgressIndicator guiIndic)
198 this.progressIndicator = guiIndic;
199 final SequenceFetcher us = this;
200 // launch initialiser thread
201 Thread sf = new Thread(new Runnable()
207 if (getSequenceFetcherSingleton(progressIndicator) != null)
209 us.initGui(progressIndicator);
213 javax.swing.SwingUtilities.invokeLater(new Runnable()
219 .showInternalMessageDialog(
222 .getString("warn.couldnt_create_sequence_fetcher_client"),
224 .getString("label.couldnt_create_sequence_fetcher"),
225 JOptionPane.ERROR_MESSAGE);
229 // raise warning dialog
236 private class DatabaseAuthority extends DefaultMutableTreeNode
241 private class DatabaseSource extends DefaultMutableTreeNode
247 * called by thread spawned by constructor
251 private void initGui(IProgressIndicator guiWindow)
253 this.guiWindow = guiWindow;
254 if (guiWindow instanceof AlignFrame)
256 alignFrame = (AlignFrame) guiWindow;
258 database = new JDatabaseTree(sfetch);
262 } catch (Exception ex)
264 ex.printStackTrace();
267 frame = new JInternalFrame();
268 frame.setContentPane(this);
269 if (new jalview.util.Platform().isAMac())
271 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
275 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
279 private String getFrameTitle()
281 return ((alignFrame == null) ? MessageManager
282 .getString("label.new_sequence_fetcher") : MessageManager
283 .getString("label.additional_sequence_fetcher"));
286 private void jbInit() throws Exception
288 this.setLayout(borderLayout2);
290 database.setFont(JvSwingUtils.getLabelFont());
291 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
292 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
293 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
294 jLabel1.setText(MessageManager
295 .getString("label.separate_multiple_accession_ids"));
297 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
299 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
300 replacePunctuation.setText(MessageManager
301 .getString("label.replace_commas_semicolons"));
302 ok.setText(MessageManager.getString("action.ok"));
303 ok.addActionListener(new ActionListener()
306 public void actionPerformed(ActionEvent e)
308 ok_actionPerformed();
311 clear.setText(MessageManager.getString("action.clear"));
312 clear.addActionListener(new ActionListener()
315 public void actionPerformed(ActionEvent e)
317 clear_actionPerformed();
321 example.setText(MessageManager.getString("label.example"));
322 example.addActionListener(new ActionListener()
325 public void actionPerformed(ActionEvent e)
327 example_actionPerformed();
330 close.setText(MessageManager.getString("action.close"));
331 close.addActionListener(new ActionListener()
334 public void actionPerformed(ActionEvent e)
336 close_actionPerformed(e);
339 textArea.setFont(JvSwingUtils.getLabelFont());
340 textArea.setLineWrap(true);
341 textArea.addKeyListener(new KeyAdapter()
344 public void keyPressed(KeyEvent e)
346 if (e.getKeyCode() == KeyEvent.VK_ENTER)
348 ok_actionPerformed();
352 jPanel3.setLayout(borderLayout1);
353 borderLayout1.setVgap(5);
355 jPanel1.add(example);
358 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
359 jPanel2.setLayout(borderLayout3);
360 databaseButt = database.getDatabaseSelectorButton();
361 databaseButt.setFont(JvSwingUtils.getLabelFont());
362 database.addActionListener(new ActionListener()
365 public void actionPerformed(ActionEvent e)
368 String currentSelection = database.getSelectedItem();
369 if (!currentSelection.equalsIgnoreCase("pdb"))
373 if (currentSelection.equalsIgnoreCase("pdb")
374 && (database.action == KeyEvent.VK_ENTER || ((debounceTrap % 2) == 0)))
378 database.action = -1;
383 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
384 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
385 JPanel jPanel2a = new JPanel(new BorderLayout());
386 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
387 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
388 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
389 // jPanel2.setPreferredSize(new Dimension())
390 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
391 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
392 this.add(jPanel3, java.awt.BorderLayout.CENTER);
393 this.add(jPanel2, java.awt.BorderLayout.NORTH);
394 jScrollPane1.getViewport().add(textArea);
398 private void pdbSourceAction()
400 databaseButt.setText(database.getSelectedItem());
401 new PDBSearchPanel(this);
405 private void otherSourceAction()
409 databaseButt.setText(database.getSelectedItem()
410 + (database.getSelectedSources().size() > 1 ? " (and "
411 + database.getSelectedSources().size() + " others)"
413 String eq = database.getExampleQueries();
414 dbeg.setText(MessageManager.formatMessage(
415 "label.example_query_param", new String[] { eq }));
416 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
417 for (DbSourceProxy dbs : database.getSelectedSources())
419 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
425 replacePunctuation.setEnabled(enablePunct);
427 } catch (Exception ex)
430 replacePunctuation.setEnabled(true);
435 protected void example_actionPerformed()
437 DbSourceProxy db = null;
440 textArea.setText(database.getExampleQueries());
441 } catch (Exception ex)
447 protected void clear_actionPerformed()
449 textArea.setText("");
453 public void close_actionPerformed(ActionEvent e)
457 frame.setClosed(true);
458 } catch (Exception ex)
463 public void ok_actionPerformed()
465 databaseButt.setEnabled(false);
466 example.setEnabled(false);
467 textArea.setEnabled(false);
468 ok.setEnabled(false);
469 close.setEnabled(false);
471 Thread worker = new Thread(this);
475 private void resetDialog()
477 databaseButt.setEnabled(true);
478 example.setEnabled(true);
479 textArea.setEnabled(true);
481 close.setEnabled(true);
488 if (!database.hasSelection())
490 error += "Please select the source database\n";
492 // TODO: make this transformation more configurable
493 com.stevesoft.pat.Regex empty;
494 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
496 empty = new com.stevesoft.pat.Regex(
497 // replace commas and spaces with a semicolon
498 "(\\s|[,; ])+", ";");
502 // just turn spaces and semicolons into single semicolons
503 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
505 textArea.setText(empty.replaceAll(textArea.getText()));
506 // see if there's anthing to search with
507 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
510 error += "Please enter a (semi-colon separated list of) database id(s)";
512 if (error.length() > 0)
514 showErrorMessage(error);
518 // TODO: Refactor to GUI independent code and write tests.
519 // indicate if successive sources should be merged into one alignment.
520 boolean addToLast = false;
521 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
522 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
523 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
526 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
528 Iterator<String> en = Arrays.asList(new String[0]).iterator();
529 int nqueries = qries.length;
530 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
532 if (!en.hasNext() && nextfetch.size() > 0)
534 en = nextfetch.iterator();
535 nqueries = nextfetch.size();
536 // save the remaining queries in the original array
537 qries = nextfetch.toArray(new String[nqueries]);
538 nextfetch = new ArrayList<String>();
541 DbSourceProxy proxy = proxies.next();
542 boolean isAliSource = false;
547 .setProgressBar(MessageManager.formatMessage(
548 "status.fetching_sequence_queries_from",
550 Integer.valueOf(nqueries).toString(),
551 proxy.getDbName() }), Thread.currentThread()
553 isAliSource = proxy.isAlignmentSource();
554 if (proxy.getAccessionSeparator() == null)
558 String item = en.next();
565 // give the server a chance to breathe
567 } catch (Exception e)
574 AlignmentI indres = null;
577 indres = proxy.getSequenceRecords(item);
578 } catch (OutOfMemoryError oome)
580 new OOMWarning("fetching " + item + " from "
581 + proxy.getDbName(), oome, this);
592 } catch (Exception e)
594 jalview.bin.Cache.log.info("Error retrieving " + item
595 + " from " + proxy.getDbName(), e);
602 StringBuffer multiacc = new StringBuffer();
603 ArrayList<String> tosend = new ArrayList<String>();
606 String nel = en.next();
608 multiacc.append(nel);
611 multiacc.append(proxy.getAccessionSeparator());
618 List<String> nores = new ArrayList<String>();
619 rslt = proxy.getSequenceRecords(multiacc.toString());
620 if (rslt == null || rslt.getHeight() == 0)
622 // no results - pass on all queries to next source
623 nextfetch.addAll(tosend);
627 aresultq.add(multiacc.toString());
630 rs = rslt.getSequencesArray();
631 // search for each query in the dbrefs associated with each
634 // ones we do not find will be used to query next source (if any)
635 for (String q : tosend)
637 DBRefEntry dbr = new DBRefEntry(), found[] = null;
638 dbr.setSource(proxy.getDbSource());
639 dbr.setVersion(null);
640 String accId = proxy.getAccessionIdFromQuery(q);
641 dbr.setAccessionId(accId);
642 boolean rfound = false;
643 for (int r = 0; r < rs.length; r++)
646 && (found = DBRefUtils.searchRefs(
647 rs[r].getDBRefs(), dbr)) != null
660 } catch (OutOfMemoryError oome)
662 new OOMWarning("fetching " + multiacc + " from "
663 + database.getSelectedItem(), oome, this);
667 } catch (Exception e)
669 showErrorMessage("Error retrieving " + textArea.getText()
670 + " from " + database.getSelectedItem());
672 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
673 System.err.println("Retrieval failed for source ='"
674 + database.getSelectedItem() + "' and query\n'"
675 + textArea.getText() + "'\n");
677 } catch (OutOfMemoryError e)
679 // resets dialog box - so we don't use OOMwarning here.
680 showErrorMessage("Out of Memory when retrieving "
683 + database.getSelectedItem()
684 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
688 showErrorMessage("Serious Error retrieving " + textArea.getText()
689 + " from " + database.getSelectedItem());
692 // Stack results ready for opening in alignment windows
693 if (aresult != null && aresult.size() > 0)
695 AlignmentI ar = null;
699 // new window for each result
700 while (aresult.size() > 0)
702 presult.add(aresult.remove(0));
703 presultTitle.add(aresultq.remove(0) + " "
704 + getDefaultRetrievalTitle());
710 if (addToLast && presult.size() > 0)
712 ar = presult.remove(presult.size() - 1);
713 titl = presultTitle.remove(presultTitle.size() - 1);
715 // concatenate all results in one window
716 while (aresult.size() > 0)
720 ar = aresult.remove(0);
724 ar.append(aresult.remove(0));
730 presultTitle.add(titl);
733 guiWindow.setProgressBar(MessageManager
734 .getString("status.finshed_querying"), Thread.currentThread()
737 guiWindow.setProgressBar(
738 (presult.size() > 0) ? MessageManager
739 .getString("status.parsing_results") : MessageManager
740 .getString("status.processing"), Thread.currentThread()
743 while (presult.size() > 0)
745 parseResult(presult.remove(0), presultTitle.remove(0), null);
747 // only remove visual delay after we finished parsing.
748 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
749 if (nextfetch.size() > 0)
751 StringBuffer sb = new StringBuffer();
752 sb.append("Didn't retrieve the following "
753 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
754 + " queries") + ": \n");
755 int l = sb.length(), lr = 0;
756 for (String s : nextfetch)
758 if (l != sb.length())
762 if (lr - sb.length() > 40)
768 showErrorMessage(sb.toString());
773 AlignmentI parseResult(String result, String title)
775 String format = new IdentifyFile().identify(result, "Paste");
776 AlignmentI sequences = null;
777 if (FormatAdapter.isValidFormat(format))
782 sequences = new FormatAdapter().readFile(result.toString(),
784 } catch (Exception ex)
788 if (sequences != null)
790 return parseResult(sequences, title, format);
795 showErrorMessage("Error retrieving " + textArea.getText() + " from "
796 + database.getSelectedItem());
804 * @return a standard title for any results retrieved using the currently
805 * selected source and settings
807 public String getDefaultRetrievalTitle()
809 return "Retrieved from " + database.getSelectedItem();
812 AlignmentI parseResult(AlignmentI al, String title,
813 String currentFileFormat)
816 if (al != null && al.getHeight() > 0)
820 title = getDefaultRetrievalTitle();
822 if (alignFrame == null)
824 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
825 AlignFrame.DEFAULT_HEIGHT);
826 if (currentFileFormat != null)
828 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
830 // NON-FormatAdapter Sourced
834 SequenceFeature[] sfs = null;
835 List<SequenceI> alsqs;
836 synchronized (alsqs = al.getSequences())
838 for (SequenceI sq : alsqs)
840 if ((sfs = sq.getSequenceFeatures()) != null)
844 af.setShowSeqFeatures(true);
851 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
852 AlignFrame.DEFAULT_HEIGHT);
854 af.statusBar.setText(MessageManager
855 .getString("label.successfully_pasted_alignment_file"));
859 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
861 } catch (Exception ex)
867 alignFrame.viewport.addAlignment(al, title);
873 void showErrorMessage(final String error)
876 javax.swing.SwingUtilities.invokeLater(new Runnable()
881 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
882 MessageManager.getString("label.error_retrieving_data"),
883 JOptionPane.WARNING_MESSAGE);
888 public IProgressIndicator getProgressIndicator()
890 return progressIndicator;
893 public void setProgressIndicator(IProgressIndicator progressIndicator)
895 this.progressIndicator = progressIndicator;