File Coverage

blib/lib/Bioinfo/PBS.pm
Criterion Covered Total %
statement 12 44 27.2
branch 0 6 0.0
condition 0 3 0.0
subroutine 4 8 50.0
pod 4 4 100.0
total 20 65 30.7


line stmt bran cond sub pod time code
1             package Bioinfo::PBS;
2 2     2   75922 use Moose;
  2         386035  
  2         14  
3 2     2   12288 use Modern::Perl;
  2         3  
  2         15  
4 2     2   294 use IO::All;
  2         3  
  2         16  
5 2     2   660 use namespace::autoclean;
  2         6572  
  2         10  
6              
7             our $VERSION = '0.1.15'; # VERSION:
8             # ABSTRACT: my perl module and CLIs for Biology
9              
10              
11             has cpu => (
12             is => 'rw',
13             isa => 'Int',
14             default => sub {'1'},
15             lazy => 1,
16             );
17              
18              
19             has name => (
20             is => 'rw',
21             isa => 'Str',
22             default => sub { 'yanxq' },
23             lazy => 1,
24             );
25              
26              
27             has queue_name => (
28             is => 'rw',
29             isa => 'Str',
30             default => sub { 'batch' },
31             lazy => 1,
32             );
33              
34              
35             has cmd => (
36             is => 'rw',
37             isa => 'Str',
38             required => 1,
39             );
40              
41              
42             has path => (
43             is => 'rw',
44             isa => 'Str',
45             default => sub { '$PBS_O_WORKDIR' },
46             lazy => 1,
47             );
48              
49              
50             has job_id => (
51             is => 'ro',
52             writer => '_set_job_id',
53             isa => 'Int',
54             );
55              
56              
57             has priority => (
58             is => 'rw',
59             isa => 'Int',
60             default => sub { '1' },
61             lazy => 1,
62             );
63              
64             has _sh_name => (
65             is => 'rw',
66             isa => 'Str',
67             );
68              
69              
70             around BUILDARGS => sub {
71             my $orig = shift;
72             my $class = shift;
73             my %para = @_ == 1 && ref $_[0] ? %{$_[0]} : @_;
74             return $class->$orig(%para);
75             };
76              
77              
78              
79             sub get_sh {
80 0     0 1   my ($self, $sh_name) = @_;
81 0   0       $sh_name ||= $self->name . "_" . time . ".sh";
82 0           my ($path, $cmd) = ($self->path, $self->cmd);
83 0           chdir $path;
84 0           my $sh_content =<<EOF;
85             cd $path
86             echo "Directory is $path"
87             NP=`cat \$PBS_NODEFILE|wc -l`
88             echo \$NP
89             echo "Excuting Hosts is flowing:"
90             cat \$PBS_NODEFILE
91             echo "begin time: `date`"
92             echo "CMD: $cmd"
93             $cmd
94             echo "finish time: `date`"
95             echo "DONE";
96             EOF
97 0           io($sh_name)->print($sh_content);
98 0           $self->_sh_name($sh_name);
99             #say "task setted attr sh_name:". $self->_sh_name. "\n";
100 0           return $sh_name;
101             }
102              
103              
104             sub qsub {
105 0     0 1   my $self = shift;
106 0           my $sh_name = $self->get_sh;
107 0           my ($name, $cpu) = ($self->name, $self->cpu);
108 0           my $queue_name = $self->queue_name;
109 0           my $qsub_result = `qsub -l nodes=1:ppn=$cpu -q $queue_name -N $name $sh_name`;
110 0           say "qsub result: $qsub_result\n";
111 0 0         if ($qsub_result =~/^(\d+?)\./) {
112 0           say "job_id: $1\n";
113 0           $self->_set_job_id($1);
114             } else {
115 0           say "Error: fail to qsub $sh_name; \nqsub output: $qsub_result\n";
116             }
117 0           return $self;
118             }
119              
120              
121             sub wait {
122 0     0 1   my $self = shift;
123 0           while ($self->job_stat) {
124 0           sleep(120);
125             }
126 0           return 0;
127             }
128              
129              
130             sub job_stat {
131 0     0 1   my $self = shift;
132 0           my $o = $self->name . ".o" . $self->job_id;
133 0 0         if (-e $o) {
134 0           my $out = `tail -1 $o`;
135 0           chomp($out);
136 0 0         if ($out eq "DONE") {
137 0           return 0;
138             } else {
139 0           return 1;
140             }
141             } else {
142 0           return -1;
143             }
144             }
145              
146             __PACKAGE__->meta->make_immutable;
147              
148             1;
149              
150             __END__
151              
152             =pod
153              
154             =encoding UTF-8
155              
156             =head1 NAME
157              
158             Bioinfo::PBS - my perl module and CLIs for Biology
159              
160             =head1 VERSION
161              
162             version 0.1.15
163              
164             =head1 SYNOPSIS
165              
166             use Bioinfo::PBS;
167             my $para = {
168             cpu => 2,
169             name => 'blast',
170             cmd => 'ls -alh; pwd',
171             };
172             my $pbs_obj = Bioinfo::PBS->new($para);
173             $pbs_obj->qsub;
174              
175             =head1 DESCRIPTION
176              
177             This module is created to simplify process of task submitting in PBS system,
178             and waiting for the finish of multiple tasks.
179              
180             =head1 ATTRIBUTES
181              
182             =head2 cpu
183              
184             cpu number that will apply
185              
186             =head2 name
187              
188             prefix of output of STANDARD and ERR
189              
190             =head2 queue_name
191              
192             =head2 cmd
193              
194             the command that will be submitted to cluster
195              
196             =head2 path
197              
198             the path that cmd will execute in
199              
200             =head2 job_id
201              
202             the job id of qsub
203              
204             =head2 priority
205              
206             the priority during the process of Batch submmit in Queue
207              
208             =head1 METHODS
209              
210             =head2 get_sh
211              
212             get shell file that will used in qsub
213              
214             =head2 qsub
215              
216             submit the program to cluster
217              
218             =head2 wait
219              
220             wait until the program finished in cluster
221              
222             =head2 job_stat
223              
224             get the status of job. C<0> will be return if the job has completed
225              
226             =head1 AUTHOR
227              
228             Yan Xueqing <yanxueqing621@163.com>
229              
230             =head1 COPYRIGHT AND LICENSE
231              
232             This software is copyright (c) 2017 by Yan Xueqing.
233              
234             This is free software; you can redistribute it and/or modify it under
235             the same terms as the Perl 5 programming language system itself.
236              
237             =cut