(* The DRYGIN database has been broken down into 1712 files named dry0000 to dry1711 and stored in the DRY2012 folder. *)

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

(* Extract all the query genes. *)

allquery={}; 
For[query=0,query<1712,query++,
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];
AppendTo[allquery,{db[[1,1]],db[[1,8]],db[[1,9]]}];
]
Print["Number of all query entries: ",Length[allquery]]

(* Among the query genes separate the genuine knockouts from temperature sensitive mutants, damp mutants and NaN queries *)

querynames=Transpose[allquery][[1]];
genuinequery={};
tsq={};
damp={};
nanquery={};
For[i=1,i≤1712,i++,
If[allquery[[i,2]]=="NaN",
AppendTo[nanquery,allquery[[i]]];Continue[]];
If[StringCount[querynames[[i]],"tsq"]>0,
AppendTo[tsq,allquery[[i]]];Continue[]];
If[StringCount[querynames[[i]],"damp"]>0,
AppendTo[damp,allquery[[i]]];Continue[]];
AppendTo[genuinequery,allquery[[i]]];
]

Print["Number of NaN queries: ",Length[nanquery]]
Print["Number of non-NaN tsq queries: ",Length[tsq]]
Print["Number of non-NaN damp queries: ",Length[damp]]
Print["Number of non-NaN genuine knockouts queries: ",Length[genuinequery]]
Print[Length[nanquery]+Length[tsq]+Length[damp]+Length[genuinequery]," queries have been classified."]

(* Extract all the array genes. *)

allarray={}; 
query=0;
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];
For[array=1,array≤3885,array++,
AppendTo[allarray,{db[[array,3]],db[[array,10]],db[[array,11]]}];
];

Print["Number of all array entries: ",Length[allarray]]

(* Among the array genes separate the genuine knockouts from the NaN array genes. *)

arraynames=Transpose[allarray][[1]];
genuinearray={};
nanarray={};
For[i=1,i≤3885,i++,
If[allarray[[i,2]]=="NaN",
AppendTo[nanarray,allarray[[i]]];Continue[]];
AppendTo[genuinearray,allarray[[i]]];
]

Print["Number of NaN array genes: ",Length[nanarray]]
Print["Number of non-NaN genuine knockouts array genes: ",Length[genuinearray]]
Print[Length[nanarray]+Length[genuinearray]," array genes have been classified."]

(* Make sure that there are no tsq and damp mutants in the array set. *)

empty={};
For[i=1,i≤Length[tsq],i++,
If[Count[allarray,tsq[[i]]]>0,AppendTo[empty,tsq[[i]]]];
]
Length[empty]

empty={};
For[i=1,i≤Length[damp],i++,
If[Count[allarray,damp[[i]]]>0,AppendTo[empty,damp[[i]]]];
]
Length[empty]

(* Identify the genes which are unique of the query set and do not appear in the array set. Then create the set of all the non-NaN genuine genes (array+query without repetition) *)

queryonly={};
For[i=1,i≤Length[genuinequery],i++,
If[Count[allarray,genuinequery[[i]]]==0,AppendTo[queryonly,genuinequery[[i]]]];
]
Print["Number of genuine genes appearing in the query set and not in the array set: ",Length[queryonly]]

allgenes=Union[genuinearray,queryonly];
Print["Number of non-NaN genuine genes (array+query): ",Length[allgenes]]

(* Extract the genes which appear both in the query and in the array subsets. *)

common={};
For[query=0,query<1712,query++,                                                     (* For each query gene.. *)
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];
name1=db[[1,1]];
For[array=0,array<3885,array++,                                                    (* ..and for each array gene.. *)If[name1==db[[array+1,3]],                                                                 (* ..if their names are the same.. *)
AppendTo[common,{name1,db[[1,8]],db[[1,9]]}];    (* ..then save {gene name, dg, sigma_dg} *)
];
];
]

Print["Number of genuine genes common to the array and query sets: ",Length[common]];

common=Intersection[common,genuinequery];
common=Intersection[common,genuinearray];
Print["Number of non-NaN genuine genes common to the array and query sets: ",Length[common] ]

(* Remove from the array set the genes common to the query set *)

arrayonly=genuinearray;
For[i=1,i≤Length[common],i++,
arrayonly=DeleteCases[arrayonly,common[[i]]];
]

Print["Number of genuine arrayonly genes: ",Length[arrayonly]]

(* Check that all the subsets of genes are ordered and summarize their cardinality *)

allgenes=Sort[allgenes];

allarray=Sort[allarray];
nanarray=Sort[nanarray];
genuinearray=Sort[genuinearray];

allquery=Sort[allquery];
nanquery=Sort[nanquery];
genuinequery=Sort[genuinequery];

common=Sort[common];
arrayonly=Sort[arrayonly];
queryonly=Sort[queryonly];

tsq=Sort[tsq];
damp=Sort[damp];


Length[allgenes]
Length[allarray]
Length[nanarray]
Length[genuinearray]
Length[allquery]
Length[nanquery]
Length[genuinequery]
Length[common]
Length[arrayonly]
Length[queryonly]
Length[tsq]
Length[damp]

(* Export the relevant subsets of genes *)

Export[StringJoin[path,"FILES2012/a_allgenes.dat"],allgenes];
Export[StringJoin[path,"FILES2012/a_allarray.dat"],allarray];
Export[StringJoin[path,"FILES2012/a_nanarray.dat"],nanarray];
Export[StringJoin[path,"FILES2012/a_genuinearray.dat"],genuinearray];
Export[StringJoin[path,"FILES2012/a_allquery.dat"],allquery];
Export[StringJoin[path,"FILES2012/a_nanquery.dat"],nanquery];
Export[StringJoin[path,"FILES2012/a_genuinequery.dat"],genuinequery];
Export[StringJoin[path,"FILES2012/a_common.dat"],common];
Export[StringJoin[path,"FILES2012/a_arrayonly.dat"],arrayonly];
Export[StringJoin[path,"FILES2012/a_queryonly.dat"],queryonly];
Export[StringJoin[path,"FILES2012/a_tsq.dat"],tsq];
Export[StringJoin[path,"FILES2012/a_damp.dat"],damp];

common=Import[StringJoin[path,"FILES2012/a_common.dat"]];
genuinearray=Import[StringJoin[path,"FILES2012/a_genuinearray.dat"]];
arrayonly=Import[StringJoin[path,"FILES2012/a_arrayonly.dat"]];
queryonly=Import[StringJoin[path,"FILES2012/a_queryonly.dat"]];
tsq=Import[StringJoin[path,"FILES2012/a_tsq.dat"]];
damp=Import[StringJoin[path,"FILES2012/a_damp.dat"]];

(* Scan the DRYGIN database and fill a matrix whose element (i,j) is the growth rate of the double knockout ij. Group A: common x common *)

genename1=common;
genename2=common;
genename1=Transpose[genename1][[1]];
genename2=Transpose[genename2][[1]];
doubleko=Table[0,{Length[genename1]},{Length[genename2]}];
doublekosigma=Table[0,{Length[genename1]},{Length[genename2]}];

For[query=0,query<1712,query++,                                                                                  (* For each gene1 (among all the query genes)..    *)
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];

For[i=1,i≤3885,i++,                                                                                                            (* ..and for each gene2 (among the array genes)..   *)
If[Count[genename1,db[[i,1]]]>0&& Count[genename2,db[[i,3]]]>0,(* ..if they are in the selected groups..           *)
                                                                                                                                                           (* ..record the growth rate of the double knockout. *)
doubleko[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,12]];
doublekosigma[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,13]];
];
];
If[Mod[query,100]==0,Print[query]];
]
Export[StringJoin[path,"FILES2012/b_doublekoA.dat"],doubleko]
Export[StringJoin[path,"FILES2012/b_doublekosigmaA.dat"],doublekosigma]

(* Scan the DRYGIN database and fill a matrix whose element (i,j) is the growth rate of the double knockout ij. Group B: common x arrayonly *)

genename1=common;
genename2=arrayonly;
genename1=Transpose[genename1][[1]];
genename2=Transpose[genename2][[1]];
doubleko=Table[0,{Length[genename1]},{Length[genename2]}];
doublekosigma=Table[0,{Length[genename1]},{Length[genename2]}];

For[query=0,query<1712,query++,                                                                                  (* For each gene1 (among all the query genes)..    *)
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];

For[i=1,i≤3885,i++,                                                                                                            (* ..and for each gene2 (among the array genes)..   *)
If[Count[genename1,db[[i,1]]]>0&& Count[genename2,db[[i,3]]]>0,(* ..if they are in the selected groups..           *)
                                                                                                                                                           (* ..record the growth rate of the double knockout. *)
doubleko[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,12]];
doublekosigma[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,13]];
];
];
If[Mod[query,100]==0,Print[query]];
]
Export[StringJoin[path,"FILES2012/b_doublekoB.dat"],doubleko]
Export[StringJoin[path,"FILES2012/b_doublekosigmaB.dat"],doublekosigma]

(* Scan the DRYGIN database and fill a matrix whose element (i,j) is the growth rate of the double knockout ij. Group C: queryonly x genuinearray *)

genename1=queryonly;
genename2=genuinearray;
genename1=Transpose[genename1][[1]];
genename2=Transpose[genename2][[1]];
doubleko=Table[0,{Length[genename1]},{Length[genename2]}];
doublekosigma=Table[0,{Length[genename1]},{Length[genename2]}];

For[query=0,query<1712,query++,                                                                                  (* For each gene1 (among all the query genes)..    *)
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];

For[i=1,i≤3885,i++,                                                                                                            (* ..and for each gene2 (among the array genes)..   *)
If[Count[genename1,db[[i,1]]]>0&& Count[genename2,db[[i,3]]]>0,(* ..if they are in the selected groups..           *)
                                                                                                                                                           (* ..record the growth rate of the double knockout. *)
doubleko[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,12]];
doublekosigma[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,13]];
];
];
If[Mod[query,100]==0,Print[query]];
]
Export[StringJoin[path,"FILES2012/b_doublekoC.dat"],doubleko]
Export[StringJoin[path,"FILES2012/b_doublekosigmaC.dat"],doublekosigma]

(* Scan the DRYGIN database and fill a matrix whose element (i,j) is the growth rate of the double knockout ij. Group D: tsq x genuinearray *)

genename1=tsq;
genename2=genuinearray;
genename1=Transpose[genename1][[1]];
genename2=Transpose[genename2][[1]];
doubleko=Table[0,{Length[genename1]},{Length[genename2]}];
doublekosigma=Table[0,{Length[genename1]},{Length[genename2]}];

For[query=0,query<1712,query++,                                                                                  (* For each gene1 (among all the query genes)..    *)
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];

For[i=1,i≤3885,i++,                                                                                                            (* ..and for each gene2 (among the array genes)..   *)
If[Count[genename1,db[[i,1]]]>0&& Count[genename2,db[[i,3]]]>0,(* ..if they are in the selected groups..           *)
                                                                                                                                                           (* ..record the growth rate of the double knockout. *)
doubleko[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,12]];
doublekosigma[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,13]];
];
];
If[Mod[query,100]==0,Print[query]];
]
Export[StringJoin[path,"FILES2012/b_doublekoD.dat"],doubleko]
Export[StringJoin[path,"FILES2012/b_doublekosigmaD.dat"],doublekosigma]

(* Scan the DRYGIN database and fill a matrix whose element (i,j) is the growth rate of the double knockout ij. Group E: damp x genuinearray *)

genename1=damp;
genename2=genuinearray;
genename1=Transpose[genename1][[1]];
genename2=Transpose[genename2][[1]];
doubleko=Table[0,{Length[genename1]},{Length[genename2]}];
doublekosigma=Table[0,{Length[genename1]},{Length[genename2]}];

For[query=0,query<1712,query++,                                                                                  (* For each gene1 (among all the query genes)..    *)
filename=StringJoin[path,"DRY2012/dry",IntegerString[query,10,4],".dat"];
db=Import[filename,"Table"];

For[i=1,i≤3885,i++,                                                                                                            (* ..and for each gene2 (among the array genes)..   *)
If[Count[genename1,db[[i,1]]]>0&& Count[genename2,db[[i,3]]]>0,(* ..if they are in the selected groups..           *)
                                                                                                                                                           (* ..record the growth rate of the double knockout. *)
doubleko[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,12]];
doublekosigma[[Position[genename1,db[[i,1]]][[1,1]],Position[genename2,db[[i,3]]][[1,1]]]]=db[[i,13]];
];
];
If[Mod[query,100]==0,Print[query]];
]
Export[StringJoin[path,"FILES2012/b_doublekoE.dat"],doubleko]
Export[StringJoin[path,"FILES2012/b_doublekosigmaE.dat"],doublekosigma]

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

singlekor=common;
singlekoc=common;
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoA.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]]))];
];
];

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

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

singlekor=common;
singlekoc=arrayonly;
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoB.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]]))];
];
];

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

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

singlekor=queryonly;
singlekoc=genuinearray;
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoC.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]]))];
];
];

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

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

singlekor=tsq;
singlekoc=genuinearray;
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoD.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]]))];
];
];

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

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

singlekor=damp;
singlekoc=genuinearray;
doubleko=Import[StringJoin[path,"FILES2012/b_doublekoE.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]]))];
];
];

];
]
Export[StringJoin[path,"FILES2012/c_epsE.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;

(* Check that the function which implements the exponential binning does what it's supposed to do. *)

LogLogPlot[expbinning[r],{r,(2^(-10)),16},PlotRange->All,AxesStyle->16]
LogLogPlot[expbinning[-r],{r,(2^(-10)),16},PlotRange->All,AxesStyle->16]
Print["Arguments slightly smaller than -2 go to bin n.",expbinning[-((2^1)+(2^(-100)))]]
Print["Arguments slightly bigger than -2 go to bin n.",expbinning[-((2^1)-(2^(-100)))],"\n"]
Print["Arguments slightly smaller than -(2^(-8)) go to bin n.",expbinning[-((2^(-8))+(2^(-100)))]]
Print["0 goes to bin n.",expbinning[0]]
Print["Arguments slightly bigger than (2^(-8)) go to bin n.",expbinning[(2^(-8))+(2^(-100))],"\n"]
Print["Arguments slightly smaller than (2^(-3)) go to bin n.",expbinning[(2^(-3))-(2^(-100))]]
Print["(2^(-3)) goes to bin n.",expbinning[(2^(-3))]]

(* 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=common;
singlekoc=common;
eps=Import[StringJoin[path,"FILES2012/c_epsA.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/d_nnanpointsA.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/d_npointsA.dat"],npoints];
Export[StringJoin[path,"FILES2012/d_delta1A.dat"],delta1];
Export[StringJoin[path,"FILES2012/d_delta2A.dat"],delta2];
Export[StringJoin[path,"FILES2012/d_binxA.dat"],binx];
Export[StringJoin[path,"FILES2012/d_nxA.dat"],nx];

Export[StringJoin[path,"FILES2012/d_q25A.dat"],q25];
Export[StringJoin[path,"FILES2012/d_q50A.dat"],q50];
Export[StringJoin[path,"FILES2012/d_q75A.dat"],q75];
Export[StringJoin[path,"FILES2012/d_q90A.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=common;
singlekoc=arrayonly;
eps=Import[StringJoin[path,"FILES2012/c_epsB.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/d_nnanpointsB.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/d_npointsB.dat"],npoints];
Export[StringJoin[path,"FILES2012/d_delta1B.dat"],delta1];
Export[StringJoin[path,"FILES2012/d_delta2B.dat"],delta2];
Export[StringJoin[path,"FILES2012/d_binxB.dat"],binx];
Export[StringJoin[path,"FILES2012/d_nxB.dat"],nx];

Export[StringJoin[path,"FILES2012/d_q25B.dat"],q25];
Export[StringJoin[path,"FILES2012/d_q50B.dat"],q50];
Export[StringJoin[path,"FILES2012/d_q75B.dat"],q75];
Export[StringJoin[path,"FILES2012/d_q90B.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=queryonly;
singlekoc=genuinearray;
eps=Import[StringJoin[path,"FILES2012/c_epsC.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/d_nnanpointsC.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/d_npointsC.dat"],npoints];
Export[StringJoin[path,"FILES2012/d_delta1C.dat"],delta1];
Export[StringJoin[path,"FILES2012/d_delta2C.dat"],delta2];
Export[StringJoin[path,"FILES2012/d_binxC.dat"],binx];
Export[StringJoin[path,"FILES2012/d_nxC.dat"],nx];

Export[StringJoin[path,"FILES2012/d_q25C.dat"],q25];
Export[StringJoin[path,"FILES2012/d_q50C.dat"],q50];
Export[StringJoin[path,"FILES2012/d_q75C.dat"],q75];
Export[StringJoin[path,"FILES2012/d_q90C.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=tsq;
singlekoc=genuinearray;
eps=Import[StringJoin[path,"FILES2012/c_epsD.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/d_nnanpointsD.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/d_npointsD.dat"],npoints];
Export[StringJoin[path,"FILES2012/d_delta1D.dat"],delta1];
Export[StringJoin[path,"FILES2012/d_delta2D.dat"],delta2];
Export[StringJoin[path,"FILES2012/d_binxD.dat"],binx];
Export[StringJoin[path,"FILES2012/d_nxD.dat"],nx];

Export[StringJoin[path,"FILES2012/d_q25D.dat"],q25];
Export[StringJoin[path,"FILES2012/d_q50D.dat"],q50];
Export[StringJoin[path,"FILES2012/d_q75D.dat"],q75];
Export[StringJoin[path,"FILES2012/d_q90D.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=damp;
singlekoc=genuinearray;
eps=Import[StringJoin[path,"FILES2012/c_epsE.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/d_nnanpointsE.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/d_npointsE.dat"],npoints];
Export[StringJoin[path,"FILES2012/d_delta1E.dat"],delta1];
Export[StringJoin[path,"FILES2012/d_delta2E.dat"],delta2];
Export[StringJoin[path,"FILES2012/d_binxE.dat"],binx];
Export[StringJoin[path,"FILES2012/d_nxE.dat"],nx];

Export[StringJoin[path,"FILES2012/d_q25E.dat"],q25];
Export[StringJoin[path,"FILES2012/d_q50E.dat"],q50];
Export[StringJoin[path,"FILES2012/d_q75E.dat"],q75];
Export[StringJoin[path,"FILES2012/d_q90E.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=common;
singlekoc=common;
eps=Import[StringJoin[path,"FILES2012/c_epsA.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=common;
singlekoc=arrayonly;
eps=Import[StringJoin[path,"FILES2012/c_epsB.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=queryonly;
singlekoc=genuinearray;
eps=Import[StringJoin[path,"FILES2012/c_epsC.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/d_nnanpointsABC.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/d_npointsABC.dat"],npoints];
Export[StringJoin[path,"FILES2012/d_delta1ABC.dat"],delta1];
Export[StringJoin[path,"FILES2012/d_delta2ABC.dat"],delta2];
Export[StringJoin[path,"FILES2012/d_binxABC.dat"],binx];
Export[StringJoin[path,"FILES2012/d_nxABC.dat"],nx];

Export[StringJoin[path,"FILES2012/d_q25ABC.dat"],q25];
Export[StringJoin[path,"FILES2012/d_q50ABC.dat"],q50];
Export[StringJoin[path,"FILES2012/d_q75ABC.dat"],q75];
Export[StringJoin[path,"FILES2012/d_q90ABC.dat"],q90];

(* TRADITIONAL DEFINITION OF EPISTATSIS *)

(* 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"]];

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]]=b-singlekor[[n1,2]]singlekoc[[n2,2]];
eps[[n2,n1]]=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]]=a-singlekor[[n1,2]]singlekoc[[n2,2]];
eps[[n2,n1]]=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]]=c-singlekor[[n1,2]]singlekoc[[n2,2]];
eps[[n2,n1]]=c-singlekor[[n1,2]]singlekoc[[n2,2]];
];
];

];
]
Export[StringJoin[path,"FILES2012/c_epsA_wrongdef.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"]];

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]]=a-singlekor[[n1,2]]singlekoc[[n2,2]];
];
];

];
]
Export[StringJoin[path,"FILES2012/c_epsB_wrongdef.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"]];

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]]=a-singlekor[[n1,2]]singlekoc[[n2,2]];
];
];

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

(* 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/c_epsA_wrongdef.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/c_epsB_wrongdef.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/c_epsC_wrongdef.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/d_nnanpointsABC_wrongdef.dat"],nnanpoints];
Export[StringJoin[path,"FILES2012/d_npointsABC_wrongdef.dat"],npoints];
Export[StringJoin[path,"FILES2012/d_delta1ABC_wrongdef.dat"],delta1];
Export[StringJoin[path,"FILES2012/d_delta2ABC_wrongdef.dat"],delta2];
Export[StringJoin[path,"FILES2012/d_binxABC_wrongdef.dat"],binx];
Export[StringJoin[path,"FILES2012/d_nxABC_wrongdef.dat"],nx];

Export[StringJoin[path,"FILES2012/d_q25ABC_wrongdef.dat"],q25];
Export[StringJoin[path,"FILES2012/d_q50ABC_wrongdef.dat"],q50];
Export[StringJoin[path,"FILES2012/d_q75ABC_wrongdef.dat"],q75];
Export[StringJoin[path,"FILES2012/d_q90ABC_wrongdef.dat"],q90];

Total[Total[nnanpoints]]

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

Count[Flatten[doubleko],"NaN"]

MatrixForm[nnanpoints]

(* see if some gene gives consistently larger uncertainties when crossed with all the others *)
doublekosigma=Import[StringJoin[path,"FILES2012/b_doublekosigmaB.dat"]];
doublekosigmat=Transpose[doublekosigma];

row=Length[doublekosigma]
column=Length[doublekosigma[[1]]]

mean=Table[0,{1138}];
For[i=1,i≤1138,i++,
list=doublekosigma[[i]];
list=DeleteCases[list,"NaN"];
mean[[i]]=Mean[list];
]
ListPlot[mean,PlotRange->All]
ListPlot[Sort[mean],PlotRange->All]

list=Flatten[doublekosigma];
list=DeleteCases[list,"NaN"];
list=Sort[list];
Take[list,100]
Take[list,-100]
Mean[list]

Length[list]

azz=Take[list,{1,-1,100}];

ListPlot[azz,PlotRange->All]

meant=Table[0,{2742}];
For[i=1,i≤2742,i++,
list=doublekosigmat[[i]];
list=DeleteCases[list,"NaN"];
meant[[i]]=Mean[list];
]
ListPlot[meant,PlotRange->All]
ListPlot[Sort[meant],PlotRange->All]

MatrixForm[arrayonly[[noisyones]]]

ListPlot[meant,PlotRange->All]

ListPlot[Sort[meant],PlotRange->All]

Max[meant]

Take[{a,b,c,d,e,f},{1,-1,3}]

list

list=DeleteCases[list,"NaN"]

Length[list]

list=doublekosigma[[1]]