PYPE

API reference

Public dataframe columns and Python identifiers use snake_case. Counts end in _count, p-values use pvalue, and uncertainty uses standard_error.

Top-level functions

The three main functions are available directly from import pype.

pype.phenome_wide_association

View source
phenome_wide_association(
    phenotypes,
    predictors,
    outcomes=None,
    covariates=None,
    min_sample_count=1000,
)

Run each predictor against each selected outcome with complete-case ordinary least squares.

ParameterDescription
phenotypesDataframe containing outcomes and covariates.
predictorsDataframe containing one or more numeric predictors with the same index.
outcomesOutcome column names. Defaults to every phenotype column outside covariates.
covariatesOptional covariate column names.
min_sample_countMinimum complete-case sample count required for a regression result.

Returns: A dataframe sorted by pvalue. See result columns.

results = pype.phenome_wide_association(
    phenotypes,
    predictors,
    outcomes=["trait_a", "trait_b"],
    covariates=["age", "sex"],
    min_sample_count=500,
)

pype.add_phenotype_metadata

View source
add_phenotype_metadata(
    results,
    metadata,
    result_field="outcome",
    metadata_field="outcome",
    description_column="description",
    category_column="category",
)

Join phenotype descriptions and plotting categories to PheWAS results.

ParameterDescription
resultsPheWAS result dataframe.
metadataMetadata dataframe or path to a delimited file.
result_fieldIdentifier column in the result dataframe.
metadata_fieldMatching identifier column in metadata.
description_columnOptional readable description column.
category_columnRequired plotting category column.

Returns: A copy of the result dataframe with category and optional description.

pype.mendelian_randomization

View source
mendelian_randomization(
    exposure,
    outcome,
    exposure_name="exposure",
    outcome_name="outcome",
    methods=("ivw", "egger", "weighted_median"),
    config=None,
    seed=None,
)

Validate and harmonize exposure and outcome summary statistics, then run selected MR estimators.

ParameterDescription
exposureExposure summary-statistic dataframe.
outcomeOutcome summary-statistic dataframe.
exposure_nameName written into output column names and diagnostics.
outcome_nameOutcome label written to the result dataframe.
methodsMethod key, iterable of keys, or "all".
configOptional method parameter dictionary. Defaults load from pype.mr/defaults.json.
seedSeed for bootstrap and empirical-distribution methods.

Returns: (results, diagnostics). Results use method, exposure, outcome, pvalue, beta, standard_error, and variant_count. Diagnostics contains heterogeneity, MR-Egger intercept, and MR-PRESSO results when requested.

Method keys are listed on the MR methods page.

Configuration keys

KeyUsed by
bootstrap_iterationsMedian and mode standard errors.
penalty_constantPenalized median and penalized mode.
bandwidth_scaleMode estimators.
run_outlier_testMR-PRESSO outlier detection.
run_distortion_testMR-PRESSO corrected-estimate comparison.
significance_thresholdMR-PRESSO tests.
simulation_countMR-PRESSO empirical distributions.

Plotting

pype.plotting.manhattan

View source
manhattan(
    results,
    output,
    alpha=0.05,
    correction="bonferroni",
    title=None,
    annotate=False,
    annotation_count=2,
    annotation_width=30,
    color_map="tab20",
    seed=None,
    width=15,
    height=8,
    dpi=300,
)

Plot association significance by phenotype category. Writes the image and a significant-result TSV.

pype.plotting.category_enrichment

View source
category_enrichment(
    results,
    output,
    alpha=0.05,
    correction="bonferroni",
    title=None,
    width=15,
    height=8,
    dpi=300,
)

Plot the percentage of significant associations within each phenotype category.

pype.plotting.volcano

View source
volcano(
    results,
    output,
    alpha=0.05,
    correction="bonferroni",
    title=None,
    annotate=False,
    annotation_count=5,
    annotation_width=30,
    width=15,
    height=8,
    dpi=300,
)

Create one effect-size versus significance plot per predictor. Writes images and significant-result TSVs.

Annotations

pype.annotations.annotate_genes

View source
annotate_genes(
    gene_intervals_file,
    variants,
    results,
    upstream_kb=10,
    downstream_kb=10,
    output_directory=None,
    output_prefix="pype",
)

Find genes whose intervals fall within the selected upstream and downstream distances from each variant.

variants requires variant_id, chromosome, and position. Distances are in kilobases. See gene interval format.

pype.annotations.annotate_variants_and_genes

View source
annotate_variants_and_genes(
    results,
    output_directory,
    variant_fields=("dbsnp",),
    gene_fields=("summary",),
)

Write one summary file per significant variant using MyVariant.info and MyGene.info. Install the annotations extra first.

MR utilities

pype.mr.harmonize

View source
harmonize(exposure, outcome)

Match variants by variant_id and chromosome, align allele pairs, flip reversed outcome effects, and remove incompatible or ambiguous palindromic rows.

pype.mr.standardize_variants

View source
standardize_variants(data)

Validate required summary-statistic columns, normalize chromosome and allele values, and resolve duplicates with sample size.

Common exceptions

ConditionException
Phenotype and predictor indexes differValueError
Requested outcome, covariate, metadata, or MR columns are missingValueError
Exposure and outcome names are identicalValueError
An unsupported MR method key is suppliedValueError
Variants do not overlap or all fail harmonizationValueError
Optional annotation clients are unavailableImportError