Alignment¶
Three modules align reads: hicstuff.iteralign truncates and extends them
(mapping mode iterative), hicstuff.cutsite cuts them at their ligation
sites (mapping mode cutsite), and hicstuff.bwamem runs bwa, as a program
or in-process through pybwa. See Aligning reads.
hicstuff.iteralign ¶
Iterative alignment
Aligns iteratively reads from a 3C fastq file: reads are trimmed with a range-sweeping number of basepairs and each read set generated this way is mapped onto the reference genome. This may result in a small increase of properly mapped reads.
@author: Remi Montagne & cmdoret
iterative_align ¶
iterative_align(fq_in, tmp_dir, ref, n_cpu, bam_out, aligner='bowtie2', min_len=20, min_qual=30, read_len=None, backend=None)
Iterative alignment
Aligns reads iteratively reads of fq_in with bowtie2, minimap2 or bwa. Reads are truncated to the 20 first nucleotides and unmapped reads are extended by 20 nucleotides and realigned on each iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fq_in
|
str
|
Path to input fastq file to align iteratively. |
required |
tmp_dir
|
str
|
Path where temporary files should be written. |
required |
ref
|
str
|
Path to the reference genome if Minimap2 is used for alignment. Path to the index genome if Bowtie2/bwa is used for alignment. |
required |
n_cpu
|
int
|
The number of CPUs to use for the iterative alignment. |
required |
bam_out
|
str
|
Path where the final alignment should be written in BAM format. |
required |
aligner
|
str
|
Choose between minimap2, bwa or bowtie2 for the alignment. |
'bowtie2'
|
min_len
|
int
|
The initial length of the fragments to align. |
20
|
min_qual
|
int
|
Minimum mapping quality required to keep Hi-C pairs. |
30
|
read_len
|
int
|
Read length in the fasta file. If set to None, the length of the first read is used. Set this value to the longest read length in the file if you have different read lengths. |
None
|
backend
|
str
|
How bwa runs: "binary" (the bwa program), "python" (in-process with pybwa) or "auto", for which see hicstuff.bwamem.resolve_backend. None reads the environment variable HICSTUFF_ALIGNER_BACKEND. In-process, the rounds run in memory, without temporary files. |
None
|
Examples:
iterative_align(fq_in='example_for.fastq', ref='example_bt2_index', bam_out='example_for.bam', aligner="bowtie2") iterative_align(fq_in='example_for.fastq', ref='example_genome.fa', bam_out='example_for.bam', aligner="minimap2")
Source code in src/hicstuff/iteralign.py
28 29 30 31 32 33 34 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 133 134 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 | |
hicstuff.cutsite ¶
Cut the reads at their ligation sites.
The module will cut at ligation sites of the given enzyme. It will from the original fastq (gzipped or not) files create new gzipped fastq files with combinations of the fragments of reads obtains by cutting at the ligation sites of the reads.
There are three choices to how combine the fragments:
- "for_vs_rev": All the combinations are made between one forward fragment and one reverse fragment. It's the most releveant one for usual workflow.
- "all": All 2-combinations are made.
- "pile": Only combinations between adjacent fragments in the initial reads are made.
This module contains the following functions:
- cut_ligation_sites
- cutsite_read
- write_pair
cut_ligation_sites ¶
Create new reads to manage pairs with a digestion and create multiple pairs to take into account all the contact present.
The function write two files for both the forward and reverse fastq with the new reads. The new reads have at the end of the ID ":[0-9]" added to differentiate the different pairs created from one read.
The function will look for all the sites present and create new pairs of reads according to the mode given to retreive as much as possible of the HiC signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fq_for
|
str
|
Path to the forward fastq file to digest. |
required |
fq_rev
|
str
|
Path to the reverse fatsq file to digest. |
required |
digest_for
|
str
|
Path to the output digested forward fatsq file to write. |
required |
digest_rev
|
str
|
Path to the output digested reverse fatsq file to write. |
required |
enzyme
|
str
|
The list of restriction enzyme used to digest the genome separated by a comma. Example: HpaII,MluCI. |
required |
mode
|
str
|
Mode to use to make the digestion. Three values possible: "all", "for_vs_rev", "pile". |
required |
seed_size
|
int
|
Minimum size of a fragment (i.e. seed size used in mapping as reads smaller won't be mapped.) |
required |
n_cpu
|
int
|
Number of CPUs. |
required |
Source code in src/hicstuff/cutsite.py
cutsite_read ¶
Find ligation sites in a given sequence and return list of the fragmented sequence and quality.
Parameters:
ligation_sites : re.Pattern Regex of all possible ligations according to the given enzymes. seq : str Sequence where to search for ligation_sites. qual : str Quality values of the sequence given. seed_size : int Minimum size of a fragment (i.e. seed size used in mapping as reads smaller won't be mapped.)
Returns:
list of str List of cut sequences. The split is made 4 bases after the start of the ligation site. list of str List of string of the qualities.
Examples:
cutsite_read(re.compile(r'GA.TA.TC'), "AAGAGTATTC", "FFF--FAFAF") (['AAGAGT', 'ATTC'], ['FFF--F', 'AFAF'])
Source code in src/hicstuff/cutsite.py
write_pair ¶
write_pair(new_reads_for, new_reads_rev, name, for_seq_list, for_qual_list, rev_seq_list, rev_qual_list, mode, final_number_of_pairs)
Function to write one pair with the combinations of fragment depending on the chosen mode.
Parameters:
new_reads_for : str Stack of the new forward reads ready to be written. new_reads_rev : str Stack of the new reverse reads ready to be written. name : str Name of the fastq read. for_seq_list : list List of forward sequences of the fastq read. for_qual_list : list List of forward qualities of the fastq read. rev_seq_list : list List of reverse sequencs of the fastq read. rev_qual_list : list List of reverse qualities of the fastq read. mode : str Mode to use to make the digestion. Three values possible: "all", "for_vs_rev", "pile". final_numbers_of_pairs : int Count of pairs after cutting.
Returns:
str Stack of forward reads ready to be written with the last pairs added. str Stack of reverse reads ready to be written with the last pairs added. int Count of pairs after cutting.
Source code in src/hicstuff/cutsite.py
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 360 361 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 | |
hicstuff.bwamem ¶
bwa mem through the bwa program, or in-process through pybwa.
hicstuff aligns reads with bwa (--aligner bwa) through one of two
backends: the bwa program, or pybwa, a dependency of hicstuff that runs
bwa's index and mem within Python
(https://github.com/fulcrumgenomics/pybwa). With pybwa, aligning reads needs
no program at all.
pybwa writes the primary records of the bwa program, read by read, once read
names are trimmed as bwa trims them. It aligns the reads of a file
BATCH_SIZE at a time, however, and bwa breaks ties between
alignments of equal score with a hash of each read's position among the
reads aligned together, which restarts with every batch. Of a read cut into
two halves of equal score, the half that is primary can then differ from the
bwa program's choice: 0.02 to 0.25% of the pairs of real libraries. With -5
(mapping mode 5spm), the 5'-most half is primary and no such tie arises.
This module contains the following functions:
- resolve_backend
- load_index
- build_index
- write_fasta
- align
- iterative_align
- align_pairs
pybwa_version ¶
The version of pybwa, or None if it cannot be imported.
describe ¶
How bwa mem runs with a backend, as the logs name it.
Source code in src/hicstuff/bwamem.py
resolve_backend ¶
The backend that aligns reads with bwa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requested
|
str or None
|
"auto", "binary" (the bwa program) or "python" (pybwa). None reads the environment variable HICSTUFF_ALIGNER_BACKEND, and is auto if it is not set. |
None
|
prefer_python
|
bool
|
Whether auto prefers pybwa to the bwa program, as mapping mode 5spm does. Otherwise auto prefers the bwa program, so that a run aligns its reads as it did before pybwa. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
"binary" or "python". |
Raises:
| Type | Description |
|---|---|
ValueError
|
If requested is not one of the backends. |
ImportError
|
If the backend requested cannot run, or, for auto, neither can, naming what to install. |
Source code in src/hicstuff/bwamem.py
build_index ¶
Build the bwa index of a fasta file at prefix with pybwa.
The BWT is built by the algorithm bwa index chooses: bwtsw for
references longer than 50 Mb, is otherwise. The index files are the same
whatever the algorithm, which only sets the time and memory taken.
Source code in src/hicstuff/bwamem.py
write_fasta ¶
Write the sequences of the bwa index at prefix to handle, in the FASTA format.
bwa keeps the reference in the .pac file, four bases a byte from its most significant bits, with A, C, G and T coded 0 to 3. It codes the other letters (N and the other IUPAC codes) as random bases, and lists each run of them in the .amb file, from which they are restored. Lowercase is not kept: the sequences are written in uppercase, as bwa aligns to them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The prefix of the bwa index. |
required |
handle
|
file object
|
A file opened for writing bytes. |
required |
line_length
|
int
|
Number of bases on each line of sequence. |
60
|
Source code in src/hicstuff/bwamem.py
load_index ¶
Load the bwa index of a reference with pybwa, building it first if there is none.
pybwa reads an index with a sequence dictionary named after the index prefix with its last suffix replaced (genome.dict for genome.fa). Rather than write one next to the reference, where another file may have that name, the index files are linked into a new directory of tmp_dir, next to a dictionary written from the index. An index that must be built is built there, from the fasta where it is, plain or compressed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genome
|
str
|
Path to the reference fasta, or to the prefix of its bwa index. |
required |
tmp_dir
|
str
|
Directory in which the index directory is created. |
required |
Returns:
| Type | Description |
|---|---|
BwaIndex
|
The index, loaded in memory. |
Source code in src/hicstuff/bwamem.py
loaded_index ¶
An index loaded by :func:load_index for the block, whose files are
removed when the block ends, or an index already loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genome
|
str or BwaIndex
|
Path to the reference or the prefix of its index, or an index. |
required |
tmp_dir
|
str
|
Directory in which a temporary directory holds the index files. |
required |
Source code in src/hicstuff/bwamem.py
align ¶
Align the reads of a fastq file with bwa mem in-process, and write their alignments to a BAM file sorted by read name.
The records written are those that hicstuff keeps of the bwa program's:
all but the supplementary alignments, and with five_spm (bwa mem
-5SPM) only the primary ones, since -M makes split alignments
secondary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reads
|
str
|
Path to the fastq file, plain or compressed. |
required |
index
|
BwaIndex
|
The index, from :func: |
required |
out_bam
|
str
|
Path to the BAM file to write. |
required |
threads
|
int
|
Threads of bwa mem and of BAM compression and sorting. |
1
|
five_spm
|
bool
|
Align with -5SPM. |
False
|
batch_size
|
int
|
Reads aligned together. |
BATCH_SIZE
|
Source code in src/hicstuff/bwamem.py
iterative_align ¶
iterative_align(reads, index, out_bam, threads=1, min_len=20, min_qual=30, read_len=None, batch_size=BATCH_SIZE)
Align the reads of a fastq file iteratively with bwa mem in-process,
as :func:hicstuff.iteralign.iterative_align does with the bwa program,
and write one record per read to a BAM file sorted by read name.
Reads are truncated to min_len bases and aligned; those whose primary alignment is unique enough (flag 0 or 16, mapping quality of min_qual or more) are kept, and the others are extended by 20 bases and aligned again, until the reads are aligned at full length. The rounds run in memory, batch_size reads at a time, without temporary files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reads
|
str
|
Path to the fastq file, plain or compressed. |
required |
index
|
BwaIndex
|
The index, from :func: |
required |
out_bam
|
str
|
Path to the BAM file to write. |
required |
threads
|
int
|
Threads of bwa mem and of BAM compression and sorting. |
1
|
min_len
|
int
|
Length of the reads in the first round. |
20
|
min_qual
|
int
|
Mapping quality from which an alignment is kept. |
30
|
read_len
|
int
|
Length of the reads, that of the first read if None. |
None
|
batch_size
|
int
|
Reads aligned together. |
BATCH_SIZE
|
Source code in src/hicstuff/bwamem.py
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 | |
align_pairs ¶
Align read pairs with bwa mem -5SPM, and write each mate's primary
alignments to a BAM file sorted by read name.
With the bwa program, the pairs are aligned in one call, whose output is split by mate. With pybwa, each mate is aligned on its own, which gives the same primary alignments: -S and -P leave mates to their own alignments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reads1
|
str
|
Paths to the fastq files of the first and second mates, in the same order, with the same names. |
required |
reads2
|
str
|
Paths to the fastq files of the first and second mates, in the same order, with the same names. |
required |
genome
|
str or BwaIndex
|
The bwa index prefix, or with pybwa an index from :func: |
required |
bam1
|
str
|
Paths to the BAM files to write, for each mate. |
required |
bam2
|
str
|
Paths to the BAM files to write, for each mate. |
required |
threads
|
int
|
Threads of bwa mem and of BAM compression and sorting. |
1
|
backend
|
str
|
"binary" or "python". |
'python'
|
tmp_dir
|
str
|
Directory of the index that pybwa loads, when genome is a path. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the bwa program fails, for instance on mates named differently. Nothing is left of its output. |