methylseg.MethylDataPrep

class methylseg.MethylDataPrep(meth_file, sample_id, resolution='auto', min_coverage=10, remove_low_coverage_like_cpgs=False, chunk_size=1000000, retain_removed_rows=True)[source]

Bases: object

Normalize methylation input tables into the canonical SampleInfo schema.

__init__(meth_file, sample_id, resolution='auto', min_coverage=10, remove_low_coverage_like_cpgs=False, chunk_size=1000000, retain_removed_rows=True)[source]

Initialize methylation-table preparation for one sample.

Parameters:
  • meth_file – Path to the methylation data file.

  • sample_id – Unique identifier for the prepared sample.

  • resolution – Methylation data format: "auto", "wgbs", "450k", "27k", or "850k".

  • min_coverage – Minimum coverage threshold for WGBS data.

  • remove_low_coverage_like_cpgs – If True, remove CpGs with beta values commonly produced by very low coverage counts, such as 0.0, 0.25, 0.33, 0.5, 0.66/0.67, 0.75, and 1.0.

  • chunk_size – Number of rows to read at a time when processing large files.

  • retain_removed_rows – If True, retain removed rows in a separate DataFrame for downstream analysis. Otherwise, discard them.

Methods

__init__(meth_file, sample_id[, resolution, ...])

Initialize methylation-table preparation for one sample.

prepare()

Prepare the methylation file and wrap it in SampleInfo.

prepare_dataframe()

Load and normalize the configured methylation file.

write_prepared_tsv(out_file[, sep])

Write the prepared methylation table to disk.

Attributes

COMMON_ALIASES

HEADER_ALIASES

INPUT_ROW_INDEX_COL

LOW_COVERAGE_LIKE_BETA_VALUES

REQUIRED_COLUMNS

REQUIRED_COLUMNS = ['CpG_chrm', 'CpG_beg', 'CpG_end', 'beta']
INPUT_ROW_INDEX_COL = '__input_row_index__'
LOW_COVERAGE_LIKE_BETA_VALUES = frozenset({0.0, 0.25, 0.33, 0.5, 0.66, 0.67, 0.75, 1.0})
COMMON_ALIASES = {'CpG_beg': ['CpG_beg', 'start', 'pos', 'position'], 'CpG_chrm': ['CpG_chrm', 'chrom', 'chr', 'chromosome'], 'CpG_end': ['CpG_end', 'end', 'stop'], 'beta': ['beta', 'meth_beta', 'methylation', 'meth_percent']}
HEADER_ALIASES = {'CpG_beg': {'cpg_beg', 'pos', 'position', 'start'}, 'CpG_chrm': {'chr', 'chrom', 'chromosome', 'cpg_chrm'}, 'CpG_end': {'cpg_end', 'end', 'stop'}, 'beta': {'beta', 'meth_beta', 'meth_percent', 'methylation'}, 'coverage': {'cov', 'coverage', 'depth', 'total_reads'}, 'meth': {'meth', 'methylated', 'methylated_reads'}, 'probe': {'cpg', 'cpg_id', 'probe', 'probe_id'}}
prepare_dataframe()[source]

Load and normalize the configured methylation file.

Returns:

Two data frames containing the filtered canonical methylation table and the rows removed during preparation. The filtered table contains CpG_chrm, CpG_beg, CpG_end, and beta columns.

Return type:

tuple of pandas.DataFrame

Raises:

ValueError – If the requested resolution is unsupported or the input cannot be normalized into the canonical schema.

prepare()[source]

Prepare the methylation file and wrap it in SampleInfo.

Returns:

A (sample_info, removed_df) pair where sample_info contains the normalized methylation rows and removed_df contains excluded input rows indexed by original row position when available.

Return type:

tuple

write_prepared_tsv(out_file, sep='\t')[source]

Write the prepared methylation table to disk.

Parameters:
  • out_file – Destination path for the normalized TSV-like output.

  • sep – Delimiter used when writing the prepared table.

Returns:

Resolved output path that was written.

Return type:

pathlib.Path