Skip to content

Commit

Permalink
use the new mass lists
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jun 7, 2024
1 parent 2f71809 commit 4284537
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 14 deletions.
11 changes: 9 additions & 2 deletions src/depiction_targeted_preproc/example/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,22 @@ def export_results(
shutil.copy(work_dir / sample_name / file, output_dir / sample_name / file)


def initial_setup(input_imzml: Path, input_mass_list: Path, params_file: Path, dir: Path, force: bool = False) -> None:
def initial_setup(
input_imzml: Path,
input_mass_list: Path,
params_file: Path,
dir: Path,
force: bool = False,
mass_list_filename: str = "images_default_mass_list.csv",
) -> None:
if not force and (dir / "raw.imzML").exists():
logger.info("Skipping initial setup, directory already exists: {dir}", dir=dir)
else:
logger.info("Setting up directory: {dir}", dir=dir)
dir.mkdir(exist_ok=True, parents=True)
shutil.copy(input_imzml, dir / "raw.imzML")
shutil.copy(input_imzml.with_suffix(".ibd"), dir / "raw.ibd")
shutil.copy(input_mass_list, dir / "images_default_mass_list.csv")
shutil.copy(input_mass_list, dir / mass_list_filename)
shutil.copy(params_file, dir / "pipeline_params.yml")


Expand Down
12 changes: 9 additions & 3 deletions src/depiction_targeted_preproc/example_compare/run_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_configs() -> dict[str, Path]:


def prepare_tasks(input_imzml_path: Path, work_dir: Path) -> list[Path]:
input_mass_list = input_imzml_path.parent / "mass_list_vend.csv"
input_mass_list = input_imzml_path.parent / "mass_list.raw.csv"
folders = set_up_work_dir(work_dir, input_imzml_path, input_mass_list)
requested_files = get_all_output_files(folders)

Expand All @@ -34,7 +34,7 @@ def main() -> None:
for imzml in imzmls:
requested_files += prepare_tasks(data_raw_dir / imzml, work_dir=work_dir)

SnakemakeInvoke().invoke(work_dir=work_dir, result_files=requested_files, n_cores=1)
SnakemakeInvoke().invoke(work_dir=work_dir, result_files=requested_files, n_cores=2)


def get_all_output_files(folders: list[Path]) -> list[Path]:
Expand All @@ -55,7 +55,13 @@ def set_up_work_dir(work_dir: Path, input_imzml: Path, input_mass_list: Path) ->
sample_name = input_imzml.stem
for config_name, config_path in configs.items():
dir = work_dir / sample_name / config_name
initial_setup(input_imzml=input_imzml, input_mass_list=input_mass_list, params_file=config_path, dir=dir)
initial_setup(
input_imzml=input_imzml,
input_mass_list=input_mass_list,
params_file=config_path,
dir=dir,
mass_list_filename="mass_list.raw.csv",
)
folders.append(dir)
return folders

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from pathlib import Path
from typing import Annotated

import polars as pl
import typer
from typer import Option


def exp_mass_list_preparation(
input_csv_path: Annotated[Path, Option()],
out_calibration_csv_path: Annotated[Path, Option()],
out_standards_csv_path: Annotated[Path, Option()],
out_visualization_csv_path: Annotated[Path, Option()],
) -> None:
input_df = pl.read_csv(input_csv_path)

# rename cols
input_df = input_df.rename({"Marker": "label", "PC-MT (M+H)+": "mass"}).drop("No.")

# add tol column
visualization_df = input_df.with_columns(tol=pl.lit(0.25))

# for the calibration remove the CHCA peaks, they have names starting with CHCA
calibration_df = visualization_df.filter(~pl.col("name").str.starts_with("CHCA"))

# for the standards csv only keep the "standard" peaks
standards_df = visualization_df.filter(pl.col("name").str.to_lowercase().contains("standard"))

# write the results
calibration_df.write_csv(out_calibration_csv_path)
standards_df.write_csv(out_standards_csv_path)
visualization_df.write_csv(out_visualization_csv_path)


if __name__ == "__main__":
typer.run(exp_mass_list_preparation)
16 changes: 15 additions & 1 deletion src/depiction_targeted_preproc/workflow/experimental.smk
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ rule exp_compare_cluster_stats:
shell:
"python -m depiction_targeted_preproc.workflow.exp.compare_cluster_stats"
" {input.csv}"
" --output-pdf {output}"
" --output-pdf {output}"

rule exp_mass_list_preparation:
input:
csv="{sample}/mass_list.raw.csv"
output:
calibration_csv="{sample}/mass_list.calibration.csv",
standards_csv="{sample}/mass_list.standards.csv",
visualization="{sample}/mass_list.visualization.csv"
shell:
"python -m depiction_targeted_preproc.workflow.exp.mass_list_preparation"
" --input-csv-path {input.csv}"
" --out-calibration-csv {output.calibration_csv}"
" --out-standards-csv {output.standards_csv}"
" --out-visualization {output.visualization}"
4 changes: 2 additions & 2 deletions src/depiction_targeted_preproc/workflow/rules/rules_proc.smk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rule proc_calibrate_remove_global_shift:
input:
imzml=multiext("{sample}/corrected.peaks",".imzML",".ibd"),
config="{sample}/pipeline_params.yml",
mass_list="{sample}/images_default_mass_list.csv",
mass_list="{sample}/mass_list.standards.csv",
output:
imzml=temp(multiext("{sample}/calibrated.tmp",".imzML",".ibd")),
shell:
Expand All @@ -40,7 +40,7 @@ rule proc_calibrate_actual:
input:
imzml=multiext("{sample}/calibrated.tmp",".imzML",".ibd"),
config="{sample}/pipeline_params.yml",
mass_list="{sample}/images_default_mass_list.csv",
mass_list="{sample}/mass_list.calibration.csv",
output:
imzml=multiext("{sample}/calibrated",".imzML",".ibd"),
calib_data="{sample}/calib_data.hdf5",
Expand Down
8 changes: 4 additions & 4 deletions src/depiction_targeted_preproc/workflow/rules/rules_qc.smk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rule qc_table_marker_distances_baseline:
input:
imzml=multiext("{sample}/corrected.peaks",".imzML",".ibd"),
mass_list="{sample}/images_default_mass_list.csv",
mass_list="{sample}/mass_list.visualization.csv",
output:
table="{sample}/qc/table_marker_distances_baseline.parquet"
shell:
Expand All @@ -12,7 +12,7 @@ rule qc_table_marker_distances_baseline:
rule qc_table_marker_distances_calib:
input:
imzml_peaks="{sample}/calibrated.imzML",
mass_list="{sample}/images_default_mass_list.csv",
mass_list="{sample}/mass_list.visualization.csv",
output:
table="{sample}/qc/table_marker_distances_calib.parquet"
shell:
Expand Down Expand Up @@ -76,7 +76,7 @@ rule qc_plot_peak_density_grouped:
rule qc_plot_calibration_map:
input:
calib_data="{sample}/calib_data.hdf5",
mass_list="{sample}/images_default_mass_list.csv",
mass_list="{sample}/mass_list.visualization.csv",
output:
pdf="{sample}/qc/plot_calibration_map.pdf"
shell:
Expand All @@ -89,7 +89,7 @@ rule qc_plot_sample_spectra_before_after:
input:
imzml_baseline="{sample}/peaks.imzML",
imzml_calib="{sample}/calibrated.imzML",
mass_list="{sample}/images_default_mass_list.csv",
mass_list="{sample}/mass_list.visualization.csv",
output:
pdf="{sample}/qc/plot_sample_spectra_before_after.pdf"
shell:
Expand Down
4 changes: 2 additions & 2 deletions src/depiction_targeted_preproc/workflow/rules/rules_vis.smk
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ rule vis_images:
input:
imzml=multiext("{sample}/calibrated",".imzML",".ibd"),
config="{sample}/pipeline_params.yml",
mass_list="{sample}/images_{label}_mass_list.csv"
mass_list="{sample}/mass_list.visualization.csv"
output:
hdf5="{sample}/images_{label}.hdf5"
hdf5="{sample}/images_default.hdf5"
shell:
"python -m depiction_targeted_preproc.workflow.vis.images "
" --imzml-path {input.imzml[0]} --mass-list-path {input.mass_list} "
Expand Down

0 comments on commit 4284537

Please sign in to comment.