Below is a detailed description of the SNAP software, including its requirements, functionalities, structure, and the changes made based on our conversation. This summary is designed to provide a comprehensive overview for you to start a new chat and continue development. I'll cover the software's purpose, technical details, key components, and the specific updates made to address issues raised during our discussion. --- ### Overview of SNAP Software **SNAP** (Simulation of Nuclear and Affected Pedigrees) is a web-based software tool hosted at `https://statgen.uni-koeln.de/` for simulating genetic data based on user-specified study designs, sampling strategies, and genotype-phenotype models. It is developed to assist researchers in statistical genetics by generating synthetic datasets for case-control studies, family-based studies, and other genetic analyses. The software takes user inputs through a web interface, processes them via a PHP backend (`run.php`), and executes a SNAP binary (`snap3.1` or `snap2.6.1`) to produce output files, including settings files, data files, and logs. ### Requirements #### Functional Requirements 1. **User Input Handling**:    - Accept a JSON object (`SNAP_JSON`) from the browser’s `localStorage`, containing configuration data for simulation parameters.    - Validate input data for completeness and correctness (e.g., flags, filenames, numeric values). 2. **Simulation Configuration**:    - Support multiple study designs: `'individuals'` (case-control or joint sampling) and `'nuclear-families'` (fixed or random family structures).    - Allow specification of phenotype types: `'qualitative'` (e.g., disease status) or `'quantitative'` (e.g., continuous traits).    - Configure sampling designs: `'separate'` (case-control) or `'joint'` (random sampling) for individuals.    - Define family parameters for nuclear families: number of families, children, and family type (`'fixed'` or `'random'`).    - Specify sex and age distributions, genotype-phenotype models, haplotype blocks, and output formatting. 3. **File Generation**:    - Generate a settings file (`.set`) containing simulation parameters in a structured format.    - Produce a data output file (`.dat`) with simulated genetic data.    - Create a modified settings log file (`.log`) reflecting the simulation run.    - Optionally generate a tarball (`.tgz`) containing all output files.    - Provide error/warning output (`.txt`) if the SNAP binary encounters issues. 4. **Output Delivery**:    - Display download links for generated files in the browser.    - Ensure files are accessible for ~24 hours via a temporary directory.    - Allow users to download individual files or a compressed archive. 5. **Error Handling**:    - Validate input JSON and parameters, displaying user-friendly error messages for invalid inputs (e.g., missing filenames).    - Log errors and debugging information to a server-side log file for troubleshooting.    - Handle SNAP binary execution errors and display relevant error messages. #### Non-Functional Requirements 1. **Performance**:    - Process JSON inputs and generate settings files quickly (within seconds).    - Handle SNAP binary execution efficiently, even for large simulations. 2. **Security**:    - Sanitize filenames to prevent injection attacks (e.g., remove non-alphanumeric characters).    - Set appropriate file permissions (e.g., `0644` for settings, `0444` for outputs) to prevent unauthorized modifications.    - Use temporary directories with unique names to avoid conflicts. 3. **Reliability**:    - Ensure robust error handling for missing or malformed JSON data.    - Clean up old files (>24 hours) to manage disk space. 4. **Maintainability**:    - Use modular PHP code with clear function responsibilities.    - Include detailed logging for debugging and future enhancements. #### Technical Requirements 1. **Server Environment**:    - PHP 7.x or later with error reporting enabled (`E_ALL`).    - Web server (e.g., Apache) with write access to `/var/www/html/statgen.uni-koeln.de/software/snap/`.    - SNAP binaries (`snap3.1`, `snap2.6.1`) installed in `/var/www/html/statgen.uni-koeln.de/software/snap/run/`.    - File system permissions for creating directories and files (`0775` for directories, `0644`/`0444` for files). 2. **File System**:    - Base path: `/var/www/html/statgen.uni-koeln.de/software/snap/`.    - Temporary directory: `/var/www/html/statgen.uni-koeln.de/software/snap/temp/`.    - Run directory: `/var/www/html/statgen.uni-koeln.de/software/snap/run/`.    - Error log: `/var/www/html/statgen.uni-koeln.de/software/snap/run_error.log`. 3. **Client-Side**:    - Web interface (HTML/JavaScript) storing `SNAP_JSON` in `localStorage`.    - Bootstrap Icons (`bi`) for UI elements (e.g., `bi-chat-left-fill`, `bi-exclamation-circle`).    - AJAX POST request to send `SNAP_JSON` to `run.php`. ### Functionalities 1. **Input Processing**:    - Reads `SNAP_JSON` from `php://input` via an AJAX POST request.    - Decodes JSON into a PHP array, validating for errors (e.g., `json_last_error()`).    - Extracts sections: `SnapSettings`, `DesignData`, `SexAgeData`, `ModelData`, `HaplotypeBloks`. 2. **Settings File Generation**:    - Creates a structured settings file (`.set`) with sections: `[General]`, `[Design]`, `[SexAge]`, `[Model]`, `[Separators]`, `[Blocks]`.    - Supports dynamic configuration based on input data (e.g., phenotype type affects `[Model]`).    - Sanitizes filenames to ensure security. 3. **Simulation Execution**:    - Executes the SNAP binary (`snap3.1` or `snap2.6.1`) with the settings file as input.    - Captures standard output and errors (`stderr`) to a file.    - Checks for success (`"Data successfully generated."`) or errors. 4. **Output Handling**:    - Moves output files (`.dat`, `.log`) to the temporary directory.    - Creates a tarball (`.tgz`) of all outputs.    - Generates HTML with download links for files.    - Displays errors if the SNAP binary fails, including `stderr` content. 5. **Cleanup**:    - Registers a shutdown function to delete files older than 24 hours.    - Removes empty temporary directories to manage disk space. ### Structure of `run.php` The `run.php` script is the core backend component, structured as follows: #### Configuration - **Constants**:   - `SNAP_BASE_PATH`: `/var/www/html/statgen.uni-koeln.de/software/snap`.   - `SNAP_TEMP_DIR`: For temporary files.   - `SNAP_RUN_DIR`: For SNAP binaries.   - `HOST_URL`: `https://statgen.uni-koeln.de/`.   - `DEFAULT_SNAP_VERSION`: `'3.1'`.   - `MAX_AGE_SECONDS`: 24 hours for file cleanup. #### Helper Functions 1. **`logError($message)`**:    - Logs messages to `run_error.log` with timestamps. 2. **`sanitizeFilename($filename)`**:    - Removes non-alphanumeric characters for security. 3. **`writeSection($handle, $content)`**:    - Writes a section to the settings file with error handling. 4. **`getArrayValue($array, $key, $default)`**:    - Safely retrieves array values with a default. 5. **`getBlockKey($key)`**:    - Maps JSON keys to settings file keys for `[Blocks]`. 6. **`search_temp($directory)`**:    - Finds an available temporary directory (e.g., `temp123.snap`). #### Core Functions 1. **`generateSettingsFile($data, $settingsFilePath)`**:    - Generates the settings file with sections:      - `[General]`: Output filenames, type, format.      - `[Design]`: Study design, sampling, phenotype, counts (individuals, cases, families, etc.).      - `[SexAge]`: Sex ratio, age distribution parameters.      - `[Model]`: Genotype-phenotype model, including penetrances or phenotypic means.      - `[Separators]`: Delimiters for output formatting.      - `[Blocks]`: Haplotype block structures.    - Validates inputs and logs issues. 2. **`generateModelTypeSettings($modelData)`**:    - Handles `[Model]` for quantitative phenotypes (homogeneous, sex-specific, age-specific). 3. **`generatePenetrancesSetting($modelData)`**:    - Generates penetrance matrices for qualitative phenotypes. 4. **`generateBlockSetting($block)`**:    - Formats haplotype block settings. 5. **`buildHTML($hint, $tempDir, $browserPath, $runDir, $host, $snapVersion)`**:    - Processes `SNAP_JSON`, validates flags, generates settings, runs SNAP, and outputs HTML with file links. 6. **Main Execution**:    - Initializes the script, creates a temp directory, registers cleanup, and calls `buildHTML`. #### Input (`SNAP_JSON`) - **Structure**:   ```json   {     "SnapSettings": {       "user_settings_filename": "string",       "data_filename": "string",       "settings_filename": "string",       "output_type": "string",       "output_format": "string",       "just_settings_file": boolean,       "behind_status": "string",       "between_haplotypes": "string",       "between_blocks": "string",       "between_snps": "string"     },     "DesignData": {       "phenotype": "qualitative|quantitative",       "study_design": "individuals|nuclear-families",       "sampling_design": "separate|joint",       "number_individuals": number,       "number_cases": number,       "number_case_controls": number,       "number_families": number,       "number_children": number,       "type_family": "fixed|random",       "design_flag": "true|false",       "penetrance_phenotypic_flag": "true|false"     },     "SexAgeData": {       "sexRatio": number,       "ageMinimum": number,       "ageMaximum": number,       "ageDistribution": "string",       "ageMean": number,       "ageStd": number,       "minimumChildAgeDist": number,       "motherAgeFirstChild": number,       "motherAgeStd": number,       "parentalAgeDiff": number,       "parentalAgeDiffStd": number     },     "ModelData": {       "number_loci": number,       "number_of_states": number,       "biallelic_check": "yes|no",       "remove_causal_snps": "yes|no",       "genotyping_error": number,       "genotyping_error_visible": "yes|no",       "random_seed": "string",       "penetrances": array|string,       "ModelType": object,       "model_flag": "true|false"     },     "HaplotypeBloks": [       {         "blockNumber": number,         "SNPs": number,  descending order: **Context**, **Issues Addressed**, **Changes Made**. #### Context - You reported warnings about undefined variables in `run.php`:   - `$designFlag`, `$penetranceFlag`, `$modelFlag`, `$blockFlag` in `buildHTML` (lines 331–340).   - `$userSettingsFile_stderr` in `buildHTML` (line 376). - You provided seven example settings files to refine the `[Design]` section logic in `generateSettingsFile`. - The goal was to fix errors, ensure robustness, and align the `[Design]` section with the example configurations. #### Issues Addressed 1. **Undefined Variable Warnings**:    - `$designFlag`, `$penetranceFlag`, `$modelFlag`, `$blockFlag` were undefined due to potential missing or non-array sections (`DesignData`, `ModelData`, `HaplotypeBloks`) in `$data`.    - `$userSettingsFile_stderr` was a typo; the correct variable was `$stderrFile`. 2. **[Design] Section Logic**:    - The original logic didn’t validate parameter combinations (e.g., `NumberOfCases` only for `'separate'` sampling).    - Default values (`'case-control'` for `StudyDesign`, `'random'` for `SamplingDesign`) didn’t match examples.    - No validation for numeric fields or allowed values (e.g., `phenotype` should be `'qualitative'` or `'quantitative'`). 3. **Input Robustness**:    - Missing checks for `$data['DesignData']` and `$data['SnapSettings']` existence.    - Potential for invalid JSON or missing sections to cause errors. #### Changes Made 1. **Fix for Undefined Flags** (Artifact ID: `a709bb42-8a9c-479b-84b0-0a3f5954b05c`):    - Added pre-checks in `buildHTML`:      ```php      $designData = isset($data['DesignData']) && is_array($data['DesignData']) ? $data['DesignData'] : [];      $modelData = isset($data['ModelData']) && is_array($data['ModelData']) ? $data['ModelData'] : [];      $haplotypeBloks = isset($data['HaplotypeBloks']) && is_array($data['HaplotypeBloks']) ? $data['HaplotypeBloks'] : [];      ```    - Updated flag assignments:      ```php      $designFlag = getArrayValue($designData, 'design_flag', 'true');      $penetranceFlag = getArrayValue($designData, 'penetrance_phenotypic_flag', 'true');      $modelFlag = getArrayValue($modelData, 'model_flag', 'true');      $blockFlag = getArrayValue($haplotypeBloks, 'block_flag', 'true');      ```    - Added logging:      ```php      logError("Flags: design=$designFlag, penetrance=$penetranceFlag, model=$modelFlag, block=$blockFlag");      ``` 2. **Fix for `$userSettingsFile_stderr`**:    - Corrected to:      ```php      $stderrFile = "{$userSettingsFile}_stderr.txt";      ``` 3. **Updated `[Design]` Logic in `generateSettingsFile`** (Latest Artifact):    - Validated `$data['DesignData']` and `$data['SnapSettings']`:      ```php      $designData = isset($data['DesignData']) && is_array($data['DesignData']) ? $data['DesignData'] : [];      $snapSettings = isset($data['SnapSettings']) && is_array($data['SnapSettings']) ? $data['SnapSettings'] : [];      ```    - Adjusted defaults:      - `StudyDesign`: `'individuals'`.      - `SamplingDesign`: `'joint'` for `'individuals'`, `'random'` for `'nuclear-families'`.    - Added validation:      ```php      $validPhenotypes = ['qualitative', 'quantitative'];      $validStudyDesigns = ['individuals', 'nuclear-families'];      $validSamplingDesigns = ['separate', 'joint'];      $validTypeFamilies = ['fixed', 'random'];      ```    - Implemented conditional logic:      - For `'individuals'`:        - Include `SamplingDesign`.        - If `'separate'`: Add `NumberOfCases`, `NumberOfControls` (if numeric, > 0).        - If `'joint'`: Add `NumberOfIndividuals` (if numeric, > 0).      - For `'nuclear-families'`:        - Omit `SamplingDesign`.        - Add `NumberOfFamilies`, `NumberOfChildren`, `TypeOfFamily` (if valid).    - Added logging for invalid/missing parameters. 4. **General Robustness**:    - Ensured `$snapSettings` validation in `buildHTML` and `generateSettingsFile`.    - Added fallback filenames:      ```php      $userSettingsFile = sanitizeFilename(getArrayValue($snapSettings, 'user_settings_filename', 'settings'));      $dataFile = sanitizeFilename(getArrayValue($snapSettings, 'data_filename', 'data'));      $settingsLogFile = sanitizeFilename(getArrayValue($snapSettings, 'settings_filename', 'settings_log'));      ``` ### Recommendations for Further Development 1. **Validate `SNAP_JSON` Structure**:    - Add a schema validation function to ensure `SNAP_JSON` contains required sections and valid values before processing.    - Example:      ```php      function validateSnapJson($data) {          $required = ['SnapSettings', 'DesignData', 'SexAgeData', 'ModelData', 'HaplotypeBloks'];          foreach ($required as $section) {              if (!isset($data[$section]) || !is_array($data[$section])) {                  logError("Missing or invalid section: $section");                  return false;              }          }          // Add specific key checks (e.g., 'phenotype' in DesignData)          return true;      }      ``` 2. **Enhance Frontend Validation**:    - Update the JavaScript frontend to enforce valid `SNAP_JSON` structures (e.g., ensure `design_flag` is `'true'` or `'false'`).    - Prevent submission if required fields are missing. 3. **Improve Error Feedback**:    - Return structured JSON errors to the frontend instead of HTML echoes:      ```php      if (!empty($errors)) {          http_response_code(400);          echo json_encode(['errors' => $errors]);          exit;      }      ``` 4. **Test with Example JSONs**:    - Create test cases for each of the seven example settings:      - Example 1: `individuals`, `separate`, `NumberOfCases=900`, `NumberOfControls=1300`.      - Example 2: `individuals`, `joint`, `NumberOfIndividuals=200`.      - Example 3: Same as 2 but `quantitative`.      - Example 4–7: `nuclear-families`, varying `NumberOfChildren`, `TypeOfFamily`.    - Use a testing script to simulate `SNAP_JSON` submissions. 5. **Optimize File Cleanup**:    - Add a cron job to handle file cleanup instead of relying on `register_shutdown_function`.    - Example cron (daily):      ```bash      find /var/www/html/statgen.uni-koeln.de/software/snap/temp/ -type f -mtime +1 -delete      find /var/www/html/statgen.uni-koeln.de/software/snap/temp/ -type d -empty -delete      ``` 6. **Add Documentation**:    - Document `run.php` with PHPDoc comments for functions.    - Create a user guide for SNAP, detailing valid `SNAP_JSON` fields and example settings files. ### Starting a New Chat When opening a new chat, include the following: - **Reference the Latest Artifact**:   - Artifact ID: `a709bb42-8a9c-479b-84b0-0a3f5954b05c`.   - Title: `run.php`.   - This contains the latest `run.php` with updated `[Design]` logic. - **Provide Context**:   - Mention that SNAP is a genetic simulation tool hosted at `https://statgen.uni-koeln.de/`.   - Note the fixes for undefined variables and `[Design]` logic alignment with seven example settings. - **Specify Next Steps**:   - Example prompts:     - “I need to add validation for `SNAP_JSON` in `run.php`. Can you implement a schema check function?”     - “Help me test `run.php` with the seven example settings provided. Simulate `SNAP_JSON` submissions.”     - “I want to improve the frontend to validate `SNAP_JSON` before sending to `run.php`. Suggest JavaScript code.” - **Include Debugging Info (if needed)**:   - Share `run_error.log` entries (especially `Decoded data` and `Flags`).   - Provide `localStorage.getItem('SNAP_JSON')` output.   - Describe any new errors or desired features. This summary should give you a solid foundation to continue SNAP development in a new chat. Let me know if you need a specific section expanded or additional details before proceeding!