molstar / mol-view-spec

https://molstar.org/mol-view-spec/
MIT License
8 stars 1 forks source link

How do hold "parent" references? #1

Open JonStargaryen opened 1 year ago

JonStargaryen commented 1 year ago

Let's say you want to create multiple representations from a structure:

builder = Root()
structure = builder.download(url=f"https://www.ebi.ac.uk/pdbe/entry-files/download/{id.lower()}_updated.cif")\
    .parse(format="mmcif")\
    .structure()
structure.component(selector="protein")\
    .representation(type="cartoon", color="white")
structure.component(selector="ligand")\
    .representation(type="ball-and-stick")

How would this be done without holding structure in a variable? Are we fine with this? Should component() be components() and take all requested components are argument? Generics + parent()/leave()/exit() method?

dsehnal commented 1 year ago

Brainstrorming:


from view_builder import nodes as ns

builder = Root()
structure = builder.download(url=f"https://www.ebi.ac.uk/pdbe/entry-files/download/{id.lower()}_updated.cif")\
    .parse(format="mmcif")\
    .structure()

def download(url: str, is_binary: bool = False, children: list[ParseNode | ...]): 
    return DownloadNode(url=url, is_binary=is_binary, children=children)

def parse(format: Literal["mmcif"], *children: Any): 
    return DownloadNode(url=url, is_binary=is_binary, children=children)

tree = download(
    url="test",
    children=[parse(
        format="mmcif",
        children=[structure(
            kind="assembly",

        )]
    )]
)

(structure
    .component(selector="protein")
    .representation(type="cartoon", color="white"))
(structure
    .component(selector="ligand")
    .representation(type="ball-and-stick"))

    # .children(lambda n: [
    #     n.component(selector="protein").representation(type="cartoon", color="white"),
    #     n.component(selector="ligand").representation(type="cartoon", color="white"),
    # ])
    # .children(
    #     ns.component(selector="protein").representation(type="cartoon", color="white"),
    #     ns.component(selector="ligand").representation(type="cartoon", color="white"),
    # )

class None:
    allowed_children_kinds = ["download", "..."]

    def _validate_child(self, child: BuilderBase):
        if child.kind not in self.allowed_children_kinds:
            raise ValueError(...)

def many(cb: Callable[[T], list[Builder]]) -> Root:
    pass

def many(*children: list[Builder]) -> Root:
    pass

structure.component(selector="protein")\
    .representation(type="cartoon", color="white")
structure.component(selector="ligand")\
    .representation(type="ball-and-stick")
JonStargaryen commented 1 year ago

The outcome of the discussion was: No immediate changes, but revisit in the future, right?

dsehnal commented 1 year ago

Yes