File Coverage

blib/lib/Bioinfo/PBS.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 6 0.0
condition 0 3 0.0
subroutine 4 8 50.0
pod 4 4 100.0
total 20 64 31.2


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