-
Notifications
You must be signed in to change notification settings - Fork 296
/
readstats.py
executable file
·99 lines (77 loc) · 2.99 KB
/
readstats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /usr/bin/env python2
#
# This file is part of khmer, http://github.com/ged-lab/khmer/, and is
# Copyright (C) Michigan State University, 2009-2013. It is licensed under
# the three-clause BSD license; see doc/LICENSE.txt.
# Contact: khmer-project@idyll.org
#
"""
Display summary statistics for one or more FASTA/FASTQ files.
% scripts/readstats.py [ -o output.txt ] <file1> <file2>
Use '-h' for parameter help.
"""
import sys
import screed
import argparse
import textwrap
def get_parser():
descr = "Display summary statistics for one or more FASTA/FASTQ files."
epilog = ("""
Report number of bases, number of sequences, and average sequence length
for one or more FASTA/FASTQ files; and report aggregate statistics at end.
With :option:`-o`/:options:`--output`, the output will be saved to the
specified file.
Example::
readstats.py tests/test-data/test-abund-read-2.fa
""")
parser = argparse.ArgumentParser(description=descr,
epilog=textwrap.dedent(epilog))
parser.add_argument('filenames', nargs='+')
parser.add_argument('-o', '--output', dest='outfp', metavar="filename",
help="output file for statistics; defaults to stdout.",
type=argparse.FileType('w'), default=None)
return parser
def main():
"Main function - run when executed as a script."
parser = get_parser()
args = parser.parse_args()
total_bp = 0
total_seqs = 0
output = []
for filename in args.filenames:
bp = 0
seqs = 0
try:
input_iter = screed.open(filename, parse_description=False)
except (IOError, OSError, EOFError) as exc:
print >>sys.stderr, 'ERROR in opening %s:' % filename
print >>sys.stderr, ' ', str(exc)
continue
for record in input_iter:
if seqs % 100000 == 0:
print >>sys.stderr, '...', filename, seqs
bp += len(record.sequence)
seqs += 1
if seqs:
avg_len = bp / float(seqs)
s = '%d bp / %d seqs; %.1f average length -- %s' % (bp,
seqs,
avg_len,
filename)
print >>sys.stderr, '... found', s
output.append(s)
total_bp += bp
total_seqs += seqs
else:
print >>sys.stderr, 'No sequences found in %s' % filename
if total_seqs:
print >>args.outfp, '---------------'
print >>args.outfp, "\n".join(output)
print >>args.outfp, '---------------'
print >>args.outfp, '%d bp / %d seqs; %.1f average length -- total' % \
(total_bp, total_seqs, total_bp / float(total_seqs))
else:
print >>args.outfp, \
'No sequences found in %d files' % len(args.filenames)
if __name__ == '__main__':
main()