# maxhwardg/mrnafold architecture

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

- Interactive diagram: https://gitdiagram.com/maxhwardg/mrnafold
- Repository: https://github.com/maxhwardg/mrnafold

## Overview

##### Main Components
- Build/Development Infrastructure:
– CMake configuration for building the C++ source (located in cpp_src with separate subdirectories for the executable and the mwmrna_lib library).
– Build directory and out-of-source build process.
- Core C++ Engine:
– The “mwmrna_lib” library encapsulates most of the logic with modules for codon graph construction (codon_graph.hpp, codon_graph_fold.hpp, codon_graph_trace.hpp), RNA structure analysis (rna.hpp, structure.hpp), energy calculations (energy_calculator.hpp, energy_model.hpp, energy_semiring.hpp), logging, parallel processing, and protein handling.
– The “fold_codon_graph” executable in the cpp_src/exe directory uses this library to drive mRNA folding, reading configuration parameters and producing folded sequences.
- Input/Configuration Layer:
– Config files in the “configs” directory that include parameters such as the amino acid sequence (aa_seq), suboptimal-folding parameters, multithreading options, UTRs, and codon restrictions.
– Codon tables in the “codon_tables” folder provide external data inputs for codon usage (e.g., human and mouse codon tables).
- Python Interface:
– A Python runner, fold_codons.py in the “pysrc” directory, which provides a convenient function interface to call the compiled binaries. It also integrates with external packages like ViennaRNA for result evaluation.
- External Dependencies:
– Libraries/tools like the C++20 standard library (including parallel features), Intel TBB, and external Python dependency (ViennaRNA) for evaluation.

##### Relationships and Data Flow
- The client/user initiates the process by either running the C++ executable directly (passing a config file) or via the Python script.
- The configuration file is read by the executable to set up the runtime parameters, including which codon table to use (default is baked-in for human, but others can be specified).
- The “fold_codon_graph” executable (the driver) invokes underlying functions from the mwmrna_lib library. Within this library:
– The codon graph modules build the graph representation of the mRNA structure.
– Energy calculators and scoring mechanisms compute the MFE, CAI, and optionally incorporate randomness for suboptimal sampling.
– Multithreading is used if enabled, allowing different sampling rounds to be executed in parallel.
- The result—multiple folded mRNA sequences along with scores and alternative structures—gets output to the console or captured by the Python interface for further evaluation or integration.

##### Architectural Patterns and Design Principles
- Layered Design: The project separates concerns between configuration/input, core algorithm (library), and user interfaces (CLI executable and Python script).
- Extensibility and Integration: The system is designed so that the Python wrapper integrates with the successful compiled C++ backend and also depends on external packages for additional functionality (e.g., ViennaRNA).
- Configurable Workflow: The numerous configuration parameters illustrate that users can dynamically adjust behavior, making it important to show an isolated “Configuration Parser/Manager” component.

##### Relevant Technologies, Frameworks, and Libraries
- C++20 with modern constructs and the standard library for multithreading.
- CMake as the build system.
- Intel TBB (if used) for parallelism.
- Python and the ViennaRNA library as part of the interface layer.
- Additionally, external codon frequency tables and configuration files provide input—which could be highlighted as data resources.

##### Elements
- Each component should be clearly labeled (e.g., “Configuration Manager”, “Codon Graph Module”, “Energy Calculator”, “Folding Driver Executable”, “Python Interface”).
– Rectangles for executables and libraries.
- Annotations explaining inter-module interactions: for example, indicate that the “fold_codon_graph” driver calls functions from “mwmrna_lib” which in turn uses “codon_graph”, “energy_calculator”, “parallel processing” and “logging”.

##### Putting It All Together
– Branch into two user interface paths: direct C++ executable and the Python runner.
– The next layer is “Configuration Processing” which consumes config files from the “configs” directory and codon tables from the “codon_tables” directory.
– Then detail the “Core C++ Engine” with subcomponents:
- “mwmrna_lib” with codon graph, energy evaluation, and structure processing.
- “fold_codon_graph” as the main driver interfacing with the library.
– Finally, show the output layer where optimized mRNA sequences, complete with MFE, CAI, and structure information, are returned to the user or passed back to the Python interface.
– Optionally, add an “External Dependencies” node to show integration with third-party libraries/tools (C++ standard library, Intel TBB, ViennaRNA, etc.).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    U["User/Client"]:::user

    %% Interface Layer
    EXE["C++ Executable (fold_codon_graph)"]:::interface
    PY["Python Interface (fold_codons.py)"]:::interface

    %% Configuration Layer
    subgraph "Configuration Layer"
        CF["Configuration Files"]:::config
        CT["Codon Tables"]:::config
        CM["Configuration Manager"]:::config
        CF --> CM
        CT --> CM
    end

    %% Core C++ Engine Layer    
    subgraph "Core C++ Engine"
        LIB["mwmrna_lib Library"]:::core
        CG["Codon Graph Module"]:::core
        EC["Energy Calculator"]:::core
        SP["Structure Processing"]:::core
        PP["Parallel Processing"]:::core
        LG["Logging Module"]:::core
        PH["Protein Handling"]:::core
        LIB --> CG
        LIB --> EC
        LIB --> SP
        LIB --> PP
        LIB --> LG
        LIB --> PH
    end

    %% Output Layer
    OUT["Output: Folded mRNA Sequences & Scores"]:::core

    %% Build/Development Infrastructure
    subgraph "Build/Dev Infrastructure"
        CMM["CMake Config (Main)"]:::build
        CMX["CMake Config (Executable)"]:::build
    end

    %% External Dependencies
    EXT["External Dependencies (C++20, Intel TBB, ViennaRNA)"]:::dep

    %% Data Flow
    U -->|"initiates"| EXE
    U -->|"initiates"| PY
    CM -->|"provides-config"| EXE
    PY -->|"triggers"| EXE
    EXE -->|"invokes"| LIB
    LIB -->|"produces"| OUT
    EXT -->|"integrates"| LIB
    CMM -->|"builds"| LIB
    CMX -->|"builds"| EXE

    %% Styles
    classDef user fill:#ffcc00,stroke:#000,stroke-width:2px;
    classDef interface fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef config fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef core fill:#ff9999,stroke:#000,stroke-width:2px;
    classDef dep fill:#d3d3d3,stroke:#000,stroke-width:2px;
    classDef build fill:#dda0dd,stroke:#000,stroke-width:2px;

    %% Click Events
    click CMM "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/CMakeLists.txt"
    click CMX "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/exe/CMakeLists.txt"
    click LIB "https://github.com/maxhwardg/mrnafold/tree/main/cpp_src/mwmrna_lib/"
    click CG "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/mwmrna_lib/src/codon_graph.hpp"
    click EC "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/mwmrna_lib/src/energy_calculator.hpp"
    click SP "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/mwmrna_lib/src/rna.hpp"
    click PP "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/mwmrna_lib/src/parallel.hpp"
    click LG "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/mwmrna_lib/src/logging.hpp"
    click PH "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/mwmrna_lib/src/protein.cpp"
    click EXE "https://github.com/maxhwardg/mrnafold/blob/main/cpp_src/exe/src/fold_codon_graph.cpp"
    click CF "https://github.com/maxhwardg/mrnafold/tree/main/configs/"
    click CT "https://github.com/maxhwardg/mrnafold/tree/main/codon_tables/"
    click PY "https://github.com/maxhwardg/mrnafold/blob/main/pysrc/fold_codons.py"
```
