forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
603 additions
and
14 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
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
30 changes: 30 additions & 0 deletions
30
lib/galaxy/model/dataset_collections/types/sample_sheet.py
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,30 @@ | ||
from galaxy.exceptions import RequestParameterMissingException | ||
from galaxy.model import DatasetCollectionElement | ||
from . import BaseDatasetCollectionType | ||
from .sample_sheet_util import validate_row | ||
|
||
|
||
class SampleSheetDatasetCollectionType(BaseDatasetCollectionType): | ||
"""A flat list of named elements starting rows with column metadata.""" | ||
|
||
collection_type = "sample_sheet" | ||
|
||
def generate_elements(self, dataset_instances, **kwds): | ||
rows = kwds.get("rows", None) | ||
column_definitions = kwds.get("column_definitions", None) | ||
if rows is None: | ||
raise RequestParameterMissingException( | ||
"Missing or null parameter 'rows' required for 'sample_sheet' collection types." | ||
) | ||
if len(dataset_instances) != len(rows): | ||
self._validation_failed("Supplied element do not match 'rows'.") | ||
|
||
for identifier, element in dataset_instances.items(): | ||
columns = rows[identifier] | ||
validate_row(columns, column_definitions) | ||
association = DatasetCollectionElement( | ||
element=element, | ||
element_identifier=identifier, | ||
columns=columns, | ||
) | ||
yield association |
101 changes: 101 additions & 0 deletions
101
lib/galaxy/model/dataset_collections/types/sample_sheet_util.py
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,101 @@ | ||
from typing import ( | ||
List, | ||
Optional, | ||
Union, | ||
) | ||
|
||
from pydantic import ( | ||
BaseModel, | ||
ConfigDict, | ||
RootModel, | ||
TypeAdapter, | ||
) | ||
|
||
from galaxy.exceptions import RequestParameterInvalidException | ||
from galaxy.schema.schema import ( | ||
SampleSheetColumnDefinition, | ||
SampleSheetColumnDefinitions, | ||
SampleSheetColumnType, | ||
SampleSheetColumnValueT, | ||
SampleSheetRow, | ||
) | ||
from galaxy.tool_util.parser.parameter_validators import ( | ||
AnySafeValidatorModel, | ||
DiscriminatedAnySafeValidatorModel, | ||
parse_dict_validators, | ||
UnsafeValidatorConfiguredInUntrustedContext, | ||
) | ||
|
||
|
||
class SampleSheetColumnDefinitionModel(BaseModel): | ||
model_config = ConfigDict(extra="forbid") | ||
type: SampleSheetColumnType | ||
validators: Optional[List[AnySafeValidatorModel]] = None | ||
restrictions: Optional[List[SampleSheetColumnValueT]] = None | ||
suggestions: Optional[List[SampleSheetColumnValueT]] = None | ||
|
||
|
||
SampleSheetColumnDefinitionsModel = RootModel[List[SampleSheetColumnDefinitionModel]] | ||
SampleSheetColumnDefinitionDictOrModel = Union[SampleSheetColumnDefinition, SampleSheetColumnDefinitionModel] | ||
|
||
|
||
def sample_sheet_column_definition_to_model( | ||
column_definition: SampleSheetColumnDefinitionDictOrModel, | ||
) -> SampleSheetColumnDefinitionModel: | ||
if isinstance(column_definition, SampleSheetColumnDefinitionModel): | ||
return column_definition | ||
else: | ||
return SampleSheetColumnDefinitionModel.model_validate(column_definition) | ||
|
||
|
||
def validate_column_definitions(column_definitions: Optional[SampleSheetColumnDefinitions]): | ||
for column_definition in column_definitions or []: | ||
_validate_column_definition(column_definition) | ||
|
||
|
||
def _validate_column_definition(column_definition: SampleSheetColumnDefinition): | ||
# we should do most of this with pydantic but I just wanted to especially make sure | ||
# we were only using safe validators | ||
return SampleSheetColumnDefinitionModel(**column_definition) | ||
|
||
|
||
def validate_row(row: SampleSheetRow, column_definitions: Optional[SampleSheetColumnDefinitions]): | ||
if column_definitions is None: | ||
return | ||
if len(row) != len(column_definitions): | ||
raise RequestParameterInvalidException( | ||
"Sample sheet row validation failed, incorrect number of columns specified." | ||
) | ||
for column_value, column_definition in zip(row, column_definitions): | ||
validate_column_value(column_value, column_definition) | ||
|
||
|
||
def validate_column_value( | ||
column_value: SampleSheetColumnValueT, column_definition: SampleSheetColumnDefinitionDictOrModel | ||
): | ||
column_definition_model = sample_sheet_column_definition_to_model(column_definition) | ||
column_type = column_definition_model.type | ||
if column_type == "int": | ||
if not isinstance(column_value, int): | ||
raise RequestParameterInvalidException(f"{column_value} was not an integer as expected") | ||
elif column_type == "float": | ||
if not isinstance(column_value, (float, int)): | ||
raise RequestParameterInvalidException(f"{column_value} was not a number as expected") | ||
elif column_type == "string": | ||
if not isinstance(column_value, (str,)): | ||
raise RequestParameterInvalidException(f"{column_value} was not a string as expected") | ||
elif column_type == "boolean": | ||
if not isinstance(column_value, (bool,)): | ||
raise RequestParameterInvalidException(f"{column_value} was not a boolean as expected") | ||
restrictions = column_definition_model.restrictions | ||
if restrictions is not None: | ||
if column_value not in restrictions: | ||
raise RequestParameterInvalidException( | ||
f"{column_value} was not in specified list of valid values as expected" | ||
) | ||
validators = column_definition_model.validators or [] | ||
for validator in validators: | ||
try: | ||
validator.statically_validate(column_value) | ||
except ValueError as e: | ||
raise RequestParameterInvalidException(str(e)) |
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.