path="c:/users/dre2/desktop/DRYGINresubmission/";

doubleko=Import[StringJoin[path,"FILES2012/b_doublekoA.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaA.dat"]];

list=Flatten[doublekosigma];
list=DeleteCases[list,"NaN"];
list=Sort[list];
Length[list]
Mean[list]
ListPlot[Take[list,{1,-1,100}],PlotRange->All]

ratio=doublekosigma/doubleko;
ratio=Flatten[ratio];
ratio=DeleteCases[ratio,1];
ratio=DeleteCases[ratio,x_/;x<0];
ratio=Sort[ratio];
ListPlot[Take[ratio,{1,-1,100}],PlotRange->All]

doubleko=Import[StringJoin[path,"FILES2012/b_doublekoB.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaB.dat"]];

list=Flatten[doublekosigma];
list=DeleteCases[list,"NaN"];
list=Sort[list];
Length[list]
Mean[list]
ListPlot[Take[list,{1,-1,100}],PlotRange->All]

ratio=doublekosigma/doubleko;
ratio=Flatten[ratio];
ratio=DeleteCases[ratio,1];
ratio=DeleteCases[ratio,x_/;x<0];
ratio=Sort[ratio];
ListPlot[Take[ratio,{1,-1,100}],PlotRange->All]

doubleko=Import[StringJoin[path,"FILES2012/b_doublekoC.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaC.dat"]];

list=Flatten[doublekosigma];
list=DeleteCases[list,"NaN"];
list=Sort[list];
Length[list]
Mean[list]
ListPlot[Take[list,{1,-1,100}],PlotRange->All]

ratio=doublekosigma/doubleko;
ratio=Flatten[ratio];
ratio=DeleteCases[ratio,1];
ratio=DeleteCases[ratio,x_/;x<0];
ratio=Sort[ratio];
ListPlot[Take[ratio,{1,-1,100}],PlotRange->All]

doubleko=Import[StringJoin[path,"FILES2012/b_doublekoD.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaD.dat"]];

list=Flatten[doublekosigma];
list=DeleteCases[list,"NaN"];
list=Sort[list];
Length[list]
Mean[list]
ListPlot[Take[list,{1,-1,100}],PlotRange->All]

ratio=doublekosigma/doubleko;
ratio=Flatten[ratio];
ratio=DeleteCases[ratio,1];
ratio=DeleteCases[ratio,x_/;x<0];
ratio=Sort[ratio];
ListPlot[Take[ratio,{1,-1,100}],PlotRange->All]

doubleko=Import[StringJoin[path,"FILES2012/b_doublekoE.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaE.dat"]];

list=Flatten[doublekosigma];
list=DeleteCases[list,"NaN"];
list=Sort[list];
Length[list]
Mean[list]
ListPlot[Take[list,{1,-1,100}],PlotRange->All]

ratio=doublekosigma/doubleko;
ratio=Flatten[ratio];
ratio=DeleteCases[ratio,1];
ratio=DeleteCases[ratio,x_/;x<0];
ratio=Sort[ratio];
ListPlot[Take[ratio,{1,-1,100}],PlotRange->All]

(* Create the epistasis matrix for group A: common x common *)

singlekor=Import[StringJoin[path,"FILES2012/a_common.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_common.dat"]];
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoA.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaA.dat"]];

eps=Table[0,{Length[singlekor]},{Length[singlekoc]}];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

a=doubleko[[n1,n2]];
b=doubleko[[n2,n1]];

If[a=="NaN"&&b== "NaN",
eps[[n1,n2]]="NaN";
eps[[n2,n1]]="NaN";
];

If[a=="NaN"&&b≠ "NaN",
If[b<0,
Print["Non positive growthrate for the pair (",n1,",",n2,"): ",doubleko[[n1,n2]],"  ",doubleko[[n2,n1]]];
eps[[n1,n2]]="NaN";
eps[[n2,n1]]="NaN",
eps[[n1,n2]]=Log[2,(b/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
eps[[n2,n1]]=Log[2,(b/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
];
];

If[a≠ "NaN"&&b== "NaN",
If[a<0,
Print["Non positive growthrate for the pair (",n1,",",n2,"): ",doubleko[[n1,n2]],"  ",doubleko[[n2,n1]]];
eps[[n1,n2]]="NaN";
eps[[n2,n1]]="NaN",
eps[[n1,n2]]=Log[2,(a/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
eps[[n2,n1]]=Log[2,(a/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
];
];

If[a≠ "NaN"&&b≠ "NaN",
c=Mean[{a,b}];
If[a<0||b<0,
Print["Non positive growth rate for the pair (",n1,",",n2,"): ",doubleko[[n1,n2]],"  ",doubleko[[n2,n1]]];
eps[[n1,n2]]="NaN";
eps[[n2,n1]]="NaN",
eps[[n1,n2]]=Log[2,(c/(singlekor[[n1,2]]singlekor[[n2,2]]))];
eps[[n2,n1]]=Log[2,(c/(singlekoc[[n1,2]]singlekoc[[n2,2]]))];
];
];

If[((doublekosigma[[n1,n2]])/(doubleko[[n1,n2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekor[[n1,3]])/(singlekor[[n1,2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekoc[[n2,3]])/(singlekoc[[n2,2]]))>0.05,eps[[n1,n2]]="NaN"];

];
]
Export[StringJoin[path,"FILES2012/e_epsA_5pc.dat"],eps]

(* Create the epistasis matrix for group B: common x arrayonly *)

singlekor=Import[StringJoin[path,"FILES2012/a_common.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_arrayonly.dat"]];
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoB.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaB.dat"]];

eps=Table[0,{Length[singlekor]},{Length[singlekoc]}];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

a=doubleko[[n1,n2]];

If[a=="NaN",
eps[[n1,n2]]="NaN",
If[a≤0,
Print["Non positive growthrate for the pair (",n1,",",n2,"): ",doubleko[[n1,n2]]],
eps[[n1,n2]]=Log[2,(a/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
];
];

If[((doublekosigma[[n1,n2]])/(doubleko[[n1,n2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekor[[n1,3]])/(singlekor[[n1,2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekoc[[n2,3]])/(singlekoc[[n2,2]]))>0.05,eps[[n1,n2]]="NaN"];

];
]
Export[StringJoin[path,"FILES2012/e_epsB_5pc.dat"],eps]

(* Create the epistasis matrix for group C: queryonly x genuinearray *)

singlekor=Import[StringJoin[path,"FILES2012/a_queryonly.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoC.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaC.dat"]];

eps=Table[0,{Length[singlekor]},{Length[singlekoc]}];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

a=doubleko[[n1,n2]];

If[a=="NaN",
eps[[n1,n2]]="NaN",
If[a≤0,
Print["Non positive growthrate for the pair (",n1,",",n2,"): ",doubleko[[n1,n2]]],
eps[[n1,n2]]=Log[2,(a/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
];
];

If[((doublekosigma[[n1,n2]])/(doubleko[[n1,n2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekor[[n1,3]])/(singlekor[[n1,2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekoc[[n2,3]])/(singlekoc[[n2,2]]))>0.05,eps[[n1,n2]]="NaN"];

];
]
Export[StringJoin[path,"FILES2012/e_epsC_5pc.dat"],eps]

(* Create the epistasis matrix for group D: tsq x genuinearray *)

singlekor=Import[StringJoin[path,"FILES2012/a_tsq.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoD.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaD.dat"]];

eps=Table[0,{Length[singlekor]},{Length[singlekoc]}];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

a=doubleko[[n1,n2]];

If[a=="NaN",
eps[[n1,n2]]="NaN",
If[a≤0,
Print["Non positive growthrate for the pair (",n1,",",n2,"): ",doubleko[[n1,n2]]],
eps[[n1,n2]]=Log[2,(a/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
];
];

If[((doublekosigma[[n1,n2]])/(doubleko[[n1,n2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekor[[n1,3]])/(singlekor[[n1,2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekoc[[n2,3]])/(singlekoc[[n2,2]]))>0.05,eps[[n1,n2]]="NaN"];

];
]
Export[StringJoin[path,"FILES2012/e_epsD_5pc.dat"],eps]

(* Create the epistasis matrix for group E: damp x genuinearray *)

singlekor=Import[StringJoin[path,"FILES2012/a_damp.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoE.dat"]];
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaE.dat"]];

eps=Table[0,{Length[singlekor]},{Length[singlekoc]}];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

a=doubleko[[n1,n2]];

If[a=="NaN",
eps[[n1,n2]]="NaN",
If[a≤0,
Print["Non positive growthrate for the pair (",n1,",",n2,"): ",doubleko[[n1,n2]]],
eps[[n1,n2]]=Log[2,(a/(singlekor[[n1,2]]singlekoc[[n2,2]]))];
];
];

If[((doublekosigma[[n1,n2]])/(doubleko[[n1,n2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekor[[n1,3]])/(singlekor[[n1,2]]))>0.05,eps[[n1,n2]]="NaN"];
If[((singlekoc[[n2,3]])/(singlekoc[[n2,2]]))>0.05,eps[[n1,n2]]="NaN"];

];
]
Export[StringJoin[path,"FILES2012/e_epsE_5pc.dat"],eps]

(* expbinning[r] implements the exponential binning. The value returned, is an integer from 1 to dim=17. *)

expbinning[r_]:=Module[{bin},

bin=0;

If[r<-(2^1),bin=1];
If[r≥ -(2^1)&&r<-(2^(-0)),bin=2;Return[bin];];
If[r≥ -(2^(-0))&&r<-(2^(-1)),bin=3;Return[bin];];
If[r≥ -(2^(-1))&&r<-(2^(-2)),bin=4;Return[bin];];
If[r≥ -(2^(-2))&&r<-(2^(-3)),bin=5;Return[bin];];
If[r≥ -(2^(-3))&&r<-(2^(-4)),bin=6;Return[bin];];
If[r≥ -(2^(-4))&&r<-(2^(-5)),bin=7;Return[bin];];
If[r≥ -(2^(-5))&&r<-(2^(-6)),bin=8;Return[bin];];
If[r≥ -(2^(-6))&&r<-(2^(-7)),bin=9;Return[bin];];
If[r≥ -(2^(-7))&&r<-(2^(-8)),bin=10;Return[bin];];
If[r≥ -(2^(-8))&&r<(2^(-8)),bin=11;Return[bin];];
If[r≥ (2^(-8))&&r<(2^(-7)),bin=12;Return[bin];];
If[r≥ (2^(-7))&&r<(2^(-6)),bin=13;Return[bin];];
If[r≥ (2^(-6))&&r<(2^(-5)),bin=14;Return[bin];];
If[r≥ (2^(-5))&&r<(2^(-4)),bin=15;Return[bin];];
If[r≥ (2^(-4))&&r<(2^(-3)),bin=16;Return[bin];];
If[r≥ (2^(-3)),bin=17;Return[bin];];

bin
]

dim=17;

(* Bin each epistatic interaction eps(f1,f2) as a function of df1 and df2. For each bin compute the number of points in each bin and the first moment and the square of the second moment. Group A = common x common. *)

singlekor=Import[StringJoin[path,"FILES2012/a_common.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_common.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsA_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
]

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
]

nnanpoints=Table[0,{dim},{dim}];
npoints=Table[0,{dim},{dim}];
delta1=Table[0,{dim},{dim}];
delta2=Table[0,{dim},{dim}];
binx=Table[0,{dim}];
nx=Table[0,{dim}];

q25=Table[0,{dim},{dim}];
q50=Table[0,{dim},{dim}];
q75=Table[0,{dim},{dim}];
q90=Table[0,{dim},{dim}];

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

quantilelist={};

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,   (* different from B, C, D, E to double count correctly *)

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekor[[n2]];

If[a≠i||b≠j,Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;   (* different from B, C, D, E to double count correctly *)
delta1[[a,b]]+=eps[[n1,n2]];   
delta2[[a,b]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];

If[Length[quantilelist]>1,
q25[[i,j]]=Quantile[quantilelist,1/4];
q50[[i,j]]=Quantile[quantilelist,1/2];
q75[[i,j]]=Quantile[quantilelist,3/4];
q90[[i,j]]=Quantile[quantilelist,9/10];
];

Print[i,"  ",j];
];
]

Export[StringJoin[path,"FILES2012/f_nnanpointsA_5pc.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/f_npointsA_5pc.dat"],npoints];
Export[StringJoin[path,"FILES2012/f_delta1A_5pc.dat"],delta1];
Export[StringJoin[path,"FILES2012/f_delta2A_5pc.dat"],delta2];
Export[StringJoin[path,"FILES2012/f_binxA_5pc.dat"],binx];
Export[StringJoin[path,"FILES2012/f_nxA_5pc.dat"],nx];

Export[StringJoin[path,"FILES2012/f_q25A_5pc.dat"],q25];
Export[StringJoin[path,"FILES2012/f_q50A_5pc.dat"],q50];
Export[StringJoin[path,"FILES2012/f_q75A_5pc.dat"],q75];
Export[StringJoin[path,"FILES2012/f_q90A_5pc.dat"],q90];

(* Bin each epistatic interaction eps(f1,f2) as a function of df1 and df2. For each bin compute the number of points in each bin and the first moment and the square of the second moment. Group B = common x arrayonly. *)

singlekor=Import[StringJoin[path,"FILES2012/a_common.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_arrayonly.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsB_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
]

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
]

nnanpoints=Table[0,{dim},{dim}];
npoints=Table[0,{dim},{dim}];
delta1=Table[0,{dim},{dim}];
delta2=Table[0,{dim},{dim}];
binx=Table[0,{dim}];
nx=Table[0,{dim}];

q25=Table[0,{dim},{dim}];
q50=Table[0,{dim},{dim}];
q75=Table[0,{dim},{dim}];
q90=Table[0,{dim},{dim}];

For[i=1,i≤dim,i++,
For[j=i,j≤dim,j++,   (* different from A to double count correctly *)

quantilelist={};

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekoc[[n2]];

(*If[a≠i||b≠j,Continue[]];*)
If[!((a==i&&b==j)||(a==j&&b==i)),Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;nnanpoints[[b,a]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;
delta1[[a,b]]+=eps[[n1,n2]];
delta2[[a,b]]+=((eps[[n1,n2]])^2);

npoints[[b,a]]++;   (* different from A to double count correctly *)
delta1[[b,a]]+=eps[[n1,n2]];
delta2[[b,a]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];

If[Length[quantilelist]>1,
q25[[i,j]]=Quantile[quantilelist,1/4];
q50[[i,j]]=Quantile[quantilelist,1/2];
q75[[i,j]]=Quantile[quantilelist,3/4];
q90[[i,j]]=Quantile[quantilelist,9/10];
q25[[j,i]]=q25[[i,j]];
q50[[j,i]]=q50[[i,j]];
q75[[j,i]]=q75[[i,j]];
q90[[j,i]]=q90[[i,j]];
];

Print[i,"  ",j];
];
]

Export[StringJoin[path,"FILES2012/f_nnanpointsB_5pc.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/f_npointsB_5pc.dat"],npoints];
Export[StringJoin[path,"FILES2012/f_delta1B_5pc.dat"],delta1];
Export[StringJoin[path,"FILES2012/f_delta2B_5pc.dat"],delta2];
Export[StringJoin[path,"FILES2012/f_binxB_5pc.dat"],binx];
Export[StringJoin[path,"FILES2012/f_nxB_5pc.dat"],nx];

Export[StringJoin[path,"FILES2012/f_q25B_5pc.dat"],q25];
Export[StringJoin[path,"FILES2012/f_q50B_5pc.dat"],q50];
Export[StringJoin[path,"FILES2012/f_q75B_5pc.dat"],q75];
Export[StringJoin[path,"FILES2012/f_q90B_5pc.dat"],q90];

(* Bin each epistatic interaction eps(f1,f2) as a function of df1 and df2. For each bin compute the number of points in each bin and the first moment and the square of the second moment. Group C = queryonly x genuinearray. *)

singlekor=Import[StringJoin[path,"FILES2012/a_queryonly.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsC_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
]

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
]

nnanpoints=Table[0,{dim},{dim}];
npoints=Table[0,{dim},{dim}];
delta1=Table[0,{dim},{dim}];
delta2=Table[0,{dim},{dim}];
binx=Table[0,{dim}];
nx=Table[0,{dim}];

q25=Table[0,{dim},{dim}];
q50=Table[0,{dim},{dim}];
q75=Table[0,{dim},{dim}];
q90=Table[0,{dim},{dim}];

For[i=1,i≤dim,i++,
For[j=i,j≤dim,j++,   (* different from A to double count correctly *)

quantilelist={};

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekoc[[n2]];

(*If[a≠i||b≠j,Continue[]];*)
If[!((a==i&&b==j)||(a==j&&b==i)),Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;nnanpoints[[b,a]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;
delta1[[a,b]]+=eps[[n1,n2]];
delta2[[a,b]]+=((eps[[n1,n2]])^2);

npoints[[b,a]]++;   (* different from A to double count correctly *)
delta1[[b,a]]+=eps[[n1,n2]];
delta2[[b,a]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];

If[Length[quantilelist]>1,
q25[[i,j]]=Quantile[quantilelist,1/4];
q50[[i,j]]=Quantile[quantilelist,1/2];
q75[[i,j]]=Quantile[quantilelist,3/4];
q90[[i,j]]=Quantile[quantilelist,9/10];
q25[[j,i]]=q25[[i,j]];
q50[[j,i]]=q50[[i,j]];
q75[[j,i]]=q75[[i,j]];
q90[[j,i]]=q90[[i,j]];
];

Print[i,"  ",j];
];
]

Export[StringJoin[path,"FILES2012/f_nnanpointsC_5pc.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/f_npointsC_5pc.dat"],npoints];
Export[StringJoin[path,"FILES2012/f_delta1C_5pc.dat"],delta1];
Export[StringJoin[path,"FILES2012/f_delta2C_5pc.dat"],delta2];
Export[StringJoin[path,"FILES2012/f_binxC_5pc.dat"],binx];
Export[StringJoin[path,"FILES2012/f_nxC_5pc.dat"],nx];

Export[StringJoin[path,"FILES2012/f_q25C_5pc.dat"],q25];
Export[StringJoin[path,"FILES2012/f_q50C_5pc.dat"],q50];
Export[StringJoin[path,"FILES2012/f_q75C_5pc.dat"],q75];
Export[StringJoin[path,"FILES2012/f_q90C_5pc.dat"],q90];

(* Bin each epistatic interaction eps(f1,f2) as a function of df1 and df2. For each bin compute the number of points in each bin and the first moment and the square of the second moment. Group D = tsq x genuinearray. *)

singlekor=Import[StringJoin[path,"FILES2012/a_tsq.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsD_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
]

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
]

nnanpoints=Table[0,{dim},{dim}];
npoints=Table[0,{dim},{dim}];
delta1=Table[0,{dim},{dim}];
delta2=Table[0,{dim},{dim}];
binx=Table[0,{dim}];
nx=Table[0,{dim}];

q25=Table[0,{dim},{dim}];
q50=Table[0,{dim},{dim}];
q75=Table[0,{dim},{dim}];
q90=Table[0,{dim},{dim}];

For[i=1,i≤dim,i++,
For[j=i,j≤dim,j++,   (* different from A to double count correctly *)

quantilelist={};

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekoc[[n2]];

(*If[a≠i||b≠j,Continue[]];*)
If[!((a==i&&b==j)||(a==j&&b==i)),Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;nnanpoints[[b,a]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;
delta1[[a,b]]+=eps[[n1,n2]];
delta2[[a,b]]+=((eps[[n1,n2]])^2);

npoints[[b,a]]++;   (* different from A to double count correctly *)
delta1[[b,a]]+=eps[[n1,n2]];
delta2[[b,a]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];

If[Length[quantilelist]>1,
q25[[i,j]]=Quantile[quantilelist,1/4];
q50[[i,j]]=Quantile[quantilelist,1/2];
q75[[i,j]]=Quantile[quantilelist,3/4];
q90[[i,j]]=Quantile[quantilelist,9/10];
q25[[j,i]]=q25[[i,j]];
q50[[j,i]]=q50[[i,j]];
q75[[j,i]]=q75[[i,j]];
q90[[j,i]]=q90[[i,j]];
];

Print[i,"  ",j];
];
]

Export[StringJoin[path,"FILES2012/f_nnanpointsD_5pc.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/f_npointsD_5pc.dat"],npoints];
Export[StringJoin[path,"FILES2012/f_delta1D_5pc.dat"],delta1];
Export[StringJoin[path,"FILES2012/f_delta2D_5pc.dat"],delta2];
Export[StringJoin[path,"FILES2012/f_binxD_5pc.dat"],binx];
Export[StringJoin[path,"FILES2012/f_nxD_5pc.dat"],nx];

Export[StringJoin[path,"FILES2012/f_q25D_5pc.dat"],q25];
Export[StringJoin[path,"FILES2012/f_q50D_5pc.dat"],q50];
Export[StringJoin[path,"FILES2012/f_q75D_5pc.dat"],q75];
Export[StringJoin[path,"FILES2012/f_q90D_5pc.dat"],q90];

(* Bin each epistatic interaction eps(f1,f2) as a function of df1 and df2. For each bin compute the number of points in each bin and the first moment and the square of the second moment. Group E = damp x genuinearray. *)

singlekor=Import[StringJoin[path,"FILES2012/a_damp.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsE_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
]

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
]

nnanpoints=Table[0,{dim},{dim}];
npoints=Table[0,{dim},{dim}];
delta1=Table[0,{dim},{dim}];
delta2=Table[0,{dim},{dim}];
binx=Table[0,{dim}];
nx=Table[0,{dim}];

q25=Table[0,{dim},{dim}];
q50=Table[0,{dim},{dim}];
q75=Table[0,{dim},{dim}];
q90=Table[0,{dim},{dim}];

For[i=1,i≤dim,i++,
For[j=i,j≤dim,j++,   (* different from A to double count correctly *)

quantilelist={};

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekoc[[n2]];

(*If[a≠i||b≠j,Continue[]];*)
If[!((a==i&&b==j)||(a==j&&b==i)),Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;nnanpoints[[b,a]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;
delta1[[a,b]]+=eps[[n1,n2]];
delta2[[a,b]]+=((eps[[n1,n2]])^2);

npoints[[b,a]]++;   (* different from A to double count correctly *)
delta1[[b,a]]+=eps[[n1,n2]];
delta2[[b,a]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];

If[Length[quantilelist]>1,
q25[[i,j]]=Quantile[quantilelist,1/4];
q50[[i,j]]=Quantile[quantilelist,1/2];
q75[[i,j]]=Quantile[quantilelist,3/4];
q90[[i,j]]=Quantile[quantilelist,9/10];
q25[[j,i]]=q25[[i,j]];
q50[[j,i]]=q50[[i,j]];
q75[[j,i]]=q75[[i,j]];
q90[[j,i]]=q90[[i,j]];
];

Print[i,"  ",j];
];
]

Export[StringJoin[path,"FILES2012/f_nnanpointsE_5pc.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/f_npointsE_5pc.dat"],npoints];
Export[StringJoin[path,"FILES2012/f_delta1E_5pc.dat"],delta1];
Export[StringJoin[path,"FILES2012/f_delta2E_5pc.dat"],delta2];
Export[StringJoin[path,"FILES2012/f_binxE_5pc.dat"],binx];
Export[StringJoin[path,"FILES2012/f_nxE_5pc.dat"],nx];

Export[StringJoin[path,"FILES2012/f_q25E_5pc.dat"],q25];
Export[StringJoin[path,"FILES2012/f_q50E_5pc.dat"],q50];
Export[StringJoin[path,"FILES2012/f_q75E_5pc.dat"],q75];
Export[StringJoin[path,"FILES2012/f_q90E_5pc.dat"],q90];

(* Bin each epistatic interaction eps(f1,f2) as a function of df1 and df2. For each bin compute the number of points in each bin and the first moment and the square of the second moment. Group ABC. *)

nnanpoints=Table[0,{dim},{dim}];
npoints=Table[0,{dim},{dim}];
delta1=Table[0,{dim},{dim}];
delta2=Table[0,{dim},{dim}];
binx=Table[0,{dim}];
nx=Table[0,{dim}];

q25=Table[0,{dim},{dim}];
q50=Table[0,{dim},{dim}];
q75=Table[0,{dim},{dim}];
q90=Table[0,{dim},{dim}];

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

quantilelist={};



(* Group A = common x common. *)

singlekor=Import[StringJoin[path,"FILES2012/a_common.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_common.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsA_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
];

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,   (* different from B, C, D, E to double count correctly *)

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekor[[n2]];

If[a≠i||b≠j,Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;   (* different from B, C, D, E to double count correctly *)
delta1[[a,b]]+=eps[[n1,n2]];   
delta2[[a,b]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];

If[j<i,Continue[]];   (* different from A to double count correctly *)

(* Group B = common x arrayonly. *)

singlekor=Import[StringJoin[path,"FILES2012/a_common.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_arrayonly.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsB_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
];

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekoc[[n2]];

(*If[a≠i||b≠j,Continue[]];*)
If[!((a==i&&b==j)||(a==j&&b==i)),Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;nnanpoints[[b,a]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;
delta1[[a,b]]+=eps[[n1,n2]];
delta2[[a,b]]+=((eps[[n1,n2]])^2);

npoints[[b,a]]++;   (* different from A to double count correctly *)
delta1[[b,a]]+=eps[[n1,n2]];
delta2[[b,a]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];



(* Group C = queryonly x genuinearray. *)

singlekor=Import[StringJoin[path,"FILES2012/a_queryonly.dat"]];
singlekoc=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
eps=Import[StringJoin[path,"FILES2012/e_epsC_5pc.dat"]];

(* Create look-up tables associating to each mutant its log-growth rate and its bin. *)

logsinglekor=Table[0,{Length[singlekor]}];
binsinglekor=Table[0,{Length[singlekor]}];
For[n=1,n≤Length[singlekor],n++,
logsinglekor[[n]]=Log[2,singlekor[[n,2]]];
binsinglekor[[n]]=expbinning[logsinglekor[[n]]];
];

logsinglekoc=Table[0,{Length[singlekoc]}];
binsinglekoc=Table[0,{Length[singlekoc]}];
For[n=1,n≤Length[singlekoc],n++,
logsinglekoc[[n]]=Log[2,singlekoc[[n,2]]];
binsinglekoc[[n]]=expbinning[logsinglekoc[[n]]];
];

For[n1=1,n1≤Length[singlekor],n1++,
For[n2=1,n2≤Length[singlekoc],n2++,

df1=logsinglekor[[n1]];
df2=logsinglekoc[[n2]];
a=binsinglekor[[n1]];
b=binsinglekoc[[n2]];

(*If[a≠i||b≠j,Continue[]];*)
If[!((a==i&&b==j)||(a==j&&b==i)),Continue[]];

If[eps[[n1,n2]]=="NaN",nnanpoints[[a,b]]++;nnanpoints[[b,a]]++;Continue[]];

(* each double-knockout is counted as two points *)
npoints[[a,b]]++;
delta1[[a,b]]+=eps[[n1,n2]];
delta2[[a,b]]+=((eps[[n1,n2]])^2);

npoints[[b,a]]++;   (* different from A to double count correctly *)
delta1[[b,a]]+=eps[[n1,n2]];
delta2[[b,a]]+=((eps[[n1,n2]])^2);

AppendTo[quantilelist,((eps[[n1,n2]])^2)];

binx[[a]]+= df1;
nx[[a]]++;
binx[[b]]+= df2;
nx[[b]]++;
];
];

If[Length[quantilelist]>1,
q25[[i,j]]=Quantile[quantilelist,1/4];
q50[[i,j]]=Quantile[quantilelist,1/2];
q75[[i,j]]=Quantile[quantilelist,3/4];
q90[[i,j]]=Quantile[quantilelist,9/10];
q25[[j,i]]=q25[[i,j]];
q50[[j,i]]=q50[[i,j]];
q75[[j,i]]=q75[[i,j]];
q90[[j,i]]=q90[[i,j]];
];

Print[i,"  ",j];
];
];

Export[StringJoin[path,"FILES2012/f_nnanpointsABC_5pc.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/f_npointsABC_5pc.dat"],npoints];
Export[StringJoin[path,"FILES2012/f_delta1ABC_5pc.dat"],delta1];
Export[StringJoin[path,"FILES2012/f_delta2ABC_5pc.dat"],delta2];
Export[StringJoin[path,"FILES2012/f_binxABC_5pc.dat"],binx];
Export[StringJoin[path,"FILES2012/f_nxABC_5pc.dat"],nx];

Export[StringJoin[path,"FILES2012/f_q25ABC_5pc.dat"],q25];
Export[StringJoin[path,"FILES2012/f_q50ABC_5pc.dat"],q50];
Export[StringJoin[path,"FILES2012/f_q75ABC_5pc.dat"],q75];
Export[StringJoin[path,"FILES2012/f_q90ABC_5pc.dat"],q90];

(* Go back from exponential binning to linear scale and plot: number of points, average deviation from the linear model, variance and standard deviation. Group A *)

nnanpoints=Import[StringJoin[path,"FILES2012/f_nnanpointsA_5pc.dat"]];
npoints=Import[StringJoin[path,"FILES2012/f_npointsA_5pc.dat"]];
delta1=Import[StringJoin[path,"FILES2012/f_delta1A_5pc.dat"]];
delta2=Import[StringJoin[path,"FILES2012/f_delta2A_5pc.dat"]];
binx=Flatten[Import[StringJoin[path,"FILES2012/f_binxA_5pc.dat"]]];
nx=Flatten[Import[StringJoin[path,"FILES2012/f_nxA_5pc.dat"]]];

q25=Import[StringJoin[path,"FILES2012/f_q25A_5pc.dat"]];
q50=Import[StringJoin[path,"FILES2012/f_q50A_5pc.dat"]];
q75=Import[StringJoin[path,"FILES2012/f_q75A_5pc.dat"]];
q90=Import[StringJoin[path,"FILES2012/f_q90A_5pc.dat"]];

meanx=Table[0,{dim}];
For[i=1,i≤dim,i++,
If[nx[[i]]≠0,meanx[[i]]=((binx[[i]])/(nx[[i]])),meanx[[i]]=Null];
]

nangrid={};
eps0={};
eps1={};
eps2={};
epssigma={};

eps25={};
eps50={};
eps75={};
eps90={};

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

x=meanx[[i]];
y=meanx[[j]];

If[npoints[[i,j]]>1,
AppendTo[nangrid,{x,y,Log[10,nnanpoints[[i,j]]]}];
AppendTo[eps0,{x,y,Log[10,npoints[[i,j]]]}];
AppendTo[eps1,{x,y,((delta1[[i,j]])/(npoints[[i,j]]))}];
AppendTo[eps2,{x,y,((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)}];
AppendTo[epssigma,{x,y,Sqrt[((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)]}];

AppendTo[eps25,{x,y,Sqrt[q25[[i,j]]]}];
AppendTo[eps50,{x,y,Sqrt[q50[[i,j]]]}];
AppendTo[eps75,{x,y,Sqrt[q75[[i,j]]]}];
AppendTo[eps90,{x,y,Sqrt[q90[[i,j]]]}];
];
];
];
(*ListPlot3D[nangrid,PlotRange->All,AxesStyle->18]*)
ListPlot3D[eps0,PlotRange->All,AxesStyle->18]
ListPlot3D[eps1,PlotRange->All,AxesStyle->18]
ListPlot3D[eps2,PlotRange->All,AxesStyle->18]
prmsf5pc=ListPlot3D[epssigma,PlotRange->All,AxesStyle->18]

p25f5pc=ListPlot3D[eps25,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p50f5pc=ListPlot3D[eps50,PlotRange->All,AxesStyle->18,PlotStyle->Red]
p75f5pc=ListPlot3D[eps75,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p90f5pc=ListPlot3D[eps90,PlotRange->All,AxesStyle->18,PlotStyle->Yellow]

Show[p25f5pc,p50f5pc,p75f5pc,p90f5pc,prmsf5pc]

(* Go back from exponential binning to linear scale and plot: number of points, average deviation from the linear model, variance and standard deviation. Group B *)

nnanpoints=Import[StringJoin[path,"FILES2012/f_nnanpointsB_5pc.dat"]];
npoints=Import[StringJoin[path,"FILES2012/f_npointsB_5pc.dat"]];
delta1=Import[StringJoin[path,"FILES2012/f_delta1B_5pc.dat"]];
delta2=Import[StringJoin[path,"FILES2012/f_delta2B_5pc.dat"]];
binx=Flatten[Import[StringJoin[path,"FILES2012/f_binxB_5pc.dat"]]];
nx=Flatten[Import[StringJoin[path,"FILES2012/f_nxB_5pc.dat"]]];

q25=Import[StringJoin[path,"FILES2012/f_q25B_5pc.dat"]];
q50=Import[StringJoin[path,"FILES2012/f_q50B_5pc.dat"]];
q75=Import[StringJoin[path,"FILES2012/f_q75B_5pc.dat"]];
q90=Import[StringJoin[path,"FILES2012/f_q90B_5pc.dat"]];

meanx=Table[0,{dim}];
For[i=1,i≤dim,i++,
If[nx[[i]]≠0,meanx[[i]]=((binx[[i]])/(nx[[i]])),meanx[[i]]=Null];
]

nangrid={};
eps0={};
eps1={};
eps2={};
epssigma={};

eps25={};
eps50={};
eps75={};
eps90={};

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

x=meanx[[i]];
y=meanx[[j]];

If[npoints[[i,j]]>1,
AppendTo[nangrid,{x,y,Log[10,nnanpoints[[i,j]]]}];
AppendTo[eps0,{x,y,Log[10,npoints[[i,j]]]}];
AppendTo[eps1,{x,y,((delta1[[i,j]])/(npoints[[i,j]]))}];
AppendTo[eps2,{x,y,((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)}];
AppendTo[epssigma,{x,y,Sqrt[((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)]}];

AppendTo[eps25,{x,y,Sqrt[q25[[i,j]]]}];
AppendTo[eps50,{x,y,Sqrt[q50[[i,j]]]}];
AppendTo[eps75,{x,y,Sqrt[q75[[i,j]]]}];
AppendTo[eps90,{x,y,Sqrt[q90[[i,j]]]}];
];
];
];
(*ListPlot3D[nangrid,PlotRange->All,AxesStyle->18]*)
ListPlot3D[eps0,PlotRange->All,AxesStyle->18]
ListPlot3D[eps1,PlotRange->All,AxesStyle->18]
ListPlot3D[eps2,PlotRange->All,AxesStyle->18]
prmsf5pc=ListPlot3D[epssigma,PlotRange->All,AxesStyle->18]

p25f5pc=ListPlot3D[eps25,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p50f5pc=ListPlot3D[eps50,PlotRange->All,AxesStyle->18,PlotStyle->Red]
p75f5pc=ListPlot3D[eps75,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p90f5pc=ListPlot3D[eps90,PlotRange->All,AxesStyle->18,PlotStyle->Yellow]

Show[p25f5pc,p50f5pc,p75f5pc,p90f5pc,prmsf5pc]

(* Go back from exponential binning to linear scale and plot: number of points, average deviation from the linear model, variance and standard deviation. Group C *)

nnanpoints=Import[StringJoin[path,"FILES2012/f_nnanpointsC_5pc.dat"]];
npoints=Import[StringJoin[path,"FILES2012/f_npointsC_5pc.dat"]];
delta1=Import[StringJoin[path,"FILES2012/f_delta1C_5pc.dat"]];
delta2=Import[StringJoin[path,"FILES2012/f_delta2C_5pc.dat"]];
binx=Flatten[Import[StringJoin[path,"FILES2012/f_binxC_5pc.dat"]]];
nx=Flatten[Import[StringJoin[path,"FILES2012/f_nxC_5pc.dat"]]];

q25=Import[StringJoin[path,"FILES2012/f_q25C_5pc.dat"]];
q50=Import[StringJoin[path,"FILES2012/f_q50C_5pc.dat"]];
q75=Import[StringJoin[path,"FILES2012/f_q75C_5pc.dat"]];
q90=Import[StringJoin[path,"FILES2012/f_q90C_5pc.dat"]];

meanx=Table[0,{dim}];
For[i=1,i≤dim,i++,
If[nx[[i]]≠0,meanx[[i]]=((binx[[i]])/(nx[[i]])),meanx[[i]]=Null];
]

nangrid={};
eps0={};
eps1={};
eps2={};
epssigma={};

eps25={};
eps50={};
eps75={};
eps90={};

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

x=meanx[[i]];
y=meanx[[j]];

If[npoints[[i,j]]>1,
AppendTo[nangrid,{x,y,Log[10,nnanpoints[[i,j]]]}];
AppendTo[eps0,{x,y,Log[10,npoints[[i,j]]]}];
AppendTo[eps1,{x,y,((delta1[[i,j]])/(npoints[[i,j]]))}];
AppendTo[eps2,{x,y,((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)}];
AppendTo[epssigma,{x,y,Sqrt[((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)]}];

AppendTo[eps25,{x,y,Sqrt[q25[[i,j]]]}];
AppendTo[eps50,{x,y,Sqrt[q50[[i,j]]]}];
AppendTo[eps75,{x,y,Sqrt[q75[[i,j]]]}];
AppendTo[eps90,{x,y,Sqrt[q90[[i,j]]]}];
];
];
];
(*ListPlot3D[nangrid,PlotRange->All,AxesStyle->18]*)
ListPlot3D[eps0,PlotRange->All,AxesStyle->18]
ListPlot3D[eps1,PlotRange->All,AxesStyle->18]
ListPlot3D[eps2,PlotRange->All,AxesStyle->18]
prmsf5pc=ListPlot3D[epssigma,PlotRange->All,AxesStyle->18]

p25f5pc=ListPlot3D[eps25,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p50f5pc=ListPlot3D[eps50,PlotRange->All,AxesStyle->18,PlotStyle->Red]
p75f5pc=ListPlot3D[eps75,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p90f5pc=ListPlot3D[eps90,PlotRange->All,AxesStyle->18,PlotStyle->Yellow]

Show[p25f5pc,p50f5pc,p75f5pc,p90f5pc,prmsf5pc]

(* Go back from exponential binning to linear scale and plot: number of points, average deviation from the linear model, variance and standard deviation. Group D *)

nnanpoints=Import[StringJoin[path,"FILES2012/f_nnanpointsD_5pc.dat"]];
npoints=Import[StringJoin[path,"FILES2012/f_npointsD_5pc.dat"]];
delta1=Import[StringJoin[path,"FILES2012/f_delta1D_5pc.dat"]];
delta2=Import[StringJoin[path,"FILES2012/f_delta2D_5pc.dat"]];
binx=Flatten[Import[StringJoin[path,"FILES2012/f_binxD_5pc.dat"]]];
nx=Flatten[Import[StringJoin[path,"FILES2012/f_nxD_5pc.dat"]]];

q25=Import[StringJoin[path,"FILES2012/f_q25D_5pc.dat"]];
q50=Import[StringJoin[path,"FILES2012/f_q50D_5pc.dat"]];
q75=Import[StringJoin[path,"FILES2012/f_q75D_5pc.dat"]];
q90=Import[StringJoin[path,"FILES2012/f_q90D_5pc.dat"]];

meanx=Table[0,{dim}];
For[i=1,i≤dim,i++,
If[nx[[i]]≠0,meanx[[i]]=((binx[[i]])/(nx[[i]])),meanx[[i]]=Null];
]

nangrid={};
eps0={};
eps1={};
eps2={};
epssigma={};

eps25={};
eps50={};
eps75={};
eps90={};

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

x=meanx[[i]];
y=meanx[[j]];

If[npoints[[i,j]]>1,
AppendTo[nangrid,{x,y,Log[10,nnanpoints[[i,j]]]}];
AppendTo[eps0,{x,y,Log[10,npoints[[i,j]]]}];
AppendTo[eps1,{x,y,((delta1[[i,j]])/(npoints[[i,j]]))}];
AppendTo[eps2,{x,y,((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)}];
AppendTo[epssigma,{x,y,Sqrt[((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)]}];

AppendTo[eps25,{x,y,Sqrt[q25[[i,j]]]}];
AppendTo[eps50,{x,y,Sqrt[q50[[i,j]]]}];
AppendTo[eps75,{x,y,Sqrt[q75[[i,j]]]}];
AppendTo[eps90,{x,y,Sqrt[q90[[i,j]]]}];
];
];
];
(*ListPlot3D[nangrid,PlotRange->All,AxesStyle->18]*)
ListPlot3D[eps0,PlotRange->All,AxesStyle->18]
ListPlot3D[eps1,PlotRange->All,AxesStyle->18]
ListPlot3D[eps2,PlotRange->All,AxesStyle->18]
prmsf5pc=ListPlot3D[epssigma,PlotRange->All,AxesStyle->18]

p25f5pc=ListPlot3D[eps25,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p50f5pc=ListPlot3D[eps50,PlotRange->All,AxesStyle->18,PlotStyle->Red]
p75f5pc=ListPlot3D[eps75,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p90f5pc=ListPlot3D[eps90,PlotRange->All,AxesStyle->18,PlotStyle->Yellow]

Show[p25f5pc,p50f5pc,p75f5pc,p90f5pc,prmsf5pc]

(* Go back from exponential binning to linear scale and plot: number of points, average deviation from the linear model, variance and standard deviation. Group E *)

nnanpoints=Import[StringJoin[path,"FILES2012/f_nnanpointsE_5pc.dat"]];
npoints=Import[StringJoin[path,"FILES2012/f_npointsE_5pc.dat"]];
delta1=Import[StringJoin[path,"FILES2012/f_delta1E_5pc.dat"]];
delta2=Import[StringJoin[path,"FILES2012/f_delta2E_5pc.dat"]];
binx=Flatten[Import[StringJoin[path,"FILES2012/f_binxE_5pc.dat"]]];
nx=Flatten[Import[StringJoin[path,"FILES2012/f_nxE_5pc.dat"]]];

q25=Import[StringJoin[path,"FILES2012/f_q25E_5pc.dat"]];
q50=Import[StringJoin[path,"FILES2012/f_q50E_5pc.dat"]];
q75=Import[StringJoin[path,"FILES2012/f_q75E_5pc.dat"]];
q90=Import[StringJoin[path,"FILES2012/f_q90E_5pc.dat"]];

meanx=Table[0,{dim}];
For[i=1,i≤dim,i++,
If[nx[[i]]≠0,meanx[[i]]=((binx[[i]])/(nx[[i]])),meanx[[i]]=Null];
]

nangrid={};
eps0={};
eps1={};
eps2={};
epssigma={};

eps25={};
eps50={};
eps75={};
eps90={};

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

x=meanx[[i]];
y=meanx[[j]];

If[npoints[[i,j]]>1,
AppendTo[nangrid,{x,y,Log[10,nnanpoints[[i,j]]]}];
AppendTo[eps0,{x,y,Log[10,npoints[[i,j]]]}];
AppendTo[eps1,{x,y,((delta1[[i,j]])/(npoints[[i,j]]))}];
AppendTo[eps2,{x,y,((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)}];
AppendTo[epssigma,{x,y,Sqrt[((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)]}];

AppendTo[eps25,{x,y,Sqrt[q25[[i,j]]]}];
AppendTo[eps50,{x,y,Sqrt[q50[[i,j]]]}];
AppendTo[eps75,{x,y,Sqrt[q75[[i,j]]]}];
AppendTo[eps90,{x,y,Sqrt[q90[[i,j]]]}];
];
];
];
(*ListPlot3D[nangrid,PlotRange->All,AxesStyle->18]*)
ListPlot3D[eps0,PlotRange->All,AxesStyle->18]
ListPlot3D[eps1,PlotRange->All,AxesStyle->18]
ListPlot3D[eps2,PlotRange->All,AxesStyle->18]
prmsf5pc=ListPlot3D[epssigma,PlotRange->All,AxesStyle->18]

p25f5pc=ListPlot3D[eps25,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p50f5pc=ListPlot3D[eps50,PlotRange->All,AxesStyle->18,PlotStyle->Red]
p75f5pc=ListPlot3D[eps75,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p90f5pc=ListPlot3D[eps90,PlotRange->All,AxesStyle->18,PlotStyle->Yellow]

Show[p25f5pc,p50f5pc,p75f5pc,p90f5pc,prmsf5pc]

(* Go back from exponential binning to linear scale and plot: number of points, average deviation from the linear model, variance and standard deviation. Groups A, B and C combined. *)

nnanpoints=Import[StringJoin[path,"FILES2012/f_nnanpointsABC_5pc.dat"]];
npoints=Import[StringJoin[path,"FILES2012/f_npointsABC_5pc.dat"]];
delta1=Import[StringJoin[path,"FILES2012/f_delta1ABC_5pc.dat"]];
delta2=Import[StringJoin[path,"FILES2012/f_delta2ABC_5pc.dat"]];
binx=Flatten[Import[StringJoin[path,"FILES2012/f_binxABC_5pc.dat"]]];
nx=Flatten[Import[StringJoin[path,"FILES2012/f_nxABC_5pc.dat"]]];

q25=Import[StringJoin[path,"FILES2012/f_q25ABC_5pc.dat"]];
q50=Import[StringJoin[path,"FILES2012/f_q50ABC_5pc.dat"]];
q75=Import[StringJoin[path,"FILES2012/f_q75ABC_5pc.dat"]];
q90=Import[StringJoin[path,"FILES2012/f_q90ABC_5pc.dat"]];

meanx=Table[0,{dim}];
For[i=1,i≤dim,i++,
If[nx[[i]]≠0,meanx[[i]]=((binx[[i]])/(nx[[i]])),meanx[[i]]=Null];
]

nangrid={};
eps0={};
eps1={};
eps2={};
epssigma={};

eps25={};
eps50={};
eps75={};
eps90={};

For[i=1,i≤dim,i++,
For[j=1,j≤dim,j++,

x=meanx[[i]];
y=meanx[[j]];

If[npoints[[i,j]]>4,   (* the (-2,-2) bin has only 4 points *)
AppendTo[nangrid,{x,y,Log[10,nnanpoints[[i,j]]]}];
AppendTo[eps0,{x,y,Log[10,npoints[[i,j]]]}];
AppendTo[eps1,{x,y,((delta1[[i,j]])/(npoints[[i,j]]))}];
AppendTo[eps2,{x,y,((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)}];
AppendTo[epssigma,{x,y,Sqrt[((delta2[[i,j]])/(npoints[[i,j]]-1))(*-((npoints[[i,j]])/(npoints[[i,j]]-1))((avgdelta1[[i,j]])^2)*)]}];

AppendTo[eps25,{x,y,Sqrt[q25[[i,j]]]}];
AppendTo[eps50,{x,y,Sqrt[q50[[i,j]]]}];
AppendTo[eps75,{x,y,Sqrt[q75[[i,j]]]}];
AppendTo[eps90,{x,y,Sqrt[q90[[i,j]]]}];
];
];
];
(*ListPlot3D[nangrid,PlotRange->All,AxesStyle->18]*)
ListPlot3D[eps0,PlotRange->All,AxesStyle->18]
ListPlot3D[eps1,PlotRange->All,AxesStyle->18]
ListPlot3D[eps2,PlotRange->All,AxesStyle->18]
prmsf5pc=ListPlot3D[epssigma,PlotRange->All,AxesStyle->18]

p25f5pc=ListPlot3D[eps25,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p50f5pc=ListPlot3D[eps50,PlotRange->All,AxesStyle->18,PlotStyle->Red]
p75f5pc=ListPlot3D[eps75,PlotRange->All,AxesStyle->18,PlotStyle->Orange]
p90f5pc=ListPlot3D[eps90,PlotRange->All,AxesStyle->18,PlotStyle->Yellow]

Show[p25f5pc,p50f5pc,p75f5pc,p90f5pc,prmsf5pc]

(* Comparison between sea creatures using all the data and or only data points whose relative uncertainty is less than 5%. *)

Show[,]

(* In the ABC group very few single gene knockouts have growth rates smaller than 0.5 and relative errors smaller than 5%. *)

v=Import[StringJoin[path,"FILES2012/a_common.dat"]];
g=Transpose[v][[2]];
s=Transpose[v][[3]];
ratio=s/g;
ListPlot[Sort[ratio],PlotRange->All]

For[i=1,i≤Length[g],i++,
If[g[[i]]<0.5,
Print[g[[i]],"   ",s[[i]],"   ",((s[[i]])/(g[[i]]))];
];
]

v=Import[StringJoin[path,"FILES2012/a_queryonly.dat"]];
g=Transpose[v][[2]];
s=Transpose[v][[3]];
ratio=s/g;
ListPlot[Sort[ratio],PlotRange->All]

For[i=1,i≤Length[g],i++,
If[g[[i]]<0.5,
Print[g[[i]],"   ",s[[i]],"   ",((s[[i]])/(g[[i]]))];
];
]

v=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
g=Transpose[v][[2]];
s=Transpose[v][[3]];
ratio=s/g;
ListPlot[Sort[ratio],PlotRange->All]

For[i=1,i≤Length[g],i++,
If[g[[i]]<0.5,
Print[g[[i]],"   ",s[[i]],"   ",((s[[i]])/(g[[i]]))];
];
]