commit ed877fe8be1f8047fe324272f9be65e54441c949
parent 9e8c9d1fa733578e3c10923f490ed9e8548a969b
Author: Walther Chen <walther.chen@gmail.com>
Date: Wed, 23 Oct 2024 15:58:21 -0400
ba1c reverse complement
Diffstat:
A | ba1c.c3 | | | 36 | ++++++++++++++++++++++++++++++++++++ |
1 file changed, 36 insertions(+), 0 deletions(-)
diff --git a/ba1c.c3 b/ba1c.c3
@@ -0,0 +1,36 @@
+// reverse complement
+
+module ba1c;
+
+import std::io;
+import std::io::file;
+import std::collections;
+
+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 pattern = io::treadline(&f)!;
+ io::printfn(reverse_complement(pattern));
+}
+
+fn String reverse_complement(String pattern, Allocator alloc = allocator::heap()) {
+ char[] s = allocator::alloc_array(alloc, char, pattern.len);
+ foreach (i, c : pattern) {
+ ulong idx = s.len - i - 1;
+ switch (c) {
+ case 'A': s[idx] = 'T';
+ case 'C': s[idx] = 'G';
+ case 'T': s[idx] = 'A';
+ case 'G': s[idx] = 'C';
+ default: $$unreachable();
+ }
+ }
+ return (String)s;
+}
+
+fn void test_reverse_complement() @test {
+ assert(reverse_complement("AAAACCCGGT") == "ACCGGGTTTT");
+}