Beispiel #1
0
def new_model(session):
    """Force creation of a new model."""

    with session.with_cache(timeout=60) as sess:
        operation_type = sess.OperationType.find_by_name("Assemble Plasmid")
        sess.Operation.last(500,
                            query=dict(operation_type_id=operation_type.id))
        sess.browser.get("Operation", "plans")
        plans = sess.browser.get("Plan")
    apm = AutoPlannerModel(session.browser, plans)
    apm.log.set_verbose(True)
    apm.build()
    return apm
Beispiel #2
0
def test_num_requests(session):

    with session.with_cache() as sess:
        browser = sess.browser

        plans = sess.Plan.last(50)

        n1 = sess._aqhttp.num_requests
        model = AutoPlannerModel(browser, plans)
        assert n1 == sess._aqhttp.num_requests

        sess.set_verbose(True)
        model.build()
        print(sess._aqhttp.num_requests - n1)
Beispiel #3
0
    def load_model(self, model_json, filename=None):
        print(os.getcwd())
        if filename and os.path.isfile(filename):
            return AutoPlannerModel.load(filename)
        else:
            if "model_class" not in model_json:
                model_json["model_class"] = "Plan"

            print("Building model...")
            model = AutoPlannerModel(self.session.browser,
                                     plans=self.make_query(model_json))
            model.build()
            if filename:
                model.dump(filename)
            return model
Beispiel #4
0
def autoplan_model(session, datadir, filepath) -> AutoPlannerModel:
    """The default autoplanner object used in tests.

    Preferrably loads a pickled object. If the pickled object does not
    exist, a new autoplanner object is created and pickled. This object
    is then unpickled and used.
    """

    if not os.path.isfile(filepath):
        print("TESTS: No file found with path '{}'".format(filepath))
        print("TESTS: Creating new pickled autoplanner...")
        model = new_model(session)
        model.log.set_verbose(True)
        model.build()
        print("TESTS: dumping {}".format(filepath))
        model.dump(filepath)

    print("TESTS: Loading '{}'".format(filepath))
    model = AutoPlannerModel.load(filepath)
    model.log.set_verbose(False)
    return model
Beispiel #5
0
def test_build_and_plan(session):
    # pull last 10 experimental to build model
    with session.with_cache(timeout=60) as sess:
        model = AutoPlannerModel(sess.browser, plans=sess.Plan.last(10))
        model.build()

        ignore_ots = sess.OperationType.where(
            {"category": ["Control Blocks", "Library Cloning"], "deployed": True}
        )
        ignore_ots += sess.OperationType.where({"name": "Yeast Mating"})
        ignore_ots += sess.OperationType.where(
            {"name": "Yeast Auxotrophic Plate Mating"}
        )
        ignore = [ot.id for ot in ignore_ots]
        model.add_model_filter(
            "AllowableFieldType",
            AutoPlannerModel.EXCLUDE_FILTER,
            lambda m: m.field_type.parent_id in ignore,
        )

        edges = [
            ("DTBA_backboneA_splitAMP", "pyMOD-URA-URA3.A.1-pGPD-yeVenus-tCYC1"),
            ("T1MC_NatMX-Cassette_MCT2 (JV)", "pyMOD-URA-URA3.A.1-pGPD-yeVenus-tCYC1"),
            (
                "BBUT_URA3.A.0_homology1_UTP1 (from genome)",
                "pyMOD-URA-URA3.A.1-pGPD-yeVenus-tCYC1",
            ),
            ("DH5alpha", "pyMOD-URA-URA3.A.1-pGPD-yeVenus-tCYC1"),
            ("TP-IRES-EGFP-TS", "pyMOD-URA-URA3.A.1-pGPD-yeVenus-tCYC1"),
            (
                "pyMOD-URA-URA3.A.1-pGPD-yeVenus-tCYC1",
                "CEN.PK2 - MAT alpha | his-pGRR-W5-W8-RGR-W36",
            ),
        ]
        factory = NetworkFactory(model)
        network = factory.new_from_edges(edges)

        ignore_items = []  # optional to not include certain items in the search.
        desired_object_type = sess.ObjectType.find_by_name("Yeast Glycerol Stock")

        network.run(desired_object_type, ignore=ignore_items)
        print("template graph")
        print(nx.info(model.template_graph.graph))

        print("graph")
        print(nx.info(network.solution.graph.graph))
        plan = network.plan()
Beispiel #6
0
 def func():
     browser.clear()
     model = AutoPlannerModel(browser, plans)
     model.build()
Beispiel #7
0
    def create_sample_composition_graphs(self, template_graph, browser,
                                         sample_composition):
        """Break a template_graph into subgraphs comprising of individual
        samples obtained from the sample composition graph.

        The `sample_composition` graph is a directed graph that defines how samples may
        be built from other samples. Meanwhile, the `template_graph` defines all
        possible connections between :class:`AllowableFieldType` and the associated
        weights of each edge determined from the :class:`AutoPlannerModel`. Using the
        `sample_composition` graph, we grab individual subgraphs from the template graph
        for each node in the sample compositions graph (using SampleType). The edges
        of the `sample_composition` graph determines which edges of these new subgraphs
         can be connected to each other, forming the final graph used in the Steiner
         tree optimization algorithm.

        :param template_graph:
        :param browser:
        :param sample_composition:
        :return:
        """
        sample_edges = []
        graphs = []

        samples = []
        for item in sample_composition.nodes:
            samples.append(sample_composition.nodes[item]["sample"])
        sample_graph_dict = self.decompose_template_graph_into_samples(
            template_graph, samples)

        for s1, s2 in sample_composition.edges:
            s1 = sample_composition.nodes[s1]["sample"]
            s2 = sample_composition.nodes[s2]["sample"]

            sample_graph1 = sample_graph_dict[s1.id]
            sample_graph2 = sample_graph_dict[s2.id]

            graphs += [sample_graph1.graph, sample_graph2.graph]

            input_afts = [
                aft for aft in sample_graph1.iter_models("AllowableFieldType")
                if aft.field_type.role == "input"
            ]
            output_afts = [
                aft for aft in sample_graph2.iter_models("AllowableFieldType")
                if aft.field_type.role == "output"
            ]

            pairs = AutoPlannerModel._match_internal_afts(
                input_afts, output_afts)
            pairs = [(sample_graph1.node_id(aft1), sample_graph2.node_id(aft2))
                     for aft1, aft2 in pairs]
            sample_edges += pairs

        # include the afts from operations that have no sample_type_id (e.g. Order Primer)
        if None in sample_graph_dict:
            graphs.append(sample_graph_dict[None].graph)
            # TODO: add None edges for internal graphs...

        graph = BrowserGraph(browser)
        graph.graph = nx.compose_all(graphs)

        graph.cache_models()

        self.log.info("Adding {} sample-to-sample edges".format(
            len(sample_edges)))

        for n1, n2 in tqdm(sample_edges):
            assert n1 in graph
            assert n2 in graph

            node1 = graph.get_node(n1)
            node2 = graph.get_node(n2)
            aft1 = node1["model"]
            aft2 = node2["model"]

            x = (template_graph.node_id(aft1), template_graph.node_id(aft2))
            edge = template_graph.get_edge(*x)
            graph.add_edge(n1,
                           n2,
                           edge_type="sample_to_sample",
                           weight=edge["weight"])

        afts = list(graph.iter_models(model_class="AllowableFieldType"))
        browser.retrieve(afts, "field_type")
        sample_ids = list({
            ndata["sample"].id
            for _, ndata in graph.iter_model_data()
            if ndata["sample"] is not None
        })

        ##############################
        # Get items
        ##############################

        non_part_afts = [aft for aft in afts if not aft.field_type.part]
        object_type_ids = list({aft.object_type_id for aft in non_part_afts})

        self._cinfo(
            "finding all relevant items for {} samples and {} object_types".
            format(len(sample_ids), len(object_type_ids)))
        items = browser.where(
            model_class="Item",
            query={
                "sample_id": sample_ids,
                "object_type_id": object_type_ids
            },
        )
        items = [item for item in items if item.location != "deleted"]
        self.log.info("{} total items found".format(len(items)))
        items_by_object_type_id = defaultdict(list)
        for item in items:
            items_by_object_type_id[item.object_type_id].append(item)

        ##############################
        # Get parts
        ##############################

        self._cinfo("finding relevant parts/collections")
        part_by_sample_by_type = self._find_parts_for_samples(browser,
                                                              sample_ids,
                                                              lim=50)
        self._cinfo("found {} collection types".format(
            len(part_by_sample_by_type)))

        ##############################
        # Assign Items/Parts/Collections
        ##############################

        new_items = []
        new_edges = []
        for node, ndata in graph.iter_model_data(
                model_class="AllowableFieldType"):
            aft = ndata["model"]
            sample = ndata["sample"]
            if sample:
                sample_id = sample.id
                sample_type_id = sample.sample_type_id
            else:
                sample_id = None
                sample_type_id = None
            if aft.sample_type_id == sample_type_id:
                if aft.field_type.part:
                    parts = part_by_sample_by_type.get(aft.object_type_id,
                                                       {}).get(sample_id, [])
                    for part in parts[-1:]:
                        if part.sample_id == sample_id:
                            new_items.append(part)
                            new_edges.append((part, sample, node))
                else:
                    items = items_by_object_type_id[aft.object_type_id]
                    for item in items:
                        if item.sample_id == sample_id:
                            new_items.append(item)
                            new_edges.append((item, sample, node))

        for item in new_items:
            graph.add_model(item)

        for item, sample, node in new_edges:
            graph.add_edge(graph.node_id(item), node, weight=0)

        self.log.info("{} items added to various allowable_field_types".format(
            len(new_edges)))
        return graph
Beispiel #8
0
 def load_model(cls, path):
     model = AutoPlannerModel.load(path)
     return cls(model)
Beispiel #9
0
def test_model_bulid(session, plans):
    model = AutoPlannerModel(Browser(session), plans=plans)
    model.build()
Beispiel #10
0
def test_init_from_plans(session, plans):
    model = AutoPlannerModel(Browser(session), plans=plans)
    assert model
Beispiel #11
0
def test_build(session, tmp_path):
    # pull last 10 experimental to build model
    with session.with_cache(timeout=60) as sess:
        model = AutoPlannerModel(sess.browser, plans=sess.Plan.last(10))
        model.build()
        model.save(join(tmp_path, "tmpmodel.pkl"))