blib/lib/Bioinfo/App/Cmd/Blast/Cmd/SplitSubmit.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 150 CC 7 SCAR 32.4
CC
7
Cov
0%
execute
56.0
submit_pbs
2.0
local_blast
2.0
CRAP 25.6
1 package Bioinfo::App::Cmd::Blast::Cmd::SplitSubmit;
2 use Modern::Perl;
3 use Moo;
4 use MooX::Cmd;
5 use MooX::Options prefer_commandline => 1;
6 use IO::All;
7 use Bioinfo::PBS::Queue;
8  
9 our $VERSION = '0.1.14'; # VERSION:
10 # ABSTRACT: submit blast after splitting a fasta file into multiple files;
11  
12  
13 option input => (
14 is => 'ro',
15 required => 1,
16 format => 's',
17 short => 'i',
18 doc => 'a file of fasta format'
19 );
20  
21  
22 option num => (
23 is => 'ro',
24 format => 'i',
25 short => 'n',
26 default => sub { '6' },
27 doc => 'number of files that the fasta file will be split'
28 );
29  
30  
31 option db => (
32 is => 'rw',
33 format => 's',
34 short => 'd',
35 default => sub { 'nr_plant' },
36 doc => 'the database that blast will use',
37 );
38  
39  
40 option type => (
41 is => 'rw',
42 format => 's',
43 short => 't',
44 default => sub { 'pbs' },
45 doc => "where the blast will be runned, enum['local','pbs']. default:'pbs'"
46 );
47  
48  
49 option blast => (
50 is => 'rw',
51 format => 's',
52 short => 'b',
53 default => sub { 'blastp' },
54 doc => 'which blast program will be used',
55 );
56  
57  
58 option cpu => (
59 is => 'ro',
60 format => 'i',
61 short => 'c',
62 default => sub { '4' },
63 doc => "cpu number used in one node"
64 );
65  
66  
67 option max_target_seqs => (
68 is => 'ro',
69 format => 'i',
70 short => 'm',
71 default => sub { '10000' },
72 doc => "max_target_seqs, Default:10000"
73 );
74  
75  
76 option evalue => (
77 is => 'ro',
78 format => 's',
79 short => 'e',
80 default => sub { '1e-5' },
81 doc => "evalue, Default:1e-5"
82 );
83  
84  
85 option outfmt => (
86 is => 'ro',
87 format => 'i',
88 short => 'f',
89 default => sub { 5 },
90 doc => "outfmt, Default:5"
91 );
92  
93 option outdir => (
94 is => 'ro',
95 format => 's',
96 short => 'o',
97 default => sub { './' },
98 doc => 'outdir of split files',
99 );
100  
101  
102 option prefix => (
103 is => 'ro',
104 format => 's',
105 short => 'p',
106 default => sub { '' },
107 doc => 'the prefix of the split file',
108 );
109  
110  
111 sub execute {
112 my ($self, $args_ref, $chain_ref) = @_;
113 $self->options_usage unless (@$args_ref);
114 my $input = $self->input;
115 my $outdir = $self->outdir;
116 my $num = $self->num;
117 my $prefix = $self->prefix;
118 system("mkdir -p $outdir") unless -e $outdir;
119 my %id2seq;
120 my $io_fa = io($input)->chomp;
121 my ($total_base, $seqid) = (0, '');
122 while (defined (my $line = $io_fa->getline)) {
123 if ($line =~/^>/) {
124 $seqid = $line;
125 } else {
126 $id2seq{$seqid} .= $line;
127 $total_base += length($line);
128 }
129 }
130 say "number of files:$num\t prefix:$prefix\toutdir:$outdir";
131 say "total base: $total_base";
132 my $file_base = int($total_base / $num) + 2;
133 my ($file_base_tmp, $index_tmp) = (0, 1);
134 my $single_file_name = "$outdir/$prefix$index_tmp.fa";
135 my $single_file_content = "";
136 for my $id (keys %id2seq) {
137 if ($file_base_tmp < $file_base) {
138 $single_file_content .= sprintf("%s\n%s\n", $id, $id2seq{$id});
139 $file_base_tmp += length($id2seq{$id});
140 } else {
141 io($single_file_name)->print("$single_file_content");
142 say "$single_file_name\t$file_base_tmp";
143  
144 # set next file parameter
145 $single_file_content = sprintf("%s\n%s\n", $id, $id2seq{$id});
146 $file_base_tmp = length($id2seq{$id});
147 $index_tmp++;
148 $single_file_name = "$outdir/$prefix$index_tmp.fa";
149 }
150 }
151  
152 # the last one file
153 if ($file_base_tmp < $file_base) {
154 $single_file_name = "$outdir/$prefix$index_tmp.fa";
155 io($single_file_name)->print("$single_file_content");
156 say "$single_file_name\t$file_base_tmp";
157 }
158 say "finished to split $input, outdir is :$outdir";
159 if ($self->type eq 'pbs') {
160 $self->submit_pbs;
161 } else {
162 $self->local_blast;
163 }
164 }
165  
166 # submit PBS queue
167 sub submit_pbs {
168 my $self = shift;
169 my ($input, $outdir, $cpu, $db) = ($self->input, $self->outdir, $self->cpu, $self->db);
170 my $max_target_seqs = $self->max_target_seqs;
171 my $evalue = $self->evalue;
172 my $outfmt = $self->outfmt;
173 my @io_fas = io("$outdir")->filter( sub {
174 $_->filename =~/\.fa/;
175 }
176 )->all_files;
177  
178 my $queue_name = io($input)->filename;
179 $queue_name =~s/\.fa|\.pep|\.fasta//;
180 say "PBS Queue name: $queue_name";
181 chdir "$outdir";
182 my $pbs = Bioinfo::PBS::Queue->new(name => $queue_name);
183 for my $fa (@io_fas) {
184 my $fa_name = $fa->filename;
185 my $cmd = "blastp -query $fa_name -out $fa_name.blast -db $db -outfmt $outfmt -evalue $evalue -num_threads $cpu -max_target_seqs $max_target_seqs";
186 $fa_name =~s/\.fa|\.pep|\.fasta//;
187 my $para = {
188 cpu => $cpu,
189 name => $fa_name,
190 cmd => "$cmd",
191 };
192 $pbs->add_tasks($para);
193 }
194 $pbs->execute;
195 system("cat *.blast >$queue_name.blast");
196 say "finished all blast";
197 }
198  
199 sub local_blast {
200 my $self = shift;
201 my ($input, $outdir, $cpu, $db) = ($self->input, $self->outdir, $self->cpu, $self->db);
202 my @io_fas = io("$outdir")->filter( sub {
203 $_->filename =~/\.fa/;
204 }
205 )->all_files;
206  
207 my $in_name = io($input)->filename;
208 $in_name =~s/\.fa|\.fasta|\.pep//;
209 chdir "$outdir";
210 use Parallel::ForkManager;
211 my $pm = Parallel::ForkManager->new($cpu);
212 LOOP_DATA:
213 for my $fa (@io_fas) {
214 my $pid = $pm->start and next LOOP_DATA;
215 my $fa_name = $fa->filename;
216 my $cmd = "blastp -query $fa_name -out $fa.xml -db $db -outfmt 5 -evalue 1e-5 -num_threads $cpu -max_target_seqs 10";
217 system($cmd);
218 $pm->finish;
219 }
220 $pm->wait_all_chilren;
221 system("cat *.blast >$in_name.xml");
222 say "finished all";
223 }
224  
225 1;
226  
227 __END__
228  
229 =pod
230  
231 =encoding UTF-8
232  
233 =head1 NAME
234  
235 Bioinfo::App::Cmd::Blast::Cmd::SplitSubmit - submit blast after splitting a fasta file into multiple files;
236  
237 =head1 VERSION
238  
239 version 0.1.14
240  
241 =head1 SYNOPSIS
242  
243 use Bioinfo::App::Cmd::Blast::Cmd::SplitSubmit;
244  
245 Bioinfo::App::Cmd::Blast::Cmd::SplitSubmit->new_with_cmd;
246  
247 =head1 DESCRIPTION
248  
249 this module splits a fasta file into multiple files, then submit these files on parallel.
250  
251 =head1 ATTRIBUTES
252  
253 =head2 input
254  
255 The input file is a file of fasta format
256  
257 =head2 num
258  
259 number of files that the fasta file will be split
260  
261 =head2 db
262  
263 the database that blast will use
264  
265 =head2 type
266  
267 =head2 blast
268  
269 =head2 cpu
270  
271 =head2 max_target_seqs
272  
273 =head2 evalue
274  
275 =head2 outfmt
276  
277 =head2 outdir
278  
279 =head2 prefix
280  
281 the prefix of the split file
282  
283 =head1 METHODS
284  
285 =head2 execute
286  
287 =head1 AUTHOR
288  
289 Yan Xueqing <yanxueqing621@163.com>
290  
291 =head1 COPYRIGHT AND LICENSE
292  
293 This software is copyright (c) 2017 by Yan Xueqing.
294  
295 This is free software; you can redistribute it and/or modify it under
296 the same terms as the Perl 5 programming language system itself.
297  
298 =cut