| 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 List::Util qw(sum0 min max); | ||
| 11 | use Statistics::Lite qw(median stddevp); | ||
| 12 | use Statistics::Descriptive; | ||
| 13 | use Bio::ToolBox::Data; | ||
| 14 | use Bio::ToolBox::db_helper qw( | ||
| 15 | open_db_connection | ||
| 16 | verify_or_request_feature_types | ||
| 17 | ); | ||
| 18 | use Bio::ToolBox::utility qw(format_with_commas); | ||
| 19 | |||
| 20 | my $parallel; | ||
| 21 | eval { | ||
| 22 | # check for parallel support | ||
| 23 | require Parallel::ForkManager; | ||
| 24 | $parallel = 1; | ||
| 25 | }; | ||
| 26 | |||
| 27 | # ANOVA Parameters for P-value Statistics | ||
| 28 | my $anova; | ||
| 29 | eval { | ||
| 30 | # check for ANOVA support | ||
| 31 | require Statistics::ANOVA; | ||
| 32 | $anova = 1; | ||
| 33 | }; | ||
| 34 | my $INDEPENDENCE = 0; | ||
| 35 | my $PARAMETRIC = 1; | ||
| 36 | my $ORDINAL = 0; | ||
| 37 | |||
| 38 | use constant LOG10 => log(10); | ||
| 39 | |||
| 40 | our $VERSION = '2.03'; | ||
| 41 | |||
| 42 | print "\n This program will correlate positions of occupancy between two datasets\n\n"; | ||
| 43 | |||
| 44 | ### Quick help | ||
| 45 | unless (@ARGV) { | ||
| 46 | |||
| 47 | # when no command line options are present | ||
| 48 | # print SYNOPSIS | ||
| 49 | pod2usage( | ||
| 50 | { | ||
| 51 | '-verbose' => 0, | ||
| 52 | '-exitval' => 1, | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | |||
| 57 | ### Get command line options and initialize values | ||
| 58 | my ( | ||
| 59 | $infile, $outfile, $database, $data_database, | ||
| 60 | $refDataSet, $testDataSet, $find_pvalue, $find_shift, | ||
| 61 | $radius, $position, $set_strand, $norm_method, | ||
| 62 | $gz, $cpu, $help, $print_version, | ||
| 63 | ); | ||
| 64 | |||
| 65 | # Command line options | ||
| 66 | GetOptions( | ||
| 67 | 'i|in=s' => \$infile, # the input data file | ||
| 68 | 'o|out=s' => \$outfile, # name of output file | ||
| 69 | 'd|db=s' => \$database, # name of database | ||
| 70 | 'D|ddb=s' => \$data_database, # database containing datasets | ||
| 71 | 'r|ref=s' => \$refDataSet, # reference dataset | ||
| 72 | 't|test=s' => \$testDataSet, # test dataset | ||
| 73 | 'pval!' => \$find_pvalue, # calculate student t-test | ||
| 74 | 'shift!' => \$find_shift, # calculate optimum shift | ||
| 75 | 'radius=i' => \$radius, # for collecting data when shifting | ||
| 76 | 'p|pos=s' => \$position, # set the relative feature position | ||
| 77 | 'force_strand|set_strand' => \$set_strand, # enforce an artificial strand | ||
| 78 | # force_strand is preferred option, but respect the old option | ||
| 79 | 'norm=s' => \$norm_method, # method of normalization | ||
| 80 | 'z|gz!' => \$gz, # compress output | ||
| 81 | 'c|cpu=i' => \$cpu, # number of execution threads | ||
| 82 | 'h|help' => \$help, # request help | ||
| 83 | 'v|version' => \$print_version, # print the version | ||
| 84 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 85 | |||
| 86 | # Print help | ||
| 87 | if ($help) { | ||
| 88 | |||
| 89 | # print entire POD | ||
| 90 | pod2usage( | ||
| 91 | { | ||
| 92 | '-verbose' => 2, | ||
| 93 | '-exitval' => 1, | ||
| 94 | } | ||
| 95 | ); | ||
| 96 | } | ||
| 97 | |||
| 98 | # Print version | ||
| 99 | if ($print_version) { | ||
| 100 | print " Biotoolbox script correlate_position_data.pl, version $VERSION\n\n"; | ||
| 101 | exit; | ||
| 102 | } | ||
| 103 | |||
| 104 | ### Check for requirements | ||
| 105 | check_defaults(); | ||
| 106 | |||
| 107 | ### Open input file | ||
| 108 | my $Data = Bio::ToolBox::Data->new( file => $infile ) | ||
| 109 | or die " unable to open input file '$infile'!\n"; | ||
| 110 | printf " Loaded %s features from $infile.\n", format_with_commas( $Data->last_row ); | ||
| 111 | $Data->program("$PROGRAM_NAME, v $VERSION"); | ||
| 112 | |||
| 113 | ### Open database connection | ||
| 114 | if ($database) { | ||
| 115 | |||
| 116 | # update or add database as required | ||
| 117 | if ( $Data->database and $database ne $Data->database ) { | ||
| 118 | |||
| 119 | # update with new database | ||
| 120 | printf " updating main database name from '%s' to '%s'\n", | ||
| 121 | $Data->database, $database; | ||
| 122 | print " Re-run without --db option if you do not want this to happen\n"; | ||
| 123 | $Data->database($database); | ||
| 124 | } | ||
| 125 | elsif ( not $Data->database ) { | ||
| 126 | $Data->database($database); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | my $ddb; | ||
| 130 | if ( defined $data_database ) { | ||
| 131 | |||
| 132 | # specifically defined a data database | ||
| 133 | $ddb = open_db_connection($data_database) | ||
| 134 | or die "unable to establish data database connection to $data_database!\n"; | ||
| 135 | } | ||
| 136 | |||
| 137 | ### Validate input datasets | ||
| 138 | validate_or_request_dataset(); | ||
| 139 | |||
| 140 | ### Collect the data correlations | ||
| 141 | my $start_time = time; | ||
| 142 | |||
| 143 | # check whether it is worth doing parallel execution | ||
| 144 | if ( $cpu > 1 ) { | ||
| 145 | while ( $cpu > 1 and $Data->last_row / $cpu < 100 ) { | ||
| 146 | |||
| 147 | # We need at least 1000 lines in each fork split to make | ||
| 148 | # it worthwhile to do the split, otherwise, reduce the number of | ||
| 149 | # splits to something more worthwhile | ||
| 150 | $cpu--; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | if ( $cpu > 1 ) { | ||
| 155 | |||
| 156 | # parallel execution | ||
| 157 | parallel_execution(); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | # single process execution | ||
| 161 | single_execution(); | ||
| 162 | } | ||
| 163 | |||
| 164 | ### Write output file | ||
| 165 | { | ||
| 166 | my $success = $Data->write_file( | ||
| 167 | 'filename' => $outfile, | ||
| 168 | 'gz' => $gz, | ||
| 169 | ); | ||
| 170 | if ($success) { | ||
| 171 | print " wrote output file $success\n"; | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | print " unable to write output file!\n"; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | printf " Finished in %.2f minutes\n", ( time - $start_time ) / 60; | ||
| 178 | |||
| 179 | ######################## Subroutines ################################### | ||
| 180 | |||
| 181 | sub check_defaults { | ||
| 182 | |||
| 183 | unless ($infile) { | ||
| 184 | if (@ARGV) { | ||
| 185 | $infile = shift @ARGV; | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | print STDERR " FATAL: no input file! use --help for more information\n"; | ||
| 189 | exit 1; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | unless ( defined $gz ) { | ||
| 193 | $gz = 0; | ||
| 194 | } | ||
| 195 | |||
| 196 | if ( defined $position ) { | ||
| 197 | |||
| 198 | # check the position value | ||
| 199 | unless ( $position eq 'm' | ||
| 200 | or $position == 5 | ||
| 201 | or $position == 3 | ||
| 202 | or $position == 4 ) | ||
| 203 | { | ||
| 204 | print STDERR " FATAL: Unknown relative position '$position'!\n"; | ||
| 205 | exit 1; | ||
| 206 | } | ||
| 207 | if ( $position eq 'm' ) { $position = 4 } # change to match internal usage | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | # default position to use the midpoint | ||
| 211 | $position = 4; | ||
| 212 | } | ||
| 213 | |||
| 214 | if ($norm_method) { | ||
| 215 | unless ( $norm_method =~ /^rank|sum$/i ) { | ||
| 216 | print STDERR " FATAL: Unrecognized normalization method! see help\n"; | ||
| 217 | exit 1; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | if ($find_pvalue) { | ||
| 222 | unless ($anova) { | ||
| 223 | print STDERR | ||
| 224 | " FATAL: Cannot calculate requested P-value! Pleast install Statistics::ANOVA\n"; | ||
| 225 | exit 1; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | if ($find_shift) { | ||
| 230 | unless ($radius) { | ||
| 231 | print STDERR | ||
| 232 | " FATAL: Must define a radius and reference point to calculate optimum shift\n"; | ||
| 233 | exit 1; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | if ($parallel) { | ||
| 238 | $cpu ||= 4; | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | # disable cores | ||
| 242 | print " WARNING: disabling parallel CPU execution, no support present\n" if $cpu; | ||
| 243 | $cpu = 0; | ||
| 244 | } | ||
| 245 | |||
| 246 | unless ($outfile) { | ||
| 247 | $outfile = $infile; | ||
| 248 | } | ||
| 249 | |||
| 250 | } | ||
| 251 | |||
| 252 | sub validate_or_request_dataset { | ||
| 253 | |||
| 254 | # Process the reference dataset | ||
| 255 | $refDataSet = verify_or_request_feature_types( | ||
| 256 | 'db' => $ddb || $Data->database, | ||
| 257 | 'feature' => $refDataSet, | ||
| 258 | 'prompt' => "Enter the reference data set ", | ||
| 259 | 'single' => 1, | ||
| 260 | ); | ||
| 261 | unless ($refDataSet) { | ||
| 262 | die " A valid reference data set must be provided!\n"; | ||
| 263 | } | ||
| 264 | |||
| 265 | # Process the test dataset | ||
| 266 | $testDataSet = verify_or_request_feature_types( | ||
| 267 | 'db' => $ddb || $Data->database, | ||
| 268 | 'feature' => $testDataSet, | ||
| 269 | 'prompt' => "Enter the test data set ", | ||
| 270 | 'single' => 1, | ||
| 271 | ); | ||
| 272 | unless ($testDataSet) { | ||
| 273 | die " A valid test data set must be provided!\n"; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | sub parallel_execution { | ||
| 278 | my $pm = Parallel::ForkManager->new($cpu); | ||
| 279 | $pm->run_on_start( sub { sleep 1; } ); | ||
| 280 | |||
| 281 | # give a chance for child to start up and open databases, files, etc | ||
| 282 | # without creating race conditions | ||
| 283 | |||
| 284 | # Prepare new columns | ||
| 285 | my ( $r_i, $p_i, $shift_i, $shiftr_i ) = add_new_columns(); | ||
| 286 | |||
| 287 | # generate base name for child processes | ||
| 288 | my $child_base_name = $outfile . ".$PID"; | ||
| 289 | |||
| 290 | # variables for reporting the summary of results | ||
| 291 | my @correlations; | ||
| 292 | my @optimal_shifts; | ||
| 293 | my @optimal_correlations; | ||
| 294 | my $count = 0; | ||
| 295 | my $not_enough_data = 0; | ||
| 296 | my $no_variance = 0; | ||
| 297 | $pm->run_on_finish( | ||
| 298 | sub { | ||
| 299 | my ( $pid, $exit_code, $ident, $exit_signal, $core_dump, $result ) = @_; | ||
| 300 | push @correlations, @{ $result->[0] }; | ||
| 301 | push @optimal_shifts, @{ $result->[1] }; | ||
| 302 | push @optimal_correlations, @{ $result->[2] }; | ||
| 303 | $count += $result->[3]; | ||
| 304 | $not_enough_data += $result->[4]; | ||
| 305 | $no_variance += $result->[5]; | ||
| 306 | } | ||
| 307 | ); | ||
| 308 | |||
| 309 | # Split the input data into parts and execute in parallel in separate forks | ||
| 310 | print " Forking into $cpu children for parallel correlation collections\n"; | ||
| 311 | for my $i ( 1 .. $cpu ) { | ||
| 312 | $pm->start and next; | ||
| 313 | |||
| 314 | #### In child #### | ||
| 315 | |||
| 316 | # splice the data structure | ||
| 317 | $Data->split_data( $i, $cpu ); | ||
| 318 | |||
| 319 | # re-open database objects to make them clone safe | ||
| 320 | my $db = $Data->open_database(1); | ||
| 321 | if ($data_database) { | ||
| 322 | $ddb = open_db_connection( $data_database, 1 ); | ||
| 323 | } | ||
| 324 | |||
| 325 | # collect | ||
| 326 | my @results = collect_correlations( $r_i, $p_i, $shift_i, $shiftr_i ); | ||
| 327 | |||
| 328 | # write output file | ||
| 329 | my $success = $Data->write_file( | ||
| 330 | 'filename' => sprintf( "$child_base_name.%03s", $i ), | ||
| 331 | 'gz' => 0, # faster to write without compression | ||
| 332 | ); | ||
| 333 | if ($success) { | ||
| 334 | print " wrote output file $success\n"; | ||
| 335 | } | ||
| 336 | else { | ||
| 337 | # failure! the subroutine will have printed error messages | ||
| 338 | die " unable to write file!\n"; | ||
| 339 | |||
| 340 | # no need to continue | ||
| 341 | } | ||
| 342 | |||
| 343 | # Finished | ||
| 344 | $pm->finish( 0, \@results ); | ||
| 345 | } | ||
| 346 | $pm->wait_all_children; | ||
| 347 | |||
| 348 | # reassemble children files into output file | ||
| 349 | my @files = glob "$child_base_name.*"; | ||
| 350 | unless (@files) { | ||
| 351 | die "unable to find children files!\n"; | ||
| 352 | } | ||
| 353 | unless ( scalar @files == $cpu ) { | ||
| 354 | die "only found " . scalar(@files) . " child files when there should be $cpu!\n"; | ||
| 355 | } | ||
| 356 | my $new_count = $Data->reload_children(@files); | ||
| 357 | printf " reloaded %s features from children\n", format_with_commas($new_count); | ||
| 358 | |||
| 359 | # summarize the results | ||
| 360 | summarize_results( \@correlations, \@optimal_shifts, \@optimal_correlations, | ||
| 361 | $count, $not_enough_data, $no_variance ); | ||
| 362 | } | ||
| 363 | |||
| 364 | sub single_execution { | ||
| 365 | |||
| 366 | # run in a single thread | ||
| 367 | |||
| 368 | # Prepare new columns | ||
| 369 | my ( $r_i, $p_i, $shift_i, $shiftr_i ) = add_new_columns(); | ||
| 370 | |||
| 371 | # collect | ||
| 372 | print " Collecting correlations....\n"; | ||
| 373 | my @results = collect_correlations( $r_i, $p_i, $shift_i, $shiftr_i ); | ||
| 374 | |||
| 375 | # summarize | ||
| 376 | summarize_results(@results); | ||
| 377 | } | ||
| 378 | |||
| 379 | sub collect_correlations { | ||
| 380 | my ( $r_i, $p_i, $shift_i, $shiftr_i ) = @_; | ||
| 381 | |||
| 382 | # Set variables for summary analysis | ||
| 383 | my @correlations; | ||
| 384 | my @optimal_shifts; | ||
| 385 | my @optimal_correlations; | ||
| 386 | my $count = 0; | ||
| 387 | my $not_enough_data = 0; | ||
| 388 | my $no_variance = 0; | ||
| 389 | |||
| 390 | # check that we can collect information | ||
| 391 | unless ( $Data->feature_type eq 'named' or $Data->feature_type eq 'coordinate' ) { | ||
| 392 | die " Unable to identify the type of features in the input file." | ||
| 393 | . " File must be a recognizable format and/or have column header names\n" | ||
| 394 | . " for chromosome, start, stop; or named database features\n"; | ||
| 395 | } | ||
| 396 | |||
| 397 | # Walk through features | ||
| 398 | my $stream = $Data->row_stream; | ||
| 399 | while ( my $row = $stream->next_row ) { | ||
| 400 | |||
| 401 | # Collect positioned data | ||
| 402 | my %ref_pos2data; | ||
| 403 | my %test_pos2data; | ||
| 404 | if ($radius) { | ||
| 405 | |||
| 406 | # collecting data in a radius around a reference point | ||
| 407 | |||
| 408 | # collect data | ||
| 409 | %ref_pos2data = $row->get_relative_point_position_scores( | ||
| 410 | ddb => $ddb, | ||
| 411 | dataset => $refDataSet, | ||
| 412 | position => $position, | ||
| 413 | extend => ( 2 * $radius ), | ||
| 414 | ); | ||
| 415 | %test_pos2data = $row->get_relative_point_position_scores( | ||
| 416 | ddb => $ddb, | ||
| 417 | dataset => $testDataSet, | ||
| 418 | position => $position, | ||
| 419 | extend => ( 2 * $radius ), | ||
| 420 | ); | ||
| 421 | } | ||
| 422 | else { | ||
| 423 | # just collect data over the region | ||
| 424 | # collect data | ||
| 425 | %ref_pos2data = $row->get_region_position_scores( | ||
| 426 | ddb => $ddb, | ||
| 427 | dataset => $refDataSet, | ||
| 428 | position => $position, | ||
| 429 | ); | ||
| 430 | %test_pos2data = $row->get_region_position_scores( | ||
| 431 | ddb => $ddb, | ||
| 432 | dataset => $testDataSet, | ||
| 433 | position => $position, | ||
| 434 | ); | ||
| 435 | } | ||
| 436 | |||
| 437 | # Verify minimum data count | ||
| 438 | if ( sum0( map {abs} values %ref_pos2data ) == 0 | ||
| 439 | or sum0( map {abs} values %test_pos2data ) == 0 ) | ||
| 440 | { | ||
| 441 | # not enough data points to work with | ||
| 442 | $row->value( $r_i, '.' ); | ||
| 443 | $row->value( $p_i, '.' ) if ($find_pvalue); | ||
| 444 | if ($find_shift) { | ||
| 445 | $row->value( $shift_i, '.' ); | ||
| 446 | $row->value( $shiftr_i, '.' ); | ||
| 447 | } | ||
| 448 | $not_enough_data++; | ||
| 449 | next; | ||
| 450 | } | ||
| 451 | |||
| 452 | # Calculate Paired T-Test P value | ||
| 453 | # going to try this before normalizing | ||
| 454 | if ($find_pvalue) { | ||
| 455 | $row->value( $p_i, calculate_ttest( \%ref_pos2data, \%test_pos2data ) ); | ||
| 456 | } | ||
| 457 | |||
| 458 | # Normalize the data | ||
| 459 | # there are couple ways to do this | ||
| 460 | if ( $norm_method and $norm_method =~ /rank/i ) { | ||
| 461 | |||
| 462 | # convert to rank values | ||
| 463 | # essentially a Spearman's rank correlation | ||
| 464 | normalize_values_by_rank( \%ref_pos2data ); | ||
| 465 | normalize_values_by_rank( \%test_pos2data ); | ||
| 466 | } | ||
| 467 | elsif ( $norm_method and $norm_method =~ /sum/i ) { | ||
| 468 | |||
| 469 | # normalize the sums of both | ||
| 470 | # good for read counts | ||
| 471 | normalize_values_by_sum( \%ref_pos2data ); | ||
| 472 | normalize_values_by_sum( \%test_pos2data ); | ||
| 473 | } | ||
| 474 | |||
| 475 | # Collect the (normalized) data from the position data | ||
| 476 | # this may be all of the data or a portion of it, depending | ||
| 477 | my @ref_data; | ||
| 478 | my @test_data; | ||
| 479 | if ($radius) { | ||
| 480 | for my $i ( ( 0 - $radius ) .. $radius ) { | ||
| 481 | |||
| 482 | # get values for each position, default 0 | ||
| 483 | push @ref_data, $ref_pos2data{$i} || 0; | ||
| 484 | push @test_data, $test_pos2data{$i} || 0; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | else { | ||
| 488 | for ( my $i = 1; $i < $row->length; $i++ ) { | ||
| 489 | |||
| 490 | # get values for each position, default 0 | ||
| 491 | push @ref_data, $ref_pos2data{$i} || 0; | ||
| 492 | push @test_data, $test_pos2data{$i} || 0; | ||
| 493 | } | ||
| 494 | } | ||
| 495 | |||
| 496 | # verify the reference data | ||
| 497 | if ( min(@ref_data) == max(@ref_data) ) { | ||
| 498 | |||
| 499 | # no variance in the data! cannot calculate Pearson | ||
| 500 | $row->value( $r_i, '.' ); | ||
| 501 | $row->value( $p_i, '.' ) if ($find_pvalue); | ||
| 502 | if ($find_shift) { | ||
| 503 | $row->value( $shift_i, '.' ); | ||
| 504 | $row->value( $shiftr_i, '.' ); | ||
| 505 | } | ||
| 506 | $no_variance++; | ||
| 507 | next; | ||
| 508 | } | ||
| 509 | |||
| 510 | # Calculate the Pearson correlation between test and reference | ||
| 511 | my $r; | ||
| 512 | if ( min(@test_data) == max(@test_data) ) { | ||
| 513 | |||
| 514 | # test has no variance | ||
| 515 | # report a Pearson of 0 | ||
| 516 | $r = 0; | ||
| 517 | |||
| 518 | # this may not be a total loss, as there may be data points | ||
| 519 | # outside of the window that would generate an optimal Pearson | ||
| 520 | # so we will continue rather than bailing | ||
| 521 | } | ||
| 522 | else { | ||
| 523 | # test has variance, calculate Pearson | ||
| 524 | $r = calculate_pearson( \@ref_data, \@test_data ) || 0; | ||
| 525 | } | ||
| 526 | $row->value( $r_i, $r ); | ||
| 527 | push @correlations, $r if $r; | ||
| 528 | |||
| 529 | # Determine optimal shift if requested | ||
| 530 | if ($find_shift) { | ||
| 531 | |||
| 532 | # find optimal shift | ||
| 533 | my ( $best_shift, $best_r ) = | ||
| 534 | calculate_optimum_shift( \%test_pos2data, \@ref_data, $r, ); | ||
| 535 | |||
| 536 | # change strand | ||
| 537 | if ($set_strand) { | ||
| 538 | |||
| 539 | # user is imposing a strand | ||
| 540 | if ( $row->strand < 0 ) { | ||
| 541 | |||
| 542 | # only change shift direction if reverse strand | ||
| 543 | $best_shift *= -1; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | # record in data table | ||
| 548 | $row->value( $shift_i, $best_shift ); | ||
| 549 | $row->value( $shiftr_i, $best_r > $r ? $best_r : '.' ); | ||
| 550 | |||
| 551 | # record for summary analyses | ||
| 552 | push @optimal_shifts, $best_shift if ( $best_r > $r ); | ||
| 553 | push @optimal_correlations, $best_r if ( $best_r > $r ); | ||
| 554 | } | ||
| 555 | |||
| 556 | # Finished with this row | ||
| 557 | $count++; | ||
| 558 | } | ||
| 559 | |||
| 560 | # Return correlation results for summary | ||
| 561 | return ( \@correlations, \@optimal_shifts, \@optimal_correlations, | ||
| 562 | $count, $not_enough_data, $no_variance ); | ||
| 563 | } | ||
| 564 | |||
| 565 | sub add_new_columns { | ||
| 566 | |||
| 567 | # Required columns | ||
| 568 | # the Pearson column | ||
| 569 | my $r_i = $Data->add_column('Pearson_correlation'); | ||
| 570 | $Data->metadata( $r_i, 'reference', $refDataSet ); | ||
| 571 | $Data->metadata( $r_i, 'test', $testDataSet ); | ||
| 572 | |||
| 573 | # extra information | ||
| 574 | $Data->metadata( $r_i, 'data_database', $data_database ) if $data_database; | ||
| 575 | $Data->metadata( $r_i, 'normalization', $norm_method ) if $norm_method; | ||
| 576 | if ($radius) { | ||
| 577 | $Data->metadata( $r_i, 'radius', $radius ); | ||
| 578 | $Data->metadata( $r_i, 'reference_point', | ||
| 579 | $position == 5 ? '5_prime' : $position == 4 ? 'midpoint' : '3_prime' ); | ||
| 580 | } | ||
| 581 | |||
| 582 | # Optional columns if we're calculating an optimal shift | ||
| 583 | my ( $shift_i, $shiftr_i ); | ||
| 584 | if ($find_shift) { | ||
| 585 | |||
| 586 | # optimal test-reference shift value | ||
| 587 | $shift_i = $Data->add_column('optimal_shift'); | ||
| 588 | $Data->metadata( $shift_i, 'reference', $refDataSet ); | ||
| 589 | $Data->metadata( $shift_i, 'test', $testDataSet ); | ||
| 590 | $Data->metadata( $shift_i, 'radius', $radius ); | ||
| 591 | $Data->metadata( $shift_i, 'data_database', $data_database ) if $data_database; | ||
| 592 | $Data->metadata( $shift_i, 'reference_point', | ||
| 593 | $position == 5 ? '5_prime' : $position == 4 ? 'midpoint' : '3_prime' ); | ||
| 594 | |||
| 595 | # optimal test-reference shift value | ||
| 596 | $shiftr_i = $Data->add_column('shift_correlation'); | ||
| 597 | $Data->metadata( $shiftr_i, 'reference', $refDataSet ); | ||
| 598 | $Data->metadata( $shiftr_i, 'test', $testDataSet ); | ||
| 599 | $Data->metadata( $shiftr_i, 'radius', $radius ); | ||
| 600 | $Data->metadata( $shiftr_i, 'data_database', $data_database ) if $data_database; | ||
| 601 | $Data->metadata( $shiftr_i, 'reference_point', | ||
| 602 | $position == 5 ? '5_prime' : $position == 4 ? 'midpoint' : '3_prime' ); | ||
| 603 | $Data->metadata( $shiftr_i, 'normalization', $norm_method ) if $norm_method; | ||
| 604 | } | ||
| 605 | |||
| 606 | # Paired Student T-Test P-value | ||
| 607 | my $p_i; | ||
| 608 | if ($find_pvalue) { | ||
| 609 | $p_i = $Data->add_column('ANOVA_Pval'); | ||
| 610 | $Data->metadata( $p_i, 'reference', $refDataSet ); | ||
| 611 | $Data->metadata( $p_i, 'test', $testDataSet ); | ||
| 612 | $Data->metadata( $p_i, 'transform', '-Log10' ); | ||
| 613 | } | ||
| 614 | |||
| 615 | # record the indices | ||
| 616 | return ( $r_i, $p_i, $shift_i, $shiftr_i ); | ||
| 617 | } | ||
| 618 | |||
| 619 | sub calculate_optimum_shift { | ||
| 620 | |||
| 621 | # Calculate an optimal shift that maximizes the Pearson correlation | ||
| 622 | |||
| 623 | my ( $test_pos2data, $ref_data, $r ) = @_; | ||
| 624 | |||
| 625 | # we will calculate a Pearson correlation for each shift | ||
| 626 | # calculating window of 2*radius, shifting 2*radius across region | ||
| 627 | my %shift2pearson; | ||
| 628 | for my $shift ( 1 .. $radius ) { | ||
| 629 | |||
| 630 | # we will test shifts incrementally from 0 to radius in both directions | ||
| 631 | |||
| 632 | ## forward shift | ||
| 633 | my @test_data; | ||
| 634 | for my $i ( ( 0 - $radius + $shift ) .. ( $radius + $shift ) ) { | ||
| 635 | push @test_data, $test_pos2data->{$i} || 0; | ||
| 636 | } | ||
| 637 | |||
| 638 | # check the test array | ||
| 639 | if ( min(@test_data) == max(@test_data) ) { | ||
| 640 | |||
| 641 | # no variance! | ||
| 642 | next; | ||
| 643 | } | ||
| 644 | |||
| 645 | # calculate Pearson correlation | ||
| 646 | my $pearson = calculate_pearson( $ref_data, \@test_data ); | ||
| 647 | if ( defined $pearson and $pearson > 0.5 ) { | ||
| 648 | $shift2pearson{$shift} = $pearson; | ||
| 649 | } | ||
| 650 | |||
| 651 | ## Reverse shift | ||
| 652 | @test_data = (); | ||
| 653 | for my $i ( ( 0 - $radius - $shift ) .. ( $radius - $shift ) ) { | ||
| 654 | push @test_data, $test_pos2data->{$i} || 0; | ||
| 655 | } | ||
| 656 | |||
| 657 | # check the test array | ||
| 658 | if ( min(@test_data) == max(@test_data) ) { | ||
| 659 | |||
| 660 | # no variance! | ||
| 661 | next; | ||
| 662 | } | ||
| 663 | |||
| 664 | # calculate Pearson correlation for reverse shift | ||
| 665 | my $pearson1 = calculate_pearson( $ref_data, \@test_data ); | ||
| 666 | if ( defined $pearson1 and $pearson1 > 0.5 ) { | ||
| 667 | $shift2pearson{ $shift * -1 } = $pearson1; | ||
| 668 | } | ||
| 669 | } | ||
| 670 | |||
| 671 | # identify the best shift | ||
| 672 | my $best_r = $r; | ||
| 673 | my $best_shift = 1000000; # an impossibly high number | ||
| 674 | foreach my $shift ( keys %shift2pearson ) { | ||
| 675 | if ( $shift2pearson{$shift} == $best_r ) { | ||
| 676 | |||
| 677 | # same as before | ||
| 678 | if ( $best_shift != 1000000 and abs($shift) < abs($best_shift) ) { | ||
| 679 | |||
| 680 | # if it's a lower shift, then keep it, | ||
| 681 | # but not if it's original | ||
| 682 | $best_r = $shift2pearson{$shift}; | ||
| 683 | $best_shift = $shift; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | elsif ( $shift2pearson{$shift} > $best_r ) { | ||
| 687 | |||
| 688 | # a good looking r value | ||
| 689 | # but we only want those with the smallest shift | ||
| 690 | if ( abs($shift) < abs($best_shift) ) { | ||
| 691 | $best_r = $shift2pearson{$shift}; | ||
| 692 | $best_shift = $shift; | ||
| 693 | } | ||
| 694 | } | ||
| 695 | } | ||
| 696 | if ( $best_shift == 1000000 ) { | ||
| 697 | |||
| 698 | # no good shift found!? then that means no shift | ||
| 699 | $best_shift = 0; | ||
| 700 | } | ||
| 701 | |||
| 702 | # done | ||
| 703 | return ( $best_shift, $best_r ); | ||
| 704 | } | ||
| 705 | |||
| 706 | sub normalize_values_by_rank { | ||
| 707 | |||
| 708 | # convert values to rank value | ||
| 709 | |||
| 710 | my $data = shift; | ||
| 711 | my %ranks; # a hash for the quantile lookup | ||
| 712 | |||
| 713 | # put values into quantile lookup hash | ||
| 714 | my $i = 1; | ||
| 715 | foreach ( sort { $a <=> $b } values %{$data} ) { | ||
| 716 | |||
| 717 | # for the quantile hash, the key will be the original data value | ||
| 718 | # and the hash value will be the rank in the ordered data array | ||
| 719 | # the rank is stored in anonymous array since there may be more | ||
| 720 | # than one identical data value and we want the ranks for all of | ||
| 721 | # them | ||
| 722 | push @{ $ranks{$_} }, $i; | ||
| 723 | $i++; | ||
| 724 | } | ||
| 725 | |||
| 726 | # replace the current value with the scaled quantile value | ||
| 727 | foreach my $k ( keys %{$data} ) { | ||
| 728 | |||
| 729 | # usually there is only one rank value for each data value | ||
| 730 | # sometimes, there are two or more identical data values within the | ||
| 731 | # the dataset, in which case we will simply take the mean rank | ||
| 732 | |||
| 733 | # we need to look up the rank(s) for the current data value | ||
| 734 | # then replace it with the mean rank | ||
| 735 | # many times there will only be a single value | ||
| 736 | $data->{$k} = mean( @{ $ranks{ $data->{$k} } } ); | ||
| 737 | } | ||
| 738 | } | ||
| 739 | |||
| 740 | sub normalize_values_by_sum { | ||
| 741 | |||
| 742 | # Scale the data sums to 100 | ||
| 743 | |||
| 744 | my $data = shift; | ||
| 745 | my $ref_scale_factor = 100 / sum0( map {abs} values %{$data} ); | ||
| 746 | map { $data->{$_} = $ref_scale_factor * $data->{$_} } keys %{$data}; | ||
| 747 | } | ||
| 748 | |||
| 749 | sub normalize_values_by_median { | ||
| 750 | |||
| 751 | # Scale the data median to 100 | ||
| 752 | |||
| 753 | my $data = shift; | ||
| 754 | my $ref_scale_factor = 100 / median( map {abs} values %{$data} ); | ||
| 755 | map { $data->{$_} = $ref_scale_factor * $data->{$_} } keys %{$data}; | ||
| 756 | } | ||
| 757 | |||
| 758 | sub calculate_pearson { | ||
| 759 | |||
| 760 | # positioned data hashes, start, and stop coordinates for evaluating | ||
| 761 | my ( $ref, $test ) = @_; | ||
| 762 | |||
| 763 | # calculate correlation | ||
| 764 | # my $stat = Statistics::LineFit->new(); | ||
| 765 | # $stat->setData($ref, $test) or warn " bad data!\n"; | ||
| 766 | # return $stat->rSquared(); | ||
| 767 | my $stat = Statistics::Descriptive::Full->new(); | ||
| 768 | $stat->add_data($ref); | ||
| 769 | my ( $q, $m, $r, $rms ) = $stat->least_squares_fit( @{$test} ); | ||
| 770 | return $r; | ||
| 771 | } | ||
| 772 | |||
| 773 | sub calculate_ttest { | ||
| 774 | |||
| 775 | # generate the data arrays | ||
| 776 | my ( $ref, $test ) = @_; | ||
| 777 | my @ref_data; | ||
| 778 | my @test_data; | ||
| 779 | |||
| 780 | # load the data arrays | ||
| 781 | my $min_value = min( min( keys %{$ref} ), min( keys %{$test} ) ); | ||
| 782 | my $max_value = max( max( keys %{$ref} ), max( keys %{$test} ) ); | ||
| 783 | for my $i ( $min_value .. $max_value ) { | ||
| 784 | push @ref_data, $ref->{$i} || 0; | ||
| 785 | push @test_data, $test->{$i} || 0; | ||
| 786 | } | ||
| 787 | |||
| 788 | # calculate ANOVA | ||
| 789 | my $p_value; | ||
| 790 | my $aov = Statistics::ANOVA->new(); | ||
| 791 | eval { | ||
| 792 | $aov->load( | ||
| 793 | { | ||
| 794 | 'test' => \@test_data, | ||
| 795 | 'ref' => \@ref_data, | ||
| 796 | } | ||
| 797 | ); | ||
| 798 | $aov->anova( | ||
| 799 | 'independent' => $INDEPENDENCE, | ||
| 800 | 'parametric' => $PARAMETRIC, | ||
| 801 | 'ordinal' => $ORDINAL, | ||
| 802 | ); | ||
| 803 | $p_value = $aov->{_stat}{p_value}; | ||
| 804 | }; | ||
| 805 | |||
| 806 | $p_value = -1 * ( log($p_value) / LOG10 ) if $p_value; | ||
| 807 | return $p_value || '.'; | ||
| 808 | } | ||
| 809 | |||
| 810 | sub summarize_results { | ||
| 811 | |||
| 812 | # the results from the correlation analysis | ||
| 813 | my ( $correlations, $optimal_shifts, $optimal_correlations, | ||
| 814 | $count, $not_enough_data, $no_variance ) | ||
| 815 | = @_; | ||
| 816 | |||
| 817 | # Summary analyses | ||
| 818 | printf " Correlated %s features\n", format_with_commas($count); | ||
| 819 | printf " Mean Pearson correlation was %.3f +/- %.3f\n", | ||
| 820 | mean( @{$correlations} ), stddevp( @{$correlations} ); | ||
| 821 | |||
| 822 | if ($find_shift) { | ||
| 823 | printf " Mean absolute optimal shift was %.0f +/- %.0f bp\n", | ||
| 824 | mean( map {abs} @{$optimal_shifts} ), | ||
| 825 | stddevp( map {abs} @{$optimal_shifts} ); | ||
| 826 | printf " Mean optimal Pearson correlation was %.3f +/- %.3f\n", | ||
| 827 | mean( @{$optimal_correlations} ), stddevp( @{$optimal_correlations} ); | ||
| 828 | } | ||
| 829 | if ($not_enough_data) { | ||
| 830 | printf " %s features did not have enough data points\n", | ||
| 831 | format_with_commas($not_enough_data); | ||
| 832 | } | ||
| 833 | if ($no_variance) { | ||
| 834 | printf " %s features had no variance in the reference data points\n", | ||
| 835 | format_with_commas($no_variance); | ||
| 836 | } | ||
| 837 | } | ||
| 838 | |||
| 839 | sub mean { | ||
| 840 | return sum0(@_) / ( scalar @_ || 1 ); | ||
| 841 | } | ||
| 842 | |||
| 843 | __END__ |