« blib/lib/Bioinfo/App/Cmd/Blast/Cmd/SplitSubmit.pm
↑ Summary
blib/lib/Bioinfo/App/Cmd/Fasta/Cmd/Split.pm »
| 1 | package Bioinfo::App::Cmd::Fasta::Cmd::SplitBlast; | ||
| 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: my perl module and CLIs for Biology | ||
| 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 outdir => ( | ||
| 68 | is => 'ro', | ||
| 69 | format => 's', | ||
| 70 | short => 'o', | ||
| 71 | default => sub { './' }, | ||
| 72 | doc => 'outdir of split files', | ||
| 73 | ); | ||
| 74 | |||
| 75 | |||
| 76 | option prefix => ( | ||
| 77 | is => 'ro', | ||
| 78 | format => 's', | ||
| 79 | short => 'p', | ||
| 80 | default => sub { '' }, | ||
| 81 | doc => 'the prefix of the split file', | ||
| 82 | ); | ||
| 83 | |||
| 84 | |||
| 85 | sub execute { | ||
| 86 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 87 | $self->options_usage unless (@$args_ref); | ||
| 88 | my $input = $self->input; | ||
| 89 | my $outdir = $self->outdir; | ||
| 90 | my $num = $self->num; | ||
| 91 | my $prefix = $self->prefix; | ||
| 92 | system("mkdir -p $outdir") unless -e $outdir; | ||
| 93 | my %id2seq; | ||
| 94 | my $io_fa = io($input)->chomp; | ||
| 95 | my ($total_base, $seqid) = (0, ''); | ||
| 96 | while (defined (my $line = $io_fa->getline)) { | ||
| 97 | if ($line =~/^>/) { | ||
| 98 | $seqid = $line; | ||
| 99 | } else { | ||
| 100 | $id2seq{$seqid} .= $line; | ||
| 101 | $total_base += length($line); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | say "number of files:$num\t prefix:$prefix\toutdir:$outdir"; | ||
| 105 | say "total base: $total_base"; | ||
| 106 | my $file_base = int($total_base / $num) + 2; | ||
| 107 | my ($file_base_tmp, $index_tmp) = (0, 1); | ||
| 108 | my $single_file_name = "$outdir/$prefix$index_tmp.fa"; | ||
| 109 | my $single_file_content = ""; | ||
| 110 | for my $id (keys %id2seq) { | ||
| 111 | if ($file_base_tmp < $file_base) { | ||
| 112 | $single_file_content .= sprintf("%s\n%s\n", $id, $id2seq{$id}); | ||
| 113 | $file_base_tmp += length($id2seq{$id}); | ||
| 114 | } else { | ||
| 115 | io($single_file_name)->print("$single_file_content"); | ||
| 116 | say "$single_file_name\t$file_base_tmp"; | ||
| 117 | |||
| 118 | # set next file parameter | ||
| 119 | $single_file_content = sprintf("%s\n%s\n", $id, $id2seq{$id}); | ||
| 120 | $file_base_tmp = length($id2seq{$id}); | ||
| 121 | $index_tmp++; | ||
| 122 | $single_file_name = "$outdir/$prefix$index_tmp.fa"; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | # the last one file | ||
| 127 | if ($file_base_tmp < $file_base) { | ||
| 128 | $single_file_name = "$outdir/$prefix$index_tmp.fa"; | ||
| 129 | io($single_file_name)->print("$single_file_content"); | ||
| 130 | say "$single_file_name\t$file_base_tmp"; | ||
| 131 | } | ||
| 132 | say "finished to split $input, outdir is :$outdir"; | ||
| 133 | if ($self->type eq 'pbs') { | ||
| 134 | $self->submit_pbs; | ||
| 135 | } else { | ||
| 136 | $self->local_blast; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # submit PBS queue | ||
| 141 | sub submit_pbs { | ||
| 142 | my $self = shift; | ||
| 143 | my ($input, $outdir, $cpu, $db) = ($self->input, $self->outdir, $self->cpu, $self->db); | ||
| 144 | my @io_fas = io("$outdir")->filter( sub { | ||
| 145 | $_->filename =~/\.fa/; | ||
| 146 | } | ||
| 147 | )->all_files; | ||
| 148 | |||
| 149 | my $queue_name = io($input)->filename; | ||
| 150 | $queue_name =~s/\.fa|\.pep|\.fasta//; | ||
| 151 | say "PBS Queue name: $queue_name"; | ||
| 152 | chdir "$outdir"; | ||
| 153 | my $pbs = Bioinfo::PBS::Queue->new(name => $queue_name); | ||
| 154 | for my $fa (@io_fas) { | ||
| 155 | my $fa_name = $fa->filename; | ||
| 156 | my $cmd = "blastp -query $fa_name -out $fa_name.blast -db $db -outfmt 5 -evalue 1e-5 -num_threads $cpu -max_target_seqs 10"; | ||
| 157 | $fa_name =~s/\.fa|\.pep|\.fasta//; | ||
| 158 | my $para = { | ||
| 159 | cpu => $cpu, | ||
| 160 | name => $fa_name, | ||
| 161 | cmd => "$cmd", | ||
| 162 | }; | ||
| 163 | $pbs->add_tasks($para); | ||
| 164 | } | ||
| 165 | $pbs->execute; | ||
| 166 | system("cat *.blast >$queue_name.blast"); | ||
| 167 | say "finished all blast"; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub local_blast { | ||
| 171 | my $self = shift; | ||
| 172 | my ($input, $outdir, $cpu, $db) = ($self->input, $self->outdir, $self->cpu, $self->db); | ||
| 173 | my @io_fas = io("$outdir")->filter( sub { | ||
| 174 | $_->filename =~/\.fa/; | ||
| 175 | } | ||
| 176 | )->all_files; | ||
| 177 | |||
| 178 | my $in_name = io($input)->filename; | ||
| 179 | $in_name =~s/\.fa|\.fasta|\.pep//; | ||
| 180 | chdir "$outdir"; | ||
| 181 | use Parallel::ForkManager; | ||
| 182 | my $pm = Parallel::ForkManager->new($cpu); | ||
| 183 | LOOP_DATA: | ||
| 184 | for my $fa (@io_fas) { | ||
| 185 | my $pid = $pm->start and next LOOP_DATA; | ||
| 186 | my $fa_name = $fa->filename; | ||
| 187 | my $cmd = "blastp -query $fa_name -out $fa.xml -db $db -outfmt 5 -evalue 1e-5 -num_threads $cpu -max_target_seqs 10"; | ||
| 188 | system($cmd); | ||
| 189 | $pm->finish; | ||
| 190 | } | ||
| 191 | $pm->wait_all_chilren; | ||
| 192 | system("cat *.blast >$in_name.xml"); | ||
| 193 | say "finished all"; | ||
| 194 | } | ||
| 195 | |||
| 196 | 1; | ||
| 197 | |||
| 198 | __END__ | ||
| 199 | |||
| 200 | =pod | ||
| 201 | |||
| 202 | =encoding UTF-8 | ||
| 203 | |||
| 204 | =head1 NAME | ||
| 205 | |||
| 206 | Bioinfo::App::Cmd::Fasta::Cmd::SplitBlast - my perl module and CLIs for Biology | ||
| 207 | |||
| 208 | =head1 VERSION | ||
| 209 | |||
| 210 | version 0.1.14 | ||
| 211 | |||
| 212 | =head1 SYNOPSIS | ||
| 213 | |||
| 214 | use Bioinfo::App::Cmd::Fasta; | ||
| 215 | ... | ||
| 216 | |||
| 217 | =head1 DESCRIPTION | ||
| 218 | |||
| 219 | =head1 ATTRIBUTES | ||
| 220 | |||
| 221 | =head2 input | ||
| 222 | |||
| 223 | The input file is a file of fasta format | ||
| 224 | |||
| 225 | =head2 num | ||
| 226 | |||
| 227 | number of files that the fasta file will be split | ||
| 228 | |||
| 229 | =head2 db | ||
| 230 | |||
| 231 | the database that blast will use | ||
| 232 | |||
| 233 | =head2 type | ||
| 234 | |||
| 235 | =head2 blast | ||
| 236 | |||
| 237 | =head2 cpu | ||
| 238 | |||
| 239 | =head2 outdir | ||
| 240 | |||
| 241 | =head2 prefix | ||
| 242 | |||
| 243 | the prefix of the split file | ||
| 244 | |||
| 245 | =head1 METHODS | ||
| 246 | |||
| 247 | =head2 execute | ||
| 248 | |||
| 249 | =head1 AUTHOR | ||
| 250 | |||
| 251 | Yan Xueqing <yanxueqing621@163.com> | ||
| 252 | |||
| 253 | =head1 COPYRIGHT AND LICENSE | ||
| 254 | |||
| 255 | This software is copyright (c) 2017 by Yan Xueqing. | ||
| 256 | |||
| 257 | This is free software; you can redistribute it and/or modify it under | ||
| 258 | the same terms as the Perl 5 programming language system itself. | ||
| 259 | |||
| 260 | =cut |