-
Notifications
You must be signed in to change notification settings - Fork 6
Feature seurat upload #1273
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
Draft
dlesper
wants to merge
23
commits into
devel
Choose a base branch
from
feature_seurat_upload
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+342
−48
Draft
Feature seurat upload #1273
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
70f122b
Create SeuratUploader.py
dlesper db8bb98
Update process_uploaded_expression_dataset.cgi
dlesper eaa4612
Update BioCManager to 3.22
dlesper 663753c
add packages necessary for SeuratUploader
dlesper 24e8794
Update Dockerfile.r
dlesper 93adac2
Enabled Seurat/RDS
dlesper cc67206
SeuratUploader: Change gene_names to gene_symbol
dlesper b398e82
Add packages to apt-get for Seurat
dlesper 0d9456c
Add RDATA to list of dataset formats
dlesper 04411ad
Change button text from 'Selected' to 'Choose'
dlesper ffed734
Package installation finalization
dlesper f18e569
Update requirements and setup documentation for additional packages
adkinsrs 1b841b2
Fix formatting in Python setup instructions for package installation
adkinsrs 2b61749
Merge branch 'devel' into feature_seurat_upload
adkinsrs 2f9a249
Fixing squashed commits already in devel. Adjusting permissions of l…
adkinsrs 9b6b51f
Rename SeuratUploader.py to seuratuploader.py
adkinsrs a1c923e
Enhance error handling and parameter retrieval in dataset processing …
adkinsrs 5568761
Fix file extension check for RDS format in store_expression_dataset.cgi
adkinsrs ee9ef8c
Update Docker output descriptions to include "latest" tag for images
adkinsrs df93f5d
Clarify Docker image tag description in setup documentation
adkinsrs 3dee550
Merge branch 'devel' into feature_seurat_upload
adkinsrs e450944
Fix font color assignment in update_stacked_violin_annotations to def…
adkinsrs 4d59376
Refactor error handling in SeuratUploader and update progress trackin…
adkinsrs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
|
adkinsrs marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import argparse | ||
| import os | ||
| import sys | ||
|
|
||
| import mygene | ||
| import pandas as pd | ||
| import rpy2.rinterface_lib.callbacks as r_cbs | ||
| import rpy2.robjects as ro | ||
| import rpy2.robjects.packages as rpackages | ||
| import scanpy | ||
| from rpy2.robjects.packages import importr | ||
|
|
||
|
|
||
| def silent_handler(s:str) -> None: | ||
| # way to bypass the R stderr output | ||
| pass | ||
|
|
||
| def argument_parser(): | ||
| parser = argparse.ArgumentParser(usage="%(prog)s -r [RDS Object] -s [Share ID]",add_help=True) | ||
| parser.add_argument('-r', '--rds', required=True, type=str) | ||
| parser.add_argument('-s', '--share-id', required=True, type=str) | ||
| args = vars(parser.parse_args()) | ||
| return args | ||
|
|
||
| def r_package_installer() -> None: | ||
| utils = rpackages.importr('utils') | ||
| # Install BiocManager if not installed | ||
| if not rpackages.isinstalled('BiocManager'): | ||
| utils.install_packages('BiocManager') | ||
| # Import BiocManager | ||
| BiocManager = importr('BiocManager') | ||
| # Install Seurat, anndataR and rhdf5 | ||
| if not rpackages.isinstalled('reticulate'): | ||
| utils.install_packages('reticulate') | ||
| if not rpackages.isinstalled('Seurat'): | ||
| utils.install_packages('Seurat') | ||
| if not rpackages.isinstalled('anndataR'): | ||
| BiocManager.install('anndataR') | ||
| if not rpackages.isinstalled('rhdf5'): | ||
| BiocManager.install('rhdf5') | ||
|
|
||
|
|
||
| def r_package_importer(package_name:str): | ||
| """ | ||
| Import installed package, if not installed return message | ||
| Input: | ||
| package_name: R package name to import | ||
| Output: | ||
| The R package that was imported or if there's an error the message will be returned | ||
| """ | ||
| importErrorMessage = "" | ||
| try: | ||
| pkg = importr(package_name) | ||
| return pkg | ||
| except Exception: | ||
| importErrorMessage += f"{package_name} not installed or can not be imported" | ||
| sys.exit(importErrorMessage) | ||
|
|
||
|
|
||
|
|
||
| def seurat_to_anndata(file_path: str, share_name: str, output_dir: str = "."): | ||
| """ | ||
| file_path: path to rds or rdata file | ||
| share_name: final h5ad string name to be expected (without h5ad) | ||
| output_dir: directory to write the temporary h5ad file into | ||
|
|
||
| return: | ||
| absolute path to tmp h5ad, or False on failure | ||
| """ | ||
| # Suppress R console output and ensure required packages are loaded, | ||
| # since this function may be called as a module in cgi script (not via main()). | ||
| r_cbs.consolewrite_print = silent_handler | ||
| r_cbs.consolewrite_warnerror = silent_handler | ||
| # Import required R packages | ||
| base = rpackages.importr('base') | ||
| r_package_importer('Seurat') | ||
| r_package_importer('rhdf5') | ||
| r_package_importer('anndataR') | ||
| # Use R's readRDS to load the object. | ||
| # The result is an R object within the Python environment. | ||
| r_seurat_obj = base.readRDS(file_path) | ||
| ro.globalenv['seurat_obj'] = r_seurat_obj | ||
| # Using anndataR write out a converted h5ad | ||
| ro.r('adata <- as_AnnData(seurat_obj)') | ||
| output_path = os.path.join(output_dir, f'tmp_{share_name}.h5ad') | ||
| try: | ||
| ro.r(f'write_h5ad(adata, "{output_path}")') | ||
| return output_path | ||
| # In cases where the write fails we will assume the h5ad already exists | ||
| except Exception: | ||
| print(f"h5ad name already exists {output_path}") | ||
| raise | ||
|
|
||
| def openh5ad(h5ad_name): | ||
| """Just open the supplied h5ad file""" | ||
| adata = scanpy.read_h5ad(h5ad_name) | ||
| return adata | ||
|
|
||
| def genes_to_ensembl(adata, taxid=None): | ||
| # We are calling an external API for genes to ensembl mapping | ||
| # Potentially problematic down the road if this shuts down | ||
| if taxid is None: | ||
| return None | ||
| genes = adata.var.index.tolist() | ||
| try: | ||
| # TODO: Perhaps add a retry mechanism in case the API returns 500 | ||
| mg = mygene.MyGeneInfo() | ||
| mg_genes = mg.querymany(genes, scopes="symbol", fields="ensembl.gene", species=f"{taxid}") | ||
| except Exception as e: | ||
| print(f"Error occurred while querying MyGene: {e}", file=sys.stderr) | ||
| raise | ||
| ensembl_mapping_dict = {} | ||
| for mg_gene in mg_genes: | ||
| gene_name = mg_gene['query'] | ||
| if 'ensembl' in mg_gene.keys(): | ||
| if isinstance(mg_gene['ensembl'],list): | ||
| # Currently taking first value, not sure of a better way to handle one gene having multiple ensembl IDs | ||
| ensembl_mapping_dict[gene_name] = mg_gene['ensembl'][0]['gene'] | ||
| else: | ||
| ensembl_mapping_dict[gene_name] = mg_gene['ensembl']['gene'] | ||
| count = 0 | ||
| # We still need an ensembl id for the genes that do not actually have them. | ||
| # So here we create a FAKE# for each one so that it can be searchable in gEAR | ||
| for gene in genes: | ||
| if gene not in ensembl_mapping_dict.keys(): | ||
| ensembl_mapping_dict[gene] = f"Fake{count}" | ||
| count += 1 | ||
| # Overwrite the current adata.var | ||
| adata.var = pd.DataFrame( | ||
| index=list(ensembl_mapping_dict.values()), data={"gene_symbol": list(ensembl_mapping_dict.keys())} | ||
| ) | ||
| return adata | ||
|
|
||
|
|
||
| def reduction_to_metadata(adata): | ||
| # Discussion with Carlo and Brian resulted in us determining we would like to | ||
| # take the first 2 values of each reduction | ||
| # PCA in the future, and potentially other reductions may need more | ||
| for reduction in adata.obsm: | ||
| if adata.obsm[reduction].shape[1] > 1: | ||
| for i in range(2): | ||
| adata.obs[f'{reduction}_{i+1}'] = adata.obsm[reduction][:,i] | ||
| return adata | ||
|
|
||
|
|
||
| def layer_to_X(adata, layer_name): | ||
| # Possibility for Seurat -> Anndata conversion doesn not create the X matrix. | ||
| # Use adata.layers['data'] as X | ||
| adata.X = adata.layers[layer_name] | ||
| return adata | ||
|
|
||
| def main(): | ||
| arguments = argument_parser() | ||
| # Args | ||
| rds_path = arguments['rds'] | ||
| share_name = arguments['share_id'] | ||
| r_package_installer() | ||
| # Take the RDS and output the most basic h5ad | ||
| h5ad_name = seurat_to_anndata(rds_path,share_name) | ||
| # Below are some changes and checks to the h5ad to correctly format for gEAR | ||
| if h5ad_name: | ||
| adata = openh5ad(f'tmp_{h5ad_name}') | ||
| adata = genes_to_ensembl(adata) | ||
| if adata is None: | ||
| sys.exit("TaxID not supplied") | ||
| adata = reduction_to_metadata(adata) | ||
| adata.write({h5ad_name.replace('tmp_','')}) | ||
| os.remove(f'tmp_{h5ad_name}') | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.