Simple Flask API that shells out to automata_sim (or automata_sim.exe on Windows), parses the CLI output, and returns structured JSON optimized for web visualization. Use it to bridge the C++ simulator with a web frontend.
The backend is modularized for maintainability:
app.py- Flask routes and endpoints onlyconfig.py- Configuration, binary path management, and error handlingutils.py- Utility functions for command building and file operationsparser.py- Parsing logic to convert stdout into structured JSON
-
Python 3.11+ (same version used by your frontend tooling).
-
pip / venv.
-
Platform-specific binary built from the project root:
- Windows:
automata_sim.exe - macOS/Linux:
automata_sim
Copy the appropriate binary into this
BACKEND/folder or setAUTOMATA_SIM_PATHenvironment variable. - Windows:
cd BACKEND
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txtcd BACKEND
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtcd BACKEND
.venv\Scripts\activate
$env:FLASK_APP="app.py"
flask run --port 5000cd BACKEND
source .venv/bin/activate
export FLASK_APP=app.py
flask run --port 5000Windows (PowerShell):
$env:AUTOMATA_SIM_PATH="C:\path\to\build\bin\automata_sim.exe"macOS/Linux (bash/zsh):
export AUTOMATA_SIM_PATH=/path/to/build/bin/automata_simNote for macOS/Linux: Ensure the binary has execute permissions:
chmod +x BACKEND/automata_simQuery parameters (all optional except pattern for regex modes):
pattern: The regex pattern to match (e.g.,A(CG|TT)*)mode: Automaton mode -auto,nfa,dfa,efa, orpda(default:auto)mismatch_budget: Integer value for mismatch budgetallow_dot_bracket: Boolean (true/false/1/0/yes/no)input_path: Path to input file (e.g.,datasets/dna/sample.txt)sequences: Multiple sequences can be passed as repeated query parameters (used wheninput_pathis omitted)
Response (structured JSON optimized for visualization):
Status 200 indicates success; 500 means the simulator failed; 400 covers validation errors.
Note: The response is parsed from stdout and structured for easy visualization. Match ranges are sorted by start position, and coverage metrics are calculated automatically.
You can skip input_path and send raw sequences directly as query parameters. Add the same key multiple times to submit several sequences.
GET http://127.0.0.1:5000/simulate?mode=dfa&pattern=A(CG|TT)*&sequences=ACGTACGTACGT
GET http://127.0.0.1:5000/simulate?mode=efa&pattern=ACGT&mismatch_budget=2&sequences=ACGTACGTACGT
GET http://127.0.0.1:5000/simulate?mode=pda&allow_dot_bracket=true&sequences=(()())&sequences=((..))
When the simulator runs in PDA mode, the response includes a pda_sequences array in addition to the regular sequences data. Each entry surfaces PDA/RNA-specific validation details:
"pda_sequences": [
{
"sequence_number": 1,
"sequence": "AGCU",
"dot_bracket": "(..)",
"result": "Valid",
"valid_rna_bases": true,
"checks": [
"1th nucleotide A <-> 4th nucleotide U -> valid? [OK]",
"Parentheses balanced? [OK]"
],
"messages": [], // length mismatches or other PDA warnings
"has_matches": true,
"match_count": 1,
"coverage": 1.0
}
]Sequences that fail PDA validation (length mismatch, invalid base pairs, etc.) populate messages and report result: "Invalid", making it easier to display per-sequence status without parsing raw stdout.
Each inline request returns the parsed results plus the automaton structure (states + transitions) for nfa, dfa, efa, and pda modes. Sample response:
{
"pattern": "ACGT",
"automaton_mode": "EFA",
"sequences": [
{
"sequence_number": 1,
"length": 12,
"matches": ["[0,4)", "[4,8)", "[8,12)"],
"sequence_text": "ACGTACGTACGT",
"states_visited": 36
}
],
"runs": 1,
"matches": 3,
"automaton": {
"kind": "EFA",
"pattern": "ACGT",
"start": 0,
"states": [
{
"id": 0,
"accept": false,
"transitions": [
{"symbol": "A", "to": 3, "type": "match"},
{"symbol": "C", "to": 4, "type": "mismatch"}
// ...
]
}
// ...
]
}
}Quick check to confirm the binary is reachable.
curl "http://127.0.0.1:5000/simulate?pattern=A(CG|TT)*&mode=nfa&input_path=datasets/dna/sample.txt"Or with Invoke-RestMethod:
Invoke-RestMethod -Method Get -Uri "http://127.0.0.1:5000/simulate?pattern=A(CG|TT)*&mode=dfa&input_path=datasets/dna/sample.txt"curl "http://127.0.0.1:5000/simulate?pattern=A(CG|TT)*&mode=nfa&input_path=datasets/dna/sample.txt"Note: For multiple sequences, use repeated query parameters:
curl "http://127.0.0.1:5000/simulate?pattern=A(CG|TT)*&sequences=ACGTACGT&sequences=TTTTTT"Your frontend can hit /simulate with user-selected parameters to get structured JSON data optimized for visualization, including parsed match positions, coverage metrics, and sequence information.
{ "pattern": "A(CG|TT)*", "datasets": "1 sequence(s)", "dataset_count": 1, "automaton_mode": "DFA", "sequences": [ { "sequence_number": 1, "length": 12, "matches": ["[0,1)", "[0,3)", "[4,5)", "[4,7)", "[8,9)", "[8,11)"], "match_ranges": [ { "range": "[0,1)", "start": 0, "end": 1, "length": 1 }, // ... more match ranges ], "sequence_text": "ACGTACGTACGT", "states_visited": 23, "match_count": 6, "has_matches": true, "coverage": 0.5 } ], "runs": 1, "matches": 6, "all_accepted": false, "total_sequences": 1, "sequences_with_matches": 1, "total_states_visited": 23, "average_coverage": 0.5 }