2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
24 import java.util.List;
27 import java.awt.event.*;
30 import javax.swing.tree.DefaultMutableTreeNode;
32 import com.stevesoft.pat.Regex;
34 import jalview.datamodel.*;
36 import jalview.util.DBRefUtils;
37 import jalview.util.MessageManager;
38 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
39 import jalview.ws.seqfetcher.DbSourceProxy;
40 import java.awt.BorderLayout;
42 public class SequenceFetcher extends JPanel implements Runnable
44 // ASequenceFetcher sfetch;
47 IProgressIndicator guiWindow;
49 AlignFrame alignFrame;
53 final String noDbSelected = "-- Select Database --";
55 private static jalview.ws.SequenceFetcher sfetch = null;
57 private static long lastDasSourceRegistry = -3;
59 private static DasSourceRegistryI dasRegistry = null;
61 private static boolean _initingFetcher = false;
63 private static Thread initingThread = null;
66 * Blocking method that initialises and returns the shared instance of the
67 * SequenceFetcher client
70 * - where the initialisation delay message should be shown
71 * @return the singleton instance of the sequence fetcher client
73 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
74 final IProgressIndicator guiWindow)
76 if (_initingFetcher && initingThread != null && initingThread.isAlive())
78 if (guiWindow != null)
80 guiWindow.setProgressBar(
81 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
82 Thread.currentThread().hashCode());
84 // initting happening on another thread - so wait around to see if it
86 while (_initingFetcher && initingThread != null
87 && initingThread.isAlive())
97 if (guiWindow != null)
99 guiWindow.setProgressBar(
100 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
101 Thread.currentThread().hashCode());
105 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
106 || lastDasSourceRegistry != (jalview.bin.Cache
107 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
108 .getDasSourceRegistry().getLocalSourceString())
111 _initingFetcher = true;
112 initingThread = Thread.currentThread();
114 * give a visual indication that sequence fetcher construction is occuring
116 if (guiWindow != null)
118 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
119 Thread.currentThread().hashCode());
121 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
122 dasRegistry.refreshSources();
124 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
125 if (guiWindow != null)
127 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
128 Thread.currentThread().hashCode());
130 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
131 .getLocalSourceString()).hashCode();
133 _initingFetcher = false;
134 initingThread = null;
139 public SequenceFetcher(IProgressIndicator guiIndic)
141 final IProgressIndicator guiWindow = guiIndic;
142 final SequenceFetcher us = this;
143 // launch initialiser thread
144 Thread sf = new Thread(new Runnable()
150 if (getSequenceFetcherSingleton(guiWindow) != null)
152 us.initGui(guiWindow);
156 javax.swing.SwingUtilities.invokeLater(new Runnable()
162 .showInternalMessageDialog(
164 MessageManager.getString("warn.couldnt_create_sequence_fetcher_client"),
165 MessageManager.getString("label.couldnt_create_sequence_fetcher"),
166 JOptionPane.ERROR_MESSAGE);
170 // raise warning dialog
177 private class DatabaseAuthority extends DefaultMutableTreeNode
182 private class DatabaseSource extends DefaultMutableTreeNode
188 * called by thread spawned by constructor
192 private void initGui(IProgressIndicator guiWindow)
194 this.guiWindow = guiWindow;
195 if (guiWindow instanceof AlignFrame)
197 alignFrame = (AlignFrame) guiWindow;
199 database = new JDatabaseTree(sfetch);
203 } catch (Exception ex)
205 ex.printStackTrace();
208 frame = new JInternalFrame();
209 frame.setContentPane(this);
210 if (new jalview.util.Platform().isAMac())
212 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
216 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
220 private String getFrameTitle()
222 return ((alignFrame == null) ? MessageManager.getString("label.new_sequence_fetcher") : MessageManager.getString("label.additional_sequence_fetcher"));
225 private void jbInit() throws Exception
227 this.setLayout(borderLayout2);
229 database.setFont(JvSwingUtils.getLabelFont());
230 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
231 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
232 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
233 jLabel1.setText(MessageManager
234 .getString("label.separate_multiple_accession_ids"));
236 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
238 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
239 replacePunctuation.setText(MessageManager
240 .getString("label.replace_commas_semicolons"));
241 ok.setText(MessageManager.getString("action.ok"));
242 ok.addActionListener(new ActionListener()
245 public void actionPerformed(ActionEvent e)
247 ok_actionPerformed();
250 clear.setText(MessageManager.getString("action.clear"));
251 clear.addActionListener(new ActionListener()
254 public void actionPerformed(ActionEvent e)
256 clear_actionPerformed();
260 example.setText(MessageManager.getString("label.example"));
261 example.addActionListener(new ActionListener()
264 public void actionPerformed(ActionEvent e)
266 example_actionPerformed();
269 close.setText(MessageManager.getString("action.close"));
270 close.addActionListener(new ActionListener()
273 public void actionPerformed(ActionEvent e)
275 close_actionPerformed(e);
278 textArea.setFont(JvSwingUtils.getLabelFont());
279 textArea.setLineWrap(true);
280 textArea.addKeyListener(new KeyAdapter()
283 public void keyPressed(KeyEvent e)
285 if (e.getKeyCode() == KeyEvent.VK_ENTER)
286 ok_actionPerformed();
289 jPanel3.setLayout(borderLayout1);
290 borderLayout1.setVgap(5);
292 jPanel1.add(example);
295 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
296 jPanel2.setLayout(borderLayout3);
297 databaseButt = database.getDatabaseSelectorButton();
298 databaseButt.setFont(JvSwingUtils.getLabelFont());
299 database.addActionListener(new ActionListener()
303 public void actionPerformed(ActionEvent e)
307 databaseButt.setText(database.getSelectedItem()
308 + (database.getSelectedSources().size() > 1 ? " (and "
309 + database.getSelectedSources().size()
311 String eq = database.getExampleQueries();
312 dbeg.setText(MessageManager.formatMessage(
313 "label.example_query_param", new String[]
315 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
316 for (DbSourceProxy dbs : database.getSelectedSources())
318 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
324 replacePunctuation.setEnabled(enablePunct);
326 } catch (Exception ex)
329 replacePunctuation.setEnabled(true);
335 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
336 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
337 JPanel jPanel2a = new JPanel(new BorderLayout());
338 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
339 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
340 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
341 // jPanel2.setPreferredSize(new Dimension())
342 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
343 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
344 this.add(jPanel3, java.awt.BorderLayout.CENTER);
345 this.add(jPanel2, java.awt.BorderLayout.NORTH);
346 jScrollPane1.getViewport().add(textArea);
350 protected void example_actionPerformed()
352 DbSourceProxy db = null;
355 textArea.setText(database.getExampleQueries());
356 } catch (Exception ex)
362 protected void clear_actionPerformed()
364 textArea.setText("");
368 JLabel dbeg = new JLabel();
370 JDatabaseTree database;
372 JButton databaseButt;
374 JLabel jLabel1 = new JLabel();
376 JCheckBox replacePunctuation = new JCheckBox();
378 JButton ok = new JButton();
380 JButton clear = new JButton();
382 JButton example = new JButton();
384 JButton close = new JButton();
386 JPanel jPanel1 = new JPanel();
388 JTextArea textArea = new JTextArea();
390 JScrollPane jScrollPane1 = new JScrollPane();
392 JPanel jPanel2 = new JPanel();
394 JPanel jPanel3 = new JPanel();
396 JPanel jPanel4 = new JPanel();
398 BorderLayout borderLayout1 = new BorderLayout();
400 BorderLayout borderLayout2 = new BorderLayout();
402 BorderLayout borderLayout3 = new BorderLayout();
404 public void close_actionPerformed(ActionEvent e)
408 frame.setClosed(true);
409 } catch (Exception ex)
414 public void ok_actionPerformed()
416 databaseButt.setEnabled(false);
417 example.setEnabled(false);
418 textArea.setEnabled(false);
419 ok.setEnabled(false);
420 close.setEnabled(false);
422 Thread worker = new Thread(this);
426 private void resetDialog()
428 databaseButt.setEnabled(true);
429 example.setEnabled(true);
430 textArea.setEnabled(true);
432 close.setEnabled(true);
439 if (!database.hasSelection())
441 error += "Please select the source database\n";
443 // TODO: make this transformation more configurable
444 com.stevesoft.pat.Regex empty;
445 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
447 empty = new com.stevesoft.pat.Regex(
448 // replace commas and spaces with a semicolon
449 "(\\s|[,; ])+", ";");
453 // just turn spaces and semicolons into single semicolons
454 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
456 textArea.setText(empty.replaceAll(textArea.getText()));
457 // see if there's anthing to search with
458 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
461 error += "Please enter a (semi-colon separated list of) database id(s)";
463 if (error.length() > 0)
465 showErrorMessage(error);
469 // indicate if successive sources should be merged into one alignment.
470 boolean addToLast = false;
471 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
472 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
473 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
476 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
478 Iterator<String> en = Arrays.asList(new String[0]).iterator();
479 int nqueries = qries.length;
480 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
482 if (!en.hasNext() && nextfetch.size() > 0)
484 en = nextfetch.iterator();
485 nqueries = nextfetch.size();
486 // save the remaining queries in the original array
487 qries = nextfetch.toArray(new String[nqueries]);
488 nextfetch = new ArrayList<String>();
491 DbSourceProxy proxy = proxies.next();
492 boolean isAliSource = false;
496 guiWindow.setProgressBar(MessageManager.formatMessage("status.fetching_sequence_queries_from", new String[]{Integer.valueOf(nqueries).toString(),proxy.getDbName()}), Thread
497 .currentThread().hashCode());
498 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
499 if (proxy.getAccessionSeparator() == null)
503 String item = en.next();
510 // give the server a chance to breathe
512 } catch (Exception e)
519 AlignmentI indres = null;
522 indres = proxy.getSequenceRecords(item);
523 } catch (OutOfMemoryError oome)
525 new OOMWarning("fetching " + item + " from "
526 + proxy.getDbName(), oome, this);
537 } catch (Exception e)
539 jalview.bin.Cache.log.info("Error retrieving " + item
540 + " from " + proxy.getDbName(), e);
547 StringBuffer multiacc = new StringBuffer();
548 ArrayList<String> tosend = new ArrayList<String>();
551 String nel = en.next();
553 multiacc.append(nel);
556 multiacc.append(proxy.getAccessionSeparator());
563 List<String> nores = new ArrayList<String>();
564 rslt = proxy.getSequenceRecords(multiacc.toString());
565 if (rslt == null || rslt.getHeight() == 0)
567 // no results - pass on all queries to next source
568 nextfetch.addAll(tosend);
572 aresultq.add(multiacc.toString());
575 rs = rslt.getSequencesArray();
576 // search for each query in the dbrefs associated with each
579 // ones we do not find will be used to query next source (if any)
580 for (String q : tosend)
582 DBRefEntry dbr = new DBRefEntry(), found[] = null;
583 dbr.setSource(proxy.getDbSource());
584 dbr.setVersion(null);
585 if (proxy.getAccessionValidator() != null)
587 Regex vgr = proxy.getAccessionValidator();
589 if (vgr.numSubs() > 0)
591 dbr.setAccessionId(vgr.stringMatched(1));
595 dbr.setAccessionId(vgr.stringMatched());
600 dbr.setAccessionId(q);
602 boolean rfound = false;
603 for (int r = 0; r < rs.length; r++)
606 && (found = DBRefUtils.searchRefs(
607 rs[r].getDBRef(), dbr)) != null
621 } catch (OutOfMemoryError oome)
623 new OOMWarning("fetching " + multiacc + " from "
624 + database.getSelectedItem(), oome, this);
628 } catch (Exception e)
630 showErrorMessage("Error retrieving " + textArea.getText()
631 + " from " + database.getSelectedItem());
633 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
634 System.err.println("Retrieval failed for source ='"
635 + database.getSelectedItem() + "' and query\n'"
636 + textArea.getText() + "'\n");
638 } catch (OutOfMemoryError e)
640 // resets dialog box - so we don't use OOMwarning here.
641 showErrorMessage("Out of Memory when retrieving "
644 + database.getSelectedItem()
645 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
649 showErrorMessage("Serious Error retrieving " + textArea.getText()
650 + " from " + database.getSelectedItem());
653 // Stack results ready for opening in alignment windows
654 if (aresult != null && aresult.size() > 0)
656 AlignmentI ar = null;
660 // new window for each result
661 while (aresult.size() > 0)
663 presult.add(aresult.remove(0));
664 presultTitle.add(aresultq.remove(0) + " "
665 + getDefaultRetrievalTitle());
671 if (addToLast && presult.size() > 0)
673 ar = presult.remove(presult.size() - 1);
674 titl = presultTitle.remove(presultTitle.size() - 1);
676 // concatenate all results in one window
677 while (aresult.size() > 0)
681 ar = aresult.remove(0);
685 ar.append(aresult.remove(0));
691 presultTitle.add(titl);
694 guiWindow.setProgressBar(MessageManager.getString("status.finshed_querying"), Thread.currentThread()
697 guiWindow.setProgressBar((presult.size() > 0) ? MessageManager.getString("status.parsing_results")
698 : MessageManager.getString("status.processing"), Thread.currentThread().hashCode());
700 while (presult.size() > 0)
702 parseResult(presult.remove(0), presultTitle.remove(0), null);
704 // only remove visual delay after we finished parsing.
705 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
706 if (nextfetch.size() > 0)
708 StringBuffer sb = new StringBuffer();
709 sb.append("Didn't retrieve the following "
710 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
711 + " queries") + ": \n");
712 int l = sb.length(), lr = 0;
713 for (String s : nextfetch)
715 if (l != sb.length())
719 if (lr - sb.length() > 40)
725 showErrorMessage(sb.toString());
730 AlignmentI parseResult(String result, String title)
732 String format = new IdentifyFile().Identify(result, "Paste");
733 Alignment sequences = null;
734 if (FormatAdapter.isValidFormat(format))
739 sequences = new FormatAdapter().readFile(result.toString(),
741 } catch (Exception ex)
745 if (sequences != null)
747 return parseResult(sequences, title, format);
752 showErrorMessage("Error retrieving " + textArea.getText() + " from "
753 + database.getSelectedItem());
761 * @return a standard title for any results retrieved using the currently
762 * selected source and settings
764 public String getDefaultRetrievalTitle()
766 return "Retrieved from " + database.getSelectedItem();
769 AlignmentI parseResult(AlignmentI al, String title,
770 String currentFileFormat)
773 if (al != null && al.getHeight() > 0)
775 if (alignFrame == null)
777 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
778 AlignFrame.DEFAULT_HEIGHT);
779 if (currentFileFormat != null)
781 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
783 // NON-FormatAdapter Sourced
789 title = getDefaultRetrievalTitle();
791 SequenceFeature[] sfs = null;
792 List<SequenceI> alsqs;
793 synchronized (alsqs = al.getSequences())
795 for (SequenceI sq : alsqs)
797 if ((sfs = (sq).getDatasetSequence().getSequenceFeatures()) != null)
801 af.setShowSeqFeatures(true);
808 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
809 AlignFrame.DEFAULT_HEIGHT);
811 af.statusBar.setText(MessageManager
812 .getString("label.successfully_pasted_alignment_file"));
816 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
818 } catch (Exception ex)
824 for (int i = 0; i < al.getHeight(); i++)
826 alignFrame.viewport.getAlignment().addSequence(
827 al.getSequenceAt(i)); // this
834 alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
836 alignFrame.viewport.getAlignment().getWidth();
837 alignFrame.viewport.firePropertyChange("alignment", null,
838 alignFrame.viewport.getAlignment().getSequences());
844 void showErrorMessage(final String error)
847 javax.swing.SwingUtilities.invokeLater(new Runnable()
852 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
853 MessageManager.getString("label.error_retrieving_data"),
854 JOptionPane.WARNING_MESSAGE);