1
+ import collections
1
2
import logging
2
3
import sqlite3
3
4
import pathlib
@@ -13,14 +14,27 @@ def dict_factory(cursor, row):
13
14
return {key : value for key , value in zip (col_names , row )}
14
15
15
16
16
- class MetadataDb :
17
+ class MetadataDb ( collections . abc . Mapping ) :
17
18
def __init__ (self , path ):
18
19
uri = f"file:{ path } "
19
20
uri += "?mode=ro"
20
21
self .uri = uri
21
22
self .conn = sqlite3 .connect (uri , uri = True )
22
23
self .conn .row_factory = dict_factory
23
24
25
+ @staticmethod
26
+ def import_csv (csv_path , db_path ):
27
+ df = pd .read_csv (csv_path , sep = "\t " )
28
+ db_path = pathlib .Path (db_path )
29
+ if db_path .exists ():
30
+ db_path .unlink ()
31
+ with sqlite3 .connect (db_path ) as conn :
32
+ df .to_sql ("samples" , conn , index = False )
33
+ conn .execute (
34
+ "CREATE UNIQUE INDEX [ix_samples_strain] on 'samples' ([strain]);"
35
+ )
36
+ conn .execute ("CREATE INDEX [ix_samples_date] on 'samples' ([date]);" )
37
+
24
38
def __enter__ (self ):
25
39
return self
26
40
@@ -36,23 +50,23 @@ def __len__(self):
36
50
row = self .conn .execute (sql ).fetchone ()
37
51
return row ["COUNT(*)" ]
38
52
53
+ def __getitem__ (self , key ):
54
+ sql = "SELECT * FROM samples WHERE strain==?"
55
+ with self .conn :
56
+ result = self .conn .execute (sql , [key ]).fetchone ()
57
+ if result is None :
58
+ raise KeyError (f"strain { key } not in DB" )
59
+ return result
60
+
61
+ def __iter__ (self ):
62
+ sql = "SELECT strain FROM samples"
63
+ with self .conn :
64
+ for result in self .conn .execute (sql ):
65
+ yield result ["strain" ]
66
+
39
67
def close (self ):
40
68
self .conn .close ()
41
69
42
- @staticmethod
43
- def import_csv (csv_path , db_path ):
44
- df = pd .read_csv (
45
- csv_path ,
46
- sep = "\t " ,
47
- )
48
- db_path = pathlib .Path (db_path )
49
- if db_path .exists ():
50
- db_path .unlink ()
51
- with sqlite3 .connect (db_path ) as conn :
52
- df .to_sql ("samples" , conn , index = False )
53
- conn .execute ("CREATE INDEX [ix_samples_strain] on 'samples' ([strain]);" )
54
- conn .execute ("CREATE INDEX [ix_samples_date] on 'samples' ([date]);" )
55
-
56
70
def get (self , date ):
57
71
sql = "SELECT * FROM samples WHERE date==?"
58
72
with self .conn :
0 commit comments