Coverage Estimation

Runs mosdepth across all samples in binned mode, then computes an overlap-weighted mean depth for the KIV-2 region.

grid.utils.mosdepth.build_mosdepth_command(cram_path, ref_fasta, output_prefix, by, fast_mode, threads=1)

Build mosdepth command with appropriate flags.

Parameters:
  • cram_path (str) – Path to CRAM file

  • ref_fasta (str) – Path to reference genome FASTA

  • output_prefix (Path) – Output prefix for mosdepth

  • by (int) – Bin size for coverage calculation

  • fast_mode (bool) – Whether to use fast mode

  • threads (int) – Number of threads to use

Returns:

List of command arguments

Return type:

List[str]

grid.utils.mosdepth.check_mosdepth_available()

Check if mosdepth executable is available in PATH.

Parameters:

None

Returns:

None

Raises:

RuntimeError – If mosdepth is not found.

grid.utils.mosdepth.compute_mosdepth(config, console=None)
grid.utils.mosdepth.compute_region_coverage(regions_file, chrom, start, end)

Compute average coverage for a genomic region from mosdepth output.

Parameters:
  • regions_file (Path) – Path to mosdepth regions.bed.gz file

  • chrom (str) – Chromosome name

  • start (int) – Start position

  • end (int) – End position

Returns:

Average coverage as integer (rounded and scaled by 100)

Return type:

int

grid.utils.mosdepth.remove_intermediate_files(work_dir, console=None, include_region_bed_gz=False)

Remove intermediate mosdepth files from the working directory.

Parameters:
  • work_dir (Path) – Path to working directory

  • console – Optional console for logging

  • include_region_bed_gz (bool) – Whether to include regions.bed.gz files in removal

Returns:

None

Return type:

None

grid.utils.mosdepth.run_mosdepth_single_cram(cram_path, ref_fasta, work_dir, chrom, start, end, region_name, by, fast_mode, threads=1, console=None)

Run mosdepth on a single CRAM file and compute coverage for region.

Parameters:
  • cram_path (str) – Path to CRAM file

  • ref_fasta (str) – Path to reference genome FASTA

  • work_dir (Path) – Working directory for intermediate files

  • chrom (str) – Chromosome name

  • start (int) – Start position

  • end (int) – End position

  • by (int) – Bin size for mosdepth

  • fast_mode (bool) – Whether to use fast mode

  • threads (int) – Number of threads to use

  • region_name (str)

Returns:

Tuple of (sample_name, coverage) where coverage is int or “Error”

Return type:

Tuple[str, int | str]

grid.utils.mosdepth.wait_for_mosdepth_output(work_dir, sample_name, console=None, max_attempts=3, sleep_seconds=2)

Wait for mosdepth output file to appear.

Parameters:
  • work_dir (Path) – Working directory where mosdepth writes output

  • sample_name (str) – Sample name to search for

  • max_attempts (int) – Maximum number of attempts to find file

  • sleep_seconds (int) – Seconds to wait between attempts

Returns:

Path to regions.bed.gz file

Raises:

FileNotFoundError – If file is not found after max attempts

Return type:

Path

grid.utils.mosdepth.write_coverage_result(output_file, sample_name, coverage, write_lock)

Write coverage result to output file in a thread-safe manner.

Parameters:
  • output_file (Path) – Path to output TSV file

  • sample_name (str) – Sample name

  • coverage (int) – Coverage value

  • write_lock (allocate_lock) – Threading lock for safe file writing

Return type:

None