scverse / scverse/scanpy

Allow scanpy.tl.pca to accept a layer argument

Open
#2,476 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.6k
Forks
779
Avg merge
1d 4h
Merged PRs (30d)
27

Description

  • Additional function parameters / changed functionality / changed defaults?
  • New analysis tool: A simple analysis tool you have been using and are missing in sc.tools?
  • New plotting function: A kind of plot you would like to seein sc.pl?
  • External tools: Do you know an existing package that should go into sc.external.*?
  • Other?

...

I'm adapting ScanPy for my compositional data analysis (CoDA) methodologies in the same vein as https://github.com/scverse/scanpy/issues/2475 . In this, I would like to perform Aitchison PCA but I'd also like to keep my Anndata object in counts form instead of creating another one.

For example:

import numpy as np
import pandas as pd
from typing import Union

def clr(x:Union[np.ndarray, pd.Series], multiplicative_replacement:Union[None,str,float,int]="auto") -> Union[np.ndarray, pd.Series]:
    """
    http://scikit-bio.org/docs/latest/generated/skbio.stats.composition.clr.html#skbio.stats.composition.clr
    """
    assert np.all(x >= 0)
    if multiplicative_replacement == "auto":
        if np.any(x == 0):
            multiplicative_replacement = 1/(len(x)**2)
    if multiplicative_replacement is None:
        multiplicative_replacement = 0
    x = x.copy() + multiplicative_replacement
    x = x/x.sum()
    log_x = np.log(x)
    geometric_mean = log_x.mean()
    return log_x - geometric_mean

# Add CLR to layers
adata.layers["clr"] = adata.to_df().apply(clr, axis=1) # Not the fastest way but just to show example

sc.tl.pca(adata, svd_solver='arpack', layer="clr") # -> Return addata object where PCA is performed on CLR

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the scanpy.tl.pca entry point and trace how the input matrix is selected from AnnData. Add support for choosing a named layer so PCA runs on that data while existing behavior remains unchanged; verify the requested layer-based example and relevant PCA behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
bioinformatics, machine-learning
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.