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 static jalview.ws.SequenceFetcher sfetch = null;
71 JLabel exampleAccession;
73 JComboBox<String> database;
75 JCheckBox replacePunctuation;
89 IProgressIndicator guiWindow;
91 AlignFrame alignFrame;
93 GFTSPanel parentSearchPanel;
95 IProgressIndicator progressIndicator;
97 volatile boolean _isConstructing = false;
100 * Returns the shared instance of the SequenceFetcher client
104 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton()
108 sfetch = new jalview.ws.SequenceFetcher();
114 * Constructor given a client to receive any status or progress messages
115 * (currently either the Desktop, or an AlignFrame panel)
119 public SequenceFetcher(IProgressIndicator guiIndic)
121 this(guiIndic, null, null);
125 * Constructor with specified database and accession(s) to retrieve
131 public SequenceFetcher(IProgressIndicator guiIndic,
132 final String selectedDb, final String queryString)
134 this.progressIndicator = guiIndic;
135 getSequenceFetcherSingleton();
136 this.guiWindow = progressIndicator;
138 if (progressIndicator instanceof AlignFrame)
140 alignFrame = (AlignFrame) progressIndicator;
144 textArea.setText(queryString);
146 frame = new JInternalFrame();
147 frame.setContentPane(this);
148 Desktop.addInternalFrame(frame, getFrameTitle(), true, 400,
149 Platform.isAMacAndNotJS() ? 240 : 180);
152 private String getFrameTitle()
154 return ((alignFrame == null)
155 ? MessageManager.getString("label.new_sequence_fetcher")
157 .getString("label.additional_sequence_fetcher"));
160 private void jbInit(String selectedDb)
162 this.setLayout(new BorderLayout());
164 database = new JComboBox<>();
165 database.setFont(JvSwingUtils.getLabelFont());
166 database.setPrototypeDisplayValue("ENSEMBLGENOMES ");
167 String[] sources = new jalview.ws.SequenceFetcher().getSupportedDb();
168 Arrays.sort(sources, String.CASE_INSENSITIVE_ORDER);
169 database.addItem(MessageManager.getString("action.select_ddbb"));
170 for (String source : sources)
172 database.addItem(source);
174 database.setSelectedItem(selectedDb);
175 if (database.getSelectedIndex() == -1)
177 database.setSelectedIndex(0);
179 database.setMaximumRowCount(database.getItemCount());
180 database.addActionListener(new ActionListener()
183 public void actionPerformed(ActionEvent e)
185 String currentSelection = (String) database.getSelectedItem();
186 updateExampleQuery(currentSelection);
188 if ("pdb".equalsIgnoreCase(currentSelection))
191 new PDBFTSPanel(SequenceFetcher.this);
193 else if ("uniprot".equalsIgnoreCase(currentSelection))
196 new UniprotFTSPanel(SequenceFetcher.this);
205 exampleAccession = new JLabel("");
206 exampleAccession.setFont(new Font("Verdana", Font.BOLD, 11));
207 JLabel jLabel1 = new JLabel(MessageManager
208 .getString("label.separate_multiple_accession_ids"));
209 jLabel1.setFont(new Font("Verdana", Font.ITALIC, 11));
210 jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
212 replacePunctuation = new JCheckBox(
213 MessageManager.getString("label.replace_commas_semicolons"));
214 replacePunctuation.setHorizontalAlignment(SwingConstants.LEFT);
215 replacePunctuation.setFont(new Font("Verdana", Font.ITALIC, 11));
216 okBtn = new JButton(MessageManager.getString("action.ok"));
217 okBtn.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 ok_actionPerformed();
225 JButton clear = new JButton(MessageManager.getString("action.clear"));
226 clear.addActionListener(new ActionListener()
229 public void actionPerformed(ActionEvent e)
231 clear_actionPerformed();
235 exampleBtn = new JButton(MessageManager.getString("label.example"));
236 exampleBtn.addActionListener(new ActionListener()
239 public void actionPerformed(ActionEvent e)
241 example_actionPerformed();
244 closeBtn = new JButton(MessageManager.getString("action.cancel"));
245 closeBtn.addActionListener(new ActionListener()
248 public void actionPerformed(ActionEvent e)
250 close_actionPerformed(e);
253 backBtn = new JButton(MessageManager.getString("action.back"));
254 backBtn.addActionListener(new ActionListener()
257 public void actionPerformed(ActionEvent e)
259 parentSearchPanel.btn_back_ActionPerformed();
262 // back not visible unless embedded
263 backBtn.setVisible(false);
265 textArea = new JTextArea();
266 textArea.setFont(JvSwingUtils.getLabelFont());
267 textArea.setLineWrap(true);
268 textArea.addKeyListener(new KeyAdapter()
271 public void keyPressed(KeyEvent e)
273 if (e.getKeyCode() == KeyEvent.VK_ENTER)
275 ok_actionPerformed();
280 JPanel actionPanel = new JPanel();
281 actionPanel.add(backBtn);
282 actionPanel.add(exampleBtn);
283 actionPanel.add(clear);
284 actionPanel.add(okBtn);
285 actionPanel.add(closeBtn);
287 JPanel databasePanel = new JPanel();
288 databasePanel.setLayout(new BorderLayout());
289 databasePanel.add(database, BorderLayout.NORTH);
290 databasePanel.add(exampleAccession, BorderLayout.CENTER);
291 JPanel jPanel2a = new JPanel(new BorderLayout());
292 jPanel2a.add(jLabel1, BorderLayout.NORTH);
293 jPanel2a.add(replacePunctuation, BorderLayout.SOUTH);
294 databasePanel.add(jPanel2a, BorderLayout.SOUTH);
296 JPanel idsPanel = new JPanel();
297 idsPanel.setLayout(new BorderLayout(0, 5));
298 JScrollPane jScrollPane1 = new JScrollPane();
299 jScrollPane1.getViewport().add(textArea);
300 idsPanel.add(jScrollPane1, BorderLayout.CENTER);
302 this.add(actionPanel, BorderLayout.SOUTH);
303 this.add(idsPanel, BorderLayout.CENTER);
304 this.add(databasePanel, BorderLayout.NORTH);
308 * Answers a semi-colon-delimited string with the example query or queries for
309 * the selected database
314 protected String getExampleQueries(String db)
316 StringBuilder sb = new StringBuilder();
317 HashSet<String> hs = new HashSet<>();
318 for (DbSourceProxy dbs : sfetch.getSourceProxy(db))
320 String tq = dbs.getTestQuery();
321 if (hs.add(tq)) // not a duplicate source
330 return sb.toString();
334 * Action on selecting a database other than Uniprot or PDB is to enable or
335 * disable 'Replace commas', and await input in the query field
337 protected void otherSourceAction()
341 String eq = exampleAccession.getText();
342 // TODO this should be a property of the SequenceFetcher whether commas
343 // are allowed in the IDs...
345 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
346 replacePunctuation.setEnabled(enablePunct);
348 } catch (Exception ex)
350 exampleAccession.setText("");
351 replacePunctuation.setEnabled(true);
357 * Sets the text of the example query to incorporate the example accession
358 * provided by the selected database source
360 * @param selectedDatabase
363 protected String updateExampleQuery(String selectedDatabase)
365 String eq = getExampleQueries(selectedDatabase);
366 exampleAccession.setText(MessageManager
367 .formatMessage("label.example_query_param", new String[]
373 * Action on clicking the 'Example' button is to write the example accession
374 * as the query text field value
376 protected void example_actionPerformed()
378 String eq = getExampleQueries((String) database.getSelectedItem());
379 textArea.setText(eq);
384 * Clears the query input field
386 protected void clear_actionPerformed()
388 textArea.setText("");
393 * Action on Close button is to close this frame, and also (if it is embedded
394 * in a search panel) to close the search panel
398 protected void close_actionPerformed(ActionEvent e)
402 frame.setClosed(true);
403 if (parentSearchPanel != null)
405 parentSearchPanel.btn_cancel_ActionPerformed();
407 } catch (Exception ex)
413 * Action on OK is to start the fetch for entered accession(s)
415 public void ok_actionPerformed()
418 * tidy inputs and check there is something to search for
420 String t0 = textArea.getText();
421 String text = text.trim(); // BH 2019.01.30
422 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
424 text = text.replace(",", ";");
426 text = text.replaceAll("(\\s|[,; ])+", ";");
427 if (!t0.equals(text)) // BH 2019.01.30 no need to do setting if it is the same
428 textArea.setText(text);
433 "Please enter a (semi-colon separated list of) database id(s)");
437 exampleBtn.setEnabled(false);
438 textArea.setEnabled(false);
439 okBtn.setEnabled(false);
440 closeBtn.setEnabled(false);
441 backBtn.setEnabled(false);
443 Thread worker = new Thread(this);
447 private void resetDialog()
449 exampleBtn.setEnabled(true);
450 textArea.setEnabled(true);
451 okBtn.setEnabled(true);
452 closeBtn.setEnabled(true);
453 backBtn.setEnabled(parentSearchPanel != null);
459 boolean addToLast = false;
460 List<String> aresultq = new ArrayList<>();
461 List<String> presultTitle = new ArrayList<>();
462 List<AlignmentI> presult = new ArrayList<>();
463 List<AlignmentI> aresult = new ArrayList<>();
464 List<DbSourceProxy> sources = sfetch
465 .getSourceProxy((String) database.getSelectedItem());
466 Iterator<DbSourceProxy> proxies = sources.iterator();
468 List<String> nextFetch = Arrays
469 .asList(qries = textArea.getText().split(";"));
470 Iterator<String> en = Arrays.asList(new String[0]).iterator();
471 int nqueries = qries.length;
473 FeatureSettingsModelI preferredFeatureColours = null;
474 while (proxies.hasNext() && (en.hasNext() || nextFetch.size() > 0))
476 if (!en.hasNext() && nextFetch.size() > 0)
478 en = nextFetch.iterator();
479 nqueries = nextFetch.size();
480 // save the remaining queries in the original array
481 qries = nextFetch.toArray(new String[nqueries]);
482 nextFetch = new ArrayList<>();
485 DbSourceProxy proxy = proxies.next();
489 guiWindow.setProgressBar(MessageManager.formatMessage(
490 "status.fetching_sequence_queries_from", new String[]
491 { Integer.valueOf(nqueries).toString(),
492 proxy.getDbName() }),
493 Thread.currentThread().hashCode());
494 if (proxy.getMaximumQueryCount() == 1)
497 * proxy only handles one accession id at a time
501 String acc = en.next();
502 if (!fetchSingleAccession(proxy, acc, aresultq, aresult))
511 * proxy can fetch multiple accessions at one time
513 fetchMultipleAccessions(proxy, en, aresultq, aresult, nextFetch);
515 } catch (Exception e)
517 showErrorMessage("Error retrieving " + textArea.getText() + " from "
518 + database.getSelectedItem());
520 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
521 System.err.println("Retrieval failed for source ='"
522 + database.getSelectedItem() + "' and query\n'"
523 + textArea.getText() + "'\n");
525 } catch (OutOfMemoryError e)
527 showErrorMessage("Out of Memory when retrieving "
528 + textArea.getText() + " from " + database.getSelectedItem()
529 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
533 showErrorMessage("Serious Error retrieving " + textArea.getText()
534 + " from " + database.getSelectedItem());
538 // Stack results ready for opening in alignment windows
539 if (aresult != null && aresult.size() > 0)
541 FeatureSettingsModelI proxyColourScheme = proxy
542 .getFeatureColourScheme();
543 if (proxyColourScheme != null)
545 preferredFeatureColours = proxyColourScheme;
548 AlignmentI ar = null;
549 if (proxy.isAlignmentSource())
552 // new window for each result
553 while (aresult.size() > 0)
555 presult.add(aresult.remove(0));
557 aresultq.remove(0) + " " + getDefaultRetrievalTitle());
563 if (addToLast && presult.size() > 0)
565 ar = presult.remove(presult.size() - 1);
566 titl = presultTitle.remove(presultTitle.size() - 1);
568 // concatenate all results in one window
569 while (aresult.size() > 0)
573 ar = aresult.remove(0);
577 ar.append(aresult.remove(0));
582 presultTitle.add(titl);
585 guiWindow.setProgressBar(
586 MessageManager.getString("status.finshed_querying"),
587 Thread.currentThread().hashCode());
593 .getString("status.parsing_results")
594 : MessageManager.getString("status.processing"),
595 Thread.currentThread().hashCode());
597 while (presult.size() > 0)
599 parseResult(presult.remove(0), presultTitle.remove(0), null,
600 preferredFeatureColours);
602 // only remove visual delay after we finished parsing.
603 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
604 if (nextFetch.size() > 0)
606 StringBuffer sb = new StringBuffer();
607 sb.append("Didn't retrieve the following "
608 + (nextFetch.size() == 1 ? "query"
609 : nextFetch.size() + " queries")
611 int l = sb.length(), lr = 0;
612 for (String s : nextFetch)
614 if (l != sb.length())
618 if (lr - sb.length() > 40)
624 showErrorMessage(sb.toString());
630 * Tries to fetch one or more accession ids from the database proxy
634 * the queries to fetch
636 * a successful queries list to add to
638 * a list of retrieved alignments to add to
640 * failed queries are added to this list
643 void fetchMultipleAccessions(DbSourceProxy proxy,
644 Iterator<String> accessions, List<String> aresultq,
645 List<AlignmentI> aresult, List<String> nextFetch) throws Exception
647 StringBuilder multiacc = new StringBuilder();
648 List<String> tosend = new ArrayList<>();
649 while (accessions.hasNext())
651 String nel = accessions.next();
653 multiacc.append(nel);
654 if (accessions.hasNext())
656 multiacc.append(proxy.getAccessionSeparator());
662 String query = multiacc.toString();
663 AlignmentI rslt = proxy.getSequenceRecords(query);
664 if (rslt == null || rslt.getHeight() == 0)
666 // no results - pass on all queries to next source
667 nextFetch.addAll(tosend);
673 if (tosend.size() > 1)
675 checkResultForQueries(rslt, tosend, nextFetch, proxy);
678 } catch (OutOfMemoryError oome)
680 new OOMWarning("fetching " + multiacc + " from "
681 + database.getSelectedItem(), oome, this);
686 * Query for a single accession id via the database proxy
691 * a list of successful queries to add to
693 * a list of retrieved alignments to add to
694 * @return true if the fetch was successful, else false
696 boolean fetchSingleAccession(DbSourceProxy proxy, String accession,
697 List<String> aresultq, List<AlignmentI> aresult)
699 boolean success = false;
706 // give the server a chance to breathe
708 } catch (Exception e)
714 AlignmentI indres = null;
717 indres = proxy.getSequenceRecords(accession);
718 } catch (OutOfMemoryError oome)
721 "fetching " + accession + " from " + proxy.getDbName(),
726 aresultq.add(accession);
730 } catch (Exception e)
732 Cache.log.info("Error retrieving " + accession + " from "
733 + proxy.getDbName(), e);
739 * Checks which of the queries were successfully retrieved by searching the
740 * DBRefs of the retrieved sequences for a match. Any not found are added to
741 * the 'nextFetch' list.
748 void checkResultForQueries(AlignmentI rslt, List<String> queries,
749 List<String> nextFetch, DbSourceProxy proxy)
751 SequenceI[] rs = rslt.getSequencesArray();
753 for (String q : queries)
755 // BH 2019.01.25 dbr is never used.
756 // DBRefEntry dbr = new DBRefEntry();
757 // dbr.setSource(proxy.getDbSource());
758 // dbr.setVersion(null);
759 String accId = proxy.getAccessionIdFromQuery(q);
760 // dbr.setAccessionId(accId);
761 boolean rfound = false;
762 for (int r = 0, nr = rs.length; r < nr; r++)
766 List<DBRefEntry> found = DBRefUtils.searchRefs(rs[r].getDBRefs(),
768 if (!found.isEmpty())
784 * @return a standard title for any results retrieved using the currently
785 * selected source and settings
787 public String getDefaultRetrievalTitle()
789 return "Retrieved from " + database.getSelectedItem();
792 AlignmentI parseResult(AlignmentI al, String title,
793 FileFormatI currentFileFormat,
794 FeatureSettingsModelI preferredFeatureColours)
797 if (al != null && al.getHeight() > 0)
801 title = getDefaultRetrievalTitle();
803 if (alignFrame == null)
805 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
806 AlignFrame.DEFAULT_HEIGHT);
807 if (currentFileFormat != null)
809 af.currentFileFormat = currentFileFormat;
812 List<SequenceI> alsqs = al.getSequences();
815 for (SequenceI sq : alsqs)
817 if (sq.getFeatures().hasFeatures())
819 af.setShowSeqFeatures(true);
825 if (preferredFeatureColours != null)
827 af.getViewport().applyFeaturesStyle(preferredFeatureColours);
829 if (Cache.getDefault("HIDE_INTRONS", true))
831 af.hideFeatureColumns(SequenceOntologyI.EXON, false);
833 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
834 AlignFrame.DEFAULT_HEIGHT);
836 af.setStatus(MessageManager
837 .getString("label.successfully_pasted_alignment_file"));
841 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
842 } catch (Exception ex)
848 alignFrame.viewport.addAlignment(al, title);
854 void showErrorMessage(final String error)
857 javax.swing.SwingUtilities.invokeLater(new Runnable()
862 JvOptionPane.showInternalMessageDialog(Desktop.desktop, error,
863 MessageManager.getString("label.error_retrieving_data"),
864 JvOptionPane.WARNING_MESSAGE);
869 public IProgressIndicator getProgressIndicator()
871 return progressIndicator;
874 public void setProgressIndicator(IProgressIndicator progressIndicator)
876 this.progressIndicator = progressIndicator;
880 * Hide this panel (on clicking the database button to open the database
885 frame.setVisible(false);
888 public void setQuery(String ids)
890 textArea.setText(ids);
894 * Called to modify the search panel for embedding as an alternative tab of a
895 * free text search panel. The database choice list is hidden (since the
896 * choice has been made), and a Back button is made visible (which reopens the
897 * Sequence Fetcher panel).
901 public void embedIn(GFTSPanel parentPanel)
903 database.setVisible(false);
904 backBtn.setVisible(true);
905 parentSearchPanel = parentPanel;