Skip to content

Commit b0d12fb

Browse files
authoredDec 19, 2023
sort workflows by total_conditions_satisfied (#3342)
* sort workflows by total_conditions_satisfied * for wk -> for _, wk * add test
1 parent 02dc7a0 commit b0d12fb

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed
 

‎qiita_db/metadata_template/prep_template.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -853,33 +853,32 @@ def _get_predecessors(workflow, node):
853853

854854
all_workflows = [wk for wk in qdb.software.DefaultWorkflow.iter()]
855855
# are there any workflows with parameters?
856-
check_requirements = False
857-
default_parameters = {'prep': {}, 'sample': {}}
858-
if [wk for wk in all_workflows if wk.parameters != default_parameters]:
859-
check_requirements = True
860856
ST = qdb.metadata_template.sample_template.SampleTemplate
861857
workflows = []
862858
for wk in all_workflows:
863859
if wk.artifact_type == pt_artifact and pt_dt in wk.data_type:
864-
if check_requirements and wk.parameters == default_parameters:
865-
continue
866860
wk_params = wk.parameters
867861
reqs_satisfied = True
862+
total_conditions_satisfied = 0
868863

869864
if wk_params['sample']:
870865
df = ST(self.study_id).to_dataframe(samples=list(self))
871866
for k, v in wk_params['sample'].items():
872867
if k not in df.columns or v not in df[k].unique():
873868
reqs_satisfied = False
869+
else:
870+
total_conditions_satisfied += 1
874871

875872
if wk_params['prep']:
876873
df = self.to_dataframe()
877874
for k, v in wk_params['prep'].items():
878875
if k not in df.columns or v not in df[k].unique():
879876
reqs_satisfied = False
877+
else:
878+
total_conditions_satisfied += 1
880879

881880
if reqs_satisfied:
882-
workflows.append(wk)
881+
workflows.append((total_conditions_satisfied, wk))
883882

884883
if not workflows:
885884
# raises option a.
@@ -888,8 +887,12 @@ def _get_predecessors(workflow, node):
888887
'could be due to required parameters, please check the '
889888
'available workflows.')
890889
raise ValueError(msg)
890+
891+
# let's just keep one, let's give it preference to the one with the
892+
# most total_conditions_satisfied
893+
workflows = sorted(workflows, key=lambda x: x[0], reverse=True)[:1]
891894
missing_artifacts = dict()
892-
for wk in workflows:
895+
for _, wk in workflows:
893896
missing_artifacts[wk] = dict()
894897
for node, degree in wk.graph.out_degree():
895898
if degree != 0:

‎qiita_db/metadata_template/test/test_prep_template.py

+24
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,30 @@ def test_artifact_setter(self):
14471447
'Pick closed-reference OTUs', 'Pick closed-reference OTUs',
14481448
'Pick closed-reference OTUs'])
14491449

1450+
# at this point we can error all the previous steps, add a new smaller
1451+
# workflow and make sure you get the same one as before because it will
1452+
# have a higher match than the new one
1453+
for pj in wk.graph.nodes:
1454+
pj._set_error('Killed')
1455+
sql = """UPDATE qiita.default_workflow_data_type
1456+
SET data_type_id = 1
1457+
WHERE default_workflow_id = 2"""
1458+
qdb.sql_connection.perform_as_transaction(sql)
1459+
wk = pt.add_default_workflow(qdb.user.User('test@foo.bar'))
1460+
self.assertEqual(len(wk.graph.nodes), 5)
1461+
self.assertEqual(len(wk.graph.edges), 3)
1462+
self.assertCountEqual(
1463+
[x.command.name for x in wk.graph.nodes],
1464+
# we should have 2 split libraries and 3 close reference
1465+
['Split libraries FASTQ', 'Split libraries FASTQ',
1466+
'Pick closed-reference OTUs', 'Pick closed-reference OTUs',
1467+
'Pick closed-reference OTUs'])
1468+
# let's return it back
1469+
sql = """UPDATE qiita.default_workflow_data_type
1470+
SET data_type_id = 2
1471+
WHERE default_workflow_id = 2"""
1472+
qdb.sql_connection.perform_as_transaction(sql)
1473+
14501474
# now let's try to generate again and it should fail cause the jobs
14511475
# are already created
14521476
with self.assertRaisesRegex(ValueError, "Cannot create job because "

0 commit comments

Comments
 (0)