Skip to content

Commit

Permalink
add code to filter peaks
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jun 19, 2024
1 parent ee716b2 commit d0ebe51
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/depiction_targeted_preproc/workflow/proc/filter_peaks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pathlib import Path
from typing import Annotated

import typer
from typer import Option

from depiction.parallel_ops import ParallelConfig, WriteSpectraParallel
from depiction.persistence import ImzmlReadFile, ImzmlWriteFile, ImzmlReader, ImzmlWriter, ImzmlModeEnum
from depiction.spectrum.peak_filtering import FilterNHighestIntensityPartitioned, PeakFilteringType


def proc_filter_peaks(
input_imzml_path: Annotated[Path, Option()],
output_imzml_path: Annotated[Path, Option()]
) -> None:
read_file = ImzmlReadFile(input_imzml_path)
peaks_filter = FilterNHighestIntensityPartitioned(max_count=500, n_partitions=8)
parallel_config = ParallelConfig(n_jobs=10)

write_file = ImzmlWriteFile(output_imzml_path, imzml_mode=ImzmlModeEnum.PROCESSED)

write_parallel = WriteSpectraParallel.from_config(parallel_config)
write_parallel.map_chunked_to_file(
read_file=read_file,
write_file=write_file,
operation=filter_chunk,
bind_args={
"peaks_filter": peaks_filter
}
)


def filter_chunk(reader: ImzmlReader, indices: list[int], writer: ImzmlWriter, peaks_filter: PeakFilteringType) -> None:
for spectrum_id in indices:
mz_arr, int_arr, coords = reader.get_spectrum_with_coords(spectrum_id)
mz_arr, int_arr = peaks_filter.filter_peaks(mz_arr, int_arr, mz_arr, int_arr)
writer.add_spectrum(mz_arr, int_arr, coords)


if __name__ == "__main__":
typer.run(proc_filter_peaks)

0 comments on commit d0ebe51

Please sign in to comment.