| 1 |
import argparse |
| 2 |
import sys |
| 3 |
import random |
| 4 |
from pyfastaq import sequences, utils |
| 5 |
|
| 6 |
def run(description): |
| 7 |
parser = argparse.ArgumentParser( |
| 8 |
description = 'Takes a random subset of reads from a sequence file and optionally the corresponding read ' + |
| 9 |
'from a mates file. Output is interleaved if mates file given', |
| 10 |
usage = 'fastaq to_random_subset [options] <infile> <outfile> <percent>') |
| 11 |
parser.add_argument('--mate_file', help='Name of mates file') |
| 12 |
parser.add_argument('--seed', help='Seed for random number generator. If not given, python\'s default is used', metavar='INT') |
| 13 |
parser.add_argument('infile', help='Name of input file') |
| 14 |
parser.add_argument('outfile', help='Name of output file') |
| 15 |
parser.add_argument('percent', type=float, help='Per cent probability of keeping any given read (pair) in [0,100]', metavar='FLOAT') |
| 16 |
options = parser.parse_args() |
| 17 |
|
| 18 |
random.seed(a=options.seed) |
| 19 |
seq_reader = sequences.file_reader(options.infile) |
| 20 |
fout = utils.open_file_write(options.outfile) |
| 21 |
|
| 22 |
if options.mate_file: |
| 23 |
mate_seq_reader = sequences.file_reader(options.mate_file) |
| 24 |
|
| 25 |
for seq in seq_reader: |
| 26 |
if options.mate_file: |
| 27 |
try: |
| 28 |
mate_seq = next(mate_seq_reader) |
| 29 |
except StopIteration: |
| 30 |
print('Error! Didn\'t get mate for read', seq.id, file=sys.stderr) |
| 31 |
sys.exit(1) |
| 32 |
if 100 * random.random() <= options.percent: |
| 33 |
print(seq, file=fout) |
| 34 |
if options.mate_file: |
| 35 |
print(mate_seq, file=fout) |
| 36 |
|
| 37 |
utils.close(fout) |