Exemplo n.º 1
0
    def create_from_cx(self, cx):
        niceCx = NiceCXNetwork()
        for aspect in cx:
            if 'status' in aspect:
                if aspect['status'][0]['success']:
                    continue
                else:
                    raise RuntimeError("Error in CX status aspect: " +
                                       aspect['status'][0]['error'])
            if "numberVerification" in aspect:
                # new status and numberVerification will be added when the network is output to_cx
                continue
            if 'subNetworks' in aspect:
                for subnetwork in aspect.get('subNetworks'):
                    id = subnetwork.get('@id')
                    if self.subnetwork_id != None:
                        raise ValueError(
                            "niceCX does not support collections!")
                    self.subnetwork_id = id
            elif 'cyViews' in aspect:
                for cyViews in aspect.get('cyViews'):
                    id = cyViews.get('@id')
                    if self.view_id != None:
                        raise ValueError(
                            "niceCX does not support more than one view!")
                    self.view_id = id
            elif 'metaData' in aspect:
                available_aspects = []
                for ae in (o for o in aspect.get('metaData')):
                    available_aspects.append(ae.get(
                        CX_CONSTANTS.METADATA_NAME))
                    mde = MetaDataElement(json_obj=ae)
                    niceCx.add_metadata(mde)
                opaque_aspects = set(available_aspects).difference(
                    known_aspects)

                continue
            elif 'provenanceHistory' in aspect:
                elements = aspect['provenanceHistory']
                if len(elements) > 0:
                    if len(elements) > 1 or self.provenance:
                        raise RuntimeError(
                            'profenanceHistory aspect can only have one element.'
                        )
                    else:
                        self.provenance = elements[0]
            elif '@context' in aspect:
                elements = aspect['@context']
                if len(elements) > 0:
                    if len(elements) > 1 or self.namespaces:
                        raise RuntimeError(
                            '@context aspect can only have one element')
                    else:
                        self.namespaces = elements[0]
            # TODO elif if it's an aspect we want to keep out we put an elif for that aspect
            else:
                self.unclassified_cx.append(aspect)

            cx = self.unclassified_cx
Exemplo n.º 2
0
def create_nice_cx_from_filename(filename):
    if os.path.isfile(filename):
        my_nicecx = NiceCXNetwork()
        with open(filename, 'rU') as file_cx:
            #====================================
            # BUILD NICECX FROM FILE
            #====================================
            my_nicecx.create_from_cx(json.load(file_cx))
            return my_nicecx
    else:
        raise Exception('The file provided does not exist.')
Exemplo n.º 3
0
def create_nice_cx_from_file(path):
    """
    Create a NiceCXNetwork based on CX JSON from a file.
    :param path: the path from which the CX will be loaded
    :return: NiceCXNetwork
    """
    if os.path.isfile(path):
        my_nicecx = NiceCXNetwork()
        with open(path, 'rU') as file_cx:
            # ====================================
            # BUILD NICECX FROM FILE
            # ====================================
            my_nicecx.create_from_cx(json.load(file_cx))
            return my_nicecx
    else:
        raise Exception('The file " + path + " does not exist.')
    def test_create_from_pandas_with_headers2(self):
        path_to_network = os.path.join(path_this, 'CTD_genes_pathways.txt')

        with open(path_to_network, 'rU') as tsvfile:
            header = [h.strip() for h in tsvfile.readline().split('\t')]

            df = pd.read_csv(tsvfile,
                             delimiter='\t',
                             engine='python',
                             names=header)

            niceCx = NiceCXNetwork()
            niceCx = ndex2.create_nice_cx_from_pandas(
                df,
                source_field='GeneSymbol',
                target_field='PathwayName',
                source_node_attr=['GeneID'],
                target_node_attr=['Pathway Source'],
                edge_attr=[])

            #for k, v in niceCx.get_edges():
            #    print(k)
            #    print(v)
            #niceCx.create_from_pandas(df, source_field='GeneSymbol', target_field='PathwayName', source_node_attr=['GeneID'], target_node_attr=['Pathway Source'], edge_attr=[])
            #my_cx_json = niceCx.to_cx()
            #print(json.dumps(my_cx_json))
            upload_message = True  #niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
Exemplo n.º 5
0
    def create_from_server(self, server, username, password, uuid):
        if server and uuid:
            niceCx = NiceCXNetwork()

            #===================
            # METADATA
            #===================
            available_aspects = []
            for ae in (o for o in self.stream_aspect(uuid, 'metaData')):
                available_aspects.append(ae.get(CX_CONSTANTS.METADATA_NAME))
                mde = MetaDataElement(json_obj=ae)
                niceCx.add_metadata(mde)

            #available_aspects = ['edges', 'nodes'] # TODO - remove this
            opaque_aspects = set(available_aspects).difference(known_aspects_min)

            print(opaque_aspects)

            #====================
            # NETWORK ATTRIBUTES
            #====================
            objects = self.stream_aspect(uuid, 'networkAttributes')
            obj_items = (o for o in objects)
            for network_item in obj_items:
                add_this_network_attribute = NetworkAttributesElement(cx_fragment=network_item)

                niceCx.add_network_attribute(add_this_network_attribute)

            #===================
            # NODES
            #===================
            objects = self.stream_aspect(uuid, 'nodes')
            obj_items = (o for o in objects)
            for node_item in obj_items:
                add_this_node = NodeElement(cx_fragment=node_item)

                niceCx.create_node(add_this_node)

            #===================
            # EDGES
            #===================
            objects = self.stream_aspect(uuid, 'edges')
            obj_items = (o for o in objects)
            for edge_item in obj_items:
                add_this_edge = EdgeElement(cx_fragment=edge_item)

                niceCx.create_edge(add_this_edge)

            #===================
            # NODE ATTRIBUTES
            #===================
            objects = self.stream_aspect(uuid, 'nodeAttributes')
            obj_items = (o for o in objects)
            for att in obj_items:
                add_this_node_att = NodeAttributesElement(json_obj=att)

                niceCx.add_node_attribute(add_this_node_att)

            #===================
            # EDGE ATTRIBUTES
            #===================
            objects = self.stream_aspect(uuid, 'edgeAttributes')
            obj_items = (o for o in objects)
            for att in obj_items:
                add_this_edge_att = EdgeAttributesElement(json_obj=att)

                niceCx.add_edge_attribute(add_this_edge_att)

            #===================
            # CITATIONS
            #===================
            objects = self.stream_aspect(uuid, 'citations')
            obj_items = (o for o in objects)
            for cit in obj_items:
                add_this_citation = CitationElement(cx_fragment=cit)

                niceCx.add_citation(add_this_citation)

            #===================
            # SUPPORTS
            #===================
            objects = self.stream_aspect(uuid, 'supports')
            obj_items = (o for o in objects)
            for sup in obj_items:
                add_this_supports = SupportElement(cx_fragment=sup)

                niceCx.add_support(add_this_supports)

            #===================
            # NODE CITATIONS
            #===================
            objects = self.stream_aspect(uuid, 'nodeCitations')
            obj_items = (o for o in objects)
            for node_cit in obj_items:
                niceCx.add_node_citations_from_cx(node_cit)

            #===================
            # EDGE CITATIONS
            #===================
            objects = self.stream_aspect(uuid, 'edgeCitations')
            obj_items = (o for o in objects)
            for edge_cit in obj_items:
                niceCx.add_edge_citations_from_cx(edge_cit)

            #===================
            # OPAQUE ASPECTS
            #===================
            for oa in opaque_aspects:
                objects = self.stream_aspect(uuid, oa)
                obj_items = (o for o in objects)
                for oa_item in obj_items:
                    aspect_element = AspectElement(oa_item, oa)
                    niceCx.add_opaque_aspect_element(aspect_element)

            return niceCx
        else:
            raise Exception('Server and uuid not specified')
Exemplo n.º 6
0
__author__ = 'aarongary'

from nicecxModel.NiceCXNetwork import NiceCXNetwork

niceCx = NiceCXNetwork()

#================================
# Load network from ndex server
#================================
niceCx.create_from_server('public.ndexbio.org', None, None, 'dfba0dfb-6192-11e5-8ac5-06603eb7f303')

#=============================
# convert to pandas dataframe
#=============================
my_pd = niceCx.to_pandas_dataframe()

#=====================
# Export to csv file
#=====================
my_pd.to_csv('CXExport.csv', sep=',')

print('Done')

Exemplo n.º 7
0
def create_nice_cx_from_networkx(G):
    """
    Create a NiceCXNetwork based on a networkx graph. The resulting NiceCXNetwork
    contains the nodes edges and their attributes from the networkx graph and also
    preserves the graph 'pos' attribute as a CX cartesian coordinates aspect.
    :param G: networkx graph
    :type G: networkx graph
    :return: NiceCXNetwork
    :rtype: NiceCXNetwork
    """
    if G is None:
        raise Exception('Networkx input is empty')

    my_nicecx = NiceCXNetwork()

    if G.graph.get('name'):
        my_nicecx.set_name(G.graph.get('name'))
    else:
        my_nicecx.set_name('created from networkx')

    my_nicecx.add_metadata_stub('networkAttributes')
    for n, d in G.nodes_iter(data=True):
        # =============
        # ADD NODES
        # =============
        if d and d.get('name'):
            my_nicecx.create_node(id=n,
                                  node_name=d.get('name'),
                                  node_represents=d.get('name'))
        else:
            my_nicecx.create_node(id=n, node_name=n, node_represents=n)

        # ======================
        # ADD NODE ATTRIBUTES
        # ======================
        for k, v in d.items():
            attr_type = None
            if isinstance(v, float):
                attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
            elif isinstance(v, int):
                attr_type = ATTRIBUTE_DATA_TYPE.INTEGER

            my_nicecx.set_node_attribute(n, k, v, type=attr_type)

    index = 0
    for u, v, d in G.edges_iter(data=True):
        # =============
        # ADD EDGES
        # =============
        my_nicecx.create_edge(id=index,
                              edge_source=u,
                              edge_target=v,
                              edge_interaction=d.get('interaction'))

        # ==============================
        # ADD EDGE ATTRIBUTES
        # ==============================
        for k, val in d.items():
            if k != 'interaction':
                attr_type = None
                if isinstance(val, float):
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif isinstance(val, int):
                    attr_type = ATTRIBUTE_DATA_TYPE.INTEGER
                my_nicecx.set_edge_attribute(index, k, val, type=attr_type)

        index += 1

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if my_nicecx.nodeAttributes:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if my_nicecx.edgeAttributes:
        my_nicecx.add_metadata_stub('edgeAttributes')

    if hasattr(G, 'pos'):
        aspect = _create_cartesian_coordinates_aspect_from_networkx(G)
        my_nicecx.add_opaque_aspect('cartesianLayout', aspect)
        my_nicecx.add_metadata_stub('cartesianLayout')

    return my_nicecx
Exemplo n.º 8
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
Exemplo n.º 9
0
def create_empty_nice_cx():
    my_nicecx = NiceCXNetwork()
    return my_nicecx
Exemplo n.º 10
0
def create_nice_cx_from_pandas(df,
                               source_field=None,
                               target_field=None,
                               source_node_attr=[],
                               target_node_attr=[],
                               edge_attr=[],
                               edge_interaction=None):
    """
    Constructor that uses a pandas dataframe to build niceCX
    :param df: dataframe
    :type df: Pandas Dataframe
    :param headers:
    :type headers:
    :return: none
    :rtype: n/a
    """

    my_nicecx = NiceCXNetwork()

    #====================================================
    # IF NODE FIELD NAME (SOURCE AND TARGET) IS PROVIDED
    # THEN USE THOSE FIELDS OTHERWISE USE INDEX 0 & 1
    #====================================================
    my_nicecx.set_name('Pandas Upload')
    my_nicecx.add_metadata_stub('networkAttributes')
    count = 0
    if source_field and target_field:
        for index, row in df.iterrows():
            if count % 10000 == 0:
                print(count)
            count += 1
            #=============
            # ADD NODES
            #=============
            my_nicecx.create_node(id=row[source_field],
                                  node_name=row[source_field],
                                  node_represents=row[source_field])
            my_nicecx.create_node(id=row[target_field],
                                  node_name=row[target_field],
                                  node_represents=row[target_field])

            #=============
            # ADD EDGES
            #=============
            if edge_interaction:
                if row.get(edge_interaction):
                    my_nicecx.create_edge(
                        id=index,
                        edge_source=row[source_field],
                        edge_target=row[target_field],
                        edge_interaction=row[edge_interaction])
                else:
                    my_nicecx.create_edge(id=index,
                                          edge_source=row[source_field],
                                          edge_target=row[target_field],
                                          edge_interaction=edge_interaction)
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[source_field],
                                      edge_target=row[target_field],
                                      edge_interaction='neighbor-of')

            #==============================
            # ADD SOURCE NODE ATTRIBUTES
            #==============================
            for sp in source_node_attr:
                attr_type = None
                if type(row[sp]) is float and math.isnan(row[sp]):
                    row[sp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[sp]) is float and math.isinf(row[sp]):
                    row[sp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                my_nicecx.set_node_attribute(row[source_field],
                                             sp,
                                             row[sp],
                                             type=attr_type)

            #==============================
            # ADD TARGET NODE ATTRIBUTES
            #==============================
            for tp in target_node_attr:
                attr_type = None
                if type(row[tp]) is float and math.isnan(row[tp]):
                    row[tp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[tp]) is float and math.isinf(row[tp]):
                    row[tp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                my_nicecx.set_node_attribute(row[target_field],
                                             tp,
                                             row[tp],
                                             type=attr_type)

            #==============================
            # ADD EDGE ATTRIBUTES
            #==============================
            for ep in edge_attr:
                attr_type = None
                if type(row[ep]) is float and math.isnan(row[ep]):
                    row[ep] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[ep]) is float and math.isinf(row[ep]):
                    row[ep] = 'INFINITY'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT

                my_nicecx.set_edge_attribute(index,
                                             ep,
                                             row[ep],
                                             type=attr_type)

    else:
        for index, row in df.iterrows():
            #=============
            # ADD NODES
            #=============
            my_nicecx.create_node(id=row[0],
                                  node_name=row[0],
                                  node_represents=row[0])
            my_nicecx.create_node(id=row[1],
                                  node_name=row[1],
                                  node_represents=row[1])

            #=============
            # ADD EDGES
            #=============
            if len(row) > 2:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction=row[2])
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction='interacts-with')

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if source_node_attr or target_node_attr:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if edge_attr:
        my_nicecx.add_metadata_stub('edgeAttributes')

    return my_nicecx
Exemplo n.º 11
0
def create_nice_cx_from_cx(cx):
    """
    Create a NiceCXNetwork from a CX list.
    :param cx: a list in CX format
    :return: NiceCXNetwork
    """
    my_nicecx = NiceCXNetwork()

    if cx:
        # ===================
        # METADATA
        # ===================
        available_aspects = []
        for ae in (
                o
                for o in my_nicecx.get_frag_from_list_by_key(cx, 'metaData')):
            available_aspects.append(ae.get(CX_CONSTANTS.METADATA_NAME))
            mde = MetaDataElement(cx_fragment=ae)
            my_nicecx.add_metadata(mde)

        opaque_aspects = set(available_aspects).difference(known_aspects_min)

        # ====================
        # NETWORK ATTRIBUTES
        # ====================
        if 'networkAttributes' in available_aspects:
            objects = my_nicecx.get_frag_from_list_by_key(
                cx, 'networkAttributes')
            for network_item in objects:
                add_this_network_attribute = NetworkAttributesElement(
                    cx_fragment=network_item)

                my_nicecx.add_network_attribute(
                    network_attribute_element=add_this_network_attribute)
            my_nicecx.add_metadata_stub('networkAttributes')

        # ===================
        # NODES
        # ===================
        if 'nodes' in available_aspects:
            objects = my_nicecx.get_frag_from_list_by_key(cx, 'nodes')
            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_frag_from_list_by_key(cx, 'edges')
            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_frag_from_list_by_key(cx, 'nodeAttributes')
            for att in objects:
                # my_nicecx.set_node_attribute(None, None, None, cx_fragment=att)
                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_frag_from_list_by_key(cx, 'edgeAttributes')
            for att in objects:
                #my_nicecx.set_edge_attribute(None, None, None, cx_fragment=att)
                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_frag_from_list_by_key(cx, 'citations')
            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_frag_from_list_by_key(cx, 'supports')
            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_frag_from_list_by_key(cx, 'edgeSupports')
            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_frag_from_list_by_key(cx, 'nodeCitations')
            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_frag_from_list_by_key(cx, 'edgeCitations')
            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_frag_from_list_by_key(cx, oa)
            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)

        return my_nicecx
    else:
        raise Exception('CX is empty')
Exemplo n.º 12
0
    def test_nice_cx_model(self):

        niceCx = NiceCXNetwork()
        #main_map = NdexGraph(server='http://dev2.ndexbio.org', username='******', password='******', uuid='7246d8cf-c644-11e6-b48c-0660b7976219')

        uuid = '6b968fd2-02a4-11e6-b550-06603eb7f303'

        #====================
        # NETWORK QUERY
        #====================

        networkQuery = NetworkQuery()
        networkQuery.query_network('40f1def0-3aa4-11e7-b12f-0660b7976219',
                                   'HSPA5,HSPA4')

        my_na = NodeAttributesElement(
            subnetwork=1,
            property_of=11,
            name=22,
            values=33,
            type=ATTRIBUTE_DATA_TYPE.convert_to_data_type('string'))

        #====================
        # NETWORK ATTRIBUTES
        #====================
        #objects = ijson.items(urlopen('http://dev2.ndexbio.org/v2/network/' + uuid + '/aspect/nodes'), 'item')
        objects = loadAspect('networkAttributes')
        obj_items = (o for o in objects)
        for network_item in obj_items:
            add_this_network_attribute = NetworkAttributesElement(
                cx_fragment=network_item)

            niceCx.add_network_attribute(add_this_network_attribute)

        #===================
        # NODES
        #===================
        #objects = ijson.items(urlopen('http://dev2.ndexbio.org/v2/network/' + uuid + '/aspect/nodes'), 'item')
        objects = loadAspect('nodes')
        obj_items = (o for o in objects)
        for node_item in obj_items:
            add_this_node = NodeElement(json_obj=node_item)

            niceCx.create_node(add_this_node)

        #===================
        # EDGES
        #===================
        #objects = ijson.items(urlopen('http://dev2.ndexbio.org/v2/network/' + uuid + '/aspect/edges'), 'item')

        objects = loadAspect('edges')
        obj_items = (o for o in objects)
        for edge_item in obj_items:
            add_this_edge = EdgeElement(json_obj=edge_item)

            niceCx.create_edge(add_this_edge)

        #===================
        # NODE ATTRIBUTES
        #===================
        #objects = ijson.items(urlopen('http://dev2.ndexbio.org/v2/network/' + uuid + '/aspect/nodeAttributes'), 'item')
        objects = loadAspect('nodeAttributes')
        obj_items = (o for o in objects)
        for att in obj_items:
            add_this_node_att = NodeAttributesElement(json_obj=att)

            niceCx.add_node_attribute(add_this_node_att)

        #===================
        # EDGE ATTRIBUTES
        #===================
        #objects = ijson.items(urlopen('http://dev2.ndexbio.org/v2/network/' + uuid + '/aspect/edgeAttributes'), 'item')
        objects = loadAspect('edgeAttributes')
        obj_items = (o for o in objects)
        for att in obj_items:
            add_this_edge_att = EdgeAttributesElement(json_obj=att)

            niceCx.add_edge_attribute(add_this_edge_att)

        #===================
        # CITATIONS
        #===================
        #objects = ijson.items(urlopen('http://dev2.ndexbio.org/v2/network/' + uuid + '/aspect/edgeAttributes'), 'item')
        objects = loadAspect('citations')
        obj_items = (o for o in objects)
        for cit in obj_items:
            add_this_citation = CitationElement(cx_fragment=cit)

            niceCx.add_citation(add_this_citation)

        #===================
        # SUPPORTS
        #===================
        objects = loadAspect('supports')
        obj_items = (o for o in objects)
        for sup in obj_items:
            add_this_supports = SupportElement(cx_fragment=sup)

            niceCx.add_support(add_this_supports)

        #===================
        # NODE CITATIONS
        #===================
        objects = loadAspect('nodeCitations')
        obj_items = (o for o in objects)
        for node_cit in obj_items:
            niceCx.add_node_citations_from_cx(node_cit)

        #===================
        # EDGE CITATIONS
        #===================
        objects = loadAspect('edgeCitations')
        obj_items = (o for o in objects)
        for edge_cit in obj_items:
            niceCx.add_edge_citations_from_cx(edge_cit)

        nice_cx_json = niceCx.to_cx()

        #        serialized = pickle.dumps(niceCx, protocol=0)
        #        print 'Serialized memory:', sys.getsizeof(serialized)

        print('starting to_pandas_dataframe')
        niceCx.to_pandas_dataframe()
        parser = ijson.parse(
            urlopen('http://dev2.ndexbio.org/v2/network/' + uuid +
                    '/aspect/nodes'))

        node_id, node_n, node_r = '', '', ''
        edge_id, edge_s, edge_t, edge_i = '', '', '', ''
        node_matches = {}
        edge_matches = {}
        edge_connected = {}
        node_found = False
        edge_found = False
        count = 0
        done_searching = False

        start_time = time.time()

        for prefix, event, value in parser:
            if (prefix) == ('item.@id'):
                if count % 10000 == 0:
                    print(count)
                count += 1
                node_id = value
            elif (prefix) == ('item.n'):
                node_n = value
                node_found = True
            elif (prefix) == ('item.r'):
                node_r = value
                if node_found:
                    node_matches[node_id] = {'n': node_n, 'r': node_r}
                    add_this_node = NodeElement(id=node_id,
                                                node_name=node_n,
                                                node_represents=node_r)
                    niceCx.create_node(add_this_node)
                    node_found = False
            else:
                # No represents found
                if node_found:
                    node_matches[node_id] = {'n': node_n}
                    add_this_node = NodeElement(id=node_id, node_name=node_n)
                    niceCx.create_node(add_this_node)
                    node_found = False

        print('Response time (Node search): ' + str(time.time() - start_time))
        start_time = time.time()

        print(edge_matches)
        print(node_matches)

        parser = ijson.parse(
            urlopen('http://dev2.ndexbio.org/v2/network/' + uuid +
                    '/aspect/edges'))

        for prefix, event, value in parser:
            if (prefix) == ('item.@id'):
                edge_id = value
            elif (prefix) == ('item.s'):
                edge_s = value
                edge_found = True
            elif (prefix) == ('item.t'):
                edge_t = value
                edge_found = True
            elif (prefix) == ('item.i'):
                edge_i = value
                if edge_found:
                    edge_matches[edge_id] = {
                        's': edge_s,
                        't': edge_t,
                        'i': edge_i
                    }
                    add_this_edge = EdgeElement(id=edge_id,
                                                edge_source=edge_s,
                                                edge_target=edge_t,
                                                edge_interaction=edge_i)
                    niceCx.create_edge(add_this_edge)
                    edge_connected[edge_s] = 1
                    edge_connected[edge_t] = 1
                    edge_found = False
            else:
                # No interaction found
                if edge_found:
                    edge_matches[edge_id] = {'s': edge_s, 't': edge_t}
                    add_this_edge = EdgeElement(id=edge_id,
                                                edge_source=edge_s,
                                                edge_target=edge_t)
                    niceCx.create_edge(add_this_edge)
                    edge_connected[edge_s] = 1
                    edge_connected[edge_t] = 1
                    edge_found = False

        print('Response time (Edge search): ' + str(time.time() - start_time))
        start_time = time.time()

        print(edge_matches)
        print(node_matches)

        self.assertTrue(niceCx is not None)
Exemplo n.º 13
0
def create_nice_cx_from_networkx(G):
    """
    Constructor that uses a networkx graph to build niceCX
    :param G: networkx graph
    :type G: networkx graph
    :return: none
    :rtype: none
    """
    my_nicecx = NiceCXNetwork()

    if G.graph.get('name'):
        my_nicecx.set_name(G.graph.get('name'))
    else:
        my_nicecx.set_name('created from networkx')

    my_nicecx.add_metadata_stub('networkAttributes')
    for n, d in G.nodes_iter(data=True):
        #=============
        # ADD NODES
        #=============
        if d and d.get('name'):
            my_nicecx.create_node(id=n,
                                  node_name=d.get('name'),
                                  node_represents=d.get('name'))
        else:
            my_nicecx.create_node(id=n, node_name=n, node_represents=n)

        #======================
        # ADD NODE ATTRIBUTES
        #======================
        for k, v in d.items():
            my_nicecx.set_node_attribute(n, k, v)

    index = 0
    for u, v, d in G.edges_iter(data=True):
        #=============
        # ADD EDGES
        #=============
        my_nicecx.create_edge(id=index,
                              edge_source=u,
                              edge_target=v,
                              edge_interaction=d.get('interaction'))

        #==============================
        # ADD EDGE ATTRIBUTES
        #==============================
        for k, v in d.items():
            if k != 'interaction':
                my_nicecx.set_edge_attribute(index, k, v)

        index += 1

    #Cartesian aspect
    cartesian_aspect = []
    #for

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if my_nicecx.nodeAttributes:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if my_nicecx.edgeAttributes:
        my_nicecx.add_metadata_stub('edgeAttributes')

    if hasattr(G, 'pos'):
        G_pos = create_cartesian_coordinates_aspect_from_networkx(G)

        my_nicecx.set_opaque_aspect('cartesianLayout',
                                    G_pos.get('cartesianLayout'))
        my_nicecx.add_metadata_stub('cartesianLayout')

    return my_nicecx
Exemplo n.º 14
0
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
Exemplo n.º 15
0
    def query_network(self, uuid, search_string, max_edges):
        myConst = CX_CONSTANTS

        niceCx = NiceCXNetwork()
        #uuid = '7246d8cf-c644-11e6-b48c-0660b7976219'
        search_terms_dict = {k:1 for k in search_string.split(',')}

        solr = pysolr.Solr(solr_url + uuid + '/', timeout=10)

        try:
            results = solr.search(search_string, rows=10000)
            #search_terms_array = [int(n['id']) for n in results.docs]
            search_terms_array = {int(n['id']):1 for n in results.docs}
            if(not search_terms_array):
                return {'message': 'No nodes found'}

            print('starting nodes 1')
            #===================
            # METADATA
            #===================
            available_aspects = []
            for ae in (o for o in self.stream_aspect(uuid, 'metaData')):
                available_aspects.append(ae.get(CX_CONSTANTS.METADATA_NAME))
                mde = MetaDataElement(json_obj=ae)
                niceCx.add_metadata(mde)

            #available_aspects = ['edges', 'nodes'] # TODO - remove this
            opaque_aspects = set(available_aspects).difference(known_aspects_min)

            print(opaque_aspects)

            #===================
            # NODES
            #===================
            if 'nodes' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'nodes')):
                    if search_terms_array.get(ae.get(CX_CONSTANTS.ID)):
                        add_this_node = NodeElement(cx_fragment=ae)
                        niceCx.create_node(add_this_node)
            else:
                raise Exception('Network does not contain any nodes.  Cannot query')

            print('starting edges 1')
            #===================
            # EDGES
            #===================
            edge_count = 0
            added_edges = 0
            start_time = time.time()
            if 'edges' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'edges')):
                    if niceCx.nodes.get(ae.get(CX_CONSTANTS.EDGE_SOURCE_NODE_ID_OR_SUBNETWORK)) is not None or niceCx.nodes.get(ae.get(CX_CONSTANTS.EDGE_TARGET_NODE_ID)) is not None:
                        add_this_edge = EdgeElement(cx_fragment=ae)
                        niceCx.create_edge(add_this_edge)
                        added_edges += 1
                    if edge_count % 5000 == 0:
                        print(edge_count)

                    #if edge_count > 30000:
                    #    break

                    if added_edges > max_edges:
                        raise StopIteration('Max edges reached')
                    edge_count += 1
            else:
                raise Exception('Network does not contain any nodes.  Cannot query')

            print('Response time (Edge search): ' + str(time.time() - start_time))
            print('starting nodes 2')
            #===================
            # NODES
            #===================
            for ae in (o for o in self.stream_aspect(uuid, 'nodes')):
                if niceCx.get_missing_nodes().get(ae.get(CX_CONSTANTS.ID)):
                    add_this_node = NodeElement(cx_fragment=ae)
                    niceCx.create_node(add_this_node)

            #====================
            # NETWORK ATTRIBUTES
            #====================
            if 'networkAttributes' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'networkAttributes')):
                    add_this_network_attribute = NetworkAttributesElement(cx_fragment=ae)
                    niceCx.add_network_attribute(add_this_network_attribute)

            #===================
            # NODE ATTRIBUTES
            #===================
            if 'nodeAttributes' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'nodeAttributes')):
                    if niceCx.nodes.get(ae.get(CX_CONSTANTS.PROPERTY_OF)):
                        add_this_node_att = NodeAttributesElement(json_obj=ae)
                        niceCx.add_node_attribute(add_this_node_att)

            #===================
            # EDGE ATTRIBUTES
            #===================
            if 'edgeAttributes' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'edgeAttributes')):
                    if niceCx.edges.get(ae.get(CX_CONSTANTS.PROPERTY_OF)):
                        add_this_edge_att = EdgeAttributesElement(json_obj=ae)
                        niceCx.set_edge_attribute()

            #===================
            # NODE CITATIONS
            #===================
            if 'nodeCitations' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'nodeCitations')):
                    for e_po in ae.get(CX_CONSTANTS.PROPERTY_OF):
                        if niceCx.get_nodes().get(e_po) is not None:
                            niceCx.add_node_citations_from_cx(ae)

            #===================
            # EDGE CITATIONS
            #===================
            ec_count = 0
            if 'edgeCitations' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'edgeCitations')):
                    for e_po in ae.get(CX_CONSTANTS.PROPERTY_OF):
                        if niceCx.get_edges().get(e_po) is not None:
                            niceCx.add_edge_citations_from_cx(ae)
                    ec_count += 1
                    if ec_count % 500 == 0:
                        print(ec_count)

            #===================
            # CITATIONS
            #===================
            if 'citations' in available_aspects:
                #======================================================
                # FILTER CITATIONS IF THERE ARE EDGE OR NODE CITATIONS
                # OTHERWISE ADD THEM ALL (NO-FILTER) -- TODO
                #======================================================
                for ae in (o for o in self.stream_aspect(uuid, 'citations')):
                    add_this_citation = CitationElement(cx_fragment=ae)
                    niceCx.add_citation(add_this_citation)

            #===================
            # OPAQUE ASPECTS
            #===================
            for oa in opaque_aspects:
                objects = self.stream_aspect(uuid, oa)
                obj_items = (o for o in objects)
                for oa_item in obj_items:
                    aspect_element = AspectElement(oa_item, oa)
                    niceCx.add_opaque_aspect(aspect_element)

        except SolrError as se:
            if('404' in se.message):
                ndex2.get_logger('SOLR').warning('Network not found ' + self.uuid + ' on ' + solr_url + ' server.')
                raise Exception("Network not found (SOLR)")
            else:
                ndex2.get_logger('SOLR').warning('Network error ' + self.uuid + ' on ' + solr_url + ' server. ' + se.message)
                raise Exception(se.message)
        except StopIteration as si:
                ndex2.get_logger('QUERY').warning("Found more than max edges.  Raising exception")
                raise StopIteration(si.message)


        #nice_cx_json = niceCx.to_cx()

        return niceCx
Exemplo n.º 16
0
def create_nice_cx_from_pandas(df,
                               source_field=None,
                               target_field=None,
                               source_node_attr=[],
                               target_node_attr=[],
                               edge_attr=[],
                               edge_interaction=None):
    """
    Create a NiceCXNetwork from a pandas dataframe in which each row
    specifies one edge in the network.

    If only the df argument is provided the dataframe is treated as 'SIF' format,
    where the first two columns specify the source and target node ids of the edge
    and all other columns are ignored. The edge interaction is defaulted to "interacts-with"

    If both the source_field and target_field arguments are provided, the those and any other
    arguments refer to headers in the dataframe, controlling the mapping of columns to
    the attributes of nodes, and edges in the resulting NiceCXNetwork. If a header is not
    mapped the corresponding column is ignored. If the edge_interaction is not specified it
    defaults to "interacts-with"
    :param df: pandas dataframe to process
    :param source_field: header name specifying the name of the source node.
    :param target_field: header name specifying the name of the target node.
    :param source_node_attr: list of header names specifying attributes of the source node.
    :param target_node_attr: list of header names specifying attributes of the target node.
    :param edge_attr: list of header names specifying attributes of the edge.
    :param edge_interaction: the relationship between the source node and the target node, defaulting to "interacts-with"
    :return: NiceCXNetwork
    """

    my_nicecx = NiceCXNetwork()

    # ====================================================
    # IF NODE FIELD NAME (SOURCE AND TARGET) IS PROVIDED
    # THEN USE THOSE FIELDS OTHERWISE USE INDEX 0 & 1
    # ====================================================
    my_nicecx.set_name('Pandas Upload')
    my_nicecx.add_metadata_stub('networkAttributes')
    count = 0
    if source_field and target_field:
        for index, row in df.iterrows():
            if count % 10000 == 0:
                print(count)
            count += 1
            # =============
            # ADD NODES
            # =============
            my_nicecx.create_node(id=row[source_field],
                                  node_name=row[source_field],
                                  node_represents=row[source_field])
            my_nicecx.create_node(id=row[target_field],
                                  node_name=row[target_field],
                                  node_represents=row[target_field])

            # =============
            # ADD EDGES
            # =============
            if edge_interaction:
                if row.get(edge_interaction):
                    my_nicecx.create_edge(
                        id=index,
                        edge_source=row[source_field],
                        edge_target=row[target_field],
                        edge_interaction=row[edge_interaction])
                else:
                    my_nicecx.create_edge(id=index,
                                          edge_source=row[source_field],
                                          edge_target=row[target_field],
                                          edge_interaction=edge_interaction)
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[source_field],
                                      edge_target=row[target_field],
                                      edge_interaction='interacts-with')

            # ==============================
            # ADD SOURCE NODE ATTRIBUTES
            # ==============================
            for sp in source_node_attr:
                attr_type = None
                if type(row[sp]) is float and math.isnan(row[sp]):
                    row[sp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[sp]) is float and math.isinf(row[sp]):
                    row[sp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[sp]) is float:
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif isinstance(row[sp], int):
                    attr_type = ATTRIBUTE_DATA_TYPE.INTEGER
                my_nicecx.set_node_attribute(row[source_field],
                                             sp,
                                             row[sp],
                                             type=attr_type)

            # ==============================
            # ADD TARGET NODE ATTRIBUTES
            # ==============================
            for tp in target_node_attr:
                attr_type = None
                if type(row[tp]) is float and math.isnan(row[tp]):
                    row[tp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[tp]) is float and math.isinf(row[tp]):
                    row[tp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[tp]) is float:
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif isinstance(row[tp], int):
                    attr_type = ATTRIBUTE_DATA_TYPE.INTEGER
                my_nicecx.set_node_attribute(row[target_field],
                                             tp,
                                             row[tp],
                                             type=attr_type)

            # ==============================
            # ADD EDGE ATTRIBUTES
            # ==============================
            for ep in edge_attr:
                attr_type = None
                if type(row[ep]) is float and math.isnan(row[ep]):
                    row[ep] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[ep]) is float and math.isinf(row[ep]):
                    row[ep] = 'INFINITY'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT

                my_nicecx.set_edge_attribute(index,
                                             ep,
                                             row[ep],
                                             type=attr_type)

    else:
        for index, row in df.iterrows():
            # =============
            # ADD NODES
            # =============
            my_nicecx.create_node(id=row[0],
                                  node_name=row[0],
                                  node_represents=row[0])
            my_nicecx.create_node(id=row[1],
                                  node_name=row[1],
                                  node_represents=row[1])

            # =============
            # ADD EDGES
            # =============
            if len(row) > 2:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction=row[2])
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction='interacts-with')

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if source_node_attr or target_node_attr:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if edge_attr:
        my_nicecx.add_metadata_stub('edgeAttributes')

    return my_nicecx
Exemplo n.º 17
0
    def query_network_fast(self, uuid, search_string, max_edges):
        niceCx = NiceCXNetwork()
        #uuid = '7246d8cf-c644-11e6-b48c-0660b7976219'
        search_terms_dict = {k:1 for k in search_string.split(',')}
        edge_keepers = set([])
        node_keepers = set([])

        solr = pysolr.Solr(solr_url + uuid + '/', timeout=10)

        try:
            results = solr.search(search_string, rows=10000)
            #search_terms_array = [int(n['id']) for n in results.docs]
            search_terms_array = {int(n['id']):1 for n in results.docs}
            search_terms_set = set([int(n['id']) for n in results.docs])
            if(not search_terms_array):
                return {'message': 'No nodes found'}

            print('starting nodes 1')
            #===================
            # METADATA
            #===================
            available_aspects = []
            for ae in (o for o in self.stream_aspect(uuid, 'metaData')):
                available_aspects.append(ae.get(CX_CONSTANTS.METADATA_NAME))
                mde = MetaDataElement(json_obj=ae)
                niceCx.add_metadata(mde)

            opaque_aspects = set(available_aspects).difference(known_aspects_min)

            print(opaque_aspects)

            #===================
            # EDGES
            #===================
            edge_count = 0
            added_edges = 0
            edges = []
            edge_found = False
            edge_id = -1
            edge_s = None
            edge_t = None
            edge_i = None
            e_count = 0

            start_time = time.time()
            parser = self.stream_aspect_raw(uuid, 'edges')

            for prefix, event, value in parser:
                # process event, prefix and value

                if(event == 'end_map'):
                    e_count += 1
                    if e_count % 5000 == 0:
                        print(e_count)
                    if edge_found:
                        edge_keepers.add(edge_id)
                        node_keepers.update([edge_s, edge_t])
                        edges.append(EdgeElement(id=edge_id, edge_source=edge_s, edge_target=edge_t, edge_interaction=edge_i))

                    edge_id = -1
                    edge_s = None
                    edge_t = None
                    edge_i = None
                    edge_found = False

                if (prefix) == ('item.s'):
                    if value in search_terms_set:
                        edge_found = True
                    edge_s = value

                elif (prefix) == ('item.t'):
                    if value in search_terms_set:
                        edge_found = True
                    edge_t = value

                elif (prefix) == ('item.i'):
                    if edge_found:
                        edge_i = value

                elif (prefix) == ('item.@id'):
                    edge_id = value

            print('Response time (Edge search): ' + str(time.time() - start_time))
            print(node_keepers)
            print(edge_keepers)

            '''
            if 'edges' in available_aspects:
                for ae in (o for o in self.stream_aspect(uuid, 'edges')):
                    if ae.get(CX_CONSTANTS.EDGE_SOURCE_NODE_ID) in search_terms_set or ae.get(CX_CONSTANTS.EDGE_TARGET_NODE_ID) in search_terms_set:
                        edge_keepers.add(ae.get(CX_CONSTANTS.ID))
                        node_keepers.update([ae.get(CX_CONSTANTS.EDGE_SOURCE_NODE_ID), ae.get(CX_CONSTANTS.EDGE_TARGET_NODE_ID)])

                    if edge_count % 5000 == 0:
                        print(edge_count)

                    if added_edges > max_edges:
                        raise StopIteration('Max edges reached')

                    edge_count += 1
            else:
                raise Exception('Network does not contain any nodes.  Cannot query')
            '''

        except SolrError as se:
            if('404' in se.message):
                ndex2.get_logger('SOLR').warning('Network not found ' + self.uuid + ' on ' + solr_url + ' server.')
                raise Exception("Network not found (SOLR)")
            else:
                ndex2.get_logger('SOLR').warning('Network error ' + self.uuid + ' on ' + solr_url + ' server. ' + se.message)
                raise Exception(se.message)
        except StopIteration as si:
                ndex2.get_logger('QUERY').warning("Found more than max edges.  Raising exception")
                raise StopIteration(si.message)
Exemplo n.º 18
0
    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'))