Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:08:18+00:00 2026-05-16T00:08:18+00:00

I’m trying to get the mean length of fasta sequences using Erlang . A

  • 0

I’m trying to get the mean length of fasta sequences using Erlang. A fasta file looks like this

>title1
ATGACTAGCTAGCAGCGATCGACCGTCGTACGC
ATCGATCGCATCGATGCTACGATCGATCATATA
ATGACTAGCTAGCAGCGATCGACCGTCGTACGC
ATCGATCGCATCGATGCTACGATCTCGTACGC
>title2
ATCGATCGCATCGATGCTACGATCTCGTACGC
ATGACTAGCTAGCAGCGATCGACCGTCGTACGC
ATCGATCGCATCGATGCTACGATCGATCATATA
ATGACTAGCTAGCAGCGATCGACCGTCGTACGC
>title3
ATCGATCGCATCGAT(...)

I tried to answser this question using the following Erlang code:

-module(golf).
-export([test/0]).

line([],{Sequences,Total}) ->  {Sequences,Total};
line(">" ++ Rest,{Sequences,Total}) -> {Sequences+1,Total};
line(L,{Sequences,Total}) -> {Sequences,Total+string:len(string:strip(L))}.

scanLines(S,Sequences,Total)->
        case io:get_line(S,'') of
            eof -> {Sequences,Total};
            {error,_} ->{Sequences,Total};
            Line -> {S2,T2}=line(Line,{Sequences,Total}), scanLines(S,S2,T2)
        end  .

test()->
    {Sequences,Total}=scanLines(standard_io,0,0),
    io:format("~p\n",[Total/(1.0*Sequences)]),
    halt().

Compilation/Execution:

erlc golf.erl
erl -noshell -s golf test < sequence.fasta
563.16

this code seems to work fine for a small fasta file but it takes hours to parse a larger one (>100Mo). Why ? I’m an Erlang newbie, can you please improve this code ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:08:18+00:00Added an answer on May 16, 2026 at 12:08 am

    If you need really fast IO then you have to do little bit more trickery than usual.

    -module(g).
    -export([s/0]).
    s()->
      P = open_port({fd, 0, 1}, [in, binary, {line, 256}]),
      r(P, 0, 0),
      halt().
    r(P, C, L) ->
      receive
        {P, {data, {eol, <<$>:8, _/binary>>}}} ->
          r(P, C+1, L);
        {P, {data, {eol, Line}}} ->
          r(P, C, L + size(Line));
        {'EXIT', P, normal} ->
          io:format("~p~n",[L/C])
      end.
    

    It is fastest IO as I know but note -noshell -noinput.
    Compile just like erlc +native +"{hipe, [o3]}" g.erl but with -smp disable

    erl -smp disable -noinput -mode minimal -boot start_clean -s erl_compile compile_cmdline @cwd /home/hynek/Download @option native @option '{hipe, [o3]}' @files g.erl
    

    and run:

    time erl -smp disable -noshell -mode minimal -boot start_clean -noinput -s g s < uniprot_sprot.fasta
    352.6697028442464
    
    real    0m3.241s
    user    0m3.060s
    sys     0m0.124s
    

    With -smp enable but native it takes:

    $ erlc +native +"{hipe, [o3]}" g.erl
    $ time erl -noshell -mode minimal -boot start_clean -noinput -s g s<uniprot_sprot.fasta
    352.6697028442464
    
    real    0m5.103s
    user    0m4.944s
    sys     0m0.112s
    

    Byte code but with -smp disable (almost in par with native because most of work is done in port!):

    $ erlc g.erl
    $ time erl -smp disable -noshell -mode minimal -boot start_clean -noinput -s g s<uniprot_sprot.fasta
    352.6697028442464
    
    real    0m3.565s
    user    0m3.436s
    sys     0m0.104s
    

    Just for completeness byte code with smp:

    $ time erl -noshell -mode minimal -boot start_clean -noinput -s g s<uniprot_sprot.fasta 
    352.6697028442464
    
    real    0m5.433s
    user    0m5.236s
    sys     0m0.128s
    

    For comparison sarnold version gives me wrong answer and takes more on same HW:

    $ erl -smp disable -noinput -mode minimal -boot start_clean -s erl_compile compile_cmdline @cwd /home/hynek/Download @option native @option '{hipe, [o3]}' @files golf.erl
    ./golf.erl:5: Warning: variable 'Rest' is unused
    $ time erl -smp disable -noshell -mode minimal -s golf test
    359.04679841439776
    
    real    0m17.569s
    user    0m16.749s
    sys     0m0.664s
    

    EDIT: I have looked at characteristics of uniprot_sprot.fasta and I’m little bit surprised. It is 3824397 rows and 232MB. It means that -smp disabled version can handle 1.18 million text lines per second (71MB/s in line oriented IO).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.