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.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.fts.service.pdb.PDBFTSPanel;
30 import jalview.io.gff.SequenceOntologyI;
31 import jalview.util.DBRefUtils;
32 import jalview.util.MessageManager;
33 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
34 import jalview.ws.seqfetcher.DbSourceProxy;
36 import java.awt.BorderLayout;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.KeyAdapter;
41 import java.awt.event.KeyEvent;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Iterator;
45 import java.util.List;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JInternalFrame;
50 import javax.swing.JLabel;
51 import javax.swing.JOptionPane;
52 import javax.swing.JPanel;
53 import javax.swing.JScrollPane;
54 import javax.swing.JTextArea;
55 import javax.swing.SwingConstants;
56 import javax.swing.tree.DefaultMutableTreeNode;
58 public class SequenceFetcher extends JPanel implements Runnable
60 JLabel dbeg = new JLabel();
62 JDatabaseTree database;
66 JLabel jLabel1 = new JLabel();
68 JCheckBox replacePunctuation = new JCheckBox();
70 JButton ok = new JButton();
72 JButton clear = new JButton();
74 JButton example = new JButton();
76 JButton close = new JButton();
78 JPanel jPanel1 = new JPanel();
80 JTextArea textArea = new JTextArea();
82 JScrollPane jScrollPane1 = new JScrollPane();
84 JPanel jPanel2 = new JPanel();
86 JPanel jPanel3 = new JPanel();
88 JPanel jPanel4 = new JPanel();
90 BorderLayout borderLayout1 = new BorderLayout();
92 BorderLayout borderLayout2 = new BorderLayout();
94 BorderLayout borderLayout3 = new BorderLayout();
98 IProgressIndicator guiWindow;
100 AlignFrame alignFrame;
104 final String noDbSelected = "-- Select Database --";
106 private static jalview.ws.SequenceFetcher sfetch = null;
108 private static long lastDasSourceRegistry = -3;
110 private static DasSourceRegistryI dasRegistry = null;
112 private static boolean _initingFetcher = false;
114 private static Thread initingThread = null;
116 int debounceTrap = 0;
118 public JTextArea getTextArea()
124 * Blocking method that initialises and returns the shared instance of the
125 * SequenceFetcher client
128 * - where the initialisation delay message should be shown
129 * @return the singleton instance of the sequence fetcher client
131 public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
132 final IProgressIndicator guiWindow)
134 if (_initingFetcher && initingThread != null && initingThread.isAlive())
136 if (guiWindow != null)
141 .getString("status.waiting_sequence_database_fetchers_init"),
142 Thread.currentThread().hashCode());
144 // initting happening on another thread - so wait around to see if it
146 while (_initingFetcher && initingThread != null
147 && initingThread.isAlive())
152 } catch (Exception e)
157 if (guiWindow != null)
162 .getString("status.waiting_sequence_database_fetchers_init"),
163 Thread.currentThread().hashCode());
167 || dasRegistry != Cache.getDasSourceRegistry()
168 || lastDasSourceRegistry != (Cache.getDasSourceRegistry()
169 .getDasRegistryURL() + Cache
170 .getDasSourceRegistry().getLocalSourceString())
173 _initingFetcher = true;
174 initingThread = Thread.currentThread();
176 * give a visual indication that sequence fetcher construction is occuring
178 if (guiWindow != null)
180 guiWindow.setProgressBar(MessageManager
181 .getString("status.init_sequence_database_fetchers"),
182 Thread.currentThread().hashCode());
184 dasRegistry = Cache.getDasSourceRegistry();
185 dasRegistry.refreshSources();
187 jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
188 if (guiWindow != null)
190 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
192 lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
193 .getLocalSourceString()).hashCode();
195 _initingFetcher = false;
196 initingThread = null;
201 private IProgressIndicator progressIndicator;
203 public SequenceFetcher(IProgressIndicator guiIndic)
205 this.progressIndicator = guiIndic;
206 final SequenceFetcher us = this;
207 // launch initialiser thread
208 Thread sf = new Thread(new Runnable()
214 if (getSequenceFetcherSingleton(progressIndicator) != null)
216 us.initGui(progressIndicator);
220 javax.swing.SwingUtilities.invokeLater(new Runnable()
226 .showInternalMessageDialog(
229 .getString("warn.couldnt_create_sequence_fetcher_client"),
231 .getString("label.couldnt_create_sequence_fetcher"),
232 JOptionPane.ERROR_MESSAGE);
236 // raise warning dialog
243 private class DatabaseAuthority extends DefaultMutableTreeNode
248 private class DatabaseSource extends DefaultMutableTreeNode
254 * called by thread spawned by constructor
258 private void initGui(IProgressIndicator guiWindow)
260 this.guiWindow = guiWindow;
261 if (guiWindow instanceof AlignFrame)
263 alignFrame = (AlignFrame) guiWindow;
265 database = new JDatabaseTree(sfetch);
269 } catch (Exception ex)
271 ex.printStackTrace();
274 frame = new JInternalFrame();
275 frame.setContentPane(this);
276 if (new jalview.util.Platform().isAMac())
278 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
282 Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
286 private String getFrameTitle()
288 return ((alignFrame == null) ? MessageManager
289 .getString("label.new_sequence_fetcher") : MessageManager
290 .getString("label.additional_sequence_fetcher"));
293 private void jbInit() throws Exception
295 this.setLayout(borderLayout2);
297 database.setFont(JvSwingUtils.getLabelFont());
298 dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
299 jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
300 jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
301 jLabel1.setText(MessageManager
302 .getString("label.separate_multiple_accession_ids"));
304 replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
306 .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
307 replacePunctuation.setText(MessageManager
308 .getString("label.replace_commas_semicolons"));
309 ok.setText(MessageManager.getString("action.ok"));
310 ok.addActionListener(new ActionListener()
313 public void actionPerformed(ActionEvent e)
315 ok_actionPerformed();
318 clear.setText(MessageManager.getString("action.clear"));
319 clear.addActionListener(new ActionListener()
322 public void actionPerformed(ActionEvent e)
324 clear_actionPerformed();
328 example.setText(MessageManager.getString("label.example"));
329 example.addActionListener(new ActionListener()
332 public void actionPerformed(ActionEvent e)
334 example_actionPerformed();
337 close.setText(MessageManager.getString("action.close"));
338 close.addActionListener(new ActionListener()
341 public void actionPerformed(ActionEvent e)
343 close_actionPerformed(e);
346 textArea.setFont(JvSwingUtils.getLabelFont());
347 textArea.setLineWrap(true);
348 textArea.addKeyListener(new KeyAdapter()
351 public void keyPressed(KeyEvent e)
353 if (e.getKeyCode() == KeyEvent.VK_ENTER)
355 ok_actionPerformed();
359 jPanel3.setLayout(borderLayout1);
360 borderLayout1.setVgap(5);
362 jPanel1.add(example);
365 jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
366 jPanel2.setLayout(borderLayout3);
367 databaseButt = database.getDatabaseSelectorButton();
368 databaseButt.setFont(JvSwingUtils.getLabelFont());
369 database.addActionListener(new ActionListener()
372 public void actionPerformed(ActionEvent e)
375 String currentSelection = database.getSelectedItem();
376 if (!currentSelection.equalsIgnoreCase("pdb"))
380 if (currentSelection.equalsIgnoreCase("pdb")
381 && (database.action == KeyEvent.VK_ENTER || ((debounceTrap % 2) == 0)))
385 database.action = -1;
390 jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
391 jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
392 JPanel jPanel2a = new JPanel(new BorderLayout());
393 jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
394 jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
395 jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
396 // jPanel2.setPreferredSize(new Dimension())
397 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
398 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
399 this.add(jPanel3, java.awt.BorderLayout.CENTER);
400 this.add(jPanel2, java.awt.BorderLayout.NORTH);
401 jScrollPane1.getViewport().add(textArea);
405 private void pdbSourceAction()
407 databaseButt.setText(database.getSelectedItem());
408 new PDBFTSPanel(this);
412 private void otherSourceAction()
416 databaseButt.setText(database.getSelectedItem()
417 + (database.getSelectedSources().size() > 1 ? " (and "
418 + database.getSelectedSources().size() + " others)"
420 String eq = database.getExampleQueries();
421 dbeg.setText(MessageManager.formatMessage(
422 "label.example_query_param", new String[] { eq }));
423 boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
424 for (DbSourceProxy dbs : database.getSelectedSources())
426 if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
432 replacePunctuation.setEnabled(enablePunct);
434 } catch (Exception ex)
437 replacePunctuation.setEnabled(true);
442 protected void example_actionPerformed()
444 DbSourceProxy db = null;
447 textArea.setText(database.getExampleQueries());
448 } catch (Exception ex)
454 protected void clear_actionPerformed()
456 textArea.setText("");
460 public void close_actionPerformed(ActionEvent e)
464 frame.setClosed(true);
465 } catch (Exception ex)
470 public void ok_actionPerformed()
472 databaseButt.setEnabled(false);
473 example.setEnabled(false);
474 textArea.setEnabled(false);
475 ok.setEnabled(false);
476 close.setEnabled(false);
478 Thread worker = new Thread(this);
482 private void resetDialog()
484 databaseButt.setEnabled(true);
485 example.setEnabled(true);
486 textArea.setEnabled(true);
488 close.setEnabled(true);
495 if (!database.hasSelection())
497 error += "Please select the source database\n";
499 // TODO: make this transformation more configurable
500 com.stevesoft.pat.Regex empty;
501 if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
503 empty = new com.stevesoft.pat.Regex(
504 // replace commas and spaces with a semicolon
505 "(\\s|[,; ])+", ";");
509 // just turn spaces and semicolons into single semicolons
510 empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
512 textArea.setText(empty.replaceAll(textArea.getText()));
513 // see if there's anthing to search with
514 if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
517 error += "Please enter a (semi-colon separated list of) database id(s)";
519 if (error.length() > 0)
521 showErrorMessage(error);
525 // TODO: Refactor to GUI independent code and write tests.
526 // indicate if successive sources should be merged into one alignment.
527 boolean addToLast = false;
528 ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
529 ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
530 Iterator<DbSourceProxy> proxies = database.getSelectedSources()
533 List<String> nextfetch = Arrays.asList(qries = textArea.getText()
535 Iterator<String> en = Arrays.asList(new String[0]).iterator();
536 int nqueries = qries.length;
538 FeatureSettingsModelI preferredFeatureColours = null;
539 while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
541 if (!en.hasNext() && nextfetch.size() > 0)
543 en = nextfetch.iterator();
544 nqueries = nextfetch.size();
545 // save the remaining queries in the original array
546 qries = nextfetch.toArray(new String[nqueries]);
547 nextfetch = new ArrayList<String>();
550 DbSourceProxy proxy = proxies.next();
551 boolean isAliSource = false;
556 .setProgressBar(MessageManager.formatMessage(
557 "status.fetching_sequence_queries_from",
559 Integer.valueOf(nqueries).toString(),
560 proxy.getDbName() }), Thread.currentThread()
562 isAliSource = proxy.isAlignmentSource();
563 if (proxy.getMaximumQueryCount() == 1)
567 String item = en.next();
574 // give the server a chance to breathe
576 } catch (Exception e)
583 AlignmentI indres = null;
586 indres = proxy.getSequenceRecords(item);
587 } catch (OutOfMemoryError oome)
589 new OOMWarning("fetching " + item + " from "
590 + proxy.getDbName(), oome, this);
601 } catch (Exception e)
604 "Error retrieving " + item
605 + " from " + proxy.getDbName(), e);
612 StringBuffer multiacc = new StringBuffer();
613 ArrayList<String> tosend = new ArrayList<String>();
616 String nel = en.next();
618 multiacc.append(nel);
621 multiacc.append(proxy.getAccessionSeparator());
628 List<String> nores = new ArrayList<String>();
629 rslt = proxy.getSequenceRecords(multiacc.toString());
630 if (rslt == null || rslt.getHeight() == 0)
632 // no results - pass on all queries to next source
633 nextfetch.addAll(tosend);
637 aresultq.add(multiacc.toString());
640 rs = rslt.getSequencesArray();
641 // search for each query in the dbrefs associated with each
644 // ones we do not find will be used to query next source (if any)
645 for (String q : tosend)
647 DBRefEntry dbr = new DBRefEntry(), found[] = null;
648 dbr.setSource(proxy.getDbSource());
649 dbr.setVersion(null);
650 String accId = proxy.getAccessionIdFromQuery(q);
651 dbr.setAccessionId(accId);
652 boolean rfound = false;
653 for (int r = 0; r < rs.length; r++)
657 found = DBRefUtils.searchRefs(rs[r].getDBRefs(), accId);
658 if (found != null && found.length > 0)
671 } catch (OutOfMemoryError oome)
673 new OOMWarning("fetching " + multiacc + " from "
674 + database.getSelectedItem(), oome, this);
678 } catch (Exception e)
680 showErrorMessage("Error retrieving " + textArea.getText()
681 + " from " + database.getSelectedItem());
683 // +="Couldn't retrieve sequences from "+database.getSelectedItem();
684 System.err.println("Retrieval failed for source ='"
685 + database.getSelectedItem() + "' and query\n'"
686 + textArea.getText() + "'\n");
688 } catch (OutOfMemoryError e)
690 // resets dialog box - so we don't use OOMwarning here.
691 showErrorMessage("Out of Memory when retrieving "
694 + database.getSelectedItem()
695 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
699 showErrorMessage("Serious Error retrieving " + textArea.getText()
700 + " from " + database.getSelectedItem());
703 // Stack results ready for opening in alignment windows
704 if (aresult != null && aresult.size() > 0)
706 FeatureSettingsModelI proxyColourScheme = proxy
707 .getFeatureColourScheme();
708 if (proxyColourScheme != null)
710 preferredFeatureColours = proxyColourScheme;
713 AlignmentI ar = null;
717 // new window for each result
718 while (aresult.size() > 0)
720 presult.add(aresult.remove(0));
721 presultTitle.add(aresultq.remove(0) + " "
722 + getDefaultRetrievalTitle());
728 if (addToLast && presult.size() > 0)
730 ar = presult.remove(presult.size() - 1);
731 titl = presultTitle.remove(presultTitle.size() - 1);
733 // concatenate all results in one window
734 while (aresult.size() > 0)
738 ar = aresult.remove(0);
742 ar.append(aresult.remove(0));
748 presultTitle.add(titl);
751 guiWindow.setProgressBar(MessageManager
752 .getString("status.finshed_querying"), Thread.currentThread()
755 guiWindow.setProgressBar(
756 (presult.size() > 0) ? MessageManager
757 .getString("status.parsing_results") : MessageManager
758 .getString("status.processing"), Thread.currentThread()
761 while (presult.size() > 0)
763 parseResult(presult.remove(0), presultTitle.remove(0), null,
764 preferredFeatureColours);
766 // only remove visual delay after we finished parsing.
767 guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
768 if (nextfetch.size() > 0)
770 StringBuffer sb = new StringBuffer();
771 sb.append("Didn't retrieve the following "
772 + (nextfetch.size() == 1 ? "query" : nextfetch.size()
773 + " queries") + ": \n");
774 int l = sb.length(), lr = 0;
775 for (String s : nextfetch)
777 if (l != sb.length())
781 if (lr - sb.length() > 40)
787 showErrorMessage(sb.toString());
794 * @return a standard title for any results retrieved using the currently
795 * selected source and settings
797 public String getDefaultRetrievalTitle()
799 return "Retrieved from " + database.getSelectedItem();
802 AlignmentI parseResult(AlignmentI al, String title,
803 String currentFileFormat,
804 FeatureSettingsModelI preferredFeatureColours)
807 if (al != null && al.getHeight() > 0)
811 title = getDefaultRetrievalTitle();
813 if (alignFrame == null)
815 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
816 AlignFrame.DEFAULT_HEIGHT);
817 if (currentFileFormat != null)
819 af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
821 // NON-FormatAdapter Sourced
825 SequenceFeature[] sfs = null;
826 List<SequenceI> alsqs;
827 synchronized (alsqs = al.getSequences())
829 for (SequenceI sq : alsqs)
831 if ((sfs = sq.getSequenceFeatures()) != null)
835 af.setShowSeqFeatures(true);
843 if (preferredFeatureColours != null)
845 af.getViewport().applyFeaturesStyle(preferredFeatureColours);
847 if (Cache.getDefault("HIDE_INTRONS", true))
849 hideIntronsIfPresent(af);
852 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
853 AlignFrame.DEFAULT_HEIGHT);
855 af.statusBar.setText(MessageManager
856 .getString("label.successfully_pasted_alignment_file"));
860 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN",
862 } catch (Exception ex)
868 alignFrame.viewport.addAlignment(al, title);
875 * Hide columns not containing 'exon' features, provided there are exon
876 * features on the alignment
880 public void hideIntronsIfPresent(AlignFrame af)
882 boolean hasExons = af.avc.markColumnsContainingFeatures(false, false,
884 SequenceOntologyI.EXON);
887 af.avc.markColumnsContainingFeatures(true, false, true,
888 SequenceOntologyI.EXON);
889 af.getViewport().hideSelectedColumns();
893 void showErrorMessage(final String error)
896 javax.swing.SwingUtilities.invokeLater(new Runnable()
901 JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
902 MessageManager.getString("label.error_retrieving_data"),
903 JOptionPane.WARNING_MESSAGE);
908 public IProgressIndicator getProgressIndicator()
910 return progressIndicator;
913 public void setProgressIndicator(IProgressIndicator progressIndicator)
915 this.progressIndicator = progressIndicator;