def make_network_json_single_value(nodes, clust_order, mat ): import json import d3_clustergram # initialize dict d3_json = d3_clustergram.ini_d3_json() # append row dicts to array for i in range(len(nodes['row'])): inst_dict = {} inst_dict['name'] = nodes['row'][i] inst_dict['sort'] = clust_order['row'].index(i) d3_json['row_nodes'].append(inst_dict) # append col dicts to array for i in range(len(nodes['col'])): inst_dict = {} inst_dict['name'] = nodes['col'][i] inst_dict['sort'] = clust_order['col'].index(i) d3_json['col_nodes'].append(inst_dict) # links - generate edge list for i in range(len(nodes['row'])): for j in range(len(nodes['col'])): inst_dict = {} inst_dict['source'] = i inst_dict['target'] = j inst_dict['value'] = mat[i,j] d3_json['links'].append( inst_dict ) # return the json return d3_json
def make_network_json_single_value(nodes, clust_order, mat): import json import d3_clustergram # initialize dict d3_json = d3_clustergram.ini_d3_json() # append row dicts to array for i in range(len(nodes['row'])): inst_dict = {} inst_dict['name'] = nodes['row'][i] inst_dict['sort'] = clust_order['row'].index(i) d3_json['row_nodes'].append(inst_dict) # append col dicts to array for i in range(len(nodes['col'])): inst_dict = {} inst_dict['name'] = nodes['col'][i] inst_dict['sort'] = clust_order['col'].index(i) d3_json['col_nodes'].append(inst_dict) # links - generate edge list for i in range(len(nodes['row'])): for j in range(len(nodes['col'])): inst_dict = {} inst_dict['source'] = i inst_dict['target'] = j inst_dict['value'] = mat[i, j] d3_json['links'].append(inst_dict) # return the json return d3_json
def d3_clust_single_value(nodes, clust_order, data_mat): import json import d3_clustergram # initialize dict d3_json = d3_clustergram.ini_d3_json() # append row dicts to array for i in range(len(nodes['row'])): inst_dict = {} inst_dict['name'] = nodes['row'][i] inst_dict['clust'] = clust_order['clust']['row'].index(i) inst_dict['rank'] = clust_order['rank']['row'][i] # append to row_nodes d3_json['row_nodes'].append(inst_dict) # append col dicts to array for i in range(len(nodes['col'])): inst_dict = {} inst_dict['name'] = nodes['col'][i] inst_dict['clust'] = clust_order['clust']['col'].index(i) inst_dict['rank'] = clust_order['rank']['col'][i] # add to d3_json d3_json['col_nodes'].append(inst_dict) # links - generate edge list ############################### # simply transfer the data from mat for i in range(len(nodes['row'])): for j in range(len(nodes['col'])): # only gather links that are non-zero if data_mat[i,j] > 0: # create link dict #################### # initialize dict inst_dict = {} # set source and target inst_dict['source'] = i inst_dict['target'] = j # get the value from the original matrix inst_value = data_mat[i,j] # save the inst_dict['value'] inst_dict['value'] = inst_value # append to links d3_json['links'].append( inst_dict ) return d3_json
def write_json_single_value(nodes, clust_order, mat, full_path, row_class={}, col_class={}, link_hl={} ): import json import json_scripts import d3_clustergram # initialize dict d3_json = d3_clustergram.ini_d3_json() # generate distance cutoffs all_dist = [] for i in range(11): all_dist.append(float(i)/10) #!! generate tmp classes import random random.seed(122341) # append row dicts to array for i in range(len(nodes['row'])): inst_dict = {} inst_dict['name'] = nodes['row'][i] inst_dict['clust'] = clust_order['clust']['row'].index(i) # do not need to get index inst_dict['rank'] = clust_order['rank']['row'][i] # save group inst_dict['group'] = [] for inst_dist in all_dist: inst_dict['group'].append( float(clust_order['group']['row'][inst_dist][i]) ) # save value for bar inst_dict['value'] = random.random() # add class information inst_dict['class'] = row_class[nodes['row'][i]] # append dictionary d3_json['row_nodes'].append(inst_dict) # append col dicts to array for i in range(len(nodes['col'])): inst_dict = {} inst_dict['name'] = nodes['col'][i] inst_dict['clust'] = clust_order['clust']['col'].index(i) # do not need to get index inst_dict['rank'] = clust_order['rank']['col'][i] # save group data for different cutoffs inst_dict['group'] = [] for inst_dist in all_dist: inst_dict['group'].append( float(clust_order['group']['col'][inst_dist][i]) ) # save value for bar inst_dict['value'] = random.random() # add class information inst_dict['class'] = col_class[nodes['col'][i]] # append dictionary d3_json['col_nodes'].append(inst_dict) # links - generate edge list for i in range(len(nodes['row'])): for j in range(len(nodes['col'])): if abs(mat[i,j]) > 0: inst_dict = {} inst_dict['source'] = i inst_dict['target'] = j inst_dict['value'] = mat[i,j] # initailize with no highlight inst_dict['highlight'] = 0 # add highlight if necessary if len(link_hl) > 0: # check highlight if nodes['col'][j] in link_hl: # check if gene is a known target of the transcription factor if nodes['row'][i] in link_hl[nodes['col'][j]]: # highlight inst_dict['highlight'] = 1 d3_json['links'].append( inst_dict ) # write json ############## # fw = open(full_path, 'w') # fw.write( json.dumps( d3_json, indent=2) ) # fw.close() json_scripts.save_to_json(d3_json, full_path, 'noindent')
def d3_clust_single_value(nodes, clust_order, mat, terms_colors): import json import d3_clustergram # initialize dict d3_json = d3_clustergram.ini_d3_json() # append row dicts to array for i in range(len(nodes['row'])): inst_dict = {} inst_dict['name'] = nodes['row'][i] inst_dict['clust'] = clust_order['clust']['row'].index(i) inst_dict['rank'] = clust_order['rank']['row'][i] inst_dict['nl_pval'] = clust_order['nl_pval']['row'][i] # append to row_nodes d3_json['row_nodes'].append(inst_dict) # append col dicts to array for i in range(len(nodes['col'])): inst_dict = {} inst_dict['name'] = nodes['col'][i] inst_dict['clust'] = clust_order['clust']['col'].index(i) inst_dict['rank'] = clust_order['rank']['col'][i] inst_dict['pval'] = clust_order['pval']['col'][i] inst_dict['nl_pval'] = clust_order['nl_pval']['col'][i] inst_dict['pval_bh'] = clust_order['pval_bh']['col'][i] inst_dict['color'] = terms_colors[nodes['col'][i]] # print(nodes['col'][i]) # add to d3_json d3_json['col_nodes'].append(inst_dict) # get max and min col and row value for scaling # the row nl_pval is just the sum of enriched terms the gene appears in max_row_value = max(clust_order['nl_pval']['row']) max_col_value = max(clust_order['nl_pval']['col']) # links - generate edge list for i in range(len(nodes['row'])): for j in range(len(nodes['col'])): # initialize dict inst_dict = {} # set source and target inst_dict['source'] = i inst_dict['target'] = j # calculate the inst_value, a combination of col and row attributes # scale value by largest row_value = clust_order['nl_pval']['row'][i] / max_row_value col_value = clust_order['nl_pval']['col'][j] / max_col_value # take the mean of the two values times the binary mat[i,j] inst_value = ( row_value + col_value )/ 2 * mat[i,j] # make minimum inst_value 0.1 if inst_value > 0: # save the inst_dict['value'] inst_dict['value'] = inst_value + 0.1 # add the color inst_dict['color'] = terms_colors[nodes['col'][j]] # only keep non-zero value links if inst_value > 0: # append to links d3_json['links'].append( inst_dict ) return d3_json
def write_json_single_value(nodes, clust_order, LDR, full_path, perts, row_class={}, col_class={}, link_hl={} ): import json import json_scripts import d3_clustergram print(perts.keys()) #!! special case, encode extra released information for LDR mat = LDR['mat'] # get release data rl = LDR['rl'] print('\n\nchecking rl\n\n') # print(rl['t']) # initialize dict d3_json = d3_clustergram.ini_d3_json() # generate distance cutoffs all_dist = [] for i in range(11): all_dist.append(float(i)/10) #!! generate tmp classes import random random.seed(122341) # append row dicts to array for i in range(len(nodes['row'])): inst_dict = {} inst_dict['name'] = nodes['row'][i] inst_dict['clust'] = clust_order['clust']['row'].index(i) # do not need to get index inst_dict['rank'] = clust_order['rank']['row'][i] # # save group # inst_dict['group'] = [] # for inst_dist in all_dist: # inst_dict['group'].append( float(clust_order['group']['row'][inst_dist][i]) ) # # save value for bar # inst_dict['value'] = random.random() # # add class information # inst_dict['class'] = row_class[nodes['row'][i]] # append dictionary d3_json['row_nodes'].append(inst_dict) # append col dicts to array for i in range(len(nodes['col'])): inst_dict = {} inst_dict['name'] = nodes['col'][i] inst_dict['clust'] = clust_order['clust']['col'].index(i) # do not need to get index inst_dict['rank'] = clust_order['rank']['col'][i] # # save group data for different cutoffs # inst_dict['group'] = [] # for inst_dist in all_dist: # inst_dict['group'].append( float(clust_order['group']['col'][inst_dist][i]) ) # # save value for bar # inst_dict['value'] = random.random() # # add class information # inst_dict['class'] = col_class[nodes['col'][i]] # append dictionary d3_json['col_nodes'].append(inst_dict) # links - generate edge list for i in range(len(nodes['row'])): for j in range(len(nodes['col'])): if abs(mat[i,j]) > 0: inst_dict = {} inst_dict['source'] = i inst_dict['target'] = j inst_dict['value'] = mat[i,j] # !! custom change for LDRgram inst_dict['value_up'] = rl['t'][i,j] inst_dict['value_dn'] = -rl['f'][i,j] # print('\tas: '+nodes['row'][i]) # print('\tcl: '+nodes['col'][j]) # add perturbation information inst_tuple = ( nodes['row'][i], nodes['col'][j] ) # print( perts[inst_tuple] ) # add to dictionary inst_dict['perts'] = perts[inst_tuple] d3_json['links'].append( inst_dict ) # write json ############## json_scripts.save_to_json(d3_json, full_path, 'indent')
def write_json_single_value(nodes, clust_order, LDR, full_path, perts, row_class={}, col_class={}, link_hl={}): import json import json_scripts import d3_clustergram print(perts.keys()) #!! special case, encode extra released information for LDR mat = LDR['mat'] # get release data rl = LDR['rl'] print('\n\nchecking rl\n\n') # print(rl['t']) # initialize dict d3_json = d3_clustergram.ini_d3_json() # generate distance cutoffs all_dist = [] for i in range(11): all_dist.append(float(i) / 10) #!! generate tmp classes import random random.seed(122341) # append row dicts to array for i in range(len(nodes['row'])): inst_dict = {} inst_dict['name'] = nodes['row'][i] inst_dict['clust'] = clust_order['clust']['row'].index(i) # do not need to get index inst_dict['rank'] = clust_order['rank']['row'][i] # # save group # inst_dict['group'] = [] # for inst_dist in all_dist: # inst_dict['group'].append( float(clust_order['group']['row'][inst_dist][i]) ) # # save value for bar # inst_dict['value'] = random.random() # # add class information # inst_dict['class'] = row_class[nodes['row'][i]] # append dictionary d3_json['row_nodes'].append(inst_dict) # append col dicts to array for i in range(len(nodes['col'])): inst_dict = {} inst_dict['name'] = nodes['col'][i] inst_dict['clust'] = clust_order['clust']['col'].index(i) # do not need to get index inst_dict['rank'] = clust_order['rank']['col'][i] # # save group data for different cutoffs # inst_dict['group'] = [] # for inst_dist in all_dist: # inst_dict['group'].append( float(clust_order['group']['col'][inst_dist][i]) ) # # save value for bar # inst_dict['value'] = random.random() # # add class information # inst_dict['class'] = col_class[nodes['col'][i]] # append dictionary d3_json['col_nodes'].append(inst_dict) # links - generate edge list for i in range(len(nodes['row'])): for j in range(len(nodes['col'])): if abs(mat[i, j]) > 0: inst_dict = {} inst_dict['source'] = i inst_dict['target'] = j inst_dict['value'] = mat[i, j] # !! custom change for LDRgram inst_dict['value_up'] = rl['t'][i, j] inst_dict['value_dn'] = -rl['f'][i, j] # print('\tas: '+nodes['row'][i]) # print('\tcl: '+nodes['col'][j]) # add perturbation information inst_tuple = (nodes['row'][i], nodes['col'][j]) # print( perts[inst_tuple] ) # add to dictionary inst_dict['perts'] = perts[inst_tuple] d3_json['links'].append(inst_dict) # write json ############## json_scripts.save_to_json(d3_json, full_path, 'indent')