Preparation of SpatialCOC

We recommend using a dedicated conda environment to run SpatialCOC. Please download and install Anaconda3 from the official website. > https://www.anaconda.com/download

1. Install SpatialCOC

1.1 Create a conda environment

Please execute the following commands in the Anaconda Prompt:

[ ]:
# Create a new environment named SpatialCOC with Python 3.11.5
conda create -n SpatialCOC python=3.11.5

# Activate the environment
conda activate SpatialCOC

1.2 Install the SpatialCOC package

Download the SpatialCOC package from the GitHub repository. > https://github.com/xjtu-omics/SpatialCOC

After downloading, navigate to the package directory and run the following command to install the package:

[ ]:
pip install .

Once the installation is complete, you can verify that SpatialCOC is imported successfully:

[12]:
try:
    import SpatialCOC
    print("✓ SpatialCOC imported successfully!")
    print(f"  SpatialCOC version: {SpatialCOC.__version__}")

except ImportError as e:
    print(f"✗ Import failed: {e}")
    print("  Please install SpatialCOC: pip install SpatialCOC")
except Exception as e:
    print(f"✗ Other error: {e}")
✓ SpatialCOC imported successfully!
  SpatialCOC version: 1.0.0

2. Configure the R Environment

SpatialCOC uses the mclust algorithm, which requires both the rpy2 Python package and the mclust R package.

2.1 Install the R software

Download and install R version 4.3.1 to a specific directory. > https://cran.r-project.org/bin/windows/base/old/4.3.1/

2.2 Install the rpy2 package

Please run the code below to install the rpy2 package:

[ ]:
conda install -c conda-forge rpy2

2.3 Set the correct paths

To enable rpy2 to work properly, configure the following environment variables. Replace the placeholders with your actual paths:

[16]:
import os
os.environ['RPY2_CFFI_MODE'] = 'ABI'

os.environ['R_HOME'] = '***'    # Path to the R installation, e.g., 'E:/R-4.3.1'
os.environ['R_USER'] = '***'    # Path to rpy2 within the virtual environment, e.g., 'E:/anaconda3/envs/SpatialCOC/Lib/site-packages/rpy2'

Please run the following code to confirm that rpy2 is correctly installed and can communicate with R:

[17]:
try:
    import rpy2.robjects as robjects
    import rpy2.robjects.numpy2ri
    print("✓ rpy2 imported successfully!")
    print(f"  rpy2 version: {robjects.__version__}")

    # Get R version
    r_version = robjects.r('R.version.string')[0]
    print(f"  R version: {r_version}")

except ImportError as e:
    print(f"✗ Import failed: {e}")
    print("  Please install rpy2: pip install rpy2")
except Exception as e:
    print(f"✗ Other error: {e}")
✓ rpy2 imported successfully!
  rpy2 version: 3.6.3
  R version: R version 4.3.1 (2023-06-16 ucrt)