From 6ec8a5d1de6b6f0f84a76d0d52c77f3d96420479 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 13 Apr 2021 08:21:15 +0300 Subject: Add method for inserting publications --- gn3/db/traits.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/db/traits.py b/gn3/db/traits.py index b0d1831..5ac342f 100644 --- a/gn3/db/traits.py +++ b/gn3/db/traits.py @@ -1,7 +1,7 @@ """This contains all the necessary functions that are required to add traits to the published database""" from collections import namedtuple -from typing import Any +from typing import Any, Dict, Optional riset = namedtuple('riset', ['name', 'id']) @@ -29,3 +29,22 @@ def get_riset(data_type: str, name: str, conn: Any): if _name == "BXD300": _name = "BXD" return riset(_name, _id) + + +def insert_publication(pubmed_id: int, publication: Optional[Dict], + conn: Any): + """Creates a new publication record if it's not available""" + sql = ("SELECT Id FROM Publication where " + "PubMed_ID = %d" % pubmed_id) + _id = None + with conn.cursor() as cursor: + cursor.execute(sql) + _id = cursor.fetchone() + if not _id: + # The Publication contains the fields: 'authors', 'title', 'abstract', + # 'journal','volume','pages','month','year' + insert_query = ("INSERT into Publication (%s) Values (%s)" % + (", ".join(publication.keys()), + ", ".join(['%s'] * len(publication)))) + with conn.cursor() as cursor: + cursor.execute(insert_query, tuple(publication.values())) -- cgit v1.2.3