| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Fasops::Command::stat; | 
| 2 | 21 |  |  | 21 |  | 14641 | use strict; | 
|  | 21 |  |  |  |  | 49 |  | 
|  | 21 |  |  |  |  | 639 |  | 
| 3 | 21 |  |  | 21 |  | 116 | use warnings; | 
|  | 21 |  |  |  |  | 50 |  | 
|  | 21 |  |  |  |  | 531 |  | 
| 4 | 21 |  |  | 21 |  | 120 | use autodie; | 
|  | 21 |  |  |  |  | 46 |  | 
|  | 21 |  |  |  |  | 122 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 21 |  |  | 21 |  | 126421 | use Text::CSV_XS; | 
|  | 21 |  |  |  |  | 233217 |  | 
|  | 21 |  |  |  |  | 1297 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 21 |  |  | 21 |  | 182 | use App::Fasops -command; | 
|  | 21 |  |  |  |  | 52 |  | 
|  | 21 |  |  |  |  | 289 |  | 
| 9 | 21 |  |  | 21 |  | 8522 | use App::Fasops::Common; | 
|  | 21 |  |  |  |  | 54 |  | 
|  | 21 |  |  |  |  | 18082 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub abstract { | 
| 12 | 2 |  |  | 2 | 1 | 48 | return 'basic statistics on alignments'; | 
| 13 |  |  |  |  |  |  | } | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub opt_spec { | 
| 16 |  |  |  |  |  |  | return ( | 
| 17 | 6 |  |  | 6 | 1 | 50 | [ "outfile|o=s", "output filename. [stdout] for screen" ], | 
| 18 |  |  |  |  |  |  | [ "length|l=i", "the threshold of alignment length", { default => 1 } ], | 
| 19 |  |  |  |  |  |  | [ 'outgroup',   'alignments have an outgroup', ], | 
| 20 |  |  |  |  |  |  | { show_defaults => 1, } | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub usage_desc { | 
| 25 | 6 |  |  | 6 | 1 | 49299 | return "fasops stat [options] "; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub description { | 
| 29 | 1 |  |  | 1 | 1 | 781 | my $desc; | 
| 30 | 1 |  |  |  |  | 4 | $desc .= ucfirst(abstract) . ".\n"; | 
| 31 | 1 |  |  |  |  | 3 | $desc .= <<'MARKDOWN'; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | *  are paths to axt files, .axt.gz is supported | 
| 34 |  |  |  |  |  |  | * infile == stdin means reading from STDIN | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | MARKDOWN | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 1 |  |  |  |  | 3 | return $desc; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub validate_args { | 
| 42 | 5 |  |  | 5 | 1 | 4858 | my ( $self, $opt, $args ) = @_; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 5 | 100 |  |  |  | 9 | if ( @{$args} != 1 ) { | 
|  | 5 |  |  |  |  | 23 |  | 
| 45 | 1 |  |  |  |  | 3 | my $message = "This command need one input file.\n\tIt found"; | 
| 46 | 1 |  |  |  |  | 2 | $message .= sprintf " [%s]", $_ for @{$args}; | 
|  | 1 |  |  |  |  | 3 |  | 
| 47 | 1 |  |  |  |  | 4 | $message .= ".\n"; | 
| 48 | 1 |  |  |  |  | 11 | $self->usage_error($message); | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 4 |  |  |  |  | 8 | for ( @{$args} ) { | 
|  | 4 |  |  |  |  | 12 |  | 
| 51 | 4 | 50 |  |  |  | 15 | next if lc $_ eq "stdin"; | 
| 52 | 4 | 100 |  |  |  | 24 | if ( !Path::Tiny::path($_)->is_file ) { | 
| 53 | 1 |  |  |  |  | 118 | $self->usage_error("The input file [$_] doesn't exist."); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 3 | 50 |  |  |  | 270 | if ( !exists $opt->{outfile} ) { | 
| 58 | 0 |  |  |  |  | 0 | $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".csv"; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub execute { | 
| 63 | 3 |  |  | 3 | 1 | 23 | my ( $self, $opt, $args ) = @_; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | #@type IO::Handle | 
| 66 | 3 |  |  |  |  | 5 | my $in_fh; | 
| 67 | 3 | 50 |  |  |  | 12 | if ( lc $args->[0] eq "stdin" ) { | 
| 68 | 0 |  |  |  |  | 0 | $in_fh = *STDIN{IO}; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | else { | 
| 71 | 3 |  |  |  |  | 32 | $in_fh = IO::Zlib->new( $args->[0], "rb" ); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 3 |  |  |  |  | 4597 | my $out_fh; | 
| 75 | 3 | 50 |  |  |  | 14 | if ( lc( $opt->{outfile} ) eq "stdout" ) { | 
| 76 | 3 |  |  |  |  | 9 | $out_fh = *STDOUT{IO}; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | else { | 
| 79 | 0 |  |  |  |  | 0 | open $out_fh, ">", $opt->{outfile}; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | # csv object | 
| 83 | 3 |  |  |  |  | 34 | my $csv = Text::CSV_XS->new( { eol => $/, } ); | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # headers | 
| 86 | 3 |  |  |  |  | 442 | my @headers = qw{ | 
| 87 |  |  |  |  |  |  | first legnth comparables identities differences gaps ns errors D indel | 
| 88 |  |  |  |  |  |  | }; | 
| 89 | 3 |  |  |  |  | 78 | $csv->print( $out_fh, \@headers ); | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 3 |  |  |  |  | 91 | my $content = '';    # content of one block | 
| 92 | 3 |  |  |  |  | 6 | while (1) { | 
| 93 | 84 | 100 | 66 |  |  | 913 | last if $in_fh->eof and $content eq ''; | 
| 94 | 81 |  |  |  |  | 3101 | my $line = ''; | 
| 95 | 81 | 50 |  |  |  | 210 | if ( !$in_fh->eof ) { | 
| 96 | 81 |  |  |  |  | 2644 | $line = $in_fh->getline; | 
| 97 |  |  |  |  |  |  | } | 
| 98 | 81 | 50 |  |  |  | 8064 | next if substr( $line, 0, 1 ) eq "#"; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 81 | 100 | 66 |  |  | 376 | if ( ( $line eq '' or $line =~ /^\s+$/ ) and $content ne '' ) { | 
|  |  |  | 66 |  |  |  |  | 
| 101 | 9 |  |  |  |  | 47 | my $info_of = App::Fasops::Common::parse_block( $content, 1 ); | 
| 102 | 9 |  |  |  |  | 18 | $content = ''; | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 9 |  |  |  |  | 14 | my @full_names; | 
| 105 | 9 |  |  |  |  | 19 | my $seq_refs = []; | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 9 |  |  |  |  | 20 | for my $key ( keys %{$info_of} ) { | 
|  | 9 |  |  |  |  | 35 |  | 
| 108 | 36 |  |  |  |  | 447 | push @full_names, $key; | 
| 109 | 36 |  |  |  |  | 41 | push @{$seq_refs}, $info_of->{$key}{seq}; | 
|  | 36 |  |  |  |  | 80 |  | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 9 | 50 |  |  |  | 97 | if ( $opt->{length} ) { | 
| 113 | 9 | 100 |  |  |  | 26 | next if length $info_of->{ $full_names[0] }{seq} < $opt->{length}; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # outgroup | 
| 117 | 8 |  |  |  |  | 84 | my $out_seq; | 
| 118 | 8 | 100 |  |  |  | 27 | if ( $opt->{outgroup} ) { | 
| 119 | 3 |  |  |  |  | 6 | $out_seq = pop @{$seq_refs}; | 
|  | 3 |  |  |  |  | 7 |  | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 8 |  |  |  |  | 14 | my $first_name  = $full_names[0]; | 
| 123 | 8 |  |  |  |  | 31 | my $result      = App::Fasops::Common::multi_seq_stat($seq_refs); | 
| 124 | 8 |  |  |  |  | 39 | my $indel_sites = App::Fasops::Common::get_indels($seq_refs); | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 8 |  |  |  |  | 19 | $csv->print( $out_fh, [ $first_name, @{$result}, scalar( @{$indel_sites} ) ] ); | 
|  | 8 |  |  |  |  | 16 |  | 
|  | 8 |  |  |  |  | 271 |  | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  | else { | 
| 129 | 72 |  |  |  |  | 138 | $content .= $line; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 3 |  |  |  |  | 641 | close $out_fh; | 
| 134 | 0 |  |  |  |  |  | $in_fh->close; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | 1; |