aboutsummaryrefslogtreecommitdiff
path: root/wqflask/scripts/index.py
diff options
context:
space:
mode:
authorArun Isaac2022-09-27 22:33:41 +0530
committerArun Isaac2022-09-29 16:15:47 +0530
commitf5442998edbbdcad423b570c69521a4813e74854 (patch)
tree26754bc56dd591a5e6f5cd38dbba226b1f310f0f /wqflask/scripts/index.py
parent55173799cf43660a655f23682d2c7f172cd515f8 (diff)
downloadgenenetwork2-f5442998edbbdcad423b570c69521a4813e74854.tar.gz
Abstract out writing documents into a xapian database.
* wqflask/scripts/index.py (write_document): New function. (main): Use write_document.
Diffstat (limited to 'wqflask/scripts/index.py')
-rw-r--r--wqflask/scripts/index.py25
1 files changed, 11 insertions, 14 deletions
diff --git a/wqflask/scripts/index.py b/wqflask/scripts/index.py
index fe2b819c..f59a8788 100644
--- a/wqflask/scripts/index.py
+++ b/wqflask/scripts/index.py
@@ -19,6 +19,15 @@ def index_text(termgenerator, text):
termgenerator.increase_termpos()
+# pylint: disable=invalid-name
+def write_document(db, idterm, doctype, doc):
+ """Write document into xapian database."""
+ # We use the XT prefix to indicate the type.
+ doc.add_boolean_term(f"XT{doctype}")
+ doc.add_boolean_term(idterm)
+ db.replace_document(idterm, doc)
+
+
# pylint: disable=missing-function-docstring
def main():
with database_connection() as conn, conn.cursor(MonadicDictCursor) as cursor:
@@ -75,14 +84,8 @@ def main():
trait.pop("unigeneid").bind(indexer)
trait.pop("probe_target_description").bind(indexer)
- # Identify document as type "gene". We use the XT
- # prefix to indicate the type.
- doc.add_boolean_term("XTgene")
doc.set_data(json.dumps(trait.data))
- # Write document into xapian database.
- idterm = trait["name"].bind(lambda name: "Q" + name)
- doc.add_boolean_term(idterm)
- db.replace_document(idterm, doc)
+ write_document(db, trait["name"].bind(lambda name: f"Q{name}"), "gene", doc)
cursor.execute("""
SELECT Species.Name AS species,
@@ -133,14 +136,8 @@ def main():
trait["authors"] = trait["authors"].map(
lambda s: [author.strip() for author in s.split(",")])
- # Identify document as type "phenotype". We use the XT
- # prefix to indicate the type.
- doc.add_boolean_term("XTphenotype")
- # Write document into xapian database.
doc.set_data(json.dumps(trait.data))
- idterm = f"Q{i}"
- doc.add_boolean_term(idterm)
- db.replace_document(idterm, doc)
+ write_document(db, f"Q{i}", "phenotype", doc)
if __name__ == "__main__":