-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_evidence_source.py
More file actions
377 lines (308 loc) · 13.5 KB
/
add_evidence_source.py
File metadata and controls
377 lines (308 loc) · 13.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
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
#!/usr/bin/env python3
"""
Add Evidence Source to YAML Files
Automatically adds evidence_source field to evidence items based on:
1. Paper keywords (field study, culture, metagenome, etc.)
2. Community origin (natural=IN_VIVO, engineered=IN_VITRO)
3. Manual review mode for uncertain cases
Usage:
# Automatic mode (uses heuristics)
python scripts/add_evidence_source.py --auto
# Interactive mode (ask for each item)
python scripts/add_evidence_source.py --interactive
# Single file
python scripts/add_evidence_source.py --file FILE.yaml --auto
"""
import sys
from pathlib import Path
from typing import Dict, Optional
import yaml
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from communitymech.literature_enhanced import EnhancedLiteratureFetcher
from communitymech.curate.curation_event import record_curation_event
from communitymech.validation.write_validated import (
ValidationFailedError,
write_validated_community,
)
class EvidenceSourceAdder:
"""Add evidence_source to evidence items"""
def __init__(self):
self.fetcher = EnhancedLiteratureFetcher(
cache_dir=".literature_cache",
use_fallback_pdf=False
)
self.stats = {
'total_evidence': 0,
'already_has_source': 0,
'auto_added': 0,
'manual_added': 0,
'skipped': 0
}
# Keywords for automatic classification
self.in_vitro_keywords = [
'culture', 'batch', 'bioreactor', 'enrichment', 'laboratory',
'pure culture', 'co-culture', 'synthetic', 'engineered',
'flask', 'medium', 'cultivation', 'isolated', 'strain'
]
self.in_vivo_keywords = [
'field', 'environmental', 'sample', 'site', 'natural',
'sediment', 'soil', 'water', 'community', 'microbiome',
'16s rrna', 'metagenome', 'metatranscriptome', 'amplicon'
]
self.computational_keywords = [
'genome', 'model', 'simulation', 'bioinformatic', 'in silico',
'reconstruction', 'prediction', 'annotation', 'assembly',
'metabolic network', 'flux balance'
]
self.review_keywords = [
'review', 'meta-analysis', 'survey'
]
def guess_evidence_source(
self,
snippet: str,
abstract: str = None,
title: str = None,
community_origin: str = None
) -> Optional[str]:
"""Guess evidence source using heuristics"""
# Combine text for keyword matching
text = ' '.join(filter(None, [snippet, abstract, title])).lower()
# Check for review first (highest specificity)
if any(kw in text for kw in self.review_keywords):
return 'REVIEW'
# Check computational
computational_count = sum(1 for kw in self.computational_keywords if kw in text)
if computational_count >= 2:
return 'COMPUTATIONAL'
# Check in vitro
in_vitro_count = sum(1 for kw in self.in_vitro_keywords if kw in text)
# Check in vivo
in_vivo_count = sum(1 for kw in self.in_vivo_keywords if kw in text)
# Use community origin as tiebreaker
if in_vitro_count > in_vivo_count:
return 'IN_VITRO'
elif in_vivo_count > in_vitro_count:
return 'IN_VIVO'
elif community_origin == 'ENGINEERED' or community_origin == 'SYNTHETIC':
return 'IN_VITRO'
elif community_origin == 'NATURAL':
return 'IN_VIVO'
return None # Can't determine
def process_yaml(
self,
yaml_path: Path,
auto_mode: bool = False,
interactive: bool = False
) -> Dict:
"""Process a YAML file and add evidence_source"""
with open(yaml_path, 'r') as f:
data = yaml.safe_load(f)
changes = []
community_origin = data.get('origin')
# Process taxonomy
if 'taxonomy' in data:
for taxon_idx, taxon_entry in enumerate(data['taxonomy']):
if 'evidence' not in taxon_entry:
continue
organism = taxon_entry.get('taxon_term', {}).get('preferred_term', 'Unknown')
for ev_idx, ev in enumerate(taxon_entry['evidence']):
if 'evidence_source' in ev and ev['evidence_source']:
self.stats['already_has_source'] += 1
continue
self.stats['total_evidence'] += 1
# Get snippet and reference
snippet = ev.get('snippet', '')
reference = ev.get('reference', '')
# Try to fetch abstract for better classification
abstract = None
title = None
try:
paper = self.fetcher.fetch_paper(reference, download_pdf=False)
abstract = paper.get('abstract')
title = paper.get('title')
except:
pass
# Guess evidence source
guessed_source = self.guess_evidence_source(
snippet, abstract, title, community_origin
)
if auto_mode and guessed_source:
ev['evidence_source'] = guessed_source
changes.append({
'context': 'taxonomy',
'organism': organism,
'reference': reference,
'source': guessed_source,
'confidence': 'auto'
})
self.stats['auto_added'] += 1
elif interactive:
# Show evidence and ask user
print(f"\nOrganism: {organism}")
print(f"Reference: {reference}")
print(f"Snippet: {snippet[:150]}...")
if guessed_source:
print(f"Suggested: {guessed_source}")
choice = input("Source [I=IN_VITRO, V=IN_VIVO, C=COMPUTATIONAL, R=REVIEW, S=skip]: ").upper()
source_map = {
'I': 'IN_VITRO',
'V': 'IN_VIVO',
'C': 'COMPUTATIONAL',
'R': 'REVIEW'
}
if choice in source_map:
ev['evidence_source'] = source_map[choice]
changes.append({
'context': 'taxonomy',
'organism': organism,
'reference': reference,
'source': source_map[choice],
'confidence': 'manual'
})
self.stats['manual_added'] += 1
else:
self.stats['skipped'] += 1
# Process interactions (similar logic)
if 'ecological_interactions' in data:
for int_idx, interaction in enumerate(data['ecological_interactions']):
if 'evidence' not in interaction:
continue
int_name = interaction.get('name', 'Unknown')
for ev_idx, ev in enumerate(interaction['evidence']):
if 'evidence_source' in ev and ev['evidence_source']:
self.stats['already_has_source'] += 1
continue
self.stats['total_evidence'] += 1
snippet = ev.get('snippet', '')
reference = ev.get('reference', '')
abstract = None
title = None
try:
paper = self.fetcher.fetch_paper(reference, download_pdf=False)
abstract = paper.get('abstract')
title = paper.get('title')
except:
pass
guessed_source = self.guess_evidence_source(
snippet, abstract, title, community_origin
)
if auto_mode and guessed_source:
ev['evidence_source'] = guessed_source
changes.append({
'context': 'interaction',
'organism': int_name,
'reference': reference,
'source': guessed_source,
'confidence': 'auto'
})
self.stats['auto_added'] += 1
elif interactive:
print(f"\nInteraction: {int_name}")
print(f"Reference: {reference}")
print(f"Snippet: {snippet[:150]}...")
if guessed_source:
print(f"Suggested: {guessed_source}")
choice = input("Source [I/V/C/R/S]: ").upper()
source_map = {
'I': 'IN_VITRO',
'V': 'IN_VIVO',
'C': 'COMPUTATIONAL',
'R': 'REVIEW'
}
if choice in source_map:
ev['evidence_source'] = source_map[choice]
changes.append({
'context': 'interaction',
'organism': int_name,
'reference': reference,
'source': source_map[choice],
'confidence': 'manual'
})
self.stats['manual_added'] += 1
else:
self.stats['skipped'] += 1
# Write back if changes made
if changes:
# Summarize the changes for the curation trail.
auto_count = sum(1 for c in changes if c.get('confidence') == 'auto')
manual_count = sum(1 for c in changes if c.get('confidence') == 'manual')
change_summary = (
f"Backfilled evidence_source on {len(changes)} evidence item(s) "
f"(auto={auto_count}, manual={manual_count})"
)
record_curation_event(
data,
curator="add_evidence_source",
action="BACKFILL_EVIDENCE_SOURCE",
changes=change_summary,
)
# Backup then write via closed-schema-gated writer. If validation
# fails, restore the backup so the loop can continue on the next
# community without leaving the disk in a torn state.
backup_path = yaml_path.with_suffix('.yaml.bak_source')
yaml_path.rename(backup_path)
try:
write_validated_community(data, yaml_path)
except ValidationFailedError as exc:
backup_path.rename(yaml_path)
print(
f" ✗ validation failed for {yaml_path.name}: {exc.summary()} "
"(original restored)",
file=sys.stderr,
)
return {
'file': yaml_path.name,
'changes': [],
'count': 0,
'validation_failed': True,
}
return {
'file': yaml_path.name,
'changes': changes,
'count': len(changes)
}
def main():
import argparse
parser = argparse.ArgumentParser(description="Add evidence_source to YAML files")
parser.add_argument('--file', help="Specific YAML file to process")
parser.add_argument('--auto', action='store_true', help="Auto-add using heuristics")
parser.add_argument('--interactive', action='store_true', help="Interactive mode")
parser.add_argument('--dry-run', action='store_true', help="Don't write changes")
args = parser.parse_args()
if not args.auto and not args.interactive:
print("Error: Must specify --auto or --interactive mode")
sys.exit(1)
adder = EvidenceSourceAdder()
kb_dir = Path('kb/communities')
if args.file:
yaml_files = [kb_dir / args.file]
else:
yaml_files = sorted(kb_dir.glob('*.yaml'))
print("Evidence Source Adder")
print("=" * 80)
print(f"Mode: {'AUTO' if args.auto else 'INTERACTIVE'}")
print()
results = []
for yaml_path in yaml_files:
print(f"Processing {yaml_path.name}...")
if not args.dry_run:
result = adder.process_yaml(yaml_path, auto_mode=args.auto, interactive=args.interactive)
if result['count'] > 0:
print(f" Added {result['count']} evidence_source fields")
results.append(result)
else:
print(" (dry run - no changes)")
print()
print("=" * 80)
print("Statistics:")
print(f" Total evidence items: {adder.stats['total_evidence']}")
print(f" Already had source: {adder.stats['already_has_source']}")
print(f" Auto-added: {adder.stats['auto_added']}")
print(f" Manual-added: {adder.stats['manual_added']}")
print(f" Skipped: {adder.stats['skipped']}")
print()
if results:
print(f"Modified {len(results)} files")
print("Backups saved as .yaml.bak_source")
if __name__ == '__main__':
main()