| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # documentation at end of file | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use Pod::Usage; | ||
| 8 | use Getopt::Long qw(:config no_ignore_case bundling); | ||
| 9 | use IO::Handle; | ||
| 10 | use Bio::ToolBox; | ||
| 11 | use Bio::ToolBox::big_helper qw( | ||
| 12 | get_wig_to_bigwig_app | ||
| 13 | check_wigToBigWig_version | ||
| 14 | generate_chromosome_file | ||
| 15 | open_wig_to_bigwig_fh | ||
| 16 | open_bigwig_to_wig_fh | ||
| 17 | wig_to_bigwig_conversion | ||
| 18 | ); | ||
| 19 | |||
| 20 | our $VERSION = '2.02'; | ||
| 21 | |||
| 22 | ### Quick help | ||
| 23 | unless (@ARGV) { # when no command line options are present | ||
| 24 | # print SYNOPSIS | ||
| 25 | pod2usage( | ||
| 26 | { | ||
| 27 | '-verbose' => 0, | ||
| 28 | '-exitval' => 1, | ||
| 29 | } | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | |||
| 33 | ### Options | ||
| 34 | my $infile; | ||
| 35 | my $outfile; | ||
| 36 | my $skip; | ||
| 37 | my $apply; | ||
| 38 | my $doNull = 0; | ||
| 39 | my $deLogValue; | ||
| 40 | my $doAbsolute = 0; | ||
| 41 | my $multiplyValue; | ||
| 42 | my $addValue; | ||
| 43 | my $logValue; | ||
| 44 | my $places; | ||
| 45 | my $minValue; | ||
| 46 | my $maxValue; | ||
| 47 | my $noZeroes; | ||
| 48 | my $doStats; | ||
| 49 | my $bw2wig_app; | ||
| 50 | my $wig2bw_app; | ||
| 51 | my $chromofile; | ||
| 52 | my $database; | ||
| 53 | my $help; | ||
| 54 | my $print_version; | ||
| 55 | |||
| 56 | ### Command line options | ||
| 57 | GetOptions( | ||
| 58 | 'i|input=s' => \$infile, | ||
| 59 | 'o|output=s' => \$outfile, | ||
| 60 | 'k|skip=s' => \$skip, | ||
| 61 | 'y|apply=s' => \$apply, | ||
| 62 | 'u|null!' => \$doNull, | ||
| 63 | 'd|delog=i' => \$deLogValue, | ||
| 64 | 'b|abs!' => \$doAbsolute, | ||
| 65 | 'm|multiply=f' => \$multiplyValue, | ||
| 66 | 'a|add=f' => \$addValue, | ||
| 67 | 'l|log=i' => \$logValue, | ||
| 68 | 'p|place=i' => \$places, | ||
| 69 | 'n|minimum=f' => \$minValue, | ||
| 70 | 'x|maximum=f' => \$maxValue, | ||
| 71 | 'z|zero' => \$noZeroes, | ||
| 72 | 't|stats!' => \$doStats, | ||
| 73 | 'bw2w=s' => \$bw2wig_app, | ||
| 74 | 'w2bw=s' => \$wig2bw_app, | ||
| 75 | 'chromo=s' => \$chromofile, | ||
| 76 | 'db=s' => \$database, | ||
| 77 | 'h|help' => \$help, | ||
| 78 | 'v|version' => \$print_version, | ||
| 79 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 80 | |||
| 81 | ### Print help if requested | ||
| 82 | if ($help) { | ||
| 83 | |||
| 84 | # print entire POD | ||
| 85 | pod2usage( | ||
| 86 | { | ||
| 87 | '-verbose' => 2, | ||
| 88 | '-exitval' => 1, | ||
| 89 | } | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | ### Print version | ||
| 94 | if ($print_version) { | ||
| 95 | print " Biotoolbox script manipulate_datasets.pl, version $VERSION\n"; | ||
| 96 | printf " Biotoolbox package version %s\n", Bio::ToolBox->VERSION; | ||
| 97 | exit; | ||
| 98 | } | ||
| 99 | |||
| 100 | ### Checks | ||
| 101 | unless ($infile) { | ||
| 102 | if (@ARGV) { | ||
| 103 | $infile = shift @ARGV; | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | print STDERR " FATAL: no input file! use --help for more information\n"; | ||
| 107 | exit 1; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | my $doMin = defined $minValue ? 1 : 0; | ||
| 111 | my $doMax = defined $maxValue ? 1 : 0; | ||
| 112 | if ($logValue) { | ||
| 113 | $logValue = $logValue == 2 ? log(2) : $logValue == 10 ? log(10) : undef; | ||
| 114 | die "bad log value!\n" unless defined $logValue; | ||
| 115 | } | ||
| 116 | if ( defined $places ) { | ||
| 117 | $places = '%.' . $places . 'f'; | ||
| 118 | } | ||
| 119 | |||
| 120 | # chromosome skipping regex | ||
| 121 | my ( $skip_regex, $apply_regex ); | ||
| 122 | if ($skip) { | ||
| 123 | $skip_regex = qr($skip); | ||
| 124 | } | ||
| 125 | if ($apply) { | ||
| 126 | $apply_regex = qr($apply); | ||
| 127 | } | ||
| 128 | |||
| 129 | ### Open file handles | ||
| 130 | |||
| 131 | # Input | ||
| 132 | my ( $infh, $outfh ); | ||
| 133 | if ( $infile =~ /^stdin$/i ) { | ||
| 134 | $infh = IO::Handle->new; | ||
| 135 | $infh->fdopen( fileno(STDIN), 'r' ); | ||
| 136 | } | ||
| 137 | elsif ( $infile =~ m/(?: bw | bigwig )$/xi and -e $infile ) { | ||
| 138 | $infh = open_bigwig_to_wig_fh( | ||
| 139 | bw => $infile, | ||
| 140 | bwapppath => $bw2wig_app, | ||
| 141 | ) or die "unable to open input bigWig file '$infile'!\n"; | ||
| 142 | } | ||
| 143 | elsif ( -e $infile ) { | ||
| 144 | $infh = Bio::ToolBox->read_file($infile) | ||
| 145 | or die "can't open $infile!"; | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | die "unrecognized $infile!"; | ||
| 149 | } | ||
| 150 | |||
| 151 | # Output | ||
| 152 | my $bw_post_convert; | ||
| 153 | if ($outfile) { | ||
| 154 | if ( $outfile =~ /^stdout$/i ) { | ||
| 155 | $outfh = IO::Handle->new; | ||
| 156 | $outfh->fdopen( fileno(STDOUT), 'w' ); | ||
| 157 | } | ||
| 158 | elsif ( $outfile =~ m/(?: bw | bigwig ) $/xi ) { | ||
| 159 | |||
| 160 | # find external app | ||
| 161 | my $do_bw; | ||
| 162 | unless ($wig2bw_app) { | ||
| 163 | $wig2bw_app = get_wig_to_bigwig_app(); | ||
| 164 | } | ||
| 165 | if ($wig2bw_app) { | ||
| 166 | if ( check_wigToBigWig_version($wig2bw_app) ) { | ||
| 167 | |||
| 168 | # this supports direct pipe | ||
| 169 | $bw_post_convert = 0; | ||
| 170 | $do_bw = 1; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | # must write wig file and convert afterwards | ||
| 174 | $bw_post_convert = 1; | ||
| 175 | $do_bw = 0; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | print "external wigToBigWig utility not available, writing to wig\n"; | ||
| 180 | $bw_post_convert = 0; | ||
| 181 | $do_bw = 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | # check for chromosome file | ||
| 185 | if ( $do_bw or $bw_post_convert ) { | ||
| 186 | unless ($chromofile) { | ||
| 187 | if ($database) { | ||
| 188 | |||
| 189 | # this is ok | ||
| 190 | } | ||
| 191 | elsif ( not $database and $infile =~ m/(?: bw | bigwig )$/xi ) { | ||
| 192 | |||
| 193 | # use input bigwig as database | ||
| 194 | $database = $infile; | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | die "unable to write output bigWig without chromosome information!\n"; | ||
| 198 | } | ||
| 199 | $chromofile = generate_chromosome_file( $database, $skip ) | ||
| 200 | or die "unable to generate chromosome file from '$database'!\n"; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | # open file handle appropriately | ||
| 205 | if ($do_bw) { | ||
| 206 | $outfh = open_wig_to_bigwig_fh( | ||
| 207 | bw => $outfile, | ||
| 208 | chromo => $chromofile, | ||
| 209 | bwapppath => $wig2bw_app, | ||
| 210 | ) or die "unable to open output bigWig file '$outfile'!\n"; | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | $outfile =~ s/(?: bw | bigwig ) $/wig/xi; | ||
| 214 | $outfh = Bio::ToolBox->write_file($outfile) | ||
| 215 | or die "can't open $outfile!"; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | else { | ||
| 219 | $outfh = Bio::ToolBox->write_file($outfile) | ||
| 220 | or die "can't open $outfile!"; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | ### stats hash | ||
| 225 | my $stats = { | ||
| 226 | count => 0, | ||
| 227 | sumData => 0, | ||
| 228 | sumSquares => 0, | ||
| 229 | minVal => undef, | ||
| 230 | maxVal => undef, | ||
| 231 | }; | ||
| 232 | |||
| 233 | ### Walk through the file | ||
| 234 | my $count = 0; | ||
| 235 | my $span = 1; | ||
| 236 | my $chrom_skip = 0; | ||
| 237 | my $chrom_ignore = 0; | ||
| 238 | my $wig_process_sub; | ||
| 239 | while ( my $line = $infh->getline ) { | ||
| 240 | |||
| 241 | # look at the first characters to determine the type of line we have | ||
| 242 | my $prefix = lc substr( $line, 0, 5 ); | ||
| 243 | if ( $prefix eq 'track' ) { | ||
| 244 | |||
| 245 | # track line | ||
| 246 | $outfh->print($line) if $outfh; | ||
| 247 | next; | ||
| 248 | } | ||
| 249 | elsif ( $prefix eq 'brows' ) { | ||
| 250 | |||
| 251 | # browser line | ||
| 252 | $outfh->print($line) if $outfh; | ||
| 253 | next; | ||
| 254 | } | ||
| 255 | elsif ( $prefix eq 'varia' or $prefix eq 'fixed' ) { | ||
| 256 | |||
| 257 | # a step definition line | ||
| 258 | if ( $line =~ m/chrom=( [\w\-\.]+ )/x ) { | ||
| 259 | |||
| 260 | # check the chromosome | ||
| 261 | my $chrom = $1; | ||
| 262 | if ( $skip_regex and $chrom =~ $skip_regex ) { | ||
| 263 | $chrom_skip = 1; | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | $chrom_skip = 0; | ||
| 267 | } | ||
| 268 | if ( $apply and $chrom !~ $apply_regex ) { | ||
| 269 | $chrom_ignore = 1; | ||
| 270 | } | ||
| 271 | else { | ||
| 272 | $chrom_ignore = 0; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | if ( $line =~ /span=(\d+)/i ) { | ||
| 276 | |||
| 277 | # capture span size if present | ||
| 278 | $span = $1; | ||
| 279 | } | ||
| 280 | $outfh->print($line) if $outfh; | ||
| 281 | next; | ||
| 282 | } | ||
| 283 | elsif ( substr( $prefix, 0, 1 ) eq '#' ) { | ||
| 284 | |||
| 285 | # comment line | ||
| 286 | $outfh->print($line) if $outfh; | ||
| 287 | next; | ||
| 288 | } | ||
| 289 | |||
| 290 | # skipping current chromosome | ||
| 291 | next if $chrom_skip; | ||
| 292 | |||
| 293 | # ignoring current chromosome | ||
| 294 | if ($chrom_ignore) { | ||
| 295 | $outfh->print($line) if $outfh; | ||
| 296 | next; | ||
| 297 | } | ||
| 298 | |||
| 299 | # determine format | ||
| 300 | unless ( defined $wig_process_sub ) { | ||
| 301 | my @data = split /\s+/, $line; | ||
| 302 | my $statement; | ||
| 303 | if ( scalar @data == 4 ) { | ||
| 304 | $statement = " processing bedGraph...\n"; | ||
| 305 | $wig_process_sub = \&process_bedGraph; | ||
| 306 | } | ||
| 307 | elsif ( scalar @data == 2 ) { | ||
| 308 | $statement = " processing variableStep...\n"; | ||
| 309 | $wig_process_sub = \&process_variableStep; | ||
| 310 | } | ||
| 311 | elsif ( scalar @data == 1 ) { | ||
| 312 | $statement = " processing fixedStep...\n"; | ||
| 313 | $wig_process_sub = \&process_fixedStep; | ||
| 314 | } | ||
| 315 | if ( $outfile and $outfile =~ /stdout/i ) { | ||
| 316 | print STDERR $statement; | ||
| 317 | } | ||
| 318 | else { | ||
| 319 | print STDOUT $statement; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | # process | ||
| 324 | chomp $line; | ||
| 325 | &{$wig_process_sub}($line); | ||
| 326 | } | ||
| 327 | |||
| 328 | ### close filehandles | ||
| 329 | $infh->close; | ||
| 330 | $outfh->close if $outfh; | ||
| 331 | |||
| 332 | # convert as necessary | ||
| 333 | if ($bw_post_convert) { | ||
| 334 | my $final_bw = wig_to_bigwig_conversion( | ||
| 335 | wig => $outfile, | ||
| 336 | chromo => $chromofile, | ||
| 337 | bwapppath => $wig2bw_app, | ||
| 338 | ); | ||
| 339 | if ($final_bw) { | ||
| 340 | unlink $outfile; | ||
| 341 | $outfile = $final_bw; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | # remove chromosome file if we generated it | ||
| 346 | if ( $outfile | ||
| 347 | and $outfile =~ m/(?: bw | bigwig )$/xi | ||
| 348 | and $database | ||
| 349 | and $chromofile =~ m/^chr_sizes_\w{5}$/x ) | ||
| 350 | { | ||
| 351 | unlink $chromofile; | ||
| 352 | } | ||
| 353 | |||
| 354 | ### Print final messages | ||
| 355 | my $statMessage; | ||
| 356 | if ($doStats) { | ||
| 357 | my $basecount = $stats->{count} || 0; | ||
| 358 | my $min = $stats->{minVal} || 0; | ||
| 359 | my $max = $stats->{maxVal} || q(); | ||
| 360 | my $mean = | ||
| 361 | $stats->{count} ? sprintf( "%.05f", $stats->{sumData} / $stats->{count} ) : 0; | ||
| 362 | my $stddev = sprintf( "%.05f", sqrt( binVariance() ) ); | ||
| 363 | $statMessage = <<STATS; | ||
| 364 | basesCovered: $basecount | ||
| 365 | mean: $mean | ||
| 366 | min: $min | ||
| 367 | max: $max | ||
| 368 | std: $stddev | ||
| 369 | STATS | ||
| 370 | } | ||
| 371 | |||
| 372 | if ( $outfile and $outfile =~ /stdout/i ) { | ||
| 373 | print STDERR " converted $count lines, wrote file $outfile\n" if $outfile; | ||
| 374 | print STDERR $statMessage if $statMessage; | ||
| 375 | } | ||
| 376 | else { | ||
| 377 | print STDOUT " converted $count lines, wrote file $outfile\n" if $outfile; | ||
| 378 | print STDOUT $statMessage if $statMessage; | ||
| 379 | } | ||
| 380 | |||
| 381 | #### Subroutines | ||
| 382 | |||
| 383 | sub process_bedGraph { | ||
| 384 | my @data = split /\t/, shift; | ||
| 385 | return if ( $skip and $data[0] =~ $skip_regex ); | ||
| 386 | if ( $apply and $data[0] !~ $apply_regex ) { | ||
| 387 | $outfh->printf( "%s\t%d\t%d\t%s\n", $data[0], $data[1], $data[2], $data[3] ) | ||
| 388 | if $outfh; | ||
| 389 | return; | ||
| 390 | } | ||
| 391 | $data[3] = process_score( $data[3] ); | ||
| 392 | return if not defined $data[3]; | ||
| 393 | $outfh->printf( "%s\t%d\t%d\t%s\n", $data[0], $data[1], $data[2], $data[3] ) | ||
| 394 | if ( defined $data[3] and $outfh ); | ||
| 395 | $count++; | ||
| 396 | if ( $doStats and defined $data[3] ) { | ||
| 397 | my $length = $data[2] - $data[1]; | ||
| 398 | $stats->{count} += $length; | ||
| 399 | $stats->{sumData} += ( $length * $data[3] ); | ||
| 400 | $stats->{sumSquares} += ( ( $data[3]**2 ) * $length ); | ||
| 401 | $stats->{minVal} = $data[3] if not defined $stats->{minVal}; | ||
| 402 | $stats->{minVal} = $data[3] if $data[3] < $stats->{minVal}; | ||
| 403 | $stats->{maxVal} = $data[3] if not defined $stats->{maxVal}; | ||
| 404 | $stats->{maxVal} = $data[3] if $data[3] > $stats->{maxVal}; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | sub process_variableStep { | ||
| 409 | my @data = split /\s+/, shift; # could be either tab or space | ||
| 410 | $data[1] = process_score( $data[1] ); | ||
| 411 | $outfh->printf( "%d %s\n", $data[0], $data[1] ) if ( defined $data[1] and $outfh ); | ||
| 412 | $count++; | ||
| 413 | process_step_stats( $data[1] ) if $doStats; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub process_fixedStep { | ||
| 417 | my $score = shift; | ||
| 418 | $score = process_score($score); | ||
| 419 | $outfh->printf( "%s\n", $score ) if ( defined $score and $outfh ); | ||
| 420 | $count++; | ||
| 421 | process_step_stats($score) if $doStats; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub process_score { | ||
| 425 | my $v = shift; # score | ||
| 426 | if ( $doNull and $v =~ m/^(?:n.?[na]) | (?:\-?inf) $/xi ) { $v = 0 } | ||
| 427 | if ($deLogValue) { $v = $deLogValue**$v } | ||
| 428 | if ($doAbsolute) { $v = abs($v) } | ||
| 429 | if ($multiplyValue) { $v *= $multiplyValue } | ||
| 430 | if ($addValue) { $v += $addValue } | ||
| 431 | if ($logValue) { $v = $v == 0 ? 0 : log($v) / $logValue } | ||
| 432 | if ( $doMin and $v < $minValue ) { $v = $minValue } | ||
| 433 | if ( $doMax and $v > $maxValue ) { $v = $maxValue } | ||
| 434 | if ($places) { $v = sprintf( $places, $v ) } | ||
| 435 | return undef if ( $noZeroes and $v == 0 ); | ||
| 436 | return $v; | ||
| 437 | } | ||
| 438 | |||
| 439 | sub process_step_stats { | ||
| 440 | return unless defined $_[0]; | ||
| 441 | for ( 1 .. $span ) { | ||
| 442 | $stats->{count} += 1; | ||
| 443 | $stats->{sumData} += $_[0]; | ||
| 444 | $stats->{sumSquares} += $_[0]**2; | ||
| 445 | $stats->{minVal} = $_[0] if not defined $stats->{minVal}; | ||
| 446 | $stats->{maxVal} = $_[0] if not defined $stats->{maxVal}; | ||
| 447 | $stats->{minVal} = $_[0] if $_[0] < $stats->{minVal}; | ||
| 448 | $stats->{maxVal} = $_[0] if $_[0] > $stats->{maxVal}; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | sub binVariance { | ||
| 453 | return 0 unless $stats->{count}; | ||
| 454 | my $var = $stats->{sumSquares} - $stats->{sumData}**2 / $stats->{count}; | ||
| 455 | if ( $stats->{count} > 1 ) { | ||
| 456 | $var /= $stats->{count} - 1; | ||
| 457 | } | ||
| 458 | return 0 if $var < 0; | ||
| 459 | return $var; | ||
| 460 | } | ||
| 461 | |||
| 462 | __END__ |