# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Introduction

import DSGRN
import Berry_2019_figures_results as Berry
from min_interval_posets import posets, poset_distance
from copy import deepcopy
from numpy import linspace
from IPython import display
import matplotlib.pyplot as plt
# from matplotlib import cm
from importlib import reload
from matplotlib import rc
rc('text', usetex=True)
fontsize=20
rc('axes', labelsize=fontsize)    # fontsize of the x and y labels
rc('xtick', labelsize=fontsize)    # fontsize of the tick labels
rc('ytick', labelsize=fontsize)    # fontsize of the tick labels
rc('legend', fontsize=12)    # legend fontsize
# %matplotlib inline

wt1_file = "WT1_WT2_microarray_interpolated/wt1_microarray_coregenes_lifepoints_interpol_trim.csv"
wt2_file = "WT1_WT2_microarray_interpolated/wt2_microarray_coregenes_lifepoints_interpol.csv"


# graph data
def make_fig(fname,savename,start_time=None,end_time=None,names=None):
    curves = Berry.row(fname)
    subset_curves = deepcopy(curves)
    if names is not None:
        for name in curves:
            if name not in names:
                subset_curves.pop(name)
    for name,curve in sorted(subset_curves.items()):
        n = curve.normalize()
        if start_time is not None and end_time is not None:
            n = curve.trim(start_time,end_time)
        times,vals = zip(*n.items())
        plt.plot(times,vals,label=r"${}$".format(name))
    lgd = plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
    plt.ylabel(r"\textbf{normalized expression}")
    plt.xlabel(r"\textbf{time points}")
    plt.savefig(savename,bbox_extra_artists=(lgd,), bbox_inches='tight') 
    plt.savefig(savename, bbox_inches='tight') 
    display.display(plt.show())
    return [name for name in sorted(curves)]
    


# +
start_time = 26
end_time = 170
names = ["CDC20"]
epsilons = [0.00, 0.01, 0.02, 0.03, 0.04, 0.05]


# -

_ = make_fig(wt1_file,"time_series_wt1_{}_trimmed.pdf".format(names[0]),start_time,end_time,names)
# _ = make_fig(wt2_file,"time_series_rep2_trimmed.pdf",start_time,end_time,names)

intervals = Berry.getintervals(wt1_file,"row",epsilons,names,start_time,end_time)
for intv in intervals[names[0]]:
    print(intv)


# +
def makeboxes(intervals,start_time,end_time,savename="intervals_CDC20.pdf"):
    marker = "s"
    for k,i in enumerate(intervals):
        eps = i[0]
        ints = i[1]
        for j in ints:
            xl = (int(j[0][0]),int(j[0][1])+1)
            x = list(range(*xl))
            if j[1][1] == "min":
                color = "k"
            else:
                color = "r"
            if len(x) == 1:
                alpha = 0.25
            else:
                alpha = 0.1
            plt.plot(x,[eps]*len(x),linestyle=None,marker=marker,color=color,alpha=alpha)
#     plt.xlim(start_time,end_time) 
    plt.yticks(epsilons)
    plt.ylabel(r"$\mbox{{\Huge$\epsilon$}}$")
    plt.xlabel(r"\textbf{time points}")
    plt.savefig(savename, bbox_inches='tight') 
    display.display(plt.show())
            
    
    

# -

makeboxes(intervals[names[0]],start_time,end_time)

# +
names = ["CDC20","NDD1"]
epsilons = [0.0,0.03,0.05]
_ = make_fig(wt1_file,"time_series_wt1_{}_{}_trimmed.pdf".format(names[0],names[1]),start_time,end_time,names)

posets1 = Berry.getposets(wt1_file,"row",epsilons,names,start_time,end_time)


# -

network = DSGRN.Network("\n".join(["{} : {}".format(name,name) for name in names]))
network.specification()


def make_posets(p,network):
    events = list(p[1][0])
    event_ordering = list(p[1][1]) 
    poe = DSGRN.PosetOfExtrema(network, events, event_ordering)
    return poe,p[0]


for p in posets1:
    poe, eps = make_posets(p,network)
    print("Epsilon = {}".format(eps))
    display.display(DSGRN.DrawGraph(poe))
    with open("poset_intro_{}.dot".format(int(100*eps)),"w") as f:
        f.write(poe.graphviz())





StarcoderdataJupyterScriptsDedupFiltered