SetDirectory[NotebookDirectory[]];
list=Import["4932-S.cerevisiae_whole_organism-integrated_dataset_91.txt","TSV"];
{genes,pa}=Transpose[list[[10;;-1,2;;3]]];

Histogram[Log[10,pa],{-3,5,0.1}]

{acc,seq}=Import["SCorf_coding.fasta",{{"Header","Sequence"}}];
acclist=Table[StringSplit[acc[[j]]][[1]],{j,Length[acc]}];
lon=Table[StringLength[seq[[h]]],{h,Length[acc]}];
gename=Table[StringSplit[genes[[g]],"."][[2]],{g,Length[genes]}];
lista={};Do[pos=Position[acclist,gename[[o]]];
If[pos≠{},
AppendTo[lista,{pos[[1,1]],gename[[o]],pa[[o]],(lon[[pos[[1,1]]]]-3)/3}]],{o,Length[gename]}];

Export["list_4932.dat",lista,"Table"]

lista=Import["list_4932.dat","Table"];
indx=Ordering[lista[[1;;-1,3]]];listaf=lista[[indx]];

low={};gl=0;h=1;While[gl<500,If[Exp[-(listaf[[h,4]]-370)^2/(2 15000)]>0.5 RandomReal[],{gl=gl+1;AppendTo[low,listaf[[h]]]}];
h=h+1];h
high={};gl=0;h=Length[listaf];While[gl<500,If[Exp[-(listaf[[h,4]]-370)^2/(2 15000)]>0.5 RandomReal[],{gl=gl+1;AppendTo[high,listaf[[h]]]}];
h=h-1];
Export["SC_Hlist500.dat",high,"TSV"];
Export["SC_Llist500.dat",low,"TSV"];

listafh=Import["SC_Hlist500.dat","TSV"];
listafl=Import["SC_Llist500.dat","TSV"];

GraphicsGrid[{{Histogram[{Style[Log[10,pa],LightGray],Style[Labeled[Log[10,listaf[[1;;500,3]]],"Low AP",Above],Red],Style[Labeled[Log[10,listaf[[-500;;-1,3]]],"High AP",Above],Blue]},{-3,5,0.1},Frame->True,FrameTicks->{Automatic,Automatic,None,None},FrameLabel->{"(Subscript[Log, 10]) (AP)","# of sequences",None,None},FrameStyle->Directive[16,FontFamily->"Helvetica"],Axes->None,ImageSize->400],Histogram[{Style[listaf[[1;;500,4]],Red],Style[listaf[[-500;;-1,4]],Blue]},{0,1200,50},Frame->True,FrameTicks->{Automatic,Automatic,None,None},FrameLabel->{"(Subscript[Log, 10]) (AP)","# of sequences",None,None},FrameStyle->Directive[16,FontFamily->"Helvetica"],Axes->None,ImageSize->400]},{Histogram[{Style[Log[10,pa],LightGray],Style[Labeled[Log[10,listafl[[1;;-1,3]]],"Low AP",Above],Red],Style[Labeled[Log[10,listafh[[1;;-1,3]]],"High AP",Above],Blue]},{-3,5,0.1},Frame->True,FrameTicks->{Automatic,Automatic,None,None},FrameLabel->{"(Subscript[Log, 10]) (AP)","# of sequences",None,None},
FrameStyle->Directive[16,FontFamily->"Helvetica"],Axes->None,ImageSize->400],Histogram[{Style[listafl[[1;;-1,4]],Red],Style[listafh[[1;;-1,4]],Blue]},{0,1200,50},Frame->True,FrameTicks->{Automatic,Automatic,None,None},FrameLabel->{"(Subscript[Log, 10]) (AP)","# of sequences",None,None},FrameStyle->Directive[16,FontFamily->"Helvetica"],Axes->None,ImageSize->400]}},Spacings->{2,2}]

DEG={{3,"*",{"TAA","TGA","TAG"}},{4,"A",{"GCT","GCC","GCA","GCG"}},{6,"L",{"TTA","TTG","CTT","CTC","CTA","CTG"}},{6,"R",{"CGT","CGC","CGA","CGG","AGA","AGG"}},{2,"K",{"AAA","AAG"}},{2,"N",{"AAT","AAC"}},{2,"D",{"GAT","GAC"}},{2,"F",{"TTT","TTC"}},{2,"C",{"TGT","TGC"}},{4,"P",{"CCT","CCC","CCA","CCG"}},{2,"Q",{"CAA","CAG"}},{6,"S",{"TCT","TCC","TCA","TCG","AGT","AGC"}},{2,"E",{"GAA","GAG"}},{4,"T",{"ACT","ACC","ACA","ACG"}},{4,"G",{"GGT","GGC","GGA","GGG"}},{1,"W",{"TGG"}},{2,"H",{"CAT","CAC"}},{2,"Y",{"TAT","TAC"}},{3,"I",{"ATT","ATC","ATA"}},{4,"V",{"GTT","GTC","GTA","GTG"}},{1,"M",{"ATG"}}};
sal=Flatten[Table[Table[Outer[StringJoin,DEG[[i,3]],DEG[[j,3]]],{i,2,21}],{j,21}],1];cod=Table[Flatten[sal[[h]]],{h,420}];
aa=Flatten[Table[Table[StringJoin[{DEG[[i,2]],DEG[[j,2]]}],{i,2,21}],{j,21}],1];dim=Flatten[Table[Table[DEG[[i,1]] DEG[[j,1]],{i,2,21}],{j,21}]];
tableau=Flatten[Table[Table[{h,cod[[h,j]]},{j,dim[[h]]}],{h,420}],1];
tabla=Flatten[Table[Table[{i,DEG[[i,3,j]]},{j,Length[DEG[[i,3]]]}],{i,21}],1];

{acc,seq}=Import["SCorf_coding.fasta",{{"Header","Sequence"}}];

low=Import["SC_Llist500.dat","TSV"];high=Import["SC_Hlist500.dat","TSV"];

lico6={};lico3={};Do[
orf=seq[[low[[g,1]]]];
seq1=StringSplit[orf,""];lon1=Length[seq1];
part=Partition[seq1,6,3];lon2=Length[part];
codon=Table[StringJoin[part[[h]]],{h,lon2}];
lon3=Length[codon];
Do[loc=Position[tableau,codon[[j]]][[1,1]];
AppendTo[lico6,{tableau[[loc,1]],loc}],{j,lon3}];
part=Partition[seq1,3];lonp=Length[part];
codon=Table[StringJoin[part[[h]]],{h,lonp}];
lonp=Length[codon];
Do[loc=Position[tabla,codon[[j]]][[1,1]];
AppendTo[lico3,{tabla[[loc,1]],loc}],{j,lonp}];
Print[g," ",low[[g]]],{g,500}];
Export["SCmin_coduse500_6b.dat",lico6,"Table"];
Export["SCmin_coduse500_3b.dat",lico3,"Table"];

lico6={};lico3={};Do[
orf=seq[[high[[g,1]]]];
seq1=StringSplit[orf,""];lon1=Length[seq1];
part=Partition[seq1,6,3];lon2=Length[part];
codon=Table[StringJoin[part[[h]]],{h,lon2}];
lon3=Length[codon];
Do[loc=Position[tableau,codon[[j]]][[1,1]];
AppendTo[lico6,{tableau[[loc,1]],loc}],{j,lon3}];
part=Partition[seq1,3];lonp=Length[part];
codon=Table[StringJoin[part[[h]]],{h,lonp}];
lonp=Length[codon];
Do[loc=Position[tabla,codon[[j]]][[1,1]];
AppendTo[lico3,{tabla[[loc,1]],loc}],{j,lonp}];Print[g," ",high[[g]]],
{g,500}];
Export["SCmax_coduse500_6b.dat",lico6,"Table"];
Export["SCmax_coduse500_3b.dat",lico3,"Table"];

SetDirectory[NotebookDirectory[]];

DEG={{3,"*",{"TAA","TGA","TAG"}},{4,"A",{"GCT","GCC","GCA","GCG"}},{6,"L",{"TTA","TTG","CTT","CTC","CTA","CTG"}},{6,"R",{"CGT","CGC","CGA","CGG","AGA","AGG"}},{2,"K",{"AAA","AAG"}},{2,"N",{"AAT","AAC"}},{2,"D",{"GAT","GAC"}},{2,"F",{"TTT","TTC"}},{2,"C",{"TGT","TGC"}},{4,"P",{"CCT","CCC","CCA","CCG"}},{2,"Q",{"CAA","CAG"}},{6,"S",{"TCT","TCC","TCA","TCG","AGT","AGC"}},{2,"E",{"GAA","GAG"}},{4,"T",{"ACT","ACC","ACA","ACG"}},{4,"G",{"GGT","GGC","GGA","GGG"}},{1,"W",{"TGG"}},{2,"H",{"CAT","CAC"}},{2,"Y",{"TAT","TAC"}},{3,"I",{"ATT","ATC","ATA"}},{4,"V",{"GTT","GTC","GTA","GTG"}},{1,"M",{"ATG"}}};
sal=Flatten[Table[Table[Outer[StringJoin,DEG[[i,3]],DEG[[j,3]]],{i,2,21}],{j,21}],1];cod=Table[Flatten[sal[[h]]],{h,420}];
aa=Flatten[Table[Table[StringJoin[{DEG[[i,2]],DEG[[j,2]]}],{i,2,21}],{j,21}],1];dim=Flatten[Table[Table[DEG[[i,1]] DEG[[j,1]],{i,2,21}],{j,21}]];
tableau=Flatten[Table[Table[{h,cod[[h,j]]},{j,dim[[h]]}],{h,420}],1];
tabla=Flatten[Table[Table[{i,DEG[[i,3,j]]},{j,Length[DEG[[i,3]]]}],{i,21}],1];
fac[x_]:=Piecewise[{{Sqrt[2 Pi x] (x/E)^x (Exp[1/(12 x)]),x>25},{x!,x≤25}}];

datm6=Import["SCmin_coduse500_6b.dat","Table"];
datM6=Import["SCmax_coduse500_6b.dat","Table"];
datm3=Import["SCmin_coduse500_3b.dat","Table"];
datM3=Import["SCmax_coduse500_3b.dat","Table"];

liston={};
Do[am=aa[[pos]];
faa=StringTake[am,{1}];
saa=StringTake[am,{2}];
bicod=tableau[[Position[tableau,Position[aa,am][[1,1]]][[1;;-1,1]],2]];
lon=Length[bicod];
bound=Append[Position[tableau,pos][[{1,-1},1]],1]+{0,1,0};
patm6=Select[datm6,#[[1]]==pos&];
patM6=Select[datM6,#[[1]]==pos&];
prob6=N[BinCounts[patm6[[1;;-1,2]],bound]];
n6=N[Length[patm6]];
Prob6=N[BinCounts[patM6[[1;;-1,2]],bound]];
N6=N[Length[patM6]];
pvmM=Table[(fac[n6] fac[N6] fac[Prob6[[i]]+prob6[[i]]] fac[(n6-prob6[[i]])+(N6-Prob6[[i]])])/(fac[prob6[[i]]] fac[Prob6[[i]]] fac[N6-Prob6[[i]]] fac[n6-prob6[[i]]] fac[n6+N6]),{i,lon}];
pos1a=Position[DEG,faa][[1,1]];
pos2a=Position[DEG,saa][[1,1]];
patm31=Select[datm3,#[[1]]==pos1a&];
patm32=Select[datm3,#[[1]]==pos2a&];
prob31=Drop[BinCounts[patm31[[1;;-1,2]],1],1];
prob32=Drop[BinCounts[patm32[[1;;-1,2]],1],1];
r6=Flatten[Outer[Times,prob31,prob32]];
eoij=N[r6 Length[datm6]/Length[datm3]^2];
enij=eoij Total[prob6]/Total[eoij];
xim=(prob6-enij)^2/enij;
patm31=Select[datM3,#[[1]]==pos1a&];
patm32=Select[datM3,#[[1]]==pos2a&];
prob31=Drop[BinCounts[patm31[[1;;-1,2]],1],1];
prob32=Drop[BinCounts[patm32[[1;;-1,2]],1],1];
r6=Flatten[Outer[Times,prob31,prob32]];
eoij=N[r6 Length[datM6]/Length[datM3]^2];
enij=eoij Total[Prob6]/Total[eoij];
xiM=(Prob6-enij)^2/enij;
Do[AppendTo[liston,{pos,am,bicod[[j]],Floor[prob6[[j]]],Floor[Prob6[[j]]],NumberForm[Chop[xim[[j]],10^-4],{Infinity,4}],NumberForm[Chop[xiM[[j]],10^-4],{Infinity,4}],NumberForm[Chop[-Log[10,pvmM[[j]]],10^-4],{Infinity,4}]}],{j,lon}],{pos,420}];
Export["res_SC.dat",liston,"TSV"];