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

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 186 CC 9 SCAR 21.9
CC
9
Cov
0%
merge_to_phases
20.0
merge_vcf
20.0
merge_to_chromosomes
6.0
CRAP 9.0
1 package NGS::Tools::BAMSurgeon::Merge;
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);
12 use HPCI;
13 use Cwd qw(abs_path);
14  
15 sub merge_to_phases {
16 my $self = shift;
17  
18 $self->log->info("merge_phases\n");
19 $self->log->info("\tbam: " . $self->bam . "\n");
20 $self->log->info("\tsex: " . $self->sex . "\n");
21  
22 my $stages = [];
23  
24 foreach my $chr (@{$self->chromosomes}) {
25  
26 my $phase_a_command = 'java -Xmx14g -jar $PICARDROOT/picard.jar MergeSamFiles USE_THREADING=true VALIDATION_STRINGENCY=LENIENT ';
27 my $phase_b_command = 'java -Xmx14g -jar $PICARDROOT/picard.jar MergeSamFiles USE_THREADING=true VALIDATION_STRINGENCY=LENIENT ';
28  
29 foreach my $leaf (keys %{$self->leaves->{$chr}}) {
30 $leaf =~ s/^([^_]*)_//;
31 my $current_phase = $1;
32 if ($current_phase eq 'a') {
33 $phase_a_command .= "INPUT=" . $self->output_dir . "/chr$chr/$leaf.bam_extracted ";
34 }
35 elsif ($current_phase eq 'b') {
36 $phase_b_command .= "INPUT=" . $self->output_dir . "/chr$chr/$leaf.bam_extracted ";
37 }
38 }
39 make_path($self->output_dir . "/chr$chr/output/");
40 $phase_a_command .= "OUTPUT=" . $self->output_dir . "/chr$chr/output/phase.0.T.bam; samtools index " . $self->output_dir . "/chr$chr/output/phase.0.T.bam; ";
41 $phase_b_command .= "OUTPUT=" . $self->output_dir . "/chr$chr/output/phase.1.T.bam; samtools index " . $self->output_dir . "/chr$chr/output/phase.1.T.bam; ";
42  
43 my $merge_phase_0_stage = $self->HPCI_group->stage(
44 name => "bs_merge_phase_0_$chr",
45 command => $phase_a_command,
46 resources_required => {
47 h_vmem => '18G'
48 },
49 modules_to_load => [ 'picard/1.130', 'samtools/1.2' ],
50 extra_sge_args_string => " -q transient "
51 );
52 push(@{$stages}, $merge_phase_0_stage);
53 if ('X' ne $chr and 'Y' ne $chr) {
54 my $merge_phase_1_stage = $self->HPCI_group->stage(
55 name => "bs_merge_phase_1_$chr",
56 command => $phase_b_command,
57 resources_required => {
58 h_vmem => '18G'
59 },
60 modules_to_load => [ 'picard/1.130', 'samtools/1.2' ],
61 extra_sge_args_string => " -q transient "
62 );
63 push(@{$stages}, $merge_phase_1_stage);
64 }
65 }
66 return $stages;
67 }
68  
69 sub merge_to_chromosomes {
70 my $self = shift;
71  
72 $self->log->info("merge_chromosomes\n");
73 $self->log->info("\tbam: " . $self->bam . "\n");
74 $self->log->info("\tsex: " . $self->sex . "\n");
75  
76 my $stages = [];
77  
78 foreach my $chr (@{$self->chromosomes}) {
79 my $inputs = "INPUT=" . $self->output_dir . "/chr$chr/output/phase.0.T.bam ";
80 if ('X' ne $chr and 'Y' ne $chr) {
81 $inputs .= "INPUT=" . $self->output_dir . "/chr$chr/output/phase.1.T.bam ";
82 }
83  
84 my $merge_command = 'java -Xmx10g -jar $PICARDROOT/picard.jar MergeSamFiles USE_THREADING=true VALIDATION_STRINGENCY=LENIENT '.
85 $inputs .
86 "OUTPUT=" . $self->output_dir . "/chr$chr/output/T.bam; samtools index " . $self->output_dir . "/chr$chr/output/T.bam; ";
87 $merge_command .= "rm -f " . $self->output_dir . "/chr$chr/output/phase.*.T.bam*; ";
88  
89 my $merge_stage = $self->HPCI_group->stage(
90 name => "bs_merge_chr_$chr",
91 command => $merge_command,
92 resources_required => {
93 h_vmem => '16G'
94 },
95 modules_to_load => [ 'picard/1.130', 'samtools/1.2' ],
96 extra_sge_args_string => " -q transient "
97 );
98 push(@{$stages}, $merge_stage);
99 }
100 return($stages);
101 }
102  
103 sub merge_to_final {
104 my $self = shift;
105  
106 $self->log->info("merge_chromosomes\n");
107 $self->log->info("\tbam: " . $self->bam . "\n");
108 $self->log->info("\tsex: " . $self->sex . "\n");
109  
110 my $stages = [];
111  
112 my $command = 'java -Xmx16g -jar $PICARDROOT/picard.jar MergeSamFiles USE_THREADING=true VALIDATION_STRINGENCY=LENIENT ';
113  
114 my @chromosomes = (1..22);
115 push @chromosomes, 'X';
116 if ($self->sex eq 'M') {
117 push @chromosomes, 'Y';
118 }
119 foreach my $chr (@chromosomes) {
120 $command .= "INPUT=" . $self->output_dir . "/chr$chr/output/T.bam ";
121 }
122 $command .= "OUTPUT=" . $self->output_dir . "/T.bam; samtools index " . $self->output_dir . "/T.bam; ";
123 my $merge_stage = $self->HPCI_group->stage(
124 name => "bs_merge_final",
125 command => $command,
126 resources_required => {
127 h_vmem => '20G'
128 },
129 modules_to_load => [ 'picard/1.130', 'samtools/1.2' ],
130 extra_sge_args_string => " -q transient "
131 );
132 push(@{$stages}, $merge_stage);
133 return $stages;
134 }
135  
136 sub merge_vcf {
137 my $self = shift;
138  
139 $self->log->info("merge_vcf\n");
140 $self->log->info("\treference: " . $self->config->{reference} . "\n");
141  
142 my $stages = [];
143  
144 foreach my $chr (@{$self->chromosomes}) {
145 my $codes = {
146 snv => {
147 py => 'makevcf.py',
148 r => '',
149 l => '',
150 regex => ".bam_temp.haplo_$chr\_*_*.log"
151 },
152 indel => {
153 py => 'makevcf_indels.py',
154 r => '',
155 l => '',
156 regex => ".bam_temp.$chr\_*_*.log"
157 },
158 sv => {
159 py => 'makevcf_sv.py',
160 r => '-r',
161 l => '-l',
162 regex => ".bam_temp_$chr\_*_*_*.log"
163 }
164 };
165  
166 my $leaf_nodes = [];
167 my $pre_reqs = [];
168 while (my ($leaf, $attr) = each(%{$self->leaves->{$chr}})) {
169 $leaf =~ s/^([^_]*)_//;
170 my $current_phase = $1;
171  
172 my $node = $leaf;
173 $node =~ s/^([^_]*)_//;
174 $node =~ s/_n\d//g;
175 push(@{$leaf_nodes}, $node) unless grep {$_ eq $node} @{$leaf_nodes};
176  
177 foreach my $mutation_type (('snv','indel','sv')) {
178  
179 my $mutation_folder = $self->output_dir . "/mutlogs$chr/$node/$mutation_type/";
180 make_path($mutation_folder);
181 make_path($self->output_dir . "/vcf/");
182  
183 my $check_command = "if [ -d " . $self->output_dir . "/add" . $mutation_type . "_logs_" . $leaf . ".bam_temp/ ];";
184 my $find_command = "find " . $self->output_dir . "/add" . $mutation_type . "_logs_" . $leaf . ".bam_temp/ -name " . $leaf . $codes->{$mutation_type}->{regex} . " -exec cp {} $mutation_folder \\;";
185  
186 my $find_stage = $self->HPCI_group->stage(
187 name => "find_logs_$chr\_" . $current_phase . "_$leaf\_$mutation_type",
188 command => join(' ', $check_command, 'then', $find_command, '; fi'),
189 resources_required => {
190 h_vmem => '2G'
191 },
192 modules_to_load => [],
193 extra_sge_args_string => " -q transient "
194 );
195 push(@{$pre_reqs}, $find_stage);
196 push(@{$stages}, $find_stage);
197 }
198 }
199
200 foreach my $node (@{$leaf_nodes}) {
201 foreach my $mutation_type (('snv','indel','sv')) {
202 my $makevcf_command = '';
203 my $ref = 'snv' eq $mutation_type ? '' : $self->config->{reference};
204 my $mutation_folder = $self->output_dir . "/mutlogs$chr/$node/$mutation_type/";
205
206 if ('sv' eq $mutation_type) {
207 $makevcf_command .= "cat $mutation_folder/\* > $mutation_folder/svs.txt; ";
208 $mutation_folder = $mutation_folder . "/svs.txt";
209 }
210  
211 $makevcf_command .= "$codes->{$mutation_type}->{py} $codes->{$mutation_type}->{l} $mutation_folder $codes->{$mutation_type}->{r} $ref > " . $self->output_dir . "/vcf/chr$chr\_$node\_$mutation_type.vcf";
212  
213 my $merge_vcf_stage = $self->HPCI_group->stage(
214 name => "bs_merge_vcf_$chr\_$node\_$mutation_type",
215 command => $makevcf_command,
216 resources_required => {
217 h_vmem => '4G',
218 },
219 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'],
220 extra_sge_args_string => " -q transient "
221 );
222 $self->HPCI_group->add_deps(
223 pre_reqs => $pre_reqs,
224 deps => $merge_vcf_stage
225 );
226 push(@{$stages}, $merge_vcf_stage);
227 }
228 }
229 }
230 return $stages;
231 }
232  
233 1;