-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPIPELINETOOLS.pm
362 lines (294 loc) · 10.8 KB
/
PIPELINETOOLS.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#-------------------------------------------------------------------------------
# PIPELINETOOLS.pm
#
# Author: Raphael Hablesreiter (1130649)
#
# Description:
# The PIPELINETOOLS - module consists of all the essential function, which
# are used more often in the entire pipeline.
#
#------------------------------------------------------------------------------
#
package PIPELINETOOLS;
use strict;
use warnings;
use Backticks;
use File::chdir;
use File::Copy;
#-------------------------------------------------------------------------------
# WriteToLogFile: This function takes the input as the text, that has to be
# written to the log.file. If the log.file doesn't exist it
# creates the log.file in the project folder.
# Input: text for log.file [string]
#-------------------------------------------------------------------------------
sub WriteToLogFile {
my $log_name = ($::global_cfg -> param("results.std"))."/log.file";
my $log_text = $_[0];
#creates the log.file, attaches the header and adds the first log entry
if ($log_text ne "" && !(-e $log_name))
{
open(LOGFILE, ">", $log_name)
or die "ERROR: Can't create log-file!\n";
print LOGFILE CreateLogHeader();
print LOGFILE $log_text;
close LOGFILE;
}
#opens the log-file for append and attaches the log entry
elsif ($log_text ne "" && -e $log_name)
{
my $log_number = $::global_cfg -> param("tmp.lognumber");
open(LOGFILE, ">>", $log_name)
or die "ERROR: Can't open log-file!\n";
print LOGFILE $log_text;
close LOGFILE;
}
else
{
die "ERROR: Problem with the log-file!\n";
}
return;
}
#-------------------------------------------------------------------------------
# WriteToLogChapter: This function takes the input as the text,
# that has to be written to the log.file as a chapter.
# Input: text for log.file chapter [string]
#-------------------------------------------------------------------------------
sub WriteLogChapter{
my $chapter = $_[0];
my $space1 = int((78 - length($chapter))/2);
my $time = "Started at ".(PIPELINETOOLS::GetTime(1));
my $space2 = int((78 - length($time))/2);
my $chapter_txt = ("#" x 80)."\n";
$chapter_txt = $chapter_txt."#".(" " x 78)."#\n";
$chapter_txt = $chapter_txt."#".(" " x $space1).$chapter;
$chapter_txt = $chapter_txt.(" " x (78 - length($chapter) - $space1))."#\n";
$chapter_txt = $chapter_txt."#".(" " x $space2).$time;
$chapter_txt = $chapter_txt.(" " x (78 - length($time) - $space2))."#\n";
$chapter_txt = $chapter_txt."#".(" " x 78)."#\n";
$chapter_txt = $chapter_txt.("#" x 80)."\n";
PIPELINETOOLS::WriteToLogFile($chapter_txt);
return;
}
#-------------------------------------------------------------------------------
# WriteLogOut: This function writes the std::Out of a function into the log.file
# and finish this with line that provides the finishing time.
# Input: std::Out of a function for log.file [string]
#-------------------------------------------------------------------------------
sub WriteLogOut{
my $p_out = $_[0]."\n";
my $ad = "*" x 27;
$p_out = $p_out.$ad.
"Task finished at ".(PIPELINETOOLS::GetTime(2)).$ad."*\n\n\n";
PIPELINETOOLS::WriteToLogFile($p_out);
return;
}
#-------------------------------------------------------------------------------
# CreateLogHeader: This function writes the header into the log.file.
#-------------------------------------------------------------------------------
sub CreateLogHeader {
#This subprogramm is creating a header for the log-file
my $author = "Raphael Hablesreiter";
my $version = "V0.1";
my $institute =
"Graz University of Technology - Institute of Molecular Biotechnology";
my $seqtype;
if (($::global_cfg -> param("required.sequence_1")) eq "PE")
{
$seqtype = "Paired End Reads";
}
else
{
$seqtype = "Single End Reads";
}
my $header = ("#" x 80)."\n";
$header = $header."Author: $author\n"
."Institute: $institute\n"
."Version: $version\n"
."Created: SS16\n"
.("#" x 80)."\n"
."Run Information:\n\n"
." User: ".($::global_cfg -> param("required.User"))."\n"
." Projectname: ".($::global_cfg -> param("required.ProjectName"))."\n\n"
." 1. Sequence Name: ".($::global_cfg -> param("required.sequence_1"))."\n"
." 2. Sequence Name: ".($::global_cfg -> param("required.sequence_2"))."\n"
." Sequences Type: ".$seqtype."\n\n"
." Mapping Type: ".($::global_cfg -> param("required.MappingType"))."\n"
.("#" x 80)."\n\n";
return $header;
}
#-------------------------------------------------------------------------------
# GetTime: This function provides two types of time and date.
# Input = 1 -> Return: $mday.$mon.$year $hour:$min:$sec
# Input = 2 -> Return: $hour:$min:$sec
#-------------------------------------------------------------------------------
sub GetTime{
#FROM: Perl Cookbook S.71
#time - returns the number of seconds that have psased sunce the last Epoch
#localtime - converts the output of time into distinct values
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
#the range for months and days are starting from 0
$mon += 1;
my $time;
$sec = sprintf('%02s',$sec);
$hour = sprintf('%02s',$hour);
$min = sprintf('%02s',$min);
$mday = sprintf('%02s',$mday);
$mon = sprintf('%02s',$mon);
if ($_[0] == 1)
{
$time = "$mday.$mon.$year $hour:$min:$sec";
}
elsif ($_[0] == 2)
{
$time = "$hour:$min:$sec";
}
else
{
$time = "$hour:$min:$sec";
}
return $time;
}
#-------------------------------------------------------------------------------
# ReadDirectory: This function provides an array with all the files in a folder.
# Input: $_[0] = directory
# $_[1] = string, that should be in the filename,
# if $_[1] = "", all files of a folder
# are returned.
# Return: Array with all the files in a folder that matches a
# certain criteria.
#-------------------------------------------------------------------------------
sub ReadDirectory{
my $directory = $_[0]; #directory
my $file_name;
my @output = ();
my $name_containing = "";
if (scalar(@_) > 1)
{
$name_containing = $_[1]; #name, that has to be in the files
}
opendir(my $dir, $directory) or die "Cannot open $directory: $!";
while (defined ($file_name = readdir($dir)))
{
next if $file_name =~ /^\./; #skips files with .. and .
next if $file_name !~ /\./;
if ($name_containing ne "")
{
next if $file_name !~ /$name_containing/;
}
push @output, $file_name;
}
closedir $dir;
if (@output)
{
return @output;
}
else
{
@output = ();
return @output;
}
}
#-------------------------------------------------------------------------------
# ChangeWorkingDir: This function changes the working directory.
# Input: $_[0] = directory
#-------------------------------------------------------------------------------
sub ChangeWorkingDir{
my $path = $_[0];
$CWD = $path;
return;
}
#-------------------------------------------------------------------------------
# MoveFile: This function changes the working directory.
# Input: $_[0] = sourcefile
# $_[1] = destinationfolder/-file
#-------------------------------------------------------------------------------
sub MoveFile{
my $sourcefile = $_[0];
my $destinationfile = $_[1];
my $mv_cmd = "mv ".$sourcefile." ".$destinationfile;
my $cmd = `$mv_cmd`;
if ($cmd -> success != 1)
{
print "ERROR: Moving $sourcefile to $destinationfile failed!\n";
}
return;
}
#-------------------------------------------------------------------------------
# CreateSuccess: This function creates a successfile (part###.succ) and this
# successfile is in the style of an INI-file.
# see Config::Simple
#
# Input: $_[0] = name of part of pipeline finished
# "Cutadapt" -> for Cutadapt has finished
# "Trimmomatic" -> for Trimmomatic has finished
# "rCorrector" -> for rCorrector has finished
# "Mapping" -> for Mapping has finished
# "Cufflinks" -> for Cufflinks has finished
# "Cuffmerge" -> for Cuffmerge has finished
# "Cuffdiff" -> for Cuffdiff has finished
# "CummeRbund" -> for CummeRbund has finished
#
# Layout sample:
# ;Config::Simple 4.58
# ;Wed Apr 20 09:28:07 2016
#
# [success]
# done=okay
# cmd=Cutadapt
# time=20.04.2016 09:28:07
#-------------------------------------------------------------------------------
sub CreateSuccess{
#call with the wanted command-parameter
my $prgm_cmd = $_[0];
my $succ_name;
my $i = 0;
my $time = PIPELINETOOLS::GetTime(1);
my $succ_dir = $::global_cfg -> param("results.tmp");
my $succ = new Config::Simple(syntax=>'ini');
$succ->param("success.cmd","$prgm_cmd");
$succ->param("success.time","$time");
$succ->param("success.done","okay");
my @succ_files = PIPELINETOOLS::ReadDirectory($succ_dir,".succ");
if (@succ_files)
{
#getting the number(i) of the last successfile and creating new
#successfilename(i+1)
@succ_files = sort @succ_files;
my $last_succ = pop(@succ_files);
$last_succ = substr($last_succ,4,-5);
print "Last Success = $last_succ\n";
$last_succ = int($last_succ) + 1;
$succ_name = $succ_dir."/part".sprintf("%03s",$last_succ).".succ";
}
else
{
$succ_name = "part001.succ";
}
$succ -> write($succ_name) or die "ERROR";
return 0;
}
#-------------------------------------------------------------------------------
# MoveFile: This prints to Std::out, if pipeline parameter silent is not TRUE.
# Input: $_[0] = std::out of a cmd program.
#-------------------------------------------------------------------------------
sub PrintStd{
my $stdoutput = $_[0];
$stdoutput = $stdoutput."\n";
if (!($::global_cfg -> param("cmdline.silent")))
{
print $stdoutput;
}
return;
}
#-------------------------------------------------------------------------------
# MoveFile: This prints to Std::out, if pipeline parameter silent is not TRUE.
# Input: $_[0] = pipeline part
#-------------------------------------------------------------------------------
sub PrintTask{
my $stdoutput = $_[0];
$stdoutput = $stdoutput." @ ".(PIPELINETOOLS::GetTime(1))."\n";
print $stdoutput;
return;
}
1;