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.threedbeacons.TDBeaconsFTSPanel;
31 import jalview.fts.service.uniprot.UniprotFTSPanel;
32 import jalview.io.FileFormatI;
33 import jalview.io.gff.SequenceOntologyI;
34 import jalview.util.DBRefUtils;
35 import jalview.util.MessageManager;
36 import jalview.util.Platform;
37 import jalview.ws.seqfetcher.DbSourceProxy;
39 import java.awt.BorderLayout;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.KeyAdapter;
44 import java.awt.event.KeyEvent;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.HashSet;
48 import java.util.Iterator;
49 import java.util.List;
51 import javax.swing.JButton;
52 import javax.swing.JCheckBox;
53 import javax.swing.JComboBox;
54 import javax.swing.JInternalFrame;
55 import javax.swing.JLabel;
56 import javax.swing.JPanel;
57 import javax.swing.JScrollPane;
58 import javax.swing.JTextArea;
59 import javax.swing.SwingConstants;
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 static jalview.ws.SequenceFetcher sfetch = null;
72 JLabel exampleAccession;
74 JComboBox<String> database;
76 JCheckBox replacePunctuation;
90 IProgressIndicator guiWindow;
92 AlignFrame alignFrame;
94 GFTSPanel parentSearchPanel;
96 IProgressIndicator progressIndicator;
98 volatile boolean _isConstructing = false;
101 * Returns the shared instance of the SequenceFetcher client
105 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton()
109 sfetch = new jalview.ws.SequenceFetcher();
115 * Constructor given a client to receive any status or progress messages
116 * (currently either the Desktop, or an AlignFrame panel)
120 public SequenceFetcher(IProgressIndicator guiIndic)
122 this(guiIndic, null, null);
126 * Constructor with specified database and accession(s) to retrieve
132 public SequenceFetcher(IProgressIndicator guiIndic,
133 final String selectedDb, final String queryString)
135 this.progressIndicator = guiIndic;
136 getSequenceFetcherSingleton();
137 this.guiWindow = progressIndicator;
139 if (progressIndicator instanceof AlignFrame)
141 alignFrame = (AlignFrame) progressIndicator;
145 textArea.setText(queryString);
147 frame = new JInternalFrame();
148 frame.setContentPane(this);
149 Desktop.addInternalFrame(frame, getFrameTitle(), true, 400,
150 Platform.isAMacAndNotJS() ? 240 : 180);
153 private String getFrameTitle()
155 return ((alignFrame == null)
156 ? MessageManager.getString("label.new_sequence_fetcher")
158 .getString("label.additional_sequence_fetcher"));
161 private void jbInit(String selectedDb)
163 this.setLayout(new BorderLayout());
165 database = new JComboBox<>();
166 database.setFont(JvSwingUtils.getLabelFont());
167 database.setPrototypeDisplayValue("ENSEMBLGENOMES ");
168 String[] sources = new jalview.ws.SequenceFetcher().getSupportedDb();
169 Arrays.sort(sources, String.CASE_INSENSITIVE_ORDER);
170 database.addItem(MessageManager.getString("action.select_ddbb"));
171 for (String source : sources)
173 database.addItem(source);
175 database.setSelectedItem(selectedDb);
176 if (database.getSelectedIndex() == -1)
178 database.setSelectedIndex(0);
180 database.setMaximumRowCount(database.getItemCount());
181 database.addActionListener(new ActionListener()
184 public void actionPerformed(ActionEvent e)
186 String currentSelection = (String) database.getSelectedItem();
187 updateExampleQuery(currentSelection);
189 if ("pdb".equalsIgnoreCase(currentSelection))
192 new PDBFTSPanel(SequenceFetcher.this);
194 else if ("uniprot".equalsIgnoreCase(currentSelection))
197 new UniprotFTSPanel(SequenceFetcher.this);
199 else if ("3d-beacons".equalsIgnoreCase(currentSelection))
202 new TDBeaconsFTSPanel(SequenceFetcher.this);
211 exampleAccession = new JLabel("");
212 exampleAccession.setFont(new Font("Verdana", Font.BOLD, 11));
213 JLabel jLabel1 = new JLabel(MessageManager
214 .getString("label.separate_multiple_accession_ids"));
215 jLabel1.setFont(new Font("Verdana", Font.ITALIC, 11));
216 jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
218 replacePunctuation = new JCheckBox(
219 MessageManager.getString("label.replace_commas_semicolons"));
220 replacePunctuation.setHorizontalAlignment(SwingConstants.LEFT);
221 replacePunctuation.setFont(new Font("Verdana", Font.ITALIC, 11));
222 okBtn = new JButton(MessageManager.getString("action.ok"));
223 okBtn.addActionListener(new ActionListener()
226 public void actionPerformed(ActionEvent e)
228 ok_actionPerformed();
231 JButton clear = new JButton(MessageManager.getString("action.clear"));
232 clear.addActionListener(new ActionListener()
235 public void actionPerformed(ActionEvent e)
237 clear_actionPerformed();
241 exampleBtn = new JButton(MessageManager.getString("label.example"));
242 exampleBtn.addActionListener(new ActionListener()
245 public void actionPerformed(ActionEvent e)
247 example_actionPerformed();
250 closeBtn = new JButton(MessageManager.getString("action.cancel"));
251 closeBtn.addActionListener(new ActionListener()
254 public void actionPerformed(ActionEvent e)
256 close_actionPerformed(e);
259 backBtn = new JButton(MessageManager.getString("action.back"));
260 backBtn.addActionListener(new ActionListener()
263 public void actionPerformed(ActionEvent e)
265 parentSearchPanel.btn_back_ActionPerformed();
268 // back not visible unless embedded
269 backBtn.setVisible(false);
271 textArea = new JTextArea();
272 textArea.setFont(JvSwingUtils.getLabelFont());
273 textArea.setLineWrap(true);
274 textArea.addKeyListener(new KeyAdapter()
277 public void keyPressed(KeyEvent e)
279 if (e.getKeyCode() == KeyEvent.VK_ENTER)
281 ok_actionPerformed();
286 JPanel actionPanel = new JPanel();
287 actionPanel.add(backBtn);
288 actionPanel.add(exampleBtn);
289 actionPanel.add(clear);
290 actionPanel.add(okBtn);
291 actionPanel.add(closeBtn);
293 JPanel databasePanel = new JPanel();
294 databasePanel.setLayout(new BorderLayout());
295 databasePanel.add(database, BorderLayout.NORTH);
296 databasePanel.add(exampleAccession, BorderLayout.CENTER);
297 JPanel jPanel2a = new JPanel(new BorderLayout());
298 jPanel2a.add(jLabel1, BorderLayout.NORTH);
299 jPanel2a.add(replacePunctuation, BorderLayout.SOUTH);
300 databasePanel.add(jPanel2a, BorderLayout.SOUTH);
302 JPanel idsPanel = new JPanel();
303 idsPanel.setLayout(new BorderLayout(0, 5));
304 JScrollPane jScrollPane1 = new JScrollPane();
305 jScrollPane1.getViewport().add(textArea);
306 idsPanel.add(jScrollPane1, BorderLayout.CENTER);
308 this.add(actionPanel, BorderLayout.SOUTH);
309 this.add(idsPanel, BorderLayout.CENTER);
310 this.add(databasePanel, BorderLayout.NORTH);
314 * Answers a semi-colon-delimited string with the example query or queries for
315 * the selected database
320 protected String getExampleQueries(String db)
322 StringBuilder sb = new StringBuilder();
323 HashSet<String> hs = new HashSet<>();
324 for (DbSourceProxy dbs : sfetch.getSourceProxy(db))
326 String tq = dbs.getTestQuery();
327 if (hs.add(tq)) // not a duplicate source
336 return sb.toString();
340 * Action on selecting a database other than Uniprot or PDB is to enable or
341 * disable 'Replace commas', and await input in the query field
343 protected void otherSourceAction()
347 String eq = exampleAccession.getText();
348 // TODO this should be a property of the SequenceFetcher whether commas
349 // are allowed in the IDs...
351 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
352 replacePunctuation.setEnabled(enablePunct);
354 } catch (Exception ex)
356 exampleAccession.setText("");
357 replacePunctuation.setEnabled(true);
363 * Sets the text of the example query to incorporate the example accession
364 * provided by the selected database source
366 * @param selectedDatabase
369 protected String updateExampleQuery(String selectedDatabase)
371 String eq = getExampleQueries(selectedDatabase);
372 exampleAccession.setText(MessageManager
373 .formatMessage("label.example_query_param", new String[]
379 * Action on clicking the 'Example' button is to write the example accession
380 * as the query text field value
382 protected void example_actionPerformed()
384 String eq = getExampleQueries((String) database.getSelectedItem());
385 textArea.setText(eq);
390 * Clears the query input field
392 protected void clear_actionPerformed()
394 textArea.setText("");
399 * Action on Close button is to close this frame, and also (if it is embedded
400 * in a search panel) to close the search panel
404 protected void close_actionPerformed(ActionEvent e)
408 frame.setClosed(true);
409 if (parentSearchPanel != null)
411 parentSearchPanel.btn_cancel_ActionPerformed();
413 } catch (Exception ex)
419 * Action on OK is to start the fetch for entered accession(s)
421 public void ok_actionPerformed()
424 * tidy inputs and check there is something to search for
426 String t0 = textArea.getText();
427 String text = t0.trim();
428 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
430 text = text.replace(",", ";");
432 text = text.replaceAll("(\\s|[; ])+", ";");
433 if (!t0.equals(text))
435 textArea.setText(text);
441 "Please enter a (semi-colon separated list of) database id(s)");
445 if (database.getSelectedIndex() == 0)
448 showErrorMessage("Please choose a database");
453 exampleBtn.setEnabled(false);
454 textArea.setEnabled(false);
455 okBtn.setEnabled(false);
456 closeBtn.setEnabled(false);
457 backBtn.setEnabled(false);
459 Thread worker = new Thread(this);
463 private void resetDialog()
465 exampleBtn.setEnabled(true);
466 textArea.setEnabled(true);
467 okBtn.setEnabled(true);
468 closeBtn.setEnabled(true);
469 backBtn.setEnabled(parentSearchPanel != null);
475 boolean addToLast = false;
476 List<String> aresultq = new ArrayList<>();
477 List<String> presultTitle = new ArrayList<>();
478 List<AlignmentI> presult = new ArrayList<>();
479 List<AlignmentI> aresult = new ArrayList<>();
480 List<DbSourceProxy> sources = sfetch
481 .getSourceProxy((String) database.getSelectedItem());
482 Iterator<DbSourceProxy> proxies = sources.iterator();
483 String[] qries = textArea.getText().trim().split(";");
484 List<String> nextFetch = Arrays.asList(qries);
485 Iterator<String> en = Arrays.asList(new String[0]).iterator();
486 int nqueries = qries.length;
488 FeatureSettingsModelI preferredFeatureColours = null;
489 while (proxies.hasNext() && (en.hasNext() || nextFetch.size() > 0))
491 if (!en.hasNext() && nextFetch.size() > 0)
493 en = nextFetch.iterator();
494 nqueries = nextFetch.size();
495 // save the remaining queries in the original array
496 qries = nextFetch.toArray(new String[nqueries]);
497 nextFetch = new ArrayList<>();
500 DbSourceProxy proxy = proxies.next();
504 guiWindow.setProgressBar(MessageManager.formatMessage(
505 "status.fetching_sequence_queries_from", new String[]
506 { Integer.valueOf(nqueries).toString(),
507 proxy.getDbName() }),
508 Thread.currentThread().hashCode());
509 if (proxy.getMaximumQueryCount() == 1)
512 * proxy only handles one accession id at a time
516 String acc = en.next();
517 if (!fetchSingleAccession(proxy, acc, aresultq, aresult))
526 * proxy can fetch multiple accessions at one time
528 fetchMultipleAccessions(proxy, en, aresultq, aresult, nextFetch);
530 } catch (Exception e)
532 showErrorMessage("Error retrieving " + textArea.getText() + " from "
533 + database.getSelectedItem());
535 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
536 System.err.println("Retrieval failed for source ='"
537 + database.getSelectedItem() + "' and query\n'"
538 + textArea.getText() + "'\n");
540 } catch (OutOfMemoryError e)
542 showErrorMessage("Out of Memory when retrieving "
543 + textArea.getText() + " from " + database.getSelectedItem()
544 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
548 showErrorMessage("Serious Error retrieving " + textArea.getText()
549 + " from " + database.getSelectedItem());
553 // Stack results ready for opening in alignment windows
554 if (aresult != null && aresult.size() > 0)
556 FeatureSettingsModelI proxyColourScheme = proxy
557 .getFeatureColourScheme();
558 if (proxyColourScheme != null)
560 preferredFeatureColours = proxyColourScheme;
563 AlignmentI ar = null;
564 if (proxy.isAlignmentSource())
567 // new window for each result
568 while (aresult.size() > 0)
570 presult.add(aresult.remove(0));
572 aresultq.remove(0) + " " + getDefaultRetrievalTitle());
578 if (addToLast && presult.size() > 0)
580 ar = presult.remove(presult.size() - 1);
581 titl = presultTitle.remove(presultTitle.size() - 1);
583 // concatenate all results in one window
584 while (aresult.size() > 0)
588 ar = aresult.remove(0);
592 ar.append(aresult.remove(0));
597 presultTitle.add(titl);
600 guiWindow.setProgressBar(
601 MessageManager.getString("status.finshed_querying"),
602 Thread.currentThread().hashCode());
608 .getString("status.parsing_results")
609 : MessageManager.getString("status.processing"),
610 Thread.currentThread().hashCode());
612 while (presult.size() > 0)
614 parseResult(presult.remove(0), presultTitle.remove(0), null,
615 preferredFeatureColours);
617 // only remove visual delay after we finished parsing.
618 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
619 if (nextFetch.size() > 0)
621 StringBuffer sb = new StringBuffer();
622 sb.append("Didn't retrieve the following "
623 + (nextFetch.size() == 1 ? "query"
624 : nextFetch.size() + " queries")
626 int l = sb.length(), lr = 0;
627 for (String s : nextFetch)
629 if (l != sb.length())
633 if (lr - sb.length() > 40)
639 showErrorMessage(sb.toString());
645 * Tries to fetch one or more accession ids from the database proxy
649 * the queries to fetch
651 * a successful queries list to add to
653 * a list of retrieved alignments to add to
655 * failed queries are added to this list
658 void fetchMultipleAccessions(DbSourceProxy proxy,
659 Iterator<String> accessions, List<String> aresultq,
660 List<AlignmentI> aresult, List<String> nextFetch) throws Exception
662 StringBuilder multiacc = new StringBuilder();
663 List<String> tosend = new ArrayList<>();
664 while (accessions.hasNext())
666 String nel = accessions.next();
668 multiacc.append(nel);
669 if (accessions.hasNext())
671 multiacc.append(proxy.getAccessionSeparator());
677 String query = multiacc.toString();
678 AlignmentI rslt = proxy.getSequenceRecords(query);
679 if (rslt == null || rslt.getHeight() == 0)
681 // no results - pass on all queries to next source
682 nextFetch.addAll(tosend);
688 if (tosend.size() > 1)
690 checkResultForQueries(rslt, tosend, nextFetch, proxy);
693 } catch (OutOfMemoryError oome)
695 new OOMWarning("fetching " + multiacc + " from "
696 + database.getSelectedItem(), oome, this);
701 * Query for a single accession id via the database proxy
706 * a list of successful queries to add to
708 * a list of retrieved alignments to add to
709 * @return true if the fetch was successful, else false
711 boolean fetchSingleAccession(DbSourceProxy proxy, String accession,
712 List<String> aresultq, List<AlignmentI> aresult)
714 boolean success = false;
721 // give the server a chance to breathe
723 } catch (Exception e)
729 AlignmentI indres = null;
732 indres = proxy.getSequenceRecords(accession);
733 } catch (OutOfMemoryError oome)
736 "fetching " + accession + " from " + proxy.getDbName(),
741 aresultq.add(accession);
745 } catch (Exception e)
747 Cache.log.info("Error retrieving " + accession + " from "
748 + proxy.getDbName(), e);
754 * Checks which of the queries were successfully retrieved by searching the
755 * DBRefs of the retrieved sequences for a match. Any not found are added to
756 * the 'nextFetch' list.
763 void checkResultForQueries(AlignmentI rslt, List<String> queries,
764 List<String> nextFetch, DbSourceProxy proxy)
766 SequenceI[] rs = rslt.getSequencesArray();
768 for (String q : queries)
770 // BH 2019.01.25 dbr is never used.
771 // DBRefEntry dbr = new DBRefEntry();
772 // dbr.setSource(proxy.getDbSource());
773 // dbr.setVersion(null);
774 String accId = proxy.getAccessionIdFromQuery(q);
775 // dbr.setAccessionId(accId);
776 boolean rfound = false;
777 for (int r = 0, nr = rs.length; r < nr; r++)
781 List<DBRefEntry> found = DBRefUtils.searchRefs(rs[r].getDBRefs(),
783 if (!found.isEmpty())
799 * @return a standard title for any results retrieved using the currently
800 * selected source and settings
802 public String getDefaultRetrievalTitle()
804 return "Retrieved from " + database.getSelectedItem();
807 * constructs an alignment frame given the data and metadata
810 * @param currentFileFormat
811 * @param preferredFeatureColours
812 * @return the alignment
814 public AlignmentI parseResult(AlignmentI al, String title,
815 FileFormatI currentFileFormat,
816 FeatureSettingsModelI preferredFeatureColours)
819 if (al != null && al.getHeight() > 0)
823 title = getDefaultRetrievalTitle();
825 if (alignFrame == null)
827 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
828 AlignFrame.DEFAULT_HEIGHT);
829 if (currentFileFormat != null)
831 af.currentFileFormat = currentFileFormat;
834 List<SequenceI> alsqs = al.getSequences();
837 for (SequenceI sq : alsqs)
839 if (sq.getFeatures().hasFeatures())
841 af.setShowSeqFeatures(true);
847 af.getViewport().applyFeaturesStyle(preferredFeatureColours);
848 if (Cache.getDefault("HIDE_INTRONS", true))
850 af.hideFeatureColumns(SequenceOntologyI.EXON, false);
852 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
853 AlignFrame.DEFAULT_HEIGHT);
855 af.setStatus(MessageManager
856 .getString("label.successfully_pasted_alignment_file"));
860 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
861 } catch (Exception ex)
867 alignFrame.viewport.addAlignment(al, title);
873 void showErrorMessage(final String error)
876 javax.swing.SwingUtilities.invokeLater(new Runnable()
881 JvOptionPane.showInternalMessageDialog(Desktop.desktop, error,
882 MessageManager.getString("label.error_retrieving_data"),
883 JvOptionPane.WARNING_MESSAGE);
888 public IProgressIndicator getProgressIndicator()
890 return progressIndicator;
893 public void setProgressIndicator(IProgressIndicator progressIndicator)
895 this.progressIndicator = progressIndicator;
899 * Hide this panel (on clicking the database button to open the database
904 frame.setVisible(false);
907 public void setQuery(String ids)
909 textArea.setText(ids);
913 * Called to modify the search panel for embedding as an alternative tab of a
914 * free text search panel. The database choice list is hidden (since the
915 * choice has been made), and a Back button is made visible (which reopens the
916 * Sequence Fetcher panel).
920 public void embedIn(GFTSPanel parentPanel)
922 database.setVisible(false);
923 backBtn.setVisible(true);
924 parentSearchPanel = parentPanel;