(* Here we set the number of compartments, which are numbered C0, C1, ...,Cn *) n=4;

(*  First list all the trees ; Do this in a cycle. a[i] is the number of differentiations from compartment i. *)  W={{2^n}};Do[cnt=1; Do[Do[T[cnt]=Append[W[[i]],j]; cnt=cnt+1,{j,0,Last[W[[i]]]/2}],{i,1,Length[W]}]; W=Table[T[i],{i,1,cnt-1}],{j,n-1,0,-1}]

cha=Table[0,{i,0,n}];Do[(* Go over all the trees in W with index jj *) W0=W[[jj]];Do[a[n-i+1]=W0[[i]],{i,1,n+1}];(* Calculate the probability of this tree *) Do[l=2*a[i];p[i]= (1-v[i+1])^l*v[i+1]^(a[i+1]-l)*CC[l,a[i+1]]+(1-v[i+1])^(l+1)*v[i+1]^(a[i+1]-l-1)*CC[l+1,a[i+1]],{i,n-1,0,-1}]; PP[jj]=Product[p[i],{i,0,n-1}]/.{v[n]->0};

(* Calculate the change in the number of mutants resulting from this tree *) CC=.;(* chose k out of K *) CC[k_,K_]=K!/k!/(K-k)!;Do[ (* differentiation from row k to row k+1 and proliferation in k+1 *) change[k]=(* Consequences of differentiation of a[k] cells *) Sum[ (* i mutants are removed among the a[k] differentiating cells in k, will be added to k+1 times 2 *)CC[i,a[k]]*(m[k]/NN[k])^i*(1- m[k]/NN[k])^(a[k]-i)*Sum[(* l mutations among the 2*(a[k]-i) offspring will added to k+1 *) CC[l,2*(a[k]-i)]*u^l*(1-u)^(2*(a[k]-i)-l)*ReplacePart[cha,{k+1->-i,k+2->2*i+l}],{l,0,2*(a[k]-i)}],{i,0,a[k]}]+
(* Consequences of proliferation  of a[k+1]-2*a[k] cells *) Sum[ (* i mutants are reproducing among the a[k+1]-2*a[k] cells in k+1, will be added to k+1 times 1 *)CC[i,a[k+1]-2*a[k]]*(m[k+1]/NN[k+1])^i*(1- m[k+1]/NN[k+1])^(a[k+1]-2*a[k]-i)*Sum[(* l mutations among the 2*(a[k+1]-2*a[k]-i) offspring will add to k+1 *) CC[l,2*(a[k+1]-2*a[k]-i)]*u^l*(1-u)^(2*(a[k+1]-2*a[k]-i)-l)*ReplacePart[cha,{k+2->i+l}],{l,0,2*(a[k+1]-2*a[k]-i)}],{i,0,a[k+1]-2*a[k]}]
,{k,0,n-1}];(* Treat k=0 separately *) change0=
(* Consequences of proliferation  of a[0] cells *) Sum[ (* i mutants are reproducing among the a[0] cells in k=0, will be added to k=0 times 1 *)CC[i,a[0]]*(m[0]/NN[0])^i*(1- m[0]/NN[0])^(a[0]-i)*Sum[(* l mutations among the 2*(a[0]-i) offspring will add to k=0 *) CC[l,2*(a[0]-i)]*u^l*(1-u)^(2*(a[0]-i)-l)*ReplacePart[cha,{1->i+l}],{l,0,2*(a[0]-i)}],{i,0,a[0]}];(* Treat k=n separately *) changen=(* Consequences of death of a[n] cells *) Sum[ (* i mutants are removed among the a[n] differentiating cells in k, will be subtracted from k=n *)CC[i,a[n]]*(m[n]/NN[n])^i*(1- m[n]/NN[n])^(a[n]-i)*ReplacePart[cha,{n+1->-i}],{i,0,a[n]}];CHA[jj]=FullSimplify[Total[Table[change[k],{k,0,n-1}]]+change0+changen]; Print[jj, ", tree=", W0,", probability=", PP[jj], ", change=", Simplify[Series[CHA[jj],{u,0,0}]]],{jj,1,Length[W]}]

(* For the right hand sides of the ODEs *) RHSv=Simplify[Sum[PP[jj]*CHA[jj],{jj,1,Length[W]}]]

(* Calculate the first occurence of a tree for a  nonzero probability *) nonz=0; Do[If[PP[j]==0, {nonz=j+1}],{j,1,Length[W]}]; nonz

(* Prepare substitutions for the printout of trees, changes, and probabilities *) Subs=Table[m[i]->mu[i]*NN[i],{i,0,n}];Do[Sub[i]={mu[i]->del[i+1]+mu[i+1]},{i,0,n}];SS=Table[v[i]->Subscript[v,i],{i,0,n}];SD=Table[del[i]->Subscript[Δ,i],{i,0,n}];

TableForm[FullSimplify[Table[{W[[i]],Reverse[Simplify[(CHA[i]/.{u->0}/.Subs)]],PP[i]/.SS},{i,nonz,Length[W]}]]]

Do[Q=Reverse[Simplify[(CHA[i]/.{u->0}/.Subs)]]; Do[Q=Q/.Sub[k],{k,0,n}]; CHA0[i]=Q,{i,nonz,Length[W]}]

Z=TableForm[FullSimplify[Table[{W[[i]],CHA0[i]/.SD,PP[i]/.SS},{i,nonz,Length[W]}]]]