def run(self, triplepack, env):
        identifiers.swap_version(env.version)
        if env.version == "sbol_2":
            return self.run_sbol_2(triplepack,env)

        if env.version == "sbol_3":
            return self.run_sbol_3(triplepack,env)
Example #2
0
    def run(self, triplepack, env):
        subjects = list(triplepack.subjects)
        identifiers.swap_version("sbol_2")
        for i in range(len(subjects)):
            SBOLCompliant(subjects[i]).run(triplepack, subjects)

        validate(subjects, triplepack)

        return triplepack
Example #3
0
def produce_shortbol(sbol_xml_fn,
                     shortbol_libary,
                     output_fn=None,
                     no_validation=False,
                     prune=False,
                     prune_list=None,
                     no_enhancement=False,
                     version="sbol_2"):
    if version == "sbol_3":
        identifiers.swap_version("sbol_3")
        no_validation = True

    # Perform full file validation before producing ShortBOL.
    if not no_validation and not general_validation(sbol_xml_fn):
        print("Warn:: Can't validate input.")
    g = rdflib.Graph()
    g.load(sbol_xml_fn)

    # Manipulate the triplepack to move from a graph structure to
    # an easier to use tree form.
    heirachy_tree = {}
    tree_roots = find_graph_roots(g)
    # We can (and likely will with larger designs) have
    # multiple roots which means multiple trees,
    # So the first level of the dict is multiple roots.
    for root in tree_roots:
        heirachy_tree[str(root[0])] = get_tree(g,
                                               root[0],
                                               prune=prune,
                                               prune_list=prune_list)

    if len(heirachy_tree.keys()) == 0:
        return output_fn
    # Now have a structure that is easier to use.
    # Create the actual ShortBOL from this.
    shortbol_code = convert(heirachy_tree, shortbol_libary, no_enhancement,
                            version)

    if output_fn is None:
        print(shortbol_code)
        return shortbol_code
    else:
        if os.path.isfile(output_fn):
            os.remove(output_fn)
        f = open(output_fn, "a")
        f.write(shortbol_code)
        f.close()
        return output_fn
    return
    def run_sbol_3(self,triplepack,env):
        identifiers.swap_version("sbol_3")
        if not is_param_valid(triplepack, self.cd_name):
            raise ValueError("Extension Parameter is Invalid")

        template = get_template(triplepack,self.cd_name)
        variable_component_names = triplepack.search((self.cd_name,identifiers.predicates.has_variable_component,None))
        
        for variable_component_name in variable_component_names:
            variable = triplepack.search((variable_component_name[2],identifiers.predicates.variable,None))[0]
            variants = get_variants(variable_component_name[2], triplepack)

            if variable[2] not in [n[2] for n in template]:
                raise ValueError(f'The variable object {variable[2]} is not a sub-component of the Template {template[0][0]}.')
            
            for variant in variants:
                new_template_name = Uri(template[0][0].uri + "_" + variable_component_name[2].split()[-1] + "_" + variant[2].split()[-1])
                for s,p,o in template:
                    # When the triple pertains to the variable component.
                    if variable[2] == o:
                        # Variant is a CD need Component
                        component_name = generate_name(variant,o)
                        component = generate_sub_component(variant[2],component_name)
                        for c_t in component:
                            triplepack.add((c_t[0],c_t[1],c_t[2]))
                        o = component_name

                    # A component that is NOT the variable component.
                    elif p == identifiers.predicates.has_feature:
                        component_name = generate_name(variant,o)
                        definition = triplepack.search((o, identifiers.predicates.instance_of, None))[0]
                        component = generate_sub_component(definition[2], component_name)
                        locations = triplepack.search((o,identifiers.predicates.has_location,None))
                        if len(locations) != 0:
                            for location in locations:
                                component = component + generate_location_sbol_3(location,definition,triplepack)
                        for c_t in component:
                            triplepack.add((c_t[0],c_t[1],c_t[2]))
                        o = component_name
                        
                    # A Sequence Constraint.
                    elif p == identifiers.predicates.has_constraint:
                        # A constraint requires:
                        # A new name
                        constraint_name = generate_name(variant,o)
                        # The previous constraint being copied triples.
                        constraint_triples = triplepack.search((o,None,None))
                        # The New Subject and Object triples.
                        # Note there is a de-link here where we are assuming that the name of the subject and object.
                        subject = triplepack.search((o,identifiers.predicates.subject,None))
                        object = triplepack.search((o,identifiers.predicates.object,None))
                        subject_name = generate_name(variant,subject[0][2])
                        object_name = generate_name(variant,object[0][2])

                        constraint = generate_constraint(constraint_triples,constraint_name,subject_name,object_name)
                        for c_t in constraint:
                            triplepack.add((c_t[0],c_t[1],c_t[2]))
                        o = constraint_name
                    # Any other triples that can just be copied directly (names, descriptions etc)
                    else:
                        pass

                    triplepack.add((new_template_name, p, o))

            for n in triplepack.search((variable_component_name[2],None,None)):               
                triplepack.triples.remove(n)
        for triple in triplepack.search((self.cd_name,None,None)):
            triplepack.triples.remove(triple)
        return triplepack