script/make_yaml_het.pl untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 253 CC 23 SCAR 45.1
CC
23
Cov
0%
make_yaml
156.0
main
132.0
calculate_bases
6.0
CRAP 90.8
1 #!/usr/bin/env perl
2  
3 ### bam_YAML_creater.pl #########################################################################
4 # Generate HET YAML file using a template yaml structure based upon the node structure and proportions
5  
6 ### HISTORY #######################################################################################
7 # Version Date Developer Comments
8 # 0.01 2015-05-25 ssivanandan Initial code development.
9 #
10 ### INCLUDES ######################################################################################
11 use warnings;
12 use strict;
13 use Carp;
14 use Getopt::Long;
15 use Pod::Usage;
16 use Path::Class;
17 use NGS::Tools::BAMSurgeon::Helper;
18 use Params::Validate qw(:all);
19 use YAML qw(LoadFile DumpFile Dump);
20 use Data::Dumper;
21 use POSIX qw(strftime ceil floor);
22 use Math::Random;
23  
24 our %opts = (
25 yaml_template => undef,
26 yaml_dir => undef,
27 sex => undef,
28 probability_wce => 1,
29 cb_dir => '/path/to/callable_bases_bed_dir/'
30 );
31  
32 #### MAIN CALLER #####################################################################################
33 my $result = main();
34 exit($result);
35  
36 #### main ############################################################################################
37 sub main{
38  
39 GetOptions(
40 \%opts,
41 "help|?",
42 "man",
43 "yaml_template:s",
44 "yaml_dir:s",
45 "probability_wce:s",
46 "name:s",
47 "sex:s",
48 "cb_dir:s" => \$opts{cb_dir}
49 ) or pod2usage(64);
50  
51 if ($opts{'help'}) { pod2usage(1) };
52 if ($opts{'man'}) { pod2usage(-exitstatus => 0, -verbose => 2) };
53  
54 my $yaml_dir_str = File::Spec->rel2abs($opts{'yaml_dir'});
55 my $yaml_dir = dir($yaml_dir_str);
56  
57 File::Path::make_path("$yaml_dir");
58  
59 my ($types, $array, $string)=YAML::LoadFile($opts{'yaml_template'});
60  
61 my @chr = (1..22);
62 push @chr,'X';
63 if($opts{sex} eq 'M'){
64 push @chr, 'Y';
65 }
66  
67
68 # find all collapsed bed files in the directory
69 # Callable bases calculation
70 my $cb_dir = dir($opts{'cb_dir'});
71 opendir(DIR, $cb_dir) or croak "unable to open bed directory at : $cb_dir";
72 my @cb_files = ();
73 while (my $file = readdir(DIR)) {
74 next unless (-f "$cb_dir/$file");
75 next unless ($file =~ m/\_collapsed.bed$/);
76 push (@cb_files, $file);
77 }
78 @cb_files = sort @cb_files;
79 # End of callable bases files retrieval
80  
81 my $yaml = {};
82 my $cb = {};
83  
84 foreach my $bed_file (@cb_files) {
85 my $cb_bed = "$cb_dir/$bed_file";
86 my ($chrom,$bases) = &calculate_bases($cb_bed);
87 $cb->{$chrom} = $bases;
88 }
89  
90 my @seen_chrs = ();
91 foreach my $template (keys $types->{templates}) {
92 my $template_chrs = $types->{templates}->{$template}->{chrs};
93 my @template_chrs = split(',', $template_chrs);
94  
95 foreach my $chrom (@template_chrs){
96 if (not grep $_ eq $chrom, @chr) {
97 print "ERROR: Chromosome $chrom not acceptable\n";
98 exit(1);
99 }
100 elsif (grep $_ eq $chrom, @seen_chrs) {
101 print "ERROR: Chromosome $chrom is duplicated\n";
102 exit(1);
103 }
104 push @seen_chrs, $chrom;
105
106 if($opts{sex} eq 'F' || ($opts{sex} eq 'M' && $chrom ne 'X' && $chrom ne 'Y')){
107 $yaml->{$chrom}={input_a => "./chr$chrom/phase.0.T.bam",input_b => "./chr$chrom/phase.1.T.bam",root => {}};
108 }
109 else{
110 $yaml->{$chrom}={input_a => "./chr$chrom/phase.0.T.bam",input_b => "",root => {}};
111 }
112 $yaml->{$chrom}{root} = make_yaml('node',$yaml->{$chrom}{root},'template_node',$types->{templates}->{$template}->{root},'chrom',$chrom,'cb',$cb,'probability_wce',$opts{probability_wce},'counts',$types->{counts},'rates',$types->{rates},'tree',$types->{root});
113 }
114 }
115  
116 if (scalar(@seen_chrs) ne scalar(@chr)) {
117 my @missing_chrs;
118 foreach my $chrom (@chr) {
119 if (not grep $_ eq $chrom, @seen_chrs) {
120 push @missing_chrs, $chrom;
121 }
122 }
123 print "Template missing for chromosomes: ", Dumper(@missing_chrs);
124 }
125 DumpFile("$yaml_dir_str/$opts{name}.yaml",$yaml);
126 }
127  
128  
129 ### calculate_muts #################################################################################
130 # Description:
131 # Calculate the number of picks from the covered bases
132 # Input Variables:
133 # $mut_rate = mutation rate per callable base
134 # $cb_bed = bed file containing callable bases
135 # Output Variables:
136 # $SNV_num = number of SNVs to spike in
137 # $chromosome = the chromosome of the bed file
138  
139 sub calculate_bases {
140 my ($cb_bed) = @_;
141  
142 open (my $bed_in, '<', $cb_bed) or croak "unable to open bed file at : $cb_bed";
143  
144 #read first line to get the chromosome
145 my $first_line = <$bed_in>;
146 $first_line =~ s/\W+$//;
147 my @row = split('\t', $first_line);
148 my $chromosome = $row[0];
149 $chromosome =~ s/chr//;
150 my $bases = ($row[2]-$row[1]) + 1;
151  
152 while (<$bed_in>) {
153 s/\W+$//;
154 my @row = split('\t');
155 $row[0] =~ s/chr//;
156 croak "BED file not for single chromosome" if ($chromosome ne $row[0]);
157  
158 #bed is inclusive of start and end
159 $bases += ($row[2]-$row[1]) + 1;
160 }
161 close($bed_in);
162  
163 return ($chromosome,$bases);
164 }
165  
166  
167  
168 ### make_yaml #################################################################################
169 # Description:
170 # Recursive function to build YAML file from template
171 # Input Variables:
172 # node = Node in the yaml file
173 # template_node = Node in the template file
174 # chrom = Chromosome
175 # cb = Callable bases HASHREF
176 # probability_wce = Probability of Whole chromosome event introduced randomly
177 # Output Variables:
178 # YAML node structure (object)
179 #
180  
181 sub make_yaml{
182 my %args = validate(
183 @_,
184 {
185 node => {
186 type => HASHREF,
187 required => 1,
188 },
189 template_node => {
190 type => HASHREF,
191 required => 1,
192 },
193 chrom => {
194 type => SCALAR,
195 required => 1,
196 },
197 cb => {
198 type => HASHREF,
199 required => 1,
200 },
201 probability_wce => {
202 type => SCALAR,
203 required => 1,
204 },
205 counts => {
206 type => HASHREF,
207 required => 1,
208 },
209 rates => {
210 type => HASHREF,
211 required => 1,
212 },
213 tree => {
214 type => HASHREF,
215 required => 1,
216 }
217 });
218 my $node = $args{node};
219 $node->{percent} = $args{tree}->{percent};
220 if(exists $args{template_node}->{mut_type} && ref($args{template_node}->{mut_type}) eq 'ARRAY'){
221 $node->{mut_type}=[];
222 $node->{mut_indx}=[];
223 $node->{mut_arg}=[];
224  
225 foreach my $elem ( keys $args{template_node}->{mut_type}){
226 my @mut_type_vals = split(' ', $args{template_node}->{mut_type}[$elem]);
227 my $mut_type = $mut_type_vals[0];
228 my $instances = $mut_type_vals[1];
229
230 my @mut_indx_vals = split(' ', $args{template_node}->{mut_indx}[$elem]);
231
232 my $mut_str = substr($mut_type,0,-2) eq 'snv' ? 'SNV'
233 : substr($mut_type,0,-2) eq 'indel' ? 'Indel'
234 : substr($mut_type,0,-2) eq 'sv' ? 'SV'
235 : 'WCE';
236  
237 next if ($args{chrom} eq 'X' or $args{chrom} eq 'Y') and substr($mut_type,-2) eq '_b';
238  
239 if ($mut_str ne 'WCE') {
240 my $mut_arg = $args{tree}->{$mut_str} / $args{counts}->{$mut_str} * $args{rates}->{$mut_str};
241 $mut_arg *= 2 if $args{chrom} eq 'X' or $args{chrom} eq 'Y';
242 my $muts = $args{cb}->{$args{chrom}} * $mut_arg;
243 $muts /= $instances;
244  
245 $muts = int($muts+0.5) > int $muts ? ceil($muts) : floor($muts);
246 next if $muts eq 0;
247  
248 foreach my $idx(@mut_indx_vals) {
249 push $node->{mut_arg}, $muts ;
250 push $node->{mut_type}, $mut_type;
251 push $node->{mut_indx}, $idx;
252 #print "Chromosome : $args{chrom} :: $args{cb}->{$args{chrom}}"
253 }
254 }
255 else{
256 my $r = random_uniform();
257 if($r < ($args{probability_wce}*1.0)){
258 foreach my $idx(@mut_indx_vals) {
259 push $node->{mut_arg}, substr($mut_type,0,-2);
260 push $node->{mut_type}, 'wce'.substr($mut_type,-2);
261 push $node->{mut_indx}, $idx;
262 }
263 }
264 }
265 }
266 }
267 else{
268 $node->{mut_type}=undef;
269 $node->{mut_indx}=undef;
270 $node->{mut_arg}=undef;
271 }
272  
273 if(exists $args{template_node}->{children}){
274 $node->{children} = [];
275 foreach my $child ( keys $args{template_node}->{children}) {
276 $node->{children}[$child] = {};
277 $node->{children}[$child] = make_yaml('node',$node->{children}[$child],'template_node',$args{template_node}->{children}[$child],'chrom',$args{chrom},'cb',$args{cb},'probability_wce',$args{probability_wce},'counts',$args{counts},'rates',$args{rates},'tree',$args{tree}->{children}[$child]);
278 }
279 }
280 return $node;
281 }
282  
283 ### USAGE ###
284 # perl make_yaml_het.pl --yaml_template S1_template.yaml --yaml_dir output_dir --name tumour_S1 --sex M --probability_wce 1 --cb_dir /path/to/callable_bases_bed_dir/
285 #
286 #
287 # AUTHORS
288 #
289 # Srinivasan Sivanandan
290 #
291 # Shadrielle Melijah G. Espiritu
292 #
293 # ACKNOWLEDGEMENTS
294 #
295 # Paul Boutros, PhD, PI -- Boutros Lab
296 #
297 # Takafumi Yamaguchi -- Boutros Lab
298 #
299 # SUPPORT
300 #
301 # For support please mail << <srinivasan.iitkgp at gmail.com> >>
302 #
303 # LICENSE AND COPYRIGHT
304 #
305 # Copyright 2015 Srinivasan Sivanandan.
306 #
307 # This program is free software; you can redistribute it and/or modify it
308 # under the terms of either: the GNU General Public License as published
309 # by the Free Software Foundation; or the Artistic License.
310 #
311 # See http://dev.perl.org/licenses/ for more information.
312 #