# Path: epitator/get_database_connection.py
# def get_database_connection(create_database=False):
#     databse_exists = os.path.exists(ANNOTATOR_DB_PATH)
#     if databse_exists or create_database:
#         if not databse_exists:
#             print("Creating database at:", ANNOTATOR_DB_PATH)
#         connection = sqlite3.connect(ANNOTATOR_DB_PATH)
#         cur = connection.cursor()
#         cur.execute("PRAGMA foreign_keys = ON")
#         cur.execute("""
#         CREATE TABLE IF NOT EXISTS metadata (
#             property TEXT PRIMARY KEY ASC, value TEXT
#         )""")
#         if not databse_exists:
#             # Initialize the database
#             cur.execute("""
#             CREATE TABLE entities (
#                 id TEXT PRIMARY KEY, label TEXT, type TEXT, source TEXT
#             )""")
#             cur.execute("""
#             CREATE TABLE synonyms (
#                 synonym TEXT,
#                 entity_id TEXT REFERENCES entities(id),
#                 weight INTEGER
#             )""")
#             cur.execute('''
#             CREATE INDEX synonym_index ON synonyms (synonym);
#             ''')
#             cur.execute("INSERT INTO metadata VALUES ('dbversion', '0.0.1')")
#             connection.commit()
#         db_version = next(cur.execute("""
#         SELECT value AS version FROM metadata WHERE property = 'dbversion'
#         """), None)
#         if not db_version or db_version[0] != "0.0.1":
#             raise Exception("The database at " + ANNOTATOR_DB_PATH +
#                             " has a version that is not compatible by this version of EpiTator.\n"
#                             "You will need to rerun the data import scripts.")
#         return connection
#     else:
#         raise Exception("There is no EpiTator database at: " + ANNOTATOR_DB_PATH +
#                         "\nRun `python -m epitator.importers.import_all` to create a new database"
#                         "\nor set ANNOTATOR_DB_PATH to use a database at a different location.")
# 
# Path: epitator/utils.py
# def normalize_disease_name(name):
#     if len(name) <= 6:
#         # Shorter syn_strings are likely to be acronyms so only exact
#         # matches are allowed.
#         return name
#     else:
#         name = re.sub(r"['\"]", "", name)
#         return re.sub(r"[\s\-\/]+", " ", name).strip()


from ..get_database_connection import get_database_connection
from six.moves.urllib.request import urlopen
from six.moves.urllib.parse import urlencode
from six.moves.urllib.error import URLError
from ..utils import normalize_disease_name
import six
import json
import datetime
    import argparse
            """
        })))
    except URLError:
        print("If you are operating behind a firewall, try setting the HTTP_PROXY/HTTPS_PROXY environment variables.")
        raise
    results = json.loads(six.text_type(response.read(), 'utf-8'))['results']['bindings']
    print("Importing %s wikidata entities..." % len(results))
    cur.executemany("INSERT INTO entities VALUES (?, ?, 'disease', 'Wikidata')", [
        (result['item']['value'], result['itemLabel']['value'])
        for result in results])
    cur.executemany("INSERT INTO synonyms VALUES (?, ?, 1)", [
        (normalize_disease_name(result['itemLabel']['value']), result['item']['value'])
        for result in results])
    print("Importing manually added diseases not in disease ontology...")
    # Wikidata entities are used in place of those from the disease ontology.
    additional_diseases = [
        ('https://www.wikidata.org/wiki/Q16654806', 'Middle East respiratory syndrome',),
        ('https://www.wikidata.org/wiki/Q6946', 'Cytomegalovirus'),
        ('https://www.wikidata.org/wiki/Q1142751', 'Norovirus',),
        ('https://www.wikidata.org/wiki/Q15928531', 'Nipah virus',),
        ('https://www.wikidata.org/wiki/Q18350119', 'Acute flaccid myelitis',),
        ('https://www.wikidata.org/wiki/Q6163830', 'Seoul virus',),
        ('https://www.wikidata.org/wiki/Q101896', 'Gonorrhoea'),
        ('https://www.wikidata.org/wiki/Q62658688', 'Rotavirus Disease'),
        ('https://www.wikidata.org/wiki/Q2600216', 'Yersiniosis'),
        ('https://www.wikidata.org/wiki/Q19000403', 'Enterovirus infectious disease'),
        ('https://www.wikidata.org/wiki/Q837054', 'Adenovirus infection'),
    ]
    cur.executemany("INSERT INTO entities VALUES (?, ?, 'disease', 'Wikidata')", additional_diseases)
    cur.executemany("INSERT INTO synonyms VALUES (?, ?, 3)", [

# Path: epitator/get_database_connection.py
# def get_database_connection(create_database=False):
#     databse_exists = os.path.exists(ANNOTATOR_DB_PATH)
#     if databse_exists or create_database:
#         if not databse_exists:
#             print("Creating database at:", ANNOTATOR_DB_PATH)
#         connection = sqlite3.connect(ANNOTATOR_DB_PATH)
#         cur = connection.cursor()
#         cur.execute("PRAGMA foreign_keys = ON")
#         cur.execute("""
#         CREATE TABLE IF NOT EXISTS metadata (
#             property TEXT PRIMARY KEY ASC, value TEXT
#         )""")
#         if not databse_exists:
#             # Initialize the database
#             cur.execute("""
#             CREATE TABLE entities (
#                 id TEXT PRIMARY KEY, label TEXT, type TEXT, source TEXT
#             )""")
#             cur.execute("""
#             CREATE TABLE synonyms (
#                 synonym TEXT,
#                 entity_id TEXT REFERENCES entities(id),
#                 weight INTEGER
#             )""")
#             cur.execute('''
#             CREATE INDEX synonym_index ON synonyms (synonym);
#             ''')
#             cur.execute("INSERT INTO metadata VALUES ('dbversion', '0.0.1')")
#             connection.commit()
#         db_version = next(cur.execute("""
#         SELECT value AS version FROM metadata WHERE property = 'dbversion'
#         """), None)
#         if not db_version or db_version[0] != "0.0.1":
#             raise Exception("The database at " + ANNOTATOR_DB_PATH +
#                             " has a version that is not compatible by this version of EpiTator.\n"
#                             "You will need to rerun the data import scripts.")
#         return connection
#     else:
#         raise Exception("There is no EpiTator database at: " + ANNOTATOR_DB_PATH +
#                         "\nRun `python -m epitator.importers.import_all` to create a new database"
#                         "\nor set ANNOTATOR_DB_PATH to use a database at a different location.")
# 
# Path: epitator/utils.py
# def normalize_disease_name(name):
#     if len(name) <= 6:
#         # Shorter syn_strings are likely to be acronyms so only exact
#         # matches are allowed.
#         return name
#     else:
#         name = re.sub(r"['\"]", "", name)
#         return re.sub(r"[\s\-\/]+", " ", name).strip()


# Path: epitator/importers/import_wikidata.py
from ..get_database_connection import get_database_connection
from six.moves.urllib.request import urlopen
from six.moves.urllib.parse import urlencode
from six.moves.urllib.error import URLError
from ..utils import normalize_disease_name
import six
import json
import datetime
    import argparse

            """
        })))
    except URLError:
        print("If you are operating behind a firewall, try setting the HTTP_PROXY/HTTPS_PROXY environment variables.")
        raise
    results = json.loads(six.text_type(response.read(), 'utf-8'))['results']['bindings']
    print("Importing %s wikidata entities..." % len(results))
    cur.executemany("INSERT INTO entities VALUES (?, ?, 'disease', 'Wikidata')", [
        (result['item']['value'], result['itemLabel']['value'])
        for result in results])
    cur.executemany("INSERT INTO synonyms VALUES (?, ?, 1)", [
        (normalize_disease_name(result['itemLabel']['value']), result['item']['value'])
        for result in results])
    print("Importing manually added diseases not in disease ontology...")
    # Wikidata entities are used in place of those from the disease ontology.
    additional_diseases = [
        ('https://www.wikidata.org/wiki/Q16654806', 'Middle East respiratory syndrome',),
        ('https://www.wikidata.org/wiki/Q6946', 'Cytomegalovirus'),
        ('https://www.wikidata.org/wiki/Q1142751', 'Norovirus',),
        ('https://www.wikidata.org/wiki/Q15928531', 'Nipah virus',),
        ('https://www.wikidata.org/wiki/Q18350119', 'Acute flaccid myelitis',),
        ('https://www.wikidata.org/wiki/Q6163830', 'Seoul virus',),
        ('https://www.wikidata.org/wiki/Q101896', 'Gonorrhoea'),
        ('https://www.wikidata.org/wiki/Q62658688', 'Rotavirus Disease'),
        ('https://www.wikidata.org/wiki/Q2600216', 'Yersiniosis'),
        ('https://www.wikidata.org/wiki/Q19000403', 'Enterovirus infectious disease'),
        ('https://www.wikidata.org/wiki/Q837054', 'Adenovirus infection'),
    ]
    cur.executemany("INSERT INTO entities VALUES (?, ?, 'disease', 'Wikidata')", additional_diseases)
    cur.executemany("INSERT INTO synonyms VALUES (?, ?, 3)", [

        (normalize_disease_name(disease_name), uri,) for uri, disease_name in additional_diseases])