File Coverage

lib/Bio/VertRes/Config/Pipelines/Common.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Bio::VertRes::Config::Pipelines::Common;
2              
3             # ABSTRACT: A set of attributes common to all pipeline config files
4              
5              
6 1     1   166266 use Moose;
  0            
  0            
7             use File::Slurp;
8             use Bio::VertRes::Config::Types;
9             use Data::Dumper;
10             use File::Basename;
11             use File::Path qw(make_path);
12             with 'Bio::VertRes::Config::Pipelines::Roles::RootDatabaseLookup';
13              
14             has 'prefix' => ( is => 'ro', isa => 'Bio::VertRes::Config::Prefix', default => '_' );
15             has 'pipeline_short_name' => ( is => 'ro', isa => 'Str', required => 1 );
16             has 'module' => ( is => 'ro', isa => 'Str', required => 1 );
17             has 'toplevel_action' => ( is => 'ro', isa => 'Str', required => 1 );
18              
19             has 'overwrite_existing_config_file' => ( is => 'ro', isa => 'Bool', default => 0 );
20              
21             has 'log' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_log' );
22             has 'log_base' => ( is => 'ro', isa => 'Str', required => 1 );
23             has 'log_file_name' => ( is => 'ro', isa => 'Str', default => 'logfile.log' );
24              
25             has 'config' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_config' );
26             has 'config_base' => ( is => 'ro', isa => 'Str', required => 1 );
27             has 'config_file_name' => ( is => 'ro', isa => 'Str', default => 'global.conf' );
28              
29             has 'root' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_root' );
30             has 'root_base' => ( is => 'ro', isa => 'Str', required => 1 );
31             has 'root_pipeline_suffix' => ( is => 'ro', isa => 'Str', default => 'seq-pipelines' );
32              
33             has 'database' => ( is => 'ro', isa => 'Str', required => 1 );
34             has 'host' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_host' );
35             has 'port' => ( is => 'ro', isa => 'Int', lazy => 1, builder => '_build_port' );
36             has 'user' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_user' );
37             has 'password' => ( is => 'ro', isa => 'Maybe[Str]', lazy => 1, builder => '_build_password' );
38              
39             has 'database_connect_file' => ( is => 'ro', isa => 'Str', required => 1 );
40             has '_database_connection_details' =>
41             ( is => 'ro', isa => 'Maybe[HashRef]', lazy => 1, builder => '_build__database_connection_details' );
42              
43             sub _build_root {
44             my ($self) = @_;
45             join( '/', ( $self->root_base, $self->root_database_name, $self->root_pipeline_suffix ) );
46             }
47              
48             sub _build_config {
49             my ($self) = @_;
50             my $conf_file_name = join( '_', ( $self->pipeline_short_name, $self->config_file_name ) );
51             join( '/', ( $self->config_base, $self->root_database_name, $self->pipeline_short_name, $conf_file_name ) );
52             }
53              
54             sub _build_log {
55             my ($self) = @_;
56             my $log_file_name = join( '_', ( $self->pipeline_short_name, $self->log_file_name ) );
57             join( '/', ( $self->log_base, $self->root_database_name, $log_file_name ) );
58             }
59              
60             sub _build_host {
61             my ($self) = @_;
62             if ( defined( $self->_database_connection_details ) ) {
63             return $self->_database_connection_details->{host};
64             }
65             return $ENV{VRTRACK_HOST} || 'localhost';
66             }
67              
68             sub _build_port {
69             my ($self) = @_;
70             if ( defined( $self->_database_connection_details ) ) {
71             return $self->_database_connection_details->{port};
72             }
73             return $ENV{VRTRACK_PORT} || 3306;
74             }
75              
76             sub _build_user {
77             my ($self) = @_;
78             if ( defined( $self->_database_connection_details ) ) {
79             return $self->_database_connection_details->{user};
80             }
81             return $ENV{VRTRACK_RW_USER} || 'root';
82             }
83              
84             sub _build_password {
85             my ($self) = @_;
86             if ( defined( $self->_database_connection_details ) ) {
87             return $self->_database_connection_details->{password};
88             }
89             return $ENV{VRTRACK_PASSWORD};
90             }
91              
92             sub _build__database_connection_details {
93             my ($self) = @_;
94             my $connection_details;
95             if ( -f $self->database_connect_file ) {
96             my $text = read_file( $self->database_connect_file );
97             $connection_details = eval($text);
98             }
99             return $connection_details;
100             }
101              
102             sub _limits_values_part_of_filename {
103             my ($self) = @_;
104             my $output_filename = "";
105             my @limit_values;
106             for my $limit_type (qw(project sample library species lane)) {
107             if ( defined $self->limits->{$limit_type} ) {
108             my $list_of_limit_values = $self->limits->{$limit_type};
109             for my $limit_value ( @{$list_of_limit_values} ) {
110             $limit_value =~ s/^\s+|\s+$//g;
111             push( @limit_values, $limit_value );
112              
113             }
114             }
115             }
116             if ( @limit_values > 0 ) {
117             $output_filename = join( '_', @limit_values );
118             }
119             return $output_filename;
120             }
121              
122             sub _filter_characters_truncate_and_add_suffix {
123             my ( $self, $output_filename, $suffix ) = @_;
124             $output_filename =~ s!\W+!_!g;
125             $output_filename =~ s/_$//g;
126             $output_filename =~ s/_+/_/g;
127              
128             if ( length($output_filename) > 150 ) {
129             $output_filename = substr( $output_filename, 0, 146 ) . '_' . int( rand(999) );
130             }
131             return join( '.', ( $output_filename, $suffix ) );
132             }
133              
134             sub create_config_file {
135             my ($self) = @_;
136            
137             my $mode = 0777;
138             if ( !( -e $self->config ) ) {
139             my ( $config_filename, $directories, $suffix ) = fileparse( $self->config );
140             make_path( $directories, {mode => $mode} );
141             }
142              
143             # If the file exists and you dont want to overwrite existing files, skip it
144             return if ( ( -e $self->config ) && $self->overwrite_existing_config_file == 0 );
145              
146             # dont print out an extra wrapper variable
147             $Data::Dumper::Terse = 1;
148             write_file( $self->config, Dumper( $self->to_hash ) );
149             chmod $mode, $self->config;
150             }
151              
152             sub to_hash {
153             my ($self) = @_;
154              
155             my %output_hash = (
156             root => $self->root,
157             module => $self->module,
158             prefix => $self->prefix,
159             log => $self->log,
160             db => {
161             database => $self->database,
162             host => $self->host,
163             port => $self->port,
164             user => $self->user,
165             password => $self->password,
166             },
167             data => {
168             dont_wait => 0,
169             db => {
170             database => $self->database,
171             host => $self->host,
172             port => $self->port,
173             user => $self->user,
174             password => $self->password,
175             },
176             }
177             );
178             return \%output_hash;
179             }
180              
181             no Moose;
182             __PACKAGE__->meta->make_immutable;
183              
184             1;
185              
186             __END__
187              
188             =pod
189              
190             =head1 NAME
191              
192             Bio::VertRes::Config::Pipelines::Common - A set of attributes common to all pipeline config files
193              
194             =head1 VERSION
195              
196             version 1.133090
197              
198             =head1 SYNOPSIS
199              
200             A set of attributes common to all pipeline config files. It is ment to be extended rather than used on its own.
201             use Bio::VertRes::Config::Pipelines::Common;
202             extends 'Bio::VertRes::Config::Pipelines::Common';
203              
204             =head1 AUTHOR
205              
206             Andrew J. Page <ap13@sanger.ac.uk>
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
211              
212             This is free software, licensed under:
213              
214             The GNU General Public License, Version 3, June 2007
215              
216             =cut