lib/NGS/Tools/BAMSurgeon/Somatic.pm untested

filter: Statementstmt 0.0% 0 / 475 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 680 CC 49 SCAR 40.4
CC
49
Cov
0%
_mutation_generation
182.0
_determineProfiles
90.0
_designatePortions
56.0
CRAP 56.9
1 package NGS::Tools::BAMSurgeon::Somatic;
2  
3 use warnings;
4 use strict;
5 use Carp;
6 use Moose::Role;
7 use FindBin qw($Bin);
8 use Params::Validate qw(:all);
9 use Data::Dumper;
10 use File::Path qw(make_path);
11 use YAML qw(LoadFile DumpFile Dump);
12 use HPCI;
13 use Cwd qw(abs_path);
14 use List::Util qw(sum min max);
15 use File::Basename;
16 use File::Copy;
17 use List::MoreUtils qw(each_array uniq);
18  
19 our %opts = ();
20 our $max_total_copies = 1;
21  
22 has 'leaves' => (
23 isa => 'HashRef',
24 is => 'rw',
25 default => sub { {} }
26 );
27  
28 sub adjust_yaml_for_cna {
29 my $self = shift;
30  
31 $self->log->info("adjust_yaml_for_cna\n");
32 $self->log->info("\toriginal_somatic_profile_path: " . $self->original_somatic_profile_path . "\n");
33 $self->log->info("\tadjusted_somatic_profile_path: " . $self->adjusted_somatic_profile_path . "\n");
34
35 $opts{yaml} = $self->original_somatic_profile_path;
36 $opts{output_dir} = $self->output_dir;
37 $opts{output_yaml} = "adjusted_somatic_profile";
38  
39 my $config = YAML::LoadFile($opts{yaml});
40  
41 my $total = {};
42  
43 # calculate total data per chromosome, per phase
44 foreach my $chr (keys $config) {
45 my @phases = ('a');
46 if ("X" ne $chr and "Y" ne $chr) { push(@phases, 'b'); }
47  
48 foreach my $phase (@phases) {
49 my ($cellularPrevalence_hash, $copyNumber_hash) = $self->_determineProfiles($config->{$chr}->{root}, $phase, {'' => 1}, '', {});
50  
51 $total->{cellularPrevalence}->{$chr}->{$phase} = $cellularPrevalence_hash;
52 $total->{copyNumber}->{$chr}->{$phase} = $copyNumber_hash;
53 $total->{total_copies}->{$chr}->{$phase} = 1;
54  
55 my $total_copies = 0;
56 foreach my $name (keys $total->{cellularPrevalence}->{$chr}->{$phase}) {
57 $total_copies = $total_copies + $total->{cellularPrevalence}->{$chr}->{$phase}->{$name} * $total->{copyNumber}->{$chr}->{$phase}->{$name};
58 }
59 $total->{total_copies}->{$chr}->{$phase} = $total_copies;
60 if ($total_copies >= $max_total_copies) {
61 $max_total_copies = $total_copies;
62 }
63 }
64 }
65
66 my $new_config = $config;
67 # assign new cellularPrevalence to tree structure, taking into account CNAs
68 foreach my $chr (keys $config) {
69 my @phases = ('a');
70 if ("X" ne $chr and "Y" ne $chr) { push(@phases, 'b'); }
71 foreach my $phase (@phases) {
72 $self->_designatePortions($new_config->{$chr}->{root}, $total, $chr, $phase, $max_total_copies, '');
73 $self->_getReads($new_config->{$chr}->{root}, $total, $chr, $phase, '', 1);
74 }
75 }
76
77 DumpFile("$opts{output_dir}/$opts{output_yaml}.yaml", $new_config);
78  
79 # print leaf files
80 foreach my $chr (keys $total->{cellularPrevalence}) {
81 foreach my $phase (keys $total->{cellularPrevalence}->{$chr}) {
82 my $cellularPrevalence_hash = $total->{cellularPrevalence}->{$chr}->{$phase};
83 my $copyNumber_hash = $total->{copyNumber}->{$chr}->{$phase};
84 my $total_copies_phase = $total->{total_copies}->{$chr}->{$phase};
85  
86 # #printing to leaf text files
87 (my $bampath = $config->{$chr}->{"input_".$phase}) =~ s/\.[^.]+$//;
88  
89 my $leaf_out = "$opts{output_dir}/leaf_files_phase_" . $phase . "_$chr.txt";
90  
91 (open my $leaf_fh, ">", $leaf_out) || die "unable to open file: $leaf_out";
92 print($leaf_fh $total_copies_phase / $max_total_copies, "\n");
93 close($leaf_fh);
94 } # foreach phase
95 } # foreach chr
96
97 DumpFile("$opts{output_dir}/$opts{output_yaml}.total.txt", $total);
98  
99 # create matrix
100 my $skip = 0;
101 open(my $fh, '>', "$opts{output_dir}/$opts{output_yaml}.matrix.csv");
102 }
103  
104 sub pick_somatic_mutations {
105 my $self = shift;
106 my $picktrinucleotides = shift;
107  
108 $self->log->info("pick_somatic_mutations\n");
109 $self->log->info("\tsomatic_profile: " . "$self->somatic_profile" . "\n");
110  
111 my $phase_a_stages = [];
112 my $phase_b_stages = [];
113  
114 # For every chromosome, for every mutation type in the profile, for every subclone create a file of randomly chosen mutations for that subclone and all of it's children to contain
115 foreach my $chr (@{$self->chromosomes}) {
116 my $top = $self->somatic_profile->{$chr};
117  
118 my $phase0 = basename($top->{input_a});
119 $phase0 =~ s/.bam$//;
120 my $phase1 = basename($top->{input_b});
121 $phase1 =~ s/.bam$//;
122  
123 $self->_pick_traversal(
124 root => $top->{root}->{children},
125 stem => [$phase0],
126 phase => "a",
127 stages => $phase_a_stages,
128 chr => $chr,
129 parent_muts => {
130 snv => 0,
131 indel => 0,
132 sv => 0
133 },
134 cat_commands => {
135 snv => "",
136 indel => "",
137 sv => ""
138 },
139 picktrinucleotides => $picktrinucleotides
140 );
141  
142 if ($chr ne 'X' and $chr ne 'Y') {
143 $self->_pick_traversal(
144 root => $top->{root}->{children},
145 stem => [$phase1],
146 phase => "b",
147 stages => $phase_b_stages,
148 chr => $chr,
149 parent_muts => {
150 snv => 0,
151 indel => 0,
152 sv => 0
153 },
154 cat_commands => {
155 snv => "",
156 indel => "",
157 sv => ""
158 },
159 picktrinucleotides => $picktrinucleotides
160 );
161 }
162 }
163 return [@{$phase_a_stages}, @{$phase_b_stages}];
164 }
165  
166 sub split_subclones {
167 my $self = shift;
168  
169 my $phase_a_stages = [];
170 my $phase_b_stages = [];
171  
172 foreach my $chr (@{$self->chromosomes}) {
173 my $top = $self->somatic_profile->{$chr};
174  
175 if (! -e $self->output_dir . "/chr$chr") {
176 make_path($self->output_dir . "/chr$chr") or die "Making " . $self->output_dir . "/chr$chr failed: $!";
177 }
178
179 my $cp_phase_0;
180 if (! -e $self->output_dir . "/chr$chr/phase.0.T.bam") {
181 $cp_phase_0 = $self->HPCI_group->stage(
182 name => 'bs_cp_p0_' . $chr,
183 command => "cp " . $self->wd . "/chrbam/chr$chr/phase.0/phase.0.T.bam " . $self->output_dir . "/chr$chr/phase.0.T.bam;",
184 resources_required => {
185 h_vmem => '4G'
186 },
187 extra_sge_args_string => " -q transient "
188 );
189 push(@{$phase_a_stages}, $cp_phase_0);
190 }
191  
192 my $cp_phase_1 = undef;
193 if ($chr ne 'X' and $chr ne 'Y') {
194 if (! -e $self->output_dir . "/chr$chr/phase.1.T.bam") {
195 $cp_phase_1 = $self->HPCI_group->stage(
196 name => 'bs_cp_p1_' . $chr,
197 command => "cp " . $self->wd . "/chrbam/chr$chr/phase.1/phase.1.T.bam " . $self->output_dir . "/chr$chr/phase.1.T.bam;",
198 resources_required => {
199 h_vmem => '4G'
200 },
201 extra_sge_args_string => " -q transient "
202 );
203 push(@{$phase_b_stages}, $cp_phase_1);
204 }
205 }
206  
207 my $phase0 = basename($top->{input_a});
208 $phase0 =~ s/.bam$//;
209 my $phase1 = basename($top->{input_b});
210 $phase1 =~ s/.bam$//;
211  
212 my $split_phase_0 = $self->_split_multiple(
213 root => $top->{root},
214 stem => $phase0,
215 phase => "a",
216 deps => [ $cp_phase_0 ],
217 chr => $chr
218 );
219 push(@{$phase_a_stages}, $split_phase_0);
220  
221 if ($chr ne 'X' and $chr ne 'Y') {
222 my $split_phase_1 = $self->_split_multiple(
223 root => $top->{root},
224 stem => $phase1,
225 phase => "b",
226 deps => [ $cp_phase_1 ],
227 chr => $chr
228 );
229 push(@{$phase_b_stages}, $split_phase_1);
230 }
231 }
232 return [@{$phase_a_stages}, @{$phase_b_stages}];
233 }
234  
235 sub somatic_simulation {
236 my $self = shift;
237  
238 $self->log->info("somatic_simulation\n");
239 $self->log->info("\tsomatic_profile: " . "$self->somatic_profile" . "\n");
240 $self->log->info("\tbam: " . $self->bam . "\n");
241 $self->log->info("\treference: " . $self->config->{reference} . "\n");
242 $self->log->info("\tsex: " . $self->sex . "\n");
243  
244 my $stages = [];
245
246 # For every chromosome call copybam2dir from module
247 # After all copies are completed, call bam_simulator in module
248 foreach my $chr (@{$self->chromosomes}) {
249 while (my ($leaf, $attr) = each(%{$self->leaves->{$chr}})) {
250 $leaf =~ s/^([^_]*)_//;
251 my $current_phase = $1;
252 my $pre_req;
253 while (my ($mutation_type, $cat_command) = each(%{$attr->{cats}})) {
254 my $h_vmem;
255  
256 my $sort_before_mut_stage = $self->_sortFile(
257 jobname => "mut_$chr\_sort_before_" . $mutation_type . "_" . $leaf,
258 filename => $self->output_dir . "/chr$chr/$leaf",
259 pre_req => $pre_req,
260 sortByRead => 0
261 );
262 $pre_req = $sort_before_mut_stage;
263  
264 $cat_command .= "> " . $self->output_dir . "/pick_$chr\_$current_phase\_$leaf\_final\_$mutation_type.txt;";
265
266 my $command_line_args = " -v pick_$chr\_$current_phase\_$leaf\_final\_$mutation_type.txt -f " . $self->output_dir . "/chr$chr/$leaf.bam -r " . $self->config->{reference} . " -o " . $self->output_dir . "/chr$chr/$leaf.bam_temp -n $attr->{muts}->{$mutation_type} -p 4 --tmpdir chr$chr\_$leaf\_$mutation_type";
267
268 if ($mutation_type eq "snv") {
269 $command_line_args .= " --mindepth 10 --ignoresnps";
270 $h_vmem = "12G";
271 }
272 if ($mutation_type eq "indel") {
273 $command_line_args .= ' --mindepth 10 --picardjar $PICARDROOT/picard.jar --aligner mem';
274 $h_vmem = "20G";
275 }
276 if ($mutation_type eq "sv") {
277 my $nix = $self->config->{reference};
278 $nix =~ s/fa$/nix/;
279 $command_line_args .= " --aligner novoalign --alignopts novoref:" . $nix;
280 $h_vmem = "45G";
281 }
282  
283 # if mutations are spiked in correctly, a temp file will be created
284 # otherwise -> NO MUTATIONS
285 my $postmut_command =
286 " if [ -s " . $self->output_dir . "/chr$chr/$leaf.bam_temp ]; then mv " .
287 $self->output_dir . "/chr$chr/$leaf.bam_temp " . $self->output_dir . "/chr$chr/$leaf.bam; fi";
288 my $mut_stage = $self->HPCI_group->stage(
289 name => "mut_$chr\_" . $mutation_type . "_" . $leaf,
290 command => "$cat_command cd ". $self->output_dir ."; add" . $mutation_type . ".py " . $command_line_args . "; $postmut_command",
291 resources_required => {
292 h_vmem => $h_vmem
293 },
294 retry_resources_required => {
295 h_vmem => ['6G', '12G', '24G', '32G', '45G', '60G']
296 },
297 modules_to_load => ['Python-BL', 'bamsurgeon/0.1.0-local', 'picard/1.130', 'bwa/0.7.12', 'novocraft/3.02.12', 'samtools/1.2', 'velvet/1.0.13', 'exonerate/2.2.0'],
298 extra_sge_args_string => " -q transient "
299 );
300  
301 $self->HPCI_group->add_deps(
302 pre_req => $pre_req,
303 deps => $mut_stage
304 ) if $pre_req;
305 $pre_req = $mut_stage;
306  
307 push(@{$stages}, $sort_before_mut_stage);
308 push(@{$stages}, $mut_stage);
309 }
310 }
311 }
312 return $stages;
313 }
314  
315 sub extract_leafs {
316 my $self = shift;
317
318 $self->log->info("extract_leafs\n");
319 my $stages = [];
320  
321 foreach my $chr (@{$self->chromosomes}) {
322 while (my ($leaf, $attr) = each(%{$self->leaves->{$chr}})) {
323 $leaf =~ s/^([^_]*)_//;
324 my $current_phase = $1;
325 # generated in adjust_yaml_for_cna
326 my $leaf_file = $self->output_dir . "/leaf_files_phase_$current_phase\_$chr.txt";
327 next unless -e $leaf_file;
328  
329 open(my $fh, '<', $leaf_file);
330 my $prop = <$fh>;
331 chomp($prop);
332 my $other_prop = 1 - $prop;
333 $self->log->info("prop: $prop\n");
334 $self->log->info("other prop: $other_prop\n");
335 my $wd = $self->output_dir . "/chr$chr";
336  
337 my $sortcheck = "samtools index $wd/$leaf.bam 2> $wd/$leaf.bam.log; if [ -s $wd/$leaf.bam.log ];";
338 my $extract_command =
339 "$sortcheck then samtools sort $wd/$leaf.bam $wd/$leaf.sorted; mv $wd/$leaf.sorted.bam $wd/$leaf.bam; fi; " .
340 "bamsplit_multiple.py " .
341 "$wd/$leaf.bam $wd $leaf.bam_extracted,$leaf.bam_delete_me $prop,$other_prop; rm -f $wd/$leaf.bam_delete_me";
342  
343 my $get_reads_command =
344 "samtools view $wd/$leaf.bam | grep -oP \"^[^\\t]*|RG:[^\\t]*\" | awk '{f=\$0; getline; print \$0, f}' | sort -u > $wd/$leaf.bam.reads; " .
345 "samtools view $wd/$leaf.bam_extracted | grep -oP \"^[^\\t]*|RG:[^\\t]*\" | awk '{f=\$0; getline; print \$0, f}' | sort -u > $wd/$leaf.bam_extracted.reads";
346  
347 my $extract_stage = $self->HPCI_group->stage(
348 name => "extract_leaf_$chr\_" . $leaf,
349 command => $extract_command,
350 resources_required => {
351 h_vmem => "16G"
352 },
353 retry_resources_required => {
354 h_vmem => ['6G', '12G', '24G', '32G', '45G', '60G']
355 },
356 modules_to_load => [ 'samtools/1.2', 'Python-BL', 'bamsurgeon/0.1.0-local' ],
357 extra_sge_args_string => " -q transient "
358 );
359  
360 my $get_reads_stage = $self->HPCI_group->stage(
361 name => "get_reads_$chr\_" . $leaf,
362 command => $get_reads_command,
363 resources_required => {
364 h_vmem => "4G"
365 },
366 retry_resources_required => {
367 h_vmem => ['6G', '12G', '24G', '32G', '45G', '60G']
368 },
369 modules_to_load => [ 'samtools/1.2' ],
370 extra_sge_args_string => " -q transient "
371 );
372  
373 $self->HPCI_group->add_deps(
374 pre_reqs => $extract_stage,
375 deps => $get_reads_stage
376 );
377  
378 push(@{$stages}, $extract_stage);
379 }
380 }
381 return $stages;
382 }
383  
384 sub _pick_traversal {
385 my $self = shift;
386 my %args = (
387 root => undef,
388 stem => '',
389 phase => '',
390 stages => [],
391 chr => '',
392 parent_muts => {
393 snv => 0,
394 indel => 0,
395 sv => 0
396 },
397 cat_commands => {
398 snv => "",
399 indel => "",
400 sv => ""
401 },
402 picktrinucleotides => 0,
403 @_
404 );
405  
406 my $root = $args{root};
407 my $current_clone = 0;
408  
409 if (scalar(@{$root}) == 2) {
410 #Use bamsplit the two subclones for both phases and traverse the two clones
411 my $split_deps = [];
412 my $sub0_stem = [];
413 my $sub1_stem = [];
414 foreach my $stem (@{$args{stem}}) {
415 push(@{$sub0_stem}, $stem . "_c" . $current_clone);
416 push(@{$sub1_stem}, $stem . "_c" . ($current_clone+1));
417 }
418  
419 # Handle mutations for subclone 0
420 $self->_mutation_generation(
421 child => $root->[0],
422 stem => $sub0_stem,
423 phase => $args{phase},
424 stages => $args{stages},
425 chr => $args{chr},
426 parent_muts => $args{parent_muts},
427 cat_commands => $args{cat_commands},
428 picktrinucleotides => $args{picktrinucleotides}
429 );
430  
431 # Handle mutations for subclone 1
432 $self->_mutation_generation(
433 child => $root->[1],
434 stem => $sub1_stem,
435 phase => $args{phase},
436 stages => $args{stages},
437 chr => $args{chr},
438 parent_muts => $args{parent_muts},
439 cat_commands => $args{cat_commands},
440 picktrinucleotides => $args{picktrinucleotides}
441 );
442 }
443 else {
444 # BAMs are split sequentially for children > 2
445 my $percent_sum = 0;
446 my $pre_req = $args{deps};
447  
448 foreach my $child (@{$root}) {
449 my $sub_stem = [];
450 foreach my $stem (@{$args{stem}}) {
451 push(@{$sub_stem}, $stem . "_c" . $current_clone);
452 }
453  
454 $self->_mutation_generation(
455 child => $child,
456 stem => $sub_stem,
457 phase => $args{phase},
458 stages => $args{stages},
459 chr => $args{chr},
460 parent_muts => $args{parent_muts},
461 cat_commands => $args{cat_commands},
462 picktrinucleotides => $args{picktrinucleotides}
463 );
464 $current_clone++;
465 }
466 }
467 }
468  
469 sub _mutation_generation {
470 my $self = shift;
471 my %args = (
472 child => undef,
473 stem => [],
474 phase => '',
475 stages => [],
476 chr => '',
477 parent_muts => {
478 snv => 0,
479 indel => 0,
480 sv => 0
481 },
482 cat_commands => {
483 snv => "",
484 indel => "",
485 sv => ""
486 },
487 picktrinucleotides => 0,
488 @_
489 );
490 my $child = $args{child};
491 my $current_phase = $args{phase};
492 my %muts = %{$args{parent_muts}};
493 my %cats = %{$args{cat_commands}};
494  
495 if ((defined($child->{mut_arg})) && (@{$args{stem}} > 0)) {
496 my $mutations = each_array(@{$child->{mut_arg}}, @{$child->{mut_type}}, @{$child->{mut_indx}});
497 while (my ($arg, $type, $indx) = $mutations->()) {
498 if (($type =~ m/(.*)_$current_phase$/) && ($1 ne "wce")) {
499 my $full_command;
500 if ('snv' eq $1 and 1 eq $args{picktrinucleotides}) {
501 $full_command = "perl " . "$Bin/generate_signature.pl " .
502 "--c " . $self->config_path . " " .
503 "--chromosome $args{chr} " .
504 "--mf " . $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt " .
505 "--n $arg " .
506 "--cb_bed " . $self->wd . "/cb_bed/$args{chr}\_collapsed.bed " .
507 "--reference " . $self->wd . "/chrFa/$args{chr}.fa " .
508 "--seed " . ($self->seed // 12345);
509 }
510 else {
511 my $pick_command = 'randomsites.py ' .
512 "-g " . $self->wd . "/chrFa/$args{chr}.fa " .
513 "-n $arg " .
514 "--avoidN " .
515 "--minvaf " . ($self->minvaf // 1) . " " .
516 "--maxvaf " . ($self->maxvaf // 1) . " " .
517 "--vafbeta1 " . ($self->vafbeta1 // 2) . " " .
518 "--vafbeta2 " . ($self->vafbeta2 // 2) . " " .
519 "'$1' ";
520
521 $pick_command .= " --minlen " . $self->mut_minlens->{$1} if defined $self->mut_minlens->{$1};
522 $pick_command .= " --maxlen " . $self->mut_maxlens->{$1} if defined $self->mut_maxlens->{$1};
523
524 $pick_command .= " > " . $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt";
525
526 # RND sites require insertion library
527 my $filter_command = ' grep -v RND ' . $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt";
528
529 # for indel and sv only
530 if (defined $self->mut_types->{$1}) {
531 my $types = join('|', (split(',', $self->mut_types->{$1})));
532 $filter_command .= " | egrep \'$types\'";
533 }
534
535 $filter_command .= " | head -n $arg > " . $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt_temp; mv " . $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt_temp " . $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt";
536
537 $full_command = join(';', $pick_command, $filter_command);
538 }
539
540 if (! -e $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt" or -s $self->output_dir . "/pick_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1.txt" <= 1) {
541 my $pick_mut_stage = $self->HPCI_group->stage(
542 name => "bs_pick_somatic_$args{chr}\_$current_phase\_$args{stem}->[$indx]\_$1",
543 command => $full_command,
544 resources_required => {
545 h_vmem => '2G'
546 },
547 modules_to_load => ['Python-BL', 'bamsurgeon/0.1.0-local'],
548 extra_sge_args_string => " -q transient "
549 );
550 push(@{$args{stages}}, $pick_mut_stage);
551 }
552 $muts{$1} += $arg;
553 $cats{$1} .= "$args{stem}->[$indx] ";
554 }
555 }
556  
557 # currently applies all gain AFTER mutations applied to node
558 map {
559 if ($child->{mut_type}->[$_] eq "wce_" . $current_phase) {
560 push(@{$args{stem}}, $args{stem}->[$child->{mut_indx}->[$_]]);
561 # Label both the old index of interest and the new element to have _n0 and _n1 to signify that they are chromosome gains
562 $args{stem}->[$child->{mut_indx}->[$_]] .= "_n0";
563 $args{stem}->[-1] .= "_n1";
564 }
565 } grep {
566 $child->{mut_arg}->[$_] eq 'gain';
567 } 0 .. $#{$child->{mut_arg}};
568  
569  
570 map {
571 if ($child->{mut_type}->[$_] eq "wce_" . $current_phase) {
572 splice @{$args{stem}}, $child->{mut_indx}->[$_], 1;
573 }
574 } grep {
575 $child->{mut_arg}->[$_] eq 'del';
576 } 0 .. $#{$child->{mut_arg}};
577  
578 }
579  
580 # If the current subclone has children, continue descending
581 if (ref($child->{children}) eq 'ARRAY') {
582 $self->_pick_traversal(
583 root => $child->{children},
584 stem => $args{stem},
585 phase => $args{phase},
586 stages => $args{stages},
587 chr => $args{chr},
588 parent_muts => \%muts,
589 cat_commands => \%cats,
590 picktrinucleotides => $args{picktrinucleotides}
591 );
592 }
593 else {
594 foreach my $leaf (@{$args{stem}}) {
595 my %temp_cats = %cats;
596 while (my ($mutation_type, $cat_command) = each %temp_cats) {
597 if (defined $muts{$mutation_type} and $muts{$mutation_type} == 0) {
598 delete($muts{$mutation_type});
599 delete($temp_cats{$mutation_type});
600 }
601 else {
602 my @stems_of_interest = split(" ", $temp_cats{$mutation_type});
603 $temp_cats{$mutation_type} = "cat ";
604 foreach my $stem_of_interest (@stems_of_interest) {
605 if (($current_phase . $leaf) =~ m/$stem_of_interest/) {
606 $temp_cats{$mutation_type} .= $self->output_dir . "/pick_$args{chr}\_$current_phase\_$stem_of_interest\_$mutation_type.txt ";
607 }
608 }
609 }
610 }
611 $self->leaves->{$args{chr}}->{"$current_phase\_$leaf"}->{muts} = \%muts;
612 $self->leaves->{$args{chr}}->{"$current_phase\_$leaf"}->{cats} = \%temp_cats;
613 }
614 }
615 }
616  
617 sub _split_multiple {
618 my $self = shift;
619
620 my %args = (
621 root => undef,
622 stem => '',
623 deps => [],
624 phase => '',
625 chr => '',
626 @_
627 );
628  
629 my $root = $args{root};
630 my $reads = $self->_getReads($root, {}, $args{chr}, $args{phase}, '', 1);
631 my (@phase_bams, @outbams, @percentages);
632  
633 while (my ($leaf, $attr) = each(%{$self->leaves->{$args{chr}}})) {
634 $leaf =~ s/^([^_]*)_//;
635 if ($1 eq $args{phase}) {
636 push @phase_bams, $leaf;
637 }
638 }
639  
640 while (my ($node, $attr) = each(%{$reads->{$args{chr}}->{$args{phase}}})) {
641 $node =~ s/^_//g;
642 my @c = split('_', $node);
643 my $regex = join('[_n\d]*', @c, '');
644 $regex = basename($args{stem}) . "_$regex" . '$';
645  
646 my @bams_for_split;
647 foreach my $leaf (@phase_bams) {
648 if ($leaf =~ /$regex/) {
649 push @bams_for_split, $leaf;
650 }
651 }
652  
653 foreach my $leaf (@bams_for_split) {
654 push @outbams, "$leaf.bam";
655 push @percentages, $attr / scalar(@bams_for_split);
656 }
657 }
658  
659 my $sort_before_split_stage = $self->_sortFile(
660 jobname => "bs_$args{chr}\_sort_before_split_" . basename($args{stem}),
661 filename => $self->output_dir . "/chr$args{chr}/" . $args{stem},
662 pre_req => $args{deps},
663 sortByRead => 0
664 );
665  
666 # split into multiple BAMs, then delete original copied BAM
667 my $remove_cp = "rm -f " . $self->output_dir . "/chr$args{chr}/" . $args{stem} . ".bam ";
668 my $split_stage = $self->HPCI_group->stage(
669 name => "bs_$args{chr}\_split_" . basename($args{stem}),
670 command => "bamsplit_multiple.py " . $self->output_dir . "/chr$args{chr}/" . $args{stem} . ".bam " . $self->output_dir . "/chr$args{chr}/ " . join(',', @outbams) . " " . join(',', @percentages) . " ; $remove_cp ",
671 resources_required => {
672 h_vmem => "16G"
673 },
674 retry_resources_required => {
675 h_vmem => ['8G', '12G', '24G', '32G', '45G', '60G']
676 },
677 modules_to_load => ['Python-BL', 'bamsurgeon/0.1.0-local'],
678 extra_sge_args_string => " -q transient "
679 );
680  
681 $self->HPCI_group->add_deps(
682 pre_req => $sort_before_split_stage,
683 deps => $split_stage
684 );
685  
686 return($split_stage)
687 }
688  
689 sub _sortFile {
690 my $self = shift;
691 my %args = (
692 jobname => undef,
693 filename => undef,
694 pre_req => undef,
695 sortByRead => 0,
696 @_
697 );
698
699 # default sort by coordinate
700 my $sortcheck = "samtools index $args{filename}.bam 2> $args{filename}.bam.log; if [ -s $args{filename}.bam.log ];";
701 my $samtools = "samtools sort";
702 # if sorting by read ID
703 if (1 eq $args{sortByRead}) {
704 $samtools .= " -n";
705 }
706
707 my $sort_stage = $self->HPCI_group->stage(
708 name => $args{jobname},
709 command => "cd " . $self->output_dir . "; $sortcheck then $samtools $args{filename}.bam $args{filename}.sorted; mv $args{filename}.sorted.bam $args{filename}.bam; samtools index $args{filename}.bam; fi",
710 resources_required => {
711 h_vmem => "4G"
712 },
713 retry_resources_required => {
714 h_vmem => ['6G', '12G', '24G', '32G', '45G', '60G']
715 },
716 modules_to_load => ['samtools/1.2'],
717 extra_sge_args_string => " -q transient ",
718 );
719
720 $self->HPCI_group->add_deps(
721 pre_req => $args{pre_req},
722 deps => $sort_stage
723 ) if $args{pre_req};
724  
725 return($sort_stage)
726 }
727  
728 sub _determineProfiles {
729 my $self = shift;
730 # TYPES: hash, string, hashref, string, hashref
731 # $phase = 'a' or 'b'
732 # $child = '_c0', '_c1', etc.
733 my ($config_hash, $phase, $cellularPrevalence_ref, $child, $copyNumber_ref) = @_;
734  
735 # multiply all cellularPrevalence_ref as indicated on yaml file
736 my $percent = $config_hash->{percent} * 1; # convert percent string to decimal
737 my $new_cellularPrevalence = {};
738 my $levels; # counts number of tree levels for this node
739  
740 while (my ($name, $prop) = each %$cellularPrevalence_ref ) {
741 # e.g. parent = _c0; child = _c0_c1
742 my $parent = $name;
743 $name = $name . $child; # new child name
744 $prop = $prop * $percent; # associated proportion
745 $new_cellularPrevalence->{$name} = $prop;
746  
747 $levels = ($name =~ tr/c//);
748
749 if (defined $copyNumber_ref->{$parent}) {
750 $copyNumber_ref->{$name} = $copyNumber_ref->{$parent};
751 }
752 else { # base case when passing in root node
753 $copyNumber_ref->{$name} = 1;
754 }
755 }
756 $cellularPrevalence_ref = $new_cellularPrevalence;
757  
758 # check for any chromosome phase gains or deletions
759 if (defined $config_hash->{mut_arg} and defined $config_hash->{mut_type}) {
760 for my $idx (0 .. scalar(@{$config_hash->{mut_arg}})-1) {
761 # check if applicable to current phase
762 if (@{$config_hash->{mut_type}}[$idx] eq "wce_".$phase) {
763 # check for del or gain
764 if (@{$config_hash->{mut_arg}}[$idx] eq "del") {
765 #print "Deleting phase " .$phase. "\n";
766  
767 foreach my $key (keys $cellularPrevalence_ref) {
768 $copyNumber_ref->{$key}--;
769 if (0 eq $copyNumber_ref->{$key}) {
770 delete $cellularPrevalence_ref->{$key};
771 }
772 }
773  
774 my $sum = 0;
775 foreach my $key (keys $copyNumber_ref) {
776 $sum = $sum + $copyNumber_ref->{$key};
777 }
778  
779 # if this chromosome phase has no more total_copies
780 if (0 == $sum) { return; }
781 }
782 else {
783 #print "Gaining phase " .$phase. "\n";
784 foreach my $key (keys $cellularPrevalence_ref) {
785 $copyNumber_ref->{$key}++;
786 }
787 }
788 }
789 }
790 }
791  
792 # if at leaf node
793 if (! defined $config_hash->{children} ) {
794 return ($cellularPrevalence_ref, $copyNumber_ref);
795 }
796 else {
797 my $new_cellularPrevalence = {};
798 my $new_copyNumber_ref;
799 my $child_num = 0;
800
801 foreach my $config (@{$config_hash->{children}}) {
802 # create copy for each child
803 my %child_cellularPrevalence = %$cellularPrevalence_ref;
804 my $add_cellularPrevalence;
805 ($add_cellularPrevalence, $new_copyNumber_ref) = $self->_determineProfiles($config, $phase, \%child_cellularPrevalence, "_c".$child_num, $copyNumber_ref);
806  
807
808 # child phases not completely deleted
809 if (defined $add_cellularPrevalence) {
810 # merge hash
811 $new_cellularPrevalence = {%$add_cellularPrevalence, %$new_cellularPrevalence};
812 $copyNumber_ref = $new_copyNumber_ref;
813 }
814 $child_num = $child_num + 1;
815 }
816 return ($new_cellularPrevalence, $new_copyNumber_ref);
817 }
818 } # _determineProfiles
819  
820  
821 sub _designatePortions {
822 my $self = shift;
823 # HR = hash ref
824 my ($config_HR, $total_HR, $chr, $phase, $max_total_copies, $child) = @_;
825  
826 # if at leaf node
827 if (! defined $config_HR->{children} ) {
828 #print "AT LEAF NODE -- child $child\n";
829 # if at root node (i.e. entire chromosome is deleted) -> should just remove entirely...
830 if ('' eq $child) { return; }
831 # for complete phase deletions
832 if (0 eq $total_HR->{total_copies}->{$chr}->{$phase}) { return 0; }
833 if (! defined $total_HR->{copyNumber}->{$chr}->{$phase}->{$child}) { return 0; }
834 if (! defined $total_HR->{cellularPrevalence}->{$chr}->{$phase}->{$child}) { return 0; }
835  
836 my $quantity = $total_HR->{cellularPrevalence}->{$chr}->{$phase}->{$child} * $total_HR->{copyNumber}->{$chr}->{$phase}->{$child} / $total_HR->{total_copies}->{$chr}->{$phase};
837  
838 return ($quantity);
839 }
840
841 else {
842 my $num = 0;
843 my $quantity_sum = 0;
844 my $quantities = {};
845 my $configs = {};
846 my @children = (); # used to keep track of children in the order they appear on config file
847  
848 foreach my $config (@{$config_HR->{children}}) {
849 push @children, "$child\_c$num";
850 $configs->{"$child\_c$num"} = $config;
851 $quantities->{"$child\_c$num"} = $self->_designatePortions($config, $total_HR, $chr, $phase, $max_total_copies, "$child\_c$num");
852  
853 $quantity_sum = $quantity_sum + $quantities->{"$child\_c$num"};
854 $num = $num + 1;
855 }
856  
857 #$self->subclone_proportions->{$chr}->{$phase} = { %{$self->subclone_proportions->{$chr}->{$phase}}, %{$quantities} };
858  
859 # reconstruct child config
860 $config_HR->{children} = ();
861 foreach my $child (@children) {
862 if (0 eq $quantity_sum) {
863 $configs->{$child}->{"percent_$phase"} = 0;
864 }
865 else {
866 $configs->{$child}->{"percent_$phase"} = $quantities->{$child} / $quantity_sum;
867 }
868 push @{$config_HR->{children}}, $configs->{$child};
869 }
870  
871 return ($quantity_sum);
872 }
873 } # _designatePortions
874  
875  
876 sub _getReads {
877 my $self = shift;
878 my ($config_HR, $total_HR, $chr, $phase, $child, $parent_percent) = @_;
879  
880 # if at leaf node
881 if (! defined $config_HR->{children}) {
882 if ($child eq '') {
883 $total_HR->{reads}->{$chr}->{$phase}->{$child} = $config_HR->{percent};
884 }
885 else {
886 $total_HR->{reads}->{$chr}->{$phase}->{$child} = $parent_percent * $config_HR->{"percent_$phase"};
887 }
888 }
889 else {
890 my $num = 0;
891 foreach my $config (@{$config_HR->{children}}) {
892 if ($child eq '') {
893 $self->_getReads($config, $total_HR, $chr, $phase, "$child\_c$num", $config_HR->{"percent"});
894 }
895 else {
896 $self->_getReads($config, $total_HR, $chr, $phase, "$child\_c$num", $parent_percent*$config_HR->{"percent_$phase"});
897 }
898 $num = $num + 1;
899 }
900 }
901
902 return($total_HR->{reads});
903 } # _getReads
904  
905 1;