Created
July 10, 2018 19:24
-
-
Save cplaisier/070995969d8f0a87a9b46c790aac1f81 to your computer and use it in GitHub Desktop.
Had to modify the 'network.py' file from the BooleanNet python package to get this to work. Needed to be modified to allow components to be imported instead of component. And also to get all strongly connected components produces a generator, which needed to be turned into a list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import util | |
| import random | |
| from itertools import count | |
| try: | |
| import networkx | |
| from networkx import components | |
| except ImportError: | |
| util.error( "networkx is missing, install it from https://networkx.lanl.gov/") | |
| # color constants | |
| BLUE, RED, GREEN = "#0000DD", "#DD0000", "#00DD00" | |
| WHITE, PURPLE, ORANGE = "#FFFFFF", "#990066", "#FF3300" | |
| TEAL, CRIMSON, GOLD, NAVY, SIENNA = "#009999", "#DC143C", "#FFD700", "#000080", "#A0522D" | |
| LIGHT_GREEN, SPRING_GREEN, YELLOW_GREEN = "#33FF00", "#00FF7F", "#9ACD32" | |
| def component_colormap(graph): | |
| """ | |
| Colormap by strong compoments | |
| """ | |
| # automatically color by components | |
| # a list of colors in hexadecimal Red/Gree/Blue notation | |
| colors = [ ORANGE, SPRING_GREEN, GOLD, TEAL, PURPLE, NAVY, SIENNA, CRIMSON, BLUE, ] | |
| # find the strongly connected components | |
| components1 = [i for i in components.strongly_connected_components( graph )] | |
| # make sure we have as many colors as components | |
| if len(colors) < len(components1): | |
| util.warn( 'there are more components than colors!' ) | |
| # create the colormap | |
| colormap = {} | |
| for color, comp in zip(colors, components1): | |
| for node in comp: | |
| colormap[node] = color | |
| return colormap | |
| def write_gml( graph, fname, colormap={} ): | |
| "Custom gml exporter" | |
| fp = open(fname, 'wt') | |
| text = [ 'graph [', 'directed 1' ] | |
| nodepatt = 'node [ id %(node)s label "%(node)s" graphics [ fill "%(color)s" w 40 h 30 x %(x)s y %(y)s type "ellipse" ]]' | |
| rnd = random.randint | |
| for node in graph.nodes(): | |
| x, y = rnd(50,200), rnd(50, 200) | |
| color = colormap.get(node, '#CCCCFF') | |
| param = dict( node=node, x=x, y=y, color=color ) | |
| text.append( nodepatt % param) | |
| edgepatt = 'edge [ source %(source)s target %(target)s graphics [ fill "%(color)s" targetArrow "delta" ]]' | |
| for source, target in graph.edges(): | |
| pair = (source, target) | |
| color = colormap.get(pair, '#000000') | |
| param = dict( source=source, target=target, color=color ) | |
| text.append( edgepatt % param) | |
| text.append( ']' ) | |
| fp.write( util.join( text, sep="\n" ) ) | |
| fp.close() | |
| class TransGraph(object): | |
| """ | |
| Represents a transition graph | |
| """ | |
| def __init__(self, logfile, verbose=False): | |
| self.graph = networkx.MultiDiGraph( ) | |
| self.fp = open( logfile, 'wt') | |
| self.verbose = verbose | |
| self.seen = set() | |
| self.store = dict() | |
| self.colors = dict() | |
| def add(self, states, times=None): | |
| "Adds states to the transition" | |
| # generating the fingerprints and sto | |
| times = times or range(len(states)) | |
| fprints = [] | |
| for state in states: | |
| if self.verbose: | |
| fp = state.bin() | |
| else: | |
| fp = state.fp() | |
| fprints.append( fp ) | |
| self.store[fp] = state | |
| self.fp.write( '*** transitions from %s ***\n' % fprints[0] ) | |
| for head, tail, tstamp in zip(fprints, fprints[1:], times ): | |
| pair = (head, tail) | |
| self.fp.write('T=%s: %s->%s\n' % (tstamp, head, tail) ) | |
| if pair not in self.seen: | |
| self.graph.add_edge(head, tail) | |
| self.seen.add(pair) | |
| def save(self, fname, colormap={}): | |
| "Saves the graph as gml" | |
| write_gml(graph=self.graph, fname=fname, colormap=colormap) | |
| self.fp.write( '*** node values ***\n' ) | |
| # writes the mapping | |
| first = self.store.values()[0] | |
| header = [ 'state' ] + first.keys() | |
| self.fp.write( util.join(header) ) | |
| for fprint, state in sorted( self.store.items() ): | |
| line = [ fprint ] + map(int, state.values() ) | |
| self.fp.write( util.join(line) ) | |
| def test(): | |
| """ | |
| Main testrunnner | |
| """ | |
| import boolmodel | |
| text = """ | |
| A = True | |
| B = False | |
| C = False | |
| 1: A* = A | |
| 2: B* = not B | |
| 3: C* = A and B | |
| """ | |
| model = boolmodel.BoolModel( mode='sync', text=text ) | |
| model.initialize( missing=util.true ) | |
| model.iterate( steps = 5 ) | |
| #for state in model.states: | |
| # print state | |
| trans = TransGraph( logfile='states.txt' ) | |
| trans.add( model.states ) | |
| # generate the colormap based on components | |
| colormap = component_colormap( trans.graph ) | |
| trans.save( fname='test.gml', colormap=colormap ) | |
| if __name__ == '__main__': | |
| test() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ########################################################## | |
| ## Consistilator: plotNetworkMotifs.py ## | |
| ## ______ ______ __ __ ## | |
| ## /\ __ \ /\ ___\ /\ \/\ \ ## | |
| ## \ \ __ \ \ \___ \ \ \ \_\ \ ## | |
| ## \ \_\ \_\ \/\_____\ \ \_____\ ## | |
| ## \/_/\/_/ \/_____/ \/_____/ ## | |
| ## @Developed by: Plaisier Lab ## | |
| ## (https://plaisierlab.engineering.asu.edu/) ## | |
| ## Arizona State University ## | |
| ## 242 ISTB1, 550 E Orange St ## | |
| ## Tempe, AZ 85281 ## | |
| ## @Author: Chris Plaisier ## | |
| ## @License: GNU GPLv3 ## | |
| ## ## | |
| ## If this program is used in your analysis please ## | |
| ## mention who built it. Thanks. :-) ## | |
| ########################################################## | |
| from subprocess import * | |
| import numpy as np | |
| import pandas as pd | |
| import networkx as nx | |
| import matplotlib.pyplot as plt | |
| from scipy.stats import pearsonr | |
| from lifelines import KaplanMeierFitter | |
| from matplotlib.backends.backend_pdf import PdfPages | |
| #import copy | |
| #from multiprocessing import Pool, cpu_count, Manager | |
| ## Plot network information | |
| ## ___________________ | |
| ## | | | | |
| ## | Expr. | R^2 | | |
| ## | | | | |
| ## ___________________ | |
| ## | | | | |
| ## | Net. | Attra. | | |
| ## | | | | |
| ## ___________________ | |
| ## | | | | |
| ## | Surv. | State | | |
| ## | | Dist. | | |
| ## ___________________ | |
| #def plotNetworks(geneSets, ids, consistencies, networks, rsq, exp, binExp, pheno, symbol2entrez): | |
| def plotNetworks(geneSets, ids, consistencies, networks, rsq, exp, binExp, pheno, exp_lgg, binExp_lgg, pheno_lgg, symbol2entrez): | |
| nodeColors = ['k','r','g'] | |
| pp = PdfPages('gbmNetMotifs_3node.pdf') | |
| for set1 in range(len(geneSets)): | |
| nodes = dict(zip(geneSets[set1],nodeColors)) | |
| fig = plt.figure(figsize=(11,8.5)) | |
| grid = plt.GridSpec(3,3, wspace=0.25, hspace=0.25, left=0.075, right=0.95, bottom=0.05, top=0.95) | |
| # Plot Gene Expresion [0,0] | |
| ax = plt.subplot(grid[0,0]) | |
| gl1 = exp.loc[[int(symbol2entrez[str(geneSets[set1][0])]),int(symbol2entrez[str(geneSets[set1][1])]),int(symbol2entrez[str(geneSets[set1][2])])]] | |
| #gl1 = exp.loc[[str(geneSets[set1][0]),str(geneSets[set1][1]),str(geneSets[set1][2])]] | |
| cols1 = gl1.columns[gl1.mean().argsort()] | |
| gl2 = gl1[cols1] | |
| plt.plot(gl2.loc[int(symbol2entrez[str(geneSets[set1][0])])].tolist(),nodes[geneSets[set1][0]],gl2.loc[int(symbol2entrez[str(geneSets[set1][1])])].tolist(),nodes[geneSets[set1][1]],gl2.loc[int(symbol2entrez[str(geneSets[set1][2])])].tolist(),nodes[geneSets[set1][2]]) | |
| #plt.plot(gl2.loc[str(geneSets[set1][0])].tolist(),nodes[geneSets[set1][0]],gl2.loc[str(geneSets[set1][1])].tolist(),nodes[geneSets[set1][1]],gl2.loc[str(geneSets[set1][2])].tolist(),nodes[geneSets[set1][2]]) | |
| ax.set_ylabel('Relative Expression') | |
| ax.set_xlabel('Patients') | |
| plt.tight_layout() | |
| # Plot R squared values [0,1] | |
| ax = plt.subplot(grid[0,1]) | |
| index = np.arange(len(geneSets[set1])) | |
| print index | |
| plt.xticks(index, geneSets[set1]) | |
| g1, g2, g3 = plt.bar(index, [consistencies[set1]['all'][j] for j in geneSets[set1]]) | |
| print consistencies[set1]['all'] | |
| plt.ylim((0,1)) | |
| plt.ylabel('$R^2$') | |
| plt.xlabel('Gene') | |
| ax.axhline(rsq,color='k',linestyle='--',alpha=0.5) | |
| g1.set_facecolor(nodes[geneSets[set1][0]]) | |
| g2.set_facecolor(nodes[geneSets[set1][1]]) | |
| g3.set_facecolor(nodes[geneSets[set1][2]]) | |
| plt.tight_layout() | |
| # Network plot [0,2] | |
| plt.subplot(grid[0,2]) | |
| G = networks[set1] | |
| node_colors = [nodes[i] for i in list(G.nodes)] | |
| edges = G.edges() | |
| edge_colors = [G[u][v]['color'] for u,v in edges] | |
| nx.draw(G, pos=nx.circular_layout(G),label_pos=3,with_labels=False,node_size=500,node_color=node_colors,edge_color=edge_colors,width=3,arrowsize=25,font_color='w') | |
| #createing label offset on network figure | |
| label_ratio = 0.27 | |
| pos_labels = {} | |
| #For each node in the Graph | |
| pos = nx.circular_layout(G) | |
| p=0 | |
| for aNode in G.nodes(): | |
| #Get the node's position from the layout | |
| x,y = pos[aNode] | |
| #Set Offset | |
| if p==0: | |
| pos_labels[aNode] = (x-label_ratio, y) | |
| else: | |
| pos_labels[aNode] = (x+label_ratio, y) | |
| p+=1 | |
| nx.draw_networkx_labels(G,pos=pos_labels,fontsize=3) | |
| """# Plot Gene Expresion [2,1] | |
| ax = plt.subplot(grid[2,0]) | |
| #gl1 = exp_lgg.loc[[int(symbol2entrez[str(geneSets[set1][0])]),int(symbol2entrez[str(geneSets[set1][1])]),int(symbol2entrez[str(geneSets[set1][2])])]] | |
| gl1 = exp_lgg.loc[[str(geneSets[set1][0]),str(geneSets[set1][1]),str(geneSets[set1][2])]] | |
| cols1 = gl1.columns[gl1.mean().argsort()] | |
| gl2 = gl1[cols1] | |
| #plt.plot(gl2.loc[int(symbol2entrez[str(geneSets[set1][0])])].tolist(),nodes[geneSets[set1][0]],gl2.loc[int(symbol2entrez[str(geneSets[set1][1])])].tolist(),nodes[geneSets[set1][1]],gl2.loc[int(symbol2entrez[str(geneSets[set1][2])])].tolist(),nodes[geneSets[set1][2]]) | |
| plt.plot(gl2.loc[str(geneSets[set1][0])].tolist(),nodes[geneSets[set1][0]],gl2.loc[str(geneSets[set1][1])].tolist(),nodes[geneSets[set1][1]],gl2.loc[str(geneSets[set1][2])].tolist(),nodes[geneSets[set1][2]]) | |
| ax.set_ylabel('Relative Expression') | |
| ax.set_xlabel('Patients') | |
| plt.tight_layout() | |
| """ | |
| # Attractors [1,1] | |
| ### TODO ### | |
| # Make binary data for each | |
| states = ['000', '100','010','001','110','011','101','111'] | |
| states_colors = dict(zip(states,['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray'])) | |
| tmp = binExp.loc[geneSets[set1]].transpose() | |
| #print tmp | |
| tmp1 = dict(zip(tmp.index,[''.join([str(k) for k in tmp.loc[j].values]) for j in tmp.index])) | |
| tmp2 = [] | |
| for j in pheno.index: | |
| if not j in tmp1: | |
| tmp2.append(np.nan) | |
| else: | |
| tmp2.append(tmp1[j]) | |
| print len(tmp2) | |
| pheno2 = pheno.assign(binSet=tmp2) | |
| # Survival [2,0] | |
| ax = plt.subplot(grid[1,0]) | |
| kmf = KaplanMeierFitter() | |
| pheno3 = pheno2[['SURVIVAL','DEAD','binSet']].dropna(axis='rows') | |
| #pheno3 = pheno2[['survival','vital_status','binSet']].dropna(axis='rows') | |
| T = pheno3['SURVIVAL'] | |
| #T = pheno3['survival'] | |
| E = pheno3['DEAD']=='DEAD' | |
| #E = pheno3['vital_status']=='dead' | |
| groups = pheno3['binSet'] | |
| dist1 = groups.value_counts() | |
| for k in states: | |
| if k in dist1: | |
| ix = (groups==k) | |
| kmf.fit(T[ix], E[ix], label=k) | |
| kmf.plot(ax=ax, ci_show=False, color=states_colors[k]) | |
| #print dir(ax) | |
| #print ax._get_lines().get_next_color() | |
| """# Survival grouped by attractor states [1,1] | |
| ax = plt.subplot(grid[1,1]) | |
| kmf = KaplanMeierFitter() | |
| pheno3 = pheno2[['SURVIVAL','DEAD','binSet']].dropna(axis='rows') | |
| #pheno3 = pheno2[['survival','vital_status','binSet']].dropna(axis='rows') | |
| T = pheno3['SURVIVAL'] | |
| #T = pheno3['survival'] | |
| E = pheno3['DEAD']=='DEAD' | |
| #E = pheno3['vital_status']=='dead' | |
| groups = pheno3['binSet'] | |
| dist1 = groups.value_counts() | |
| onOff_colors = {'off':'k','on':'r'} | |
| onOff = {'off':['010','100','000','110'], 'on':['011','101','001','111']} | |
| for k in onOff: | |
| #if k in dist1: | |
| ix = [] | |
| for i in groups: | |
| if not i in onOff[k]: | |
| ix.append(False) | |
| else: | |
| ix.append(True) | |
| print ix | |
| kmf.fit(T[ix], E[ix], label=k) | |
| kmf.plot(ax=ax, ci_show=False, color=onOff_colors[k]) | |
| #print dir(ax) | |
| #print ax._get_lines().get_next_color() | |
| """ | |
| # Distribution of states [2,1] | |
| ax = plt.subplot(grid[1,1]) | |
| groups = pheno2['binSet'] | |
| dist1 = groups.value_counts() | |
| dist2 = [] | |
| total = float(sum(list(dist1))) | |
| for i in states: | |
| if i in dist1: | |
| dist2.append(float(dist1[i])/total) | |
| else: | |
| dist2.append(0) | |
| index = np.arange(len(states)) | |
| plt.xticks(index, states) | |
| g1, g2, g3, g4, g5, g6, g7, g8 = plt.bar(index, dist2) | |
| g1.set_facecolor(states_colors[states[0]]) | |
| g2.set_facecolor(states_colors[states[1]]) | |
| g3.set_facecolor(states_colors[states[2]]) | |
| g4.set_facecolor(states_colors[states[3]]) | |
| g5.set_facecolor(states_colors[states[4]]) | |
| g6.set_facecolor(states_colors[states[5]]) | |
| g7.set_facecolor(states_colors[states[6]]) | |
| g8.set_facecolor(states_colors[states[7]]) | |
| plt.ylim((0,1)) | |
| plt.ylabel('% Patients') | |
| plt.xlabel('States') | |
| #ax.axhline(rsq,color='k',linestyle='--',alpha=0.5) | |
| #g1.set_facecolor(nodes[geneSets[set1][0]]) | |
| #g2.set_facecolor(nodes[geneSets[set1][1]]) | |
| #g3.set_facecolor(nodes[geneSets[set1][2]]) | |
| plt.tight_layout() | |
| # Bin for LGG+GBM | |
| tmp = binExp_lgg.loc[geneSets[set1]].transpose() | |
| #print tmp | |
| tmp1 = dict(zip(tmp.index,[''.join([str(k) for k in tmp.loc[j].values]) for j in tmp.index])) | |
| tmp2 = [] | |
| for j in pheno_lgg.index: | |
| if not j in tmp1: | |
| tmp2.append(np.nan) | |
| else: | |
| tmp2.append(tmp1[j]) | |
| print len(tmp2) | |
| pheno2 = pheno_lgg.assign(binSet=tmp2) | |
| # LGG+GBM Survival [3,0] | |
| ax = plt.subplot(grid[2,0]) | |
| kmf = KaplanMeierFitter() | |
| #pheno3 = pheno2[['SURVIVAL','DEAD','binSet']].dropna(axis='rows') | |
| pheno3 = pheno2[['survival','vital_status','binSet']].dropna(axis='rows') | |
| #T = pheno3['SURVIVAL'] | |
| T = pheno3['survival'] | |
| #E = pheno3['DEAD']=='DEAD' | |
| E = pheno3['vital_status']=='dead' | |
| groups = pheno3['binSet'] | |
| dist1 = groups.value_counts() | |
| for k in states: | |
| if k in dist1: | |
| ix = (groups==k) | |
| kmf.fit(T[ix], E[ix], label=k) | |
| kmf.plot(ax=ax, ci_show=False, color=states_colors[k]) | |
| #print dir(ax) | |
| #print ax._get_lines().get_next_color() | |
| # Distribution of LGG+GBM states [3,1] | |
| ax = plt.subplot(grid[2,1]) | |
| groups = pheno2['binSet'] | |
| dist1 = groups.value_counts() | |
| dist2 = [] | |
| total = float(sum(list(dist1))) | |
| for i in states: | |
| if i in dist1: | |
| dist2.append(float(dist1[i])/total) | |
| else: | |
| dist2.append(0) | |
| index = np.arange(len(states)) | |
| plt.xticks(index, states) | |
| g1, g2, g3, g4, g5, g6, g7, g8 = plt.bar(index, dist2) | |
| g1.set_facecolor(states_colors[states[0]]) | |
| g2.set_facecolor(states_colors[states[1]]) | |
| g3.set_facecolor(states_colors[states[2]]) | |
| g4.set_facecolor(states_colors[states[3]]) | |
| g5.set_facecolor(states_colors[states[4]]) | |
| g6.set_facecolor(states_colors[states[5]]) | |
| g7.set_facecolor(states_colors[states[6]]) | |
| g8.set_facecolor(states_colors[states[7]]) | |
| plt.ylim((0,1)) | |
| plt.ylabel('% Patients') | |
| plt.xlabel('States') | |
| #ax.axhline(rsq,color='k',linestyle='--',alpha=0.5) | |
| #g1.set_facecolor(nodes[geneSets[set1][0]]) | |
| #g2.set_facecolor(nodes[geneSets[set1][1]]) | |
| #g3.set_facecolor(nodes[geneSets[set1][2]]) | |
| plt.tight_layout() | |
| # Distribution of LGG+GBM states [2,2] | |
| ax = plt.subplot(grid[2,2]) | |
| groups = pheno2.loc[pheno2['disease']=='gbm']['binSet'] | |
| dist1 = groups.value_counts() | |
| dist2 = [] | |
| total = float(sum(list(dist1))) | |
| for i in states: | |
| if i in dist1: | |
| dist2.append(float(dist1[i])/total) | |
| else: | |
| dist2.append(0) | |
| index = np.arange(len(states)) | |
| plt.xticks(index, states) | |
| g1, g2, g3, g4, g5, g6, g7, g8 = plt.bar(index, dist2) | |
| g1.set_facecolor(states_colors[states[0]]) | |
| g2.set_facecolor(states_colors[states[1]]) | |
| g3.set_facecolor(states_colors[states[2]]) | |
| g4.set_facecolor(states_colors[states[3]]) | |
| g5.set_facecolor(states_colors[states[4]]) | |
| g6.set_facecolor(states_colors[states[5]]) | |
| g7.set_facecolor(states_colors[states[6]]) | |
| g8.set_facecolor(states_colors[states[7]]) | |
| plt.ylim((0,1)) | |
| plt.ylabel('% Patients') | |
| plt.xlabel('States') | |
| #ax.axhline(rsq,color='k',linestyle='--',alpha=0.5) | |
| #g1.set_facecolor(nodes[geneSets[set1][0]]) | |
| #g2.set_facecolor(nodes[geneSets[set1][1]]) | |
| #g3.set_facecolor(nodes[geneSets[set1][2]]) | |
| plt.tight_layout() | |
| #plt.show() | |
| pp.savefig(fig) | |
| pp.close() | |
| # Read in Biotapesty file | |
| inEdges = {} | |
| inFile = open('biotapestry_GBM.csv','r') | |
| while 1: | |
| line = inFile.readline() | |
| if not line: | |
| break | |
| splitUp = line.strip().split(',') | |
| if splitUp[0]=='"# Standard Interactions"': | |
| inFile.readline() # Remove header | |
| break | |
| while 1: | |
| line = inFile.readline() | |
| if not line: | |
| break | |
| splitUp = line.strip().split(',') | |
| node1 = splitUp[3].strip('"') | |
| node2 = splitUp[5].strip('"') | |
| for i in node2.split(';'): | |
| if not i in inEdges: | |
| inEdges[i] = {} | |
| if not node1 in inEdges[i]: | |
| inEdges[i][node1] = splitUp[6].strip('"') | |
| inFile.close() | |
| # Load up genesets and netMatrices | |
| data = {} | |
| consistent = {} | |
| gene2probe = {} | |
| subsets = ['all'] | |
| rsq = 0.9 | |
| for subset in subsets: | |
| inFile = open('results_'+subset+'.csv','r') | |
| inFile.readline() # Get rid of header | |
| while 1: | |
| line = inFile.readline() | |
| if not line: | |
| break | |
| splitUp = line.strip().split(',') | |
| if not splitUp[1] in data: | |
| data[splitUp[1]] = {} | |
| consistent[splitUp[1]] = {} | |
| if not splitUp[3] in data[splitUp[1]]: | |
| data[splitUp[1]][splitUp[3]] = {} | |
| consistent[splitUp[1]][splitUp[3]] = {} | |
| data[splitUp[1]][splitUp[3]][subset] = splitUp | |
| if not (splitUp[6]=='Inconsistent' or splitUp[9]=='Inconsistent' or splitUp[12]=='Inconsistent') and (splitUp[6]=='NA' or float(splitUp[6])>=rsq) and (splitUp[9]=='NA' or float(splitUp[9])>=rsq) and (splitUp[12]=='NA' or float(splitUp[12])>=rsq): | |
| consistent[splitUp[1]][splitUp[3]][subset] = 'Yes' | |
| else: | |
| consistent[splitUp[1]][splitUp[3]][subset] = 'No' | |
| inFile.close() | |
| # To translate gene ids later | |
| symbol2entrez = {} | |
| entrez2symbol = {} | |
| with open('gene2entrezId.csv','r') as inFile: | |
| while 1: | |
| inLine = inFile.readline() | |
| if not inLine: | |
| break | |
| split = inLine.strip().split(',') | |
| entrez2symbol[split[1]] = split[0] | |
| symbol2entrez[split[0]] = split[1] | |
| # Read in expression data | |
| exp = pd.read_csv('tfExp.csv', header=0, index_col=0)#.transpose() | |
| exp_lgg = pd.read_csv('gbmlgg.csv', header=0, index_col=0)#.transpose() | |
| # Read in binarized data | |
| binExp = pd.read_csv('tfBin_TCGA_GBM.csv', header=0, index_col=0)#.transpose() | |
| binExp_lgg = pd.read_csv('binGeneExp_VkMeans_lgg.csv', header=0, index_col=0)#.transpose() | |
| #binExp_lgg = pd.read_csv('binGeneExp_VkMeans_lgg_unlogged.csv', header=0, index_col=0)#.transpose() | |
| #binExp_lgg = pd.read_csv('binGeneExp_VkMeans_lgg_unlogged_BASC.csv', header=0, index_col=0)#.transpose() | |
| binExp_lgg.columns = [i.replace('.','-') for i in binExp_lgg.columns.values] | |
| # Phenotypes | |
| pheno = pd.read_csv('phenotypes.csv', header=0, index_col=0) | |
| pheno_lgg = pd.read_csv('phenotypes_lgg.csv', header=0, index_col=0) | |
| # Gather data | |
| geneSets = [] | |
| ids = [] | |
| netMatrices = [] | |
| consistencies = [] | |
| networks = [] | |
| for netMotif in data: | |
| for instance in data[netMotif]: | |
| # Only plot if significant amount of variance explained | |
| if len([i for i in subsets if consistent[netMotif][instance][i]=='Yes']) > 0: | |
| genes = data[netMotif][instance]['all'][3].split(';') | |
| if len(set(genes))==len(genes): | |
| geneSets.append(genes) | |
| ids.append('id'+data[netMotif][instance]['all'][1]+' '+data[netMotif][instance]['all'][2]) | |
| #netMatrices.append([i for i in data[netMotif][instance]['all'][2]]) | |
| tmp = {'all':dict(zip(genes, [data[netMotif][instance]['all'][i] for i in [6,9,12]]))} | |
| for i in tmp: | |
| for j in tmp[i]: | |
| if tmp[i][j]=='Inconsistent': | |
| tmp[i][j] = -0.25 | |
| elif not tmp[i][j]=='NA': | |
| tmp[i][j] = float(tmp[i][j]) | |
| else: | |
| tmp[i][j] = 0 | |
| consistencies.append(tmp) | |
| # Make networks | |
| G = nx.DiGraph() | |
| for gene1 in genes: | |
| if gene1 in inEdges: | |
| for gene2 in inEdges[gene1]: | |
| if gene2 in genes: | |
| if inEdges[gene1][gene2]=='positive': | |
| G.add_edge(gene2,gene1,color='g') | |
| elif inEdges[gene1][gene2]=='negative': | |
| G.add_edge(gene2,gene1,color='r') | |
| networks.append(G) | |
| # Plot them | |
| #plotGenes(geneSets, probeSets, ids, netMatrices, consistencies, networks, rsq) | |
| #plotGenes(geneSets, ids, netMatrices, consistencies, networks, rsq) | |
| plotNetworks(geneSets, ids, consistencies, networks, rsq, exp, binExp, pheno, exp_lgg, binExp_lgg, pheno_lgg, symbol2entrez) | |
| #plotNetworks(geneSets, ids, consistencies, networks, rsq, exp, binExp, pheno, symbol2entrez) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boolean2 | |
| from boolean2 import util, state, network | |
| import matplotlib.pyplot as plt | |
| import networkx as nx | |
| '''rules = """ | |
| # updating rules | |
| IRF4* = MYB | |
| MYB* = GATA3 | |
| GATA3* = IRF4 | |
| """ | |
| ''' | |
| rules = """ | |
| # updating rules | |
| KLF1* = TFAP2C | |
| SPDEF* = TFAP2C and KLF1 | |
| """ | |
| def simulation( trans ): | |
| "One simulation step will update the transition graph" | |
| # create the model | |
| model = boolean2.Model( text=rules, mode='sync') | |
| # generates all states, set limit to a value to keep only the first that many states | |
| # when limit is a number it will take the first that many initial states | |
| initializer = state.all_initial_states( model.nodes, limit=None ) | |
| # the data is the inital data, the func is the initializer | |
| for data, initfunc in initializer: | |
| model.initialize(missing=initfunc) | |
| model.iterate(5) | |
| trans.add( model.states, times=range(5) ) | |
| def main(): | |
| "This is the main code that runs the simulation many times" | |
| # this will hold the transition graph | |
| trans = network.TransGraph( logfile='threenodes.log', verbose=True ) | |
| # will run the simulation this many times | |
| for num in range( 1 ): | |
| simulation ( trans ) | |
| # generate the colormap based on components | |
| colormap = network.component_colormap( trans.graph ) | |
| # saves the transition graph into a gml file | |
| trans.save( 'threenodes.gml', colormap=colormap ) | |
| #nx.draw(trans.graph, with_lables=True) | |
| #pos = nx.spring_layout(trans.graph) | |
| pos = nx.spring_layout(trans.graph,scale=2) | |
| nx.draw_networkx_nodes(trans.graph,pos) | |
| nx.draw_networkx_edges(trans.graph,pos,fontsize=3) | |
| nx.draw_networkx_labels(trans.graph,pos,fontsize=3) | |
| #nx.draw_networkx_labels(trans.graph,pos=nx.spring_layout(trans.graph),fontsize=3) | |
| plt.show() | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment