def _query_and_parameters(self): """ A tuple of the Cypher query and parameters used to select the nodes that match the criteria for this selection. :return: Cypher query string """ clauses = [ "MATCH (_%s)" % "".join(":%s" % cypher_escape(label) for label in self._labels) ] parameters = {} if self._conditions: conditions = [] for condition in self._conditions: if isinstance(condition, tuple): condition, param = condition parameters.update(param) conditions.append(condition) clauses.append("WHERE %s" % " AND ".join(conditions)) clauses.append("RETURN _") if self._order_by: clauses.append("ORDER BY %s" % (", ".join(self._order_by))) if self._skip: clauses.append("SKIP %d" % self._skip) if self._limit is not None: clauses.append("LIMIT %d" % self._limit) return " ".join(clauses), parameters
def __db_create__(self, tx): from py2neo.database.cypher import cypher_escape nodes = list(self.nodes()) reads = [] writes = [] parameters = {} returns = {} for i, node in enumerate(nodes): node_id = "a%d" % i param_id = "x%d" % i remote_node = remote(node) if remote_node: reads.append("MATCH (%s) WHERE id(%s)={%s}" % (node_id, node_id, param_id)) parameters[param_id] = remote_node._id else: label_string = "".join(":" + cypher_escape(label) for label in sorted(node.labels())) writes.append("CREATE (%s%s {%s})" % (node_id, label_string, param_id)) parameters[param_id] = dict(node) node._set_remote_pending(tx) returns[node_id] = node for i, relationship in enumerate(self.relationships()): if not remote(relationship): rel_id = "r%d" % i start_node_id = "a%d" % nodes.index(relationship.start_node()) end_node_id = "a%d" % nodes.index(relationship.end_node()) type_string = cypher_escape(relationship.type()) param_id = "y%d" % i writes.append("CREATE UNIQUE (%s)-[%s:%s]->(%s) SET %s={%s}" % (start_node_id, rel_id, type_string, end_node_id, rel_id, param_id)) parameters[param_id] = dict(relationship) returns[rel_id] = relationship relationship._set_remote_pending(tx) statement = "\n".join(reads + writes + ["RETURN %s LIMIT 1" % ", ".join(returns)]) tx.entities.append(returns) tx.run(statement, parameters)
def _property_equality_conditions(properties, offset=1): for i, (key, value) in enumerate(properties.items(), start=offset): if key == "__id__": condition = "id(_)" else: condition = "_.%s" % cypher_escape(key) if isinstance(value, (tuple, set, frozenset)): condition += " IN {%d}" % i parameters = {"%d" % i: list(value)} else: condition += " = {%d}" % i parameters = {"%d" % i: value} yield condition, parameters
def __db_merge__(self, tx, primary_label=None, primary_key=None): from py2neo.database.cypher import cypher_escape, cypher_repr nodes = list(self.nodes()) match_clauses = [] merge_clauses = [] parameters = {} returns = {} for i, node in enumerate(nodes): node_id = "a%d" % i param_id = "x%d" % i remote_node = remote(node) if remote_node: match_clauses.append("MATCH (%s) WHERE id(%s)={%s}" % (node_id, node_id, param_id)) parameters[param_id] = remote_node._id else: merge_label = getattr(node, "__primarylabel__", None) or primary_label if merge_label is None: label_string = "".join(":" + cypher_escape(label) for label in sorted(node.labels())) elif node.labels(): label_string = ":" + cypher_escape(merge_label) else: label_string = "" merge_keys = getattr(node, "__primarykey__", None) or primary_key if merge_keys is None: merge_keys = () elif is_collection(merge_keys): merge_keys = tuple(merge_keys) else: merge_keys = (merge_keys, ) if merge_keys: property_map_string = cypher_repr({ k: v for k, v in dict(node).items() if k in merge_keys }) else: property_map_string = cypher_repr(dict(node)) merge_clauses.append( "MERGE (%s%s %s)" % (node_id, label_string, property_map_string)) if node.labels(): merge_clauses.append( "SET %s%s" % (node_id, "".join(":" + cypher_escape(label) for label in sorted(node.labels())))) if merge_keys: merge_clauses.append("SET %s={%s}" % (node_id, param_id)) parameters[param_id] = dict(node) node._set_remote_pending(tx) returns[node_id] = node clauses = match_clauses + merge_clauses for i, relationship in enumerate(self.relationships()): if not remote(relationship): rel_id = "r%d" % i start_node_id = "a%d" % nodes.index(relationship.start_node()) end_node_id = "a%d" % nodes.index(relationship.end_node()) type_string = cypher_escape(relationship.type()) param_id = "y%d" % i clauses.append("MERGE (%s)-[%s:%s]->(%s) SET %s={%s}" % (start_node_id, rel_id, type_string, end_node_id, rel_id, param_id)) parameters[param_id] = dict(relationship) returns[rel_id] = relationship relationship._set_remote_pending(tx) statement = "\n".join(clauses + ["RETURN %s LIMIT 1" % ", ".join(returns)]) tx.entities.append(returns) tx.run(statement, parameters)
def process_results(gearman_worker, gearman_job): disease = gearman_job.data['disease'] node_label = gearman_job.data['node_label'] retstart = gearman_job.data['retstart'] retmax = gearman_job.data['retmax'] results = search_field("\"{}\"".format(disease), retstart, retmax) id_list = results['IdList'] papers = fetch_details(id_list) countries = dict() driver = GraphDatabase.driver(neo4j_url, auth=basic_auth(neo4j_user, neo4j_password)) session = driver.session() for i, paper in enumerate(papers): if 'MedlineCitation' in paper.keys(): if 'Article' in paper['MedlineCitation'].keys(): if 'AuthorList' in paper['MedlineCitation']['Article'].keys(): author_list = ( paper['MedlineCitation']['Article']['AuthorList']) authors = Set() for index in range(0, min(len(author_list), 12)): a = author_list[index] name = '' if 'LastName' in a.keys() and 'ForeName' in a.keys(): name = "{}_{}".format( a['ForeName'].encode('utf-8').replace( ' ', '_'), a['LastName'].encode('utf-8').replace(' ', '')) authors.add(name) if (name not in countries.keys() or countries[name] == "Not found"): if 'AffiliationInfo' in a.keys(): if len(a['AffiliationInfo']) > 0: if ('Affiliation' in a['AffiliationInfo'][0].keys()): s_country = (find_between_r( (a['AffiliationInfo'][0] ['Affiliation']), ',', '.')) if 'China' in s_country: s_country = 'China' if 'United States' in s_country: s_country = 'USA' countries[name] = s_country else: countries[name] = "Not found" authors = list(authors) # create network for i in range(len(authors)): u = authors[i] for j in range(i + 1, len(authors)): v = authors[j] session.run("MERGE (p1:`{node}` {{name:\"{}\"}}) " "MERGE (p2:`{node}` {{name:\"{}\"}}) " "CREATE (p1)-[:KNOWS]->(p2)".format( cypher_escape(u).encode('utf-8'), cypher_escape(v).encode('utf-8'), node=node_label)) session.close() return countries
def __db_merge__(self, tx, primary_label=None, primary_key=None): from py2neo.database.cypher import cypher_escape, cypher_repr nodes = list(self.nodes()) match_clauses = [] merge_clauses = [] parameters = {} returns = {} for i, node in enumerate(nodes): node_id = "a%d" % i param_id = "x%d" % i remote_node = remote(node) if remote_node: match_clauses.append("MATCH (%s) WHERE id(%s)={%s}" % (node_id, node_id, param_id)) parameters[param_id] = remote_node._id else: merge_label = getattr(node, "__primarylabel__", None) or primary_label if merge_label is None: label_string = "".join(":" + cypher_escape(label) for label in sorted(node.labels())) elif node.labels(): label_string = ":" + cypher_escape(merge_label) else: label_string = "" merge_keys = getattr(node, "__primarykey__", None) or primary_key if merge_keys is None: merge_keys = () elif is_collection(merge_keys): merge_keys = tuple(merge_keys) else: merge_keys = (merge_keys,) if merge_keys: property_map_string = cypher_repr({k: v for k, v in dict(node).items() if k in merge_keys}) else: property_map_string = cypher_repr(dict(node)) merge_clauses.append("MERGE (%s%s %s)" % (node_id, label_string, property_map_string)) if node.labels(): merge_clauses.append("SET %s%s" % ( node_id, "".join(":" + cypher_escape(label) for label in sorted(node.labels())))) if merge_keys: merge_clauses.append("SET %s={%s}" % (node_id, param_id)) parameters[param_id] = dict(node) node._set_remote_pending(tx) returns[node_id] = node clauses = match_clauses + merge_clauses for i, relationship in enumerate(self.relationships()): if not remote(relationship): rel_id = "r%d" % i start_node_id = "a%d" % nodes.index(relationship.start_node()) end_node_id = "a%d" % nodes.index(relationship.end_node()) type_string = cypher_escape(relationship.type()) param_id = "y%d" % i clauses.append("MERGE (%s)-[%s:%s]->(%s) SET %s={%s}" % (start_node_id, rel_id, type_string, end_node_id, rel_id, param_id)) parameters[param_id] = dict(relationship) returns[rel_id] = relationship relationship._set_remote_pending(tx) statement = "\n".join(clauses + ["RETURN %s LIMIT 1" % ", ".join(returns)]) tx.entities.append(returns) tx.run(statement, parameters)