-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_example.py
More file actions
318 lines (269 loc) · 11.5 KB
/
generate_example.py
File metadata and controls
318 lines (269 loc) · 11.5 KB
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
#!/usr/bin/env python3
"""Generate new METAINFORMANT examples from templates.
This script creates new example files based on domain-specific templates,
ensuring consistent structure and best practices.
Usage:
python scripts/generate_example.py <domain> <name> [--description TEXT] [--features LIST]
Arguments:
domain: Target domain (dna, rna, gwas, etc.)
name: Example name (will create example_<name>.py)
Options:
--description: Brief description of what the example demonstrates
--features: Comma-separated list of features demonstrated
--template: Specific template to use (default: auto-detect)
--dry-run: Show what would be created without creating files
"""
from __future__ import annotations
import argparse
import re
from pathlib import Path
from typing import Dict, List
class ExampleGenerator:
"""Generate METAINFORMANT examples from templates."""
def __init__(self):
self.templates_dir = Path("examples/templates")
self.examples_dir = Path("examples")
# Domain-specific configurations
self.domain_configs = {
"core": {
"template": "base_template.py",
"description": "Core utilities demonstration",
"features": ["Configuration management", "I/O operations", "Logging setup"],
},
"dna": {
"template": "dna_template.py",
"description": "DNA sequence analysis",
"features": ["Sequence processing", "Analysis algorithms", "Result visualization"],
},
"rna": {
"template": "rna_template.py",
"description": "RNA analysis workflows",
"features": ["Transcriptomic analysis", "Expression quantification", "Workflow orchestration"],
},
"gwas": {
"template": "gwas_template.py",
"description": "Genome-wide association studies",
"features": ["Association testing", "Statistical analysis", "Visualization"],
},
"protein": {
"template": "protein_template.py",
"description": "Protein sequence analysis",
"features": ["Structure analysis", "Sequence alignment", "Property calculation"],
},
"ml": {
"template": "ml_template.py",
"description": "Machine learning analysis",
"features": ["Model training", "Feature selection", "Performance evaluation"],
},
}
def generate_example(
self,
domain: str,
name: str,
description: str | None = None,
features: List[str] | None = None,
template: str | None = None,
dry_run: bool = False,
) -> Dict[str, Any]:
"""Generate a new example file."""
if domain not in self.domain_configs:
available_domains = list(self.domain_configs.keys())
raise ValueError(f"Unknown domain '{domain}'. Available: {available_domains}")
# Get domain config
config = self.domain_configs[domain]
# Set defaults
description = description or config["description"]
features = features or config["features"]
template = template or config["template"]
# Template path
template_path = self.templates_dir / template
if not template_path.exists():
raise FileNotFoundError(f"Template not found: {template_path}")
# Output paths
example_dir = self.examples_dir / domain
example_file = example_dir / f"example_{name}.py"
# Check if example already exists
if example_file.exists():
raise FileExistsError(f"Example already exists: {example_file}")
# Load template
with open(template_path, "r", encoding="utf-8") as f:
template_content = f.read()
# Prepare template variables
template_vars = {
"domain": domain,
"name": name,
"description": description,
"features": features,
"analysis_type": self._get_analysis_type(domain, name),
"imports": self._get_domain_imports(domain),
"custom_imports": self._get_custom_imports(domain),
"code_block": self._get_code_block(domain, name),
}
# Render template
rendered_content = self._render_template(template_content, template_vars)
if dry_run:
return {
"action": "dry_run",
"example_file": str(example_file),
"content": rendered_content,
"template_vars": template_vars,
}
# Create directory if needed
example_dir.mkdir(parents=True, exist_ok=True)
# Write example file
with open(example_file, "w", encoding="utf-8") as f:
f.write(rendered_content)
# Update README if it exists
readme_path = example_dir / "README.md"
if readme_path.exists():
self._update_readme(readme_path, name, description)
return {
"action": "created",
"example_file": str(example_file),
"readme_updated": readme_path.exists(),
"template_used": template,
}
def _render_template(self, template: str, variables: Dict[str, Any]) -> str:
"""Render template with variable substitution."""
# Simple template rendering (could be enhanced with Jinja2)
result = template
# Replace {{variable}} patterns
for key, value in variables.items():
if isinstance(value, list):
# Handle list variables
if key == "features":
# Special handling for features list
features_text = "\n".join(f"- {feature}" for feature in value)
result = result.replace(
"{% for feature in features -%}\n- {{ feature }}\n{% endfor %}", features_text
)
elif key == "imports":
imports_text = "\n".join(value)
result = result.replace(
"{% for import_line in imports -%}\n{{ import_line }}\n{% endfor %}", imports_text
)
else:
result = result.replace(f"{{{{ {key} }}}}", str(value))
else:
result = result.replace(f"{{{{ {key} }}}}", str(value))
# Clean up any remaining template tags
result = re.sub(r"{%.*?%}", "", result)
result = re.sub(r"{{.*?}}", "", result)
return result
def _get_analysis_type(self, domain: str, name: str) -> str:
"""Get analysis type based on domain and name."""
analysis_types = {
"dna": {
"sequences": "Sequence",
"alignment": "Alignment",
"phylogeny": "Phylogenetic",
"population": "Population Genetics",
},
"rna": {"amalgkit": "Amalgkit Workflow", "quantification": "Expression Quantification"},
"gwas": {"association": "Association Testing", "visualization": "GWAS Visualization"},
}
return analysis_types.get(domain, {}).get(name, "Analysis")
def _get_domain_imports(self, domain: str) -> List[str]:
"""Get domain-specific imports."""
imports_map = {
"dna": ["from metainformant.dna import sequences, alignment"],
"rna": ["from metainformant.rna import workflow"],
"gwas": ["from metainformant.gwas import association"],
"protein": ["from metainformant.protein import sequences"],
"ml": ["from metainformant.ml import classification"],
"core": [],
}
return imports_map.get(domain, [])
def _get_custom_imports(self, domain: str) -> str:
"""Get custom imports section."""
return "# Additional imports as needed"
def _get_code_block(self, domain: str, name: str) -> str:
"""Get the main code block for the example."""
code_blocks = {
"dna": """
# Perform sequence analysis
if "{{analysis_type}}" == "Sequence":
results_data[seq_id].update({
"reverse_complement": sequences.reverse_complement(sequence),
"gc_skew": sequences.gc_content(sequence) - 0.5, # Simplified
"complexity": len(set(sequence)) / len(sequence) # Simplified
})
elif "{{analysis_type}}" == "Alignment":
# Simple alignment example
results_data[seq_id]["aligned_with_seq1"] = "Example alignment result"
""",
"core": """
# Example core functionality
results_data = {
"config_loaded": True,
"io_operations": ["load_json", "dump_json"],
"logging_setup": True,
"paths_validated": True
}
""",
}
return code_blocks.get(
domain,
"""
# Add your example code here
results_data = {
"placeholder": "Replace with actual analysis results"
}
""",
)
def _update_readme(self, readme_path: Path, name: str, description: str) -> None:
"""Update domain README to include new example."""
try:
with open(readme_path, "r", encoding="utf-8") as f:
content = f.read()
# Add to examples list if there's a section
if "## Examples" in content:
example_entry = f"- `example_{name}.py`: {description}\n"
content = content.replace("## Examples", f"## Examples\n{example_entry}", 1)
with open(readme_path, "w", encoding="utf-8") as f:
f.write(content)
except Exception:
# Don't fail if README update fails
pass
def main():
"""Main function."""
parser = argparse.ArgumentParser(description="Generate METAINFORMANT examples")
parser.add_argument("domain", help="Target domain (dna, rna, gwas, etc.)")
parser.add_argument("name", help="Example name")
parser.add_argument("--description", help="Brief description")
parser.add_argument("--features", help="Comma-separated list of features")
parser.add_argument("--template", help="Specific template to use")
parser.add_argument("--dry-run", action="store_true", help="Show what would be created")
args = parser.parse_args()
# Parse features
features = None
if args.features:
features = [f.strip() for f in args.features.split(",")]
# Create generator
generator = ExampleGenerator()
try:
result = generator.generate_example(
domain=args.domain,
name=args.name,
description=args.description,
features=features,
template=args.template,
dry_run=args.dry_run,
)
if args.dry_run:
print("DRY RUN - Would create:")
print(f"File: {result['example_file']}")
print("\nContent preview:")
print("-" * 40)
print(result["content"][:500] + "..." if len(result["content"]) > 500 else result["content"])
else:
print(f"✅ Created example: {result['example_file']}")
if result.get("readme_updated"):
print("✅ Updated README.md")
print(f"Template used: {result['template_used']}")
except Exception as e:
print(f"❌ Error: {e}")
return 1
return 0
if __name__ == "__main__":
exit(main())