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