2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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;
29 import com.stevesoft.pat.Regex;
31 import jalview.datamodel.*;
33 import jalview.util.DBRefUtils;
34 import jalview.util.MessageManager;
35 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
36 import jalview.ws.seqfetcher.DbSourceProxy;
37 import java.awt.BorderLayout;
39 public class SequenceFetcher extends JPanel implements Runnable
41 // ASequenceFetcher sfetch;
44 IProgressIndicator guiWindow;
46 AlignFrame alignFrame;
50 final String noDbSelected = "-- Select Database --";
52 private static jalview.ws.SequenceFetcher sfetch = null;
54 private static long lastDasSourceRegistry = -3;
56 private static DasSourceRegistryI dasRegistry = null;
58 private static boolean _initingFetcher = false;
60 private static Thread initingThread = null;
63 * Blocking method that initialises and returns the shared instance of the
64 * SequenceFetcher client
67 * - where the initialisation delay message should be shown
68 * @return the singleton instance of the sequence fetcher client
70 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
71 final IProgressIndicator guiWindow)
73 if (_initingFetcher && initingThread != null && initingThread.isAlive())
75 if (guiWindow != null)
77 guiWindow.setProgressBar(
78 "Waiting for Sequence Database Fetchers to initialise",
79 Thread.currentThread().hashCode());
81 // initting happening on another thread - so wait around to see if it
83 while (_initingFetcher && initingThread != null
84 && initingThread.isAlive())
94 if (guiWindow != null)
96 guiWindow.setProgressBar(
97 "Waiting for Sequence Database Fetchers to initialise",
98 Thread.currentThread().hashCode());
102 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
103 || lastDasSourceRegistry != (jalview.bin.Cache
104 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
105 .getDasSourceRegistry().getLocalSourceString())
108 _initingFetcher = true;
109 initingThread = Thread.currentThread();
111 * give a visual indication that sequence fetcher construction is occuring
113 if (guiWindow != null)
115 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
116 Thread.currentThread().hashCode());
118 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
119 dasRegistry.refreshSources();
121 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
122 if (guiWindow != null)
124 guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
125 Thread.currentThread().hashCode());
127 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
128 .getLocalSourceString()).hashCode();
130 _initingFetcher = false;
131 initingThread = null;
136 public SequenceFetcher(IProgressIndicator guiIndic)
138 final IProgressIndicator guiWindow = guiIndic;
139 final SequenceFetcher us = this;
140 // launch initialiser thread
141 Thread sf = new Thread(new Runnable()
147 if (getSequenceFetcherSingleton(guiWindow) != null)
149 us.initGui(guiWindow);
153 javax.swing.SwingUtilities.invokeLater(new Runnable()
159 .showInternalMessageDialog(
161 "Could not create the sequence fetcher client. Check error logs for details.",
162 "Couldn't create SequenceFetcher",
163 JOptionPane.ERROR_MESSAGE);
167 // raise warning dialog
174 private class DatabaseAuthority extends DefaultMutableTreeNode
179 private class DatabaseSource extends DefaultMutableTreeNode
185 * called by thread spawned by constructor
189 private void initGui(IProgressIndicator guiWindow)
191 this.guiWindow = guiWindow;
192 if (guiWindow instanceof AlignFrame)
194 alignFrame = (AlignFrame) guiWindow;
196 database = new JDatabaseTree(sfetch);
200 } catch (Exception ex)
202 ex.printStackTrace();
205 frame = new JInternalFrame();
206 frame.setContentPane(this);
207 if (new jalview.util.Platform().isAMac())
209 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
213 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
217 private String getFrameTitle()
219 return ((alignFrame == null) ? "New " : "Additional ")
220 + "Sequence Fetcher";
223 private void jbInit() throws Exception
225 this.setLayout(borderLayout2);
227 database.setFont(JvSwingUtils.getLabelFont());
228 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
229 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
230 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
231 jLabel1.setText(MessageManager.getString("label.separate_multiple_accession_ids"));
233 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
235 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
236 replacePunctuation.setText(MessageManager.getString("label.replace_commas_semicolons"));
237 ok.setText(MessageManager.getString("action.ok"));
238 ok.addActionListener(new ActionListener()
241 public void actionPerformed(ActionEvent e)
243 ok_actionPerformed();
246 clear.setText(MessageManager.getString("action.clear"));
247 clear.addActionListener(new ActionListener()
250 public void actionPerformed(ActionEvent e)
252 clear_actionPerformed();
256 example.setText(MessageManager.getString("label.example"));
257 example.addActionListener(new ActionListener()
260 public void actionPerformed(ActionEvent e)
262 example_actionPerformed();
265 close.setText(MessageManager.getString("action.close"));
266 close.addActionListener(new ActionListener()
269 public void actionPerformed(ActionEvent e)
271 close_actionPerformed(e);
274 textArea.setFont(JvSwingUtils.getLabelFont());
275 textArea.setLineWrap(true);
276 textArea.addKeyListener(new KeyAdapter()
279 public void keyPressed(KeyEvent e)
281 if (e.getKeyCode() == KeyEvent.VK_ENTER)
282 ok_actionPerformed();
285 jPanel3.setLayout(borderLayout1);
286 borderLayout1.setVgap(5);
288 jPanel1.add(example);
291 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
292 jPanel2.setLayout(borderLayout3);
293 databaseButt = database.getDatabaseSelectorButton();
294 databaseButt.setFont(JvSwingUtils.getLabelFont());
295 database.addActionListener(new ActionListener()
299 public void actionPerformed(ActionEvent e)
303 databaseButt.setText(database.getSelectedItem()
304 + (database.getSelectedSources().size() > 1 ? " (and "
305 + database.getSelectedSources().size()
307 String eq = database.getExampleQueries();
308 dbeg.setText(MessageManager.formatMessage("label.example_query_param", new String[]{eq}));
309 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
310 for (DbSourceProxy dbs : database.getSelectedSources())
312 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
318 replacePunctuation.setEnabled(enablePunct);
320 } catch (Exception ex)
323 replacePunctuation.setEnabled(true);
329 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
330 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
331 JPanel jPanel2a = new JPanel(new BorderLayout());
332 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
333 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
334 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
335 // jPanel2.setPreferredSize(new Dimension())
336 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
337 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
338 this.add(jPanel3, java.awt.BorderLayout.CENTER);
339 this.add(jPanel2, java.awt.BorderLayout.NORTH);
340 jScrollPane1.getViewport().add(textArea);
344 protected void example_actionPerformed()
346 DbSourceProxy db = null;
349 textArea.setText(database.getExampleQueries());
350 } catch (Exception ex)
356 protected void clear_actionPerformed()
358 textArea.setText("");
362 JLabel dbeg = new JLabel();
364 JDatabaseTree database;
366 JButton databaseButt;
368 JLabel jLabel1 = new JLabel();
370 JCheckBox replacePunctuation = new JCheckBox();
372 JButton ok = new JButton();
374 JButton clear = new JButton();
376 JButton example = new JButton();
378 JButton close = new JButton();
380 JPanel jPanel1 = new JPanel();
382 JTextArea textArea = new JTextArea();
384 JScrollPane jScrollPane1 = new JScrollPane();
386 JPanel jPanel2 = new JPanel();
388 JPanel jPanel3 = new JPanel();
390 JPanel jPanel4 = new JPanel();
392 BorderLayout borderLayout1 = new BorderLayout();
394 BorderLayout borderLayout2 = new BorderLayout();
396 BorderLayout borderLayout3 = new BorderLayout();
398 public void close_actionPerformed(ActionEvent e)
402 frame.setClosed(true);
403 } catch (Exception ex)
408 public void ok_actionPerformed()
410 databaseButt.setEnabled(false);
411 example.setEnabled(false);
412 textArea.setEnabled(false);
413 ok.setEnabled(false);
414 close.setEnabled(false);
416 Thread worker = new Thread(this);
420 private void resetDialog()
422 databaseButt.setEnabled(true);
423 example.setEnabled(true);
424 textArea.setEnabled(true);
426 close.setEnabled(true);
433 if (!database.hasSelection())
435 error += "Please select the source database\n";
437 // TODO: make this transformation more configurable
438 com.stevesoft.pat.Regex empty;
439 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
441 empty = new com.stevesoft.pat.Regex(
442 // replace commas and spaces with a semicolon
443 "(\\s|[,; ])+", ";");
447 // just turn spaces and semicolons into single semicolons
448 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
450 textArea.setText(empty.replaceAll(textArea.getText()));
451 // see if there's anthing to search with
452 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
455 error += "Please enter a (semi-colon separated list of) database id(s)";
457 if (error.length() > 0)
459 showErrorMessage(error);
463 // indicate if successive sources should be merged into one alignment.
464 boolean addToLast = false;
465 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
466 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
467 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
470 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
472 Iterator<String> en = Arrays.asList(new String[0]).iterator();
473 int nqueries = qries.length;
474 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
476 if (!en.hasNext() && nextfetch.size() > 0)
478 en = nextfetch.iterator();
479 nqueries = nextfetch.size();
480 // save the remaining queries in the original array
481 qries = nextfetch.toArray(new String[nqueries]);
482 nextfetch = new ArrayList<String>();
485 DbSourceProxy proxy = proxies.next();
486 boolean isAliSource = false;
490 guiWindow.setProgressBar("Fetching " + nqueries
491 + " sequence queries from " + proxy.getDbName(), Thread
492 .currentThread().hashCode());
493 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
494 if (proxy.getAccessionSeparator() == null)
498 String item = en.next();
505 // give the server a chance to breathe
507 } catch (Exception e)
514 AlignmentI indres = null;
517 indres = proxy.getSequenceRecords(item);
518 } catch (OutOfMemoryError oome)
520 new OOMWarning("fetching " + item + " from "
521 + proxy.getDbName(), oome, this);
532 } catch (Exception e)
534 jalview.bin.Cache.log.info("Error retrieving " + item
535 + " from " + proxy.getDbName(), e);
542 StringBuffer multiacc = new StringBuffer();
543 ArrayList<String> tosend = new ArrayList<String>();
546 String nel = en.next();
548 multiacc.append(nel);
551 multiacc.append(proxy.getAccessionSeparator());
558 List<String> nores = new ArrayList<String>();
559 rslt = proxy.getSequenceRecords(multiacc.toString());
560 if (rslt == null || rslt.getHeight() == 0)
562 // no results - pass on all queries to next source
563 nextfetch.addAll(tosend);
567 aresultq.add(multiacc.toString());
570 rs = rslt.getSequencesArray();
571 // search for each query in the dbrefs associated with each
574 // ones we do not find will be used to query next source (if any)
575 for (String q : tosend)
577 DBRefEntry dbr = new DBRefEntry(), found[] = null;
578 dbr.setSource(proxy.getDbSource());
579 dbr.setVersion(null);
580 if (proxy.getAccessionValidator() != null)
582 Regex vgr = proxy.getAccessionValidator();
584 if (vgr.numSubs() > 0)
586 dbr.setAccessionId(vgr.stringMatched(1));
590 dbr.setAccessionId(vgr.stringMatched());
595 dbr.setAccessionId(q);
597 boolean rfound = false;
598 for (int r = 0; r < rs.length; r++)
601 && (found = DBRefUtils.searchRefs(
602 rs[r].getDBRef(), dbr)) != null
616 } catch (OutOfMemoryError oome)
618 new OOMWarning("fetching " + multiacc + " from "
619 + database.getSelectedItem(), oome, this);
623 } catch (Exception e)
625 showErrorMessage("Error retrieving " + textArea.getText()
626 + " from " + database.getSelectedItem());
628 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
629 System.err.println("Retrieval failed for source ='"
630 + database.getSelectedItem() + "' and query\n'"
631 + textArea.getText() + "'\n");
633 } catch (OutOfMemoryError e)
635 // resets dialog box - so we don't use OOMwarning here.
636 showErrorMessage("Out of Memory when retrieving "
639 + database.getSelectedItem()
640 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
644 showErrorMessage("Serious Error retrieving " + textArea.getText()
645 + " from " + database.getSelectedItem());
648 // Stack results ready for opening in alignment windows
649 if (aresult != null && aresult.size() > 0)
651 AlignmentI ar = null;
655 // new window for each result
656 while (aresult.size() > 0)
658 presult.add(aresult.remove(0));
659 presultTitle.add(aresultq.remove(0) + " "
660 + getDefaultRetrievalTitle());
666 if (addToLast && presult.size() > 0)
668 ar = presult.remove(presult.size() - 1);
669 titl = presultTitle.remove(presultTitle.size() - 1);
671 // concatenate all results in one window
672 while (aresult.size() > 0)
676 ar = aresult.remove(0);
680 ar.append(aresult.remove(0));
686 presultTitle.add(titl);
689 guiWindow.setProgressBar("Finished querying", Thread.currentThread()
692 guiWindow.setProgressBar((presult.size() > 0) ? "Parsing results."
693 : "Processing ..", Thread.currentThread().hashCode());
695 while (presult.size() > 0)
697 parseResult(presult.remove(0), presultTitle.remove(0), null);
699 // only remove visual delay after we finished parsing.
700 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
701 if (nextfetch.size() > 0)
703 StringBuffer sb = new StringBuffer();
704 sb.append("Didn't retrieve the following "
705 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
706 + " queries") + ": \n");
707 int l = sb.length(), lr = 0;
708 for (String s : nextfetch)
710 if (l != sb.length())
714 if (lr - sb.length() > 40)
720 showErrorMessage(sb.toString());
725 AlignmentI parseResult(String result, String title)
727 String format = new IdentifyFile().Identify(result, "Paste");
728 Alignment sequences = null;
729 if (FormatAdapter.isValidFormat(format))
734 sequences = new FormatAdapter().readFile(result.toString(),
736 } catch (Exception ex)
740 if (sequences != null)
742 return parseResult(sequences, title, format);
747 showErrorMessage("Error retrieving " + textArea.getText() + " from "
748 + database.getSelectedItem());
756 * @return a standard title for any results retrieved using the currently
757 * selected source and settings
759 public String getDefaultRetrievalTitle()
761 return "Retrieved from " + database.getSelectedItem();
764 AlignmentI parseResult(AlignmentI al, String title,
765 String currentFileFormat)
768 if (al != null && al.getHeight() > 0)
770 if (alignFrame == null)
772 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
773 AlignFrame.DEFAULT_HEIGHT);
774 if (currentFileFormat != null)
776 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
778 // NON-FormatAdapter Sourced
784 title = getDefaultRetrievalTitle();
786 SequenceFeature[] sfs = null;
787 List<SequenceI> alsqs;
788 synchronized (alsqs = al.getSequences())
790 for (SequenceI sq : alsqs)
792 if ((sfs = (sq).getDatasetSequence().getSequenceFeatures()) != null)
796 af.setShowSeqFeatures(true);
803 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
804 AlignFrame.DEFAULT_HEIGHT);
806 af.statusBar.setText(MessageManager.getString("label.successfully_pasted_alignment_file"));
810 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
812 } catch (Exception ex)
818 for (int i = 0; i < al.getHeight(); i++)
820 alignFrame.viewport.getAlignment().addSequence(
821 al.getSequenceAt(i)); // this
828 alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
830 alignFrame.viewport.getAlignment().getWidth();
831 alignFrame.viewport.firePropertyChange("alignment", null,
832 alignFrame.viewport.getAlignment().getSequences());
838 void showErrorMessage(final String error)
841 javax.swing.SwingUtilities.invokeLater(new Runnable()
846 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
847 MessageManager.getString("label.error_retrieving_data"), JOptionPane.WARNING_MESSAGE);