forked from browning-lab/hap-ibd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitFileWriterManager.java
More file actions
56 lines (46 loc) · 1.88 KB
/
SplitFileWriterManager.java
File metadata and controls
56 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package hapibd;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import blbutil.Utilities;
public class SplitFileWriterManager implements AutoCloseable {
public static enum MatchType {HBD, IBD};
private final String outputPrefix;
private final String splitFilename;
private final Map<Integer, PrintWriter> ibdWriters;
private final Map<Integer, PrintWriter> hbdWriters;
public SplitFileWriterManager(HapIbdPar par) {
this.outputPrefix = par.out();
this.splitFilename = par.splitFilename();
this.ibdWriters = new ConcurrentHashMap<>();
this.hbdWriters = new ConcurrentHashMap<>();
}
public PrintWriter getWriterForProxyKey(int proxyKey, MatchType type) {
Map<Integer, PrintWriter> writers = type.equals(MatchType.IBD) ? ibdWriters : hbdWriters;
return writers.computeIfAbsent(proxyKey, key -> {
try {
String dirPath = outputPrefix + "/" + key;
File dir = new File(dirPath);
if (!dir.mkdirs() && !dir.isDirectory()) {
Utilities.exit("ERROR: Failed to create directory " + dirPath);
}
String filename = dirPath + "/" + splitFilename + "." + type.toString().toLowerCase();
return new PrintWriter(new File(filename));
}
catch (IOException e) {
Utilities.exit("ERROR creating " + type + " file for proxy key " + key + ": ", e);
return null; // This will never be reached due to Utilities.exit
}
});
}
public synchronized void close() {
for (PrintWriter writer : ibdWriters.values()) {
writer.close();
}
for (PrintWriter writer : hbdWriters.values()) {
writer.close();
}
}
}