Process Hi-C read pairs using pairtools. Parse alignments, filter duplicates, classify pairs, and generate contact statistics from Hi-C sequencing data. Use when processing raw Hi-C read pairs.
Before using code patterns, verify installed versions match. If versions differ:
Python: pip show <package> then help(module.function) to check signatures
CLI: <tool> --version then <tool> --help to confirm flags
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
Hi-C Contact Pairs Processing
"Process my Hi-C read pairs" → Parse aligned Hi-C reads into contact pairs, filter duplicates, classify pair types (cis/trans), and generate contact statistics.
BAM (aligned reads)
|
v
pairtools parse (extract pairs)
|
v
pairtools sort
|
v
pairtools dedup (remove duplicates)
|
v
pairtools select (filter by type)
|
v
Valid pairs for matrix generation
import pandas as pd
# Read pairs file (skip header)
with open('valid.pairs.gz', 'rt') as f:
header_lines = 0
for line in f:
if line.startswith('#'):
header_lines += 1
else:
break
pairs = pd.read_csv(
'valid.pairs.gz',
sep='\t',
skiprows=header_lines,
names=['readID', 'chrom1', 'pos1', 'chrom2', 'pos2', 'strand1', 'strand2', 'pair_type']
)
print(f'Total pairs: {len(pairs):,}')
print(f'\nPair types:')
print(pairs['pair_type'].value_counts())
Full Processing Pipeline
Goal: Process raw Hi-C alignments into a balanced contact matrix ready for downstream analysis (TADs, loops, compartments).
Approach: Chain pairtools operations (parse, restrict, sort, dedup, select) into a single pipeline, then aggregate valid pairs into a cooler matrix file.