File Coverage

blib/lib/App/Fasops/Command/vars.pm
Criterion Covered Total %
statement 120 126 95.2
branch 37 50 74.0
condition 12 15 80.0
subroutine 13 13 100.0
pod 6 7 85.7
total 188 211 89.1


line stmt bran cond sub pod time code
1             package App::Fasops::Command::vars;
2 20     20   14383 use strict;
  20         52  
  20         691  
3 20     20   130 use warnings;
  20         45  
  20         518  
4 20     20   108 use autodie;
  20         43  
  20         112  
5              
6 20     20   134160 use Excel::Writer::XLSX;
  20         3824674  
  20         1245  
7              
8 20     20   223 use App::Fasops -command;
  20         52  
  20         347  
9 20     20   9339 use App::Fasops::Common;
  20         54  
  20         30101  
10              
11             sub abstract {
12 2     2 1 46 return 'list substitutions';
13             }
14              
15             sub opt_spec {
16             return (
17 10     10 1 131 [ "outfile|o=s", "Output filename. [stdout] for screen" ],
18             [ "annotation|a=s", "YAML file for cds/repeat" ],
19             [ "length|l=i", "the threshold of alignment length", { default => 1 } ],
20             [ 'outgroup', 'alignments have an outgroup', ],
21             [ 'nosingle', 'omit singleton', ],
22             [ 'nocomplex', 'omit complex', ],
23             { show_defaults => 1, }
24             );
25             }
26              
27             sub usage_desc {
28 10     10 1 71979 return "fasops vars [options] ";
29             }
30              
31             sub description {
32 1     1 1 1517 my $desc;
33 1         5 $desc .= ucfirst(abstract) . ".\n";
34 1         4 $desc .= <<'MARKDOWN';
35              
36             * are paths to axt files, .fas.gz is supported
37             * infile == stdin means reading from STDIN
38              
39             MARKDOWN
40              
41 1         4 return $desc;
42             }
43              
44             sub validate_args {
45 9     9 1 15486 my ( $self, $opt, $args ) = @_;
46              
47 9 100       21 if ( @{$args} != 1 ) {
  9         35  
48 1         4 my $message = "This command need one input file.\n\tIt found";
49 1         2 $message .= sprintf " [%s]", $_ for @{$args};
  1         4  
50 1         3 $message .= ".\n";
51 1         12 $self->usage_error($message);
52             }
53 8         15 for ( @{$args} ) {
  8         21  
54 8 50       30 next if lc $_ eq "stdin";
55 8 100       43 if ( !Path::Tiny::path($_)->is_file ) {
56 1         120 $self->usage_error("The input file [$_] doesn't exist.");
57             }
58             }
59              
60 7 100       603 if ( exists $opt->{annotation} ) {
61 1 50       5 if ( !Path::Tiny::path( $opt->{annotation} )->is_file ) {
62 0         0 $self->usage_error("The annotation file [$opt->{annotation}] doesn't exist.");
63             }
64             }
65              
66 7 50       82 if ( !exists $opt->{outfile} ) {
67 0         0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".tsv";
68             }
69             }
70              
71             sub execute {
72 7     7 1 48 my ( $self, $opt, $args ) = @_;
73              
74             #@type IO::Handle
75 7         14 my $in_fh;
76 7 50       33 if ( lc $args->[0] eq "stdin" ) {
77 0         0 $in_fh = *STDIN{IO};
78             }
79             else {
80 7         67 $in_fh = IO::Zlib->new( $args->[0], "rb" );
81             }
82              
83 7         12787 my $out_fh;
84 7 50       33 if ( lc( $opt->{outfile} ) eq "stdout" ) {
85 7         18 $out_fh = *STDOUT{IO};
86             }
87             else {
88 0         0 open $out_fh, ">", $opt->{outfile};
89             }
90              
91 7         15 my $cds_set_of = {};
92 7         14 my $repeat_set_of = {};
93 7 100       25 if ( $opt->{annotation} ) {
94 1         6 my $anno = YAML::Syck::LoadFile( $opt->{annotation} );
95 1 50       311 Carp::confess "Invalid annotation YAML. Need cds.\n" unless defined $anno->{cds};
96 1 50       3 Carp::confess "Invalid annotation YAML. Need repeat.\n" unless defined $anno->{repeat};
97 1         8 $cds_set_of = App::RL::Common::runlist2set( $anno->{cds} );
98 1         8482 $repeat_set_of = App::RL::Common::runlist2set( $anno->{repeat} );
99             }
100              
101 7         8115 my $content = ''; # content of one block
102 7         12 while (1) {
103 505 100 66     4181 last if $in_fh->eof and $content eq '';
104 498         22189 my $line = '';
105 498 50       1676 if ( !$in_fh->eof ) {
106 498         19753 $line = $in_fh->getline;
107             }
108 498 50       96755 next if substr( $line, 0, 1 ) eq "#";
109              
110 498 100 66     7199 if ( ( $line eq '' or $line =~ /^\s+$/ ) and $content ne '' ) {
      66        
111 34         144 my $info_of = App::Fasops::Common::parse_block( $content, 1 );
112 34         59 $content = '';
113              
114 34         62 my @full_names;
115 34         63 my $seq_refs = [];
116              
117 34         46 for my $key ( keys %{$info_of} ) {
  34         109  
118 232         3273 push @full_names, $key;
119 232         309 push @{$seq_refs}, $info_of->{$key}{seq};
  232         612  
120             }
121              
122 34 50       420 if ( $opt->{length} ) {
123 34 100       104 next if length $info_of->{ $full_names[0] }{seq} < $opt->{length};
124             }
125              
126             # Use $target_seq_set to transform align positions to chr positions
127 19         275 my $align_set = AlignDB::IntSpan->new()->add_pair( 1, length $seq_refs->[0] );
128 19         1471 my $target_indel_set = App::Fasops::Common::indel_intspan( $seq_refs->[0] );
129 19         70 my $target_seq_set = $align_set->diff($target_indel_set);
130              
131             # Write lines
132 19         8175 my $vars = get_vars( $seq_refs, $opt );
133              
134 19         36 for my $pos ( sort { $a <=> $b } keys %{$vars} ) {
  4283         5215  
  19         428  
135 714         9036 my $var = $vars->{$pos};
136              
137 714         2380 my $chr_name = $info_of->{ $full_names[0] }{chr};
138             my $snp_chr_pos = App::Fasops::Common::align_to_chr(
139             $target_seq_set, $var->{snp_pos},
140             $info_of->{ $full_names[0] }{start},
141             $info_of->{ $full_names[0] }{strand},
142 714         9024 );
143              
144 714         2006 my $snp_coding = "";
145 714         925 my $snp_repeats = "";
146 714 100       1485 if ( $opt->{annotation} ) {
147 439 50       958 if ( defined $cds_set_of->{$chr_name} ) {
148 439         998 $snp_coding = $cds_set_of->{$chr_name}->contains($snp_chr_pos);
149             }
150 439 50       24310 if ( defined $repeat_set_of->{$chr_name} ) {
151 439         909 $snp_repeats = $repeat_set_of->{$chr_name}->contains($snp_chr_pos);
152             }
153             }
154              
155 714         5296 print {$out_fh} join "\t",
156             $full_names[0],
157             $chr_name,
158             $var->{snp_pos},
159             $snp_chr_pos,
160             "$chr_name:$snp_chr_pos",
161             $var->{snp_target_base},
162             $var->{snp_query_base},
163             $var->{snp_all_bases},
164             $var->{snp_mutant_to},
165             $var->{snp_freq},
166             $var->{snp_occured},
167 714         23657 $snp_coding,
168             $snp_repeats;
169 714         10172 print {$out_fh} "\n";
  714         1980  
170             }
171             }
172             else {
173 464         2222 $content .= $line;
174             }
175             }
176              
177 7         1274 $in_fh->close;
178 7         1276 $out_fh->close;
179              
180 0         0 return;
181             }
182              
183             # store all variations
184             sub get_vars {
185 19     19 0 31 my $seq_refs = shift;
186 19         32 my $opt = shift;
187              
188             # outgroup
189 19         29 my $out_seq;
190 19 100       52 if ( $opt->{outgroup} ) {
191 9         16 $out_seq = pop @{$seq_refs};
  9         19  
192             }
193              
194 19         27 my $seq_count = scalar @{$seq_refs};
  19         41  
195 19 50       49 if ( $seq_count < 2 ) {
196 0         0 Carp::confess "Too few sequences [$seq_count]\n";
197             }
198              
199 19         28 my %variations;
200              
201 19         71 my $snp_sites = App::Fasops::Common::get_snps($seq_refs);
202 19 100       63 if ( $opt->{outgroup} ) {
203 9         28 App::Fasops::Common::polarize_snp( $snp_sites, $out_seq );
204             }
205              
206 19         31 for my $site ( @{$snp_sites} ) {
  19         38  
207 1163 100 100     2952 if ( $opt->{nocomplex} and $site->{snp_freq} == -1 ) {
208 39         84 next;
209             }
210 1124 100 100     2666 if ( $opt->{nosingle} and $site->{snp_freq} <= 1 ) {
211 410         590 next;
212             }
213              
214 714         1257 $variations{ $site->{snp_pos} } = $site;
215             }
216              
217 19         372 return \%variations;
218             }
219              
220             1;