2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 java.awt.BorderLayout;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.KeyAdapter;
28 import java.awt.event.KeyEvent;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.List;
35 import javax.swing.JButton;
36 import javax.swing.JCheckBox;
37 import javax.swing.JComboBox;
38 import javax.swing.JInternalFrame;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTextArea;
43 import javax.swing.SwingConstants;
45 import jalview.api.FeatureSettingsModelI;
46 import jalview.bin.Cache;
47 import jalview.datamodel.AlignmentI;
48 import jalview.datamodel.DBRefEntry;
49 import jalview.datamodel.SequenceI;
50 import jalview.fts.core.GFTSPanel;
51 import jalview.fts.service.pdb.PDBFTSPanel;
52 import jalview.fts.service.threedbeacons.TDBeaconsFTSPanel;
53 import jalview.fts.service.uniprot.UniprotFTSPanel;
54 import jalview.io.FileFormatI;
55 import jalview.io.gff.SequenceOntologyI;
56 import jalview.util.DBRefUtils;
57 import jalview.util.MessageManager;
58 import jalview.util.Platform;
59 import jalview.ws.seqfetcher.DbSourceProxy;
62 * A panel where the use may choose a database source, and enter one or more
63 * accessions, to retrieve entries from the database.
65 * If the selected source is Uniprot or PDB, a free text search panel is opened
66 * instead to perform the search and selection.
68 public class SequenceFetcher extends JPanel implements Runnable
70 private class StringPair
74 private String display;
76 public StringPair(String s1, String s2)
82 public StringPair(String s)
87 public String getKey()
92 public String getDisplay()
98 public String toString()
103 public boolean equals(StringPair other)
105 return other.key == this.key;
109 private static jalview.ws.SequenceFetcher sfetch = null;
111 JLabel exampleAccession;
113 JComboBox<StringPair> database;
115 JCheckBox replacePunctuation;
127 JInternalFrame frame;
129 IProgressIndicator guiWindow;
131 AlignFrame alignFrame;
133 GFTSPanel parentSearchPanel;
135 IProgressIndicator progressIndicator;
137 volatile boolean _isConstructing = false;
140 * Returns the shared instance of the SequenceFetcher client
144 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton()
148 sfetch = new jalview.ws.SequenceFetcher();
154 * Constructor given a client to receive any status or progress messages
155 * (currently either the Desktop, or an AlignFrame panel)
159 public SequenceFetcher(IProgressIndicator guiIndic)
161 this(guiIndic, null, null);
165 * Constructor with specified database and accession(s) to retrieve
171 public SequenceFetcher(IProgressIndicator guiIndic,
172 final String selectedDb, final String queryString)
174 this.progressIndicator = guiIndic;
175 getSequenceFetcherSingleton();
176 this.guiWindow = progressIndicator;
178 if (progressIndicator instanceof AlignFrame)
180 alignFrame = (AlignFrame) progressIndicator;
184 textArea.setText(queryString);
186 frame = new JInternalFrame();
187 frame.setContentPane(this);
188 Desktop.addInternalFrame(frame, getFrameTitle(), true, 400,
189 Platform.isAMacAndNotJS() ? 240 : 180);
192 private String getFrameTitle()
194 return ((alignFrame == null)
195 ? MessageManager.getString("label.new_sequence_fetcher")
197 .getString("label.additional_sequence_fetcher"));
200 private void jbInit(String selectedDb)
202 this.setLayout(new BorderLayout());
204 database = new JComboBox<>();
205 database.setFont(JvSwingUtils.getLabelFont());
206 StringPair instructionItem = new StringPair(
207 MessageManager.getString("action.select_ddbb"));
208 database.setPrototypeDisplayValue(instructionItem);
209 String[] sources = new jalview.ws.SequenceFetcher().getSupportedDb();
210 Arrays.sort(sources, String.CASE_INSENSITIVE_ORDER);
211 database.addItem(instructionItem);
212 for (String source : sources)
214 List<DbSourceProxy> slist = sfetch.getSourceProxy(source);
215 if (slist.size() == 1 && slist.get(0) != null)
217 database.addItem(new StringPair(source, slist.get(0).getDbName()));
221 database.addItem(new StringPair(source));
224 setDatabaseSelectedItem(selectedDb);
225 if (database.getSelectedIndex() == -1)
227 database.setSelectedIndex(0);
229 database.setMaximumRowCount(database.getItemCount());
230 database.addActionListener(new ActionListener()
233 public void actionPerformed(ActionEvent e)
235 String currentSelection = ((StringPair) database.getSelectedItem())
237 updateExampleQuery(currentSelection);
239 if ("pdb".equalsIgnoreCase(currentSelection))
242 new PDBFTSPanel(SequenceFetcher.this);
244 else if ("uniprot".equalsIgnoreCase(currentSelection))
247 new UniprotFTSPanel(SequenceFetcher.this);
249 else if ("3d-beacons".equalsIgnoreCase(currentSelection))
252 new TDBeaconsFTSPanel(SequenceFetcher.this);
261 exampleAccession = new JLabel("");
262 exampleAccession.setFont(new Font("Verdana", Font.BOLD, 11));
263 JLabel jLabel1 = new JLabel(MessageManager
264 .getString("label.separate_multiple_accession_ids"));
265 jLabel1.setFont(new Font("Verdana", Font.ITALIC, 11));
266 jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
268 replacePunctuation = new JCheckBox(
269 MessageManager.getString("label.replace_commas_semicolons"));
270 replacePunctuation.setHorizontalAlignment(SwingConstants.LEFT);
271 replacePunctuation.setFont(new Font("Verdana", Font.ITALIC, 11));
272 okBtn = new JButton(MessageManager.getString("action.ok"));
273 okBtn.addActionListener(new ActionListener()
276 public void actionPerformed(ActionEvent e)
278 ok_actionPerformed();
281 JButton clear = new JButton(MessageManager.getString("action.clear"));
282 clear.addActionListener(new ActionListener()
285 public void actionPerformed(ActionEvent e)
287 clear_actionPerformed();
291 exampleBtn = new JButton(MessageManager.getString("label.example"));
292 exampleBtn.addActionListener(new ActionListener()
295 public void actionPerformed(ActionEvent e)
297 example_actionPerformed();
300 closeBtn = new JButton(MessageManager.getString("action.cancel"));
301 closeBtn.addActionListener(new ActionListener()
304 public void actionPerformed(ActionEvent e)
306 close_actionPerformed(e);
309 backBtn = new JButton(MessageManager.getString("action.back"));
310 backBtn.addActionListener(new ActionListener()
313 public void actionPerformed(ActionEvent e)
315 parentSearchPanel.btn_back_ActionPerformed();
318 // back not visible unless embedded
319 backBtn.setVisible(false);
321 textArea = new JTextArea();
322 textArea.setFont(JvSwingUtils.getLabelFont());
323 textArea.setLineWrap(true);
324 textArea.addKeyListener(new KeyAdapter()
327 public void keyPressed(KeyEvent e)
329 if (e.getKeyCode() == KeyEvent.VK_ENTER)
331 ok_actionPerformed();
336 JPanel actionPanel = new JPanel();
337 actionPanel.add(backBtn);
338 actionPanel.add(exampleBtn);
339 actionPanel.add(clear);
340 actionPanel.add(okBtn);
341 actionPanel.add(closeBtn);
343 JPanel databasePanel = new JPanel();
344 databasePanel.setLayout(new BorderLayout());
345 databasePanel.add(database, BorderLayout.NORTH);
346 databasePanel.add(exampleAccession, BorderLayout.CENTER);
347 JPanel jPanel2a = new JPanel(new BorderLayout());
348 jPanel2a.add(jLabel1, BorderLayout.NORTH);
349 jPanel2a.add(replacePunctuation, BorderLayout.SOUTH);
350 databasePanel.add(jPanel2a, BorderLayout.SOUTH);
352 JPanel idsPanel = new JPanel();
353 idsPanel.setLayout(new BorderLayout(0, 5));
354 JScrollPane jScrollPane1 = new JScrollPane();
355 jScrollPane1.getViewport().add(textArea);
356 idsPanel.add(jScrollPane1, BorderLayout.CENTER);
358 this.add(actionPanel, BorderLayout.SOUTH);
359 this.add(idsPanel, BorderLayout.CENTER);
360 this.add(databasePanel, BorderLayout.NORTH);
363 private void setDatabaseSelectedItem(String db)
365 for (int i = 0; i < database.getItemCount(); i++)
367 StringPair sp = database.getItemAt(i);
368 if (sp != null && db != null && db.equals(sp.getKey()))
370 database.setSelectedIndex(i);
377 * Answers a semi-colon-delimited string with the example query or queries for
378 * the selected database
383 protected String getExampleQueries(String db)
385 StringBuilder sb = new StringBuilder();
386 HashSet<String> hs = new HashSet<>();
387 for (DbSourceProxy dbs : sfetch.getSourceProxy(db))
389 String tq = dbs.getTestQuery();
390 if (hs.add(tq)) // not a duplicate source
399 return sb.toString();
403 * Action on selecting a database other than Uniprot or PDB is to enable or
404 * disable 'Replace commas', and await input in the query field
406 protected void otherSourceAction()
410 String eq = exampleAccession.getText();
411 // TODO this should be a property of the SequenceFetcher whether commas
412 // are allowed in the IDs...
414 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
415 replacePunctuation.setEnabled(enablePunct);
417 } catch (Exception ex)
419 exampleAccession.setText("");
420 replacePunctuation.setEnabled(true);
426 * Sets the text of the example query to incorporate the example accession
427 * provided by the selected database source
429 * @param selectedDatabase
432 protected String updateExampleQuery(String selectedDatabase)
434 String eq = getExampleQueries(selectedDatabase);
435 exampleAccession.setText(MessageManager
436 .formatMessage("label.example_query_param", new String[]
442 * Action on clicking the 'Example' button is to write the example accession
443 * as the query text field value
445 protected void example_actionPerformed()
447 String eq = getExampleQueries(
448 ((StringPair) database.getSelectedItem()).getKey());
449 textArea.setText(eq);
454 * Clears the query input field
456 protected void clear_actionPerformed()
458 textArea.setText("");
463 * Action on Close button is to close this frame, and also (if it is embedded
464 * in a search panel) to close the search panel
468 protected void close_actionPerformed(ActionEvent e)
472 frame.setClosed(true);
473 if (parentSearchPanel != null)
475 parentSearchPanel.btn_cancel_ActionPerformed();
477 } catch (Exception ex)
483 * Action on OK is to start the fetch for entered accession(s)
485 public void ok_actionPerformed()
488 * tidy inputs and check there is something to search for
490 String t0 = textArea.getText();
491 String text = t0.trim();
492 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
494 text = text.replace(",", ";");
496 text = text.replaceAll("(\\s|[; ])+", ";");
497 if (!t0.equals(text))
499 textArea.setText(text);
505 "Please enter a (semi-colon separated list of) database id(s)");
509 if (database.getSelectedIndex() == 0)
512 showErrorMessage("Please choose a database");
517 exampleBtn.setEnabled(false);
518 textArea.setEnabled(false);
519 okBtn.setEnabled(false);
520 closeBtn.setEnabled(false);
521 backBtn.setEnabled(false);
523 Thread worker = new Thread(this);
527 private void resetDialog()
529 exampleBtn.setEnabled(true);
530 textArea.setEnabled(true);
531 okBtn.setEnabled(true);
532 closeBtn.setEnabled(true);
533 backBtn.setEnabled(parentSearchPanel != null);
539 boolean addToLast = false;
540 List<String> aresultq = new ArrayList<>();
541 List<String> presultTitle = new ArrayList<>();
542 List<AlignmentI> presult = new ArrayList<>();
543 List<AlignmentI> aresult = new ArrayList<>();
544 List<DbSourceProxy> sources = sfetch.getSourceProxy(
545 ((StringPair) database.getSelectedItem()).getKey());
546 Iterator<DbSourceProxy> proxies = sources.iterator();
547 String[] qries = textArea.getText().trim().split(";");
548 List<String> nextFetch = Arrays.asList(qries);
549 Iterator<String> en = Arrays.asList(new String[0]).iterator();
550 int nqueries = qries.length;
552 FeatureSettingsModelI preferredFeatureColours = null;
553 while (proxies.hasNext() && (en.hasNext() || nextFetch.size() > 0))
555 if (!en.hasNext() && nextFetch.size() > 0)
557 en = nextFetch.iterator();
558 nqueries = nextFetch.size();
559 // save the remaining queries in the original array
560 qries = nextFetch.toArray(new String[nqueries]);
561 nextFetch = new ArrayList<>();
564 DbSourceProxy proxy = proxies.next();
568 guiWindow.setProgressBar(MessageManager.formatMessage(
569 "status.fetching_sequence_queries_from", new String[]
570 { Integer.valueOf(nqueries).toString(),
571 proxy.getDbName() }),
572 Thread.currentThread().hashCode());
573 if (proxy.getMaximumQueryCount() == 1)
576 * proxy only handles one accession id at a time
580 String acc = en.next();
581 if (!fetchSingleAccession(proxy, acc, aresultq, aresult))
590 * proxy can fetch multiple accessions at one time
592 fetchMultipleAccessions(proxy, en, aresultq, aresult, nextFetch);
594 } catch (Exception e)
596 showErrorMessage("Error retrieving " + textArea.getText() + " from "
597 + ((StringPair) database.getSelectedItem()).getDisplay());
599 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
600 System.err.println("Retrieval failed for source ='"
601 + ((StringPair) database.getSelectedItem()).getDisplay()
602 + "' and query\n'" + textArea.getText() + "'\n");
604 } catch (OutOfMemoryError e)
606 showErrorMessage("Out of Memory when retrieving "
607 + textArea.getText() + " from "
608 + ((StringPair) database.getSelectedItem()).getDisplay()
609 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
613 showErrorMessage("Serious Error retrieving " + textArea.getText()
615 + ((StringPair) database.getSelectedItem()).getDisplay());
619 // Stack results ready for opening in alignment windows
620 if (aresult != null && aresult.size() > 0)
622 FeatureSettingsModelI proxyColourScheme = proxy
623 .getFeatureColourScheme();
624 if (proxyColourScheme != null)
626 preferredFeatureColours = proxyColourScheme;
629 AlignmentI ar = null;
630 if (proxy.isAlignmentSource())
633 // new window for each result
634 while (aresult.size() > 0)
636 presult.add(aresult.remove(0));
638 aresultq.remove(0) + " " + getDefaultRetrievalTitle());
644 if (addToLast && presult.size() > 0)
646 ar = presult.remove(presult.size() - 1);
647 titl = presultTitle.remove(presultTitle.size() - 1);
649 // concatenate all results in one window
650 while (aresult.size() > 0)
654 ar = aresult.remove(0);
658 ar.append(aresult.remove(0));
663 presultTitle.add(titl);
666 guiWindow.setProgressBar(
667 MessageManager.getString("status.finshed_querying"),
668 Thread.currentThread().hashCode());
674 .getString("status.parsing_results")
675 : MessageManager.getString("status.processing"),
676 Thread.currentThread().hashCode());
678 while (presult.size() > 0)
680 parseResult(presult.remove(0), presultTitle.remove(0), null,
681 preferredFeatureColours);
683 // only remove visual delay after we finished parsing.
684 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
685 if (nextFetch.size() > 0)
687 StringBuffer sb = new StringBuffer();
688 sb.append("Didn't retrieve the following "
689 + (nextFetch.size() == 1 ? "query"
690 : nextFetch.size() + " queries")
692 int l = sb.length(), lr = 0;
693 for (String s : nextFetch)
695 if (l != sb.length())
699 if (lr - sb.length() > 40)
705 showErrorMessage(sb.toString());
711 * Tries to fetch one or more accession ids from the database proxy
715 * the queries to fetch
717 * a successful queries list to add to
719 * a list of retrieved alignments to add to
721 * failed queries are added to this list
724 void fetchMultipleAccessions(DbSourceProxy proxy,
725 Iterator<String> accessions, List<String> aresultq,
726 List<AlignmentI> aresult, List<String> nextFetch) throws Exception
728 StringBuilder multiacc = new StringBuilder();
729 List<String> tosend = new ArrayList<>();
730 while (accessions.hasNext())
732 String nel = accessions.next();
734 multiacc.append(nel);
735 if (accessions.hasNext())
737 multiacc.append(proxy.getAccessionSeparator());
743 String query = multiacc.toString();
744 AlignmentI rslt = proxy.getSequenceRecords(query);
745 if (rslt == null || rslt.getHeight() == 0)
747 // no results - pass on all queries to next source
748 nextFetch.addAll(tosend);
754 if (tosend.size() > 1)
756 checkResultForQueries(rslt, tosend, nextFetch, proxy);
759 } catch (OutOfMemoryError oome)
761 new OOMWarning("fetching " + multiacc + " from "
762 + ((StringPair) database.getSelectedItem()).getDisplay(),
768 * Query for a single accession id via the database proxy
773 * a list of successful queries to add to
775 * a list of retrieved alignments to add to
776 * @return true if the fetch was successful, else false
778 boolean fetchSingleAccession(DbSourceProxy proxy, String accession,
779 List<String> aresultq, List<AlignmentI> aresult)
781 boolean success = false;
788 // give the server a chance to breathe
790 } catch (Exception e)
796 AlignmentI indres = null;
799 indres = proxy.getSequenceRecords(accession);
800 } catch (OutOfMemoryError oome)
803 "fetching " + accession + " from " + proxy.getDbName(),
808 aresultq.add(accession);
812 } catch (Exception e)
814 Cache.log.info("Error retrieving " + accession + " from "
815 + proxy.getDbName(), e);
821 * Checks which of the queries were successfully retrieved by searching the
822 * DBRefs of the retrieved sequences for a match. Any not found are added to
823 * the 'nextFetch' list.
830 void checkResultForQueries(AlignmentI rslt, List<String> queries,
831 List<String> nextFetch, DbSourceProxy proxy)
833 SequenceI[] rs = rslt.getSequencesArray();
835 for (String q : queries)
837 // BH 2019.01.25 dbr is never used.
838 // DBRefEntry dbr = new DBRefEntry();
839 // dbr.setSource(proxy.getDbSource());
840 // dbr.setVersion(null);
841 String accId = proxy.getAccessionIdFromQuery(q);
842 // dbr.setAccessionId(accId);
843 boolean rfound = false;
844 for (int r = 0, nr = rs.length; r < nr; r++)
848 List<DBRefEntry> found = DBRefUtils.searchRefs(rs[r].getDBRefs(),
850 if (!found.isEmpty())
866 * @return a standard title for any results retrieved using the currently
867 * selected source and settings
869 public String getDefaultRetrievalTitle()
871 return "Retrieved from "
872 + ((StringPair) database.getSelectedItem()).getDisplay();
876 * constructs an alignment frame given the data and metadata
880 * @param currentFileFormat
881 * @param preferredFeatureColours
882 * @return the alignment
884 public AlignmentI parseResult(AlignmentI al, String title,
885 FileFormatI currentFileFormat,
886 FeatureSettingsModelI preferredFeatureColours)
889 if (al != null && al.getHeight() > 0)
893 title = getDefaultRetrievalTitle();
895 if (alignFrame == null)
897 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
898 AlignFrame.DEFAULT_HEIGHT);
899 if (currentFileFormat != null)
901 af.currentFileFormat = currentFileFormat;
904 List<SequenceI> alsqs = al.getSequences();
907 for (SequenceI sq : alsqs)
909 if (sq.getFeatures().hasFeatures())
911 af.setShowSeqFeatures(true);
917 af.getViewport().applyFeaturesStyle(preferredFeatureColours);
918 if (Cache.getDefault("HIDE_INTRONS", true))
920 af.hideFeatureColumns(SequenceOntologyI.EXON, false);
922 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
923 AlignFrame.DEFAULT_HEIGHT);
925 af.setStatus(MessageManager
926 .getString("label.successfully_pasted_alignment_file"));
930 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
931 } catch (Exception ex)
937 alignFrame.viewport.addAlignment(al, title);
943 void showErrorMessage(final String error)
946 javax.swing.SwingUtilities.invokeLater(new Runnable()
951 JvOptionPane.showInternalMessageDialog(Desktop.desktop, error,
952 MessageManager.getString("label.error_retrieving_data"),
953 JvOptionPane.WARNING_MESSAGE);
958 public IProgressIndicator getProgressIndicator()
960 return progressIndicator;
963 public void setProgressIndicator(IProgressIndicator progressIndicator)
965 this.progressIndicator = progressIndicator;
969 * Hide this panel (on clicking the database button to open the database
974 frame.setVisible(false);
977 public void setQuery(String ids)
979 textArea.setText(ids);
983 * Called to modify the search panel for embedding as an alternative tab of a
984 * free text search panel. The database choice list is hidden (since the
985 * choice has been made), and a Back button is made visible (which reopens the
986 * Sequence Fetcher panel).
990 public void embedIn(GFTSPanel parentPanel)
992 database.setVisible(false);
993 backBtn.setVisible(true);
994 parentSearchPanel = parentPanel;