File Coverage

blib/lib/App/Rangeops/Command/create.pm
Criterion Covered Total %
statement 21 63 33.3
branch 0 18 0.0
condition 0 3 0.0
subroutine 9 11 81.8
pod 6 6 100.0
total 36 101 35.6


line stmt bran cond sub pod time code
1             package App::Rangeops::Command::create;
2 8     8   4360 use strict;
  8         17  
  8         202  
3 8     8   36 use warnings;
  8         12  
  8         160  
4 8     8   33 use autodie;
  8         13  
  8         35  
5              
6 8     8   35071 use App::Rangeops -command;
  8         16  
  8         59  
7 8     8   2114 use App::Rangeops::Common;
  8         17  
  8         5363  
8              
9             sub abstract {
10 2     2 1 39 return 'create blocked fasta files from range links';
11             }
12              
13             sub opt_spec {
14             return (
15 1     1 1 9 [ "outfile|o=s", "Output filename. [stdout] for screen." ],
16             [ "genome|g=s", "Reference genome file.", { required => 1 }, ],
17             [ "name|n=s", "Default name for ranges.", ],
18             );
19             }
20              
21             sub usage_desc {
22 1     1 1 25398 return "rangeops create [options] ";
23             }
24              
25             sub description {
26 1     1 1 853 my $desc;
27 1         3 $desc .= ucfirst(abstract) . ".\n";
28 1         2 return $desc;
29             }
30              
31             sub validate_args {
32 0     0 1   my ( $self, $opt, $args ) = @_;
33              
34 0 0         if ( !@{$args} ) {
  0            
35 0           $self->usage_error("This command need one or more input files.");
36             }
37 0           for ( @{$args} ) {
  0            
38 0 0         next if lc $_ eq "stdin";
39 0 0         if ( !Path::Tiny::path($_)->is_file ) {
40 0           $self->usage_error("The input file [$_] doesn't exist.");
41             }
42             }
43              
44 0 0         if ( !exists $opt->{outfile} ) {
45             $opt->{outfile}
46 0           = Path::Tiny::path( $args->[0] )->absolute . ".fas";
47             }
48             }
49              
50             sub execute {
51 0     0 1   my ( $self, $opt, $args ) = @_;
52              
53             #----------------------------#
54             # Output
55             #----------------------------#
56 0           my $out_fh;
57 0 0         if ( lc( $opt->{outfile} ) eq "stdout" ) {
58 0           $out_fh = \*STDOUT;
59             }
60             else {
61 0           open $out_fh, ">", $opt->{outfile};
62             }
63              
64             #----------------------------#
65             # Loading
66             #----------------------------#
67 0           my $info_of = {};
68 0           for my $file ( @{$args} ) {
  0            
69 0           for my $line ( App::RL::Common::read_lines($file) ) {
70 0           $info_of = App::Rangeops::Common::build_info( [$line], $info_of );
71 0           my @parts;
72 0           for my $part ( split /\t/, $line ) {
73 0 0         next unless exists $info_of->{$part};
74 0           push @parts, $part;
75             }
76 0 0         next unless @parts >= 2;
77              
78 0           for my $range (@parts) {
79 0           my $info = $info_of->{$range};
80             my $location = sprintf "%s:%d-%d", $info->{chr}, $info->{start},
81 0           $info->{end};
82             my $seq = App::Rangeops::Common::get_seq_faidx( $opt->{genome},
83 0           $location );
84 0 0 0       if ( defined $info->{strand} and $info->{strand} ne "+" ) {
85 0           $seq = App::Fasops::Common::revcom($seq);
86             }
87 0 0         if ( $opt->{name} ) {
88 0           $info->{name} = $opt->{name};
89 0           $range = App::RL::Common::encode_header($info);
90             }
91 0           print {$out_fh} ">$range\n";
  0            
92 0           print {$out_fh} "$seq\n";
  0            
93             }
94 0           print {$out_fh} "\n";
  0            
95              
96             }
97             }
98              
99 0           close $out_fh;
100             }
101              
102             1;