# mingyuanxu/tree-invent architecture

> Architecture diagram of the GitHub repository mingyuanxu/tree-invent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mingyuanxu/tree-invent
- Repository: https://github.com/mingyuanxu/tree-invent

## Overview

##### Project type and purpose
- Tree-Invent (FBG_3DGen) is a Python‐based, full‐stack molecular graph generative model.
- It integrates graph construction, graph neural network (GNN) modules, reinforcement learning (RL), and external cheminformatics/docking tools to generate 3D‐constrained molecules under user‐defined topological constraints.

##### File structure
Top‐level directories and their roles:
- FBG_3DGen/graphs       – graph‐model data structures and RDKit preprocessing
- FBG_3DGen/gnn          – MPNN variants for message passing and aggregation
- FBG_3DGen/model        – core generative model classes (Dataset, Model, Model_RL, Z‐matrix utils)
- FBG_3DGen/dock         – docking‐related utility functions (external tool interfaces)
- FBG_3DGen/scores       – scoring‐function wrappers (QED, SAScore, QSAR, similarity, docking)
- pretrained_models/     – serialized sub‐models (node addition, ring generation, node connection) and datasets

Configuration & entry points:
- setup.py               – installable package definition
- TreeInvent.yml         – likely experiment/config settings
- README.md              – architectural overview and workflow images

##### README for additional insights
- Three independent sub‐models: node addition, ring generation, node connection.
- User‐specified topology constraints drive generation.
- Scoring‐guided RL loop integrates optional external functions: docking, similarity, QED, etc.
- Workflow comprises: building topological tree → GNN inference → optional RL policy updates → output 3D molecule.

##### Main components & interactions
a. Inputs & Data Layer
- User topology constraints (YAML/API)
- Preprocessed datasets (SMILES/pickles in pretrained_models/datasets)
- RDKit‐based feature extraction (graphs/rdkitutils.py → features.py)

b. Core Generative Engine
- Graph Representation Module (graphs/Utils3d_np.py, graphroute.py)
- Three Sub‐Models (loaded from pretrained_models):
– Node Addition Model (graph expansion)
– Ring Generation Model (cycle closure)
– Node Connection Model (bond formation)
- Model.py orchestrates sub‐model calls sequentially to build the molecular graph.
- Z-matrix generation/3D coordinate utilities (model/Zmat.py, Utils3d.py)

c. GNN Inference
- MPNN infrastructure (gnn/mpnn.py, aggregation_mpnn.py, edge_mpnn.py, summation_mpnn.py)
- Custom modules for node/edge message passing (gnn/modules.py)
- Aggregation and update functions drive sub‐model predictions

d. Scoring & External Services
- Docking utilities call external binary/APIs via utils_dock.py

e. Reinforcement Learning Loop (optional)
- Model_RL.py wraps Model and Scoring to form environment + policy
- Policy gradients or other RL algorithms adjust sub‐model parameters according to a reward signal

f. Outputs
- Generated molecules (SMILES, 3D coordinates)
- Training logs (pretrained_models/.../Training.log)
- Metrics and scores

##### Architectural patterns & design principles
- Modular separation: graph preprocessing, model inference, scoring, docking.
- Plug-and-play sub‐models: each generative operation is an independent module.
- Clear data flow: constraints → graph features → GNN inference → molecule assembly → scoring → RL update.
- External integration via adapter pattern (docking, RDKit, YAML config).

##### Detailed elements
- A small inset showing filesystem mapping (e.g., “pretrained_models/model/*.cpk”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration & Entry Points
    subgraph "Configuration & Entry Points"
        direction TB
        Setup["setup.py"]:::internal
        ConfigYML["TreeInvent.yml"]:::internal
        Readme["README.md"]:::internal
    end

    %% Data & Preprocessing
    subgraph "Data & Preprocessing"
        direction TB
        RDKitUtils["rdkitutils.py"]:::internal
        Features["features.py"]:::internal
        Utils3dNP["Utils3d_np.py"]:::internal
        GraphRoute["graphroute.py"]:::internal
        MFTree["mftree.py"]:::internal
    end

    %% GNN Core
    subgraph "GNN Core (MPNN)"
        direction TB
        MPnn["mpnn.py"]:::internal
        Agg["aggregation_mpnn.py"]:::internal
        Edge["edge_mpnn.py"]:::internal
        Sum["summation_mpnn.py"]:::internal
        Modules["modules.py"]:::internal
    end

    %% Core Generative Engine
    subgraph "Core Generative Engine"
        direction TB
        Dataset["Dataset.py"]:::internal
        Model["Model.py"]:::internal
        SubNodeAdd["Node Addition Model"]:::internal
        SubRing["Ring Generation Model"]:::internal
        SubNodeConn["Node Connection Model"]:::internal
        Utils3d["Utils3d.py"]:::internal
        Zmat["Zmat.py"]:::internal
    end

    %% Scoring & Docking
    subgraph "Scoring & External Services"
        direction TB
        ScoringFn["Scoringfunction.py"]:::internal
        DockScore["dock_score.py"]:::internal
        ROCS["rocs_similarity.py"]:::internal
        DockAdapter["utils_dock.py"]:::external
    end

    %% Reinforcement Learning Controller
    RL["Model_RL.py"]:::internal

    %% Pretrained Models & Datasets
    PretrainedModels["Pretrained Models (Cylinder)"]:::datastore
    PretrainedDatasets["Pretrained Datasets (Cylinder)"]:::datastore

    %% Outputs
    Outputs["Generated Molecules & Logs"]:::datastore

    %% Connections
    Setup -->|loads| RDKitUtils
    ConfigYML -->|drives| RDKitUtils
    Readme --> Setup

    RDKitUtils -->|extract features| Features
    Features -->|graph data| MPnn

    MPnn -->|messages| SubNodeAdd
    MPnn -->|messages| SubRing
    MPnn -->|messages| SubNodeConn

    SubNodeAdd -->|expansion| Model
    SubRing -->|closure| Model
    SubNodeConn -->|bond formation| Model

    Model -->|graph| Zmat
    Zmat -->|3D coords| Outputs

    Model -->|graph| ScoringFn
    ScoringFn -->|score| RL
    DockAdapter -->|docking| ScoringFn

    RL -.->|policy update| SubNodeAdd
    RL -.->|policy update| SubRing
    RL -.->|policy update| SubNodeConn

    PretrainedModels --> Model
    PretrainedDatasets --> Dataset
    Model --> PretrainedModels
    Dataset --> PretrainedDatasets

    Outputs -->|logs| PretrainedModels

    %% Click Events
    click Setup "https://github.com/mingyuanxu/tree-invent/blob/main/setup.py"
    click ConfigYML "https://github.com/mingyuanxu/tree-invent/blob/main/TreeInvent.yml"
    click Readme "https://github.com/mingyuanxu/tree-invent/blob/main/README.md"
    click RDKitUtils "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/graphs/rdkitutils.py"
    click Features "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/graphs/features.py"
    click Utils3dNP "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/graphs/Utils3d_np.py"
    click GraphRoute "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/graphs/graphroute.py"
    click MFTree "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/graphs/mftree.py"
    click MPnn "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/gnn/mpnn.py"
    click Agg "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/gnn/aggregation_mpnn.py"
    click Edge "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/gnn/edge_mpnn.py"
    click Sum "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/gnn/summation_mpnn.py"
    click Modules "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/gnn/modules.py"
    click Dataset "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/model/Dataset.py"
    click Model "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/model/Model.py"
    click RL "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/model/Model_RL.py"
    click Utils3d "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/model/Utils3d.py"
    click Zmat "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/model/Zmat.py"
    click DockAdapter "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/dock/utils_dock.py"
    click ScoringFn "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/scores/Scoringfunction.py"
    click DockScore "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/scores/dock_score.py"
    click ROCS "https://github.com/mingyuanxu/tree-invent/blob/main/FBG_3DGen/scores/rocs_similarity.py"
    click PretrainedModels "https://github.com/mingyuanxu/tree-invent/tree/main/pretrained_models/MFTs_Gen_Model/model"
    click PretrainedDatasets "https://github.com/mingyuanxu/tree-invent/tree/main/pretrained_models/datasets"

    %% Styles
    classDef internal fill:#D6EAF8,stroke:#1F618D,stroke-width:1px
    classDef external fill:#FAD7A0,stroke:#B9770E,stroke-width:1px
    classDef datastore fill:#D5F5E3,stroke:#196F3D,stroke-width:1px
    class RL dashed
```
