Added notes_on_cvs
[PsN.git] / bin / scm
blob521a394f880d41b07ec1609908f6e02f954b024b
1 #!/usr/local/bin/perl
3 use FindBin qw($Bin);
4 use lib "$Bin/../lib";
6 # Don't edit the line below, it must look exactly like this.
7 # Everything above this line will be replaced #
9 use PsN;
10 use file;
11 use model;
12 use tool::scm;
13 use tool::scm::config_file;
14 use debug;
15 use strict;
16 use Getopt::Long;
17 use Cwd;
18 use common_options;
19 use ui;
21 my $cmd_line = $0 . " " . join( " ", @ARGV );
23 my %options;
25 my %required_options = ( "config_file:s" => '');
27 my %optional_options = ( "fix" => '',
28 "search_direction:s" => 'forward|backward|both',
29 "gof:s" => '',
30 "global_init:s" => '',
31 "logfile:s" => '',
32 "model:s" => '',
33 "backwards_dir:s" => '',
34 "p_value:s" => '',
35 "p_forward:s" => '',
36 "p_backward:s" => '',
37 "do_not_drop:s" => '');
39 my $res = GetOptions( \%options,
40 @common_options::get_opt_strings,
41 keys(%required_options),
42 keys(%optional_options)
44 exit unless $res;
46 common_options::set_globals( \%options );
48 my %help_text;
49 $help_text{Pre_help_message} = <<'EOF';
50 <h3 class="heading1">scm</h3>
52 Perl script for stepwise covariate model building from NONMEM models.
54 <h3 class="heading1">Usage:</h3>
55 EOF
57 $help_text{Description} = <<'EOF';
58 <h3 class="heading1">Description:</h3>
60 The Stepwise Covariate Model (SCM) building tool of PsN implements
61 Forward Selection and Backward Elimination of covariates to a
62 model. In short, one model for each relevant parameter-covariate
63 relationship is prepared and tested in a univariate manner. In the
64 first step the model that gives the best fit of the data according
65 to some criteria is retained and taken forward to the next
66 step. In the following steps all remaining parameter-covariate
67 combinations are tested until no more covariates meet the criteria
68 for being included into the model. The Forward Selection can be
69 followed by Backward Elimination, which proceeds as the Forward
70 Selection but reversely, using stricter criteria for model
71 improvement.
72 <br><br>
73 The Stepwise Covariate Model building procedure is run by the scm
74 utility. The options to the scm utility are of three diffrent
75 types, first are the common options which works like the common
76 options for all tools. Then are the SCM specific command line
77 options. And last are options which have a more complex structures
78 and are considered to cumbersome to specify on the command
79 line. These options must therefore be specified in a separate
80 configuration file. It is possible to provide values for all
81 options, including the common options, in the configuration file
82 but it should be noted that the command line overrides the
83 configuration file, if an option is specified at both
84 locations.
86 <!--/>Read more on our homepage: www.sf.net/psn.<-->
88 EOF
90 $help_text{Examples} = <<'EOF';
91 <h3 class="heading1">Examples:</h3>
93 Execute an SCM using parameters set in the config file
94 'phenobarbital.scm'.
96 <p class="style2">$ scm -config_file=phenobarbital.scm</p>
98 Execute an SCM using parameters set in the config file
99 'phenobarbital.scm'. But override the retries and the seed
100 parameter.
102 <p class="style2">$ scm -config_file=phenobarbital.scm -retries=5 -seed=12345 phenobarbital.mod</p>
105 $help_text{Options} = <<'EOF';
106 <h3 class="heading1">Options:</h3>
108 The options are given here in their long form. Any option may be
109 abbreviated to any nonconflicting prefix. The -threads option may
110 be abbreviated to <span class="style2">-thr</span> but <span class="style2">-debug</span> may not be abbreviated to <span class="style2">-d</span>
111 because it conflicts with <span class="style2">-debug_packages</span> and <span class="style2">-debug_subroutines</span>.
113 The following options are valid:
116 $help_text{-h} = <<'EOF';
117 <p class="style2">-h | -?</p>
119 With -h or -? scm will print a list of options and exit.
122 $help_text{-help} = <<'EOF';
123 <p class="style2">-help</p>
125 With -help scm will print this, longer, help message.
128 $help_text{-config_file} = <<'EOF';
129 <p class="style2">-config_file</p>
131 A path and file name to an scm configuration file.
134 $help_text{-fix} = <<'EOF';
135 <p class="style2">-fix</p>
138 $help_text{-search_direction} = <<'EOF';
139 <p class="style2">-search_direction</p>
141 Which search task to perform: backward, forward or both is allowed.
144 $help_text{-gof} = << 'EOF';
145 <p class="style2">-gof</p>
148 $help_text{-global_init} = << 'EOF';
149 <p class="style2">-global_init</p>
152 $help_text{-logfile} = << 'EOF';
153 <p class="style2">-logfile</p>
155 The name of the logfile.
158 $help_text{-model} = << 'EOF';
159 <p class="style2">-model</p>
161 The name of the basic model file, without any parameter-covariate
162 relations included.
165 $help_text{-p_value} = << 'EOF';
166 <p class="style2">-p_value</p>
168 Use this option to set the p_value for both forward and backward
169 steps.
172 $help_text{-p_forward} = << 'EOF';
173 <p class="style2">-p_forward</p>
175 Using the p_forward option, you can specify the p-value to use for
176 the forward selection.
179 $help_text{-p_backward} = << 'EOF';
180 <p class="style2">-p_backward</p>
182 Using the p_backward option, you can specify the p-value to use
183 for the backward deletion.
186 $help_text{-do_not_drop} = << 'EOF';
187 <p class="style2">-do_not_drop</p>
189 Since the number of columns are restricted to 20 for NONMEM it is
190 necessary to minimize the number of undropped columns. The scm
191 utility uses the '=DROP' syntax of NONMEM to exclude the covariate
192 columns that are not used. If some covariates are used in the PK
193 or PRED code in the basic model you must list them using the
194 do_not_drop option to prevent them from being dropped.
197 $help_text{Post_help_message} = <<'EOF';
198 Also see 'execute.pl -help' for a description of common options.
201 common_options::online_help( 'scm', \%options, \%help_text, \%required_options, \%optional_options);
203 if ( $options{'config_file'} eq '' ){
204 print "Please specify a config file \n";
205 exit;
208 'ui' -> category( 'scm' );
209 'ui' -> silent(1) if( $options{'silent'} );
211 debug -> level( $options{'debug'} );
212 debug -> package( $options{'debug_package'} );
213 debug -> subroutine( $options{'debug_subroutine'} );
214 debug -> warn_with_trace( $options{'warn_with_trace'} );
216 my $config_file;
217 if( -e $options{'config_file'} ){
218 my $file = file -> new( name => $options{'config_file'}, path => '.' );
219 $config_file = 'tool::scm::config_file' -> new ( file => $file );
221 foreach my $option ( keys %{$config_file -> valid_scalar_options} ){
222 if( $options{$option} ) {
223 $config_file -> $option($options{$option});
227 foreach my $option ( keys %{$config_file -> valid_code_options} ){
228 if( $options{$option} ){
229 $config_file -> $option(eval($options{$option}));
233 foreach my $option ( keys %{$config_file -> valid_array_options} ){
234 if( $options{$option} ){
235 my @arr = split( /,/ , $options{$option});
236 $config_file -> $option(\@arr);
240 } else {
241 print "Error: config file $options{'config_file'} is missing.\n" ;
242 exit;
245 # Moved here as a quick fix. Consider automating option handling from
246 # the config file diagram.
247 common_options::get_defaults( \%options, 'scm' );
249 my $eval_string = common_options::model_parameters(\%options);
251 if( defined $config_file and $config_file -> extra_data_files ){
252 foreach my $extra_data_file ( keys %{$config_file -> extra_data_files} ){
253 push( @{$options{'extra_data_files'}}, $extra_data_file );
254 push( @{$options{'extra_data_headers'}}, $config_file -> extra_data_files -> {$extra_data_file} );
258 if( defined $config_file -> gof ) {
259 use lib getcwd();
260 my $tmp = $config_file -> gof;
261 require $tmp.".pm";
262 # $tmp =~ s/\.pm//;
263 $tmp = $tmp.'::test';
264 $config_file -> gof( \&$tmp );
267 my $models_array = [ model -> new ( eval $eval_string,
268 filename => $config_file -> model,
269 target => 'disk' ) ] ;
271 my $direction = $config_file -> search_direction;
273 my $scm;
275 if( $direction eq 'forward' or $direction eq 'both' ){
276 print ui -> print( category => 'scm',
277 message => "Starting scm forward search" );
279 my $orig_ofv;
280 my $orig_p_value;
281 if( -e $options{'config_file'} ){
283 if( defined $config_file -> ofv_forward ){
284 $orig_ofv = $config_file -> ofv_change;
285 $config_file -> ofv_change($config_file -> ofv_forward);
288 if( defined $config_file -> p_forward ){
289 $orig_p_value = $config_file -> p_value;
290 $config_file -> p_value( $config_file -> p_forward );
295 $config_file -> search_direction( 'forward' );
297 $scm = tool::scm ->
298 new ( eval( $common_options::parameters ),
299 models => $models_array,
300 config_file => $config_file );
301 open(CMD, ">", $scm -> directory . "/command.txt");
302 print CMD $cmd_line, "\n";
303 close(CMD);
305 $scm -> run;
307 #$scm -> print_results;
309 if( -e $options{'config_file'} ){
310 if( defined $orig_ofv ){
311 $config_file -> ofv_change( $orig_ofv );
314 if( defined $orig_p_value ){
315 $config_file -> p_value( $orig_p_value );
318 if( defined $scm -> base_criteria_values ){
319 $config_file -> base_criteria_values( @{$scm -> base_criteria_values}[0] );
322 if( defined $scm -> included_relations ){
323 $config_file -> included_relations( @{$scm -> included_relations}[0] );
328 if( $direction eq 'backward' or $direction eq 'both' ){
330 if( -e $options{'config_file'} ){
331 if( defined $config_file -> ofv_backward ){
332 $config_file -> ofv_change( $config_file -> ofv_backward );
335 if( defined $config_file -> p_backward ){
336 $config_file -> p_value( $config_file -> p_backward );
341 $config_file -> search_direction( 'backward' );
343 my $scm_back = tool::scm ->
344 new ( eval( $common_options::parameters ),
345 # included_relations => $scm -> included_relations,
346 directory => ( defined $options{'backwards_dir'} and
347 $options{'backwards_dir'} ne '' ) ? $options{'backwards_dir'} : $config_file -> directory,
348 models => $models_array,
349 config_file => $config_file );
351 ui -> print( category => 'scm',
352 message => "Starting scm backward search" );
354 open(CMD, ">", $scm_back -> directory . "/command.txt");
355 print CMD $cmd_line, "\n";
356 close(CMD);
358 $scm_back -> run;
360 #$scm_back -> print_results;
363 ui -> print( category => 'scm',
364 message => "Done" );