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