def munge_sumstats(j, ancestry, ld_scores, sumstat_file, N, munging_script):
    """ Set up commands for munging summary statsitcs. This command assumes that summary statistics
    files are of the pan-ancestry variety and as such will subset the summary statistics to only
    look at the relevant ancestry groups. Will also generate a log to track munging steps while also
    keeping only variants that are found in the LD score file. Several additional munging steps are taken
    as part of `munge_manual.py` including:

    - Merge CHR/POS/REF/ALT into a single column (SNP)
    - Ensure ref is A1
    - Add a sample size column (N)
    - Produce Z scores from p-values signed by betas.
    - Remove any rows with NAs in pvalue, AF, beta, low_confidence columns
    - Remove low confidence variants
    - Remove variants with AF < 0.01
    - Remove variants with out of bounds p-values

    Will return the following line if ancestry specific columns are not found:
    `5 (or 6 in the case of case/control) ancestry specific columns not found; exiting.`

    Parameters
    ----------
    j : :obj: `Job`

    ancestry: :obj: `str`
        Ancestry abbreviation that will be used to subset the summary statistcs.
    ld_scores: :obj: `ResourceGroup`
        A resource group with the `.ldscore.gz`, `.M`, and `.M.5.50` files required for
        sumstat munging.
    sumstat_file: :obj: `InputResourceFile`
        Location of the summary statistics file, with format `.tsv.gz`.
    N : :obj:`float`
        Sample size.
    munging_script : :obj: `InputResourceFile`
        Location of the munging script, `munge_manual.py`

    Returns
    -------
    Tuple:

    :obj: `str`
        Command to be run for performing munging.
    :obj: `Job`

    :obj: `str`
        Location of the sumstats file to be outputted.
    :obj: `ResourceFile`
        Location of the munging log to be outputted.
    """
    j = j.storage('20Gi')
    j = j.memory('8Gi')
    command_pre_script = f"""
        gunzip -c {ld_scores}.l2.ldscore.gz > {j.extracted_ldscore}
        gunzip -c {sumstat_file} > {j.extracted_sumstats}
        touch {j.logout}

        awk 'FNR==NR {{ a[$2]; next }} {{ b=$1":"$2":"$3":"$4; if (b in a) {{ print }} }}' {j.extracted_ldscore} {j.extracted_sumstats} > {j.intermediate_sumstats_1}
        cat <(head -n1 {j.extracted_sumstats}) {j.intermediate_sumstats_1} > {j.intermediate_sumstats_2}
        printf "Extracted sumstats and trimmed by variants present in LD score file.\n" >> {j.logout}
        printf "Original number of rows: %s\n" $(wc -l < "{j.extracted_sumstats}") >> {j.logout}
        printf "New number of rows after filtering by LD score file: %s\n" $(wc -l < "{j.intermediate_sumstats_2}") >> {j.logout}

        head -n1 {j.intermediate_sumstats_2} | tr "\t" "\n" | grep {ancestry} | cat > {j.headers_ancestry_specific}
        head -n1 {j.intermediate_sumstats_2} | tr "\t" "\n" | head -n4 > {j.headers_all}

        printf "Searching for ancestry: %s\n" {ancestry} >> {j.logout}
        export nlines=$(wc -l < "{j.headers_ancestry_specific}")
        printf "Found %s ancestry specific columns:\n" "${{nlines}}" >> {j.logout}
        cat {j.headers_ancestry_specific} >> {j.logout}"""
    command_condition = f"""
        if [ "${{nlines}}" -eq 5 ] || [ "${{nlines}}" -eq 6 ]; then
            cat {j.headers_all} {j.headers_ancestry_specific} > {j.headers_final}
            cut -f $(paste -sd, <(cut -f1 -d: <(grep -Fxn "$(<{j.headers_final})" < <(head -n1 {j.intermediate_sumstats_2} | tr "\t" "\n")))) {j.intermediate_sumstats_2} > {j.intermediate_sumstats_3}
            printf "Trimmed sumstats files to contain only %s columns.\n" {ancestry} >> {j.logout}
            python {munging_script} --sumstats {j.intermediate_sumstats_3} --N {N} --out {j.final_sumstats_file} --logfile {j.log_tmp}
            cp {j.final_sumstats_file} {j.final_sumstats_file + ".sumstats.gz"}
            printf -- "----------------------------------" >> {j.logout}
            printf "\n" >> {j.logout}
            cat {j.log_tmp} >> {j.logout}
        else
            printf "5 (or 6 in the case of case/control) ancestry specific columns not found; exiting.\n" >> {j.logout}
            touch {j.final_sumstats_file}
        fi"""
    sumstat_out = j.final_sumstats_file
    command_out = command_pre_script+command_condition
    return command_out, j, sumstat_out, j.logout