Exemple #1
0
    def test_optimize(self, session):

        with session.with_cache() as sess:
            canvas = Planner(sess)

            q = self.sql({
                "object_type_id":
                sess.ObjectType.find_by_name("Plasmid Stock").id,
                "location":
                self.Not("deleted"),
            })
            item = sess.Item.one(query=q)
            assert item

            ops = canvas.chain("Make Miniprep", "Yeast Transformation",
                               "Yeast Overnight Suspension")

            canvas.set_field_value_and_propogate(ops[0].inputs[0],
                                                 sample=item.sample)
            canvas.set_to_available_item(ops[0].inputs[0])

            ops = canvas.chain("Make Miniprep", "Yeast Transformation",
                               "Yeast Overnight Suspension")

            canvas.set_field_value_and_propogate(ops[0].inputs[0],
                                                 sample=item.sample)
            canvas.set_to_available_item(ops[0].inputs[0])

            assert len(canvas.plan.operations) == 6

            canvas.optimize()

            assert len(canvas.plan.operations) == 5
Exemple #2
0
    def test_optimize_case2(self, session):
        """Here, we are trying to optimize two chains of 5 operations.

        We expect to merge 10 operation to 5 operations.
        """
        with session.with_cache() as sess:
            canvas = Planner(sess)

            q = self.sql({
                "object_type_id":
                sess.ObjectType.find_by_name("E coli Plate of Plasmid").id,
                "location":
                self.Not("deleted"),
            })
            item = sess.Item.one(query=q)
            assert item

            chain = [
                "Check Plate",
                "Make Overnight Suspension",
                "Make Miniprep",
                "Yeast Transformation",
                "Yeast Overnight Suspension",
            ]

            yeasts = session.Sample.last(
                2,
                query={
                    "sample_type_id":
                    session.SampleType.find_by_name("Yeast Strain").id
                },
            )

            for i in range(2):
                ops = canvas.chain(*chain)
                canvas.set_field_value_and_propogate(ops[0].inputs[0],
                                                     sample=item.sample)
                canvas.set_to_available_item(ops[0].inputs[0])
                canvas.set_field_value_and_propogate(ops[-1].outputs[0],
                                                     sample=yeasts[0])
                canvas.set_field_value(ops[-2].inputs[1], sample=yeasts[1])
            assert len(canvas.plan.operations) == 10

            canvas.optimize()

            # we expect to merge everything except 'Yeast Transformation' and
            # 'Yeast Overnight' since these have absent sample definition for either
            # their input or outputs and FieldValues with no samples are never mergable.
            expected_op_types = [
                "Check Plate",
                "Make Miniprep",
                "Make Overnight Suspension",
                "Yeast Overnight Suspension",
                "Yeast Transformation",
            ]
            op_types = sorted(
                [op.operation_type.name for op in canvas.operations])
            assert len(canvas.plan.operations) == 5
            assert expected_op_types == op_types
Exemple #3
0
    def test_optimize3(self, session):
        """Here we setup two operations chains of 5.

        6 of these operations will be mergable (10 to 7). We also add
        additional operations to one of the Miniprep operations. We
        should end up with 9 operations after the optimization.
        Additionally, the merged Miniprep should have a single output
        with 3 wires.
        """
        with session.with_cache() as sess:
            canvas = Planner(sess)

            q = self.sql({
                "object_type_id":
                sess.ObjectType.find_by_name("E coli Plate of Plasmid").id,
                "location":
                self.Not("deleted"),
            })
            item = sess.Item.one(query=q)
            assert item

            chain = [
                "Check Plate",
                "Make Overnight Suspension",
                "Make Miniprep",
                "Yeast Transformation",
                "Yeast Overnight Suspension",
            ]

            ops = canvas.chain(*chain)
            canvas.set_field_value_and_propogate(ops[0].inputs[0],
                                                 sample=item.sample)
            canvas.set_to_available_item(ops[0].inputs[0])
            ops[-1].tagged = "YES"

            ops = canvas.chain(*chain)
            canvas.set_field_value_and_propogate(ops[0].inputs[0],
                                                 sample=item.sample)
            canvas.set_to_available_item(ops[0].inputs[0])

            canvas.chain(ops[2], "Yeast Transformation")
            canvas.chain(ops[2], "Make PCR Fragment")

            assert len(canvas.plan.operations) == 12
            assert len(canvas.get_outgoing_wires(ops[2].outputs[0])) == 3

            canvas.optimize()

            assert len(canvas.plan.operations) == 9
            assert len(canvas.get_outgoing_wires(ops[2].outputs[0])) == 4
            for yt in canvas.get_op_by_name("Yeast Transformation"):
                print(yt.inputs[0].name)
                assert len(canvas.get_incoming_wires(yt.inputs[0]))
Exemple #4
0
    def test_restrict_to_one(self, session, restrict_to_one):
        with session.with_cache() as sess:
            sess.set_verbose(True)
            p = Planner(sess)
            for i in range(2):

                comp_cell_type = session.ObjectType.find_by_name(
                    "Yeast Competent Cell")
                comp_cells = session.Item.last(
                    100,
                    query='object_type_id = {} AND location != "deleted"'.
                    format(comp_cell_type.id),
                )
                grouped_by_samples = {}
                for c in comp_cells:
                    grouped_by_samples.setdefault(c.sample_id,
                                                  list()).append(c)
                more_than_one = [
                    cells for cells in grouped_by_samples.values()
                    if len(cells) > 1
                ][0]
                assert more_than_one
                parent_sample = more_than_one[0].sample

                op = p.create_operation_by_name("Yeast Transformation")
                p.set_field_value(op.input("Parent"), sample=parent_sample)
                if restrict_to_one:
                    p.set_to_available_item(
                        op.input("Parent"),
                        item_preference=p.ITEM_SELECTION_PREFERENCE.
                        RESTRICT_TO_ONE,
                    )
                else:
                    p.set_to_available_item(op.input("Parent"))
            items = set()
            for op in p.plan.operations:
                for fv in op.field_values:
                    if fv.child_item_id:
                        items.add(fv.child_item_id)
            if restrict_to_one:
                assert len(items) == 2
            else:
                assert len(items) == 1
Exemple #5
0
def test_prettify(session):
    with session.with_cache() as sess:
        canvas = Planner(sess)
        chain = [
            "Check Plate",
            "Make Overnight Suspension",
            "Make Miniprep",
            "Yeast Transformation",
            "Yeast Overnight Suspension",
        ]

        ops = canvas.chain(*chain)
        canvas.set_field_value_and_propogate(ops[0].inputs[0])
        canvas.set_to_available_item(ops[0].inputs[0])

        ops = canvas.chain(*chain)
        canvas.set_field_value_and_propogate(ops[0].inputs[0])
        canvas.set_to_available_item(ops[0].inputs[0])

        canvas.prettify()
Exemple #6
0
    def test_optimize_with_existing_plan(self, session):

        with session.with_cache() as sess:
            canvas = Planner(sess)

            q = self.sql({
                "object_type_id":
                sess.ObjectType.find_by_name("E coli Plate of Plasmid").id,
                "location":
                self.Not("deleted"),
            })
            item = sess.Item.one(query=q)
            assert item

            chain = [
                "Check Plate",
                "Make Overnight Suspension",
                "Make Miniprep",
                "Yeast Transformation",
                "Yeast Overnight Suspension",
            ]

            ops = canvas.chain(*chain)
            canvas.set_field_value_and_propogate(ops[0].inputs[0],
                                                 sample=item.sample)
            canvas.set_to_available_item(ops[0].inputs[0])

            ops = canvas.chain(*chain)
            canvas.set_field_value_and_propogate(ops[0].inputs[0],
                                                 sample=item.sample)
            canvas.set_to_available_item(ops[0].inputs[0])

            assert len(canvas.plan.operations) == 10
            canvas.save()

        plan_id = canvas.plan.id
        with session.with_cache(timeout=60) as sess:
            canvas = Planner(sess.Plan.find(plan_id))
            canvas.optimize()
            assert len(canvas.plan.operations) == 7