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