/*
 * Copyright (c) 1999 Johnny C. Lam.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 */

#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>

#include "commandline.h"
#include "dictionary.h"
#include "haplotype.h"
#include "segment_factory.h"
#include "types.h"

int
main(int argc, char* argv[])
{
    CommandLine::options_t options[] = {
	CommandLine::SEGMENT,
	CommandLine::ALLELE_FREQ,
	CommandLine::NUM_NORMALS,
	CommandLine::NORMAL_FNAME
    };

    CommandLine::options_t required[] = {
	CommandLine::NORMAL_FNAME
    };

    CommandLine::Instance()->Parse(argc, argv,
				   begin_array(options), end_array(options),
				   begin_array(required), end_array(required));

    std::cout << "Initialization." << endl;

    auto_ptr<SegmentProbability> segment(
	SegmentProbabilityFactory::Instance()->Make(
	    CommandLine::Instance()->Segment(),
	    true));

    auto_ptr<HDictionary> dictionary(new HDictionary(segment.get()));
    {
	ifstream istr(CommandLine::Instance()->NormalFname().data());
	if (CommandLine::Instance()->AlleleFreq()) {
	    dictionary->ReadFrequencies(istr,
					CommandLine::Instance()->NumNormals());
	} else {
	    dictionary->ReadDelimited(istr, '\n',
				      CommandLine::Instance()->NumNormals());
	}
    }

    Haplotype h;
    unsigned int location;
    probability	prob;
    
    while (true) {
	std::cout << "Enter a recombinant ('999' to quit): ";
	std::cout.flush();
	h.ReadDelimited(std::cin, Haplotype::DISEASED);
	if (!h[0].IsNA() && (h[0].Value() == 999))
	    break;
	std::cout << "Enter starting location: "; cout.flush();
	std::cin >> location;
	prob = dictionary->Probability(h, location);
	std::cout << "-log(Probability): " << -logP(prob) << endl << endl;
    }

    return 0;
}
