JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / io / FileLoader.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.io;
22
23 import jalview.api.ComplexAlignFile;
24 import jalview.api.FeaturesDisplayedI;
25 import jalview.bin.Jalview;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.gui.AlignViewport;
32 import jalview.gui.Desktop;
33 import jalview.gui.Jalview2XML;
34 import jalview.schemes.ColourSchemeI;
35 import jalview.structure.StructureSelectionManager;
36 import jalview.util.MessageManager;
37
38 import java.util.StringTokenizer;
39 import java.util.Vector;
40
41 import javax.swing.JOptionPane;
42 import javax.swing.SwingUtilities;
43
44 public class FileLoader implements Runnable
45 {
46   String file;
47
48   String protocol;
49
50   String format;
51
52   FileParse source = null; // alternative specification of where data comes
53
54   // from
55
56   AlignViewport viewport;
57
58   AlignFrame alignFrame;
59
60   long loadtime;
61
62   long memused;
63
64   boolean raiseGUI = true;
65
66   /**
67    * default constructor always raised errors in GUI dialog boxes
68    */
69   public FileLoader()
70   {
71     this(true);
72   }
73
74   /**
75    * construct a Fileloader that may raise errors non-interactively
76    * 
77    * @param raiseGUI
78    *          true if errors are to be raised as GUI dialog boxes
79    */
80   public FileLoader(boolean raiseGUI)
81   {
82     this.raiseGUI = raiseGUI;
83   }
84
85   public void LoadFile(AlignViewport viewport, String file,
86           String protocol, String format)
87   {
88     this.viewport = viewport;
89     LoadFile(file, protocol, format);
90   }
91
92   public void LoadFile(String file, String protocol, String format)
93   {
94     this.file = file;
95     this.protocol = protocol;
96     this.format = format;
97
98     final Thread loader = new Thread(this);
99
100     SwingUtilities.invokeLater(new Runnable()
101     {
102       public void run()
103       {
104         loader.start();
105       }
106     });
107   }
108
109   /**
110    * Load a (file, protocol) source of unknown type
111    * 
112    * @param file
113    * @param protocol
114    */
115   public void LoadFile(String file, String protocol)
116   {
117     LoadFile(file, protocol, null);
118   }
119
120   /**
121    * Load alignment from (file, protocol) and wait till loaded
122    * 
123    * @param file
124    * @param protocol
125    * @return alignFrame constructed from file contents
126    */
127   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
128   {
129     return LoadFileWaitTillLoaded(file, protocol, null);
130   }
131
132   /**
133    * Load alignment from (file, protocol) of type format and wait till loaded
134    * 
135    * @param file
136    * @param protocol
137    * @param format
138    * @return alignFrame constructed from file contents
139    */
140   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
141           String format)
142   {
143     this.file = file;
144     this.protocol = protocol;
145     this.format = format;
146     return _LoadFileWaitTillLoaded();
147   }
148
149   /**
150    * Load alignment from FileParse source of type format and wait till loaded
151    * 
152    * @param source
153    * @param format
154    * @return alignFrame constructed from file contents
155    */
156   public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
157   {
158     this.source = source;
159
160     file = source.getInFile();
161     protocol = source.type;
162     this.format = format;
163     return _LoadFileWaitTillLoaded();
164   }
165
166   /**
167    * start thread and wait until finished, then return the alignFrame that's
168    * (hopefully) been read.
169    * 
170    * @return
171    */
172   protected AlignFrame _LoadFileWaitTillLoaded()
173   {
174     Thread loader = new Thread(this);
175     loader.start();
176
177     while (loader.isAlive())
178     {
179       try
180       {
181         Thread.sleep(500);
182       } catch (Exception ex)
183       {
184       }
185     }
186
187     return alignFrame;
188   }
189
190   public void updateRecentlyOpened()
191   {
192     Vector recent = new Vector();
193     if (protocol.equals(FormatAdapter.PASTE))
194     {
195       // do nothing if the file was pasted in as text... there is no filename to
196       // refer to it as.
197       return;
198     }
199     String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
200             : "RECENT_URL";
201
202     String historyItems = jalview.bin.Cache.getProperty(type);
203
204     StringTokenizer st;
205
206     if (historyItems != null)
207     {
208       st = new StringTokenizer(historyItems, "\t");
209
210       while (st.hasMoreTokens())
211       {
212         recent.addElement(st.nextElement().toString().trim());
213       }
214     }
215
216     if (recent.contains(file))
217     {
218       recent.remove(file);
219     }
220
221     StringBuffer newHistory = new StringBuffer(file);
222     for (int i = 0; i < recent.size() && i < 10; i++)
223     {
224       newHistory.append("\t");
225       newHistory.append(recent.elementAt(i));
226     }
227
228     jalview.bin.Cache.setProperty(type, newHistory.toString());
229
230     if (protocol.equals(FormatAdapter.FILE))
231     {
232       jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
233     }
234   }
235
236   public void run()
237   {
238     String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
239             : file;
240     Runtime rt = Runtime.getRuntime();
241     try
242     {
243       if (Desktop.instance != null)
244       {
245         Desktop.instance.startLoading(file);
246       }
247       if (format == null)
248       {
249         // just in case the caller didn't identify the file for us
250         if (source != null)
251         {
252           format = new IdentifyFile().Identify(source, false); // identify
253           // stream and
254           // rewind rather
255           // than close
256         }
257         else
258         {
259           format = new IdentifyFile().Identify(file, protocol);
260         }
261
262       }
263
264       if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
265       {
266         Desktop.instance.stopLoading();
267         System.err.println("The input file \"" + file
268                 + "\" has null or unidentifiable data content!");
269         if (!Jalview.isHeadlessMode())
270         {
271           javax.swing.JOptionPane.showInternalMessageDialog(
272                   Desktop.desktop,
273                   MessageManager.getString("label.couldnt_read_data")
274                           + " in " + file + "\n"
275                           + AppletFormatAdapter.SUPPORTED_FORMATS,
276                   MessageManager.getString("label.couldnt_read_data"),
277                   JOptionPane.WARNING_MESSAGE);
278         }
279         return;
280       }
281       // TODO: cache any stream datasources as a temporary file (eg. PDBs
282       // retrieved via URL)
283       if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
284       {
285         System.gc();
286         memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
287         // memory
288         // before
289         // load
290       }
291       loadtime = -System.currentTimeMillis();
292       AlignmentI al = null;
293
294       if (format.equalsIgnoreCase("Jalview"))
295       {
296         if (source != null)
297         {
298           // Tell the user (developer?) that this is going to cause a problem
299           System.err
300                   .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
301           // We read the data anyway - it might make sense.
302         }
303         alignFrame = new Jalview2XML(raiseGUI).loadJalviewAlign(file);
304       }
305       else
306       {
307         String error = AppletFormatAdapter.SUPPORTED_FORMATS;
308         if (FormatAdapter.isValidFormat(format))
309         {
310           try
311           {
312             if (source != null)
313             {
314               // read from the provided source
315               al = new FormatAdapter().readFromFile(source, format);
316             }
317             else
318             {
319
320               // open a new source and read from it
321               FormatAdapter fa = new FormatAdapter();
322               al = fa.readFile(file, protocol, format);
323               source = fa.getAlignFile(); // keep reference for later if
324                                           // necessary.
325             }
326           } catch (java.io.IOException ex)
327           {
328             error = ex.getMessage();
329           }
330         }
331         else
332         {
333           if (format != null && format.length() > 7)
334           {
335             // ad hoc message in format.
336             error = format + "\n" + error;
337           }
338         }
339
340         if ((al != null) && (al.getHeight() > 0) && al.hasValidSequence())
341         {
342           // construct and register dataset sequences
343           for (SequenceI sq : al.getSequences())
344           {
345             while (sq.getDatasetSequence() != null)
346             {
347               sq = sq.getDatasetSequence();
348             }
349             if (sq.getAllPDBEntries() != null)
350             {
351               for (PDBEntry pdbe : sq.getAllPDBEntries())
352               {
353                 // register PDB entries with desktop's structure selection
354                 // manager
355                 StructureSelectionManager.getStructureSelectionManager(
356                         Desktop.instance).registerPDBEntry(pdbe);
357               }
358             }
359           }
360
361           if (viewport != null)
362           {
363             // append to existing alignment
364             viewport.addAlignment(al, title);
365           }
366           else
367           {
368             // otherwise construct the alignFrame
369
370             if (source instanceof ComplexAlignFile)
371             {
372               ColumnSelection colSel = ((ComplexAlignFile) source)
373                       .getColumnSelection();
374               SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
375                       .getHiddenSequences();
376               boolean showSeqFeatures = ((ComplexAlignFile) source)
377                       .isShowSeqFeatures();
378               ColourSchemeI cs = ((ComplexAlignFile) source)
379                       .getColourScheme();
380               FeaturesDisplayedI fd = ((ComplexAlignFile) source)
381                       .getDisplayedFeatures();
382               alignFrame = new AlignFrame(al, hiddenSeqs, colSel,
383                       AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
384
385               alignFrame.getViewport().setShowSequenceFeatures(
386                       showSeqFeatures);
387               alignFrame.getViewport().setFeaturesDisplayed(fd);
388               alignFrame.changeColour(cs);
389             }
390             else
391             {
392               alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
393                       AlignFrame.DEFAULT_HEIGHT);
394             }
395             // add metadata and update ui
396             if (!protocol.equals(AppletFormatAdapter.PASTE))
397             {
398               alignFrame.setFileName(file, format);
399             }
400
401             alignFrame.statusBar.setText(MessageManager.formatMessage(
402                     "label.successfully_loaded_file",
403                     new String[] { title }));
404
405             if (raiseGUI)
406             {
407               // add the window to the GUI
408               // note - this actually should happen regardless of raiseGUI
409               // status in Jalview 3
410               // TODO: define 'virtual desktop' for benefit of headless scripts
411               // that perform queries to find the 'current working alignment'
412               Desktop.addInternalFrame(alignFrame, title,
413                       AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
414             }
415
416             try
417             {
418               alignFrame.setMaximum(jalview.bin.Cache.getDefault(
419                       "SHOW_FULLSCREEN", false));
420             } catch (java.beans.PropertyVetoException ex)
421             {
422             }
423           }
424         }
425         else
426         {
427           if (Desktop.instance != null)
428           {
429             Desktop.instance.stopLoading();
430           }
431
432           final String errorMessage = MessageManager
433                   .getString("label.couldnt_load_file")
434                   + " "
435                   + title
436                   + "\n" + error;
437           // TODO: refactor FileLoader to be independent of Desktop / Applet GUI
438           // bits ?
439           if (raiseGUI && Desktop.desktop != null)
440           {
441             javax.swing.SwingUtilities.invokeLater(new Runnable()
442             {
443               public void run()
444               {
445                 JOptionPane.showInternalMessageDialog(Desktop.desktop,
446                         errorMessage, MessageManager
447                                 .getString("label.error_loading_file"),
448                         JOptionPane.WARNING_MESSAGE);
449               }
450             });
451           }
452           else
453           {
454             System.err.println(errorMessage);
455           }
456         }
457       }
458
459       updateRecentlyOpened();
460
461     } catch (Exception er)
462     {
463       System.err.println("Exception whilst opening file '" + file);
464       er.printStackTrace();
465       if (raiseGUI)
466       {
467         javax.swing.SwingUtilities.invokeLater(new Runnable()
468         {
469           public void run()
470           {
471             javax.swing.JOptionPane.showInternalMessageDialog(
472                     Desktop.desktop, MessageManager.formatMessage(
473                             "label.problems_opening_file",
474                             new String[] { file }), MessageManager
475                             .getString("label.file_open_error"),
476                     javax.swing.JOptionPane.WARNING_MESSAGE);
477           }
478         });
479       }
480       alignFrame = null;
481     } catch (OutOfMemoryError er)
482     {
483
484       er.printStackTrace();
485       alignFrame = null;
486       if (raiseGUI)
487       {
488         javax.swing.SwingUtilities.invokeLater(new Runnable()
489         {
490           public void run()
491           {
492             javax.swing.JOptionPane.showInternalMessageDialog(
493                     Desktop.desktop, MessageManager.formatMessage(
494                             "warn.out_of_memory_loading_file", new String[]
495                             { file }), MessageManager
496                             .getString("label.out_of_memory"),
497                     javax.swing.JOptionPane.WARNING_MESSAGE);
498           }
499         });
500       }
501       System.err.println("Out of memory loading file " + file + "!!");
502
503     }
504     loadtime += System.currentTimeMillis();
505     // TODO: Estimate percentage of memory used by a newly loaded alignment -
506     // warn if more memory will be needed to work with it
507     // System.gc();
508     memused = memused
509             - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
510     // in free
511     // memory
512     // after
513     // load
514     if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
515     {
516       if (alignFrame != null)
517       {
518         AlignmentI al = alignFrame.getViewport().getAlignment();
519
520         System.out.println("Loaded '" + title + "' in "
521                 + (loadtime / 1000.0) + "s, took an additional "
522                 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
523                 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
524       }
525       else
526       {
527         // report that we didn't load anything probably due to an out of memory
528         // error
529         System.out.println("Failed to load '" + title + "' in "
530                 + (loadtime / 1000.0) + "s, took an additional "
531                 + (1.0 * memused / (1024.0 * 1024.0))
532                 + " MB (alignment is null)");
533       }
534     }
535     // remove the visual delay indicator
536     if (Desktop.instance != null)
537     {
538       Desktop.instance.stopLoading();
539     }
540
541   }
542
543   /*
544    * (non-Javadoc)
545    * 
546    * @see java.lang.Object#finalize()
547    */
548   protected void finalize() throws Throwable
549   {
550     source = null;
551     alignFrame = null;
552     viewport = null;
553     super.finalize();
554   }
555
556 }