-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy path1-m.d
209 lines (184 loc) · 4.5 KB
/
1-m.d
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
// port from 8-m.rs and 1.zig
import std;
import std.outbuffer : OutBuffer;
alias Map = uint[Nullable!Code];
static double hundreed = 100.0;
static struct Code
{
ulong data;
void push(ubyte c, ulong mask)
{
data = ((data << 2) | cast(ulong) c) & mask;
}
static Nullable!Code fromStr(ubyte[] s)
{
auto mask = Code.makeMask(s.length);
auto res = Code(0);
foreach (c; s)
res.push(Code.encodeByte(c), mask);
return nullable(res);
}
string toStr(size_t frame)
{
char[] res;
auto code = this.data;
ubyte c;
foreach (_; 0 .. frame)
{
switch (cast(ubyte) code & 0b11)
{
case Code.encodeByte('A'):
c = 'A';
break;
case Code.encodeByte('C'):
c = 'C';
break;
case Code.encodeByte('G'):
c = 'G';
break;
case Code.encodeByte('T'):
c = 'T';
break;
default:
break;
}
res ~= c;
code >>= 2;
}
return cast(string) res.reverse;
}
pragma(inline, true)
static ulong makeMask(size_t frame)
{
return (1L << (2 * frame)) - 1L;
}
pragma(inline, true)
static ubyte encodeByte(ubyte c)
{
return (c >> 1) & 0b11;
}
}
static struct CodeRange
{
size_t i = 0;
ubyte[] input;
Nullable!Code code;
ulong mask;
bool empty() {
return this.i >= this.input.length;
}
Nullable!Code front() {
const c = this.input[this.i];
this.code.get.push(c, this.mask);
return this.code;
}
void popFront() {
this.i += 1;
}
this(ubyte[] input, size_t frame)
{
const mask = Code.makeMask(frame);
Nullable!Code tmpCode = Code(0);
foreach (c; input[0 .. frame - 1])
tmpCode.get.push(c, mask);
this.mask = mask;
this.code = tmpCode;
this.input = input[frame - 1 .. $];
}
}
Map genMap(Tuple!(ubyte[], size_t) t)
{
Map myMap;
foreach(code; CodeRange(t[0], t[1]))
{
myMap.update(code,
() => 1,
(ref uint v) { v += 1; });
}
return myMap;
}
struct CountCode
{
ulong count;
Nullable!Code code;
}
void printMap(size_t self, Map myMap, ref OutBuffer buf)
{
CountCode[] v;
ulong total;
uint count;
foreach (pair; myMap.byPair)
{
total += pair.value;
v ~= CountCode(pair.value, pair.key);
}
alias asc = (a, b) =>
a.count < b.count ||
(a.count == b.count && b.code.get.data < a.code.get.data);
v.sort!(asc);
foreach (i; iota(cast(int)(v.length) - 1, -1, -1))
{
auto cc = v[i];
buf.writefln("%s %.3f", cc.code.get.toStr(self), cast(double) cc.count / cast(
double) total * hundreed);
}
buf.write("\n");
}
void printOcc(ubyte[] s, ref Map myMap, ref OutBuffer buf)
{
auto tmp = Code.fromStr(s);
buf.writefln("%d\t%s", myMap.get(tmp, 0), cast(string) s);
}
ubyte[] readInput(string[] args)
{
immutable fileName = args.length > 1 ? args[1] : "25000_in";
char key = '>';
ubyte[] res;
auto app = appender(&res);
app.reserve(2_500_120);
auto file = File(args[1]);
byte x = 3;
foreach (line; file.byLine())
{
if (line[0] == key)
x--;
else
continue;
if (x == 0)
break;
}
foreach (line; file.byChunk(61))
{
app ~= line[0..$-1].map!(a => Code.encodeByte(a));
}
return res;
}
void main(string[] args)
{
auto buf1 = new OutBuffer();
auto buf2 = new OutBuffer();
static ubyte[][5] occs = [
cast(ubyte[]) "GGTATTTTAATTTATAGT",
cast(ubyte[]) "GGTATTTTAATT",
cast(ubyte[]) "GGTATT",
cast(ubyte[]) "GGTA",
cast(ubyte[]) "GGT",
];
auto input = readInput(args);
alias myTaskType = Task!(run, uint[Nullable!(Code)]function(Tuple!(ubyte[], ulong)), Tuple!(ubyte[], ulong))*;
myTaskType[] calls;
foreach (i; 0 .. occs.length)
{
auto t = task(&genMap, tuple(input, occs[i].length));
t.executeInNewThread();
calls ~= t;
}
printMap(1, genMap(tuple(input, 1UL)), buf1);
printMap(2, genMap(tuple(input, 2UL)), buf1);
foreach (i; iota(4, -1, -1))
{
printOcc(occs[i], calls[i].yieldForce(), buf2);
}
write(buf1);
write(buf2);
}