-
Notifications
You must be signed in to change notification settings - Fork 0
/
vecscreen_candidate_generation.pm
293 lines (262 loc) · 10.5 KB
/
vecscreen_candidate_generation.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
#!/usr/bin/perl
#
# vecscreen_candidate_generation.pm
# Alejandro Schaffer and Eric Nawrocki
#
use strict;
use warnings;
use Time::HiRes qw(gettimeofday); # for timings
#####################################################################
# SUBROUTINES
#####################################################################
# List of subroutines:
#
# Functions for output:
# output_banner: output the banner with info on the script and options used
# output_progress_prior: output routine for a step, prior to running the step
# output_progress_complete: output routine for a step, after the running the step
#
# Miscellaneous functions:
# run_command: run a command using system()
# seconds_since_epoch: number of seconds since the epoch, for timings
#
#################################################################
# Subroutine : output_progress_prior()
# Incept: EPN, Fri Feb 12 17:22:24 2016 [dnaorg.pm]
#
# Purpose: Output to $FH1 (and possibly $FH2) a message indicating
# that we're about to do 'something' as explained in
# $outstr.
#
# Caller should call *this* function, then do
# the 'something', then call output_progress_complete().
#
# We return the number of seconds since the epoch, which
# should be passed into the downstream
# output_progress_complete() call if caller wants to
# output running time.
#
# Arguments:
# $outstr: string to print to $FH
# $progress_w: width of progress messages
# $FH1: file handle to print to, can be undef
# $FH2: another file handle to print to, can be undef
#
# Returns: Number of seconds and microseconds since the epoch.
#
#################################################################
sub output_progress_prior {
my $nargs_expected = 4;
my $sub_name = "output_progress_prior()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($outstr, $progress_w, $FH1, $FH2) = @_;
if(defined $FH1) { printf $FH1 ("# %-*s ... ", $progress_w, $outstr); }
if(defined $FH2) { printf $FH2 ("# %-*s ... ", $progress_w, $outstr); }
return seconds_since_epoch();
}
#################################################################
# Subroutine : output_progress_complete()
# Incept: EPN, Fri Feb 12 17:28:19 2016 [dnaorg.pm]
#
# Purpose: Output to $FH1 (and possibly $FH2) a
# message indicating that we've completed
# 'something'.
#
# Caller should call *this* function,
# after both a call to output_progress_prior()
# and doing the 'something'.
#
# If $start_secs is defined, we determine the number
# of seconds the step took, output it, and
# return it.
#
# Arguments:
# $start_secs: number of seconds either the step took
# (if $secs_is_total) or since the epoch
# (if !$secs_is_total)
# $extra_desc: extra description text to put after timing
# $FH1: file handle to print to, can be undef
# $FH2: another file handle to print to, can be undef
#
# Returns: Number of seconds the step took (if $secs is defined,
# else 0)
#
#################################################################
sub output_progress_complete {
my $nargs_expected = 4;
my $sub_name = "output_progress_complete()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($start_secs, $extra_desc, $FH1, $FH2) = @_;
my $total_secs = undef;
if(defined $start_secs) {
$total_secs = seconds_since_epoch() - $start_secs;
}
if(defined $FH1) { printf $FH1 ("done."); }
if(defined $FH2) { printf $FH2 ("done."); }
if(defined $total_secs || defined $extra_desc) {
if(defined $FH1) { printf $FH1 (" ["); }
if(defined $FH2) { printf $FH2 (" ["); }
}
if(defined $total_secs) {
if(defined $FH1) { printf $FH1 (sprintf("%.1f seconds%s", $total_secs, (defined $extra_desc) ? ", " : "")); }
if(defined $FH2) { printf $FH2 (sprintf("%.1f seconds%s", $total_secs, (defined $extra_desc) ? ", " : "")); }
}
if(defined $extra_desc) {
if(defined $FH1) { printf $FH1 $extra_desc };
if(defined $FH2) { printf $FH2 $extra_desc };
}
if(defined $total_secs || defined $extra_desc) {
if(defined $FH1) { printf $FH1 ("]"); }
if(defined $FH2) { printf $FH2 ("]"); }
}
if(defined $FH1) { printf $FH1 ("\n"); }
if(defined $FH2) { printf $FH2 ("\n"); }
return (defined $total_secs) ? $total_secs : 0.;
}
#####################################################################
# Subroutine: output_banner()
# Incept: EPN, Thu Oct 30 09:43:56 2014 (rnavore)
#
# Purpose: Output the banner with info on the script, input arguments
# and options used.
#
# Arguments:
# $FH: file handle to print to
# $version: version
# $releasedate: month/year of version (e.g. "Feb 2016")
# $synopsis: string reporting the date
# $date: date information to print
#
# Returns: Nothing, if it returns, everything is valid.
#
# Dies: never
####################################################################
sub output_banner {
my $nargs_expected = 5;
my $sub_name = "output_banner()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($FH, $version, $releasedate, $synopsis, $date) = @_;
print $FH ("\# $synopsis\n");
print $FH ("\# version: $version ($releasedate)\n");
print $FH ("\# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
if(defined $date) { print $FH ("# date: $date\n"); }
printf $FH ("#\n");
return;
}
#################################################################
# Subroutine: run_command()
# Incept: EPN, Mon Dec 19 10:43:45 2016
#
# Purpose: Runs a command using system() and exits in error
# if the command fails. If $be_verbose, outputs
# the command to stdout. If $FH_HR->{"cmd"} is
# defined, outputs command to that file handle.
#
# Arguments:
# $cmd: command to run, with a "system" command;
# $be_verbose: '1' to output command to stdout before we run it, '0' not to
#
# Returns: amount of time the command took, in seconds
#
# Dies: if $cmd fails
#################################################################
sub run_command {
my $sub_name = "run_command()";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($cmd, $be_verbose) = @_;
if($be_verbose) {
print STDERR ("Running cmd: $cmd\n");
}
my ($seconds, $microseconds) = gettimeofday();
my $start_time = ($seconds + ($microseconds / 1000000.));
system($cmd);
($seconds, $microseconds) = gettimeofday();
my $stop_time = ($seconds + ($microseconds / 1000000.));
if($? != 0) {
die "ERROR in $sub_name, the following command failed:\n$cmd\n";
}
return ($stop_time - $start_time);
}
#################################################################
# Subroutine : seconds_since_epoch()
# Incept: EPN, Sat Feb 13 06:17:03 2016
#
# Purpose: Return the seconds and microseconds since the
# Unix epoch (Jan 1, 1970) using
# Time::HiRes::gettimeofday().
#
# Arguments: NONE
#
# Returns: Number of seconds and microseconds
# since the epoch.
#
#################################################################
sub seconds_since_epoch {
my $nargs_expected = 0;
my $sub_name = "seconds_since_epoch()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($seconds, $microseconds) = gettimeofday();
return ($seconds + ($microseconds / 1000000.));
}
#################################################################
# Subroutine : parse_filelist_file()
# Incept: EPN, Thu Mar 16 14:21:52 2017
#
# Purpose: Parse the file $filelist_file by saving each line
# as an element in @AR, after verifying that the file
# listed on that line actually exists. The file can
# exist as either a relative path (in which case we
# prepend the current working directory) or an
# absolute path.
#
# Arguments:
# $filelist_file: name of file to open and read from
# $AR: ref to array to fill with file names
#
# Returns: Number of files/lines read
#
# Dies: If any file listed in the $filelist_file does not
# exist.
#
#################################################################
sub parse_filelist_file {
my $nargs_expected = 2;
my $sub_name = "parse_filelist_file()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($filelist_file, $AR) = @_;
my $directory; #current directory
my $count; #count of files
my $relname; #relative path name of an input fasta file
my $absname; #absolute path name of an input fasta file
my $nextline; #one line of the input file
my $errmsg; #error message used in case we have to die
$directory = cwd() . "/";
$count = 0;
$errmsg = "";
open(IN, "<", $filelist_file) or die "ERROR in $sub_name, unable to open $filelist_file for reading";
while(defined($nextline = <IN>)) {
chomp($nextline);
$relname = $directory . $nextline;
$absname = $nextline;
if(-e $relname) {
$AR->[$count] = $relname;
}
elsif(-e $absname) {
$AR->[$count] = $absname;
}
else {
$errmsg .= "$nextline\n";
}
$count++;
}
close(IN);
if($errmsg ne "") {
die "ERROR in $sub_name, the following files listed in $filelist_file do not exist (as relative paths or absolute paths):\n$errmsg\n";
}
return $count;
}
####################################################################
# the next line is critical, a perl module must return a true value
return 1;
####################################################################