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.bin.Console;
48 import jalview.datamodel.AlignmentI;
49 import jalview.datamodel.DBRefEntry;
50 import jalview.datamodel.SequenceI;
51 import jalview.fts.core.GFTSPanel;
52 import jalview.fts.service.pdb.PDBFTSPanel;
53 import jalview.fts.service.threedbeacons.TDBeaconsFTSPanel;
54 import jalview.fts.service.uniprot.UniprotFTSPanel;
55 import jalview.io.FileFormatI;
56 import jalview.io.gff.SequenceOntologyI;
57 import jalview.util.DBRefUtils;
58 import jalview.util.IdUtils;
59 import jalview.util.IdUtils.IdType;
60 import jalview.util.MessageManager;
61 import jalview.util.Platform;
62 import jalview.ws.seqfetcher.DbSourceProxy;
65 * A panel where the use may choose a database source, and enter one or more
66 * accessions, to retrieve entries from the database.
68 * If the selected source is Uniprot or PDB, a free text search panel is opened
69 * instead to perform the search and selection.
71 public class SequenceFetcher extends JPanel implements Runnable
73 private class StringPair
77 private String display;
79 public StringPair(String s1, String s2)
85 public StringPair(String s)
90 public String getKey()
95 public String getDisplay()
101 public String toString()
106 public boolean equals(StringPair other)
108 return other.key == this.key;
112 private static jalview.ws.SequenceFetcher sfetch = null;
114 JLabel exampleAccession;
116 JComboBox<StringPair> database;
118 JCheckBox replacePunctuation;
130 JInternalFrame frame;
132 IProgressIndicator guiWindow;
134 AlignFrame alignFrame;
136 GFTSPanel parentSearchPanel;
138 IProgressIndicator progressIndicator;
140 volatile boolean _isConstructing = false;
143 * Returns the shared instance of the SequenceFetcher client
147 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton()
151 sfetch = new jalview.ws.SequenceFetcher();
157 * Constructor given a client to receive any status or progress messages
158 * (currently either the Desktop, or an AlignFrame panel)
162 public SequenceFetcher(IProgressIndicator guiIndic)
164 this(guiIndic, null, null);
168 * Constructor with specified database and accession(s) to retrieve
174 public SequenceFetcher(IProgressIndicator guiIndic,
175 final String selectedDb, final String queryString)
177 this.progressIndicator = guiIndic;
178 getSequenceFetcherSingleton();
179 this.guiWindow = progressIndicator;
181 if (progressIndicator instanceof AlignFrame)
183 alignFrame = (AlignFrame) progressIndicator;
187 textArea.setText(queryString);
189 frame = new JInternalFrame();
190 frame.setContentPane(this);
191 frame.setFrameIcon(null);
192 Desktop.addInternalFrame(frame, getFrameTitle(), true, 400,
193 Platform.isAMacAndNotJS() ? 240 : 180);
196 private String getFrameTitle()
198 return ((alignFrame == null)
199 ? MessageManager.getString("label.new_sequence_fetcher")
201 .getString("label.additional_sequence_fetcher"));
204 private void jbInit(String selectedDb)
206 this.setLayout(new BorderLayout());
208 database = new JComboBox<>();
209 database.setFont(JvSwingUtils.getLabelFont());
210 StringPair instructionItem = new StringPair(
211 MessageManager.getString("action.select_ddbb"));
212 database.setPrototypeDisplayValue(instructionItem);
213 String[] sources = new jalview.ws.SequenceFetcher().getSupportedDb();
214 Arrays.sort(sources, String.CASE_INSENSITIVE_ORDER);
215 database.addItem(instructionItem);
216 for (String source : sources)
218 List<DbSourceProxy> slist = sfetch.getSourceProxy(source);
219 if (slist.size() == 1 && slist.get(0) != null)
221 database.addItem(new StringPair(source, slist.get(0).getDbName()));
225 database.addItem(new StringPair(source));
228 setDatabaseSelectedItem(selectedDb);
229 if (database.getSelectedIndex() == -1)
231 database.setSelectedIndex(0);
233 database.setMaximumRowCount(database.getItemCount());
234 database.addActionListener(new ActionListener()
237 public void actionPerformed(ActionEvent e)
239 String currentSelection = ((StringPair) database.getSelectedItem())
241 updateExampleQuery(currentSelection);
243 if ("pdb".equalsIgnoreCase(currentSelection))
246 new PDBFTSPanel(SequenceFetcher.this);
248 else if ("uniprot".equalsIgnoreCase(currentSelection))
251 new UniprotFTSPanel(SequenceFetcher.this);
253 else if ("3d-beacons".equalsIgnoreCase(currentSelection))
256 new TDBeaconsFTSPanel(SequenceFetcher.this);
265 exampleAccession = new JLabel("");
266 exampleAccession.setFont(new Font("Verdana", Font.BOLD, 11));
267 JLabel jLabel1 = new JLabel(MessageManager
268 .getString("label.separate_multiple_accession_ids"));
269 jLabel1.setFont(new Font("Verdana", Font.ITALIC, 11));
270 jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
272 replacePunctuation = new JCheckBox(
273 MessageManager.getString("label.replace_commas_semicolons"));
274 replacePunctuation.setHorizontalAlignment(SwingConstants.LEFT);
275 replacePunctuation.setFont(new Font("Verdana", Font.ITALIC, 11));
276 okBtn = new JButton(MessageManager.getString("action.ok"));
277 okBtn.addActionListener(new ActionListener()
280 public void actionPerformed(ActionEvent e)
282 ok_actionPerformed();
285 JButton clear = new JButton(MessageManager.getString("action.clear"));
286 clear.addActionListener(new ActionListener()
289 public void actionPerformed(ActionEvent e)
291 clear_actionPerformed();
295 exampleBtn = new JButton(MessageManager.getString("label.example"));
296 exampleBtn.addActionListener(new ActionListener()
299 public void actionPerformed(ActionEvent e)
301 example_actionPerformed();
304 closeBtn = new JButton(MessageManager.getString("action.cancel"));
305 closeBtn.addActionListener(new ActionListener()
308 public void actionPerformed(ActionEvent e)
310 close_actionPerformed(e);
313 backBtn = new JButton(MessageManager.getString("action.back"));
314 backBtn.addActionListener(new ActionListener()
317 public void actionPerformed(ActionEvent e)
319 parentSearchPanel.btn_back_ActionPerformed();
322 // back not visible unless embedded
323 backBtn.setVisible(false);
325 textArea = new JTextArea();
326 textArea.setFont(JvSwingUtils.getLabelFont());
327 textArea.setLineWrap(true);
328 textArea.addKeyListener(new KeyAdapter()
331 public void keyPressed(KeyEvent e)
333 if (e.getKeyCode() == KeyEvent.VK_ENTER)
335 ok_actionPerformed();
340 JPanel actionPanel = new JPanel();
341 actionPanel.add(backBtn);
342 actionPanel.add(exampleBtn);
343 actionPanel.add(clear);
344 actionPanel.add(okBtn);
345 actionPanel.add(closeBtn);
347 JPanel databasePanel = new JPanel();
348 databasePanel.setLayout(new BorderLayout());
349 databasePanel.add(database, BorderLayout.NORTH);
350 databasePanel.add(exampleAccession, BorderLayout.CENTER);
351 JPanel jPanel2a = new JPanel(new BorderLayout());
352 jPanel2a.add(jLabel1, BorderLayout.NORTH);
353 jPanel2a.add(replacePunctuation, BorderLayout.SOUTH);
354 databasePanel.add(jPanel2a, BorderLayout.SOUTH);
356 JPanel idsPanel = new JPanel();
357 idsPanel.setLayout(new BorderLayout(0, 5));
358 JScrollPane jScrollPane1 = new JScrollPane();
359 jScrollPane1.getViewport().add(textArea);
360 idsPanel.add(jScrollPane1, BorderLayout.CENTER);
362 this.add(actionPanel, BorderLayout.SOUTH);
363 this.add(idsPanel, BorderLayout.CENTER);
364 this.add(databasePanel, BorderLayout.NORTH);
367 private void setDatabaseSelectedItem(String db)
369 for (int i = 0; i < database.getItemCount(); i++)
371 StringPair sp = database.getItemAt(i);
372 if (sp != null && db != null && db.equals(sp.getKey()))
374 database.setSelectedIndex(i);
381 * Answers a semi-colon-delimited string with the example query or queries for
382 * the selected database
387 protected String getExampleQueries(String db)
389 StringBuilder sb = new StringBuilder();
390 HashSet<String> hs = new HashSet<>();
391 for (DbSourceProxy dbs : sfetch.getSourceProxy(db))
393 String tq = dbs.getTestQuery();
394 if (hs.add(tq)) // not a duplicate source
403 return sb.toString();
407 * Action on selecting a database other than Uniprot or PDB is to enable or
408 * disable 'Replace commas', and await input in the query field
410 protected void otherSourceAction()
414 String eq = exampleAccession.getText();
415 // TODO this should be a property of the SequenceFetcher whether commas
416 // are allowed in the IDs...
418 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
419 replacePunctuation.setEnabled(enablePunct);
421 } catch (Exception ex)
423 exampleAccession.setText("");
424 replacePunctuation.setEnabled(true);
430 * Sets the text of the example query to incorporate the example accession
431 * provided by the selected database source
433 * @param selectedDatabase
436 protected String updateExampleQuery(String selectedDatabase)
438 String eq = getExampleQueries(selectedDatabase);
439 exampleAccession.setText(MessageManager
440 .formatMessage("label.example_query_param", new String[]
446 * Action on clicking the 'Example' button is to write the example accession
447 * as the query text field value
449 protected void example_actionPerformed()
451 String eq = getExampleQueries(
452 ((StringPair) database.getSelectedItem()).getKey());
453 textArea.setText(eq);
458 * Clears the query input field
460 protected void clear_actionPerformed()
462 textArea.setText("");
467 * Action on Close button is to close this frame, and also (if it is embedded
468 * in a search panel) to close the search panel
472 protected void close_actionPerformed(ActionEvent e)
476 frame.setClosed(true);
477 if (parentSearchPanel != null)
479 parentSearchPanel.btn_cancel_ActionPerformed();
481 } catch (Exception ex)
487 * Action on OK is to start the fetch for entered accession(s)
489 public void ok_actionPerformed()
492 * tidy inputs and check there is something to search for
494 String t0 = textArea.getText();
495 String text = t0.trim();
496 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
498 text = text.replace(",", ";");
500 text = text.replaceAll("(\\s|[; ])+", ";");
501 if (!t0.equals(text))
503 textArea.setText(text);
509 "Please enter a (semi-colon separated list of) database id(s)");
513 if (database.getSelectedIndex() == 0)
516 showErrorMessage("Please choose a database");
521 exampleBtn.setEnabled(false);
522 textArea.setEnabled(false);
523 okBtn.setEnabled(false);
524 closeBtn.setEnabled(false);
525 backBtn.setEnabled(false);
527 Thread worker = new Thread(this);
531 private void resetDialog()
533 exampleBtn.setEnabled(true);
534 textArea.setEnabled(true);
535 okBtn.setEnabled(true);
536 closeBtn.setEnabled(true);
537 backBtn.setEnabled(parentSearchPanel != null);
543 boolean addToLast = false;
544 List<String> aresultq = new ArrayList<>();
545 List<String> presultTitle = new ArrayList<>();
546 List<AlignmentI> presult = new ArrayList<>();
547 List<AlignmentI> aresult = new ArrayList<>();
548 List<DbSourceProxy> sources = sfetch.getSourceProxy(
549 ((StringPair) database.getSelectedItem()).getKey());
550 Iterator<DbSourceProxy> proxies = sources.iterator();
551 String[] qries = textArea.getText().trim().split(";");
552 List<String> nextFetch = Arrays.asList(qries);
553 Iterator<String> en = Arrays.asList(new String[0]).iterator();
554 int nqueries = qries.length;
556 FeatureSettingsModelI preferredFeatureColours = null;
557 while (proxies.hasNext() && (en.hasNext() || nextFetch.size() > 0))
559 if (!en.hasNext() && nextFetch.size() > 0)
561 en = nextFetch.iterator();
562 nqueries = nextFetch.size();
563 // save the remaining queries in the original array
564 qries = nextFetch.toArray(new String[nqueries]);
565 nextFetch = new ArrayList<>();
568 DbSourceProxy proxy = proxies.next();
572 guiWindow.setProgressBar(MessageManager.formatMessage(
573 "status.fetching_sequence_queries_from", new String[]
574 { Integer.valueOf(nqueries).toString(),
575 proxy.getDbName() }),
576 IdUtils.newId(IdType.PROGRESS, Thread.currentThread()));
577 if (proxy.getMaximumQueryCount() == 1)
580 * proxy only handles one accession id at a time
584 String acc = en.next();
585 if (!fetchSingleAccession(proxy, acc, aresultq, aresult))
594 * proxy can fetch multiple accessions at one time
596 fetchMultipleAccessions(proxy, en, aresultq, aresult, nextFetch);
598 } catch (Exception e)
600 showErrorMessage("Error retrieving " + textArea.getText() + " from "
601 + ((StringPair) database.getSelectedItem()).getDisplay());
603 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
604 jalview.bin.Console.errPrintln("Retrieval failed for source ='"
605 + ((StringPair) database.getSelectedItem()).getDisplay()
606 + "' and query\n'" + textArea.getText() + "'\n");
608 } catch (OutOfMemoryError e)
610 showErrorMessage("Out of Memory when retrieving "
611 + textArea.getText() + " from "
612 + ((StringPair) database.getSelectedItem()).getDisplay()
613 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
617 showErrorMessage("Serious Error retrieving " + textArea.getText()
619 + ((StringPair) database.getSelectedItem()).getDisplay());
623 // Stack results ready for opening in alignment windows
624 if (aresult != null && aresult.size() > 0)
626 FeatureSettingsModelI proxyColourScheme = proxy
627 .getFeatureColourScheme();
628 if (proxyColourScheme != null)
630 preferredFeatureColours = proxyColourScheme;
633 AlignmentI ar = null;
634 if (proxy.isAlignmentSource())
637 // new window for each result
638 while (aresult.size() > 0)
640 presult.add(aresult.remove(0));
642 aresultq.remove(0) + " " + getDefaultRetrievalTitle());
648 if (addToLast && presult.size() > 0)
650 ar = presult.remove(presult.size() - 1);
651 titl = presultTitle.remove(presultTitle.size() - 1);
653 // concatenate all results in one window
654 while (aresult.size() > 0)
658 ar = aresult.remove(0);
662 ar.append(aresult.remove(0));
667 presultTitle.add(titl);
670 guiWindow.setProgressBar(
671 MessageManager.getString("status.finshed_querying"),
672 IdUtils.newId(IdType.PROGRESS, Thread.currentThread()));
678 .getString("status.parsing_results")
679 : MessageManager.getString("status.processing"),
680 IdUtils.newId(IdType.PROGRESS, Thread.currentThread()));
682 while (presult.size() > 0)
684 parseResult(presult.remove(0), presultTitle.remove(0), null,
685 preferredFeatureColours);
687 // only remove visual delay after we finished parsing.
688 guiWindow.setProgressBar(null,
689 IdUtils.newId(IdType.PROGRESS, Thread.currentThread()));
690 if (nextFetch.size() > 0)
692 StringBuffer sb = new StringBuffer();
693 sb.append("Didn't retrieve the following "
694 + (nextFetch.size() == 1 ? "query"
695 : nextFetch.size() + " queries")
697 int l = sb.length(), lr = 0;
698 for (String s : nextFetch)
700 if (l != sb.length())
704 if (lr - sb.length() > 40)
710 showErrorMessage(sb.toString());
716 * Tries to fetch one or more accession ids from the database proxy
720 * the queries to fetch
722 * a successful queries list to add to
724 * a list of retrieved alignments to add to
726 * failed queries are added to this list
729 void fetchMultipleAccessions(DbSourceProxy proxy,
730 Iterator<String> accessions, List<String> aresultq,
731 List<AlignmentI> aresult, List<String> nextFetch) throws Exception
733 StringBuilder multiacc = new StringBuilder();
734 List<String> tosend = new ArrayList<>();
735 while (accessions.hasNext())
737 String nel = accessions.next();
739 multiacc.append(nel);
740 if (accessions.hasNext())
742 multiacc.append(proxy.getAccessionSeparator());
748 String query = multiacc.toString();
749 AlignmentI rslt = proxy.getSequenceRecords(query);
750 if (rslt == null || rslt.getHeight() == 0)
752 // no results - pass on all queries to next source
753 nextFetch.addAll(tosend);
759 if (tosend.size() > 1)
761 checkResultForQueries(rslt, tosend, nextFetch, proxy);
764 } catch (OutOfMemoryError oome)
766 new OOMWarning("fetching " + multiacc + " from "
767 + ((StringPair) database.getSelectedItem()).getDisplay(),
773 * Query for a single accession id via the database proxy
778 * a list of successful queries to add to
780 * a list of retrieved alignments to add to
781 * @return true if the fetch was successful, else false
783 boolean fetchSingleAccession(DbSourceProxy proxy, String accession,
784 List<String> aresultq, List<AlignmentI> aresult)
786 boolean success = false;
793 // give the server a chance to breathe
795 } catch (Exception e)
801 AlignmentI indres = null;
804 indres = proxy.getSequenceRecords(accession);
805 } catch (OutOfMemoryError oome)
808 "fetching " + accession + " from " + proxy.getDbName(),
813 aresultq.add(accession);
817 } catch (Exception e)
819 Console.info("Error retrieving " + accession + " from "
820 + proxy.getDbName(), e);
826 * Checks which of the queries were successfully retrieved by searching the
827 * DBRefs of the retrieved sequences for a match. Any not found are added to
828 * the 'nextFetch' list.
835 void checkResultForQueries(AlignmentI rslt, List<String> queries,
836 List<String> nextFetch, DbSourceProxy proxy)
838 SequenceI[] rs = rslt.getSequencesArray();
840 for (String q : queries)
842 // BH 2019.01.25 dbr is never used.
843 // DBRefEntry dbr = new DBRefEntry();
844 // dbr.setSource(proxy.getDbSource());
845 // dbr.setVersion(null);
846 String accId = proxy.getAccessionIdFromQuery(q);
847 // dbr.setAccessionId(accId);
848 boolean rfound = false;
849 for (int r = 0, nr = rs.length; r < nr; r++)
853 List<DBRefEntry> found = DBRefUtils.searchRefs(rs[r].getDBRefs(),
855 if (!found.isEmpty())
871 * @return a standard title for any results retrieved using the currently
872 * selected source and settings
874 public String getDefaultRetrievalTitle()
876 return "Retrieved from "
877 + ((StringPair) database.getSelectedItem()).getDisplay();
881 * constructs an alignment frame given the data and metadata
885 * @param currentFileFormat
886 * @param preferredFeatureColours
887 * @return the alignment
889 public AlignmentI parseResult(AlignmentI al, String title,
890 FileFormatI currentFileFormat,
891 FeatureSettingsModelI preferredFeatureColours)
894 if (al != null && al.getHeight() > 0)
898 title = getDefaultRetrievalTitle();
900 if (alignFrame == null)
902 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
903 AlignFrame.DEFAULT_HEIGHT);
904 if (currentFileFormat != null)
906 af.currentFileFormat = currentFileFormat;
909 List<SequenceI> alsqs = al.getSequences();
912 for (SequenceI sq : alsqs)
914 if (sq.getFeatures().hasFeatures())
916 af.setShowSeqFeatures(true);
922 af.getViewport().applyFeaturesStyle(preferredFeatureColours);
923 if (Cache.getDefault("HIDE_INTRONS", true))
925 af.hideFeatureColumns(SequenceOntologyI.EXON, false);
927 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
928 AlignFrame.DEFAULT_HEIGHT);
930 af.setStatus(MessageManager
931 .getString("label.successfully_pasted_alignment_file"));
935 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
936 } catch (Exception ex)
942 alignFrame.viewport.addAlignment(al, title);
948 void showErrorMessage(final String error)
951 javax.swing.SwingUtilities.invokeLater(new Runnable()
956 JvOptionPane.showInternalMessageDialog(Desktop.desktop, error,
957 MessageManager.getString("label.error_retrieving_data"),
958 JvOptionPane.WARNING_MESSAGE);
963 public IProgressIndicator getProgressIndicator()
965 return progressIndicator;
968 public void setProgressIndicator(IProgressIndicator progressIndicator)
970 this.progressIndicator = progressIndicator;
974 * Hide this panel (on clicking the database button to open the database
979 frame.setVisible(false);
982 public void setQuery(String ids)
984 textArea.setText(ids);
988 * Called to modify the search panel for embedding as an alternative tab of a
989 * free text search panel. The database choice list is hidden (since the
990 * choice has been made), and a Back button is made visible (which reopens the
991 * Sequence Fetcher panel).
995 public void embedIn(GFTSPanel parentPanel)
997 database.setVisible(false);
998 backBtn.setVisible(true);
999 parentSearchPanel = parentPanel;