/*
 * 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 "no_latents.h"

#include <algorithm>
#include <limits>
#include "haplotype.h"
#include "probability.h"
#include "types.h"

Path
NoLatentsPathStrategy::MakePath(const Haplotype& s,
				const Haplotype& t, size_type disease,
				Probability* rules) const
{
    bool left_diff = std::equal(s.const_iterator_at(rules->LeftMask()),
				s.const_iterator_at(disease),
				t.const_iterator_at(rules->LeftMask()));

    bool right_diff = std::equal(s.const_iterator_at(disease),
				 s.const_iterator_at(rules->RightMask()),
				 t.const_iterator_at(disease));

    Path path(s);
    if (left_diff && right_diff) {
	path.Add(Path::Edge(t, std::numeric_limits<log_probability>::max()));
    } else {
	if (Verbose()) {
	    std::cout << "\t" << "Source: " << s << endl
		      << "\t" << "Target: " << t << endl
		      << "\t" << "Disease: " << disease << endl;
	}
	log_probability path_weight = rules->log_P(s, t, disease);
	path.Add(Path::Edge(t, path_weight));
	if (Verbose()) {
	    std::cout << "\t" << "Path:" << endl
		      << "\t" << "        " << s << endl
		      << "\t" << "  ----> " << t << endl
		      << "\t" << "Path weight (-log(prob)): "
		      << path_weight << endl;
	}
    }
    return path;
}
