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.
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.FormatAdapter;
30 import jalview.io.IdentifyFile;
31 import jalview.util.DBRefUtils;
32 import jalview.util.MessageManager;
33 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
34 import jalview.ws.seqfetcher.DbSourceProxy;
36 import java.awt.BorderLayout;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.KeyAdapter;
41 import java.awt.event.KeyEvent;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Iterator;
45 import java.util.List;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JInternalFrame;
50 import javax.swing.JLabel;
51 import javax.swing.JOptionPane;
52 import javax.swing.JPanel;
53 import javax.swing.JScrollPane;
54 import javax.swing.JTextArea;
55 import javax.swing.SwingConstants;
56 import javax.swing.tree.DefaultMutableTreeNode;
58 import com.stevesoft.pat.Regex;
60 public class SequenceFetcher extends JPanel implements Runnable
62 // ASequenceFetcher sfetch;
65 IProgressIndicator guiWindow;
67 AlignFrame alignFrame;
71 final String noDbSelected = "-- Select Database --";
73 private static jalview.ws.SequenceFetcher sfetch = null;
75 private static long lastDasSourceRegistry = -3;
77 private static DasSourceRegistryI dasRegistry = null;
79 private static boolean _initingFetcher = false;
81 private static Thread initingThread = null;
84 * Blocking method that initialises and returns the shared instance of the
85 * SequenceFetcher client
88 * - where the initialisation delay message should be shown
89 * @return the singleton instance of the sequence fetcher client
91 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
92 final IProgressIndicator guiWindow)
94 if (_initingFetcher && initingThread != null && initingThread.isAlive())
96 if (guiWindow != null)
98 guiWindow.setProgressBar(
99 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
100 Thread.currentThread().hashCode());
102 // initting happening on another thread - so wait around to see if it
104 while (_initingFetcher && initingThread != null
105 && initingThread.isAlive())
110 } catch (Exception e)
115 if (guiWindow != null)
117 guiWindow.setProgressBar(
118 MessageManager.getString("status.waiting_sequence_database_fetchers_init"),
119 Thread.currentThread().hashCode());
123 || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
124 || lastDasSourceRegistry != (jalview.bin.Cache
125 .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
126 .getDasSourceRegistry().getLocalSourceString())
129 _initingFetcher = true;
130 initingThread = Thread.currentThread();
132 * give a visual indication that sequence fetcher construction is occuring
134 if (guiWindow != null)
136 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
137 Thread.currentThread().hashCode());
139 dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
140 dasRegistry.refreshSources();
142 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
143 if (guiWindow != null)
145 guiWindow.setProgressBar(MessageManager.getString("status.init_sequence_database_fetchers"),
146 Thread.currentThread().hashCode());
148 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
149 .getLocalSourceString()).hashCode();
151 _initingFetcher = false;
152 initingThread = null;
157 public SequenceFetcher(IProgressIndicator guiIndic)
159 final IProgressIndicator guiWindow = guiIndic;
160 final SequenceFetcher us = this;
161 // launch initialiser thread
162 Thread sf = new Thread(new Runnable()
168 if (getSequenceFetcherSingleton(guiWindow) != null)
170 us.initGui(guiWindow);
174 javax.swing.SwingUtilities.invokeLater(new Runnable()
180 .showInternalMessageDialog(
182 MessageManager.getString("warn.couldnt_create_sequence_fetcher_client"),
183 MessageManager.getString("label.couldnt_create_sequence_fetcher"),
184 JOptionPane.ERROR_MESSAGE);
188 // raise warning dialog
195 private class DatabaseAuthority extends DefaultMutableTreeNode
200 private class DatabaseSource extends DefaultMutableTreeNode
206 * called by thread spawned by constructor
210 private void initGui(IProgressIndicator guiWindow)
212 this.guiWindow = guiWindow;
213 if (guiWindow instanceof AlignFrame)
215 alignFrame = (AlignFrame) guiWindow;
217 database = new JDatabaseTree(sfetch);
221 } catch (Exception ex)
223 ex.printStackTrace();
226 frame = new JInternalFrame();
227 frame.setContentPane(this);
228 if (new jalview.util.Platform().isAMac())
230 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
234 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
238 private String getFrameTitle()
240 return ((alignFrame == null) ? MessageManager.getString("label.new_sequence_fetcher") : MessageManager.getString("label.additional_sequence_fetcher"));
243 private void jbInit() throws Exception
245 this.setLayout(borderLayout2);
247 database.setFont(JvSwingUtils.getLabelFont());
248 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
249 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
250 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
251 jLabel1.setText(MessageManager
252 .getString("label.separate_multiple_accession_ids"));
254 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
256 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
257 replacePunctuation.setText(MessageManager
258 .getString("label.replace_commas_semicolons"));
259 ok.setText(MessageManager.getString("action.ok"));
260 ok.addActionListener(new ActionListener()
263 public void actionPerformed(ActionEvent e)
265 ok_actionPerformed();
268 clear.setText(MessageManager.getString("action.clear"));
269 clear.addActionListener(new ActionListener()
272 public void actionPerformed(ActionEvent e)
274 clear_actionPerformed();
278 example.setText(MessageManager.getString("label.example"));
279 example.addActionListener(new ActionListener()
282 public void actionPerformed(ActionEvent e)
284 example_actionPerformed();
287 close.setText(MessageManager.getString("action.close"));
288 close.addActionListener(new ActionListener()
291 public void actionPerformed(ActionEvent e)
293 close_actionPerformed(e);
296 textArea.setFont(JvSwingUtils.getLabelFont());
297 textArea.setLineWrap(true);
298 textArea.addKeyListener(new KeyAdapter()
301 public void keyPressed(KeyEvent e)
303 if (e.getKeyCode() == KeyEvent.VK_ENTER)
305 ok_actionPerformed();
309 jPanel3.setLayout(borderLayout1);
310 borderLayout1.setVgap(5);
312 jPanel1.add(example);
315 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
316 jPanel2.setLayout(borderLayout3);
317 databaseButt = database.getDatabaseSelectorButton();
318 databaseButt.setFont(JvSwingUtils.getLabelFont());
319 database.addActionListener(new ActionListener()
323 public void actionPerformed(ActionEvent e)
327 databaseButt.setText(database.getSelectedItem()
328 + (database.getSelectedSources().size() > 1 ? " (and "
329 + database.getSelectedSources().size()
331 String eq = database.getExampleQueries();
332 dbeg.setText(MessageManager.formatMessage(
333 "label.example_query_param", new String[]
335 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
336 for (DbSourceProxy dbs : database.getSelectedSources())
338 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
344 replacePunctuation.setEnabled(enablePunct);
346 } catch (Exception ex)
349 replacePunctuation.setEnabled(true);
355 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
356 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
357 JPanel jPanel2a = new JPanel(new BorderLayout());
358 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
359 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
360 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
361 // jPanel2.setPreferredSize(new Dimension())
362 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
363 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
364 this.add(jPanel3, java.awt.BorderLayout.CENTER);
365 this.add(jPanel2, java.awt.BorderLayout.NORTH);
366 jScrollPane1.getViewport().add(textArea);
370 protected void example_actionPerformed()
372 DbSourceProxy db = null;
375 textArea.setText(database.getExampleQueries());
376 } catch (Exception ex)
382 protected void clear_actionPerformed()
384 textArea.setText("");
388 JLabel dbeg = new JLabel();
390 JDatabaseTree database;
392 JButton databaseButt;
394 JLabel jLabel1 = new JLabel();
396 JCheckBox replacePunctuation = new JCheckBox();
398 JButton ok = new JButton();
400 JButton clear = new JButton();
402 JButton example = new JButton();
404 JButton close = new JButton();
406 JPanel jPanel1 = new JPanel();
408 JTextArea textArea = new JTextArea();
410 JScrollPane jScrollPane1 = new JScrollPane();
412 JPanel jPanel2 = new JPanel();
414 JPanel jPanel3 = new JPanel();
416 JPanel jPanel4 = new JPanel();
418 BorderLayout borderLayout1 = new BorderLayout();
420 BorderLayout borderLayout2 = new BorderLayout();
422 BorderLayout borderLayout3 = new BorderLayout();
424 public void close_actionPerformed(ActionEvent e)
428 frame.setClosed(true);
429 } catch (Exception ex)
434 public void ok_actionPerformed()
436 databaseButt.setEnabled(false);
437 example.setEnabled(false);
438 textArea.setEnabled(false);
439 ok.setEnabled(false);
440 close.setEnabled(false);
442 Thread worker = new Thread(this);
446 private void resetDialog()
448 databaseButt.setEnabled(true);
449 example.setEnabled(true);
450 textArea.setEnabled(true);
452 close.setEnabled(true);
459 if (!database.hasSelection())
461 error += "Please select the source database\n";
463 // TODO: make this transformation more configurable
464 com.stevesoft.pat.Regex empty;
465 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
467 empty = new com.stevesoft.pat.Regex(
468 // replace commas and spaces with a semicolon
469 "(\\s|[,; ])+", ";");
473 // just turn spaces and semicolons into single semicolons
474 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
476 textArea.setText(empty.replaceAll(textArea.getText()));
477 // see if there's anthing to search with
478 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
481 error += "Please enter a (semi-colon separated list of) database id(s)";
483 if (error.length() > 0)
485 showErrorMessage(error);
489 // indicate if successive sources should be merged into one alignment.
490 boolean addToLast = false;
491 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
492 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
493 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
496 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
498 Iterator<String> en = Arrays.asList(new String[0]).iterator();
499 int nqueries = qries.length;
500 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
502 if (!en.hasNext() && nextfetch.size() > 0)
504 en = nextfetch.iterator();
505 nqueries = nextfetch.size();
506 // save the remaining queries in the original array
507 qries = nextfetch.toArray(new String[nqueries]);
508 nextfetch = new ArrayList<String>();
511 DbSourceProxy proxy = proxies.next();
512 boolean isAliSource = false;
516 guiWindow.setProgressBar(MessageManager.formatMessage("status.fetching_sequence_queries_from", new String[]{Integer.valueOf(nqueries).toString(),proxy.getDbName()}), Thread
517 .currentThread().hashCode());
518 isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
519 if (proxy.getAccessionSeparator() == null)
523 String item = en.next();
530 // give the server a chance to breathe
532 } catch (Exception e)
539 AlignmentI indres = null;
542 indres = proxy.getSequenceRecords(item);
543 } catch (OutOfMemoryError oome)
545 new OOMWarning("fetching " + item + " from "
546 + proxy.getDbName(), oome, this);
557 } catch (Exception e)
559 jalview.bin.Cache.log.info("Error retrieving " + item
560 + " from " + proxy.getDbName(), e);
567 StringBuffer multiacc = new StringBuffer();
568 ArrayList<String> tosend = new ArrayList<String>();
571 String nel = en.next();
573 multiacc.append(nel);
576 multiacc.append(proxy.getAccessionSeparator());
583 List<String> nores = new ArrayList<String>();
584 rslt = proxy.getSequenceRecords(multiacc.toString());
585 if (rslt == null || rslt.getHeight() == 0)
587 // no results - pass on all queries to next source
588 nextfetch.addAll(tosend);
592 aresultq.add(multiacc.toString());
595 rs = rslt.getSequencesArray();
596 // search for each query in the dbrefs associated with each
599 // ones we do not find will be used to query next source (if any)
600 for (String q : tosend)
602 DBRefEntry dbr = new DBRefEntry(), found[] = null;
603 dbr.setSource(proxy.getDbSource());
604 dbr.setVersion(null);
605 if (proxy.getAccessionValidator() != null)
607 Regex vgr = proxy.getAccessionValidator();
609 if (vgr.numSubs() > 0)
611 dbr.setAccessionId(vgr.stringMatched(1));
615 dbr.setAccessionId(vgr.stringMatched());
620 dbr.setAccessionId(q);
622 boolean rfound = false;
623 for (int r = 0; r < rs.length; r++)
626 && (found = DBRefUtils.searchRefs(
627 rs[r].getDBRef(), dbr)) != null
641 } catch (OutOfMemoryError oome)
643 new OOMWarning("fetching " + multiacc + " from "
644 + database.getSelectedItem(), oome, this);
648 } catch (Exception e)
650 showErrorMessage("Error retrieving " + textArea.getText()
651 + " from " + database.getSelectedItem());
653 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
654 System.err.println("Retrieval failed for source ='"
655 + database.getSelectedItem() + "' and query\n'"
656 + textArea.getText() + "'\n");
658 } catch (OutOfMemoryError e)
660 // resets dialog box - so we don't use OOMwarning here.
661 showErrorMessage("Out of Memory when retrieving "
664 + database.getSelectedItem()
665 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
669 showErrorMessage("Serious Error retrieving " + textArea.getText()
670 + " from " + database.getSelectedItem());
673 // Stack results ready for opening in alignment windows
674 if (aresult != null && aresult.size() > 0)
676 AlignmentI ar = null;
680 // new window for each result
681 while (aresult.size() > 0)
683 presult.add(aresult.remove(0));
684 presultTitle.add(aresultq.remove(0) + " "
685 + getDefaultRetrievalTitle());
691 if (addToLast && presult.size() > 0)
693 ar = presult.remove(presult.size() - 1);
694 titl = presultTitle.remove(presultTitle.size() - 1);
696 // concatenate all results in one window
697 while (aresult.size() > 0)
701 ar = aresult.remove(0);
705 ar.append(aresult.remove(0));
711 presultTitle.add(titl);
714 guiWindow.setProgressBar(MessageManager.getString("status.finshed_querying"), Thread.currentThread()
717 guiWindow.setProgressBar((presult.size() > 0) ? MessageManager.getString("status.parsing_results")
718 : MessageManager.getString("status.processing"), Thread.currentThread().hashCode());
720 while (presult.size() > 0)
722 parseResult(presult.remove(0), presultTitle.remove(0), null);
724 // only remove visual delay after we finished parsing.
725 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
726 if (nextfetch.size() > 0)
728 StringBuffer sb = new StringBuffer();
729 sb.append("Didn't retrieve the following "
730 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
731 + " queries") + ": \n");
732 int l = sb.length(), lr = 0;
733 for (String s : nextfetch)
735 if (l != sb.length())
739 if (lr - sb.length() > 40)
745 showErrorMessage(sb.toString());
750 AlignmentI parseResult(String result, String title)
752 String format = new IdentifyFile().Identify(result, "Paste");
753 Alignment sequences = null;
754 if (FormatAdapter.isValidFormat(format))
759 sequences = new FormatAdapter().readFile(result.toString(),
761 } catch (Exception ex)
765 if (sequences != null)
767 return parseResult(sequences, title, format);
772 showErrorMessage("Error retrieving " + textArea.getText() + " from "
773 + database.getSelectedItem());
781 * @return a standard title for any results retrieved using the currently
782 * selected source and settings
784 public String getDefaultRetrievalTitle()
786 return "Retrieved from " + database.getSelectedItem();
789 AlignmentI parseResult(AlignmentI al, String title,
790 String currentFileFormat)
793 if (al != null && al.getHeight() > 0)
795 if (alignFrame == null)
797 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
798 AlignFrame.DEFAULT_HEIGHT);
799 if (currentFileFormat != null)
801 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
803 // NON-FormatAdapter Sourced
809 title = getDefaultRetrievalTitle();
811 SequenceFeature[] sfs = null;
812 List<SequenceI> alsqs;
813 synchronized (alsqs = al.getSequences())
815 for (SequenceI sq : alsqs)
817 if ((sfs = (sq).getDatasetSequence().getSequenceFeatures()) != null)
821 af.setShowSeqFeatures(true);
828 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
829 AlignFrame.DEFAULT_HEIGHT);
831 af.statusBar.setText(MessageManager
832 .getString("label.successfully_pasted_alignment_file"));
836 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
838 } catch (Exception ex)
844 alignFrame.viewport.addAlignment(al, title);
850 void showErrorMessage(final String error)
853 javax.swing.SwingUtilities.invokeLater(new Runnable()
858 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
859 MessageManager.getString("label.error_retrieving_data"),
860 JOptionPane.WARNING_MESSAGE);