nucleotide_count
This commit is contained in:
30
elixir/nucleotide-count/lib/nucleotide_count.ex
Normal file
30
elixir/nucleotide-count/lib/nucleotide_count.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule NucleotideCount do
|
||||
@nucleotides [?A, ?C, ?G, ?T]
|
||||
|
||||
@doc """
|
||||
Counts individual nucleotides in a DNA strand.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NucleotideCount.count(~c"AATAA", ?A)
|
||||
4
|
||||
|
||||
iex> NucleotideCount.count(~c"AATAA", ?T)
|
||||
1
|
||||
"""
|
||||
@spec count(charlist(), char()) :: non_neg_integer()
|
||||
def count(strand, nucleotide), do: Enum.count(strand, &(&1 == nucleotide))
|
||||
|
||||
@doc """
|
||||
Returns a summary of counts by nucleotide.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NucleotideCount.histogram(~c"AATAA")
|
||||
%{?A => 4, ?T => 1, ?C => 0, ?G => 0}
|
||||
"""
|
||||
@spec histogram(charlist()) :: map()
|
||||
def histogram(strand) do
|
||||
for nucleotide <- @nucleotides, into: %{}, do: {nucleotide, count(strand, nucleotide)}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user