
(*==============================================================================

     bootstrap.tl

     This file is part of the TREEFINDER software.

                                    COPYRIGHT (C) 1997-2008 BY GANGOLF JOBB
==============================================================================*)

BootstrapAnalysis[
 data,
 NReplicates->1000,
 ConsensusLevel->50,
 WithEdgeSupport->True,
 WithEdgeLengths->True,
 WithEdgeLengthIntervals->True,
 ConsensusFile->~Void~,
 SampleFile->~Void~,
 ReturnConsensus->False,
 ShowTopologies->False,
 SubstitutionModel->~Void~,
 PartitionRates->~Void~,
 Tree->~Void~,
 SearchDepth->~Void~,
 ResolveMultifurcations->~Void~,
 StartTrees->~Void~,
 IterationLimit->~Void~
][
 report,trees:={},consensus,sf,msg
]:=<<

 data=LoadSequences[data];

 If[!IsVoid[SampleFile],sf=Open[SampleFile,"w"];Spell["{\n",sf]];

 For[

  msg=" "<>i<>" ";Spell[msg];

  report=
   {
    ResampleSequences[data],
    If[!IsVoid[SubstitutionModel],SubstitutionModel->SubstitutionModel],
    If[!IsVoid[PartitionRates],PartitionRates->PartitionRates],
    If[!IsVoid[Tree],Tree->Tree],
    If[!IsVoid[Tree],FirstTreeOnly->True],
    If[!IsVoid[SearchDepth],SearchDepth->SearchDepth],
    If[!IsVoid[ResolveMultifurcations],ResolveMultifurcations->ResolveMultifurcations],
    If[!IsVoid[StartTrees],StartTrees->StartTrees],
    If[!IsVoid[IterationLimit],IterationLimit->IterationLimit],
    AcceptFlatness->True,
    WithEdgeSupport->False
   }@ReconstructPhylogeny;

  For[Spell["\b \b"],(=j,1:StringLength[msg]=)];

  AppendR[trees,report|1|'Phylogeny];

  If[!IsVoid[SampleFile],Spell[" ",sf];Write[report|1,sf];Spell[",\n",sf]];

  Gc;

  ,(=i,1:NReplicates=)
 ];

 If[!IsVoid[SampleFile],Spell[" ()\n}\n",sf];Close[sf]];

 consensus=
  BuildConsensusTree[
   trees,
   ConsensusLevel->ConsensusLevel,
   WithEdgeSupport->WithEdgeSupport,
   WithEdgeLengths->WithEdgeLengths,
   WithEdgeLengthIntervals->WithEdgeLengthIntervals
  ];

 If[!IsVoid[ConsensusFile],SaveTree[consensus,ConsensusFile]];

 If[ReturnConsensus,Return[consensus]];

 PlotTree[
  consensus,
  Header->"Consensus over "<>NReplicates<>" Replicates",
  Description->"Consensus Level:  "<>ConsensusLevel
 ];

 If[ShowTopologies,PlotTree[CountTopologies[trees,AsPercentage->True,TopTenOnly->True],Header->"Top Ten Topologies"]];

 ()
>>,
SetMask[BootstrapAnalysis,"1."], Protect[BootstrapAnalysis],
Create[ConsensusFile], Protect[ConsensusFile],
Create[SampleFile], Protect[SampleFile],
Create[ReturnConsensus], Protect[ReturnConsensus],
Create[ShowTopologies], Protect[ShowTopologies],

(*============================================================================*)

()
