python/cell_cycle_gating/run_cell_cycle_gating.py
def gate_well(df, dfm_ord=None, ph3_channel=True, ldr_channel=True,
              px_edu=None, x_ldr=None, control_based_gating=False,
              control_gates=None, fudge_gates=np.array([0, 0, 0, 0]),
              fig=None, plot_num=0, ldr_gates=None, ldr_control_cutoff=2):
    """Gating on a single well
    
    Parameters
    ----------
    df : pandas.DataFrame
       single cell data for a single well of the plate

    Return
    ------
    fractions : dict
       Fraction of cells classified to be in each phase of the cell cycle
    gates : dict
       Value of EdU and DNA at gates for given cell line and well
    cell_identity : list[int]
       mapping of each cell to a phase of the cell cycle
    """
    well = df.well.unique()[0]
    well = "%s%s" % (well[0], well[1:].zfill(2))
    edu = np.array(df['edu'].tolist())
    dna = np.array(df['dna'].tolist())
    edu = edu.astype(np.float)
    #edu[edu < 0] = np.nan

    if ph3_channel:
        ph3 = np.array(df['ph3'].tolist())
        cells_notnan = ~(np.isnan(edu) | np.isnan(ph3))
        ph3 = ph3[cells_notnan]
    else:
        cells_notnan = ~np.isnan(edu)
    edu = edu[cells_notnan]
    #if edu.min() < 0:
    #    edu += np.abs(edu.min())
    dna = dna[cells_notnan]
         
    # Get phases based on DNA and EdU
    if dfm_ord is not None:
        # dfm_ord.index = dfm_ord.well
        cell_line = dfm_ord.loc[well, 'cell_line']
        agent = dfm_ord.loc[well, 'agent']
        conc = dfm_ord.loc[well, 'concentration']
        title = "%s %s %s (%s um)" % (well, cell_line, agent, conc)
    else:
        title = well
    if control_gates is not None:
       control_dna_gates = control_gates[control_gates.cell_line == cell_line][
           'dna_gates'].mean()
       #control_edu_gates = control_gates[control_gates.cell_line == cell_line][
       #    'edu_gates'].mean()
       control_edu_gates=None
       control_ldr_cutoff =  control_gates[control_gates.cell_line == cell_line]['ldr_cutoff'].mean()
       control_edu_cutoff_bounds = [control_gates[control_gates.cell_line == cell_line]['mean_Gphase_edu'].mean(),
                                   control_gates[control_gates.cell_line == cell_line]['mean_Sphase_edu'].mean()]
                                   

    else:
        control_dna_gates = None
        control_edu_gates = None
        control_ldr_cutoff = 1.2
        control_edu_cutoff_bounds = None
        #control_edu_gates = rb_gates

    # Get live dead
    if ldr_channel:
        ldrint = np.array(df['ldrint'].tolist())
        ldrint = ldrint[cells_notnan]
        logldr_peak = dcf_int.get_ldr_peak_val(ldrint)
        ldr_gates, ldr_lims = dcf_int.get_ldrgates(ldrint, ldr_control_cutoff, peak_loc=control_ldr_cutoff)
        logint = np.log10(ldrint)
        logint[np.isnan(logint)] = -10 
        ldr_inner = ((ldr_gates[1] >= logint) & (logint >= ldr_gates[0]))
        if np.sum(ldr_inner) < 50:
            #dna = None
            dna_gates=None
        else:
            #dna = df['dna']
            dna_gates = dcf_int.get_dna_gating(dna, ldrint, ldr_gates)
            # if dna_gates is None:
            #     dna=None
        cell_fate_dict, outcome = dcf_int.live_dead(ldrint, ldr_gates=ldr_gates, dna=dna,
                                                    dna_gates= dna_gates,
                                                    ldr_control_cutoff=ldr_control_cutoff)
        live_cols = [s for s in list(cell_fate_dict.keys()) if 'alive' in s]
        dead_cols = [s for s in list(cell_fate_dict.keys()) if 'dead' in s]
        a = 0
        d = 0
        for col in live_cols:
            a += cell_fate_dict[col]
        for col in dead_cols:
            d += cell_fate_dict[col]
    else:
        outcome = np.array([1] * len(dna))
    try:   
        fractions, cell_identity, gates = cc.plot_summary(dna[outcome>=1], edu[outcome>=1], fig=None,
                                                          title=title,
                                                          plot='scatter',
                                                          plot_num=plot_num,
                                                          px_edu=px_edu,
                                                          control_dna_gates=control_dna_gates,
                                                          control_edu_gates=control_edu_gates,
                                                          fudge_gates=np.array(fudge_gates),
                                                          control_edu_cutoff_bounds=control_edu_cutoff_bounds)
    except ValueError:
        fractions = {}
        gates = {}
        cell_identity = np.array([0]*len(dna[outcome>=1]))
        

    # Revise phases based on pH3
    if ph3_channel:
        #grid_size = (5, 2)
        #plot_num = plot_num % 10
        #rel_pos = np.unravel_index(plot_num, grid_size)
        #axp = plt.subplot2grid(grid_size, rel_pos)
        
        log_ph3 = pf.compute_log_ph3(ph3[outcome>=1])
        #x_ph3 = np.arange(2.5, 8, 0.02)
        #f_ph3 = get_kde(log_ph3, x_ph3)
        #peak_amp, peak_loc, peak_width = findpeaks(f_ph3.tolist(), npeaks=2, thresh=0.1)
        #if len(peak_amp) >= 2:
        #    cutoff_loc, _ = get_prominence_reference_level(
        #        f_ph3.tolist(), peak_amp[1], peak_loc[1])
        #    ph3_cutoff = x_ph3[cutoff_loc]
        #del_ph3 = x_ph3[peak_loc[0]] - log_ph3.min()
        #ph3_cutoff = x_ph3[peak_loc[0]] + del_ph3
        #else:
        f_ph3, ph3_cutoff, ph3_lims = pf.get_ph3_gates(ph3[outcome>=1], cell_identity)
        fractions, cell_identity = pf.evaluate_Mphase(log_ph3, ph3_cutoff, cell_identity)
        #sns.kdeplot(log_ph3, ax=axp)
        #axp.vlines(ph3_cutoff, 0, 2)
        #axp.text(ph3_cutoff+0.1, 1, str(fractions['M']))
        #axp.set_title(title, fontsize=6)

    edu_live = edu[outcome >=1]   
    fractions['mean_Sphase_edu'] = edu_live[cell_identity==2].mean()
    gates['mean_Sphase_edu'] = np.log10(edu_live[cell_identity==2].mean())
    gates['mean_Gphase_edu'] = np.log10(edu_live[cell_identity==1].mean())
    
    if ldr_channel:
        fractions['cell_count'] = a
        fractions['cell_count__dead'] = d
        gates['ldr_cutoff'] = logldr_peak
    fractions['well'] = well
    fractions['cell_count__total'] = len(dna)

    if dfm_ord is not None:
        gates['well'] = well
        gates['cell_line'] = cell_line


    return fractions, gates, cell_identity
Gating on a single well
    
    Parameters
    ----------
    df : pandas.DataFrame
       single cell data for a single well of the plate

    Return
    ------
    fractions : dict
       Fraction of cells classified to be in each phase of the cell cycle
    gates : dict
       Value of EdU and DNA at gates for given cell line and well
    cell_identity : list[int]
       mapping of each cell to a phase of the cell cycle
    
Gating on a single well
Parameters

df : pandas.DataFrame
single cell data for a single well of the plate

Return

fractions : dict
Fraction of cells classified to be in each phase of the cell cycle
gates : dict
Value of EdU and DNA at gates for given cell line and well
cell_identity : list[int]
mapping of each cell to a phase of the cell cycle
Gating on a single well Parameters df : pandas . DataFrame single cell data for a single well of the plate Return fractions : dict Fraction of cells classified to be in each phase of the cell cycle gates : dict Value of EdU and DNA at gates for given cell line and well cell_identity : list [ int ] mapping of each cell to a phase of the cell cycle
def gate_well(df, dfm_ord=None, ph3_channel=True, ldr_channel=True,
              px_edu=None, x_ldr=None, control_based_gating=False,
              control_gates=None, fudge_gates=np.array([0, 0, 0, 0]),
              fig=None, plot_num=0, ldr_gates=None, ldr_control_cutoff=2):
    well = df.well.unique()[0]
    well = "%s%s" % (well[0], well[1:].zfill(2))
    edu = np.array(df['edu'].tolist())
    dna = np.array(df['dna'].tolist())
    edu = edu.astype(np.float)
    if ph3_channel:
        ph3 = np.array(df['ph3'].tolist())
        cells_notnan = ~(np.isnan(edu) | np.isnan(ph3))
        ph3 = ph3[cells_notnan]
    else:
        cells_notnan = ~np.isnan(edu)
    edu = edu[cells_notnan]
    dna = dna[cells_notnan]
    if dfm_ord is not None:
        cell_line = dfm_ord.loc[well, 'cell_line']
        agent = dfm_ord.loc[well, 'agent']
        conc = dfm_ord.loc[well, 'concentration']
        title = "%s %s %s (%s um)" % (well, cell_line, agent, conc)
    else:
        title = well
    if control_gates is not None:
       control_dna_gates = control_gates[control_gates.cell_line == cell_line][
           'dna_gates'].mean()
       control_edu_gates=None
       control_ldr_cutoff =  control_gates[control_gates.cell_line == cell_line]['ldr_cutoff'].mean()
       control_edu_cutoff_bounds = [control_gates[control_gates.cell_line == cell_line]['mean_Gphase_edu'].mean(),
                                   control_gates[control_gates.cell_line == cell_line]['mean_Sphase_edu'].mean()]
    else:
        control_dna_gates = None
        control_edu_gates = None
        control_ldr_cutoff = 1.2
        control_edu_cutoff_bounds = None
    if ldr_channel:
        ldrint = np.array(df['ldrint'].tolist())
        ldrint = ldrint[cells_notnan]
        logldr_peak = dcf_int.get_ldr_peak_val(ldrint)
        ldr_gates, ldr_lims = dcf_int.get_ldrgates(ldrint, ldr_control_cutoff, peak_loc=control_ldr_cutoff)
        logint = np.log10(ldrint)
        logint[np.isnan(logint)] = -10 
        ldr_inner = ((ldr_gates[1] >= logint) & (logint >= ldr_gates[0]))
        if np.sum(ldr_inner) < 50:
            dna_gates=None
        else:
            dna_gates = dcf_int.get_dna_gating(dna, ldrint, ldr_gates)
        cell_fate_dict, outcome = dcf_int.live_dead(ldrint, ldr_gates=ldr_gates, dna=dna,
                                                    dna_gates= dna_gates,
                                                    ldr_control_cutoff=ldr_control_cutoff)
        live_cols = [s for s in list(cell_fate_dict.keys()) if 'alive' in s]
        dead_cols = [s for s in list(cell_fate_dict.keys()) if 'dead' in s]
        a = 0
        d = 0
        for col in live_cols:
            a += cell_fate_dict[col]
        for col in dead_cols:
            d += cell_fate_dict[col]
    else:
        outcome = np.array([1] * len(dna))
    try:   
        fractions, cell_identity, gates = cc.plot_summary(dna[outcome>=1], edu[outcome>=1], fig=None,
                                                          title=title,
                                                          plot='scatter',
                                                          plot_num=plot_num,
                                                          px_edu=px_edu,
                                                          control_dna_gates=control_dna_gates,
                                                          control_edu_gates=control_edu_gates,
                                                          fudge_gates=np.array(fudge_gates),
                                                          control_edu_cutoff_bounds=control_edu_cutoff_bounds)
    except ValueError:
        fractions = {}
        gates = {}
        cell_identity = np.array([0]*len(dna[outcome>=1]))
    if ph3_channel:
        log_ph3 = pf.compute_log_ph3(ph3[outcome>=1])
        f_ph3, ph3_cutoff, ph3_lims = pf.get_ph3_gates(ph3[outcome>=1], cell_identity)
        fractions, cell_identity = pf.evaluate_Mphase(log_ph3, ph3_cutoff, cell_identity)
    edu_live = edu[outcome >=1]   
    fractions['mean_Sphase_edu'] = edu_live[cell_identity==2].mean()
    gates['mean_Sphase_edu'] = np.log10(edu_live[cell_identity==2].mean())
    gates['mean_Gphase_edu'] = np.log10(edu_live[cell_identity==1].mean())
    if ldr_channel:
        fractions['cell_count'] = a
        fractions['cell_count__dead'] = d
        gates['ldr_cutoff'] = logldr_peak
    fractions['well'] = well
    fractions['cell_count__total'] = len(dna)
    if dfm_ord is not None:
        gates['well'] = well
        gates['cell_line'] = cell_line
    return fractions, gates, cell_identity
def gate_well ( df , dfm_ord = None , ph3_channel = True , ldr_channel = True , px_edu = None , x_ldr = None , control_based_gating = False , control_gates = None , fudge_gates = np . array ( [ 0 , 0 , 0 , 0 ] ) , fig = None , plot_num = 0 , ldr_gates = None , ldr_control_cutoff = 2 ) : well = df . well . unique ( ) [ 0 ] well = "%s%s" % ( well [ 0 ] , well [ 1 : ] . zfill ( 2 ) ) edu = np . array ( df [ 'edu' ] . tolist ( ) ) dna = np . array ( df [ 'dna' ] . tolist ( ) ) edu = edu . astype ( np . float ) if ph3_channel : ph3 = np . array ( df [ 'ph3' ] . tolist ( ) ) cells_notnan = ~ ( np . isnan ( edu ) | np . isnan ( ph3 ) ) ph3 = ph3 [ cells_notnan ] else : cells_notnan = ~ np . isnan ( edu ) edu = edu [ cells_notnan ] dna = dna [ cells_notnan ] if dfm_ord is not None : cell_line = dfm_ord . loc [ well , 'cell_line' ] agent = dfm_ord . loc [ well , 'agent' ] conc = dfm_ord . loc [ well , 'concentration' ] title = "%s %s %s (%s um)" % ( well , cell_line , agent , conc ) else : title = well if control_gates is not None : control_dna_gates = control_gates [ control_gates . cell_line == cell_line ] [ 'dna_gates' ] . mean ( ) control_edu_gates = None control_ldr_cutoff = control_gates [ control_gates . cell_line == cell_line ] [ 'ldr_cutoff' ] . mean ( ) control_edu_cutoff_bounds = [ control_gates [ control_gates . cell_line == cell_line ] [ 'mean_Gphase_edu' ] . mean ( ) , control_gates [ control_gates . cell_line == cell_line ] [ 'mean_Sphase_edu' ] . mean ( ) ] else : control_dna_gates = None control_edu_gates = None control_ldr_cutoff = 1.2 control_edu_cutoff_bounds = None if ldr_channel : ldrint = np . array ( df [ 'ldrint' ] . tolist ( ) ) ldrint = ldrint [ cells_notnan ] logldr_peak = dcf_int . get_ldr_peak_val ( ldrint ) ldr_gates , ldr_lims = dcf_int . get_ldrgates ( ldrint , ldr_control_cutoff , peak_loc = control_ldr_cutoff ) logint = np . log10 ( ldrint ) logint [ np . isnan ( logint ) ] = - 10 ldr_inner = ( ( ldr_gates [ 1 ] >= logint ) & ( logint >= ldr_gates [ 0 ] ) ) if np . sum ( ldr_inner ) < 50 : dna_gates = None else : dna_gates = dcf_int . get_dna_gating ( dna , ldrint , ldr_gates ) cell_fate_dict , outcome = dcf_int . live_dead ( ldrint , ldr_gates = ldr_gates , dna = dna , dna_gates = dna_gates , ldr_control_cutoff = ldr_control_cutoff ) live_cols = [ s for s in list ( cell_fate_dict . keys ( ) ) if 'alive' in s ] dead_cols = [ s for s in list ( cell_fate_dict . keys ( ) ) if 'dead' in s ] a = 0 d = 0 for col in live_cols : a += cell_fate_dict [ col ] for col in dead_cols : d += cell_fate_dict [ col ] else : outcome = np . array ( [ 1 ] * len ( dna ) ) try : fractions , cell_identity , gates = cc . plot_summary ( dna [ outcome >= 1 ] , edu [ outcome >= 1 ] , fig = None , title = title , plot = 'scatter' , plot_num = plot_num , px_edu = px_edu , control_dna_gates = control_dna_gates , control_edu_gates = control_edu_gates , fudge_gates = np . array ( fudge_gates ) , control_edu_cutoff_bounds = control_edu_cutoff_bounds ) except ValueError : fractions = { } gates = { } cell_identity = np . array ( [ 0 ] * len ( dna [ outcome >= 1 ] ) ) if ph3_channel : log_ph3 = pf . compute_log_ph3 ( ph3 [ outcome >= 1 ] ) f_ph3 , ph3_cutoff , ph3_lims = pf . get_ph3_gates ( ph3 [ outcome >= 1 ] , cell_identity ) fractions , cell_identity = pf . evaluate_Mphase ( log_ph3 , ph3_cutoff , cell_identity ) edu_live = edu [ outcome >= 1 ] fractions [ 'mean_Sphase_edu' ] = edu_live [ cell_identity == 2 ] . mean ( ) gates [ 'mean_Sphase_edu' ] = np . log10 ( edu_live [ cell_identity == 2 ] . mean ( ) ) gates [ 'mean_Gphase_edu' ] = np . log10 ( edu_live [ cell_identity == 1 ] . mean ( ) ) if ldr_channel : fractions [ 'cell_count' ] = a fractions [ 'cell_count__dead' ] = d gates [ 'ldr_cutoff' ] = logldr_peak fractions [ 'well' ] = well fractions [ 'cell_count__total' ] = len ( dna ) if dfm_ord is not None : gates [ 'well' ] = well gates [ 'cell_line' ] = cell_line return fractions , gates , cell_identity
Gating on a single well
Parameters
Gating on a single well Parameters
"""Gating on a single well
    
    Parameters
    ----------
    df : pandas.DataFrame
       single cell data for a single well of the plate

    Return
    ------
    fractions : dict
       Fraction of cells classified to be in each phase of the cell cycle
    gates : dict
       Value of EdU and DNA at gates for given cell line and well
    cell_identity : list[int]
       mapping of each cell to a phase of the cell cycle
    """ #edu[edu < 0] = np.nan #if edu.min() < 0: #    edu += np.abs(edu.min()) # Get phases based on DNA and EdU # dfm_ord.index = dfm_ord.well #control_edu_gates = control_gates[control_gates.cell_line == cell_line][ #    'edu_gates'].mean() #control_edu_gates = rb_gates # Get live dead #dna = None #dna = df['dna'] # if dna_gates is None: #     dna=None # Revise phases based on pH3 #grid_size = (5, 2) #plot_num = plot_num % 10 #rel_pos = np.unravel_index(plot_num, grid_size) #axp = plt.subplot2grid(grid_size, rel_pos) #x_ph3 = np.arange(2.5, 8, 0.02) #f_ph3 = get_kde(log_ph3, x_ph3) #peak_amp, peak_loc, peak_width = findpeaks(f_ph3.tolist(), npeaks=2, thresh=0.1) #if len(peak_amp) >= 2: #    cutoff_loc, _ = get_prominence_reference_level( #        f_ph3.tolist(), peak_amp[1], peak_loc[1]) #    ph3_cutoff = x_ph3[cutoff_loc] #del_ph3 = x_ph3[peak_loc[0]] - log_ph3.min() #ph3_cutoff = x_ph3[peak_loc[0]] + del_ph3 #else: #sns.kdeplot(log_ph3, ax=axp) #axp.vlines(ph3_cutoff, 0, 2) #axp.text(ph3_cutoff+0.1, 1, str(fractions['M'])) #axp.set_title(title, fontsize=6)
df
dfm_ord
ph3_channel
ldr_channel
px_edu
x_ldr
control_based_gating
control_gates
fudge_gates
fig
plot_num
ldr_gates
ldr_control_cutoff