-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknucleotide.java
More file actions
229 lines (205 loc) · 8.15 KB
/
knucleotide.java
File metadata and controls
229 lines (205 loc) · 8.15 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) 2011. Peter Lawrey
*
* "THE BEER-WARE LICENSE" (Revision 128)
* As long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy me a beer in return
* There is no warranty.
*/
/* The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
contributed by Peter Lawrey
*/
import java.io.FileInputStream;
import java.lang.management.ManagementFactory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.concurrent.*;
public class knucleotide {
static final String ATCG = "ATCG";
static final int A = 0, T = 1, C = 2, G = 3;
static final int LONGEST_SEARCH = 18;
public static final int WARMUP = LONGEST_SEARCH + 2;
static final long MASK18 = (1L << (2 * LONGEST_SEARCH)) - 1;
static byte[] values = new byte[256]; static {
Arrays.fill(values, (byte) -1);
values['A'] = values['a'] = A;
values['T'] = values['t'] = T;
values['C'] = values['c'] = C;
values['G'] = values['g'] = G;
}
private static long encode(String code) {
long l = 0;
for (int i = 0; i < code.length(); i++)
l = (l << 2) | values[code.charAt(i)];
return l;
}
static final long GGT = encode("GGT");
static final long GGTA = encode("GGTA");
static final long GGTATT = encode("GGTATT");
static final long GGTATTTTAATT = encode("GGTATTTTAATT");
static final long GGTATTTTAATTTATAGT = encode("GGTATTTTAATTTATAGT");
static int nThreads = Runtime.getRuntime().availableProcessors();
public static void main(String... args) throws Exception {
String processId = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
FileInputStream in = new FileInputStream("/proc/" + processId + "/fd/0");
ExecutorService es = Executors.newFixedThreadPool(nThreads - 1);
FileChannel fc = in.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
int startFrom = findStartOfThree(bb, es, nThreads);
bb.position(startFrom);
int blockSize = (bb.remaining() + nThreads - 1) / nThreads;
for (int i = 0; i < nThreads; i++) {
int min = startFrom + i * blockSize;
int max = min + blockSize;
bb.limit(Math.min(max, bb.capacity()));
bb.position(min - WARMUP);
final ByteBuffer bb3 = bb.slice().order(ByteOrder.nativeOrder());
final boolean warmup = i > 0;
final Runnable task = new Runnable() {
@Override
public void run() {
new Results().process(bb3, 0, bb3.limit(), warmup);
}
};
if (i < nThreads - 1)
es.submit(task);
else
task.run();
}
in.close();
es.shutdown();
es.awaitTermination(1, TimeUnit.MINUTES);
Results.report((char) bb.get(startFrom));
}
private static int findStartOfThree(ByteBuffer bb, ExecutorService es, int nThreads) throws InterruptedException {
final ArrayBlockingQueue<Integer> startOfThree = new ArrayBlockingQueue<Integer>(1);
int blockSize = bb.remaining() / nThreads;
for (int i = 0; i < nThreads; i++) {
final int min = i * blockSize;
final int max = min + blockSize;
final FindThreeRunnable task = new FindThreeRunnable(bb, startOfThree, min, max);
if (i == nThreads - 1)
task.run();
else
es.submit(task);
}
return startOfThree.take();
}
static class Results {
static final int LEN = 256 * 1024;
static final int KEY_SIZE = 2 * LONGEST_SEARCH;
static final long COUNT_BASE = 1L << KEY_SIZE;
static final long KEY_MASK = COUNT_BASE - 1;
final long[] keyValues = new long[LEN];
static final Set<Results> ALL_RESULTS = new CopyOnWriteArraySet<Results>();
Results() {
ALL_RESULTS.add(this);
}
void increment(long id) {
if (!tryIncrement(id, id)) return;
for (int i = 1; i < LEN; i++)
if (!tryIncrement(id, id + i)) return;
throw new AssertionError(id);
}
private boolean tryIncrement(long id, long id2) {
int hash = (int) ((id2 + (id2 >>> 17)) & (LEN - 1));
long key = keyValues[hash];
if (key == 0) {
keyValues[hash] = id | COUNT_BASE;
} else if ((key & KEY_MASK) == id) {
keyValues[hash] += COUNT_BASE;
} else {
return true;
}
return false;
}
public static void report(char firstLetter) {
int[] count1s = new int[4];
int[] count2s = new int[4 * 4];
int[] ggtCounts = new int[5];
for (Results results : ALL_RESULTS)
for (long key1 : results.keyValues) {
if (key1 == 0) continue;
final long key = key1 & KEY_MASK;
final int value = (int) (key1 >>> KEY_SIZE);
count1s[((int) (key & ((1 << 2 * 1) - 1)))] += value;
count2s[((int) (key & ((1 << 2 * 2) - 1)))] += value;
if ((key & ((1 << 2 * 3) - 1)) == GGT) ggtCounts[0] += value;
if ((key & ((1 << 2 * 4) - 1)) == GGTA) ggtCounts[1] += value;
if ((key & ((1 << 2 * 6) - 1)) == GGTATT) ggtCounts[2] += value;
if ((key & ((1 << 2 * 12) - 1)) == GGTATTTTAATT) ggtCounts[3] += value;
if (key == GGTATTTTAATTTATAGT) ggtCounts[4] += value;
}
// first letter is counted incorrectly as a pair when there was no previous letter.
count2s[((int) encode("A" + firstLetter))]--;
long sum = 0;
SortedMap<Integer, Integer> singles = new TreeMap<Integer, Integer>(Collections.<Object>reverseOrder());
for (int i = 0, count1sLength = count1s.length; i < count1sLength; i++) {
sum += count1s[i];
singles.put(count1s[i], i);
}
for (Map.Entry<Integer, Integer> entry : singles.entrySet())
System.out.printf("" + ATCG.charAt(entry.getValue()) + " %5.3f%n", 100.0 * entry.getKey() / sum);
System.out.println();
SortedMap<Integer, Integer> pairs = new TreeMap<Integer, Integer>(Collections.<Object>reverseOrder());
for (int i = 0, count2sLength = count2s.length; i < count2sLength; i++)
pairs.put(count2s[i], i);
for (Map.Entry<Integer, Integer> entry : pairs.entrySet())
System.out.printf("" + ATCG.charAt(entry.getValue() / 4) + ATCG.charAt(entry.getValue() % 4) + " %5.3f%n", 100.0 * entry.getKey() / sum);
System.out.println();
String[] names = "GGT GGTA GGTATT GGTATTTTAATT GGTATTTTAATTTATAGT".split(" ");
for (int i = 0; i < ggtCounts.length; i++)
System.out.printf("%d\t%s%n", ggtCounts[i], names[i]);
}
public void process(ByteBuffer bytes, int start, int end, boolean warmup) {
long l = 0;
if (warmup)
for (int i = start; i < start + WARMUP; i++) {
int b = values[bytes.get(i)];
if (b < 0) continue;
l = (l << 2) | b;
}
for (int i = start + WARMUP; i < end; i++) {
int b = values[bytes.get(i)];
if (b < 0) continue;
l = (l << 2) | b;
increment(l & MASK18);
}
}
}
static class FindThreeRunnable implements Runnable {
static final int THREE_L = ('>' << 24) | ('T' << 16) | ('H' << 8) | ('R' << 0);
static final int THREE_B = ('>' << 0) | ('T' << 8) | ('H' << 16) | ('R' << 24);
static volatile boolean found = false;
private final ByteBuffer bb;
private final BlockingQueue<Integer> startOfThree;
private final int min;
private final int max;
public FindThreeRunnable(ByteBuffer bb, BlockingQueue<Integer> startOfThree, int min, int max) {
this.bb = bb;
this.startOfThree = startOfThree;
this.min = min;
this.max = max;
}
@Override
public void run() {
try {
for (int i = min; i < max && !found; i++) {
switch (bb.getInt(i)) {
case THREE_B:
case THREE_L:
while (bb.get(i++) != '\n') ;
startOfThree.add(i);
found = true;
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}