コード例 #1
0
    def test_create_from_tsv_manipulate_and_save(self):
        print('Testing: mgdb_mutations.txt')
        path_to_network = os.path.join(path_this, 'mgdb_mutations.txt')

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

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

            niceCx = ndex2.create_nice_cx_from_pandas(df, source_field='CDS Mutation', target_field='Gene Symbol', source_node_attr=['Primary Tissue', 'Histology', 'Genomic Locus'], target_node_attr=['Gene ID'], edge_interaction='variant-gene-relationship') #NiceCXNetwork()

            nice_networkx = niceCx.to_networkx()
            nice_pandas = niceCx.to_pandas_dataframe()
            my_csv = nice_pandas.to_csv(sep='\t')

            with open("pandas_to_cx_to_tsv_results.txt", "w") as text_file:
                text_file.write(my_csv)

            niceCx_from_netx = ndex2.create_nice_cx_from_networkx(nice_networkx)

            # Restore template
            niceCx_from_netx.apply_template('ndexbio.org', '2e8f9bdc-1e5f-11e8-b939-0ac135e8bacf')
            niceCx_from_netx.set_name('Round trip from server to networkx to NDEx')

            upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #2
0
    def test_node_data_types_from_tsv(self):
        self.assertFalse(upload_username == 'username')
        path_to_network = os.path.join(path_this, 'mgdb_mutations.txt')

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

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

            niceCx = ndex2.create_nice_cx_from_pandas(df, source_field='CDS Mutation', target_field='Gene Symbol',
                                                      source_node_attr=['Primary Tissue', 'Histology', 'Genomic Locus',
                                                                        'Gene ID'], target_node_attr=['Gene ID'],
                                                      edge_interaction='variant-gene-relationship') #NiceCXNetwork()

            found_string_type = False
            for id, node in niceCx.get_nodes():
                abc_node_attrs = niceCx.get_node_attributes(node)

                if abc_node_attrs is not None:
                    for node_attr in abc_node_attrs:
                        if node_attr.get('d') == 'string':
                            found_string_type = True

            self.assertTrue(found_string_type)
コード例 #3
0
    def test_create_from_small_cx(self):
        print('Testing: Pandas DataFrame.from_items')
        my_cx = [
            {"numberVerification":[{"longNumber":281474976710655}]},
            {"metaData":[{"consistencyGroup":1,"elementCount":2,"idCounter":2,"name":"nodes","version":"1.0"},
            {"consistencyGroup":1,"elementCount":1,"idCounter":1,"name":"edges","version":"1.0"}]},
            {"nodes":[{"@id": 1, "n": "node1", "r": "ABC"}, {"@id": 2, "n": "node2", "r": "DEF"}]},
            {"edges":[{"@id": 1, "s": 1, "t": 2, "i": "neighbor"}]},
            {"status":[{"error":"","success":True}]}
        ]

        #niceCx = NiceCXNetwork(cx=my_cx)

        #data = [('Source', 'Target', 'interaction', 'EdgeProp'), ('ABC', 'DEF', 'interacts-with', 'Edge property 1'), ('DEF', 'XYZ', 'neighbor-of', 'Edge property 2')]
        #df = pd.DataFrame.from_records(data)
        #niceCx = NiceCXNetwork(pandas_df=df)

        df = pd.DataFrame.from_items([('Source', ['ABC', 'DEF']),
                                        ('Target', ['DEF', 'XYZ']),
                                        ('Interaction', ['interacts-with', 'neighbor-of']),
                                        ('EdgeProp', [np.float(0.843), 1.2345])])

        #niceCx = ndex2.create_empty_nice_cx() #NiceCXNetwork()
        #niceCx.create_from_pandas(df, source_field='Source', target_field='Target', edge_attr=['EdgeProp'], edge_interaction='Interaction')

        niceCx = ndex2.create_nice_cx_from_pandas(df, source_field='Source', target_field='Target', edge_attr=['EdgeProp'], edge_interaction='Interaction')
        #niceCx = '' #NiceCXNetwork(server='public.ndexbio.org', uuid='f1dd6cc3-0007-11e6-b550-06603eb7f303')

        upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
コード例 #4
0
    def test_create_from_tsv_manipulate_and_save(self):
        path_to_network = os.path.join(path_this, 'mgdb_mutations.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 = ndex2.create_nice_cx_from_pandas(df, source_field='CDS Mutation', target_field='Gene Symbol', source_node_attr=['Primary Tissue', 'Histology', 'Genomic Locus'], target_node_attr=['Gene ID'], edge_interaction='variant-gene-relationship') #NiceCXNetwork()

            nice_networkx = niceCx.to_networkx()
            nice_pandas = niceCx.to_pandas_dataframe()
            my_csv = nice_pandas.to_csv(sep='\t')

            with open("pandas_to_cx_to_tsv_results.txt", "w") as text_file:
                text_file.write(my_csv)

            niceCx_from_netx = ndex2.create_nice_cx_from_networkx(nice_networkx)

            # Restore template
            niceCx_from_netx.apply_template('public.ndexbio.org', '72ef5c3a-caff-11e7-ad58-0ac135e8bacf')
            niceCx_from_netx.set_name('Round trip from server to networkx to NDEx')

            upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #5
0
ファイル: api.py プロジェクト: NuriaQueralt/comm2net
def upload_file():
    try:
        data = request.files.get('file')
    except Exception as e:
        raise HTTPError(500, e)

    if data and data.file:
        if (request.query.alpha):
            alpha = request.query.alpha

        if (request.query.beta):
            beta = request.query.beta

        wb = xlrd.open_workbook(file_contents=data.file.read())

        df = pd.read_excel(wb, engine='xlrd')

        source_column = request.forms.get('source')
        target_column = request.forms.get('target')
        edge_column = request.forms.get('edge')

        nice_cx = ndex2.create_nice_cx_from_pandas(
            df,
            source_field='CDS Mutation',
            target_field='Gene Symbol',
            source_node_attr=['Primary Tissue'],
            target_node_attr=['Gene ID'],
            edge_attr=[])

        #upload_message = nice_cx.upload_to(upload_server, upload_username, upload_password)

        nice_cx.print_summary()
コード例 #6
0
    def test_pandas_loading(self):
        print('Testing: edge_list_network_adrian_small.txt')
        path_to_network = os.path.join(path_this,
                                       'edge_list_network_adrian_small.txt')
        niceCxBuilder = NiceCXBuilder()
        context = {
            'signor': 'http://signor.uniroma2.it/relation_result.php?id=',
            'BTO': 'http://identifiers.org/bto/BTO:',
            'uniprot': 'http://identifiers.org/uniprot/',
            'pubmed': 'http://identifiers.org/pubmed/',
            'CID': 'http://identifiers.org/pubchem.compound/',
            'SID': 'http://identifiers.org/pubchem.substance/',
            'chebi': 'http://identifiers.org/chebi/CHEBI:'
        }

        with open(path_to_network, 'r') as tsvfile:
            header = ['Source', 'Target']

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

            nice_cx = ndex2.create_nice_cx_from_pandas(
                df)  #NiceCXNetwork(pandas_df=df)
            nice_cx.set_context(context)

            upload_message = nice_cx.upload_to(upload_server, upload_username,
                                               upload_password)

            self.assertTrue('error' not in upload_message)
コード例 #7
0
    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)
コード例 #8
0
    def test_pandas_loading(self):
        print('Testing: edge_list_network_adrian_small.txt')
        path_to_network = os.path.join(path_this, 'edge_list_network_adrian_small.txt')
        niceCxBuilder = NiceCXBuilder()
        context = {
            'signor': 'http://signor.uniroma2.it/relation_result.php?id=',
            'BTO': 'http://identifiers.org/bto/BTO:',
            'uniprot': 'http://identifiers.org/uniprot/',
            'pubmed': 'http://identifiers.org/pubmed/',
            'CID': 'http://identifiers.org/pubchem.compound/',
            'SID': 'http://identifiers.org/pubchem.substance/',
            'chebi': 'http://identifiers.org/chebi/CHEBI:'
        }

        with open(path_to_network, 'r') as tsvfile:
            header = ['Source', 'Target']

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

            nice_cx = ndex2.create_nice_cx_from_pandas(df) #NiceCXNetwork(pandas_df=df)
            nice_cx.set_context(context)

            upload_message = nice_cx.upload_to(upload_server, upload_username, upload_password)

            self.assertTrue('error' not in upload_message)
コード例 #9
0
    def test_node_data_types_from_tsv(self):
        self.assertFalse(upload_username == 'username')
        path_to_network = os.path.join(path_this, 'mgdb_mutations.txt')

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

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

            niceCx = ndex2.create_nice_cx_from_pandas(
                df,
                source_field='CDS Mutation',
                target_field='Gene Symbol',
                source_node_attr=[
                    'Primary Tissue', 'Histology', 'Genomic Locus', 'Gene ID'
                ],
                target_node_attr=['Gene ID'],
                edge_interaction='variant-gene-relationship')  #NiceCXNetwork()

            found_string_type = False
            for id, node in niceCx.get_nodes():
                abc_node_attrs = niceCx.get_node_attributes(node)

                if abc_node_attrs is not None:
                    for node_attr in abc_node_attrs:
                        if node_attr.get('d') == 'string':
                            found_string_type = True

            self.assertTrue(found_string_type)
コード例 #10
0
    def test_edit_network_attributes(self):
        with open('SIMPLE_WITH_ATTRIBUTES.txt', '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 = ndex2.create_nice_cx_from_pandas(
                df,
                source_field='SOURCE',
                target_field='TARGET',
                source_node_attr=['NODEATTR'],
                target_node_attr=[],
                edge_attr=['EDGEATTR'])  #NiceCXNetwork()

            for k, v in niceCx.get_nodes():
                node_attr = niceCx.get_node_attribute(k, 'NODEATTR')
                if node_attr:
                    niceCx.set_node_attribute(k, 'NODEATTR',
                                              node_attr.get('v') + 'xyz')
                print(node_attr)

            for k, v in niceCx.get_edges():
                edge_attr = niceCx.get_edge_attribute(k, 'EDGEATTR')
                if edge_attr:
                    niceCx.set_edge_attribute(k, 'EDGEATTR',
                                              edge_attr.get('v') + 'abc')
                print(edge_attr)

            my_cx_json = niceCx.to_cx()
            print(json.dumps(my_cx_json))
コード例 #11
0
    def test_create_from_pandas_no_headers(self):
        path_to_network = os.path.join(path_this, 'SIMPLE.txt')

        with open(path_to_network, 'rU') as tsvfile:
            df = pd.read_csv(tsvfile,delimiter='\t',engine='python',header=None)

            niceCx = ndex2.create_nice_cx_from_pandas(df) #NiceCXNetwork(pandas_df=df)
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #12
0
    def test_create_from_pandas_no_headers(self):
        print('Testing: SIMPLE.txt')
        path_to_network = os.path.join(path_this, 'SIMPLE.txt')

        with open(path_to_network, 'r') as tsvfile:
            df = pd.read_csv(tsvfile,delimiter='\t',engine='python',header=None)

            niceCx = ndex2.create_nice_cx_from_pandas(df) #NiceCXNetwork(pandas_df=df)
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #13
0
    def test_load_from_pandas(self):
        path_to_network = os.path.join(path_this, 'SIMPLE3.txt')

        with open(path_to_network, 'r') as tsvfile:
            df = pd.read_csv(tsvfile,delimiter='\t',engine='python',header=None)

            #====================================
            # BUILD NICECX FROM PANDAS DATAFRAME
            #====================================
            niceCx = ndex2.create_nice_cx_from_pandas(df) #NiceCXNetwork(pandas_df=df)
            niceCx.apply_template('public.ndexbio.org', '56cbe15a-5f7e-11e8-a4bf-0ac135e8bacf')
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
コード例 #14
0
    def test_load_from_pandas(self):
        path_to_network = os.path.join(path_this, 'SIMPLE3.txt')

        with open(path_to_network, 'r') as tsvfile:
            df = pd.read_csv(tsvfile,delimiter='\t',engine='python',header=None)

            #====================================
            # BUILD NICECX FROM PANDAS DATAFRAME
            #====================================
            niceCx = ndex2.create_nice_cx_from_pandas(df) #NiceCXNetwork(pandas_df=df)
            niceCx.apply_template('public.ndexbio.org', '56cbe15a-5f7e-11e8-a4bf-0ac135e8bacf')
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
コード例 #15
0
    def test_create_from_pandas_no_headers_3_columns(self):
        path_to_network = os.path.join(path_this, 'SIMPLE3.txt')

        with open(path_to_network, 'rU') as tsvfile:
            df = pd.read_csv(tsvfile,delimiter='\t',engine='python',header=None)

            #====================================
            # BUILD NICECX FROM PANDAS DATAFRAME
            #====================================
            niceCx = ndex2.create_nice_cx_from_pandas(df) #NiceCXNetwork(pandas_df=df)
            niceCx.apply_template('public.ndexbio.org', '72ef5c3a-caff-11e7-ad58-0ac135e8bacf')
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #16
0
    def test_create_from_pandas_with_headers2(self):
        print('Testing: CTD_genes_pathways_small.txt')
        path_to_network = os.path.join(path_this, 'CTD_genes_pathways_small.txt')

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

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

            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=[])

            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #17
0
    def test_create_from_pandas_with_headers(self):
        path_to_network = os.path.join(path_this, 'MDA1.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 = ndex2.create_nice_cx_from_pandas(df, source_field='Bait', target_field='Prey', source_node_attr=['AvePSM'], target_node_attr=['WD'], edge_attr=['Z', 'Entropy']) #NiceCXNetwork()
            #niceCx.create_from_pandas(df, source_field='Bait', target_field='Prey', source_node_attr=['AvePSM'], target_node_attr=['WD'], edge_attr=['Z', 'Entropy'])
            my_cx_json = niceCx.to_cx()
            #print(json.dumps(my_cx_json, cls=DecimalEncoder))
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #18
0
    def test_create_from_big_tsv(self):
        print('Testing: identifier_mappings_small.txt')
        path_to_network = os.path.join(path_this, 'identifier_mappings_small.txt')

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

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

            niceCx = ndex2.create_nice_cx_from_pandas(df, source_field='Preferred_Name', target_field='Name',
                                                      source_node_attr=['Source'],
                                                      edge_interaction='neighbor-of')

            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #19
0
    def test_create_from_pandas_with_headers(self):
        print('Testing: MDA1.txt')
        path_to_network = os.path.join(path_this, 'MDA1.txt')

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

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

            niceCx = ndex2.create_nice_cx_from_pandas(df, source_field='Bait', target_field='Prey', source_node_attr=['AvePSM'], target_node_attr=['WD'], edge_attr=['Z', 'Entropy']) #NiceCXNetwork()
            #niceCx.create_from_pandas(df, source_field='Bait', target_field='Prey', source_node_attr=['AvePSM'], target_node_attr=['WD'], edge_attr=['Z', 'Entropy'])
            my_cx_json = niceCx.to_cx()
            #print(json.dumps(my_cx_json, cls=DecimalEncoder))
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
コード例 #20
0
    def test_pandas(self):

        df = pd.DataFrame.from_dict({'Source': ['a', 'b', 'c','e'],
                                      'Target': ['b', 'c', 'd', 'f'],
                                      'Interaction': ['touches', 'smells', 'tastes', 'hears'],
                                      'color': ['red', 'blue', 'yellow', 'green'],
                                      'size':['1', '2', '3', '4'],
                                      'width': ['az', 'by', 'cx', 'mn']})
        niceCx_df_with_headers = ndex2.create_nice_cx_from_pandas(df, source_field='Source', target_field='Target',
                                                                  edge_attr=['color'],
                                                                  edge_interaction='Interaction', source_node_attr=["size"],
                                                                  target_node_attr=["width"])
        upload_message = niceCx_df_with_headers.upload_to(upload_server, upload_username, upload_password)
        niceCx_df_with_headers.print_summary()

        self.assertTrue(upload_message)
コード例 #21
0
    def test_pandas(self):

        df = pd.DataFrame.from_dict({'Source': ['a', 'b', 'c','e'],
                                      'Target': ['b', 'c', 'd', 'f'],
                                      'Interaction': ['touches', 'smells', 'tastes', 'hears'],
                                      'color': ['red', 'blue', 'yellow', 'green'],
                                      'size':['1', '2', '3', '4'],
                                      'width': ['az', 'by', 'cx', 'mn']})
        niceCx_df_with_headers = ndex2.create_nice_cx_from_pandas(df, source_field='Source', target_field='Target',
                                                                  edge_attr=['color'],
                                                                  edge_interaction='Interaction', source_node_attr=["size"],
                                                                  target_node_attr=["width"])
        upload_message = niceCx_df_with_headers.upload_to(upload_server, upload_username, upload_password)
        niceCx_df_with_headers.print_summary()

        self.assertTrue(upload_message)
コード例 #22
0
    def test_create_nice_cx_from_pandas_with_sif(self):
        data = {
            'source': ['Node 1', 'Node 2'],
            'target': ['Node 2', 'Node 3'],
            'interaction': ['helps', 'hurts']
        }
        df = pd.DataFrame.from_dict(data)
        # on older versions of pandas this order is not consistent
        # so setting it explicitly
        df = df[['source', 'target', 'interaction']]

        net = ndex2.create_nice_cx_from_pandas(df)
        self.assertEqual(2, len(net.get_edges()))
        self.assertEqual(3, len(net.get_nodes()))
        for node_id, node in net.get_nodes():
            self.assertEqual(
                {
                    '@id': node_id,
                    'n': 'Node ' + str(node_id + 1),
                    'r': 'Node ' + str(node_id + 1)
                }, node)
        for edge_id, edge in net.get_edges():
            if edge_id == 0:
                self.assertEqual({
                    '@id': 0,
                    's': 0,
                    't': 1,
                    'i': 'helps'
                }, edge)
            elif edge_id == 1:
                self.assertEqual({
                    '@id': 1,
                    's': 1,
                    't': 2,
                    'i': 'hurts'
                }, edge)
            else:
                self.fail('Unexpected edge: ' + str(edge))
コード例 #23
0
    def test_edit_network_attributes(self):
        with open('SIMPLE_WITH_ATTRIBUTES.txt', '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 = ndex2.create_nice_cx_from_pandas(df, source_field='SOURCE', target_field='TARGET', source_node_attr=['NODEATTR'], target_node_attr=[], edge_attr=['EDGEATTR']) #NiceCXNetwork()

            for k, v in niceCx.get_nodes():
                node_attr = niceCx.get_node_attribute(k, 'NODEATTR')
                if node_attr:
                    niceCx.set_node_attribute(k, 'NODEATTR', node_attr.get('v') + 'xyz')
                print(node_attr)

            for k, v in niceCx.get_edges():
                edge_attr = niceCx.get_edge_attribute(k, 'EDGEATTR')
                if edge_attr:
                    niceCx.set_edge_attribute(k, 'EDGEATTR', edge_attr.get('v') + 'abc')
                print(edge_attr)


            my_cx_json = niceCx.to_cx()
            print(json.dumps(my_cx_json))
コード例 #24
0
    def test_create_nice_cx_from_networkx_roundtrip_no_attrs(self):
        net = ndex2.nice_cx_network.NiceCXNetwork()
        node_one = net.create_node('Node 1')
        node_two = net.create_node('Node 2')
        net.create_edge(edge_source=node_one, edge_target=node_two)
        net_df = net.to_pandas_dataframe(include_attributes=True)
        net_roundtrip = ndex2.create_nice_cx_from_pandas(net_df,
                                                         source_field='source',
                                                         target_field='target')
        self.assertEqual(
            'created from pandas by '
            'ndex2.create_nice_cx_from_pandas()', net_roundtrip.get_name())
        self.assertEqual(1, len(net_roundtrip.get_edges()))
        self.assertEqual(2, len(net_roundtrip.get_nodes()))
        self.assertEqual((0, {
            '@id': 0,
            's': 0,
            't': 1,
            'i': 'interacts-with'
        }),
                         list(net_roundtrip.get_edges())[0])

        for node_id, node_obj in net_roundtrip.get_nodes():
            if node_id == 0:
                self.assertEqual({
                    '@id': 0,
                    'n': 'Node 1',
                    'r': 'Node 1'
                }, node_obj)
            elif node_id == 1:
                self.assertEqual({
                    '@id': 1,
                    'n': 'Node 2',
                    'r': 'Node 2'
                }, node_obj)
            else:
                self.fail('Invalid node: ' + str(node_obj))
コード例 #25
0
ファイル: api.py プロジェクト: NuriaQueralt/comm2net
def upload_file():
    try:
        data = request.files.get('file')
        node_data = request.files.get('nodeFile')
    except Exception as e:
        raise HTTPError(500, e)

    if data and data.file:
        if (request.query.alpha):
            alpha = request.query.alpha

        if (request.query.beta):
            beta = request.query.beta

        source_column = request.forms.get('source')
        target_column = request.forms.get('target')
        edge_column = request.forms.get('edge')
        node_file_id_col = request.forms.get('nodeFileIDColumn')

        with tempfile.NamedTemporaryFile('wb', delete=False) as f:
            f.write(data.file.read())
            f_name = f.name
            f.close()

        with open(f_name, 'r') as tsvfile:
            header = [h.strip() for h in tsvfile.readline().split(',')]
            edge_attrs = [
                e for e in header
                if e not in [source_column, target_column, edge_column]
            ]

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

            nice_cx = ndex2.create_nice_cx_from_pandas(
                df,
                source_field=source_column,
                target_field=target_column,
                source_node_attr=[],
                target_node_attr=[],
                edge_attr=edge_attrs,
                edge_interaction=edge_column)  #NiceCXNetwork()

        if node_data and node_data.file:
            with tempfile.NamedTemporaryFile('wb', delete=False) as f2:
                f2.write(node_data.file.read())
                f_name2 = f2.name
                f2.close()

            with open(f_name2, 'r') as tsvfile2:
                header2 = [h.strip() for h in tsvfile2.readline().split(',')]
                dtype_set = {h_n: str for h_n in header2}

                df2 = pd.read_csv(tsvfile2,
                                  delimiter=',',
                                  engine='python',
                                  names=header2,
                                  dtype=dtype_set)

                for index, row in df2.iterrows():
                    node_name_lookup = {
                        node.get('n'): node.get('@id')
                        for node_id, node in nice_cx.get_nodes()
                    }
                    node_id = node_name_lookup.get(row[node_file_id_col])
                    for header_column in header2:
                        if header_column != node_file_id_col:
                            if header_column == 'name':
                                nice_cx.add_node_attribute(
                                    property_of=node_id,
                                    name='name_attr',
                                    values=row[header_column])
                            else:
                                nice_cx.add_node_attribute(
                                    property_of=node_id,
                                    name=header_column,
                                    values=row[header_column])

        upload_message = nice_cx.upload_to(upload_server, upload_username,
                                           upload_password)

        nice_cx.print_summary()

        return upload_message
コード例 #26
0
# #### *ndex2.create_nice_cx_from_networkx(networkx)

# In[ ]:

#create an Empty NetworkX
G = nx.Graph()
nice_cx_from_networkx = ndex2.create_nice_cx_from_networkx(G)

# #### *ndex2.create_nice_cx_from_pandas(dataframe)

# In[ ]:

#create an Empty Pandas Dataframe
df = pd.DataFrame()
nice_cx_from_pandas = ndex2.create_nice_cx_from_pandas(df)

# #### *ndex2.create_nice_cx_from_server(server, username=None, password=None, uuid=None)

# In[ ]:

uuid = "1c69beff-1229-11e6-a039-06603eb7f303"
nice_cx_from_server = ndex2.create_nice_cx_from_server(
    server='http://public.ndexbio.org', uuid=uuid)
nice_cx_from_server.print_summary()

# #### create_node(name, represents=None)

# In[ ]:

#Starting with an Empty Network
コード例 #27
0
def push_to_ndex(biggim_id,
                 username='******',
                 password='******',
                 server="http://public.ndexbio.org",
                 network_name=None,
                 network_set_name=None,
                 network_set_description=None):
    glogger.debug("Looking for biggim request [%s]" % (biggim_id))
    request_status = get_request_status(biggim_id)
    while request_status['status'] == 'running':
        time.sleep(1)
        request_status = get_request_status(biggim_id)
    glogger.debug("Request search response[%s]" % (str(request_status)))
    if request_status['status'] == 'complete':
        csvs = request_status['request_uri']
    else:
        glogger.error("Error attempting to get [%s]" % (biggim_id))
        return request_status
    if network_set_name is None:
        network_set_name = "Anonymous"
        network_set_description = "Biggim network"
    if network_name is None:
        network_name = "Biggim - [%s]" % (biggim_id, )
    try:
        my_ndex = ndex2.Ndex2(server, username, password)
        my_ndex.update_status()
    except Exception as inst:
        glogger.exception("Could not access account %s with password %s" %
                          (username, password))
        return {'status': 'error', 'message': "Invalid authentication to NDEX"}
    user = my_ndex.get_user_by_username(username)
    if network_set_name is None:
        network_set = "Anonymous"
        network_set_description = "Autogenerated biggim ndex file.  CSV source at %s" % csv_url
    ctr = 0
    ndex_urls = []
    ndex_ids = []
    public_url = []
    for csv_url in csvs:
        df = pandas.read_csv(csv_url)

        df.loc[:, 'Interaction'] = 'coexpression'

        non_att = ['GPID', 'Gene1', 'Gene2', 'Interaction']
        edge_attr = [c for c in df.columns if c not in non_att]
        df.loc[:, 'Gene1'] = df.apply(lambda x: "ncbigene:%i" % x['Gene1'],
                                      axis=1)
        df.loc[:, 'Gene2'] = df.apply(lambda x: "ncbigene:%i" % x['Gene2'],
                                      axis=1)
        niceCx_df_with_headers = ndex2.create_nice_cx_from_pandas(
            df,
            source_field='Gene1',
            target_field='Gene2',
            edge_attr=edge_attr,
            edge_interaction='Interaction')
        # add gene context for lookup
        context = [{'ncbigene': 'http://identifiers.org/ncbigene/'}]
        niceCx_df_with_headers.set_name("%s - [%i]" % (network_name, ctr))
        ndex_network_url = niceCx_df_with_headers.upload_to(
            server, username, password, visibility='PUBLIC')

        ndex_urls.append(ndex_network_url)
        ndex_network_uuid = ndex_network_url.split('/')[-1]
        ndex_ids.append(ndex_network_uuid)
        public_url.append("http://www.ndexbio.org/#/network/%s" %
                          (ndex_network_uuid, ))
        ctr += 1
    net_set_uuid = None
    network_sets = my_ndex.get('/user/%s/networksets' % user['externalId'])
    for ns in network_sets:
        if ns['name'] == network_set_name:
            net_set_uuid = ns['externalId']
    if net_set_uuid is None:
        net_set_url = my_ndex.create_networkset(network_set_name,
                                                network_set_description)
        net_set_uuid = net_set_url.split('/')[-1]
    my_ndex.add_networks_to_networkset(net_set_uuid, [ndex_network_uuid])
    return {
        'status': 'complete',
        'request_id': biggim_id,
        'ndex_network_url': ndex_urls,
        'ndex_network_uuid': ndex_ids,
        'ndex_public_url': public_url
    }
コード例 #28
0
    def test_create_nice_cx_from_networkx_roundtrip_with_attrs(self):
        net = ndex2.nice_cx_network.NiceCXNetwork()
        node_one = net.create_node('Node 1')
        node_two = net.create_node('Node 2')
        net.set_node_attribute(node_one, 'somestrfield', 'hi')

        edge_one = net.create_edge(edge_source=node_one,
                                   edge_target=node_two,
                                   edge_interaction='breaks')
        net.set_edge_attribute(edge_one, 'somestrfield', 'bye')
        net.set_opaque_aspect(ndex2.constants.CARTESIAN_LAYOUT_ASPECT,
                              [{
                                  'node': node_one,
                                  'x': 1.0,
                                  'y': 2.0
                              }, {
                                  'node': node_two,
                                  'x': 3.0,
                                  'y': 4.0
                              }])

        net.set_name('mynetwork')
        net_df = net.to_pandas_dataframe(include_attributes=True)
        net_roundtrip = ndex2.create_nice_cx_from_pandas(
            net_df,
            source_field='source',
            target_field='target',
            edge_interaction='interaction',
            edge_attr=['somestrfield'],
            source_node_attr=['source_somestrfield'])
        self.assertEqual(
            'created from pandas by '
            'ndex2.create_nice_cx_from_pandas()', net_roundtrip.get_name())
        self.assertEqual(1, len(net_roundtrip.get_edges()))
        self.assertEqual(2, len(net_roundtrip.get_nodes()))

        self.assertEqual((0, {
            '@id': 0,
            's': 0,
            't': 1,
            'i': 'breaks'
        }),
                         list(net_roundtrip.get_edges())[0],
                         '\n\n' + str(net_df.head()))

        for node_id, node_obj in net_roundtrip.get_nodes():
            if node_id == 0:
                self.assertEqual({
                    '@id': 0,
                    'n': 'Node 1',
                    'r': 'Node 1'
                }, node_obj)
            elif node_id == 1:
                self.assertEqual({
                    '@id': 1,
                    'n': 'Node 2',
                    'r': 'Node 2'
                }, node_obj)
            else:
                self.fail('Invalid node: ' + str(node_obj))

        n_a = net_roundtrip.get_node_attribute(node_one, 'source_somestrfield')
        self.assertEqual(
            {
                'po': 0,
                'n': 'source_somestrfield',
                'v': 'hi',
                'd': 'string'
            }, n_a)

        n_a = net_roundtrip.get_edge_attribute(node_one, 'somestrfield')
        self.assertEqual(
            {
                'po': 0,
                'n': 'somestrfield',
                'v': 'bye',
                'd': 'string'
            }, n_a)
コード例 #29
0
 def test_create_nice_cx_from_pandas_with_invalidstr(self):
     try:
         ndex2.create_nice_cx_from_pandas('invalid')
         self.fail('Expected Exception')
     except AttributeError as e:
         self.assertTrue('object has no' in str(e))
コード例 #30
0
 def test_create_nice_cx_from_pandas_with_none(self):
     try:
         ndex2.create_nice_cx_from_pandas(None)
         self.fail('AssertionError')
     except AttributeError as e:
         self.assertTrue('NoneType' in str(e))