Newer
Older
sub sort_chromosomes {
my ($self, $chr_hashref) = @_;
$chr_hashref = $self->get_chrlength unless ($chr_hashref);
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
throw("You have to pass a hashref of your chromosomes")
unless ($chr_hashref and ref($chr_hashref) eq 'HASH');
return (sort _by_chr_num keys %$chr_hashref);
}
=head2 _by_chr_num
Example : my @sorted = sort _by_chr_num qw(X, 6-COX, 14, 7);
Description : Subroutine to use in sort for sorting chromosomes. Sorts
numerically, then alphabetically
Return type : values to be used by sort
Exceptions : none
Caller : internal ($self->sort_chromosomes)
=cut
sub _by_chr_num {
my @awords = split /-/, $a;
my @bwords = split /-/, $b;
my $anum = $awords[0];
my $bnum = $bwords[0];
if ($anum !~ /^[0-9]*$/) {
if ($bnum !~ /^[0-9]*$/) {
return $anum cmp $bnum;
} else {
return 1;
}
}
if ($bnum !~ /^[0-9]*$/) {
return -1;
}
if ($anum <=> $bnum) {
return $anum <=> $bnum;
} else {
if ($#awords == 0) {
return -1;
} elsif ($#bwords == 0) {
return 1;
} else {
return $awords[1] cmp $bwords[1];
}
}
}
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
=head2 split_chromosomes_by_size
Arg[1] : (optional) Int $cutoff - the cutoff in bp between small and
large chromosomes
Example : my $chr_slices = $support->split_chromosomes_by_size;
foreach my $block_size (keys %{ $chr_slices }) {
print "Chromosomes with blocksize $block_size: ";
print join(", ", map { $_->seq_region_name }
@{ $chr_slices->{$block_size} });
}
Description : Determines block sizes for storing DensityFeatures on
chromosomes, and return slices for each chromosome. The block
size is determined so that you have 150 bins for the smallest
chromosome over 5 Mb in length. For chromosomes smaller than 5
Mb, an additional smaller block size is used to yield 150 bins
for the overall smallest chromosome. This will result in
reasonable resolution for small chromosomes and high
performance for big ones.
Return type : Hashref (key: block size; value: Arrayref of chromosome
Bio::EnsEMBL::Slices)
Exceptions : none
Caller : density scripts
=cut
sub split_chromosomes_by_size {
my $self = shift;
my $cutoff = shift || 5000000;
my $slice_adaptor = $self->dba->get_SliceAdaptor;
my $top_slices;
if ($self->param('chromosomes')) {
foreach my $chr ($self->param('chromosomes')) {
push @{ $top_slices }, $slice_adaptor->fetch_by_region('chromosome', $chr);
}
} else {
$top_slices = $slice_adaptor->fetch_all("toplevel");
}
my ($big_chr, $small_chr, $min_big_chr, $min_small_chr);
foreach my $slice (@{ $top_slices }) {
if ($slice->length < $cutoff) {
if (! $min_small_chr or ($min_small_chr > $slice->length)) {
$min_small_chr = $slice->length;
}
# push small chromosomes onto $small_chr
push @{ $small_chr }, $slice;
}
if (! $min_big_chr or ($min_big_chr > $slice->length) && $slice->length > $cutoff) {
$min_big_chr = $slice->length;
}
# push _all_ chromosomes onto $big_chr
push @{ $big_chr }, $slice;
}
my $chr_slices;
$chr_slices->{int($min_big_chr/150)} = $big_chr if $min_big_chr;
$chr_slices->{int($min_small_chr/150)} = $small_chr if $min_small_chr;
return $chr_slices;
}
=head2 log
Arg[1] : String $txt - the text to log
Arg[2] : Int $indent - indentation level for log message
Example : my $log = $support->log_filehandle;
$support->log('Log foo.\n', 1);
Description : Logs a message to the filehandle initialised by calling
$self->log_filehandle(). You can supply an indentation level
to get nice hierarchical log messages.
Return type : true on success
Exceptions : thrown when no filehandle can be obtained
Caller : general
=cut
sub log {
my ($self, $txt, $indent) = @_;
$indent ||= 0;
# strip off leading linebreaks so that indenting doesn't break
$txt =~ s/^(\n*)//;
$txt = $1." "x$indent . $txt;
my $fh = $self->{'_log_filehandle'};
throw("Unable to obtain log filehandle") unless $fh;
print $fh "$txt";
return(1);
}
=head2 log_warning
Arg[1] : String $txt - the warning text to log
Arg[2] : Int $indent - indentation level for log message
Example : my $log = $support->log_filehandle;
$support->log_warning('Log foo.\n', 1);
Description : Logs a message via $self->log and increases the warning counter.
Return type : true on success
Exceptions : none
Caller : general
=cut
sub log_warning {
my ($self, $txt, $indent) = @_;
$txt = "WARNING: " . $txt;
$self->log($txt, $indent);
$self->{'_warnings'}++;
return(1);
}
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
=head2 log_error
Arg[1] : String $txt - the error text to log
Arg[2] : Int $indent - indentation level for log message
Example : my $log = $support->log_filehandle;
$support->log_error('Log foo.\n', 1);
Description : Logs a message via $self->log and exits the script.
Return type : none
Exceptions : none
Caller : general
=cut
sub log_error {
my ($self, $txt, $indent) = @_;
$txt = "ERROR: ".$txt;
$self->log($txt, $indent);
$self->log("Exiting.\n");
exit;
}
=head2 log_verbose
Arg[1] : String $txt - the warning text to log
Arg[2] : Int $indent - indentation level for log message
Example : my $log = $support->log_filehandle;
$support->log_verbose('Log this verbose message.\n', 1);
Description : Logs a message via $self->log if --verbose option was used
Return type : TRUE on success, FALSE if not verbose
Exceptions : none
Caller : general
=cut
sub log_verbose {
my ($self, $txt, $indent) = @_;
return(0) unless $self->param('verbose');
$self->log($txt, $indent);
return(1);
}
=head2 log_stamped
Arg[1] : String $txt - the warning text to log
Arg[2] : Int $indent - indentation level for log message
Example : my $log = $support->log_filehandle;
$support->log_stamped('Log this stamped message.\n', 1);
Description : Appends timestamp and memory usage to a message and logs it via
$self->log
Return type : TRUE on success
Exceptions : none
Caller : general
=cut
sub log_stamped {
my ($self, $txt, $indent) = @_;
# append timestamp and memory usage to log text
$txt =~ s/(\n*)$//;
$txt .= " ".$self->date_and_mem.$1;
$self->log($txt, $indent);
return(1);
}
=head2 log_filehandle
Arg[1] : (optional) String $mode - file access mode
Example : my $log = $support->log_filehandle;
# print to the filehandle
print $log 'Lets start logging...\n';
# log via the wrapper $self->log()
$support->log('Another log message.\n');
Description : Returns a filehandle for logging (STDERR by default, logfile if
set from config or commandline). You can use the filehandle
directly to print to, or use the smart wrapper $self->log().
Logging mode (truncate or append) can be set by passing the
mode as an argument to log_filehandle(), or with the
--logappend commandline option (default: truncate)
Return type : Filehandle - the filehandle to log to
Exceptions : thrown if logfile can't be opened
Caller : general
=cut
sub log_filehandle {
my ($self, $mode) = @_;
$mode ||= '>';
$mode = '>>' if ($self->param('logappend'));
my $fh = \*STDERR;
if (my $logfile = $self->param('logfile')) {
Patrick Meidl
committed
if (my $logpath = $self->param('logpath')) {
unless (-e $logpath) {
system("mkdir $logpath") == 0 or
$self->log_error("Can't create log dir $logpath: $!\n");
}
Patrick Meidl
committed
$logfile = "$logpath/$logfile";
}
open($fh, "$mode", $logfile) or throw(
"Unable to open $logfile for writing: $!");
}
$self->{'_log_filehandle'} = $fh;
return $self->{'_log_filehandle'};
}
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
=head2 filehandle
Arg[1] : String $mode - file access mode
Arg[2] : String $file - input or output file
Example : my $fh = $support->filehandle('>>', '/path/to/file');
# print to the filehandle
print $fh 'Your text goes here...\n';
Description : Returns a filehandle (*STDOUT for writing, *STDIN for reading
by default) to print to or read from.
Return type : Filehandle - the filehandle
Exceptions : thrown if file can't be opened
Caller : general
=cut
sub filehandle {
my ($self, $mode, $file) = @_;
$mode ||= ">";
my $fh;
if ($file) {
open($fh, "$mode", $file) or throw(
"Unable to open $file for writing: $!");
} elsif ($mode =~ />/) {
$fh = \*STDOUT;
} elsif ($mode =~ /</) {
$fh = \*STDIN;
}
return $fh;
}
=head2 init_log
Example : $support->init_log;
Description : Opens a filehandle to the logfile and prints some header
information to this file. This includes script name, date, user
running the script and parameters the script will be running
with.
Return type : Filehandle - the log filehandle
Exceptions : none
Caller : general
=cut
sub init_log {
my $self = shift;
# get a log filehandle
my $log = $self->log_filehandle;
# print script name, date, user who is running it
my $hostname = `hostname`;
chomp $hostname;
my $script = "$hostname:$Bin/$Script";
my $user = `whoami`;
chomp $user;
$self->log("Script: $script\nDate: ".$self->date."\nUser: $user\n");
# print parameters the script is running with
$self->log("Parameters:\n\n");
$self->log($self->list_all_params);
# remember start time
$self->{'_start_time'} = time;
return $log;
}
=head2 finish_log
Example : $support->finish_log;
Description : Writes footer information to a logfile. This includes the
number of logged warnings, timestamp and memory footprint.
Return type : TRUE on success
Exceptions : none
Caller : general
=cut
sub finish_log {
my $self = shift;
$self->log("\nAll done. ".$self->warnings." warnings. ");
if ($self->{'_start_time'}) {
$self->log("Runtime ");
my $diff = time - $self->{'_start_time'};
my $sec = $diff % 60;
$diff = ($diff - $sec) / 60;
my $min = $diff % 60;
my $hours = ($diff - $min) / 60;
$self->log("${hours}h ${min}min ${sec}sec ");
}
$self->log($self->date_and_mem."\n\n");
return(1);
}
=head2 date_and_mem
Example : print LOG "Time, memory usage: ".$support->date_and_mem."\n";
Description : Prints a timestamp and the memory usage of your script.
Return type : String - timestamp and memory usage
Exceptions : none
Caller : general
=cut
sub date_and_mem {
my $date = strftime "%Y-%m-%d %T", localtime;
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
chomp $mem;
return "[$date, mem $mem]";
}
=head2 date
Example : print "Date: " . $support->date . "\n";
Description : Prints a nicely formatted timestamp (YYYY-DD-MM hh:mm:ss)
Return type : String - the timestamp
Exceptions : none
Caller : general
=cut
sub date {
return strftime "%Y-%m-%d %T", localtime;
}
=head2 mem
Example : print "Memory usage: " . $support->mem . "\n";
Description : Prints the memory used by your script. Not sure about platform
dependence of this call ...
Return type : String - memory usage
Exceptions : none
Caller : general
=cut
sub mem {
chomp $mem;
return $mem;
}
1;