Skip to content

Commit

Permalink
feat(breadbox): Added support for associations to breadbox client and…
Browse files Browse the repository at this point in the history
… other associations small fixes (#164)
  • Loading branch information
pgm authored Jan 8, 2025
1 parent 4c21f40 commit 5f2eaea
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
39 changes: 29 additions & 10 deletions breadbox-client/breadbox_facade/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@
from breadbox_client.api.groups import remove_group_access as remove_group_access_client
from breadbox_client.api.groups import get_groups as get_groups_client
from breadbox_client.api.types import add_dimension_type as add_dimension_type_client
from breadbox_client.api.types import add_feature_type as add_feature_type_client
from breadbox_client.api.types import add_sample_type as add_sample_type_client
from breadbox_client.api.types import get_dimension_type as get_dimension_type_client
from breadbox_client.api.types import get_dimension_types as get_dimension_types_client
from breadbox_client.api.types import get_feature_types as get_feature_types_client
from breadbox_client.api.types import get_sample_types as get_sample_types_client
from breadbox_client.api.types import remove_dimension_type as remove_dimension_type_client
from breadbox_client.api.types import update_dimension_type as update_dimension_type_client
from breadbox_client.api.types import update_feature_type_metadata as update_feature_type_metadata_client
from breadbox_client.api.types import update_sample_type_metadata as update_sample_type_metadata_client
from breadbox_client.api.temp import get_associations as get_associations_client
from breadbox_client.api.temp import add_associations as add_associations_client
from breadbox_client.api.temp import get_associations_for_slice as get_associations_for_slice_client
#

from breadbox_client.models import (
AccessType,
AddDatasetResponse,
AddDimensionType,
Associations,
AssociationTable,
AssociationsIn,
AssociationsInAxis,
AddDimensionTypeAxis,
AnnotationTypeMap,
BodyAddDataType,
BodyAddFeatureType,
BodyAddSampleType,
BodyGetDatasetData,
BodyUpdateFeatureTypeMetadata,
BodyUpdateSampleTypeMetadata,
BodyUploadFile,
ColumnMetadata,
ComputeParams,
Expand All @@ -66,12 +66,13 @@
GroupEntry,
GroupEntryIn,
GroupOut,
IdMapping,
MatrixDatasetParams,
MatrixDatasetParamsDatasetMetadataType0,
MatrixDatasetParamsFormat,
MatrixDatasetResponse,
SampleTypeOut,
SliceQuery,
SliceQueryIdentifierType,
TableDatasetParams,
TableDatasetParamsColumnsMetadata,
TableDatasetParamsDatasetMetadataType0,
Expand Down Expand Up @@ -472,6 +473,24 @@ def remove_group_access(self, group_entry_id: str) -> str:
breadbox_response = remove_group_access_client.sync_detailed(client=self.client, group_entry_id=group_entry_id)
return self._parse_client_response(breadbox_response)

# ASSOCIATIONS
def get_associations(self) -> List[AssociationTable]:
breadbox_response = get_associations_client.sync_detailed(client=self.client)
return self._parse_client_response(breadbox_response)

def add_associations(self, dataset_1_id:str, dataset_2_id: str, axis :str, associations_table_filename : str) -> AssociationTable:
with open(associations_table_filename, "rb") as fd:
uploaded_file = self.upload_file(fd)

associations_in = AssociationsIn(axis = AssociationsInAxis(axis), dataset_1_id=dataset_1_id, dataset_2_id=dataset_2_id, file_ids=uploaded_file.file_ids, md5=uploaded_file.md5)
breadbox_response = add_associations_client.sync_detailed(client=self.client, body=associations_in)

return self._parse_client_response(breadbox_response)

def get_associations_for_slice(self, dataset_id: str, identifier: str, identifier_type: str) -> Associations:
breadbox_response = get_associations_for_slice_client.sync_detailed(client=self.client, body=SliceQuery(dataset_id=dataset_id, identifier=identifier,
identifier_type=SliceQueryIdentifierType(identifier_type)))
return self._parse_client_response(breadbox_response)

# API

Expand Down
11 changes: 10 additions & 1 deletion breadbox/breadbox/api/temp/associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from breadbox.crud import associations as associations_crud
import uuid
from breadbox.db.util import transaction
from typing import cast, Literal

from .router import router

Expand Down Expand Up @@ -46,7 +47,12 @@ def query_associations_for_slice(
def get_associations(db: Annotated[SessionWithUser, Depends(get_db_with_user)]):
return [
AssociationTable(
id=a.id, dataset_1_id=a.dataset_1_id, dataset_2_id=a.dataset_2_id
id=a.id,
dataset_1_id=a.dataset_1_id,
dataset_2_id=a.dataset_2_id,
dataset_1_name=a.dataset_1.name,
dataset_2_name=a.dataset_2.name,
axis=a.axis,
)
for a in associations_crud.get_association_tables(db, None)
]
Expand Down Expand Up @@ -117,4 +123,7 @@ def add_associations(
id=assoc_table.id,
dataset_1_id=assoc_table.dataset_1_id,
dataset_2_id=assoc_table.dataset_2_id,
dataset_1_name=assoc_table.dataset_1.name,
dataset_2_name=assoc_table.dataset_2.name,
axis=cast(Literal["sample", "feature"], assoc_table.axis),
)
17 changes: 17 additions & 0 deletions breadbox/breadbox/crud/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ColumnMetadata,
UpdateDatasetParams,
)

from ..schemas.custom_http_exception import (
DatasetAccessError,
ResourceNotFoundError,
Expand All @@ -39,6 +40,7 @@
PropertyToIndex,
ValueType,
DimensionType,
PrecomputedAssociation,
)
from breadbox.crud.group import (
get_group,
Expand Down Expand Up @@ -1202,9 +1204,24 @@ def update_dataset(
def delete_dataset(
db: SessionWithUser, user: str, dataset: Dataset, filestore_location: str
):
from .associations import delete_association_table

if not user_has_access_to_group(dataset.group, user, write_access=True):
return False

log.info("delete any referenced precomputed associations")
for associations in (
db.query(PrecomputedAssociation)
.filter(
or_(
PrecomputedAssociation.dataset_1_id == dataset.id,
PrecomputedAssociation.dataset_2_id == dataset.id,
)
)
.all()
):
delete_association_table(db, associations.id, filestore_location)

log.info("delete Dimension")
db.query(Dimension).filter(Dimension.dataset_id == dataset.id).delete()
log.info("delete_dataset %s delete dataset itself", dataset.id)
Expand Down
3 changes: 3 additions & 0 deletions breadbox/breadbox/schemas/associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ class AssociationsIn(BaseModel):

class AssociationTable(BaseModel):
id: str
dataset_1_name: str
dataset_2_name: str
axis: Literal["sample", "feature"]
dataset_1_id: str
dataset_2_id: str

0 comments on commit 5f2eaea

Please sign in to comment.