# Path: covest/constants.py
# DEFAULT_K = 21
# 
# Path: covest/data.py
# def sample_reads(src_reads_file, dest_reads_file, factor):
#     prob = 1.0 / factor
#     with open(dest_reads_file, 'w') as f:
#         for read_id, read in load_reads(src_reads_file):
#             if random.random() < prob:
#                 f.write('>{}\n'.format(read_id))
#                 f.write('{}\n'.format(read))
# 
# def count_reads_stats(fname):
#     s = n = 0
#     for _, read in load_reads(fname):
#         s += len(read)
#         n += 1
# 
#     ar = round(s / n) if n else 0
#     return ar, s
# 
# Path: covest/utils.py
# def run(command, shell=False, output=None, verbose=False):
#     if verbose:
#         print(command, file=sys.stderr)
#     f = open(output, 'w') if output else None
#     if not shell:
#         command = command.split()
#     return subprocess.call(command, shell=shell, stdout=f)


import argparse
import json
import os
import shutil
import sys
from pathlib import Path
from tempfile import mkstemp
from covest.constants import DEFAULT_K
from covest.data import sample_reads as _sample_reads, count_reads_stats
from covest.utils import run

def sample_reads(reads_file, config, sample_info):
    print('Sampling reads...', file=sys.stderr)
    if len(sample_info) == 2:
        tc, gs = sample_info
        if 'reads_size' in config:
            rs = config['reads_size']
            del config['reads_size']
        else:
            _, rs = count_reads_stats(str(reads_file))
        c = rs / gs
        factor = c / tc
        print(
            'Current coverage: {c}, target coverage: {tc}, genome size: {gs}, '
            'factor: {factor}'.format(
                c=c, tc=tc, gs=gs, factor=factor
            ),
            file=sys.stderr
        )
    elif len(sample_info) == 1:
        factor = sample_info[0]
        print(
            'Factor: {factor}'.format(factor=factor),
            file=sys.stderr
        )
    else:
        print('Please specify a valid sample_info.', file=sys.stderr)
        exit(1)

    fd, rf_sampled = mkstemp(prefix='reads', suffix='.fa')

# Path: covest/constants.py
# DEFAULT_K = 21
# 
# Path: covest/data.py
# def sample_reads(src_reads_file, dest_reads_file, factor):
#     prob = 1.0 / factor
#     with open(dest_reads_file, 'w') as f:
#         for read_id, read in load_reads(src_reads_file):
#             if random.random() < prob:
#                 f.write('>{}\n'.format(read_id))
#                 f.write('{}\n'.format(read))
# 
# def count_reads_stats(fname):
#     s = n = 0
#     for _, read in load_reads(fname):
#         s += len(read)
#         n += 1
# 
#     ar = round(s / n) if n else 0
#     return ar, s
# 
# Path: covest/utils.py
# def run(command, shell=False, output=None, verbose=False):
#     if verbose:
#         print(command, file=sys.stderr)
#     f = open(output, 'w') if output else None
#     if not shell:
#         command = command.split()
#     return subprocess.call(command, shell=shell, stdout=f)


# Path: tools/prepare_experiment.py
import argparse
import json
import os
import shutil
import sys
from pathlib import Path
from tempfile import mkstemp
from covest.constants import DEFAULT_K
from covest.data import sample_reads as _sample_reads, count_reads_stats
from covest.utils import run


def sample_reads(reads_file, config, sample_info):
    print('Sampling reads...', file=sys.stderr)
    if len(sample_info) == 2:
        tc, gs = sample_info
        if 'reads_size' in config:
            rs = config['reads_size']
            del config['reads_size']
        else:
            _, rs = count_reads_stats(str(reads_file))
        c = rs / gs
        factor = c / tc
        print(
            'Current coverage: {c}, target coverage: {tc}, genome size: {gs}, '
            'factor: {factor}'.format(
                c=c, tc=tc, gs=gs, factor=factor
            ),
            file=sys.stderr
        )
    elif len(sample_info) == 1:
        factor = sample_info[0]
        print(
            'Factor: {factor}'.format(factor=factor),
            file=sys.stderr
        )
    else:
        print('Please specify a valid sample_info.', file=sys.stderr)
        exit(1)

    fd, rf_sampled = mkstemp(prefix='reads', suffix='.fa')

    _sample_reads(str(reads_file), rf_sampled, factor)