line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Reorder::TSV; ## no critic (RequireTidyCode) |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
143272
|
use strictures 2; |
|
1
|
|
|
|
|
17
|
|
|
1
|
|
|
|
|
46
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.1.0'; ## VERSION |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# ABSTRACT: Reorder columns of TSV file by template |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
272
|
use autodie; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
10
|
1
|
|
|
1
|
|
5553
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
64
|
|
11
|
1
|
|
|
1
|
|
599
|
use IO::Uncompress::Gunzip qw($GunzipError); |
|
1
|
|
|
|
|
38245
|
|
|
1
|
|
|
|
|
108
|
|
12
|
1
|
|
|
1
|
|
9
|
use Exporter qw( import ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
870
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw( reorder ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub reorder { |
16
|
15
|
|
|
15
|
1
|
22473
|
my ($arg_ref) = @_; |
17
|
|
|
|
|
|
|
|
18
|
15
|
100
|
|
|
|
60
|
confess 'TSV argument missing' if !defined $arg_ref->{tsv}; |
19
|
14
|
100
|
|
|
|
39
|
confess 'Template argument missing' if !defined $arg_ref->{template}; |
20
|
|
|
|
|
|
|
|
21
|
13
|
|
|
|
|
28
|
my $tsv_fh = _open_tsv( $arg_ref->{tsv} ); |
22
|
11
|
|
|
|
|
32
|
my $template_fh = _open_template( $arg_ref->{template} ); |
23
|
|
|
|
|
|
|
|
24
|
10
|
|
|
|
|
24
|
my @new_cols = _get_cols($template_fh); |
25
|
10
|
|
|
|
|
22
|
my @old_cols = _get_cols($tsv_fh); |
26
|
10
|
|
|
|
|
42
|
_output_reorder( $arg_ref->{fh}, $tsv_fh, \@new_cols, \@old_cols ); |
27
|
|
|
|
|
|
|
|
28
|
10
|
|
|
|
|
42
|
close $tsv_fh; |
29
|
10
|
|
|
|
|
1633
|
close $template_fh; |
30
|
|
|
|
|
|
|
|
31
|
10
|
|
|
|
|
511
|
return; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _open_tsv { |
35
|
13
|
|
|
13
|
|
18
|
my ($tsv) = @_; |
36
|
|
|
|
|
|
|
|
37
|
13
|
100
|
|
|
|
252
|
confess sprintf 'Input TSV file does not exist (%s)', $tsv if !-e $tsv; |
38
|
12
|
|
|
|
|
32
|
my $fh; |
39
|
12
|
100
|
|
|
|
49
|
if ( $tsv =~ m/[.]gz \z/xms ) { |
40
|
3
|
100
|
|
|
|
27
|
$fh = IO::Uncompress::Gunzip->new( |
41
|
|
|
|
|
|
|
$tsv, |
42
|
|
|
|
|
|
|
MultiStream => 1, |
43
|
|
|
|
|
|
|
Transparent => 0 |
44
|
|
|
|
|
|
|
) or confess sprintf 'gunzip failed (%s): %s', $tsv, $GunzipError; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
9
|
|
|
|
|
35
|
open $fh, q{<}, $tsv; ## no critic (RequireBriefOpen) |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
11
|
|
|
|
|
7345
|
return $fh; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _open_template { |
54
|
11
|
|
|
11
|
|
23
|
my ($template) = @_; |
55
|
|
|
|
|
|
|
|
56
|
11
|
100
|
|
|
|
193
|
confess sprintf 'Template TSV does not exist (%s)', $template |
57
|
|
|
|
|
|
|
if !-e $template; |
58
|
10
|
|
|
|
|
49
|
open my $fh, q{<}, $template; |
59
|
|
|
|
|
|
|
|
60
|
10
|
|
|
|
|
1258
|
return $fh; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _get_cols { |
64
|
20
|
|
|
20
|
|
33
|
my ($fh) = @_; |
65
|
|
|
|
|
|
|
|
66
|
20
|
|
|
|
|
285
|
my $line = <$fh>; |
67
|
20
|
|
|
|
|
221
|
chomp $line; |
68
|
|
|
|
|
|
|
|
69
|
20
|
|
|
|
|
83
|
my @cols = split /\t/xms, $line; |
70
|
|
|
|
|
|
|
|
71
|
20
|
|
|
|
|
78
|
return @cols; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _output_reorder { |
75
|
10
|
|
|
10
|
|
27
|
my ( $fh, $tsv_fh, $new_cols, $old_cols ) = @_; |
76
|
|
|
|
|
|
|
|
77
|
10
|
|
|
|
|
13
|
_write_line( $fh, @{$new_cols} ); # Header |
|
10
|
|
|
|
|
29
|
|
78
|
|
|
|
|
|
|
|
79
|
10
|
|
|
|
|
48
|
while ( my $line = <$tsv_fh> ) { |
80
|
16
|
|
|
|
|
306
|
chomp $line; |
81
|
16
|
|
|
|
|
44
|
my @fields = split /\t/xms, $line; |
82
|
|
|
|
|
|
|
|
83
|
16
|
|
|
|
|
27
|
my %value_for; |
84
|
16
|
|
|
|
|
20
|
foreach my $i ( 0 .. ( scalar @{$old_cols} ) - 1 ) { |
|
16
|
|
|
|
|
43
|
|
85
|
64
|
|
|
|
|
141
|
$value_for{ $old_cols->[$i] } = $fields[$i]; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
16
|
|
|
|
|
25
|
my @output; |
89
|
16
|
|
|
|
|
20
|
foreach my $new_col ( @{$new_cols} ) { |
|
16
|
|
|
|
|
26
|
|
90
|
64
|
|
100
|
|
|
168
|
push @output, $value_for{$new_col} || q{}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
16
|
|
|
|
|
32
|
_write_line( $fh, @output ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
10
|
|
|
|
|
49
|
return; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _write_line { |
100
|
26
|
|
|
26
|
|
66
|
my ( $fh, @fields ) = @_; |
101
|
|
|
|
|
|
|
|
102
|
26
|
100
|
|
|
|
56
|
if ( !defined $fh ) { |
103
|
13
|
|
|
|
|
23
|
$fh = \*STDOUT; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
26
|
|
|
|
|
35
|
printf {$fh} "%s\n", join "\t", @fields; |
|
26
|
|
|
|
|
394
|
|
107
|
|
|
|
|
|
|
|
108
|
26
|
|
|
|
|
189
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__END__ |