Skip to content

Commit f47d5cf

Browse files
committed
Update compound annotation (sqlite in memory)
1 parent 7dcd091 commit f47d5cf

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

beams/annotation.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import time
66
import itertools
7+
import gzip
78
import sqlite3
89
from collections import OrderedDict
910
from future.moves.urllib.parse import urlparse
@@ -669,25 +670,30 @@ def annotate_molecular_formulae(peaklist, lib_adducts, ppm, db_out, db_in="http:
669670

670671
def annotate_compounds(peaklist, lib_adducts, ppm, db_out, db_name, db_in=""):
671672

672-
# TODO: calculate DBE
673-
674673
if db_in is None or db_in == "":
675674
path_dbs = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'databases')
676675
conn_local = None
677676
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+
685691
if conn_local is None:
686692
raise ValueError("Database {} not available".format(db_name))
687693

688694
elif os.path.isfile(db_in):
689695
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':
691697
conn_local = sqlite3.connect(db_in)
692698
cursor_local = conn_local.cursor()
693699
cursor_local.execute("SELECT name FROM sqlite_master WHERE type='table'")

0 commit comments

Comments
 (0)