-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexredux_3.java
78 lines (62 loc) · 2.67 KB
/
regexredux_3.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
/*
The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Francois Green
*/
import java.io.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.Map.Entry;
import java.util.function.*;
import java.util.regex.*;
import static java.util.stream.Collectors.*;
public class regexredux_3 {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
{
byte[] buf = new byte[65536];
int count;
while ((count = System.in.read(buf)) > 0) {
baos.write(buf, 0, count);
}
}
final String input = baos.toString("US-ASCII");
final int initialLength = input.length();
final String sequence = input.replaceAll(">.*\n|\n", "");
CompletableFuture<String> replacements = CompletableFuture.supplyAsync(() -> {
final Map<String, String> iub = new LinkedHashMap<>();
iub.put("tHa[Nt]", "<4>");
iub.put("aND|caN|Ha[DS]|WaS", "<3>");
iub.put("a[NSt]|BY", "<2>");
iub.put("<[^>]*>", "|");
iub.put("\\|[^|][^|]*\\|", "-");
String buffer = sequence;
for (Map.Entry<String, String> entry : iub.entrySet()) {
buffer = Pattern.compile(entry.getKey()).matcher(buffer).replaceAll(entry.getValue());
}
return buffer;
});
final int codeLength = sequence.length();
final List<String> variants = Arrays.asList("agggtaaa|tttaccct",
"[cgt]gggtaaa|tttaccc[acg]",
"a[act]ggtaaa|tttacc[agt]t",
"ag[act]gtaaa|tttac[agt]ct",
"agg[act]taaa|ttta[agt]cct",
"aggg[acg]aaa|ttt[cgt]ccct",
"agggt[cgt]aa|tt[acg]accct",
"agggta[cgt]a|t[acg]taccct",
"agggtaa[cgt]|[acg]ttaccct");
BiFunction<String, String, Entry<String, Long>> counts = (v, s) -> {
Long count = Pattern.compile(v).splitAsStream(s).count() - 1; //Off by one
return new AbstractMap.SimpleEntry<>(v, count);
};
final Map<String, Long> results = variants.parallelStream()
.map(variant -> counts.apply(variant, sequence))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
variants.forEach(variant -> System.out.println(variant + " " + results.get(variant)));
System.out.println();
System.out.println(initialLength);
System.out.println(codeLength);
System.out.println(replacements.join().length());
}
}