|
| 1 | +from typing import Dict, Iterable, List, Union, Set, Optional |
| 2 | + |
| 3 | +import sbol3 |
| 4 | +import tyto |
| 5 | + |
| 6 | +from sbol_utilities.workarounds import get_parent, id_sort |
| 7 | + |
| 8 | + |
| 9 | +# TODO: consider allowing return of LocalSubComponent and ExternallyDefined |
| 10 | +def contained_components(roots: Union[sbol3.TopLevel, Iterable[sbol3.TopLevel]]) -> Set[sbol3.Component]: |
| 11 | + """Find the set of all SBOL Components contained within the roots or their children |
| 12 | + This will explore via Collection.member relations and Component.feature relations |
| 13 | +
|
| 14 | + :param roots: single TopLevel or iterable collection of TopLevel objects to explore |
| 15 | + :return: set of Components found, including roots |
| 16 | + """ |
| 17 | + if isinstance(roots, sbol3.TopLevel): |
| 18 | + roots = [roots] |
| 19 | + explored = set() # set being built via traversal |
| 20 | + |
| 21 | + # subfunction for walking containment tree |
| 22 | + def walk_tree(obj: sbol3.TopLevel): |
| 23 | + if obj not in explored: |
| 24 | + explored.add(obj) |
| 25 | + if isinstance(obj, sbol3.Component): |
| 26 | + for f in (f.instance_of.lookup() for f in obj.features if isinstance(f, sbol3.SubComponent)): |
| 27 | + walk_tree(f) |
| 28 | + elif isinstance(obj, sbol3.Collection): |
| 29 | + for m in obj.members: |
| 30 | + walk_tree(m.lookup()) |
| 31 | + for r in roots: |
| 32 | + walk_tree(r) |
| 33 | + # filter result for containers: |
| 34 | + return {c for c in explored if isinstance(c, sbol3.Component)} |
| 35 | + |
| 36 | + |
| 37 | +def ensure_singleton_feature(system: sbol3.Component, target: Union[sbol3.Feature, sbol3.Component]): |
| 38 | + """Return a feature associated with the target, i.e., the target itself if a feature, or a SubComponent |
| 39 | + If the target is not already in the system, add it. |
| 40 | + Raises ValueError if given a Component with multiple instances |
| 41 | +
|
| 42 | + :return: associated feature |
| 43 | + """ |
| 44 | + if isinstance(target, sbol3.Feature): # features are returned directly |
| 45 | + if target not in system.features: |
| 46 | + system.features.append(target) |
| 47 | + return target |
| 48 | + instances = [f for f in system.features if isinstance(f, sbol3.SubComponent) and f.instance_of == target.identity] |
| 49 | + if len(instances) == 1: # if there is precisely one SubComponent, return it |
| 50 | + return instances[0] |
| 51 | + elif not len(instances): # if there are no SubComponents, add one |
| 52 | + return add_feature(system, target) |
| 53 | + else: # if there are multiple SubComponents, raise an exception |
| 54 | + raise ValueError(f'Ambiguous reference: {len(instances)} instances of {target.identity} in {system.identity}') |
| 55 | + |
| 56 | + |
| 57 | +def ensure_singleton_system(system: Optional[sbol3.Component], *features: Union[sbol3.Feature, sbol3.Component])\ |
| 58 | + -> sbol3.Component: |
| 59 | + """Check that the system referred to is unambiguous. Raises ValueError if there are multiple or zero systems |
| 60 | +
|
| 61 | + :param system: Optional explicit specification of system |
| 62 | + :param features: features in the same system or components to be referenced from it |
| 63 | + :return: Component for the identified system |
| 64 | + """ |
| 65 | + systems = set(filter(None,(get_parent(f) for f in features if isinstance(f, sbol3.Feature)))) |
| 66 | + if system: |
| 67 | + systems |= {system} |
| 68 | + if len(systems) == 1: |
| 69 | + system = systems.pop() |
| 70 | + if not isinstance(system, sbol3.Component): |
| 71 | + raise ValueError(f'Could not find system, instead found {system}') |
| 72 | + return system |
| 73 | + elif not systems: |
| 74 | + raise ValueError(f'Could not find system: no features in {features}') |
| 75 | + else: |
| 76 | + raise ValueError(f'Multiple systems referred to: {systems}') |
| 77 | + |
| 78 | + |
| 79 | +def add_feature(component: sbol3.Component, to_add: Union[sbol3.Feature, sbol3.Component]) -> sbol3.Feature: |
| 80 | + """Pass-through adder for adding a Feature to a Component for allowing slightly more compact code. |
| 81 | + Note that unlike ensure_singleton_feature, this allows adding multiple instances |
| 82 | +
|
| 83 | + :param component: Component to add the Feature to |
| 84 | + :param to_add: Feature or Component to be added to system. Components will be wrapped in a SubComponent Feature |
| 85 | + :return: feature added (SubComponent if to_add was a Component) |
| 86 | + """ |
| 87 | + if isinstance(to_add, sbol3.Component): |
| 88 | + to_add = sbol3.SubComponent(to_add) |
| 89 | + component.features.append(to_add) |
| 90 | + return to_add |
| 91 | + |
| 92 | + |
| 93 | +def contains(container: Union[sbol3.Feature, sbol3.Component], contained: Union[sbol3.Feature, sbol3.Component], |
| 94 | + system: Optional[sbol3.Component] = None) -> sbol3.Feature: |
| 95 | + """Assert a topological containment constraint between two features (e.g., a promoter contained in a plasmid) |
| 96 | + Implicitly identifies system and creates/adds features as necessary |
| 97 | +
|
| 98 | + :param container: containing feature |
| 99 | + :param contained: feature that is contained |
| 100 | + :param system: optional explicit statement of system |
| 101 | + :return: contained feature |
| 102 | + """ |
| 103 | + # transform implicit arguments into explicit |
| 104 | + system = ensure_singleton_system(system, container, contained) |
| 105 | + container = ensure_singleton_feature(system, container) |
| 106 | + contained = ensure_singleton_feature(system, contained) |
| 107 | + # add a containment relation |
| 108 | + system.constraints.append(sbol3.Constraint(sbol3.SBOL_CONTAINS, subject=container, object=contained)) |
| 109 | + return contained |
| 110 | + |
| 111 | + |
| 112 | +def order(five_prime: Union[sbol3.Feature, sbol3.Component], three_prime: Union[sbol3.Feature, sbol3.Component], |
| 113 | + system: Optional[sbol3.Component] = None) -> sbol3.Feature: |
| 114 | + """Assert a topological ordering constraint between two features (e.g., a CDS followed by a terminator) |
| 115 | + Implicitly identifies system and creates/adds features as necessary |
| 116 | +
|
| 117 | + :param five_prime: containing feature |
| 118 | + :param three_prime: feature that is contained |
| 119 | + :param system: optional explicit statement of system |
| 120 | + :return: three_prime feature |
| 121 | + """ |
| 122 | + # transform implicit arguments into explicit |
| 123 | + system = ensure_singleton_system(system, five_prime, three_prime) |
| 124 | + five_prime = ensure_singleton_feature(system, five_prime) |
| 125 | + three_prime = ensure_singleton_feature(system, three_prime) |
| 126 | + # add a containment relation |
| 127 | + system.constraints.append(sbol3.Constraint(sbol3.SBOL_MEETS, subject=five_prime, object=three_prime)) |
| 128 | + return three_prime |
| 129 | + |
| 130 | + |
| 131 | +def regulate(five_prime: Union[sbol3.Feature, sbol3.Component], target: Union[sbol3.Feature, sbol3.Component], |
| 132 | + system: Optional[sbol3.Component] = None) -> sbol3.Feature: |
| 133 | + """Connect a 5' regulatory region to control the expression of a 3' target region |
| 134 | + Note: this function is an alias for "order" |
| 135 | +
|
| 136 | + :param five_prime: Regulatory region to place upstream of target |
| 137 | + :param target: region to be regulated (e.g., a CDS or ncRNA) |
| 138 | + :param system: optional explicit statement of system |
| 139 | + :return: target feature |
| 140 | + """ |
| 141 | + return order(five_prime, target, system) |
| 142 | + |
| 143 | + |
| 144 | +def constitutive(target: Union[sbol3.Feature, sbol3.Component], system: Optional[sbol3.Component] = None)\ |
| 145 | + -> sbol3.Feature: |
| 146 | + """Add a constitutive promoter regulating the target feature |
| 147 | +
|
| 148 | + :param target: 5' region for promoter to regulate |
| 149 | + :param system: optional explicit statement of system |
| 150 | + :return: newly created constitutive promoter |
| 151 | + """ |
| 152 | + # transform implicit arguments into explicit |
| 153 | + system = ensure_singleton_system(system, target) |
| 154 | + target = ensure_singleton_feature(system, target) |
| 155 | + |
| 156 | + # create a constitutive promoter and use it to regulate the target |
| 157 | + promoter = add_feature(system, sbol3.LocalSubComponent([sbol3.SBO_DNA], roles=[tyto.SO.constitutive_promoter])) |
| 158 | + regulate(promoter, target) |
| 159 | + |
| 160 | + # also add the promoter into any containers that hold the target |
| 161 | + # TODO: add lookups for constraints like we have for interactions |
| 162 | + containers = [c.subject for c in system.constraints |
| 163 | + if c.restriction == sbol3.SBOL_CONTAINS and c.object == target.identity] |
| 164 | + for c in containers: |
| 165 | + contains(c.lookup(), promoter) |
| 166 | + |
| 167 | + return promoter |
| 168 | + |
| 169 | + |
| 170 | +def add_interaction(interaction_type: str, |
| 171 | + participants: Dict[Union[sbol3.Feature, sbol3.Component], str], |
| 172 | + system: sbol3.Component = None, |
| 173 | + name: str = None) -> sbol3.Interaction: |
| 174 | + """Compact function for creation of an interaction |
| 175 | + Implicitly identifies system and creates/adds features as necessary |
| 176 | +
|
| 177 | + :param interaction_type: SBO type of interaction to be to be added |
| 178 | + :param participants: dictionary assigning features/components to roles for participations |
| 179 | + :param system: system to add interaction to |
| 180 | + :param name: name for the interaction |
| 181 | + :return: interaction |
| 182 | + """ |
| 183 | + # transform implicit arguments into explicit |
| 184 | + system = ensure_singleton_system(system, *participants.keys()) |
| 185 | + participations = [sbol3.Participation([r], ensure_singleton_feature(system, p)) for p, r in participants.items()] |
| 186 | + # make and return interaction |
| 187 | + interaction = sbol3.Interaction([interaction_type], participations=participations, name=name) |
| 188 | + system.interactions.append(interaction) |
| 189 | + return interaction |
| 190 | + |
| 191 | + |
| 192 | +def in_role(interaction: sbol3.Interaction, role: str) -> sbol3.Feature: |
| 193 | + """Find the (precisely one) feature with a given role in the interaction |
| 194 | +
|
| 195 | + :param interaction: interaction to search |
| 196 | + :param role: role to search for |
| 197 | + :return Feature playing that role |
| 198 | + """ |
| 199 | + feature_participation = [p for p in interaction.participations if role in p.roles] |
| 200 | + if len(feature_participation) != 1: |
| 201 | + raise ValueError(f'Role can be in 1 participant: found {len(feature_participation)} in {interaction.identity}') |
| 202 | + return feature_participation[0].participant.lookup() |
| 203 | + |
| 204 | + |
| 205 | +def all_in_role(interaction: sbol3.Interaction, role: str) -> List[sbol3.Feature]: |
| 206 | + """Find the features with a given role in the interaction |
| 207 | +
|
| 208 | + :param interaction: interaction to search |
| 209 | + :param role: role to search for |
| 210 | + :return sorted list of Features playing that role |
| 211 | + """ |
| 212 | + return id_sort([p.participant.lookup() for p in interaction.participations if role in p.roles]) |
0 commit comments