Simulations
InterSIM bridge utilities and semi-synthetic trajectory generators for multi-omics simulation studies.
R dependency
The bridge is optional and requires Rscript plus the R InterSIM package:
install.packages(
"InterSIM",
repos = c("https://cran.r-universe.dev", "https://cloud.r-project.org")
)
Check availability before running a simulation:
from motco.simulations import check_intersim_available
availability = check_intersim_available()
if not availability.available:
print(availability.message)
Example
from motco.simulations import InterSIMParams, run_intersim
result = run_intersim(
InterSIMParams(
seed=1203,
n_sample=100,
cluster_sample_prop=(0.3, 0.3, 0.4),
delta_methyl=1.0,
delta_expr=1.0,
delta_protein=1.0,
p_dmp=0.1,
)
)
methylation = result.methylation
expression = result.expression
proteomics = result.proteomics
clusters = result.clusters
Semi-synthetic trajectory generation
The semi-synthetic trajectory generator samples directly from the numpy-native InterSIM reimplementation (no R at runtime) and returns a MOTCO-ready dataset with:
- aligned methylation, gene expression, and proteomics matrices
- sample metadata containing
sample_id,group, andstage - truth metadata recording trajectory mode, per-stage/group differential indicators, per-omic δ, and the generator seed
Group A is a random baseline trajectory; group B is a deterministic transform of
group A's per-stage differential indicators selected by trajectory_mode.
from motco.simulations import (
SemiSyntheticTrajectoryParams,
generate_semisynthetic_trajectory,
)
dataset = generate_semisynthetic_trajectory(
SemiSyntheticTrajectoryParams(
seed=99,
trajectory_mode="magnitude",
n_samples=120,
n_stages=3,
group_effect_size=0.2,
group_ratio=0.5,
),
)
sample_metadata = dataset.metadata
truth = dataset.truth
Supported trajectory modes (all governed by the unified group_effect_size knob,
where 0 is the null for every mode):
| Mode | Injected group-specific pattern |
|---|---|
none |
Identical to baseline; useful for Type I error scenarios |
translation |
Constant observed-space location offset |
magnitude |
Scales δ (size); magnitude_kind='all' scales every stage, 'extremes' scales only the endpoint stages' methylation indicators |
orientation |
One global per-omic feature permutation (rotation) |
shape |
Permutes interior stages only (bend); requires at least three stages |
Evaluation harness
The evaluation harness runs one SemiSyntheticTrajectoryDataset through MOTCO integration and trajectory testing. It is the per-replicate layer used before larger Type I error or power grids.
from motco.simulations import (
SimulationEvaluationParams,
evaluate_semisynthetic_trajectory,
)
evaluation = evaluate_semisynthetic_trajectory(
dataset,
SimulationEvaluationParams(
integration_method="concat",
permutations=0,
),
)
observed_delta = evaluation.pair_statistics["delta"]
truth = evaluation.truth_metadata
Supported integration methods:
| Method | Behavior |
|---|---|
concat |
Column-binds methylation, expression, and proteomics matrices after deterministic per-feature standardization by default |
snf |
Builds per-omic affinity matrices, fuses them with SNF, and uses spectral embedding as the trajectory outcome matrix |
Set permutations=0 for observed statistics only. When permutations > 0, the harness runs RRPP and computes upper-tail empirical p-values with plus-one correction:
p = (1 + count(null >= observed)) / (1 + n_permutations)
The result includes observed delta, angle, and shape matrices, scalar two-group pair statistics, optional p-values, latent matrix metadata, generator truth metadata, runtime metadata, group/stage levels, and the trajectory contrast. Shape pair statistics and p-values are reported as unavailable for datasets with fewer than three stages.
Grid orchestration
The grid orchestration layer enumerates parameter cells, runs local replicates through the evaluation harness, persists one JSONL row per replicate, resumes completed work, and summarizes rejection rates for Type I error or power studies.
from pathlib import Path
from motco.simulations import (
SemiSyntheticTrajectoryParams,
SimulationEvaluationParams,
SimulationRunConfig,
enumerate_type_i_grid,
run_simulation_grid,
summarize_rejection_rates,
)
grid = enumerate_type_i_grid(
baseline_generator_params=SemiSyntheticTrajectoryParams(seed=2, n_samples=60),
evaluation_params=SimulationEvaluationParams(integration_method="concat", permutations=99),
axes={
"generator.n_samples": [60, 120],
"generator.group_ratio": [0.5, 0.7],
},
n_replicates=3,
base_seed=2026,
)
records = run_simulation_grid(
grid,
config=SimulationRunConfig(output_path=Path("simulation-results.jsonl")),
)
summaries = summarize_rejection_rates(records, alpha=0.05)
Each SimulationCell stores a stable cell_id, phase, SemiSyntheticTrajectoryParams, SimulationEvaluationParams, replicate count, base seed, and metadata such as the varied axis. Axis names use explicit namespaces: generator.<field> or evaluation.<field>.
Initial persistence is JSON Lines. Each row records cell and replicate IDs, deterministic seeds, a parameter signature, status, p-values, pair statistics, truth metadata, runtime metadata, cell metadata, and optional error details. With resume=True, completed rows with matching parameter signatures are skipped. A matching cell/replicate with a different parameter signature raises unless overwrite=True.
summarize_rejection_rates groups completed replicate rows by cell and statistic, then reports available replicate count, rejection count, rejection rate, Monte Carlo standard error, and unavailable replicate count. Missing statistics, such as shape p-values for two-stage datasets, remain unavailable rather than being counted as non-significant.
API
motco.simulations.InterSIMParams
dataclass
Parameters for the InterSIM R package.
Parameters left as None are omitted from the R call, allowing InterSIM
defaults to apply. The covariance parameters currently support InterSIM's
native "indep" string option or None.
Source code in src/motco/simulations/intersim.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
motco.simulations.InterSIMResult
dataclass
Normalized InterSIM simulation output.
Source code in src/motco/simulations/intersim.py
74 75 76 77 78 79 80 81 82 83 | |
motco.simulations.InterSIMAvailability
dataclass
Availability result for the external R InterSIM dependency.
Source code in src/motco/simulations/intersim.py
40 41 42 43 44 45 46 | |
motco.simulations.SemiSyntheticTrajectoryParams
dataclass
Parameters for semi-synthetic trajectory generation.
group_ratio is the proportion assigned to the first label in
group_labels within every stage. group_effect_size is the unified
effect knob described in the module docstring (0 is null for all modes).
p_dmp is the per-stage probability that a methylation feature is
differential (InterSIM's p.DMP); expression/protein indicators are
derived from it via the cross-omic maps. delta_* are the per-omic
mean-shift sizes (InterSIM's delta.*). shape_kind selects the
single-interior-stage perturbation used by shape. magnitude_kind
selects whether magnitude scales group B's methylation effect at all
stages (the default, a uniform δ scale) or only at the extreme stages
(first and last), leaving interior stages at the baseline effect.
Source code in src/motco/simulations/semisynthetic.py
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 | |
motco.simulations.SemiSyntheticTrajectoryDataset
dataclass
MOTCO-ready semi-synthetic trajectory dataset.
Source code in src/motco/simulations/semisynthetic.py
106 107 108 109 110 111 112 113 114 | |
motco.simulations.SimulationEvaluationParams
dataclass
Parameters for evaluating one semi-synthetic trajectory dataset.
Source code in src/motco/simulations/evaluation.py
48 49 50 51 52 53 54 55 56 57 58 59 60 | |
motco.simulations.SimulationEvaluationResult
dataclass
Result from evaluating one semi-synthetic trajectory dataset.
Source code in src/motco/simulations/evaluation.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
motco.simulations.SimulationTrajectoryDesign
dataclass
Trajectory design objects derived from sample metadata.
Source code in src/motco/simulations/evaluation.py
71 72 73 74 75 76 77 78 79 80 | |
motco.simulations.LatentIntegrationResult
dataclass
Integrated latent/outcome matrix and metadata.
Source code in src/motco/simulations/evaluation.py
63 64 65 66 67 68 | |
motco.simulations.SimulationCell
dataclass
One simulation parameter cell with one or more replicates.
Source code in src/motco/simulations/grid.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
motco.simulations.SimulationGrid
dataclass
Collection of simulation cells.
Source code in src/motco/simulations/grid.py
57 58 59 60 61 62 63 64 65 66 67 68 | |
motco.simulations.SimulationReplicateResult
dataclass
One persisted row for a simulation cell replicate.
Source code in src/motco/simulations/grid.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
motco.simulations.SimulationRunConfig
dataclass
Runtime options for local grid execution.
Source code in src/motco/simulations/grid.py
108 109 110 111 112 113 114 115 116 | |
motco.simulations.SimulationSummaryResult
dataclass
Rejection-rate summary for one cell and statistic.
Source code in src/motco/simulations/grid.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
motco.simulations.check_intersim_available(rscript='Rscript')
Check whether Rscript and the R InterSIM package are available.
Source code in src/motco/simulations/intersim.py
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 | |
motco.simulations.run_intersim(params, *, rscript='Rscript', check_dependency=True)
Invoke R InterSIM and return aligned omics matrices plus cluster metadata.
Source code in src/motco/simulations/intersim.py
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 | |
motco.simulations.generate_semisynthetic_trajectory(params, *, reference=None)
Generate a semi-synthetic trajectory dataset using the numpy generator.
Source code in src/motco/simulations/semisynthetic.py
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 | |
motco.simulations.integrate_semisynthetic_dataset(dataset, params)
Construct the molecular latent space from aligned omics layers.
snf and pls build genuine latent spaces (the measurement substrate
for trajectory geometry); concat is a standardized-feature-concatenation
baseline, not a constructed latent space. See the module docstring for the
latent-space architecture.
Source code in src/motco/simulations/evaluation.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
motco.simulations.build_simulation_trajectory_design(metadata, *, group_col='group', stage_col='stage')
Build full/reduced model matrices, LS means, and two-group contrast.
Source code in src/motco/simulations/evaluation.py
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 | |
motco.simulations.evaluate_semisynthetic_trajectory(dataset, params=None)
Evaluate one semi-synthetic trajectory dataset through MOTCO routines.
Source code in src/motco/simulations/evaluation.py
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 | |
motco.simulations.enumerate_type_i_grid(*, baseline_generator_params, evaluation_params=None, axes=None, n_replicates=1, base_seed=0)
Enumerate a null Type I grid with baseline plus one-factor-at-a-time axes.
Source code in src/motco/simulations/grid.py
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 | |
motco.simulations.enumerate_power_grid(*, baseline_generator_params, evaluation_params=None, trajectory_modes, effect_sizes, axes=None, n_replicates=1, base_seed=0)
Enumerate power cells from trajectory modes, effect sizes, and optional axes.
Source code in src/motco/simulations/grid.py
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 | |
motco.simulations.run_simulation_replicate(cell, replicate_index, *, evaluator=None, error_policy='raise')
Run one replicate, using a fake evaluator in tests or the default harness in production.
Source code in src/motco/simulations/grid.py
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 | |
motco.simulations.run_simulation_grid(grid, *, config=None, evaluator=None)
Run a grid locally with optional JSONL persistence and resume support.
Source code in src/motco/simulations/grid.py
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 | |
motco.simulations.read_replicate_results(path)
Read replicate records from a JSONL file.
Source code in src/motco/simulations/grid.py
401 402 403 404 405 406 407 408 409 410 411 412 | |
motco.simulations.append_replicate_results(path, records)
Append replicate records to a JSONL file.
Source code in src/motco/simulations/grid.py
392 393 394 395 396 397 398 | |
motco.simulations.summarize_rejection_rates(records, *, alpha=0.05, statistics=('delta', 'angle', 'shape'))
Summarize p-value rejection rates by cell and statistic.
Source code in src/motco/simulations/grid.py
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 | |