Ejemplo n.º 1
0
    def test_update_network(self):
        client = self.get_ndex2_client()
        # create network and add it
        net = NiceCXNetwork()
        oneid = net.create_node('node1')
        twoid = net.create_node('node2')
        net.create_edge(oneid, twoid, 'hello')
        netname = 'ndex2-client integration test network' + str(datetime.now())
        net.set_name(netname)
        res = client.save_new_network(net.to_cx(), visibility='PRIVATE')
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)

            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(netid, netsum['externalId'])
            self.assertEqual(netname, netsum['name'])
            self.assertEqual('PRIVATE', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(1, netsum['edgeCount'])
            self.assertEqual(2, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])

            net.create_node(node_name='hello', node_represents='something')
            net.create_node(node_name='hoho', node_represents='heheh')
            newnetname = 'update ' + netname
            self.assertEqual(4, len(net.get_nodes()))
            net.set_name(newnetname)
            if sys.version_info.major == 3:
                stream = io.BytesIO(json.dumps(net.to_cx(),
                                               cls=DecimalEncoder)
                                    .encode('utf-8'))
            else:
                stream = io.BytesIO(json.dumps(net.to_cx(),
                                               cls=DecimalEncoder))
            newres = client.update_cx_network(stream, netid)
            self.assertEqual('', newres)
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(netid, netsum['externalId'])
            self.assertEqual(newnetname, netsum['name'])
            self.assertEqual('PRIVATE', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(1, netsum['edgeCount'])
            self.assertEqual(4, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])
        finally:
            client.delete_network(netid)
            try:
                client.get_network_as_cx_stream(netid)
                self.fail('Expected exception')
            except HTTPError:
                pass
Ejemplo n.º 2
0
    def test_upload_network_with_no_network_attributes(self):
        """
        So if one uploads a network with NO network attributes
        to NDEx and then edits the network (setting name visibility, description)
        via [updateNetworkSummary]	[/v2/network/6b89b286-e142-11ec-b397-0ac135e8bacf/summary
        the CX2 endpoint shows the changed network attributes, but the CX1
        is missing the network attributes
        :return:
        """
        client = self.get_ndex2_client()
        # create network and add it
        net = NiceCXNetwork()
        oneid = net.create_node('node1')
        twoid = net.create_node('node2')
        net.create_edge(oneid, twoid, 'hello')

        res = client.save_new_network(net.to_cx(), visibility='PRIVATE')
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)
            netsum = self.wait_for_network_to_be_ready(client, netid)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')
            self.assertEqual(netid, netsum['externalId'])
            self.assertTrue('name' not in netsum, msg=str(netsum))

            # okay now we have the network, lets update the name
            # and description and then get the network back again
            # via cx1 and cx2 endpoints
            netname = 'ndex2-client integration test network' + str(
                datetime.now())
            client.update_network_profile(netid, {'name': netname})
            netsum = self.wait_for_network_to_be_ready(client, netid)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')
            cx2_resp = client.get_network_as_cx2_stream(network_id=netid)
            cx2_json = json.loads(cx2_resp.content)
            net_attrs = None
            for aspect in cx2_json:
                print(aspect)
                if 'networkAttributes' in aspect:
                    net_attrs = aspect['networkAttributes']
                    break
            self.assertEqual([{'name': netname}], net_attrs)

            client_resp = client.get_network_as_cx_stream(network_id=netid)
            cx1_net = ndex2.create_nice_cx_from_raw_cx(
                json.loads(client_resp.content))
            self.assertEqual(netname, cx1_net.get_name(),
                             'Special test to expose '
                             'bug in NDEx server')
        finally:
            client.delete_network(netid)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
                                  values=height)

# TODO: need to handle defaults. The following doesn't appear to work.
# node_properties = [
#    {"properties_of": "nodes:default", "properties": {"NODE_SHAPE": "RECTANGLE"}}
# ]
# set_visual_properties_aspect(cx_network, node_properties)

style_network = ndex2.create_nice_cx_from_file("./WP4571.cx.json")
cx_network.apply_style_from_network(style_network)

set_cartesian_layout_aspect(cx_network, cartesianLayout)

cx_network.print_summary()

mycx = cx_network.to_cx()
# print(mycx)
pprint.pprint(mycx, depth=4)

# result = cx_network.upload_to(
#    server="http://test.ndexbio.org", username=NDEX_USER, password=NDEX_PWD
# )

result = cx_network.update_to(
    server="http://test.ndexbio.org",
    username=NDEX_USER,
    password=NDEX_PWD,
    uuid="99698d91-9155-11e9-b7e4-0660b7976219",
)

pprint.pprint(result, depth=3)
Ejemplo n.º 5
0
    def test_update_cx2_network_that_was_uploaded_as_cx1(self):
        client = self.get_ndex2_client()
        # create network and add it
        net = NiceCXNetwork()
        oneid = net.create_node('node1')
        twoid = net.create_node('node2')
        net.create_edge(oneid, twoid, 'hello')
        netname = 'ndex2-client integration test network' + str(datetime.now())
        net.set_name(netname)
        res = client.save_new_network(net.to_cx(), visibility='PRIVATE')
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)
            netsum = self.wait_for_network_to_be_ready(client, netid)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')
            self.assertEqual(netid, netsum['externalId'])
            self.assertTrue('name' in netsum, msg=str(netsum))
            self.assertEqual(netname, netsum['name'], str(netsum))
            self.assertEqual('PRIVATE', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(1, netsum['edgeCount'])
            self.assertEqual(2, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])

            # load network
            with open(TestClientIntegration.CX2_GLYPICAN2_FILE, 'r') as f:
                cx = json.load(f)

            # set the name
            for frag in cx[1:-1]:
                cur_aspect = list(frag.keys())[0]
                if cur_aspect == 'networkAttributes':
                    frag[cur_aspect][0]['name'] = 'ndex2-client integration ' \
                                                  'test network2 ' + \
                                                  str(datetime.now())
                    break

            if sys.version_info.major == 3:
                stream = io.BytesIO(
                    json.dumps(cx, cls=DecimalEncoder).encode('utf-8'))
            else:
                stream = io.BytesIO(json.dumps(cx, cls=DecimalEncoder))
            client.update_cx2_network(stream, netid)

            netsum = self.wait_for_network_to_be_ready(client, netid)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')

            res = client.get_network_as_cx2_stream(netid)
            self.assertEqual(200, res.status_code)
            self.check_glypican_cx2_is_correct(res.json())
        finally:
            client.delete_network(netid)
            try:
                client.get_network_as_cx_stream(netid)
                self.fail('Expected exception')
            except HTTPError:
                pass