 package coalescent.example;

 import  coalescent.tree.*;
 import  java.util.*;
 import  coalescent.util.*;
 import  coalescent.process.*;
 import  coalescent.dna.*;
 import  coalescent.parameter.*;
/**
 *
 */
public class Example4{
	public static void main(String[] args){
		int waitLength=4;
            int number=3;
		if( args.length > 0 ) number= Integer.parseInt(args[0]);
		boolean debug = true;

		//CoalescentProcess cp = new SimpleRecombination();
		SimpleRecombination cp = new SimpleRecombination();
		//cp.setDebug(debug);

            cp.setParameter( new SampleSize(30) );
		cp.setParameter( new Theta(1.0));
		cp.setParameter( new Recombination.Rate(3.0));
		cp.setParameter( new Recombination.nLoci(5));
		int samplesize=((Integer)cp.getParameter("SampleSize").getValue()).intValue();
		double theta=((Double)cp.getParameter("Theta").getValue()).doubleValue();

		double an=0.0e0;
		for(int i=1; i<samplesize; i++) an+=1.0e0/i;

		Parameter[] p= cp.getParameters();
		for(int i=0; i<p.length; i++){
			System.out.println(p[i]);
		}


		boolean showtree=true;
		JTree dp = new JTree();
		dp.showMutation();
		if( showtree) dp.display();

		int count=0;
		double K=0.0e0, treeLength=0.0e0;
		while(count++ <number){
			Genealogy g=(Genealogy)cp.simulate();
			GenealogySummary sum = g.getSummary();

			if( debug){
			System.out.println(g);
			Sequence[] seq= g.getSequences();
			for(int i=0; i<seq.length; i++) seq[i].print();
			}

			K+= sum.mutation();
			treeLength += sum.treeLength();
			int sumK=0;
			double sumL=0.0e0;
			for(int i=1; i<samplesize; i++){
				sumK+=sum.mutationSize(i);
				sumL+=sum.branchSize(i);
                  }
			if(debug){
			System.out.println("K="+sum.mutation()+" sum="+sumK+" treeleng="+
				TextUtility.df4.format(sum.treeLength())+" "+
				TextUtility.df4.format(sumL) );
			}

			if(count%1000==0)
				System.out.println("n="+count+
				" Mean K="+ TextUtility.df4.format(K/count)+ " expected ="+
				TextUtility.df4.format(an*theta) + " Treelength="+
				TextUtility.df4.format(treeLength/count)+ " exp="+
				TextUtility.df4.format(an) );

			if(showtree){
           			//System.out.println("The whole tree is:\n");

				dp.setHeight(g.height());
				dp.setTree(g);

				try{
					Thread.sleep(waitLength*1000);
				}catch(Exception e){}

				if( g.getLociCount() > 1){
					for(int k=0; k<g.getLociCount(); k++){

						Genealogy gg=g.getGenealogy(k);
						System.out.println("Showing genealogy at locus "+k);
						//System.out.println(gg);

						dp.setTree( gg);
						try{
							Thread.sleep(waitLength*1000/3);
						}catch(Exception e){}
					}
				}
			}
		}


	}
}
