#!/usr/bin/env python3
import argparse
import gzip
import pickle
import sys

from frea.simulation import *

parser = argparse.ArgumentParser()
parser.add_argument('chromosome', help='Chromosome (integer)')
parser.add_argument('pheno', help='Merged phenotype file (output of frea-combine-pheno)')
parser.add_argument('events', help='Recombination events file (output frea-sample-events)')
parser.add_argument('prefix', help='Prefix for output dataset')
args = parser.parse_args()

with open(args.events, 'rb') as f:
    state = pickle.load(f)
with open(args.pheno, 'rb') as f:
    y = pickle.load(f)
with gzip.open('{}.vcf.gz'.format(args.prefix), 'wt') as f:
    output_genotypes(seed=state['seed'], n=state['y'].shape[0],
                     events=state['events'], chromosome=args.chromosome,
                     hotspot_file=state['hotspot_file'],
                     legend_file=state['legend_file'],
                     haps_file=state['haps_file'],
                     sample_file=state['sample_file'], file=f)
with open('{}.pheno'.format(args.prefix), 'w') as f:
    output_phenotype(y, file=f)
