/
_mixin.py
553 lines (455 loc) · 18.1 KB
/
_mixin.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------
import abc
import copy
import pandas as pd
from skbio.util._decorator import stable, experimental
from skbio.metadata import IntervalMetadata
class MetadataMixin(metaclass=abc.ABCMeta):
@property
@stable(as_of="0.4.0")
def metadata(self):
"""``dict`` containing metadata which applies to the entire object.
Notes
-----
This property can be set and deleted. When setting new metadata a
shallow copy of the dictionary is made.
Examples
--------
.. note:: scikit-bio objects with metadata share a common interface for
accessing and manipulating their metadata. The following examples
use scikit-bio's ``Sequence`` class to demonstrate metadata
behavior. These examples apply to all other scikit-bio objects
storing metadata.
Create a sequence with metadata:
>>> from pprint import pprint
>>> from skbio import Sequence
>>> seq = Sequence('ACGT', metadata={'description': 'seq description',
... 'id': 'seq-id'})
Retrieve metadata:
>>> pprint(seq.metadata) # using pprint to display dict in sorted order
{'description': 'seq description', 'id': 'seq-id'}
Update metadata:
>>> seq.metadata['id'] = 'new-id'
>>> seq.metadata['pubmed'] = 12345
>>> pprint(seq.metadata)
{'description': 'seq description', 'id': 'new-id', 'pubmed': 12345}
Set metadata:
>>> seq.metadata = {'abc': 123}
>>> seq.metadata
{'abc': 123}
Delete metadata:
>>> seq.has_metadata()
True
>>> del seq.metadata
>>> seq.metadata
{}
>>> seq.has_metadata()
False
"""
if self._metadata is None:
# Not using setter to avoid copy.
self._metadata = {}
return self._metadata
@metadata.setter
def metadata(self, metadata):
if not isinstance(metadata, dict):
raise TypeError("metadata must be a dict, not type %r" %
type(metadata).__name__)
# Shallow copy.
self._metadata = metadata.copy()
@metadata.deleter
def metadata(self):
self._metadata = None
@abc.abstractmethod
def __init__(self, metadata=None):
raise NotImplementedError
def _init_(self, metadata=None):
if metadata is None:
# Could use deleter but this is less overhead and needs to be fast.
self._metadata = None
else:
# Use setter for validation and copy.
self.metadata = metadata
@abc.abstractmethod
def __eq__(self, other):
raise NotImplementedError
def _eq_(self, other):
# We're not simply comparing self.metadata to other.metadata in order
# to avoid creating "empty" metadata representations on the objects if
# they don't have metadata.
if self.has_metadata() and other.has_metadata():
return self.metadata == other.metadata
elif not (self.has_metadata() or other.has_metadata()):
# Both don't have metadata.
return True
else:
# One has metadata while the other does not.
return False
@abc.abstractmethod
def __ne__(self, other):
raise NotImplementedError
def _ne_(self, other):
return not (self == other)
@abc.abstractmethod
def __copy__(self):
raise NotImplementedError
def _copy_(self):
if self.has_metadata():
return self.metadata.copy()
else:
return None
@abc.abstractmethod
def __deepcopy__(self, memo):
raise NotImplementedError
def _deepcopy_(self, memo):
if self.has_metadata():
return copy.deepcopy(self.metadata, memo)
else:
return None
@stable(as_of="0.4.0")
def has_metadata(self):
"""Determine if the object has metadata.
An object has metadata if its ``metadata`` dictionary is not empty
(i.e., has at least one key-value pair).
Returns
-------
bool
Indicates whether the object has metadata.
Examples
--------
.. note:: scikit-bio objects with metadata share a common interface for
accessing and manipulating their metadata. The following examples
use scikit-bio's ``Sequence`` class to demonstrate metadata
behavior. These examples apply to all other scikit-bio objects
storing metadata.
>>> from skbio import Sequence
>>> seq = Sequence('ACGT')
>>> seq.has_metadata()
False
>>> seq = Sequence('ACGT', metadata={})
>>> seq.has_metadata()
False
>>> seq = Sequence('ACGT', metadata={'id': 'seq-id'})
>>> seq.has_metadata()
True
"""
return self._metadata is not None and bool(self.metadata)
class PositionalMetadataMixin(metaclass=abc.ABCMeta):
@abc.abstractmethod
def _positional_metadata_axis_len_(self):
"""Return length of axis that positional metadata applies to.
Returns
-------
int
Positional metadata axis length.
"""
raise NotImplementedError
@property
@stable(as_of="0.4.0")
def positional_metadata(self):
"""``pd.DataFrame`` containing metadata along an axis.
Notes
-----
This property can be set and deleted. When setting new positional
metadata, a shallow copy is made and the ``pd.DataFrame`` index is set
to ``pd.RangeIndex(start=0, stop=axis_len, step=1)``.
Examples
--------
.. note:: scikit-bio objects with positional metadata share a common
interface for accessing and manipulating their positional metadata.
The following examples use scikit-bio's ``DNA`` class to demonstrate
positional metadata behavior. These examples apply to all other
scikit-bio objects storing positional metadata.
Create a DNA sequence with positional metadata:
>>> from skbio import DNA
>>> seq = DNA(
... 'ACGT',
... positional_metadata={'exons': [True, True, False, True],
... 'quality': [3, 3, 20, 11]})
>>> seq
DNA
-----------------------------
Positional metadata:
'exons': <dtype: bool>
'quality': <dtype: int64>
Stats:
length: 4
has gaps: False
has degenerates: False
has definites: True
GC-content: 50.00%
-----------------------------
0 ACGT
Retrieve positional metadata:
>>> seq.positional_metadata
exons quality
0 True 3
1 True 3
2 False 20
3 True 11
Update positional metadata:
>>> seq.positional_metadata['gaps'] = seq.gaps()
>>> seq.positional_metadata
exons quality gaps
0 True 3 False
1 True 3 False
2 False 20 False
3 True 11 False
Set positional metadata:
>>> seq.positional_metadata = {'degenerates': seq.degenerates()}
>>> seq.positional_metadata # doctest: +NORMALIZE_WHITESPACE
degenerates
0 False
1 False
2 False
3 False
Delete positional metadata:
>>> seq.has_positional_metadata()
True
>>> del seq.positional_metadata
>>> seq.positional_metadata
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]
>>> seq.has_positional_metadata()
False
"""
if self._positional_metadata is None:
# Not using setter to avoid copy.
self._positional_metadata = pd.DataFrame(
index=self._get_positional_metadata_index())
return self._positional_metadata
@positional_metadata.setter
def positional_metadata(self, positional_metadata):
try:
# Pass copy=True to copy underlying data buffer.
positional_metadata = pd.DataFrame(positional_metadata, copy=True)
# Different versions of pandas will raise different error types. We
# don't really care what the type of the error is, just its message, so
# a blanket Exception will do.
except Exception as e:
raise TypeError(
"Invalid positional metadata. Must be consumable by "
"`pd.DataFrame` constructor. Original pandas error message: "
"\"%s\"" % e)
num_rows = len(positional_metadata.index)
axis_len = self._positional_metadata_axis_len_()
if num_rows != axis_len:
raise ValueError(
"Number of positional metadata values (%d) must match the "
"positional metadata axis length (%d)."
% (num_rows, axis_len))
positional_metadata.index = self._get_positional_metadata_index()
self._positional_metadata = positional_metadata
@positional_metadata.deleter
def positional_metadata(self):
self._positional_metadata = None
def _get_positional_metadata_index(self):
"""Create a memory-efficient integer index for positional metadata."""
return pd.RangeIndex(start=0,
stop=self._positional_metadata_axis_len_(),
step=1)
@abc.abstractmethod
def __init__(self, positional_metadata=None):
raise NotImplementedError
def _init_(self, positional_metadata=None):
if positional_metadata is None:
# Could use deleter but this is less overhead and needs to be fast.
self._positional_metadata = None
else:
# Use setter for validation and copy.
self.positional_metadata = positional_metadata
@abc.abstractmethod
def __eq__(self, other):
raise NotImplementedError
def _eq_(self, other):
# We're not simply comparing self.positional_metadata to
# other.positional_metadata in order to avoid creating "empty"
# positional metadata representations on the objects if they don't have
# positional metadata.
if self.has_positional_metadata() and other.has_positional_metadata():
return self.positional_metadata.equals(other.positional_metadata)
elif not (self.has_positional_metadata() or
other.has_positional_metadata()):
# Both don't have positional metadata.
return (self._positional_metadata_axis_len_() ==
other._positional_metadata_axis_len_())
else:
# One has positional metadata while the other does not.
return False
@abc.abstractmethod
def __ne__(self, other):
raise NotImplementedError
def _ne_(self, other):
return not (self == other)
@abc.abstractmethod
def __copy__(self):
raise NotImplementedError
def _copy_(self):
if self.has_positional_metadata():
# deep=True makes a shallow copy of the underlying data buffer.
return self.positional_metadata.copy(deep=True)
else:
return None
@abc.abstractmethod
def __deepcopy__(self, memo):
raise NotImplementedError
def _deepcopy_(self, memo):
if self.has_positional_metadata():
# `copy.deepcopy` no longer recursively copies contents of the
# DataFrame, so we must handle the deep copy ourselves.
# Reference: https://github.com/pandas-dev/pandas/issues/17406
df = self.positional_metadata
data_cp = copy.deepcopy(df.values.tolist(), memo)
return pd.DataFrame(data_cp,
index=df.index.copy(deep=True),
columns=df.columns.copy(deep=True),
copy=False)
else:
return None
@stable(as_of="0.4.0")
def has_positional_metadata(self):
"""Determine if the object has positional metadata.
An object has positional metadata if its ``positional_metadata``
``pd.DataFrame`` has at least one column.
Returns
-------
bool
Indicates whether the object has positional metadata.
Examples
--------
.. note:: scikit-bio objects with positional metadata share a common
interface for accessing and manipulating their positional metadata.
The following examples use scikit-bio's ``DNA`` class to demonstrate
positional metadata behavior. These examples apply to all other
scikit-bio objects storing positional metadata.
>>> import pandas as pd
>>> from skbio import DNA
>>> seq = DNA('ACGT')
>>> seq.has_positional_metadata()
False
>>> seq = DNA('ACGT', positional_metadata=pd.DataFrame(index=range(4)))
>>> seq.has_positional_metadata()
False
>>> seq = DNA('ACGT', positional_metadata={'quality': range(4)})
>>> seq.has_positional_metadata()
True
"""
return (self._positional_metadata is not None and
len(self.positional_metadata.columns) > 0)
class IntervalMetadataMixin(metaclass=abc.ABCMeta):
@abc.abstractmethod
def _interval_metadata_axis_len_(self):
'''Return length of axis that interval metadata applies to.
Returns
-------
int
Interval metadata axis length.
'''
raise NotImplementedError
@abc.abstractmethod
def __init__(self, interval_metadata=None):
raise NotImplementedError
def _init_(self, interval_metadata=None):
if interval_metadata is None:
# Could use deleter but this is less overhead and needs to be fast.
self._interval_metadata = None
else:
# Use setter for validation and copy.
self.interval_metadata = interval_metadata
@property
@experimental(as_of="0.5.1")
def interval_metadata(self):
'''``IntervalMetadata`` object containing info about interval features.
Notes
-----
This property can be set and deleted. When setting new
interval metadata, a shallow copy of the ``IntervalMetadata``
object is made.
'''
if self._interval_metadata is None:
# Not using setter to avoid copy.
self._interval_metadata = IntervalMetadata(
self._interval_metadata_axis_len_())
return self._interval_metadata
@interval_metadata.setter
def interval_metadata(self, interval_metadata):
if isinstance(interval_metadata, IntervalMetadata):
upper_bound = interval_metadata.upper_bound
lower_bound = interval_metadata.lower_bound
axis_len = self._interval_metadata_axis_len_()
if lower_bound != 0:
raise ValueError(
'The lower bound for the interval features (%d) '
'must be zero.' % lower_bound)
if upper_bound is not None and upper_bound != axis_len:
raise ValueError(
'The upper bound for the interval features (%d) '
'must match the interval metadata axis length (%d)'
% (upper_bound, axis_len))
# copy all the data to the mixin
self._interval_metadata = IntervalMetadata(
axis_len, copy_from=interval_metadata)
else:
raise TypeError('You must provide `IntervalMetadata` object, '
'not type %s.' % type(interval_metadata).__name__)
@interval_metadata.deleter
def interval_metadata(self):
self._interval_metadata = None
@experimental(as_of="0.5.1")
def has_interval_metadata(self):
"""Determine if the object has interval metadata.
An object has interval metadata if its ``interval_metadata``
has at least one ```Interval`` objects.
Returns
-------
bool
Indicates whether the object has interval metadata.
"""
return (self._interval_metadata is not None and
self.interval_metadata.num_interval_features > 0)
@abc.abstractmethod
def __eq__(self, other):
raise NotImplementedError
def _eq_(self, other):
# We're not simply comparing self.interval_metadata to
# other.interval_metadata in order to avoid creating "empty"
# interval metadata representations on the objects if they don't have
# interval metadata.
if self.has_interval_metadata() and other.has_interval_metadata():
return self.interval_metadata == other.interval_metadata
elif not (self.has_interval_metadata() or
other.has_interval_metadata()):
# Both don't have interval metadata.
return (self._interval_metadata_axis_len_() ==
other._interval_metadata_axis_len_())
else:
# One has interval metadata while the other does not.
return False
@abc.abstractmethod
def __ne__(self, other):
raise NotImplementedError
def _ne_(self, other):
return not (self == other)
@abc.abstractmethod
def __copy__(self):
raise NotImplementedError
def _copy_(self):
if self.has_interval_metadata():
return copy.copy(self.interval_metadata)
else:
return None
@abc.abstractmethod
def __deepcopy__(self, memo):
raise NotImplementedError
def _deepcopy_(self, memo):
if self.has_interval_metadata():
return copy.deepcopy(self.interval_metadata, memo)
else:
return None