python/cell_cycle_gating/process_brdu_ph3.py
def plot_scatter(dfc, dfm, object_level_directory,
                 object_level_files, filename='scatter.pdf'):
    """Plots BrdU and pH3 scatter and applies gating based on
        average gating of control wells

    Parameters
    ----------
    dfc : pandas dataframe
        table containing well level metadata and BrdU and pH3 gates per well.
    dfm : pandas dataframe
        table containing well level metadata mapping wells to cell_line,
        agents, and concentration.
    object_level_directory : str
        path to folder containing object level .txt files
    object_level_files : list of str
        list of object level .txt files.
    filename : Optional[str]
        Name of .pdf file in which the scatter plots for each well is saved.
        Default is 'scatter.pdf'.

    Returns
    -------
    dfc : pandas dataframe
        metadata file appended with pH3 and BrdU gates.
    """
    # Merge agent and concentration columns in the case of
    # combination experiments.
    dfm = process_metadata_file(dfm)

    # PDF setup
    pdf_pages = PdfPages(filename)
    nb_plots = len(object_level_files)
    nb_plots_per_page = 15
    # nb_pages = int(np.ceil(nb_plots / float(nb_plots_per_page)))
    plt.ioff()

    brdu_cutoff_mean = dfc.groupby(['agent', 'concentration'])[
        'brdu_cutoff'].mean().loc['DMSO'].values[0]
    ph3_cutoff_mean = dfc.groupby(['agent', 'concentration'])[
        'ph3_cutoff'].mean().loc['DMSO'].values[0]
    df_summary = pd.DataFrame()
    for i, file in enumerate(object_level_files):
        if i % nb_plots_per_page == 0:
            fig = plt.figure(figsize=(8.27, 11.69), dpi=100)
        fractions = {}
        df = pd.read_table('%s/%s' % (object_level_directory, file))
        well = re.search('result.(.*?)\[', file).group(1)
        well = "%s%s" % (well[0], well[1:].zfill(2))
        agent = dfm[dfm.well == well].agent.values[0]
        concentration = dfm[dfm.well == well].concentration.values[0]
        if concentration == '0':
            conc = ''
        else:
            conc = "(%s um)" % concentration
            conc.replace('0.3333333333333', '0.33')
        title = "%s - %s %s" % (well, agent, conc)
        fractions['well'] = well

        brdu = np.array(df['Nuclei Selected - Nucleus A647 Mean'].tolist())
        # dna = np.array(df['Nuclei Selected - DNA Content'].tolist())
        ph3 = np.array(df['Nuclei Selected - Nucleus A488 Mean'].tolist())

        high_brdu = brdu[brdu > brdu_cutoff_mean]
        high_ph3 = ph3[ph3 > ph3_cutoff_mean]
        brduf = brdu[(brdu > brdu_cutoff_mean) & (ph3 > ph3_cutoff_mean)]
        ph3f = ph3[(brdu > brdu_cutoff_mean) & (ph3 > ph3_cutoff_mean)]
        fractions['total_cells'] = len(brdu)
        fractions['ph3_positive'] = len(high_ph3)
        fractions['brdu_positive'] = len(high_brdu)
        fractions['double_positive'] = len(brduf)
        fractions['brdu_cutoff_control_mean'] = brdu_cutoff_mean
        fractions['ph3_cutoff_control_mean'] = ph3_cutoff_mean
        fraction_s_phase = 100 * (len(brduf)/len(brdu))
        # fig = plt.figure()
        rel_pos = np.unravel_index(i % nb_plots_per_page, (5, 3))
        ax = plt.subplot2grid((5, 3), rel_pos)
        ax.scatter(brdu, ph3, color='black', s=10)
        ax.scatter(brduf, ph3f, color='red', s=10)
        ax.set_yscale('log')
        ax.set_xscale('log')
        ax.set_title(title, fontsize=6)
        ax.set_xlabel('BrdU')
        ax.set_ylabel('pH3')
        # ylim = ax.get_ylim()
        # xlim = ax.get_xlim()
        ax.text(1.5e3, ph3_cutoff_mean+500, '%.2f%%' % fraction_s_phase,
                fontsize=10, color='black')
        ax.set_ylim(1e2, 2.5e4)
        ax.set_xlim(50, 5e3)
        ax.plot([brdu_cutoff_mean, brdu_cutoff_mean], [1e2, 2.5e4],
                'black', linewidth=1)
        ax.plot([50, 5e3], [ph3_cutoff_mean, ph3_cutoff_mean],
                'black', linewidth=1)
        df_summary = df_summary.append(fractions, ignore_index=True)

        if (i + 1) % nb_plots_per_page == 0 or (i + 1) == nb_plots:
            plt.tight_layout()
            pdf_pages.savefig(fig)

    dfc.index = dfc['well']
    df_summary.index = df_summary['well']
    dfc = pd.concat([dfc, df_summary], axis=1)
    pdf_pages.close()
    return dfc
Plots BrdU and pH3 scatter and applies gating based on
        average gating of control wells

    Parameters
    ----------
    dfc : pandas dataframe
        table containing well level metadata and BrdU and pH3 gates per well.
    dfm : pandas dataframe
        table containing well level metadata mapping wells to cell_line,
        agents, and concentration.
    object_level_directory : str
        path to folder containing object level .txt files
    object_level_files : list of str
        list of object level .txt files.
    filename : Optional[str]
        Name of .pdf file in which the scatter plots for each well is saved.
        Default is 'scatter.pdf'.

    Returns
    -------
    dfc : pandas dataframe
        metadata file appended with pH3 and BrdU gates.
    
Plots BrdU and pH3 scatter and applies gating based on
average gating of control wells

Parameters

dfc : pandas dataframe
table containing well level metadata and BrdU and pH3 gates per well.
dfm : pandas dataframe
table containing well level metadata mapping wells to cell_line,
agents, and concentration.
object_level_directory : str
path to folder containing object level .txt files
object_level_files : list of str
list of object level .txt files.
filename : Optional[str]
Name of .pdf file in which the scatter plots for each well is saved.

Returns

dfc : pandas dataframe
metadata file appended with pH3 and BrdU gates.
Plots BrdU and pH3 scatter and applies gating based on average gating of control wells Parameters dfc : pandas dataframe table containing well level metadata and BrdU and pH3 gates per well . dfm : pandas dataframe table containing well level metadata mapping wells to cell_line agents and concentration . object_level_directory : str path to folder containing object level . txt files object_level_files : list of str list of object level . txt files . filename : Optional [ str ] Name of . pdf file in which the scatter plots for each well is saved . Returns dfc : pandas dataframe metadata file appended with pH3 and BrdU gates .
def plot_scatter(dfc, dfm, object_level_directory,
                 object_level_files, filename='scatter.pdf'):
    dfm = process_metadata_file(dfm)
    pdf_pages = PdfPages(filename)
    nb_plots = len(object_level_files)
    nb_plots_per_page = 15
    plt.ioff()
    brdu_cutoff_mean = dfc.groupby(['agent', 'concentration'])[
        'brdu_cutoff'].mean().loc['DMSO'].values[0]
    ph3_cutoff_mean = dfc.groupby(['agent', 'concentration'])[
        'ph3_cutoff'].mean().loc['DMSO'].values[0]
    df_summary = pd.DataFrame()
    for i, file in enumerate(object_level_files):
        if i % nb_plots_per_page == 0:
            fig = plt.figure(figsize=(8.27, 11.69), dpi=100)
        fractions = {}
        df = pd.read_table('%s/%s' % (object_level_directory, file))
        well = re.search('result.(.*?)\[', file).group(1)
        well = "%s%s" % (well[0], well[1:].zfill(2))
        agent = dfm[dfm.well == well].agent.values[0]
        concentration = dfm[dfm.well == well].concentration.values[0]
        if concentration == '0':
            conc = ''
        else:
            conc = "(%s um)" % concentration
            conc.replace('0.3333333333333', '0.33')
        title = "%s - %s %s" % (well, agent, conc)
        fractions['well'] = well
        brdu = np.array(df['Nuclei Selected - Nucleus A647 Mean'].tolist())
        ph3 = np.array(df['Nuclei Selected - Nucleus A488 Mean'].tolist())
        high_brdu = brdu[brdu > brdu_cutoff_mean]
        high_ph3 = ph3[ph3 > ph3_cutoff_mean]
        brduf = brdu[(brdu > brdu_cutoff_mean) & (ph3 > ph3_cutoff_mean)]
        ph3f = ph3[(brdu > brdu_cutoff_mean) & (ph3 > ph3_cutoff_mean)]
        fractions['total_cells'] = len(brdu)
        fractions['ph3_positive'] = len(high_ph3)
        fractions['brdu_positive'] = len(high_brdu)
        fractions['double_positive'] = len(brduf)
        fractions['brdu_cutoff_control_mean'] = brdu_cutoff_mean
        fractions['ph3_cutoff_control_mean'] = ph3_cutoff_mean
        fraction_s_phase = 100 * (len(brduf)/len(brdu))
        rel_pos = np.unravel_index(i % nb_plots_per_page, (5, 3))
        ax = plt.subplot2grid((5, 3), rel_pos)
        ax.scatter(brdu, ph3, color='black', s=10)
        ax.scatter(brduf, ph3f, color='red', s=10)
        ax.set_yscale('log')
        ax.set_xscale('log')
        ax.set_title(title, fontsize=6)
        ax.set_xlabel('BrdU')
        ax.set_ylabel('pH3')
        ax.text(1.5e3, ph3_cutoff_mean+500, '%.2f%%' % fraction_s_phase,
                fontsize=10, color='black')
        ax.set_ylim(1e2, 2.5e4)
        ax.set_xlim(50, 5e3)
        ax.plot([brdu_cutoff_mean, brdu_cutoff_mean], [1e2, 2.5e4],
                'black', linewidth=1)
        ax.plot([50, 5e3], [ph3_cutoff_mean, ph3_cutoff_mean],
                'black', linewidth=1)
        df_summary = df_summary.append(fractions, ignore_index=True)
        if (i + 1) % nb_plots_per_page == 0 or (i + 1) == nb_plots:
            plt.tight_layout()
            pdf_pages.savefig(fig)
    dfc.index = dfc['well']
    df_summary.index = df_summary['well']
    dfc = pd.concat([dfc, df_summary], axis=1)
    pdf_pages.close()
    return dfc
def plot_scatter ( dfc , dfm , object_level_directory , object_level_files , filename = 'scatter.pdf' ) : dfm = process_metadata_file ( dfm ) pdf_pages = PdfPages ( filename ) nb_plots = len ( object_level_files ) nb_plots_per_page = 15 plt . ioff ( ) brdu_cutoff_mean = dfc . groupby ( [ 'agent' , 'concentration' ] ) [ 'brdu_cutoff' ] . mean ( ) . loc [ 'DMSO' ] . values [ 0 ] ph3_cutoff_mean = dfc . groupby ( [ 'agent' , 'concentration' ] ) [ 'ph3_cutoff' ] . mean ( ) . loc [ 'DMSO' ] . values [ 0 ] df_summary = pd . DataFrame ( ) for i , file in enumerate ( object_level_files ) : if i % nb_plots_per_page == 0 : fig = plt . figure ( figsize = ( 8.27 , 11.69 ) , dpi = 100 ) fractions = { } df = pd . read_table ( '%s/%s' % ( object_level_directory , file ) ) well = re . search ( 'result.(.*?)\[' , file ) . group ( 1 ) well = "%s%s" % ( well [ 0 ] , well [ 1 : ] . zfill ( 2 ) ) agent = dfm [ dfm . well == well ] . agent . values [ 0 ] concentration = dfm [ dfm . well == well ] . concentration . values [ 0 ] if concentration == '0' : conc = '' else : conc = "(%s um)" % concentration conc . replace ( '0.3333333333333' , '0.33' ) title = "%s - %s %s" % ( well , agent , conc ) fractions [ 'well' ] = well brdu = np . array ( df [ 'Nuclei Selected - Nucleus A647 Mean' ] . tolist ( ) ) ph3 = np . array ( df [ 'Nuclei Selected - Nucleus A488 Mean' ] . tolist ( ) ) high_brdu = brdu [ brdu > brdu_cutoff_mean ] high_ph3 = ph3 [ ph3 > ph3_cutoff_mean ] brduf = brdu [ ( brdu > brdu_cutoff_mean ) & ( ph3 > ph3_cutoff_mean ) ] ph3f = ph3 [ ( brdu > brdu_cutoff_mean ) & ( ph3 > ph3_cutoff_mean ) ] fractions [ 'total_cells' ] = len ( brdu ) fractions [ 'ph3_positive' ] = len ( high_ph3 ) fractions [ 'brdu_positive' ] = len ( high_brdu ) fractions [ 'double_positive' ] = len ( brduf ) fractions [ 'brdu_cutoff_control_mean' ] = brdu_cutoff_mean fractions [ 'ph3_cutoff_control_mean' ] = ph3_cutoff_mean fraction_s_phase = 100 * ( len ( brduf ) / len ( brdu ) ) rel_pos = np . unravel_index ( i % nb_plots_per_page , ( 5 , 3 ) ) ax = plt . subplot2grid ( ( 5 , 3 ) , rel_pos ) ax . scatter ( brdu , ph3 , color = 'black' , s = 10 ) ax . scatter ( brduf , ph3f , color = 'red' , s = 10 ) ax . set_yscale ( 'log' ) ax . set_xscale ( 'log' ) ax . set_title ( title , fontsize = 6 ) ax . set_xlabel ( 'BrdU' ) ax . set_ylabel ( 'pH3' ) ax . text ( 1.5e3 , ph3_cutoff_mean + 500 , '%.2f%%' % fraction_s_phase , fontsize = 10 , color = 'black' ) ax . set_ylim ( 1e2 , 2.5e4 ) ax . set_xlim ( 50 , 5e3 ) ax . plot ( [ brdu_cutoff_mean , brdu_cutoff_mean ] , [ 1e2 , 2.5e4 ] , 'black' , linewidth = 1 ) ax . plot ( [ 50 , 5e3 ] , [ ph3_cutoff_mean , ph3_cutoff_mean ] , 'black' , linewidth = 1 ) df_summary = df_summary . append ( fractions , ignore_index = True ) if ( i + 1 ) % nb_plots_per_page == 0 or ( i + 1 ) == nb_plots : plt . tight_layout ( ) pdf_pages . savefig ( fig ) dfc . index = dfc [ 'well' ] df_summary . index = df_summary [ 'well' ] dfc = pd . concat ( [ dfc , df_summary ] , axis = 1 ) pdf_pages . close ( ) return dfc
Plots BrdU and pH3 scatter and applies gating based on
average gating of control wells
Plots BrdU and pH3 scatter and applies gating based on average gating of control wells
"""Plots BrdU and pH3 scatter and applies gating based on
        average gating of control wells

    Parameters
    ----------
    dfc : pandas dataframe
        table containing well level metadata and BrdU and pH3 gates per well.
    dfm : pandas dataframe
        table containing well level metadata mapping wells to cell_line,
        agents, and concentration.
    object_level_directory : str
        path to folder containing object level .txt files
    object_level_files : list of str
        list of object level .txt files.
    filename : Optional[str]
        Name of .pdf file in which the scatter plots for each well is saved.
        Default is 'scatter.pdf'.

    Returns
    -------
    dfc : pandas dataframe
        metadata file appended with pH3 and BrdU gates.
    """ # Merge agent and concentration columns in the case of # combination experiments. # PDF setup # nb_pages = int(np.ceil(nb_plots / float(nb_plots_per_page))) # dna = np.array(df['Nuclei Selected - DNA Content'].tolist()) # fig = plt.figure() # ylim = ax.get_ylim() # xlim = ax.get_xlim()
dfc
dfm
object_level_directory
object_level_files
filename