-
Notifications
You must be signed in to change notification settings - Fork 77
/
manifest.py
345 lines (274 loc) · 11 KB
/
manifest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
Manifests for collections of signatures.
"""
import csv
import ast
import gzip
import os.path
from abc import abstractmethod
import itertools
from sourmash.picklist import SignaturePicklist
class BaseCollectionManifest:
"""
Signature metadata for a collection of signatures.
Manifests support selection and rapid lookup of signatures.
* 'select_to_manifest(...)' matches the Index selector protocol
* 'rows' is a public iterable that can be used to iterate over the manifest
contents.
* 'locations()' returns all distinct locations for e.g. lazy loading
* supports container protocol for signatures, e.g. 'if ss in manifest: ...'
"""
# each manifest row must have the following, although they may be empty.
required_keys = ('internal_location',
'md5', 'md5short', 'ksize', 'moltype', 'num',
'scaled', 'n_hashes', 'with_abundance',
'name', 'filename')
@classmethod
@abstractmethod
def load_from_manifest(cls, manifest, **kwargs):
"Load this manifest from another manifest object."
@classmethod
def load_from_filename(cls, filename):
# SQLite db?
db = cls.load_from_sql(filename)
if db is not None:
return db
# not a SQLite db? CTB: fix this to actually try loading this as .gz...
if filename.endswith('.gz'):
xopen = gzip.open
else:
xopen = open
with xopen(filename, 'rt', newline="") as fp:
return cls.load_from_csv(fp)
@classmethod
def load_from_csv(cls, fp):
"load a manifest from a CSV file."
manifest_list = []
firstline = fp.readline().rstrip()
if not firstline.startswith('# SOURMASH-MANIFEST-VERSION: '):
raise ValueError("manifest is missing version header")
version = firstline[len('# SOURMASH-MANIFEST-VERSION: '):]
if float(version) != 1.0:
raise ValueError(f"unknown manifest version number {version}")
r = csv.DictReader(fp)
if not r.fieldnames:
raise ValueError("missing column headers in manifest")
for k in cls.required_keys:
if k not in r.fieldnames:
raise ValueError(f"missing column '{k}' in manifest.")
row = None
# do row type conversion
introws = ('num', 'scaled', 'ksize', 'n_hashes')
boolrows = ('with_abundance',)
for row in r:
for k in introws:
row[k] = int(row[k])
for k in boolrows:
row[k] = bool(ast.literal_eval(str(row[k])))
row['signature'] = None
manifest_list.append(row)
return CollectionManifest(manifest_list)
@classmethod
def load_from_sql(cls, filename):
from sourmash.index.sqlite_index import load_sqlite_index
db = load_sqlite_index(filename, request_manifest=True)
if db is not None:
return db.manifest
return None
def write_to_filename(self, filename, *, database_format='csv',
ok_if_exists=False):
if database_format == 'csv':
from .sourmash_args import FileOutputCSV
if ok_if_exists or not os.path.exists(filename):
with FileOutputCSV(filename) as fp:
return self.write_to_csv(fp, write_header=True)
elif os.path.exists(filename) and not ok_if_exists:
raise Exception("output manifest already exists")
elif database_format == 'sql':
from sourmash.index.sqlite_index import SqliteCollectionManifest
SqliteCollectionManifest.load_from_manifest(self, dbfile=filename,
append=ok_if_exists)
@classmethod
def write_csv_header(cls, fp):
"write header for manifest CSV format"
fp.write('# SOURMASH-MANIFEST-VERSION: 1.0\n')
w = csv.DictWriter(fp, fieldnames=cls.required_keys)
w.writeheader()
def write_to_csv(self, fp, write_header=False):
"write manifest CSV to specified file handle"
w = csv.DictWriter(fp, fieldnames=self.required_keys,
extrasaction='ignore')
if write_header:
self.write_csv_header(fp)
for row in self.rows:
# don't write signature!
if 'signature' in row:
del row['signature']
w.writerow(row)
@classmethod
def make_manifest_row(cls, ss, location, *, include_signature=True):
"make a manifest row dictionary."
row = {}
row['md5'] = ss.md5sum()
row['md5short'] = row['md5'][:8]
row['ksize'] = ss.minhash.ksize
row['moltype'] = ss.minhash.moltype
row['num'] = ss.minhash.num
row['scaled'] = ss.minhash.scaled
row['n_hashes'] = len(ss.minhash)
row['with_abundance'] = 1 if ss.minhash.track_abundance else 0
row['name'] = ss.name
row['filename'] = ss.filename
row['internal_location'] = location
assert set(row.keys()) == set(cls.required_keys)
# if requested, include the signature in the manifest.
if include_signature:
row['signature'] = ss
return row
@classmethod
def create_manifest(cls, locations_iter, *, include_signature=True):
"""Create a manifest from an iterator that yields (ss, location)
Stores signatures in manifest rows by default.
Note: do NOT catch exceptions here, so this passes through load excs.
"""
manifest_list = []
for ss, location in locations_iter:
row = cls.make_manifest_row(ss, location,
include_signature=include_signature)
manifest_list.append(row)
return cls(manifest_list)
## implement me
@abstractmethod
def __add__(self, other):
"Add two manifests"
@abstractmethod
def __bool__(self):
"Test if manifest is empty"
@abstractmethod
def __len__(self):
"Get number of entries in manifest"
@abstractmethod
def __eq__(self, other):
"Check for equality of manifest based on rows"
@abstractmethod
def select_to_manifest(self, **kwargs):
"Select compatible signatures"
@abstractmethod
def filter_rows(self, row_filter_fn):
"Filter rows based on a pattern matching function."
@abstractmethod
def filter_on_columns(self, col_filter_fn, col_names):
"Filter on column values."
@abstractmethod
def locations(self):
"Return a list of distinct locations"
@abstractmethod
def __contains__(self, ss):
"Determine if a particular SourmashSignature is in this manifest."
@abstractmethod
def to_picklist(self):
"Convert manifest to a picklist."
class CollectionManifest(BaseCollectionManifest):
"""
An in-memory manifest that simply stores the rows in a list.
"""
def __init__(self, rows=[]):
"Initialize from an iterable of metadata dictionaries."
self.rows = []
self._md5_set = set()
self._add_rows(rows)
@classmethod
def load_from_manifest(cls, manifest, **kwargs):
"Load this manifest from another manifest object."
return cls(manifest.rows)
def add_row(self, row):
self._add_rows([row])
def _add_rows(self, rows):
self.rows.extend(rows)
# maintain a fast check for md5sums for __contains__ check.
md5set = self._md5_set
for row in self.rows:
md5set.add(row['md5'])
def __iadd__(self, other):
self._add_rows(other.rows)
return self
def __add__(self, other):
mf = CollectionManifest(self.rows)
mf._add_rows(other.rows)
return mf
def __bool__(self):
return bool(self.rows)
def __len__(self):
return len(self.rows)
def __eq__(self, other):
"Check equality on a row-by-row basis. May fail on out-of-order rows."
for (a, b) in itertools.zip_longest(self.rows, other.rows):
if a is None or b is None:
return False
# ignore non-required keys.
for k in self.required_keys:
if a[k] != b[k]:
return False
return True
def _select(self, *, ksize=None, moltype=None, scaled=0, num=0,
containment=False, abund=None, picklist=None):
"""Yield manifest rows for sigs that match the specified requirements.
Internal method; call `select_to_manifest` instead.
"""
matching_rows = self.rows
if ksize:
matching_rows = ( row for row in matching_rows
if row['ksize'] == ksize )
if moltype:
matching_rows = ( row for row in matching_rows
if row['moltype'] == moltype )
if scaled or containment:
if containment and not scaled:
raise ValueError("'containment' requires 'scaled' in Index.select'")
matching_rows = ( row for row in matching_rows
if row['scaled'] and not row['num'] )
if num:
matching_rows = ( row for row in matching_rows
if row['num'] and not row['scaled'] )
if abund:
# only need to concern ourselves if abundance is _required_
matching_rows = ( row for row in matching_rows
if row['with_abundance'] )
if picklist:
matching_rows = ( row for row in matching_rows
if picklist.matches_manifest_row(row) )
# return only the internal filenames!
for row in matching_rows:
yield row
def select_to_manifest(self, **kwargs):
"Do a 'select' and return a new CollectionManifest object."
new_rows = self._select(**kwargs)
return CollectionManifest(new_rows)
def filter_rows(self, row_filter_fn):
"Create a new manifest filtered through row_filter_fn."
new_rows = [ row for row in self.rows if row_filter_fn(row) ]
return CollectionManifest(new_rows)
def filter_on_columns(self, col_filter_fn, col_names):
"Create a new manifest based on column matches."
def row_filter_fn(row):
x = [ row[col] for col in col_names if row[col] is not None ]
return col_filter_fn(x)
return self.filter_rows(row_filter_fn)
def locations(self):
"Return all distinct locations."
seen = set()
for row in self.rows:
loc = row['internal_location']
# track/remove duplicates
if loc not in seen:
seen.add(loc)
yield loc
def __contains__(self, ss):
"Does this manifest contain this signature?"
md5 = ss.md5sum()
return md5 in self._md5_set
def to_picklist(self):
"Convert this manifest to a picklist."
picklist = SignaturePicklist('md5')
picklist.pickset = set(self._md5_set)
return picklist