def test_types(): G = NdexGraph() n = G.add_new_node('Node with Types') n1 = G.add_new_node('A') n2 = G.add_new_node('B') G.add_edge_between(n, n1) G.add_edge_between(n, n2) G.set_name('Test Types') G.node[n]['string'] = 'mystring' G.node[n]['bool'] = True G.node[n]['int'] = 5 G.node[n]['double'] = 2.5 # Python3 doesn't support Long (you cannot have a = 5L); # If we need an integer being a long in Python 2 and still be compatible with Python 3, # we need to define up a long variable to be the same as the int class under Python 3, # and it can then be used explicitly to make sure the integer is a long. # # it is taken from http: //python3porting.com/noconv.html if sys.version_info.major == 3: long = int # long(5) will be 5L in Python 2 and just int 5 (which is long) in Python 3 G.node[n]['long'] = long(5) G.node[n]['string_list'] = ['mystring', 'myotherstring'] G.node[n]['bool_list'] = [False, True] G.node[n]['int_list'] = [5, -20] G.node[n]['double_list'] = [2.5, 3.7] # long(5), long(75) will be 5L, 75L in Python 2 and just int 5, 75 (which is long) in Python 3 G.node[n]['long_list'] = [long(5), long(75)] G.write_to('temp_test_type.cx')
def scratch_test(): G = NdexGraph() G.add_new_node('1') G.add_new_node('2') G.add_edge_between(1,2) G.set_name('scratch-test1 - jing') print(G.graph())
def scratch_test(): G = NdexGraph() G.add_new_node('1') G.add_new_node('2') G.add_edge_between(1, 2) G.set_name('scratch-test1 - jing') print(G.graph())
def test_layout(): nx_G = nx.complete_graph(11) # nx_G.remove_node(0) # nx_G.remove_node(1) G = NdexGraph() G.add_new_node('1') G.add_new_node('2') G.add_new_node('3') G.add_new_node('4') G.add_new_node('5') G.add_new_node('6') G.add_edge_between(1, 3) G.add_edge_between(1, 4) G.add_edge_between(2, 3) G.add_edge_between(2, 4) G.add_edge_between(3, 5) G.add_edge_between(3, 6) G.add_edge_between(4, 5) G.add_edge_between( 4, 6, interaction='AAABBBCCC') #attr_dict={'interaction':'testing'}) initial_pos = { 1: (0.0, 1.0), 2: (0.0, 0.0), # 3: (0.5, 0.5), # 4: (0.5, 0.5), 5: (1.0, 1.0), 6: (1.0, 0.0), } fixed = [1, 2, 5, 6] # G.add_new_node('3') # G.add_new_node('4') # G.add_new_node('5') G.pos = nx.spring_layout(G.to_undirected(), pos=initial_pos, fixed=fixed) print(G.pos) G.set_name('spring_layout undirected')
def test_layout(): nx_G = nx.complete_graph(11) # nx_G.remove_node(0) # nx_G.remove_node(1) G = NdexGraph() G.add_new_node('1') G.add_new_node('2') G.add_new_node('3') G.add_new_node('4') G.add_new_node('5') G.add_new_node('6') G.add_edge_between(1, 3) G.add_edge_between(1, 4) G.add_edge_between(2, 3) G.add_edge_between(2, 4) G.add_edge_between(3, 5) G.add_edge_between(3, 6) G.add_edge_between(4, 5) G.add_edge_between(4, 6, interaction='AAABBBCCC') #attr_dict={'interaction':'testing'}) initial_pos = { 1: (0.0, 1.0), 2: (0.0, 0.0), # 3: (0.5, 0.5), # 4: (0.5, 0.5), 5: (1.0, 1.0), 6: (1.0, 0.0), } fixed = [1,2,5,6] # G.add_new_node('3') # G.add_new_node('4') # G.add_new_node('5') G.pos = nx.spring_layout(G.to_undirected(), pos=initial_pos, fixed=fixed) print(G.pos) G.set_name('spring_layout undirected')
def test_types(): G = NdexGraph() n = G.add_new_node('Node with Types') n1 = G.add_new_node('A') n2 = G.add_new_node('B') G.add_edge_between(n, n1) G.add_edge_between(n, n2) G.set_name('Test Types') G.node[n]['string'] = 'mystring' G.node[n]['bool'] = True G.node[n]['int'] = 5 G.node[n]['double'] = 2.5 # Python3 doesn't support Long (you cannot have a = 5L); # If we need an integer being a long in Python 2 and still be compatible with Python 3, # we need to define up a long variable to be the same as the int class under Python 3, # and it can then be used explicitly to make sure the integer is a long. # # it is taken from http: //python3porting.com/noconv.html if sys.version_info.major == 3: long = int # long(5) will be 5L in Python 2 and just int 5 (which is long) in Python 3 G.node[n]['long'] = long(5) G.node[n]['string_list'] = ['mystring','myotherstring'] G.node[n]['bool_list'] = [False, True] G.node[n]['int_list'] = [5, -20] G.node[n]['double_list'] = [2.5, 3.7] # long(5), long(75) will be 5L, 75L in Python 2 and just int 5, 75 (which is long) in Python 3 G.node[n]['long_list'] = [long(5), long(75)] G.write_to('temp_test_type.cx')
def test_types(): G = NdexGraph() n = G.add_new_node('Node with Types') n1 = G.add_new_node('A') n2 = G.add_new_node('B') G.add_edge_between(n, n1) G.add_edge_between(n, n2) G.set_name('Test Types') G.node[n]['string'] = 'mystring' G.node[n]['bool'] = True G.node[n]['int'] = 5 G.node[n]['double'] = 2.5 G.node[n]['long'] = 5L G.node[n]['string_list'] = ['mystring', 'myotherstring'] G.node[n]['bool_list'] = [False, True] G.node[n]['int_list'] = [5, -20] G.node[n]['double_list'] = [2.5, 3.7] G.node[n]['long_list'] = [5L, 75L] G.write_to('temp_test_type.cx') G.upload_to('http://test.ndexbio.org', 'scratch', 'scratch')
def test_types(): G = NdexGraph() n = G.add_new_node('Node with Types') n1 = G.add_new_node('A') n2 = G.add_new_node('B') G.add_edge_between(n, n1) G.add_edge_between(n, n2) G.set_name('Test Types') G.node[n]['string'] = 'mystring' G.node[n]['bool'] = True G.node[n]['int'] = 5 G.node[n]['double'] = 2.5 G.node[n]['long'] = 5L G.node[n]['string_list'] = ['mystring','myotherstring'] G.node[n]['bool_list'] = [False, True] G.node[n]['int_list'] = [5, -20] G.node[n]['double_list'] = [2.5, 3.7] G.node[n]['long_list'] = [5L, 75L] G.write_to('temp_test_type.cx') G.upload_to('http://test.ndexbio.org', 'scratch', 'scratch')
def test_complex_layout_create(): G = NdexGraph() s = G.add_new_node('Source', type='Source') t = G.add_new_node('Target', type='Target') f = G.add_new_node('Forward', type='Forward') r = G.add_new_node('Reverse', type='Reverse') b = G.add_new_node('Both', type='Both') G.add_edge_between(s, f) G.add_edge_between(f, t) G.add_edge_between(t, r) G.add_edge_between(r, s) G.add_edge_between(s, b) G.add_edge_between(b, t) G.add_edge_between(t, b) G.add_edge_between(b, s) G.set_name('layout template') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def test_complex_layout_create(): G = NdexGraph() s = G.add_new_node('Source', type='Source') t = G.add_new_node('Target', type='Target') f = G.add_new_node('Forward', type='Forward') r = G.add_new_node('Reverse', type='Reverse') b = G.add_new_node('Both', type='Both') G.add_edge_between(s, f) G.add_edge_between(f, t) G.add_edge_between(t, r) G.add_edge_between(r, s) G.add_edge_between(s, b) G.add_edge_between(b, t) G.add_edge_between(t, b) G.add_edge_between(b, s) G.set_name('layout template') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def test_template(): template_id = 'f35fbfd3-4918-11e6-a5c7-06603eb7f303' G = NdexGraph() G.add_new_node('1', color='green') G.add_new_node('2', color='red') G.add_new_node('3', color='green') G.add_new_node('4', color='green') G.add_new_node('5', color='green') G.add_new_node('6', color='red') G.add_edge_between(1, 2) G.add_edge_between(2, 3) G.add_edge_between(3, 4) G.add_edge_between(4, 5) G.add_edge_between(5, 6) toolbox.apply_template(G, template_id) G.set_name('template apply') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def test_template(): template_id = 'f35fbfd3-4918-11e6-a5c7-06603eb7f303' G = NdexGraph() G.add_new_node('1', color='green') G.add_new_node('2', color='red') G.add_new_node('3', color='green') G.add_new_node('4', color='green') G.add_new_node('5', color='green') G.add_new_node('6', color='red') G.add_edge_between(1, 2) G.add_edge_between(2, 3) G.add_edge_between(3, 4) G.add_edge_between(4, 5) G.add_edge_between(5, 6) toolbox.apply_template(G, template_id) G.set_name('template apply') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
for x in name: if x.isdigit(): i = i + 1 G.add_new_node( name, {"Number of Integers": sum(nd.isdigit() for nd in name)}) #adding edges counter = 0 j = 1 iteration = 0 while j < 10: counter = j + 1 while counter < 11: iteration = iteration + 1 G.add_edge_between(j, counter) similar = 0 for jChar in G.get_node_name_by_id(j): for counterChar in G.get_node_name_by_id(counter): if jChar == counterChar: similar = similar + 1 counter = counter + 1 G.set_edge_attribute(iteration, "Similar Characters", similar) j = j + 1 #print statements #edgesx = combinations(list, 2) print "There are " + str(iteration) + " edges" dictEdges = {}
def create_similarity_map(name, e_set, min_subsumption, id_attribute="genes", max_edges=5): similarity_graph = NdexGraph() similarity_graph.set_name(name) set_name_to_node_id_map = {} id_sets = {} for network_id in e_set.id_set_map: id_set_object = e_set.id_set_map[network_id] network_name = id_set_object.name id_set = id_set_object.set id_sets[network_id] = id_set att = {id_attribute: list(id_set)} node_id = similarity_graph.add_new_node(network_name, att) set_name_to_node_id_map[network_id] = node_id source_similarities = {} for network_id_1 in id_sets.keys(): source_node_id = set_name_to_node_id_map[network_id_1] list_1 = list(id_sets[network_id_1]) set_1_size = len(list_1) similarities = [] for network_id_2 in id_sets.keys(): if network_id_1 != network_id_2: set_1 = id_sets[network_id_1] set_2 = id_sets[network_id_2] target_node_id = set_name_to_node_id_map[network_id_2] list_2 = list(id_sets[network_id_2]) set_2_size = len(list_2) overlap = list(set_1.intersection(set_2)) size_overlap=len(overlap) if size_overlap != 0: subsumes = size_overlap/set_2_size #subsumes_1 = size_overlap/set_2_size #subsumes_2 = size_overlap/set_1_size #subsumes = min(subsumes_1, subsumes_2) if size_overlap > 3: print "overlap: %s %s" % (size_overlap, overlap) similarity = {"source_node_id": source_node_id, "target_node_id": target_node_id, "subsumes": subsumes} similarity["atts"] = {"subsumes": subsumes, "overlap": overlap, "overlap_size": size_overlap} similarities.append(similarity) else: print "no overlap" # rank the similarities similarities = sorted(similarities, key=operator.itemgetter('subsumes'), reverse=True) source_similarities[network_id_1] = similarities # always include the most similar node to make sure that each node has at least one edge and the graph is connected # don't connect more than max_edges for network_id, similarities in source_similarities.iteritems(): count = 0 for similarity in similarities: if count >= max_edges: break if count == 0 or similarity["subsumes"] > min_subsumption: atts = similarity["atts"] source_node_id = similarity["source_node_id"] target_node_id = similarity["target_node_id"] similarity_graph.add_edge_between(source_node_id, target_node_id, attr_dict=atts) count = count + 1 return similarity_graph
def test_complex_layout(): G = NdexGraph() s1 = G.add_new_node('S1', st_layout='Source') s2 = G.add_new_node('S2', st_layout='Source') s3 = G.add_new_node('S3', st_layout='Source') t1 = G.add_new_node('T1', st_layout='Target') t2 = G.add_new_node('T2', st_layout='Target') f1 = G.add_new_node('F1', st_layout='Forward') f2 = G.add_new_node('F2', st_layout='Forward') f3 = G.add_new_node('F3', st_layout='Forward') f4 = G.add_new_node('F4', st_layout='Forward') f5 = G.add_new_node('F5', st_layout='Forward') r1 = G.add_new_node('R1', st_layout='Reverse') r2 = G.add_new_node('R2', st_layout='Reverse') r3 = G.add_new_node('R3', st_layout='Reverse') b = G.add_new_node('B1', st_layout='Both') G.add_edge_between(r3,r2) G.add_edge_between(r2,r1) G.add_edge_between(r1,s1) G.add_edge_between(r3,b) G.add_edge_between(s2, f3) G.add_edge_between(s3, f1) G.add_edge_between(f1, f2) G.add_edge_between(f2, t1) G.add_edge_between(f1, b) G.add_edge_between(f3, f4) G.add_edge_between(f4, f5) G.add_edge_between(f5, t2) G.add_edge_between(b, t1) G.add_edge_between(b, s2) G.add_edge_between(b, r1) G.add_edge_between(t1,r3) toolbox.apply_source_target_layout(G) template_id = 'd1856d17-4937-11e6-a5c7-06603eb7f303' toolbox.apply_template(G, template_id) G.set_name('experiment1') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def test_complex_layout(): G = NdexGraph() s1 = G.add_new_node('S1', st_layout='Source') s2 = G.add_new_node('S2', st_layout='Source') s3 = G.add_new_node('S3', st_layout='Source') t1 = G.add_new_node('T1', st_layout='Target') t2 = G.add_new_node('T2', st_layout='Target') f1 = G.add_new_node('F1', st_layout='Forward') f2 = G.add_new_node('F2', st_layout='Forward') f3 = G.add_new_node('F3', st_layout='Forward') f4 = G.add_new_node('F4', st_layout='Forward') f5 = G.add_new_node('F5', st_layout='Forward') r1 = G.add_new_node('R1', st_layout='Reverse') r2 = G.add_new_node('R2', st_layout='Reverse') r3 = G.add_new_node('R3', st_layout='Reverse') b = G.add_new_node('B1', st_layout='Both') G.add_edge_between(r3, r2) G.add_edge_between(r2, r1) G.add_edge_between(r1, s1) G.add_edge_between(r3, b) G.add_edge_between(s2, f3) G.add_edge_between(s3, f1) G.add_edge_between(f1, f2) G.add_edge_between(f2, t1) G.add_edge_between(f1, b) G.add_edge_between(f3, f4) G.add_edge_between(f4, f5) G.add_edge_between(f5, t2) G.add_edge_between(b, t1) G.add_edge_between(b, s2) G.add_edge_between(b, r1) G.add_edge_between(t1, r3) toolbox.apply_source_target_layout(G) template_id = 'd1856d17-4937-11e6-a5c7-06603eb7f303' toolbox.apply_template(G, template_id) G.set_name('experiment1') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def ebs_to_network(ebs, name="not named"): G = NdexGraph() G.set_name(name) node_id_map = {} identifier_citation_id_map = {} node_mapping = {} mg = mygene.MyGeneInfo() # Create Nodes # PARTICIPANT PARTICIPANT_TYPE PARTICIPANT_NAME UNIFICATION_XREF RELATIONSHIP_XREF id_list = [] for node in ebs.get("node_table"): if "PARTICIPANT" in node: participant = node["PARTICIPANT"] participant = clean_brackets(participant) if participant is not None and '_HUMAN' in participant and node_mapping.get( participant) is None: id_list.append(participant) url = 'https://biodbnet-abcc.ncifcrf.gov/webServices/rest.php/biodbnetRestApi.json?method=db2db&input=uniprot entry name&inputValues=' + ','.join( id_list) + '&outputs=genesymbol&taxonId=9606&format=row' look_up_req = requests.get(url) look_up_json = look_up_req.json() if look_up_json is not None: for bio_db_item in look_up_json: gene_symbol_mapping[bio_db_item.get( 'InputValue')] = bio_db_item.get('Gene Symbol') node_mapping[bio_db_item.get('InputValue')] = bio_db_item.get( 'Gene Symbol') for node in ebs.get("node_table"): attributes = {} if "PARTICIPANT" in node: participant = node["PARTICIPANT"] participant = clean_brackets(participant) participant_symbol = '' if participant is not None: #participant = participant.replace('_HUMAN', '') participant_symbol = gene_symbol_mapping.get(participant) #===================================== # if the node is not a protien type # then skip mygene.info look up #===================================== if node.get('PARTICIPANT_TYPE') is not None and node.get( 'PARTICIPANT_TYPE') != 'ProteinReference': participant_symbol = participant if participant_symbol is None: participant_symbol = mg.query(participant, species='human') if participant_symbol is not None and participant_symbol.get( 'hits') is not None and len( participant_symbol.get('hits')) > 0: ph = participant_symbol.get('hits')[0] if 'symbol' in ph: participant_symbol = str(ph.get('symbol')) gene_symbol_mapping[ participant] = participant_symbol else: participant_symbol = None participant_name = None if participant_symbol is not None: node_name = participant_symbol else: node_name = participant node_name_tmp = participant if "PARTICIPANT_NAME" in node: name = node["PARTICIPANT_NAME"] if name is not None and len(name) > 0: attributes["name"] = name participant_name = name node_type = _get_node_type(node.get("PARTICIPANT_TYPE")) if "UNIFICATION_XREF" in node: alias_string = node["UNIFICATION_XREF"] if alias_string is not None and alias_string is not "": aliases = alias_string.split(";") attributes["alias"] = aliases # attributes["represents"] = alias[0] - can't take first alias for ndexebs. # Need to resolve uniprot primary id for the gene if node_type == "Other": node_type = layouts.aliases_to_node_type(aliases) attributes["type"] = node_type if node_name_tmp.startswith("CHEBI") and participant_name: node_name = participant_name # check mygene.info #if there is a result use the gene symbol and add the uniprot id as an alias node_id = G.add_new_node(node_name, attributes) node_id_map[participant] = node_id # Create Edges # PARTICIPANT_A INTERACTION_TYPE PARTICIPANT_B INTERACTION_DATA_SOURCE # INTERACTION_PUBMED_ID PATHWAY_NAMES MEDIATOR_IDS for edge in ebs.get("edge_table"): if "INTERACTION_TYPE" not in edge: raise "No interaction type for edge " + str(edge) if "PARTICIPANT_A" not in edge: raise "No participant A in edge " + str(edge) if "PARTICIPANT_B" not in edge: raise "No participant B in edge " + str(edge) attributes = {} interaction = edge["INTERACTION_TYPE"] attributes["interaction"] = interaction if interaction in DIRECTED_INTERACTIONS: attributes["directed"] = True # handle pmids as edge attribute if "INTERACTION_PUBMED_ID" in edge: pmid_string = edge["INTERACTION_PUBMED_ID"] if pmid_string is not None and pmid_string is not "": pmids = pmid_string.split(";") attributes["pmid"] = pmids participant_a = clean_brackets(edge["PARTICIPANT_A"]) participant_b = clean_brackets(edge["PARTICIPANT_B"]) #if participant_a is not None: # participant_a = participant_a.replace('_HUMAN', '') #if participant_b is not None: # participant_b = participant_b.replace('_HUMAN', '') source_node_id = node_id_map.get(participant_a) target_node_id = node_id_map.get(participant_b) edge_id = G.add_edge_between(source_node_id, target_node_id, interaction=interaction, attr_dict=attributes) # handle pmids in CX citation and edgeCitation aspect representations in networkn network object if "pmid" in attributes: prefixed_pmids = [] for pmid in attributes["pmid"]: prefixed_pmids.append("pmid:" + str(pmid)) for prefixed_pmid in prefixed_pmids: if prefixed_pmid not in identifier_citation_id_map: citation_id = G.add_citation(identifier=prefixed_pmid) identifier_citation_id_map[prefixed_pmid] = citation_id G.add_citation_to_edge( edge_id, prefixed_pmid ) #add_edge_citation_ref(edge_id, citation_id) else: citation_id = identifier_citation_id_map[prefixed_pmid] G.add_citation_to_edge( edge_id, prefixed_pmid ) #add_edge_citation_ref(edge_id, citation_id) # save gene_symbol_mapping to file (caching) with open(join(current_directory, 'gene_symbol_mapping.json'), 'w') as outfile: json.dump(gene_symbol_mapping, outfile, indent=4) return G