111db90f18f0bd3a9eb50cd220654a063d4eef6f
[jalview.git] / src / jalview / gui / SequenceFetcher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.util.*;
21 import java.util.List;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import javax.swing.*;
27 import javax.swing.tree.DefaultMutableTreeNode;
28 import javax.swing.tree.MutableTreeNode;
29 import javax.swing.tree.TreeModel;
30
31 import jalview.datamodel.*;
32 import jalview.io.*;
33 import jalview.util.DBRefUtils;
34 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
35 import jalview.ws.seqfetcher.DbSourceProxy;
36
37 import java.awt.BorderLayout;
38
39 public class SequenceFetcher extends JPanel implements Runnable
40 {
41   // ASequenceFetcher sfetch;
42   JInternalFrame frame;
43
44   IProgressIndicator guiWindow;
45
46   AlignFrame alignFrame;
47
48   StringBuffer result;
49
50   final String noDbSelected = "-- Select Database --";
51
52   private static jalview.ws.SequenceFetcher sfetch = null;
53
54   private static long lastDasSourceRegistry = -3;
55
56   private static DasSourceRegistryI dasRegistry = null;
57
58   private static boolean _initingFetcher = false;
59
60   private static Thread initingThread = null;
61
62   /**
63    * Blocking method that initialises and returns the shared instance of the
64    * SequenceFetcher client
65    * 
66    * @param guiWindow
67    *          - where the initialisation delay message should be shown
68    * @return the singleton instance of the sequence fetcher client
69    */
70   public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(
71           final IProgressIndicator guiWindow)
72   {
73     if (_initingFetcher && initingThread != null && initingThread.isAlive())
74     {
75       if (guiWindow != null)
76       {
77         guiWindow.setProgressBar(
78                 "Waiting for Sequence Database Fetchers to initialise",
79                 Thread.currentThread().hashCode());
80       }
81       // initting happening on another thread - so wait around to see if it
82       // finishes.
83       while (_initingFetcher && initingThread != null
84               && initingThread.isAlive())
85       {
86         try
87         {
88           Thread.sleep(10);
89         } catch (Exception e)
90         {
91         }
92         ;
93       }
94       if (guiWindow != null)
95       {
96         guiWindow.setProgressBar(
97                 "Waiting for Sequence Database Fetchers to initialise",
98                 Thread.currentThread().hashCode());
99       }
100     }
101     if (sfetch == null
102             || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
103             || lastDasSourceRegistry != (jalview.bin.Cache
104                     .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
105                     .getDasSourceRegistry().getLocalSourceString())
106                     .hashCode())
107     {
108       _initingFetcher = true;
109       initingThread = Thread.currentThread();
110       /**
111        * give a visual indication that sequence fetcher construction is occuring
112        */
113       if (guiWindow != null)
114       {
115         guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
116                 Thread.currentThread().hashCode());
117       }
118       dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
119       dasRegistry.refreshSources();
120
121       jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
122       if (guiWindow != null)
123       {
124         guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
125                 Thread.currentThread().hashCode());
126       }
127       lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
128               .getLocalSourceString()).hashCode();
129       sfetch = sf;
130       _initingFetcher = false;
131       initingThread = null;
132     }
133     return sfetch;
134   }
135
136   public SequenceFetcher(IProgressIndicator guiIndic)
137   {
138     final IProgressIndicator guiWindow = guiIndic;
139     final SequenceFetcher us = this;
140     // launch initialiser thread
141     Thread sf = new Thread(new Runnable()
142     {
143
144       public void run()
145       {
146         if (getSequenceFetcherSingleton(guiWindow) != null)
147         {
148           us.initGui(guiWindow);
149         }
150         else
151         {
152           javax.swing.SwingUtilities.invokeLater(new Runnable()
153           {
154             public void run()
155             {
156               JOptionPane
157                       .showInternalMessageDialog(
158                               Desktop.desktop,
159                               "Could not create the sequence fetcher client. Check error logs for details.",
160                               "Couldn't create SequenceFetcher",
161                               JOptionPane.ERROR_MESSAGE);
162             }
163           });
164
165           // raise warning dialog
166         }
167       }
168     });
169     sf.start();
170   }
171
172   private class DatabaseAuthority extends DefaultMutableTreeNode
173   {
174
175   };
176
177   private class DatabaseSource extends DefaultMutableTreeNode
178   {
179
180   };
181
182   /**
183    * called by thread spawned by constructor
184    * 
185    * @param guiWindow
186    */
187   private void initGui(IProgressIndicator guiWindow)
188   {
189     this.guiWindow = guiWindow;
190     if (guiWindow instanceof AlignFrame)
191     {
192       alignFrame = (AlignFrame) guiWindow;
193     }
194     database = new JDatabaseTree(sfetch);
195     try
196     {
197       jbInit();
198     } catch (Exception ex)
199     {
200       ex.printStackTrace();
201     }
202
203     frame = new JInternalFrame();
204     frame.setContentPane(this);
205     if (new jalview.util.Platform().isAMac())
206     {
207       Desktop.addInternalFrame(frame, getFrameTitle(), 400, 240);
208     }
209     else
210     {
211       Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
212     }
213   }
214
215   private String getFrameTitle()
216   {
217     return ((alignFrame == null) ? "New " : "Additional ")
218             + "Sequence Fetcher";
219   }
220
221   private void jbInit() throws Exception
222   {
223     this.setLayout(borderLayout2);
224
225     database.setFont(JvSwingUtils.getLabelFont());
226     dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
227     jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
228     jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
229     jLabel1.setText("Separate multiple accession ids with semi colon \";\"");
230
231     replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
232     replacePunctuation
233             .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
234     replacePunctuation.setText("Replace commas with semi-colons");
235     ok.setText("OK");
236     ok.addActionListener(new ActionListener()
237     {
238       public void actionPerformed(ActionEvent e)
239       {
240         ok_actionPerformed();
241       }
242     });
243     clear.setText("Clear");
244     clear.addActionListener(new ActionListener()
245     {
246       public void actionPerformed(ActionEvent e)
247       {
248         clear_actionPerformed();
249       }
250     });
251
252     example.setText("Example");
253     example.addActionListener(new ActionListener()
254     {
255       public void actionPerformed(ActionEvent e)
256       {
257         example_actionPerformed();
258       }
259     });
260     close.setText("Close");
261     close.addActionListener(new ActionListener()
262     {
263       public void actionPerformed(ActionEvent e)
264       {
265         close_actionPerformed(e);
266       }
267     });
268     textArea.setFont(JvSwingUtils.getLabelFont());
269     textArea.setLineWrap(true);
270     textArea.addKeyListener(new KeyAdapter()
271     {
272       public void keyPressed(KeyEvent e)
273       {
274         if (e.getKeyCode() == KeyEvent.VK_ENTER)
275           ok_actionPerformed();
276       }
277     });
278     jPanel3.setLayout(borderLayout1);
279     borderLayout1.setVgap(5);
280     jPanel1.add(ok);
281     jPanel1.add(example);
282     jPanel1.add(clear);
283     jPanel1.add(close);
284     jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
285     jPanel2.setLayout(borderLayout3);
286     databaseButt = database.getDatabaseSelectorButton();
287     databaseButt.setFont(JvSwingUtils.getLabelFont());
288     database.addActionListener(new ActionListener()
289     {
290
291       public void actionPerformed(ActionEvent e)
292       {
293         DbSourceProxy db = null;
294         try
295         {
296           databaseButt.setText(database.getSelectedItem()
297                   + (database.getSelectedSources().size() > 1 ? " (and "
298                           + database.getSelectedSources().size()
299                           + " others)" : ""));
300           String eq = database.getExampleQueries();
301           dbeg.setText("Example query: " + eq);
302           replacePunctuation.setEnabled(!(eq != null && eq.indexOf(",") > -1));
303         } catch (Exception ex)
304         {
305           dbeg.setText("");
306           replacePunctuation.setEnabled(true);
307         }
308         jPanel2.repaint();
309       }
310     });
311     dbeg.setText("");
312     jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
313     jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
314     JPanel jPanel2a = new JPanel(new BorderLayout());
315     jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
316     jPanel2a.add(replacePunctuation, java.awt.BorderLayout.SOUTH);
317     jPanel2.add(jPanel2a, java.awt.BorderLayout.SOUTH);
318     // jPanel2.setPreferredSize(new Dimension())
319     jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);
320     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
321     this.add(jPanel3, java.awt.BorderLayout.CENTER);
322     this.add(jPanel2, java.awt.BorderLayout.NORTH);
323     jScrollPane1.getViewport().add(textArea);
324
325   }
326
327   protected void example_actionPerformed()
328   {
329     DbSourceProxy db = null;
330     try
331     {
332       textArea.setText(database.getExampleQueries());
333     } catch (Exception ex)
334     {
335     }
336     jPanel3.repaint();
337   }
338
339   protected void clear_actionPerformed()
340   {
341     textArea.setText("");
342     jPanel3.repaint();
343   }
344
345   JLabel dbeg = new JLabel();
346
347   JDatabaseTree database;
348
349   JButton databaseButt;
350
351   JLabel jLabel1 = new JLabel();
352
353   JCheckBox replacePunctuation = new JCheckBox();
354
355   JButton ok = new JButton();
356
357   JButton clear = new JButton();
358
359   JButton example = new JButton();
360
361   JButton close = new JButton();
362
363   JPanel jPanel1 = new JPanel();
364
365   JTextArea textArea = new JTextArea();
366
367   JScrollPane jScrollPane1 = new JScrollPane();
368
369   JPanel jPanel2 = new JPanel();
370
371   JPanel jPanel3 = new JPanel();
372
373   JPanel jPanel4 = new JPanel();
374
375   BorderLayout borderLayout1 = new BorderLayout();
376
377   BorderLayout borderLayout2 = new BorderLayout();
378
379   BorderLayout borderLayout3 = new BorderLayout();
380
381   public void close_actionPerformed(ActionEvent e)
382   {
383     try
384     {
385       frame.setClosed(true);
386     } catch (Exception ex)
387     {
388     }
389   }
390
391   public void ok_actionPerformed()
392   {
393     database.setEnabled(false);
394     textArea.setEnabled(false);
395     ok.setEnabled(false);
396     close.setEnabled(false);
397
398     Thread worker = new Thread(this);
399     worker.start();
400   }
401
402   private void resetDialog()
403   {
404     database.setEnabled(true);
405     textArea.setEnabled(true);
406     ok.setEnabled(true);
407     close.setEnabled(true);
408   }
409
410   public void run()
411   {
412     String error = "";
413     if (!database.hasSelection())
414     {
415       error += "Please select the source database\n";
416     }
417     // TODO: make this transformation more configurable
418     com.stevesoft.pat.Regex empty;
419     if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
420     {
421       empty = new com.stevesoft.pat.Regex(
422       // replace commas and spaces with a semicolon
423               "(\\s|[,; ])+", ";");
424     }
425     else
426     {
427       // just turn spaces and semicolons into single semicolons
428       empty = new com.stevesoft.pat.Regex("(\\s|[; ])+", ";");
429     }
430     textArea.setText(empty.replaceAll(textArea.getText()));
431     // see if there's anthing to search with
432     if (!new com.stevesoft.pat.Regex("[A-Za-z0-9_.]").search(textArea
433             .getText()))
434     {
435       error += "Please enter a (semi-colon separated list of) database id(s)";
436     }
437     if (error.length() > 0)
438     {
439       showErrorMessage(error);
440       resetDialog();
441       return;
442     }
443     // indicate if successive sources should be merged into one alignment.
444     boolean addToLast=false;
445     ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
446     ArrayList<AlignmentI> presult = new ArrayList<AlignmentI>(), aresult = new ArrayList<AlignmentI>();
447     Iterator<DbSourceProxy> proxies = database.getSelectedSources()
448             .iterator();
449     String[] qries;
450     List<String> nextfetch = Arrays.asList(qries = textArea.getText()
451             .split(";"));
452     Iterator<String> en = Arrays.asList(new String[0]).iterator();
453     int nqueries = qries.length;
454     while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
455     {
456       if (!en.hasNext() && nextfetch.size() > 0)
457       {
458         en = nextfetch.iterator();
459         nqueries = nextfetch.size();
460         // save the remaining queries in the original array
461         qries = nextfetch.toArray(new String[nqueries]);
462         nextfetch = new ArrayList<String>();
463       }
464
465       DbSourceProxy proxy = proxies.next();
466       boolean isAliSource = false;
467       try
468       {
469         // update status
470         guiWindow.setProgressBar("Fetching " + nqueries
471                 + " sequence queries from " + proxy.getDbName(), Thread
472                 .currentThread().hashCode());
473         isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
474         if (proxy.getAccessionSeparator() == null)
475         {
476           while (en.hasNext())
477           {
478             String item = en.next();
479             try
480             {
481               if (aresult != null)
482               {
483                 try
484                 {
485                   // give the server a chance to breathe
486                   Thread.sleep(5);
487                 } catch (Exception e)
488                 {
489                   //
490                 }
491
492               }
493
494               AlignmentI indres = null;
495               try
496               {
497                 indres = proxy.getSequenceRecords(item);
498               } catch (OutOfMemoryError oome)
499               {
500                 new OOMWarning("fetching " + item + " from "
501                         + proxy.getDbName(), oome, this);
502               }
503               if (indres != null)
504               {
505                 aresultq.add(item);
506                 aresult.add(indres);
507               }
508               else
509               {
510                 nextfetch.add(item);
511               }
512             } catch (Exception e)
513             {
514               jalview.bin.Cache.log.info("Error retrieving " + item
515                       + " from " + proxy.getDbName(), e);
516               nextfetch.add(item);
517             }
518           }
519         }
520         else
521         {
522           StringBuffer multiacc = new StringBuffer();
523           ArrayList<String> tosend = new ArrayList<String>();
524           while (en.hasNext())
525           {
526             String nel = en.next();
527             tosend.add(nel);
528             multiacc.append(nel);
529             if (en.hasNext())
530             {
531               multiacc.append(proxy.getAccessionSeparator());
532             }
533           }
534           try
535           {
536             AlignmentI rslt;
537             SequenceI[] rs;
538             List<String> nores = new ArrayList<String>();
539             rslt = proxy.getSequenceRecords(multiacc.toString());
540             if (rslt == null || rslt.getHeight() == 0)
541             {
542               // no results - pass on all queries to next source
543               nextfetch.addAll(tosend);
544             }
545             else
546             {
547               aresultq.add(multiacc.toString());
548               aresult.add(rslt);
549
550               rs = rslt.getSequencesArray();
551               // search for each query in the dbrefs associated with each
552               // sequence
553               // returned.
554               // ones we do not find will be used to query next source (if any)
555               for (String q : tosend)
556               {
557                 DBRefEntry dbr = new DBRefEntry(), found[] = null;
558                 dbr.setSource(proxy.getDbSource());
559                 boolean rfound = false;
560                 for (int r = 0; r < rs.length; r++)
561                 {
562                   if (rs[r] != null
563                           && (found = DBRefUtils.searchRefs(
564                                   rs[r].getDBRef(), dbr)) != null
565                           && found.length > 0)
566                   {
567                     rfound = true;
568                     rs[r] = null;
569                     continue;
570                   }
571                 }
572                 if (!rfound)
573                 {
574                   nextfetch.add(q);
575                 }
576               }
577             }
578           } catch (OutOfMemoryError oome)
579           {
580             new OOMWarning("fetching " + multiacc + " from "
581                     + database.getSelectedItem(), oome, this);
582           }
583         }
584
585       } catch (Exception e)
586       {
587         showErrorMessage("Error retrieving " + textArea.getText()
588                 + " from " + database.getSelectedItem());
589         // error
590         // +="Couldn't retrieve sequences from "+database.getSelectedItem();
591         System.err.println("Retrieval failed for source ='"
592                 + database.getSelectedItem() + "' and query\n'"
593                 + textArea.getText() + "'\n");
594         e.printStackTrace();
595       } catch (OutOfMemoryError e)
596       {
597         // resets dialog box - so we don't use OOMwarning here.
598         showErrorMessage("Out of Memory when retrieving "
599                 + textArea.getText()
600                 + " from "
601                 + database.getSelectedItem()
602                 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
603         e.printStackTrace();
604       } catch (Error e)
605       {
606         showErrorMessage("Serious Error retrieving " + textArea.getText()
607                 + " from " + database.getSelectedItem());
608         e.printStackTrace();
609       }
610       // Stack results ready for opening in alignment windows
611       if (aresult != null && aresult.size() > 0)
612       {
613         AlignmentI ar = null;
614         if (isAliSource)
615         {
616           addToLast=false;
617           // new window for each result
618           while (aresult.size() > 0)
619           {
620             presult.add(aresult.remove(0));
621             presultTitle.add(aresultq.remove(0) + " "
622                     + getDefaultRetrievalTitle());
623           }
624         }
625         else
626         {
627           String titl=null;
628           if (addToLast && presult.size()>0)
629           {
630             ar=presult.remove(presult.size()-1);
631             titl=presultTitle.remove(presultTitle.size()-1);
632           }
633           // concatenate all results in one window
634           while (aresult.size() > 0)
635           {
636             if (ar == null)
637             {
638               ar = aresult.remove(0);
639             }
640             else
641             {
642               ar.append(aresult.remove(0));
643             }
644             ;
645           }
646           addToLast=true;
647           presult.add(ar);
648           presultTitle.add(titl);
649         }
650       }
651       guiWindow.setProgressBar("Finished querying", Thread.currentThread().hashCode());
652     }
653     guiWindow.setProgressBar((presult.size()>0) ? "Parsing results.":"Processing ..", Thread.currentThread().hashCode());
654     // process results
655     while (presult.size() > 0)
656     {
657       parseResult(presult.remove(0), presultTitle.remove(0), null);
658     }
659     // only remove visual delay after we finished parsing.
660     guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
661     if (nextfetch.size() > 0)
662     {
663       StringBuffer sb = new StringBuffer();
664       sb.append("Didn't retrieve the following "
665               + (nextfetch.size() == 1 ? "query" : nextfetch.size()
666                       + " queries") + ": \n");
667       int l = sb.length(),lr=0;
668       for (String s : nextfetch)
669       {
670         if (l != sb.length())
671         {
672           sb.append("; ");
673         }
674         if (lr-sb.length()>40)
675         {
676           sb.append("\n");
677         }
678         sb.append(s);
679       }
680       showErrorMessage(sb.toString());
681     }
682     resetDialog();
683   }
684
685   AlignmentI parseResult(String result, String title)
686   {
687     String format = new IdentifyFile().Identify(result, "Paste");
688     Alignment sequences = null;
689     if (FormatAdapter.isValidFormat(format))
690     {
691       sequences = null;
692       try
693       {
694         sequences = new FormatAdapter().readFile(result.toString(),
695                 "Paste", format);
696       } catch (Exception ex)
697       {
698       }
699
700       if (sequences != null)
701       {
702         return parseResult(sequences, title, format);
703       }
704     }
705     else
706     {
707       showErrorMessage("Error retrieving " + textArea.getText() + " from "
708               + database.getSelectedItem());
709     }
710
711     return null;
712   }
713
714   /**
715    * 
716    * @return a standard title for any results retrieved using the currently
717    *         selected source and settings
718    */
719   public String getDefaultRetrievalTitle()
720   {
721     return "Retrieved from " + database.getSelectedItem();
722   }
723
724   AlignmentI parseResult(AlignmentI al, String title,
725           String currentFileFormat)
726   {
727
728     if (al != null && al.getHeight() > 0)
729     {
730       if (alignFrame == null)
731       {
732         AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
733                 AlignFrame.DEFAULT_HEIGHT);
734         if (currentFileFormat != null)
735         {
736           af.currentFileFormat = currentFileFormat; // WHAT IS THE DEFAULT
737           // FORMAT FOR
738           // NON-FormatAdapter Sourced
739           // Alignments?
740         }
741
742         if (title == null)
743         {
744           title = getDefaultRetrievalTitle();
745         }
746         SequenceFeature[] sfs = null;
747         for (Enumeration sq = al.getSequences().elements(); sq
748                 .hasMoreElements();)
749         {
750           if ((sfs = ((SequenceI) sq.nextElement()).getDatasetSequence()
751                   .getSequenceFeatures()) != null)
752           {
753             if (sfs.length > 0)
754             {
755               af.setShowSeqFeatures(true);
756               break;
757             }
758           }
759
760         }
761         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
762                 AlignFrame.DEFAULT_HEIGHT);
763
764         af.statusBar.setText("Successfully pasted alignment file");
765
766         try
767         {
768           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
769                   false));
770         } catch (Exception ex)
771         {
772         }
773       }
774       else
775       {
776         for (int i = 0; i < al.getHeight(); i++)
777         {
778           alignFrame.viewport.getAlignment().addSequence(
779                   al.getSequenceAt(i)); // this
780           // also
781           // creates
782           // dataset
783           // sequence
784           // entries
785         }
786         alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
787                 .getHeight());
788         alignFrame.viewport.getAlignment().getWidth();
789         alignFrame.viewport.firePropertyChange("alignment", null,
790                 alignFrame.viewport.getAlignment().getSequences());
791       }
792     }
793     return al;
794   }
795
796   void showErrorMessage(final String error)
797   {
798     resetDialog();
799     javax.swing.SwingUtilities.invokeLater(new Runnable()
800     {
801       public void run()
802       {
803         JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
804                 "Error Retrieving Data", JOptionPane.WARNING_MESSAGE);
805       }
806     });
807   }
808 }