-
Notifications
You must be signed in to change notification settings - Fork 295
/
load-into-counting.py
executable file
·184 lines (147 loc) · 6.62 KB
/
load-into-counting.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#! /usr/bin/env python2
#
# This file is part of khmer, http://github.com/ged-lab/khmer/, and is
# Copyright (C) Michigan State University, 2009-2015. It is licensed under
# the three-clause BSD license; see LICENSE.
# Contact: khmer-project@idyll.org
# pylint: disable=missing-docstring,invalid-name
"""
Build a counting Bloom filter from the given sequences, save in <htname>.
% load-into-counting.py <htname> <data1> [ <data2> <...> ]
Use '-h' for parameter help.
"""
import json
import os
import sys
import threading
import textwrap
import khmer
from khmer.khmer_args import build_counting_args, report_on_config, info,\
add_threading_args
from khmer.kfile import check_file_writable
from khmer.kfile import check_input_files, check_space
from khmer.kfile import check_space_for_hashtable
def get_parser():
epilog = """
Note: with :option:`-b` the output will be the exact size of the
k-mer counting table and this script will use a constant amount of memory.
In exchange k-mer counts will stop at 255. The memory usage of this script
with :option:`-b` will be about 1.15x the product of the :option:`-x` and
:option:`-N` numbers.
Example::
load-into-counting.py -k 20 -x 5e7 out.ct data/100k-filtered.fa
Multiple threads can be used to accelerate the process, if you have extra
cores to spare.
Example::
load-into-counting.py -k 20 -x 5e7 -T 4 out.ct data/100k-filtered.fa
"""
parser = build_counting_args("Build a k-mer counting table from the given"
" sequences.", epilog=textwrap.dedent(epilog))
add_threading_args(parser)
parser.add_argument('output_countingtable_filename', help="The name of the"
" file to write the k-mer counting table to.")
parser.add_argument('input_sequence_filename', nargs='+',
help="The names of one or more FAST[AQ] input "
"sequence files.")
parser.add_argument('-b', '--no-bigcount', dest='bigcount', default=True,
action='store_false', help="The default behaviour is "
"to count past 255 using bigcount. This flag turns "
"bigcount off, limiting counts to 255.")
parser.add_argument('--summary-info', '-s', default=None, metavar="FORMAT",
choices=['json', 'tsv'],
help="What format should the machine readable run "
"summary be in? (json or tsv, disabled by default)")
parser.add_argument('--report-total-kmers', '-t', action='store_true',
help="Prints the total number of k-mers to stderr")
parser.add_argument('-f', '--force', default=False, action='store_true',
help='Overwrite output file if it exists')
return parser
def main():
info('load-into-counting.py', ['counting', 'SeqAn'])
args = get_parser().parse_args()
report_on_config(args)
base = args.output_countingtable_filename
filenames = args.input_sequence_filename
for name in args.input_sequence_filename:
check_input_files(name, args.force)
check_space(args.input_sequence_filename, args.force)
check_space_for_hashtable(args.n_tables * args.min_tablesize, args.force)
check_file_writable(base)
check_file_writable(base + ".info")
print >>sys.stderr, 'Saving k-mer counting table to %s' % base
print >>sys.stderr, 'Loading kmers from sequences in %s' % repr(filenames)
# clobber the '.info' file now, as we always open in append mode below
if os.path.exists(base + '.info'):
os.remove(base + '.info')
print >>sys.stderr, 'making k-mer counting table'
htable = khmer.new_counting_hash(args.ksize, args.min_tablesize,
args.n_tables)
htable.set_use_bigcount(args.bigcount)
filename = None
total_num_reads = 0
for index, filename in enumerate(filenames):
rparser = khmer.ReadParser(filename)
threads = []
print >>sys.stderr, 'consuming input', filename
for _ in xrange(args.threads):
cur_thrd = \
threading.Thread(
target=htable.consume_fasta_with_reads_parser,
args=(rparser, )
)
threads.append(cur_thrd)
cur_thrd.start()
for thread in threads:
thread.join()
if index > 0 and index % 10 == 0:
check_space_for_hashtable(args.n_tables * args.min_tablesize,
args.force)
print >>sys.stderr, 'mid-save', base
htable.save(base)
with open(base + '.info', 'a') as info_fh:
print >> info_fh, 'through', filename
total_num_reads += rparser.num_reads
n_kmers = htable.n_unique_kmers()
if args.report_total_kmers:
print >> sys.stderr, 'Total number of unique k-mers:', n_kmers
with open(base + '.info', 'a') as info_fp:
print >>info_fp, 'Total number of unique k-mers:', n_kmers
print >>sys.stderr, 'saving', base
htable.save(base)
# Change max_false_pos=0.2 only if you really grok it. HINT: You don't
fp_rate = \
khmer.calc_expected_collisions(htable, args.force, max_false_pos=.2)
with open(base + '.info', 'a') as info_fp:
print >> info_fp, 'fp rate estimated to be %1.3f\n' % fp_rate
if args.summary_info:
mr_fmt = args.summary_info.lower()
mr_file = base + '.info.' + mr_fmt
print >> sys.stderr, "Writing summmary info to", mr_file
with open(mr_file, 'w') as mr_fh:
if mr_fmt == 'json':
mr_data = {
"ht_name": os.path.basename(base),
"fpr": fp_rate,
"num_kmers": n_kmers,
"files": filenames,
"mrinfo_version": "0.2.0",
"num_reads": total_num_reads,
}
json.dump(mr_data, mr_fh)
mr_fh.write('\n')
elif mr_fmt == 'tsv':
mr_fh.write("ht_name\tfpr\tnum_kmers\tnum_reads\tfiles\n")
vals = [
os.path.basename(base),
"{:1.3f}".format(fp_rate),
str(n_kmers),
str(total_num_reads),
";".join(filenames),
]
mr_fh.write("\t".join(vals) + "\n")
print >> sys.stderr, 'fp rate estimated to be %1.3f' % fp_rate
print >>sys.stderr, 'DONE.'
print >>sys.stderr, 'wrote to:', base + '.info'
if __name__ == '__main__':
main()
# vim: set ft=python ts=4 sts=4 sw=4 et tw=79: