krlsdu

krlsdu

Please help me improve my solution to the "Computing GC Content" challenge on the Rosalind site (bioinformatics topic)

Hi guys!
I would like your help to improve my solution.
my test
I resolved the code-challenge, but not is clear.

In my mind this can be much better with other people helping.

First 3 of 3 Posts Switch mode

OvermindDL1

OvermindDL1

Eh, it could be shortened a bit and made a bit more efficient but not really that much more readable either. I’d probably have the actual calculation be something like this though (as it is significantly faster):

iex(1)> dna = "AGCTATAG"
"AGCTATAG"
iex(2)> Enum.reduce(to_charlist(dna), 0, &if(&1==?C or &1==?G, do: &2+1, else: &2))/byte_size(dna)
0.375

Which if wrapped in a case do ... end then the whole thing could be pipelined into just a dozen lines or so. :slight_smile:

mbuhot

mbuhot

Here’s a version with some helper functions split out, using Stream to eliminate intermediate lists, and binary matching to count the G and C characters:

defmodule Gc do
  def gc_content(dataset) do
    {key, gc_percent} =
      dataset
      |> parse_lines()
      |> Stream.map(fn {k, v} -> {k, gc_percent(v)} end)
      |> Enum.max_by(&elem(&1, 1))

    "#{key}\n#{gc_percent}"
  end

  @spec parse_lines(String.t()) :: Enumerable.t()
  def parse_lines(dataset) do
    dataset
    |> String.replace("\n", "")
    |> String.split(">", trim: true)
    |> Stream.map(&String.split_at(&1, 13))
  end

  @spec gc_percent(String.t()) :: float
  def gc_percent(val), do: Float.round(100 * gc_count(val) / String.length(val), 7)

  @spec gc_count(String.t(), integer) :: integer
  def gc_count(val, n \\ 0)
  def gc_count("", n), do: n
  def gc_count("G" <> rest, n), do: gc_count(rest, n + 1)
  def gc_count("C" <> rest, n), do: gc_count(rest, n + 1)
  def gc_count(<<_::utf8>> <> rest, n), do: gc_count(rest, n)
end
krlsdu

krlsdu OP

cool your solution!
Your words is true, your code is unreadable :sweat_smile:
But sometimes performance is more necessary.
Whereas bioinformatics usually works with large files and data, it’s really important.

Thinking about your solution, and looking to improve the readable.
The “if” can turn into functions.

What do you think?

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement