diff options
-rw-r--r-- | gn3/auth/authorisation/data/views.py | 18 | ||||
-rw-r--r-- | scripts/search_phenotypes.py | 19 |
2 files changed, 26 insertions, 11 deletions
diff --git a/gn3/auth/authorisation/data/views.py b/gn3/auth/authorisation/data/views.py index d84b080..6431389 100644 --- a/gn3/auth/authorisation/data/views.py +++ b/gn3/auth/authorisation/data/views.py @@ -175,7 +175,7 @@ def __search_genotypes__(): limit=limit, offset=offset) return jsonify(with_db_connection(__ungrouped__)) -def __search_phenotypes__(): +def __search_xapian__(): # launch the external process to search for phenotypes redisuri = app.config["REDIS_URI"] with redis.Redis.from_url(redisuri, decode_responses=True) as redisconn: @@ -184,6 +184,7 @@ def __search_phenotypes__(): command =[ sys.executable, "-m", "scripts.search_phenotypes", __request_key__("species_name"), + __request_key__("dataset_type"), __request_key__("query"), str(job_id), f"--host={__request_key__('gn3_server_uri')}", @@ -193,6 +194,10 @@ def __search_phenotypes__(): f"--per-page={__request_key__('per_page')}"] +( [f"--selected={json.dumps(selected)}"] if len(selected) > 0 else []) + print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++") + import shlex + print(shlex.join(command)) + print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++") jobs.create_job(redisconn, { "job_id": job_id, "command": command, "status": "queued", "search_results": tuple()}) @@ -203,20 +208,21 @@ def __search_phenotypes__(): "command": command }) -@data.route("/search", methods=["GET"]) +@data.route("/search", methods=["POST"]) @require_oauth("profile group resource") +@require_json def search_unlinked_data(): """Search for various unlinked data.""" dataset_type = request.json["dataset_type"] search_fns = { "mrna": __search_mrna__, - "genotype": __search_genotypes__, - "phenotype": __search_phenotypes__ + "genotype": __search_xapian__, + "phenotype": __search_xapian__ } return search_fns[dataset_type]() -@data.route("/search/phenotype/<uuid:job_id>", methods=["GET"]) -def pheno_search_results(job_id: uuid.UUID) -> Response: +@data.route("/search/results/<uuid:job_id>", methods=["GET"]) +def search_job_results(job_id: uuid.UUID) -> Response: """Get the search results from the external script""" def __search_error__(err): raise NotFoundError(err["error_description"]) diff --git a/scripts/search_phenotypes.py b/scripts/search_phenotypes.py index 38b992b..1e8d0fc 100644 --- a/scripts/search_phenotypes.py +++ b/scripts/search_phenotypes.py @@ -22,10 +22,16 @@ class NoSearchResults(Exception): """Raise when there are no results for a search.""" def do_search( - host: str, query: str, per_page: int, page: int = 1) -> Iterable[dict[str, Any]]: + dataset_type: str, host: str, query: str, per_page: int, + page: int = 1) -> Iterable[dict[str, Any]]: """Do the search and return the results""" + search_types = { + "phenotype": "phenotype", + "genotype": "gene" + } search_uri = urljoin(host, (f"search/?page={page}&per_page={per_page}" - f"&type=phenotype&query={query}")) + f"&type={search_types[dataset_type]}" + f"&query={query}")) response = requests.get(search_uri) results = response.json() if len(results) > 0: @@ -61,6 +67,7 @@ def expire_redis_results(redisconn: redis.Redis, redisname: str): @click.command() @click.argument("species") +@click.argument("dataset_type") @click.argument("query") @click.argument("job-id", type=click.UUID) @click.option( @@ -76,8 +83,9 @@ def expire_redis_results(redisconn: redis.Redis, redisname: str): "--redis-uri", default="redis://:@localhost:6379/0", help="The URI to the redis server.") def search(# pylint: disable=[too-many-arguments, too-many-locals] - species: str, query: str, job_id: uuid.UUID, host: str, per_page: int, - selected: str, auth_db_uri: str, gn3_db_uri: str, redis_uri: str): + species: str, dataset_type: str, query: str, job_id: uuid.UUID, + host: str, per_page: int, selected: str, auth_db_uri: str, + gn3_db_uri: str, redis_uri: str): """ Search for phenotype traits, filtering out any linked and selected traits, loading more and more pages until the `per_page` quota is fulfilled or the @@ -104,7 +112,8 @@ def search(# pylint: disable=[too-many-arguments, too-many-locals] while count < per_page: results = tuple(remove_linked( remove_selected( - do_search(host, search_query, per_page, page), + do_search( + dataset_type, host, search_query, per_page, page), selected_traits), linked))[0:per_page-count] count = count + len(results) |