Datasets:

Languages:
English
License:
File size: 5,758 Bytes
ef78b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Consolidated script to diagnose and fix h5ad files for transcriptformer.

This script performs a series of checks to validate an AnnData object and
automatically applies fixes for common issues, preparing the data for
inference with transcriptformer.

Usage:
    python preprocess_adata.py <input_h5ad_file> <output_h5ad_file>
"""

import sys
import os
import numpy as np
import anndata as ad
import scanpy as sc
from pathlib import Path

def preprocess_adata(input_path, output_path):
    """
    Diagnose and fix an h5ad file for transcriptformer compatibility.
    """
    print(f"🚀 Starting preprocessing for: {input_path}")
    print("=" * 70)

    # 1. Load Data
    print("📖 1. Loading AnnData object...")
    if not os.path.exists(input_path):
        print(f"❌ ERROR: Input file not found: {input_path}")
        return False
    
    try:
        adata = ad.read_h5ad(input_path)
        print(f"✅ Loaded: {adata.shape[0]} cells × {adata.shape[1]} genes")
    except Exception as e:
        print(f"❌ ERROR: Could not load AnnData file. Reason: {e}")
        return False

    original_shape = adata.shape

    # 2. Run Diagnostics
    print("\n🔬 2. Running Diagnostics...")
    issues_found = []

    # Check for NaN/Inf values
    has_nan = np.isnan(adata.X.data).any() if hasattr(adata.X, 'data') else np.isnan(adata.X).any()
    has_inf = np.isinf(adata.X.data).any() if hasattr(adata.X, 'data') else np.isinf(adata.X).any()
    if has_nan: issues_found.append("NaN values found in data matrix.")
    if has_inf: issues_found.append("Infinite values found in data matrix.")
    print(f"   - NaN/Inf values: {'❌ Found' if has_nan or has_inf else '✅ None'}")

    # Check for unique gene indices
    if adata.var.index.nunique() < len(adata.var.index):
        issues_found.append("Duplicate gene indices (var_names) found.")
        print("   - Duplicate gene indices: ❌ Found")
    else:
        print("   - Duplicate gene indices: ✅ Unique")

    # Check for ensembl_id column
    if 'ensembl_id' not in adata.var.columns:
        issues_found.append("'ensembl_id' column missing in var.")
        print("   - 'ensembl_id' column: ❌ Missing")
    else:
        print("   - 'ensembl_id' column: ✅ Present")

    # Check for zero-expression genes
    genes_before_filter = adata.n_vars
    sc.pp.filter_genes(adata, min_cells=1)
    if adata.n_vars < genes_before_filter:
        num_removed = genes_before_filter - adata.n_vars
        issues_found.append(f"{num_removed} genes with zero expression found.")
        print(f"   - Zero-expression genes: ❌ Found ({num_removed} genes)")
    else:
        print("   - Zero-expression genes: ✅ None")
    
    # Restore original object for fixing step
    adata = ad.read_h5ad(input_path)

    # 3. Apply Fixes
    print("\n🔧 3. Applying Fixes...")
    fixes_applied = []

    # Fix: Ensure var_names are unique
    if adata.var.index.nunique() < len(adata.var.index):
        adata.var_names_make_unique()
        fixes_applied.append("Made var_names unique using .var_names_make_unique()")
        print("   - ✅ Made gene indices (var_names) unique.")
    else:
        print("   - ✅ Gene indices are already unique.")

    # Fix: Add ensembl_id column if it's missing
    if 'ensembl_id' not in adata.var.columns:
        print("   - Adding 'ensembl_id' column from var.index.")
        adata.var['ensembl_id'] = adata.var.index
        fixes_applied.append("Added 'ensembl_id' column from var.index.")
    else:
        print("   - ✅ 'ensembl_id' column already exists.")

    # Fix: Filter out genes with zero expression
    genes_before_filter = adata.n_vars
    sc.pp.filter_genes(adata, min_cells=1)
    if adata.n_vars < genes_before_filter:
        num_removed = genes_before_filter - adata.n_vars
        fixes_applied.append(f"Removed {num_removed} genes with no expression.")
        print(f"   - ✅ Removed {num_removed} zero-expression genes.")
    else:
        print("   - ✅ No zero-expression genes to remove.")

    # 4. Save Processed File
    print("\n💾 4. Saving Processed File...")
    try:
        adata.write(output_path)
        print(f"   - ✅ Successfully saved to: {output_path}")
    except Exception as e:
        print(f"❌ ERROR: Could not save file. Reason: {e}")
        return False

    # 5. Final Summary
    print("\n📋 5. Summary")
    print("-" * 70)
    print(f"   - Original shape: {original_shape[0]} cells × {original_shape[1]} genes")
    print(f"   - Final shape:    {adata.shape[0]} cells × {adata.shape[1]} genes")
    print("\n   - Issues Found:")
    if issues_found:
        for issue in issues_found:
            print(f"     - {issue}")
    else:
        print("     - None")
    
    print("\n   - Fixes Applied:")
    if fixes_applied:
        for fix in fixes_applied:
            print(f"     - {fix}")
    else:
        print("     - None")
    
    print("\n🎉 Preprocessing complete!")
    return True

def main():
    if len(sys.argv) != 3:
        print("Usage: python preprocess_adata.py <input_h5ad_file> <output_h5ad_file>")
        sys.exit(1)
    
    input_path = sys.argv[1]
    output_path = sys.argv[2]
    
    if os.path.abspath(input_path) == os.path.abspath(output_path):
        print("❌ ERROR: Input and output paths cannot be the same.")
        sys.exit(1)

    if os.path.exists(output_path):
        response = input(f"⚠️  Output file already exists: {output_path}\nOverwrite? (y/N): ")
        if response.lower() != 'y':
            print("Operation cancelled.")
            sys.exit(1)

    success = preprocess_adata(input_path, output_path)
    
    if not success:
        sys.exit(1)

if __name__ == "__main__":
    main()