« lib/BioX/Workflow/Command/run/Rules/Directives/Walk.pm
↑ Summary
lib/BioX/Workflow/Command/Utils/Files.pm »
| 1 | package BioX::Workflow::Command::stats; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use MooseX::App::Command; | ||
| 5 | use namespace::autoclean; | ||
| 6 | |||
| 7 | use Log::Log4perl qw(:easy); | ||
| 8 | use DateTime; | ||
| 9 | use Text::ASCIITable; | ||
| 10 | use Number::Bytes::Human qw(format_bytes parse_bytes); | ||
| 11 | use File::Details; | ||
| 12 | use File::Basename; | ||
| 13 | use List::Util qw(uniq); | ||
| 14 | use Try::Tiny; | ||
| 15 | use Path::Tiny; | ||
| 16 | |||
| 17 | extends qw( BioX::Workflow::Command ); | ||
| 18 | |||
| 19 | use BioSAILs::Utils::Traits qw(ArrayRefOfStrs); | ||
| 20 | |||
| 21 | use BioX::Workflow::Command::run::Rules::Directives; | ||
| 22 | |||
| 23 | with 'BioX::Workflow::Command::Utils::Log'; | ||
| 24 | with 'BioX::Workflow::Command::run::Utils::Samples'; | ||
| 25 | with 'BioX::Workflow::Command::run::Utils::Attributes'; | ||
| 26 | with 'BioX::Workflow::Command::run::Rules::Rules'; | ||
| 27 | with 'BioX::Workflow::Command::run::Utils::WriteMeta'; | ||
| 28 | with 'BioX::Workflow::Command::run::Utils::Files::TrackChanges'; | ||
| 29 | with 'BioX::Workflow::Command::run::Utils::Files::ResolveDeps'; | ||
| 30 | with 'BioX::Workflow::Command::Utils::Files'; | ||
| 31 | with 'BioX::Workflow::Command::Utils::Log'; | ||
| 32 | |||
| 33 | command_short_description 'Get the status of INPUT/OUTPUT for your workflow'; | ||
| 34 | command_long_description | ||
| 35 | 'If you are unsure on where you are in your workflow, run this step. ' | ||
| 36 | . 'It will give you a breakdown of rules with associated files, ' | ||
| 37 | . 'and whether or not they have been created or modified. '; | ||
| 38 | |||
| 39 | has 'table_log' => ( | ||
| 40 | is => 'rw', | ||
| 41 | default => sub { | ||
| 42 | my $self = shift; | ||
| 43 | my $t = Text::ASCIITable->new(); | ||
| 44 | $t->setCols( [ 'Rule', 'Sample', 'I/O', 'File', 'Exists', 'Size' ] ); | ||
| 45 | return $t; | ||
| 46 | } | ||
| 47 | ); | ||
| 48 | |||
| 49 | option 'use_abs' => ( | ||
| 50 | is => 'rw', | ||
| 51 | isa => 'Bool', | ||
| 52 | default => 0, | ||
| 53 | documentation => 'Use the absolute path name instead of the basename' | ||
| 54 | ); | ||
| 55 | |||
| 56 | our $human = Number::Bytes::Human->new( | ||
| 57 | bs => 1024, | ||
| 58 | round_style => 'round', | ||
| 59 | precision => 2 | ||
| 60 | ); | ||
| 61 | |||
| 62 | has 'add_row' => ( | ||
| 63 | is => 'rw', | ||
| 64 | default => 0, | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub execute { | ||
| 68 | my $self = shift; | ||
| 69 | |||
| 70 | $self->stdout(1); | ||
| 71 | if ( !$self->load_yaml_workflow ) { | ||
| 72 | $self->app_log->warn('Exiting now.'); | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | $self->apply_global_attributes; | ||
| 76 | $self->get_global_keys; | ||
| 77 | $self->get_samples; | ||
| 78 | |||
| 79 | $self->iterate_rules; | ||
| 80 | say $self->table_log; | ||
| 81 | } | ||
| 82 | |||
| 83 | around 'pre_FILES' => sub { | ||
| 84 | my $orig = shift; | ||
| 85 | my $self = shift; | ||
| 86 | my $attr = shift; | ||
| 87 | my $cond = shift; | ||
| 88 | |||
| 89 | my $index = | ||
| 90 | $self->first_index_select_rule_keys( sub { $_ eq $self->rule_name } ); | ||
| 91 | return if $index == -1; | ||
| 92 | |||
| 93 | $self->$orig( $attr, $cond ); | ||
| 94 | |||
| 95 | return unless $self->has_files; | ||
| 96 | return unless $self->files; | ||
| 97 | |||
| 98 | $DB::single = 2; | ||
| 99 | |||
| 100 | for my $file ( $self->all_files ) { | ||
| 101 | $self->preprocess_row( $file, $cond ); | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | |||
| 105 | sub preprocess_row { | ||
| 106 | my $self = shift; | ||
| 107 | my $file = shift; | ||
| 108 | my $cond = shift; | ||
| 109 | |||
| 110 | $self->iter_file_samples( $file, $cond ); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub gen_row { | ||
| 114 | my $self = shift; | ||
| 115 | my $file = shift; | ||
| 116 | my $cond = shift; | ||
| 117 | my $sample = shift; | ||
| 118 | my $sample_files = shift; | ||
| 119 | |||
| 120 | foreach my $file ( @{$sample_files} ) { | ||
| 121 | |||
| 122 | my @trow = (); | ||
| 123 | |||
| 124 | push( @trow, $self->rule_name ); | ||
| 125 | push( @trow, $sample ); | ||
| 126 | push( @trow, $cond ); | ||
| 127 | |||
| 128 | my $rel = ''; | ||
| 129 | $rel = $file; | ||
| 130 | $rel = path($file)->absolute if $self->use_abs; | ||
| 131 | |||
| 132 | my $basename = basename($file) unless $self->use_abs; | ||
| 133 | |||
| 134 | #Add the filename | ||
| 135 | push( @trow, $rel ) if $self->use_abs; | ||
| 136 | push( @trow, $basename ) if !$self->use_abs; | ||
| 137 | |||
| 138 | #Does the file exist? | ||
| 139 | if ( -e $file ) { | ||
| 140 | push( @trow, 1 ); | ||
| 141 | |||
| 142 | #File Size | ||
| 143 | my $details = File::Details->new($file); | ||
| 144 | my $hsize = $human->format( $details->size ); | ||
| 145 | push( @trow, $hsize ); | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | push( @trow, 0 ); | ||
| 149 | push( @trow, '' ); | ||
| 150 | } | ||
| 151 | $self->table_log->addRow( \@trow ); | ||
| 152 | $self->add_row(1); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub iter_file_samples { | ||
| 157 | my $self = shift; | ||
| 158 | my $file = shift; | ||
| 159 | my $cond = shift; | ||
| 160 | |||
| 161 | my $dummy_sample = $self->dummy_sample; | ||
| 162 | |||
| 163 | foreach my $sample ( $self->all_samples ) { | ||
| 164 | my @sample_files = (); | ||
| 165 | my $new_file = $file; | ||
| 166 | $new_file =~ s/$dummy_sample/$sample/g; | ||
| 167 | my $chunk_files = $self->iter_file_chunks($new_file); | ||
| 168 | |||
| 169 | if ($chunk_files) { | ||
| 170 | map { push( @sample_files, $_ ) } @{$chunk_files}; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | push( @sample_files, $new_file ); | ||
| 174 | } | ||
| 175 | @sample_files = uniq(@sample_files); | ||
| 176 | $self->gen_row( $file, $cond, $sample, \@sample_files ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | sub iter_file_chunks { | ||
| 181 | my $self = shift; | ||
| 182 | my $file = shift; | ||
| 183 | |||
| 184 | my @files = (); | ||
| 185 | my $use_iter = $self->use_iterables; | ||
| 186 | |||
| 187 | return 0 if !$use_iter; | ||
| 188 | |||
| 189 | my $all = $use_iter->[0]; | ||
| 190 | my $elem = $use_iter->[1]; | ||
| 191 | |||
| 192 | my $dummy_iter = $self->dummy_iterable; | ||
| 193 | foreach my $chunk ( $self->local_attr->$all ) { | ||
| 194 | my $new_file = $file; | ||
| 195 | $new_file =~ s/$dummy_iter/$chunk/g; | ||
| 196 | push( @files, $new_file ); | ||
| 197 | } | ||
| 198 | return \@files; | ||
| 199 | } | ||
| 200 | |||
| 201 | after 'template_process' => sub { | ||
| 202 | my $self = shift; | ||
| 203 | |||
| 204 | return unless $self->add_row; | ||
| 205 | try { | ||
| 206 | $self->table_log->addRowLine(); | ||
| 207 | } | ||
| 208 | }; | ||
| 209 | |||
| 210 | around 'print_process_workflow' => sub { | ||
| 211 | }; | ||
| 212 | |||
| 213 | __PACKAGE__->meta->make_immutable; | ||
| 214 | |||
| 215 | 1; |