R/get_PI_control_data_loader.R
prepare_PI_control_data.RdPrepares control (neutral/non-causal) variant lists for PI estimation training. Supports multiple input formats with GDS as the unified intermediate format for genotype data. Control variants are used to estimate the null distribution of variant-importance scores in the GLOW framework.
prepare_PI_control_data(
source,
format = "auto",
strategy = "provided",
n_controls = NULL,
gds_output = NULL,
random_seed = NULL,
verbose = 1
)Character or data.frame. Input source:
PLINK prefix (without .bed/.bim/.fam extension)
Path to GDS file (.gds)
Path to CSV file (.csv)
data.frame with variant information
Character. Input format: "auto" (default), "plink", "gds", "csv", "dataframe". When "auto", format is detected from file extension or object type.
Character. Control selection strategy (Phase 1: only "provided"):
"provided" (default): Use all variants from source as controls
Future strategies: "synonymous", "intergenic", "common" (requires annotation)
Integer or NULL. If specified, randomly sample to this number of variants. If NULL (default), use all variants from source.
Character or NULL. When format="plink", path to save the intermediate GDS file. If NULL (default), creates a temporary file that is deleted after variant extraction. Set this to save GDS for reuse in pipeline workflows.
Integer or NULL. Seed for reproducible random sampling when
n_controls is specified. Use this for reproducible training sets.
Integer. Verbosity level: 0=silent, 1=messages (default), 2=detailed.
An S3 object of class glow_pi_control_data containing:
data: data.frame with columns:
VarInfo: FAVOR format "CHR-POS-REF-ALT"
rsID: Variant identifier (from GDS annotation/id, or NA)
CHR: Chromosome (character or numeric)
POS: Position (integer)
REF: Reference allele
ALT: Alternate allele
metadata: List with:
n_input: Number of variants in source
n_output: Number of variants after sampling
format: Detected or specified format
gds_path: Path to GDS file (if created/used)
gds_persistent: Whether GDS was saved permanently
strategy: Control selection strategy used
random_seed: Seed used (if sampling performed)
date_prepared: Date of preparation
Processing Pipeline (format="plink"):
Convert PLINK to GDS using plink_to_gds
Extract variant information from GDS (CHR, POS, REF, ALT, rsID)
Generate VarInfo column in FAVOR format (CHR-POS-REF-ALT)
Random sample if n_controls specified
Delete temp GDS if gds_output is NULL
Processing Pipeline (format="gds"):
Open GDS file (read-only)
Extract variant information from GDS
Generate VarInfo column
Random sample if n_controls specified
Processing Pipeline (format="csv" or "dataframe"):
Load data from CSV or accept data.frame
Validate required columns (CHR, POS, REF, ALT)
Generate VarInfo column
Random sample if n_controls specified
GDS as Canonical Format:
GDS (Genomic Data Structure) serves as the unified intermediate format in GLOWpipeline. This design ensures:
Consistent variant representation across input formats
Efficient extraction of variant information
Integration with downstream pipeline steps (aGDS, FAVOR)
Optional persistence for pipeline reuse
Control Variant Selection:
Phase 1 implementation uses "provided" strategy: all variants from the source are treated as controls. Users are responsible for providing appropriate control variants (e.g., common synonymous variants, intergenic variants).
Future strategies (requires FAVOR annotation):
"synonymous": Filter to synonymous variants
"intergenic": Filter to intergenic variants
"common": Filter to common variants (MAF > threshold)
Random Sampling:
When n_controls is specified:
Random sampling without replacement
Set random_seed for reproducibility
Sampling applied after format conversion
If n_controls > available variants, uses all variants with warning
Computational Complexity:
PLINK format: O(n x m) for conversion + O(n) for extraction
GDS format: O(n) for extraction
CSV format: O(n) for reading
Random sampling: O(n)
where n = number of variants, m = number of samples.
plink_to_gds for PLINK to GDS conversion
get_PI for PI estimation using prepared control data
prepare_PI_case_data for case variant preparation
if (FALSE) { # \dontrun{
# Example 1: From PLINK files (GDS saved permanently for reuse)
controls <- prepare_PI_control_data(
source = "data/reference_panel",
format = "plink",
gds_output = "data/reference_panel.gds",
verbose = 1
)
# Example 2: From PLINK files (temp GDS, deleted after use)
controls <- prepare_PI_control_data(
source = "data/reference_panel",
format = "plink",
gds_output = NULL, # Default: temp file
verbose = 1
)
# Example 3: From existing GDS
controls <- prepare_PI_control_data(
source = "data/reference_panel.gds",
format = "gds"
)
# Example 4: With random sampling (reproducible)
controls <- prepare_PI_control_data(
source = "data/reference_panel.gds",
n_controls = 1000,
random_seed = 42,
verbose = 1
)
# Example 5: From CSV file with variant list
controls <- prepare_PI_control_data(
source = "data/control_variants.csv",
format = "csv"
)
# Example 6: From data.frame
control_df <- data.frame(
CHR = c(1, 1, 2),
POS = c(12345, 67890, 11111),
REF = c("A", "G", "C"),
ALT = c("G", "T", "T")
)
controls <- prepare_PI_control_data(
source = control_df,
format = "dataframe"
)
# Access results
print(controls)
summary(controls)
head(controls$data)
str(controls$metadata)
} # }