python/methylnet/model_interpretability.py
def interpret_biology(all_cpgs_pickle,shapley_data_list,output_dir, analysis, n_workers, lola_db, individuals, classes, max_gap, class_intersect, length_output, set_subtraction, intersection, collections, gsea_analyses, gsea_pickle, depletion, overlap_test, cpg_set, top_global, extract_library):
    """Interrogate CpGs with high SHAPley scores for individuals, classes, or overall, for enrichments, genes, GO, KEGG, LOLA, overlap with popular cpg sets, GSEA, find nearby cpgs to top."""
    os.makedirs(output_dir,exist_ok=True)
    if os.path.exists(all_cpgs_pickle):
        all_cpgs=list(pickle.load(open(all_cpgs_pickle,'rb')))
    else:
        all_cpgs = []
    #top_cpgs=pickle.load(open(top_cpgs_pickle,'rb'))
    head_individuals=list(filter(None,individuals))
    head_classes=list(filter(None,classes))
    collections=list(filter(None,collections))
    gsea_analyses=list(filter(None,gsea_analyses))
    if gsea_analyses:
        analysis='GSEA'
    for i,shapley_data in enumerate(shapley_data_list):
        shapley_data=ShapleyData.from_pickle(shapley_data)
        shapley_data_explorer=ShapleyDataExplorer(shapley_data)
        if not top_global:
            if head_classes and head_classes[0]=='all':
                classes = shapley_data_explorer.list_classes()
            else:
                classes = copy.deepcopy(head_classes)
            if head_individuals and head_individuals[0]=='all':
                individuals = shapley_data_explorer.list_individuals(return_list=True)
            else:
                individuals = copy.deepcopy(head_individuals)
            cpg_exclusion_sets,cpg_sets=None,None
            if set_subtraction:
                _,cpg_exclusion_sets=shapley_data_explorer.return_cpg_sets()
            elif intersection:
                cpg_sets,_=shapley_data_explorer.return_cpg_sets()
            top_cpgs=shapley_data_explorer.return_top_cpgs(classes=classes,individuals=individuals,cpg_exclusion_sets=cpg_exclusion_sets,cpg_sets=cpg_sets)
        else:
            top_cpgs = {'global':shapley_data_explorer.return_global_importance_cpgs()}
        bio_interpreter = BioInterpreter(top_cpgs)
        if overlap_test:
            output_library_cpgs=bio_interpreter.return_overlap_score(set_cpgs=cpg_set,all_cpgs=all_cpgs,output_csv=join(output_dir,'{}_overlaps.csv'.format(cpg_set)),extract_library=extract_library)
            if output_library_cpgs is not None:
                pickle.dump(output_library_cpgs,open(join(output_dir,'cpg_library.pkl'),'wb'))
        else:
            if analysis in ['GO','KEGG','GENE']:
                analysis_outputs=bio_interpreter.gometh(analysis, allcpgs=all_cpgs, length_output=length_output)
            elif analysis == 'LOLA':
                analysis_outputs=bio_interpreter.run_lola(all_cpgs=all_cpgs, lola_db=lola_db, cores=n_workers, collections=collections, depletion=depletion)
            elif analysis == 'NEAR_CPGS':
                analysis_outputs=bio_interpreter.get_nearby_cpg_shapleys(all_cpgs=all_cpgs,max_gap=max_gap, class_intersect=class_intersect)
            elif gsea_analyses:
                analysis_outputs=bio_interpreter.gometh('', allcpgs=all_cpgs, length_output=length_output, gsea_analyses=gsea_analyses, gsea_pickle=gsea_pickle)
            for k in analysis_outputs:
                output_csv=join(output_dir,'{}_{}_{}.csv'.format(shapley_data_list[i].split('/')[-1],k,analysis if not gsea_analyses else '_'.join(gsea_analyses)).replace('/','-'))
                analysis_outputs[k].to_csv(output_csv)
Interrogate CpGs with high SHAPley scores for individuals, classes, or overall, for enrichments, genes, GO, KEGG, LOLA, overlap with popular cpg sets, GSEA, find nearby cpgs to top.
Interrogate CpGs with high SHAPley scores for individuals, classes, or overall, for enrichments, genes, GO, KEGG, LOLA, overlap with popular cpg sets, GSEA, find nearby cpgs to top.
Interrogate CpGs with high SHAPley scores for individuals classes or overall for enrichments genes GO KEGG LOLA overlap with popular cpg sets GSEA find nearby cpgs to top .
def interpret_biology(all_cpgs_pickle,shapley_data_list,output_dir, analysis, n_workers, lola_db, individuals, classes, max_gap, class_intersect, length_output, set_subtraction, intersection, collections, gsea_analyses, gsea_pickle, depletion, overlap_test, cpg_set, top_global, extract_library):
    os.makedirs(output_dir,exist_ok=True)
    if os.path.exists(all_cpgs_pickle):
        all_cpgs=list(pickle.load(open(all_cpgs_pickle,'rb')))
    else:
        all_cpgs = []
    head_individuals=list(filter(None,individuals))
    head_classes=list(filter(None,classes))
    collections=list(filter(None,collections))
    gsea_analyses=list(filter(None,gsea_analyses))
    if gsea_analyses:
        analysis='GSEA'
    for i,shapley_data in enumerate(shapley_data_list):
        shapley_data=ShapleyData.from_pickle(shapley_data)
        shapley_data_explorer=ShapleyDataExplorer(shapley_data)
        if not top_global:
            if head_classes and head_classes[0]=='all':
                classes = shapley_data_explorer.list_classes()
            else:
                classes = copy.deepcopy(head_classes)
            if head_individuals and head_individuals[0]=='all':
                individuals = shapley_data_explorer.list_individuals(return_list=True)
            else:
                individuals = copy.deepcopy(head_individuals)
            cpg_exclusion_sets,cpg_sets=None,None
            if set_subtraction:
                _,cpg_exclusion_sets=shapley_data_explorer.return_cpg_sets()
            elif intersection:
                cpg_sets,_=shapley_data_explorer.return_cpg_sets()
            top_cpgs=shapley_data_explorer.return_top_cpgs(classes=classes,individuals=individuals,cpg_exclusion_sets=cpg_exclusion_sets,cpg_sets=cpg_sets)
        else:
            top_cpgs = {'global':shapley_data_explorer.return_global_importance_cpgs()}
        bio_interpreter = BioInterpreter(top_cpgs)
        if overlap_test:
            output_library_cpgs=bio_interpreter.return_overlap_score(set_cpgs=cpg_set,all_cpgs=all_cpgs,output_csv=join(output_dir,'{}_overlaps.csv'.format(cpg_set)),extract_library=extract_library)
            if output_library_cpgs is not None:
                pickle.dump(output_library_cpgs,open(join(output_dir,'cpg_library.pkl'),'wb'))
        else:
            if analysis in ['GO','KEGG','GENE']:
                analysis_outputs=bio_interpreter.gometh(analysis, allcpgs=all_cpgs, length_output=length_output)
            elif analysis == 'LOLA':
                analysis_outputs=bio_interpreter.run_lola(all_cpgs=all_cpgs, lola_db=lola_db, cores=n_workers, collections=collections, depletion=depletion)
            elif analysis == 'NEAR_CPGS':
                analysis_outputs=bio_interpreter.get_nearby_cpg_shapleys(all_cpgs=all_cpgs,max_gap=max_gap, class_intersect=class_intersect)
            elif gsea_analyses:
                analysis_outputs=bio_interpreter.gometh('', allcpgs=all_cpgs, length_output=length_output, gsea_analyses=gsea_analyses, gsea_pickle=gsea_pickle)
            for k in analysis_outputs:
                output_csv=join(output_dir,'{}_{}_{}.csv'.format(shapley_data_list[i].split('/')[-1],k,analysis if not gsea_analyses else '_'.join(gsea_analyses)).replace('/','-'))
                analysis_outputs[k].to_csv(output_csv)
def interpret_biology ( all_cpgs_pickle , shapley_data_list , output_dir , analysis , n_workers , lola_db , individuals , classes , max_gap , class_intersect , length_output , set_subtraction , intersection , collections , gsea_analyses , gsea_pickle , depletion , overlap_test , cpg_set , top_global , extract_library ) : os . makedirs ( output_dir , exist_ok = True ) if os . path . exists ( all_cpgs_pickle ) : all_cpgs = list ( pickle . load ( open ( all_cpgs_pickle , 'rb' ) ) ) else : all_cpgs = [ ] head_individuals = list ( filter ( None , individuals ) ) head_classes = list ( filter ( None , classes ) ) collections = list ( filter ( None , collections ) ) gsea_analyses = list ( filter ( None , gsea_analyses ) ) if gsea_analyses : analysis = 'GSEA' for i , shapley_data in enumerate ( shapley_data_list ) : shapley_data = ShapleyData . from_pickle ( shapley_data ) shapley_data_explorer = ShapleyDataExplorer ( shapley_data ) if not top_global : if head_classes and head_classes [ 0 ] == 'all' : classes = shapley_data_explorer . list_classes ( ) else : classes = copy . deepcopy ( head_classes ) if head_individuals and head_individuals [ 0 ] == 'all' : individuals = shapley_data_explorer . list_individuals ( return_list = True ) else : individuals = copy . deepcopy ( head_individuals ) cpg_exclusion_sets , cpg_sets = None , None if set_subtraction : _ , cpg_exclusion_sets = shapley_data_explorer . return_cpg_sets ( ) elif intersection : cpg_sets , _ = shapley_data_explorer . return_cpg_sets ( ) top_cpgs = shapley_data_explorer . return_top_cpgs ( classes = classes , individuals = individuals , cpg_exclusion_sets = cpg_exclusion_sets , cpg_sets = cpg_sets ) else : top_cpgs = { 'global' : shapley_data_explorer . return_global_importance_cpgs ( ) } bio_interpreter = BioInterpreter ( top_cpgs ) if overlap_test : output_library_cpgs = bio_interpreter . return_overlap_score ( set_cpgs = cpg_set , all_cpgs = all_cpgs , output_csv = join ( output_dir , '{}_overlaps.csv' . format ( cpg_set ) ) , extract_library = extract_library ) if output_library_cpgs is not None : pickle . dump ( output_library_cpgs , open ( join ( output_dir , 'cpg_library.pkl' ) , 'wb' ) ) else : if analysis in [ 'GO' , 'KEGG' , 'GENE' ] : analysis_outputs = bio_interpreter . gometh ( analysis , allcpgs = all_cpgs , length_output = length_output ) elif analysis == 'LOLA' : analysis_outputs = bio_interpreter . run_lola ( all_cpgs = all_cpgs , lola_db = lola_db , cores = n_workers , collections = collections , depletion = depletion ) elif analysis == 'NEAR_CPGS' : analysis_outputs = bio_interpreter . get_nearby_cpg_shapleys ( all_cpgs = all_cpgs , max_gap = max_gap , class_intersect = class_intersect ) elif gsea_analyses : analysis_outputs = bio_interpreter . gometh ( '' , allcpgs = all_cpgs , length_output = length_output , gsea_analyses = gsea_analyses , gsea_pickle = gsea_pickle ) for k in analysis_outputs : output_csv = join ( output_dir , '{}_{}_{}.csv' . format ( shapley_data_list [ i ] . split ( '/' ) [ - 1 ] , k , analysis if not gsea_analyses else '_' . join ( gsea_analyses ) ) . replace ( '/' , '-' ) ) analysis_outputs [ k ] . to_csv ( output_csv )
Interrogate CpGs with high SHAPley scores for individuals, classes, or overall, for enrichments, genes, GO, KEGG, LOLA, overlap with popular cpg sets, GSEA, find nearby cpgs to top.
Interrogate CpGs with high SHAPley scores for individuals classes or overall for enrichments genes GO KEGG LOLA overlap with popular cpg sets GSEA find nearby cpgs to top .
"""Interrogate CpGs with high SHAPley scores for individuals, classes, or overall, for enrichments, genes, GO, KEGG, LOLA, overlap with popular cpg sets, GSEA, find nearby cpgs to top.""" #top_cpgs=pickle.load(open(top_cpgs_pickle,'rb'))
all_cpgs_pickle
shapley_data_list
output_dir
analysis
n_workers
lola_db
individuals
classes
max_gap
class_intersect
length_output
set_subtraction
intersection
collections
gsea_analyses
gsea_pickle
depletion
overlap_test
cpg_set
top_global
extract_library