# Reading Current Prism Data State ## The Problem Prism scripts **cannot read data values** into variables or use them for conditional logic. This is a fundamental limitation of the scripting language. **What you CANNOT do:** - Read a cell value into a variable - Use if/else conditionals based on data - Make script decisions based on analysis results - Loop until a condition is met - Compare values or perform calculations in the script **The only "read" command** is `SetValueToInfo`, which copies a value from one location to another but doesn't let you use it in logic. ## The Solution: PZFX XML Files Since Prism's scripting language can't read data, use the **PZFX file format** instead. ### What is PZFX? - **PZFX** = Prism's XML file format - Plain text XML structure (human and machine readable) - **Data tables**: Fully readable and editable in XML - **Info tables**: Fully readable and editable in XML - **Analysis results**: Encrypted but readable as XML - **Graph/analysis settings**: Encrypted (not editable externally) ### How Prism Automation Works ``` Prism's Built-in Automation: Data → Auto-updates → Graphs Data → Auto-updates → Analyses Data → Auto-updates → Paste-linked tables ``` When you edit data in a PZFX file externally, Prism sees it as a data change and **auto-updates everything**. ## Workflow Patterns ### Pattern 1: Read-Only Analysis (Python/R) **Use case**: Extract data/results for external analysis ```python # Python example import xml.etree.ElementTree as ET # Read PZFX file tree = ET.parse('experiment.pzfx') root = tree.getroot() # Extract data from table for table in root.findall('.//Table'): table_name = table.get('ID') # Parse data cells for cell in table.findall('.//d'): value = cell.text # Analyze, plot, whatever you need ``` ### Pattern 2: Modify Data, Let Prism Analyze **Use case**: Pre-process data externally, let Prism do analysis/graphing ```python # Python example import xml.etree.ElementTree as ET # 1. Read PZFX template tree = ET.parse('template.pzfx') # 2. Modify data in XML for table in tree.findall('.//Table[@ID="Data 1"]'): # Update cell values cells = table.findall('.//d') cells[0].text = str(new_value) # 3. Save modified file tree.write('processed.pzfx') # 4. Open in Prism - graphs/analyses auto-update! ``` ### Pattern 3: Conditional Logic + Prism Script **Use case**: Make decisions based on data, then run appropriate Prism script ```python # Python: Read data and decide what to do tree = ET.parse('results.pzfx') p_value = float(tree.find('.//Cell[@Row="5"][@Col="3"]').text) if p_value < 0.05: script_to_run = "significant.pzc" else: script_to_run = "not_significant.pzc" # Launch appropriate Prism script import subprocess subprocess.run(['prism', script_to_run]) ``` ### Pattern 4: Full Integration (Python + Prism) **Complete workflow**: ```python #!/usr/bin/env python3 import xml.etree.ElementTree as ET import subprocess import os # 1. Prepare data externally data = process_experimental_data() # 2. Create/modify PZFX file tree = ET.parse('template.pzfx') # ... populate data in XML ... tree.write('experiment.pzfx') # 3. Run Prism script to generate graphs subprocess.run(['prism', '@export_graphs.pzc']) # 4. Read results back from PZFX tree = ET.parse('experiment.pzfx') results = extract_results(tree) # 5. Make decisions, generate reports if results['significant']: generate_report(results) ``` ## Platform-Specific Integration ### macOS Approach **Problem**: AppleScript can only launch Prism scripts, not read data. **Solution**: Use PZFX + external tool ```applescript -- Save current work (user does this manually or via script) tell application "Prism" -- Prism must already have file open, saved as PZFX end tell -- Process with Python/R/etc do shell script "python3 ~/scripts/analyze_prism.py" -- Reopen in Prism (optional - user can do manually) tell application "Prism" activate open POSIX file "/path/to/processed.pzfx" end tell ``` ### Windows Approach **Same principle**, but can use VBA, PowerShell, or Python: ```vbnet ' Excel VBA example Sub ProcessPrismData() ' 1. Tell Prism to save as PZFX ' (user must do manually or via Save command in script) ' 2. Parse XML (using MSXML2.DOMDocument) Dim xmlDoc As Object Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.Load "C:\Data\experiment.pzfx" ' 3. Read values Dim pValue As Double pValue = CDbl(xmlDoc.SelectSingleNode("//Cell[@Row='5']").Text) ' 4. Make decision If pValue < 0.05 Then Shell "C:\Prism\prism.exe @C:\Scripts\significant.pzc" End If End Sub ``` ## XML Structure Reference ### Data Table Structure ```xml