(* Copyright B. Waclaw 2016 *)

LargeTumourMarkRGB[name, hh] makes a 2d section of a tumour, colouring three most common PMs with red, green, and blue similarly to Fig. 4 from the paper.
name = name of the directory with files cells*.dat, most_abund*.dat etc.
hh = thickness of the slice in cells. Min = 1 cell.

Clear[LargeTumourMarkRGB]
LargeTumourMarkRGB[name_,hh_]:=Module[{cells,i,j,ln,str,genname,mostabund,gen,ngens,img,allpms,color,tmp2,minx,maxx,miny,maxy,data},SetDirectory[name];
cells=Import[FileNames["cells_*.dat"][[1]],"Table"];Print["cells loaded"];
tmp2=Select[cells,Abs[#[[3]]]<hh &];
minx=Min[tmp2[[All,1]]];
maxx=Max[tmp2[[All,1]]];
miny=Min[tmp2[[All,2]]];
maxy=Max[tmp2[[All,2]]];
data=Table[{0,0,0},{j,maxy-miny+1},{i,maxx-minx+1}];Print["bitmap prepared"];



If[Length[FileNames["most_abund_*.dat"]]>0,
mostabund=Import[FileNames["most_abund_*.dat"][[1]]];
color[i_]=color[i]=0.4{1,1,1};
Do[color[mostabund[[i,4]]]=0.5mostabund[[i,1;;3]]+0.4{1,1,1},{i,Length[mostabund]}];
Do[data[[tmp2[[i,2]]-miny+1,tmp2[[i,1]]-minx+1]]=color[tmp2[[i,4]]],{i,Length[tmp2]}];
,
genname=FileNames["genotypes_*.dat"][[1]];
ngens=ReadList["!\"C:\\Program Files (x86)\\GnuWin32\\bin\\wc\" -l < "<>genname,Number]//First;
Print["genes=",ngens];
allpms=Import[FileNames["all_PMs_*.dat"][[1]],"Table"];
Print["allpms=",allpms[[1;;10]]];
(* this colors only 3 most abundant PMs*)
(* this is most-often used colouring scheme throughout the paper*)
color=Table[0.4{1,1,1},{i,ngens}];
str=OpenRead[genname];
Print[Read[str,Record,NullRecords->True]];i=2;j=4;
ln=Read[str,Record,NullRecords->True];
While[ln=!=EndOfFile,
gen=ToExpression[StringSplit[ln]][[6;;]];
If[Length[Position[BitAnd[gen,2^30-1],allpms[[1,2]]]]>0,color[[i]]={0.9,color[[i,2]],color[[i,3]]}];If[Length[Position[BitAnd[gen,2^30-1],allpms[[2,2]]]]>0,color[[i]]={color[[i,1]],0.9,color[[i,3]]}];If[Length[Position[BitAnd[gen,2^30-1],allpms[[3,2]]]]>0,color[[i]]={color[[i,1]],color[[i,2]],0.9}];
i++;ln=Read[str,Record,NullRecords->True];If[i==j,j*=2;Print[i]]];
Close[str];
Do[data[[tmp2[[i,2]]-miny+1,tmp2[[i,1]]-minx+1]]=color[[tmp2[[i,4]]+1]],{i,Length[tmp2]}];
];
Image[Reverse[data]]]

(* replace the path with your own path *)
LargeTumourMarkRGB["C:\\cprog\\bio\\cancer\\universal_3d_multiple\\available_on_www\\test_new",5]

LargeTumourMarkRGB[name, hh] makes a 2 d section of a tumour, colouring cell lineages. Cells with similar mutations have similar colours.

Clear[LargeTumourMarkLineage,FindPrevGen,RandChange]
FindPrevGen[gens_,mini_,maxi_,gen_]:=Module[{i,i1=mini,i2=maxi,n=0},While[i2≥i1,i=Floor[(i1+i2)/2];If[gens[[i,1]]==gen,Return[i]];If[gens[[i,1]]>gen,i2=i-1,i1=i+1];];i];
RandChange[c_]:=Module[{cn,q=0.2},cn={c[[1]](1-q+2q Random[]),c[[2]](1-q+2q Random[]),c[[3]](1-q+2q Random[])};0.7cn/Max[cn]];
LargeTumourMarkLineage[name_,hh_]:=Module[{cells,i,j,ln,str,gens,mostabund,gen,ngens,img,allpms,color,tmp2,minx,maxx,miny,maxy,data},SetDirectory[name];
cells=Import[FileNames["cells_*.dat"][[1]],"Table"];Print["cells loaded"];
tmp2=Select[cells,Abs[#[[3]]]<hh &];
minx=Min[tmp2[[All,1]]];
maxx=Max[tmp2[[All,1]]];
miny=Min[tmp2[[All,2]]];
maxy=Max[tmp2[[All,2]]];
data=Table[{0,0,0},{j,maxy-miny+1},{i,maxx-minx+1}];Print["bitmap prepared"];

gens=Import[FileNames["genotypes_*.dat"][[1]],"Table"];Print["genotypes loaded"];
Print["genes=",Length[gens]];
color=Table[0.4{1,1,1},{i,Length[gens]}];
SeedRandom[1234];
Monitor[Do[color[[i]]=RandChange[color[[FindPrevGen[gens,1,i,gens[[i,2]]]]]],{i,2,Length[gens]}],i];
Do[data[[tmp2[[i,2]]-miny+1,tmp2[[i,1]]-minx+1]]=color[[tmp2[[i,4]]+1]],{i,Length[tmp2]}];
Image[Reverse[data]]]

(* replace the path with your own path *)
LargeTumourMarkLineage["C:\\cprog\\bio\\cancer\\universal_3d_multiple\\available_on_www\\test_new",5]