Skip to content

Commit

Permalink
feat: add --seed to simphenotype (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryarm authored Jan 10, 2023
1 parent bcf778a commit 9f7890a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
8 changes: 4 additions & 4 deletions docs/api/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,11 @@ To write to a **.bp** file, you must first initialize a :class:`Breakpoints` obj
breakpoints = data.Breakpoints('tests/data/example-write.bp')
breakpoints.data = {
'HG00096': [
np.array([('YRI','chr1',10114,4.3),('CEU','chr1',10116,5.2)], dtype=data.HapBlock)
np.array([('CEU','chr1',10114,4.3),('YRI','chr1',10116,5.2)], dtype=data.HapBlock)
np.array([('YRI','chr1',10114,4.3),('CEU','chr1',10116,5.2)], dtype=data.HapBlock),
np.array([('CEU','chr1',10114,4.3),('YRI','chr1',10116,5.2)], dtype=data.HapBlock),
], 'HG00097': [
np.array([('YRI','chr1',10114,4.3),('CEU','chr2',10116,5.2)], dtype=data.HapBlock)
np.array([('CEU','chr1',10114,4.3),('YRI','chr2',10116,5.2)], dtype=data.HapBlock)
np.array([('YRI','chr1',10114,4.3),('CEU','chr2',10116,5.2)], dtype=data.HapBlock),
np.array([('CEU','chr1',10114,4.3),('YRI','chr2',10116,5.2)], dtype=data.HapBlock),
]
}
breakpoints.write()
9 changes: 9 additions & 0 deletions haptools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ def simgenotype(
show_default="all variants",
help="If using a PGEN file, read genotypes in chunks of X variants; reduces memory",
)
@click.option(
"--seed",
type=int,
default=None,
show_default="chosen randomly",
help="Use this option across executions to make the output reproducible",
)
@click.option(
"-o",
"--output",
Expand Down Expand Up @@ -370,6 +377,7 @@ def simphenotype(
ids: tuple[str] = tuple(),
ids_file: Path = None,
chunk_size: int = None,
seed: int = None,
output: Path = Path("-"),
verbosity: str = "INFO",
):
Expand Down Expand Up @@ -421,6 +429,7 @@ def simphenotype(
samples,
ids,
chunk_size,
seed,
output,
log,
)
Expand Down
3 changes: 2 additions & 1 deletion haptools/sim_phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def simulate_pt(
samples: list[str] = None,
haplotype_ids: set[str] = None,
chunk_size: int = None,
seed: int = None,
output: Path = Path("-"),
log: logging.Logger = None,
):
Expand Down Expand Up @@ -319,7 +320,7 @@ def simulate_pt(

# Initialize phenotype simulator (haptools simphenotype)
log.info("Simulating phenotypes")
pt_sim = PhenoSimulator(gt, output=output, log=log)
pt_sim = PhenoSimulator(gt, output=output, seed=seed, log=log)
for i in range(num_replications):
pt_sim.run(hp.data.values(), heritability, prevalence, normalize)
log.info("Writing phenotypes")
Expand Down
24 changes: 24 additions & 0 deletions tests/test_simphenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,27 @@ def test_no_normalize(self, capfd):
assert result.exit_code == 0

tmp_transform.unlink()

def test_seed(self, capfd):
# first, create a temporary file containing the output of transform
tmp_transform = Path("temp-transform.vcf")
with open(tmp_transform, "w") as file:
file.write(self._get_transform_stdin())

cmd = f"simphenotype --seed 42 {tmp_transform} tests/data/simple.hap"
runner = CliRunner()
result = runner.invoke(main, cmd.split(" "), catch_exceptions=False)
captured = capfd.readouterr()
assert captured.out
assert result.exit_code == 0

captured1 = captured.out

cmd = f"simphenotype --seed 42 {tmp_transform} tests/data/simple.hap"
runner = CliRunner()
result = runner.invoke(main, cmd.split(" "), catch_exceptions=False)
captured = capfd.readouterr()
assert captured.out == captured1
assert result.exit_code == 0

tmp_transform.unlink()

0 comments on commit 9f7890a

Please sign in to comment.