# 4d47's solution

## to Nucleotide Count in the Clojure Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.

The genetic language of every living thing on the planet is DNA. DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.

Here is an analogy:

• twigs are to birds nests as
• nucleotides are to DNA as
• legos are to lego houses as
• words are to sentences as...

## Source

The Calculating DNA Nucleotides_problem at Rosalind http://rosalind.info/problems/dna/

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### nucleotide_count_test.clj

``````(ns nucleotide-count-test
(:require [clojure.test :refer [deftest is]]
nucleotide-count))

(is (= 0 (nucleotide-count/count \A, ""))))

(deftest empty-dna-strand-has-no-nucleotides
(is (= {\A 0, \T 0, \C 0, \G 0}
(nucleotide-count/nucleotide-counts ""))))

(deftest repetitive-cytidine-gets-counted
(is (= 5 (nucleotide-count/count \C "CCCCC"))))

(deftest repetitive-sequence-has-only-guanosine
(is (= {\A 0, \T 0, \C 0, \G 8}
(nucleotide-count/nucleotide-counts "GGGGGGGG"))))

(deftest counts-only-thymidine
(is (= 1 (nucleotide-count/count \T "GGGGGTAACCCGG"))))

(deftest validates-nucleotides
(is (thrown? Throwable (nucleotide-count/count \X "GACT"))))

(deftest counts-all-nucleotides
(let [s "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"]
(is (= {\A 20, \T 21, \G 17, \C 12}
(nucleotide-count/nucleotide-counts s)))))``````
``````(ns nucleotide-count
(:refer-clojure :exclude [count]))

(def nucleotide-counts frequencies)

(defn count [c dna]
{:pre [(#{\A \T \C \G} c)]}
(get (nucleotide-counts dna) c 0))``````