python/methylnet/interpretation_classes.py
def return_overlap_score(self, set_cpgs='IDOL', platform='450k', all_cpgs=[], output_csv='output_bio_intersect.csv', extract_library=False):
        """Perform overlap test, overlapping top CpGs with IDOL CpGs, age related cpgs, etc.

        Parameters
        ----------
        set_cpgs : type
            Set of reference cpgs.
        platform : type
            450K or 850K
        all_cpgs : type
            All CpGs to build a universe.
        output_csv : type
            Output CSV for results of overlaps between classes, how much doo they share these CpGs overlapped.
        extract_library : type
            Can extract a list of the CpGs that ended up beinng overlapped with.

        Returns
        -------
        List
            Optional output of overlapped library of CpGs.

        """
        import rpy2.robjects as robjects
        from rpy2.robjects import pandas2ri
        pandas2ri.activate()
        if set_cpgs == 'IDOL':
            cpg_reference_set=set(robjects.r('IDOLOptimizedCpGs') if platform=='epic' else robjects.r('IDOLOptimizedCpGs450klegacy'))
        elif set_cpgs == 'clock':
            cpg_reference_set=set(open('data/age_cpgs.txt').read().splitlines()) # clock
        elif set_cpgs == 'epitoc':
            cpg_reference_set=set(robjects.r('EpiTOCcpgs'))
        elif set_cpgs == 'horvath':
            cpg_reference_set=set(robjects.r('as.vector(HorvathLongCGlist$MR_var)'))
        elif set_cpgs == 'hannum':
            cpg_reference_set=set(robjects.r('as.vector(hannumModel$marker)'))
        else:
            cpg_reference_set=set(robjects.r('as.vector(HorvathLongCGlist$MR_var)'))
        if all_cpgs:
            cpg_reference_set = cpg_reference_set.intersection(all_cpgs)
        intersected_cpg_sets = {}
        for k in self.top_cpgs:
            query_cpgs=set(self.top_cpgs[k].values[:,0])
            intersected_cpgs=query_cpgs.intersection(cpg_reference_set)
            intersected_cpg_sets[k]=intersected_cpgs
            overlap_score = round(float(len(intersected_cpgs)) / len(cpg_reference_set) * 100,2)
            print("{} top cpgs overlap with {}% of {} cpgs".format(k,overlap_score,set_cpgs))

        keys=list(intersected_cpg_sets.keys())
        if len(keys)>1:
            from itertools import combinations
            from functools import reduce
            similarity_matrix=pd.DataFrame(np.eye(len(keys)),index=keys,columns=keys)
            for i,j in combinations(keys,r=2):
                similarity_matrix.loc[i,j]=len(intersected_cpg_sets[i].intersection(intersected_cpg_sets[j]))
                similarity_matrix.loc[j,i]=similarity_matrix.loc[i,j]
            for k in keys:
                similarity_matrix.loc[k,k]=len(intersected_cpg_sets[k])
                print("{} shared cpgs: {}/{}".format(k,len(set(reduce(lambda x,y:x.union(y),[intersected_cpg_sets[k].intersection(intersected_cpgs) for k2,intersected_cpgs in intersected_cpg_sets.items() if k2 != k]))),similarity_matrix.loc[k,k]))
            similarity_matrix.loc[np.sort(keys),np.sort(keys)].to_csv(output_csv)
            if extract_library:
                return reduce(np.union1d,[list(intersected_cpg_sets[k]) for k in keys])
        return None
Perform overlap test, overlapping top CpGs with IDOL CpGs, age related cpgs, etc.

        Parameters
        ----------
        set_cpgs : type
            Set of reference cpgs.
        platform : type
            450K or 850K
        all_cpgs : type
            All CpGs to build a universe.
        output_csv : type
            Output CSV for results of overlaps between classes, how much doo they share these CpGs overlapped.
        extract_library : type
            Can extract a list of the CpGs that ended up beinng overlapped with.

        Returns
        -------
        List
            Optional output of overlapped library of CpGs.

        
Perform overlap test, overlapping top CpGs with IDOL CpGs, age related cpgs, etc.
Parameters

set_cpgs : type
Set of reference cpgs.
platform : type
450K or 850K
all_cpgs : type
All CpGs to build a universe.
output_csv : type
Output CSV for results of overlaps between classes, how much doo they share these CpGs overlapped.
extract_library : type
Can extract a list of the CpGs that ended up beinng overlapped with.

Returns

List
Optional output of overlapped library of CpGs.
Perform overlap test overlapping top CpGs with IDOL CpGs age related cpgs etc . Parameters set_cpgs : type Set of reference cpgs . platform : type 450K or 850K all_cpgs : type All CpGs to build a universe . output_csv : type Output CSV for results of overlaps between classes how much doo they share these CpGs overlapped . extract_library : type Can extract a list of the CpGs that ended up beinng overlapped with . Returns List Optional output of overlapped library of CpGs .
def return_overlap_score(self, set_cpgs='IDOL', platform='450k', all_cpgs=[], output_csv='output_bio_intersect.csv', extract_library=False):
        import rpy2.robjects as robjects
        from rpy2.robjects import pandas2ri
        pandas2ri.activate()
        if set_cpgs == 'IDOL':
            cpg_reference_set=set(robjects.r('IDOLOptimizedCpGs') if platform=='epic' else robjects.r('IDOLOptimizedCpGs450klegacy'))
        elif set_cpgs == 'clock':
            cpg_reference_set=set(open('data/age_cpgs.txt').read().splitlines()) 
        elif set_cpgs == 'epitoc':
            cpg_reference_set=set(robjects.r('EpiTOCcpgs'))
        elif set_cpgs == 'horvath':
            cpg_reference_set=set(robjects.r('as.vector(HorvathLongCGlist$MR_var)'))
        elif set_cpgs == 'hannum':
            cpg_reference_set=set(robjects.r('as.vector(hannumModel$marker)'))
        else:
            cpg_reference_set=set(robjects.r('as.vector(HorvathLongCGlist$MR_var)'))
        if all_cpgs:
            cpg_reference_set = cpg_reference_set.intersection(all_cpgs)
        intersected_cpg_sets = {}
        for k in self.top_cpgs:
            query_cpgs=set(self.top_cpgs[k].values[:,0])
            intersected_cpgs=query_cpgs.intersection(cpg_reference_set)
            intersected_cpg_sets[k]=intersected_cpgs
            overlap_score = round(float(len(intersected_cpgs)) / len(cpg_reference_set) * 100,2)
            print("{} top cpgs overlap with {}% of {} cpgs".format(k,overlap_score,set_cpgs))
        keys=list(intersected_cpg_sets.keys())
        if len(keys)>1:
            from itertools import combinations
            from functools import reduce
            similarity_matrix=pd.DataFrame(np.eye(len(keys)),index=keys,columns=keys)
            for i,j in combinations(keys,r=2):
                similarity_matrix.loc[i,j]=len(intersected_cpg_sets[i].intersection(intersected_cpg_sets[j]))
                similarity_matrix.loc[j,i]=similarity_matrix.loc[i,j]
            for k in keys:
                similarity_matrix.loc[k,k]=len(intersected_cpg_sets[k])
                print("{} shared cpgs: {}/{}".format(k,len(set(reduce(lambda x,y:x.union(y),[intersected_cpg_sets[k].intersection(intersected_cpgs) for k2,intersected_cpgs in intersected_cpg_sets.items() if k2 != k]))),similarity_matrix.loc[k,k]))
            similarity_matrix.loc[np.sort(keys),np.sort(keys)].to_csv(output_csv)
            if extract_library:
                return reduce(np.union1d,[list(intersected_cpg_sets[k]) for k in keys])
        return None
def return_overlap_score ( self , set_cpgs = 'IDOL' , platform = '450k' , all_cpgs = [ ] , output_csv = 'output_bio_intersect.csv' , extract_library = False ) : import rpy2 . robjects as robjects from rpy2 . robjects import pandas2ri pandas2ri . activate ( ) if set_cpgs == 'IDOL' : cpg_reference_set = set ( robjects . r ( 'IDOLOptimizedCpGs' ) if platform == 'epic' else robjects . r ( 'IDOLOptimizedCpGs450klegacy' ) ) elif set_cpgs == 'clock' : cpg_reference_set = set ( open ( 'data/age_cpgs.txt' ) . read ( ) . splitlines ( ) ) elif set_cpgs == 'epitoc' : cpg_reference_set = set ( robjects . r ( 'EpiTOCcpgs' ) ) elif set_cpgs == 'horvath' : cpg_reference_set = set ( robjects . r ( 'as.vector(HorvathLongCGlist$MR_var)' ) ) elif set_cpgs == 'hannum' : cpg_reference_set = set ( robjects . r ( 'as.vector(hannumModel$marker)' ) ) else : cpg_reference_set = set ( robjects . r ( 'as.vector(HorvathLongCGlist$MR_var)' ) ) if all_cpgs : cpg_reference_set = cpg_reference_set . intersection ( all_cpgs ) intersected_cpg_sets = { } for k in self . top_cpgs : query_cpgs = set ( self . top_cpgs [ k ] . values [ : , 0 ] ) intersected_cpgs = query_cpgs . intersection ( cpg_reference_set ) intersected_cpg_sets [ k ] = intersected_cpgs overlap_score = round ( float ( len ( intersected_cpgs ) ) / len ( cpg_reference_set ) * 100 , 2 ) print ( "{} top cpgs overlap with {}% of {} cpgs" . format ( k , overlap_score , set_cpgs ) ) keys = list ( intersected_cpg_sets . keys ( ) ) if len ( keys ) > 1 : from itertools import combinations from functools import reduce similarity_matrix = pd . DataFrame ( np . eye ( len ( keys ) ) , index = keys , columns = keys ) for i , j in combinations ( keys , r = 2 ) : similarity_matrix . loc [ i , j ] = len ( intersected_cpg_sets [ i ] . intersection ( intersected_cpg_sets [ j ] ) ) similarity_matrix . loc [ j , i ] = similarity_matrix . loc [ i , j ] for k in keys : similarity_matrix . loc [ k , k ] = len ( intersected_cpg_sets [ k ] ) print ( "{} shared cpgs: {}/{}" . format ( k , len ( set ( reduce ( lambda x , y : x . union ( y ) , [ intersected_cpg_sets [ k ] . intersection ( intersected_cpgs ) for k2 , intersected_cpgs in intersected_cpg_sets . items ( ) if k2 != k ] ) ) ) , similarity_matrix . loc [ k , k ] ) ) similarity_matrix . loc [ np . sort ( keys ) , np . sort ( keys ) ] . to_csv ( output_csv ) if extract_library : return reduce ( np . union1d , [ list ( intersected_cpg_sets [ k ] ) for k in keys ] ) return None
Perform overlap test, overlapping top CpGs with IDOL CpGs, age related cpgs, etc.
Perform overlap test overlapping top CpGs with IDOL CpGs age related cpgs etc .
"""Perform overlap test, overlapping top CpGs with IDOL CpGs, age related cpgs, etc.

        Parameters
        ----------
        set_cpgs : type
            Set of reference cpgs.
        platform : type
            450K or 850K
        all_cpgs : type
            All CpGs to build a universe.
        output_csv : type
            Output CSV for results of overlaps between classes, how much doo they share these CpGs overlapped.
        extract_library : type
            Can extract a list of the CpGs that ended up beinng overlapped with.

        Returns
        -------
        List
            Optional output of overlapped library of CpGs.

        """ # clock
self
set_cpgs
platform
all_cpgs
output_csv
extract_library