1616 Session ,
1717)
1818
19+ from galaxy .exceptions import RequestParameterInvalidException
1920from galaxy .model import (
2021 Dataset ,
2122 DatasetCollection ,
2930 REQUESTED_TRANSFORM_ACTIONS ,
3031 User ,
3132)
33+ from galaxy .model .dataset_collections .types .sample_sheet_util import (
34+ validate_column_definitions ,
35+ validate_row ,
36+ )
3237from galaxy .model .scoped_session import galaxy_scoped_session
3338from galaxy .tool_util_models .parameters import (
3439 CollectionElementCollectionRequestUri ,
3742 DataRequestUri ,
3843 FileRequestUri ,
3944)
45+ from galaxy .tool_util_models .sample_sheet import SampleSheetRow
4046
4147log = logging .getLogger (__name__ )
4248
@@ -97,13 +103,21 @@ def derefence_collection_element(
97103 element : CollectionElementCollectionRequestUri ,
98104 parent_dataset_collection : DatasetCollection ,
99105 element_index : int ,
106+ rows : Optional [dict [str , SampleSheetRow ]] = None ,
100107):
101108 child_dataset_collection = DatasetCollection (collection_type = element .collection_type )
109+
110+ # Extract row for this element if present
111+ columns = None
112+ if rows and element .identifier in rows :
113+ columns = rows [element .identifier ]
114+
102115 DatasetCollectionElement (
103116 collection = parent_dataset_collection ,
104117 element = child_dataset_collection ,
105118 element_identifier = element .identifier ,
106119 element_index = element_index ,
120+ columns = columns ,
107121 )
108122 sa_session .add (child_dataset_collection )
109123 for index , child_element in enumerate (element .elements ):
@@ -126,39 +140,93 @@ def dereference_collection_dataset_element(
126140 element : CollectionElementDataRequestUri ,
127141 parent_dataset_collection : DatasetCollection ,
128142 element_index : int ,
143+ rows : Optional [dict [str , SampleSheetRow ]] = None ,
129144):
130145 hda = dereference_to_model (sa_session , user , history , element , add_to_history = False , visible = False )
131146 history .stage_addition (hda )
147+
148+ # Extract row for this element if present
149+ columns = None
150+ if rows and element .identifier in rows :
151+ columns = rows [element .identifier ]
152+
132153 dce = DatasetCollectionElement (
133154 collection = parent_dataset_collection ,
134155 element = hda ,
135156 element_identifier = element .identifier ,
136157 element_index = element_index ,
158+ columns = columns ,
137159 )
138160 parent_dataset_collection .elements .append (dce )
139161
140162
163+ def _validate_sample_sheet_metadata (
164+ data_request_uri : DataRequestCollectionUri ,
165+ ):
166+ """Validate sample sheet metadata for landing requests."""
167+ # Extract metadata from data request
168+ collection_type = data_request_uri .collection_type
169+ column_definitions = data_request_uri .column_definitions
170+ rows = data_request_uri .rows
171+
172+ # Validate that sample sheet metadata is only used with sample_sheet collection types
173+ is_sample_sheet = collection_type .startswith ("sample_sheet" )
174+ has_sample_sheet_metadata = column_definitions is not None or rows is not None
175+
176+ if has_sample_sheet_metadata and not is_sample_sheet :
177+ raise RequestParameterInvalidException (
178+ f"Sample sheet metadata (column_definitions, rows) can only be used with collection_type 'sample_sheet' or 'sample_sheet:<type>', not '{ collection_type } '"
179+ )
180+
181+ # Validate column definitions structure
182+ if column_definitions is not None :
183+ validate_column_definitions (column_definitions )
184+
185+ # Validate each row
186+ if rows :
187+ # Get element identifiers for validation
188+ element_identifiers = [elem .identifier for elem in data_request_uri .elements ]
189+
190+ for identifier , row in rows .items ():
191+ if identifier not in element_identifiers :
192+ raise RequestParameterInvalidException (
193+ f"Row identifier '{ identifier } ' not found in collection elements"
194+ )
195+ validate_row (row , column_definitions , element_identifiers )
196+
197+
141198def derefence_collection_to_model (
142199 sa_session : galaxy_scoped_session ,
143200 user : User ,
144201 history : History ,
145202 data_request_uri : DataRequestCollectionUri ,
146203 collection_name : str = "Collection" ,
147204) -> HistoryDatasetCollectionAssociation :
205+ # Validate sample sheet metadata before creating any objects
206+ _validate_sample_sheet_metadata (data_request_uri )
207+
148208 name = data_request_uri .name or collection_name
149209 hdca = HistoryDatasetCollectionAssociation (
150210 name = name ,
151211 history = history ,
152212 )
153213 sa_session .add (hdca )
154- dc = DatasetCollection (collection_type = data_request_uri .collection_type )
214+ dc = DatasetCollection (
215+ collection_type = data_request_uri .collection_type ,
216+ column_definitions = data_request_uri .column_definitions ,
217+ )
155218 sa_session .add (dc )
156219 hdca .collection = dc
220+
221+ # Extract rows for passing to element creation
222+ rows = data_request_uri .rows
223+
157224 for i , element in enumerate (data_request_uri .elements ):
158225 if element .class_ == "File" :
159- dereference_collection_dataset_element (sa_session , user , history , element , dc , element_index = i )
226+ dereference_collection_dataset_element (sa_session , user , history , element , dc , element_index = i , rows = rows )
160227 elif element .class_ == "Collection" :
161- derefence_collection_element (sa_session , user , history , element , dc , i )
228+ derefence_collection_element (sa_session , user , history , element , dc , i , rows = rows )
229+
162230 dc .populated_state = "ok"
163231 dc .element_count = len (data_request_uri .elements )
164232 history .stage_addition (hdca )
0 commit comments