2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.api.ComplexAlignFile;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.AlignFrame;
29 import jalview.gui.AlignViewport;
30 import jalview.gui.Desktop;
31 import jalview.gui.Jalview2XML;
32 import jalview.schemes.ColourSchemeI;
33 import jalview.structure.StructureSelectionManager;
34 import jalview.util.MessageManager;
36 import java.util.StringTokenizer;
37 import java.util.Vector;
39 import javax.swing.JOptionPane;
40 import javax.swing.SwingUtilities;
42 public class FileLoader implements Runnable
50 FileParse source = null; // alternative specification of where data comes
54 AlignViewport viewport;
56 AlignFrame alignFrame;
62 boolean raiseGUI = true;
65 * default constructor always raised errors in GUI dialog boxes
73 * construct a Fileloader that may raise errors non-interactively
76 * true if errors are to be raised as GUI dialog boxes
78 public FileLoader(boolean raiseGUI)
80 this.raiseGUI = raiseGUI;
83 public void LoadFile(AlignViewport viewport, String file,
84 String protocol, String format)
86 this.viewport = viewport;
87 LoadFile(file, protocol, format);
90 public void LoadFile(String file, String protocol, String format)
93 this.protocol = protocol;
96 final Thread loader = new Thread(this);
98 SwingUtilities.invokeLater(new Runnable()
108 * Load a (file, protocol) source of unknown type
113 public void LoadFile(String file, String protocol)
115 LoadFile(file, protocol, null);
119 * Load alignment from (file, protocol) and wait till loaded
123 * @return alignFrame constructed from file contents
125 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
127 return LoadFileWaitTillLoaded(file, protocol, null);
131 * Load alignment from (file, protocol) of type format and wait till loaded
136 * @return alignFrame constructed from file contents
138 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
142 this.protocol = protocol;
143 this.format = format;
144 return _LoadFileWaitTillLoaded();
148 * Load alignment from FileParse source of type format and wait till loaded
152 * @return alignFrame constructed from file contents
154 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
156 this.source = source;
158 file = source.getInFile();
159 protocol = source.type;
160 this.format = format;
161 return _LoadFileWaitTillLoaded();
165 * start thread and wait until finished, then return the alignFrame that's
166 * (hopefully) been read.
170 protected AlignFrame _LoadFileWaitTillLoaded()
172 Thread loader = new Thread(this);
175 while (loader.isAlive())
180 } catch (Exception ex)
188 public void updateRecentlyOpened()
190 Vector recent = new Vector();
191 if (protocol.equals(FormatAdapter.PASTE))
193 // do nothing if the file was pasted in as text... there is no filename to
197 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
200 String historyItems = jalview.bin.Cache.getProperty(type);
204 if (historyItems != null)
206 st = new StringTokenizer(historyItems, "\t");
208 while (st.hasMoreTokens())
210 recent.addElement(st.nextElement().toString().trim());
214 if (recent.contains(file))
219 StringBuffer newHistory = new StringBuffer(file);
220 for (int i = 0; i < recent.size() && i < 10; i++)
222 newHistory.append("\t");
223 newHistory.append(recent.elementAt(i));
226 jalview.bin.Cache.setProperty(type, newHistory.toString());
228 if (protocol.equals(FormatAdapter.FILE))
230 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
236 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
238 Runtime rt = Runtime.getRuntime();
241 if (Desktop.instance != null)
243 Desktop.instance.startLoading(file);
247 // just in case the caller didn't identify the file for us
250 format = new IdentifyFile().Identify(source, false); // identify
257 format = new IdentifyFile().Identify(file, protocol);
260 // TODO: cache any stream datasources as a temporary file (eg. PDBs
261 // retrieved via URL)
262 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
265 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
270 loadtime = -System.currentTimeMillis();
271 AlignmentI al = null;
273 if (format.equalsIgnoreCase("Jalview"))
277 // Tell the user (developer?) that this is going to cause a problem
279 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
280 // We read the data anyway - it might make sense.
282 alignFrame = new Jalview2XML(raiseGUI).loadJalviewAlign(file);
286 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
287 if (FormatAdapter.isValidFormat(format))
293 // read from the provided source
294 al = new FormatAdapter().readFromFile(source, format);
299 // open a new source and read from it
300 FormatAdapter fa = new FormatAdapter();
301 al = fa.readFile(file, protocol, format);
302 source = fa.getAlignFile(); // keep reference for later if
305 } catch (java.io.IOException ex)
307 error = ex.getMessage();
312 if (format != null && format.length() > 7)
314 // ad hoc message in format.
315 error = format + "\n" + error;
319 if ((al != null) && (al.getHeight() > 0))
321 // construct and register dataset sequences
322 for (SequenceI sq : al.getSequences())
324 while (sq.getDatasetSequence() != null)
326 sq = sq.getDatasetSequence();
328 if (sq.getPDBId() != null)
330 for (PDBEntry pdbe : sq.getPDBId())
332 // register PDB entries with desktop's structure selection
334 StructureSelectionManager.getStructureSelectionManager(
335 Desktop.instance).registerPDBEntry(pdbe);
340 if (viewport != null)
342 // append to existing alignment
343 viewport.addAlignment(al, title);
347 // otherwise construct the alignFrame
349 if (source instanceof ComplexAlignFile)
351 ColumnSelection colSel = ((ComplexAlignFile) source)
352 .getColumnSelection();
353 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
354 .getHiddenSequences();
355 boolean showSeqFeatures = ((ComplexAlignFile) source)
356 .isShowSeqFeatures();
357 ColourSchemeI cs = ((ComplexAlignFile) source)
359 alignFrame = new AlignFrame(al, hiddenSeqs, colSel,
360 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
362 alignFrame.getViewport().setShowSequenceFeatures(
364 alignFrame.changeColour(cs);
368 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
369 AlignFrame.DEFAULT_HEIGHT);
371 // add metadata and update ui
372 if (!protocol.equals(AppletFormatAdapter.PASTE))
374 alignFrame.setFileName(file, format);
377 alignFrame.statusBar.setText(MessageManager.formatMessage(
378 "label.successfully_loaded_file", new String[]
383 // add the window to the GUI
384 // note - this actually should happen regardless of raiseGUI
385 // status in Jalview 3
386 // TODO: define 'virtual desktop' for benefit of headless scripts
387 // that perform queries to find the 'current working alignment'
388 Desktop.addInternalFrame(alignFrame, title,
389 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
394 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
395 "SHOW_FULLSCREEN", false));
396 } catch (java.beans.PropertyVetoException ex)
403 if (Desktop.instance != null)
405 Desktop.instance.stopLoading();
408 final String errorMessage = "Couldn't load file " + title + "\n"
410 // TODO: refactor FileLoader to be independent of Desktop / Applet GUI
412 if (raiseGUI && Desktop.desktop != null)
414 javax.swing.SwingUtilities.invokeLater(new Runnable()
418 JOptionPane.showInternalMessageDialog(Desktop.desktop,
419 errorMessage, MessageManager
420 .getString("label.error_loading_file"),
421 JOptionPane.WARNING_MESSAGE);
427 System.err.println(errorMessage);
432 updateRecentlyOpened();
434 } catch (Exception er)
436 System.err.println("Exception whilst opening file '" + file);
437 er.printStackTrace();
440 javax.swing.SwingUtilities.invokeLater(new Runnable()
444 javax.swing.JOptionPane.showInternalMessageDialog(
445 Desktop.desktop, MessageManager.formatMessage(
446 "label.problems_opening_file", new String[]
447 { file }), MessageManager
448 .getString("label.file_open_error"),
449 javax.swing.JOptionPane.WARNING_MESSAGE);
454 } catch (OutOfMemoryError er)
457 er.printStackTrace();
461 javax.swing.SwingUtilities.invokeLater(new Runnable()
465 javax.swing.JOptionPane.showInternalMessageDialog(
466 Desktop.desktop, MessageManager.formatMessage(
467 "warn.out_of_memory_loading_file", new String[]
468 { file }), MessageManager
469 .getString("label.out_of_memory"),
470 javax.swing.JOptionPane.WARNING_MESSAGE);
474 System.err.println("Out of memory loading file " + file + "!!");
477 loadtime += System.currentTimeMillis();
478 // TODO: Estimate percentage of memory used by a newly loaded alignment -
479 // warn if more memory will be needed to work with it
482 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
487 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
489 if (alignFrame != null)
491 AlignmentI al = alignFrame.getViewport().getAlignment();
493 System.out.println("Loaded '" + title + "' in "
494 + (loadtime / 1000.0) + "s, took an additional "
495 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
496 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
500 // report that we didn't load anything probably due to an out of memory
502 System.out.println("Failed to load '" + title + "' in "
503 + (loadtime / 1000.0) + "s, took an additional "
504 + (1.0 * memused / (1024.0 * 1024.0))
505 + " MB (alignment is null)");
508 // remove the visual delay indicator
509 if (Desktop.instance != null)
511 Desktop.instance.stopLoading();
519 * @see java.lang.Object#finalize()
521 protected void finalize() throws Throwable