Ejemplo n.º 1
0
 def test_reasons_for_engine_citations(self):
     for engine in stdpopsim.all_engines():
         for citation in engine.citations:
             assert len(citation.reasons) > 0, (
                 f"No reason given for '{citation.author}' citation "
                 f"in engine {engine.id}"
             )
Ejemplo n.º 2
0
 def test_required_params(self):
     species = stdpopsim.get_species("HomSap")
     model = species.get_demographic_model("AshkSub_7G19")
     contig = (species.get_contig("chr1"), )
     for engine in stdpopsim.all_engines():
         with self.assertRaises(TypeError):
             engine.simulate(model, contig)
Ejemplo n.º 3
0
 def test_reasons_for_engine_citations(self):
     for engine in stdpopsim.all_engines():
         for citation in engine.citations:
             self.assertGreater(
                     len(citation.reasons), 0,
                     msg=f"No reason given for '{citation.author}' citation "
                         f"in engine {engine.id}")
Ejemplo n.º 4
0
 def test_zigzag_produces_warning(self):
     species = stdpopsim.get_species("HomSap")
     model = species.get_demographic_model("Zigzag_1S14")
     contig = species.get_contig("chr22")
     samples = model.get_samples(10)
     for engine in stdpopsim.all_engines():
         with self.assertWarnsRegex(UserWarning, "Zigzag_1S14"):
             engine.simulate(model, contig, samples, dry_run=True)
Ejemplo n.º 5
0
 def test_zigzag_produces_warning(self):
     species = stdpopsim.get_species("HomSap")
     model = species.get_demographic_model("Zigzag_1S14")
     contig = species.get_contig("chr22")
     samples = model.get_samples(10)
     for engine in stdpopsim.all_engines():
         with pytest.warns(UserWarning, match="Zigzag_1S14 model.*sizes 5x lower"):
             engine.simulate(model, contig, samples, dry_run=True)
Ejemplo n.º 6
0
 def test_mutation_rate_warning(self):
     species = stdpopsim.get_species("HomSap")
     model = copy.deepcopy(species.get_demographic_model("OutOfAfrica_3G09"))
     contig = species.get_contig("chr22")
     samples = model.get_samples(10, 10, 10)
     for engine in stdpopsim.all_engines():
         with pytest.warns(
             UserWarning, match="model has mutation rate.*but this simulation used"
         ):
             engine.simulate(model, contig, samples, dry_run=True)
Ejemplo n.º 7
0
 def test_mutation_rate_warning(self):
     species = stdpopsim.get_species("HomSap")
     model = copy.deepcopy(species.get_demographic_model("OutOfAfrica_3G09"))
     ###
     # unneeded after mutation rates are included in catalog!!
     model.mutation_rate = 2.35e-8
     ###
     contig = species.get_contig("chr22")
     samples = model.get_samples(10, 10, 10)
     for engine in stdpopsim.all_engines():
         with self.assertWarnsRegex(UserWarning, "mutation"):
             engine.simulate(model, contig, samples, dry_run=True)
Ejemplo n.º 8
0
 def test_simulate_nonexistent_param(self):
     species = stdpopsim.get_species("HomSap")
     model = species.get_demographic_model("AshkSub_7G19")
     good_kwargs = dict(
         demographic_model=model,
         contig=species.get_contig("chr1"),
         samples=model.get_samples(10, 10, 10),
         dry_run=True,
     )
     bad_kwargs = good_kwargs.copy().update(nonexistent_param=None)
     for engine in stdpopsim.all_engines():
         engine.simulate(**good_kwargs)
         with self.assertRaises(TypeError):
             engine.simulate(**bad_kwargs)
Ejemplo n.º 9
0
    def test_mutation_rate_match(self):
        species = stdpopsim.get_species("HomSap")
        model = copy.deepcopy(species.get_demographic_model("OutOfAfrica_3G09"))
        contig = species.get_contig("chr22")
        assert model.mutation_rate != contig.mutation_rate
        contig = species.get_contig("chr22", mutation_rate=model.mutation_rate)
        assert model.mutation_rate == contig.mutation_rate
        contig = species.get_contig(length=100)
        assert model.mutation_rate != contig.mutation_rate
        contig = species.get_contig(length=100, mutation_rate=model.mutation_rate)
        assert model.mutation_rate == contig.mutation_rate

        samples = model.get_samples(10, 10, 10)
        for engine in stdpopsim.all_engines():
            engine.simulate(model, contig, samples, dry_run=True)
Ejemplo n.º 10
0
 def test_all_engines(self):
     for engine in stdpopsim.all_engines():
         self._test_engine(engine)
Ejemplo n.º 11
0
 def test_engine_available(self):
     all_engines = [engine.id for engine in stdpopsim.all_engines()]
     self.assertEqual(IS_WINDOWS, "slim" not in all_engines)
Ejemplo n.º 12
0
def stdpopsim_cli_parser():

    # TODO the CLI defined by this hierarchical and clumsy, but it's the best
    # I could figure out. It can definitely be improved!
    top_parser = argparse.ArgumentParser(
        description="Command line interface for stdpopsim.")
    top_parser.add_argument("-V",
                            "--version",
                            action='version',
                            version='%(prog)s {}'.format(
                                stdpopsim.__version__))
    top_parser.add_argument("-v",
                            "--verbosity",
                            action='count',
                            default=0,
                            help="Increase the verbosity")
    top_parser.add_argument(
        "-c",
        "--cache-dir",
        type=str,
        default=None,
        help=("Set the cache directory to the specified value. "
              "Note that this can also be set using the environment variable "
              "STDPOPSIM_CACHE. If both the environment variable and this "
              "option are set, the option takes precedence. "
              f"Default: {stdpopsim.get_cache_dir()}"))

    top_parser.add_argument("-e",
                            "--engine",
                            default=stdpopsim.get_default_engine().id,
                            choices=[e.id for e in stdpopsim.all_engines()],
                            help="Specify a simulation engine.")

    for engine in stdpopsim.all_engines():
        group = top_parser.add_argument_group(
            f"{engine.id} specific parameters")
        engine.add_arguments(group)

    subparsers = top_parser.add_subparsers(dest="subcommand")
    subparsers.required = True

    for species in stdpopsim.all_species():
        add_simulate_species_parser(subparsers, species)

    download_maps_parser = subparsers.add_parser(
        "download-genetic-maps",
        help="Download genetic maps",
        description=(
            "Download genetic maps and store them in the cache directory. "
            "Maps are downloaded regardless of whether they are already "
            "in the cache or not. Please use the --cache-dir option to "
            "download maps to a specific directory. "))
    download_maps_parser.add_argument(
        "species",
        nargs="?",
        help=("Download genetic maps for this species. If not specified "
              "download all known genetic maps."))
    download_maps_parser.add_argument(
        "genetic_maps",
        type=str,
        nargs="*",
        help=("If specified, download these genetic maps. If no maps "
              "are provided, download all maps for this species."))

    download_maps_parser.set_defaults(runner=run_download_genetic_maps)

    return top_parser
Ejemplo n.º 13
0
def stdpopsim_cli_parser():

    # TODO the CLI defined by this hierarchical and clumsy, but it's the best
    # I could figure out. It can definitely be improved!
    top_parser = argparse.ArgumentParser(
        description="Command line interface for stdpopsim.")
    top_parser.add_argument("-V",
                            "--version",
                            action='version',
                            version='%(prog)s {}'.format(
                                stdpopsim.__version__))
    top_parser.add_argument("-v",
                            "--verbosity",
                            action='count',
                            default=1,
                            help="Increase the verbosity")
    top_parser.add_argument(
        "-c",
        "--cache-dir",
        type=str,
        default=None,
        help=("Set the cache directory to the specified value. "
              "Note that this can also be set using the environment variable "
              "STDPOPSIM_CACHE. If both the environment variable and this "
              "option are set, the option takes precedence. "
              f"Default: {stdpopsim.get_cache_dir()}"))

    top_parser.add_argument("-e",
                            "--engine",
                            default=stdpopsim.get_default_engine().id,
                            choices=[e.id for e in stdpopsim.all_engines()],
                            help="Specify a simulation engine.")

    supported_models = stdpopsim.get_engine("msprime").supported_models
    msprime_parser = top_parser.add_argument_group(
        "msprime specific parameters")
    msprime_parser.add_argument(
        "--msprime-model",
        default=supported_models[0],
        choices=supported_models,
        help="Specify the simulation model used by msprime. "
        "See msprime API documentation for details.")

    def time_or_model(arg, _arg_is_time=[
        True,
    ], parser=top_parser):
        if _arg_is_time[0]:
            try:
                arg = float(arg)
            except ValueError:
                parser.error(f"`{arg}' is not a number")
        else:
            if arg not in supported_models:
                parser.error(f"`{arg}' is not a supported model")
        _arg_is_time[0] = not _arg_is_time[0]
        return arg

    msprime_parser.add_argument(
        "--msprime-change-model",
        metavar=("T", "MODEL"),
        type=time_or_model,
        default=[],
        action="append",
        nargs=2,
        help="Change to the specified simulation MODEL at generation T. "
        "This option may provided multiple times.")

    # SLiM is not available for windows.
    if not IS_WINDOWS:

        def slim_exec(path):
            # Hack to set the SLIM environment variable at parse time,
            # before get_version() can be called.
            os.environ["SLIM"] = path
            return path

        slim_parser = top_parser.add_argument_group("SLiM specific parameters")
        slim_parser.add_argument("--slim-path",
                                 metavar="PATH",
                                 type=slim_exec,
                                 default=None,
                                 help="Full path to `slim' executable.")
        slim_parser.add_argument(
            "--slim-script",
            action="store_true",
            default=False,
            help="Write script to stdout and exit without running SLiM.")
        slim_parser.add_argument(
            "--slim-scaling-factor",
            metavar="Q",
            default=1,
            type=float,
            help="Rescale model parameters by Q to speed up simulation. "
            "See SLiM manual: `5.5 Rescaling population sizes to "
            "improve simulation performance`. "
            "[default=%(default)s].")
        slim_parser.add_argument(
            "--slim-burn-in",
            metavar="X",
            default=10,
            type=float,
            help="Length of the burn-in phase, in units of N generations "
            "[default=%(default)s].")

    subparsers = top_parser.add_subparsers(dest="subcommand")
    subparsers.required = True

    for species in stdpopsim.all_species():
        add_simulate_species_parser(subparsers, species)

    download_maps_parser = subparsers.add_parser(
        "download-genetic-maps",
        help="Download genetic maps",
        description=(
            "Download genetic maps and store them in the cache directory. "
            "Maps are downloaded regardless of whether they are already "
            "in the cache or not. Please use the --cache-dir option to "
            "download maps to a specific directory. "))
    download_maps_parser.add_argument(
        "species",
        nargs="?",
        help=("Download genetic maps for this species. If not specified "
              "download all known genetic maps."))
    download_maps_parser.add_argument(
        "genetic_maps",
        type=str,
        nargs="*",
        help=("If specified, download these genetic maps. If no maps "
              "are provided, download all maps for this species."))

    download_maps_parser.set_defaults(runner=run_download_genetic_maps)

    return top_parser