| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package App::Fasops::Command::slice; |
|
2
|
21
|
|
|
21
|
|
15474
|
use strict; |
|
|
21
|
|
|
|
|
54
|
|
|
|
21
|
|
|
|
|
652
|
|
|
3
|
21
|
|
|
21
|
|
115
|
use warnings; |
|
|
21
|
|
|
|
|
44
|
|
|
|
21
|
|
|
|
|
572
|
|
|
4
|
21
|
|
|
21
|
|
110
|
use autodie; |
|
|
21
|
|
|
|
|
51
|
|
|
|
21
|
|
|
|
|
119
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
21
|
|
|
21
|
|
107585
|
use App::Fasops -command; |
|
|
21
|
|
|
|
|
61
|
|
|
|
21
|
|
|
|
|
236
|
|
|
7
|
21
|
|
|
21
|
|
7271
|
use App::RL::Common; |
|
|
21
|
|
|
|
|
49
|
|
|
|
21
|
|
|
|
|
604
|
|
|
8
|
21
|
|
|
21
|
|
124
|
use App::Fasops::Common; |
|
|
21
|
|
|
|
|
41
|
|
|
|
21
|
|
|
|
|
30836
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub abstract { |
|
11
|
2
|
|
|
2
|
1
|
49
|
return 'extract alignment slices from a blocked fasta'; |
|
12
|
|
|
|
|
|
|
} |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub opt_spec { |
|
15
|
|
|
|
|
|
|
return ( |
|
16
|
5
|
|
|
5
|
1
|
54
|
[ "outfile|o=s", "Output filename. [stdout] for screen." ], |
|
17
|
|
|
|
|
|
|
[ "name|n=s", "According to this species. Default is the first one." ], |
|
18
|
|
|
|
|
|
|
[ "length|l=i", "the threshold of alignment length", { default => 1 } ], |
|
19
|
|
|
|
|
|
|
{ show_defaults => 1, } |
|
20
|
|
|
|
|
|
|
); |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub usage_desc { |
|
24
|
5
|
|
|
5
|
1
|
51284
|
return "fasops slice [options] "; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub description { |
|
28
|
1
|
|
|
1
|
1
|
995
|
my $desc; |
|
29
|
1
|
|
|
|
|
6
|
$desc .= ucfirst(abstract) . ".\n"; |
|
30
|
1
|
|
|
|
|
5
|
$desc .= <<'MARKDOWN'; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
* are paths to axt files, .axt.gz is supported |
|
33
|
|
|
|
|
|
|
* infile == stdin means reading from STDIN |
|
34
|
|
|
|
|
|
|
* is a App::RL dump |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
MARKDOWN |
|
37
|
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
3
|
return $desc; |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub validate_args { |
|
42
|
4
|
|
|
4
|
1
|
4522
|
my ( $self, $opt, $args ) = @_; |
|
43
|
|
|
|
|
|
|
|
|
44
|
4
|
100
|
|
|
|
7
|
if ( @{$args} != 2 ) { |
|
|
4
|
|
|
|
|
16
|
|
|
45
|
2
|
|
|
|
|
5
|
my $message = "This command need two input files.\n\tIt found"; |
|
46
|
2
|
|
|
|
|
6
|
$message .= sprintf " [%s]", $_ for @{$args}; |
|
|
2
|
|
|
|
|
9
|
|
|
47
|
2
|
|
|
|
|
6
|
$message .= ".\n"; |
|
48
|
2
|
|
|
|
|
13
|
$self->usage_error($message); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
2
|
|
|
|
|
6
|
for ( @{$args} ) { |
|
|
2
|
|
|
|
|
6
|
|
|
51
|
3
|
50
|
|
|
|
77
|
next if lc $_ eq "stdin"; |
|
52
|
3
|
100
|
|
|
|
13
|
if ( !Path::Tiny::path($_)->is_file ) { |
|
53
|
1
|
|
|
|
|
138
|
$self->usage_error("The input file [$_] doesn't exist."); |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
1
|
50
|
|
|
|
51
|
if ( !exists $opt->{outfile} ) { |
|
58
|
|
|
|
|
|
|
$opt->{outfile} |
|
59
|
0
|
|
|
|
|
0
|
= Path::Tiny::path( $args->[0] )->absolute . ".slice.fas"; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub execute { |
|
64
|
1
|
|
|
1
|
1
|
9
|
my ( $self, $opt, $args ) = @_; |
|
65
|
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
2
|
my $in_fh; |
|
67
|
1
|
50
|
|
|
|
6
|
if ( lc $args->[0] eq "stdin" ) { |
|
68
|
0
|
|
|
|
|
0
|
$in_fh = *STDIN{IO}; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
else { |
|
71
|
1
|
|
|
|
|
11
|
$in_fh = IO::Zlib->new( $args->[0], "rb" ); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
2139
|
my $out_fh; |
|
75
|
1
|
50
|
|
|
|
6
|
if ( lc( $opt->{outfile} ) eq "stdout" ) { |
|
76
|
1
|
|
|
|
|
5
|
$out_fh = *STDOUT{IO}; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
else { |
|
79
|
0
|
|
|
|
|
0
|
open $out_fh, ">", $opt->{outfile}; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
7
|
my $set_single |
|
83
|
|
|
|
|
|
|
= App::RL::Common::runlist2set( YAML::Syck::LoadFile( $args->[1] ) ); |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
{ |
|
86
|
1
|
|
|
|
|
540
|
my $content = ''; # content of one block |
|
|
1
|
|
|
|
|
3
|
|
|
87
|
1
|
|
|
|
|
3
|
while (1) { |
|
88
|
8
|
100
|
100
|
|
|
123
|
last if $in_fh->eof and $content eq ''; |
|
89
|
7
|
|
|
|
|
490
|
my $line = ''; |
|
90
|
7
|
100
|
|
|
|
27
|
if ( !$in_fh->eof ) { |
|
91
|
6
|
|
|
|
|
251
|
$line = $in_fh->getline; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
7
|
100
|
66
|
|
|
1415
|
if ( ( $line eq '' or $line =~ /^\s+$/ ) and $content ne '' ) { |
|
|
|
|
66
|
|
|
|
|
|
94
|
1
|
|
|
|
|
10
|
my $info_of = App::Fasops::Common::parse_block($content); |
|
95
|
1
|
|
|
|
|
3
|
$content = ''; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# set $opt->{name} to the first one of the first block |
|
98
|
1
|
50
|
|
|
|
4
|
if ( !defined $opt->{name} ) { |
|
99
|
0
|
|
|
|
|
0
|
( $opt->{name} ) = keys %{$info_of}; |
|
|
0
|
|
|
|
|
0
|
|
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# target name |
|
103
|
1
|
|
|
|
|
3
|
my $name = $opt->{name}; |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# basic target information |
|
106
|
1
|
|
|
|
|
4
|
my $chr_name = $info_of->{$name}{chr}; |
|
107
|
1
|
|
|
|
|
13
|
my $chr_strand = $info_of->{$name}{strand}; |
|
108
|
1
|
|
|
|
|
11
|
my $chr_start = $info_of->{$name}{start}; |
|
109
|
1
|
|
|
|
|
12
|
my $chr_end = $info_of->{$name}{end}; |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# chr present |
|
112
|
1
|
50
|
|
|
|
12
|
next unless exists $set_single->{$chr_name}; |
|
113
|
1
|
50
|
|
|
|
6
|
next if $set_single->{$chr_name}->is_empty; |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# has intersect |
|
116
|
1
|
|
|
|
|
19
|
my $i_chr_intspan; |
|
117
|
|
|
|
|
|
|
{ |
|
118
|
1
|
|
|
|
|
2
|
my AlignDB::IntSpan $slice_set = $set_single->{$chr_name}; |
|
|
1
|
|
|
|
|
3
|
|
|
119
|
1
|
|
|
|
|
6
|
my $align_chr_set = AlignDB::IntSpan->new; |
|
120
|
1
|
|
|
|
|
20
|
$align_chr_set->add_pair( $chr_start, $chr_end ); |
|
121
|
1
|
|
|
|
|
67
|
$i_chr_intspan = $slice_set->intersect($align_chr_set); |
|
122
|
|
|
|
|
|
|
} |
|
123
|
1
|
50
|
|
|
|
480
|
next if $i_chr_intspan->is_empty; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# print YAML::Syck::Dump { |
|
126
|
|
|
|
|
|
|
# name => $name, |
|
127
|
|
|
|
|
|
|
# chr_name => $chr_name, |
|
128
|
|
|
|
|
|
|
# chr_strand => $chr_strand, |
|
129
|
|
|
|
|
|
|
# chr_start => $chr_start, |
|
130
|
|
|
|
|
|
|
# chr_end => $chr_end, |
|
131
|
|
|
|
|
|
|
# i_chr_intspan => $i_chr_intspan->runlist, |
|
132
|
|
|
|
|
|
|
# }; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# target sequence intspan |
|
135
|
1
|
|
|
|
|
19
|
my $target_seq_intspan = App::Fasops::Common::seq_intspan( $info_of->{$name}{seq} ); |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# every sequence intspans |
|
138
|
1
|
|
|
|
|
6
|
my %seq_intspan_of; |
|
139
|
1
|
|
|
|
|
3
|
for my $key ( keys %{$info_of} ) { |
|
|
1
|
|
|
|
|
14
|
|
|
140
|
|
|
|
|
|
|
$seq_intspan_of{$key} |
|
141
|
3
|
|
|
|
|
57
|
= App::Fasops::Common::seq_intspan( $info_of->{$key}{seq} ); |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# all indel regions |
|
145
|
1
|
|
|
|
|
8
|
my $indel_intspan = AlignDB::IntSpan->new; |
|
146
|
1
|
|
|
|
|
14
|
for my $key ( keys %{$info_of} ) { |
|
|
1
|
|
|
|
|
10
|
|
|
147
|
|
|
|
|
|
|
$indel_intspan->add( |
|
148
|
3
|
|
|
|
|
9308
|
App::Fasops::Common::indel_intspan( $info_of->{$key}{seq} ) ); |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# there may be more than one subslice intersect this alignment |
|
152
|
1
|
|
|
|
|
2594
|
my @sub_slices; |
|
153
|
1
|
|
|
|
|
10
|
for my AlignDB::IntSpan $ss_chr_intspan ( $i_chr_intspan->sets ) { |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# chr positions to align positions |
|
156
|
1
|
|
|
|
|
219
|
my $ss_start |
|
157
|
|
|
|
|
|
|
= App::Fasops::Common::chr_to_align( $target_seq_intspan, |
|
158
|
|
|
|
|
|
|
$ss_chr_intspan->min, $chr_start, $chr_strand ); |
|
159
|
1
|
|
|
|
|
8
|
my $ss_end |
|
160
|
|
|
|
|
|
|
= App::Fasops::Common::chr_to_align( $target_seq_intspan, |
|
161
|
|
|
|
|
|
|
$ss_chr_intspan->max, $chr_start, $chr_strand ); |
|
162
|
1
|
50
|
|
|
|
5
|
next if $ss_start >= $ss_end; |
|
163
|
|
|
|
|
|
|
|
|
164
|
1
|
|
|
|
|
4
|
my $ss_intspan = AlignDB::IntSpan->new; |
|
165
|
1
|
|
|
|
|
17
|
$ss_intspan->add_pair( $ss_start, $ss_end ); |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# borders of subslice inside a indel |
|
168
|
1
|
50
|
|
|
|
120
|
if ( $indel_intspan->contains($ss_start) ) { |
|
169
|
0
|
|
|
|
|
0
|
my $indel_island = $indel_intspan->find_islands($ss_start); |
|
170
|
0
|
|
|
|
|
0
|
$ss_intspan->remove($indel_island); |
|
171
|
|
|
|
|
|
|
} |
|
172
|
1
|
50
|
|
|
|
75
|
if ( $indel_intspan->contains($ss_end) ) { |
|
173
|
0
|
|
|
|
|
0
|
my $indel_island = $indel_intspan->find_islands($ss_end); |
|
174
|
0
|
|
|
|
|
0
|
$ss_intspan->remove($indel_island); |
|
175
|
|
|
|
|
|
|
} |
|
176
|
1
|
50
|
|
|
|
58
|
next if $ss_intspan->size <= $opt->{length}; |
|
177
|
1
|
|
|
|
|
27
|
push @sub_slices, $ss_intspan; |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# write heasers and sequences |
|
181
|
1
|
|
|
|
|
6
|
for my AlignDB::IntSpan $sub_slice (@sub_slices) { |
|
182
|
1
|
|
|
|
|
4
|
my $ss_start = $sub_slice->min; |
|
183
|
1
|
|
|
|
|
24
|
my $ss_end = $sub_slice->max; |
|
184
|
|
|
|
|
|
|
|
|
185
|
1
|
|
|
|
|
20
|
for my $key ( keys %{$info_of} ) { |
|
|
1
|
|
|
|
|
11
|
|
|
186
|
|
|
|
|
|
|
my $key_start = App::Fasops::Common::align_to_chr( |
|
187
|
|
|
|
|
|
|
$seq_intspan_of{$key}, $ss_start, |
|
188
|
|
|
|
|
|
|
$info_of->{$key}{start}, |
|
189
|
|
|
|
|
|
|
$info_of->{$key}{strand} |
|
190
|
3
|
|
|
|
|
111
|
); |
|
191
|
|
|
|
|
|
|
my $key_end = App::Fasops::Common::align_to_chr( |
|
192
|
|
|
|
|
|
|
$seq_intspan_of{$key}, $ss_end, |
|
193
|
|
|
|
|
|
|
$info_of->{$key}{start}, |
|
194
|
|
|
|
|
|
|
$info_of->{$key}{strand} |
|
195
|
3
|
|
|
|
|
17
|
); |
|
196
|
|
|
|
|
|
|
my $ss_info = { |
|
197
|
|
|
|
|
|
|
name => $info_of->{$key}{name}, |
|
198
|
|
|
|
|
|
|
chr => $info_of->{$key}{chr}, |
|
199
|
|
|
|
|
|
|
strand => $info_of->{$key}{strand}, |
|
200
|
3
|
|
|
|
|
16
|
start => $key_start, |
|
201
|
|
|
|
|
|
|
end => $key_end, |
|
202
|
|
|
|
|
|
|
}; |
|
203
|
3
|
|
|
|
|
94
|
printf {$out_fh} ">%s\n", App::RL::Common::encode_header($ss_info); |
|
|
3
|
|
|
|
|
21
|
|
|
204
|
3
|
|
|
|
|
15
|
printf {$out_fh} "%s\n", |
|
205
|
3
|
|
|
|
|
281
|
substr( $info_of->{$key}{seq}, $ss_start - 1, $ss_end - $ss_start + 1 ); |
|
206
|
|
|
|
|
|
|
} |
|
207
|
1
|
|
|
|
|
33
|
print {$out_fh} "\n"; |
|
|
1
|
|
|
|
|
4
|
|
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
} |
|
211
|
|
|
|
|
|
|
else { |
|
212
|
6
|
|
|
|
|
48
|
$content .= $line; |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
} |
|
216
|
1
|
|
|
|
|
279
|
close $out_fh; |
|
217
|
0
|
|
|
|
|
|
$in_fh->close; |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
1; |