| 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 |
|
| calculate_score |
| 16 |
|
| use_minimum_mapq |
| 17 |
|
| $BAM_ADAPTER |
| 18 |
|
| $BIG_ADAPTER |
| 19 |
|
| ); |
| 20 |
|
| use Bio::ToolBox::utility qw(format_with_commas simplify_dataset_name); |
| 21 |
|
| |
| 22 |
|
| my $parallel; |
| 23 |
|
| eval { |
| 24 |
|
| # check for parallel support |
| 25 |
|
| require Parallel::ForkManager; |
| 26 |
|
| $parallel = 1; |
| 27 |
|
| }; |
| 28 |
|
| |
| 29 |
|
| # This constant determines the maximum size of the dataset hash to be |
| 30 |
|
| # returned from the get_region_dataset_hash(). To increase performance, |
| 31 |
|
| # the program normally queries the database once for each feature or |
| 32 |
|
| # region, and a hash returned with potentially a score for each basepair. |
| 33 |
|
| # This may become unwieldy for very large regions, which may be better |
| 34 |
|
| # served by separate database queries for each window. |
| 35 |
|
| use constant DATASET_HASH_LIMIT => 4999; |
| 36 |
|
| |
| 37 |
|
| our $VERSION = '2.04'; |
| 38 |
|
| |
| 39 |
|
| print |
| 40 |
|
| "\n A script to collect windowed data flanking a relative position of a feature\n\n"; |
| 41 |
|
| |
| 42 |
|
| ### Quick help |
| 43 |
|
| unless (@ARGV) { # when no command line options are present |
| 44 |
|
| # when no command line options are present |
| 45 |
|
| # print SYNOPSIS |
| 46 |
|
| pod2usage( |
| 47 |
|
| { |
| 48 |
|
| '-verbose' => 0, |
| 49 |
|
| '-exitval' => 1, |
| 50 |
|
| } |
| 51 |
|
| ); |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
|
| ### Get command line options and initialize values |
| 55 |
|
| |
| 56 |
|
| ## Initialize values |
| 57 |
|
| my ( |
| 58 |
|
| $infile, $parse, $outfile, $main_database, $data_database, |
| 59 |
|
| $feature, $method, $win, $number, $up_number, |
| 60 |
|
| $down_number, $position, $strand_sense, $set_strand, $avoid, |
| 61 |
|
| $avoidtype, $long_data, $smooth, $sum, $format, |
| 62 |
|
| $groupcol, $gz, $cpu, $help, $print_version, |
| 63 |
|
| $mapq |
| 64 |
|
| ); # command line variables |
| 65 |
|
| my @datasets; |
| 66 |
|
| |
| 67 |
|
| ## Command line options |
| 68 |
|
| GetOptions( |
| 69 |
|
| 'o|out=s' => \$outfile, # output file name |
| 70 |
|
| 'i|in=s' => \$infile, # input file name |
| 71 |
|
| 'parse!' => \$parse, # parse input file |
| 72 |
|
| 'd|db=s' => \$main_database, # main or annotation database name |
| 73 |
|
| 'D|ddb=s' => \$data_database, # data database |
| 74 |
|
| 'a|data=s' => \@datasets, # dataset name |
| 75 |
|
| 'f|feature=s' => \$feature, # type of feature |
| 76 |
|
| 'm|method=s' => \$method, # method to combine data |
| 77 |
|
| 'w|window=i' => \$win, # window size |
| 78 |
|
| 'n|number=i' => \$number, # number of windows |
| 79 |
|
| 'up=i' => \$up_number, # number of windows upstream |
| 80 |
|
| 'down=i' => \$down_number, # number of windows downstream |
| 81 |
|
| 'p|position=s' => \$position, # indicate relative location of the feature |
| 82 |
|
| 't|strand=s' => \$strand_sense, # collected stranded data |
| 83 |
|
| 'force_strand|set_strand' => \$set_strand, # enforce an artificial strand |
| 84 |
|
| # force_strand is preferred option, but respect the old option |
| 85 |
|
| 'avoid!' => \$avoid, # avoid conflicting features |
| 86 |
|
| 'avtype=s' => \$avoidtype, # type of feature to avoid |
| 87 |
|
| 'long!' => \$long_data, # collecting long data features |
| 88 |
|
| 'smooth!' => \$smooth, # smooth by interpolation |
| 89 |
|
| 'U|sum!' => \$sum, # generate average profile |
| 90 |
|
| 'r|format=i' => \$format, # decimal formatting |
| 91 |
|
| 'mapq=i' => \$mapq, # minimum map quality |
| 92 |
|
| 'g|groups' => \$groupcol, # write group column file |
| 93 |
|
| 'z|gz!' => \$gz, # compress the output file |
| 94 |
|
| 'c|cpu=i' => \$cpu, # number of execution threads |
| 95 |
|
| 'h|help' => \$help, # print help |
| 96 |
|
| 'v|version' => \$print_version, # print the version |
| 97 |
|
| 'bam=s' => \$BAM_ADAPTER, # explicitly set the bam adapter |
| 98 |
|
| 'big=s' => \$BIG_ADAPTER, # explicitly set the big adapter |
| 99 |
|
| ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; |
| 100 |
|
| |
| 101 |
|
| # Print help |
| 102 |
|
| if ($help) { |
| 103 |
|
| |
| 104 |
|
| # print entire POD |
| 105 |
|
| pod2usage( |
| 106 |
|
| { |
| 107 |
|
| '-verbose' => 2, |
| 108 |
|
| '-exitval' => 1, |
| 109 |
|
| } |
| 110 |
|
| ); |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| # Print version |
| 114 |
|
| if ($print_version) { |
| 115 |
|
| print " Biotoolbox script get_relative_data.pl, version $VERSION\n"; |
| 116 |
|
| eval { |
| 117 |
|
| require Bio::ToolBox; |
| 118 |
|
| my $v = Bio::ToolBox->VERSION; |
| 119 |
|
| print " Biotoolbox package version $v\n"; |
| 120 |
|
| }; |
| 121 |
|
| exit; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| ## Check for required values |
| 125 |
|
| my $formatter; |
| 126 |
|
| check_defaults(); |
| 127 |
|
| my $start_time = time; |
| 128 |
|
| |
| 129 |
|
| ## Generate or load the input dataset |
| 130 |
|
| my $Data; |
| 131 |
|
| if ($infile) { |
| 132 |
|
| $Data = Bio::ToolBox::Data->new( |
| 133 |
|
| file => $infile, |
| 134 |
|
| parse => $parse, |
| 135 |
|
| feature => $feature, |
| 136 |
|
| ) or die " unable to load input file '$infile'\n"; |
| 137 |
|
| if ( $Data->last_row ) { |
| 138 |
|
| printf " Loaded %s features from $infile.\n", |
| 139 |
|
| format_with_commas( $Data->last_row ); |
| 140 |
|
| } |
| 141 |
|
| else { |
| 142 |
|
| print STDERR " FATAL: No features loaded from file '$infile'!\n"; |
| 143 |
|
| exit 1; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| # update main database as necessary |
| 147 |
|
| if ($main_database) { |
| 148 |
|
| if ( $main_database ne $Data->database ) { |
| 149 |
|
| |
| 150 |
|
| # update with new database |
| 151 |
|
| printf " updating main database name from '%s' to '%s'\n", |
| 152 |
|
| $Data->database, $main_database; |
| 153 |
|
| $Data->database($main_database); |
| 154 |
|
| } |
| 155 |
|
| } |
| 156 |
|
| elsif ( $Data->database ) { |
| 157 |
|
| $main_database = $Data->database if $Data->database !~ /^Parsed/; |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| # update feature type as necessary |
| 161 |
|
| if ( not defined $Data->feature |
| 162 |
|
| and not defined $Data->type_column |
| 163 |
|
| and defined $feature ) |
| 164 |
|
| { |
| 165 |
|
| $Data->feature($feature); |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
|
| # set headers to true if file was explicitly not parsed |
| 169 |
|
| # since warnings will occur when we add columns and write to a file |
| 170 |
|
| if ( $parse == 0 ) { |
| 171 |
|
| $Data->headers(1); |
| 172 |
|
| } |
| 173 |
|
| } |
| 174 |
|
| else { |
| 175 |
|
| # generate a new file |
| 176 |
|
| print " Generating a new feature list from database '$main_database'...\n"; |
| 177 |
|
| $Data = Bio::ToolBox::Data->new( |
| 178 |
|
| db => $main_database, |
| 179 |
|
| feature => $feature, |
| 180 |
|
| ) or die " unable to generate new feature list\n"; |
| 181 |
|
| } |
| 182 |
|
| $Data->program("$PROGRAM_NAME, v $VERSION"); |
| 183 |
|
| |
| 184 |
|
| # check peak summit |
| 185 |
|
| if ( $position == 9 and $Data->format ne 'narrowPeak' ) { |
| 186 |
|
| print |
| 187 |
|
| " Peak summit indicated as reference point, but input file is not narrowPeak!\n"; |
| 188 |
|
| print " using interval midpoint instead\n"; |
| 189 |
|
| $position = 4; |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
|
| # the number of columns already in the data array |
| 193 |
|
| my $beginningcolumn = $Data->last_column; |
| 194 |
|
| my $startcolumn; # this is now calculated separately for each dataset |
| 195 |
|
| |
| 196 |
|
| # Check output file name |
| 197 |
|
| unless ($outfile) { |
| 198 |
|
| if ( $Data->filename ) { |
| 199 |
|
| |
| 200 |
|
| # reuse the input filename and overwrite |
| 201 |
|
| $outfile = $Data->filename; |
| 202 |
|
| } |
| 203 |
|
| elsif ( $Data->basename ) { |
| 204 |
|
| |
| 205 |
|
| # no filename but a basename is indicative of a parsed file |
| 206 |
|
| # so stick a txt extension on it, and write in current directory |
| 207 |
|
| $outfile = sprintf "%s.txt", $Data->basename; |
| 208 |
|
| } |
| 209 |
|
| else { |
| 210 |
|
| print STDERR " FATAL: No output file provided!\n"; |
| 211 |
|
| exit 1; |
| 212 |
|
| } |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
|
| # make sure data table supports avoid option |
| 216 |
|
| if ($avoid) { |
| 217 |
|
| unless ($main_database) { |
| 218 |
|
| print |
| 219 |
|
| " WARNING: avoid option not supported without an annotation database! Disabling\n"; |
| 220 |
|
| undef $avoid; # 0 complicates things, leave undefined |
| 221 |
|
| } |
| 222 |
|
| if ($avoidtype) { |
| 223 |
|
| if ( $avoidtype =~ /,/ ) { |
| 224 |
|
| |
| 225 |
|
| # a comma delimited list |
| 226 |
|
| $avoid = [ split( /,/, $avoidtype ) ]; |
| 227 |
|
| } |
| 228 |
|
| else { |
| 229 |
|
| # presume a single feature type, take it as is |
| 230 |
|
| $avoid = [$avoidtype]; |
| 231 |
|
| } |
| 232 |
|
| } |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
|
| ## Prepare to collect data |
| 236 |
|
| # Open data database |
| 237 |
|
| my $ddb; |
| 238 |
|
| if ( defined $data_database ) { |
| 239 |
|
| |
| 240 |
|
| # specifically defined a data database |
| 241 |
|
| $ddb = open_db_connection($data_database) |
| 242 |
|
| or die "unable to establish data database connection to $data_database!\n"; |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| # Check the dataset |
| 246 |
|
| @datasets = verify_or_request_feature_types( |
| 247 |
|
| 'db' => $ddb || $Data->database, |
| 248 |
|
| 'feature' => \@datasets, |
| 249 |
|
| 'prompt' => " Enter the dataset(s) or feature type(s) from which \n" |
| 250 |
|
| . " to collect data. Comma delimited or range is acceptable\n", |
| 251 |
|
| ); |
| 252 |
|
| unless (@datasets) { |
| 253 |
|
| print STDERR |
| 254 |
|
| " FATAL: No verifiable dataset(s) provided. Check your file path, database, or dataset.\n"; |
| 255 |
|
| exit 1; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
|
| ## Collect the relative data |
| 259 |
|
| |
| 260 |
|
| # Determine starting and ending points |
| 261 |
|
| my $starting_point = 0 - ( $win * $up_number ); |
| 262 |
|
| |
| 263 |
|
| # default values will give startingpoint of -1000 |
| 264 |
|
| my $ending_point = $win * $down_number; |
| 265 |
|
| |
| 266 |
|
| # likewise default values will give endingpoint of 1000 |
| 267 |
|
| |
| 268 |
|
| # Print collection statement |
| 269 |
|
| printf |
| 270 |
|
| " Collecting %s $method data between %d..%d from the %s in %d bp windows from %s\n", |
| 271 |
|
| $long_data ? 'long' : 'hashed', |
| 272 |
|
| $starting_point, $ending_point, |
| 273 |
|
| $position == 5 ? "5' end" |
| 274 |
|
| : $position == 4 ? "midpoint" |
| 275 |
|
| : $position == 10 ? "peak summit" |
| 276 |
|
| : "3' end", |
| 277 |
|
| $win, join( ', ', @datasets ),; |
| 278 |
|
| |
| 279 |
|
| # check whether it is worth doing parallel execution |
| 280 |
|
| if ( $cpu > 1 and ( $Data->number_rows / $cpu ) < 100 ) { |
| 281 |
|
| while ( $cpu > 1 and ( $Data->number_rows / $cpu ) < 100 ) { |
| 282 |
|
| |
| 283 |
|
| # I figure we need at least 100 lines in each fork split to make |
| 284 |
|
| # it worthwhile to do the split, otherwise, reduce the number of |
| 285 |
|
| # splits to something more worthwhile |
| 286 |
|
| $cpu--; |
| 287 |
|
| } |
| 288 |
|
| print " Reducing number of process forks to $cpu due to number of input features\n"; |
| 289 |
|
| } |
| 290 |
|
| |
| 291 |
|
| if ( $cpu > 1 ) { |
| 292 |
|
| |
| 293 |
|
| # parallel execution |
| 294 |
|
| print " Forking into $cpu children for parallel data collection...\n"; |
| 295 |
|
| parallel_execution(); |
| 296 |
|
| } |
| 297 |
|
| else { |
| 298 |
|
| # single process execution |
| 299 |
|
| single_execution(); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
|
| ## Generate summed data |
| 303 |
|
| # an average across all features at each position suitable for plotting |
| 304 |
|
| if ($sum) { |
| 305 |
|
| print " Generating trimmed-mean summary file....\n"; |
| 306 |
|
| my $sumfile = $Data->summary_file( |
| 307 |
|
| 'filename' => $outfile, |
| 308 |
|
| 'dataset' => \@datasets, |
| 309 |
|
| 'method' => 'trimmean' |
| 310 |
|
| ); |
| 311 |
|
| if ($sumfile) { |
| 312 |
|
| print " Wrote summary file '$sumfile'\n"; |
| 313 |
|
| } |
| 314 |
|
| else { |
| 315 |
|
| print " Unable to write summary file!\n"; |
| 316 |
|
| } |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
|
| ## Output the data |
| 320 |
|
| my $written_file = $Data->save( |
| 321 |
|
| 'filename' => $outfile, |
| 322 |
|
| 'gz' => $gz, |
| 323 |
|
| ); |
| 324 |
|
| if ($written_file) { |
| 325 |
|
| |
| 326 |
|
| # success! |
| 327 |
|
| print " wrote file $written_file\n"; |
| 328 |
|
| } |
| 329 |
|
| else { |
| 330 |
|
| # failure! the subroutine will have printed error messages |
| 331 |
|
| print " unable to write output file!\n"; |
| 332 |
|
| } |
| 333 |
|
| |
| 334 |
|
| # write the column group file |
| 335 |
|
| if ($groupcol) { |
| 336 |
|
| my $groupfile = $written_file; |
| 337 |
|
| $groupfile =~ s/\.txt (?:\.gz)? $/.col_groups.txt/x; |
| 338 |
|
| my $fh = Bio::ToolBox::Data->open_to_write_fh($groupfile); |
| 339 |
|
| $fh->print("Name\tDataset\n"); |
| 340 |
|
| for ( my $i = $beginningcolumn + 1; $i <= $Data->last_column; $i++ ) { |
| 341 |
|
| my $name = $Data->name($i); |
| 342 |
|
| my $dataset = $name; |
| 343 |
|
| $dataset =~ s/:[\-\d]+$//; |
| 344 |
|
| $fh->print("$name\t$dataset\n"); |
| 345 |
|
| } |
| 346 |
|
| $fh->close; |
| 347 |
|
| print " wrote group column file $groupfile\n"; |
| 348 |
|
| } |
| 349 |
|
| |
| 350 |
|
| printf " Completed in %.1f minutes\n", ( time - $start_time ) / 60; |
| 351 |
|
| |
| 352 |
|
| #### Subroutines ####### |
| 353 |
|
| |
| 354 |
|
| ## check required variables and assign default values |
| 355 |
|
| sub check_defaults { |
| 356 |
|
| unless ( $main_database or $infile ) { |
| 357 |
|
| print STDERR |
| 358 |
|
| " FATAL: You must define a database or input file!\n Use --help for more information\n"; |
| 359 |
|
| exit 1; |
| 360 |
|
| } |
| 361 |
|
| $parse = 1 if ( $infile and not defined $parse ); |
| 362 |
|
| |
| 363 |
|
| unless ( $outfile or $infile ) { |
| 364 |
|
| print STDERR |
| 365 |
|
| " FATAL: You must define an output filename !\n Use --help for more information\n"; |
| 366 |
|
| exit 1; |
| 367 |
|
| } |
| 368 |
|
| |
| 369 |
|
| # check datasets |
| 370 |
|
| if ( not @datasets and @ARGV ) { |
| 371 |
|
| @datasets = @ARGV; |
| 372 |
|
| } |
| 373 |
|
| if ( @datasets and $datasets[0] =~ /,/ ) { |
| 374 |
|
| |
| 375 |
|
| # seems to be a comma delimited list, possibly more than one????? |
| 376 |
|
| my @list; |
| 377 |
|
| foreach my $d (@datasets) { |
| 378 |
|
| push @list, ( split /,/, $d ); |
| 379 |
|
| } |
| 380 |
|
| @datasets = @list; |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
|
| # window size |
| 384 |
|
| unless ($win) { |
| 385 |
|
| print " Using default window size of 50 bp\n"; |
| 386 |
|
| $win = 50; |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
|
| # number of windows |
| 390 |
|
| if ( $up_number or $down_number ) { |
| 391 |
|
| |
| 392 |
|
| # either one could be set, assume the other is zero |
| 393 |
|
| $up_number ||= 0; |
| 394 |
|
| $down_number ||= 0; |
| 395 |
|
| } |
| 396 |
|
| elsif ($number) { |
| 397 |
|
| $up_number = $number; |
| 398 |
|
| $down_number = $number; |
| 399 |
|
| } |
| 400 |
|
| else { |
| 401 |
|
| print " Using default window number of 20 per side\n"; |
| 402 |
|
| $up_number = 20; |
| 403 |
|
| $down_number = 20; |
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
|
| if ( defined $method ) { |
| 407 |
|
| |
| 408 |
|
| # check the requested method |
| 409 |
|
| unless ( $method eq 'mean' |
| 410 |
|
| or $method eq 'median' |
| 411 |
|
| or $method eq 'sum' |
| 412 |
|
| or $method eq 'min' |
| 413 |
|
| or $method eq 'max' |
| 414 |
|
| or $method eq 'stddev' |
| 415 |
|
| or $method =~ /^\w?count$/ ) |
| 416 |
|
| { |
| 417 |
|
| print STDERR |
| 418 |
|
| " FATAL: Unknown method '$method'!\n Use --help for more information\n"; |
| 419 |
|
| exit 1; |
| 420 |
|
| } |
| 421 |
|
| } |
| 422 |
|
| else { |
| 423 |
|
| $method = 'mean'; |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
|
| if ( defined $position ) { |
| 427 |
|
| |
| 428 |
|
| # check the position value |
| 429 |
|
| if ( $position !~ /^ (?: 5 | 3 | 53 | m | p | 4 ) $/x ) { |
| 430 |
|
| print STDERR " FATAL: Unknown relative position '$position'!\n"; |
| 431 |
|
| exit 1; |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
|
| # change to match internal usage |
| 435 |
|
| if ( $position eq 'm' ) { |
| 436 |
|
| $position = 4; # midpoint |
| 437 |
|
| } |
| 438 |
|
| elsif ( $position eq 'p' ) { |
| 439 |
|
| $position = 10; # narrowPeak summit |
| 440 |
|
| } |
| 441 |
|
| } |
| 442 |
|
| else { |
| 443 |
|
| # default position to use the 5' end |
| 444 |
|
| $position = 5; |
| 445 |
|
| } |
| 446 |
|
| |
| 447 |
|
| if ( defined $strand_sense ) { |
| 448 |
|
| unless ( $strand_sense eq 'sense' |
| 449 |
|
| or $strand_sense eq 'antisense' |
| 450 |
|
| or $strand_sense eq 'all' ) |
| 451 |
|
| { |
| 452 |
|
| print STDERR " FATAL: Unknown strand value '$strand_sense'!\n"; |
| 453 |
|
| exit 1; |
| 454 |
|
| } |
| 455 |
|
| } |
| 456 |
|
| else { |
| 457 |
|
| # default |
| 458 |
|
| $strand_sense = 'all'; |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
|
| unless ( defined $sum ) { |
| 462 |
|
| |
| 463 |
|
| # assume to write a summary file, nearly always want this, at least I do |
| 464 |
|
| $sum = 1; |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
|
| # generate formatter |
| 468 |
|
| if ( defined $format ) { |
| 469 |
|
| $formatter = '%.' . $format . 'f'; |
| 470 |
|
| } |
| 471 |
|
| |
| 472 |
|
| if ($parallel) { |
| 473 |
|
| $cpu ||= 4; |
| 474 |
|
| } |
| 475 |
|
| else { |
| 476 |
|
| # disable cores |
| 477 |
|
| print " disabling parallel CPU execution, no support present\n" if $cpu; |
| 478 |
|
| $cpu = 1; |
| 479 |
|
| } |
| 480 |
|
| |
| 481 |
|
| # set minimum alignment mapping quality |
| 482 |
|
| if ( $method =~ /count/ and $mapq ) { |
| 483 |
|
| use_minimum_mapq($mapq); |
| 484 |
|
| } |
| 485 |
|
| } |
| 486 |
|
| |
| 487 |
|
| ## Run in parallel |
| 488 |
|
| sub parallel_execution { |
| 489 |
|
| my $pm = Parallel::ForkManager->new($cpu); |
| 490 |
|
| |
| 491 |
|
| # generate base name for child processes |
| 492 |
|
| my $child_base_name = $outfile . ".$PID"; |
| 493 |
|
| |
| 494 |
|
| # Split the input data into parts and execute in parallel in separate forks |
| 495 |
|
| for my $i ( 1 .. $cpu ) { |
| 496 |
|
| $pm->start and next; |
| 497 |
|
| |
| 498 |
|
| #### In child #### |
| 499 |
|
| |
| 500 |
|
| # splice the data structure |
| 501 |
|
| $Data->split_data( $i, $cpu ); |
| 502 |
|
| |
| 503 |
|
| # re-open database objects to make them clone safe |
| 504 |
|
| # pass second true to avoid cached database objects |
| 505 |
|
| my $db = $Data->open_database(1); |
| 506 |
|
| if ($data_database) { |
| 507 |
|
| $ddb = open_db_connection( $data_database, 1 ); |
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
|
| # work through each dataset |
| 511 |
|
| foreach my $dataset (@datasets) { |
| 512 |
|
| |
| 513 |
|
| # new start column for this dataset |
| 514 |
|
| $startcolumn = $Data->number_columns + 1; |
| 515 |
|
| |
| 516 |
|
| # Add the columns for each window |
| 517 |
|
| # and calculate the relative starting and ending points |
| 518 |
|
| prepare_window_datasets($dataset); |
| 519 |
|
| |
| 520 |
|
| # determine long data collection for very large regions |
| 521 |
|
| if ( $ending_point - $starting_point > DATASET_HASH_LIMIT ) { |
| 522 |
|
| $long_data = 1; |
| 523 |
|
| } |
| 524 |
|
| |
| 525 |
|
| # Select the appropriate method for data collection |
| 526 |
|
| if ($long_data) { |
| 527 |
|
| map_relative_long_data($dataset); |
| 528 |
|
| } |
| 529 |
|
| else { |
| 530 |
|
| map_relative_data($dataset); |
| 531 |
|
| } |
| 532 |
|
| |
| 533 |
|
| # Interpolate values |
| 534 |
|
| if ($smooth) { |
| 535 |
|
| go_interpolate_values(); |
| 536 |
|
| } |
| 537 |
|
| } |
| 538 |
|
| |
| 539 |
|
| # write out result |
| 540 |
|
| my $success = $Data->save( |
| 541 |
|
| 'filename' => sprintf( "$child_base_name.%03s", $i ), |
| 542 |
|
| 'gz' => 0, # faster to write without compression |
| 543 |
|
| ); |
| 544 |
|
| if ($success) { |
| 545 |
|
| printf " wrote child file $success\n"; |
| 546 |
|
| } |
| 547 |
|
| else { |
| 548 |
|
| # failure! the subroutine will have printed error messages |
| 549 |
|
| die " unable to write file!\n"; |
| 550 |
|
| |
| 551 |
|
| # no need to continue |
| 552 |
|
| } |
| 553 |
|
| |
| 554 |
|
| # Finished |
| 555 |
|
| $pm->finish; |
| 556 |
|
| } |
| 557 |
|
| $pm->wait_all_children; |
| 558 |
|
| |
| 559 |
|
| # reassemble children files into output file |
| 560 |
|
| my @files = glob "$child_base_name.*"; |
| 561 |
|
| unless (@files) { |
| 562 |
|
| die "unable to find children files!\n"; |
| 563 |
|
| } |
| 564 |
|
| unless ( scalar @files == $cpu ) { |
| 565 |
|
| die "only found " . scalar(@files) . " child files when there should be $cpu!\n"; |
| 566 |
|
| } |
| 567 |
|
| my $count = $Data->reload_children(@files); |
| 568 |
|
| printf " reloaded %s features from children\n", format_with_commas($count); |
| 569 |
|
| } |
| 570 |
|
| |
| 571 |
|
| ## Run in single thread |
| 572 |
|
| sub single_execution { |
| 573 |
|
| |
| 574 |
|
| # work through each dataset |
| 575 |
|
| foreach my $dataset (@datasets) { |
| 576 |
|
| |
| 577 |
|
| # new start column for this dataset |
| 578 |
|
| $startcolumn = $Data->number_columns + 1; |
| 579 |
|
| |
| 580 |
|
| # Add the columns for each window |
| 581 |
|
| # and calculate the relative starting and ending points |
| 582 |
|
| prepare_window_datasets($dataset); |
| 583 |
|
| |
| 584 |
|
| # determine long data collection for very large regions |
| 585 |
|
| if ( $ending_point - $starting_point > DATASET_HASH_LIMIT ) { |
| 586 |
|
| $long_data = 1; |
| 587 |
|
| } |
| 588 |
|
| |
| 589 |
|
| # Select the appropriate method for data collection |
| 590 |
|
| if ($long_data) { |
| 591 |
|
| map_relative_long_data($dataset); |
| 592 |
|
| } |
| 593 |
|
| else { |
| 594 |
|
| map_relative_data($dataset); |
| 595 |
|
| } |
| 596 |
|
| |
| 597 |
|
| # Interpolate values |
| 598 |
|
| if ($smooth) { |
| 599 |
|
| print " Interpolating missing values....\n"; |
| 600 |
|
| go_interpolate_values(); |
| 601 |
|
| } |
| 602 |
|
| } |
| 603 |
|
| } |
| 604 |
|
| |
| 605 |
|
| ## Prepare columns for each window |
| 606 |
|
| sub prepare_window_datasets { |
| 607 |
|
| my $dataset = shift; |
| 608 |
|
| |
| 609 |
|
| # generate a simplified new name |
| 610 |
|
| my $new_name = simplify_dataset_name($dataset); |
| 611 |
|
| |
| 612 |
|
| # Prepare and annotate the header names and metadata |
| 613 |
|
| for ( my $start = $starting_point; $start < $ending_point; $start += $win ) { |
| 614 |
|
| |
| 615 |
|
| # we will be progressing from the starting to ending point |
| 616 |
|
| # in increments of window size |
| 617 |
|
| |
| 618 |
|
| # set the stop position |
| 619 |
|
| my $stop = $start + $win - 1; |
| 620 |
|
| |
| 621 |
|
| # deal with the pesky 0 value |
| 622 |
|
| # since we're working with 1-base coordinates, we don't really have |
| 623 |
|
| # a real 0 coordinate, so need to skip it as it doesn't really exist |
| 624 |
|
| my $zero_check; |
| 625 |
|
| for my $i ( $start .. $stop ) { |
| 626 |
|
| $zero_check = 1 if $i == 0; |
| 627 |
|
| } |
| 628 |
|
| if ($zero_check) { |
| 629 |
|
| |
| 630 |
|
| # adjust the coordinates accordingly |
| 631 |
|
| if ( $start == 0 ) { |
| 632 |
|
| $start += 1; |
| 633 |
|
| $stop += 1; |
| 634 |
|
| } |
| 635 |
|
| elsif ( $stop == 0 ) { |
| 636 |
|
| $stop += 1; |
| 637 |
|
| } |
| 638 |
|
| else { |
| 639 |
|
| # some number in between |
| 640 |
|
| $stop += 1; |
| 641 |
|
| } |
| 642 |
|
| } |
| 643 |
|
| |
| 644 |
|
| # add new column |
| 645 |
|
| my $new_index = $Data->add_column( sprintf( "%s:%d", $new_name, $start ) ); |
| 646 |
|
| |
| 647 |
|
| # set the metadata |
| 648 |
|
| $Data->metadata( $new_index, 'start', $start ); |
| 649 |
|
| $Data->metadata( $new_index, 'stop', $stop ); |
| 650 |
|
| $Data->metadata( $new_index, 'window', $win ); |
| 651 |
|
| $Data->metadata( $new_index, 'dataset', $dataset ); |
| 652 |
|
| $Data->metadata( $new_index, 'method', $method ); |
| 653 |
|
| $Data->metadata( $new_index, 'decimal_format', $format ) if defined $format; |
| 654 |
|
| if ( $position == 5 ) { |
| 655 |
|
| $Data->metadata( $new_index, 'relative_position', '5prime_end' ); |
| 656 |
|
| } |
| 657 |
|
| elsif ( $position == 4 ) { # midpoint |
| 658 |
|
| $Data->metadata( $new_index, 'relative_position', 'center' ); |
| 659 |
|
| } |
| 660 |
|
| elsif ( $position == 9 ) { |
| 661 |
|
| $Data->metadata( $new_index, 'relative_position', 'peak_summit' ); |
| 662 |
|
| } |
| 663 |
|
| else { |
| 664 |
|
| $Data->metadata( $new_index, 'relative_position', '3prime_end' ); |
| 665 |
|
| } |
| 666 |
|
| if ($set_strand) { |
| 667 |
|
| $Data->metadata( $new_index, 'strand_implied', 1 ); |
| 668 |
|
| } |
| 669 |
|
| if ( $strand_sense =~ /sense/ ) { |
| 670 |
|
| $Data->metadata( $new_index, 'strand', $strand_sense ); |
| 671 |
|
| } |
| 672 |
|
| if ($data_database) { |
| 673 |
|
| $Data->metadata( $new_index, 'db', $data_database ); |
| 674 |
|
| } |
| 675 |
|
| if ($avoid) { |
| 676 |
|
| $Data->metadata( $new_index, 'avoid', |
| 677 |
|
| ref($avoid) eq 'ARRAY' ? join( ',', @{$avoid} ) : $avoid ); |
| 678 |
|
| } |
| 679 |
|
| if ( $dataset =~ / \. (b|cr) am $/xni and $method =~ /count/ ) { |
| 680 |
|
| $Data->metadata( $new_index, 'mapq', $mapq ); |
| 681 |
|
| } |
| 682 |
|
| } |
| 683 |
|
| } |
| 684 |
|
| |
| 685 |
|
| ## Collect relative scores using single database call |
| 686 |
|
| sub map_relative_data { |
| 687 |
|
| my $dataset = shift; |
| 688 |
|
| |
| 689 |
|
| # Collect the data |
| 690 |
|
| my $stream = $Data->row_stream; |
| 691 |
|
| while ( my $row = $stream->next_row ) { |
| 692 |
|
| my $regionscores = $row->get_relative_point_position_scores( |
| 693 |
|
| 'ddb' => $ddb, |
| 694 |
|
| 'dataset' => $dataset, |
| 695 |
|
| 'position' => $position, |
| 696 |
|
| 'extend' => $ending_point, # equivalent to the extension |
| 697 |
|
| 'stranded' => $strand_sense, |
| 698 |
|
| 'strand' => $set_strand ? $row->strand : undef, |
| 699 |
|
| 'avoid' => $avoid, |
| 700 |
|
| 'method' => $method # only really matters for count |
| 701 |
|
| ); |
| 702 |
|
| |
| 703 |
|
| # assign the scores to the windows in the region |
| 704 |
|
| for my $column ( $startcolumn .. $Data->number_columns ) { |
| 705 |
|
| |
| 706 |
|
| # record nulls if no data returned |
| 707 |
|
| unless ( scalar keys %{$regionscores} ) { |
| 708 |
|
| $row->value( $column, '.' ); |
| 709 |
|
| next; |
| 710 |
|
| } |
| 711 |
|
| |
| 712 |
|
| # get start and stop |
| 713 |
|
| my $start = $Data->metadata( $column, 'start' ); |
| 714 |
|
| my $stop = $Data->metadata( $column, 'stop' ); |
| 715 |
|
| |
| 716 |
|
| # collect a score at each position in the window |
| 717 |
|
| my @scores = map { $regionscores->{$_} } |
| 718 |
|
| grep { $_ >= $start and $_ <= $stop } |
| 719 |
|
| keys %{$regionscores}; |
| 720 |
|
| |
| 721 |
|
| # put the value into the data table |
| 722 |
|
| my $score = calculate_score( $method, \@scores ); |
| 723 |
|
| if ( $formatter and looks_like_number($score) ) { |
| 724 |
|
| $score = sprintf( $formatter, $score ); |
| 725 |
|
| } |
| 726 |
|
| $row->value( $column, $score ); |
| 727 |
|
| } |
| 728 |
|
| } |
| 729 |
|
| } |
| 730 |
|
| |
| 731 |
|
| ## Collect relative data using individual window database calls |
| 732 |
|
| sub map_relative_long_data { |
| 733 |
|
| my $dataset = shift; |
| 734 |
|
| |
| 735 |
|
| # Collect the data |
| 736 |
|
| my $stream = $Data->row_stream; |
| 737 |
|
| while ( my $row = $stream->next_row ) { |
| 738 |
|
| |
| 739 |
|
| # calculate a reference point using an internal feature method |
| 740 |
|
| my $reference = $row->calculate_reference($position); |
| 741 |
|
| |
| 742 |
|
| # collect the data for every window |
| 743 |
|
| for my $column ( $startcolumn .. $Data->number_columns ) { |
| 744 |
|
| |
| 745 |
|
| # we must modify the start and stop position with the adjustments |
| 746 |
|
| # recorded in the current column metadata |
| 747 |
|
| my $score = $row->get_score( |
| 748 |
|
| 'db' => $ddb, |
| 749 |
|
| 'dataset' => $dataset, |
| 750 |
|
| 'chromo' => $row->seq_id, |
| 751 |
|
| 'start' => $row->strand >= 0 |
| 752 |
|
| ? $reference + $Data->metadata( $column, 'start' ) |
| 753 |
|
| : $reference - $Data->metadata( $column, 'start' ), |
| 754 |
|
| 'stop' => $row->strand >= 0 |
| 755 |
|
| ? $reference + $Data->metadata( $column, 'stop' ) |
| 756 |
|
| : $reference - $Data->metadata( $column, 'stop' ), |
| 757 |
|
| 'strand' => $row->strand, |
| 758 |
|
| 'method' => $method, |
| 759 |
|
| 'stranded' => $strand_sense, |
| 760 |
|
| ); |
| 761 |
|
| if ( $formatter and looks_like_number($score) ) { |
| 762 |
|
| $score = sprintf( $formatter, $score ); |
| 763 |
|
| } |
| 764 |
|
| $row->value( $column, $score ); |
| 765 |
|
| } |
| 766 |
|
| } |
| 767 |
|
| } |
| 768 |
|
| |
| 769 |
|
| ## Interpolate the '.' values with the mean of the neighbors |
| 770 |
|
| sub go_interpolate_values { |
| 771 |
|
| |
| 772 |
|
| # determine counts |
| 773 |
|
| my $lastwindow = $Data->last_column; |
| 774 |
|
| |
| 775 |
|
| # lastwindow is the index of the last column |
| 776 |
|
| # walk through each data line and then each window |
| 777 |
|
| my $stream = $Data->row_stream; |
| 778 |
|
| while ( my $row = $stream->next_row ) { |
| 779 |
|
| my $col = $startcolumn + 1; |
| 780 |
|
| while ( $col < $lastwindow ) { |
| 781 |
|
| |
| 782 |
|
| # walk through the windows of a data row |
| 783 |
|
| # skipping the very first and last windows (columns) |
| 784 |
|
| # we will look for null values |
| 785 |
|
| # if one is found, interpolate from neighbors |
| 786 |
|
| if ( $row->value($col) eq '.' and $row->value( $col - 1 ) ne '.' ) { |
| 787 |
|
| |
| 788 |
|
| # we will interpolate the value from the neighbors |
| 789 |
|
| # first, need to check that the neighbors have values |
| 790 |
|
| |
| 791 |
|
| # find the next real value |
| 792 |
|
| my $next_i; |
| 793 |
|
| for my $i ( ( $col + 1 ) .. $lastwindow ) { |
| 794 |
|
| if ( $row->value($i) ne '.' ) { |
| 795 |
|
| $next_i = $i; |
| 796 |
|
| last; |
| 797 |
|
| } |
| 798 |
|
| } |
| 799 |
|
| unless ( defined $next_i ) { |
| 800 |
|
| $col++; |
| 801 |
|
| next; |
| 802 |
|
| } |
| 803 |
|
| |
| 804 |
|
| # determine fractional value |
| 805 |
|
| my $initial = $row->value( $col - 1 ); |
| 806 |
|
| my $fraction = |
| 807 |
|
| ( $row->value($next_i) - $initial ) / ( $next_i - $col + 1 ); |
| 808 |
|
| |
| 809 |
|
| # apply fractional values |
| 810 |
|
| for my $n ( $col .. ( $next_i - 1 ) ) { |
| 811 |
|
| my $score = $initial + ( $fraction * ( $n - $col + 1 ) ); |
| 812 |
|
| $score = sprintf( $formatter, $score ) |
| 813 |
|
| if ( $formatter and $score ne '.' ); |
| 814 |
|
| $row->value( $n, $score ); |
| 815 |
|
| } |
| 816 |
|
| |
| 817 |
|
| # jump ahead |
| 818 |
|
| $col = $next_i; |
| 819 |
|
| } |
| 820 |
|
| $col++; |
| 821 |
|
| } |
| 822 |
|
| } |
| 823 |
|
| } |
| 824 |
|
| |
| 825 |
|
| __END__ |