|
4 | 4 | import os
|
5 | 5 | import time
|
6 | 6 | import itertools
|
| 7 | +import gzip |
7 | 8 | import sqlite3
|
8 | 9 | from collections import OrderedDict
|
9 | 10 | from future.moves.urllib.parse import urlparse
|
@@ -669,25 +670,30 @@ def annotate_molecular_formulae(peaklist, lib_adducts, ppm, db_out, db_in="http:
|
669 | 670 |
|
670 | 671 | def annotate_compounds(peaklist, lib_adducts, ppm, db_out, db_name, db_in=""):
|
671 | 672 |
|
672 |
| - # TODO: calculate DBE |
673 |
| - |
674 | 673 | if db_in is None or db_in == "":
|
675 | 674 | path_dbs = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'databases')
|
676 | 675 | conn_local = None
|
677 | 676 | for db_local in os.listdir(path_dbs):
|
678 |
| - if db_name == db_local.strip(".sqlite"): |
679 |
| - conn_local = sqlite3.connect(os.path.join(path_dbs, db_local)) |
680 |
| - cursor_local = conn_local.cursor() |
681 |
| - cursor_local.execute("SELECT name FROM sqlite_master WHERE type='table'") |
682 |
| - if (db_name.strip(".sqlite"), ) not in cursor_local.fetchall(): |
683 |
| - raise ValueError("Database {} not available".format(db_name)) |
684 |
| - break |
| 677 | + if db_name == db_local.strip(".sql.gz"): |
| 678 | + |
| 679 | + with gzip.GzipFile(os.path.join(path_dbs, db_local), mode='rb') as db_dump: |
| 680 | + |
| 681 | + conn_local = sqlite3.connect(":memory:") |
| 682 | + cursor_local = conn_local.cursor() |
| 683 | + cursor_local.executescript(db_dump.read().decode('utf-8')) |
| 684 | + conn_local.commit() |
| 685 | + |
| 686 | + cursor_local.execute("SELECT name FROM sqlite_master WHERE type='table'") |
| 687 | + if (db_name.strip(".sql.gz"), ) not in cursor_local.fetchall(): |
| 688 | + raise ValueError("Database {} not available".format(db_name)) |
| 689 | + break |
| 690 | + |
685 | 691 | if conn_local is None:
|
686 | 692 | raise ValueError("Database {} not available".format(db_name))
|
687 | 693 |
|
688 | 694 | elif os.path.isfile(db_in):
|
689 | 695 | with open(db_in, 'rb') as fd:
|
690 |
| - if fd.read(100)[:16] == 'SQLite format 3\x00': |
| 696 | + if fd.read(100)[:16].decode() == 'SQLite format 3\x00': |
691 | 697 | conn_local = sqlite3.connect(db_in)
|
692 | 698 | cursor_local = conn_local.cursor()
|
693 | 699 | cursor_local.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
|
0 commit comments