Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sklages
    Senior Member
    • May 2008
    • 628

    #16
    The perl stuff is faster, at least if you have a few 100,000 of contigs

    Sven

    Comment

    • seb567
      Senior Member
      • Jul 2008
      • 260

      #17
      There is a program called getN50 in the amos package.



      Comment

      • flxlex
        Moderator
        • Nov 2008
        • 412

        #18
        A single awk, no pipes (except between sort and awk), and thereby somewhat shorter. Note sort -n, not sort -rn

        sort -n contig_lengths.txt | awk '{len[i++]=$1;sum+=$1} END {for (j=0;j<i+1;j++) {csum+=len[j]; if (csum>sum/2) {print len[j];break}}}'

        Comment

        • eslondon
          Member
          • Jul 2009
          • 21

          #19
          Perl clearly 10 times faster...

          macbook:~$ time perl -ne 'chomp(); push(@contigs,$_);$total+=$_;END{foreach(sort{$b<=>$a}@contigs){$sum+=$_;$L=$_;if($sum>=$total*0.5){print "TOTAL: $total\nN50 : $L\n";exit;} ;}}' contigs_100.lines
          TOTAL: 59789620
          N50 : 212

          real 0m0.444s
          user 0m0.348s
          sys 0m0.038s
          macbook:~ $ time sort -n contigs_100.lines | awk '{len[i++]=$1;sum+=$1} END {for (j=0;j<i+1;j++) {csum+=len[j]; if (csum>sum/2) {print len[j];break}}}'
          212

          real 0m5.502s
          user 0m4.055s
          sys 0m0.560s
          --------------------------------------
          Elia Stupka
          Co-Director and Head of Unit
          Center for Translational Genomics and Bioinformatics
          San Raffaele Scientific Institute
          Via Olgettina 58
          20132 Milano
          Italy
          ---------------------------------------

          Comment

          • flxlex
            Moderator
            • Nov 2008
            • 412

            #20
            Cool. Well, consider it an exercise in awk rather than an attempt to beat perl...

            Comment

            • vyahhi
              Junior Member
              • Feb 2011
              • 4

              #21
              maubp's code on Python requires large amount of memory and CPU if numbers in the list are huge (it creates X copies of every number X).

              I suggests to use this faster function in Python for calculating N50 based on this definition http://seqanswers.com/forums/showpos...6&postcount=4:

              PHP Code:
              def N50(numlist):
                
              """
                Abstract: Returns the N50 value of the passed list of numbers.
                Usage: N50(numlist)

                Based on the definition from this SEQanswers post
                http://seqanswers.com/forums/showpost.php?p=7496&postcount=4
                (modified Broad Institute's definition
                https://www.broad.harvard.edu/crd/wiki/index.php/N50)
                
                See SEQanswers threads for details:
                http://seqanswers.com/forums/showthread.php?t=2857
                http://seqanswers.com/forums/showthread.php?t=2332
                """
                
              numlist.sort(reverse = True)
                
              s = sum(numlist)
                
              limit = s * 0.5
                
              for l in numlist:
                  
              s -= l
                  
              if s <= limit:
                    return 
              l 
              Originally posted by maubp View Post
              OK - so the stdin is one integer per line. How about a python script like this,
              see also http://seqanswers.com/forums/showthread.php?t=2332

              Code:
              #!/usr/bin/python
              import sys
              
              def N50(numlist):
                  """
                  Abstract: Returns the N50 value of the passed list of numbers. 
                  Usage:    N50(numlist)
              
                  Based on the Broad Institute definition:
                  https://www.broad.harvard.edu/crd/wiki/index.php/N50
                  """
                  numlist.sort()
                  newlist = []
                  for x in numlist :
                      newlist += [x]*x
                  # take the mean of the two middle elements if there are an even number
                  # of elements.  otherwise, take the middle element
                  if len(newlist) % 2 == 0:
                      medianpos = len(newlist)/2  
                      return float(newlist[medianpos] + newlist[medianpos-1]) /2
                  else:
                      medianpos = len(newlist)/2
                      return newlist[medianpos]
              
              assert N50([2, 2, 2, 3, 3, 4, 8, 8]) == 6
              
              lengths = []
              for line in sys.stdin :
                  lengths.append(int(line))
              print N50(lengths)
              Then at the Unix command line, you could use it like this:
              Code:
              $ grep "^>" 454AllContigs.fna | cut -d"=" -f2 | cut -d" " -f1 | ./stdin_N50.py 
              386

              Comment

              • ebioman
                Member
                • Aug 2013
                • 41

                #22
                Originally posted by flxlex View Post
                A single awk, no pipes (except between sort and awk), and thereby somewhat shorter. Note sort -n, not sort -rn

                sort -n contig_lengths.txt | awk '{len[i++]=$1;sum+=$1} END {for (j=0;j<i+1;j++) {csum+=len[j]; if (csum>sum/2) {print len[j];break}}}'

                So most scripts here actually take the scaffold length at which the sum is bigger than half the genome size. But should it not be equal AND/OR bigger ?


                The N50 of an assembly is a weighted median of the lengths of the sequences it contains, equal to the length of the longest sequence s, such that the sum of the lengths of sequences greater than or equal in length to s is greater than or equal to half the length of the genome being assembled.
                from the Assemblathon paper

                Comment

                • flxlex
                  Moderator
                  • Nov 2008
                  • 412

                  #23
                  Originally posted by ebioman View Post
                  But should it not be equal AND/OR bigger?
                  Hehe, you're right, that is a mistake. The correct version is

                  Code:
                  sort -n contig_lengths.txt | awk '{len[i++]=$1;sum+=$1} END {for (j=0;j<i+1;j++) {csum+=len[j]; if (csum>=sum/2) {print len[j];break}}}'

                  Comment

                  Latest Articles

                  Collapse

                  • SEQadmin2
                    Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                    by SEQadmin2



                    Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                    Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                    ...
                    10-14-2026, 09:55 AM
                  • SEQadmin2
                    New Genomics Technologies Take Aim at Long-Standing Limits
                    by SEQadmin2


                    Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                    We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                    ...
                    09-28-2026, 10:25 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, Yesterday, 11:13 AM
                  0 responses
                  16 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, Yesterday, 10:48 AM
                  0 responses
                  15 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-15-2026, 10:52 AM
                  0 responses
                  35 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-14-2026, 05:39 AM
                  0 responses
                  42 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...