PageRenderTime 32ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/ilmn_pacbio/assembly_stats.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 83 lines | 62 code | 3 blank | 18 comment | 10 complexity | 2af778b1cc55a62e94a7a05b62f39c04 MD5 | raw file
  1. #!/usr/bin/env python
  2. #
  3. #Copyright (c) 2011, Pacific Biosciences of California, Inc.
  4. #
  5. #All rights reserved.
  6. #
  7. #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  8. # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. # * Neither the name of Pacific Biosciences nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  11. #
  12. #THIS SOFTWARE IS PROVIDED BY PACIFIC BIOSCIENCES AND ITS CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PACIFIC BIOSCIENCES OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  14. #DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  15. #LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16. #(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17. #
  18. import sys, os
  19. from optparse import OptionParser
  20. from galaxy import eggs
  21. import pkg_resources
  22. pkg_resources.require( 'bx-python' )
  23. from bx.seq.fasta import FastaReader
  24. def getStats( fastaFile, genomeLength, minContigLength ):
  25. lengths = []
  26. stats = { "Num" : 0,
  27. "Sum" : 0,
  28. "Max" : 0,
  29. "Avg" : 0,
  30. "N50" : 0,
  31. "99%" : 0 }
  32. fasta_reader = FastaReader( open( fastaFile, 'rb' ) )
  33. while True:
  34. seq = fasta_reader.next()
  35. if not seq:
  36. break
  37. if seq.length < minContigLength:
  38. continue
  39. lengths.append( seq.length )
  40. if lengths:
  41. stats[ 'Num' ] = len( lengths )
  42. stats[ 'Sum' ] = sum( lengths )
  43. stats[ 'Max' ] = max( lengths )
  44. stats[ 'Avg' ] = int( sum( lengths ) / float( len( lengths ) ) )
  45. stats[ 'N50' ] = 0
  46. stats[ '99%' ] = 0
  47. if genomeLength == 0:
  48. genomeLength = sum( lengths )
  49. lengths.sort()
  50. lengths.reverse()
  51. lenSum = 0
  52. stats[ "99%" ] = len( lengths )
  53. for idx, length in enumerate( lengths ):
  54. lenSum += length
  55. if ( lenSum > genomeLength / 2 ):
  56. stats[ "N50" ] = length
  57. break
  58. lenSum = 0
  59. for idx, length in enumerate( lengths ):
  60. lenSum += length
  61. if lenSum > genomeLength * 0.99:
  62. stats[ "99%" ] = idx + 1
  63. break
  64. return stats
  65. def __main__():
  66. #Parse Command Line
  67. usage = 'Usage: %prog input output --minContigLength'
  68. parser = OptionParser( usage=usage )
  69. parser.add_option( "--minContigLength", dest="minContigLength", help="Minimum length of contigs to analyze" )
  70. parser.add_option( "--genomeLength", dest="genomeLength", help="Length of genome for which to calculate N50s" )
  71. parser.set_defaults( minContigLength=0, genomeLength=0 )
  72. options, args = parser.parse_args()
  73. input_fasta_file = args[ 0 ]
  74. output_tabular_file = args[ 1 ]
  75. statKeys = "Num Sum Max Avg N50 99%".split( " " )
  76. stats = getStats( input_fasta_file, int( options.genomeLength ), int( options.minContigLength ) )
  77. fout = open( output_tabular_file, "w" )
  78. fout.write( "%s\n" % "\t".join( map( lambda key: str( stats[ key ] ), statKeys ) ) )
  79. fout.close()
  80. if __name__=="__main__": __main__()