SynBioDex / pySBOL3

Native python implementation of SBOL 3.0 specification
MIT License
36 stars 16 forks source link

Any elegant way to replace one feature with another? #207

Open jakebeal opened 3 years ago

jakebeal commented 3 years ago

I'm running into an interesting challenge in constructing derived Component objects that instantiate a ComponentDerivation.

The way I've been approaching this is to clone the template Component and then replace each variable Feature with SubComponent providing the actual specified value. Doing so, however, breaks all of the links that point to the variable, so I need to then manually go through every property of the Component looking for references to the replaced Features and changing them to point to the new one instead.

This suggests a needed workflow and/or helper function to me, which would replace a Feature with another Feature (and likely can be generalized). I see at least two ways to do it, and am not certain which is preferable.

  1. Have clone take an optional map argument turning it into "clone with replacements", i.e., change not just the identity but substitute child objects according to the map (e.g., "clone this, but change Feature A into Feature B and Constraint X into Constraint Y")
  2. Provide general support for rewriting property values, e.g., "Replace Feature A with Feature B, including all references to A".
jakebeal commented 3 years ago

An example of what this looks like in my current workflow:

   def replace_feature(component, old, new):
        component.features.remove(old)
        component.features.append(new)
        # should be more thorough, but kludging to just look at constraints
        for ct in component.constraints:
            if ct.subject == old.identity: ct.subject = new.identity
            if ct.object == old.identity: ct.object = new.identity
jakebeal commented 3 years ago

Another example of where this workflow is needed: in the iGEM distribution work, we find that there are multiple ways to refer to an object (e.g., the parts.igem.org reference vs. the synbiohub reference; NCBI references with or without their version tag). I need to unify these by replacing the non-canonical aliases with the canonical reference.

jakebeal commented 2 years ago

Test case will be based on the template copies in https://github.com/SynBioDex/SBOL-utilities/blob/develop/sbol_utilities/expand_combinatorial_derivations.py