| 1 | package App::Dazz::Command::dazzname; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use App::Dazz -command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use App::Dazz::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use constant abstract => 'rename FASTA reads for dazz_db'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | return ( | ||||||||||||||||||||||||||||
| 13 | 7 | [ "outfile|o=s", "output filename, [stdout] for screen" ], | |||||||||||||||||||||||||||
| 14 | [ "prefix=s", "prefix of names", { default => "read" }, ], | ||||||||||||||||||||||||||||
| 15 | [ "start=i", "start index", { default => 1 }, ], | ||||||||||||||||||||||||||||
| 16 | { show_defaults => 1, } | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 7 | ▶ | sub usage_desc { | ||||||||||||||||||||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 7 | return "dazz dazzname [options] <infile>"; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub description { | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 1 | my $desc; | |||||||||||||||||||||||||||
| 26 | 1 | $desc .= ucfirst(abstract) . ".\n"; | |||||||||||||||||||||||||||
| 27 | 1 | $desc .= "\tinfile == stdin means reading from STDIN\n"; | |||||||||||||||||||||||||||
| 28 | 1 | return $desc; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | ▶ | sub validate_args { | ||||||||||||||||||||||||||
|
Subroutine
validate_args: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 6 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | if ( @{$args} != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1 | my $message = "This command need one input file.\n\tIt found"; | |||||||||||||||||||||||||||
| 36 | 1 | $message .= sprintf " [%s]", $_ for @{$args}; | |||||||||||||||||||||||||||
| 37 | 1 | $message .= ".\n"; | |||||||||||||||||||||||||||
| 38 | 1 | $self->usage_error($message); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 5 | for ( @{$args} ) { | |||||||||||||||||||||||||||
| 41 | 5 | ▶ | next if lc $_ eq "stdin"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | if ( !Path::Tiny::path($_)->is_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | $self->usage_error("The input file [$_] doesn't exist."); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | if ( $opt->{prefix} =~ /\W/xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | $self->usage_error("Can't accept prefix with space or non-word characters."); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if ( $opt->{outfile} and $opt->{outfile} =~ /[^\w\.]/xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opt->{"outfile"} and $opt->{"outfile"} =~ /[^\w\.]/msx
MC/DC50%1/2
$opt->{"outfile"} and $opt->{"outfile"} =~ /[^\w\.]/msx
$opt->{"outfile"}
$opt->{"outfile"} =~ /[^\w\.]/msx
Truth table66%2/3
A and B
A= $opt->{"outfile"}B= $opt->{"outfile"} =~ /[^\w\.]/msx
| |||||||||||||||||||||||||||||
| 52 | 1 | $self->usage_error("Can't accept outfile with space or non-standard characters."); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 2 | ▶ | if ( !exists $opt->{outfile} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".fasta"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 2 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | my $replace_fn = "$opt->{outfile}.replace.tsv"; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # A stream from 'stdin' or a standard file. | ||||||||||||||||||||||||||||
| 65 | 2 | my $in_fh; | |||||||||||||||||||||||||||
| 66 | 2 | ▶ | if ( lc $args->[0] eq "stdin" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $in_fh = *STDIN{IO}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 2 | open $in_fh, '<', $args->[0]; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # A stream to 'stdout' or a standard file. | ||||||||||||||||||||||||||||
| 74 | 2 | my $out_fh; | |||||||||||||||||||||||||||
| 75 | 2 | ▶ | if ( lc $opt->{outfile} eq "stdout" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 2 | $out_fh = *STDOUT{IO}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | open $out_fh, ">", $opt->{outfile}; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | open my $replace_fh, '>', $replace_fn; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | my $ori_name = q{}; | |||||||||||||||||||||||||||
| 85 | 2 | my $data_of = {}; | |||||||||||||||||||||||||||
| 86 | 2 | my @names; # original orders | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | while ( my $line = <$in_fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 16 | chomp $line; | |||||||||||||||||||||||||||
| 90 | 16 | ▶ | if ( $line =~ /\A > (\S+) /xms ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 91 | 8 | $ori_name = $1; | |||||||||||||||||||||||||||
| 92 | 8 | ▶ | if ( exists $data_of->{$ori_name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | Carp::croak "Redundant sequence name: $ori_name\n"; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 8 | push @names, $ori_name; | |||||||||||||||||||||||||||
| 96 | 8 | $data_of->{$ori_name}{'seen'} = 1; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | elsif ( $line =~ / \A \s* [A-Za-z] /xms ) { | ||||||||||||||||||||||||||||
| 99 | 8 | $data_of->{$ori_name}{'sequence'} .= $line; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | # just ok | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 2 | close $in_fh; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | for my $name ( keys %{$data_of} ) { | |||||||||||||||||||||||||||
| 108 | 8 | $data_of->{$name}{'length'} = length( $data_of->{$name}{'sequence'} ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | my $i = $opt->{start}; | |||||||||||||||||||||||||||
| 112 | 2 | for my $name (@names) { | |||||||||||||||||||||||||||
| 113 | 8 | my $serial_no = $i; | |||||||||||||||||||||||||||
| 114 | 8 | $serial_no = sprintf( '%u', $serial_no ); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | my $new_name = sprintf "%s/%u/0_%u", $opt->{prefix}, $serial_no, | ||||||||||||||||||||||||||||
| 117 | 8 | $data_of->{$name}{'length'}; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 8 | print {$out_fh} '>' . "$new_name\n"; | |||||||||||||||||||||||||||
| 120 | 8 | print {$out_fh} "$data_of->{$name}{'sequence'}\n"; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | print {$replace_fh} "$new_name\t$name\n"; | |||||||||||||||||||||||||||
| 123 | 8 | $i++; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | close $replace_fh; | |||||||||||||||||||||||||||
| 127 | 2 | close $out_fh; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 11 | 1; | |||||||||||||||||||||||||||