|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const gpa = std.heap.c_allocator; |
| 4 | +const stdout = std.io.getStdOut().writer(); |
| 5 | + |
| 6 | +const Code = struct { |
| 7 | + data: u64, |
| 8 | + |
| 9 | + pub inline fn encodeByte(c: u8) u2 { |
| 10 | + return @intCast((c >> 1) & 0b11); |
| 11 | + } |
| 12 | + |
| 13 | + pub inline fn makeMask(frame: usize) u64 { |
| 14 | + return (@as(u64, 1) << @as(u6, @intCast(2 * frame))) - 1; |
| 15 | + } |
| 16 | + |
| 17 | + pub inline fn push(self: *Code, c: u8, mask: u64) void { |
| 18 | + self.data = ((self.data << 2) | c) & mask; |
| 19 | + } |
| 20 | + |
| 21 | + pub fn fromStr(s: []const u8) Code { |
| 22 | + const mask = Code.makeMask(s.len); |
| 23 | + var res = Code{ .data = 0 }; |
| 24 | + for (s) |c| { |
| 25 | + res.push(Code.encodeByte(c), mask); |
| 26 | + } |
| 27 | + return res; |
| 28 | + } |
| 29 | + |
| 30 | + pub fn toString(self: Code, frame: usize) ![]const u8 { |
| 31 | + var result = std.ArrayList(u8).init(gpa); |
| 32 | + var code = self.data; |
| 33 | + for (0..frame) |_| { |
| 34 | + const c: u8 = switch (@as(u2, @truncate(code))) { |
| 35 | + Code.encodeByte('A') => 'A', |
| 36 | + Code.encodeByte('T') => 'T', |
| 37 | + Code.encodeByte('G') => 'G', |
| 38 | + Code.encodeByte('C') => 'C', |
| 39 | + }; |
| 40 | + try result.append(c); |
| 41 | + code >>= 2; |
| 42 | + } |
| 43 | + std.mem.reverse(u8, result.items); |
| 44 | + return result.toOwnedSlice(); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +pub fn readInput() ![]const u8 { |
| 49 | + const args = try std.process.argsAlloc(gpa); |
| 50 | + defer std.process.argsFree(gpa, args); |
| 51 | + const file_name = if (args.len > 1) args[1] else "25000_in"; |
| 52 | + const file = try std.fs.cwd().openFile(file_name, .{}); |
| 53 | + var buffered_reader = std.io.bufferedReader(file.reader()); |
| 54 | + const reader = buffered_reader.reader(); |
| 55 | + { // skip past first lines starting with '>' |
| 56 | + var i: u8 = 0; |
| 57 | + while (i < 3) : (i += 1) { |
| 58 | + while (true) { |
| 59 | + const c = try reader.readByte(); |
| 60 | + if (c == '>') break; |
| 61 | + } |
| 62 | + } |
| 63 | + while (true) { |
| 64 | + const c = try reader.readByte(); |
| 65 | + if (c == '\n') break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + var buf = try reader.readAllAlloc(gpa, std.math.maxInt(u32)); |
| 70 | + // In place, remove all newlines from buf and encode nucleotides |
| 71 | + // using only the last 2 bits in each byte. |
| 72 | + { |
| 73 | + var i: usize = 0; |
| 74 | + for (buf) |c| { |
| 75 | + if (c != '\n') { |
| 76 | + // Gives a -> 0x00, c -> 0x01, g -> 0x03, t -> 0x02 |
| 77 | + buf[i] = (c >> 1) & 0x03; |
| 78 | + i += 1; |
| 79 | + } |
| 80 | + } |
| 81 | + buf.len = i; |
| 82 | + } |
| 83 | + return buf; |
| 84 | +} |
| 85 | + |
| 86 | +const CodeContext = struct { |
| 87 | + pub fn eql(_: CodeContext, a: Code, b: Code) bool { return a.data == b.data; } |
| 88 | + pub fn hash(_: CodeContext, c: Code) u64 { return c.data ^ (c.data >> 7); } |
| 89 | +}; |
| 90 | + |
| 91 | +const Map = std.HashMapUnmanaged(Code, u32, CodeContext, 45); |
| 92 | + |
| 93 | +fn genMap(taskIndex: usize, from: usize, to: usize, seq: []const u8, frame: usize, maps: []Map) !void { |
| 94 | + const map = &maps[taskIndex]; |
| 95 | + const mask = Code.makeMask(frame); |
| 96 | + var code = Code.fromStr(seq[from..][0..frame - 1]); |
| 97 | + for (seq[frame - 1..][from .. to]) |e| { |
| 98 | + code.push(e, mask); |
| 99 | + const kv = try map.getOrPutValue(gpa, code, 0); |
| 100 | + kv.value_ptr.* += 1; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +const CountCode = struct { |
| 105 | + count: u32, |
| 106 | + code: Code, |
| 107 | + |
| 108 | + pub fn dsc(_: void, a: CountCode, b: CountCode) bool { |
| 109 | + const order = std.math.order(a.count, b.count); |
| 110 | + return order == .gt or (order == .eq and b.code.data > a.code.data); |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +fn printMap(frame: usize, maps: []const Map) !void { |
| 115 | + const code_limit = 16; |
| 116 | + var counts: [code_limit]u32 = @splat(0); |
| 117 | + var total: u64 = 0; |
| 118 | + for (maps) |map| { |
| 119 | + var iter = map.iterator(); |
| 120 | + while (iter.next()) |it| { |
| 121 | + const code = it.key_ptr.*; |
| 122 | + const count = it.value_ptr.*; |
| 123 | + total += count; |
| 124 | + counts[code.data] += count; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + var cc: std.BoundedArray(CountCode, code_limit) = .{}; |
| 129 | + for (counts, 0..) |count, code_data| if (count > 0) { |
| 130 | + cc.appendAssumeCapacity(.{ .count = count, .code = .{ .data = @intCast(code_data) } }); |
| 131 | + }; |
| 132 | + std.mem.sort(CountCode, cc.slice(), {}, CountCode.dsc); |
| 133 | + |
| 134 | + for (cc.slice()) |c| { |
| 135 | + try stdout.print("{!s} {d:.3}\n", .{ |
| 136 | + c.code.toString(frame), |
| 137 | + @as(f32, @floatFromInt(c.count)) / @as(f32, @floatFromInt(total)) * 100.0, |
| 138 | + }); |
| 139 | + } |
| 140 | + try stdout.print("\n", .{}); |
| 141 | +} |
| 142 | + |
| 143 | +fn printOcc(occ: []const u8, maps: []const Map) !void { |
| 144 | + const code = Code.fromStr(occ); |
| 145 | + var total: u32 = 0; |
| 146 | + for (maps) |m| { |
| 147 | + if (m.get(code)) |count| total += count; |
| 148 | + } |
| 149 | + try stdout.print("{}\t{s}\n", .{ total, occ }); |
| 150 | +} |
| 151 | + |
| 152 | +fn runInParallel(task_count: usize, len: usize, comptime f: anytype, args: anytype) !void { |
| 153 | + const tasks = try gpa.alloc(std.Thread, task_count - 1); |
| 154 | + defer gpa.free(tasks); |
| 155 | + const len_per_task = @divTrunc(len, task_count); |
| 156 | + for (tasks, 0..) |*task, i| { |
| 157 | + const first = len_per_task * i; |
| 158 | + const last = first + len_per_task; |
| 159 | + task.* = try std.Thread.spawn(.{}, f, .{ i, first, last } ++ args); |
| 160 | + } |
| 161 | + try @call(.auto, f, .{ tasks.len, tasks.len * len_per_task, len } ++ args); |
| 162 | + for (tasks) |*task| task.join(); |
| 163 | +} |
| 164 | + |
| 165 | +fn genMaps(seq: []const u8, frame: usize, maps: []Map) !void { |
| 166 | + for (maps) |*m| m.clearAndFree(gpa); |
| 167 | + try runInParallel(maps.len, seq.len - (frame - 1), genMap, .{seq, frame, maps}); |
| 168 | +} |
| 169 | + |
| 170 | +pub fn main() !void { |
| 171 | + const seq = try readInput(); |
| 172 | + const task_count = try std.Thread.getCpuCount(); |
| 173 | + const maps = try gpa.alloc(Map, task_count); |
| 174 | + defer gpa.free(maps); |
| 175 | + @memset(maps, .empty); |
| 176 | + |
| 177 | + try genMaps(seq, 1, maps); |
| 178 | + try printMap(1, maps); |
| 179 | + try genMaps(seq, 2, maps); |
| 180 | + try printMap(2, maps); |
| 181 | + |
| 182 | + const occs = [_][]const u8{ |
| 183 | + "GGT", |
| 184 | + "GGTA", |
| 185 | + "GGTATT", |
| 186 | + "GGTATTTTAATT", |
| 187 | + "GGTATTTTAATTTATAGT", |
| 188 | + }; |
| 189 | + for (occs) |occ| { |
| 190 | + try genMaps(seq, occ.len, maps); |
| 191 | + try printOcc(occ, maps); |
| 192 | + } |
| 193 | +} |
0 commit comments