1
+ use clap:: Parser ;
1
2
/*
2
3
* Forward simulation without selection:
3
4
*
18
19
* and numbers of crossovers, etc.., from being entered
19
20
* on the command line.
20
21
*/
21
- use clap:: { Arg , Command } ;
22
22
use rand:: rngs:: StdRng ;
23
23
use rand:: Rng ;
24
24
use rand:: SeedableRng ;
25
25
use rand_distr:: { Exp , Uniform } ;
26
26
use tskit:: TableAccess ;
27
27
use tskit:: TskitTypeAccess ;
28
28
29
+ #[ derive( clap:: Parser ) ]
29
30
struct 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
+ ) ]
30
38
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
+ ) ]
31
46
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
+ ) ]
32
54
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
+ ) ]
33
62
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
+ ) ]
34
70
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
+ ) ]
35
78
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
+ ) ]
36
86
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
+ ) ]
37
94
pub seed : u64 ,
38
95
}
39
96
@@ -64,84 +121,6 @@ impl std::fmt::Display for BadParameter {
64
121
}
65
122
66
123
impl 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
-
145
124
// NOTE: This function is incomplete.
146
125
fn validate ( & self ) -> Result < ( ) , BadParameter > {
147
126
match self . psurvival . partial_cmp ( & 0.0 ) {
@@ -501,7 +480,7 @@ fn runsim(params: &SimParams) -> tskit::TableCollection {
501
480
}
502
481
503
482
fn main ( ) {
504
- let params = SimParams :: new ( ) ;
483
+ let params = SimParams :: parse ( ) ;
505
484
params. validate ( ) . unwrap ( ) ;
506
485
507
486
let tables = runsim ( & params) ;
@@ -516,14 +495,14 @@ fn main() {
516
495
#[ test]
517
496
#[ should_panic]
518
497
fn test_bad_genome_length ( ) {
519
- let mut params = SimParams :: new ( ) ;
498
+ let mut params = SimParams :: default ( ) ;
520
499
params. genome_length = -1.0 ;
521
500
let _tables = runsim ( & params) ;
522
501
}
523
502
524
503
#[ test]
525
504
fn test_nonoverlapping_generations ( ) {
526
- let mut params = SimParams :: new ( ) ;
505
+ let mut params = SimParams :: default ( ) ;
527
506
params. nsteps = 500 ;
528
507
params. xovers = 1e-3 ;
529
508
params. validate ( ) . unwrap ( ) ;
@@ -532,7 +511,7 @@ fn test_nonoverlapping_generations() {
532
511
533
512
#[ test]
534
513
fn test_overlapping_generations ( ) {
535
- let mut params = SimParams :: new ( ) ;
514
+ let mut params = SimParams :: default ( ) ;
536
515
params. nsteps = 100 ;
537
516
params. xovers = 1e-3 ;
538
517
params. psurvival = 0.25 ;
0 commit comments