Unconfigured Ad

Collapse
X
 
  • Filter
  • 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)
                
              sum(numlist)
                
              limit 0.5
                
              for l in numlist:
                  
              -= l
                  
              if <= limit:
                    return 

              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
                    Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                    by SEQadmin2



                    Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                    ...
                    Yesterday, 11:10 AM
                  • SEQadmin2
                    Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                    by SEQadmin2



                    Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                    There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                    07-08-2026, 05:17 AM
                  • GATTACAT
                    Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                    by GATTACAT
                    Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                    07-01-2026, 11:43 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, Yesterday, 10:04 AM
                  0 responses
                  10 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-08-2026, 10:08 AM
                  0 responses
                  7 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-07-2026, 11:05 AM
                  0 responses
                  16 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-02-2026, 11:08 AM
                  0 responses
                  31 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...