1+ use clap:: Parser ;
12/*
23 * Forward simulation without selection:
34 *
1819 * and numbers of crossovers, etc.., from being entered
1920 * on the command line.
2021 */
21- use clap:: { Arg , Command } ;
2222use rand:: rngs:: StdRng ;
2323use rand:: Rng ;
2424use rand:: SeedableRng ;
2525use rand_distr:: { Exp , Uniform } ;
2626use tskit:: TableAccess ;
2727use tskit:: TskitTypeAccess ;
2828
29+ #[ derive( clap:: Parser ) ]
2930struct SimParams {
31+ #[ clap(
32+ short = 'N' ,
33+ long = "popsize" ,
34+ value_parser,
35+ default_value_t = 1000 ,
36+ help = "Diploid population size. Default = 1,000"
37+ ) ]
3038 pub popsize : u32 ,
39+ #[ clap(
40+ short = 'n' ,
41+ long = "nsteps" ,
42+ value_parser,
43+ default_value_t = 1000 ,
44+ help = "Number of birth steps to simulate. For non-overlapping generations, this is the number of generations to simulate. Default = 1,000."
45+ ) ]
3146 pub nsteps : u32 ,
47+ #[ clap(
48+ short = 'x' ,
49+ long = "xovers" ,
50+ value_parser,
51+ default_value_t = 0.0 ,
52+ help = "Mean number of crossovers per meiosis. The number of crossovers is Poisson-distributed with this value. Default = 0.0."
53+ ) ]
3254 pub xovers : f64 ,
55+ #[ clap(
56+ short = 'P' ,
57+ long = "psurvival" ,
58+ value_parser,
59+ default_value_t = 0.0 ,
60+ help = "Survival probability. A value of 0.0 is the Wright-Fisher model of non-overlapping generations. Values must b 0.0 <= p < 1.0. Default = 0.0."
61+ ) ]
3362 pub psurvival : f64 ,
63+ #[ clap(
64+ short = 'L' ,
65+ long = "genome_length" ,
66+ value_parser,
67+ default_value_t = 1e6 ,
68+ help = "Genome length (continuous units). Default = 1e6."
69+ ) ]
3470 pub genome_length : f64 ,
71+ #[ clap(
72+ short = 's' ,
73+ long = "simplification_interval" ,
74+ value_parser,
75+ default_value_t = 100 ,
76+ help = "Number of birth steps between simplifications. Default = 100."
77+ ) ]
3578 pub simplification_interval : u32 ,
79+ #[ clap(
80+ short = 't' ,
81+ long = "treefile" ,
82+ value_parser,
83+ default_value = "treefile.trees" ,
84+ help = "Name of output file. The format is a tskit \" trees\" file. Default = \" treefile.trees\" ."
85+ ) ]
3686 pub treefile : String ,
87+ #[ clap(
88+ short = 'S' ,
89+ long = "seed" ,
90+ value_parser,
91+ default_value_t = 0 ,
92+ help = "Random number seed. Default = 0."
93+ ) ]
3794 pub seed : u64 ,
3895}
3996
@@ -64,84 +121,6 @@ impl std::fmt::Display for BadParameter {
64121}
65122
66123impl SimParams {
67- fn new ( ) -> Self {
68- let mut params = SimParams :: default ( ) ;
69-
70- let matches = Command :: new ( "forward_simulation" )
71- . arg (
72- Arg :: new ( "popsize" )
73- . short ( 'N' )
74- . long ( "popsize" )
75- . help ( "Diploid population size. Default = 1,000." )
76- . takes_value ( true ) ,
77- )
78- . arg (
79- Arg :: new ( "nsteps" )
80- . short ( 'n' )
81- . long ( "nsteps" )
82- . help ( "Number of birth steps to simulate. For non-overlapping generations, this is the number of generations to simulate. Default = 1,000." )
83- . takes_value ( true ) ,
84- )
85- . arg (
86- Arg :: new ( "xovers" )
87- . short ( 'x' )
88- . long ( "xovers" )
89- . help ( "Mean number of crossovers per meiosis. The number of crossovers is Poisson-distributed with this value. Default = 0.0." )
90- . takes_value ( true ) ,
91- )
92- . arg (
93- Arg :: new ( "genome_length" )
94- . short ( 'L' )
95- . long ( "genome_length" )
96- . help ( "Genome length (continuous units). Default = 1e6." )
97- . takes_value ( true ) ,
98- )
99- . arg (
100- Arg :: new ( "simplification_interval" )
101- . short ( 's' )
102- . long ( "simplify" )
103- . help ( "Number of birth steps between simplifications. Default = 100." )
104- . takes_value ( true ) ,
105- )
106- . arg (
107- Arg :: new ( "treefile" )
108- . short ( 't' )
109- . long ( "treefile" )
110- . help ( "Name of output file. The format is a tskit \" trees\" file. Default = \" treefile.trees\" ." )
111- . takes_value ( true ) ,
112- )
113- . arg (
114- Arg :: new ( "seed" )
115- . short ( 'S' )
116- . long ( "seed" )
117- . help ( "Random number seed. Default = 0." )
118- . takes_value ( true ) ,
119- )
120- . arg (
121- Arg :: new ( "psurvival" )
122- . short ( 'P' )
123- . long ( "psurvival" )
124- . help ( "Survival probability. A value of 0.0 is the Wright-Fisher model of non-overlapping generations. Values must b 0.0 <= p < 1.0. Default = 0.0." )
125- . takes_value ( true ) ,
126- )
127- . get_matches ( ) ;
128-
129- params. popsize = matches. value_of_t ( "popsize" ) . unwrap_or ( params. popsize ) ;
130- params. nsteps = matches. value_of_t ( "nsteps" ) . unwrap_or ( params. nsteps ) ;
131- params. xovers = matches. value_of_t ( "xovers" ) . unwrap_or ( params. xovers ) ;
132- params. genome_length = matches
133- . value_of_t ( "genome_length" )
134- . unwrap_or ( params. genome_length ) ;
135- params. simplification_interval = matches
136- . value_of_t ( "simplification_interval" )
137- . unwrap_or ( params. simplification_interval ) ;
138- params. seed = matches. value_of_t ( "seed" ) . unwrap_or ( params. seed ) ;
139- params. psurvival = matches. value_of_t ( "psurvival" ) . unwrap_or ( params. psurvival ) ;
140- params. treefile = matches. value_of_t ( "treefile" ) . unwrap_or ( params. treefile ) ;
141-
142- params
143- }
144-
145124 // NOTE: This function is incomplete.
146125 fn validate ( & self ) -> Result < ( ) , BadParameter > {
147126 match self . psurvival . partial_cmp ( & 0.0 ) {
@@ -501,7 +480,7 @@ fn runsim(params: &SimParams) -> tskit::TableCollection {
501480}
502481
503482fn main ( ) {
504- let params = SimParams :: new ( ) ;
483+ let params = SimParams :: parse ( ) ;
505484 params. validate ( ) . unwrap ( ) ;
506485
507486 let tables = runsim ( & params) ;
@@ -516,14 +495,14 @@ fn main() {
516495#[ test]
517496#[ should_panic]
518497fn test_bad_genome_length ( ) {
519- let mut params = SimParams :: new ( ) ;
498+ let mut params = SimParams :: default ( ) ;
520499 params. genome_length = -1.0 ;
521500 let _tables = runsim ( & params) ;
522501}
523502
524503#[ test]
525504fn test_nonoverlapping_generations ( ) {
526- let mut params = SimParams :: new ( ) ;
505+ let mut params = SimParams :: default ( ) ;
527506 params. nsteps = 500 ;
528507 params. xovers = 1e-3 ;
529508 params. validate ( ) . unwrap ( ) ;
@@ -532,7 +511,7 @@ fn test_nonoverlapping_generations() {
532511
533512#[ test]
534513fn test_overlapping_generations ( ) {
535- let mut params = SimParams :: new ( ) ;
514+ let mut params = SimParams :: default ( ) ;
536515 params. nsteps = 100 ;
537516 params. xovers = 1e-3 ;
538517 params. psurvival = 0.25 ;
0 commit comments