def update_association(assc_gene2gos, go2obj, relationships=None, prt=sys.stdout): """Add the GO parents of a gene's associated GO IDs to the gene's association.""" if not assc_gene2gos: print('**WARNING: {N} ASSOCATIONS. NO ACTION BY update_association'. format(N=len(assc_gene2gos))) return if prt: prt.write("Propagating term counts ") # Replaces update_association in GODag goids_avail = set(go2obj) # Get all assc GO IDs that are current assc_goid_sets = assc_gene2gos.values() goids_assoc_all = set.union(*assc_goid_sets) _chk_goids_notfound(goids_assoc_all, goids_avail) # Get the subset of GO objects in the association _goids_assoc_cur = goids_assoc_all.intersection(goids_avail) _go2obj_assc = {go: go2obj[go] for go in _goids_assoc_cur} go2ancestors = get_go2parents_go2obj(_go2obj_assc, relationships, prt) # Update the GO sets in assc_gene2gos to include all GO ancestors for assc_goids_cur in assc_goid_sets: parents = set() for goid in assc_goids_cur.intersection(goids_avail): if goid in go2ancestors: parents.update(go2ancestors[goid]) assc_goids_cur.update(parents)
def _get_go2ancestors(self, goids_assoc_usr, relationships, prt=sys.stdout): """Return go2ancestors (set of parent GO IDs) for all GO ID keys in go2obj.""" assert self.godag is not None _godag = self.godag # Get GO IDs in annotations that are in GO DAG goids_avail = set(_godag) self._rpt_goids_notfound(goids_assoc_usr, goids_avail) goids_assoc_cur = goids_assoc_usr.intersection(goids_avail) # Get GO Term for each current GO ID in the annotations _go2obj_assc = {go:_godag[go] for go in goids_assoc_cur} go2ancestors = get_go2parents_go2obj(_go2obj_assc, relationships, prt) if prt: prt.write('{N} GO IDs -> {M} go2ancestors\n'.format( N=len(goids_avail), M=len(go2ancestors))) return go2ancestors
def _plot_groups_hdrgos(self, hdrgos_usr, pltargs): """Plot GO DAGs for each group found under a specfied header GO.""" import datetime import timeit pngs = [] tic = timeit.default_timer() # Loop through GO groups. Each group of GOs is formed under a single "header GO" hdrgo2usrgos, go2obj = self._get_plt_data(hdrgos_usr) for hdrgo, usrgos in hdrgo2usrgos.items(): pngs.extend(self._plot_go_group( hdrgo, usrgos, pltargs=pltargs, go2parentids=get_go2parents_go2obj(go2obj))) sys.stdout.write("\nElapsed HMS: {HMS} to write ".format( HMS=str(datetime.timedelta(seconds=(timeit.default_timer()-tic))))) sys.stdout.write("{P:5,} GO DAG plots for {H:>5,} GO grouping headers\n".format( H=len(hdrgo2usrgos), P=len(pngs))) return sorted(set(pngs))
def _get_pltdotstrs(self, hdrgos_usr, **kws): """Plot GO DAGs for each group found under a specfied header GO.""" import datetime import timeit dotstrs_all = [] tic = timeit.default_timer() # Loop through GO groups. Each group of GOs is formed under a single "header GO" hdrgo2usrgos, go2obj = self._get_plt_data(hdrgos_usr) # get dot strings with _get_dotstrs_curs for hdrgo, usrgos in hdrgo2usrgos.items(): dotstrs_cur = self._get_dotgraphs( hdrgo, usrgos, pltargs=PltGroupedGosArgs(self.grprobj, **kws), go2parentids=get_go2parents_go2obj(go2obj)) dotstrs_all.extend(dotstrs_cur) sys.stdout.write("\nElapsed HMS: {HMS} to write ".format( HMS=str(datetime.timedelta(seconds=(timeit.default_timer()-tic))))) sys.stdout.write("{P:5,} GO DAG plots for {H:>5,} GO grouping headers\n".format( H=len(hdrgo2usrgos), P=len(dotstrs_all))) return sorted(set(dotstrs_all))
def _plot_groups_hdrgos(self, hdrgos_usr, pltargs): """Plot GO DAGs for each group found under a specfied header GO.""" import datetime import timeit pngs = [] tic = timeit.default_timer() # Loop through GO groups. Each group of GOs is formed under a single "header GO" hdrgo2usrgos, go2obj = self._get_plt_data(hdrgos_usr) for hdrgo, usrgos in hdrgo2usrgos.items(): pngs.extend( self._plot_go_group( hdrgo, usrgos, pltargs=pltargs, go2parentids=get_go2parents_go2obj(go2obj))) sys.stdout.write("\nElapsed HMS: {HMS} to write ".format( HMS=str(datetime.timedelta(seconds=(timeit.default_timer() - tic))))) sys.stdout.write( "{P:5,} GO DAG plots for {H:>5,} GO grouping headers\n".format( H=len(hdrgo2usrgos), P=len(pngs))) return sorted(set(pngs))
def _get_pltdotstrs(self, hdrgos_usr, **kws): """Plot GO DAGs for each group found under a specfied header GO.""" import datetime import timeit dotstrs_all = [] tic = timeit.default_timer() # Loop through GO groups. Each group of GOs is formed under a single "header GO" hdrgo2usrgos, go2obj = self._get_plt_data(hdrgos_usr) # get dot strings with _get_dotstrs_curs for hdrgo, usrgos in hdrgo2usrgos.items(): dotstrs_cur = self._get_dotgraphs( hdrgo, usrgos, pltargs=PltGroupedGosArgs(self.grprobj, **kws), go2parentids=get_go2parents_go2obj(go2obj)) dotstrs_all.extend(dotstrs_cur) sys.stdout.write("\nElapsed HMS: {HMS} to write ".format( HMS=str(datetime.timedelta(seconds=(timeit.default_timer() - tic))))) sys.stdout.write( "{P:5,} GO DAG plots for {H:>5,} GO grouping headers\n".format( H=len(hdrgo2usrgos), P=len(dotstrs_all))) return sorted(set(dotstrs_all))