#! /usr/bin/env python3
"""Use UPFP to parse a FASTA into a CSV file."""
import argparse
import gzip

from upfp import csv_to_fasta, smi_to_fasta

parser = argparse.ArgumentParser()
parser.add_argument(
    'csv_filepath', type=str,
    help='path to the CSV file or SMI file.'
)
parser.add_argument(
    'fasta_filepath', type=str,
    help='path where to store the FASTA file.'
)
parser.add_argument(
    '-g', '--gzipped', action='store_true',
    help=(
        'flag to indicate whether the FASTA should be gzipped. '
        'Defaults to False.'
    ),
    default=False
)
parser.add_argument(
    '-c', '--chunk_size', type=int,
    help=(
        'size of the chunks used when writing the FASTA file. '
        'Defaults to 10000.'
    ),
    default=10000
)

if __name__ == '__main__':
    # parse arguments
    args = parser.parse_args()

    # read chunks of sequences
    if args.csv_filepath.split('.')[-1].lower() == 'smi':
        chunks = smi_to_fasta(args.csv_filepath, chunk_size=args.chunk_size)
    else:
        chunks = csv_to_fasta(args.csv_filepath, chunk_size=args.chunk_size)

    # write the fasta file
    if args.gzipped:
        with gzip.open(args.fasta_filepath, 'wb') as fp:
            for chunk in chunks:
                fp.write(chunk.encode('utf-8'))
    else:
        with open(args.fasta_filepath, 'w') as fp:
            for chunk in chunks:
                fp.write(chunk)
