#!/usr/bin/env python
from __future__ import division

import sys
import logging
import vbgmm

from concoct.output import Output
from concoct.parser import arguments
from concoct.cluster import cluster
from concoct.input import load_data
from concoct.transform import perform_pca

import vbgmm

def main(args):
    # Initialize output handling
    Output(args.basename,args)

    composition, contig_lengths, threshold_filter, cov, cov_range = load_data(args)

    if cov is not None:
        joined = composition.join(
            cov.ix[:,cov_range[0]:cov_range[1]],
            how="inner"
            )
    else:
        joined = composition

    # Fix special case in pca_components
    if args.pca_components == "All":
        args.pca_components = joined[args.length_threshold_filter].shape[1]

    #PCA on the contigs that have kmer count greater than length_threshold
    transform_filter, pca = perform_pca(
        joined[threshold_filter], 
        args.pca_components
        )

    logging.info('Performed PCA, resulted in %s dimentions' % pca.n_components)
    
    Output.write_original_data(
        joined[threshold_filter],
        args.length_threshold
        )

    Output.write_pca(
        transform_filter,
        args.length_threshold,
        joined[threshold_filter].index,
        )

    Output.write_pca_components(
        pca.components_,
        args.length_threshold
        )

    logging.info('PCA transformed data.')

    logging.info('Will call vbgmm with parameters: %s, %s, %s' % (Output.CONCOCT_PATH, args.clusters, args.length_threshold))

    vbgmm.fit(Output.CONCOCT_PATH, args.clusters, args.length_threshold,args.seed,args.iterations,args.epsilon,args.converge_out)

    logging.info("CONCOCT Finished")

        
if __name__=="__main__":
    args = arguments()
    if args.total_percentage_pca == 100:
        args.pca_components = "All"
    else:
        args.pca_components = args.total_percentage_pca/100.0

    results = main(args)

    print >> sys.stderr, "CONCOCT Finished, the log shows how it went."
