This repository has been archived by the owner on Jun 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequenceDataRepository.java
180 lines (167 loc) · 7.63 KB
/
SequenceDataRepository.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package sequence.data;
import sequence.Sequence;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.LineIterator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Stream;
import static java.text.MessageFormat.format;
/**
* Created by Jan Kolomazník on 27.6.17. optimized by josef havranek not redacted
*/
@Slf4j
@Component
public class SequenceDataRepository {
private static final String FASTA = "fasta";
private static final String INTERNAL_FORMAT = "sequence-buffer";
private static final int SHARED_CPU_CAHCE = 3000000;//this is suboptimal ... it would be beter to query for cache.. i am writing solution for this
private static final int MAX_LINE_LENGTH = SHARED_CPU_CAHCE / Runtime.getRuntime().availableProcessors();
private Path storageDir;
@Value("${sequence.dir}")
public void setStorageDir(String storageDir) {
this.storageDir = Paths.get(storageDir);
// Create if not exist
if (Files.notExists(this.storageDir)) {
try {
Files.createDirectories(this.storageDir);
} catch (IOException e) {
throw new UnsupportedOperationException(format("Create sequence {0} dir failed.", storageDir), e);
}
}
}
// precessing to make lines long enough to be worth processing in parallel
// but not that long that cpus would be running out of cache constantly
// this method is heavyly optimized josef havranek
//this is where multifasta detection
//and line length optimalization hapens
public Path saveRawBuffer(UUID bufferId, String format, InputStream buffer) {
Path path = getPath(bufferId, format);
assert Files.notExists(path) : "Sequence buffer data-file can't by overridden.";
//light pre processing
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path.toFile()), StandardCharsets.UTF_8))) {
try (LineIterator raw = new LineIterator(new InputStreamReader(buffer))) {
String line;
long currentLength = 0;
if (format.equals(FASTA)) {
boolean previousWasFASTA = false;
boolean isMultiFASTA = false;
while (raw.hasNext()) {
line = raw.next();
//found fasta comment that must be on separate line... reset counter put it on separate line
if (line.startsWith(">") || line.startsWith(";")) {
currentLength = 0;
if (!previousWasFASTA) {
previousWasFASTA = true;
if (!isMultiFASTA) {
isMultiFASTA = true;
} else {
throw new UnsupportedOperationException("Illegal input detected!\n" +
"Multi-FASTA format is not supported");
}
} else {
//agregating fasta lines on one line to make extraction in stream stateless
// look here
//https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html#stateful_lambda_expressions
writer.write("\0");
}
writer.write(line);
} else {
if (previousWasFASTA) {
previousWasFASTA = false;
writer.write("\n");
}
currentLength += line.length();
if (currentLength >= MAX_LINE_LENGTH) {
writer.write("\n");
currentLength = 0;
}
writer.write(line);
}
}
} else {// plain format
while (raw.hasNext()) {
line = raw.next();
currentLength += line.length();
if (currentLength >= MAX_LINE_LENGTH) {
writer.write("\n");
currentLength = 0;
}
writer.write(line);
}
}
}
return path;
} catch (Exception e) {
try {
Files.delete(path);
} catch (IOException ex) {
ex = new IOException("Exeption during emergency deletion of partially written uploaded file " +
"exception encountered", ex);
e.addSuppressed(ex);
}
throw new UnsupportedOperationException(e);
} finally {
try {
buffer.close();
} catch (IOException e) {
log.warn("can't close stream of uploaded data.", e);
}
}
}
public Stream<String> loadRawBufferToStream(Sequence sequence, String format) {
try {
Path file = getPath(sequence.getBufferId(), format);
return Files.lines(file);
} catch (IOException e) {
throw new UnsupportedOperationException(e);
}
}
public void deleteRawBuffer(UUID bufferId, String format) {
Path file = getPath(bufferId, format);
try {
Files.delete(file);
} catch (IOException e) {
log.warn(format("Delete sequence data with format: {0} failed.", format), e);
}
}
// this method is heavyly optimized Josef Havranek
public Path save(UUID bufferId, Iterator<ByteBuffer> buffers) {
Path file = getPath(bufferId, INTERNAL_FORMAT);
assert Files.notExists(file) : "Sequence buffer data-file can't by overridden.";
try (FileChannel output = new FileOutputStream(file.toFile()).getChannel()) {
ByteBuffer current;
while (buffers.hasNext()) {
current = buffers.next().asReadOnlyBuffer();//making sure we have independent counters on buffer
current.position(0);
output.write(current);
}
//try with resources closes file itself
return file;
} catch (IOException e) {
try {
//delete unfinished file
Files.delete(file);
} catch (IOException deleteFail) {
e.addSuppressed(new IOException("Exception In exception during emergency cleanup of incompletely written procesed file", deleteFail));
}
throw new UnsupportedOperationException("Exception during saving file", e);
}
}
private Path getPath(UUID bufferId, String format) {
String suffix = (Objects.equals(format, INTERNAL_FORMAT))
? ""
: "." + format.toLowerCase();
return storageDir.resolve(bufferId + suffix);
}
}