annotate_cpg_density.py calculates three sequence-based CpG metrics for
genomic intervals. It was developed for transposable-element (TE) loci, but it
can be used with any set of genomic regions represented as a three-column BED
file.
For every input interval, the script retrieves the corresponding reference
sequence through pybedtools/bedtools nuc, counts cytosines, guanines, and
case-insensitive CG dinucleotides, and writes three BED-like output files.
For an interval of length L, let:
- NCpG be the observed number of
CGdinucleotides; - NC be the number of cytosines; and
- NG be the number of guanines.
The reported metrics are:
| Output suffix | Metric | Formula |
|---|---|---|
_CpG-per-base.bed |
CpG per interval base | NCpG / L |
_CpG-per-GC.bed |
CpG per C or G base | NCpG / (NC + NG) |
_CpG-OE.bed |
CpG observed/expected ratio | NCpG x L / (NC x NG) |
The first metric is reported as 0.000000 for a zero-length interval. The
second is NA when an interval contains neither C nor G. Observed/expected is
NA when either the C count or G count is zero. Numeric values are written to
six decimal places.
- Python 3
- BEDTools available on
PATH - pybedtools
- Optional: SAMtools to create a FASTA index
One reproducible installation route is Conda/Bioconda:
conda create --name te-cpg-density \
--channel conda-forge --channel bioconda \
python=3.10 pybedtools=0.12.0 bedtools=2.31.0 samtools
conda activate te-cpg-densityAlternatively, install the Python dependency with:
python -m pip install -r requirements.txtBEDTools is a command-line dependency and must still be installed separately
and discoverable on PATH.
The command accepts one reference genome followed by one or more BED files:
python annotate_cpg_density.py <genome.fa> <TE1.bed> [<TE2.bed> ...]
Use the same genome assembly and chromosome naming convention used to generate the BED coordinates. An accompanying FASTA index is recommended:
samtools faidx genome.faIf genome.fa.fai is absent, the script prints a warning and continues.
Input must be a headerless, tab-delimited, three-column BED file:
chr1 100000 100250
chr1 205000 205430
chrX 75000 75400
BED coordinates are zero-based and end-exclusive. Intervals are processed independently, including intervals that overlap.
Important: the current parser uses the fixed
bedtools nuccolumn positions produced from a three-column BED file. BED4, BED6, or wider input shifts those positions and is not supported by this version.
From the repository directory:
python annotate_cpg_density.py genome.fa repeatmasker_TE_loci.bedMultiple BED files can be processed in one command:
python annotate_cpg_density.py genome.fa \
LINE_loci.bed SINE_loci.bed LTR_loci.bedFor each BED file, outputs are written to the current working directory using
the input basename. For example, LINE_loci.bed produces:
LINE_loci_CpG-per-base.bed
LINE_loci_CpG-per-GC.bed
LINE_loci_CpG-OE.bed
Each output has four tab-delimited columns and no header:
chrom start end value
Example:
chr1 100000 100250 0.024000
chr1 205000 205430 0.011628
- CpG per base measures absolute CpG density across the whole interval.
- CpG per C+G normalizes the CpG count by GC-bearing bases and can help separate CpG abundance from interval length.
- CpG observed/expected compares observed CpGs with the count expected from the interval's marginal C and G composition. Values below 1 indicate CpG depletion and values above 1 indicate enrichment under this simple model.
CpG composition is reference-sequence based; it does not measure DNA methylation. For TE analyses, interpret these values in the context of TE family, locus age, genomic context, assembly version, and interval length.
If this script contributes to a publication, cite the repository and the core tools it calls:
- Quinlan AR, Hall IM. BEDTools: a flexible suite of utilities for comparing genomic features. Bioinformatics. 2010;26(6):841-842. https://doi.org/10.1093/bioinformatics/btq033
- Dale RK, Pedersen BS, Quinlan AR. Pybedtools: a flexible Python library for manipulating genomic datasets and annotations. Bioinformatics. 2011;27(24):3423-3424. https://doi.org/10.1093/bioinformatics/btr539