File size: 4,031 Bytes
d66b023
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Setup script for CAI package installation in ColiFormer.
This script handles the installation of the CAI package with proper build flags
to avoid wheel naming issues that occur with standard pip install.
"""

import subprocess
import sys
import os
import importlib.util

def check_cai_installed():
    """Check if CAI package is already installed and working."""
    try:
        spec = importlib.util.find_spec("CAI")
        if spec is None:
            return False

        # Try to import the specific functions we need
        import CAI
        from CAI import CAI as cai_func, relative_adaptiveness
        print("βœ… CAI package is already installed and working")
        return True
    except ImportError as e:
        print(f"❌ CAI package not found or not working: {e}")
        return False

def install_cai():
    """Install CAI package with proper build configuration."""
    print("πŸ”§ Installing CAI package...")

    cai_repo = "git+https://github.com/Benjamin-Lee/CodonAdaptationIndex.git@b6e017a92c58829f6a5aec8c26a21262bc2a6610"

    try:
        # First ensure we have build tools
        print("Installing build dependencies...")
        subprocess.run([
            sys.executable, "-m", "pip", "install", "--upgrade",
            "setuptools>=65.0", "wheel>=0.37.0", "pip>=21.0"
        ], check=True, capture_output=True)

        # Try installing with --no-use-pep517 flag first (preferred method)
        print("Attempting CAI installation with legacy build system...")
        try:
            result = subprocess.run([
                sys.executable, "-m", "pip", "install",
                "--no-use-pep517", "--no-cache-dir", cai_repo
            ], check=True, capture_output=True, text=True)
            print("βœ… CAI installed successfully with legacy build")
            return True
        except subprocess.CalledProcessError:
            print("⚠️ Legacy build failed, trying standard installation...")

        # Fallback to standard installation
        result = subprocess.run([
            sys.executable, "-m", "pip", "install", "--no-cache-dir", cai_repo
        ], check=True, capture_output=True, text=True)

        print("βœ… CAI installed successfully with standard build")
        return True

    except subprocess.CalledProcessError as e:
        print(f"❌ CAI installation failed: {e}")
        if hasattr(e, 'stderr') and e.stderr:
            print(f"Error output: {e.stderr}")
        return False
    except Exception as e:
        print(f"❌ Unexpected error during CAI installation: {e}")
        return False

def verify_cai_installation():
    """Verify that CAI package is working correctly."""
    try:
        import CAI
        from CAI import CAI as cai_func, relative_adaptiveness

        # Test basic functionality
        test_sequences = ["ATGAAATAA", "ATGGGCTAA"]
        weights = relative_adaptiveness(sequences=test_sequences)
        cai_score = cai_func("ATGAAATAA", weights=weights)

        print(f"βœ… CAI verification successful (test score: {cai_score:.3f})")
        return True

    except Exception as e:
        print(f"❌ CAI verification failed: {e}")
        return False

def main():
    """Main setup function."""
    print("ColiFormer CAI Setup")
    print("=" * 50)

    # Check if already installed
    if check_cai_installed():
        if verify_cai_installation():
            print("πŸŽ‰ CAI is ready to use!")
            return True
        else:
            print("⚠️ CAI is installed but not working properly, reinstalling...")

    # Install CAI
    if install_cai():
        # Verify installation
        if verify_cai_installation():
            print("πŸŽ‰ CAI setup completed successfully!")
            return True
        else:
            print("πŸ’₯ CAI installation verification failed!")
            return False
    else:
        print("πŸ’₯ CAI installation failed!")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)