comparison lib/CPT/GalaxyGetOpt.pm @ 1:8691c1c61a8e draft default tip

planemo upload commit 94b0cd1fff0826c6db3e7dc0c91c0c5a8be8bb0c
author cpt
date Mon, 05 Jun 2023 02:48:47 +0000
parents
children
comparison
equal deleted inserted replaced
0:54c7a3ea81e2 1:8691c1c61a8e
1 package CPT::GalaxyGetOpt;
2 use CPT::ParameterGroup;
3 use autodie;
4 use File::Spec::Functions qw[ catfile catdir ];
5 use Carp;
6 no warnings;
7 use Moose;
8
9 has 'appdesc' => ( is => 'rw', isa => 'Str', default => sub { 'No application description provided' } );
10 has 'appid' => ( is => 'rw', isa => 'Str', default => sub { "unknown_app_$0" } );
11 has 'appname' => ( is => 'rw', isa => 'Str', default => sub { "Unnamed application $0"} );
12 has 'appvers' => ( is => 'rw', isa => 'Str', default => sub { '0.0.1' } );
13 has 'registered_outputs' => ( is => 'rw', isa => 'HashRef' );
14 has 'opt' => ( is => 'rw', isa => 'Any');
15 has filetype_detector => (
16 is => 'rw',
17 isa => 'Any',
18 );
19
20
21 sub getOptions {
22 my ( $self, %passed_opts ) = @_;
23 require Getopt::Long::Descriptive;
24 # these get pushed through, thankfully. This way we can manually check for --genenrate_galaxy_export and exit slightly earlier.
25 my %args = map { $_ => 1 } @ARGV;
26 # Sections passed to us
27 my @script_args = @{ $passed_opts{'options'} };
28 my %defaults = @{ $passed_opts{'defaults'} };
29
30
31
32 # Output Files Stuff
33 my @getopt_formatted_outputs;
34 my %outputs;
35 # This is originally an array of output files. We transform to a hash for
36 # easier lookup
37 foreach(@{$passed_opts{'outputs'}}){
38 # Grab name/desc/opts
39 my ($name, $desc, $opts) = @{$_};
40 $outputs{$name} = {
41 description => $desc,
42 options => $opts,
43 required => 1,
44 };
45 my %tmp_opts_fix = %{$opts};
46 $tmp_opts_fix{required} = 1;
47 $tmp_opts_fix{validate} = 'File/Output';
48 push(@getopt_formatted_outputs, [$name, $desc, \%tmp_opts_fix]);
49
50 # Adds the {name}_files_path special parameter
51 my %fp_opts = %{$opts};
52 $fp_opts{hidden} = 1;
53 $fp_opts{value} = "${name}.files_path";
54 $fp_opts{default} = "${name}.files_path";
55 $fp_opts{required} = 1;
56 $fp_opts{_galaxy_specific} = 1;
57 $fp_opts{validate} = "String";
58 #$fp_opts{_show_in_galaxy} = 0;
59 my $desc2 = "Associated HTML files for $name";
60 my $name2 = "${name}_files_path";
61 my @files_path = ($name2, $desc2, \%fp_opts);
62 push(@getopt_formatted_outputs, \@files_path);
63
64 # Adds the {name}_format special parameter
65 my %fp_opts3 = %{$opts};
66 #$fp_opts3{hidden} = 1;
67 $fp_opts3{default} = $fp_opts3{"default_format"};
68 $fp_opts3{validate} = "File/OutputFormat";
69 $fp_opts{required} = 1;
70 #$fp_opts3{_galaxy_specific} = 1;
71 #$fp_opts3{_show_in_galaxy} = 0;
72 my $desc3 = "Associated Format for $name";
73 my $name3 = "${name}_format";
74 my @files_path3 = ($name3, $desc3, \%fp_opts3);
75 push(@getopt_formatted_outputs, \@files_path3);
76
77 # Adds the {name}_files_path special parameter
78 my %fp_opts4 = %{$opts};
79 $fp_opts4{hidden} = 1;
80 $fp_opts4{value} = "${name}.id";
81 $fp_opts4{default} = "${name}.id";
82 $fp_opts4{validate} = "String";
83 $fp_opts4{_galaxy_specific} = 1;
84 #$fp_opts4{_show_in_galaxy} = 0;
85 my $desc4 = "Associated ID Number for $name";
86 my $name4 = "${name}_id";
87 my @files_path4 = ($name4, $desc4, \%fp_opts4);
88 push(@getopt_formatted_outputs, \@files_path4);
89
90 }
91 $self->registered_outputs(\%outputs);
92
93
94 # Store the application's name and description
95 if($defaults{appdesc}){
96 $self->appdesc($defaults{appdesc});
97 }
98 if($defaults{appid}){
99 $self->appid($defaults{appid});
100 }
101 if($defaults{appname}){
102 $self->appname($defaults{appname});
103 }
104 if($defaults{appvers}){
105 $self->appvers($defaults{appvers});
106 }
107
108 my $usage_desc;
109 if ( $self->appname() && $self->appdesc() ) {
110 $usage_desc = sprintf( "%s: %s\n%s [options] <some-arg>", $self->appname(), $self->appdesc(), $0 );
111 }
112
113 # Individual parameter parsers
114 require CPT::Parameter;
115 # Which are stored in a collection of them
116 require CPT::ParameterCollection;
117
118
119 my $parameterCollection = CPT::ParameterCollection->new();
120
121 $parameterCollection->push_params(
122 [
123 #['Standard Options'],
124 #@extra_params,
125 #[],
126 ['outfile_supporting' , 'File or folder to output to, necessary when (class == Report || multiple calls to classyReturnResults || multiple standalone output files)' , { hidden => 1, validate => 'String', default => '__new_file_path__', _galaxy_specific => 1, _show_in_galaxy => 0 }],
127 ['galaxy' , 'Run with galaxy-specific overrides' , {validate => 'Flag' , hidden=>1, _galaxy_specific => 1, _show_in_galaxy => 0}],
128 ['generate_galaxy_xml' , 'Generate a compatible galaxy-xml file. May need editing' , {validate => 'Flag' , hidden=>1, _galaxy_specific => 1, _show_in_galaxy => 0}],
129 [],
130 ['Script Options'],
131 ]
132 );
133
134 # If there's a default specified, we should apply that.
135 foreach (@script_args) {
136 # If it's an array, push as is, because either it's old style/label/empty
137 # However, this doesn't allow setting defaults for things that aren't parameters
138 # and it won't set defaults for old style
139 if ( ref $_ eq 'ARRAY' ) {
140 $parameterCollection->push_param($_);
141 }elsif( ref $_ eq 'HASH'){
142 my $pG = CPT::ParameterGroup->new();
143 $pG->set_data($_);
144 $parameterCollection->push_group($pG);
145 }
146 }
147
148 # Our magic output files stuff
149 $parameterCollection->push_params(
150 [
151 [],
152 ['Output Files'],
153 @getopt_formatted_outputs,
154 ]
155 );
156
157 # Other standard options like verbosity/version/etc
158 $parameterCollection->push_params(
159 [
160 [],
161 ['Other Standard Options'],
162 ['verbose|v', 'Be more verbose', {validate => 'Flag', _show_in_galaxy => 0}],
163 ['version', 'Print version information', {validate => 'Flag', _show_in_galaxy => 0}],
164 ['help', 'Print usage message and exit', {validate => 'Flag', _show_in_galaxy => 0}],
165 ],
166 );
167
168 # If we want the galaxy_xml, do that before the reduction step is called.
169 if ( $args{'--version'} ) {
170 print $self->appvers() . "\n";
171 exit 1;
172 }
173
174 # If we want the galaxy_xml, do that before the reduction step is called.
175 if ( $args{'--generate_galaxy_xml'} ) {
176 require CPT::Galaxy;
177 my $galaxy_xml_generator = CPT::Galaxy->new();
178 $galaxy_xml_generator->gen(
179 full_options => $parameterCollection,
180 appdesc => $self->appdesc(),
181 appid => $self->appid(),
182 appname => $self->appname(),
183 appvers => $self->appvers(),
184 defaults => $passed_opts{'defaults'},
185 outputs => $passed_opts{'outputs'},
186 tests => $passed_opts{'tests'},
187 );
188 exit 1;
189 }
190
191 if( $args{'--gen_test'} ){
192 require CPT::GenerateTests;
193 my $tgen = CPT::GenerateTests->new();
194 if(defined $passed_opts{'tests'}){
195 print $tgen->gen(@{$passed_opts{'tests'}});
196 }else{
197 print $tgen->gen_empty();
198 }
199 exit 0;
200 }
201
202 # Now that the options_spec is complete, we reduce to something getopt will be happy with
203 my @getopt_spec = $parameterCollection->getopt();
204 #print STDERR Dumper \@getopt_spec;
205 #exit 1;
206
207 # It would be nice if there was a way to ensure that it didn't die here...
208 # Execute getopt
209 my ( $local_opt, $usage ) = Getopt::Long::Descriptive::describe_options( $usage_desc, @getopt_spec );
210 $self->opt($local_opt);
211
212 # Now that we've gotten the user's options, we need to copy their values back to our ParameterCollection
213 $parameterCollection->populate_from_getopt($self->opt());
214
215 # If they want help, print + exit
216 if ( $self->opt && $self->opt->help ) {
217 print $usage;
218 exit 1;
219 }
220 # Validate their choices
221 if ( $parameterCollection->validate($self->opt) ) {
222 return $self->opt;
223 # Easy access for the script, don't want them to have to deal with PC (just yet)
224 }
225 else {
226 # Some params failed to validate, so we die.
227 croak "Validation errors were found so cannot continue";
228 }
229 }
230
231 no Moose;
232 1;
233
234 __END__
235
236 =pod
237
238 =encoding UTF-8
239
240 =head1 NAME
241
242 CPT::GalaxyGetOpt
243
244 =head1 VERSION
245
246 version 1.99.4
247
248 =head2 getOptions
249
250 my $ggo = CPT::GalaxyGetOpt->new();
251 my $options = $ggo->getOptions(
252 'options' => [
253 [ 'file', 'Input file', { validate => 'File/Input' } ],
254 [
255 "option" => "Select an option!",
256 {
257 validate => 'Option',
258 options => \%options,
259 multiple => 1,
260 }
261 ],
262 [
263 "float" => "I'm a float",
264 { validate => 'Float' }
265 ],
266 [
267 "int" => "I'm an int",
268 { validate => 'Int', default => [42, 34], required => 1, multiple => 1 }
269 ],
270 [],
271 ['New section'],
272 [
273 "string" => "I'm a simple string",
274 { validate => 'String' }
275 ],
276 [
277 'flag' => 'This is a flag',
278 { validate => 'Flag' }
279 ],
280 ],
281 'outputs' => [
282 [
283 'my_output_data',
284 'Output TXT File',
285 {
286 validate => 'File/Output',
287 required => 1,
288 default => 'out',
289 data_format => 'text/plain',
290 default_format => 'TXT',
291 }
292 ],
293 ],
294 'defaults' => [
295 'appid' => 'TemplateScript',
296 'appname' => 'Template',
297 'appdesc' => 'A basic template for a new CPT application',
298 'appvers' => '1.94',
299 ],
300 'tests' => [
301 {
302 test_name => "Default",
303 params => {
304 },
305 outputs => {
306 'my_output_data' => ["out.txt", 'test-data/outputs/template.default.txt' ],
307 },
308 },
309 {
310 test_name => "Option A specified",
311 params => {
312 'int', '10000',
313 },
314 outputs => {
315 'my_output_data' => ["out.txt", 'test-data/outputs/template.A.txt' ],
316 },
317 },
318 ],
319 );
320
321 my @data;
322 foreach(qw(file int string option float flag)){
323 # Create a 2D array of all of our optoins
324 push(@data, [ $_, $options->{$_}]);
325 }
326
327 my %table = (
328 'Sheet1' => {
329 header => [qw(Key Value)],
330 data => \@data,
331 }
332 );
333
334 # And store it to file!
335 use CPT::OutputFiles;
336 my $crr_output = CPT::OutputFiles->new(
337 name => 'my_output_data',
338 GGO => $ggo,
339 );
340 $crr_output->CRR(data => \%table);
341
342 Gets command line options, and prints docs. Very convenient, removes the burden of writing any Getopt code from you.
343
344 =head3 Default Options Provided
345
346 =over 4
347
348 =item *
349
350 help, man - cause an early exit and printing of the POD for your script.
351
352 =item *
353
354 verbose - flag to make the script verbose. Will print everything that is sent to returnResults. Individual scripts should take advantage of this option
355
356 =back
357
358 =head3 Advanced Options Provided
359
360 =over 4
361
362 =item C<--generate_galaxy_xml>
363
364 Generates valid XML for the tool for use in Galaxy
365
366 =item C<--gen_test>
367
368 Generates code to test the script using specified test data
369
370 =back
371
372 =head1 AUTHOR
373
374 Eric Rasche <rasche.eric@yandex.ru>
375
376 =head1 COPYRIGHT AND LICENSE
377
378 This software is Copyright (c) 2014 by Eric Rasche.
379
380 This is free software, licensed under:
381
382 The GNU General Public License, Version 3, June 2007
383
384 =cut