Skip to content

crispr_analysis_utils.guide_annotation.pipeline

crispr_analysis_utils.guide_annotation.pipeline

The gene annotation: read the aligned sites, place them in genes, write the table.

outdir gets three folders and nothing else: annotations/ with the one results table and its guide_gene_annotation_columns.yaml, run_metadata/ with this invocation's record, and logs/.

RESULTS_DIR module-attribute

RESULTS_DIR = 'annotations'

Where the step's results go, so nothing of its own sits beside the inputs.

STEP module-attribute

STEP = 'guide_annotation'

The step its run records and log are named for.

PROMOTER_WINDOW module-attribute

PROMOTER_WINDOW = 1000

How far either side of a TSS counts as its promoter, by default.

RunResult dataclass

What run produced.

Attributes:

  • outdir (Path) –

    The output folder.

  • annotated (DataFrame) –

    The annotation table: one row per site per overlapping gene.

  • store (Store) –

    The annotation store the run used.

  • metadata (dict) –

    The record written under run_metadata/.

Source code in src/crispr_analysis_utils/guide_annotation/pipeline.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@dataclass
class RunResult:
    """What `run` produced.

    Attributes
    ----------
    outdir
        The output folder.
    annotated
        The annotation table: one row per site per overlapping gene.
    store
        The annotation store the run used.
    metadata
        The record written under ``run_metadata/``.
    """

    outdir: Path
    annotated: pd.DataFrame
    store: Store
    metadata: dict = field(default_factory=dict)

run

run(alignment: str | PathLike[str] | DataFrame, store: str | PathLike[str] | Store, outdir: str | PathLike[str], *, promoters: str | PathLike[str] | None = None, promoter_window: int = PROMOTER_WINDOW, distance_from: str = MIDPOINT) -> RunResult

Annotate every aligned site against the genes of the store.

Parameters:

  • alignment (str | PathLike[str] | DataFrame) –

    guide_genome_annotation.tsv from guide_alignment.run, or the same table as a DataFrame. Only the sites with a PAM are annotated, which is what it means for a guide to map.

  • store (str | PathLike[str] | Store) –

    The annotation store, as build_store wrote it.

  • outdir (str | PathLike[str]) –

    Where the table goes. Created if it does not exist; nothing is ever written outside it.

  • promoters (str | PathLike[str] | None, default: None ) –

    A TSS BED. Without one, a site per transcript is taken from the store.

  • promoter_window (int, default: PROMOTER_WINDOW ) –

    How far either side of a TSS counts as its promoter.

  • distance_from (str, default: MIDPOINT ) –

    Where distances are measured from: the protospacer midpoint, or the target site 3 bases 5' of the PAM.

Returns:

  • RunResult –

    The table, the store and the record.

Raises:

  • AnnotationError –

    The sites and the store name contigs differently, or the alignment table holds no mapped site.

Source code in src/crispr_analysis_utils/guide_annotation/pipeline.py
 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
def run(
    alignment: str | os.PathLike[str] | pd.DataFrame,
    store: str | os.PathLike[str] | Store,
    outdir: str | os.PathLike[str],
    *,
    promoters: str | os.PathLike[str] | None = None,
    promoter_window: int = PROMOTER_WINDOW,
    distance_from: str = MIDPOINT,
) -> RunResult:
    """Annotate every aligned site against the genes of the store.

    Parameters
    ----------
    alignment
        ``guide_genome_annotation.tsv`` from `guide_alignment.run`, or the
        same table as a DataFrame. Only the sites with a PAM are annotated,
        which is what it means for a guide to map.
    store
        The annotation store, as `build_store` wrote it.
    outdir
        Where the table goes. Created if it does not exist; nothing is ever
        written outside it.
    promoters
        A TSS BED. Without one, a site per transcript is taken from the store.
    promoter_window
        How far either side of a TSS counts as its promoter.
    distance_from
        Where distances are measured from: the protospacer midpoint, or the
        target site 3 bases 5' of the PAM.

    Returns
    -------
    RunResult
        The table, the store and the record.

    Raises
    ------
    AnnotationError
        The sites and the store name contigs differently, or the alignment
        table holds no mapped site.
    """
    outdir = Path(outdir)
    parameters = {
        "promoters": None if promoters is None else str(promoters),
        "promoter_window": promoter_window,
        "distance_from": distance_from,
    }
    with run_record(
        outdir, STEP, RESULTS_DIR, parameters, logger, "Gene annotation"
    ) as (state, record):
        state["store"] = {}
        logger.info("Gene annotation into %s, with %s", outdir, parameters)
        return _run(alignment, store, outdir, promoters, parameters, state, record)