Merge branch 'develop' into features/r2_11_2/JAL-3829_3dbeacons
[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.SequenceI;
28 import jalview.fts.core.GFTSPanel;
29 import jalview.fts.service.pdb.PDBFTSPanel;
30 import jalview.fts.service.threedbeacons.TDBeaconsFTSPanel;
31 import jalview.fts.service.uniprot.UniprotFTSPanel;
32 import jalview.io.FileFormatI;
33 import jalview.io.gff.SequenceOntologyI;
34 import jalview.util.DBRefUtils;
35 import jalview.util.MessageManager;
36 import jalview.util.Platform;
37 import jalview.ws.seqfetcher.DbSourceProxy;
38
39 import java.awt.BorderLayout;
40 import java.awt.Font;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.KeyAdapter;
44 import java.awt.event.KeyEvent;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.HashSet;
48 import java.util.Iterator;
49 import java.util.List;
50
51 import javax.swing.JButton;
52 import javax.swing.JCheckBox;
53 import javax.swing.JComboBox;
54 import javax.swing.JInternalFrame;
55 import javax.swing.JLabel;
56 import javax.swing.JPanel;
57 import javax.swing.JScrollPane;
58 import javax.swing.JTextArea;
59 import javax.swing.SwingConstants;
60
61 /**
62  * A panel where the use may choose a database source, and enter one or more
63  * accessions, to retrieve entries from the database.
64  * <p>
65  * If the selected source is Uniprot or PDB, a free text search panel is opened
66  * instead to perform the search and selection.
67  */
68 public class SequenceFetcher extends JPanel implements Runnable
69 {
70   private static jalview.ws.SequenceFetcher sfetch = null;
71
72   JLabel exampleAccession;
73
74   JComboBox<String> database;
75
76   JCheckBox replacePunctuation;
77
78   JButton okBtn;
79
80   JButton exampleBtn;
81
82   JButton closeBtn;
83
84   JButton backBtn;
85
86   JTextArea textArea;
87
88   JInternalFrame frame;
89
90   IProgressIndicator guiWindow;
91
92   AlignFrame alignFrame;
93
94   GFTSPanel parentSearchPanel;
95
96   IProgressIndicator progressIndicator;
97
98   volatile boolean _isConstructing = false;
99
100   /**
101    * Returns the shared instance of the SequenceFetcher client
102    * 
103    * @return
104    */
105   public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton()
106   {
107     if (sfetch == null)
108     {
109       sfetch = new jalview.ws.SequenceFetcher();
110     }
111     return sfetch;
112   }
113
114   /**
115    * Constructor given a client to receive any status or progress messages
116    * (currently either the Desktop, or an AlignFrame panel)
117    * 
118    * @param guiIndic
119    */
120   public SequenceFetcher(IProgressIndicator guiIndic)
121   {
122     this(guiIndic, null, null);
123   }
124
125   /**
126    * Constructor with specified database and accession(s) to retrieve
127    * 
128    * @param guiIndic
129    * @param selectedDb
130    * @param queryString
131    */
132   public SequenceFetcher(IProgressIndicator guiIndic,
133           final String selectedDb, final String queryString)
134   {
135     this.progressIndicator = guiIndic;
136     getSequenceFetcherSingleton();
137     this.guiWindow = progressIndicator;
138
139     if (progressIndicator instanceof AlignFrame)
140     {
141       alignFrame = (AlignFrame) progressIndicator;
142     }
143
144     jbInit(selectedDb);
145     textArea.setText(queryString);
146
147     frame = new JInternalFrame();
148     frame.setContentPane(this);
149     Desktop.addInternalFrame(frame, getFrameTitle(), true, 400, 
150                 Platform.isAMacAndNotJS() ? 240 : 180);
151   }
152
153   private String getFrameTitle()
154   {
155     return ((alignFrame == null)
156             ? MessageManager.getString("label.new_sequence_fetcher")
157             : MessageManager
158                     .getString("label.additional_sequence_fetcher"));
159   }
160
161   private void jbInit(String selectedDb)
162   {
163     this.setLayout(new BorderLayout());
164
165     database = new JComboBox<>();
166     database.setFont(JvSwingUtils.getLabelFont());
167     database.setPrototypeDisplayValue("ENSEMBLGENOMES   ");
168     String[] sources = new jalview.ws.SequenceFetcher().getSupportedDb();
169     Arrays.sort(sources, String.CASE_INSENSITIVE_ORDER);
170     database.addItem(MessageManager.getString("action.select_ddbb"));
171     for (String source : sources)
172     {
173       database.addItem(source);
174     }
175     database.setSelectedItem(selectedDb);
176     if (database.getSelectedIndex() == -1)
177     {
178       database.setSelectedIndex(0);
179     }
180     database.setMaximumRowCount(database.getItemCount());
181     database.addActionListener(new ActionListener()
182     {
183       @Override
184       public void actionPerformed(ActionEvent e)
185       {
186         String currentSelection = (String) database.getSelectedItem();
187         updateExampleQuery(currentSelection);
188
189         if ("pdb".equalsIgnoreCase(currentSelection))
190         {
191           frame.dispose();
192           new PDBFTSPanel(SequenceFetcher.this);
193         }
194         else if ("uniprot".equalsIgnoreCase(currentSelection))
195         {
196           frame.dispose();
197           new UniprotFTSPanel(SequenceFetcher.this);
198         }
199         else if ("3d-beacons".equalsIgnoreCase(currentSelection))
200         {
201           frame.dispose();
202           new TDBeaconsFTSPanel(SequenceFetcher.this);
203         }
204         else
205         {
206           otherSourceAction();
207         }
208       }
209     });
210
211     exampleAccession = new JLabel("");
212     exampleAccession.setFont(new Font("Verdana", Font.BOLD, 11));
213     JLabel jLabel1 = new JLabel(MessageManager
214             .getString("label.separate_multiple_accession_ids"));
215     jLabel1.setFont(new Font("Verdana", Font.ITALIC, 11));
216     jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
217
218     replacePunctuation = new JCheckBox(
219             MessageManager.getString("label.replace_commas_semicolons"));
220     replacePunctuation.setHorizontalAlignment(SwingConstants.LEFT);
221     replacePunctuation.setFont(new Font("Verdana", Font.ITALIC, 11));
222     okBtn = new JButton(MessageManager.getString("action.ok"));
223     okBtn.addActionListener(new ActionListener()
224     {
225       @Override
226       public void actionPerformed(ActionEvent e)
227       {
228         ok_actionPerformed();
229       }
230     });
231     JButton clear = new JButton(MessageManager.getString("action.clear"));
232     clear.addActionListener(new ActionListener()
233     {
234       @Override
235       public void actionPerformed(ActionEvent e)
236       {
237         clear_actionPerformed();
238       }
239     });
240
241     exampleBtn = new JButton(MessageManager.getString("label.example"));
242     exampleBtn.addActionListener(new ActionListener()
243     {
244       @Override
245       public void actionPerformed(ActionEvent e)
246       {
247         example_actionPerformed();
248       }
249     });
250     closeBtn = new JButton(MessageManager.getString("action.cancel"));
251     closeBtn.addActionListener(new ActionListener()
252     {
253       @Override
254       public void actionPerformed(ActionEvent e)
255       {
256         close_actionPerformed(e);
257       }
258     });
259     backBtn = new JButton(MessageManager.getString("action.back"));
260     backBtn.addActionListener(new ActionListener()
261     {
262       @Override
263       public void actionPerformed(ActionEvent e)
264       {
265         parentSearchPanel.btn_back_ActionPerformed();
266       }
267     });
268     // back not visible unless embedded
269     backBtn.setVisible(false);
270
271     textArea = new JTextArea();
272     textArea.setFont(JvSwingUtils.getLabelFont());
273     textArea.setLineWrap(true);
274     textArea.addKeyListener(new KeyAdapter()
275     {
276       @Override
277       public void keyPressed(KeyEvent e)
278       {
279         if (e.getKeyCode() == KeyEvent.VK_ENTER)
280         {
281           ok_actionPerformed();
282         }
283       }
284     });
285
286     JPanel actionPanel = new JPanel();
287     actionPanel.add(backBtn);
288     actionPanel.add(exampleBtn);
289     actionPanel.add(clear);
290     actionPanel.add(okBtn);
291     actionPanel.add(closeBtn);
292
293     JPanel databasePanel = new JPanel();
294     databasePanel.setLayout(new BorderLayout());
295     databasePanel.add(database, BorderLayout.NORTH);
296     databasePanel.add(exampleAccession, BorderLayout.CENTER);
297     JPanel jPanel2a = new JPanel(new BorderLayout());
298     jPanel2a.add(jLabel1, BorderLayout.NORTH);
299     jPanel2a.add(replacePunctuation, BorderLayout.SOUTH);
300     databasePanel.add(jPanel2a, BorderLayout.SOUTH);
301
302     JPanel idsPanel = new JPanel();
303     idsPanel.setLayout(new BorderLayout(0, 5));
304     JScrollPane jScrollPane1 = new JScrollPane();
305     jScrollPane1.getViewport().add(textArea);
306     idsPanel.add(jScrollPane1, BorderLayout.CENTER);
307
308     this.add(actionPanel, BorderLayout.SOUTH);
309     this.add(idsPanel, BorderLayout.CENTER);
310     this.add(databasePanel, BorderLayout.NORTH);
311   }
312
313   /**
314    * Answers a semi-colon-delimited string with the example query or queries for
315    * the selected database
316    * 
317    * @param db
318    * @return
319    */
320   protected String getExampleQueries(String db)
321   {
322     StringBuilder sb = new StringBuilder();
323     HashSet<String> hs = new HashSet<>();
324     for (DbSourceProxy dbs : sfetch.getSourceProxy(db))
325     {
326       String tq = dbs.getTestQuery();
327       if (hs.add(tq)) // not a duplicate source
328       {
329         if (sb.length() > 0)
330         {
331           sb.append(";");
332         }
333         sb.append(tq);
334       }
335     }
336     return sb.toString();
337   }
338
339   /**
340    * Action on selecting a database other than Uniprot or PDB is to enable or
341    * disable 'Replace commas', and await input in the query field
342    */
343   protected void otherSourceAction()
344   {
345     try
346     {
347       String eq = exampleAccession.getText();
348       // TODO this should be a property of the SequenceFetcher whether commas
349       // are allowed in the IDs...
350
351       boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
352       replacePunctuation.setEnabled(enablePunct);
353
354     } catch (Exception ex)
355     {
356       exampleAccession.setText("");
357       replacePunctuation.setEnabled(true);
358     }
359     repaint();
360   }
361
362   /**
363    * Sets the text of the example query to incorporate the example accession
364    * provided by the selected database source
365    * 
366    * @param selectedDatabase
367    * @return
368    */
369   protected String updateExampleQuery(String selectedDatabase)
370   {
371     String eq = getExampleQueries(selectedDatabase);
372     exampleAccession.setText(MessageManager
373             .formatMessage("label.example_query_param", new String[]
374             { eq }));
375     return eq;
376   }
377
378   /**
379    * Action on clicking the 'Example' button is to write the example accession
380    * as the query text field value
381    */
382   protected void example_actionPerformed()
383   {
384     String eq = getExampleQueries((String) database.getSelectedItem());
385     textArea.setText(eq);
386     repaint();
387   }
388
389   /**
390    * Clears the query input field
391    */
392   protected void clear_actionPerformed()
393   {
394     textArea.setText("");
395     repaint();
396   }
397
398   /**
399    * Action on Close button is to close this frame, and also (if it is embedded
400    * in a search panel) to close the search panel
401    * 
402    * @param e
403    */
404   protected void close_actionPerformed(ActionEvent e)
405   {
406     try
407     {
408       frame.setClosed(true);
409       if (parentSearchPanel != null)
410       {
411         parentSearchPanel.btn_cancel_ActionPerformed();
412       }
413     } catch (Exception ex)
414     {
415     }
416   }
417
418   /**
419    * Action on OK is to start the fetch for entered accession(s)
420    */
421   public void ok_actionPerformed()
422   {
423     /*
424      * tidy inputs and check there is something to search for
425      */
426     String t0 = textArea.getText();
427     String text = t0.trim();
428     if (replacePunctuation.isEnabled() && replacePunctuation.isSelected())
429     {
430       text = text.replace(",", ";");
431     }
432     text = text.replaceAll("(\\s|[; ])+", ";");
433     if (!t0.equals(text)) 
434     {
435           textArea.setText(text);
436     }
437     if (text.isEmpty())
438     {
439       // todo i18n
440       showErrorMessage(
441               "Please enter a (semi-colon separated list of) database id(s)");
442       resetDialog();
443       return;
444     }
445     if (database.getSelectedIndex() == 0)
446     {
447       // todo i18n
448       showErrorMessage("Please choose a database");
449       resetDialog();
450       return;
451     }
452
453     exampleBtn.setEnabled(false);
454     textArea.setEnabled(false);
455     okBtn.setEnabled(false);
456     closeBtn.setEnabled(false);
457     backBtn.setEnabled(false);
458
459     Thread worker = new Thread(this);
460     worker.start();
461   }
462
463   private void resetDialog()
464   {
465     exampleBtn.setEnabled(true);
466     textArea.setEnabled(true);
467     okBtn.setEnabled(true);
468     closeBtn.setEnabled(true);
469     backBtn.setEnabled(parentSearchPanel != null);
470   }
471
472   @Override
473   public void run()
474   {
475     boolean addToLast = false;
476     List<String> aresultq = new ArrayList<>();
477     List<String> presultTitle = new ArrayList<>();
478     List<AlignmentI> presult = new ArrayList<>();
479     List<AlignmentI> aresult = new ArrayList<>();
480     List<DbSourceProxy> sources = sfetch
481             .getSourceProxy((String) database.getSelectedItem());
482     Iterator<DbSourceProxy> proxies = sources.iterator();
483     String[] qries = textArea.getText().trim().split(";");
484     List<String> nextFetch = Arrays.asList(qries);
485     Iterator<String> en = Arrays.asList(new String[0]).iterator();
486     int nqueries = qries.length;
487
488     FeatureSettingsModelI preferredFeatureColours = null;
489     while (proxies.hasNext() && (en.hasNext() || nextFetch.size() > 0))
490     {
491       if (!en.hasNext() && nextFetch.size() > 0)
492       {
493         en = nextFetch.iterator();
494         nqueries = nextFetch.size();
495         // save the remaining queries in the original array
496         qries = nextFetch.toArray(new String[nqueries]);
497         nextFetch = new ArrayList<>();
498       }
499
500       DbSourceProxy proxy = proxies.next();
501       try
502       {
503         // update status
504         guiWindow.setProgressBar(MessageManager.formatMessage(
505                 "status.fetching_sequence_queries_from", new String[]
506                 { Integer.valueOf(nqueries).toString(),
507                     proxy.getDbName() }),
508                 Thread.currentThread().hashCode());
509         if (proxy.getMaximumQueryCount() == 1)
510         {
511           /*
512            * proxy only handles one accession id at a time
513            */
514           while (en.hasNext())
515           {
516             String acc = en.next();
517             if (!fetchSingleAccession(proxy, acc, aresultq, aresult))
518             {
519               nextFetch.add(acc);
520             }
521           }
522         }
523         else
524         {
525           /*
526            * proxy can fetch multiple accessions at one time
527            */
528           fetchMultipleAccessions(proxy, en, aresultq, aresult, nextFetch);
529         }
530       } catch (Exception e)
531       {
532         showErrorMessage("Error retrieving " + textArea.getText() + " from "
533                 + database.getSelectedItem());
534         // error
535         // +="Couldn't retrieve sequences from "+database.getSelectedItem();
536         System.err.println("Retrieval failed for source ='"
537                 + database.getSelectedItem() + "' and query\n'"
538                 + textArea.getText() + "'\n");
539         e.printStackTrace();
540       } catch (OutOfMemoryError e)
541       {
542         showErrorMessage("Out of Memory when retrieving "
543                 + textArea.getText() + " from " + database.getSelectedItem()
544                 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
545         e.printStackTrace();
546       } catch (Error e)
547       {
548         showErrorMessage("Serious Error retrieving " + textArea.getText()
549                 + " from " + database.getSelectedItem());
550         e.printStackTrace();
551       }
552
553       // Stack results ready for opening in alignment windows
554       if (aresult != null && aresult.size() > 0)
555       {
556         FeatureSettingsModelI proxyColourScheme = proxy
557                 .getFeatureColourScheme();
558         if (proxyColourScheme != null)
559         {
560           preferredFeatureColours = proxyColourScheme;
561         }
562
563         AlignmentI ar = null;
564         if (proxy.isAlignmentSource())
565         {
566           addToLast = false;
567           // new window for each result
568           while (aresult.size() > 0)
569           {
570             presult.add(aresult.remove(0));
571             presultTitle.add(
572                     aresultq.remove(0) + " " + getDefaultRetrievalTitle());
573           }
574         }
575         else
576         {
577           String titl = null;
578           if (addToLast && presult.size() > 0)
579           {
580             ar = presult.remove(presult.size() - 1);
581             titl = presultTitle.remove(presultTitle.size() - 1);
582           }
583           // concatenate all results in one window
584           while (aresult.size() > 0)
585           {
586             if (ar == null)
587             {
588               ar = aresult.remove(0);
589             }
590             else
591             {
592               ar.append(aresult.remove(0));
593             }
594           }
595           addToLast = true;
596           presult.add(ar);
597           presultTitle.add(titl);
598         }
599       }
600       guiWindow.setProgressBar(
601               MessageManager.getString("status.finshed_querying"),
602               Thread.currentThread().hashCode());
603     }
604     guiWindow
605             .setProgressBar(
606                     (presult.size() > 0)
607                             ? MessageManager
608                                     .getString("status.parsing_results")
609                             : MessageManager.getString("status.processing"),
610                     Thread.currentThread().hashCode());
611     // process results
612     while (presult.size() > 0)
613     {
614       parseResult(presult.remove(0), presultTitle.remove(0), null,
615               preferredFeatureColours);
616     }
617     // only remove visual delay after we finished parsing.
618     guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
619     if (nextFetch.size() > 0)
620     {
621       StringBuffer sb = new StringBuffer();
622       sb.append("Didn't retrieve the following "
623               + (nextFetch.size() == 1 ? "query"
624                       : nextFetch.size() + " queries")
625               + ": \n");
626       int l = sb.length(), lr = 0;
627       for (String s : nextFetch)
628       {
629         if (l != sb.length())
630         {
631           sb.append("; ");
632         }
633         if (lr - sb.length() > 40)
634         {
635           sb.append("\n");
636         }
637         sb.append(s);
638       }
639       showErrorMessage(sb.toString());
640     }
641     resetDialog();
642   }
643
644   /**
645    * Tries to fetch one or more accession ids from the database proxy
646    * 
647    * @param proxy
648    * @param accessions
649    *          the queries to fetch
650    * @param aresultq
651    *          a successful queries list to add to
652    * @param aresult
653    *          a list of retrieved alignments to add to
654    * @param nextFetch
655    *          failed queries are added to this list
656    * @throws Exception
657    */
658   void fetchMultipleAccessions(DbSourceProxy proxy,
659           Iterator<String> accessions, List<String> aresultq,
660           List<AlignmentI> aresult, List<String> nextFetch) throws Exception
661   {
662     StringBuilder multiacc = new StringBuilder();
663     List<String> tosend = new ArrayList<>();
664     while (accessions.hasNext())
665     {
666       String nel = accessions.next();
667       tosend.add(nel);
668       multiacc.append(nel);
669       if (accessions.hasNext())
670       {
671         multiacc.append(proxy.getAccessionSeparator());
672       }
673     }
674
675     try
676     {
677       String query = multiacc.toString();
678       AlignmentI rslt = proxy.getSequenceRecords(query);
679       if (rslt == null || rslt.getHeight() == 0)
680       {
681         // no results - pass on all queries to next source
682         nextFetch.addAll(tosend);
683       }
684       else
685       {
686         aresultq.add(query);
687         aresult.add(rslt);
688         if (tosend.size() > 1)
689         {
690           checkResultForQueries(rslt, tosend, nextFetch, proxy);
691         }
692       }
693     } catch (OutOfMemoryError oome)
694     {
695       new OOMWarning("fetching " + multiacc + " from "
696               + database.getSelectedItem(), oome, this);
697     }
698   }
699
700   /**
701    * Query for a single accession id via the database proxy
702    * 
703    * @param proxy
704    * @param accession
705    * @param aresultq
706    *          a list of successful queries to add to
707    * @param aresult
708    *          a list of retrieved alignments to add to
709    * @return true if the fetch was successful, else false
710    */
711   boolean fetchSingleAccession(DbSourceProxy proxy, String accession,
712           List<String> aresultq, List<AlignmentI> aresult)
713   {
714     boolean success = false;
715     try
716     {
717       if (aresult != null)
718       {
719         try
720         {
721           // give the server a chance to breathe
722           Thread.sleep(5);
723         } catch (Exception e)
724         {
725           //
726         }
727       }
728
729       AlignmentI indres = null;
730       try
731       {
732         indres = proxy.getSequenceRecords(accession);
733       } catch (OutOfMemoryError oome)
734       {
735         new OOMWarning(
736                 "fetching " + accession + " from " + proxy.getDbName(),
737                 oome, this);
738       }
739       if (indres != null)
740       {
741         aresultq.add(accession);
742         aresult.add(indres);
743         success = true;
744       }
745     } catch (Exception e)
746     {
747       Cache.log.info("Error retrieving " + accession + " from "
748               + proxy.getDbName(), e);
749     }
750     return success;
751   }
752
753   /**
754    * Checks which of the queries were successfully retrieved by searching the
755    * DBRefs of the retrieved sequences for a match. Any not found are added to
756    * the 'nextFetch' list.
757    * 
758    * @param rslt
759    * @param queries
760    * @param nextFetch
761    * @param proxy
762    */
763   void checkResultForQueries(AlignmentI rslt, List<String> queries,
764           List<String> nextFetch, DbSourceProxy proxy)
765   {
766     SequenceI[] rs = rslt.getSequencesArray();
767
768     for (String q : queries)
769     {
770         // BH 2019.01.25 dbr is never used.
771 //      DBRefEntry dbr = new DBRefEntry();
772 //      dbr.setSource(proxy.getDbSource());
773 //      dbr.setVersion(null);
774       String accId = proxy.getAccessionIdFromQuery(q);
775 //      dbr.setAccessionId(accId);
776       boolean rfound = false;
777       for (int r = 0, nr = rs.length; r < nr; r++)
778       {
779         if (rs[r] != null)
780         {
781           List<DBRefEntry> found = DBRefUtils.searchRefs(rs[r].getDBRefs(),
782                   accId);
783           if (!found.isEmpty())
784           {
785             rfound = true;
786             break;
787           }
788         }
789       }
790       if (!rfound)
791       {
792         nextFetch.add(q);
793       }
794     }
795   }
796
797   /**
798    * 
799    * @return a standard title for any results retrieved using the currently
800    *         selected source and settings
801    */
802   public String getDefaultRetrievalTitle()
803   {
804     return "Retrieved from " + database.getSelectedItem();
805   }
806   /**
807    * constructs an alignment frame given the data and metadata
808    * @param al
809    * @param title
810    * @param currentFileFormat
811    * @param preferredFeatureColours
812    * @return the alignment 
813    */
814   public AlignmentI parseResult(AlignmentI al, String title,
815           FileFormatI currentFileFormat,
816           FeatureSettingsModelI preferredFeatureColours)
817   {
818
819     if (al != null && al.getHeight() > 0)
820     {
821       if (title == null)
822       {
823         title = getDefaultRetrievalTitle();
824       }
825       if (alignFrame == null)
826       {
827         AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
828                 AlignFrame.DEFAULT_HEIGHT);
829         if (currentFileFormat != null)
830         {
831           af.currentFileFormat = currentFileFormat;
832         }
833
834         List<SequenceI> alsqs = al.getSequences();
835         synchronized (alsqs)
836         {
837           for (SequenceI sq : alsqs)
838           {
839             if (sq.getFeatures().hasFeatures())
840             {
841               af.setShowSeqFeatures(true);
842               break;
843             }
844           }
845         }
846
847         af.getViewport().applyFeaturesStyle(preferredFeatureColours);
848         if (Cache.getDefault("HIDE_INTRONS", true))
849         {
850           af.hideFeatureColumns(SequenceOntologyI.EXON, false);
851         }
852         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
853                 AlignFrame.DEFAULT_HEIGHT);
854
855         af.setStatus(MessageManager
856                 .getString("label.successfully_pasted_alignment_file"));
857
858         try
859         {
860           af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", 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         JvOptionPane.showInternalMessageDialog(Desktop.desktop, error,
882                 MessageManager.getString("label.error_retrieving_data"),
883                 JvOptionPane.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
898   /**
899    * Hide this panel (on clicking the database button to open the database
900    * chooser)
901    */
902   void hidePanel()
903   {
904     frame.setVisible(false);
905   }
906
907   public void setQuery(String ids)
908   {
909     textArea.setText(ids);
910   }
911
912   /**
913    * Called to modify the search panel for embedding as an alternative tab of a
914    * free text search panel. The database choice list is hidden (since the
915    * choice has been made), and a Back button is made visible (which reopens the
916    * Sequence Fetcher panel).
917    * 
918    * @param parentPanel
919    */
920   public void embedIn(GFTSPanel parentPanel)
921   {
922     database.setVisible(false);
923     backBtn.setVisible(true);
924     parentSearchPanel = parentPanel;
925   }
926 }