python/cell_cycle_gating/cellcycle_phases.py
def plot_summary(dna, edu, fig=None, x_dna=None, px_edu=None,
                 title=None, plot='all', plot_num=None,
                 control_dna_gates=None, control_edu_gates=None,
                 fudge_gates=np.array([0, 0, 0, 0]),
                 control_edu_cutoff_bounds=None):
    """Summary plots depicting kernel density estimates for EdU and DNA,
    phase candidates and cell cycle fractions
    
    Parameters
    ----------
    dna : 1d array
       DNA content across all cells in a well
    edu : 1d array
       EdU intensities across all cells in a well
    fig : figure object
    x_dna : 1d array
       uniformly spaced grid based on expected DNA content
    px_edu : 1d array
       uniformly spaced grid based on expected EdU intensities
    title : str
      plot title
    plot : str
       'all' if all summary plots are to be plotted,
       'scatter' to generate only scatrer plot
    plot_num : int
        the number which provides positional reference to place
        the subplot in larger plot

    Returns
    -------
    fractions : dict
        dictionary where keys are cell cycle phases and
        values are fractions of cells in each phase
    cell_id : 1d array
        membership of each cell in cell cycle phase (1=G1, 2=S, 3=G2)
    """
    gates = {}
    if plot == 'all':
        fig = plt.figure()
        gridspec.GridSpec(2, 3)
        ax1 = plt.subplot2grid((2, 3), (0, 0), colspan=1, rowspan=1)
        ax2 = plt.subplot2grid((2, 3), (0, 1), colspan=1, rowspan=1)
        ax3 = plt.subplot2grid((2, 3), (0, 2), colspan=1, rowspan=1)
        ax4 = plt.subplot2grid((2, 3), (1, 0), colspan=2, rowspan=1)
        ax5 = plt.subplot2grid((2, 3), (1, 2), colspan=1, rowspan=1)
    elif plot == 'scatter':
        grid_size = (5, 2)
        ax1 = None
        ax2 = None
        ax3 = None
        plot_num = plot_num % 10
        rel_pos = np.unravel_index(plot_num, grid_size)
        # print('rel_pos', rel_pos)
        ax4 = plt.subplot2grid(grid_size, rel_pos)
        ax5 = None
    elif plot is None:
        ax1=None
        ax2=None
        ax3=None
        ax4=None
        ax5=None
    if px_edu is None:
        px_edu = np.arange(-0.2, 5.3, .02)
    if x_dna is None:
        x_dna = np.arange(2.5, 8, 0.02)

    log_dna = compute_log_dna(dna, x_dna)

    edu_shift, offset_edu, edu_g1_max, edu_s_min = get_edu_gates(edu, px_edu,
                                                                 ax=ax1)
    log_edu = compute_log_edu(edu, px_edu, offset_edu)
    h = get_2d_histogram(log_dna, x_dna, log_edu, px_edu)
    g = smooth_1d(h, 5)
    f = smooth_1d(g.T, 5).T

    try:
        
        peak_candidates = iterate_2D_peak(h, x_dna, px_edu, nsmooth=5)
        phase_candidates = get_phase_candidates(peak_candidates,
                                                edu_shift, edu_s_min)
        
        dna_g1_loc = get_g1_dna_peak(log_dna, x_dna, log_edu, edu_shift,
                                     edu_s_min, edu_g1_max,
                                     phase_candidates, ax=ax2)

        low_edu_peaks = get_low_edu_peaks(log_edu, px_edu, edu_shift,
                                          edu_g1_max,
                                          log_dna, dna_g1_loc, nsmooth=5)
        edu_peaks, edu_cutoff, edu_lims, _ = get_high_edu_peaks(log_edu,
                                                                px_edu,
                                                                edu_shift,
                                                                low_edu_peaks,
                                                                log_dna,
                                                                dna_g1_loc,
                                                                nsmooth=5)
        edu_gates = edu_sns_gates(edu, control_edu_cutoff_bounds)

        dna_s_loc = get_s_phase_dna_loc(log_dna, x_dna, dna_g1_loc,
                                        log_edu, edu_cutoff, ax=ax2)

        dna_cutoff, dna_g2_loc = get_dna_cutoff(log_dna, x_dna, log_edu,
                                                edu_cutoff, dna_g1_loc,
                                                dna_s_loc,  # dna_fig,
                                                phase_candidates, 5, ax=ax2)
        dna_gates, dna_lims = get_dna_gates(log_dna, x_dna, dna_g1_loc, dna_g2_loc,
                                            dna_cutoff, log_edu, edu_cutoff)

        dna_peaks = [dna_g1_loc, dna_s_loc, dna_g2_loc]
        edu_peaks = edu_peaks + [edu_peaks[0]]

        if ax3 is not None:
            plot_2D_peaks(log_dna, x_dna, edu, px_edu,
                          f, peak_candidates, phase_candidates,
                          dna_gates, dna_lims,
                          edu_gates, edu_lims,
                          nsmooth=5, ax=ax3)

    except ValueError:
        edu_gates = np.array([0.0, 0.0])
        edu_lims = [0, 4]
        dna_gates = np.array([0.0]*4)
        dna_lims = [3.8, 5.2]
        dna_peaks=None
        edu_peaks=None

    if control_edu_gates is not None:
        edu_gates = control_override(control_edu_gates, edu_gates, 0.2)
    gates['edu_gates'] = edu_gates
    gates['edu_lims'] = edu_lims

    if control_dna_gates is not None:
        dna_gates = control_override(control_dna_gates, dna_gates, 0.1)
        # Update DNA limits
        dna_gl = list(dna_lims) + [g-0.1 for g in dna_gates]
        dna_gl2 = list(dna_lims) + [g+0.1 for g in dna_gates]
        dna_lims = np.array([np.min(dna_gl), np.max(dna_gl2)])
    
    dna_gates += fudge_gates
    gates['dna_gates'] = dna_gates
    gates['dna_lims'] = dna_lims
    
    if ax2 is not None:
        ax2.plot([dna_gates[i] for i in [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3]],
                 [np.max(ax2.get_ylim()) * i
                  for i in [0, 1, np.nan, 0, 1, np.nan, 0, 1, np.nan, 0, 1]],
                 '--', color='red')
        ax2.set_xlim(dna_lims)
        



    plot_edu_dna_scatter(dna, edu, offset_edu,
                         dna_gates, edu_gates,
                         dna_lims, edu_lims,
                         x_dna=None, px_edu=px_edu,
                         ax=ax4)

    fractions, cell_id, peaks = evaluate_cell_cycle_phase(log_dna, dna_gates,
                                                          x_dna, 
                                                          log_edu, edu_gates,
                                                          px_edu,
                                                          dna_peaks, edu_peaks,
                                                          nsmooth=5, ax=ax5)
    if ax4 is not None:
        ax4.set_title(title, fontsize=6)
    if plot == 'all':
        fig.tight_layout()
        fig.set_size_inches(w=10, h=6)
        if title:
            fig.savefig('cell_cycle_phases_%s.png' % title, dpi=300)
    return fractions, cell_id, gates
Summary plots depicting kernel density estimates for EdU and DNA,
    phase candidates and cell cycle fractions
    
    Parameters
    ----------
    dna : 1d array
       DNA content across all cells in a well
    edu : 1d array
       EdU intensities across all cells in a well
    fig : figure object
    x_dna : 1d array
       uniformly spaced grid based on expected DNA content
    px_edu : 1d array
       uniformly spaced grid based on expected EdU intensities
    title : str
      plot title
    plot : str
       'all' if all summary plots are to be plotted,
       'scatter' to generate only scatrer plot
    plot_num : int
        the number which provides positional reference to place
        the subplot in larger plot

    Returns
    -------
    fractions : dict
        dictionary where keys are cell cycle phases and
        values are fractions of cells in each phase
    cell_id : 1d array
        membership of each cell in cell cycle phase (1=G1, 2=S, 3=G2)
    
Summary plots depicting kernel density estimates for EdU and DNA,
phase candidates and cell cycle fractions

Parameters



Returns

fractions : dict
dictionary where keys are cell cycle phases and
values are fractions of cells in each phase
cell_id : 1d array
membership of each cell in cell cycle phase (1=G1, 2=S, 3=G2)
Summary plots depicting kernel density estimates for EdU and DNA phase candidates and cell cycle fractions Parameters Returns fractions : dict dictionary where keys are cell cycle phases and values are fractions of cells in each phase cell_id : 1d array membership of each cell in cell cycle phase ( 1 = G1 2 = S 3 = G2 )
def plot_summary(dna, edu, fig=None, x_dna=None, px_edu=None,
                 title=None, plot='all', plot_num=None,
                 control_dna_gates=None, control_edu_gates=None,
                 fudge_gates=np.array([0, 0, 0, 0]),
                 control_edu_cutoff_bounds=None):
    gates = {}
    if plot == 'all':
        fig = plt.figure()
        gridspec.GridSpec(2, 3)
        ax1 = plt.subplot2grid((2, 3), (0, 0), colspan=1, rowspan=1)
        ax2 = plt.subplot2grid((2, 3), (0, 1), colspan=1, rowspan=1)
        ax3 = plt.subplot2grid((2, 3), (0, 2), colspan=1, rowspan=1)
        ax4 = plt.subplot2grid((2, 3), (1, 0), colspan=2, rowspan=1)
        ax5 = plt.subplot2grid((2, 3), (1, 2), colspan=1, rowspan=1)
    elif plot == 'scatter':
        grid_size = (5, 2)
        ax1 = None
        ax2 = None
        ax3 = None
        plot_num = plot_num % 10
        rel_pos = np.unravel_index(plot_num, grid_size)
        ax4 = plt.subplot2grid(grid_size, rel_pos)
        ax5 = None
    elif plot is None:
        ax1=None
        ax2=None
        ax3=None
        ax4=None
        ax5=None
    if px_edu is None:
        px_edu = np.arange(-0.2, 5.3, .02)
    if x_dna is None:
        x_dna = np.arange(2.5, 8, 0.02)
    log_dna = compute_log_dna(dna, x_dna)
    edu_shift, offset_edu, edu_g1_max, edu_s_min = get_edu_gates(edu, px_edu,
                                                                 ax=ax1)
    log_edu = compute_log_edu(edu, px_edu, offset_edu)
    h = get_2d_histogram(log_dna, x_dna, log_edu, px_edu)
    g = smooth_1d(h, 5)
    f = smooth_1d(g.T, 5).T
    try:
        peak_candidates = iterate_2D_peak(h, x_dna, px_edu, nsmooth=5)
        phase_candidates = get_phase_candidates(peak_candidates,
                                                edu_shift, edu_s_min)
        dna_g1_loc = get_g1_dna_peak(log_dna, x_dna, log_edu, edu_shift,
                                     edu_s_min, edu_g1_max,
                                     phase_candidates, ax=ax2)
        low_edu_peaks = get_low_edu_peaks(log_edu, px_edu, edu_shift,
                                          edu_g1_max,
                                          log_dna, dna_g1_loc, nsmooth=5)
        edu_peaks, edu_cutoff, edu_lims, _ = get_high_edu_peaks(log_edu,
                                                                px_edu,
                                                                edu_shift,
                                                                low_edu_peaks,
                                                                log_dna,
                                                                dna_g1_loc,
                                                                nsmooth=5)
        edu_gates = edu_sns_gates(edu, control_edu_cutoff_bounds)
        dna_s_loc = get_s_phase_dna_loc(log_dna, x_dna, dna_g1_loc,
                                        log_edu, edu_cutoff, ax=ax2)
        dna_cutoff, dna_g2_loc = get_dna_cutoff(log_dna, x_dna, log_edu,
                                                edu_cutoff, dna_g1_loc,
                                                dna_s_loc,  
                                                phase_candidates, 5, ax=ax2)
        dna_gates, dna_lims = get_dna_gates(log_dna, x_dna, dna_g1_loc, dna_g2_loc,
                                            dna_cutoff, log_edu, edu_cutoff)
        dna_peaks = [dna_g1_loc, dna_s_loc, dna_g2_loc]
        edu_peaks = edu_peaks + [edu_peaks[0]]
        if ax3 is not None:
            plot_2D_peaks(log_dna, x_dna, edu, px_edu,
                          f, peak_candidates, phase_candidates,
                          dna_gates, dna_lims,
                          edu_gates, edu_lims,
                          nsmooth=5, ax=ax3)
    except ValueError:
        edu_gates = np.array([0.0, 0.0])
        edu_lims = [0, 4]
        dna_gates = np.array([0.0]*4)
        dna_lims = [3.8, 5.2]
        dna_peaks=None
        edu_peaks=None
    if control_edu_gates is not None:
        edu_gates = control_override(control_edu_gates, edu_gates, 0.2)
    gates['edu_gates'] = edu_gates
    gates['edu_lims'] = edu_lims
    if control_dna_gates is not None:
        dna_gates = control_override(control_dna_gates, dna_gates, 0.1)
        dna_gl = list(dna_lims) + [g-0.1 for g in dna_gates]
        dna_gl2 = list(dna_lims) + [g+0.1 for g in dna_gates]
        dna_lims = np.array([np.min(dna_gl), np.max(dna_gl2)])
    dna_gates += fudge_gates
    gates['dna_gates'] = dna_gates
    gates['dna_lims'] = dna_lims
    if ax2 is not None:
        ax2.plot([dna_gates[i] for i in [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3]],
                 [np.max(ax2.get_ylim()) * i
                  for i in [0, 1, np.nan, 0, 1, np.nan, 0, 1, np.nan, 0, 1]],
                 '--', color='red')
        ax2.set_xlim(dna_lims)
    plot_edu_dna_scatter(dna, edu, offset_edu,
                         dna_gates, edu_gates,
                         dna_lims, edu_lims,
                         x_dna=None, px_edu=px_edu,
                         ax=ax4)
    fractions, cell_id, peaks = evaluate_cell_cycle_phase(log_dna, dna_gates,
                                                          x_dna, 
                                                          log_edu, edu_gates,
                                                          px_edu,
                                                          dna_peaks, edu_peaks,
                                                          nsmooth=5, ax=ax5)
    if ax4 is not None:
        ax4.set_title(title, fontsize=6)
    if plot == 'all':
        fig.tight_layout()
        fig.set_size_inches(w=10, h=6)
        if title:
            fig.savefig('cell_cycle_phases_%s.png' % title, dpi=300)
    return fractions, cell_id, gates
def plot_summary ( dna , edu , fig = None , x_dna = None , px_edu = None , title = None , plot = 'all' , plot_num = None , control_dna_gates = None , control_edu_gates = None , fudge_gates = np . array ( [ 0 , 0 , 0 , 0 ] ) , control_edu_cutoff_bounds = None ) : gates = { } if plot == 'all' : fig = plt . figure ( ) gridspec . GridSpec ( 2 , 3 ) ax1 = plt . subplot2grid ( ( 2 , 3 ) , ( 0 , 0 ) , colspan = 1 , rowspan = 1 ) ax2 = plt . subplot2grid ( ( 2 , 3 ) , ( 0 , 1 ) , colspan = 1 , rowspan = 1 ) ax3 = plt . subplot2grid ( ( 2 , 3 ) , ( 0 , 2 ) , colspan = 1 , rowspan = 1 ) ax4 = plt . subplot2grid ( ( 2 , 3 ) , ( 1 , 0 ) , colspan = 2 , rowspan = 1 ) ax5 = plt . subplot2grid ( ( 2 , 3 ) , ( 1 , 2 ) , colspan = 1 , rowspan = 1 ) elif plot == 'scatter' : grid_size = ( 5 , 2 ) ax1 = None ax2 = None ax3 = None plot_num = plot_num % 10 rel_pos = np . unravel_index ( plot_num , grid_size ) ax4 = plt . subplot2grid ( grid_size , rel_pos ) ax5 = None elif plot is None : ax1 = None ax2 = None ax3 = None ax4 = None ax5 = None if px_edu is None : px_edu = np . arange ( - 0.2 , 5.3 , .02 ) if x_dna is None : x_dna = np . arange ( 2.5 , 8 , 0.02 ) log_dna = compute_log_dna ( dna , x_dna ) edu_shift , offset_edu , edu_g1_max , edu_s_min = get_edu_gates ( edu , px_edu , ax = ax1 ) log_edu = compute_log_edu ( edu , px_edu , offset_edu ) h = get_2d_histogram ( log_dna , x_dna , log_edu , px_edu ) g = smooth_1d ( h , 5 ) f = smooth_1d ( g . T , 5 ) . T try : peak_candidates = iterate_2D_peak ( h , x_dna , px_edu , nsmooth = 5 ) phase_candidates = get_phase_candidates ( peak_candidates , edu_shift , edu_s_min ) dna_g1_loc = get_g1_dna_peak ( log_dna , x_dna , log_edu , edu_shift , edu_s_min , edu_g1_max , phase_candidates , ax = ax2 ) low_edu_peaks = get_low_edu_peaks ( log_edu , px_edu , edu_shift , edu_g1_max , log_dna , dna_g1_loc , nsmooth = 5 ) edu_peaks , edu_cutoff , edu_lims , _ = get_high_edu_peaks ( log_edu , px_edu , edu_shift , low_edu_peaks , log_dna , dna_g1_loc , nsmooth = 5 ) edu_gates = edu_sns_gates ( edu , control_edu_cutoff_bounds ) dna_s_loc = get_s_phase_dna_loc ( log_dna , x_dna , dna_g1_loc , log_edu , edu_cutoff , ax = ax2 ) dna_cutoff , dna_g2_loc = get_dna_cutoff ( log_dna , x_dna , log_edu , edu_cutoff , dna_g1_loc , dna_s_loc , phase_candidates , 5 , ax = ax2 ) dna_gates , dna_lims = get_dna_gates ( log_dna , x_dna , dna_g1_loc , dna_g2_loc , dna_cutoff , log_edu , edu_cutoff ) dna_peaks = [ dna_g1_loc , dna_s_loc , dna_g2_loc ] edu_peaks = edu_peaks + [ edu_peaks [ 0 ] ] if ax3 is not None : plot_2D_peaks ( log_dna , x_dna , edu , px_edu , f , peak_candidates , phase_candidates , dna_gates , dna_lims , edu_gates , edu_lims , nsmooth = 5 , ax = ax3 ) except ValueError : edu_gates = np . array ( [ 0.0 , 0.0 ] ) edu_lims = [ 0 , 4 ] dna_gates = np . array ( [ 0.0 ] * 4 ) dna_lims = [ 3.8 , 5.2 ] dna_peaks = None edu_peaks = None if control_edu_gates is not None : edu_gates = control_override ( control_edu_gates , edu_gates , 0.2 ) gates [ 'edu_gates' ] = edu_gates gates [ 'edu_lims' ] = edu_lims if control_dna_gates is not None : dna_gates = control_override ( control_dna_gates , dna_gates , 0.1 ) dna_gl = list ( dna_lims ) + [ g - 0.1 for g in dna_gates ] dna_gl2 = list ( dna_lims ) + [ g + 0.1 for g in dna_gates ] dna_lims = np . array ( [ np . min ( dna_gl ) , np . max ( dna_gl2 ) ] ) dna_gates += fudge_gates gates [ 'dna_gates' ] = dna_gates gates [ 'dna_lims' ] = dna_lims if ax2 is not None : ax2 . plot ( [ dna_gates [ i ] for i in [ 0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 ] ] , [ np . max ( ax2 . get_ylim ( ) ) * i for i in [ 0 , 1 , np . nan , 0 , 1 , np . nan , 0 , 1 , np . nan , 0 , 1 ] ] , '--' , color = 'red' ) ax2 . set_xlim ( dna_lims ) plot_edu_dna_scatter ( dna , edu , offset_edu , dna_gates , edu_gates , dna_lims , edu_lims , x_dna = None , px_edu = px_edu , ax = ax4 ) fractions , cell_id , peaks = evaluate_cell_cycle_phase ( log_dna , dna_gates , x_dna , log_edu , edu_gates , px_edu , dna_peaks , edu_peaks , nsmooth = 5 , ax = ax5 ) if ax4 is not None : ax4 . set_title ( title , fontsize = 6 ) if plot == 'all' : fig . tight_layout ( ) fig . set_size_inches ( w = 10 , h = 6 ) if title : fig . savefig ( 'cell_cycle_phases_%s.png' % title , dpi = 300 ) return fractions , cell_id , gates
Summary plots depicting kernel density estimates for EdU and DNA,
phase candidates and cell cycle fractions
Summary plots depicting kernel density estimates for EdU and DNA phase candidates and cell cycle fractions
"""Summary plots depicting kernel density estimates for EdU and DNA,
    phase candidates and cell cycle fractions
    
    Parameters
    ----------
    dna : 1d array
       DNA content across all cells in a well
    edu : 1d array
       EdU intensities across all cells in a well
    fig : figure object
    x_dna : 1d array
       uniformly spaced grid based on expected DNA content
    px_edu : 1d array
       uniformly spaced grid based on expected EdU intensities
    title : str
      plot title
    plot : str
       'all' if all summary plots are to be plotted,
       'scatter' to generate only scatrer plot
    plot_num : int
        the number which provides positional reference to place
        the subplot in larger plot

    Returns
    -------
    fractions : dict
        dictionary where keys are cell cycle phases and
        values are fractions of cells in each phase
    cell_id : 1d array
        membership of each cell in cell cycle phase (1=G1, 2=S, 3=G2)
    """ # print('rel_pos', rel_pos) # dna_fig, # Update DNA limits
dna
edu
fig
x_dna
px_edu
title
plot
plot_num
control_dna_gates
control_edu_gates
fudge_gates
control_edu_cutoff_bounds