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 jalview.api.FeatureSettingsModelI;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.fts.core.GFTSPanel;
29 import jalview.fts.service.pdb.PDBFTSPanel;
30 import jalview.fts.service.uniprot.UniprotFTSPanel;
31 import jalview.io.FileFormatI;
32 import jalview.io.gff.SequenceOntologyI;
33 import jalview.util.DBRefUtils;
34 import jalview.util.MessageManager;
35 import jalview.util.Platform;
36 import jalview.ws.seqfetcher.DbSourceProxy;
38 import java.awt.BorderLayout;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.KeyAdapter;
43 import java.awt.event.KeyEvent;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.HashSet;
47 import java.util.Iterator;
48 import java.util.List;
50 import javax.swing.JButton;
51 import javax.swing.JCheckBox;
52 import javax.swing.JComboBox;
53 import javax.swing.JInternalFrame;
54 import javax.swing.JLabel;
55 import javax.swing.JPanel;
56 import javax.swing.JScrollPane;
57 import javax.swing.JTextArea;
58 import javax.swing.SwingConstants;
61 * A panel where the use may choose a database source, and enter one or more
62 * accessions, to retrieve entries from the database.
64 * If the selected source is Uniprot or PDB, a free text search panel is opened
65 * instead to perform the search and selection.
67 public class SequenceFetcher extends JPanel implements Runnable
69 private class StringPair
73 private String display;
75 public StringPair(String s1, String s2)
81 public StringPair(String s)
86 public String getKey()
91 public String getDisplay()
97 public String toString()
102 public boolean equals(StringPair other)
104 return other.key == this.key;
107 private static jalview.ws.SequenceFetcher sfetch = null;
109 JLabel exampleAccession;
111 JComboBox<StringPair> database;
113 JCheckBox replacePunctuation;
125 JInternalFrame frame;
127 IProgressIndicator guiWindow;
129 AlignFrame alignFrame;
131 GFTSPanel parentSearchPanel;
133 IProgressIndicator progressIndicator;
135 volatile boolean _isConstructing = false;
138 * Returns the shared instance of the SequenceFetcher client
142 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton()
146 sfetch = new jalview.ws.SequenceFetcher();
152 * Constructor given a client to receive any status or progress messages
153 * (currently either the Desktop, or an AlignFrame panel)
157 public SequenceFetcher(IProgressIndicator guiIndic)
159 this(guiIndic, null, null);
163 * Constructor with specified database and accession(s) to retrieve
169 public SequenceFetcher(IProgressIndicator guiIndic,
170 final String selectedDb, final String queryString)
172 this.progressIndicator = guiIndic;
173 getSequenceFetcherSingleton();
174 this.guiWindow = progressIndicator;
176 if (progressIndicator instanceof AlignFrame)
178 alignFrame = (AlignFrame) progressIndicator;
182 textArea.setText(queryString);
184 frame = new JInternalFrame();
185 frame.setContentPane(this);
186 Desktop.addInternalFrame(frame, getFrameTitle(), Desktop.FRAME_MAKE_VISIBLE, 400,
187 Platform.isAMacAndNotJS() ? 240 : 180, Desktop.FRAME_ALLOW_RESIZE, Desktop.FRAME_SET_MIN_SIZE_300);
190 private String getFrameTitle()
192 return ((alignFrame == null)
193 ? MessageManager.getString("label.new_sequence_fetcher")
195 .getString("label.additional_sequence_fetcher"));
198 private void jbInit(String selectedDb)
200 this.setLayout(new BorderLayout());
202 database = new JComboBox<>();
203 database.setFont(JvSwingUtils.getLabelFont());
204 StringPair instructionItem = new StringPair(
205 MessageManager.getString("action.select_ddbb"));
206 database.setPrototypeDisplayValue(instructionItem);
207 String[] sources = new jalview.ws.SequenceFetcher().getSupportedDb();
208 Arrays.sort(sources, String.CASE_INSENSITIVE_ORDER);
209 database.addItem(instructionItem);
210 for (String source : sources)
212 List<DbSourceProxy> slist = sfetch.getSourceProxy(source);
213 if (slist.size() == 1 && slist.get(0) != null)
215 database.addItem(new StringPair(source, slist.get(0).getDbName()));
219 database.addItem(new StringPair(source));
222 setDatabaseSelectedItem(selectedDb);
223 if (database.getSelectedIndex() == -1)
225 database.setSelectedIndex(0);
227 database.setMaximumRowCount(database.getItemCount());
228 database.addActionListener(new ActionListener()
231 public void actionPerformed(ActionEvent e)
233 String currentSelection = ((StringPair) database.getSelectedItem())
235 updateExampleQuery(currentSelection);
237 if ("pdb".equalsIgnoreCase(currentSelection))
240 new PDBFTSPanel(SequenceFetcher.this);
242 else if ("uniprot".equalsIgnoreCase(currentSelection))
245 new UniprotFTSPanel(SequenceFetcher.this);
247 else if ("3d-beacons".equalsIgnoreCase(currentSelection))
250 new TDBeaconsFTSPanel(SequenceFetcher.this);
259 exampleAccession = new JLabel("");
260 exampleAccession.setFont(new Font("Verdana", Font.BOLD, 11));
261 JLabel jLabel1 = new JLabel(MessageManager
262 .getString("label.separate_multiple_accession_ids"));
263 jLabel1.setFont(new Font("Verdana", Font.ITALIC, 11));
264 jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
266 replacePunctuation = new JCheckBox(
267 MessageManager.getString("label.replace_commas_semicolons"));
268 replacePunctuation.setHorizontalAlignment(SwingConstants.LEFT);
269 replacePunctuation.setFont(new Font("Verdana", Font.ITALIC, 11));
270 okBtn = new JButton(MessageManager.getString("action.ok"));
271 okBtn.addActionListener(new ActionListener()
274 public void actionPerformed(ActionEvent e)
276 ok_actionPerformed();
279 JButton clear = new JButton(MessageManager.getString("action.clear"));
280 clear.addActionListener(new ActionListener()
283 public void actionPerformed(ActionEvent e)
285 clear_actionPerformed();
289 exampleBtn = new JButton(MessageManager.getString("label.example"));
290 exampleBtn.addActionListener(new ActionListener()
293 public void actionPerformed(ActionEvent e)
295 example_actionPerformed();
298 closeBtn = new JButton(MessageManager.getString("action.cancel"));
299 closeBtn.addActionListener(new ActionListener()
302 public void actionPerformed(ActionEvent e)
304 close_actionPerformed(e);
307 backBtn = new JButton(MessageManager.getString("action.back"));
308 backBtn.addActionListener(new ActionListener()
311 public void actionPerformed(ActionEvent e)
313 parentSearchPanel.btn_back_ActionPerformed();
316 // back not visible unless embedded
317 backBtn.setVisible(false);
319 textArea = new JTextArea();
320 textArea.setFont(JvSwingUtils.getLabelFont());
321 textArea.setLineWrap(true);
322 textArea.addKeyListener(new KeyAdapter()
325 public void keyPressed(KeyEvent e)
327 if (e.getKeyCode() == KeyEvent.VK_ENTER)
329 ok_actionPerformed();
334 JPanel actionPanel = new JPanel();
335 actionPanel.add(backBtn);
336 actionPanel.add(exampleBtn);
337 actionPanel.add(clear);
338 actionPanel.add(okBtn);
339 actionPanel.add(closeBtn);
341 JPanel databasePanel = new JPanel();
342 databasePanel.setLayout(new BorderLayout());
343 databasePanel.add(database, BorderLayout.NORTH);
344 databasePanel.add(exampleAccession, BorderLayout.CENTER);
345 JPanel jPanel2a = new JPanel(new BorderLayout());
346 jPanel2a.add(jLabel1, BorderLayout.NORTH);
347 jPanel2a.add(replacePunctuation, BorderLayout.SOUTH);
348 databasePanel.add(jPanel2a, BorderLayout.SOUTH);
350 JPanel idsPanel = new JPanel();
351 idsPanel.setLayout(new BorderLayout(0, 5));
352 JScrollPane jScrollPane1 = new JScrollPane();
353 jScrollPane1.getViewport().add(textArea);
354 idsPanel.add(jScrollPane1, BorderLayout.CENTER);
356 this.add(actionPanel, BorderLayout.SOUTH);
357 this.add(idsPanel, BorderLayout.CENTER);
358 this.add(databasePanel, BorderLayout.NORTH);
361 private void setDatabaseSelectedItem(String db)
363 for (int i = 0; i < database.getItemCount(); i++)
365 StringPair sp = database.getItemAt(i);
366 if (sp != null && db != null && db.equals(sp.getKey()))
368 database.setSelectedIndex(i);
374 * Answers a semi-colon-delimited string with the example query or queries for
375 * the selected database
380 protected String getExampleQueries(String db)
382 StringBuilder sb = new StringBuilder();
383 HashSet<String> hs = new HashSet<>();
384 for (DbSourceProxy dbs : sfetch.getSourceProxy(db))
386 String tq = dbs.getTestQuery();
387 if (hs.add(tq)) // not a duplicate source
396 return sb.toString();
400 * Action on selecting a database other than Uniprot or PDB is to enable or
401 * disable 'Replace commas', and await input in the query field
403 protected void otherSourceAction()
407 String eq = exampleAccession.getText();
408 // TODO this should be a property of the SequenceFetcher whether commas
409 // are allowed in the IDs...
411 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
412 replacePunctuation.setEnabled(enablePunct);
414 } catch (Exception ex)
416 exampleAccession.setText("");
417 replacePunctuation.setEnabled(true);
423 * Sets the text of the example query to incorporate the example accession
424 * provided by the selected database source
426 * @param selectedDatabase
429 protected String updateExampleQuery(String selectedDatabase)
431 String eq = getExampleQueries(selectedDatabase);
432 exampleAccession.setText(MessageManager
433 .formatMessage("label.example_query_param", new String[]
439 * Action on clicking the 'Example' button is to write the example accession
440 * as the query text field value
442 protected void example_actionPerformed()
444 String eq = getExampleQueries(
445 ((StringPair) database.getSelectedItem()).getKey());
446 textArea.setText(eq);
451 * Clears the query input field
453 protected void clear_actionPerformed()
455 textArea.setText("");
460 * Action on Close button is to close this frame, and also (if it is embedded
461 * in a search panel) to close the search panel
465 protected void close_actionPerformed(ActionEvent e)
469 frame.setClosed(true);
470 if (parentSearchPanel != null)
472 parentSearchPanel.btn_cancel_ActionPerformed();
474 } catch (Exception ex)
480 * Action on OK is to start the fetch for entered accession(s)
482 public void ok_actionPerformed()
485 * tidy inputs and check there is something to search for
487 String t0 = textArea.getText();
488 String text = t0.trim();
489 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
491 text = text.replace(",", ";");
493 text = text.replaceAll("(\\s|[; ])+", ";");
494 if (!t0.equals(text))
496 textArea.setText(text);
502 "Please enter a (semi-colon separated list of) database id(s)");
506 if (database.getSelectedIndex() == 0)
509 showErrorMessage("Please choose a database");
514 exampleBtn.setEnabled(false);
515 textArea.setEnabled(false);
516 okBtn.setEnabled(false);
517 closeBtn.setEnabled(false);
518 backBtn.setEnabled(false);
520 Thread worker = new Thread(this);
524 private void resetDialog()
526 exampleBtn.setEnabled(true);
527 textArea.setEnabled(true);
528 okBtn.setEnabled(true);
529 closeBtn.setEnabled(true);
530 backBtn.setEnabled(parentSearchPanel != null);
536 boolean addToLast = false;
537 List<String> aresultq = new ArrayList<>();
538 List<String> presultTitle = new ArrayList<>();
539 List<AlignmentI> presult = new ArrayList<>();
540 List<AlignmentI> aresult = new ArrayList<>();
541 List<DbSourceProxy> sources = sfetch.getSourceProxy(
542 ((StringPair) database.getSelectedItem()).getKey());
543 Iterator<DbSourceProxy> proxies = sources.iterator();
544 String[] qries = textArea.getText().trim().split(";");
545 List<String> nextFetch = Arrays.asList(qries);
546 Iterator<String> en = Arrays.asList(new String[0]).iterator();
547 int nqueries = qries.length;
549 FeatureSettingsModelI preferredFeatureColours = null;
550 while (proxies.hasNext() && (en.hasNext() || nextFetch.size() > 0))
552 if (!en.hasNext() && nextFetch.size() > 0)
554 en = nextFetch.iterator();
555 nqueries = nextFetch.size();
556 // save the remaining queries in the original array
557 qries = nextFetch.toArray(new String[nqueries]);
558 nextFetch = new ArrayList<>();
561 DbSourceProxy proxy = proxies.next();
565 guiWindow.setProgressBar(MessageManager.formatMessage(
566 "status.fetching_sequence_queries_from", new String[]
567 { Integer.valueOf(nqueries).toString(),
568 proxy.getDbName() }),
569 Thread.currentThread().hashCode());
570 if (proxy.getMaximumQueryCount() == 1)
573 * proxy only handles one accession id at a time
577 String acc = en.next();
578 if (!fetchSingleAccession(proxy, acc, aresultq, aresult))
587 * proxy can fetch multiple accessions at one time
589 fetchMultipleAccessions(proxy, en, aresultq, aresult, nextFetch);
591 } catch (Exception e)
593 showErrorMessage("Error retrieving " + textArea.getText() + " from "
594 + ((StringPair) database.getSelectedItem()).getDisplay());
596 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
597 System.err.println("Retrieval failed for source ='"
598 + ((StringPair) database.getSelectedItem()).getDisplay()
599 + "' and query\n'" + textArea.getText() + "'\n");
601 } catch (OutOfMemoryError e)
603 showErrorMessage("Out of Memory when retrieving "
604 + textArea.getText() + " from "
605 + ((StringPair) database.getSelectedItem()).getDisplay()
606 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
610 showErrorMessage("Serious Error retrieving " + textArea.getText()
612 + ((StringPair) database.getSelectedItem()).getDisplay());
616 // Stack results ready for opening in alignment windows
617 if (aresult != null && aresult.size() > 0)
619 FeatureSettingsModelI proxyColourScheme = proxy
620 .getFeatureColourScheme();
621 if (proxyColourScheme != null)
623 preferredFeatureColours = proxyColourScheme;
626 AlignmentI ar = null;
627 if (proxy.isAlignmentSource())
630 // new window for each result
631 while (aresult.size() > 0)
633 presult.add(aresult.remove(0));
635 aresultq.remove(0) + " " + getDefaultRetrievalTitle());
641 if (addToLast && presult.size() > 0)
643 ar = presult.remove(presult.size() - 1);
644 titl = presultTitle.remove(presultTitle.size() - 1);
646 // concatenate all results in one window
647 while (aresult.size() > 0)
651 ar = aresult.remove(0);
655 ar.append(aresult.remove(0));
660 presultTitle.add(titl);
663 guiWindow.setProgressBar(
664 MessageManager.getString("status.finshed_querying"),
665 Thread.currentThread().hashCode());
671 .getString("status.parsing_results")
672 : MessageManager.getString("status.processing"),
673 Thread.currentThread().hashCode());
675 while (presult.size() > 0)
677 parseResult(presult.remove(0), presultTitle.remove(0), null,
678 preferredFeatureColours);
680 // only remove visual delay after we finished parsing.
681 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
682 if (nextFetch.size() > 0)
684 StringBuffer sb = new StringBuffer();
685 sb.append("Didn't retrieve the following "
686 + (nextFetch.size() == 1 ? "query"
687 : nextFetch.size() + " queries")
689 int l = sb.length(), lr = 0;
690 for (String s : nextFetch)
692 if (l != sb.length())
696 if (lr - sb.length() > 40)
702 showErrorMessage(sb.toString());
708 * Tries to fetch one or more accession ids from the database proxy
712 * the queries to fetch
714 * a successful queries list to add to
716 * a list of retrieved alignments to add to
718 * failed queries are added to this list
721 void fetchMultipleAccessions(DbSourceProxy proxy,
722 Iterator<String> accessions, List<String> aresultq,
723 List<AlignmentI> aresult, List<String> nextFetch) throws Exception
725 StringBuilder multiacc = new StringBuilder();
726 List<String> tosend = new ArrayList<>();
727 while (accessions.hasNext())
729 String nel = accessions.next();
731 multiacc.append(nel);
732 if (accessions.hasNext())
734 multiacc.append(proxy.getAccessionSeparator());
740 String query = multiacc.toString();
741 AlignmentI rslt = proxy.getSequenceRecords(query);
742 if (rslt == null || rslt.getHeight() == 0)
744 // no results - pass on all queries to next source
745 nextFetch.addAll(tosend);
751 if (tosend.size() > 1)
753 checkResultForQueries(rslt, tosend, nextFetch, proxy);
756 } catch (OutOfMemoryError oome)
758 new OOMWarning("fetching " + multiacc + " from "
759 + ((StringPair) database.getSelectedItem()).getDisplay(),
765 * Query for a single accession id via the database proxy
770 * a list of successful queries to add to
772 * a list of retrieved alignments to add to
773 * @return true if the fetch was successful, else false
775 boolean fetchSingleAccession(DbSourceProxy proxy, String accession,
776 List<String> aresultq, List<AlignmentI> aresult)
778 boolean success = false;
785 // give the server a chance to breathe
787 } catch (Exception e)
793 AlignmentI indres = null;
796 indres = proxy.getSequenceRecords(accession);
797 } catch (OutOfMemoryError oome)
800 "fetching " + accession + " from " + proxy.getDbName(),
805 aresultq.add(accession);
809 } catch (Exception e)
811 Cache.log.info("Error retrieving " + accession + " from "
812 + proxy.getDbName(), e);
818 * Checks which of the queries were successfully retrieved by searching the
819 * DBRefs of the retrieved sequences for a match. Any not found are added to
820 * the 'nextFetch' list.
827 void checkResultForQueries(AlignmentI rslt, List<String> queries,
828 List<String> nextFetch, DbSourceProxy proxy)
830 SequenceI[] rs = rslt.getSequencesArray();
832 for (String q : queries)
834 // BH 2019.01.25 dbr is never used.
835 // DBRefEntry dbr = new DBRefEntry();
836 // dbr.setSource(proxy.getDbSource());
837 // dbr.setVersion(null);
838 String accId = proxy.getAccessionIdFromQuery(q);
839 // dbr.setAccessionId(accId);
840 boolean rfound = false;
841 for (int r = 0, nr = rs.length; r < nr; r++)
845 List<DBRefEntry> found = DBRefUtils.searchRefs(rs[r].getDBRefs(),
847 if (!found.isEmpty())
863 * @return a standard title for any results retrieved using the currently
864 * selected source and settings
866 public String getDefaultRetrievalTitle()
868 return "Retrieved from "
869 + ((StringPair) database.getSelectedItem()).getDisplay();
873 * constructs an alignment frame given the data and metadata
877 * @param currentFileFormat
878 * @param preferredFeatureColours
879 * @return the alignment
881 public AlignmentI parseResult(AlignmentI al, String title,
882 FileFormatI currentFileFormat,
883 FeatureSettingsModelI preferredFeatureColours)
886 if (al != null && al.getHeight() > 0)
890 title = getDefaultRetrievalTitle();
892 if (alignFrame == null)
894 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
895 AlignFrame.DEFAULT_HEIGHT);
896 if (currentFileFormat != null)
898 af.currentFileFormat = currentFileFormat;
901 List<SequenceI> alsqs = al.getSequences();
904 for (SequenceI sq : alsqs)
906 if (sq.getFeatures().hasFeatures())
908 af.setShowSeqFeatures(true);
914 if (preferredFeatureColours != null)
916 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.getDesktopPane(), 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;