Mercurial > repos > xuebing > sharplabtool
comparison tools/ilmn_pacbio/assembly_stats.py @ 0:9071e359b9a3
Uploaded
author | xuebing |
---|---|
date | Fri, 09 Mar 2012 19:37:19 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9071e359b9a3 |
---|---|
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 | |
25 def getStats( fastaFile, genomeLength, minContigLength ): | |
26 lengths = [] | |
27 stats = { "Num" : 0, | |
28 "Sum" : 0, | |
29 "Max" : 0, | |
30 "Avg" : 0, | |
31 "N50" : 0, | |
32 "99%" : 0 } | |
33 fasta_reader = FastaReader( open( fastaFile, 'rb' ) ) | |
34 while True: | |
35 seq = fasta_reader.next() | |
36 if not seq: | |
37 break | |
38 if seq.length < minContigLength: | |
39 continue | |
40 lengths.append( seq.length ) | |
41 if lengths: | |
42 stats[ 'Num' ] = len( lengths ) | |
43 stats[ 'Sum' ] = sum( lengths ) | |
44 stats[ 'Max' ] = max( lengths ) | |
45 stats[ 'Avg' ] = int( sum( lengths ) / float( len( lengths ) ) ) | |
46 stats[ 'N50' ] = 0 | |
47 stats[ '99%' ] = 0 | |
48 if genomeLength == 0: | |
49 genomeLength = sum( lengths ) | |
50 lengths.sort() | |
51 lengths.reverse() | |
52 lenSum = 0 | |
53 stats[ "99%" ] = len( lengths ) | |
54 for idx, length in enumerate( lengths ): | |
55 lenSum += length | |
56 if ( lenSum > genomeLength / 2 ): | |
57 stats[ "N50" ] = length | |
58 break | |
59 lenSum = 0 | |
60 for idx, length in enumerate( lengths ): | |
61 lenSum += length | |
62 if lenSum > genomeLength * 0.99: | |
63 stats[ "99%" ] = idx + 1 | |
64 break | |
65 return stats | |
66 | |
67 def __main__(): | |
68 #Parse Command Line | |
69 usage = 'Usage: %prog input output --minContigLength' | |
70 parser = OptionParser( usage=usage ) | |
71 parser.add_option( "--minContigLength", dest="minContigLength", help="Minimum length of contigs to analyze" ) | |
72 parser.add_option( "--genomeLength", dest="genomeLength", help="Length of genome for which to calculate N50s" ) | |
73 parser.set_defaults( minContigLength=0, genomeLength=0 ) | |
74 options, args = parser.parse_args() | |
75 input_fasta_file = args[ 0 ] | |
76 output_tabular_file = args[ 1 ] | |
77 statKeys = "Num Sum Max Avg N50 99%".split( " " ) | |
78 stats = getStats( input_fasta_file, int( options.genomeLength ), int( options.minContigLength ) ) | |
79 fout = open( output_tabular_file, "w" ) | |
80 fout.write( "%s\n" % "\t".join( map( lambda key: str( stats[ key ] ), statKeys ) ) ) | |
81 fout.close() | |
82 | |
83 if __name__=="__main__": __main__() |