Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

610 Add script to apply SEACells to AnnData objects #658

Merged
merged 20 commits into from
Jul 25, 2024

Conversation

jashapiro
Copy link
Member

Closes #610

This PR adds a python script to apply the SEACells algorithm to AnnData objects.

The main steps and default parameters are derived from https://github.com/dpeerlab/SEACells/blob/main/notebooks/SEACell_computation.ipynb and associated SEACells source code.

One thing to note was that there are few subtleties to the expected formats and associated metadata for things like PCA and UMAP, so it was necessary to recalculate those values, so I wrote a small function to do that. I initally thought I might be able to get away with just renaming the matrices, but it seemed like some of the metadata stored when scanpy calculates the dimension reduction matrices was required downstream, so recalculation seemed the way to go. In my testing, the PCA results were nearly identical, but UMAP were subtly different, as might be expected.

I am currently writing out both the updated AnnData object and the SEACells model object (as a pickle) in order to have access to both for the later analysis.
The model object seems most useful for checking convergence, so it may not really be needed in most cases, but it is relatively small, so easy to keep.

The wrapper script currently runs only on one sample, and exists mostly just for tracking the commands used for testing.

@jashapiro jashapiro requested a review from allyhawkins as a code owner July 24, 2024 19:36
@jashapiro jashapiro requested review from sjspielman and removed request for allyhawkins July 24, 2024 19:36
Copy link
Member

@sjspielman sjspielman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me overall! A few of my comments focus on some choices that the script makes, so more docs may be helpful there.
In addition, I do think we want to explain somewhere in the docs the values that you had to recalculate to run SEAcells. The main purpose of that is to be clear how the input data for (at least 1 script in) this module slightly differs from the processed objects.

I'm also think it makes sense to activate the module GHA before developing further, but that should probably be in a separate PR.


## Input files

Initial analysis is expected use processed `SingleCellExperiment` objects from the ScPCA Portal, in AnnData format.

For initial testing, files were downloaded with:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming this code is right - since you wrote testing, I wondered if you meant to include the test flag? If not, then not!

analyses/metacells/run-metacells.sh Outdated Show resolved Hide resolved
analyses/metacells/scripts/run-seacells.py Outdated Show resolved Hide resolved
"""
Seacells Analysis Script
Joshua Shapiro
2024-07-18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it will get out of date real fast, for example, now.

analyses/metacells/scripts/run-seacells.py Outdated Show resolved Hide resolved
----------
adata : anndata.AnnData
An annData object containing the data to run the SEACells algorithm on.
Should contain an X_pca field with the PCA coordinates of the cells.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to formally check that X_pca if present. It doesn't seem necessary at this stage, but it might be helpful later, so can't hurt now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to modify this to actually call the convert function from within this function.

adata : anndata.AnnData
An annData object containing the data to run the SEACells algorithm on.
Should contain an X_pca field with the PCA coordinates of the cells.
cell_ratio : float
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a bit here about where the 75 default comes from?

# initialize and fit model
model.construct_kernel_matrix()
model.initialize_archetypes()
model.fit(min_iter=10, max_iter=50)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the function - any reason to not keep their max_iter=100? Is it super slow?
https://github.com/dpeerlab/SEACells/blob/3462c624ffae0df6d3930490f345f00196c3503e/SEACells/cpu.py#L653-L659

Comment on lines +119 to +121
parser.add_argument(
"--logfile", type=pathlib.Path, help="File path for log outputs"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd mention that this also controls the verbosity of SEAcells

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary for the argument definition. The point is that if you want logs I will put all the info there. If you don't want logs, the verbosity is just annoying.


## Output files

TBD
Currently, the script outputs three files per library: an updated AnnData object, a pickled SEACells model object, and a log file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it looks like it only outputs a logfile if one specified

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which it is in the run script!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, fair! Since I was mostly reviewing python, "the script" meant the python script to me, but that's not what it means here! 👍

Co-authored-by: Stephanie Spielman <[email protected]>
Copy link
Member Author

@jashapiro jashapiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I will be implementing many of your suggestions


## Output files

TBD
Currently, the script outputs three files per library: an updated AnnData object, a pickled SEACells model object, and a log file.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which it is in the run script!

analyses/metacells/run-metacells.sh Outdated Show resolved Hide resolved
)

# recompute principal components and umap (adds additional metadata)
sc.tl.pca(adata, n_comps=50, mask_var="highly_variable")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intent here is to keep it the same as the values already present, but I will make that explicit and check.

----------
adata : anndata.AnnData
An annData object containing the data to run the SEACells algorithm on.
Should contain an X_pca field with the PCA coordinates of the cells.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to modify this to actually call the convert function from within this function.

Comment on lines +119 to +121
parser.add_argument(
"--logfile", type=pathlib.Path, help="File path for log outputs"
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary for the argument definition. The point is that if you want logs I will put all the info there. If you don't want logs, the verbosity is just annoying.

call `convert` from within the main function
other responses to review
This reverts commit 06e495f.
@jashapiro
Copy link
Member Author

I think this is pretty much ready for another review. I briefly tried activating the action, but I ran into trouble which I expect is related to small samples sizes. I will investigate those as part of a separate PR when I activate the action for real.

@jashapiro jashapiro requested a review from sjspielman July 25, 2024 19:49
Copy link
Member

@sjspielman sjspielman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Left a few small comments but nothing I need to see again. Makes sense to deal with GHA separately, as well.

analyses/metacells/scripts/run-seacells.py Outdated Show resolved Hide resolved
n_comps = min(50, adata.n_vars, adata.n_obs)
sc.tl.pca(adata, n_comps=n_comps, mask_var="highly_variable")

# recompute principal components and umap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left over comment?

@jashapiro jashapiro merged commit 3ee159b into AlexsLemonade:main Jul 25, 2024
2 checks passed
@jashapiro jashapiro deleted the jashapiro/610-SEACells-script branch July 25, 2024 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add script to apply SEACells to AnnData objects
2 participants