commit 5d8e3bed9cbe43504e62dbc761a012a08b78a079
parent b1f3273fb54e449e21fcba4f628fa1c37a0fff88
Author: Walther Chen <walther.chen@gmail.com>
Date: Sat, 26 Oct 2024 21:44:56 -0400
ba1f skew
Diffstat:
A | ba1f.c3 | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 53 insertions(+), 0 deletions(-)
diff --git a/ba1f.c3 b/ba1f.c3
@@ -0,0 +1,53 @@
+// minimum skew
+
+module ba1f;
+import std::io;
+import std::io::file;
+import std::collections;
+
+import util;
+
+def IntList = List(<ulong>);
+
+fn void! main(String[] args) {
+ if (args.len != 2) {
+ io::eprintn("Please supply path to data file");
+ return IoError.FILE_NOT_FOUND?;
+ }
+ File f = file::open(args[1], "rb")!;
+ String genome = io::treadline(&f)!;
+ ulong[] min_skew_idxs = minimum_skew_idxs(genome);
+ foreach (i : min_skew_idxs) {
+ io::printf("%d ", i);
+ }
+}
+
+// TODO use min heap
+fn ulong[] minimum_skew_idxs(String genome, Allocator alloc = allocator::heap()) {
+ IntList idxs;
+ idxs.temp_init();
+ int min_skew = 0;
+ int skew = 0;
+ foreach (i, base : genome) {
+ // skew[0] is 0, skew[1] is the skew
+ // after genome[0]
+ ulong skew_idx = i + 1;
+ switch (base) {
+ case 'C': skew -= 1;
+ case 'G': skew += 1;
+ }
+ switch {
+ case skew < min_skew:
+ min_skew = skew;
+ idxs.clear();
+ idxs.push(skew_idx);
+ case skew == min_skew:
+ idxs.push(skew_idx);
+ }
+ }
+ return idxs.to_new_array(alloc);
+}
+
+fn void test_minimum_skew_idxs() @test {
+ assert(minimum_skew_idxs("TAAAGACTGCCGAGAGGCCAACACGAGTGCTAGAACGAGGGGCGTAAACGCGGGTCCGAT") == {11, 24});
+}