Skip to content

Commit

Permalink
feat(refactor): Move sub parse_io_outfiles
Browse files Browse the repository at this point in the history
- Refactor sub and test
- Update import
  • Loading branch information
henrikstranneheim committed Jan 22, 2021
1 parent 42b1ee9 commit a597452
Show file tree
Hide file tree
Showing 81 changed files with 306 additions and 407 deletions.
177 changes: 175 additions & 2 deletions lib/MIP/File_info.pm
Expand Up @@ -5,7 +5,7 @@ use Carp;
use charnames qw{ :full :short };
use English qw{ -no_match_vars };
use File::Basename qw{ basename dirname fileparse };
use File::Spec::Functions qw{ catfile splitpath };
use File::Spec::Functions qw{ catdir catfile splitpath };
use open qw{ :encoding(UTF-8) :std };
use Params::Check qw{ allow check last_error };
use utf8;
Expand All @@ -18,7 +18,7 @@ use List::MoreUtils qw { uniq };
use Readonly;

## MIPs lib/
use MIP::Constants qw{ $GENOME_VERSION $LOG_NAME $NEWLINE $SPACE };
use MIP::Constants qw{ $DOT $EMPTY_STR $GENOME_VERSION $LOG_NAME $NEWLINE $SPACE };

BEGIN {
require Exporter;
Expand All @@ -36,6 +36,7 @@ BEGIN {
get_sample_file_attribute
parse_file_compression_features
parse_files_compression_status
parse_io_outfiles
parse_sample_fastq_file_attributes
parse_select_file_contigs
set_alt_loci_contigs
Expand Down Expand Up @@ -817,6 +818,178 @@ sub parse_files_compression_status {
return;
}

sub parse_io_outfiles {

## Function : Set and get the io files per chain, id and stream
## Returns : %io
## Arguments: $chain_id => Chain of recipe
## : $file_info_href => File info hash {REF}
## : $file_name_prefixes => Build outfile using file name prefix
## : $file_name_prefixes_ref => Build outfile using file name prefixes {REF}
## : $file_paths_ref => File paths {REF}
## : $id => Id (sample or case)
## : $iterators_ref => Build outfile using iterator (e.g contigs) {REF}
## : $outdata_dir => Outdata directory
## : $parameter_href => Parameter hash {REF}
## : $recipe_name => Recipe name
## : $stream => Stream (out)
## : $temp_directory => Temporary directory

my ($arg_href) = @_;

## Flatten argument(s)
my $chain_id;
my $file_info_href;
my $file_name_prefix;
my $file_name_prefixes_ref;
my $file_paths_ref;
my $id;
my $iterators_ref;
my $outdata_dir;
my $parameter_href;
my $recipe_name;
my $temp_directory;

## Default(s)
my $stream;

my $tmpl = {
chain_id => {
defined => 1,
required => 1,
store => \$chain_id,
strict_type => 1,
},
file_info_href => {
default => {},
defined => 1,
required => 1,
store => \$file_info_href,
strict_type => 1,
},
file_name_prefix => {
store => \$file_name_prefix,
strict_type => 1,
},
file_name_prefixes_ref => {
default => [],
store => \$file_name_prefixes_ref,
strict_type => 1,
},
file_paths_ref => {
default => [],
store => \$file_paths_ref,
strict_type => 1,
},
id => {
defined => 1,
required => 1,
store => \$id,
strict_type => 1,
},
iterators_ref => {
default => [],
store => \$iterators_ref,
strict_type => 1,
},
outdata_dir => {
store => \$outdata_dir,
strict_type => 1,
},
parameter_href => {
default => {},
defined => 1,
required => 1,
store => \$parameter_href,
strict_type => 1,
},
recipe_name => {
defined => 1,
required => 1,
store => \$recipe_name,
strict_type => 1,
},
stream => {
allow => [qw{ out }],
default => q{out},
store => \$stream,
strict_type => 1,
},
temp_directory => {
store => \$temp_directory,
strict_type => 1,
},
};

check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};

use MIP::File_info qw{ get_io_files set_io_files };
use MIP::Parameter qw{ get_recipe_attributes };

## Build default @file_paths
if ( not @{$file_paths_ref} and $outdata_dir ) {

my %recipe = get_recipe_attributes(
{
parameter_href => $parameter_href,
recipe_name => $recipe_name,
}
);
my $outfile_tag = $recipe{file_tag} //= $EMPTY_STR;
my $outfile_suffix = $recipe{outfile_suffix} //= $EMPTY_STR;
my $directory = catdir( $outdata_dir, $id, $recipe_name );

## Default paths with iterators
if ( @{$iterators_ref} and $file_name_prefix ) {

## Localize as we will mutate elements
my @iterators = @{$iterators_ref};
foreach my $iterator (@iterators) {

## Add "." if not empty string
$iterator = $iterator ne $EMPTY_STR ? $DOT . $iterator : $iterator;
}
@{$file_paths_ref} =
map { catfile( $directory, $file_name_prefix . $outfile_tag . $_ . $outfile_suffix ) }
@iterators;
}
## Default paths without iterators
else {

## $file_name_prefixes_needs to be set
croak q{Missing argument!} if not @{$file_name_prefixes_ref};
@{$file_paths_ref} =
map { catfile( $directory, $_ . $outfile_tag . $outfile_suffix ) }
@{$file_name_prefixes_ref};
}
}

## Set the io files per chain and stream
set_io_files(
{
chain_id => $chain_id,
file_paths_ref => $file_paths_ref,
file_info_href => $file_info_href,
id => $id,
recipe_name => $recipe_name,
stream => $stream,
temp_directory => $temp_directory,
}
);

my %io = get_io_files(
{
file_info_href => $file_info_href,
id => $id,
parameter_href => $parameter_href,
recipe_name => $recipe_name,
stream => $stream,
}
);

return %io;
}

sub parse_sample_fastq_file_attributes {

## Function : Parse sample fastq file attributes
Expand Down
179 changes: 1 addition & 178 deletions lib/MIP/Parse/File.pm
Expand Up @@ -24,185 +24,8 @@ BEGIN {
use base qw{ Exporter };

# Functions and variables which can be optionally exported
our @EXPORT_OK = qw{ parse_io_outfiles };
our @EXPORT_OK = qw{ };

}

sub parse_io_outfiles {

## Function : Set and get the io files per chain, id and stream
## Returns : %io
## Arguments: $chain_id => Chain of recipe
## : $file_info_href => File info hash {REF}
## : $file_name_prefixes => Build outfile using file name prefix
## : $file_name_prefixes_ref => Build outfile using file name prefixes {REF}
## : $file_paths_ref => File paths {REF}
## : $id => Id (sample or case)
## : $iterators_ref => Build outfile using iterator (e.g contigs) {REF}
## : $outdata_dir => Outdata directory
## : $parameter_href => Parameter hash {REF}
## : $recipe_name => Recipe name
## : $stream => Stream (out)
## : $temp_directory => Temporary directory

my ($arg_href) = @_;

## Flatten argument(s)
my $chain_id;
my $file_info_href;
my $file_name_prefix;
my $file_name_prefixes_ref;
my $file_paths_ref;
my $id;
my $iterators_ref;
my $outdata_dir;
my $parameter_href;
my $recipe_name;
my $temp_directory;

## Default(s)
my $stream;

my $tmpl = {
chain_id => {
defined => 1,
required => 1,
store => \$chain_id,
strict_type => 1,
},
file_info_href => {
default => {},
defined => 1,
required => 1,
store => \$file_info_href,
strict_type => 1,
},
file_name_prefix => {
store => \$file_name_prefix,
strict_type => 1,
},
file_name_prefixes_ref => {
default => [],
store => \$file_name_prefixes_ref,
strict_type => 1,
},
file_paths_ref => {
default => [],
store => \$file_paths_ref,
strict_type => 1,
},
id => {
defined => 1,
required => 1,
store => \$id,
strict_type => 1,
},
iterators_ref => {
default => [],
store => \$iterators_ref,
strict_type => 1,
},
outdata_dir => {
store => \$outdata_dir,
strict_type => 1,
},
parameter_href => {
default => {},
defined => 1,
required => 1,
store => \$parameter_href,
strict_type => 1,
},
recipe_name => {
defined => 1,
required => 1,
store => \$recipe_name,
strict_type => 1,
},
stream => {
allow => [qw{ out }],
default => q{out},
store => \$stream,
strict_type => 1,
},
temp_directory => {
store => \$temp_directory,
strict_type => 1,
},
};

check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!};

use MIP::File_info qw{ get_io_files set_io_files };
use MIP::Parameter qw{ get_recipe_attributes };

my @file_paths = @{$file_paths_ref};

## Build default @file_paths
if ( not @file_paths and $outdata_dir ) {

my %rec_atr = get_recipe_attributes(
{
parameter_href => $parameter_href,
recipe_name => $recipe_name,
}
);
my $outfile_tag = $rec_atr{file_tag} //= $EMPTY_STR;
my $outfile_suffix = $rec_atr{outfile_suffix} //= $EMPTY_STR;
my $directory = catdir( $outdata_dir, $id, $recipe_name );

## Default paths with iterators
if ( @{$iterators_ref} and $file_name_prefix ) {

## Localize as we will mutate elements
my @iterators = @{$iterators_ref};
foreach my $iterator (@iterators) {

## Add "." if not empty string
if ( $iterator ne $EMPTY_STR ) {

$iterator = $DOT . $iterator;
}
}
@file_paths =
map { catfile( $directory, $file_name_prefix . $outfile_tag . $_ . $outfile_suffix ) }
@iterators;
}
## Default paths without iterators
else {

## $file_name_prefixes_needs to be set
croak q{Missing argument!} if not @{$file_name_prefixes_ref};
@file_paths =
map { catfile( $directory, $_ . $outfile_tag . $outfile_suffix ) }
@{$file_name_prefixes_ref};
}
}

## Set the io files per chain and stream
set_io_files(
{
chain_id => $chain_id,
id => $id,
file_paths_ref => \@file_paths,
file_info_href => $file_info_href,
recipe_name => $recipe_name,
stream => $stream,
temp_directory => $temp_directory,
}
);

my %io = get_io_files(
{
id => $id,
file_info_href => $file_info_href,
parameter_href => $parameter_href,
recipe_name => $recipe_name,
stream => $stream,
}
);

return %io;
}

1;

0 comments on commit a597452

Please sign in to comment.