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