Example #1
0
def test_raises_exception_wiring_with_no_afts(session):
    canvas = Planner(session)
    op1 = canvas.create_operation_by_name("Make PCR Fragment", category="Cloning")
    op2 = canvas.create_operation_by_name("Check Plate", category="Cloning")

    with pytest.raises(PlannerException):
        canvas._set_wire(op1.outputs[0], op2.inputs[0])
Example #2
0
def test_collect_matching_afts(session):
    canvas = Planner(session)

    op1 = canvas.create_operation_by_name("Check Plate", category="Cloning")
    op2 = canvas.create_operation_by_name("E Coli Lysate", category="Cloning")
    afts = canvas._collect_matching_afts(op1, op2)
    print(afts)
Example #3
0
def test_quick_wire_to_input_array_with_set_sample(session):
    canvas = Planner(session)

    frags = session.Sample.where(
        {"sample_type_id": session.SampleType.find_by_name("Fragment").id},
        opts={"limit": 10},
    )

    purify1 = canvas.create_operation_by_name("Purify Gel Slice", category="Cloning")
    purify2 = canvas.create_operation_by_name("Purify Gel Slice", category="Cloning")

    canvas.set_field_value(purify1.inputs[0], sample=frags[0])
    canvas.set_field_value(purify2.inputs[0], sample=frags[1])

    assemble = canvas.create_operation_by_name("Assemble Plasmid", category="Cloning")

    canvas.quick_wire(purify1, assemble)
    canvas.quick_wire(purify2, assemble)

    canvas.chain("Purify Gel Slice", assemble, category="Cloning")

    input_array = assemble.input_array("Fragment")

    assert len(input_array) == 3, "There should be 3 field values"
    assert input_array[0].sample == frags[0]
    assert input_array[1].sample == frags[1]
    assert input_array[2].sample is None
Example #4
0
def test_prettify(session):
    with session.with_cache() as sess:
        canvas = Planner(sess)

        for i in range(10):
            canvas.create_operation_by_name("Yeast Overnight Suspension")

        canvas.prettify()
Example #5
0
def test_canvas_add_op(session):

    canvas = Planner(session)
    canvas.create_operation_by_name("Yeast Transformation")
    canvas.create_operation_by_name("Yeast Antibiotic Plating")
    canvas.quick_wire_by_name("Yeast Transformation", "Yeast Antibiotic Plating")
    canvas.create()

    p = session.Plan.find(canvas.plan.id)
    pass
Example #6
0
def test_add_wire_sets_sample_from_source(session):
    session.set_verbose(True)
    canvas = Planner(session)
    assert len(canvas.plan.wires) == 0
    p = session.Sample.one(
        query=dict(sample_type_id=session.SampleType.find_by_name("Primer").id)
    )
    destination = canvas.create_operation_by_name(
        "Make PCR Fragment", category="Cloning"
    )
    source = canvas.create_operation_by_name("Rehydrate Primer", category="Cloning")
    canvas.set_field_value(source.outputs[0], sample=p)
    canvas.add_wire(source.outputs[0], destination.input("Forward Primer"))
    assert destination.input("Forward Primer").sample.id == p.id
Example #7
0
def test_set_input_array(session):

    canvas = Planner(session)

    op = canvas.create_operation_by_name("Assemble Plasmid", category="Cloning")

    frags = session.Sample.where(
        {"sample_type_id": session.SampleType.find_by_name("Fragment").id},
        opts={"limit": 10},
    )

    canvas.set_input_field_value_array(op, "Fragment", sample=frags[0])
    canvas.set_input_field_value_array(op, "Fragment", sample=frags[1])

    input_array = op.input_array("Fragment")

    assert (
        len(op.input_array("Fragment")) == 2
    ), "There should be exactly 2 field values in the input array"
    assert (
        input_array[0] != input_array[1]
    ), "Input array field values should be different"

    assert len(op.input_array("Fragment")) == 2
    assert (
        op.input_array("Fragment")[0].sample == frags[0]
    ), "Input array 0 should have fragment 0"
    assert (
        op.input_array("Fragment")[1].sample == frags[1]
    ), "Input array 1 should have fragment 1"
Example #8
0
def test_quick_chain_to_existing_operation_too_many_times(session):
    canvas = Planner(session)
    op = canvas.create_operation_by_name("Yeast Transformation")
    op1 = canvas.chain(op, "Check Yeast Plate")[-1]
    with pytest.raises(PlannerException):
        canvas.chain("Yeast Transformation", op1)
    assert len(canvas.plan.wires) == 1
Example #9
0
def test_add_wire(session):
    canvas = Planner(session)
    assert len(canvas.plan.wires) == 0
    op1 = canvas.create_operation_by_name("Make PCR Fragment", category="Cloning")
    op2 = canvas.create_operation_by_name("Rehydrate Primer", category="Cloning")

    canvas.add_wire(op2.outputs[0], op1.input("Forward Primer"))
    assert len(canvas.plan.wires) == 1
    wire = canvas.plan.wires[0]
    assert (
        wire.source.allowable_field_type.sample_type_id
        == wire.destination.allowable_field_type.sample_type_id
    )
    assert (
        wire.source.allowable_field_type.object_type_id
        == wire.destination.allowable_field_type.object_type_id
    )
Example #10
0
    def test_optimize_case3_array_inputs(self, session):
        """Here we test whether operation types with field_value array inputs
        are mergable despite being in a different order."""
        with session.with_cache() as sess:
            canvas = Planner(sess)

            q = self.sql({
                "object_type_id":
                sess.ObjectType.find_by_name("Fragment Stock").id,
                "location":
                self.Not("deleted"),
            })
            items = sess.Item.last(4, query=q)
            assert items

            assemble_op1 = canvas.create_operation_by_name("Assemble Plasmid")
            assemble_op2 = canvas.create_operation_by_name("Assemble Plasmid")
            assemble_op3 = canvas.create_operation_by_name("Assemble Plasmid")
            assemble_op4 = canvas.create_operation_by_name("Assemble Plasmid")

            assemble_op1.add_to_input_array("Fragment", item=items[0])
            assemble_op1.add_to_input_array("Fragment", item=items[1])

            assemble_op2.add_to_input_array("Fragment", item=items[0])
            assemble_op2.add_to_input_array("Fragment", item=items[1])

            assemble_op3.add_to_input_array("Fragment", item=items[1])
            assemble_op3.add_to_input_array("Fragment", item=items[0])

            assemble_op4.add_to_input_array("Fragment", item=items[1])
            assemble_op4.add_to_input_array("Fragment", item=items[2])

            sample = sess.Sample.one(query={
                "sample_type_id":
                sess.SampleType.find_by_name("Plasmid").id
            })
            canvas.set_field_value(assemble_op1.outputs[0], sample=sample)
            canvas.set_field_value(assemble_op2.outputs[0], sample=sample)
            canvas.set_field_value(assemble_op3.outputs[0], sample=sample)
            canvas.set_field_value(assemble_op4.outputs[0], sample=sample)

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

            canvas.optimize()

            assert len(canvas.plan.operations) == 2
Example #11
0
def test_raise_exception_if_wiring_two_outputs(session):
    canvas = Planner(session)
    assert len(canvas.plan.wires) == 0

    op1 = canvas.create_operation_by_name("Check Plate", category="Cloning")
    op2 = canvas.create_operation_by_name("Check Plate", category="Cloning")

    with pytest.raises(PlannerException):
        canvas.add_wire(op1.outputs[0], op2.outputs[0])
Example #12
0
def test_quick_wire_to_input_array_and_then_set_sample(session):
    canvas = Planner(session)

    frags = session.Sample.where(
        {"sample_type_id": session.SampleType.find_by_name("Fragment").id},
        opts={"limit": 10},
    )

    purify1 = canvas.create_operation_by_name("Purify Gel Slice", category="Cloning")
    purify2 = canvas.create_operation_by_name("Purify Gel Slice", category="Cloning")

    assemble = canvas.create_operation_by_name("Assemble Plasmid", category="Cloning")

    canvas.quick_wire(purify1, assemble)
    canvas.quick_wire(purify2, assemble)

    canvas.set_field_value_and_propogate(purify1.inputs[0], sample=frags[0])

    input_array = assemble.input_array("Fragment")

    print("purify1: " + str(purify1.rid))
    print("purify2: " + str(purify2.rid))

    for i in input_array:
        print(
            i.operation.operation_type.name
            + " "
            + i.name
            + " "
            + str(i.sample)
            + " "
            + str(canvas.get_incoming_wires(i)[0].source.operation.rid)
        )

    print("ljljklj")
    print(purify2.outputs[0].sample)

    assert (
        assemble.input_array("Fragment")[0].sample == frags[0]
    ), "Setting a wire should propogate to a field value"
    assert assemble.input_array("Fragment")[1].sample is None, (
        "Setting a wire should not propogate sample to other field"
        "values in the input array."
    )
Example #13
0
def test_inventory_updater(session):
    ot = session.ObjectType.one(query="rows > 2 AND columns > 2")
    collection = session.Collection.new(object_type=ot)

    primer = session.SampleType.find_by_name("Primer")
    fragment = session.SampleType.find_by_name("Fragment")
    plasmid = session.SampleType.find_by_name("Plasmid")
    yeast = session.SampleType.find_by_name("Yeast Strain")

    frag = fragment.new_sample(
        str(uuid4()),
        project="test",
        description="",
        properties={
            "Forward Primer":
            primer.new_sample(
                str(uuid4()),
                project="test",
                description="",
                properties={
                    "Anneal Sequence": "AGGGATAT",
                    "T Anneal": 50
                },
            ),
            "Length":
            1000,
            "Sequence":
            "",
        },
    )

    collection[0, 0] = frag

    item = session.Item.new(
        object_type=session.ObjectType.find_by_name("Fragment Stock"),
        sample=collection.sample_matrix[0, 0],
    )

    print(item.object_type)

    planner = Planner(session)
    op = planner.create_operation_by_name("Make PCR Fragment")
    planner.set_field_value(op.input("Template"), item=item)

    saved = save_inventory(session, [planner.plan, collection])

    # assert frag in saved
    assert frag.id
    assert frag.properties["Forward Primer"].id
    assert item.id
    assert planner.plan.id
    assert collection.id

    save_inventory(session, [planner.plan, collection], merge_samples=True)
Example #14
0
def test_proper_setting_of_object_types(session):

    canvas = Planner(session)
    yeast = session.Sample.where(
        {"sample_type_id": session.SampleType.find_by_name("Yeast Strain").id},
        opts={"limit": 10},
    )[-1]

    streak = canvas.create_operation_by_name("Streak Plate", category="Yeast")
    glycerol = canvas.create_operation_by_name("Yeast Glycerol Stock", category="Yeast")
    canvas.set_field_value(glycerol.inputs[0], sample=yeast)
    canvas.set_field_value(streak.inputs[0], sample=yeast)
    mating = canvas.create_operation_by_name("Yeast Mating")
    canvas.add_wire(streak.outputs[0], mating.inputs[0])
    canvas.add_wire(glycerol.outputs[0], mating.inputs[1])
    assert (
        mating.inputs[0].allowable_field_type.object_type.name == "Divided Yeast Plate"
    )
    assert (
        mating.inputs[1].allowable_field_type.object_type.name == "Yeast Glycerol Stock"
    )
Example #15
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
Example #16
0
def test_add_successive_operations_with_browser_session(session):
    with session.with_cache() as sess:
        p = Planner(sess)
        p.create_operation_by_name("Make PCR Fragment")
        p.create_operation_by_name("Run Gel")
    assert len(p.plan.operations) == 2
Example #17
0
def test_add_successive_operations(session):
    p = Planner(session)
    p.create_operation_by_name("Make PCR Fragment")
    p.create_operation_by_name("Run Gel")
    assert len(p.plan.operations) == 2
Example #18
0
def test_quick_chain_to_existing_operation(session):
    canvas = Planner(session)
    op = canvas.create_operation_by_name("Yeast Transformation")
    canvas.chain(op, "Check Yeast Plate")
    assert len(canvas.plan.wires) == 1
Example #19
0
def test_add_operation_by_name(session):
    plan = session.Plan.one()
    canvas = Planner(plan)
    canvas.create_operation_by_name("Yeast Transformation")