Project: s41467-024-53146-z | Function: DO_ENRICHMENT_STATS_collective (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Implements algorithm with 6 lines of core logic

**KEY TERMS:** {
  "Cas": "epidermidis* strains—restriction-modification (RM) ^17 , CRISPR-Cas ^18 , a mechanism of abortive infection (i",
  "Thus": "Thus, in order to develop effective therapeutic alternatives, it is critical to understand the major pathways that control the horizontal transfer of fitness/virulence factors between these species",
  "Compounding": "Compounding the problem, *S",
  "These": "These include genes responsible for methicillin resistance ( *mecA/C* ) encoded on SCC (staphylococcal cassette chromosome) *mec* cassettes ^7,8",
  "For": "For instance, one study demonstrated SCC *mec* transfer can occur by recombining into a conjugative plasmid and entering recipient cells via conjugation ^40",
  "Nhi": "programmed cell death) facilitated by the serine-threonine kinase Stk2 ^19 , and a unique innate immune system mediated by the nuclease-helicase Nhi ^20"
}

**CONTEXT CODE:**         
        ax.grid(which='major', linestyle='--', color='grey', alpha=0.5)
        ax.grid(which='major', linestyle='solid', color='grey', alpha=0.4)

        ax.xaxis.set_major_locator(plt.MultipleLocator(0.1))
        

        

        
        ax.set_axisbelow(True)
        plt.xticks(fontsize=9)
        plt.yticks(fontsize=9)
        plt.tight_layout()
        plt.savefig(path_plot_1)

    else:

        ylim_epi = 0.5
        ylim_aur = 0.5

        _, ax = plt.subplots(2, 1, figsize=(5, 3), dpi=250)

        def asdfg(ax, hist, N, mmin, mmax, median, ylim, txt):

            ax.bar(bins[:-1], hist/N, width=wd, align='center', color='blue', label='epidermidis')

            ax.set_xlabel("Fraction of defenses within 300 genes downstream of rlmh")
            ax.set_ylabel("Number of Genomes")

            ax.fill_betweenx([0, ylim], mmin, mmax, color='blue', alpha=0.2)
            line = Line2D([median, median], [0, ylim], color='blue', linestyle='dotted', linewidth=2)
            
            ax.add_line(line)
            
            ax.xaxis.set_major_locator(plt.MultipleLocator(0.1))
            ax.grid(which='major', linestyle='solid', color='grey', alpha=0.4)
            ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f'{int(round(100*x))}%'))

            ax.set_title(txt, fontsize=9)
            ax.set_axisbelow(True)

        asdfg(ax[0], hist_epi, N_epi, mmin_epi, mmax_epi, median_epi, ylim_epi, txt)
        asdfg(ax[1], hist_aur, N_aur, mmin_aur, mmax_aur, median_aur, ylim_aur, txt)

        plt.xticks(fontsize=9)
        plt.yticks(fontsize=9)
        plt.tight_layout()
        plt.savefig(path_plot_2)

def DO_ENRICHMENT_STATS_collective(dvars, mode, i = 0):
    """Implements algorithm with 6 lines of core logic"""
    raise NotImplementedError("Function implementation has been masked")
    
def SHANNON_DEFENSE_DIVERSITY(dvars, mode, i = 0):

    global pp

    path = pp + "Defense_Diversity/"
    ensure_directory(path)

    

    def get_system_lookup_dict():

        p = "/home/baslan/.macsyfinder/models/defense-finder-models/DefenseFinder_rules.tsv"

        system_lookup_dict = dict()

        txt = read_file(p, mode="whole")
        txt = re.sub(',', ' ', txt)
        txt = re.sub(' +' , ' ', txt)
        txt = re.sub('\t+', ' ', txt)
        data = txt.split('\n')

        data.pop(0)

        for line in data:

            row = line.split(' ')
            print(row)

            system = row[0]
            row.pop(0)

            row = [r for r in row if r != '']

            for gene in row:

                if gene not in system_lookup_dict.keys() :

                    if  gene[0].upper() == system[0].upper():

                        system_lookup_dict[gene] = system

        
        
        
        
        
        print(system_lookup_dict["Paris_I__AAA_15"])             

        return system_lookup_dict 

    def get_uniform_defense_dict(all_defenses):

        N = len(all_defenses)

        return {k: 1/N for k in all_defenses}
    
    def get_plasmid_defense_dict():

        path = "/Volume/biodata2" \
                "/sccmec_snail_analysis/snail_analysis_plasmids" \
                "/results/main_results_table.csv"
        
        df = pd.read_csv(path)
        df['raw def gene set'] = df['raw def gene set'].apply(ast.literal_eval)

        all_def_genes = set()
        for s in df['raw def gene set']:
            s = [d.split('_')[0] for d in s]
            print(s)
            all_def_genes = all_def_genes.union(s)

        gene_counts = {
        gene: sum(df['raw def gene set'].apply(lambda x: gene in x)) 
        for gene in all_def_genes}

        return gene_counts
    
    def get_epiderm_defense_dict():

        mode = "new"

        if mode == "old":

            d_epi_in =  {'Cas': 7, 'RM': 35, 'SEFIR': 3, 
                        'Nhi': 3, 'Stk2': 2, 
                        'AbiJ': 11, 'AbiR': 2, 
                        'Gabija': 5, 'CBASS': 2, 
                        'AbiG': 2, 'AbiH': 1, 
                        'RloC': 1, 'Rst_PARIS': 1}  
                    
            d_epi_ou =  {'Abi2': 62, 'RM': 19, 
                        'Kiwa': 2, 'AbiD': 2, 'RloC': 2, 
                        'Gabija': 3, 'AbiQ': 2, 'AbiH': 1, 
                        'PD-T4-9': 1, 'AbiK': 2, 'AbiG': 1}
            
            d_epi = Counter(d_epi_in) + Counter(d_epi_ou)


Complete the function implementation replacing "raise NotImplementedError".