From 5b438cba853f151cad27f5e1c67d6b1c1fa587db Mon Sep 17 00:00:00 2001 From: Daniel Bryce Date: Fri, 27 Oct 2023 07:42:44 -0500 Subject: [PATCH] remove import for constants --- .../protocols/growth-curve/growth_curve.py | 3 +- .../opentrons_ludox_example.py | 2 +- .../opentrons-pcr/opentrons_pcr_example.py | 457 +++++++++--------- .../opentrons-toy/opentrons_toy_protocol.py | 3 +- test/test_inputs.py | 10 +- 5 files changed, 237 insertions(+), 238 deletions(-) diff --git a/examples/protocols/growth-curve/growth_curve.py b/examples/protocols/growth-curve/growth_curve.py index 9d2b0af..005b940 100644 --- a/examples/protocols/growth-curve/growth_curve.py +++ b/examples/protocols/growth-curve/growth_curve.py @@ -2,13 +2,14 @@ import tyto import labop -from labop.constants import rpm from labop.execution.harness import ProtocolHarness, ProtocolSpecialization from labop.strings import Strings from labop_convert.markdown.markdown_specialization import MarkdownSpecialization def generate_protocol(doc, protocol: labop.Protocol) -> labop.Protocol: + from labop.constants import rpm + doc.add(rpm) ############################################# # Create the protocols diff --git a/examples/protocols/opentrons/opentrons-ludox/opentrons_ludox_example.py b/examples/protocols/opentrons/opentrons-ludox/opentrons_ludox_example.py index e27ce02..9649655 100644 --- a/examples/protocols/opentrons/opentrons-ludox/opentrons_ludox_example.py +++ b/examples/protocols/opentrons/opentrons-ludox/opentrons_ludox_example.py @@ -2,7 +2,6 @@ import tyto import labop -from labop.constants import PREFIX_MAP, ddh2o, ludox from labop.execution.harness import ProtocolHarness, ProtocolSpecialization from labop.protocol import Protocol from labop.strings import Strings @@ -14,6 +13,7 @@ def generate_protocol(doc: sbol3.Document, activity: Protocol) -> Protocol: # create the materials to be provisioned + from labop.constants import PREFIX_MAP, ddh2o, ludox doc.add(ddh2o) doc.add(ludox) diff --git a/examples/protocols/opentrons/opentrons-pcr/opentrons_pcr_example.py b/examples/protocols/opentrons/opentrons-pcr/opentrons_pcr_example.py index cc91f22..a5b4623 100644 --- a/examples/protocols/opentrons/opentrons-pcr/opentrons_pcr_example.py +++ b/examples/protocols/opentrons/opentrons-pcr/opentrons_pcr_example.py @@ -1,13 +1,10 @@ import csv +import os -import rdflib as rdfl import sbol3 import tyto import labop -import uml -from labop.constants import PREFIX_MAP, ddh2o, ludox -from labop.execution.execution_engine import ExecutionEngine from labop_convert.opentrons.opentrons_specialization import OT2Specialization # Dev Note: This is a test of the initial version of the OT2 specialization. Any specs shown here can be changed in the future. Use at your own risk. Here be dragons. @@ -91,245 +88,245 @@ def load_pcr_plan(fname: str, doc: sbol3.Document): return plan -############################################# -# set up the document -print("Setting up document") -doc = sbol3.Document() -sbol3.set_namespace("https://bbn.com/scratch/") - -############################################# -# Import the primitive libraries -print("Importing libraries") -labop.import_library("liquid_handling") -print("... Imported liquid handling") -labop.import_library("plate_handling") -print("... Imported plate handling") -labop.import_library("spectrophotometry") -print("... Imported spectrophotometry") -labop.import_library("sample_arrays") -print("... Imported sample arrays") -labop.import_library("pcr") -print("... Imported pcr") - -############################3 -# load pcr setup -primer_layout = load_primer_layout("primer_layout.csv", doc) -templates = load_templates("pcr_plan.csv", doc) -pcr_plan = load_pcr_plan("pcr_plan.csv", doc) - - -activity = labop.Protocol("pcr_example") -activity.name = "Opentrons PCR Demo" -doc.add(activity) - - -doc.add(ddh2o) -doc.add(ludox) - -p300 = sbol3.Agent("p300_single", name="P300 Single") -doc.add(p300) - -# Configure OT2 and load it with labware -load = activity.primitive_step( - "ConfigureRobot", - instrument=OT2Specialization.EQUIPMENT["p300_single"], - mount="left", -) -load = activity.primitive_step( - "ConfigureRobot", - instrument=OT2Specialization.EQUIPMENT["thermocycler"], - mount="7", -) -# load = protocol.primitive_step('ConfigureRobot', instrument=OT2Specialization.EQUIPMENT['thermocycler'], mount='10') -spec_tiprack = labop.ContainerSpec( - "tiprack", queryString="cont:Opentrons96TipRack300uL", prefixMap=PREFIX_MAP -) -load = activity.primitive_step( - "LoadRackOnInstrument", rack=spec_tiprack, coordinates="1" -) - - -# Set up rack for reagents -reagent_rack = labop.ContainerSpec( - "reagent_rack", - name="Tube rack for reagents", - queryString="cont:Opentrons24TubeRackwithEppendorf1.5mLSafe-LockSnapcap", - prefixMap=PREFIX_MAP, -) -rack = activity.primitive_step("EmptyRack", specification=reagent_rack) -load_rack = activity.primitive_step( - "LoadRackOnInstrument", rack=reagent_rack, coordinates="2" -) - -# Set up primers plate -primer_plate = labop.ContainerSpec( - "primer_plate", - name="primers in 96-well plate", - queryString="cont:Corning96WellPlate360uLFlat", - prefixMap=PREFIX_MAP, -) -load = activity.primitive_step( - "LoadRackOnInstrument", rack=primer_plate, coordinates="3" -) -primer_plate = activity.primitive_step("EmptyContainer", specification=primer_plate) - -# Set up DNA polymerase -polymerase = labop.ContainerSpec( - "polymerase", - name="DNA Polymerase", - queryString="cont:StockReagent", - prefixMap=PREFIX_MAP, -) -load_reagents = activity.primitive_step( - "LoadContainerInRack", - slots=rack.output_pin("slots"), - container=polymerase, - coordinates="A1", -) - -# Set up water -load_water = activity.primitive_step( - "LoadContainerInRack", - slots=rack.output_pin("slots"), - container=labop.ContainerSpec( - "water", - name="tube for water", - queryString="cont:MicrofugeTube", +def create_protocol(doc, activity: labop.Protocol) -> labop.Protocol: + from labop.constants import PREFIX_MAP, ddh2o, ludox + + ############################3 + # load pcr setup + primer_layout = load_primer_layout("primer_layout.csv", doc) + templates = load_templates("pcr_plan.csv", doc) + pcr_plan = load_pcr_plan("pcr_plan.csv", doc) + + doc.add(ddh2o) + doc.add(ludox) + + p300 = sbol3.Agent("p300_single", name="P300 Single") + doc.add(p300) + + # Configure OT2 and load it with labware + load = activity.primitive_step( + "ConfigureRobot", + instrument=OT2Specialization.EQUIPMENT["p300_single"], + mount="left", + ) + load = activity.primitive_step( + "ConfigureRobot", + instrument=OT2Specialization.EQUIPMENT["thermocycler"], + mount="7", + ) + # load = protocol.primitive_step('ConfigureRobot', instrument=OT2Specialization.EQUIPMENT['thermocycler'], mount='10') + spec_tiprack = labop.ContainerSpec( + "tiprack", + queryString="cont:Opentrons96TipRack300uL", prefixMap=PREFIX_MAP, - ), - coordinates="B1", -) -provision_water = activity.primitive_step( - "Provision", - resource=ddh2o, - destination=load_water.output_pin("samples"), - amount=sbol3.Measure(500, tyto.OM.microliter), -) - - -# Template DNA samples are assumed to be in microfuge tubes -# which will be added to a tube rack -template_layout = {} -for coordinate, template in templates.to_dict().items(): - template = doc.find(template) - template_container = labop.ContainerSpec( - template.display_id + "_container", - name="container of " + template.name, - queryString="cont:MicrofugeTube", + ) + load = activity.primitive_step( + "LoadRackOnInstrument", rack=spec_tiprack, coordinates="1" + ) + + # Set up rack for reagents + reagent_rack = labop.ContainerSpec( + "reagent_rack", + name="Tube rack for reagents", + queryString="cont:Opentrons24TubeRackwithEppendorf1.5mLSafe-LockSnapcap", prefixMap=PREFIX_MAP, ) - load_template = activity.primitive_step( - "LoadContainerInRack", - slots=rack.output_pin("slots"), - container=template_container, - coordinates=coordinate, + rack = activity.primitive_step("EmptyRack", specification=reagent_rack) + load_rack = activity.primitive_step( + "LoadRackOnInstrument", rack=reagent_rack, coordinates="2" ) - template_layout[template.name] = load_template.output_pin("samples") - -# Set up PCR machine -pcr_plate = labop.ContainerSpec( - "pcr_plate", - name="PCR plate", - queryString="cont:Biorad96WellPCRPlate", - prefixMap=PREFIX_MAP, -) -load_pcr_plate_on_thermocycler = activity.primitive_step( - "LoadContainerOnInstrument", - specification=pcr_plate, - instrument=OT2Specialization.EQUIPMENT["thermocycler"], - slots="A1:H12", -) - -# Pipette PCR reactions -for target_well, reaction in pcr_plan.items(): - f_primer = reaction["Forward"] - [f_primer_coordinates] = [ - c for c, p in primer_layout.to_dict().items() if doc.find(p).name == f_primer - ] - r_primer = reaction["Reverse"] - [r_primer_coordinates] = [ - c for c, p in primer_layout.to_dict().items() if doc.find(p).name == r_primer - ] - template = template_layout[reaction["Template"]] - target_sample = activity.primitive_step( - "PlateCoordinates", - source=load_pcr_plate_on_thermocycler.output_pin("samples"), - coordinates=target_well, + # Set up primers plate + primer_plate = labop.ContainerSpec( + "primer_plate", + name="primers in 96-well plate", + queryString="cont:Corning96WellPlate360uLFlat", + prefixMap=PREFIX_MAP, ) + load = activity.primitive_step( + "LoadRackOnInstrument", rack=primer_plate, coordinates="3" + ) + primer_plate = activity.primitive_step("EmptyContainer", specification=primer_plate) - # Transfer water - transfer = activity.primitive_step( - "Transfer", - source=load_water.output_pin("samples"), - destination=target_sample.output_pin("samples"), - amount=sbol3.Measure(6, tyto.OM.microliter), + # Set up DNA polymerase + polymerase = labop.ContainerSpec( + "polymerase", + name="DNA Polymerase", + queryString="cont:StockReagent", + prefixMap=PREFIX_MAP, + ) + load_reagents = activity.primitive_step( + "LoadContainerInRack", + slots=rack.output_pin("slots"), + container=polymerase, + coordinates="A1", ) - transfer.name = "Add water" - # Transfer forward primer - f_primer_sample = activity.primitive_step( - "PlateCoordinates", - source=primer_plate.output_pin("samples"), - coordinates=f_primer_coordinates, + # Set up water + load_water = activity.primitive_step( + "LoadContainerInRack", + slots=rack.output_pin("slots"), + container=labop.ContainerSpec( + "water", + name="tube for water", + queryString="cont:MicrofugeTube", + prefixMap=PREFIX_MAP, + ), + coordinates="B1", ) - transfer = activity.primitive_step( - "Transfer", - source=f_primer_sample.output_pin("samples"), - destination=target_sample.output_pin("samples"), - amount=sbol3.Measure(1, tyto.OM.microliter), + provision_water = activity.primitive_step( + "Provision", + resource=ddh2o, + destination=load_water.output_pin("samples"), + amount=sbol3.Measure(500, tyto.OM.microliter), ) - transfer.name = "Add F primer" - # Transfer reverse primer - r_primer_sample = activity.primitive_step( - "PlateCoordinates", - source=primer_plate.output_pin("samples"), - coordinates=r_primer_coordinates, + # Template DNA samples are assumed to be in microfuge tubes + # which will be added to a tube rack + template_layout = {} + for coordinate, template in templates.to_dict().items(): + template = doc.find(template) + template_container = labop.ContainerSpec( + template.display_id + "_container", + name="container of " + template.name, + queryString="cont:MicrofugeTube", + prefixMap=PREFIX_MAP, + ) + load_template = activity.primitive_step( + "LoadContainerInRack", + slots=rack.output_pin("slots"), + container=template_container, + coordinates=coordinate, + ) + template_layout[template.name] = load_template.output_pin("samples") + + # Set up PCR machine + pcr_plate = labop.ContainerSpec( + "pcr_plate", + name="PCR plate", + queryString="cont:Biorad96WellPCRPlate", + prefixMap=PREFIX_MAP, ) - transfer = activity.primitive_step( - "Transfer", - source=r_primer_sample.output_pin("samples"), - destination=target_sample.output_pin("samples"), - amount=sbol3.Measure(1, tyto.OM.microliter), + load_pcr_plate_on_thermocycler = activity.primitive_step( + "LoadContainerOnInstrument", + specification=pcr_plate, + instrument=OT2Specialization.EQUIPMENT["thermocycler"], + slots="A1:H12", ) - transfer.name = "Add R primer" - - # Transfer template - transfer = activity.primitive_step( - "Transfer", - source=template, - destination=target_sample.output_pin("samples"), - amount=sbol3.Measure(1, tyto.OM.microliter), + + # Pipette PCR reactions + for target_well, reaction in pcr_plan.items(): + f_primer = reaction["Forward"] + [f_primer_coordinates] = [ + c + for c, p in primer_layout.to_dict().items() + if doc.find(p).name == f_primer + ] + r_primer = reaction["Reverse"] + [r_primer_coordinates] = [ + c + for c, p in primer_layout.to_dict().items() + if doc.find(p).name == r_primer + ] + template = template_layout[reaction["Template"]] + + target_sample = activity.primitive_step( + "PlateCoordinates", + source=load_pcr_plate_on_thermocycler.output_pin("samples"), + coordinates=target_well, + ) + + # Transfer water + transfer = activity.primitive_step( + "Transfer", + source=load_water.output_pin("samples"), + destination=target_sample.output_pin("samples"), + amount=sbol3.Measure(6, tyto.OM.microliter), + ) + transfer.name = "Add water" + + # Transfer forward primer + f_primer_sample = activity.primitive_step( + "PlateCoordinates", + source=primer_plate.output_pin("samples"), + coordinates=f_primer_coordinates, + ) + transfer = activity.primitive_step( + "Transfer", + source=f_primer_sample.output_pin("samples"), + destination=target_sample.output_pin("samples"), + amount=sbol3.Measure(1, tyto.OM.microliter), + ) + transfer.name = "Add F primer" + + # Transfer reverse primer + r_primer_sample = activity.primitive_step( + "PlateCoordinates", + source=primer_plate.output_pin("samples"), + coordinates=r_primer_coordinates, + ) + transfer = activity.primitive_step( + "Transfer", + source=r_primer_sample.output_pin("samples"), + destination=target_sample.output_pin("samples"), + amount=sbol3.Measure(1, tyto.OM.microliter), + ) + transfer.name = "Add R primer" + + # Transfer template + transfer = activity.primitive_step( + "Transfer", + source=template, + destination=target_sample.output_pin("samples"), + amount=sbol3.Measure(1, tyto.OM.microliter), + ) + transfer.name = "Add template" + + # Transfer polymerase + transfer = activity.primitive_step( + "Transfer", + source=load_reagents.output_pin("samples"), + destination=target_sample.output_pin("samples"), + amount=sbol3.Measure(1, tyto.OM.microliter), + ) + transfer.name = "Add polymerase" + + pcr = activity.primitive_step( + "PCR", + denaturation_temp=sbol3.Measure(98.0, tyto.OM.degree_Celsius), + denaturation_time=sbol3.Measure(10, tyto.OM.second), + annealing_temp=sbol3.Measure(45.0, tyto.OM.degree_Celsius), + annealing_time=sbol3.Measure(5, tyto.OM.second), + extension_temp=sbol3.Measure(65.0, tyto.OM.degree_Celsius), + extension_time=sbol3.Measure(60, tyto.OM.second), + cycles=30, ) - transfer.name = "Add template" - - # Transfer polymerase - transfer = activity.primitive_step( - "Transfer", - source=load_reagents.output_pin("samples"), - destination=target_sample.output_pin("samples"), - amount=sbol3.Measure(1, tyto.OM.microliter), + + return activity + + +if __name__ == "__main__": + harness = labop.execution.harness.ProtocolHarness( + base_dir=os.path.dirname(__file__), + entry_point=create_protocol, + artifacts=[ + labop.execution.harness.ProtocolSpecialization( + specialization=OT2Specialization("ot2_pcr_labop") + ) + ], + libraries=[ + "liquid_handling", + "plate_handling", + "spectrophotometry", + "sample_arrays", + "pcr", + ], + namespace="https://bbn.com/scratch/", + protocol_name="pcr_example", + protocol_long_name="Opentrons PCR Demo", + protocol_version="1.0", + agent="ot2_machine", + execution_flags={"failsafe": False}, + execution_id="test_execution", ) - transfer.name = "Add polymerase" - -pcr = activity.primitive_step( - "PCR", - denaturation_temp=sbol3.Measure(98.0, tyto.OM.degree_Celsius), - denaturation_time=sbol3.Measure(10, tyto.OM.second), - annealing_temp=sbol3.Measure(45.0, tyto.OM.degree_Celsius), - annealing_time=sbol3.Measure(5, tyto.OM.second), - extension_temp=sbol3.Measure(65.0, tyto.OM.degree_Celsius), - extension_time=sbol3.Measure(60, tyto.OM.second), - cycles=30, -) - -filename = "ot2_pcr_labop" -agent = sbol3.Agent("ot2_machine", name="OT2 machine") -ee = ExecutionEngine(specializations=[OT2Specialization(filename)], failsafe=False) -parameter_values = [] -execution = ee.execute(activity, agent, id="test_execution") - -# ee = ExecutionEngine(specializations=[MarkdownSpecialization(filename + '.md')], failsafe=False, sample_format='json') -# parameter_values = [] -# execution = ee.execute(protocol, agent, id="test_execution") + harness.run(verbose=True) diff --git a/examples/protocols/opentrons/opentrons-toy/opentrons_toy_protocol.py b/examples/protocols/opentrons/opentrons-toy/opentrons_toy_protocol.py index 34baed8..02f48ba 100644 --- a/examples/protocols/opentrons/opentrons-toy/opentrons_toy_protocol.py +++ b/examples/protocols/opentrons/opentrons-toy/opentrons_toy_protocol.py @@ -5,7 +5,6 @@ from sbol3 import Document import labop -from labop.constants import PREFIX_MAP from labop.execution import ProtocolHarness, ProtocolSpecialization from labop.protocol import Protocol from labop.strings import Strings @@ -46,6 +45,8 @@ def create_protocol() -> labop.Protocol: def get_container(protocol: labop.Protocol, container_name: str, container_type: str): + from labop.constants import PREFIX_MAP + query_string = REVERSE_LABWARE_MAP[container_type] plate_spec = labop.ContainerSpec( container_name.replace(" ", "_"), diff --git a/test/test_inputs.py b/test/test_inputs.py index 1f65abf..b626eb1 100644 --- a/test/test_inputs.py +++ b/test/test_inputs.py @@ -1,13 +1,10 @@ +import os import unittest import sbol3 -import tyto import labop -import uml from labop.execution.execution_engine import ExecutionEngine -from labop_convert import MarkdownSpecialization -from labop_convert.behavior_specialization import DefaultBehaviorSpecialization PARAMETER_IN = "http://bioprotocols.org/uml#in" PARAMETER_OUT = "http://bioprotocols.org/uml#out" @@ -69,7 +66,10 @@ def container_set_execution(record): container = parameter_value_map["specification"] samples = parameter_value_map["samples"] - ee = ExecutionEngine() + if not os.path.exists("out"): + os.makedirs("out", exist_ok=True) + + ee = ExecutionEngine(out_dir="out") ee.specializations[0]._behavior_func_map[p.identity] = lambda record, ex: None ex = ee.execute( protocol,