2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.util.List;
24 import java.awt.event.*;
27 import javax.swing.tree.DefaultMutableTreeNode;
28 import jalview.datamodel.*;
30 import jalview.util.DBRefUtils;
31 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
32 import jalview.ws.seqfetcher.DbSourceProxy;
33 import java.awt.BorderLayout;
35 public class SequenceFetcher extends JPanel implements Runnable
37 // ASequenceFetcher sfetch;
40 IProgressIndicator guiWindow;
42 AlignFrame alignFrame;
46 final String noDbSelected = "-- Select Database --";
48 private static jalview.ws.SequenceFetcher sfetch = null;
50 private static long lastDasSourceRegistry = -3;
52 private static DasSourceRegistryI dasRegistry = null;
54 private static boolean _initingFetcher = false;
56 private static Thread initingThread = null;
59 * Blocking method that initialises and returns the shared instance of the
60 * SequenceFetcher client
63 * - where the initialisation delay message should be shown
64 * @return the singleton instance of the sequence fetcher client
66 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
67 final IProgressIndicator guiWindow)
69 if (_initingFetcher && initingThread != null && initingThread.isAlive())
71 if (guiWindow != null)
73 guiWindow.setProgressBar(
74 "Waiting for Sequence Database Fetchers to initialise",
75 Thread.currentThread().hashCode());
77 // initting happening on another thread - so wait around to see if it
79 while (_initingFetcher && initingThread != null
80 && initingThread.isAlive())
90 if (guiWindow != null)
92 guiWindow.setProgressBar(
93 "Waiting for Sequence Database Fetchers to initialise",
94 Thread.currentThread().hashCode());
98 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
99 || lastDasSourceRegistry != (jalview.bin.Cache
100 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
101 .getDasSourceRegistry().getLocalSourceString())
104 _initingFetcher = true;
105 initingThread = Thread.currentThread();
107 * give a visual indication that sequence fetcher construction is occuring
109 if (guiWindow != null)
111 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
112 Thread.currentThread().hashCode());
114 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
115 dasRegistry.refreshSources();
117 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
118 if (guiWindow != null)
120 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
121 Thread.currentThread().hashCode());
123 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
124 .getLocalSourceString()).hashCode();
126 _initingFetcher = false;
127 initingThread = null;
132 public SequenceFetcher(IProgressIndicator guiIndic)
134 final IProgressIndicator guiWindow = guiIndic;
135 final SequenceFetcher us = this;
136 // launch initialiser thread
137 Thread sf = new Thread(new Runnable()
143 if (getSequenceFetcherSingleton(guiWindow) != null)
145 us.initGui(guiWindow);
149 javax.swing.SwingUtilities.invokeLater(new Runnable()
155 .showInternalMessageDialog(
157 "Could not create the sequence fetcher client. Check error logs for details.",
158 "Couldn't create SequenceFetcher",
159 JOptionPane.ERROR_MESSAGE);
163 // raise warning dialog
170 private class DatabaseAuthority extends DefaultMutableTreeNode
175 private class DatabaseSource extends DefaultMutableTreeNode
181 * called by thread spawned by constructor
185 private void initGui(IProgressIndicator guiWindow)
187 this.guiWindow = guiWindow;
188 if (guiWindow instanceof AlignFrame)
190 alignFrame = (AlignFrame) guiWindow;
192 database = new JDatabaseTree(sfetch);
196 } catch (Exception ex)
198 ex.printStackTrace();
201 frame = new JInternalFrame();
202 frame.setContentPane(this);
203 if (new jalview.util.Platform().isAMac())
205 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
209 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
213 private String getFrameTitle()
215 return ((alignFrame == null) ? "New " : "Additional ")
216 + "Sequence Fetcher";
219 private void jbInit() throws Exception
221 this.setLayout(borderLayout2);
223 database.setFont(JvSwingUtils.getLabelFont());
224 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
225 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
226 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
227 jLabel1.setText("Separate multiple accession ids with semi colon \";\"");
229 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
231 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
232 replacePunctuation.setText("Replace commas with semi-colons");
234 ok.addActionListener(new ActionListener()
237 public void actionPerformed(ActionEvent e)
239 ok_actionPerformed();
242 clear.setText("Clear");
243 clear.addActionListener(new ActionListener()
246 public void actionPerformed(ActionEvent e)
248 clear_actionPerformed();
252 example.setText("Example");
253 example.addActionListener(new ActionListener()
256 public void actionPerformed(ActionEvent e)
258 example_actionPerformed();
261 close.setText("Close");
262 close.addActionListener(new ActionListener()
265 public void actionPerformed(ActionEvent e)
267 close_actionPerformed(e);
270 textArea.setFont(JvSwingUtils.getLabelFont());
271 textArea.setLineWrap(true);
272 textArea.addKeyListener(new KeyAdapter()
275 public void keyPressed(KeyEvent e)
277 if (e.getKeyCode() == KeyEvent.VK_ENTER)
278 ok_actionPerformed();
281 jPanel3.setLayout(borderLayout1);
282 borderLayout1.setVgap(5);
284 jPanel1.add(example);
287 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
288 jPanel2.setLayout(borderLayout3);
289 databaseButt = database.getDatabaseSelectorButton();
290 databaseButt.setFont(JvSwingUtils.getLabelFont());
291 database.addActionListener(new ActionListener()
295 public void actionPerformed(ActionEvent e)
299 databaseButt.setText(database.getSelectedItem()
300 + (database.getSelectedSources().size() > 1 ? " (and "
301 + database.getSelectedSources().size()
303 String eq = database.getExampleQueries();
304 dbeg.setText("Example query: " + eq);
305 boolean enablePunct=!(eq != null && eq.indexOf(",") > -1);
306 for (DbSourceProxy dbs:database.getSelectedSources()) {
307 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
313 replacePunctuation.setEnabled(enablePunct);
315 } catch (Exception ex)
318 replacePunctuation.setEnabled(true);
324 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
325 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
326 JPanel jPanel2a = new JPanel(new BorderLayout());
327 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
328 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
329 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
330 // jPanel2.setPreferredSize(new Dimension())
331 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
332 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
333 this.add(jPanel3, java.awt.BorderLayout.CENTER);
334 this.add(jPanel2, java.awt.BorderLayout.NORTH);
335 jScrollPane1.getViewport().add(textArea);
339 protected void example_actionPerformed()
341 DbSourceProxy db = null;
344 textArea.setText(database.getExampleQueries());
345 } catch (Exception ex)
351 protected void clear_actionPerformed()
353 textArea.setText("");
357 JLabel dbeg = new JLabel();
359 JDatabaseTree database;
361 JButton databaseButt;
363 JLabel jLabel1 = new JLabel();
365 JCheckBox replacePunctuation = new JCheckBox();
367 JButton ok = new JButton();
369 JButton clear = new JButton();
371 JButton example = new JButton();
373 JButton close = new JButton();
375 JPanel jPanel1 = new JPanel();
377 JTextArea textArea = new JTextArea();
379 JScrollPane jScrollPane1 = new JScrollPane();
381 JPanel jPanel2 = new JPanel();
383 JPanel jPanel3 = new JPanel();
385 JPanel jPanel4 = new JPanel();
387 BorderLayout borderLayout1 = new BorderLayout();
389 BorderLayout borderLayout2 = new BorderLayout();
391 BorderLayout borderLayout3 = new BorderLayout();
393 public void close_actionPerformed(ActionEvent e)
397 frame.setClosed(true);
398 } catch (Exception ex)
403 public void ok_actionPerformed()
405 database.setEnabled(false);
406 textArea.setEnabled(false);
407 ok.setEnabled(false);
408 close.setEnabled(false);
410 Thread worker = new Thread(this);
414 private void resetDialog()
416 database.setEnabled(true);
417 textArea.setEnabled(true);
419 close.setEnabled(true);
426 if (!database.hasSelection())
428 error += "Please select the source database\n";
430 // TODO: make this transformation more configurable
431 com.stevesoft.pat.Regex empty;
432 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
434 empty = new com.stevesoft.pat.Regex(
435 // replace commas and spaces with a semicolon
436 "(\\s|[,; ])+", ";");
440 // just turn spaces and semicolons into single semicolons
441 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
443 textArea.setText(empty.replaceAll(textArea.getText()));
444 // see if there's anthing to search with
445 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
448 error += "Please enter a (semi-colon separated list of) database id(s)";
450 if (error.length() > 0)
452 showErrorMessage(error);
456 // indicate if successive sources should be merged into one alignment.
457 boolean addToLast=false;
458 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
459 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
460 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
463 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
465 Iterator<String> en = Arrays.asList(new String[0]).iterator();
466 int nqueries = qries.length;
467 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
469 if (!en.hasNext() && nextfetch.size() > 0)
471 en = nextfetch.iterator();
472 nqueries = nextfetch.size();
473 // save the remaining queries in the original array
474 qries = nextfetch.toArray(new String[nqueries]);
475 nextfetch = new ArrayList<String>();
478 DbSourceProxy proxy = proxies.next();
479 boolean isAliSource = false;
483 guiWindow.setProgressBar("Fetching " + nqueries
484 + " sequence queries from " + proxy.getDbName(), Thread
485 .currentThread().hashCode());
486 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
487 if (proxy.getAccessionSeparator() == null)
491 String item = en.next();
498 // give the server a chance to breathe
500 } catch (Exception e)
507 AlignmentI indres = null;
510 indres = proxy.getSequenceRecords(item);
511 } catch (OutOfMemoryError oome)
513 new OOMWarning("fetching " + item + " from "
514 + proxy.getDbName(), oome, this);
525 } catch (Exception e)
527 jalview.bin.Cache.log.info("Error retrieving " + item
528 + " from " + proxy.getDbName(), e);
535 StringBuffer multiacc = new StringBuffer();
536 ArrayList<String> tosend = new ArrayList<String>();
539 String nel = en.next();
541 multiacc.append(nel);
544 multiacc.append(proxy.getAccessionSeparator());
551 List<String> nores = new ArrayList<String>();
552 rslt = proxy.getSequenceRecords(multiacc.toString());
553 if (rslt == null || rslt.getHeight() == 0)
555 // no results - pass on all queries to next source
556 nextfetch.addAll(tosend);
560 aresultq.add(multiacc.toString());
563 rs = rslt.getSequencesArray();
564 // search for each query in the dbrefs associated with each
567 // ones we do not find will be used to query next source (if any)
568 for (String q : tosend)
570 DBRefEntry dbr = new DBRefEntry(), found[] = null;
571 dbr.setSource(proxy.getDbSource());
572 boolean rfound = false;
573 for (int r = 0; r < rs.length; r++)
576 && (found = DBRefUtils.searchRefs(
577 rs[r].getDBRef(), dbr)) != null
591 } catch (OutOfMemoryError oome)
593 new OOMWarning("fetching " + multiacc + " from "
594 + database.getSelectedItem(), oome, this);
598 } catch (Exception e)
600 showErrorMessage("Error retrieving " + textArea.getText()
601 + " from " + database.getSelectedItem());
603 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
604 System.err.println("Retrieval failed for source ='"
605 + database.getSelectedItem() + "' and query\n'"
606 + textArea.getText() + "'\n");
608 } catch (OutOfMemoryError e)
610 // resets dialog box - so we don't use OOMwarning here.
611 showErrorMessage("Out of Memory when retrieving "
614 + database.getSelectedItem()
615 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
619 showErrorMessage("Serious Error retrieving " + textArea.getText()
620 + " from " + database.getSelectedItem());
623 // Stack results ready for opening in alignment windows
624 if (aresult != null && aresult.size() > 0)
626 AlignmentI ar = null;
630 // new window for each result
631 while (aresult.size() > 0)
633 presult.add(aresult.remove(0));
634 presultTitle.add(aresultq.remove(0) + " "
635 + getDefaultRetrievalTitle());
641 if (addToLast && presult.size()>0)
643 ar=presult.remove(presult.size()-1);
644 titl=presultTitle.remove(presultTitle.size()-1);
646 // concatenate all results in one window
647 while (aresult.size() > 0)
651 ar = aresult.remove(0);
655 ar.append(aresult.remove(0));
661 presultTitle.add(titl);
664 guiWindow.setProgressBar("Finished querying", Thread.currentThread().hashCode());
666 guiWindow.setProgressBar((presult.size()>0) ? "Parsing results.":"Processing ..", Thread.currentThread().hashCode());
668 while (presult.size() > 0)
670 parseResult(presult.remove(0), presultTitle.remove(0), null);
672 // only remove visual delay after we finished parsing.
673 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
674 if (nextfetch.size() > 0)
676 StringBuffer sb = new StringBuffer();
677 sb.append("Didn't retrieve the following "
678 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
679 + " queries") + ": \n");
680 int l = sb.length(),lr=0;
681 for (String s : nextfetch)
683 if (l != sb.length())
687 if (lr-sb.length()>40)
693 showErrorMessage(sb.toString());
698 AlignmentI parseResult(String result, String title)
700 String format = new IdentifyFile().Identify(result, "Paste");
701 Alignment sequences = null;
702 if (FormatAdapter.isValidFormat(format))
707 sequences = new FormatAdapter().readFile(result.toString(),
709 } catch (Exception ex)
713 if (sequences != null)
715 return parseResult(sequences, title, format);
720 showErrorMessage("Error retrieving " + textArea.getText() + " from "
721 + database.getSelectedItem());
729 * @return a standard title for any results retrieved using the currently
730 * selected source and settings
732 public String getDefaultRetrievalTitle()
734 return "Retrieved from " + database.getSelectedItem();
737 AlignmentI parseResult(AlignmentI al, String title,
738 String currentFileFormat)
741 if (al != null && al.getHeight() > 0)
743 if (alignFrame == null)
745 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
746 AlignFrame.DEFAULT_HEIGHT);
747 if (currentFileFormat != null)
749 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
751 // NON-FormatAdapter Sourced
757 title = getDefaultRetrievalTitle();
759 SequenceFeature[] sfs = null;
760 List<SequenceI> alsqs;
761 synchronized (alsqs = al.getSequences())
763 for (SequenceI sq : alsqs)
765 if ((sfs = (sq).getDatasetSequence().getSequenceFeatures()) != null)
769 af.setShowSeqFeatures(true);
776 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
777 AlignFrame.DEFAULT_HEIGHT);
779 af.statusBar.setText("Successfully pasted alignment file");
783 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
785 } catch (Exception ex)
791 for (int i = 0; i < al.getHeight(); i++)
793 alignFrame.viewport.getAlignment().addSequence(
794 al.getSequenceAt(i)); // this
801 alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
803 alignFrame.viewport.getAlignment().getWidth();
804 alignFrame.viewport.firePropertyChange("alignment", null,
805 alignFrame.viewport.getAlignment().getSequences());
811 void showErrorMessage(final String error)
814 javax.swing.SwingUtilities.invokeLater(new Runnable()
819 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
820 "Error Retrieving Data", JOptionPane.WARNING_MESSAGE);