# Troubleshooting

Start by confirming that the notebook is using the intended environment:

```python
import sys
import ipykernel
import sctk

print(sys.executable)
print(ipykernel.__version__)  # expected: 6.25.2
print(sctk.__version__)
```

## The notebook imports a different `sctk`

From the project root, install the repository into the environment and
re-register the kernel:

```bash
conda activate sctk
python -m pip install -e .
python -m ipykernel install --user --name sctk --display-name "Python (sctk)"
```

Restart Jupyter and choose `Python (sctk)`. Compare `sys.executable` in the
notebook with `which python` after activating the environment.

## `ipykernel` is not version 6.25.2

The project intentionally pins `ipykernel=6.25.2` in `environment.yml`. Update
from that file instead of independently upgrading the kernel:

```bash
mamba env update -n sctk -f environment.yml --prune
mamba run -n sctk python -c "import ipykernel; print(ipykernel.__version__)"
```

If conda and pip both claim ownership of the same package, a fresh environment
is safer than repeatedly uninstalling one copy in place.

## A count-based method rejects the matrix

Pseudobulk, PyDESeq2, scVI, scDblFinder, and SoupX require non-negative,
integer-like counts. Confirm the selected layer:

```python
counts = adata.layers["counts"]
print(counts.shape, counts.dtype)
```

Do not use log-normalized, scaled, batch-corrected, or imputed values. Preserve
raw counts before normalization:

```python
adata.layers["counts"] = adata.X.copy()
```

## A metadata key is missing

Inspect available columns and use explicit project-level names:

```python
print(adata.obs.columns.tolist())
```

`sample_key` must identify independent biological samples, not cells, droplets,
lanes, or arbitrary batches. `condition_key` should identify the modeled
condition, and a patient/pair key belongs in paired designs.

## Optional dependency errors

Install only the feature set needed:

```bash
python -m pip install -e ".[enrichment,plot,de]"
python -m pip install -e ".[integration]"
python -m pip install -e ".[ccc]"
```

For the full Python/R workflow, prefer `environment.yml`. Optional modules are
lazy-loaded, so a missing LIANA or scVI installation should not prevent core
preprocessing from importing.

## R or Bioconductor cannot be found

Check the R seen by `rpy2` and then the required package:

```bash
python -c "import rpy2.robjects as ro; print(ro.r('R.home()')[0]); print(ro.r('R.version.string')[0])"
python -c "from sctk.r import require_r_package; require_r_package('scDblFinder')"
```

Python, `rpy2`, R, and Bioconductor should come from the same conda environment.
If using an external R installation, run `Rscript requirements-r.R` with that
same R executable.

## SoupX barcode or gene errors

The filtered cells must be a subset of the raw-droplet barcodes. Gene identifiers
must also match between objects:

```python
assert adata.obs_names.isin(adata_raw.obs_names).all()
assert adata.var_names.isin(adata_raw.var_names).all()
```

The raw object normally contains many additional empty droplets. Do not pass a
second copy of the already filtered object as the raw droplet matrix.

## Integration fails or gives a poor embedding

- Harmony requires an existing PCA representation and a valid batch column.
- scVI requires raw counts and enough observations in every modeled category.
- Recompute neighbors and UMAP from the corrected representation.
- Compare batch mixing and biological-label separation; no single metric proves
  that an integration is appropriate.

## Cache or plotting permission errors

On restricted compute nodes, redirect caches to writable locations:

```bash
export SCTK_CACHE_DIR=/tmp/sctk-cache
export NUMBA_CACHE_DIR=/tmp/sctk-numba
export MPLCONFIGDIR=/tmp/sctk-matplotlib
export XDG_CACHE_HOME=/tmp/sctk-xdg
```

For long-running jobs, choose a persistent project or scratch directory instead
of `/tmp`.

## Before reporting a bug

Record the failing function call, full traceback, `adata.shape`, relevant
`.obs` keys, selected layer, sparse/dense type, and package versions:

```bash
conda list
python -m pip check
```

Avoid sharing raw patient identifiers or sensitive expression matrices.
