| 1 | package NGS::Tools::BAMSurgeon; | ||
| 2 | ### module_name.pm ################################################################################ | ||
| 3 | |||
| 4 | ### HISTORY ####################################################################################### | ||
| 5 | # Version Date Developer Comments | ||
| 6 | # 0.01 2012-06-20 Christopher Lalansingh Initial code development. | ||
| 7 | |||
| 8 | ### INCLUDES ###################################################################################### | ||
| 9 | |||
| 10 | # safe Perl | ||
| 11 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use Pod::Usage; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use YAML qw(LoadFile); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use FindBin qw($Bin); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use Params::Validate qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use File::Path qw(make_path); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use Cwd qw(abs_path); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | use Moose::Util qw( apply_all_roles ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 0 | ▶ | use HPCI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | use File::ShareDir ':ALL'; | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | NGS::Tools::BAMSurgeon | ||
| 29 | |||
| 30 | =head1 VERSION | ||
| 31 | |||
| 32 | Version 1.0.0 | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | our $VERSION = '1.0.0'; | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This package is a pipeline wrapper for BAMSurgeon (https://github.com/adamewing/bamsurgeon) and provides additional functionality for the simulation of copy number abberations in the tumour. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head1 DEPENDENCIES | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | =over | ||
| 49 | |||
| 50 | =item * | ||
| 51 | |||
| 52 | samtools (>= 1.1) | ||
| 53 | |||
| 54 | =item * | ||
| 55 | |||
| 56 | picardtools (>= 1.130) | ||
| 57 | |||
| 58 | =item * | ||
| 59 | |||
| 60 | python (2.7.x) | ||
| 61 | |||
| 62 | =item * | ||
| 63 | |||
| 64 | bamsurgeon (1.0) | ||
| 65 | |||
| 66 | =item * | ||
| 67 | |||
| 68 | bwa (>= 0.7.12) | ||
| 69 | |||
| 70 | =item * | ||
| 71 | |||
| 72 | novocraft (>= 3.02.12) | ||
| 73 | |||
| 74 | =item * | ||
| 75 | |||
| 76 | velvet (>= 1.0.13) | ||
| 77 | |||
| 78 | =item * | ||
| 79 | |||
| 80 | exonerate (>= 2.2.0) | ||
| 81 | |||
| 82 | =item * | ||
| 83 | |||
| 84 | alleleCount (>= 2.1.1) | ||
| 85 | |||
| 86 | =item * | ||
| 87 | |||
| 88 | bedtools (>= 2.24.0) | ||
| 89 | |||
| 90 | =back | ||
| 91 | |||
| 92 | =head1 USAGE | ||
| 93 | |||
| 94 | use NGS::Tools::BAMSurgeon; | ||
| 95 | |||
| 96 | my $bamsurgeon = NGS::Tools::BAMSurgeon->new( | ||
| 97 | working_dir => '/path/to/working/dir', | ||
| 98 | config => "path/to/config.yaml", | ||
| 99 | somatic_profile => "path/to/somatic.yaml", | ||
| 100 | germline_profile => "path/to/germ_mut.yaml", | ||
| 101 | bam => 'test.bam', | ||
| 102 | tumour_name => "test", | ||
| 103 | sex => 'M', | ||
| 104 | gpercent => 0.7, | ||
| 105 | seed => 12345, | ||
| 106 | # parameters for choosing mutations to spike in | ||
| 107 | minvaf => 1, | ||
| 108 | maxvaf => 1, | ||
| 109 | vafbeta1 => 2.0, | ||
| 110 | vafbeta2 => 2.0, | ||
| 111 | indel_minlen => 1, | ||
| 112 | indel_maxlen => 90, | ||
| 113 | indel_types => 'INS,DEL', | ||
| 114 | sv_minlen => 3000, | ||
| 115 | sv_maxlen => 30000, | ||
| 116 | sv_types => 'DUP,INV', | ||
| 117 | phasing => 0, | ||
| 118 | redochrs => 'all' | ||
| 119 | ); | ||
| 120 | |||
| 121 | $bamsurgeon->run( | ||
| 122 | splitbam => 0, | ||
| 123 | preparef => 0, | ||
| 124 | pickgermmut => 0, | ||
| 125 | germsim => 0, | ||
| 126 | generatecallable => 0, | ||
| 127 | picksomaticmut => 1, | ||
| 128 | picktrinucleotides => 1, | ||
| 129 | splitsubclones => 1, | ||
| 130 | somaticsim => 1, | ||
| 131 | makevcf => 1, | ||
| 132 | extractleafs => 1, | ||
| 133 | mergephases => 1, | ||
| 134 | mergechromosomes => 1, | ||
| 135 | mergefinal => 1, | ||
| 136 | allelecount => 1 | ||
| 137 | ); | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | # Define the subroutines to be exported directly to the importer when requested | ||
| 142 | use Exporter 'import'; | ||
| 143 | our @EXPORT_OK = @EXPORT_OK = qw(function1); | ||
| 144 | |||
| 145 | ### Roles ######################################################################################### | ||
| 146 | with 'BLSupport::Logging::Role'; | ||
| 147 | with 'NGS::Tools::BAMSurgeon::Germline'; | ||
| 148 | with 'NGS::Tools::BAMSurgeon::Somatic'; | ||
| 149 | with 'NGS::Tools::BAMSurgeon::Merge'; | ||
| 150 | |||
| 151 | ### Attributes #################################################################################### | ||
| 152 | |||
| 153 | has 'HPCI_group' => ( | ||
| 154 | is => 'ro', | ||
| 155 | writer => '_set_HPCI_group' | ||
| 156 | ); | ||
| 157 | |||
| 158 | has 'existing_stages' => ( | ||
| 159 | isa => 'ArrayRef', | ||
| 160 | is => 'rw' | ||
| 161 | ); | ||
| 162 | |||
| 163 | has 'sex' => ( | ||
| 164 | is => 'ro', | ||
| 165 | writer => '_set_sex', | ||
| 166 | default => 'F' | ||
| 167 | ); | ||
| 168 | |||
| 169 | has 'bam' => ( | ||
| 170 | is => 'ro', | ||
| 171 | writer => '_set_bam', | ||
| 172 | ); | ||
| 173 | |||
| 174 | has 'phasing' => ( | ||
| 175 | is => 'ro', | ||
| 176 | writer => '_set_phasing' | ||
| 177 | ); | ||
| 178 | |||
| 179 | has 'modules' => ( | ||
| 180 | isa => 'ArrayRef', | ||
| 181 | is => 'ro', | ||
| 182 | writer => '_set_modules' | ||
| 183 | ); | ||
| 184 | |||
| 185 | has 'seed' => ( | ||
| 186 | is => 'ro', | ||
| 187 | writer => '_set_seed' | ||
| 188 | ); | ||
| 189 | |||
| 190 | has 'minvaf' => ( | ||
| 191 | is => 'ro', | ||
| 192 | writer => '_set_minvaf' | ||
| 193 | ); | ||
| 194 | |||
| 195 | has 'maxvaf' => ( | ||
| 196 | is => 'ro', | ||
| 197 | writer => '_set_maxvaf' | ||
| 198 | ); | ||
| 199 | |||
| 200 | has 'vafbeta1' => ( | ||
| 201 | is => 'ro', | ||
| 202 | writer => '_set_vafbeta1' | ||
| 203 | ); | ||
| 204 | |||
| 205 | has 'vafbeta2' => ( | ||
| 206 | is => 'ro', | ||
| 207 | writer => '_set_vafbeta2' | ||
| 208 | ); | ||
| 209 | |||
| 210 | has 'chromosomes' => ( | ||
| 211 | isa => 'ArrayRef', | ||
| 212 | is => 'ro', | ||
| 213 | writer => '_set_chromosomes' | ||
| 214 | ); | ||
| 215 | |||
| 216 | has 'mut_minlens' => ( | ||
| 217 | isa => 'HashRef', | ||
| 218 | is => 'ro', | ||
| 219 | writer => '_set_mut_minlens' | ||
| 220 | ); | ||
| 221 | |||
| 222 | has 'mut_maxlens' => ( | ||
| 223 | isa => 'HashRef', | ||
| 224 | is => 'ro', | ||
| 225 | writer => '_set_mut_maxlens' | ||
| 226 | ); | ||
| 227 | |||
| 228 | has 'mut_types' => ( | ||
| 229 | isa => 'HashRef', | ||
| 230 | is => 'ro', | ||
| 231 | writer => '_set_mut_types' | ||
| 232 | ); | ||
| 233 | |||
| 234 | has 'tumour_name' => ( | ||
| 235 | is => 'ro', | ||
| 236 | writer => '_set_tumour_name' | ||
| 237 | ); | ||
| 238 | |||
| 239 | # Directories | ||
| 240 | |||
| 241 | has 'wd' => ( | ||
| 242 | is => 'ro', | ||
| 243 | writer => '_set_wd', | ||
| 244 | default => '.' | ||
| 245 | ); | ||
| 246 | |||
| 247 | has 'log_dir' => ( | ||
| 248 | is => 'ro', | ||
| 249 | writer => '_set_log_dir' | ||
| 250 | ); | ||
| 251 | |||
| 252 | has 'output_dir' => ( | ||
| 253 | is => 'ro', | ||
| 254 | writer => '_set_output_dir' | ||
| 255 | ); | ||
| 256 | |||
| 257 | has 'bin' => ( | ||
| 258 | is => 'ro', | ||
| 259 | writer => '_set_bin' | ||
| 260 | ); | ||
| 261 | |||
| 262 | has 'config_path' => ( | ||
| 263 | is => 'ro', | ||
| 264 | writer => '_set_config_path' | ||
| 265 | ); | ||
| 266 | |||
| 267 | has 'original_somatic_profile_path' => ( | ||
| 268 | is => 'ro', | ||
| 269 | writer => '_set_original_somatic_profile_path' | ||
| 270 | ); | ||
| 271 | |||
| 272 | has 'adjusted_somatic_profile_path' => ( | ||
| 273 | is => 'ro', | ||
| 274 | writer => '_set_adjusted_somatic_profile_path' | ||
| 275 | ); | ||
| 276 | |||
| 277 | # Configs: | ||
| 278 | |||
| 279 | has 'somatic_profile' => ( | ||
| 280 | is => 'ro', | ||
| 281 | writer => '_set_somatic_profile' | ||
| 282 | ); | ||
| 283 | |||
| 284 | has 'germline_profile' => ( | ||
| 285 | is => 'ro', | ||
| 286 | writer => '_set_germline_profile' | ||
| 287 | ); | ||
| 288 | |||
| 289 | has 'config' => ( | ||
| 290 | #isa => 'HashRef', | ||
| 291 | is => 'ro', | ||
| 292 | writer => '_set_config' | ||
| 293 | ); | ||
| 294 | |||
| 295 | # Get absolute paths for files and directories: | ||
| 296 | |||
| 297 | around [qw( | ||
| 298 | _set_bam | ||
| 299 | _set_wd | ||
| 300 | _set_log_dir | ||
| 301 | _set_output_dir | ||
| 302 | _set_bin | ||
| 303 | _set_config_path | ||
| 304 | _set_original_somatic_profile_path | ||
| 305 | _set_adjusted_somatic_profile_path | ||
| 306 | )] => sub { | ||
| 307 | my $orig = shift; | ||
| 308 | my $self = shift; | ||
| 309 | my $value = shift; | ||
| 310 | $self->$orig(abs_path($value)); | ||
| 311 | }; | ||
| 312 | |||
| 313 | # Stages: | ||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | =head1 METHODS | ||
| 318 | |||
| 319 | =head2 new | ||
| 320 | |||
| 321 | This subroutine creates a NGS::Tools::BAMSurgeon object with the following parameters (mandatory parameters are denoted with 1, optional with 0, or having a default specified, as per Params::Validate specifications): | ||
| 322 | |||
| 323 | config => 1, | ||
| 324 | somatic_profile => 1, | ||
| 325 | germline_profile => 1, | ||
| 326 | bam => 1, | ||
| 327 | sex => 1, | ||
| 328 | gpercent => 1, | ||
| 329 | seed => 0, | ||
| 330 | minvaf => 1, | ||
| 331 | maxvaf => 1, | ||
| 332 | vafbeta1 => { default => 2.0 }, | ||
| 333 | vafbeta2 => { default => 2.0 }, | ||
| 334 | indel_minlen => { default => 1 }, | ||
| 335 | indel_maxlen => { default => 90 }, | ||
| 336 | indel_types => { default => 'INS,DEL' }, | ||
| 337 | sv_minlen => { default => 3000 }, | ||
| 338 | sv_maxlen => { default => 30000 }, | ||
| 339 | sv_types => { default => 'DUP,INV,DEL' }, | ||
| 340 | working_dir => { default => '.' }, | ||
| 341 | tumour_name => { default => 'sm' }, | ||
| 342 | log_dir => 0, | ||
| 343 | modules => 0, | ||
| 344 | phasing => 1, | ||
| 345 | redochrs => { default => 'all' } | ||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | sub BUILD { | ||
| 350 | my $self = shift; | ||
| 351 | my %args = validate ( | ||
| 352 | @_, | ||
| 353 | { | ||
| 354 | config => 1, | ||
| 355 | somatic_profile => 1, | ||
| 356 | germline_profile => 1, | ||
| 357 | bam => 1, | ||
| 358 | sex => 1, | ||
| 359 | gpercent => 1, | ||
| 360 | seed => 0, | ||
| 361 | minvaf => 1, | ||
| 362 | maxvaf => 1, | ||
| 363 | vafbeta1 => { default => 2.0 }, | ||
| 364 | vafbeta2 => { default => 2.0 }, | ||
| 365 | indel_minlen => { default => 1 }, | ||
| 366 | indel_maxlen => { default => 90 }, | ||
| 367 | indel_types => { default => 'INS,DEL' }, | ||
| 368 | sv_minlen => { default => 3000 }, | ||
| 369 | sv_maxlen => { default => 30000 }, | ||
| 370 | sv_types => { default => 'DUP,INV,DEL' }, | ||
| 371 | working_dir => { default => '.' }, | ||
| 372 | tumour_name => { default => 'sm' }, | ||
| 373 | log_dir => 0, | ||
| 374 | modules => 0, | ||
| 375 | phasing => 1, | ||
| 376 | redochrs => { default => 'all' } | ||
| 377 | } | ||
| 378 | ); | ||
| 379 | |||
| 380 | $self->_set_config_path($args{config}); | ||
| 381 | $self->_set_config(LoadFile(abs_path($args{config}))); | ||
| 382 | $self->_set_original_somatic_profile_path($args{somatic_profile}); | ||
| 383 | $self->_set_germline_profile(LoadFile(abs_path($args{germline_profile}))); | ||
| 384 | |||
| 385 | $self->_set_tumour_name($args{tumour_name}); | ||
| 386 | $self->_set_bam($args{bam}); | ||
| 387 | $self->_set_sex($args{sex}); | ||
| 388 | $self->_set_phasing($args{phasing}); | ||
| 389 | |||
| 390 | $self->_set_gpercent($args{gpercent}); | ||
| 391 | |||
| 392 | $self->_set_wd($args{working_dir}); | ||
| 393 | $self->_set_output_dir("$args{working_dir}/$args{tumour_name}"); | ||
| 394 | if (! -e $self->output_dir) { | ||
| 395 | make_path($self->output_dir); | ||
| 396 | } | ||
| 397 | $self->_set_adjusted_somatic_profile_path($self->output_dir . "/adjusted_somatic_profile.yaml"); | ||
| 398 | $self->_set_bin($Bin); | ||
| 399 | |||
| 400 | my @chromosomes; | ||
| 401 | if ('all' eq $args{redochrs}) { | ||
| 402 | @chromosomes = (1..22); | ||
| 403 | push @chromosomes,'X'; | ||
| 404 | if($self->sex eq 'M'){ | ||
| 405 | push @chromosomes, 'Y'; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | else { | ||
| 409 | @chromosomes = split(',', $args{redochrs}); | ||
| 410 | } | ||
| 411 | $self->_set_chromosomes(\@chromosomes); | ||
| 412 | |||
| 413 | $self->_set_mut_minlens({ | ||
| 414 | indel => $args{indel_minlen}, | ||
| 415 | sv => $args{sv_minlen} | ||
| 416 | }); | ||
| 417 | $self->_set_mut_maxlens({ | ||
| 418 | indel => $args{indel_maxlen}, | ||
| 419 | sv => $args{sv_maxlen} | ||
| 420 | }); | ||
| 421 | $self->_set_mut_types({ | ||
| 422 | indel => $args{indel_types}, | ||
| 423 | sv => $args{sv_types} | ||
| 424 | }); | ||
| 425 | |||
| 426 | if (defined($args{log_dir})) { | ||
| 427 | if (! -e $args{log_dir}) { | ||
| 428 | make_path($args{log_dir}); | ||
| 429 | } | ||
| 430 | $self->_set_log_dir($args{log_dir}); | ||
| 431 | } | ||
| 432 | elsif (! -e ($self->wd . "/logs")) { | ||
| 433 | make_path($self->wd . "/logs"); | ||
| 434 | $self->_set_log_dir($self->wd . "/logs"); | ||
| 435 | } | ||
| 436 | else { | ||
| 437 | $self->_set_log_dir($self->wd . "/logs"); | ||
| 438 | } | ||
| 439 | |||
| 440 | $self->validate_config(); | ||
| 441 | |||
| 442 | $self->_set_seed($args{seed}); | ||
| 443 | $self->_set_minvaf($args{minvaf}); | ||
| 444 | $self->_set_maxvaf($args{maxvaf}); | ||
| 445 | $self->_set_vafbeta1($args{vafbeta1}); | ||
| 446 | $self->_set_vafbeta2($args{vafbeta2}); | ||
| 447 | |||
| 448 | if (defined($args{modules})) { | ||
| 449 | $self->_set_modules($args{modules}); | ||
| 450 | } | ||
| 451 | else { | ||
| 452 | $self->_set_modules([ | ||
| 453 | 'Python-BL/2.7.10', | ||
| 454 | 'samtools/1.2', | ||
| 455 | 'bcftools/1.1', | ||
| 456 | 'bwa/0.7.12', | ||
| 457 | 'picard/1.130', | ||
| 458 | 'velvet/1.0.13', | ||
| 459 | 'exonerate/2.2.0', | ||
| 460 | 'java/1.8.0_45', | ||
| 461 | 'bamql/head', | ||
| 462 | 'novocraft/3.02.12', | ||
| 463 | 'yaml/0.1.5', | ||
| 464 | 'bedtools' | ||
| 465 | ]); | ||
| 466 | } | ||
| 467 | |||
| 468 | $self->init_logging(directory => $self->log_dir); | ||
| 469 | $self->_set_HPCI_group(HPCI->group(name => 'BS-'.$args{tumour_name}, cluster => 'SGE', base_dir => $self->log_dir, max_concurrent => 50)); | ||
| 470 | } | ||
| 471 | |||
| 472 | =head2 run | ||
| 473 | |||
| 474 | This subroutine executes the pipeline with the parameters specified when the NGS::Tools::BAMSurgeon object is created. Stages can be turned on/off using the following parameters: | ||
| 475 | |||
| 476 | splitbam => 0, | ||
| 477 | preparef => 0, | ||
| 478 | pickgermmut => 0, | ||
| 479 | germsim => 0, | ||
| 480 | generatecallable => 0, | ||
| 481 | picksomaticmut => 0, | ||
| 482 | picktrinucleotides => 0, | ||
| 483 | somaticsim => 0, | ||
| 484 | makevcf => 0, | ||
| 485 | extractleafs => 0, | ||
| 486 | mergephases => 0, | ||
| 487 | mergechromosomes => 0, | ||
| 488 | mergefinal => 0, | ||
| 489 | allelecount => 0 | ||
| 490 | |||
| 491 | =cut | ||
| 492 | |||
| 493 | sub run { | ||
| 494 | my $self = shift; | ||
| 495 | my %args = ( | ||
| 496 | splitbam => 0, | ||
| 497 | preparef => 0, | ||
| 498 | pickgermmut => 0, | ||
| 499 | germsim => 0, | ||
| 500 | generatecallable => 0, | ||
| 501 | picksomaticmut => 0, | ||
| 502 | picktrinucleotides => 0, | ||
| 503 | somaticsim => 0, | ||
| 504 | makevcf => 0, | ||
| 505 | extractleafs => 0, | ||
| 506 | mergephases => 0, | ||
| 507 | mergechromosomes => 0, | ||
| 508 | mergefinal => 0, | ||
| 509 | allelecount => 0, | ||
| 510 | @_ | ||
| 511 | ); | ||
| 512 | |||
| 513 | $self->adjust_yaml_for_cna(); | ||
| 514 | $self->_set_somatic_profile(LoadFile($self->adjusted_somatic_profile_path)); | ||
| 515 | |||
| 516 | my $stage_groups = {}; | ||
| 517 | if($args{splitbam}){ | ||
| 518 | $stage_groups->{splitbam} = $self->splitbam2chr(); | ||
| 519 | } | ||
| 520 | if($args{preparef}){ | ||
| 521 | $stage_groups->{preparef} = $self->prepare_reference(); | ||
| 522 | } | ||
| 523 | if($args{pickgermmut}){ | ||
| 524 | $stage_groups->{pickgermmut} = $self->pick_germ_mutations(); | ||
| 525 | } | ||
| 526 | if($args{germsim}){ | ||
| 527 | $stage_groups->{germsim} = $self->germline_simulation(); | ||
| 528 | $self->HPCI_group->add_deps( | ||
| 529 | pre_reqs => [ | ||
| 530 | @{$stage_groups->{splitbam} // []}, | ||
| 531 | @{$stage_groups->{prepareref} // []}, | ||
| 532 | @{$stage_groups->{pickgermmut} // []} | ||
| 533 | ], | ||
| 534 | deps => $stage_groups->{germsim} | ||
| 535 | ); | ||
| 536 | #$self->check_germ(); | ||
| 537 | } | ||
| 538 | if($args{generatecallable}){ | ||
| 539 | $stage_groups->{generatecallable} = $self->generate_callable_bases(); | ||
| 540 | $self->HPCI_group->add_deps( | ||
| 541 | pre_reqs => [ | ||
| 542 | @{$stage_groups->{germsim} // []}, | ||
| 543 | @{$stage_groups->{splitbam} // []}, | ||
| 544 | @{$stage_groups->{prepareref} // []}, | ||
| 545 | @{$stage_groups->{pickgermmut} // []} | ||
| 546 | ], | ||
| 547 | deps => $stage_groups->{generatecallable} | ||
| 548 | ); | ||
| 549 | } | ||
| 550 | if($args{picksomaticmut}){ | ||
| 551 | $stage_groups->{picksomaticmut} = $self->pick_somatic_mutations($args{picktrinucleotides}); | ||
| 552 | $self->HPCI_group->add_deps( | ||
| 553 | pre_reqs => [ | ||
| 554 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 555 | @{$stage_groups->{generatecallable} // []}, | ||
| 556 | @{$stage_groups->{germsim} // []}, | ||
| 557 | @{$stage_groups->{splitbam} // []}, | ||
| 558 | @{$stage_groups->{prepareref} // []}, | ||
| 559 | @{$stage_groups->{pickgermmut} // []} | ||
| 560 | ], | ||
| 561 | deps => $stage_groups->{picksomaticmut} | ||
| 562 | ); | ||
| 563 | } | ||
| 564 | if ($args{splitsubclones}) { | ||
| 565 | $stage_groups->{splitsubclones} = $self->split_subclones(); | ||
| 566 | $self->HPCI_group->add_deps( | ||
| 567 | pre_reqs => [ | ||
| 568 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 569 | @{$stage_groups->{generatecallable} // []}, | ||
| 570 | @{$stage_groups->{germsim} // []}, | ||
| 571 | @{$stage_groups->{splitbam} // []}, | ||
| 572 | @{$stage_groups->{prepareref} // []}, | ||
| 573 | @{$stage_groups->{pickgermmut} // []} | ||
| 574 | ], | ||
| 575 | deps => $stage_groups->{splitsubclones} | ||
| 576 | ); | ||
| 577 | } | ||
| 578 | if($args{somaticsim}){ | ||
| 579 | $stage_groups->{somaticsim} = $self->somatic_simulation(); | ||
| 580 | $self->HPCI_group->add_deps( | ||
| 581 | pre_reqs => [ | ||
| 582 | @{$stage_groups->{picksomaticmut} // []}, | ||
| 583 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 584 | @{$stage_groups->{generatecallable} // []}, | ||
| 585 | @{$stage_groups->{germsim} // []}, | ||
| 586 | @{$stage_groups->{splitbam} // []}, | ||
| 587 | @{$stage_groups->{prepareref} // []}, | ||
| 588 | @{$stage_groups->{pickgermmut} // []}, | ||
| 589 | @{$stage_groups->{splitsubclones} // []} | ||
| 590 | ], | ||
| 591 | deps => $stage_groups->{somaticsim} | ||
| 592 | ); | ||
| 593 | } | ||
| 594 | if($args{makevcf}){ | ||
| 595 | $stage_groups->{makevcf} = $self->merge_vcf(); | ||
| 596 | $self->HPCI_group->add_deps( | ||
| 597 | pre_reqs => [ | ||
| 598 | @{$stage_groups->{somaticsim} // []}, | ||
| 599 | @{$stage_groups->{picksomaticmut} // []}, | ||
| 600 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 601 | @{$stage_groups->{generatecallable} // []}, | ||
| 602 | @{$stage_groups->{germsim} // []}, | ||
| 603 | @{$stage_groups->{splitbam} // []}, | ||
| 604 | @{$stage_groups->{prepareref} // []}, | ||
| 605 | @{$stage_groups->{pickgermmut} // []}, | ||
| 606 | @{$stage_groups->{splitsubclones} // []} | ||
| 607 | ], | ||
| 608 | deps => $stage_groups->{makevcf} | ||
| 609 | ); | ||
| 610 | } | ||
| 611 | if($args{extractleafs}){ | ||
| 612 | $stage_groups->{extractleafs} = $self->extract_leafs(); | ||
| 613 | $self->HPCI_group->add_deps( | ||
| 614 | pre_reqs => [ | ||
| 615 | @{$stage_groups->{somaticsim} // []}, | ||
| 616 | @{$stage_groups->{picksomaticmut} // []}, | ||
| 617 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 618 | @{$stage_groups->{generatecallable} // []}, | ||
| 619 | @{$stage_groups->{germsim} // []}, | ||
| 620 | @{$stage_groups->{splitbam} // []}, | ||
| 621 | @{$stage_groups->{prepareref} // []}, | ||
| 622 | @{$stage_groups->{pickgermmut} // []}, | ||
| 623 | @{$stage_groups->{splitsubclones} // []} | ||
| 624 | ], | ||
| 625 | deps => $stage_groups->{extractleafs} | ||
| 626 | ); | ||
| 627 | } | ||
| 628 | if($args{mergephases}){ | ||
| 629 | $stage_groups->{mergephases} = $self->merge_to_phases(); | ||
| 630 | $self->HPCI_group->add_deps( | ||
| 631 | pre_reqs => [ | ||
| 632 | @{$stage_groups->{extractleafs} // []}, | ||
| 633 | @{$stage_groups->{somaticsim} // []}, | ||
| 634 | @{$stage_groups->{picksomaticmut} // []}, | ||
| 635 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 636 | @{$stage_groups->{generatecallable} // []}, | ||
| 637 | @{$stage_groups->{germsim} // []}, | ||
| 638 | @{$stage_groups->{splitbam} // []}, | ||
| 639 | @{$stage_groups->{prepareref} // []}, | ||
| 640 | @{$stage_groups->{pickgermmut} // []}, | ||
| 641 | @{$stage_groups->{splitsubclones} // []} | ||
| 642 | ], | ||
| 643 | deps => $stage_groups->{mergephases} | ||
| 644 | ); | ||
| 645 | } | ||
| 646 | if($args{mergechromosomes}){ | ||
| 647 | $stage_groups->{mergechromosomes} = $self->merge_to_chromosomes(); | ||
| 648 | $self->HPCI_group->add_deps( | ||
| 649 | pre_reqs => [ | ||
| 650 | @{$stage_groups->{mergephases} // []}, | ||
| 651 | @{$stage_groups->{somaticsim} // []}, | ||
| 652 | @{$stage_groups->{picksomaticmut} // []}, | ||
| 653 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 654 | @{$stage_groups->{generatecallable} // []}, | ||
| 655 | @{$stage_groups->{germsim} // []}, | ||
| 656 | @{$stage_groups->{splitbam} // []}, | ||
| 657 | @{$stage_groups->{prepareref} // []}, | ||
| 658 | @{$stage_groups->{pickgermmut} // []}, | ||
| 659 | @{$stage_groups->{splitsubclones} // []} | ||
| 660 | ], | ||
| 661 | deps => $stage_groups->{mergechromosomes} | ||
| 662 | ); | ||
| 663 | } | ||
| 664 | if($args{mergefinal}){ | ||
| 665 | $stage_groups->{mergefinal} = $self->merge_to_final(); | ||
| 666 | $self->HPCI_group->add_deps( | ||
| 667 | pre_reqs => [ | ||
| 668 | @{$stage_groups->{mergechromosomes} // []}, | ||
| 669 | @{$stage_groups->{mergephases} // []}, | ||
| 670 | @{$stage_groups->{somaticsim} // []}, | ||
| 671 | @{$stage_groups->{picksomaticmut} // []}, | ||
| 672 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 673 | @{$stage_groups->{generatecallable} // []}, | ||
| 674 | @{$stage_groups->{germsim} // []}, | ||
| 675 | @{$stage_groups->{splitbam} // []}, | ||
| 676 | @{$stage_groups->{prepareref} // []}, | ||
| 677 | @{$stage_groups->{pickgermmut} // []}, | ||
| 678 | @{$stage_groups->{splitsubclones} // []} | ||
| 679 | ], | ||
| 680 | deps => $stage_groups->{mergefinal} | ||
| 681 | ); | ||
| 682 | } | ||
| 683 | if($args{allelecount}){ | ||
| 684 | $stage_groups->{allelecount} = $self->allelecount(); | ||
| 685 | $self->HPCI_group->add_deps( | ||
| 686 | pre_reqs => [ | ||
| 687 | @{$stage_groups->{mergechromosomes} // []}, | ||
| 688 | @{$stage_groups->{mergephases} // []}, | ||
| 689 | @{$stage_groups->{somaticsim} // []}, | ||
| 690 | @{$stage_groups->{picksomaticmut} // []}, | ||
| 691 | @{$stage_groups->{picktrinucleotides} // []}, | ||
| 692 | @{$stage_groups->{generatecallable} // []}, | ||
| 693 | @{$stage_groups->{germsim} // []}, | ||
| 694 | @{$stage_groups->{splitbam} // []}, | ||
| 695 | @{$stage_groups->{prepareref} // []}, | ||
| 696 | @{$stage_groups->{pickgermmut} // []}, | ||
| 697 | @{$stage_groups->{splitsubclones} // []} | ||
| 698 | ], | ||
| 699 | deps => $stage_groups->{allelecount} | ||
| 700 | ); | ||
| 701 | } | ||
| 702 | |||
| 703 | my %hpci_results = %{$self->HPCI_group->execute()}; | ||
| 704 | my $ret_val = 0; | ||
| 705 | foreach my $name (keys %hpci_results) { | ||
| 706 | my $stage = $hpci_results{$name}; | ||
| 707 | my $stage_length = scalar @{$stage}; | ||
| 708 | $stage->[$stage_length - 1]->{'wallclock_time'} =~ s/^\s+|\s+$//g ; | ||
| 709 | print STDOUT "Stage $name took " . $stage->[$stage_length - 1]->{'wallclock_time'} . " seconds.\n"; | ||
| 710 | if ($stage_length == 0 || $stage->[$stage_length - 1]->{'exit_status'} !~ m/^\s*0\s*$/g) { | ||
| 711 | print STDERR "Failure of stage $name\n"; | ||
| 712 | $ret_val = 1; | ||
| 713 | } | ||
| 714 | } | ||
| 715 | return $ret_val; | ||
| 716 | } | ||
| 717 | |||
| 718 | sub validate_config { | ||
| 719 | my $self = shift; | ||
| 720 | |||
| 721 | if (!defined($self->config->{reference})) { | ||
| 722 | croak("YAML config contains no reference file!\n"); | ||
| 723 | } | ||
| 724 | |||
| 725 | my $cancer_types = YAML::LoadFile(dist_file('NGS-Tools-BAMSurgeon', 'cancer_sigmut.yaml')); | ||
| 726 | if (!exists($cancer_types->{$self->config->{cancer}->{name}})) { | ||
| 727 | croak("The cancer type specified in the YAML config file is not supported. Please see below for specified cancer types (these are case sensitive):\n\n". | ||
| 728 | Dumper(keys %{$cancer_types})); | ||
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 | sub splitbam2chr { | ||
| 733 | my $self = shift; | ||
| 734 | |||
| 735 | $self->log->info("splitbam2chr\n"); | ||
| 736 | $self->log->info("\tbam: " . $self->bam . "\n"); | ||
| 737 | |||
| 738 | # For every chromosome, call the module's splitbam2chr function | ||
| 739 | my $stages = []; | ||
| 740 | foreach my $chr (@{$self->chromosomes}) { | ||
| 741 | if (! -e $self->wd . "/chrbam/") { | ||
| 742 | make_path($self->wd . "/chrbam/"); | ||
| 743 | } | ||
| 744 | my $stage = $self->HPCI_group->stage( | ||
| 745 | name => 'bs_split_' . $chr, | ||
| 746 | command => 'samtools view -bh ' . $self->bam . " $chr > " . $self->wd . "/chrbam/$chr.bam", | ||
| 747 | resources_required => { | ||
| 748 | h_vmem => '3G' | ||
| 749 | }, | ||
| 750 | modules_to_load => [ 'samtools/1.2' ] | ||
| 751 | ); | ||
| 752 | push(@{$stages}, $stage); | ||
| 753 | } | ||
| 754 | return $stages; | ||
| 755 | } | ||
| 756 | |||
| 757 | sub prepare_reference { | ||
| 758 | my $self = shift; | ||
| 759 | |||
| 760 | $self->log->info("prepare_reference\n"); | ||
| 761 | $self->log->info("\treference: " . $self->config->{reference} . "\n"); | ||
| 762 | |||
| 763 | if (! -e $self->wd . "/chrFa/") { | ||
| 764 | make_path($self->wd . "/chrFa/"); | ||
| 765 | } | ||
| 766 | |||
| 767 | my $stages = []; | ||
| 768 | my $command = ''; | ||
| 769 | |||
| 770 | # Call the module's generate_index function | ||
| 771 | my $program = "bwa index"; | ||
| 772 | my $params = join(' ', | ||
| 773 | "-a bwtsw", | ||
| 774 | $self->config->{reference} | ||
| 775 | ); | ||
| 776 | if (! -e $self->config->{reference} . ".bwt") { | ||
| 777 | $command .= join(' ',$program,$params); | ||
| 778 | } | ||
| 779 | |||
| 780 | my $nix = $self->config->{reference}; | ||
| 781 | $nix =~ s/.fasta/.nix/g; | ||
| 782 | $program = "novoindex"; | ||
| 783 | $params = join(' ', | ||
| 784 | "$nix", | ||
| 785 | $self->config->{reference} | ||
| 786 | ); | ||
| 787 | if (! -e $nix) { | ||
| 788 | $command .= join(' ',$program,$params); | ||
| 789 | } | ||
| 790 | |||
| 791 | my $dict = $self->config->{reference}; | ||
| 792 | $dict =~ s/.fasta/.dict/g; | ||
| 793 | $program = 'java -jar $PICARDROOT/picard.jar CreateSequenceDictionary'; | ||
| 794 | $params = join(' ', | ||
| 795 | "R=" . $self->config->{reference}, | ||
| 796 | "O=$dict" | ||
| 797 | ); | ||
| 798 | if (! -e $dict) { | ||
| 799 | $command .= join(' ',$program,$params); | ||
| 800 | } | ||
| 801 | |||
| 802 | my $gen_index_stage = $self->HPCI_group->stage( | ||
| 803 | name => 'bs_prep_ref_gen_index', | ||
| 804 | command => $command, | ||
| 805 | resources_required => { | ||
| 806 | h_vmem => '8G' | ||
| 807 | }, | ||
| 808 | modules_to_load => [ 'bwa/0.7.12', 'picard/1.130' ] | ||
| 809 | ); | ||
| 810 | push(@{$stages}, $gen_index_stage); | ||
| 811 | |||
| 812 | # Call the module's split_genome function | ||
| 813 | my $split_genome_stage = $self->HPCI_group->stage( | ||
| 814 | name => 'bs_prep_ref_split_genome', | ||
| 815 | command => "cd " . $self->wd . "/chrFa/; awk '/^>/ {s = ++d\".fa\"} {print > s}' " . $self->config->{reference} . "; mv 23.fa X.fa; mv 24.fa Y.fa; mv 25.fa MT.fa; rm [3-9][0-9].fa; rm 2[6-9].fa", | ||
| 816 | resources_required => { | ||
| 817 | h_vmem => '2G' | ||
| 818 | } | ||
| 819 | ); | ||
| 820 | push(@{$stages}, $split_genome_stage); | ||
| 821 | $self->HPCI_group->add_deps( | ||
| 822 | pre_reqs => [ $gen_index_stage ], | ||
| 823 | deps => [ $split_genome_stage ] | ||
| 824 | ); | ||
| 825 | |||
| 826 | # For every chromosome, call the module's faidx_parallel function | ||
| 827 | foreach my $chr (@{$self->chromosomes}) { | ||
| 828 | my $faidx_stage = $self->HPCI_group->stage( | ||
| 829 | name => 'bs_prep_ref_faidx_parallel_' . $chr, | ||
| 830 | command => "cd " . $self->wd . "/chrFa/; samtools faidx $chr.fa", | ||
| 831 | resources_required => { | ||
| 832 | h_vmem => '2G' | ||
| 833 | }, | ||
| 834 | modules_to_load => [ 'samtools/1.2' ] | ||
| 835 | ); | ||
| 836 | push(@{$stages}, $faidx_stage); | ||
| 837 | $self->HPCI_group->add_deps( | ||
| 838 | pre_reqs => [ $split_genome_stage ], | ||
| 839 | deps => [ $faidx_stage ] | ||
| 840 | ); | ||
| 841 | } | ||
| 842 | |||
| 843 | return $stages; | ||
| 844 | } | ||
| 845 | |||
| 846 | sub generate_callable_bases { | ||
| 847 | my $self = shift; | ||
| 848 | |||
| 849 | $self->log->info("generate_callable_bases\n"); | ||
| 850 | $self->log->info("\tbam: " . $self->bam . "\n"); | ||
| 851 | $self->log->info("\tsex: " . $self->sex . "\n"); | ||
| 852 | |||
| 853 | my $min_cov = 10; | ||
| 854 | my $stages = []; | ||
| 855 | |||
| 856 | foreach my $chr (@{$self->chromosomes}) { | ||
| 857 | my $phase_stages = []; | ||
| 858 | my @phases = (0,1); | ||
| 859 | if ($self->sex eq 'M' && ($chr eq 'X' || $chr eq 'Y')) { | ||
| 860 | @phases = (0); | ||
| 861 | } | ||
| 862 | if (! -e $self->wd . "/cb_bed/chr$chr") { | ||
| 863 | make_path($self->wd . "/cb_bed/chr$chr"); | ||
| 864 | } | ||
| 865 | foreach my $phase (@phases) { | ||
| 866 | my $cb_stage = $self->HPCI_group->stage( | ||
| 867 | name => "bs_gen_cb_p$phase\_$chr", | ||
| 868 | command => "samtools view -b " . $self->wd . "/chrbam/chr$chr/phase.$phase.bam " . | ||
| 869 | '| bedtools genomecov -bga -ibam - | awk \'$4 >= ' . $min_cov . ' {print $0}\' > ' . $self->wd . "/cb_bed/chr$chr/phase.$phase.bam.cov$min_cov.bed; " . | ||
| 870 | "bedtools merge -i " . $self->wd . "/cb_bed/chr$chr/phase.$phase.bam.cov$min_cov.bed > " . $self->wd . "/cb_bed/chr$chr/phase.$phase.bam.cov$min_cov\_collapsed.bed; ", | ||
| 871 | resources_required => { | ||
| 872 | h_vmem => '10G' | ||
| 873 | }, | ||
| 874 | modules_to_load => ['Perl-BL', 'Schedule-DRMAAc', 'samtools/1.2', 'bedtools/2.24.0'] | ||
| 875 | ); | ||
| 876 | push(@{$stages}, $cb_stage); | ||
| 877 | push(@{$phase_stages}, $cb_stage); | ||
| 878 | } | ||
| 879 | if ($self->sex eq 'M' && ($chr eq 'X' || $chr eq 'Y')) { | ||
| 880 | my $mv_stage = $self->HPCI_group->stage( | ||
| 881 | name => "bs_gen_cb_p0_$chr\_mv", | ||
| 882 | command => "mv " . $self->wd . "/cb_bed/chr$chr/phase.0.bam.cov$min_cov\_collapsed.bed " . $self->wd . "/cb_bed/$chr\_collapsed.bed", | ||
| 883 | resources_required => { | ||
| 884 | h_vmem => '2G' | ||
| 885 | } | ||
| 886 | ); | ||
| 887 | push(@{$stages}, $mv_stage); | ||
| 888 | $self->HPCI_group->add_deps( | ||
| 889 | pre_reqs => $phase_stages, | ||
| 890 | deps => [$mv_stage] | ||
| 891 | ); | ||
| 892 | } | ||
| 893 | else { | ||
| 894 | my $intersect_stage = $self->HPCI_group->stage( | ||
| 895 | name => "bs_gen_cb_intersect_$chr", | ||
| 896 | command => | ||
| 897 | "bedtools intersect -a " . $self->wd . "/cb_bed/chr$chr/phase.1.bam.cov$min_cov\_collapsed.bed -b " . $self->wd . "/cb_bed/chr$chr/phase.0.bam.cov$min_cov\_collapsed.bed > " . $self->wd . "/cb_bed/$chr\_unsorted.bed; " . | ||
| 898 | "sort -k1,1 -k2,2n " . $self->wd . "/cb_bed/$chr\_unsorted.bed > " . $self->wd . "/cb_bed/$chr\_collapsed.bed", | ||
| 899 | resources_required => { | ||
| 900 | h_vmem => '2G' | ||
| 901 | }, | ||
| 902 | modules_to_load => ['bedtools'], | ||
| 903 | extra_sge_args_string => " -q transient " | ||
| 904 | ); | ||
| 905 | push(@{$stages}, $intersect_stage); | ||
| 906 | $self->HPCI_group->add_deps( | ||
| 907 | pre_reqs => $phase_stages, | ||
| 908 | deps => [$intersect_stage] | ||
| 909 | ); | ||
| 910 | } | ||
| 911 | } | ||
| 912 | return $stages; | ||
| 913 | } | ||
| 914 | |||
| 915 | sub allelecount { | ||
| 916 | my $self = shift; | ||
| 917 | |||
| 918 | my $stages = []; | ||
| 919 | my $generation_stages = []; | ||
| 920 | |||
| 921 | if (! -e $self->output_dir . "/alleleCounts") { | ||
| 922 | make_path($self->output_dir . "/alleleCounts"); | ||
| 923 | } | ||
| 924 | |||
| 925 | foreach my $chr (@{$self->chromosomes}) { | ||
| 926 | my $chrname = $chr; | ||
| 927 | if ($chrname eq 'X') { | ||
| 928 | $chrname = 23; | ||
| 929 | } | ||
| 930 | if ($chrname eq 'Y') { | ||
| 931 | next; | ||
| 932 | } | ||
| 933 | my $allele_count_stage = $self->HPCI_group->stage( | ||
| 934 | name => 'bs_allele_count_' . $chr, | ||
| 935 | command => "alleleCounter " . | ||
| 936 | "-l " . $self->config->{loci} . "/1000genomesloci2012_chr$chr.txt " . | ||
| 937 | "-b " . $self->output_dir . "/chr$chr/output/T.bam " . | ||
| 938 | "-o " . $self->output_dir . "/alleleCounts/chr$chrname.txt " . | ||
| 939 | "-r " . $self->config->{reference} . " ", | ||
| 940 | resources_required => { | ||
| 941 | h_vmem => '4G' | ||
| 942 | }, | ||
| 943 | modules_to_load => [ 'alleleCount' ], | ||
| 944 | extra_sge_args_string => " -q transient " | ||
| 945 | ); | ||
| 946 | push(@{$stages}, $allele_count_stage); | ||
| 947 | push(@{$generation_stages}, $allele_count_stage); | ||
| 948 | } | ||
| 949 | my $coverage_stage = $self->HPCI_group->stage( | ||
| 950 | name => 'bs_allele_count_coverage', | ||
| 951 | command => "cd " . $self->output_dir . "/alleleCounts; Rscript " . $self->bin . "/get_coverage.R;", | ||
| 952 | resources_required => { | ||
| 953 | h_vmem => '4G' | ||
| 954 | }, | ||
| 955 | modules_to_load => [ 'R-BL', 'alleleCount' ], | ||
| 956 | extra_sge_args_string => " -q transient " | ||
| 957 | ); | ||
| 958 | push(@{$stages}, $coverage_stage); | ||
| 959 | $self->HPCI_group->add_deps( | ||
| 960 | pre_reqs => $generation_stages, | ||
| 961 | deps => [$coverage_stage], | ||
| 962 | ); | ||
| 963 | |||
| 964 | return $stages; | ||
| 965 | } | ||
| 966 | |||
| 967 | |||
| 968 | =head1 AUTHORS | ||
| 969 | |||
| 970 | Christopher Lalansingh - Boutros Lab | ||
| 971 | |||
| 972 | Shadrielle Melijah G. Espiritu - Boutros Lab | ||
| 973 | |||
| 974 | =head1 ACKNOWLEDGEMENTS | ||
| 975 | |||
| 976 | Paul Boutros, Phd, PI - Boutros Lab | ||
| 977 | |||
| 978 | Lydia Liu - Boutros Lab | ||
| 979 | |||
| 980 | Takafumi Yamaguchi - Boutros Lab | ||
| 981 | |||
| 982 | Srinivasan Sivanandan - Boutros Lab | ||
| 983 | |||
| 984 | Adam D. Ewing - BAMSurgeon author | ||
| 985 | |||
| 986 | The Ontario Institute for Cancer Research | ||
| 987 | |||
| 988 | =cut | ||
| 989 | |||
| 990 | 1; | ||
| 991 | |||