-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push down HCA specifics to a Bundle subclass (#4940)
- Loading branch information
1 parent
35dc241
commit e2cd04f
Showing
13 changed files
with
336 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from abc import ( | ||
ABC, | ||
) | ||
from typing import ( | ||
AbstractSet, | ||
Generic, | ||
Iterable, | ||
Optional, | ||
TypeVar, | ||
Union, | ||
) | ||
|
||
import attr | ||
from more_itertools import ( | ||
one, | ||
) | ||
|
||
from azul.indexer import ( | ||
BUNDLE_FQID, | ||
Bundle, | ||
) | ||
from azul.indexer.document import ( | ||
EntityReference, | ||
EntityType, | ||
) | ||
from azul.types import ( | ||
JSON, | ||
MutableJSON, | ||
) | ||
|
||
# AnVIL snapshots do not use UUIDs for primary/foreign keys. | ||
# This type alias helps us distinguish these keys from the document UUIDs, | ||
# which are drawn from the `datarepo_row_id` column. | ||
# Note that entities from different tables may have the same key, so | ||
# `KeyReference` should be used when mixing keys from different entity types. | ||
Key = str | ||
|
||
|
||
@attr.s(frozen=True, auto_attribs=True, kw_only=True, slots=True) | ||
class KeyReference: | ||
key: Key | ||
entity_type: EntityType | ||
|
||
|
||
ENTITY_REF = TypeVar(name='ENTITY_REF', bound=Union[EntityReference, KeyReference]) | ||
|
||
|
||
@attr.s(auto_attribs=True, frozen=True, kw_only=True, order=False) | ||
class Link(Generic[ENTITY_REF]): | ||
inputs: AbstractSet[ENTITY_REF] = attr.ib(factory=frozenset) | ||
activity: Optional[ENTITY_REF] = attr.ib(default=None) | ||
outputs: AbstractSet[ENTITY_REF] = attr.ib(factory=frozenset) | ||
|
||
@property | ||
def all_entities(self) -> AbstractSet[ENTITY_REF]: | ||
return self.inputs | self.outputs | (set() if self.activity is None else {self.activity}) | ||
|
||
def to_json(self) -> MutableJSON: | ||
return { | ||
'inputs': sorted(map(str, self.inputs)), | ||
'activity': None if self.activity is None else str(self.activity), | ||
'outputs': sorted(map(str, self.outputs)) | ||
} | ||
|
||
@classmethod | ||
def merge(cls, links: Iterable['Link']) -> 'Link': | ||
return cls(inputs=frozenset.union(*[link.inputs for link in links]), | ||
activity=one({link.activity for link in links}), | ||
outputs=frozenset.union(*[link.outputs for link in links])) | ||
|
||
def __lt__(self, other: 'Link') -> bool: | ||
if self.activity is None or other.activity is None: | ||
return False | ||
else: | ||
return self.activity < other.activity | ||
|
||
|
||
@attr.s(auto_attribs=True, kw_only=True) | ||
class AnvilBundle(Bundle[BUNDLE_FQID], ABC): | ||
entities: dict[EntityReference, MutableJSON] = attr.ib(factory=dict) | ||
links: set[Link[EntityReference]] = attr.ib(factory=set) | ||
|
||
def to_json(self) -> MutableJSON: | ||
return { | ||
'entities': { | ||
str(entity_ref): entity | ||
for entity_ref, entity in sorted(self.entities.items()) | ||
}, | ||
'links': [link.to_json() for link in sorted(self.links)] | ||
} | ||
|
||
@classmethod | ||
def from_json(cls, fqid: BUNDLE_FQID, json: JSON) -> 'Bundle': | ||
return cls( | ||
fqid=fqid, | ||
entities={ | ||
EntityReference.parse(entity_ref): entity | ||
for entity_ref, entity in json['entities'].items() | ||
}, | ||
links={ | ||
Link(inputs=set(map(EntityReference.parse, link['inputs'])), | ||
activity=None if link['activity'] is None else EntityReference.parse(link['activity']), | ||
outputs=set(map(EntityReference.parse, link['outputs']))) | ||
for link in json['links'] | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from abc import ( | ||
ABC, | ||
) | ||
|
||
import attr | ||
|
||
from azul.indexer import ( | ||
BUNDLE_FQID, | ||
Bundle, | ||
) | ||
from azul.types import ( | ||
JSON, | ||
MutableJSON, | ||
MutableJSONs, | ||
) | ||
|
||
|
||
@attr.s(auto_attribs=True, kw_only=True) | ||
class HCABundle(Bundle[BUNDLE_FQID], ABC): | ||
manifest: MutableJSONs | ||
""" | ||
Each item of the `manifest` attribute's value has this shape: | ||
{ | ||
'content-type': 'application/json; dcp-type="metadata/biomaterial"', | ||
'crc32c': 'fd239631', | ||
'indexed': True, | ||
'name': 'cell_suspension_0.json', | ||
's3_etag': 'aa31c093cc816edb1f3a42e577872ec6', | ||
'sha1': 'f413a9a7923dee616309e4f40752859195798a5d', | ||
'sha256': 'ea4c9ed9e53a3aa2ca4b7dffcacb6bbe9108a460e8e15d2b3d5e8e5261fb043e', | ||
'size': 1366, | ||
'uuid': '0136ebb4-1317-42a0-8826-502fae25c29f', | ||
'version': '2019-05-16T162155.020000Z' | ||
} | ||
""" | ||
metadata_files: MutableJSON | ||
|
||
def to_json(self) -> MutableJSON: | ||
return { | ||
'manifest': self.manifest, | ||
'metadata': self.metadata_files | ||
} | ||
|
||
@classmethod | ||
def from_json(cls, fqid: BUNDLE_FQID, json: JSON) -> 'HCABundle': | ||
return cls( | ||
fqid=fqid, | ||
manifest=json['manifest'], | ||
metadata_files=json['metadata'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.