builder
Classes for generating SAM and BAM files and records for testing¶
This module contains utility classes for the generation of SAM and BAM files and alignment records, for use in testing.
Classes¶
SamBuilder ¶
Builder for constructing one or more sam records (AlignmentSegments in pysam terms).
Provides the ability to manufacture records from minimal arguments, while generating any remaining attributes to ensure a valid record.
A builder is constructed with a handful of defaults including lengths for generated R1s and R2s, the default base quality score to use, a sequence dictionary and a single read group.
Records are then added using the add_pair()
method. Once accumulated the records can be accessed in the order in which they were created
through the to_unsorted_list()
function, or in a list sorted by coordinate order via
to_sorted_list(). The latter creates
a temporary file to do the sorting and is somewhat slower as a result. Lastly, the records can
be written to a temporary file using
to_path().
Source code in fgpyo/sam/builder.py
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 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 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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |
Attributes¶
Functions¶
__init__ ¶
__init__(r1_len: Optional[int] = None, r2_len: Optional[int] = None, base_quality: int = 30, mapping_quality: int = 60, sd: Optional[List[Dict[str, Any]]] = None, rg: Optional[Dict[str, str]] = None, extra_header: Optional[Dict[str, Any]] = None, seed: int = 42, sort_order: SamOrder = Coordinate) -> None
Initializes a new SamBuilder for generating alignment records and SAM/BAM files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r1_len
|
Optional[int]
|
The length of R1s to create unless otherwise specified |
None
|
r2_len
|
Optional[int]
|
The length of R2s to create unless otherwise specified |
None
|
base_quality
|
int
|
The base quality of bases to create unless otherwise specified |
30
|
sd
|
Optional[List[Dict[str, Any]]]
|
a sequence dictionary as a list of dicts; defaults to calling default_sd() if None |
None
|
rg
|
Optional[Dict[str, str]]
|
a single read group as a dict; defaults to calling default_sd() if None |
None
|
extra_header
|
Optional[Dict[str, Any]]
|
a dictionary of extra values to add to the header, None otherwise. See
|
None
|
seed
|
int
|
a seed value for random number/string generation |
42
|
sort_order
|
SamOrder
|
Order to sort records when writing to file, or output of to_sorted_list() |
Coordinate
|
Source code in fgpyo/sam/builder.py
__len__ ¶
add_pair ¶
add_pair(*, name: Optional[str] = None, bases1: Optional[str] = None, bases2: Optional[str] = None, quals1: Optional[List[int]] = None, quals2: Optional[List[int]] = None, chrom: Optional[str] = None, chrom1: Optional[str] = None, chrom2: Optional[str] = None, start1: int = NO_REF_POS, start2: int = NO_REF_POS, cigar1: Optional[str] = None, cigar2: Optional[str] = None, mapq1: Optional[int] = None, mapq2: Optional[int] = None, strand1: str = '+', strand2: str = '-', attrs: Optional[Dict[str, Any]] = None) -> Tuple[AlignedSegment, AlignedSegment]
Generates a new pair of reads, adds them to the internal collection, and returns them.
Most fields are optional.
Mapped pairs can be created by specifying both start1 and start2 and either chrom, for
pairs where both reads map to the same contig, or both chrom1 and chrom2, for pairs
where reads map to different contigs. i.e.:
- `add_pair(chrom, start1, start2)` will create a mapped pair where both reads map to
the same contig (`chrom`).
- `add_pair(chrom1, start1, chrom2, start2)` will create a mapped pair where the reads
map to different contigs (`chrom1` and `chrom2`).
A pair with only one of the two reads mapped can be created by setting only one start position. Flags will automatically be set correctly for the unmapped mate.
- `add_pair(chrom, start1)`
- `add_pair(chrom1, start1)`
- `add_pair(chrom, start2)`
- `add_pair(chrom2, start2)`
An unmapped pair can be created by calling the method with no parameters (specifically,
not setting chrom, chrom1, start1, chrom2, or start2). If either cigar is
provided, it will be ignored.
For a given read (i.e. R1 or R2) the length of the read is determined based on the presence or absence of bases, quals, and cigar. If values are provided for one or more of these parameters, the lengths must match, and the length will be used to generate any unsupplied values. If none of bases, quals, and cigar are provided, all three will be synthesized based on either the r1_len or r2_len stored on the class as appropriate.
When synthesizing, bases are always a random sequence of bases, quals are all the default base quality (supplied when constructing a SamBuilder) and the cigar is always a single M operator of the read length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
The name of the template. If None is given a unique name will be auto-generated. |
None
|
bases1
|
Optional[str]
|
The bases for R1. If None is given a random sequence is generated. |
None
|
bases2
|
Optional[str]
|
The bases for R2. If None is given a random sequence is generated. |
None
|
quals1
|
Optional[List[int]]
|
The list of int qualities for R1. If None, the default base quality is used. |
None
|
quals2
|
Optional[List[int]]
|
The list of int qualities for R2. If None, the default base quality is used. |
None
|
chrom
|
Optional[str]
|
The chromosome to which both reads are mapped. Defaults to the unmapped value. |
None
|
chrom1
|
Optional[str]
|
The chromosome to which R1 is mapped. If None, |
None
|
chrom2
|
Optional[str]
|
The chromosome to which R2 is mapped. If None, |
None
|
start1
|
int
|
The start position of R1. Defaults to the unmapped value. |
NO_REF_POS
|
start2
|
int
|
The start position of R2. Defaults to the unmapped value. |
NO_REF_POS
|
cigar1
|
Optional[str]
|
The cigar string for R1. Defaults to None for unmapped reads, otherwise all M. |
None
|
cigar2
|
Optional[str]
|
The cigar string for R2. Defaults to None for unmapped reads, otherwise all M. |
None
|
mapq1
|
Optional[int]
|
Mapping quality for R1. Defaults to self.mapping_quality if None. |
None
|
mapq2
|
Optional[int]
|
Mapping quality for R2. Defaults to self.mapping_quality if None. |
None
|
strand1
|
str
|
The strand for R1, either "+" or "-". Defaults to "+". |
'+'
|
strand2
|
str
|
The strand for R2, either "+" or "-". Defaults to "-". |
'-'
|
attrs
|
Optional[Dict[str, Any]]
|
An optional dictionary of SAM attribute to place on both R1 and R2. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if either strand field is not "+" or "-" |
ValueError
|
if bases/quals/cigar are set in a way that is not self-consistent |
Returns:
| Type | Description |
|---|---|
Tuple[AlignedSegment, AlignedSegment]
|
Tuple[AlignedSegment, AlignedSegment]: The pair of records created, R1 then R2. |
Source code in fgpyo/sam/builder.py
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 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 | |
add_single ¶
add_single(*, name: Optional[str] = None, read_num: Optional[int] = None, bases: Optional[str] = None, quals: Optional[List[int]] = None, chrom: str = NO_REF_NAME, start: int = NO_REF_POS, cigar: Optional[str] = None, mapq: Optional[int] = None, strand: str = '+', secondary: bool = False, supplementary: bool = False, attrs: Optional[Dict[str, Any]] = None) -> AlignedSegment
Generates a new single reads, adds them to the internal collection, and returns it.
Most fields are optional.
If read_num is None (the default) an unpaired read will be created. If read_num is
set to 1 or 2, the read will have it's paired flag set and read number flags set.
An unmapped read can be created by calling the method with no parameters (specifically, not setting chrom, start1 or start2). If cigar is provided, it will be ignored.
A mapped read is created by providing chrom and start.
The length of the read is determined based on the presence or absence of bases, quals, and cigar. If values are provided for one or more of these parameters, the lengths must match, and the length will be used to generate any unsupplied values. If none of bases, quals, and cigar are provided, all three will be synthesized based on either the r1_len or r2_len stored on the class as appropriate.
When synthesizing, bases are always a random sequence of bases, quals are all the default base quality (supplied when constructing a SamBuilder) and the cigar is always a single M operator of the read length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
The name of the template. If None is given a unique name will be auto-generated. |
None
|
read_num
|
Optional[int]
|
Either None, 1 for R1 or 2 for R2 |
None
|
bases
|
Optional[str]
|
The bases for the read. If None is given a random sequence is generated. |
None
|
quals
|
Optional[List[int]]
|
The list of qualities for the read. If None, the default base quality is used. |
None
|
chrom
|
str
|
The chromosome to which both reads are mapped. Defaults to the unmapped value. |
NO_REF_NAME
|
start
|
int
|
The start position of the read. Defaults to the unmapped value. |
NO_REF_POS
|
cigar
|
Optional[str]
|
The cigar string for R1. Defaults to None for unmapped reads, otherwise all M. |
None
|
mapq
|
Optional[int]
|
Mapping quality for the read. Default to self.mapping_quality if not given. |
None
|
strand
|
str
|
The strand for R1, either "+" or "-". Defaults to "+". |
'+'
|
secondary
|
bool
|
If true the read will be flagged as secondary |
False
|
supplementary
|
bool
|
If true the read will be flagged as supplementary |
False
|
attrs
|
Optional[Dict[str, Any]]
|
An optional dictionary of SAM attribute to place on both R1 and R2. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if strand field is not "+" or "-" |
ValueError
|
if read_num is not None, 1 or 2 |
ValueError
|
if bases/quals/cigar are set in a way that is not self-consistent |
Returns:
| Name | Type | Description |
|---|---|---|
AlignedSegment |
AlignedSegment
|
The record created |
Source code in fgpyo/sam/builder.py
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 | |
default_rg
staticmethod
¶
Returns the default read group used by the SamBuilder, as a dictionary.
default_sd
staticmethod
¶
Generates the sequence dictionary that is used by default by SamBuilder.
Matches the names and lengths of the HG19 reference in use in production.
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A new copy of the sequence dictionary as a list of dictionaries, one per chromosome. |
Source code in fgpyo/sam/builder.py
rg ¶
Returns the single read group that is defined in the header.
Source code in fgpyo/sam/builder.py
rg_id ¶
Returns the ID of the single read group that is defined in the header.
Source code in fgpyo/sam/builder.py
to_path ¶
to_path(path: Optional[Path] = None, index: bool = True, pred: Callable[[AlignedSegment], bool] = lambda r: True, tmp_file_type: Optional[SamFileType] = None) -> Path
Write the accumulated records to a file, sorts & indexes it, and returns the Path. If a path is provided, it will be written to, otherwise a temporary file is created and returned.
If path is provided, tmp_file_type may not be provided. In this case, the file type
(SAM/BAM/CRAM) will be automatically determined by the file extension when a path
is provided. See ~pysam for more details.
If path is not provided, the file type will default to BAM unless tmp_file_type is
provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[Path]
|
a path at which to write the file, otherwise a temp file is used. |
None
|
index
|
bool
|
if True and |
True
|
pred
|
Callable[[AlignedSegment], bool]
|
optional predicate to specify which reads should be output |
lambda r: True
|
tmp_file_type
|
Optional[SamFileType]
|
the file type to output when a path is not provided (default is BAM) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path to the sorted (and possibly indexed) file. |
Source code in fgpyo/sam/builder.py
to_sorted_list ¶
Returns the accumulated records in coordinate order.