#!/usr/bin/python

"""
Global patterns of genome evolution associated with generalist parasitism
Thomas Badet, Malick Mbengue, Olivier Navaud, Remi Peyraud, Adelin Barbacci, Sylvain Raffaele
"""

from pandas import *


def read(path):
	data = read_csv(path)
	return data

def mutation(data):
	# Simulates mutations 
	Datab = data.copy()
	for i,row in data.iterrows():
		mut = row[3:]*row[0]*0.01
		for codon in mut.index:
			Datab.N[Datab.Reference==codon]+=mut[codon]
		Datab.N[Datab.Reference == row.Reference] -= sum(mut)
	return Datab

def get_change(data,tps):
	# Returns the numbers of optimal, not-optimal and undiffercentiated codons
	opt = float(sum(data.N[data.optimal=="yes"]))/float(sum(data.N))
	n_opt = float(sum(data.N[data.optimal=="no"]))/float(sum(data.N))
	ind = float(sum(data.N[data.optimal=="diff"]))/float(sum(data.N))
	d = DataFrame([{'t':tps,'opti':opt,'non_opti':n_opt,'diff':ind},])
	return d

if __name__=="__main__":
	# Input file 
	D1 = read_csv('./Codon_mutations_Zymo_3.csv',sep='\t')
	# Number of optimal, not optimal and not differcentiated codons
	res = get_change(D1,0)
	for ii in range(1,1000):
		# Computing mutation
		D1 = mutation(D1)
		# Creating table of results
		res = concat([res,get_change(D1,ii)])
	# Storing output file
	res.to_csv("./res_python_Zymo_det.csv",sep='\t')
	print("finished")
