ca95211a2d98

RSTR
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 22 Feb 2019 19:32:24 -0500
parents 7052ec0c6e1d
children 474d88a2af2e
branches/tags (none)
files src/problems/prob.lisp src/problems/rstr.lisp src/utils.lisp

Changes

--- a/src/problems/prob.lisp	Fri Feb 22 19:23:17 2019 -0500
+++ b/src/problems/prob.lisp	Fri Feb 22 19:32:24 2019 -0500
@@ -13,17 +13,8 @@
     *output-prob*
   (let ((dna (read-line data))
         (gc-contents (read-all-from-string (read-line data))))
-    (labels
-        ((gcp (base)
-           "Return whether `base` is G or C."
-           (or (char= #\G base)
-               (char= #\C base)))
-         (base-probability (gc-content base)
-           "Return the probability of `base` in DNA with the given `gc-content`."
-           (if (gcp base)
-             (/ gc-content 2)
-             (/ (- 1 gc-content) 2)))
-         (prob (gc-content)
+    (flet
+        ((prob (gc-content)
            (iterate
              (for base :in-string dna)
              (summing (log (base-probability gc-content base) 10)))))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/problems/rstr.lisp	Fri Feb 22 19:32:24 2019 -0500
@@ -0,0 +1,16 @@
+(in-package :rosalind)
+
+(defparameter *input-rstr* "90000 0.6
+ATAGCCGA")
+
+(defparameter *output-rstr* "0.689")
+
+
+(define-problem rstr (data stream)
+    *input-rstr*
+    *output-rstr*
+  (let* ((n (read data))
+         (gc (coerce (read data) 'double-float))
+         (dna (read-line data))
+         (prob (product dna :key (curry #'base-probability gc))))
+    (format nil "~,3F" (- 1 (expt (- 1 prob) n)))))
--- a/src/utils.lisp	Fri Feb 22 19:23:17 2019 -0500
+++ b/src/utils.lisp	Fri Feb 22 19:32:24 2019 -0500
@@ -84,6 +84,18 @@
     ((#\C #\T #\U) 1)))
 
 
+(defun-inline gcp (base)
+  "Return whether `base` is G or C."
+  (or (char= #\G base)
+      (char= #\C base)))
+
+(defun-inline base-probability (gc-content base)
+  "Return the probability of `base` in DNA with the given `gc-content`."
+  (if (gcp base)
+    (/ gc-content 2)
+    (/ (- 1 gc-content) 2)))
+
+
 (defun mapcount (predicate sequence &rest more-sequences)
   "Map `predicate` across sequences, counting satisfactory applications."
   (let ((result 0))