コード例 #1
0
def create_nice_cx_from_server(server=None,
                               username=None,
                               password=None,
                               uuid=None):
    """
    Create a NiceCXNetwork based on a network retrieved from NDEx, specified by its UUID.
    If the network is not public, then username and password arguments for an account on
    the server with permission to access the network must be supplied.
    :param server: the URL of the NDEx server hosting the network.
    :param username: the user name of an account with permission to access the network.
    :param password: the password of an account with permission to access the network.
    :param uuid: the UUID of the network.
    :return: NiceCXNetwork
    """
    if server and uuid:
        my_nicecx = NiceCXNetwork()

        # ===================
        # METADATA
        # ===================
        available_aspects = []
        md_aspect_iter = my_nicecx.get_aspect(uuid, 'metaData', server,
                                              username, password)
        if md_aspect_iter:
            for ae in (o for o in md_aspect_iter):
                available_aspects.append(ae.get(CX_CONSTANTS.METADATA_NAME))
                mde = MetaDataElement(cx_fragment=ae)
                my_nicecx.add_metadata(mde)
        else:
            if not username or not password:
                raise Exception(
                    'Network is not available.  Username and/or password not supplied'
                )
            else:
                raise Exception('Network not available')

        opaque_aspects = set(available_aspects).difference(known_aspects_min)

        # ====================
        # NETWORK ATTRIBUTES
        # ====================
        if 'networkAttributes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'networkAttributes', server,
                                           username, password)
            for network_item in objects:
                my_nicecx.add_network_attribute(json_obj=network_item)
            my_nicecx.add_metadata_stub('networkAttributes')

        # ===================
        # @CONTEXT
        # ===================
        if '@context' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, '@context', server, username,
                                           password)
            my_nicecx.set_context(objects)
            if (my_nicecx.metadata.get('@context') is None):
                my_nicecx.add_metadata_stub('@context')
            else:
                my_nicecx.metadata.get('@context').set_element_count(1)

        # ===================
        # NODES
        # ===================
        if 'nodes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'nodes', server, username,
                                           password)
            for node_item in objects:
                my_nicecx.create_node(cx_fragment=node_item)
            my_nicecx.add_metadata_stub('nodes')

        # ===================
        # EDGES
        # ===================
        if 'edges' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edges', server, username,
                                           password)
            for edge_item in objects:
                my_nicecx.create_edge(cx_fragment=edge_item)
            my_nicecx.add_metadata_stub('edges')

        # ===================
        # NODE ATTRIBUTES
        # ===================
        if 'nodeAttributes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'nodeAttributes', server,
                                           username, password)
            for att in objects:
                # my_nicecx.set_node_attribute(att.get('po'), att.get('n'), att.get('v'), type=att.get('d'))

                node_attribute_element = NodeAttributesElement(cx_fragment=att)
                my_nicecx.nodeAttributeHeader.add(
                    node_attribute_element.get_name())
                nodeAttrs = my_nicecx.nodeAttributes.get(
                    node_attribute_element.get_property_of())
                if nodeAttrs is None:
                    nodeAttrs = []
                    my_nicecx.nodeAttributes[
                        node_attribute_element.get_property_of()] = nodeAttrs

                nodeAttrs.append(node_attribute_element)

            my_nicecx.add_metadata_stub('nodeAttributes')

        # ===================
        # EDGE ATTRIBUTES
        # ===================
        if 'edgeAttributes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edgeAttributes', server,
                                           username, password)
            for att in objects:
                edge_attribute_element = EdgeAttributesElement(cx_fragment=att)

                my_nicecx.edgeAttributeHeader.add(
                    edge_attribute_element.get_name())
                edge_attrs = my_nicecx.edgeAttributes.get(att.get('po'))
                if edge_attrs is None:
                    edge_attrs = []
                    my_nicecx.edgeAttributes[
                        edge_attribute_element.get_property_of()] = edge_attrs

                edge_attrs.append(edge_attribute_element)

            my_nicecx.add_metadata_stub('edgeAttributes')

        # ===================
        # CITATIONS
        # ===================
        if 'citations' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'citations', server, username,
                                           password)
            for cit in objects:
                aspect_element = AspectElement(cit, 'citations')
                my_nicecx.add_opaque_aspect_element(aspect_element)

            my_nicecx.add_metadata_stub('citations')

        # ===================
        # SUPPORTS
        # ===================
        if 'supports' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'supports', server, username,
                                           password)
            for sup in objects:
                aspect_element = AspectElement(sup, 'supports')
                my_nicecx.add_opaque_aspect_element(aspect_element)

            my_nicecx.add_metadata_stub('supports')

        # ===================
        # EDGE SUPPORTS
        # ===================
        if 'edgeSupports' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edgeSupports', server,
                                           username, password)
            for add_this_edge_sup in objects:
                aspect_element = AspectElement(add_this_edge_sup,
                                               'edgeSupports')
                my_nicecx.add_opaque_aspect_element(aspect_element)

            my_nicecx.add_metadata_stub('edgeSupports')

        # ===================
        # NODE CITATIONS
        # ===================
        if 'nodeCitations' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'nodeCitations', server,
                                           username, password)
            for node_cit in objects:
                aspect_element = AspectElement(node_cit, 'nodeCitations')
                my_nicecx.add_opaque_aspect_element(aspect_element)

            my_nicecx.add_metadata_stub('nodeCitations')

        # ===================
        # EDGE CITATIONS
        # ===================
        if 'edgeCitations' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edgeCitations', server,
                                           username, password)
            for edge_cit in objects:
                aspect_element = AspectElement(edge_cit, 'edgeCitations')
                my_nicecx.add_opaque_aspect_element(aspect_element)

            my_nicecx.add_metadata_stub('edgeCitations')

        # ===================
        # OPAQUE ASPECTS
        # ===================
        for oa in opaque_aspects:
            objects = my_nicecx.get_aspect(uuid, oa, server, username,
                                           password)
            for oa_item in objects:
                aspect_element = AspectElement(oa_item, oa)
                my_nicecx.add_opaque_aspect_element(aspect_element)
                my_nicecx.add_metadata_stub(oa)
    else:
        raise Exception('Server and uuid not specified')

    return my_nicecx
コード例 #2
0
ファイル: __init__.py プロジェクト: bgyori/ndex2-client
def create_nice_cx_from_server(server=None,
                               username=None,
                               password=None,
                               uuid=None):
    if server and uuid:
        my_nicecx = NiceCXNetwork()

        #===================
        # METADATA
        #===================
        available_aspects = []
        md_aspect_iter = my_nicecx.get_aspect(uuid, 'metaData', server,
                                              username, password)
        if md_aspect_iter:
            for ae in (o for o in md_aspect_iter):
                available_aspects.append(ae.get(CX_CONSTANTS.METADATA_NAME))
                mde = MetaDataElement(cx_fragment=ae)
                my_nicecx.add_metadata(mde)
        else:
            if not username or not password:
                raise Exception(
                    'Network is not available.  Username and/or password not supplied'
                )
            else:
                raise Exception('Network not available')

        opaque_aspects = set(available_aspects).difference(known_aspects_min)

        #====================
        # NETWORK ATTRIBUTES
        #====================
        if 'networkAttributes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'networkAttributes', server,
                                           username, password)
            for network_item in objects:
                my_nicecx.add_network_attribute(json_obj=network_item)
            my_nicecx.add_metadata_stub('networkAttributes')

        #===================
        # NODES
        #===================
        if 'nodes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'nodes', server, username,
                                           password)
            for node_item in objects:
                my_nicecx.create_node(cx_fragment=node_item)
            my_nicecx.add_metadata_stub('nodes')

        #===================
        # EDGES
        #===================
        if 'edges' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edges', server, username,
                                           password)
            for edge_item in objects:
                my_nicecx.create_edge(cx_fragment=edge_item)
            my_nicecx.add_metadata_stub('edges')

        #===================
        # NODE ATTRIBUTES
        #===================
        if 'nodeAttributes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'nodeAttributes', server,
                                           username, password)
            for att in objects:
                #my_nicecx.set_node_attribute(att.get('po'), att.get('n'), att.get('v'), type=att.get('d'))

                node_attribute_element = NodeAttributesElement(cx_fragment=att)
                my_nicecx.nodeAttributeHeader.add(
                    node_attribute_element.get_name())
                nodeAttrs = my_nicecx.nodeAttributes.get(
                    node_attribute_element.get_property_of())
                if nodeAttrs is None:
                    nodeAttrs = []
                    my_nicecx.nodeAttributes[
                        node_attribute_element.get_property_of()] = nodeAttrs

                nodeAttrs.append(node_attribute_element)

            my_nicecx.add_metadata_stub('nodeAttributes')

        #===================
        # EDGE ATTRIBUTES
        #===================
        if 'edgeAttributes' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edgeAttributes', server,
                                           username, password)
            for att in objects:
                edge_attribute_element = EdgeAttributesElement(cx_fragment=att)

                my_nicecx.edgeAttributeHeader.add(
                    edge_attribute_element.get_name())
                edge_attrs = my_nicecx.edgeAttributes.get(att.get('po'))
                if edge_attrs is None:
                    edge_attrs = []
                    my_nicecx.edgeAttributes[
                        edge_attribute_element.get_property_of()] = edge_attrs

                edge_attrs.append(edge_attribute_element)

            my_nicecx.add_metadata_stub('edgeAttributes')

        #===================
        # CITATIONS
        #===================
        if 'citations' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'citations', server, username,
                                           password)
            for cit in objects:
                aspect_element = AspectElement(cit, 'citations')
                my_nicecx.add_opaque_aspect(aspect_element)

            my_nicecx.add_metadata_stub('citations')

        #===================
        # SUPPORTS
        #===================
        if 'supports' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'supports', server, username,
                                           password)
            for sup in objects:
                aspect_element = AspectElement(sup, 'supports')
                my_nicecx.add_opaque_aspect(aspect_element)

            my_nicecx.add_metadata_stub('supports')

        #===================
        # EDGE SUPPORTS
        #===================
        if 'edgeSupports' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edgeSupports', server,
                                           username, password)
            for add_this_edge_sup in objects:
                aspect_element = AspectElement(add_this_edge_sup,
                                               'edgeSupports')
                my_nicecx.add_opaque_aspect(aspect_element)

            my_nicecx.add_metadata_stub('edgeSupports')

        #===================
        # NODE CITATIONS
        #===================
        if 'nodeCitations' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'nodeCitations', server,
                                           username, password)
            for node_cit in objects:
                aspect_element = AspectElement(node_cit, 'nodeCitations')
                my_nicecx.add_opaque_aspect(aspect_element)

            my_nicecx.add_metadata_stub('nodeCitations')

        #===================
        # EDGE CITATIONS
        #===================
        if 'edgeCitations' in available_aspects:
            objects = my_nicecx.get_aspect(uuid, 'edgeCitations', server,
                                           username, password)
            for edge_cit in objects:
                aspect_element = AspectElement(edge_cit, 'edgeCitations')
                my_nicecx.add_opaque_aspect(aspect_element)

            my_nicecx.add_metadata_stub('edgeCitations')

        #===================
        # OPAQUE ASPECTS
        #===================
        for oa in opaque_aspects:
            objects = my_nicecx.get_aspect(uuid, oa, server, username,
                                           password)
            for oa_item in objects:
                aspect_element = AspectElement(oa_item, oa)
                my_nicecx.add_opaque_aspect(aspect_element)
                my_nicecx.add_metadata_stub(oa)
    else:
        raise Exception('Server and uuid not specified')

    return my_nicecx