FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
growth_control.py 35.5 KiB
Newer Older
import os
import math
Argyris Z's avatar
Argyris Z committed
from itertools import permutations, product, repeat, takewhile
from functools import partial, reduce
from operator import add, itemgetter
from collections import defaultdict
from subprocess import call
Argyris Z's avatar
Argyris Z committed
from typing import Dict, List, Tuple, Set

import ast
import astor
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib as mpl
from scipy.signal import argrelmax
from scipy.stats import gaussian_kde
from scipy.stats import ttest_ind
from more_itertools import interleave_longest
Argyris Z's avatar
Argyris Z committed
from mpl_toolkits.mplot3d import Axes3D

import common.statesN as f
import common.seg as seg
import common.edict as edict
import common.lin as lin
from common.seg import STissue
import grates as grs
import patternTransitions as p

geneNms: List[str] = ['"AG"',
                      '"AHP6"',
                      '"ANT"',
                      '"AP1"',
                      '"AP2"',
                      '"AP3"',
                      '"AS1"',
                      '"ATML1"',
                      '"CLV3"',
                      '"CUC1_2_3"',
                      '"ETTIN"',
                      '"FIL"',
                      '"LFY"',
                      '"MP"',
                      '"PHB_PHV"',
                      '"PI"',
                      '"PUCHI"',
                      '"REV"',
                      '"SEP1"',
                      '"SEP2"',
                      '"SEP3"',
                      '"STM"',
                      '"SUP"',
                      '"SVP"',
                      '"WUS"']

mpl.rcParams.update(mpl.rcParamsDefault)
sts_ts = f.states_per_t()

fst = itemgetter(0)
snd = itemgetter(1)

Argyris Z's avatar
Argyris Z committed

def set_plot_params(fontsize=11):
Argyris Z's avatar
Argyris Z committed
    sns.set_style("whitegrid", {'grid.linestyle': '--'})
    plt.rc('xtick', labelsize=fontsize)
    plt.rc('ytick', labelsize=fontsize)
    plt.rc('axes', labelsize=fontsize)
    params = {'legend.fontsize': fontsize}
    plt.rcParams.update(params)

Argyris Z's avatar
Argyris Z committed

class BExpr2():

    def __init__(self, e_str):
        self.e = ge(e_str)

    def __hash__(self):
        gns = sorted([hash(g) for g in getGenes(getTopExpr(self.e))])
        op = hash(ast.dump(getTopExpr(self.e).op))

        return sum(gns) + op

    def __eq__(self, other):
        return hash(self) == hash(other)

    def __repr__(self):
        import astor
        return astor.to_source(self.e).replace('"', '').strip()


class BExpr2Region():

    def __init__(self, t, ts, e_str):
        self.t = t
        self.e = ge(e_str)
        self.ts = ts

    def __hash__(self):
        return sum(get_sts_expr(self.ts, self.e, sts_ts[self.t]))

    def __eq__(self, other):
        return hash(self) == hash(other)


class GPairRegions():
    def __init__(self, t, ts, g1, g2):
        self.t = t
        self.g1 = g1
        self.g2 = g2
        self.e1 = ge("'{g1}' and '{g2}'".format(g1=g1, g2=g2))
        self.e2 = ge("'{g1}' and not '{g2}'".format(g1=g1, g2=g2))
        self.ts = ts

    def __hash__(self):
        sts = sts_ts[self.t]
        sts_e1 = get_sts_expr(self.ts, self.e1, sts)
        sts_e2 = get_sts_expr(self.ts, self.e2, sts)

        return (hash(frozenset(sts_e1)) +
                hash(frozenset(sts_e2)))

    def __eq__(self, other):
        return hash(self) == hash(other)

    def plot(self, txt=""):
        d = dict()
        grs_state = get_grs_state()
        sts = sts_ts[self.t]
        sts_e1 = get_sts_expr(self.ts, self.e1, sts)
        sts_e2 = get_sts_expr(self.ts, self.e2, sts)
        gr_e1 = meanor0(reduce(add, [grs_state[st] for st in sts_e1], []))
        gr_e2 = meanor0(reduce(add, [grs_state[st] for st in sts_e2], []))

        he, le = [e for e, gr in sorted([(self.e1, gr_e1), (self.e2, gr_e2)],
                                        key=snd, reverse=True)]

        for c in self.ts:
            if seg.evalB(getTopExpr(he), c.exprs):
Argyris Z's avatar
Argyris Z committed
                d[c.cid] = 1.0
            else:
                d[c.cid] = 0.5

        for c in self.ts:
            if seg.evalB(getTopExpr(le), c.exprs):
Argyris Z's avatar
Argyris Z committed
                d[c.cid] = 0.0

        f = plot_ts_q_(self.ts, d,
                        lb="{g1}-{g2}".format(g1=self.g1, g2=self.g2),
                        bounds=(0.1, 0.9), txt=txt)

        return f, d

    def get_regions(self) -> Tuple[Set[int], Set[int]]:
        grs_state = get_grs_state()
        sts = sts_ts[self.t]
        sts_e1 = get_sts_expr(self.ts, self.e1, sts)
        sts_e2 = get_sts_expr(self.ts, self.e2, sts)
        gr_e1 = meanor0(reduce(add, [grs_state[st] for st in sts_e1], []))
        gr_e2 = meanor0(reduce(add, [grs_state[st] for st in sts_e2], []))
Argyris Z's avatar
Argyris Z committed
        he, le = [e for e, gr in sorted([(self.e1, gr_e1), (self.e2, gr_e2)],
                                        key=snd, reverse=True)]

        r1 = list()
        r2 = list()
        for c in self.ts:
            if seg.evalB(getTopExpr(he), c.exprs):
                r1.append(c.cid)
            elif seg.evalB(getTopExpr(le), c.exprs):
                r2.append(c.cid)
Argyris Z's avatar
Argyris Z committed
        return set(r1), set(r2)

    def plot_distr(self):
        set_plot_params(fontsize=12)
        sns.set_style("white")
        cls = sns.color_palette("coolwarm")
        grs_state = get_grs_state()
        sts = sts_ts[self.t]
        sts_e1 = get_sts_expr(self.ts, self.e1, sts)
        sts_e2 = get_sts_expr(self.ts, self.e2, sts)
        grs_e1 = reduce(add, [grs_state[st] for st in sts_e1])
        grs_e2 = reduce(add, [grs_state[st] for st in sts_e2])

        gr_e1 = np.mean(reduce(add, [grs_state[st] for st in sts_e1]))
        gr_e2 = np.mean(reduce(add, [grs_state[st] for st in sts_e2]))

        h_grs, l_grs = [e for e, gr in sorted([(grs_e1, gr_e1), (grs_e2, gr_e2)],
                                              key=snd, reverse=True)]

        he, le = [e for e, gr in sorted([(self.e1, gr_e1), (self.e2, gr_e2)],
                                        key=snd, reverse=True)]

        fig = plt.figure(figsize=(3.5, 3.7))
        ax = fig.add_subplot('111')

        ax.set_xlim(-0.05, 0.18)
        
        sns.distplot(h_grs, bins=10, color=cls[-1], kde=True, ax=ax,
Loading
Loading full blame...