| 1 | #!/usr/bin/env perl | ||
| 2 | # created on 2015-01-22 | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.010; | ||
| 7 | |||
| 8 | use Bio::Gonzales::Matrix::IO qw(:DEFAULT xlsx_slurp); | ||
| 9 | |||
| 10 | use Pod::Usage; | ||
| 11 | use Getopt::Long; | ||
| 12 | use File::Spec; | ||
| 13 | |||
| 14 | my %opt = ( suffix => '.tsv', na_value => 'NA' ); | ||
| 15 | GetOptions( \%opt, 'help', 'stdout|c', 'na_value=s', 'overwrite|f', 'index|i=i', 'suffix=s' ) or pod2usage(2); | ||
| 16 | |||
| 17 | pod2usage( -exitval => 0, -verbose => 2 ) if ( $opt{help} ); | ||
| 18 | pod2usage(2) | ||
| 19 | unless ( @ARGV && @ARGV > 0 ); | ||
| 20 | |||
| 21 | my $in_f = shift; | ||
| 22 | my $prefix = shift; | ||
| 23 | |||
| 24 | unless ($prefix) { | ||
| 25 | $prefix = $in_f; | ||
| 26 | $prefix =~ s/\..*?$//; | ||
| 27 | } | ||
| 28 | |||
| 29 | my $d = xlsx_slurp($in_f); | ||
| 30 | |||
| 31 | if ( $opt{index} && $opt{'index'} > 0 && @$d >= $opt{'index'} ) { | ||
| 32 | say STDERR "out pattern: $prefix$opt{suffix}"; | ||
| 33 | my $idx = $opt{index} - 1; | ||
| 34 | my $res_file = $prefix . $opt{suffix}; | ||
| 35 | if ( $opt{stdout} ) { | ||
| 36 | $res_file = \*STDOUT; | ||
| 37 | } else { | ||
| 38 | |||
| 39 | die("$res_file exists") if ( -e $res_file && !$opt{overwrite} ); | ||
| 40 | } | ||
| 41 | clean($d->[$idx]); | ||
| 42 | mspew( $res_file, $d->[$idx], { na_value => $opt{na_value} } ); | ||
| 43 | } else { | ||
| 44 | say STDERR "out pattern: $prefix<NUM>$opt{suffix}"; | ||
| 45 | my $i = 1; | ||
| 46 | for my $s (@$d) { | ||
| 47 | clean($s); | ||
| 48 | if ( @$s > 0 ) { | ||
| 49 | my $res_file = $prefix . $i . $opt{suffix}; | ||
| 50 | die("$res_file exists") if ( -e $res_file && !$opt{overwrite} ); | ||
| 51 | mspew( $res_file, $s, { na_value => $opt{na_value} } ); | ||
| 52 | } | ||
| 53 | $i++; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub clean { | ||
| 58 | my $d = shift; | ||
| 59 | for my $x (@$d) { | ||
| 60 | for my $y (@$x) { | ||
| 61 | $y =~ y/\r\n//d if($y); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | __END__ | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | |||
| 71 | bg_xlsx2tsv - convert xlsx files to tsv format | ||
| 72 | |||
| 73 | =head1 SYNOPSIS | ||
| 74 | |||
| 75 | sample [OPTIONS] FILE PREFIX | ||
| 76 | |||
| 77 | Options: | ||
| 78 | |||
| 79 | --help brief help message | ||
| 80 | --index|i=INT only extract this sheet (1-based) | ||
| 81 | --suffix=STRING output file suffix | ||
| 82 | --stdout|c output to stdout (requires index option) | ||
| 83 | |||
| 84 | =head1 SEE ALSO | ||
| 85 | |||
| 86 | =head1 AUTHOR | ||
| 87 | |||
| 88 | jw bargsten, C<< <jwb at cpan dot org> >> | ||
| 89 | |||
| 90 | =cut |