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/>.
23 import java.awt.event.*;
26 import javax.swing.tree.DefaultMutableTreeNode;
27 import javax.swing.tree.MutableTreeNode;
28 import javax.swing.tree.TreeModel;
30 import jalview.datamodel.*;
32 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
33 import jalview.ws.seqfetcher.DbSourceProxy;
35 import java.awt.BorderLayout;
37 public class SequenceFetcher extends JPanel implements Runnable
39 // ASequenceFetcher sfetch;
42 IProgressIndicator guiWindow;
44 AlignFrame alignFrame;
48 final String noDbSelected = "-- Select Database --";
50 private static jalview.ws.SequenceFetcher sfetch = null;
52 private static long lastDasSourceRegistry = -3;
54 private static DasSourceRegistryI dasRegistry = null;
56 private static boolean _initingFetcher = false;
58 private static Thread initingThread = null;
61 * Blocking method that initialises and returns the shared instance of the
62 * SequenceFetcher client
65 * - where the initialisation delay message should be shown
66 * @return the singleton instance of the sequence fetcher client
68 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
69 final IProgressIndicator guiWindow)
71 if (_initingFetcher && initingThread != null && initingThread.isAlive())
73 if (guiWindow != null)
75 guiWindow.setProgressBar(
76 "Waiting for Sequence Database Fetchers to initialise",
77 Thread.currentThread().hashCode());
79 // initting happening on another thread - so wait around to see if it
81 while (_initingFetcher && initingThread != null
82 && initingThread.isAlive())
92 if (guiWindow != null)
94 guiWindow.setProgressBar(
95 "Waiting for Sequence Database Fetchers to initialise",
96 Thread.currentThread().hashCode());
100 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
101 || lastDasSourceRegistry != (jalview.bin.Cache
102 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
103 .getDasSourceRegistry().getLocalSourceString())
106 _initingFetcher = true;
107 initingThread = Thread.currentThread();
109 * give a visual indication that sequence fetcher construction is occuring
111 if (guiWindow != null)
113 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
114 Thread.currentThread().hashCode());
116 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
117 dasRegistry.refreshSources();
119 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
120 if (guiWindow != null)
122 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
123 Thread.currentThread().hashCode());
125 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
126 .getLocalSourceString()).hashCode();
128 _initingFetcher = false;
129 initingThread = null;
134 public SequenceFetcher(IProgressIndicator guiIndic)
136 final IProgressIndicator guiWindow = guiIndic;
137 final SequenceFetcher us = this;
138 // launch initialiser thread
139 Thread sf = new Thread(new Runnable()
144 if (getSequenceFetcherSingleton(guiWindow) != null)
146 us.initGui(guiWindow);
150 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()
236 public void actionPerformed(ActionEvent e)
238 ok_actionPerformed();
241 clear.setText("Clear");
242 clear.addActionListener(new ActionListener()
244 public void actionPerformed(ActionEvent e)
246 clear_actionPerformed();
250 example.setText("Example");
251 example.addActionListener(new ActionListener()
253 public void actionPerformed(ActionEvent e)
255 example_actionPerformed();
258 close.setText("Close");
259 close.addActionListener(new ActionListener()
261 public void actionPerformed(ActionEvent e)
263 close_actionPerformed(e);
266 textArea.setFont(JvSwingUtils.getLabelFont());
267 textArea.setLineWrap(true);
268 textArea.addKeyListener(new KeyAdapter()
270 public void keyPressed(KeyEvent e)
272 if (e.getKeyCode() == KeyEvent.VK_ENTER)
273 ok_actionPerformed();
276 jPanel3.setLayout(borderLayout1);
277 borderLayout1.setVgap(5);
279 jPanel1.add(example);
282 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
283 jPanel2.setLayout(borderLayout3);
284 databaseButt = database.getDatabaseSelectorButton();
285 databaseButt.setFont(JvSwingUtils.getLabelFont());
286 database.addActionListener(new ActionListener()
289 public void actionPerformed(ActionEvent e)
291 DbSourceProxy db = null;
294 databaseButt.setText(database.getSelectedItem()
295 + (database.getSelectedSources().size() > 1 ? " (and "
296 + database.getSelectedSources().size()
298 String eq = database.getExampleQueries();
299 dbeg.setText("Example query: " + eq);
300 replacePunctuation.setEnabled(!(eq != null && eq.indexOf(",") > -1));
301 } catch (Exception ex)
304 replacePunctuation.setEnabled(true);
310 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
311 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
312 JPanel jPanel2a = new JPanel(new BorderLayout());
313 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
314 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
315 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
316 // jPanel2.setPreferredSize(new Dimension())
317 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
318 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
319 this.add(jPanel3, java.awt.BorderLayout.CENTER);
320 this.add(jPanel2, java.awt.BorderLayout.NORTH);
321 jScrollPane1.getViewport().add(textArea);
325 protected void example_actionPerformed()
327 DbSourceProxy db = null;
330 textArea.setText(database.getExampleQueries());
331 } catch (Exception ex)
337 protected void clear_actionPerformed()
339 textArea.setText("");
343 JLabel dbeg = new JLabel();
345 JDatabaseTree database;
347 JButton databaseButt;
349 JLabel jLabel1 = new JLabel();
351 JCheckBox replacePunctuation = new JCheckBox();
353 JButton ok = new JButton();
355 JButton clear = new JButton();
357 JButton example = new JButton();
359 JButton close = new JButton();
361 JPanel jPanel1 = new JPanel();
363 JTextArea textArea = new JTextArea();
365 JScrollPane jScrollPane1 = new JScrollPane();
367 JPanel jPanel2 = new JPanel();
369 JPanel jPanel3 = new JPanel();
371 JPanel jPanel4 = new JPanel();
373 BorderLayout borderLayout1 = new BorderLayout();
375 BorderLayout borderLayout2 = new BorderLayout();
377 BorderLayout borderLayout3 = new BorderLayout();
379 public void close_actionPerformed(ActionEvent e)
383 frame.setClosed(true);
384 } catch (Exception ex)
389 public void ok_actionPerformed()
391 database.setEnabled(false);
392 textArea.setEnabled(false);
393 ok.setEnabled(false);
394 close.setEnabled(false);
396 Thread worker = new Thread(this);
400 private void resetDialog()
402 database.setEnabled(true);
403 textArea.setEnabled(true);
405 close.setEnabled(true);
411 if (!database.hasSelection())
413 error += "Please select the source database\n";
415 // TODO: make this transformation more configurable
416 com.stevesoft.pat.Regex empty;
417 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
419 empty = new com.stevesoft.pat.Regex(
420 // replace commas and spaces with a semicolon
421 "(\\s|[,; ])+", ";");
425 // just turn spaces and semicolons into single semicolons
426 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
428 textArea.setText(empty.replaceAll(textArea.getText()));
429 // see if there's anthing to search with
430 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
433 error += "Please enter a (semi-colon separated list of) database id(s)";
435 if (error.length() > 0)
437 showErrorMessage(error);
441 ArrayList<String> aresultq = new ArrayList<String>();
442 ArrayList<AlignmentI> aresult = new ArrayList<AlignmentI>();
443 DbSourceProxy proxy = database.getSelectedSources().get(0);
444 Enumeration en = new StringTokenizer(textArea.getText(), ";");
445 boolean isAliSource = false;
448 guiWindow.setProgressBar(
449 "Fetching Sequences from " + proxy.getDbName(), Thread
450 .currentThread().hashCode());
451 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
452 if (proxy.getAccessionSeparator() == null)
454 while (en.hasMoreElements())
456 String item = (String) en.nextElement();
463 // give the server a chance to breathe
465 } catch (Exception e)
472 AlignmentI indres = null;
475 indres = proxy.getSequenceRecords(item);
476 } catch (OutOfMemoryError oome)
478 new OOMWarning("fetching " + item + " from "
479 + proxy.getDbName(), oome, this);
486 } catch (Exception e)
488 jalview.bin.Cache.log.info("Error retrieving " + item
489 + " from " + proxy.getDbName(), e);
495 StringBuffer multiacc = new StringBuffer();
496 while (en.hasMoreElements())
498 multiacc.append(en.nextElement());
499 if (en.hasMoreElements())
501 multiacc.append(proxy.getAccessionSeparator());
506 aresultq.add(multiacc.toString());
507 aresult.add(proxy.getSequenceRecords(multiacc.toString()));
508 } catch (OutOfMemoryError oome)
510 new OOMWarning("fetching " + multiacc + " from "
511 + database.getSelectedItem(), oome, this);
516 } catch (Exception e)
518 showErrorMessage("Error retrieving " + textArea.getText() + " from "
519 + database.getSelectedItem());
520 // error +="Couldn't retrieve sequences from "+database.getSelectedItem();
521 System.err.println("Retrieval failed for source ='"
522 + database.getSelectedItem() + "' and query\n'"
523 + textArea.getText() + "'\n");
525 } catch (OutOfMemoryError e)
527 // resets dialog box - so we don't use OOMwarning here.
528 showErrorMessage("Out of Memory when retrieving "
531 + database.getSelectedItem()
532 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
536 showErrorMessage("Serious Error retrieving " + textArea.getText()
537 + " from " + database.getSelectedItem());
540 if (aresult != null && aresult.size() > 0)
542 AlignmentI ar = null;
545 // new window for each result
546 while (aresult.size() > 0)
548 parseResult(aresult.remove(0), aresultq.remove(0) + " "
549 + getDefaultRetrievalTitle(), null);
554 // concatenate all results in one window
555 while (aresult.size() > 0)
559 ar = aresult.remove(0);
563 ar.append(aresult.remove(0));
567 parseResult(ar, null, null);
570 // only remove visual delay after we finished parsing.
571 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
575 AlignmentI parseResult(String result, String title)
577 String format = new IdentifyFile().Identify(result, "Paste");
578 Alignment sequences = null;
579 if (FormatAdapter.isValidFormat(format))
584 sequences = new FormatAdapter().readFile(result.toString(),
586 } catch (Exception ex)
590 if (sequences != null)
592 return parseResult(sequences, title, format);
597 showErrorMessage("Error retrieving " + textArea.getText() + " from "
598 + database.getSelectedItem());
606 * @return a standard title for any results retrieved using the currently
607 * selected source and settings
609 public String getDefaultRetrievalTitle()
611 return "Retrieved from " + database.getSelectedItem();
614 AlignmentI parseResult(AlignmentI al, String title,
615 String currentFileFormat)
618 if (al != null && al.getHeight() > 0)
620 if (alignFrame == null)
622 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
623 AlignFrame.DEFAULT_HEIGHT);
624 if (currentFileFormat != null)
626 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
628 // NON-FormatAdapter Sourced
634 title = getDefaultRetrievalTitle();
636 SequenceFeature[] sfs = null;
637 for (Enumeration sq = al.getSequences().elements(); sq
640 if ((sfs = ((SequenceI) sq.nextElement()).getDatasetSequence()
641 .getSequenceFeatures()) != null)
645 af.setShowSeqFeatures(true);
651 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
652 AlignFrame.DEFAULT_HEIGHT);
654 af.statusBar.setText("Successfully pasted alignment file");
658 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
660 } catch (Exception ex)
666 for (int i = 0; i < al.getHeight(); i++)
668 alignFrame.viewport.getAlignment().addSequence(
669 al.getSequenceAt(i)); // this
676 alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
678 alignFrame.viewport.getAlignment().getWidth();
679 alignFrame.viewport.firePropertyChange("alignment", null,
680 alignFrame.viewport.getAlignment().getSequences());
686 void showErrorMessage(final String error)
689 javax.swing.SwingUtilities.invokeLater(new Runnable()
693 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
694 "Error Retrieving Data", JOptionPane.WARNING_MESSAGE);