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.

Marked As Solved

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

Also Liked

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:

krlsdu

krlsdu

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?

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement