 package coalescent.example;

 import  coalescent.tree.*;
 import  java.util.*;
 import  coalescent.util.*;
 import  coalescent.process.*;
 import  coalescent.dna.*;
 import  coalescent.parameter.*;

/**
 *
 */
public class Example5{
	public static void main(String[] args){
		int waitLength=4;
            int number=50000;
		if( args.length > 0 ) number= Integer.parseInt(args[0]);
		boolean debug = false;

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

        cp.setParameter( new SampleSize(50) );
		cp.setParameter( new Theta(1.0));
		cp.setParameter( new Recombination.Rate(2.0));
		cp.setParameter( new Recombination.nLoci(10));
		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){
					BitSet sites=Chromosome.getRCsites();
					if( sites.cardinality() > 0){
						System.out.println("There are "+(sites.cardinality()+1)
									+" different genealogies");
						Chromosome.recombinationSites();


						Genealogy gg=g.getGenealogy(0);
						System.out.println("Showing genealogy at loci 0");
						dp.setTree( gg);
						try{
							Thread.sleep(waitLength*1000/3);
						}catch(Exception e){}

						int k=0;
					  	while ( sites.nextSetBit(k) >= 0){
							k=sites.nextSetBit(k)+1;
							gg=g.getGenealogy(k);
							System.out.println("Showing genealogy at loci "+ k);
							dp.setTree( gg);
							try{
								Thread.sleep(waitLength*1000/3);
							}catch(Exception e){}
						}
					}
				}
			}
		}


	}
}
