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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env python3
"""
Simple Documentation Generator for OpenProblems MCP Server

Generates curated documentation for:
- Nextflow best practices
- Viash components
- OpenProblems guidelines
- Docker patterns
- Spatial workflow templates
"""

import asyncio
import json
from pathlib import Path
from typing import Dict

class DocumentationGenerator:
    def __init__(self, cache_dir: str = "data/docs_cache"):
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(parents=True, exist_ok=True)

    async def generate_all_documentation(self) -> Dict[str, str]:
        """Generate comprehensive curated documentation."""
        print("πŸ“š Generating curated documentation for OpenProblems MCP Server...")

        documentation = {
            "nextflow": self._generate_nextflow_docs(),
            "viash": self._generate_viash_docs(),
            "openproblems": self._generate_openproblems_docs(),
            "docker": self._generate_docker_docs(),
            "spatial_templates": self._generate_spatial_templates()
        }

        # Save to cache
        print("πŸ”„ Saving documentation to cache...")
        await self._save_documentation_cache(documentation)

        return documentation

    def _generate_nextflow_docs(self) -> str:
        """Generate Nextflow documentation."""
        return """# Nextflow DSL2 Best Practices Guide

## Overview
Nextflow enables scalable and reproducible scientific workflows using software containers.

## Essential DSL2 Patterns

### Basic Pipeline Structure
```nextflow
#!/usr/bin/env nextflow
nextflow.enable.dsl=2

params.input = './data/*.h5ad'
params.output_dir = './results'

workflow {
    input_ch = Channel.fromPath(params.input)
    PROCESS_NAME(input_ch)
}
```

### Process Definition
```nextflow
process SPATIAL_ANALYSIS {
    tag "$sample_id"
    label 'process_medium'
    container 'quay.io/biocontainers/scanpy:1.9.1--pyhd8ed1ab_0'
    publishDir "${params.output_dir}/analysis", mode: 'copy'

    input:
    tuple val(sample_id), path(spatial_data)

    output:
    tuple val(sample_id), path("${sample_id}_analyzed.h5ad"), emit: analyzed
    path "${sample_id}_metrics.json", emit: metrics

    script:
    \"\"\"
    #!/usr/bin/env python
    import scanpy as sc
    import json

    adata = sc.read_h5ad('${spatial_data}')
    sc.pp.filter_cells(adata, min_genes=200)
    sc.pp.filter_genes(adata, min_cells=3)
    adata.write('${sample_id}_analyzed.h5ad')

    metrics = {'n_cells': adata.n_obs, 'n_genes': adata.n_vars}
    with open('${sample_id}_metrics.json', 'w') as f:
        json.dump(metrics, f, indent=2)
    \"\"\"
}
```

## Resource Management
```nextflow
process {
    withLabel: 'process_low' {
        cpus = 2
        memory = '4.GB'
        time = '1.h'
    }
    withLabel: 'process_medium' {
        cpus = 4
        memory = '8.GB'
        time = '2.h'
    }
    withLabel: 'process_high' {
        cpus = 8
        memory = '16.GB'
        time = '4.h'
    }
}

docker {
    enabled = true
    runOptions = '-u $(id -u):$(id -g)'
}
```

## Error Handling
```nextflow
process ROBUST_PROCESS {
    errorStrategy 'retry'
    maxRetries 3

    script:
    \"\"\"
    set -euo pipefail
    # Your analysis code here
    \"\"\"
}
```

## Common Issues and Solutions
1. **Out of Memory**: Increase memory allocation
2. **File Not Found**: Check file paths and staging
3. **Container Issues**: Verify container accessibility
4. **Process Hanging**: Check resource requirements
"""

    def _generate_viash_docs(self) -> str:
        """Generate Viash documentation."""
        return """# Viash Component Architecture Guide

## Overview
Viash enables building reusable, portable components across Docker, native, and Nextflow platforms.

## Component Structure

### Configuration File (config.vsh.yaml)
```yaml
name: "spatial_qc"
description: "Spatial transcriptomics quality control component"

argument_groups:
  - name: "Input/Output"
    arguments:
      - name: "--input"
        type: "file"
        description: "Input spatial data (h5ad format)"
        required: true
      - name: "--output"
        type: "file"
        direction: "output"
        description: "Output filtered data"
        required: true

  - name: "Parameters"
    arguments:
      - name: "--min_genes"
        type: "integer"
        description: "Minimum genes per cell"
        default: 200

resources:
  - type: "python_script"
    path: "script.py"

platforms:
  - type: "docker"
    image: "quay.io/biocontainers/scanpy:1.9.1--pyhd8ed1ab_0"
  - type: "nextflow"
```

### Script Implementation
```python
import argparse
import scanpy as sc
import json

parser = argparse.ArgumentParser()
parser.add_argument('--input', required=True)
parser.add_argument('--output', required=True)
parser.add_argument('--min_genes', type=int, default=200)
args = parser.parse_args()

adata = sc.read_h5ad(args.input)
sc.pp.filter_cells(adata, min_genes=args.min_genes)
adata.write(args.output)
```

## Development Workflow
```bash
# Build component
viash build config.vsh.yaml -p docker

# Test component
viash test config.vsh.yaml

# Build for Nextflow
viash build config.vsh.yaml -p nextflow -o target/nextflow/
```

## Best Practices
1. **Single Responsibility**: Each component should do one thing well
2. **Clear Interfaces**: Well-defined inputs and outputs
3. **Comprehensive Testing**: Unit tests for all functionality
4. **Documentation**: Clear descriptions and examples
"""

    def _generate_openproblems_docs(self) -> str:
        """Generate OpenProblems documentation."""
        return """# OpenProblems Framework Guide

## Overview
OpenProblems is a community effort to benchmark single-cell and spatial transcriptomics methods.

## Project Architecture

### Repository Structure
```
src/
β”œβ”€β”€ tasks/                    # Benchmark tasks
β”‚   β”œβ”€β”€ spatial_decomposition/
β”‚   β”‚   β”œβ”€β”€ methods/         # Benchmark methods
β”‚   β”‚   β”œβ”€β”€ metrics/         # Evaluation metrics
β”‚   β”‚   └── datasets/        # Task datasets
β”‚   └── other_tasks/
β”œβ”€β”€ common/                  # Shared components
└── workflows/              # Nextflow workflows
```

### Component Types

#### Dataset Components
Load benchmark datasets with standardized formats.

#### Method Components
Implement spatial analysis methods following OpenProblems standards.

#### Metric Components
Evaluate method performance with standardized metrics.

## Data Formats

### AnnData Structure
```python
import anndata as ad

# Spatial data structure
adata_spatial = ad.read_h5ad('spatial_data.h5ad')
# adata_spatial.X: expression matrix
# adata_spatial.obs: spot metadata
# adata_spatial.var: gene metadata
# adata_spatial.obsm['spatial']: spatial coordinates

# Reference single-cell data
adata_reference = ad.read_h5ad('reference_data.h5ad')
# adata_reference.obs['cell_type']: cell type annotations
```

### Standard Metadata Fields
- **Cell types**: obs['cell_type']
- **Spatial coordinates**: obsm['spatial']
- **Batch information**: obs['batch']

## Best Practices
- Follow OpenProblems naming conventions
- Use standard data formats (AnnData h5ad)
- Include comprehensive documentation
- Ensure reproducibility across platforms
"""

    def _generate_docker_docs(self) -> str:
        """Generate Docker documentation."""
        return """# Docker Best Practices for Bioinformatics

## Multi-stage Builds

### Optimized Python Environment
```dockerfile
# Build stage
FROM python:3.9-slim as builder
WORKDIR /build
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt

# Production stage
FROM python:3.9-slim
COPY --from=builder /root/.local /root/.local
RUN apt-get update && apt-get install -y procps
WORKDIR /app
```

### Bioinformatics Stack
```dockerfile
FROM python:3.9-slim

RUN apt-get update && apt-get install -y --no-install-recommends \\
    libhdf5-dev \\
    libblas-dev \\
    liblapack-dev \\
    && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir \\
    scanpy>=1.9.0 \\
    anndata>=0.8.0 \\
    pandas>=1.5.0 \\
    numpy>=1.21.0

WORKDIR /app
```

### OpenProblems Compatible Container
```dockerfile
FROM python:3.9-slim

RUN apt-get update && apt-get install -y procps
RUN pip install --no-cache-dir scanpy anndata pandas numpy

# Create non-root user for Nextflow
RUN groupadd -g 1000 nextflow && \\
    useradd -u 1000 -g nextflow nextflow

USER nextflow
WORKDIR /app
ENTRYPOINT ["python"]
```

## Best Practices
- Use specific versions for reproducibility
- Use minimal base images
- Create non-root users
- Combine RUN commands to reduce layers
- Use health checks for services
- Set appropriate resource limits
"""

    def _generate_spatial_templates(self) -> str:
        """Generate spatial workflow templates."""
        return """# Spatial Transcriptomics Pipeline Templates

## 1. Quality Control Workflow

```nextflow
#!/usr/bin/env nextflow
nextflow.enable.dsl=2

params.input_pattern = "*.h5ad"
params.output_dir = "./results"
params.min_genes_per_cell = 200

process SPATIAL_QC {
    tag "$sample_id"
    label 'process_medium'
    container 'quay.io/biocontainers/scanpy:1.9.1--pyhd8ed1ab_0'
    publishDir "${params.output_dir}/qc", mode: 'copy'

    input:
    tuple val(sample_id), path(spatial_data)

    output:
    tuple val(sample_id), path("${sample_id}_qc.h5ad"), emit: filtered_data
    path "${sample_id}_metrics.json", emit: metrics

    script:
    \"\"\"
    #!/usr/bin/env python
    import scanpy as sc
    import json

    adata = sc.read_h5ad('${spatial_data}')

    # QC metrics
    adata.var['mt'] = adata.var_names.str.startswith('MT-')
    sc.pp.calculate_qc_metrics(adata, percent_top=None, log1p=False, inplace=True)

    # Filter cells and genes
    sc.pp.filter_cells(adata, min_genes=${params.min_genes_per_cell})
    sc.pp.filter_genes(adata, min_cells=3)

    adata.write('${sample_id}_qc.h5ad')

    metrics = {
        'sample_id': '${sample_id}',
        'n_cells': int(adata.n_obs),
        'n_genes': int(adata.n_vars)
    }

    with open('${sample_id}_metrics.json', 'w') as f:
        json.dump(metrics, f, indent=2)
    \"\"\"
}

workflow {
    input_ch = Channel.fromPath(params.input_pattern)
        .map { file -> [file.baseName, file] }

    SPATIAL_QC(input_ch)
}
```

## 2. Spatial Decomposition Pipeline

```nextflow
process SPATIAL_DECOMPOSITION {
    tag "$sample_id"
    label 'process_high'
    container 'openproblems/spatial-decomposition:latest'

    input:
    tuple val(sample_id), path(spatial_data), path(reference_data)

    output:
    tuple val(sample_id), path("${sample_id}_decomposition.h5ad"), emit: results
    path "${sample_id}_proportions.csv", emit: proportions

    script:
    \"\"\"
    #!/usr/bin/env python
    import anndata as ad
    import pandas as pd
    import numpy as np

    # Load data
    adata_spatial = ad.read_h5ad('${spatial_data}')
    adata_reference = ad.read_h5ad('${reference_data}')

    # Find common genes
    common_genes = adata_spatial.var_names.intersection(adata_reference.var_names)
    adata_spatial = adata_spatial[:, common_genes].copy()
    adata_reference = adata_reference[:, common_genes].copy()

    # Get cell types
    cell_types = adata_reference.obs['cell_type'].unique()

    # Placeholder decomposition (replace with actual method)
    n_spots = adata_spatial.n_obs
    n_cell_types = len(cell_types)
    proportions_matrix = np.random.dirichlet(np.ones(n_cell_types), size=n_spots)

    # Create proportions DataFrame
    proportions_df = pd.DataFrame(
        proportions_matrix,
        columns=cell_types,
        index=adata_spatial.obs_names
    )

    proportions_df.to_csv('${sample_id}_proportions.csv')

    # Add proportions to spatial data
    for cell_type in cell_types:
        adata_spatial.obs[f'prop_{cell_type}'] = proportions_df[cell_type].values

    adata_spatial.write('${sample_id}_decomposition.h5ad')
    \"\"\"
}
```

## 3. Configuration Template

```nextflow
// nextflow.config
params {
    input_dir = './data'
    output_dir = './results'
    reference_data = './reference/atlas.h5ad'
}

process {
    withLabel: 'process_medium' {
        cpus = 4
        memory = '8.GB'
        time = '2.h'
    }
    withLabel: 'process_high' {
        cpus = 8
        memory = '16.GB'
        time = '4.h'
    }
}

docker {
    enabled = true
    runOptions = '-u $(id -u):$(id -g)'
}
```

This provides:
1. **Production-ready QC pipeline** with filtering and reporting
2. **Spatial decomposition workflow** with evaluation metrics
3. **Flexible configuration** for different environments
4. **Comprehensive monitoring** and resource tracking
"""

    async def _save_documentation_cache(self, documentation: Dict[str, str]):
        """Save documentation to cache files."""
        for source, content in documentation.items():
            cache_file = self.cache_dir / f"{source}_docs.md"
            with open(cache_file, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"   πŸ’Ύ Cached {source} documentation ({len(content):,} chars)")

    async def load_cached_documentation(self) -> Dict[str, str]:
        """Load documentation from cache if available."""
        documentation = {}

        for source in ["nextflow", "viash", "openproblems", "docker", "spatial_templates"]:
            cache_file = self.cache_dir / f"{source}_docs.md"
            if cache_file.exists():
                with open(cache_file, 'r', encoding='utf-8') as f:
                    documentation[source] = f.read()

        return documentation

async def main():
    """Main function to generate and cache documentation."""
    print("πŸ“š OpenProblems Documentation Generator")
    print("=" * 50)

    generator = DocumentationGenerator()

    print("πŸ”„ Generating curated documentation...")
    documentation = await generator.generate_all_documentation()

    print(f"\nπŸ“Š Documentation generation complete!")
    total_chars = 0
    for source, content in documentation.items():
        chars = len(content)
        total_chars += chars
        print(f"   βœ… {source}: {chars:,} characters")

    print(f"\nπŸŽ‰ Total: {total_chars:,} characters of documentation cached!")
    print("   πŸ’Ύ Documentation saved to: data/docs_cache/")
    print("   πŸ”— Now available via MCP Resources in your server")

    return documentation

if __name__ == "__main__":
    asyncio.run(main())