# Sar Analyzer

> >

- **Type:** Skill
- **Install:** `agentstack add skill-qinsizheng-sar-analyzer-skill-sar-analyzer-skill`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [qinsizheng](https://agentstack.voostack.com/s/qinsizheng)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [qinsizheng](https://github.com/qinsizheng)
- **Source:** https://github.com/qinsizheng/sar-analyzer-skill

## Install

```sh
agentstack add skill-qinsizheng-sar-analyzer-skill-sar-analyzer-skill
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# SAR Analyzer Skill

## Overview

The **SAR Analyzer** skill provides a complete computational workflow for Structure-Activity Relationship (SAR) analysis of small molecule libraries. It is designed for medicinal chemists and computational chemists working on drug discovery projects who need to:

- **Identify and cluster molecular scaffolds** using Murcko framework extraction
- **Perform R-group decomposition** to understand substituent contributions to activity
- **Calculate molecular properties** and efficiency metrics (LE, LLE)
- **Generate publication-quality SAR tables** with chemical structures
- **Visualize potency-lipophilicity relationships** and scaffold comparisons
- **Identify optimal compounds** and prioritize scaffolds for further optimization

This skill integrates RDKit-based cheminformatics with statistical analysis and visualization to provide actionable insights for medicinal chemistry campaigns.

---

## When to Use This Skill

Use the **SAR Analyzer** skill when the user requests:

- SAR analysis of a compound library
- Scaffold identification or clustering
- R-group decomposition or substituent analysis
- Molecular property calculations (MW, cLogP, TPSA, etc.)
- Ligand efficiency (LE) or lipophilic ligand efficiency (LLE) calculations
- Generation of SAR tables with structures
- Identification of optimal compounds or scaffolds
- Analysis of structure-activity trends
- Comparison of chemical series or scaffolds

**Typical user queries:**
- "Analyze the SAR of this compound library"
- "Identify scaffolds and perform R-group decomposition"
- "Which compounds have the best LLE?"
- "Generate SAR tables for each scaffold"
- "What are the key SAR trends in this dataset?"

---

## Required Input Data

The skill expects a **CSV file** with the following columns:

### Mandatory Columns:
- `Example_Number` or `Compound_ID`: Unique identifier for each compound
- `SMILES`: Molecular structure in SMILES notation
- Activity data (at least one):
  - `Ki_uM` or `IC50_uM` 

### Optional Columns:
- `Chemical_Name`: Compound name or identifier
- `cLogP`: Pre-calculated lipophilicity (will be calculated if missing)
- Any other experimental data

### Data Format Notes:
- Ki/IC50 values may contain special characters: ``, `≥` (will be parsed correctly)
- Missing activity values are handled gracefully
- SMILES must be valid and parseable by RDKit

### Example Files:

The skill includes example files in the `examples/` directory:

- **`example_input.csv`**: Example input file demonstrating the expected CSV format with compound structures (SMILES), activity data (Ki values), and identifiers. Use this as a template for preparing your own compound libraries.

- **`Example_SAR_Table.png`**: Example output showing a publication-quality SAR table for scaffold S1 with R-group decomposition, activity data, and color-coded highlighting. This demonstrates the expected output format when using the SAR table generation workflow.

**Note**: The workflow using this skill should generate SAR table figures similar to `Example_SAR_Table.png`, with:
- Core scaffold structure with labeled R-group positions
- R-group substituents displayed as chemical structures
- Activity data columns (Ki values, LLE, cLogP)
- Color-coded highlighting for top compounds and excellent values
- Summary statistics

---

## Complete SAR Analysis Workflow

### Step 1: Initial Data Loading and Property Calculation

**Script**: `sar_analysis.py`

**Purpose**: Load the CSV data, parse SMILES structures, calculate molecular properties, and compute efficiency metrics.

**Usage**:
```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_analysis.py 
```

**What it does**:
1. Loads CSV and validates SMILES structures
2. Parses Ki/IC50 values (handles , ≥ symbols)
3. Calculates molecular properties:
   - Molecular weight (MW)
   - Lipophilicity (cLogP using Wildman-Crippen)
   - Hydrogen bond acceptors/donors (HBA/HBD)
   - Topological polar surface area (TPSA)
   - Rotatable bonds
   - Heavy atom count
4. Computes efficiency metrics:
   - pKi = -log₁₀(Ki in M)
   - Ligand Efficiency (LE) = pKi / Heavy Atoms
   - Lipophilic Ligand Efficiency (LLE) = pKi - cLogP
5. Generates summary statistics

**Output**: `data_with_analysis.csv` (full dataset with calculated properties)

**Example**:
```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_analysis.py Table1_Complete_with_SMILES.csv
```

---

### Step 2: Scaffold Identification and Clustering

**Script**: `scaffold_analysis.py`

**Purpose**: Generate Murcko scaffolds, cluster compounds by scaffold, and calculate scaffold-level statistics.

**Usage**:
```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/scaffold_analysis.py
```

**Prerequisites**: Must run after `sar_analysis.py` (requires `data_with_analysis.csv`)

**What it does**:
1. Generates Murcko scaffolds (removes side chains, keeps ring systems)
2. Clusters compounds by identical scaffold
3. Identifies major scaffolds (≥5 compounds by default)
4. Calculates scaffold statistics:
   - Number of compounds per scaffold
   - Mean/median/min/max activity
   - Mean LLE and other properties
5. Assigns scaffold IDs (S1, S2, S3, etc.)

**Outputs**:
- `scaffold_statistics.csv`: Summary statistics per scaffold
- `scaffold_mapping.csv`: Compound-to-scaffold mapping
- `data_with_scaffolds.csv`: Full data with scaffold assignments

**Customization**: Edit the script to change the minimum compound threshold (default: 5)

---

### Step 3: R-Group Decomposition

**Script**: `rgroup_analysis.py`

**Purpose**: Decompose compounds into core scaffold + R-groups to understand substituent contributions.

**Usage**:
```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/rgroup_analysis.py
```

**Prerequisites**: Must run after `scaffold_analysis.py`

**What it does**:
1. For each major scaffold, defines core structure with attachment points
2. Performs R-group decomposition using RDKit
3. Extracts R-group SMILES for each position
4. Maps R-groups to activity data
5. Identifies SAR trends per R-group position

**Output**: `rgroup_decomposition.csv`

**Columns**:
- `Core`: Core scaffold SMILES with attachment points ([*:1], [*:2], etc.)
- `R1`, `R2`, `R3`, ...: R-group SMILES for each position
- `Example_Number`: Compound identifier
- `Ki_BD1`, `Ki_BD2`, `Sum_Ki`: Activity data
- `cLogP`, `Avg_LLE`: Physicochemical properties
- `Scaffold_ID`: Scaffold identifier (S1, S2, etc.)

**Note**: The number of R-groups varies by scaffold (typically 4-9 positions)

---

### Step 4: Statistical Analysis and SAR Trends

**Script**: `sar_trends_analysis.py`

**Purpose**: Perform correlation analysis, identify top compounds, and analyze SAR trends.

**Usage**:
```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_trends_analysis.py
```

**Prerequisites**: Requires `data_with_analysis.csv` and `scaffold_statistics.csv`

**What it does**:
1. Correlation analysis:
   - Pearson and Spearman correlations
   - cLogP vs potency, MW vs potency, etc.
2. Scaffold comparison:
   - One-way ANOVA or Kruskal-Wallis test
   - Identify significantly different scaffolds
3. Top compound identification:
   - Best potency (lowest Ki)
   - Best LLE (highest efficiency)
   - Best LE (highest ligand efficiency)
4. Generates summary statistics

**Output**: Console output with statistical results (can be redirected to file)

---

### Step 5: Visualization

**Script**: `create_visualizations.py`

**Purpose**: Generate publication-quality figures for SAR analysis.

**Usage**:
```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/create_visualizations.py
```

**Prerequisites**: Requires `data_with_analysis.csv` and `scaffold_statistics.csv`

**What it does**:
1. Potency vs lipophilicity scatter plot (color-coded by LLE)
2. Scaffold comparison bar charts (mean Ki, LLE)
3. Top compounds visualization
4. Property distribution histograms

**Outputs** (in `/figures/` directory):
- `potency_vs_lipophilicity.png`
- `scaffold_comparison.png`
- `top_potency_compounds.png`
- Additional figures as needed

**Customization**: Edit the script to adjust figure size, colors, or add additional plots

---

### Step 6: SAR Table Generation

**Scripts**: 
- `create_s1_s7_tables.py`: Generate detailed SAR tables for all major scaffolds (automatically detects all scaffolds)
- `draw_all_scaffolds.py`: Generate scaffold structure images
- `draw_all_unique_scaffolds.py`: Comprehensive scaffold visualization

**Purpose**: Create publication-ready SAR tables with structures and activity data.

**Usage**:
```bash
# Generate SAR tables for all major scaffolds (automatically processes all scaffolds in the dataset)
python3 /home/ubuntu/skills/sar-analyzer/scripts/create_s1_s7_tables.py

# Generate scaffold structure images
python3 /home/ubuntu/skills/sar-analyzer/scripts/draw_all_scaffolds.py

# Generate comprehensive scaffold overview
python3 /home/ubuntu/skills/sar-analyzer/scripts/draw_all_unique_scaffolds.py
```

**Prerequisites**: Requires `rgroup_decomposition.csv`

**What it does**:
1. Creates SAR tables with:
   - Core scaffold structure with R-group labels
   - R-group substituents for each compound
   - Activity data (Ki, LLE, cLogP)
   - Color-coded highlighting (best compounds in gold, excellent values in green)
   - Summary statistics
2. Generates high-resolution structure images
3. Creates scaffold comparison figures

**Outputs**:
- `SAR_Table_{scaffold_id}_Complete.png` for each major scaffold (e.g., `SAR_Table_S1_Complete.png`, `SAR_Table_S2_Complete.png`, etc.)
- `{scaffold_id}_Core_Structure.png` for each scaffold (e.g., `S1_Core_Structure.png`, `S2_Core_Structure.png`, etc.)
- `all_scaffolds_batch*.png` (from draw_all_scaffolds.py)
- `top20_scaffolds_summary.png` (from draw_all_unique_scaffolds.py)

---

## Efficiency Metrics Explained

### Ligand Efficiency (LE)

**Formula**: `LE = pKi / Heavy Atom Count`

**Interpretation**:
- LE > 0.3: Excellent
- LE 0.25-0.3: Good
- LE  7.0: Excellent (optimal drug-like properties)
- LLE 5.0-7.0: Good
- LLE 3.0-5.0: Moderate
- LLE 7.0) over raw potency
- **Scaffold comparison**: Use statistical tests to confirm differences between scaffolds
- **SAR trends**: Look for consistent patterns across multiple R-group positions
- **Outliers**: Investigate compounds with unusual properties or activity

### 5. Visualization
- **Color coding**: Use consistent color schemes (e.g., green for good, red for poor)
- **Log scale**: Use log scale for activity data (Ki, IC50) in plots
- **Structure images**: Ensure structures are large enough to be readable
- **Publication quality**: Generate figures at 300 dpi for publication

---

## Common Workflows

### Workflow 1: Complete SAR Analysis (Recommended)

Run all scripts in sequence for comprehensive analysis:

```bash
# Step 1: Load data and calculate properties
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_analysis.py input_data.csv

# Step 2: Identify scaffolds
python3 /home/ubuntu/skills/sar-analyzer/scripts/scaffold_analysis.py

# Step 3: R-group decomposition
python3 /home/ubuntu/skills/sar-analyzer/scripts/rgroup_analysis.py

# Step 4: Statistical analysis
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_trends_analysis.py

# Step 5: Generate visualizations
python3 /home/ubuntu/skills/sar-analyzer/scripts/create_visualizations.py

# Step 6: Create SAR tables
python3 /home/ubuntu/skills/sar-analyzer/scripts/create_s1_s7_tables.py
python3 /home/ubuntu/skills/sar-analyzer/scripts/draw_all_unique_scaffolds.py
```

### Workflow 2: Quick Property Calculation

For rapid property calculation without full SAR analysis:

```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_analysis.py input_data.csv
```

Output: `data_with_analysis.csv` with all calculated properties

### Workflow 3: Scaffold-Focused Analysis

For projects focused on scaffold hopping or series comparison:

```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_analysis.py input_data.csv
python3 /home/ubuntu/skills/sar-analyzer/scripts/scaffold_analysis.py
python3 /home/ubuntu/skills/sar-analyzer/scripts/draw_all_unique_scaffolds.py
```

### Workflow 4: R-Group SAR Tables Only

If scaffolds are already known and you want detailed R-group tables:

```bash
python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_analysis.py input_data.csv
python3 /home/ubuntu/skills/sar-analyzer/scripts/scaffold_analysis.py
python3 /home/ubuntu/skills/sar-analyzer/scripts/rgroup_analysis.py
python3 /home/ubuntu/skills/sar-analyzer/scripts/create_s1_s7_tables.py
```

---

## Output Files Reference

### Data Files
| File | Description | Generated By |
|------|-------------|--------------|
| `data_with_analysis.csv` | Full dataset with calculated properties and metrics | `sar_analysis.py` |
| `scaffold_statistics.csv` | Scaffold-level summary statistics | `scaffold_analysis.py` |
| `scaffold_mapping.csv` | Compound-to-scaffold mapping | `scaffold_analysis.py` |
| `data_with_scaffolds.csv` | Full data with scaffold assignments | `scaffold_analysis.py` |
| `rgroup_decomposition.csv` | R-group analysis results | `rgroup_analysis.py` |

### Figure Files
| File | Description | Generated By |
|------|-------------|--------------|
| `potency_vs_lipophilicity.png` | Main SAR plot | `create_visualizations.py` |
| `scaffold_comparison.png` | Scaffold performance comparison | `create_visualizations.py` |
| `top_potency_compounds.png` | Best compounds visualization | `create_visualizations.py` |
| `SAR_Table_S*_Complete.png` | Detailed SAR tables per scaffold | `create_s1_s7_tables.py` |
| `S*_Core_Structure.png` | High-resolution scaffold images | `create_s1_s7_tables.py` |
| `all_scaffolds_batch*.png` | Comprehensive scaffold overview | `draw_all_unique_scaffolds.py` |
| `top20_scaffolds_summary.png` | Top 20 scaffolds summary | `draw_all_unique_scaffolds.py` |

---

## Troubleshooting

### Issue: RDKit cannot parse SMILES
**Solution**: Check for invalid SMILES strings in the input CSV. Use a SMILES validator or regenerate SMILES from structure files.

### Issue: No scaffolds identified
**Solution**: Reduce the minimum scaffold size threshold in `scaffold_analysis.py` (default: 5 compounds).

### Issue: R-group decomposition fails
**Solution**: Ensure the core structure is correctly defined with attachment points. Check that compounds actually match the core scaffold.

### Issue: Missing activity data
**Solution**: The scripts handle missing data gracefully. Compounds with missing Ki/IC50 will be excluded from specific analyses but retained in the dataset.

### Issue: Memory errors with large datasets
**Solution**: Process scaffolds separately or reduce image resolution in visualization scripts.

### Issue: Figures are too small/large
**Solution**: Edit the `figsize` parameter in visualization scripts (e.g., `figsize=(12, 8)`).

---

## Dependencies

The skill requires the following Python packages:

```
rdkit>=2023.9.1
pandas>=2.1.0
numpy>=1.24.3
matplotlib>=3.7.2
seaborn>=0.12.2
scipy>=1.11.2
```

**Installation**:
```bash
sudo pip3 install rdkit pandas numpy matplotlib seaborn scipy
```

**Note**: RDKit is pre-installed in the Manus sandbox environment.

---

## Advanced Usage

### Customizing Scaffold Definitions

To modify scaffold definitions or add custom cores:

1. Edit `rgroup_analysis.py`
2. Define custom core SMILES with attachment points:
   ```python
   core_smiles = "O=c1c2c(ccn1[*:5])C([*:1])=CC1=NN(c3ccc([*:3])cc3[*:4])C3C1=C2NCC3[*:2]"
   ```
3. Update the scaffold ID mapping

### Adding New Efficiency Metrics

To add custom efficiency metrics:

1. Edit `sar_analysis.py`
2. Add calculation after the LLE section:
   ```python
   df['Custom_Metric'] = df['pKi'] - (0.5 * df['cLogP']) + (0.1 * df['TPSA'])
   ```
3. Update visualization scripts to include the new metric

### Batch Processing Multiple Datasets

To analyze multiple datasets:

```bash
for file in *.csv; do
    echo "Processing $file"
    python3 /home/ubuntu/skills/sar-analyzer/scripts/sar_ana

…

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [qinsizheng](https://github.com/qinsizheng)
- **Source:** [qinsizheng/sar-analyzer-skill](https://github.com/qinsizheng/sar-analyzer-skill)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-qinsizheng-sar-analyzer-skill-sar-analyzer-skill
- Seller: https://agentstack.voostack.com/s/qinsizheng
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
