-
Notifications
You must be signed in to change notification settings - Fork 294
/
sample-reads-randomly.py
executable file
·194 lines (162 loc) · 7.21 KB
/
sample-reads-randomly.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
185
186
187
188
189
190
191
192
193
194
#! /usr/bin/env python
#
# This script is part of khmer, https://github.com/dib-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=invalid-name,missing-docstring
"""
Subsample sequences from multiple files.
Take a list of files containing sequences, and subsample 100,000 sequences (-N)
uniformly, using reservoir sampling. Stop after first 100m sequences (-M).
By default take one subsample, but take -S samples if specified.
% scripts/sample-reads-randomly.py <infile>
Reads FASTQ and FASTA input, retains format for output.
"""
from __future__ import print_function
import argparse
import screed
import os.path
import random
import textwrap
import sys
import khmer
from khmer.kfile import (check_input_files, add_output_compression_type,
get_file_writer)
from khmer.khmer_args import info, sanitize_epilog
from khmer.utils import write_record, broken_paired_reader
DEFAULT_NUM_READS = int(1e5)
DEFAULT_MAX_READS = int(1e8)
DEBUG = True
def get_parser():
epilog = ("""
Take a list of files containing sequences, and subsample 100,000
sequences (:option:`-N`/:option:`--num_reads`) uniformly, using
reservoir sampling. Stop after first 100m sequences
(:option:`-M`/:option:`--max_reads`). By default take one subsample,
but take :option:`-S`/:option:`--samples` samples if specified.
The output is placed in :option:`-o`/:option:`--output` <file>
(for a single sample) or in `<file>.subset.0` to `<file>.subset.S-1`
(for more than one sample).
This script uses the `reservoir sampling
<http://en.wikipedia.org/wiki/Reservoir_sampling>`__ algorithm.
""") # noqa
parser = argparse.ArgumentParser(
description="Uniformly subsample sequences from a collection of files",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
epilog=textwrap.dedent(epilog))
parser.add_argument('filenames', nargs='+')
parser.add_argument('-N', '--num_reads', type=int, dest='num_reads',
default=DEFAULT_NUM_READS)
parser.add_argument('-M', '--max_reads', type=int, dest='max_reads',
default=DEFAULT_MAX_READS)
parser.add_argument('-S', '--samples', type=int, dest='num_samples',
default=1)
parser.add_argument('-R', '--random-seed', type=int, dest='random_seed',
help='Provide a random seed for the generator')
parser.add_argument('--force_single', default=False, action='store_true',
help='Ignore read pair information if present')
parser.add_argument('-o', '--output', dest='output_file',
type=argparse.FileType('wb'),
metavar="filename", default=None)
parser.add_argument('--version', action='version', version='%(prog)s ' +
khmer.__version__)
parser.add_argument('-f', '--force', default=False, action='store_true',
help='Overwrite output file if it exits')
add_output_compression_type(parser)
return parser
def main():
info('sample-reads-randomly.py')
args = sanitize_epilog(get_parser()).parse_args()
for _ in args.filenames:
check_input_files(_, args.force)
# seed the random number generator?
if args.random_seed:
random.seed(args.random_seed)
# bound n_samples
num_samples = max(args.num_samples, 1)
#
# Figure out what the output filename is going to be
if args.output_file:
output_filename = args.output_file.name
if num_samples > 1:
sys.stderr.write(
"Error: cannot specify -o with more than one sample.")
if not args.force:
print("NOTE: This can be overridden using the --force"
" argument", file=sys.stderr)
sys.exit(1)
else:
filename = args.filenames[0]
if filename in ('/dev/stdin', '-'):
print("Accepting input from stdin; output filename must "
"be provided with '-o'.", file=sys.stderr)
sys.exit(1)
output_filename = os.path.basename(filename) + '.subset'
if num_samples == 1:
print('Subsampling %d reads using reservoir sampling.' %
args.num_reads, file=sys.stderr)
print('Subsampled reads will be placed in %s' %
output_filename, file=sys.stderr)
print('', file=sys.stderr)
else: # > 1
print('Subsampling %d reads, %d times,'
% (args.num_reads, num_samples), ' using reservoir sampling.',
file=sys.stderr)
print('Subsampled reads will be placed in %s.N'
% output_filename, file=sys.stderr)
print('', file=sys.stderr)
reads = []
for n in range(num_samples):
reads.append([])
# read through all the sequences and load/resample the reservoir
for filename in args.filenames:
print('opening', filename, 'for reading', file=sys.stderr)
screed_iter = screed.open(filename)
for count, (_, ispair, rcrd1, rcrd2) in enumerate(broken_paired_reader(
screed_iter,
force_single=args.force_single)):
if count % 10000 == 0:
print('...', count, 'reads scanned', file=sys.stderr)
if count >= args.max_reads:
print('reached upper limit of %d reads' %
args.max_reads, '(see -M); exiting', file=sys.stderr)
break
# collect first N reads
if count < args.num_reads:
for n in range(num_samples):
reads[n].append((rcrd1, rcrd2))
else:
assert len(reads[n]) <= count
# use reservoir sampling to replace reads at random
# see http://en.wikipedia.org/wiki/Reservoir_sampling
for n in range(num_samples):
guess = random.randint(1, count)
if guess <= args.num_reads:
reads[n][guess - 1] = (rcrd1, rcrd2)
# output all the subsampled reads:
if len(reads) == 1:
print('Writing %d sequences to %s' %
(len(reads[0]), output_filename), file=sys.stderr)
output_file = args.output_file
if not output_file:
output_file = open(output_filename, 'wb')
output_file = get_file_writer(output_file, args.gzip, args.bzip)
for records in reads[0]:
write_record(records[0], output_file)
if records[1] is not None:
write_record(records[1], output_file)
else:
for n in range(num_samples):
n_filename = output_filename + '.%d' % n
print('Writing %d sequences to %s' %
(len(reads[n]), n_filename), file=sys.stderr)
output_file = get_file_writer(open(n_filename, 'wb'), args.gzip,
args.bzip)
for records in reads[n]:
write_record(records[0], output_file)
if records[1] is not None:
write_record(records[1], output_file)
if __name__ == '__main__':
main()