|
| 1 | +# Parallelization Fix for nf-proteindesign |
| 2 | + |
| 3 | +## Problem Summary |
| 4 | + |
| 5 | +The pipeline was not correctly parallelizing Boltz2 refolding across all ProteinMPNN-generated sequences from all budget designs. |
| 6 | + |
| 7 | +### Original Behavior: |
| 8 | +1. ✅ Boltzgen runs once per samplesheet row |
| 9 | +2. ✅ Takes N best designs based on budget parameter (e.g., budget=2 → 2 designs) |
| 10 | +3. ✅ ProteinMPNN runs once for EACH budget design (parallel execution) |
| 11 | +4. ❌ **Boltz2 only refolded sequences from ONE budget design** (not all) |
| 12 | +5. ✅ ipSAE and Prodigy run on structures (but missing some due to Boltz2 issue) |
| 13 | + |
| 14 | +### Desired Behavior: |
| 15 | +1. ✅ Run Boltzgen on each row of samplesheet in parallel |
| 16 | +2. ✅ Take the N best designs based on budget number (high quality, filtered designs) |
| 17 | +3. ✅ Run ProteinMPNN on these sequences to generate X new sequences per design |
| 18 | +4. ✅ **Run Boltz2 in parallel to fold each ProteinMPNN sequence from ALL budget designs** |
| 19 | +5. ✅ Skip the first sequence in each ProteinMPNN FASTA (it's the original Boltzgen sequence) |
| 20 | +6. ✅ Calculate ipSAE and Prodigy on original Boltzgen + all ProteinMPNN refolded structures |
| 21 | +7. ✅ Combine all metrics into one comprehensive table |
| 22 | + |
| 23 | +## Root Cause |
| 24 | + |
| 25 | +The issue was in the channel joining logic in `workflows/protein_design.nf`: |
| 26 | + |
| 27 | +### Original Code (Lines 108-120): |
| 28 | +```groovy |
| 29 | +ch_boltz2_per_sequence = PROTEINMPNN_OPTIMIZE.out.sequences |
| 30 | + .flatMap { ... } |
| 31 | + .map { meta, fasta -> |
| 32 | + [meta.parent_id, meta, fasta] |
| 33 | + } |
| 34 | + .join( // ❌ PROBLEM: join only matches FIRST item with same key! |
| 35 | + EXTRACT_TARGET_SEQUENCES.out.target_sequences.map { meta, seq -> |
| 36 | + [meta.id, seq] |
| 37 | + } |
| 38 | + ) |
| 39 | + .map { parent_id, meta, fasta, target_seq -> |
| 40 | + [meta, fasta, target_seq] |
| 41 | + } |
| 42 | +``` |
| 43 | + |
| 44 | +### Why This Failed: |
| 45 | +- **Multiple ProteinMPNN outputs** from different budget designs: |
| 46 | + - `2vsm_protein_binder_rank_1` (parent_id: "2vsm_protein_binder") |
| 47 | + - `2vsm_protein_binder_rank_2` (parent_id: "2vsm_protein_binder") |
| 48 | +- **One EXTRACT_TARGET_SEQUENCES output**: |
| 49 | + - `2vsm_protein_binder` (id: "2vsm_protein_binder") |
| 50 | +- **join() behavior**: When multiple items have the same key, join only matches the FIRST one and drops the rest! |
| 51 | + |
| 52 | +## Solution |
| 53 | + |
| 54 | +### Fix #1: Use `combine` Instead of `join` |
| 55 | + |
| 56 | +Changed from `join()` to `combine(by: 0)` to ensure ALL ProteinMPNN sequences are paired with the target sequence: |
| 57 | + |
| 58 | +```groovy |
| 59 | +ch_boltz2_per_sequence = PROTEINMPNN_OPTIMIZE.out.sequences |
| 60 | + .flatMap { ... } |
| 61 | + .map { meta, fasta -> |
| 62 | + [meta.parent_id, meta, fasta] |
| 63 | + } |
| 64 | + .combine( // ✅ FIXED: combine pairs ALL items with same key! |
| 65 | + EXTRACT_TARGET_SEQUENCES.out.target_sequences.map { meta, seq -> |
| 66 | + [meta.id, seq] |
| 67 | + }, |
| 68 | + by: 0 // Combine by parent_id (index 0) |
| 69 | + ) |
| 70 | + .map { parent_id, meta, fasta, target_seq -> |
| 71 | + [meta, fasta, target_seq] |
| 72 | + } |
| 73 | +``` |
| 74 | + |
| 75 | +**Key Difference:** |
| 76 | +- `join()`: One-to-one matching (first match only) |
| 77 | +- `combine(by: key)`: All-to-all matching for items with same key |
| 78 | + |
| 79 | +### Fix #2: Skip First Sequence in ProteinMPNN FASTA |
| 80 | + |
| 81 | +Added logic in `modules/local/boltz2_refold.nf` to skip the first sequence: |
| 82 | + |
| 83 | +```python |
| 84 | +# Skip the first sequence (it's always the original sequence from Boltzgen) |
| 85 | +# We only want to refold the NEW sequences generated by ProteinMPNN |
| 86 | +sequences_to_process = sequences[1:] if len(sequences) > 1 else [] |
| 87 | + |
| 88 | +if not sequences_to_process: |
| 89 | + print(f"⚠ Warning: Only found 1 sequence (original), no new MPNN sequences to refold") |
| 90 | + continue |
| 91 | + |
| 92 | +print(f"Processing {len(sequences_to_process)} new MPNN sequences (skipping original)") |
| 93 | +``` |
| 94 | + |
| 95 | +**Why This Matters:** |
| 96 | +- ProteinMPNN FASTA files always include the original sequence as the first entry |
| 97 | +- We already have the Boltzgen structure for this sequence |
| 98 | +- No need to refold it again with Boltz2 |
| 99 | +- Only refold the NEW ProteinMPNN-optimized sequences |
| 100 | + |
| 101 | +## Expected Flow After Fix |
| 102 | + |
| 103 | +### Example with budget=2, mpnn_num_seq_per_target=8: |
| 104 | + |
| 105 | +1. **Boltzgen**: Generates 2 designs (rank_1, rank_2) |
| 106 | + - `2vsm_protein_binder_output/intermediate_designs_inverse_folded/rank_1.cif` |
| 107 | + - `2vsm_protein_binder_output/intermediate_designs_inverse_folded/rank_2.cif` |
| 108 | + |
| 109 | +2. **ProteinMPNN**: Runs on EACH design (2 parallel executions) |
| 110 | + - Processes `rank_1.pdb` → generates 8 sequences (1 original + 7 new) |
| 111 | + - Processes `rank_2.pdb` → generates 8 sequences (1 original + 7 new) |
| 112 | + - Total: 16 FASTA files (8 per design) |
| 113 | + |
| 114 | +3. **Extract Target Sequences**: Runs once |
| 115 | + - Extracts target sequence from first Boltzgen CIF |
| 116 | + - Output: One target sequence file (same for all designs) |
| 117 | + |
| 118 | +4. **Boltz2 Refolding**: Runs on ALL new sequences (14 parallel executions) |
| 119 | + - From rank_1: 7 new sequences × 1 = 7 Boltz2 runs |
| 120 | + - From rank_2: 7 new sequences × 1 = 7 Boltz2 runs |
| 121 | + - Total: 14 Boltz2 predictions (skipping 2 original sequences) |
| 122 | + |
| 123 | +5. **ipSAE**: Runs on all structures |
| 124 | + - 2 original Boltzgen structures (rank_1, rank_2) |
| 125 | + - 14 Boltz2 refolded structures |
| 126 | + - Total: 16 ipSAE calculations |
| 127 | + |
| 128 | +6. **Prodigy**: Runs on all structures |
| 129 | + - 2 original Boltzgen structures |
| 130 | + - 14 Boltz2 refolded structures |
| 131 | + - Total: 16 Prodigy predictions |
| 132 | + |
| 133 | +7. **Consolidation**: Combines all metrics into one table |
| 134 | + - 16 rows (2 Boltzgen + 14 Boltz2) |
| 135 | + - Columns: design_name, source, pLDDT, ipSAE, prodigy_affinity, etc. |
| 136 | + |
| 137 | +## Testing Recommendations |
| 138 | + |
| 139 | +1. **Test with budget=1**: Verify ProteinMPNN + Boltz2 work with single design |
| 140 | +2. **Test with budget=2**: Verify all sequences from both designs are refolded |
| 141 | +3. **Check logs**: Ensure "Processing X new MPNN sequences (skipping original)" appears |
| 142 | +4. **Verify counts**: |
| 143 | + - ProteinMPNN sequences = budget × mpnn_num_seq_per_target |
| 144 | + - Boltz2 predictions = budget × (mpnn_num_seq_per_target - 1) |
| 145 | + - Total structures = budget + [budget × (mpnn_num_seq_per_target - 1)] |
| 146 | + |
| 147 | +## Files Modified |
| 148 | + |
| 149 | +1. `workflows/protein_design.nf` (lines 108-120) |
| 150 | + - Changed `join()` to `combine(by: 0)` |
| 151 | + |
| 152 | +2. `modules/local/boltz2_refold.nf` (lines ~114-120) |
| 153 | + - Added logic to skip first sequence in FASTA files |
| 154 | + |
| 155 | +## Channel Operation Comparison |
| 156 | + |
| 157 | +### `join()` behavior: |
| 158 | +``` |
| 159 | +Channel A: [key1, dataA1], [key1, dataA2] |
| 160 | +Channel B: [key1, dataB] |
| 161 | +Result: [key1, dataA1, dataB] // Only FIRST match! |
| 162 | + [dataA2 is DROPPED] |
| 163 | +``` |
| 164 | + |
| 165 | +### `combine(by: 0)` behavior: |
| 166 | +``` |
| 167 | +Channel A: [key1, dataA1], [key1, dataA2] |
| 168 | +Channel B: [key1, dataB] |
| 169 | +Result: [key1, dataA1, dataB] // ALL matches! |
| 170 | + [key1, dataA2, dataB] |
| 171 | +``` |
| 172 | + |
| 173 | +This is exactly what we need - every ProteinMPNN sequence paired with the target sequence! |
0 commit comments