lama/stats/permutation_stats/run_permutation_stats.py
def run(wt_dir: Path,
        mut_dir: Path,
        out_dir: Path,
        num_perms: int,
        label_info: Path = None,
        label_map_path: Path = None,
        line_fdr: float = 0.05,
        specimen_fdr: float = 0.2,
        normalise_to_whole_embryo: bool = True,
        qc_file: Path = None,
        voxel_size: float = 1.0):
    """
    Run the permutation-based stats pipeline

    Parameters
    ----------
    wt_dir
        Root of the wild type registration output
        This should contain an 'output' folder that contains a single baseline folder that contains multiple specimen folders
    mut_dir
        Root of the mutant registration output
        This should contain 'output' folder that contains multiple mutant lines folder, each containing one or more mutant specimen folders
    out_dir
        Where to store the intermediate results of the permutation testing
    num_perms
        number of permutations to do
    log_dependent
        if True, apply numpy.log to all the dependent values (organ volumes)
    label_info
        if supplied, use it to annotate the results with label names. Also can be used to filter certain labels from the
        analysis using the 'no_analysis' column
    line_fdr
        the FDR threshold at which to accept line level calls
    specimen_fdr
        the FDR threshold at which to accept specimen-level calls
    normalise_to_whole_embryo:
        Whether to divide the organ each organ volume by whole embryo volume
    qc_file
        csv indicating labels from specimens that should be excluded from the analysis
        columns:
        - id: the specimen id
        - line: the line id
        - label: the label to exclude (int)
        - label_name (optional)
    voxel_size
        For calcualting organ volumes
    """
    # Collate all the staging and organ volume data into csvs
    np.random.seed(999)
    init_logging(out_dir / 'stats.log')
    logging.info(git_log())
    logging.info(f'Running {__name__} with following commands\n{common.command_line_agrs()}')

    logging.info('Searching for staging data')
    wt_staging = get_staging_data(wt_dir)
    mut_staging = get_staging_data(mut_dir)

    logging.info('searching for organ volume data')
    wt_organ_vol = get_organ_volume_data(wt_dir)
    mut_organ_vol = get_organ_volume_data(mut_dir)

    # data
    # index: spec_id
    # cols: label_nums, with staging and line columns at the end
    data = prepare_data(wt_organ_vol,
                        wt_staging,
                        mut_organ_vol,
                        mut_staging,
                        label_meta=label_info,
                        normalise_to_whole_embryo=normalise_to_whole_embryo,
                        qc_file=qc_file)

    # Make plots
    # data_for_plots = data.copy()
    # data_for_plots.columns = [x.strip('x') for x in data_for_plots.columns]  # Strip any xs
    # # If data has been normalised to WEV revert back for plots
    # if normalise_to_whole_embryo:
    #     for col in data_for_plots.columns:
    #         if col.isdigit():
    #             data_for_plots[col] = data_for_plots[col] * data_for_plots['staging']
    lines_root_dir = out_dir / 'lines'
    #make_plots(data_for_plots, label_info, lines_root_dir, voxel_size=voxel_size)

    # Keep a record of the input data used in the analsysis
    data.to_csv(out_dir / 'input_data.csv')

    # Keep raw data for plotting
    # raw_wt_vols = wt_organ_vol.copy()   # These includes QCd speciemns need to remove

    out_dir.mkdir(exist_ok=True, parents=True)  # Root directory for output

    # make directory to store distributions and thresholds
    dists_out = out_dir / 'distributions'
    dists_out.mkdir(exist_ok=True)

    # Get the null distributions
    logging.info('Generating null distribution')
    line_null, specimen_null = distributions.null(data, num_perms)

    # with open(dists_out / 'null_ids.yaml', 'w') as fh:
    #     yaml.dump(null_ids, fh)

    null_line_pvals_file = dists_out / 'null_line_dist_pvalues.csv'
    null_specimen_pvals_file = dists_out / 'null_specimen_dist_pvalues.csv'

    # Write the null distributions to file
    line_null.to_csv(null_line_pvals_file)
    specimen_null.to_csv(null_specimen_pvals_file)

    # Get the alternative p-value distribution (and t-values now (2 and 3)
    logging.info('Generating alternative distribution')
    line_alt, spec_alt, line_alt_t, spec_alt_t = distributions.alternative(data)

    line_alt_pvals_file = dists_out / 'alt_line_dist_pvalues.csv'
    spec_alt_pvals_file = dists_out / 'alt_specimen_dist_pvalues.csv'

    # Write the alternative distributions to file
    line_alt.to_csv(line_alt_pvals_file)
    spec_alt.to_csv(spec_alt_pvals_file)

    line_organ_thresholds = p_thresholds.get_thresholds(line_null, line_alt)
    specimen_organ_thresholds = p_thresholds.get_thresholds(specimen_null, spec_alt)

    line_thresholds_path = dists_out / 'line_organ_p_thresholds.csv'
    spec_thresholds_path = dists_out / 'specimen_organ_p_thresholds.csv'

    line_organ_thresholds.to_csv(line_thresholds_path)
    specimen_organ_thresholds.to_csv(spec_thresholds_path)

    logging.info('Annotating lines')

    lines_root_dir = out_dir / 'lines'
    lines_root_dir.mkdir(exist_ok=True)

    # Annotate lines
    logging.info(f"Annotating lines, using a FDR threshold of {line_fdr}")
    line_hits = annotate(line_organ_thresholds, line_alt, lines_root_dir, label_info=label_info,
             label_map=label_map_path, write_thresholded_inv_labels=True, fdr_threshold=line_fdr, t_values=line_alt_t,
             organ_volumes=data)

    line_hits.to_csv(out_dir / 'line_hits.csv')

    # Annotate specimens
    logging.info(f"Annotating specimens, using a FDR threshold of {specimen_fdr}")
    spec_hits = annotate(specimen_organ_thresholds, spec_alt, lines_root_dir, is_line_level=False,
                         label_info=label_info, label_map=label_map_path, fdr_threshold=specimen_fdr, t_values=spec_alt_t,
                         organ_volumes=data)

    spec_hits.to_csv(out_dir / 'specimen_level_hits.csv')

    # Make plots
    data_for_plots = data.copy()
    data_for_plots.columns = [x.strip('x') for x in data_for_plots.columns]  # Strip any xs
    # If data has been normalised to WEV revert back for plots
    if normalise_to_whole_embryo:
        for col in data_for_plots.columns:
            if col.isdigit():
                data_for_plots[col] = data_for_plots[col] * data_for_plots['staging']

    make_plots(data_for_plots, label_info, lines_root_dir, voxel_size=voxel_size)

    # Get specimen info. Currently just the WEV z-score to highlight specimens that are too small/large
    spec_info_file = out_dir / 'specimen_info.csv'
    write_specimen_info(wt_staging, mut_staging, spec_info_file)

    dist_plot_root = out_dir / 'distribution_plots'
    line_plot_dir = dist_plot_root / 'line_level'
    line_plot_dir.mkdir(parents=True, exist_ok=True)
    pvalue_dist_plots(line_null, line_alt, line_organ_thresholds, line_plot_dir)

    specimen_plot_dir = dist_plot_root / 'specimen_level'
    specimen_plot_dir.mkdir(parents=True, exist_ok=True)

    pvalue_dist_plots(specimen_null, spec_alt.drop(columns=['line']), specimen_organ_thresholds, specimen_plot_dir)

    heatmaps_for_permutation_stats(lines_root_dir)

    Run the permutation-based stats pipeline

    Parameters
    ----------
    wt_dir
        Root of the wild type registration output
        This should contain an 'output' folder that contains a single baseline folder that contains multiple specimen folders
    mut_dir
        Root of the mutant registration output
        This should contain 'output' folder that contains multiple mutant lines folder, each containing one or more mutant specimen folders
    out_dir
        Where to store the intermediate results of the permutation testing
    num_perms
        number of permutations to do
    log_dependent
        if True, apply numpy.log to all the dependent values (organ volumes)
    label_info
        if supplied, use it to annotate the results with label names. Also can be used to filter certain labels from the
        analysis using the 'no_analysis' column
    line_fdr
        the FDR threshold at which to accept line level calls
    specimen_fdr
        the FDR threshold at which to accept specimen-level calls
    normalise_to_whole_embryo:
        Whether to divide the organ each organ volume by whole embryo volume
    qc_file
        csv indicating labels from specimens that should be excluded from the analysis
        columns:
        - id: the specimen id
        - line: the line id
        - label: the label to exclude (int)
        - label_name (optional)
    voxel_size
        For calcualting organ volumes
    
Run the permutation-based stats pipeline
Parameters

wt_dir
Root of the wild type registration output
This should contain an 'output' folder that contains a single baseline folder that contains multiple specimen folders
mut_dir
Root of the mutant registration output
This should contain 'output' folder that contains multiple mutant lines folder, each containing one or more mutant specimen folders
out_dir
Where to store the intermediate results of the permutation testing
num_perms
number of permutations to do
log_dependent
if True, apply numpy.log to all the dependent values (organ volumes)
label_info
if supplied, use it to annotate the results with label names.
Run the permutation - based stats pipeline Parameters wt_dir Root of the wild type registration output This should contain an ' output ' folder that contains a single baseline folder that contains multiple specimen folders mut_dir Root of the mutant registration output This should contain ' output ' folder that contains multiple mutant lines folder each containing one or more mutant specimen folders out_dir Where to store the intermediate results of the permutation testing num_perms number of permutations to do log_dependent if True apply numpy . log to all the dependent values ( organ volumes ) label_info if supplied use it to annotate the results with label names .
def run(wt_dir: Path,
        mut_dir: Path,
        out_dir: Path,
        num_perms: int,
        label_info: Path = None,
        label_map_path: Path = None,
        line_fdr: float = 0.05,
        specimen_fdr: float = 0.2,
        normalise_to_whole_embryo: bool = True,
        qc_file: Path = None,
        voxel_size: float = 1.0):
    np.random.seed(999)
    init_logging(out_dir / 'stats.log')
    logging.info(git_log())
    logging.info(f'Running {__name__} with following commands\n{common.command_line_agrs()}')
    logging.info('Searching for staging data')
    wt_staging = get_staging_data(wt_dir)
    mut_staging = get_staging_data(mut_dir)
    logging.info('searching for organ volume data')
    wt_organ_vol = get_organ_volume_data(wt_dir)
    mut_organ_vol = get_organ_volume_data(mut_dir)
    data = prepare_data(wt_organ_vol,
                        wt_staging,
                        mut_organ_vol,
                        mut_staging,
                        label_meta=label_info,
                        normalise_to_whole_embryo=normalise_to_whole_embryo,
                        qc_file=qc_file)
    _for_plots = data.copy()
    _for_plots.columns = [x.strip('x') for x in data_for_plots.columns]  
    lines_root_dir = out_dir / 'lines'
    data.to_csv(out_dir / 'input_data.csv')
    out_dir.mkdir(exist_ok=True, parents=True)  
    dists_out = out_dir / 'distributions'
    dists_out.mkdir(exist_ok=True)
    logging.info('Generating null distribution')
    line_null, specimen_null = distributions.null(data, num_perms)
    null_line_pvals_file = dists_out / 'null_line_dist_pvalues.csv'
    null_specimen_pvals_file = dists_out / 'null_specimen_dist_pvalues.csv'
    line_null.to_csv(null_line_pvals_file)
    specimen_null.to_csv(null_specimen_pvals_file)
    logging.info('Generating alternative distribution')
    line_alt, spec_alt, line_alt_t, spec_alt_t = distributions.alternative(data)
    line_alt_pvals_file = dists_out / 'alt_line_dist_pvalues.csv'
    spec_alt_pvals_file = dists_out / 'alt_specimen_dist_pvalues.csv'
    line_alt.to_csv(line_alt_pvals_file)
    spec_alt.to_csv(spec_alt_pvals_file)
    line_organ_thresholds = p_thresholds.get_thresholds(line_null, line_alt)
    specimen_organ_thresholds = p_thresholds.get_thresholds(specimen_null, spec_alt)
    line_thresholds_path = dists_out / 'line_organ_p_thresholds.csv'
    spec_thresholds_path = dists_out / 'specimen_organ_p_thresholds.csv'
    line_organ_thresholds.to_csv(line_thresholds_path)
    specimen_organ_thresholds.to_csv(spec_thresholds_path)
    logging.info('Annotating lines')
    lines_root_dir = out_dir / 'lines'
    lines_root_dir.mkdir(exist_ok=True)
    logging.info(f"Annotating lines, using a FDR threshold of {line_fdr}")
    line_hits = annotate(line_organ_thresholds, line_alt, lines_root_dir, label_info=label_info,
             label_map=label_map_path, write_thresholded_inv_labels=True, fdr_threshold=line_fdr, t_values=line_alt_t,
             organ_volumes=data)
    line_hits.to_csv(out_dir / 'line_hits.csv')
    logging.info(f"Annotating specimens, using a FDR threshold of {specimen_fdr}")
    spec_hits = annotate(specimen_organ_thresholds, spec_alt, lines_root_dir, is_line_level=False,
                         label_info=label_info, label_map=label_map_path, fdr_threshold=specimen_fdr, t_values=spec_alt_t,
                         organ_volumes=data)
    spec_hits.to_csv(out_dir / 'specimen_level_hits.csv')
    data_for_plots = data.copy()
    data_for_plots.columns = [x.strip('x') for x in data_for_plots.columns]  
    if normalise_to_whole_embryo:
        for col in data_for_plots.columns:
            if col.isdigit():
                data_for_plots[col] = data_for_plots[col] * data_for_plots['staging']
    make_plots(data_for_plots, label_info, lines_root_dir, voxel_size=voxel_size)
    spec_info_file = out_dir / 'specimen_info.csv'
    write_specimen_info(wt_staging, mut_staging, spec_info_file)
    dist_plot_root = out_dir / 'distribution_plots'
    line_plot_dir = dist_plot_root / 'line_level'
    line_plot_dir.mkdir(parents=True, exist_ok=True)
    pvalue_dist_plots(line_null, line_alt, line_organ_thresholds, line_plot_dir)
    specimen_plot_dir = dist_plot_root / 'specimen_level'
    specimen_plot_dir.mkdir(parents=True, exist_ok=True)
    pvalue_dist_plots(specimen_null, spec_alt.drop(columns=['line']), specimen_organ_thresholds, specimen_plot_dir)
    heatmaps_for_permutation_stats(lines_root_dir)
def run ( wt_dir : Path , mut_dir : Path , out_dir : Path , num_perms : int , label_info : Path = None , label_map_path : Path = None , line_fdr : float = 0.05 , specimen_fdr : float = 0.2 , normalise_to_whole_embryo : bool = True , qc_file : Path = None , voxel_size : float = 1.0 ) : np . random . seed ( 999 ) init_logging ( out_dir / 'stats.log' ) logging . info ( git_log ( ) ) logging . info ( f'Running {__name__} with following commands\n{common.command_line_agrs()}' ) logging . info ( 'Searching for staging data' ) wt_staging = get_staging_data ( wt_dir ) mut_staging = get_staging_data ( mut_dir ) logging . info ( 'searching for organ volume data' ) wt_organ_vol = get_organ_volume_data ( wt_dir ) mut_organ_vol = get_organ_volume_data ( mut_dir ) data = prepare_data ( wt_organ_vol , wt_staging , mut_organ_vol , mut_staging , label_meta = label_info , normalise_to_whole_embryo = normalise_to_whole_embryo , qc_file = qc_file ) lines_root_dir = out_dir / 'lines' data . to_csv ( out_dir / 'input_data.csv' ) out_dir . mkdir ( exist_ok = True , parents = True ) dists_out = out_dir / 'distributions' dists_out . mkdir ( exist_ok = True ) logging . info ( 'Generating null distribution' ) line_null , specimen_null = distributions . null ( data , num_perms ) null_line_pvals_file = dists_out / 'null_line_dist_pvalues.csv' null_specimen_pvals_file = dists_out / 'null_specimen_dist_pvalues.csv' line_null . to_csv ( null_line_pvals_file ) specimen_null . to_csv ( null_specimen_pvals_file ) logging . info ( 'Generating alternative distribution' ) line_alt , spec_alt , line_alt_t , spec_alt_t = distributions . alternative ( data ) line_alt_pvals_file = dists_out / 'alt_line_dist_pvalues.csv' spec_alt_pvals_file = dists_out / 'alt_specimen_dist_pvalues.csv' line_alt . to_csv ( line_alt_pvals_file ) spec_alt . to_csv ( spec_alt_pvals_file ) line_organ_thresholds = p_thresholds . get_thresholds ( line_null , line_alt ) specimen_organ_thresholds = p_thresholds . get_thresholds ( specimen_null , spec_alt ) line_thresholds_path = dists_out / 'line_organ_p_thresholds.csv' spec_thresholds_path = dists_out / 'specimen_organ_p_thresholds.csv' line_organ_thresholds . to_csv ( line_thresholds_path ) specimen_organ_thresholds . to_csv ( spec_thresholds_path ) logging . info ( 'Annotating lines' ) lines_root_dir = out_dir / 'lines' lines_root_dir . mkdir ( exist_ok = True ) logging . info ( f"Annotating lines, using a FDR threshold of {line_fdr}" ) line_hits = annotate ( line_organ_thresholds , line_alt , lines_root_dir , label_info = label_info , label_map = label_map_path , write_thresholded_inv_labels = True , fdr_threshold = line_fdr , t_values = line_alt_t , organ_volumes = data ) line_hits . to_csv ( out_dir / 'line_hits.csv' ) logging . info ( f"Annotating specimens, using a FDR threshold of {specimen_fdr}" ) spec_hits = annotate ( specimen_organ_thresholds , spec_alt , lines_root_dir , is_line_level = False , label_info = label_info , label_map = label_map_path , fdr_threshold = specimen_fdr , t_values = spec_alt_t , organ_volumes = data ) spec_hits . to_csv ( out_dir / 'specimen_level_hits.csv' ) data_for_plots = data . copy ( ) data_for_plots . columns = [ x . strip ( 'x' ) for x in data_for_plots . columns ] if normalise_to_whole_embryo : for col in data_for_plots . columns : if col . isdigit ( ) : data_for_plots [ col ] = data_for_plots [ col ] * data_for_plots [ 'staging' ] make_plots ( data_for_plots , label_info , lines_root_dir , voxel_size = voxel_size ) spec_info_file = out_dir / 'specimen_info.csv' write_specimen_info ( wt_staging , mut_staging , spec_info_file ) dist_plot_root = out_dir / 'distribution_plots' line_plot_dir = dist_plot_root / 'line_level' line_plot_dir . mkdir ( parents = True , exist_ok = True ) pvalue_dist_plots ( line_null , line_alt , line_organ_thresholds , line_plot_dir ) specimen_plot_dir = dist_plot_root / 'specimen_level' specimen_plot_dir . mkdir ( parents = True , exist_ok = True ) pvalue_dist_plots ( specimen_null , spec_alt . drop ( columns = [ 'line' ] ) , specimen_organ_thresholds , specimen_plot_dir ) heatmaps_for_permutation_stats ( lines_root_dir )
Run the permutation-based stats pipeline
Parameters
Run the permutation - based stats pipeline Parameters
"""
    Run the permutation-based stats pipeline

    Parameters
    ----------
    wt_dir
        Root of the wild type registration output
        This should contain an 'output' folder that contains a single baseline folder that contains multiple specimen folders
    mut_dir
        Root of the mutant registration output
        This should contain 'output' folder that contains multiple mutant lines folder, each containing one or more mutant specimen folders
    out_dir
        Where to store the intermediate results of the permutation testing
    num_perms
        number of permutations to do
    log_dependent
        if True, apply numpy.log to all the dependent values (organ volumes)
    label_info
        if supplied, use it to annotate the results with label names. Also can be used to filter certain labels from the
        analysis using the 'no_analysis' column
    line_fdr
        the FDR threshold at which to accept line level calls
    specimen_fdr
        the FDR threshold at which to accept specimen-level calls
    normalise_to_whole_embryo:
        Whether to divide the organ each organ volume by whole embryo volume
    qc_file
        csv indicating labels from specimens that should be excluded from the analysis
        columns:
        - id: the specimen id
        - line: the line id
        - label: the label to exclude (int)
        - label_name (optional)
    voxel_size
        For calcualting organ volumes
    """ # Collate all the staging and organ volume data into csvs # data # index: spec_id # cols: label_nums, with staging and line columns at the end # Make plots # data_for_plots = data.copy() # data_for_plots.columns = [x.strip('x') for x in data_for_plots.columns]  # Strip any xs # # If data has been normalised to WEV revert back for plots # if normalise_to_whole_embryo: #     for col in data_for_plots.columns: #         if col.isdigit(): #             data_for_plots[col] = data_for_plots[col] * data_for_plots['staging'] #make_plots(data_for_plots, label_info, lines_root_dir, voxel_size=voxel_size) # Keep a record of the input data used in the analsysis # Keep raw data for plotting # raw_wt_vols = wt_organ_vol.copy()   # These includes QCd speciemns need to remove # Root directory for output # make directory to store distributions and thresholds # Get the null distributions # with open(dists_out / 'null_ids.yaml', 'w') as fh: #     yaml.dump(null_ids, fh) # Write the null distributions to file # Get the alternative p-value distribution (and t-values now (2 and 3) # Write the alternative distributions to file # Annotate lines # Annotate specimens # Make plots # Strip any xs # If data has been normalised to WEV revert back for plots # Get specimen info. Currently just the WEV z-score to highlight specimens that are too small/large
wt_dir
Path
mut_dir
Path
out_dir
Path
num_perms
int
label_info
Path
label_map_path
Path
line_fdr
float
specimen_fdr
float
normalise_to_whole_embryo
bool
qc_file
Path
voxel_size
float