From a9dc52644c5e5bc925a0f5fcc17480bae293d516 Mon Sep 17 00:00:00 2001 From: Gianmauro Cuccuru Date: Mon, 21 Oct 2024 21:59:57 +0200 Subject: [PATCH] add meta-query command --- gwasstudio/cli/metadata/query.py | 25 +++++++++++++++++++++++++ gwasstudio/main.py | 2 ++ gwasstudio/mongo/mixin.py | 16 ++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 gwasstudio/cli/metadata/query.py diff --git a/gwasstudio/cli/metadata/query.py b/gwasstudio/cli/metadata/query.py new file mode 100644 index 0000000..c66957d --- /dev/null +++ b/gwasstudio/cli/metadata/query.py @@ -0,0 +1,25 @@ +import cloup + +from gwasstudio.mongo.models import EnhancedDataProfile + +help_doc = """ +Metadata query +""" + + +@cloup.command("meta_query", no_args_is_help=True, help=help_doc) +@cloup.option( + "--key", + default=None, + help="query key", +) +@cloup.option( + "--value", + default=None, + help="query value", +) +def meta_query(key, value): + obj = EnhancedDataProfile() + objs = obj.query(**{key: value}) + print(objs) + pass diff --git a/gwasstudio/main.py b/gwasstudio/main.py index 77605d2..ddc51b4 100644 --- a/gwasstudio/main.py +++ b/gwasstudio/main.py @@ -6,6 +6,7 @@ # from gwasstudio.cli.export import export from gwasstudio.cli.info import info from gwasstudio.cli.metadata.ingest import meta_ingest +from gwasstudio.cli.metadata.query import meta_query from gwasstudio.cli.metadata.view import meta_view # from gwasstudio.cli.ingest import ingest @@ -92,6 +93,7 @@ def main(): # cli_init.add_command(ingest) cli_init.add_command(meta_ingest) cli_init.add_command(meta_view) + cli_init.add_command(meta_query) logger.info("{} started".format(__appname__.capitalize())) cli_init(obj={}) diff --git a/gwasstudio/mongo/mixin.py b/gwasstudio/mongo/mixin.py index c3a8a50..0848a1b 100644 --- a/gwasstudio/mongo/mixin.py +++ b/gwasstudio/mongo/mixin.py @@ -66,6 +66,22 @@ def view(self): logger.debug(detail) return detail + def query(self, **kwargs): + """ + + :param kwargs: + :return: + """ + docs = [] + if len(kwargs) > 0: + with self.mec: + if "trait_desc" in kwargs.keys(): + docs = self.klass.objects(trait_desc__contains=kwargs.get("trait_desc")).as_pymongo() + else: + docs = self.klass.objects(**kwargs).as_pymongo() + logger.debug("found {} documents".format(len(docs))) + return docs + def modify(self, **kwargs): """ Perform an atomic update of the document in the database and