Digestion and filtering¶
hicstuff.digest cuts a genome into fragments (write_frag_info,
get_restriction_table) and assigns the mates of each pair to them
(attribute_fragments). hicstuff.filter finds the uncuts and loops of a
library (get_thresholds) and removes them (filter_events).
See Digesting a genome and Filtering 3C events.
hicstuff.digest ¶
Genome digestion
Functions used to write auxiliary instagraal compatible sparse matrices.
write_frag_info ¶
write_frag_info(fasta, enzyme, min_size=DEFAULT_THRESHOLD_SIZE, circular=False, output_contigs=DEFAULT_INFO_CONTIGS_FILE_NAME, output_frags=DEFAULT_FRAGMENTS_LIST_FILE_NAME, output_dir=None)
Digest and write fragment information
Write the fragments_list.txt and info_contigs.txt that are necessary for instagraal to run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fasta
|
Path or str
|
The path to the reference genome |
required |
enzyme
|
str, int or list of str
|
If a string, must be the name of an enzyme (e.g. DpnII) and the genome will be cut at the enzyme's restriction sites. If a number, the genome will be cut uniformly into chunks with length equal to that number. A list of enzymes can also be specified if using multiple enzymes. |
required |
min_size
|
float
|
Size below which shorter contigs are discarded. Default is 0, i.e. all contigs are retained. |
DEFAULT_THRESHOLD_SIZE
|
circular
|
bool
|
Whether the genome is circular. Default is False. |
False
|
output_contigs
|
str
|
The name of the file with contig info. Default is info_contigs.txt |
DEFAULT_INFO_CONTIGS_FILE_NAME
|
output_frags
|
str
|
The name of the file with fragment info. Default is fragments_list.txt |
DEFAULT_FRAGMENTS_LIST_FILE_NAME
|
output_dir
|
str
|
The directory, which must exist, in which output_contigs and output_frags are written. By default, they are paths of their own. |
None
|
Source code in src/hicstuff/digest.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
attribute_fragments ¶
Writes the indexed pairs file, which has two more columns than the input pairs file corresponding to the restriction fragment index of each read. Note that pairs files have 1bp point positions whereas restriction table has 0bp point poisitions.
Fragments are numbered genome-wide as in the fragments list written by
write_frag_info: contig by contig, in the order of restriction_table (that
of the genome), whatever #chromsize lines the pairs header has.
Contigs shorter than min_size are left out, as write_frag_info leaves them
out of the fragments list: their fragments get no index, their
#chromsize lines are removed, and pairs with a mate on one of them
are discarded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pairs_file
|
Path the the input pairs file. Consists of 7 tab-separated columns: readID, chr1, pos1, chr2, pos2, strand1, strand2 |
required | |
idx_pairs_file
|
Path to the output indexed pairs file. Consists of 9 white space separated columns: readID, chr1, pos1, chr2, pos2, strand1, strand2, frag1, frag2. frag1 and frag2 are 0-based restriction fragments based on whole genome. |
required | |
restriction_table
|
Dictionary with chromosome identifiers (str) as keys and list of positions (int) of restriction sites as values, in the order of the genome. The last position of each list is the length of the chromosome, as returned by get_restriction_table. |
required | |
min_size
|
float
|
Size below which contigs are left out. Default is 0, i.e. all contigs are retained. |
DEFAULT_THRESHOLD_SIZE
|
Returns:
| Type | Description |
|---|---|
int
|
The number of pairs discarded because a mate is on a contig shorter than min_size. |
Source code in src/hicstuff/digest.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
get_restriction_table ¶
Get the restriction table for a single genomic sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
Seq object
|
A biopython Seq object representing a chromosomes or contig. |
required |
enzyme
|
int, str or list of str
|
The name of the restriction enzyme used, or a list of restriction enzyme names. Can also be an integer, to digest by fixed chunk size. |
required |
circular
|
bool
|
Wether the genome is circular. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
numpy.array:
|
List of restriction fragment boundary positions for the input sequence. |
|
>>> from Bio.Seq import Seq
|
|
|
>>> get_restriction_table(Seq("AAGCCGGATCGG"),"HpaII")
|
|
|
array([0, 4, 12])
|
|
|
>>> get_restriction_table(Seq("AA"),["HpaII", "MluCI"])
|
|
|
array([0, 2])
|
|
|
>>> get_restriction_table(Seq("AA"),"aeiou1")
|
|
|
Traceback (most recent call last):
|
... |
|
ValueError |
aeiou1 is not a valid restriction enzyme.
|
|
>>> get_restriction_table("AA","HpaII")
|
|
|
Traceback (most recent call last):
|
... |
|
TypeError |
expected Seq or MutableSeq, got <class 'str'>
|
|
Source code in src/hicstuff/digest.py
find_frag ¶
Use binary search to find the index of a chromosome restriction fragment corresponding to an input genomic position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
int
|
Genomic position, in base pairs. |
required |
r_sites
|
list
|
List of genomic positions corresponding to restriction sites. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int
|
The 0-based index of the restriction fragment to which the position belongs. |
|
>>> find_frag(15, [0, 20, 30])
|
|
|
0
|
|
|
>>> find_frag(15, [10, 20, 30])
|
|
|
Traceback (most recent call last):
|
... |
|
ValueError |
The first position in the restriction table is not 0.
|
|
>>> find_frag(31, [0, 20, 30])
|
|
|
Traceback (most recent call last):
|
... |
|
ValueError |
Read position is larger than last entry in restriction table.
|
|
Source code in src/hicstuff/digest.py
frag_len ¶
frag_len(frags_file_name=DEFAULT_FRAGMENTS_LIST_FILE_NAME, output_dir=None, plot=False, fig_path=None)
logs summary statistics of fragment length distribution based on an input fragment file. Can optionally show a histogram instead of text summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frags_file_name
|
str
|
Path to the output list of fragments. |
DEFAULT_FRAGMENTS_LIST_FILE_NAME
|
output_dir
|
str
|
Directory where the list should be saved. |
None
|
plot
|
bool
|
Wether a histogram of fragment length should be shown. |
False
|
fig_path
|
str
|
If a path is given, the figure will be saved instead of shown. |
None
|
Source code in src/hicstuff/digest.py
gen_enzyme_religation_regex ¶
Return a regex which corresponds to all possible religation sites given a set of enzyme.
Parameters:
enzyme : str String that contains the names of the enzyme separated by a comma.
Returns:
re.Pattern : Regex that corresponds to all possible ligation sites given a set of enzyme.
Examples:
gen_enzyme_religation_regex('HpaII') re.compile('CCGCGG') gen_enzyme_religation_regex('HpaII,MluCI') re.compile('AATTAATT|AATTCGG|CCGAATT|CCGCGG')
Source code in src/hicstuff/digest.py
hicstuff.filter ¶
Hi-C event filtering
Analyse the contents of a 3C library and filters spurious events such as loops and uncuts to improve the overall signal. Filtering consists in excluding +- and -+ Hi-C pairs if their reads are closer than a threshold in minimum number of restriction fragments. This threshold represents the distance at which the abundance of these events deviate significantly from the rest of the library. It is estimated automatically by default using the median absolute deviation of pairs at longer distances, but can also be entered manually.
The input is a pairs file whose mates have been assigned to restriction fragments, with 9 columns: readID, chr1, pos1, chr2, pos2, strand1, strand2, frag1, frag2. The fragment indices are 0-based, counted across the genome.
@author: cmdoret (reimplementation of Axel KournaK's code)
process_read_pair ¶
Process and order read pairs in a .pairs record.
Takes a read pair (line) from a .pairs file as input, reorders the pair so that read 1 in intrachromosomal pairs always has the smallest genomic coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
Record from a pairs file with columns: readID, chr1, pos1, chr2, pos2, strand1, strand2, frag1, frag2 |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with reordered pair where each column from the line as an entry. The number of restriction sites separating reads in the pair and the type of event are also stored in the dictionary, under keys "nsites" and "type". |
Examples:
>>> d = process_read_pair("readX\ta\t1\tb\t20\t-\t-\t1\t3")
>>> for u in sorted(d.items()):
... print(u)
('chr1', 'a')
('chr2', 'b')
('frag1', 1)
('frag2', 3)
('nsites', 2)
('pos1', 1)
('pos2', 20)
('readID', 'readX')
('strand1', '-')
('strand2', '-')
('type', 'inter')
>>> d = process_read_pair('readY\ta\t20\ta\t10\t-\t+\t2\t1')
>>> [d[x] for x in sorted(d.keys())]
['a', 'a', 1, 2, 1, 10, 20, 'readY', '+', '-', '+-']
Source code in src/hicstuff/filter.py
get_thresholds ¶
get_thresholds(in_dat, interactive=False, plot_events=False, fig_path=None, prefix=None, sample_size=1000000)
Guess distance threshold for event filtering
Analyse the events in about a million Hi-C pairs drawn from the whole library, the same pairs whatever the order of the file, plot the occurrences of each event type according to number of restriction fragments, and ask user interactively for the minimum threshold for uncuts and loops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dat
|
Path to the .pairs file containing Hi-C pairs. |
required | |
interactive
|
If True, plots are diplayed and thresholds are required interactively. |
False
|
|
plot_events
|
bool
|
Whether to show the plot |
False
|
fig_path
|
str
|
Path where the figure will be saved. If None, the figure will be diplayed interactively. |
None
|
prefix
|
str
|
If the library has a name, it will be shown on plots. |
None
|
sample_size
|
int
|
Number of pairs from which the thresholds are estimated. Larger libraries are sampled by a hash of each line, the same pairs whatever the order of the file. Smaller ones are used whole. |
1000000
|
Returns:
| Type | Description |
|---|---|
dictionary
|
dictionary with keys "uncuts" and "loops" where the values are the corresponding thresholds entered by the user. |
Source code in src/hicstuff/filter.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
filter_events ¶
filter_events(in_dat, out_filtered, thr_uncut, thr_loop, plot_events=False, fig_path=None, prefix=None)
Filter events (loops, uncuts and weirds)
Filter out spurious intrachromosomal Hi-C pairs from input file. +- pairs with reads closer or at the uncut threshold and -+ pairs with reads closer or at the loop thresholds are excluded from the ouput file. -- and ++ pairs with both mates on the same fragments are also discarded. All others are written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dat
|
file object
|
File handle in read mode to the 2D BED file containing Hi-C pairs. |
required |
out_filtered
|
file object
|
File handle in write mode the output filtered 2D BED file. |
required |
thr_uncut
|
int
|
Minimum number of restriction sites between reads to keep an intrachromosomal +- pair. |
required |
thr_loop
|
int
|
Minimum number of restriction sites between reads to keep an intrachromosomal -+ pair. |
required |
plot_events
|
bool
|
If True, a plot showing the proportion of each type of event will be shown after filtering. |
False
|
fig_path
|
str
|
Path where the figure will be saved. If None, figure is displayed interactively. |
None
|
prefix
|
str
|
If the library has a name, it will be shown on plots. |
None
|
Source code in src/hicstuff/filter.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |