def testGrantGroupPermission(self): ndex2 = nc.Ndex(TESTSERVER, username=testUser2, password=testUserpasswd, debug=True) with self.assertRaises(Exception) as context: ndex2.get_network_summary(self._networkId) # print context.exception count = 0 while count < 30: try: self._ndex.update_network_group_permission( 'd7ef9957-de81-11e6-8835-06832d634f41', self._networkId, 'READ') count = 60 except Exception as inst: d = json.loads(inst.response.content) if d.get('errorCode').startswith( "NDEx_Concurrent_Modification"): print("retry in 3 seconds(" + str(count) + ")") count += 1 time.sleep(3) else: raise inst summary = ndex2.get_network_summary(self._networkId) self.assertEqual(summary.get(u'externalId'), self._networkId) print("update_network_group_permission() passed.")
def make_network_public(uuid, ndex_server, ndex_user, ndex_pass, timeout=60, error=False): ndex = nc.Ndex(ndex_server, ndex_user, ndex_pass) sleep_time = 0.25 start = time.time() while True: if time.time() - start > timeout: print('Failed to make network public: error message:') print(traceback.print_exc()) if error: raise Exception('Could not make the NDEX network %s public' % uuid) else: break else: try: ndex.make_network_public(uuid) break except: time.sleep(sleep_time) sleep_time = min(5, 2 * sleep_time)
def testConstructorException(self): with self.assertRaises(Exception): print("testing ndex client constructor.") ndex = nc.Ndex(host="www.google.com", username="******", password="******", debug=True)
def get_edges(thread_name, G, network_id): ndex_dev = nc.Ndex("http://public.ndexbio.org", username="******", password="******") # ndex_dev.set_debug_mode(True) route = "/network/%s/edge/asNetwork/0/500" % (network_id) for i in range(50000): ndex_dev.get(route)
def setUpClass(cls): cls._ndex = nc.Ndex(host=TESTSERVER, username=testUser1, password=testUserpasswd, debug=True) with open(path.join(HERE, 'The_RAS_Machine.cx'), 'r') as cx_file: cls._nrc = cls._ndex.save_cx_stream_as_new_network(cx_file) networkId = uuid.UUID('{' + cls._nrc[-36:] + '}') cls._networkId = str(networkId) time.sleep(2)
def update_network(thread_name, G, network_id): ndex_dev = nc.Ndex("http://54.213.4.3", username="******", password="******") # ndex_dev.set_debug_mode(True) G = copy.deepcopy(G) G.set_name('RAS: ' + network_id) for i in range(10000): print i, thread_name, network_id ndex_dev.update_cx_network(json.dumps(G.to_cx()), network_id) print 'done with', i, thread_name, print 'Thread ', thread_name, ' finished updating.'
def upload_to(self, server, username, password): ''' Upload this network to the specified server to the account specified by username and password. :param server: The NDEx server to upload the network to. :type server: str :param username: The username of the account to store the network. :type username: str :param password: The password for the account. :type password: str Example: ndexGraph.upload_to('http://test.ndexbio.org', 'myusername', 'mypassword') ''' ndex = nc.Ndex(server, username, password) ndex.save_cx_stream_as_new_network(self.to_cx_stream())
def test_get_user_by_username(self): ndex = nc.Ndex(host=ndex_host) user_ttt = ndex.get_user_by_username('ttt') self.assertTrue(str(user_ttt['externalId']) == 'f4015d8b-aaa3-11e6-8e12-06832d634f41') self.assertTrue(str(user_ttt['firstName']) == 'ttt') self.assertTrue(str(user_ttt['lastName']) == 'ttt') self.assertTrue(str(user_ttt['userName']) == 'ttt') self.assertTrue(str(user_ttt['emailAddress']) == '*****@*****.**') user_drh = ndex.get_user_by_username('drh') self.assertTrue(str(user_drh['externalId']) == '662d2d42-7d24-11e6-9265-06832d634f41') self.assertTrue(str(user_drh['firstName']) == 'Doctor') self.assertTrue(str(user_drh['lastName']) == 'Horrible') self.assertTrue(str(user_drh['userName']) == 'drh') self.assertEqual(str(user_drh['emailAddress']), '*****@*****.**')
def testNetworkVisibility(self): ndex2 = nc.Ndex(tt.TESTSERVER) with self.assertRaises(Exception) as context: ndex2.get_network_summary(self._networkId) # print context.exception count = 0 while count < 30 : try : self._ndex.make_network_public(self._networkId) break except Exception as inst : d = json.loads(inst.response.content) if d.get('errorCode').startswith("NDEx_Concurrent_Modification"): print("retry in 5 seconds(" + str(count) + ")") count += 1 time.sleep(5) else : raise inst summary = ndex2.get_network_summary(self._networkId) self.assertEqual(summary.get(u'externalId'), self._networkId) print("make_network_public() passed.") time.sleep(1) count = 0 while count < 30: try: self._ndex.make_network_private(self._networkId) break except Exception as inst: d = json.loads(inst.response.content) if d.get('errorCode').startswith("NDEx_Concurrent_Modification"): print("retry in 5 seconds(" + str(count) + ")") count += 1 time.sleep(5) else: raise inst with self.assertRaises(Exception) as context: ndex2.get_network_summary(self._networkId) print("make_network_private() passed.")
def test_get_network_as_cx_stream(self): ndex = nc.Ndex(host=ndex_host, username=username_1, password=password_1) with open(path.join(path.abspath(path.dirname(__file__)),example_network_1), 'r') as file_handler: network_in_cx_from_file = file_handler.read() # test save_cx_stream_as_new_network test_network_1_uri = ndex.save_cx_stream_as_new_network(network_in_cx_from_file) self.assertTrue(test_network_1_uri.startswith(ndex_host + ndex_network_resource)) network_UUID = str(test_network_1_uri.split("/")[-1]) network_as_cx_stream = ndex.get_network_as_cx_stream(network_UUID) network_as_cx = str(network_as_cx_stream.text) self.assertTrue(network_in_cx_from_file == network_as_cx) time.sleep(10) # test delete_network del_network_return = ndex.delete_network(network_UUID) self.assertTrue(del_network_return == '')
def test_load_nodes(self): with open('model_RAS1.cx') as model_ras: my_client = nc.Ndex(username='******', password='******') provenance = { "creationEvent": { "eventType": "Update Network", "endAtTime": 1504207374563, "startedAtTime": 1504207370219, "inputs": [{ "creationEvent": { "eventType": "Create Network", "inputs": [] }, "uri": "http://public.ndexbio.org/v2/network/c9ad5d14-aea8-11e7-94d3-0ac135e8bacf/summary", "properties": [] }] }, "uri": "http://public.ndexbio.org/v2/network/c9ad5d14-aea8-11e7-94d3-0ac135e8bacf/summary", "properties": [{ "name": "update_count", "value": "400" }] } my_client.set_provenance('c9ad5d14-aea8-11e7-94d3-0ac135e8bacf', provenance) my_client.update_cx_network( model_ras, 'c9ad5d14-aea8-11e7-94d3-0ac135e8bacf') save_these_properties = {'version': '1.88888'}
def main(): parser = argparse.ArgumentParser( description='create NDEx network from TSV, one edge per line') parser.add_argument('username') parser.add_argument('password') parser.add_argument('server') parser.add_argument('tsv') parser.add_argument('plan') parser.add_argument('name') parser.add_argument('desc') parser.add_argument( '-t', action='store', dest='template_id', help='network id for the network to use as a graphic template') parser.add_argument('-l', action='store', dest='layout', help='name of the layout to apply') parser.add_argument('-u', action='store', dest='update_uuid', help='uuid of the network to update') # parser.add_argument('update_username' ) # parser.add_argument('update_password') # parser.add_argument('update_server') arg = parser.parse_args() try: # set up the ndex connection # error thrown if cannot authenticate my_ndex = nc.Ndex("http://" + arg.server, arg.username, arg.password) # current_directory = os.path.dirname(os.path.abspath(__file__)) # plan_filename = os.path.join(current_directory, "import_plans", arg.plan) print "loading plan from: " + arg.plan try: import_plan = d2c.TSVLoadingPlan(arg.plan) except jsonschema.ValidationError as e1: print "Failed to parse the loading plan '" + arg.plan + "': " + e1.message print 'at path: ' + str(e1.absolute_path) print "in block: " print e1.instance return # set up the tsv -> cx converter print "parsing tsv file using loading plan ..." tsv_converter = d2c.TSV2CXConverter(import_plan) #print json.dumps(cx, indent=4) template_network = None if arg.template_id: response = my_ndex.get_network_as_cx_stream(arg.template_id) template_cx = response.json() template_network = networkn.NdexGraph(template_cx) # If update_uuid is set, then we get the existing network's attributes and provenance if arg.update_uuid: response = my_ndex.get_network_aspect_as_cx_stream( arg.update_uuid, "networkAttributes") network_attributes = response.json() provenance = my_ndex.get_provenance(arg.update_uuid) ng = tsv_converter.convert_tsv_to_cx( arg.tsv, network_attributes=network_attributes, provenance=provenance) if template_network: toolbox.apply_network_as_template(ng, template_network) else: response = my_ndex.get_network_aspect_as_cx_stream( arg.update_uuid, "cyVisualProperties") visual_properties = response.json() if len(visual_properties) > 0: ng.unclassified_cx.append( {"cyVisualProperties": visual_properties}) if arg.layout: if arg.layout == "df_simple": layouts.apply_directed_flow_layout(ng) my_ndex.update_cx_network(ng.to_cx_stream(), arg.update_uuid) else: ng = tsv_converter.convert_tsv_to_cx(arg.tsv, name=arg.name, description=arg.desc) if template_network: toolbox.apply_network_as_template(ng, template_network) if arg.layout: if arg.layout == "df_simple": layouts.apply_directed_flow_layout(ng) my_ndex.save_cx_stream_as_new_network(ng.to_cx_stream()) print "Done." except jsonschema.exceptions.ValidationError as ve: print str(ve) exit(1) except requests.exceptions.RequestException, e: print "error in request to NDEx server: " + str(e) raise e
G = copy.deepcopy(G) G.set_name('RAS: ' + network_id) for i in range(10000): print i, thread_name, network_id ndex_dev.update_cx_network(json.dumps(G.to_cx()), network_id) print 'done with', i, thread_name, print 'Thread ', thread_name, ' finished updating.' def get_edges(thread_name, G, network_id): ndex_dev = nc.Ndex("http://public.ndexbio.org", username="******", password="******") # ndex_dev.set_debug_mode(True) route = "/network/%s/edge/asNetwork/0/500" % (network_id) for i in range(50000): ndex_dev.get(route) ndex = nc.Ndex("http://public.ndexbio.org", username="******", password="******") # ndex_dev = nc.Ndex("http://test.ndexbio.org", username="******", password="******") noi_id = '50e3dff7-133e-11e6-a039-06603eb7f303' noi_cx = ndex.get_network_as_cx_stream(noi_id).json() noi = NdexGraph(noi_cx) print 'start' try: # DEV # thread.start_new_thread(update_network, ('thread-1', noi, '65141932-397d-11e6-ab1d-06832d634f41')) # thread.start_new_thread(update_network, ('thread-2', noi, '53a00511-397d-11e6-ab1d-06832d634f41')) # thread.start_new_thread(update_network, ('thread-3', noi, '3db7a6e0-397d-11e6-ab1d-06832d634f41'))
# -u # "mupit" # -t # "8981e7f2-900f-11e6-93d8-0660b7976219" # -g # "test group 1" # -m # 5 # -d # "test_dir/pid_EXTENDED_BINARY_SIF_2016-09-24T14:04:47.203937" args = parser.parse_args() print vars(args) ndex = nc.Ndex(args.server, args.username, args.password) template_network = None if args.template_id: print "template_id: " + str(args.template_id) response = ndex.get_network_as_cx_stream(args.template_id) template_cx = response.json() template_network = networkn.NdexGraph(template_cx) else: path = "test_dir/NCI_Style.cx" with open(path, 'rU') as cxfile: cx = json.load(cxfile) template_network = networkn.NdexGraph(cx) nci_table = False if args.nci:
import ndex.client as nc import ndex_examples.merge.bindingdb_merger as merger ndex = nc.Ndex("http://dev.ndexbio.org", "test", "ndex") ndex.set_debug_mode(True) provA = ndex.get_provenance('523b0ecb-2007-11e5-8169-0aa4c1de39d1') provB = ndex.get_provenance('52456f0c-2007-11e5-8169-0aa4c1de39d1') provC = ndex.get_provenance('524f0bfd-2007-11e5-8169-0aa4c1de39d1') merged_prov = merger.merge_provenance(provA, provB, provC) ndex.set_provenance('524f0bfd-2007-11e5-8169-0aa4c1de39d1', merged_prov)
__author__ = 'aarongary' import ndextsv.delim2cx as d2c import ndex.client as nc import os if False: import_plan = d2c.TSVLoadingPlan( os.path.join('import_plans', 'Justin1.json')) tsv_converter = d2c.TSV2CXConverter(import_plan) my_ndex = nc.Ndex("http://dev2.ndexbio.org", 'scratch', 'scratch') ng = tsv_converter.convert_tsv_to_cx(os.path.join('import', 'DIP_Symbol.txt'), name='my network name', description='my network description') mystr = '' #else: import requests url = 'http://ec2-52-34-7-132.us-west-2.compute.amazonaws.com:3000/upload?save_plan=false' #url = 'http://localhost:8183/upload?save_plan=false' files = { 'plan': open('import_plans/Minkyu.json', 'rb'), 'upload': open('import/MDA231_All_AP-MS_20170804_corrected.txt', 'rb') } data = {
files=build_networks_from_dict_and_mat(top_regulators, connections_matrix,fh_stem=fh_stem) #push edge network to NDEx uuids={} for f in files: net=netn.NdexGraph() netb.load(net,f,edge_attributes=['strength'],header=True) net_name=f.replace(opts.network_directory,'') net.set_name(net_name) uuids[net_name]=net.upload_to('http://public.ndexbio.org','decarlin','perfect6') #retrieve edge networks uuids ndex=nc.Ndex(host='http://public.ndexbio.org', username='******', password='******') id_dict={} for nn in uuids.keys(): ndex.make_network_public(uuids[nn]) #assign uuids to development edges cell_map_file=id_dict_to_net(uuids,ndex,file_name=opts.map) #push development network to NDEx net=netn.NdexGraph() netb.load(net,cell_map_file,edge_attributes=['ndex:internalLink'],header=True) net.set_name(opts.study+' cell lineage map')
def load_edgeMatrix(ndex_uuid, ndex_server, ndex_user, ndex_pass, ndex=None, json=None, verbose=True): """Loads a NumPy array from a NdexGraph with a special CX aspect called "edge_matrix". Parameters ---------- ndex_uuid : str NDEx UUID of ontology ndex_server : str URL of NDEx server ndex_user : str NDEx username ndex_pass : str NDEx password json : module JSON module with "loads" function. Default: the simplejson package (must be installed) Returns ------- X : np.ndarray X_cols : list Column names X_rows : list Row names """ if json is None: import simplejson json = simplejson if ndex is None: ndex = nc.Ndex(ndex_server, ndex_user, ndex_pass) start = time.time() response = ndex.get_network_as_cx_stream(ndex_uuid) if verbose: print('NDEx download time (sec):', time.time() - start) start = time.time() # cx = json.loads(response.text) cx = json.loads(response.content) if verbose: print('Read HTTP response as JSON', json.__name__, 'time (sec):', time.time() - start) start_loop = time.time() for aspect in cx: if 'matrix' in aspect: assert 'matrix_dtype' in aspect assert 'matrix_cols' in aspect assert 'matrix_rows' in aspect # Convert text back into binary data start = time.time() binary_data = base64.decodestring(aspect.get('matrix')) if verbose: print('base64 decoding time (sec):', time.time() - start) dtype = np.dtype(aspect.get('matrix_dtype')) rows = aspect.get('matrix_rows') cols = aspect.get('matrix_cols') dim = (len(rows), len(cols)) # Create a NumPy array, which is nothing but a glorified # pointer in C to the binary data in RAM X = np.frombuffer(binary_data, dtype=dtype).reshape(dim) if verbose: print('loop time (sec):', time.time() - start_loop) return X, rows, cols
def __init__(self): logger.debug('Starting QCA') self.host = "http://www.ndexbio.org" self.results_directory = "qca_results" self.directed_path_query_url = \ 'http://general.bigmech.ndexbio.org:5603/directedpath/query' self.context_expression_query_url = \ 'http://general.bigmech.ndexbio.org:8081' + \ '/context/expression/cell_line' self.context_mutation_query_url = \ 'http://general.bigmech.ndexbio.org:8081/' + \ 'context/mutation/cell_line' # dict of reference network descriptors by network name self.reference_networks = [ #{ # "id": "84f321c6-dade-11e6-86b1-0ac135e8bacf", # "name": "prior", # "type": "canonical", # "server": "public.ndexbio.org" #} { "id": "d68677b8-173d-11e7-b39e-0ac135e8bacf", #"id": "89274295-1730-11e7-b39e-0ac135e8bacf", "name": "Ras Machine", "type": "canonical", "server": "public.ndexbio.org" } ] # -------------------------- # Schemas self.query_result_schema = { "network_description": {}, "forward_paths": [], "reverse_paths": [], "forward_mutation_paths": [], "reverse_mutation_paths": [] } self.reference_network_schema = { "id": "", "name": "", "type": "canonical" } self.query_schema = { "source_names": [], "target_names": [], "cell_line": "" } # -------------------------- # Cell Lines self.cell_lines = [] # -------------------------- # Queries # list of query dicts self.queries = [] self.ndex = nc.Ndex(host=self.host)
import ndex.client as nc import ndex_examples.merge.bindingdb_merger as merger devNdex = nc.Ndex("http://dev.ndexbio.org", "drh", "drh") devNdex.set_debug_mode(True) reactome_network = devNdex.get_complete_network( 'f8cddf73-1c49-11e5-8169-0aa4c1de39d1') bindingdb_network = devNdex.get_complete_network( '5cf1ae26-1c1d-11e5-8169-0aa4c1de39d1') merger.merge_network(bindingdb_network, reactome_network) reactome_network['name'] = "My Network" reactome_network['description'] = "My Network Description" reactome_id = reactome_network['externalId'] bindingdb_id = bindingdb_network['externalId'] reactome_provenance = devNdex.get_provenance(reactome_id) bindingdb_provenance = devNdex.get_provenance(bindingdb_id) new_network_summary = devNdex.save_new_network(reactome_network) new_network_id = new_network_summary['externalId'] new_network_provenance = devNdex.get_provenance(new_network_id) merged_provenance = merger.merge_provenance(reactome_provenance, bindingdb_provenance, new_network_provenance)
def test_network_properties(self): ndex = nc.Ndex(host=tt.TESTSERVER, username=tt.testUser1, password=tt.testUserpasswd, debug=True) with open( path.join(path.abspath(path.dirname(__file__)), example_network_1), 'r') as file_handler: network_in_cx = file_handler.read() # test save_cx_stream_as_new_network test_network_1_uri = ndex.save_cx_stream_as_new_network(network_in_cx) self.assertTrue( test_network_1_uri.startswith(tt.TESTSERVER + ndex_network_resource)) network_UUID = str(test_network_1_uri.split("/")[-1]) ################### first, we add a new property with subNetworkId == None ################### # get network summary network_summary = ndex.get_network_summary(network_UUID) property_list = [] if "properties" in network_summary: property_list = network_summary['properties'] property_list.append({ 'dataType': 'string', 'predicateString': 'newProperty', 'subNetworkId': None, 'value': 'New Value' }) time.sleep(10) number_of_properties = ndex.set_network_properties( network_UUID, property_list) self.assertTrue(number_of_properties == len(property_list)) # get network summary with the new properties network_summary_1 = ndex.get_network_summary(network_UUID) property_list_1 = network_summary_1['properties'] self.assertTrue(property_list == property_list_1) ################### now, we add a new property with subNetworkId != None ################### self.assertTrue("subnetworkIds" in network_summary, "'subnetworkIds' structure is not in network_summary") number_of_subnetworks = len(network_summary["subnetworkIds"]) self.assertTrue(number_of_subnetworks == 1, "Expected 1 subnetwork in network summary, but there are " \ + str(number_of_subnetworks)) subnetwork_id = network_summary["subnetworkIds"][0] property_list_1.append({ 'dataType': 'string', 'predicateString': 'newProperty', 'subNetworkId': subnetwork_id, 'value': 'New Value' }) number_of_properties = ndex.set_network_properties( network_UUID, property_list_1) self.assertTrue(number_of_properties == len(property_list_1)) # get network summary with the new properties network_summary_2 = ndex.get_network_summary(network_UUID) property_list_2 = network_summary_2['properties'] self.assertTrue(property_list_1 == property_list_2) # test delete_network del_network_return = ndex.delete_network(network_UUID) self.assertTrue(del_network_return == '')
action='store_const', const=True, default=False, help='verbose mode') arg = parser.parse_args() if arg.verbose: print "Starting diffusion service in verbose mode" else: print "Starting diffusion service" app = default_app() app.config['verbose'] = arg.verbose app.config['ndex'] = nc.Ndex() current_kernel_id = None username = None password = None @route('/hello') def index(name='User'): verbose_mode = app.config.get("verbose") if verbose_mode: return template( '<b>This is the test method saying Hello verbosely</b>!', name=name) else: return 'Hello!'
raise Exception("in attempt to update property %s with value %s, no CX data type could be determined" % (name, value)) for property in property_list: if property["predicateString"] == name: property["value"] = str(value) property["dataType"] = data_type return property_list.append({ "predicateString": name, "subNetworkId": subnetwork_id, "value": str(value), "dataType": data_type }) example_network_1_id = "b612d677-c714-11e6-b48c-0660b7976219" ndex = nc.Ndex(host=ndex_host, username=username_1, password=password_1,debug=True) example_network_1_summary = ndex.get_network_summary(example_network_1_id) print(json.dumps(example_network_1_summary, indent=4)) # Get the network example_network_1 = get_network(ndex, example_network_1_id) # Save it as a new network test_network_1_uri = ndex.save_new_network(example_network_1) test_network_1_id = network_id_from_uri(test_network_1_uri)
def getNetworkProperty(summary, prop_name): for prop in summary['properties']: if ( prop['predicateString'] == prop_name) : return prop['value'] return None if False: netpath_base_uri = 'http://www.netpath.org/pathways?path_id=' ndex_server = 'http://dev.ndexbio.org' current_directory = os.path.dirname(os.path.abspath(__file__)) run_normal = False current_netpath_metadata = {} ndex = nc.Ndex(ndex_server,'netpathtest','netpathtest') summaries = ndex.search_networks('*', 'netpathtest',0,50) for summary in summaries.get('networks'): print summary.get('name') #print summary.get('externalId') #print summary.get('description') #print getNetworkProperty(summary, 'Reference') print '====================' current_netpath_metadata[summary.get('name')] = { 'uuid': summary.get('externalId'), 'description': summary.get('description'), 'reference': getNetworkProperty(summary, 'Reference') }
def test_scratch(): #qca = QCA() # source_names = ["CALM3"] # target_names = ["NFATC2"] source_names = ["AKT1", "AKT2", "AKT3"] target_names = [ "CCND1" ] # ,"CDKN1A","FOXO3","GSK3B","MAP2K1","MAPK3","MTOR","PARP1","PIK3CA","RICTOR","TP53"] #AKT1 #AKT1S1 #AKT2 #AKT3 results_list = [ ] # qca.find_causal_path(source_names, target_names, relation_types=None) # ["Activation", "controls-state-change-of", "in-complex-with", "controls-transport-of", "controls-phosphorylation-of"]) print "results_list:" print results_list host = "http://www.ndexbio.org" directed_path_query_url = 'http://general.bigmech.ndexbio.org/directedpath/query' ndex = nc.Ndex(host=host) #reference_network_cx = ndex.get_network_as_cx_stream("5294f70b-618f-11e5-8ac5-06603eb7f303") #==================== # Assemble REST url #==================== uuid_prior = "84f321c6-dade-11e6-86b1-0ac135e8bacf" uuid_high_confidence = "b04e406b-dc88-11e6-86b1-0ac135e8bacf" target = ",".join(target_names) source = ",".join(source_names) max_number_of_paths = 200 url = directed_path_query_url + '?source=' + source + '&target=' + target + '&uuid=' + uuid_prior + '&server=www.ndexbio.org' + '&pathnum=' + str( max_number_of_paths) #f = io.BytesIO() #f.write(reference_network_cx.content) #f.seek(0) #r = requests.post(url, files={'network_cx': f}) r = requests.post(url) ''' r1 = requests.post(url) r2 = requests.post(url) r3 = requests.post(url) r4 = requests.post(url) r5 = requests.post(url) r6 = requests.post(url) r7 = requests.post(url) r8 = requests.post(url) ''' result_json = json.loads(r.content) edge_results = result_json.get("data").get("forward_english") path_scoring = PathScoring() A_all_scores = [] for i, edge in enumerate(edge_results): print len(edge) top_edge = None if i == 24: mystr = "" if i == 14: mystr = "" for ranked_edges in path_scoring.cx_edges_to_tuples(edge, "A"): if top_edge is None: top_edge = ranked_edges else: if ranked_edges[1] < top_edge[1]: top_edge = ranked_edges A_all_scores.append(("A" + str(i), top_edge[1])) #('A0', [('A1', 1)]) print A_all_scores race_results = path_scoring.calculate_average_position(A_all_scores, []) print race_results #print r.content ''' print r1.content print r2.content print r3.content print r4.content print r5.content print r6.content print r7.content print r8.content ''' #lispify_helper = Lispify(results_list) #path_statements = lispify_helper.to_lisp() print results_list
def testConstructor2(self): ndex = nc.Ndex(host=TESTSERVER, update_status=True, debug=True) self.assertTrue( ndex.status.get('properties')['ServerVersion'].startswith("2."))
def load_edgeMatrix(ndex_uuid, ndex_server, ndex_user, ndex_pass, ndex=None, json=None, verbose=True): """Loads a NumPy array from a NdexGraph with a special CX aspect called "edge_matrix". Parameters ---------- ndex_uuid : str NDEx UUID of ontology ndex_server : str URL of NDEx server ndex_user : str NDEx username ndex_pass : str NDEx password json : module JSON module with "loads" function. Default: the simplejson package (must be installed) Returns ------- X : np.ndarray X_cols : list Column names X_rows : list Row names """ if json is None: # import ijson # json = ijson import simplejson json = simplejson if ndex is None: ndex = nc.Ndex(ndex_server, ndex_user, ndex_pass) start = time.time() response = ndex.get_network_as_cx_stream(ndex_uuid) response.raw.decode_content = True try: cx = json.load(response.raw) finally: response.close() if verbose: print('NDEx download and CX parse time (sec):', time.time() - start) start_loop = time.time() for aspect in cx: if 'matrix_cols' in aspect: cols = aspect.get('matrix_cols')[0].get('v') if 'matrix_rows' in aspect: rows = aspect.get('matrix_rows')[0].get('v') if 'matrix_dtype' in aspect: dtype = np.dtype(aspect.get('matrix_dtype')[0].get('v')) dim = (len(rows), len(cols)) #X_buf = bytearray(dim[0] * dim[1] * np.dtype(dtype).itemsize) X_buf = [] pointer = 0 if verbose: print('Dim:', dim) # print('Bytes:', len(X_buf)) print('Bytes:', dim[0] * dim[1] * np.dtype(dtype).itemsize) for aspect in cx: if 'matrix' in aspect: for x in aspect.get('matrix'): if sys.version_info.major==3: binary_data = base64.decodebytes(x.get('v').encode('utf-8')) else: binary_data = base64.b64decode(x.get('v')) del x['v'] X_buf.append(binary_data) # X_buf[pointer : pointer + len(binary_data)] = binary_data # pointer += len(binary_data) X_buf = (b"").join(X_buf) # Create a NumPy array X = np.frombuffer(X_buf, dtype=dtype).reshape(dim) if verbose: print('Iterate through CX and construct array time (sec):', time.time() - start_loop) return X, rows, cols
def test_provenance_and_profile(self): ndex = nc.Ndex(host=tt.TESTSERVER, username=tt.testUser1, password=tt.testUserpasswd, debug=True) with open( path.join(path.abspath(path.dirname(__file__)), example_network_1), 'r') as file_handler: network_in_cx = file_handler.read() # test save_cx_stream_as_new_network test_network_1_uri = ndex.save_cx_stream_as_new_network(network_in_cx) self.assertTrue( test_network_1_uri.startswith(tt.TESTSERVER + ndex_network_resource)) # test get_provenance network_UUID = str(test_network_1_uri.split("/")[-1]) provenance = ndex.get_provenance(network_UUID) self.assertTrue(provenance) provenance_keys = provenance.keys() # check that fields creationEvent, properties and uri are fields in the provenance structure self.assertTrue('creationEvent' in provenance_keys) self.assertTrue('properties' in provenance_keys) self.assertTrue('uri' in provenance_keys) uri1 = str(provenance['uri']) uri2 = tt.TESTSERVER + ndex_network_resource + network_UUID + "/summary" self.assertTrue(uri1 == uri2) # test update_network_profile new_version = "1.55" new_name = "New network name" new_description = "New network description" test_profile = { "version": new_version, "name": new_name, "description": new_description } time.sleep(10) ndex.update_network_profile(network_UUID, test_profile) # test get_network_summary network_summary = ndex.get_network_summary(network_UUID) self.assertTrue(new_version == network_summary['version']) self.assertTrue(new_name == network_summary['name']) self.assertTrue(new_description == network_summary['description']) time.sleep(10) test_profile['visibility'] = 'PUBLIC' with self.assertRaises(Exception) as e: ndex.update_network_profile(network_UUID, test_profile) # errorMessage = str(e) # print errorMessage # self.assertTrue(errorMessage.startswith("Ndex 2.x doesn't support setting visibility by this function.")) provenance_1 = ndex.get_provenance(network_UUID) # test set_provenance - change description of event provenance_1['creationEvent'][ 'eventType'] = 'Modified Program Upload in CX' ndex.set_provenance(network_UUID, provenance_1) provenance_2 = ndex.get_provenance(network_UUID) self.assertTrue(provenance_2['creationEvent']['eventType'] == provenance_1['creationEvent']['eventType']) # set provenance back to the original one ndex.set_provenance(network_UUID, provenance) provenance_3 = ndex.get_provenance(network_UUID) self.assertTrue(provenance['creationEvent']['eventType'] == provenance_3['creationEvent']['eventType']) # test delete_network del_network_return = ndex.delete_network(network_UUID) self.assertTrue(del_network_return == '')
def test_get_user_by_username(self): ndex = nc.Ndex(host=tt.TESTSERVER, username=tt.testUser1, password=tt.testUserpasswd, debug=True) with open( path.join(path.abspath(path.dirname(__file__)), example_network_1), 'r') as file_handler: network_in_cx = file_handler.read() # test save_cx_stream_as_new_network test_network_1_uri = ndex.save_cx_stream_as_new_network(network_in_cx) self.assertTrue( test_network_1_uri.startswith(tt.TESTSERVER + ndex_network_resource)) network_UUID = str(test_network_1_uri.split("/")[-1]) # get network summary network_summary = ndex.get_network_summary(network_UUID) network_visibility = str(network_summary['visibility']) network_read_only = network_summary['isReadOnly'] self.assertTrue(network_visibility.upper() == 'PRIVATE') self.assertTrue(network_read_only == False) # test set_network_system_properties time.sleep(10) # make network ReadOnly and PUBLIC ndex.set_network_system_properties(network_UUID, {'readOnly': True}) ndex.set_network_system_properties(network_UUID, {'visibility': 'PUBLIC'}) # check if we succeeded in making the netwrk ReadOnly and PUBLIC network_summary = ndex.get_network_summary(network_UUID) self.assertTrue(str(network_summary['visibility']).upper() == 'PUBLIC') self.assertTrue(network_summary['isReadOnly'] == True) # make network Read-Write and PRIVATE ndex.set_network_system_properties(network_UUID, {'readOnly': False}) ndex.set_network_system_properties(network_UUID, {'visibility': 'PRIVATE'}) network_summary = ndex.get_network_summary(network_UUID) self.assertTrue( str(network_summary['visibility']).upper() == 'PRIVATE') self.assertTrue(network_summary['isReadOnly'] == False) # make network ReadOnly and PUBLIC in one call ndex.set_network_system_properties(network_UUID, { 'readOnly': True, 'visibility': 'PUBLIC' }) network_summary = ndex.get_network_summary(network_UUID) self.assertTrue(str(network_summary['visibility']).upper() == 'PUBLIC') self.assertTrue(network_summary['isReadOnly'] == True) # make network ReadWrite and PRIVATE in one call ndex.set_network_system_properties(network_UUID, { 'readOnly': False, 'visibility': 'PRIVATE' }) network_summary = ndex.get_network_summary(network_UUID) self.assertTrue( str(network_summary['visibility']).upper() == 'PRIVATE') self.assertTrue(network_summary['isReadOnly'] == False) # make network ReadOnly and PRIVATE in one call ndex.set_network_system_properties(network_UUID, { 'readOnly': True, 'visibility': 'PRIVATE' }) network_summary = ndex.get_network_summary(network_UUID) self.assertTrue( str(network_summary['visibility']).upper() == 'PRIVATE') self.assertTrue(network_summary['isReadOnly'] == True) # make network ReadWrite and PUBLIC in one call ndex.set_network_system_properties(network_UUID, { 'readOnly': False, 'visibility': 'PUBLIC' }) network_summary = ndex.get_network_summary(network_UUID) self.assertTrue(str(network_summary['visibility']).upper() == 'PUBLIC') self.assertTrue(network_summary['isReadOnly'] == False) # test delete_network del_network_return = ndex.delete_network(network_UUID) self.assertTrue(del_network_return == '')
def __init__(self, cx=None, server=None, username=None, password=None, uuid=None, data=None, **attr): '''There are generally four ways to create a graph. 1. An empty graph. G = NdexGraph() 2. Using a cx dictionary. G = NdexGraph(cx) 3. Loading it from an NDEx server. G = NdexGraph(server='http://test.ndexbio.org' uuid='983a2b93-2c55-11e6-a7c5-0630eb0972a1') 4. Just like any other NetworkX MultiDiGraph(). ''' MultiDiGraph.__init__(self, data, **attr) self.subnetwork_id = None self.view_id = None self.max_node_id = None self.max_edge_id = None self.pos = {} self.unknown_cx = [] # Maps edge ids to node ids. e.g. { edge1: (source_node, target_node), edge2: (source_node, target_node) } self.edgemap = {} if not cx and server and uuid: ndex = nc.Ndex(server, username, password) cx = ndex.get_network_as_cx_stream(uuid).json() if not cx: raise RuntimeError("Failed to retrieve network with uuid " + uuid + " from " + server) # If there is no CX to process, just return. if cx == None: return # First pass, get information about subnetworks. for aspect in cx: if 'subNetworks' in aspect: for subnetwork in aspect['subNetworks']: id = subnetwork['@id'] if self.subnetwork_id != None: raise ValueError( "networkn does not support collections!") self.subnetwork_id = id elif 'cyViews' in aspect: for cyViews in aspect['cyViews']: id = cyViews['@id'] if self.view_id != None: raise ValueError( "networkn does not support more than one view!") self.view_id = id elif 'metaData' in aspect: # Strip metaData continue else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Second pass, just build basic graph. self.unknown_cx = [] for aspect in cx: if 'nodes' in aspect: for node in aspect['nodes']: id = node['@id'] name = node['n'] if 'n' in node else None if name: self.add_node(id, name=name) else: self.add_node(id) represents = node['r'] if 'r' in node else None if represents: self.node[id]['represents'] = represents elif 'edges' in aspect: for edge in aspect['edges']: id = edge['@id'] interaction = edge['i'] if 'i' in edge else None s = edge['s'] t = edge['t'] self.edgemap[id] = (s, t) if interaction: self.add_edge(s, t, key=id, interaction=interaction) else: self.add_edge(s, t, key=id) else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Third pass, handle attributes # Notes. Not handled, datatypes. self.unknown_cx = [] for aspect in cx: if 'networkAttributes' in aspect: for networkAttribute in aspect['networkAttributes']: name = networkAttribute['n'] # special: ignore selected if name == 'selected': continue value = networkAttribute['v'] if 'd' in networkAttribute: d = networkAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in networkAttribute or name not in self.graph: self.graph[name] = value elif 'nodeAttributes' in aspect: for nodeAttribute in aspect['nodeAttributes']: id = nodeAttribute['po'] name = nodeAttribute['n'] # special: ignore selected if name == 'selected': continue value = nodeAttribute['v'] if 'd' in nodeAttribute: d = nodeAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in nodeAttribute or name not in self.node[id]: self.node[id][name] = value elif 'edgeAttributes' in aspect: for edgeAttribute in aspect['edgeAttributes']: id = edgeAttribute['po'] s, t = self.edgemap[id] name = edgeAttribute['n'] # special: ignore selected and shared_name columns if name == 'selected' or name == 'shared name': continue value = edgeAttribute['v'] if 'd' in edgeAttribute: d = edgeAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in edgeAttribute or name not in self[s][t][id]: self[s][t][id][name] = value else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Fourth pass, node locations self.pos = {} self.unknown_cx = [] for aspect in cx: if 'cartesianLayout' in aspect: for nodeLayout in aspect['cartesianLayout']: id = nodeLayout['node'] x = nodeLayout['x'] y = nodeLayout['y'] self.pos[id] = np.array([x, y], dtype='float32') else: self.unknown_cx.append(aspect)