crispr_analysis_utils.element_mapping¶
crispr_analysis_utils.element_mapping ¶
Map guides to the elements a screen targets.
A site is targeting when the protospacer overlaps an element and
safe_targeting when it overlaps none. A guide that matches nowhere is
non_targeting and has no site.
How safe a safe-targeting site really is depends on what the screen reads
out, so that is left as distance_to_tss rather than decided by a threshold
here. The column is filled for every site, since it is worth having for a
targeting one too.
The class is per site, not per guide, so a guide whose sites disagree reports each of them.
CLASSES
module-attribute
¶
CLASSES = (TARGETING, SAFE_TARGETING, NON_TARGETING)
Every class, in the order the counts report them.
SITE_COLUMNS
module-attribute
¶
SITE_COLUMNS = ('guide_id', 'chr', 'start', 'end', 'strand')
What map_to_elements needs of every site.
COLUMNS
module-attribute
¶
COLUMNS = {'guide_id': 'The sequence that was aligned. With the four site columns, the key back to guide_genome_annotation.tsv, which holds the rest.', 'chr': 'Contig of this site. Empty when the guide matches nowhere.', 'start': 'First base of the protospacer, 0-based.', 'end': 'One past its last base.', 'strand': "Strand the protospacer reads 5' to 3' on.", 'element_id': "The element this site overlaps, from the BED's fourth column when it has one, else chr:start-end. Empty unless targeting.", 'element_category': "The BED's fifth column, when it has one.", 'target_class': 'targeting: the protospacer overlaps this element. safe_targeting: it maps but overlaps no element. non_targeting: the guide matches nowhere in the genome.', 'distance_to_tss': "Bases from the target site to the nearest protein-coding TSS, signed in that gene's orientation. How far a safe-targeting site has to be to be safe depends on the screen, so threshold this yourself rather than trusting the class alone. From guide_gene_annotation.tsv, so empty without it.", 'putative_target_genes': 'Semicolon-separated ENSEMBL ids of the genes measured against this element, from the pairs table given to --genes. The same genes the IGVF export carries, repeated here so the mapping stands alone. Empty for an element with no measured pair, and without --genes.'}
Every column of the mapping table, and what it holds.
RESULTS_DIR
module-attribute
¶
RESULTS_DIR = 'elements'
Where the step's results go, so nothing of its own sits beside the inputs.
ElementError ¶
Bases: RuntimeError
The elements cannot be compared with the guides at all.
Source code in src/crispr_analysis_utils/element_mapping.py
82 83 | |
RunResult
dataclass
¶
What run produced.
Attributes:
-
outdir(Path) –The output folder.
-
mapping(DataFrame) –The mapping table: one row per site per element it overlaps.
-
metadata(dict) –The record written under
run_metadata/.
Source code in src/crispr_analysis_utils/element_mapping.py
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
read_elements ¶
read_elements(path: str | PathLike[str]) -> pd.DataFrame
Read the elements BED.
Parameters:
-
path(str | PathLike[str]) –A BED of the regions the screen targets. Three columns at least; a fourth is the element's id, a fifth its category.
Returns:
-
DataFrame–Chromosome,Start,End,element_idandelement_category.
Raises:
-
ElementError–The file is missing, empty, or has fewer than three columns.
Source code in src/crispr_analysis_utils/element_mapping.py
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 | |
map_to_elements ¶
map_to_elements(sites: DataFrame, elements: DataFrame, guides: Series | None = None, distances: DataFrame | None = None, genes: Mapping[str, list[str]] | None = None) -> pd.DataFrame
Map every site to the elements it overlaps, and classify it.
Parameters:
-
sites(DataFrame) –The mapped sites, with
SITE_COLUMNS. -
elements(DataFrame) –The elements, as
read_elementsreturns them. -
guides(Series | None, default:None) –Every guide in the library, so the ones that map nowhere are classified too. Defaults to the guides the sites name.
-
distances(DataFrame | None, default:None) –guide_gene_annotation.tsv, for itsdistance_to_tss. Without it that column is empty; the classes do not depend on it.
Returns:
-
DataFrame–One row per site per element it overlaps, one row for a site that overlaps none, and one row for every guide that matches nowhere.
Raises:
-
ElementError–No site contig appears among the elements.
Source code in src/crispr_analysis_utils/element_mapping.py
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 | |
counts ¶
counts(mapping: DataFrame) -> dict
Count the sites in each class, and the elements that got a guide.
Parameters:
-
mapping(DataFrame) –What
map_to_elementsreturned.
Returns:
-
dict–The class counts over sites, the number of guides in each class, and how many elements have at least one guide.
Source code in src/crispr_analysis_utils/element_mapping.py
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 | |
write_mapping_tsv ¶
write_mapping_tsv(path: str | PathLike[str], mapping: DataFrame) -> None
Write the guide-to-element mapping.
Coordinates and distances are whole numbers, so they are written without the trailing .0 that a merge against an absent row leaves behind.
Source code in src/crispr_analysis_utils/element_mapping.py
282 283 284 285 286 287 288 289 290 291 | |
write_qc_tsv ¶
write_qc_tsv(path: str | PathLike[str], mapping: DataFrame, elements: DataFrame, sites: DataFrame | None = None) -> None
Write qc.tsv: how the library covers the elements.
Parameters:
-
path(str | PathLike[str]) –The file to write.
-
mapping(DataFrame) –What
map_to_elementsreturned. -
elements(DataFrame) –The elements, as
read_elementsreturns them, so the ones no guide reaches are counted rather than missing. -
sites(DataFrame | None, default:None) –The mapped sites, used to say how far an element with no guide is from the nearest one.
Notes
Three columns, section, name and value. Guides per element
counts distinct guides, and the density beside it is per 100 bases,
because a wide element collects more guides without being better covered.
Source code in src/crispr_analysis_utils/element_mapping.py
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 | |
write_columns_yaml ¶
write_columns_yaml(path: str | PathLike[str]) -> None
Write guide_element_mapping_columns.yaml, generated from COLUMNS so it cannot drift.
Source code in src/crispr_analysis_utils/element_mapping.py
416 417 418 419 420 | |
run ¶
run(alignment: str | PathLike[str] | DataFrame, elements: str | PathLike[str], outdir: str | PathLike[str], *, annotation: str | PathLike[str] | DataFrame | None = None, genes: str | PathLike[str] | None = None) -> RunResult
Map a library to the elements a screen targets, and write the table.
Parameters:
-
alignment(str | PathLike[str] | DataFrame) –guide_genome_annotation.tsvfromguide_alignment.run, or the same table as a DataFrame. Every guide in it is classified, including the ones that match nowhere. -
elements(str | PathLike[str]) –A BED of the regions the screen targets.
-
outdir(str | PathLike[str]) –Where the table goes. Created if it does not exist; nothing is ever written outside it.
-
annotation(str | PathLike[str] | DataFrame | None, default:None) –guide_gene_annotation.tsvfromguide_annotation.run, which fills thedistance_to_tsscolumn.
Returns:
-
RunResult–The mapping and the record.
Source code in src/crispr_analysis_utils/element_mapping.py
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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |