| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # documentation at end of file | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use English qw(-no_match_vars); | ||
| 8 | use Getopt::Long qw(:config no_ignore_case bundling); | ||
| 9 | use Pod::Usage; | ||
| 10 | use Scalar::Util qw(looks_like_number); | ||
| 11 | use Bio::ToolBox::Data; | ||
| 12 | use Bio::ToolBox::db_helper qw( | ||
| 13 | open_db_connection | ||
| 14 | verify_or_request_feature_types | ||
| 15 | check_dataset_for_rpm_support | ||
| 16 | calculate_score | ||
| 17 | use_minimum_mapq | ||
| 18 | $BAM_ADAPTER | ||
| 19 | $BIG_ADAPTER | ||
| 20 | ); | ||
| 21 | use Bio::ToolBox::utility qw(format_with_commas simplify_dataset_name); | ||
| 22 | |||
| 23 | my $parallel; | ||
| 24 | eval { | ||
| 25 | # check for parallel support | ||
| 26 | require Parallel::ForkManager; | ||
| 27 | $parallel = 1; | ||
| 28 | }; | ||
| 29 | |||
| 30 | our $VERSION = '2.03'; | ||
| 31 | |||
| 32 | print "\n A program to collect data for a list of features\n\n"; | ||
| 33 | |||
| 34 | ### Display quick help | ||
| 35 | unless (@ARGV) { | ||
| 36 | |||
| 37 | # print SYNOPSIS | ||
| 38 | pod2usage( | ||
| 39 | { | ||
| 40 | '-verbose' => 0, | ||
| 41 | '-exitval' => 1, | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | |||
| 46 | ### Get command line options and initialize values | ||
| 47 | |||
| 48 | # Initialize values | ||
| 49 | my ( | ||
| 50 | $infile, $new, $parse, $outfile, | ||
| 51 | $main_database, $data_database, $feature, $method, | ||
| 52 | $stranded, $subfeature, $exon_subfeature, $extend, | ||
| 53 | $start_adj, $stop_adj, $fstart, $fstop, | ||
| 54 | $limit, $position, $fpkm_method, $tpm, | ||
| 55 | $format, $win, $step, $exclusion_file, | ||
| 56 | $chrskip, $prefix_text, $set_strand, $discard, | ||
| 57 | $gz, $cpu, $help, $print_version, | ||
| 58 | $mapq | ||
| 59 | ); | ||
| 60 | my @datasets; # an array of names of dataset values to be retrieved | ||
| 61 | |||
| 62 | # Command line options | ||
| 63 | GetOptions( | ||
| 64 | 'i|in=s' => \$infile, # load a pre-existing file | ||
| 65 | 'new' => \$new, # generate a new file | ||
| 66 | 'parse!' => \$parse, # parse input file | ||
| 67 | 'o|out=s' => \$outfile, # name of new output file | ||
| 68 | 'd|db=s' => \$main_database, # main or annotation database name | ||
| 69 | 'D|ddb=s' => \$data_database, # data database | ||
| 70 | 'f|feature=s' => \$feature, # name of genomic feature to analyze | ||
| 71 | 'm|method=s' => \$method, # method of collecting & reporting data | ||
| 72 | 'a|data=s' => \@datasets, # the list of datasets to collect data from | ||
| 73 | 't|strand=s' => \$stranded, # indicate strandedness of data | ||
| 74 | 'u|subfeature=s' => \$subfeature, # indicate to restrict to subfeatures | ||
| 75 | 'exons!' => \$exon_subfeature, # old parameter | ||
| 76 | 'x|extend=i' => \$extend, # extend the size of the genomic feature | ||
| 77 | 'b|begin|start=i' => \$start_adj, # adjustment to relative position | ||
| 78 | 'e|end|stop=i' => \$stop_adj, # adjustment relative position | ||
| 79 | 'fstart=f' => \$fstart, # fractional start position | ||
| 80 | 'fstop=f' => \$fstop, # fractional stop position | ||
| 81 | 'limit=i' => \$limit, # size limit to fractionate a feature | ||
| 82 | 'p|pos=s' => \$position, # set the relative feature position | ||
| 83 | 'fpkm|rpkm=s' => \$fpkm_method, # set the fpkm method | ||
| 84 | 'tpm!' => \$tpm, # calculate a tpm | ||
| 85 | 'r|format=i' => \$format, # decimal formatting | ||
| 86 | 'mapq=i' => \$mapq, # minimum map quality | ||
| 87 | 'win=i' => \$win, # indicate the size of genomic intervals | ||
| 88 | 'step=i' => \$step, # step size for genomic intervals | ||
| 89 | 'blacklist=s' => \$exclusion_file, # exclusion intervals for genomic intervals | ||
| 90 | 'chrskip=s' => \$chrskip, # chromosome exclusion regex for genomic intervals | ||
| 91 | 'prefix=s' => \$prefix_text, # prefix text for naming genomic intervals | ||
| 92 | 'force_strand|set_strand' => \$set_strand, # enforce a specific strand | ||
| 93 | # force_strand is preferred option, but respect the old option | ||
| 94 | 'discard=f' => \$discard, # discard feature below threshold | ||
| 95 | 'z|gz!' => \$gz, # compress output file | ||
| 96 | 'c|cpu=i' => \$cpu, # number of execution threads | ||
| 97 | 'h|help' => \$help, # request help | ||
| 98 | 'v|version' => \$print_version, # print the version | ||
| 99 | 'bam=s' => \$BAM_ADAPTER, # explicitly set the bam adapter | ||
| 100 | 'big=s' => \$BIG_ADAPTER, # explicitly set the big adapter | ||
| 101 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 102 | |||
| 103 | # print help if requested | ||
| 104 | if ($help) { | ||
| 105 | |||
| 106 | # print entire POD | ||
| 107 | pod2usage( | ||
| 108 | { | ||
| 109 | '-verbose' => 2, | ||
| 110 | '-exitval' => 1, | ||
| 111 | } | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | # Print version | ||
| 116 | if ($print_version) { | ||
| 117 | print " Biotoolbox script get_datasets.pl, version $VERSION\n"; | ||
| 118 | eval { | ||
| 119 | require Bio::ToolBox; | ||
| 120 | my $v = Bio::ToolBox->VERSION; | ||
| 121 | print " Biotoolbox package version $v\n"; | ||
| 122 | }; | ||
| 123 | exit; | ||
| 124 | } | ||
| 125 | |||
| 126 | # Assign default values | ||
| 127 | my $formatter; | ||
| 128 | set_defaults(); | ||
| 129 | my $start_time = time; | ||
| 130 | my $collapsed = 0; # global value to indicate transcripts are collapsed | ||
| 131 | |||
| 132 | ### Initialize main data, database, and datasets | ||
| 133 | |||
| 134 | # Generate or open Data table | ||
| 135 | my $Data; | ||
| 136 | if ($infile) { | ||
| 137 | $Data = Bio::ToolBox::Data->new( | ||
| 138 | file => $infile, | ||
| 139 | parse => $parse, | ||
| 140 | feature => $feature, | ||
| 141 | subfeature => $subfeature, | ||
| 142 | ) or die " unable to load input file '$infile'\n"; | ||
| 143 | if ( $Data->last_row ) { | ||
| 144 | printf " Loaded %s features from $infile.\n", | ||
| 145 | format_with_commas( $Data->last_row ); | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | print STDERR " FATAL: No features loaded from file '$infile'!\n"; | ||
| 149 | exit 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | # update main database as necessary | ||
| 153 | if ($main_database) { | ||
| 154 | if ( $main_database ne $Data->database ) { | ||
| 155 | |||
| 156 | # update with new database | ||
| 157 | printf " updating main database name from '%s' to '%s'\n", | ||
| 158 | $Data->database, $main_database; | ||
| 159 | $Data->database($main_database); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | elsif ( $Data->database ) { | ||
| 163 | $main_database = $Data->database if $Data->database !~ /^Parsed/; | ||
| 164 | } | ||
| 165 | |||
| 166 | # update feature type as necessary | ||
| 167 | if ( not defined $Data->feature | ||
| 168 | and not defined $Data->type_column | ||
| 169 | and defined $feature ) | ||
| 170 | { | ||
| 171 | $Data->feature($feature); | ||
| 172 | } | ||
| 173 | |||
| 174 | # set headers to true if file was explicitly not parsed | ||
| 175 | # since warnings will occur when we add columns and write to a file | ||
| 176 | if ( $parse == 0 ) { | ||
| 177 | $Data->headers(1); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | elsif ($new) { | ||
| 181 | |||
| 182 | # generate a new file | ||
| 183 | print " Generating a new feature list from database '$main_database'...\n"; | ||
| 184 | $Data = Bio::ToolBox::Data->new( | ||
| 185 | db => $main_database, | ||
| 186 | feature => $feature, | ||
| 187 | win => $win, | ||
| 188 | step => $step, | ||
| 189 | chrskip => $chrskip, | ||
| 190 | exclude => $exclusion_file | ||
| 191 | ) or die " unable to generate new feature list\n"; | ||
| 192 | |||
| 193 | # add a genomic interval name | ||
| 194 | if ( $feature eq 'genome' and $prefix_text ) { | ||
| 195 | my $i = $Data->add_column('Name'); | ||
| 196 | $Data->iterate( | ||
| 197 | sub { | ||
| 198 | my $row = shift; | ||
| 199 | $row->value( $i, sprintf( "%s%d", $prefix_text, $row->row_index ) ); | ||
| 200 | } | ||
| 201 | ); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | # update program name | ||
| 206 | $Data->program("$PROGRAM_NAME, v $VERSION"); | ||
| 207 | |||
| 208 | # Check output file name | ||
| 209 | unless ($outfile) { | ||
| 210 | if ( $Data->filename ) { | ||
| 211 | |||
| 212 | # reuse the input filename and overwrite | ||
| 213 | $outfile = $Data->filename; | ||
| 214 | } | ||
| 215 | elsif ( $Data->basename ) { | ||
| 216 | |||
| 217 | # no filename but a basename is indicative of a parsed file | ||
| 218 | # so stick a txt extension on it, and write in current directory | ||
| 219 | $outfile = sprintf "%s.txt", $Data->basename; | ||
| 220 | } | ||
| 221 | else { | ||
| 222 | print STDERR " FATAL: No output file provided!\n"; | ||
| 223 | exit 1; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | # Open data database | ||
| 228 | my $ddb; | ||
| 229 | if ( defined $data_database ) { | ||
| 230 | $ddb = open_db_connection($data_database) | ||
| 231 | or die "unable to establish data database connection to $data_database!\n"; | ||
| 232 | } | ||
| 233 | |||
| 234 | # Check the datasets | ||
| 235 | unless ( $datasets[0] eq 'none' ) { | ||
| 236 | @datasets = verify_or_request_feature_types( | ||
| 237 | 'db' => $ddb || $Data->database, | ||
| 238 | 'feature' => [@datasets], | ||
| 239 | 'prompt' => " Enter the dataset(s) or feature type(s) from which \n" | ||
| 240 | . " to collect data. Comma delimited or range is acceptable\n", | ||
| 241 | ); | ||
| 242 | } | ||
| 243 | unless (@datasets) { | ||
| 244 | print STDERR | ||
| 245 | " FATAL: No verifiable datasets provided. Check your file path, database, or dataset.\n"; | ||
| 246 | exit 1; | ||
| 247 | } | ||
| 248 | |||
| 249 | # Working with RPKM value datasets | ||
| 250 | my %dataset2sum; # for genomic totals of reads for rpkm determination | ||
| 251 | if ( $fpkm_method and $fpkm_method eq 'genome' ) { | ||
| 252 | foreach my $d (@datasets) { | ||
| 253 | print " Summing total reads for dataset '$d'...\n"; | ||
| 254 | my $sum = check_dataset_for_rpm_support( $d, $cpu ); | ||
| 255 | if ($sum) { | ||
| 256 | $dataset2sum{$d} = $sum; | ||
| 257 | printf " %s total features\n", format_with_commas($sum); | ||
| 258 | } | ||
| 259 | else { | ||
| 260 | die " $method method requested but not supported for " | ||
| 261 | . "dataset $d!\n FPKM is only supported with Bam and BigBed.\n"; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | ### Collect the data from each datasets | ||
| 267 | |||
| 268 | # check that we have a dataset | ||
| 269 | if ( $datasets[0] eq 'none' ) { | ||
| 270 | print " Nothing to collect!\n"; | ||
| 271 | if ($new) { | ||
| 272 | my $success = $Data->save( | ||
| 273 | 'filename' => $outfile, | ||
| 274 | 'gz' => $gz, | ||
| 275 | ); | ||
| 276 | if ($success) { | ||
| 277 | printf " wrote file $success\n"; | ||
| 278 | } | ||
| 279 | else { | ||
| 280 | # failure! the subroutine will have printed error messages | ||
| 281 | print " unable to write file!\n"; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | exit; | ||
| 285 | } | ||
| 286 | |||
| 287 | # check whether it is worth doing parallel execution | ||
| 288 | if ( $cpu > 1 and ( $Data->number_rows / $cpu ) < 100 ) { | ||
| 289 | while ( $cpu > 1 and ( $Data->number_rows / $cpu ) < 100 ) { | ||
| 290 | |||
| 291 | # We need at least 100 lines in each fork split to make | ||
| 292 | # it worthwhile to do the split, otherwise, reduce the number of | ||
| 293 | # splits to something more worthwhile | ||
| 294 | # dang it! it all depends on what we're collecting from | ||
| 295 | # bw mean goes super fast, but bam rpkms are really slow | ||
| 296 | $cpu--; | ||
| 297 | } | ||
| 298 | print " Reducing number of process forks to $cpu due to number of input features\n"; | ||
| 299 | } | ||
| 300 | |||
| 301 | # execute data collection in 1 or more processes | ||
| 302 | if ( $cpu > 1 ) { | ||
| 303 | |||
| 304 | # parallel execution | ||
| 305 | # print statements here before we fork, less we have duplicate statements! | ||
| 306 | print " Collecting $method scores from datasets @datasets\n"; | ||
| 307 | print " Forking into $cpu children for parallel data collection...\n"; | ||
| 308 | parallel_execution(); | ||
| 309 | } | ||
| 310 | else { | ||
| 311 | # single threaded execution | ||
| 312 | single_execution(); | ||
| 313 | } | ||
| 314 | |||
| 315 | # filter out unwanted features | ||
| 316 | discard_features() if defined $discard; # handle zero | ||
| 317 | |||
| 318 | # calculate normalized values | ||
| 319 | calculate_fpkm_values() if ( $fpkm_method or $tpm ); | ||
| 320 | |||
| 321 | ### Finished | ||
| 322 | # write the output file | ||
| 323 | # we will rewrite the file after each collection | ||
| 324 | # appropriate extensions and compression should be taken care of | ||
| 325 | { | ||
| 326 | my $success = $Data->save( | ||
| 327 | 'filename' => $outfile, | ||
| 328 | 'gz' => $gz, | ||
| 329 | ); | ||
| 330 | if ($success) { | ||
| 331 | printf " wrote file $success\n"; | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | # failure! the subroutine will have printed error messages | ||
| 335 | print " unable to write file!\n"; | ||
| 336 | } | ||
| 337 | printf " Finished in %.1f minutes\n", ( time - $start_time ) / 60; | ||
| 338 | } | ||
| 339 | |||
| 340 | ############# Subroutines ###################################################### | ||
| 341 | |||
| 342 | ### Set default parameters if undefined | ||
| 343 | sub set_defaults { | ||
| 344 | |||
| 345 | # assign default values | ||
| 346 | # these are all global values that could've been assigned on the | ||
| 347 | # command line | ||
| 348 | |||
| 349 | # Check for required values | ||
| 350 | # we will let the parser set the feature value | ||
| 351 | unless ($infile) { | ||
| 352 | if ( @ARGV and not $feature ) { | ||
| 353 | |||
| 354 | # making an assumption that first unnamed variable is input file | ||
| 355 | # but only if no new file feature defined | ||
| 356 | $infile = shift @ARGV; | ||
| 357 | } | ||
| 358 | else { | ||
| 359 | # we will assume the user wants to make a new file | ||
| 360 | $new = 1; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | $parse = 1 if ( $infile and not defined $parse ); | ||
| 364 | if ($new) { | ||
| 365 | unless ($outfile) { | ||
| 366 | print STDERR " FATAL: You must define an output filename!\n"; | ||
| 367 | exit 1; | ||
| 368 | } | ||
| 369 | |||
| 370 | # database for new files checked below | ||
| 371 | } | ||
| 372 | if ( defined $start_adj or defined $stop_adj ) { | ||
| 373 | |||
| 374 | # set other to zero if not defined | ||
| 375 | $start_adj ||= 0; | ||
| 376 | $stop_adj ||= 0; | ||
| 377 | } | ||
| 378 | if ( defined $fstart or defined $fstop ) { | ||
| 379 | |||
| 380 | # set defaults in case one is not defined | ||
| 381 | $fstart ||= 0; | ||
| 382 | $fstop ||= 1; | ||
| 383 | } | ||
| 384 | |||
| 385 | # check parallel support | ||
| 386 | if ($parallel) { | ||
| 387 | $cpu ||= 4; | ||
| 388 | } | ||
| 389 | else { | ||
| 390 | # disable cores | ||
| 391 | print " disabling parallel CPU execution, no support present\n" if $cpu; | ||
| 392 | $cpu = 0; | ||
| 393 | } | ||
| 394 | |||
| 395 | # check datasets | ||
| 396 | if ( not @datasets and @ARGV ) { | ||
| 397 | @datasets = @ARGV; | ||
| 398 | } | ||
| 399 | if ( @datasets and $datasets[0] =~ /,/ ) { | ||
| 400 | |||
| 401 | # seems to be a comma delimited list, possibly more than one????? | ||
| 402 | my @list; | ||
| 403 | foreach my $d (@datasets) { | ||
| 404 | push @list, ( split /,/, $d ); | ||
| 405 | } | ||
| 406 | @datasets = @list; | ||
| 407 | } | ||
| 408 | |||
| 409 | # check method | ||
| 410 | if ($method) { | ||
| 411 | |||
| 412 | # check the method that was defined on the command line | ||
| 413 | unless ( $method =~ | ||
| 414 | m/^ (?: median | mean | stddev | min | max | range | sum | count | pcount | ncount )$/x | ||
| 415 | ) | ||
| 416 | { | ||
| 417 | print " FATAL: unknown method '$method'!\n"; | ||
| 418 | exit 1; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | else { | ||
| 422 | # set the default to use the mean | ||
| 423 | $method = 'mean'; | ||
| 424 | } | ||
| 425 | |||
| 426 | # check fpkm method | ||
| 427 | if ($fpkm_method) { | ||
| 428 | unless ( $fpkm_method eq 'region' or $fpkm_method eq 'genome' ) { | ||
| 429 | print STDERR | ||
| 430 | " FATAL: fpkm option must be one of 'region' or 'genome'! see help\n"; | ||
| 431 | exit 1; | ||
| 432 | } | ||
| 433 | unless ( $method =~ /count|sum/ ) { | ||
| 434 | print STDERR " FATAL: method must be a count if you use the fpkm option!\n"; | ||
| 435 | exit 1; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | # check strandedness of data to collect | ||
| 440 | if ( defined $stranded ) { | ||
| 441 | |||
| 442 | # check the strand request that was defined on the command line | ||
| 443 | unless ( $stranded =~ m/^ (?: all | antisense | sense )$/xi ) { | ||
| 444 | print STDERR " FATAL: unknown strand '$stranded'!\n"; | ||
| 445 | exit 1; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | else { | ||
| 449 | # default value | ||
| 450 | $stranded = 'all'; | ||
| 451 | } | ||
| 452 | |||
| 453 | # check the relative position | ||
| 454 | if ( defined $position ) { | ||
| 455 | |||
| 456 | # check the position value | ||
| 457 | unless ( $position =~ m/^ (?: 5 | 4 | 53 | 3 | m | p )$/x ) { | ||
| 458 | print STDERR " FATAL: Unknown relative position '$position'!\n"; | ||
| 459 | exit 1; | ||
| 460 | } | ||
| 461 | |||
| 462 | # change to match internal usage as necessary | ||
| 463 | $position = 4 if $position eq 'm'; | ||
| 464 | $position = 10 if $position eq 'p'; | ||
| 465 | } | ||
| 466 | else { | ||
| 467 | # default position to use the 5' end | ||
| 468 | $position = 5; | ||
| 469 | } | ||
| 470 | |||
| 471 | # check the limit when using fractional start and stop | ||
| 472 | if ( $fstart and $fstop ) { | ||
| 473 | |||
| 474 | # fractional start and stop requested | ||
| 475 | unless ($limit) { | ||
| 476 | |||
| 477 | # set a minimum size limit on sub fractionating a feature | ||
| 478 | $limit = 10; | ||
| 479 | } | ||
| 480 | if ( $position == 4 or $position == 10 ) { | ||
| 481 | print STDERR | ||
| 482 | " FATAL: set position to 5 or 3 only when using fractional start and stop\n"; | ||
| 483 | exit 1; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | # Assign database for new feature lists | ||
| 488 | if ( $new and not defined $main_database ) { | ||
| 489 | |||
| 490 | # creating a new feature list requires a main database | ||
| 491 | # otherwise we will postpone this till after loading the input file | ||
| 492 | |||
| 493 | if ( defined $data_database ) { | ||
| 494 | |||
| 495 | # reuse the data database | ||
| 496 | $main_database = $data_database; | ||
| 497 | } | ||
| 498 | elsif ( @datasets and $feature eq 'genome' ) { | ||
| 499 | |||
| 500 | # we could use a dataset file only if we're collecting genome windows | ||
| 501 | # take the first element | ||
| 502 | $main_database = $datasets[0]; | ||
| 503 | } | ||
| 504 | elsif ( defined $infile ) { | ||
| 505 | |||
| 506 | # input could be a gene table in gff or ucsc format to be parsed | ||
| 507 | # hope for the best here.... | ||
| 508 | } | ||
| 509 | else { | ||
| 510 | print STDERR | ||
| 511 | " FATAL: You must define a database or an appropriate dataset file! see help\n"; | ||
| 512 | exit 1; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | # Check subfeatures | ||
| 517 | if ($exon_subfeature) { | ||
| 518 | |||
| 519 | # legacy option | ||
| 520 | $subfeature = 'exon'; | ||
| 521 | } | ||
| 522 | if ( $subfeature and ( defined $fstart or defined $start_adj ) ) { | ||
| 523 | print STDERR | ||
| 524 | " FATAL: Cannot modify coordinates when subfeatures are requested!\n"; | ||
| 525 | exit 1; | ||
| 526 | } | ||
| 527 | if ( $subfeature | ||
| 528 | and $subfeature !~ /^ (?: exon | cds | 5p_utr | 3p_utr | intron )$/x ) | ||
| 529 | { | ||
| 530 | print STDERR | ||
| 531 | " FATAL: unrecognized subfeature option '$subfeature'! Use exon, cds, 5p_utr 3p_utr, or intron\n"; | ||
| 532 | exit 1; | ||
| 533 | } | ||
| 534 | |||
| 535 | # generate formatter | ||
| 536 | if ( defined $format ) { | ||
| 537 | $formatter = '%.' . $format . 'f'; | ||
| 538 | } | ||
| 539 | |||
| 540 | # discard features | ||
| 541 | if ( $discard and $method !~ /count/ ) { | ||
| 542 | print " discard features best intended for count data, not $method data\n"; | ||
| 543 | } | ||
| 544 | |||
| 545 | # set minimum alignment mapping quality | ||
| 546 | if ( $method =~ /count/ and $mapq ) { | ||
| 547 | use_minimum_mapq($mapq); | ||
| 548 | } | ||
| 549 | } | ||
| 550 | |||
| 551 | sub parallel_execution { | ||
| 552 | my $pm = Parallel::ForkManager->new($cpu); | ||
| 553 | |||
| 554 | # generate base name for child processes | ||
| 555 | my $child_base_name = $outfile . ".$PID"; | ||
| 556 | |||
| 557 | # Split the input data into parts and execute in parallel in separate forks | ||
| 558 | for my $i ( 1 .. $cpu ) { | ||
| 559 | $pm->start and next; | ||
| 560 | |||
| 561 | #### In child #### | ||
| 562 | |||
| 563 | # splice the data structure | ||
| 564 | $Data->split_data( $i, $cpu ); | ||
| 565 | |||
| 566 | # re-open database objects to make them clone safe | ||
| 567 | # pass second true to avoid cached database objects | ||
| 568 | my $db = $Data->open_database(1); | ||
| 569 | if ($data_database) { | ||
| 570 | $ddb = open_db_connection( $data_database, 1 ); | ||
| 571 | } | ||
| 572 | |||
| 573 | # collapse transcripts if needed | ||
| 574 | if ( $feature | ||
| 575 | and $feature =~ /gene/i | ||
| 576 | and $subfeature | ||
| 577 | and $subfeature =~ m/exon | intron/xi ) | ||
| 578 | { | ||
| 579 | my $c = $Data->collapse_gene_transcripts; | ||
| 580 | if ( $c != $Data->last_row ) { | ||
| 581 | printf " Not all row SeqFeatures could be collapsed, %d failed\n", | ||
| 582 | $Data->last_row - $c; | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | # collect the dataset | ||
| 587 | foreach my $dataset (@datasets) { | ||
| 588 | next if $dataset eq 'none'; | ||
| 589 | collect_dataset($dataset); | ||
| 590 | } | ||
| 591 | |||
| 592 | # write out result | ||
| 593 | my $success = $Data->save( | ||
| 594 | 'filename' => sprintf( "$child_base_name.%03s", $i ), | ||
| 595 | 'gz' => 0, # faster to write without compression | ||
| 596 | ); | ||
| 597 | if ($success) { | ||
| 598 | printf " wrote child file $success\n"; | ||
| 599 | } | ||
| 600 | |||
| 601 | # Finished | ||
| 602 | $pm->finish; | ||
| 603 | } | ||
| 604 | $pm->wait_all_children; | ||
| 605 | |||
| 606 | # reassemble children files into output file | ||
| 607 | my @files = glob "$child_base_name.*"; | ||
| 608 | unless (@files) { | ||
| 609 | die "unable to find children files!\n"; | ||
| 610 | } | ||
| 611 | unless ( scalar @files == $cpu ) { | ||
| 612 | die "only found " . scalar(@files) . " child files when there should be $cpu!\n"; | ||
| 613 | } | ||
| 614 | my $count = $Data->reload_children(@files); | ||
| 615 | printf " reloaded %s features from children\n", format_with_commas($count); | ||
| 616 | } | ||
| 617 | |||
| 618 | sub single_execution { | ||
| 619 | |||
| 620 | # collapse transcripts if needed | ||
| 621 | if ( $feature | ||
| 622 | and $feature =~ /gene/i | ||
| 623 | and $subfeature | ||
| 624 | and $subfeature =~ m/exon | intron/xi ) | ||
| 625 | { | ||
| 626 | my $c = $Data->collapse_gene_transcripts; | ||
| 627 | if ( $c != $Data->last_row ) { | ||
| 628 | printf " Not all row SeqFeatures could be collapsed, %d failed\n", | ||
| 629 | $Data->last_row - $c; | ||
| 630 | } | ||
| 631 | } | ||
| 632 | |||
| 633 | # collect the datasets | ||
| 634 | foreach my $dataset (@datasets) { | ||
| 635 | next if $dataset eq 'none'; | ||
| 636 | print " Collecting $method scores from dataset '$dataset'...\n"; | ||
| 637 | collect_dataset($dataset); | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | # Dataset collection | ||
| 642 | sub collect_dataset { | ||
| 643 | my $dataset = shift; | ||
| 644 | |||
| 645 | # set the new metadata for this new dataset | ||
| 646 | my $index = add_new_dataset($dataset); | ||
| 647 | |||
| 648 | # check that we have strand data if necessary | ||
| 649 | if ($set_strand) { | ||
| 650 | unless ( defined $Data->strand_column ) { | ||
| 651 | print STDERR | ||
| 652 | " FATAL: requested to set strand but a strand column was not found!\n"; | ||
| 653 | exit 1; | ||
| 654 | } | ||
| 655 | } | ||
| 656 | |||
| 657 | # collect | ||
| 658 | if ( defined $start_adj and defined $stop_adj ) { | ||
| 659 | |||
| 660 | # specifically defined relative start and stop positions | ||
| 661 | get_adjusted_dataset( $dataset, $index ); | ||
| 662 | } | ||
| 663 | elsif ( defined $fstart and defined $fstop ) { | ||
| 664 | |||
| 665 | # use a subfraction of the region | ||
| 666 | get_fractionated_dataset( $dataset, $index ); | ||
| 667 | } | ||
| 668 | else { | ||
| 669 | # everything else | ||
| 670 | get_dataset( $dataset, $index ); | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | sub get_dataset { | ||
| 675 | my ( $dataset, $index ) = @_; | ||
| 676 | |||
| 677 | # collect the scores from the dataset for this index | ||
| 678 | $Data->iterate( | ||
| 679 | sub { | ||
| 680 | my $row = shift; | ||
| 681 | my $score = $row->get_score( | ||
| 682 | 'db' => $ddb, | ||
| 683 | 'dataset' => $dataset, | ||
| 684 | 'method' => $method, | ||
| 685 | 'stranded' => $stranded, | ||
| 686 | 'extend' => $extend, | ||
| 687 | 'subfeature' => $subfeature, | ||
| 688 | ); | ||
| 689 | if ( $formatter and looks_like_number($score) ) { | ||
| 690 | $score = sprintf( $formatter, $score ); | ||
| 691 | } | ||
| 692 | $row->value( $index, $score ); | ||
| 693 | } | ||
| 694 | ); | ||
| 695 | } | ||
| 696 | |||
| 697 | sub get_adjusted_dataset { | ||
| 698 | my ( $dataset, $index ) = @_; | ||
| 699 | |||
| 700 | # collect the scores from the dataset for this index | ||
| 701 | $Data->iterate( | ||
| 702 | sub { | ||
| 703 | my $row = shift; | ||
| 704 | |||
| 705 | # adjust coordinates as requested | ||
| 706 | # depends on feature strand and relative position | ||
| 707 | my ( $start, $stop ); | ||
| 708 | if ( $position == 5 ) { | ||
| 709 | if ( $row->strand >= 0 ) { | ||
| 710 | |||
| 711 | # 5' end of forward strand | ||
| 712 | $start = $row->start + $start_adj; | ||
| 713 | $stop = $row->start + $stop_adj; | ||
| 714 | } | ||
| 715 | else { | ||
| 716 | # 5' end of reverse strand | ||
| 717 | $start = $row->end - $stop_adj; | ||
| 718 | $stop = $row->end - $start_adj; | ||
| 719 | } | ||
| 720 | } | ||
| 721 | elsif ( $position == 3 ) { | ||
| 722 | if ( $row->strand >= 0 ) { | ||
| 723 | |||
| 724 | # 3' end of forward strand | ||
| 725 | $start = $row->end + $start_adj; | ||
| 726 | $stop = $row->end + $stop_adj; | ||
| 727 | } | ||
| 728 | else { | ||
| 729 | # 3' end of reverse strand | ||
| 730 | $start = $row->start - $stop_adj; | ||
| 731 | $stop = $row->start - $start_adj; | ||
| 732 | } | ||
| 733 | } | ||
| 734 | elsif ( $position == 4 ) { | ||
| 735 | |||
| 736 | # middle position | ||
| 737 | my $middle = int( ( $row->start + $row->end ) / 2 ); | ||
| 738 | if ( $row->strand >= 0 ) { | ||
| 739 | $start = $middle + $start_adj; | ||
| 740 | $stop = $middle + $stop_adj; | ||
| 741 | } | ||
| 742 | else { | ||
| 743 | $start = $middle - $stop_adj; | ||
| 744 | $stop = $middle - $start_adj; | ||
| 745 | } | ||
| 746 | } | ||
| 747 | elsif ( $position == 53 ) { | ||
| 748 | if ( $row->strand >= 0 ) { | ||
| 749 | |||
| 750 | # forward strand | ||
| 751 | $start = $row->start + $start_adj; | ||
| 752 | $stop = $row->end + $stop_adj; | ||
| 753 | } | ||
| 754 | else { | ||
| 755 | # reverse strand | ||
| 756 | $start = $row->start - $start_adj; | ||
| 757 | $stop = $row->end - $stop_adj; | ||
| 758 | } | ||
| 759 | } | ||
| 760 | elsif ( $position == 10 ) { | ||
| 761 | |||
| 762 | # peak summit position | ||
| 763 | my $middle = $row->peak; | ||
| 764 | if ( $row->strand >= 0 ) { | ||
| 765 | $start = $middle + $start_adj; | ||
| 766 | $stop = $middle + $stop_adj; | ||
| 767 | } | ||
| 768 | else { | ||
| 769 | $start = $middle - $stop_adj; | ||
| 770 | $stop = $middle - $start_adj; | ||
| 771 | } | ||
| 772 | } | ||
| 773 | |||
| 774 | # now collect score | ||
| 775 | my $score = $row->get_score( | ||
| 776 | 'start' => $start, | ||
| 777 | 'stop' => $stop, | ||
| 778 | 'db' => $ddb, | ||
| 779 | 'dataset' => $dataset, | ||
| 780 | 'method' => $method, | ||
| 781 | 'stranded' => $stranded, | ||
| 782 | ); | ||
| 783 | if ( $formatter and looks_like_number($score) ) { | ||
| 784 | $score = sprintf( $formatter, $score ); | ||
| 785 | } | ||
| 786 | $row->value( $index, $score ); | ||
| 787 | } | ||
| 788 | ); | ||
| 789 | } | ||
| 790 | |||
| 791 | sub get_fractionated_dataset { | ||
| 792 | my ( $dataset, $index ) = @_; | ||
| 793 | |||
| 794 | # collect the scores from the dataset for this index | ||
| 795 | $Data->iterate( | ||
| 796 | sub { | ||
| 797 | my $row = shift; | ||
| 798 | |||
| 799 | # calculate length | ||
| 800 | my $length = $row->length; | ||
| 801 | |||
| 802 | # calculate new fractional start and stop positions | ||
| 803 | # the fraction depends on the length | ||
| 804 | # this depends on both feature orientation and the | ||
| 805 | # relative position requested | ||
| 806 | my $relative_start = int( ( $length * $fstart ) + 0.5 ); | ||
| 807 | my $relative_stop = int( ( $length * $fstop ) + 0.5 ); | ||
| 808 | my ( $start, $stop ); | ||
| 809 | if ( $length >= $limit ) { | ||
| 810 | |||
| 811 | # length exceeds our minimum limit | ||
| 812 | # we can take a fractional length | ||
| 813 | |||
| 814 | if ( $position == 5 and $row->strand >= 0 ) { | ||
| 815 | |||
| 816 | # 5' end of forward strand | ||
| 817 | $start = $row->start + $relative_start; | ||
| 818 | $stop = $row->start + $relative_stop; | ||
| 819 | } | ||
| 820 | elsif ( $position == 5 and $row->strand < 0 ) { | ||
| 821 | |||
| 822 | # 5' end of reverse strand | ||
| 823 | $start = $row->end - $relative_stop; | ||
| 824 | $stop = $row->end - $relative_start; | ||
| 825 | } | ||
| 826 | elsif ( $position == 3 and $row->strand >= 0 ) { | ||
| 827 | |||
| 828 | # 3' end of forward strand | ||
| 829 | $start = $row->end + $relative_start; | ||
| 830 | $stop = $row->end + $relative_stop; | ||
| 831 | } | ||
| 832 | elsif ( $position == 3 and $row->strand < 0 ) { | ||
| 833 | |||
| 834 | # 3' end of reverse strand | ||
| 835 | $start = $row->start - $relative_stop; | ||
| 836 | $stop = $row->start - $relative_start; | ||
| 837 | } | ||
| 838 | |||
| 839 | # midpoint is not accepted | ||
| 840 | } | ||
| 841 | else { | ||
| 842 | # length doesn't meet minimum limit | ||
| 843 | # simply take the whole fragment | ||
| 844 | $start = $row->start; | ||
| 845 | $stop = $row->end; | ||
| 846 | } | ||
| 847 | |||
| 848 | # now collect score | ||
| 849 | my $score = $row->get_score( | ||
| 850 | 'start' => $start, | ||
| 851 | 'stop' => $stop, | ||
| 852 | 'db' => $ddb, | ||
| 853 | 'dataset' => $dataset, | ||
| 854 | 'method' => $method, | ||
| 855 | 'stranded' => $stranded, | ||
| 856 | ); | ||
| 857 | if ( $formatter and looks_like_number($score) ) { | ||
| 858 | $score = sprintf( $formatter, $score ); | ||
| 859 | } | ||
| 860 | $row->value( $index, $score ); | ||
| 861 | } | ||
| 862 | ); | ||
| 863 | } | ||
| 864 | |||
| 865 | # subroutine to record the metadata for a dataset | ||
| 866 | sub add_new_dataset { | ||
| 867 | my $dataset = shift; | ||
| 868 | |||
| 869 | # generate column name | ||
| 870 | my $column_name = simplify_dataset_name($dataset); | ||
| 871 | |||
| 872 | # add new column | ||
| 873 | my $index = $Data->add_column($column_name); | ||
| 874 | |||
| 875 | # update metadata | ||
| 876 | $Data->metadata( $index, 'dataset', $dataset ); | ||
| 877 | $Data->metadata( $index, 'method', $method ); | ||
| 878 | $Data->metadata( $index, 'strand', $stranded ); | ||
| 879 | $Data->metadata( $index, 'extend', $extend ) if defined $extend; | ||
| 880 | $Data->metadata( $index, 'start', $start_adj ) if defined $start_adj; | ||
| 881 | $Data->metadata( $index, 'stop', $stop_adj ) if defined $stop_adj; | ||
| 882 | $Data->metadata( $index, 'fstart', $fstart ) if defined $fstart; | ||
| 883 | $Data->metadata( $index, 'fstop', $fstop ) if defined $fstop; | ||
| 884 | $Data->metadata( $index, 'limit', $limit ) if defined $limit; | ||
| 885 | $Data->metadata( $index, 'subfeature', $subfeature ) if $subfeature; | ||
| 886 | $Data->metadata( $index, 'forced_strand', 'yes' ) if $set_strand; | ||
| 887 | $Data->metadata( $index, 'decimal_format', $format ) if defined $format; | ||
| 888 | $Data->metadata( $index, 'total_reads', $dataset2sum{$dataset} ) | ||
| 889 | if exists $dataset2sum{$dataset}; | ||
| 890 | |||
| 891 | if ( $position == 5 ) { | ||
| 892 | $Data->metadata( $index, 'relative_position', "5'end" ); | ||
| 893 | } | ||
| 894 | if ( $position == 3 ) { | ||
| 895 | $Data->metadata( $index, 'relative_position', "3'end" ); | ||
| 896 | } | ||
| 897 | elsif ( $position == 4 ) { | ||
| 898 | $Data->metadata( $index, 'relative_position', 'middle' ); | ||
| 899 | } | ||
| 900 | elsif ( $position == 9 ) { | ||
| 901 | $Data->metadata( $index, 'relative_position', 'summit' ); | ||
| 902 | } | ||
| 903 | |||
| 904 | # add map quality as appropriate | ||
| 905 | if ( $dataset =~ / \. (b|cr) am $/xni and $method =~ /count/ ) { | ||
| 906 | $Data->metadata( $index, 'mapq', $mapq ); | ||
| 907 | } | ||
| 908 | |||
| 909 | # add database name if different | ||
| 910 | if ( defined $data_database ) { | ||
| 911 | $Data->metadata( $index, 'db', $data_database ); | ||
| 912 | } | ||
| 913 | |||
| 914 | # return the index number | ||
| 915 | return $index; | ||
| 916 | } | ||
| 917 | |||
| 918 | sub discard_features { | ||
| 919 | print " Discarding features with sum below $discard....\n"; | ||
| 920 | |||
| 921 | # calculate which indices; | ||
| 922 | my @indices = ( $Data->number_columns - scalar(@datasets) + 1 ) .. $Data->last_column; | ||
| 923 | |||
| 924 | # identify features to delete | ||
| 925 | my @to_delete; | ||
| 926 | $Data->iterate( | ||
| 927 | sub { | ||
| 928 | my $row = shift; | ||
| 929 | my $value = | ||
| 930 | calculate_score( 'sum', [ map { $row->value($_) } @indices ] ); | ||
| 931 | push @to_delete, $row->row_index if $value < $discard; | ||
| 932 | } | ||
| 933 | ); | ||
| 934 | |||
| 935 | # delete the features | ||
| 936 | if (@to_delete) { | ||
| 937 | $Data->delete_row(@to_delete); | ||
| 938 | printf " discarded %d features, %d remaining.\n", scalar(@to_delete), | ||
| 939 | $Data->last_row; | ||
| 940 | } | ||
| 941 | |||
| 942 | # add metadata | ||
| 943 | foreach my $i (@indices) { | ||
| 944 | $Data->metadata( $i, 'discarded_below', $discard ); | ||
| 945 | } | ||
| 946 | } | ||
| 947 | |||
| 948 | sub calculate_fpkm_values { | ||
| 949 | print " Calculating FPKM values....\n" if $fpkm_method; | ||
| 950 | print " Calculating TPM values....\n" if $tpm; | ||
| 951 | |||
| 952 | # calculate which indices; | ||
| 953 | my @indices = ( $Data->number_columns - scalar(@datasets) + 1 ) .. $Data->last_column; | ||
| 954 | |||
| 955 | # identify the length column as necessary | ||
| 956 | my $length_i; | ||
| 957 | if ($subfeature) { | ||
| 958 | if ( $subfeature eq 'exon' ) { | ||
| 959 | $length_i = $Data->find_column('Merged_Transcript_Length'); | ||
| 960 | } | ||
| 961 | elsif ( $subfeature eq 'cds' ) { | ||
| 962 | $length_i = $Data->find_column('Transcript_CDS_Length'); | ||
| 963 | } | ||
| 964 | elsif ( $subfeature eq '5p_utr' ) { | ||
| 965 | $length_i = $Data->find_column('Transcript_5pUTR_Length'); | ||
| 966 | } | ||
| 967 | elsif ( $subfeature eq '3p_utr' ) { | ||
| 968 | $length_i = $Data->find_column('Transcript_3pUTR_Length'); | ||
| 969 | } | ||
| 970 | unless ( defined $length_i ) { | ||
| 971 | $length_i = $Data->add_transcript_length($subfeature); | ||
| 972 | } | ||
| 973 | } | ||
| 974 | |||
| 975 | # work through each collected dataset | ||
| 976 | foreach my $index (@indices) { | ||
| 977 | |||
| 978 | # determine total number of reads for this dataset | ||
| 979 | my $total; | ||
| 980 | if ( $fpkm_method eq 'genome' ) { | ||
| 981 | |||
| 982 | # this was calculated earlier | ||
| 983 | $total = $dataset2sum{ $Data->metadata( $index, 'dataset' ) }; | ||
| 984 | } | ||
| 985 | elsif ( $fpkm_method eq 'region' ) { | ||
| 986 | |||
| 987 | # use the total from the counted regions or genes | ||
| 988 | $total = 0; | ||
| 989 | $Data->iterate( | ||
| 990 | sub { | ||
| 991 | my $row = shift; | ||
| 992 | $total += $row->value($index); | ||
| 993 | } | ||
| 994 | ); | ||
| 995 | } | ||
| 996 | |||
| 997 | # standard lengths | ||
| 998 | my ( $standard_length, $fractional_length ); | ||
| 999 | if ( defined $start_adj and defined $stop_adj ) { | ||
| 1000 | $standard_length = abs( $stop_adj - $start_adj ); | ||
| 1001 | } | ||
| 1002 | if ( defined $fstart and defined $fstop ) { | ||
| 1003 | $fractional_length = $fstop - $fstart; | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | # add FPKM column | ||
| 1007 | if ($fpkm_method) { | ||
| 1008 | |||
| 1009 | # add new column | ||
| 1010 | my $fpkm_index = $Data->add_column( $Data->name($index) . '_FPKM' ); | ||
| 1011 | $Data->metadata( $fpkm_index, 'dataset', | ||
| 1012 | $Data->metadata( $index, 'dataset' ) ); | ||
| 1013 | $Data->metadata( $fpkm_index, 'fpkm_method', $fpkm_method ); | ||
| 1014 | $Data->metadata( $fpkm_index, 'total_count', $total ); | ||
| 1015 | |||
| 1016 | # calculate and store the fpkms | ||
| 1017 | $Data->iterate( | ||
| 1018 | sub { | ||
| 1019 | my $row = shift; | ||
| 1020 | my $length; | ||
| 1021 | if ($standard_length) { | ||
| 1022 | $length = $standard_length; | ||
| 1023 | } | ||
| 1024 | elsif ( defined $length_i ) { | ||
| 1025 | $length = $row->value($length_i); | ||
| 1026 | } | ||
| 1027 | else { | ||
| 1028 | $length = $row->length; | ||
| 1029 | } | ||
| 1030 | $length ||= 1; # why would the length be zero? | ||
| 1031 | if ($fractional_length) { | ||
| 1032 | if ( defined $limit ) { | ||
| 1033 | $length = int( $length * $fractional_length ) | ||
| 1034 | if $length >= $limit; | ||
| 1035 | } | ||
| 1036 | else { | ||
| 1037 | $length = int( $length * $fractional_length ); | ||
| 1038 | } | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | my $fpkm = | ||
| 1042 | ( $row->value($index) * 1_000_000_000 ) / ( $length * $total ); | ||
| 1043 | |||
| 1044 | # this is equivalent to traditional way: count / ( (total/1e6) * (length/1000) ) | ||
| 1045 | $row->value( $fpkm_index, sprintf( "%.3f", $fpkm ) ); | ||
| 1046 | } | ||
| 1047 | ); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | # add TPM column | ||
| 1051 | if ($tpm) { | ||
| 1052 | |||
| 1053 | # add new column | ||
| 1054 | my $tpm_index = $Data->add_column( $Data->name($index) . '_TPM' ); | ||
| 1055 | $Data->metadata( $tpm_index, 'dataset', | ||
| 1056 | $Data->metadata( $index, 'dataset' ) ); | ||
| 1057 | |||
| 1058 | # first calculate the length normalized counts | ||
| 1059 | my $tpm_total = 0; | ||
| 1060 | $Data->iterate( | ||
| 1061 | sub { | ||
| 1062 | my $row = shift; | ||
| 1063 | my $length; | ||
| 1064 | if ($standard_length) { | ||
| 1065 | $length = $standard_length; | ||
| 1066 | } | ||
| 1067 | elsif ( defined $length_i ) { | ||
| 1068 | $length = $row->value($length_i); | ||
| 1069 | } | ||
| 1070 | else { | ||
| 1071 | $length = $row->length; | ||
| 1072 | } | ||
| 1073 | $length ||= 1; # why would the length be zero? | ||
| 1074 | if ($fractional_length) { | ||
| 1075 | if ( defined $limit ) { | ||
| 1076 | $length = int( $length * $fractional_length ) | ||
| 1077 | if $length >= $limit; | ||
| 1078 | } | ||
| 1079 | else { | ||
| 1080 | $length = int( $length * $fractional_length ); | ||
| 1081 | } | ||
| 1082 | } | ||
| 1083 | my $tpm_value = $row->value($index) / ( $length / 1000 ); | ||
| 1084 | $row->value( $tpm_index, $tpm_value ); | ||
| 1085 | $tpm_total += $tpm_value; | ||
| 1086 | } | ||
| 1087 | ); | ||
| 1088 | |||
| 1089 | # then scale to million reads | ||
| 1090 | my $scale = $tpm_total / 1000000; | ||
| 1091 | $Data->iterate( | ||
| 1092 | sub { | ||
| 1093 | my $row = shift; | ||
| 1094 | my $tpm_value = $row->value($tpm_index) / $scale; | ||
| 1095 | $row->value( $tpm_index, sprintf( "%.3f", $tpm_value ) ); | ||
| 1096 | } | ||
| 1097 | ); | ||
| 1098 | } | ||
| 1099 | } | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | __END__ |