def run_simulation(args): if args.demographic_model is None: model = stdpopsim.PiecewiseConstantSize(species.population_size) model.generation_time = species.generation_time model.citations.extend(species.population_size_citations) model.citations.extend(species.generation_time_citations) qc_complete = True else: model = get_model_wrapper(species, args.demographic_model) qc_complete = model.qc_model is not None if len(args.samples) > model.num_sampling_populations: exit( f"Cannot sample from more than {model.num_sampling_populations} " "populations") samples = model.get_samples(*args.samples) contig = species.get_contig( args.chromosome, genetic_map=args.genetic_map, length_multiplier=args.length_multiplier, length=args.length, inclusion_mask=args.inclusion_mask, exclusion_mask=args.exclusion_mask, ) engine = stdpopsim.get_engine(args.engine) logger.info(f"Running simulation model {model.id} for {species.id} on " f"{contig} with {len(samples)} samples using {engine.id}.") write_simulation_summary(engine=engine, model=model, contig=contig, samples=samples, seed=args.seed) if not qc_complete: warnings.warn( stdpopsim.QCMissingWarning( f"{model.id} has not been QCed. Use at your own risk! " "Demographic models that have not undergone stdpopsim's " "Quality Control procedure may contain implementation " "errors, leading to differences between simulations " "and the model described in the original publication. " "More information about the QC process can be found in " "the developer documentation. " "https://stdpopsim.readthedocs.io/en/latest/development.html" "#demographic-model-review-process")) # extract simulate() parameters from CLI args accepted_params = inspect.signature(engine.simulate).parameters.keys() kwargs = {k: v for k, v in vars(args).items() if k in accepted_params} kwargs.update(demographic_model=model, contig=contig, samples=samples) ts = engine.simulate(**kwargs) summarise_usage() if ts is not None: write_output(ts, args) # Non-QCed models shouldn't be used in publications, so we skip the # "If you use this simulation in published work..." citation request. if qc_complete: write_citations(engine, model, contig, species) if args.bibtex_file is not None: write_bibtex(engine, model, contig, species, args.bibtex_file)
def run_simulation(args): if args.demographic_model is None: model = stdpopsim.PiecewiseConstantSize(species.population_size) model.generation_time = species.generation_time for citation in species.citations: reasons = { stdpopsim.CiteReason.POP_SIZE, stdpopsim.CiteReason.GEN_TIME, } if len(citation.reasons & reasons) > 0: model.citations.append(citation) qc_complete = True else: model = get_model_wrapper(species, args.demographic_model) qc_complete = model.qc_model is not None if len(args.samples) > model.num_sampling_populations: exit( f"Cannot sample from more than {model.num_sampling_populations} " "populations" ) samples = model.get_samples(*args.samples) contig = species.get_contig( args.chromosome, genetic_map=args.genetic_map, length_multiplier=args.length_multiplier, length=args.length, inclusion_mask=args.inclusion_mask, exclusion_mask=args.exclusion_mask, mutation_rate=model.mutation_rate, ) engine = stdpopsim.get_engine(args.engine) logger.info( f"Running simulation model {model.id} for {species.id} on " f"{contig} with {len(samples)} samples using {engine.id}." ) # DFE assignment dfe = None intervals_summary_str = None if args.dfe is None: if args.dfe_interval is not None: exit( "A DFE interval has been assigned without a DFE. " "Please specify a DFE." ) if args.dfe_annotation is not None: exit( "A DFE annotation has been assigned without a DFE. " "Please specify a DFE." ) if args.dfe_bed_file is not None: exit( "A DFE bed file has been assigned without a DFE. " "Please specify a DFE." ) else: if args.dfe_interval is not None: if args.dfe_annotation is not None: exit( "A DFE annotation and a DFE interval have been " "selected. Please only use one." ) if args.dfe_bed_file is not None: exit( "A DFE bed file and a DFE interval have been " "selected. Please only use one." ) left, right = args.dfe_interval.split(",") intervals = np.array([[int(left), int(right)]]) intervals_summary_str = f"[{left}, {right})" if args.dfe_annotation is not None: if args.dfe_bed_file is not None: exit( "A DFE bed file and a DFE annotation have been " "selected. Please only use one." ) annot = species.get_annotations(args.dfe_annotation) intervals = annot.get_chromosome_annotations(args.chromosome) intervals_summary_str = f"{annot.id} elements on {args.chromosome}" if args.dfe_bed_file is not None: intervals = np.loadtxt(args.dfe_bed_file, usecols=[1, 2], dtype="int") left = np.min(intervals) right = np.max(intervals) intervals_summary_str = f"[{left}, {right})" else: # case where no intervals specified but we have a DFE intervals = np.array( [[0, int(contig.recombination_map.sequence_length)]] ) intervals_summary_str = f"[{intervals[0][0]}, {intervals[0][1]})" dfe = species.get_dfe(args.dfe) contig.add_dfe( intervals=intervals, DFE=dfe, ) logger.info( f"Applying selection under the DFE model {dfe.id} " f"in intervals {intervals_summary_str}." ) write_simulation_summary( engine=engine, model=model, contig=contig, samples=samples, dfe=args.dfe, dfe_interval=intervals_summary_str, seed=args.seed, ) if not qc_complete: warnings.warn( stdpopsim.QCMissingWarning( f"{model.id} has not been QCed. Use at your own risk! " "Demographic models that have not undergone stdpopsim's " "Quality Control procedure may contain implementation " "errors, leading to differences between simulations " "and the model described in the original publication. " "More information about the QC process can be found in " "the developer documentation. " "https://popsim-consortium.github.io/stdpopsim-docs/" "latest/development.html#demographic-model-review-process" ) ) # extract simulate() parameters from CLI args accepted_params = inspect.signature(engine.simulate).parameters.keys() kwargs = {k: v for k, v in vars(args).items() if k in accepted_params} kwargs.update(demographic_model=model, contig=contig, samples=samples) ts = engine.simulate(**kwargs) summarise_usage() if ts is not None: write_output(ts, args) # Non-QCed models shouldn't be used in publications, so we skip the # "If you use this simulation in published work..." citation request. if qc_complete: write_citations(engine, model, contig, species, dfe) if args.bibtex_file is not None: write_bibtex(engine, model, contig, species, args.bibtex_file, dfe)