Skip to content

Commit

Permalink
update analysis to be compatible with > 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
nargesr committed Sep 4, 2024
1 parent d9f590b commit 229b1ad
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Topyfic's full documentation can be found at [here](https://mortazavilab.github.

## Installation

To install Topyfic, python version 3.8 or greater is required.
To install Topyfic, python version 3.9 or greater is required.

**IMPORTANT**: If you upgrade Topyfic from any version below 0.4.5 you need to run this

Expand Down
6 changes: 3 additions & 3 deletions Topyfic/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def __init__(self,
if colors_topics is None:
colors = sns.color_palette("turbo", self.top_model.N).as_hex()

def myfunction():
return 0.1
#def myfunction():
# return 0.1

random.shuffle(colors, myfunction)
random.shuffle(colors)
index = list(self.top_model.topics.keys())
self.colors_topics = pd.DataFrame({'colors': colors}, index=index)
else:
Expand Down
6 changes: 5 additions & 1 deletion workflow/snakemake/workflow/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ include: "rules/topModel.smk"
#### check if merging is necessary ###
merging = []
if 'merge' in config:
merging = f"{config['workdir']}/topModel_{'_'.join(config['names'])}.p"
merging = [f"{config['workdir']}/topModel_{'_'.join(config['names'])}.p",
f"{config['workdir']}/analysis_{'_'.join(config['names'])}.p"]


##### target rules #####
Expand All @@ -39,4 +40,7 @@ rule all:
expand(f"{config['workdir']}/{{name}}/{{n_topic}}/topmodel/topModel_{{name}}_{{n_topic}}.p",
name=config['names'],
n_topic=config["n_topics"]),
expand(f"{config['workdir']}/{{name}}/{{n_topic}}/topmodel/analysis_{{name}}_{{n_topic}}.p",
name=config['names'],
n_topic=config["n_topics"]),
merging,
26 changes: 25 additions & 1 deletion workflow/snakemake/workflow/rules/topModel.smk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rule run_top_model:
name=config["names"],
n_topic=config["n_topics"])
output:
f"{config['workdir']}/{{name}}/{{n_topic}}/topmodel/topModel_{{name}}_{{n_topic}}.p",
f"{config['workdir']}/{{name}}/{{n_topic}}/topmodel/topModel_{{name}}_{{n_topic}}.p"
params:
name=lambda wildcards: wildcards.name,
n_topic=lambda wildcards: wildcards.n_topic,
Expand All @@ -32,6 +32,24 @@ rule run_top_model:
config['top_model']['min_cell_participation']),
topmodel_output=f"{config['workdir']}/{params.name}/{params.n_topic}/topmodel/")

# Rule to run analysis for one input adata
rule run_analysis:
input:
expand(f"{config['workdir']}/{{name}}/{{n_topic}}/topmodel/topModel_{{name}}_{{n_topic}}.p",
name=config["names"],
n_topic=config["n_topics"])
output:
f"{config['workdir']}/{{name}}/{{n_topic}}/topmodel/analysis_{{name}}_{{n_topic}}.p"
params:
name=lambda wildcards: wildcards.name,
n_topic=lambda wildcards: wildcards.n_topic,
run:
top_model = Topyfic.read_topModel(f"{config['workdir']}/{params.name}/{params.n_topic}/topmodel/topModel_{params.name}_{params.n_topic}.p")

make_topmodel.make_analysis(adata_paths=config['count_adata'][params.name],
top_model=top_model,
topmodel_output=f"{config['workdir']}/{params.name}/{params.n_topic}/topmodel/")

# Rule to run topModel for merging multiple input adatas
if 'merge' in config:
rule run_multiple_top_model:
Expand Down Expand Up @@ -75,3 +93,9 @@ if 'merge' in config:
min_cell_participation=None if config['top_model']['min_cell_participation'] == "None" else float(
config['top_model']['min_cell_participation']),
topmodel_output=f"{config['workdir']}")

top_model = Topyfic.read_topModel(f"{config['workdir']}/topModel_{'_'.join(config['names'])}.p")

make_topmodel.make_analysis(adata_paths=adata_paths,
top_model=top_model,
topmodel_output=f"{config['workdir']}")
22 changes: 20 additions & 2 deletions workflow/snakemake/workflow/scripts/make_topmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def find_best_n_topic(n_topics, names, topmodel_outputs):

def make_top_model(trains, adata_paths, n_top_genes=50, resolution=1, max_iter_harmony=10, min_cell_participation=None,
topmodel_output=None):

# Check if the file exists
if not os.path.exists(topmodel_output):
os.makedirs(topmodel_output, mode=0o777)

sc.settings.figdir = f"{topmodel_output}/figures/"

adata = None
print(adata_paths)
if isinstance(adata_paths, str):
adata = sc.read_h5ad(adata_paths)
else:
Expand All @@ -56,10 +56,28 @@ def make_top_model(trains, adata_paths, n_top_genes=50, resolution=1, max_iter_h

print(top_model.name, topmodel_output)
top_model.save_topModel(save_path=topmodel_output)
print(f"{topmodel_output}topModel_{top_model.name}.p")

adata_topmodel.write_h5ad(f"{topmodel_output}/topic_weight_umap.h5ad")
clustering.to_csv(f"{topmodel_output}/topic_cluster_mapping.csv")


def make_analysis(adata_paths, top_model, topmodel_output=None):
# Check if the file exists
if not os.path.exists(topmodel_output):
os.makedirs(topmodel_output, mode=0o777)

adata = None
if isinstance(adata_paths, str):
adata = sc.read_h5ad(adata_paths)
else:
for adata_path in adata_paths:
tmp = sc.read_h5ad(adata_path)
if adata is None:
adata = sc.read_h5ad(adata_path)
else:
adata = adata.concatenate(tmp)

analysis_top_model = Topyfic.Analysis(Top_model=top_model)
analysis_top_model.calculate_cell_participation(data=adata)
analysis_top_model.save_analysis(save_path=topmodel_output)

0 comments on commit 229b1ad

Please sign in to comment.