bin/gonz_unmap.pl

filter: Statementstmt 85.7% 54 / 63 Branchbran 44.7% 17 / 38 Conditioncond 25.0% 5 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 59.7% 83 / 139 CC 1 SCAR 1.2
CC
1
Cov
63%
__ANON__
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Bio/Gonzales/Util/Cerial.pm ↑ Summary blib/lib/Bio/Gonzales.pm »
1 #!/usr/bin/env perl
2 # created on 2013-12-19
3  
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Bio::Gonzales::Matrix::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Bio::Gonzales::Util::File qw/gonzopen/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Getopt::Long qw(:config);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 my %opt = ();
15 2 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, "skip_na", "comment=s", "keep", "header|h", "col|m=i\@", "filter=s", "na_prefix:s", "na_suffix:s", "from|k=i", "to|v=i", "multi", "help|?") or pod2usage(2) 0 2 2
16 \%opt, 'skip_na', 'comment=s', 'keep', 'header|h', 'col|m=i@',
17 'filter=s', 'na_prefix:s', 'na_suffix:s', 'from|k=i', 'to|v=i', 'multi', 'help|?'
18 ) or pod2usage(2);
19 2 ▶pod2usage(1) if($opt{help});
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 2 2
20  
21 2 my ( $map_fn, $data_fn, $out_fn ) = @ARGV;
22 2 ▶pod2usage("$map_fn is no file") unless ( -f $map_fn );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $map_fn 0 2 2
23 2 ▶pod2usage("$data_fn is no file") unless ( -f $data_fn || $data_fn eq '-' );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $data_fn or $data_fn eq "-" 0 2 2
Condition33%1/3
-f $data_fn or $data_fn eq "-"
l!l&&r!l&&!r
200
MC/DC0%0/2
-f $data_fn or $data_fn eq "-"
-f $data_fn $data_fn eq "-"
Truth table33%1/3
A or B
A= -f $data_fnB= $data_fn eq "-"
ABresult
1X1
011
000
24 2 ▶pod2usage("no output file (- for stdout)") unless ($out_fn);
Branch: 1 branch
BranchTrueFalseTotal
unless $out_fn 0 2 2
25 2 ▶pod2usage("no columns selected") unless ( exists( $opt{from} ) && exists( $opt{to} ) && exists( $opt{col} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"from"} and exists $opt{"to"} and exists $opt{"col"} 0 2 2
Condition33%2/6
exists $opt{"from"} and exists $opt{"to"} and exists $opt{"col"}
!ll&&!rl&&r
002
exists $opt{"from"} and exists $opt{"to"}
!ll&&!rl&&r
002
MC/DC0%0/3
exists $opt{"from"} and exists $opt{"to"} and exists $opt{"col"}
exists $opt{"from"} exists $opt{"to"} exists $opt{"col"}
Truth table0%0/4
A and B and C
A= exists $opt{"from"}B= exists $opt{"to"}C= exists $opt{"col"}
ABCresult
0XX0
10X0
1100
1111
26  
27 2 ▶$out_fn = \*STDOUT if ( $out_fn eq '-' );
Branch: 1 branch
BranchTrueFalseTotal
if $out_fn eq "-" 0 2 2
28 2 ▶$data_fn = \*STDIN if ( $data_fn eq '-' );
Branch: 1 branch
BranchTrueFalseTotal
if $data_fn eq "-" 0 2 2
29  
30 2 my $filter;
31 2 ▶if ( $opt{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"filter"}) 0 2 2
32 $filter = sub {
33 0 ▶ return $_[0] =~ /$opt{filter}/;
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 }
35 0 }
36  
37 my $map = dict_slurp( $map_fn,
38 2 { key_idx => $opt{from}, val_idx => $opt{to}, uniq => 0, record_filter => $filter } );
39  
40 2 my $fh = gonzopen( $data_fn, '<' );
41 2 my $out_fh = gonzopen( $out_fn, '>' );
42  
43 2 ▶if ( $opt{header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"header"}) 0 2 2
44 0 print $out_fh scalar <$fh>;
45 }
46 2 my $keep = 0;
47  
48 2 ▶$keep = 1 if ( $opt{keep} );
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"keep"} 0 2 2
49  
50 ROW:
51 2 ▶while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 2 2 4
52 2 ▶ print $out_fh $_ and next if ( $opt{comment} && /^$opt{comment}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"comment"} and /^$opt{"comment"}/ 0 2 2
Condition25%2/8
$opt{'comment'} && /^$opt{'comment'}/ && (print($out_fh $_) && (next))
!ll&&!rl&&r
200
$opt{"comment"} and /^$opt{"comment"}/
!ll&&!rl&&r
200
print $out_fh $_ and next
!ll
00
MC/DC0%0/3
$opt{'comment'} && /^$opt{'comment'}/ && (print($out_fh $_) && (next))
$opt{"comment"} /^$opt{"comment"}/ print $out_fh $_
Truth table0%0/4
A and B && C
A= $opt{"comment"}B= /^$opt{"comment"}/C= print $out_fh $_
ABCresult
0XX0
10X0
1100
1111
53 2 chomp;
54 2 my @f = split /\t/;
55 2 for my $c ( @{ $opt{col} } ) {
56 2 my $id = $f[$c];
57  
58 2 ▶ unless ( exists $map->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map->{$id}) { } 2 0 2
59  
60 0 ▶ next ROW if ( $opt{skip_na} );
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"skip_na"} 0 0 0
61  
62 die "could not map $id ($data_fn)"
63 0 ▶ unless ( exists( $opt{na_suffix} ) || exists( $opt{na_prefix} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"na_suffix"} or exists $opt{"na_prefix"} 0 0 0
Condition0%0/3
exists $opt{"na_suffix"} or exists $opt{"na_prefix"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $opt{"na_suffix"} or exists $opt{"na_prefix"}
exists $opt{"na_suffix"} exists $opt{"na_prefix"}
Truth table0%0/3
A or B
A= exists $opt{"na_suffix"}B= exists $opt{"na_prefix"}
ABresult
1X1
011
000
64  
65 0 ▶ $id .= $opt{na_suffix} if ( exists( $opt{na_suffix} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"na_suffix"} 0 0 0
66 0 ▶ $id = $opt{na_prefix} . $id if ( exists( $opt{na_prefix} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"na_prefix"} 0 0 0
67 0 splice @f, $c + $keep, 1 - $keep, $id;
68 0 say $out_fh join "\t", @f;
69 } else {
70 2 for my $v ( @{ $map->{$id} } ) {
71 3 my @ff = @f;
72 3 splice @ff, $c + $keep, 1 - $keep, $v;
73 3 say $out_fh join "\t", @ff;
74 3 ▶ last unless ( $opt{multi} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"multi"} 1 2 3
75 }
76 }
77 }
78 }
79 2 $fh->close;
80 2 $out_fh->close;
81  
82 __END__
83  
84 =head1 NAME
85  
86 gonz_unmap.pl - unmap or remap identifiers/strings using a given id mapping
87  
88 =head1 SYNOPSIS
89  
90 Usage: gonz_unmap.pl [OPTIONS] <map_file> <input_file> <output_file>
91  
92 gonz_unmap.pl will (un)map or remap identifiers of tables using a given id
93 mapping. All input files are exptected to be tab-separated. Column numbers
94 are zero-based.
95  
96 Examples:
97 gonz_unmap.pl --from 0 --to 1 --col 0 map.tsv data.tsv data.unmapped.tsv
98 # remaps column 0 (the 1st column) of data.tsv using the mapping of
99 # column 0 -> column 1 in map.tsv and saves it to data.unmapped.tsv.
100  
101 OPTIONS:
102  
103 -?, --help show the full help with all options explained
104  
105 -k, --from
106 -v, --to
107 -m, --col=<NUMBER>
108 --skip_na
109 --comment=<REGEX>
110 --keep
111 -h, --header
112 --filter=<REGEX>
113 --na_prefix=<STRING>
114 --na_suffix=<STRING>
115 --multi
116
117 =head1 SEE ALSO
118  
119 =head1 OPTIONS
120  
121 =over 4
122  
123 =item B<< --from >>
124  
125 The column to match the ids against
126  
127 =item B<< --to >>
128  
129 If the C<from> column has a match, output the id in column C<to>.
130  
131 =item B<< --col >>
132  
133 The columns that should be mapped. Can be used multiple times to map multiple
134 columns with the same mapping at the same time.
135  
136 =item B<< --skip_na >>
137  
138 Do only output sucessfully mapped ids/strings.
139  
140 (usually C<gonz_unmap.pl> dies if a mapping fails)
141  
142 =item B<< --comment=<REGEX> >>
143  
144 The data input has comments of the form C<< <REGEX> >>, just copy it directly
145 to the output.
146  
147 =item B<< --keep >>
148  
149 Do not replace the ids in the C<col>-column, but add a new column after it
150 with the mapped ids.
151  
152 =item B<< --header >>
153  
154 The data file has a header, just copy it directly to the output.
155  
156 =item B<< --filter=<REGEX> >>
157  
158 Prefilter the input data by C< <REGEX> >
159  
160 =item B<< --na_prefix=<STRING> >>
161  
162 Prefix the original ids with C<< <STRING> >> and take the prefixed version as
163 mapped id.
164  
165 (usually C<gonz_unmap.pl> dies if a mapping fails)
166  
167 =item B<< --na_suffix >>
168  
169 Add the suffix C<< <STRING> >> the original ids and take the suffixed version
170 as mapped id.
171  
172 (usually C<gonz_unmap.pl> dies if a mapping fails)
173  
174 =item B<< --multi >>
175  
176 If one id (from) maps to multiple ids (to), output all mappings. Default is to
177 take the last encountered (from,to)-pair as mapping entry.
178  
179 =back
180  
181 =head1 AUTHOR
182  
183 jw bargsten, C<< <jwb at cpan dot org> >>
184  
185 =cut
« blib/lib/Bio/Gonzales/Util/Cerial.pm ↑ Summary blib/lib/Bio/Gonzales.pm »