comparison java-genomics-toolkit/gui/edu/unc/genomics/AssemblyManager.java @ 0:1daf3026d231

Upload alpha version
author timpalpant
date Mon, 13 Feb 2012 21:55:55 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1daf3026d231
1 package edu.unc.genomics;
2
3 import java.io.IOException;
4 import java.nio.file.DirectoryStream;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.zip.DataFormatException;
11
12 import org.apache.log4j.Logger;
13
14 /**
15 * Suite of static methods for managing the built-in assemblies in the resources dir
16 * as well as keeping track of the last used assembly
17 *
18 * @author timpalpant
19 *
20 */
21 public class AssemblyManager {
22
23 private static final Logger log = Logger.getLogger(AssemblyManager.class);
24
25 /**
26 * The last used Assembly
27 */
28 private static Assembly lastUsed;
29
30 /**
31 * Returns all available assemblies in the resources directory
32 * @return the assemblies available in the resources directory
33 */
34 public static List<Assembly> getAvailableAssemblies() {
35 List<Assembly> assemblies = new ArrayList<>();
36
37 try (DirectoryStream<Path> stream = Files.newDirectoryStream(ResourceManager.getAssembliesDirectory(), "*.{len}")) {
38 for (Path entry : stream) {
39 log.debug("Loading assembly: " + entry);
40 try {
41 Assembly a = new Assembly(entry);
42 assemblies.add(a);
43 } catch (IOException | DataFormatException e1) {
44 log.warn("Error loading assembly: " + entry);
45 }
46 }
47 } catch (IOException e) {
48 log.error("Error listing assemblies");
49 e.printStackTrace();
50 }
51
52 return assemblies;
53 }
54
55 public static void deleteAssembly(Assembly a) throws IOException {
56 Files.deleteIfExists(a.getPath());
57 }
58
59 public static Assembly loadCustomAssembly(Path assemblyFile) throws IOException, DataFormatException {
60 log.debug("Loading custom assembly from file: " + assemblyFile);
61 Assembly a = new Assembly(assemblyFile);
62
63 // TODO: Warn if this assembly is already loaded
64
65 // Copy the assembly file into the built-in assemblies directory
66 Files.copy(assemblyFile, ResourceManager.getAssembliesDirectory().resolve(assemblyFile.getFileName()));
67 return a;
68 }
69
70 /**
71 * @return the lastUsed
72 */
73 public static Assembly getLastUsed() {
74 return lastUsed;
75 }
76
77 /**
78 * @param lastUsed the lastUsed to set
79 */
80 public static void setLastUsed(Assembly lastUsed) {
81 AssemblyManager.lastUsed = lastUsed;
82 }
83 }