Skip to content

Commit b297640

Browse files
author
Alan Christie
committed
fix: Protein and compound now checked against target
1 parent b89bd99 commit b297640

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

viewer/squonk_job_file_transfer.py

+46-36
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import urllib.parse
66
from pathlib import Path
7-
from typing import Dict, List, Tuple
7+
from typing import Any, Dict, List, Optional, Tuple
88

99
from celery.utils.log import get_task_logger
1010
from django.conf import settings
@@ -100,7 +100,7 @@ def process_file_transfer(auth_token, job_transfer_id):
100100

101101
def validate_file_transfer_files(
102102
request,
103-
) -> Tuple[Dict[str, str], List[Path], List[Path]]:
103+
) -> Tuple[Optional[Dict[str, str]], Optional[List[Path]], Optional[List[Path]]]:
104104
"""Check the request and return a list of proteins and/or computed molecule file
105105
path references (paths relative to the media directory).
106106
@@ -121,9 +121,9 @@ def validate_file_transfer_files(
121121
list of validated computed molecules (ComputedMolecule)
122122
"""
123123

124-
logger.info('+ Validating file transfer files...')
124+
target_id = request.data['target']
125+
logger.info('+ Validating file transfer files ()...')
125126

126-
error: Dict[str, str] = {}
127127
protein_files: List[Path] = []
128128
compound_files: List[Path] = []
129129

@@ -133,21 +133,24 @@ def validate_file_transfer_files(
133133
p.strip() for p in request.data['proteins'].split(',')
134134
]
135135
for protein_path_and_file in protein_paths_and_files:
136-
# It's a filename
137136
if protein_path_and_file.endswith('_apo-desolv.pdb'):
138-
if SiteObservation.objects.filter(
139-
apo_desolv_file=protein_path_and_file
140-
).first():
137+
if not (
138+
s_ob := SiteObservation.objects.filter(
139+
apo_desolv_file=protein_path_and_file
140+
).first()
141+
):
142+
return tfr_validation_error(
143+
f'Unknown Protein: {protein_path_and_file}',
144+
status.HTTP_404_NOT_FOUND,
145+
)
146+
147+
if s_ob.experiment.experiment_upload.target.id == target_id:
141148
protein_files.append(Path(protein_path_and_file))
142149
else:
143-
error['message'] = f'Unknown Protein: {protein_path_and_file}'
144-
error['status'] = status.HTTP_404_NOT_FOUND
145-
return error, protein_files, compound_files
146-
147-
if not protein_files:
148-
error['message'] = 'API expects a list of comma-separated protein codes'
149-
error['status'] = status.HTTP_404_NOT_FOUND
150-
return error, protein_files, compound_files
150+
return tfr_validation_error(
151+
f'Protein does not belong to Target: {protein_path_and_file}',
152+
status.HTTP_400_BAD_REQUEST,
153+
)
151154

152155
logger.info(
153156
"+ Validated proteins (SiteObservations) [%d]",
@@ -159,36 +162,43 @@ def validate_file_transfer_files(
159162
p.strip() for p in request.data['compounds'].split(',')
160163
]
161164
for compound_path_and_file in compound_paths_and_files:
162-
if SiteObservation.objects.filter(
165+
if not SiteObservation.objects.filter(
163166
ligand_mol=compound_path_and_file
164167
).first():
168+
return tfr_validation_error(
169+
f'Unknown Compound: {compound_path_and_file}',
170+
status.HTTP_404_NOT_FOUND,
171+
)
172+
173+
if s_ob.experiment.experiment_upload.target.id == target_id:
165174
compound_files.append(Path(compound_path_and_file))
166175
else:
167-
error['message'] = f'Unknown Compound: {compound_path_and_file}'
168-
error['status'] = status.HTTP_404_NOT_FOUND
169-
return error, protein_files, compound_files
170-
171-
if not compound_files:
172-
error['message'] = 'API expects a list of comma-separated compound names'
173-
error['status'] = status.HTTP_400_BAD_REQUEST
174-
return error, protein_files, compound_files
176+
return tfr_validation_error(
177+
f'Compound does not belong to Target: {compound_path_and_file}',
178+
status.HTTP_400_BAD_REQUEST,
179+
)
175180

176181
logger.info(
177182
"+ Validated compounds (SiteObservations) [%d]",
178183
len(compound_files),
179184
)
180185

181-
if protein_files or compound_files:
182-
logger.info(
183-
"- Validated file transfer files (%d, %d)",
184-
len(protein_files),
185-
len(compound_files),
186+
if not protein_files and not compound_files:
187+
return tfr_validation_error(
188+
'A valid set of protein codes and/or a list of valid compound names must be provided',
189+
status.HTTP_400_BAD_REQUEST,
186190
)
187-
return error, protein_files, compound_files
188191

189-
error['message'] = (
190-
'A valid set of protein codes and/or a list of valid'
191-
' compound names must be provided'
192+
logger.info(
193+
"- Validated file transfer files (%d, %d)",
194+
len(protein_files),
195+
len(compound_files),
192196
)
193-
error['status'] = status.HTTP_400_BAD_REQUEST
194-
return error, protein_files, compound_files
197+
return None, protein_files, compound_files
198+
199+
200+
def tfr_validation_error(
201+
error: str, status_code: int
202+
) -> Tuple[Dict[str, Any], None, None]:
203+
"""Returns the error and HTTP status code as a tuple for a response."""
204+
return {'message': error, 'status': status_code}, None, None

0 commit comments

Comments
 (0)