File size: 7,731 Bytes
30b7e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Comprehensive validation of example variants:
1. Literature accuracy
2. Example diversity and quality
3. Track type coverage in results
"""

import sys

sys.path.insert(0, ".")

from app import predict_single_variant
import pandas as pd
from collections import defaultdict
import json

# Known variants from literature
VARIANT_REFERENCES = {
    ("chr17", 7675088, "C", "T"): {
        "name": "TP53 R175H",
        "disease": "Cancer (tumor suppressor)",
        "frequency": "~6% of cancers",
        "literature": "Highly common hotspot, eliminates DNA binding domain",
        "impact_expected": "HIGH",
        "regions_expected": ["coding", "regulatory"],
    },
    ("chr7", 117559593, "ATCT", "A"): {
        "name": "CFTR F508del",
        "disease": "Cystic Fibrosis",
        "frequency": "~70% of CF patients",
        "literature": "Most common CF mutation, causes protein misfolding",
        "impact_expected": "HIGH",
        "regions_expected": ["coding", "structural"],
    },
    ("chr13", 32332771, "AGAGA", "AGA"): {
        "name": "BRCA2 frameshift",
        "disease": "Hereditary breast/ovarian cancer",
        "frequency": "Rare, pathogenic",
        "literature": "BRCA2 frameshift deletion (c.5946delT), causes loss of function",
        "impact_expected": "HIGH",
        "regions_expected": ["coding", "frameshift"],
    },
    ("chr11", 5227002, "T", "A"): {
        "name": "HBB E6V",
        "disease": "Sickle cell disease",
        "frequency": "Common in African populations",
        "literature": "Missense mutation (rs334) causing hemoglobin S polymerization",
        "impact_expected": "HIGH",
        "regions_expected": ["coding", "regulatory"],
    },
    ("chr17", 43092418, "T", "C"): {
        "name": "BRCA1 synonymous",
        "disease": "Benign control variant",
        "frequency": "Common",
        "literature": "Synonymous variant (c.3113A>G, rs16941), expected benign",
        "impact_expected": "LOW",
        "regions_expected": ["coding"],
    },
}


def categorize_track(feature_name, feature_type):
    """Categorize track type (BED or BigWig, and specific kind)"""
    if feature_type == "BED":
        if any(
            x in feature_name.lower() for x in ["splice", "exon", "intron", "codon"]
        ):
            return "bed_splicing"
        elif any(x in feature_name.lower() for x in ["cds", "coding"]):
            return "bed_coding"
        elif any(x in feature_name.lower() for x in ["promoter", "enhancer"]):
            return "bed_regulatory"
        elif any(x in feature_name.lower() for x in ["utr", "5utr", "3utr"]):
            return "bed_utr"
        else:
            return "bed_other"
    elif feature_type == "BigWig":
        if "Histone" in feature_name:
            return "bw_histone"
        elif "RNA" in feature_name or "CAGE" in feature_name:
            return "bw_expression"
        elif "DNase" in feature_name or "ATAC" in feature_name:
            return "bw_accessibility"
        elif "ChIP-seq" in feature_name:
            return "bw_chipseq"
        else:
            return "bw_other"
    return "unknown"


def validate_example(chrom, pos, ref, alt):
    """Validate a single example"""
    key = (chrom, pos, ref, alt)
    ref_data = VARIANT_REFERENCES.get(key)

    if not ref_data:
        return None

    print("\n" + "=" * 80)
    print(f"VARIANT: {ref_data['name']} ({chrom}:{pos} {ref}>{alt})")
    print("=" * 80)

    # Literature context
    print("\n📚 LITERATURE CONTEXT:")
    print(f"  Disease: {ref_data['disease']}")
    print(f"  Frequency: {ref_data['frequency']}")
    print(f"  Summary: {ref_data['literature']}")
    print(f"  Expected Impact: {ref_data['impact_expected']}")

    # Run prediction
    print("\n🔬 Running prediction...")
    result = predict_single_variant(chrom, pos, ref, alt)
    (
        summary_md,
        interpretation_md,
        top_table_df,
        fp_fig,
        rt_fig,
        csv_path,
        bed_df,
        mlm_md,
        ranked,
    ) = result

    # Extract impact score from summary
    impact_from_summary = None
    if "BED Impact Score" in summary_md:
        import re

        m = re.search(r"BED Impact Score\s*\|\s*([\d.]+)", summary_md)
        if m:
            impact_from_summary = float(m.group(1))

    print("\n📊 RESULTS SUMMARY:")
    gene_region = "N/A"
    if "**Gene:**" in summary_md:
        gene_part = summary_md.split("**Gene:**")[1]
        gene_region = gene_part.split("\n")[0].strip()
    print(f"  Gene/Region: {gene_region}")
    print(f"  Top tracks: {len(top_table_df)} features")
    print(f"  Total ranked: {len(ranked)} tracks")
    print(f"  Interpretation panel present: {bool(interpretation_md)}")
    print(
        f"  Impact Score (BED): {impact_from_summary if impact_from_summary else 'N/A'}"
    )

    # Track diversity analysis
    print("\n🎯 TRACK DIVERSITY ANALYSIS:")

    track_categories = defaultdict(int)
    sources = set()

    # Analyze ranked tracks
    for r in ranked:
        feat = r["display_name"]
        ftype = r["track_type"]
        category = categorize_track(feat, ftype)
        track_categories[category] += 1

        if "|" in feat:  # BigWig with tissue info
            tissue = feat.split("|")[0].strip()
            sources.add(tissue)

    print("\n  Track Categories:")
    for cat, count in sorted(track_categories.items(), key=lambda x: -x[1]):
        cat_display = cat.replace("bed_", "BED: ").replace("bw_", "BigWig: ")
        print(f"    - {cat_display}: {count}")

    print(f"\n  Tissue/Cell Sources Found: {len(sources)}")
    if len(sources) > 0:
        for source in sorted(list(sources))[:5]:  # Show first 5
            print(f"    - {source}")
        if len(sources) > 5:
            print(f"    ... and {len(sources) - 5} more")

    # Quality assessment
    print("\n✓ VARIANT QUALITY ASSESSMENT:")

    # Has diverse tracks
    diversity_score = len(track_categories)
    if diversity_score >= 3:
        print(f"  ✓ Good track diversity ({diversity_score} categories)")
    else:
        print(f"  ⚠️  Limited track diversity ({diversity_score} categories)")

    # Has gains and losses
    gains = [r for r in ranked if r["delta"] > 0]
    losses = [r for r in ranked if r["delta"] < 0]
    if gains and losses:
        print(f"  ✓ Shows both gains ({len(gains)}) and losses ({len(losses)})")
    elif gains:
        print("  ⚠️  Only shows gains, no losses")
    elif losses:
        print("  ⚠️  Only shows losses, no gains")
    else:
        print("  ✗ No gains or losses - may not be suitable example")

    # Expected impact matches actual
    has_high_impacts = any(abs(r["delta"]) >= 0.1 for r in ranked)

    if ref_data["impact_expected"] == "HIGH" and has_high_impacts:
        print("  ✓ HIGH impact expected and observed")
    elif ref_data["impact_expected"] == "MODERATE" and not has_high_impacts:
        print("  ✓ MODERATE impact expected and observed (no extreme deltas)")
    else:
        if ref_data["impact_expected"] == "HIGH":
            print("  ⚠️  HIGH impact expected but weak observed")

    # Relevant to disease
    print(f"  ✓ Disease-relevant example ({ref_data['disease']})")

    return True


# Validate all examples
examples = [
    ("chr17", 7675088, "C", "T"),
    ("chr7", 117559593, "ATCT", "A"),
    ("chr13", 32332771, "AGAGA", "AGA"),
    ("chr11", 5227002, "T", "A"),
    ("chr17", 43092418, "T", "C"),
]

print("\n" + "=" * 80)
print("COMPREHENSIVE EXAMPLE VALIDATION")
print("=" * 80)

for chrom, pos, ref, alt in examples:
    validate_example(chrom, pos, ref, alt)

print("\n" + "=" * 80)
print("VALIDATION COMPLETE")
print("=" * 80)