def test_bad_node_attr(self): niceCx_creatures = NiceCXNetwork() niceCx_creatures.set_name("Test Network") fox_node = niceCx_creatures.create_node(node_name=453) niceCx_creatures.add_node_attribute(property_of=fox_node, name='Color', values=['Red',"tree"]) upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def test_bad_node_attr(self): niceCx_creatures = NiceCXNetwork() niceCx_creatures.set_name("Test Network") fox_node = niceCx_creatures.create_node(node_name=453) niceCx_creatures.add_node_attribute(property_of=fox_node, name='Color', values=['Red',"tree"]) upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def test_add_int_attr(self): niceCx = NiceCXNetwork() int_node = niceCx.create_node(node_name="testint") niceCx.add_node_attribute(property_of=int_node, name='Size', values=1, type="integer") upload_message = niceCx.upload_to(upload_server, upload_username, upload_password) UUID = upload_message.split('/')[-1] time.sleep(1) imported_cx = ndex2.create_nice_cx_from_server(server='dev.ndexbio.org', uuid=UUID, username=upload_username, password=upload_password) for i in imported_cx.get_node_attributes(0): self.assertEqual(i.get('d'), "integer")
def test_add_lst_flt(self): niceCx = NiceCXNetwork() list_float_node = niceCx.create_node(node_name="testlstflt") niceCx.add_node_attribute(property_of=list_float_node, name='Too many scores', values=[15.3,43.6,-34.0,43.3], type="list_of_float") upload_message = niceCx.upload_to(upload_server, upload_username, upload_password) UUID = upload_message.split('/')[-1] time.sleep(1) imported_cx = ndex2.create_nice_cx_from_server(server='dev.ndexbio.org', uuid=UUID, username=upload_username, password=upload_password) for i in imported_cx.get_node_attributes(0): self.assertEqual(i.get('d'), "list_of_double")
def test_add_lst_int(self): niceCx = NiceCXNetwork() list_int_node = niceCx.create_node(node_name="testlstint") niceCx.add_node_attribute(property_of=list_int_node, name='Too many sizes', values=[1, 2, 3, 4, 5], type="list_of_integer") upload_message = niceCx.upload_to(upload_server, upload_username, upload_password) UUID = upload_message.split('/')[-1] time.sleep(1) imported_cx = ndex2.create_nice_cx_from_server( server='dev.ndexbio.org', uuid=UUID, username=upload_username, password=upload_password) for i in imported_cx.get_node_attributes(0): self.assertEqual(i.get('d'), "list_of_integer")
def test_add_flt(self): niceCx = NiceCXNetwork() float_node = niceCx.create_node(node_name="testflt") niceCx.add_node_attribute(property_of=float_node, name='Score', values=1.0, type="float") upload_message = niceCx.upload_to(upload_server, upload_username, upload_password) UUID = upload_message.split('/')[-1] time.sleep(1) imported_cx = ndex2.create_nice_cx_from_server( server='dev.ndexbio.org', uuid=UUID, username=upload_username, password=upload_password) for i in imported_cx.get_node_attributes(0): self.assertEqual(i.get('d'), "double")
def test_simple_create(self): niceCx_creatures = NiceCXNetwork() niceCx_creatures.set_name("Food Web") fox_node = niceCx_creatures.create_node(node_name='Fox') mouse_node = niceCx_creatures.create_node(node_name='Mouse') bird_node = niceCx_creatures.create_node(node_name='Bird') fox_bird_edge = niceCx_creatures.create_edge( edge_source=fox_node, edge_target=bird_node, edge_interaction='interacts-with') fox_mouse_edge = niceCx_creatures.create_edge( edge_source=fox_node, edge_target=mouse_node, edge_interaction='interacts-with') niceCx_creatures.add_node_attribute(property_of=fox_node, name='Color', values='Red') niceCx_creatures.add_node_attribute(property_of=mouse_node, name='Color', values='Gray') niceCx_creatures.add_node_attribute(property_of=bird_node, name='Color', values='Blue') niceCx_creatures.add_edge_attribute(property_of=fox_mouse_edge, name='Hunted', values='On the ground') print(niceCx_creatures.get_node_attribute(fox_node, 'Color'))
def test_simple_create(self): niceCx_creatures = NiceCXNetwork() niceCx_creatures.set_name("Food Web") fox_node = niceCx_creatures.create_node(node_name='Fox') mouse_node = niceCx_creatures.create_node(node_name='Mouse') bird_node = niceCx_creatures.create_node(node_name='Bird') fox_bird_edge = niceCx_creatures.create_edge(edge_source=fox_node, edge_target=bird_node, edge_interaction='interacts-with') fox_mouse_edge = niceCx_creatures.create_edge(edge_source=fox_node, edge_target=mouse_node, edge_interaction='interacts-with') niceCx_creatures.add_node_attribute(property_of=fox_node, name='Color', values='Red') niceCx_creatures.add_node_attribute(property_of=mouse_node, name='Color', values='Gray') niceCx_creatures.add_node_attribute(property_of=bird_node, name='Color', values='Blue') niceCx_creatures.add_edge_attribute(property_of=fox_mouse_edge, name='Hunted', values='On the ground') print(niceCx_creatures.get_node_attribute(fox_node, 'Color'))
class NiceCxAssembler(object): """Assembles a Nice CX network from a set of INDRA Statements. Parameters ---------- stmts : Optional[list[indra.statements.Statement]] A list of INDRA Statements to be assembled. network_name : Optional[str] The name of the network to be assembled. Default: indra_assembled Attributes ---------- network : ndex2.nice_cx_network.NiceCXNetwork A Nice CX network object that is assembled from Statements. """ def __init__(self, stmts=None, network_name=None): self.statements = stmts if stmts else [] self.network = NiceCXNetwork() self.network.set_network_attribute( 'name', (network_name if network_name else 'indra_assembled')) self.node_keys = {} self.context_prefixes = { 'pubmed': 'https://identifiers.org/pubmed:', 'hgnc.symbol': 'https://identifiers.org/hgnc.symbol:' } def make_model(self, self_loops=False, network_attributes=None): """Return a Nice CX network object after running assembly. Parameters ---------- self_loops : Optional[bool] If False, self-loops are excluded from the network. Default: False network_attributes : Optional[dict] A dictionary containing attributes to be added to the assembled network. Returns ------- ndex2.nice_cx_network.NiceCXNetwork The assembled Nice CX network. """ for stmt in self.statements: agents = stmt.agent_list() not_none_agents = [a for a in agents if a is not None] if len(not_none_agents) < 2: continue for a1, a2 in itertools.combinations(not_none_agents, 2): a1_id = self.add_node(a1) a2_id = self.add_node(a2) if not self_loops and a1_id == a2_id: continue edge_id = self.add_edge(a1_id, a2_id, stmt) self.network.set_network_attribute('@context', json.dumps(self.context_prefixes)) if network_attributes: for k, v in network_attributes.items(): self.network.set_network_attribute(k, v, 'string') return self.network def add_node(self, agent): """Add an Agent to the network as a node.""" agent_key = self.get_agent_key(agent) # If the node already exists if agent_key in self.node_keys: return self.node_keys[agent_key] # If the node doesn't exist yet db_ns, db_id = agent.get_grounding() # TODO: handle more represents name spaces if db_ns == 'HGNC': represents = 'hgnc.symbol:%s' % agent.name else: represents = None node_id = self.network.create_node(agent.name, node_represents=represents) self.node_keys[agent_key] = node_id # Add db_refs as aliases db_refs_list = [ '%s:%s' % (db_name, db_id) for db_name, db_id in agent.db_refs.items() if db_name in url_prefixes ] if db_refs_list: self.network.add_node_attribute(property_of=node_id, name='aliases', values=db_refs_list, type='list_of_string') # Add the type of the node, inferred from grounding if db_ns: mapped_type = db_ns_type_mappings.get(db_ns) if mapped_type: self.network.add_node_attribute(property_of=node_id, name='type', values=mapped_type, type='string') return node_id def add_edge(self, a1_id, a2_id, stmt): """Add a Statement to the network as an edge.""" stmt_type = stmt.__class__.__name__ edge_id = self.network.create_edge(a1_id, a2_id, stmt_type) evs = [] for ev in stmt.evidence: # We skip evidences with no PMID if not ev.pmid: continue # We take a maximum 200 character snippet of the evidence text if not ev.text: ev_txt = 'Evidence text not available.' elif len(ev.text) > 200: ev_txt = ev.text[:200] + '...' else: ev_txt = ev.text # Construct a clickable PMID link with the source and evidence text ev_str = ('<a target="_blank" ' 'href="https://identifiers.org/pubmed:%s">' 'pubmed:%s</a> (%s) %s') % (ev.pmid, ev.pmid, ev.source_api, ev_txt) evs.append((ev_str, 0 if ev.text is None else 1)) # Reorder to have ones with text first evs = sorted(evs, key=lambda x: x[1], reverse=True) # Cap at 10 pieces of evidence evs = [e[0] for e in evs[:10]] self.network.set_edge_attribute(edge_id, 'citation', evs, type='list_of_string') return edge_id def print_model(self): """Return the CX string of the assembled model.""" return self.network.to_cx() @staticmethod def get_agent_key(agent): return agent.name
def get_nice_cx_network_from_clusterfile(inputfile): """ :param inputfile: :return: """ network = NiceCXNetwork() network.set_name('clustertocx from ' + str(os.path.basename(os.path.dirname(inputfile)))) with open(inputfile, 'r') as f: data = f.read() node_map = {} protein_map = {} for line in data.split(';'): slist = line.split(',') if len(slist) != 3: sys.stderr.write(line + ' does not have appropriate number of columns. skipping\n') continue if slist[2].startswith('c-c'): target_is_protein = False else: target_is_protein = True if slist[0] not in node_map: source_node_id = network.create_node(slist[0]) node_map[slist[0]] = source_node_id network.add_node_attribute(property_of=source_node_id, name='suid', values=int(slist[0]), type='long') network.add_node_attribute(property_of=source_node_id, name='type', values='term', type='string') else: source_node_id = node_map[slist[0]] if target_is_protein: if slist[0] not in protein_map: protein_map[slist[0]] = set() protein_map[slist[0]].add(slist[1]) else: if slist[1] not in protein_map[slist[0]]: protein_map[slist[0]].add(slist[1]) else: target_node_id = network.create_node(slist[1]) network.create_edge(source_node_id, target_node_id, 'Child-Parent') network.add_node_attribute(property_of=target_node_id, name='suid', values=int(slist[1]), type='long') network.add_node_attribute(property_of=target_node_id, name='type', values='term', type='string') node_map[slist[1]] = target_node_id for nodename in protein_map: genelist = protein_map[nodename] if len(genelist) > 0: genesymbol_list = [] for entry in genelist: genesymbol_list.append(entry) network.add_node_attribute(property_of=node_map[nodename], name='member', values=genesymbol_list, type='list_of_string') network.add_node_attribute(property_of=node_map[nodename], name='type', values='complex', type='string', overwrite=True) del node_map del protein_map return network
# #### set_node_attribute(node, attribute_name, values, type=None, subnetwork=None) # In[ ]: NiceCX_creatures = NiceCXNetwork() node_id = NiceCX_creatures.create_node("new node") NiceCX_creatures.set_node_attribute(node_id, "new attribute", "new attribute value is xxxx") # #### *add_node_attribute(node, attribute_name, values) # In[ ]: NiceCX_creatures = NiceCXNetwork() node_id = NiceCX_creatures.create_node("new node") NiceCX_creatures.add_node_attribute(node_id, "new attribute", "new attribute value is xxxx") # #### get_node_attribute(node, attribute_name, subnetwork=None) # In[ ]: NiceCX_creatures = NiceCXNetwork() node_id = NiceCX_creatures.create_node("new node") NiceCX_creatures.set_node_attribute(node_id, "new attribute", "new attribute value is nnnn") NiceCX_creatures.get_node_attribute(node_id, "new attribute") # ##### get_node_attribute_objects(node, attribute_name) # In[ ]:
graph_id_manager = GraphIdManager() for gpml_node in dom.findall("ns3:Group", NAMESPACES): # add_cx_node_from_gpml(cx_network, gpml_node) group_id = gpml_node.get("GroupId") graph_id = gpml_node.get("GraphId") if graph_id is None: graph_id = graph_id_manager.generate_and_record() cx_node = cx_network.create_node(node_name=graph_id) entities_by_id[graph_id] = cx_node group_id_to_entity_id[group_id] = cx_node # TODO: some GPML elements have default ShapeType of Rectangle, but # the default Shape for CX is Ellipse. cx_network.add_node_attribute(property_of=cx_node, name="Shape", values="Rectangle") # text_label = gpml_node.get("TextLabel") # if text_label: # cx_network.add_node_attribute( # property_of=cx_node, name="shared name", values=text_label # ) for name, value in sorted(gpml_node.items()): kv = process_kv(name, value) cx_network.add_node_attribute(property_of=cx_node, name=kv["key"], values=kv["value"]) for gpml_node in dom.findall("ns3:DataNode", NAMESPACES): add_cx_node_from_gpml(cx_network, gpml_node)