-
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
0af765e
commit d8f612a
Showing
20 changed files
with
1,222 additions
and
1,183 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
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,104 @@ | ||
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, converter=frozenset) | ||
activity: Optional[ENTITY_REF] = attr.ib(default=None) | ||
outputs: AbstractSet[ENTITY_REF] = attr.ib(factory=frozenset, converter=frozenset) | ||
|
||
@property | ||
def all_entities(self) -> AbstractSet[ENTITY_REF]: | ||
return self.inputs | self.outputs | (set() if self.activity is None else {self.activity}) | ||
|
||
@classmethod | ||
def from_json(cls, link: JSON) -> 'Link': | ||
return cls(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']))) | ||
|
||
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: | ||
return min(self.inputs) < min(other.inputs) | ||
|
||
|
||
@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) -> 'AnvilBundle': | ||
return cls( | ||
fqid=fqid, | ||
entities={ | ||
EntityReference.parse(entity_ref): entity | ||
for entity_ref, entity in json_['entities'].items() | ||
}, | ||
links=set(map(Link.from_json, 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,53 @@ | ||
from abc import ( | ||
ABC, | ||
) | ||
import logging | ||
|
||
import attr | ||
|
||
from azul.indexer import ( | ||
BUNDLE_FQID, | ||
Bundle, | ||
) | ||
from azul.types import ( | ||
JSON, | ||
MutableJSON, | ||
MutableJSONs, | ||
) | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
@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) -> 'Bundle': | ||
manifest = json_['manifest'] | ||
metadata = json_['metadata'] | ||
assert isinstance(manifest, list), manifest | ||
assert isinstance(metadata, dict), metadata | ||
return cls(fqid=fqid, manifest=manifest, metadata_files=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.