script/generate_signature.pl untested

filter: Statementstmt 0.0% 0 / 539 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 775 CC 63 SCAR 51.2
CC
63
Cov
0%
main
462.0
read_config
306.0
read_replication_time
110.0
CRAP 167.1
1 #!/usr/bin/env perl
2  
3 ### generate_signature.pl ############################################################################
4 # Generate mutation input varfile according to the Trinucleotide Signature and Proportions specified
5 # - read the signatures file table for mutation types and probabilities
6 # - calculate mutation number and probabilities for each signature specified
7 # - randomly choose positions in the chromosome according to mutation type (BY INDEPENDENT SAMPLING)
8 # - specify chromosome, start, end, vaf, and alternative base in BEDfile format as output (varfile)
9 # - take in custome mutations vcf file and incorporate according to chromosome
10 # - take in cancer file and cancer name to generate cancer proportioned mutations and rates
11 # - take in covered bases bed file and only spike in mutation in bases with over 10x coverage
12  
13 # Usage: <generate_signature.pl> [options] [file ...]
14 # Options:
15 # --help brief help message
16 # --man full documentation
17 # --config configuration file in YAML format containing signature information (Required)
18 # --chromosome the chromosome that mutations are generated for (Required)
19 # --cb_bed the bed file for the callable bases of the chromosome (default = "None")
20 # --mut_num number of sites to generate (default = 0)
21 # --mut_rate rate of mutation to generate (default = 0)
22 # --seed seed used for reproducible random mutation location generation (default = "None")
23 # --mut_file file to output mutations to (default = "./mut_file.txt")
24 # --minvaf minimum variant allele fraction (default = 0.25)
25 # --maxvaf maximum variant allele fraction (default = 0.5)
26 # --vafbeta1 left shape parameter for beta distribution of VAFs (default = 2.0)
27 # --vafbeta2 right shape parameter for beta distribution of VAFs (default = 2.0)
28  
29  
30 ### HISTORY #######################################################################################
31 # Version Date Developer Comments
32 # 0.01 2015-05-15 lliu Initial code development.
33 #
34 # 0.02 2016-01-10 lliu Incorporate replication timing effect in mutation rate
35 #
36 # 0.03 2016-05-01 lliu Incorporate mutation rate and signature variations
37 #
38 #
39 ### INCLUDES ######################################################################################
40 use NGS::Tools::BAMSurgeon::Helper;
41 use warnings;
42 use strict;
43 use Carp;
44 use Getopt::Long;
45 use Pod::Usage;
46 use File::Basename;
47 use File::Path qw(make_path); # makes directories given paths
48 use FindBin qw($Bin); # finds bin where this method was invoked
49 use Path::Class;
50 use YAML qw(LoadFile);
51 use YAML::Tiny;
52 use Data::Dumper;
53 use List::MoreUtils qw( each_array );
54 use POSIX qw(strftime);
55 use POSIX qw(ceil);
56 use Bio::SeqIO; # reads reference genome fasta file
57 use Bio::SeqIO::fasta;
58 use Math::Random;
59 use POSIX qw(ceil);
60 use File::ShareDir ':ALL';
61  
62 ### COMMAND LINE DEFAULT ARGUMENTS ################################################################
63 # list of arguments and default values go here as hash key/value pairs
64 our %opts = (
65 config => "$Bin/../share/config.yaml", ### the yaml config file
66 chromosome => undef,
67 reference => undef,
68 cb_bed => undef, ### full path to chromosome callable bases bed file
69 mut_num => 0,
70 mut_rate => 0,
71 seed => "None", ###seed to make reproducible random picks
72 mut_file => "./snv_mutations.txt", ###file to output mutations to
73 );
74  
75 $opts {'scripts'} = {
76 };
77  
78 ### MAIN CALLER ###################################################################################
79 my $result = main();
80 exit($result);
81  
82 ### FUNCTIONS #####################################################################################
83  
84 ### main ##########################################################################################
85 # Description:
86 # Main subroutine for program
87 # Input Variables:
88 # %opts = command line arguments
89 # Output Variables:
90 # N/A
91 sub main {
92 GetOptions(
93 \%opts,
94 "help|?",
95 "man",
96 "config|c=s" => \$opts{'config'}, ### YAML file,
97 "chromosome|chrom=s" => \$opts{'chromosome'},
98 "reference|ref=s" => \$opts{'reference'},
99 "cb_bed=s" => \$opts{'cb_bed'},
100 "mut_num|n:i" => \$opts{'mut_num'},
101 "mut_rate:f" => \$opts{'mut_rate'},
102 "seed:i" => \$opts{'seed'},
103 "mut_file|mf:s" => \$opts{'mut_file'},
104 ) or pod2usage(64);
105  
106 if ($opts{'help'}) { pod2usage(1) };
107 if ($opts{'man'}) { pod2usage(-exitstatus => 0, -verbose => 2) };
108  
109 ### check for undef arguments in %opts ###
110 while(my ($arg, $value) = each(%opts)) {
111 if (!defined $value) {
112 print("ERROR: Missing argument $arg\n");
113 pod2usage(128); # prints out SYNOPSIS
114 }
115 } # while
116  
117 #load seed
118 if ($opts{'seed'} eq 'None') {
119 srand;
120 } else {
121 srand($opts{'seed'});
122 random_set_seed_from_phrase($opts{'seed'});
123 }
124  
125 #read in files
126 my $config = YAML::LoadFile($opts{'config'});
127 my $cb_bed = $opts{'cb_bed'};
128 my $chromosome = $opts{'chromosome'};
129  
130 #read in chromosome genome
131 my $genome = Bio::SeqIO->new(-file => "<$opts{'reference'}",
132 -format => "largefasta");
133 my $primary_id;
134 my $seq;
135 while ($seq = $genome->next_seq){
136 $primary_id = $seq->primary_id;
137 last if ($primary_id eq $chromosome);
138 }
139  
140 croak "ERROR: chromosome $chromosome inputted not found in the reference genome given" if ($primary_id ne $chromosome);
141  
142 #read in signature file
143 my $signature_file = dist_file('NGS-Tools-BAMSurgeon', 'signatures.txt');
144 croak "ERROR: no signatures file given, please use --picksomatic to generate random mutations" if (not $signature_file);
145 my $signatures = &read_signature($signature_file);
146  
147 #read in proportions from config file
148 my ($signature, $mut_rate, $vaf_param) = &read_config($config, $signatures, $config->{'cancer'}->{'sig_vary'});
149  
150 #determine number of mutations to pick and basic information of the chromosome
151 my $num_picks;
152 my $min_loc; # smallest covered base location
153 my $max_loc; # largest covered base location
154 my $bases; # number of covered bases
155 if ($opts{'mut_rate'} != 0){ #if mutation rate is given
156 $mut_rate = $opts{'mut_rate'};
157 ($num_picks, $min_loc, $max_loc, $bases) = &calculate_muts($mut_rate, $cb_bed, $chromosome);
158 } elsif ($mut_rate != 0){ #if given cancer
159 ($num_picks, $min_loc, $max_loc, $bases) = &calculate_muts($mut_rate, $cb_bed, $chromosome);
160 } else { #use default mutation rate
161 ($num_picks, $min_loc, $max_loc, $bases) = &calculate_muts(0.0000002, $cb_bed, $chromosome);
162 }
163 if ($opts{'mut_num'} != 0) { # if mutation number is given, takes precedence
164 $num_picks = $opts{'mut_num'};
165 say ("INFO: choosing $num_picks mutations.");
166 } else {
167 say ("INFO: choosing $num_picks mutations with mutation rate of $mut_rate.");
168 }
169
170 my $locations = []; #chosen mutations
171 my %loc = (); #variable for testing for duplicate locations
172 my $vcf_num = 0;
173  
174 #read vcf file and extract relevant mutations
175 if ($config->{'vcf_file'}) {
176 my $vcf_file = $config->{'vcf_file'};
177 my $vcf_mutations = &read_vcf($vcf_file, $chromosome);
178 my @vcf_muts;
179  
180 foreach my $i (0..scalar(@{$vcf_mutations})-1) {
181 if (exists $loc{${${$vcf_mutations}[$i]}[1]}) {
182 say ("WARN: duplicated location in vcf file, omitted");
183 } else {
184 my $vaf = &choose_vaf($vaf_param);
185 push (@vcf_muts, [${${$vcf_mutations}[$i]}[0], ${${$vcf_mutations}[$i]}[1], ${${$vcf_mutations}[$i]}[1], $vaf, ${${$vcf_mutations}[$i]}[2]]);
186 $loc{${${$vcf_mutations}[$i]}[1]} = 1;
187 }
188 }
189  
190 $vcf_num = scalar(@vcf_muts);
191 say ("INFO: added $vcf_num SNVs from vcf file");
192 push (@{$locations}, @vcf_muts);
193  
194 $num_picks -= $vcf_num;
195 say ("INFO: choosing $num_picks after subtracting from vcf file");
196 }
197  
198 #read in replication timing
199 my $timing_effect = $config->{'rep_time_effect'};
200 my $rep_time_file = dist_file('NGS-Tools-BAMSurgeon', 'imputed_byref.csv');
201 $timing_effect = 1 if ((!defined $timing_effect) & (defined $rep_time_file));
202 my $rep_time = &read_replication_time($rep_time_file, $chromosome, $timing_effect);
203  
204 my $i = 1; #number chosen so far
205 my @alt_C = ('A', 'T', 'G');
206 my @alt_T = ('A', 'C', 'G');
207  
208 my $current_signature = {};
209  
210 while ($i <= $num_picks) {
211  
212 my $vaf = &choose_vaf($vaf_param);
213  
214 my $location = []; #current mutation in construction
215 push (@{$location}, $chromosome); #chrom[0]
216  
217 my ($base, $tri_region) = &sample_base($bases, $min_loc, $max_loc, $cb_bed, $seq);
218 push (@{$location}, $base); #location[1]
219 push (@{$location}, substr($tri_region, 1, 1)); #ref[2]
220  
221 my ($mut_prob, $NA_use) = &mutation_probability($base, $rep_time);
222  
223 if ($NA_use){
224 say ("WARN: position with no replication information ".join(" ", @{$location})." chosen at number $i, choosing again");
225 next;
226 }
227  
228 if (exists $loc{${$location}[1]}) {
229 say ("WARN: duplicated location ".join(" ", @{$location})." chosen at number $i, choosing again");
230 next;
231 }
232  
233 #Layer 1: replication time
234 my $rand_num = random_uniform(1, 0, 1);
235 next if ($rand_num > $mut_prob);
236  
237 #Layer 2: signature
238 my $alt;
239 if (substr($tri_region, 1, 1) eq 'C'){
240 $alt = $alt_C[rand @alt_C];
241 } else {
242 $alt = $alt_T[rand @alt_T];
243 }
244 #my $alt_rand = random_uniform(1, 0, 1);
245 #next if ($alt_rand > ($signature->{$tri_region}->{$alt} / $signature->{'max'}));
246 if (!$current_signature->{$tri_region}->{$alt}){
247 $current_signature->{$tri_region}->{$alt} = 0;
248 }
249 my $current_rate = $current_signature->{$tri_region}->{$alt} / $i;
250 next if ($current_rate >= $signature->{$tri_region}->{$alt});
251  
252 #Passed all layers
253 push (@{$location}, $alt); #alt[3]
254 $current_signature->{$tri_region}->{$alt} += 1;
255  
256 #actual format required by BAMsurgeon
257 #'chr', 'start', 'end', 'vaf', 'alt'
258 push (@{$locations}, [${$location}[0], ${$location}[1], ${$location}[1], $vaf, ${$location}[3]]);
259  
260 #used for testing with R script
261 #'chr', 'start', 'ref', 'alt'
262 #push (@{$locations}, $location);
263 say "INFO: ".$i." mutations chosen" if ($i % 100 == 0);
264 $loc{${$location}[1]} = 1;
265 $i += 1;
266 }
267
268 my $mut_file = $opts{'mut_file'};
269  
270 #sort mutations by ascending location
271 @{$locations} = sort { $a->[1] <=> $b->[1] } @{$locations};
272  
273 (open my $mutation_file, ">", "$mut_file") or croak "ERROR: unable to open mut_file file at: $mut_file";
274 foreach my $location (@{$locations}) {
275 print ($mutation_file join("\t", @{$location})."\n");
276 }
277 close ($mutation_file);
278  
279 return 0;
280 } #main
281  
282 ### read_signature #################################################################################
283 # Description:
284 # Read the signature file provided assuming that the third column is "Somatic Mutation Type"
285 # Input Variables:
286 # $signature_file = signature file
287 # Output Variables:
288 # $signatures = all of the signatures stored in a hash of hash of hash
289  
290 sub read_signature {
291 my ($signature_file) = @_;
292 say "INFO: reading signatures file $signature_file";
293
294 open (my $sig_in, '<', $signature_file) or croak "ERROR: unable to open signatures file at : $signature_file";
295  
296 my $signatures = {};
297  
298 my $header = <$sig_in>;
299 $header =~ s/\W+$//;
300 my @header = split('\t', $header);
301  
302 #read the header line and make a key for each of the valid mutations
303 foreach my $signature (@header){
304 #if ($signature =~ m/Signature\s\d/) { #assume that signatures that begin with a number are validated
305 if ($signature =~ m/Signature/){
306 $signatures->{$signature} = {};
307 } else {
308 say ("WARN: unvalidated signature $signature dropped") if ($signature =~ m/Signature/);
309 }
310 }
311  
312 #read each line and assign the probabilities of each mutation to each signature
313 while (<$sig_in>){
314 s/\W+$//;
315 my %row;
316 @row{@header} = split('\t');
317 foreach my $signature (keys %$signatures){
318 $signatures->{$signature}->{$row{$header[2]}} = $row{$signature}; #assume that the 3rd column is 'somatic mutation type'
319 }
320 }
321 close($sig_in);
322  
323 #validate that each signature has probabilities rdding up to 1
324 foreach my $signature (keys %{$signatures}) {
325 my $sum = 0;
326 foreach my $mutation (keys %{$signatures->{$signature}}) {
327 $sum += $signatures->{$signature}->{$mutation};
328 }
329 $sum = int(($sum * 10000.0) + 0.5) / 10000.0;
330  
331 if ($sum != 1.0000) {
332 delete $signatures->{$signature};
333 say ("WARN: $signature dropped for probabilities not added up to 1");
334  
335 print $sum;
336  
337 }
338 }
339  
340 return ($signatures);
341 }
342  
343 ### read_config ####################################################################################
344 # Description:
345 # Read the config hash specified by the YAML file
346 # Input Variables:
347 # $config = config of signature type or cancer type to simulate
348 # $signatures = the hash of probabilities of all signatures from file
349 # $sig_vary = file containing variation in cancer signature
350 # Output Variables:
351 # $signature = the overall trinucleotide signature to follow
352 # $mut_rate = the mutation rate determined by the cancer or by default
353 sub read_config {
354 my ($config, $signatures) = @_;
355  
356 my $proportions = {};
357 my $mut_rate = 0.000001;
358  
359 #checking if signature or cancer information present, if both present signature info is used
360 if ((not $config->{'signatures'}) && (not $config->{'cancer'}->{'name'})) {
361 croak "ERROR: no signature or cancer type has been specified, please use randomsites.py to generate random mutations";
362  
363 } elsif (not $config->{'signatures'}) {
364 #read in cancer related files
365 my ($cancer_sigs) = &read_cancer($config, $config->{'cancer'}->{'mut_vary'});
366  
367 my $cancer = $config->{'cancer'}->{'name'};
368 $cancer = lc ($cancer);
369  
370 my $proportions_sum = 0;
371 my @sig_list;
372 my $pro_hash;
373  
374 #TODO find better way to generation variation in signatures
375 if ((defined $config->{'cancer'}->{'sig_vary'}) && ($config->{'cancer'}->{'sig_vary'}) == 1) {
376 #read in cancer signatures variation file
377 my $yaml = YAML::Tiny->read(dist_file('NGS-Tools-BAMSurgeon', 'cancer_sig_vary.yaml'));
378 my $cancer_var = $yaml->[0];
379 #save lowercase cancer keys
380 foreach my $cancer (keys %{$cancer_var}) {
381 my $mod_cancer = lc ($cancer);
382 $cancer_var->{$mod_cancer} = $cancer_var->{$cancer};
383 }
384 if ($cancer_var->{$cancer}){
385 #pick a signature proportion for the cancer at random
386 my @cancer_pp = @{$cancer_var->{$cancer}};
387 my $chosen_pp = $cancer_pp[rand @cancer_pp];
388 @sig_list = (keys $chosen_pp);
389 $pro_hash = $chosen_pp;
390 }
391 else {
392 say "WARN: sig_vary chosen as 1 but no sig_vary information available for ".$cancer.", using default signatures";
393 }
394  
395 }
396  
397 if ((!@sig_list) || (!defined $pro_hash)){
398 croak "ERROR: $cancer signature is not defined in the cancer files provided" if (scalar (keys %{$cancer_sigs->{$cancer}}) <= 1);
399 @sig_list = keys %{$cancer_sigs->{$cancer}};
400 $pro_hash = $cancer_sigs->{$cancer};
401 }
402 my @requests = ();
403 foreach my $request (@sig_list) {
404 next if ($request eq 'Mut rate');
405 if ($signatures->{$request}) {
406 push (@requests, $request);
407 $proportions_sum += $pro_hash->{$request};
408 } else {
409 say "WARN: $request is not defined in signatures file, proportions recalculated";
410 }
411 }
412 say ("INFO: using signature information of $cancer");
413 foreach my $request (@requests) {
414 next if ($request eq 'Mut rate');
415 $proportions->{$request}->{'mutations'} = $signatures->{$request};
416 $proportions->{$request}->{'proportion'} = ($pro_hash->{$request})/$proportions_sum;
417 say "INFO: $request with proportion ".$proportions->{$request}->{'proportion'};
418 }
419 if (!$cancer_sigs->{$cancer}->{'Mut rate'}){
420 say "WARN: $cancer mutation rate is not defined in the cancer files provided, using default of 0.0000002 if necessary";
421 $mut_rate = 0.0000002;
422 } else {
423 $mut_rate = $cancer_sigs->{$cancer}->{'Mut rate'};
424 }
425 } else {
426 my @requests = (keys %{$config->{'signatures'}});
427 my $proportions_sum = 0;
428 say ("INFO: using specified signature information");
429 foreach my $request (@requests) {
430 my $mod_request = ucfirst (lc ($request));
431 $mod_request =~ s/1a$/1A/;
432 $mod_request =~ s/1b$/1B/;
433 croak "ERROR: $mod_request is not defined in signatures file" if (not $signatures->{$mod_request});
434 $proportions->{$request}->{'mutations'} = $signatures->{$mod_request};
435 $proportions->{$request}->{'proportion'} = $config->{'signatures'}->{$request};
436 $proportions_sum += $proportions->{$request}->{'proportion'};
437 say "INFO: $request with proportion ".$proportions->{$request}->{'proportion'};
438 }
439 croak "ERROR: signatures proportions not adding up to 1" if ($proportions_sum != 1);
440 }
441  
442 my $signature = &concat_signature($proportions);
443  
444 my $vaf_param;
445 $vaf_param->{'minvaf'} = (defined $config->{'minvaf'})? $config->{'minvaf'} : 1;
446 $vaf_param->{'maxvaf'} = (defined $config->{'maxvaf'})? $config->{'maxvaf'} : 1;
447 $vaf_param->{'vafbeta1'} = (defined $config->{'vafbeta1'})? $config->{'vafbeta1'} : 2.0;
448 $vaf_param->{'vafbeta2'} = (defined $config->{'vafbeta2'})? $config->{'vafbeta2'} : 2.0;
449  
450 return ($signature, $mut_rate, $vaf_param);
451 }
452  
453 ### read_cancer ####################################################################################
454 # Description:
455 # Read the config hash with regards to the cancer information
456 # Input Variables:
457 # $config = config of signature type or cancer type to simulate
458 # $mut_vary = file containing information of the mutation rate distribution of cancers
459 # Output Variables:
460 # $cancer_sigs = the trinucleotide signatures and mutation rate information of cancers
461 sub read_cancer {
462 my ($config, $mut_vary) = @_;
463  
464 my $cancer_sigs = YAML::LoadFile(dist_file('NGS-Tools-BAMSurgeon', 'cancer_sigmut.yaml'));
465 #save lowercase cancer keys
466 foreach my $cancer (keys %{$cancer_sigs}) {
467 my $mod_cancer = lc ($cancer);
468 $cancer_sigs->{$mod_cancer} = $cancer_sigs->{$cancer};
469 }
470  
471 #alter chosen cancer mutation and signature according to vary options
472 my @cancers = (keys %{$cancer_sigs});
473 my $cancer = $config->{'cancer'}->{'name'};
474 $cancer = lc ($cancer);
475 my @full_names = grep/$cancer/i, @cancers;
476  
477 #TODO change variation to scale
478 if ((defined $mut_vary) && $mut_vary == 1) {
479 #read in mutation rate variation file
480 my $yaml = YAML::Tiny->read(dist_file( 'NGS-Tools-BAMSurgeon', 'cancer_mut_vary.yaml'));
481 my $cancer_muts = $yaml->[0];
482 #save lowercase cancer keys
483 foreach my $cancer_mut (keys %{$cancer_muts}) {
484 my $mod_cancer = lc ($cancer_mut);
485 $cancer_muts->{$mod_cancer} = $cancer_muts->{$cancer_mut};
486 }
487  
488 if (not $cancer_muts->{$cancer}){
489 say "WARN: mut_vary chosen as 1 but no mut_vary information available for ".$cancer.", using default mutation rates";
490 }
491 else {
492 foreach my $name (@full_names) {
493 $cancer_sigs->{$name}->{'Mut rate'} = &cancer_mut_sim($cancer_muts->{$cancer});
494 }
495 #also add in name in case it is not found in sigmut file
496 $cancer_sigs->{$cancer}->{'Mut rate'} = &cancer_mut_sim($cancer_muts->{$cancer});
497 }
498 }
499  
500 return ($cancer_sigs);
501 }
502  
503 ### cancer_mut_sim ###############################################################################
504 # Description:
505 # Simulate the mutation rate of the cancer according to 2 Normal Gaussian mixture model *see R script
506 # Input Variables:
507 # $cancer_model: the lambda, mu, sigma parameters of the specific cancer
508 # Output Variables:
509 # $rate = the simulated mutation rate of the cancer
510 sub cancer_mut_sim {
511 my ($cancer_model) = @_;
512  
513 my $mu;
514 my $sigma;
515 my $rate;
516  
517 my $rand_num = random_uniform(1, 0, 1);
518  
519 if ($rand_num <= $cancer_model->{'lambda'}->[0]){
520 $mu = $cancer_model->{'mu'}->[0];
521 $sigma = $cancer_model->{'sigma'}->[0];
522 } else {
523 $mu = $cancer_model->{'mu'}->[1];
524 $sigma = $cancer_model->{'sigma'}->[1];
525 }
526  
527 my $log_rate = random_normal(1, $mu, $sigma);
528 $rate = 10**$log_rate/1000000;
529  
530 return ($rate);
531 }
532  
533 ### concat_signature ###############################################################################
534 # Description:
535 # Calcualte the overall trinucleotide signature
536 # Input Variables:
537 # $proportion = the mutations and their proportion makeup of the signature
538 # Output Variables:
539 # $signature = the overall trinucleotide signature to follow
540  
541 sub concat_signature {
542 my ($proportions) = @_;
543  
544 my @nucleotides = ('A', 'T', 'C', 'G');
545 my $signature = {};
546  
547 #generate all trinucleodie combinations
548 foreach my $first (@nucleotides) {
549 foreach my $middle ('C', 'T') {
550 foreach my $last (@nucleotides) {
551 $signature->{"$first$middle$last"} = {};
552 }
553 }
554 }
555  
556 #cumulate the effects of all signatures
557 foreach my $mut_sig (keys %{$proportions}) {
558 foreach my $mutation (keys %{$proportions->{$mut_sig}->{'mutations'}}){
559 $proportions->{'Total'}->{'mutations'}->{$mutation} += $proportions->{$mut_sig}->{'mutations'}->{$mutation} * $proportions->{$mut_sig}->{'proportion'};
560 }
561 }
562  
563 #find the maximum for normalization
564 my $pro_max = 0;
565 foreach my $mutation (keys %{$proportions->{'Total'}->{'mutations'}}){
566 if ($proportions->{'Total'}->{'mutations'}->{$mutation} > $pro_max){
567 $pro_max = $proportions->{'Total'}->{'mutations'}->{$mutation};
568 }
569 }
570  
571 #assign to "XXX" -> "ALT" format
572 foreach my $mutation (keys %{$proportions->{'Total'}->{'mutations'}}){
573 my $mut_tri = substr($mutation, 0, 1).substr($mutation, 2, 1).substr($mutation, 6, 1);
574 my $alt_base = substr($mutation, 4, 1);
575 $signature->{$mut_tri}->{$alt_base} += $proportions->{'Total'}->{'mutations'}->{$mutation};
576 }
577  
578 $signature->{'max'} = $pro_max;
579  
580 return ($signature);
581 }
582  
583 ### sample_base ###################################################################################
584 # Description:
585 # Randomly sampling a base location in the genome, in the covered bases given
586 # Input Variables:
587 # $bases = total number of callable bases
588 # $min_loc = minimum location of covered base
589 # $max_loc = maximum location of covered base
590 # $cb_bed = cb_bed file
591 # $seq = the genome sequence
592 # Output Variables:
593 # $base = the randomly sampled location
594 # $tri_region = the trinucleotide context of the location
595  
596 sub sample_base {
597 my ($bases, $min_loc, $max_loc, $cb_bed, $seq) = @_;
598 my $flag = 0;
599 my $base;
600 my $tri_region;
601 while (!$flag){
602 my $position = random_uniform_integer(1, 1, $bases);
603  
604 $base = &find_location($position, $cb_bed);
605  
606 $tri_region = $seq->subseq($base-1,$base+1);
607 $tri_region = uc($tri_region);
608  
609 my $ref = substr($tri_region, 1, 1);
610 if ($ref =~ m/A|G/) {
611 $tri_region =~ tr/ATCG/TAGC/;
612 $tri_region =~ s/^(.)(.)(.)$/$3$2$1/;
613 }
614
615 #skip regions that are not given in the reference genome
616 $flag = ($tri_region !~ m/N+/);
617 say "WARN: callable trinucleotide region ".$base." ".$tri_region." contains N in reference genome, choosing again" if (!$flag);
618 }
619  
620 return ($base, $tri_region);
621 }
622  
623 ### find_location #############################################################################
624 # Description:
625 # Find location of base in covered bases
626 # Input Variables:
627 # $position = position of base in covered bases
628 # $cb_bed = covered bases bed file
629 # Output Variables:
630 # $base = location of base
631  
632 sub find_location {
633 my ($position, $cb_bed) = @_;
634 my $base;
635 my $sum = 0;
636 open (my $bed_in, '<', $cb_bed) or croak "ERROR: unable to open bed file at : $cb_bed";
637 while (<$bed_in>) {
638 s/\W+$//;
639 my @row = split('\t');
640 my $pos = $position + $row[1] - 1;
641 if ($pos >= $row[2]){
642 $position = $position - ($row[2] - $row[1]);
643 } else {
644 $base = $pos;
645 last;
646 }
647 }
648 close($bed_in);
649  
650 #correct for the fact that bed files are 0 based
651 $base = $base + 1;
652  
653 return ($base);
654 }
655  
656 ### mutation_probability #########################################################################
657 # Description:
658 # Calcualte the probability that the given base would be mutated, based on replication timing
659 # Input Variables:
660 # $base = the location of the base
661 # $rep_time = replicating timing data
662 # Output Variables:
663 # $mut_prob = probability of mutation of given base
664 # $NA_use = whether the region has no replication timing information
665  
666 sub mutation_probability {
667 my ($base, $rep_time) = @_;
668  
669 my $mut_prob;
670 my $NA_use = 0;
671  
672 my $base_region = ceil($base / 100000) - 1;
673 $mut_prob = $rep_time->[$base_region];
674  
675 if (!defined $mut_prob) {
676 $NA_use = 1;
677 }
678  
679 return ($mut_prob, $NA_use);
680 }
681  
682 ### read_replication_time #########################################################################
683 # Description:
684 # Read and process file storing replication time of the genome
685 # Input Variables:
686 # $rep_time_file = csv file storing chromosome location and replication time
687 # $chromosome = the current chromosome
688 # $timing_effect = how much the mutation rate of regions should be affected by replication
689 # Output Variables:
690 # $rep_times = store replciation times of chromosome locations
691  
692 sub read_replication_time {
693 my ($rep_time_file, $chromosome, $timing_effect) = @_;
694  
695 my $rep_time = [];
696 my @equal_time = (1)x3000;
697  
698 $chromosome = 23 if ($chromosome eq 'X');
699  
700 if (not $rep_time_file){
701 say "WARN: no replication timing effect file given, treating all bases as with same replication time";
702 $rep_time = \@equal_time;
703 return $rep_time;
704 }
705  
706 if ($chromosome eq 'Y'){
707 say "WARN: no replication timing available for chromosome Y, treating all bases as with same replication time";
708 $rep_time = \@equal_time;
709 return $rep_time;
710 }
711  
712 open (my $csv_in, '<', $rep_time_file) or croak "ERROR: unable to open vcf file at : $rep_time_file";
713 my $header_line = <$csv_in>;
714 $header_line =~ s/\W+$//;
715 my @header = split('\t', $header_line);
716 my ($index) = grep {$header[$_] eq 'corr' } (0 .. @header-1);
717  
718 if (not defined $index){
719 say "WARN: replication timing effect file does not have \'corr\' column, treating all bases as same";
720 $rep_time = \@equal_time;
721 close($csv_in);
722 return $rep_time;
723 }
724  
725 my $min = 2000;
726 my $max = -2000;
727  
728 #read each line and assign the mutation rate to the chromosome location
729 while (<$csv_in>){
730 s/\W+$//;
731 my $row = [split('\t')];
732 next if (${$row}[0] != $chromosome); #skip to current chromosome
733  
734 if (${$row}[$index] eq "NA" || ${$row}[$index] eq "NaN"){
735 push (@{$rep_time}, undef);
736 } else{
737 my $corr = ${$row}[$index];
738 $corr = 10**$corr;
739 push (@{$rep_time}, $corr);
740 $max = $corr if ($corr > $max);
741 $min = $corr if ($corr < $min);
742 }
743 }
744 close($csv_in);
745  
746 my $diff = $max - $min;
747  
748 my $scale = ($max - $diff * $timing_effect) / $max;
749 #my $scale = 1 - abs(($diff * $timing_effect) / $max);
750  
751 #scale the range by timing effect
752 foreach my $i (0..scalar(@{$rep_time})-1) {
753 next if (!defined $rep_time->[$i]);
754 $rep_time->[$i] = ((1 - $scale)*($rep_time->[$i] - $min)) / $diff + $scale;
755 }
756 return ($rep_time);
757 }
758  
759 ### rep_mut_correlation ###########################################################################
760 # Description:
761 # Use correlation to calculate mutation rate of region by replication time
762 # Input Variables:
763 # $replication = replication time of region
764 # $$rep_mut_numbers = contains coefficients and values used to calculate mutation rate
765 # Output Variables:
766 # $with_error = mutation of region calculated
767  
768 sub rep_mut_correlation {
769 my ($replication, $rep_mut_numbers) = @_;
770  
771 my $slope = $rep_mut_numbers->{'rep_mut_regression'}->{'slope'};
772 my $intercept = $rep_mut_numbers->{'rep_mut_regression'}->{'intercept'};
773 my $se = $rep_mut_numbers->{'rep_mut_regression'}->{'se'};
774  
775 my $rep_mut = $replication*$slope + $intercept;
776  
777 #my $with_error = random_normal(1, $rep_mut, $se);
778  
779 return ($rep_mut);
780 }
781  
782 ### choose_vaf ###############################################################################
783 # Description:
784 # Choose the variant allele fraction according to parameters described
785 # Input Variables:
786 # $vaf_param = the parameters for generating vaf
787 # Output Variables:
788 # $vafs = the vafs generated
789  
790 sub choose_vaf {
791 my ($vaf_param) = @_;
792  
793 my $vaf = Math::Random::random_beta(1, $vaf_param->{'vafbeta1'}, $vaf_param->{'vafbeta2'});
794  
795 $vaf = $vaf * ($vaf_param->{'maxvaf'}-$vaf_param->{'minvaf'}) + $vaf_param->{'minvaf'};
796  
797 return ($vaf);
798 }
799  
800 ### calculate_muts #################################################################################
801 # Description:
802 # Calculate the number of picks from the covered bases
803 # Input Variables:
804 # $mut_rate = mutation rate per callable base
805 # $cb_bed = bed file containing callable bases
806 # $chromosome = the fasta chromosome
807 # Output Variables:
808 # $SNV_num = number of SNVs to spike in
809  
810 sub calculate_muts {
811 my ($mut_rate, $cb_bed, $chromosome) = @_;
812  
813 my $bases = 0;
814 my $min_loc = 0;
815 my $max_loc;
816  
817 open (my $bed_in, '<', $cb_bed) or croak "ERROR: unable to open bed file at : $cb_bed";
818  
819 my $first_line = <$bed_in>;
820 my @first_row = split('\t', $first_line);
821 croak "ERROR: primary_id of fasta file not the same as chromosome of bed file given" if ($first_row[0] ne $chromosome);
822 #bed file includes position at chromSTART and excludes position at chromEND
823 #range calculated by END - START + 1 - 1
824 $bases += ($first_row[2] - $first_row[1]);
825 $min_loc = $first_row[1];
826  
827 my @last_row;
828 while (<$bed_in>) {
829 s/\W+$//;
830 my @row = split('\t');
831 croak "ERROR: primary_id of fasta file not the same as chromosome of bed file given" if ($row[0] ne $chromosome);
832
833 $bases += ($row[2] - $row[1]);
834 @last_row = @row;
835 }
836 close($bed_in);
837 $max_loc = $last_row[2] - 1;
838 print "INFO: Chromosome: $chromosome\tCovered bases: $bases\tMin: $min_loc\tMax: $max_loc\n";
839  
840  
841 my $SNV_num = 200;
842 if ($bases){
843 $SNV_num = $bases * $mut_rate;
844 $SNV_num = sprintf("%.0f", $SNV_num);
845 } else {
846 say ("INFO: cannot obtain readcount information for chromosome $chromosome, picking 200 mutations.");
847 }
848  
849 return ($SNV_num, $min_loc, $max_loc, $bases);
850 }
851  
852 ### check_location ################################################################################
853 # Description:
854 # Check if the location generated is one of the callable bases
855 # Input Variables:
856 # $location = location to spike in mutation
857 # $cb_bed = covered bases bed file
858 # Output Variables:
859 # $flag = 0 for location exists, 1 for location not covered
860  
861 sub check_location {
862 my ($location, $cb_bed) = @_;
863  
864 # preset flag assuming that location is NOT found
865 my $flag = 1;
866  
867 #correct for the fact that bed files are 0 based
868 my $pos = $location - 1;
869 open (my $bed_in, '<', $cb_bed) or croak "ERROR: unable to open bed file at : $cb_bed";
870 while (<$bed_in>) {
871 s/\W+$//;
872 my @row = split('\t');
873 #bed file includes position at chromSTART and excludes position at chromEND
874 if ($pos >= $row[1] && $pos < $row[2]){
875 $flag = 0;
876 last;
877 }
878 }
879 close($bed_in);
880  
881 return ($flag);
882 }
883  
884 ### read_vcf #################################################################################
885 # Description:
886 # Read the vcf file with the required mutations and record those applicable to this chromosome
887 # Input Variables:
888 # $vcf_file = vcf file
889 # $chromosome = the current chromosome
890 # Output Variables:
891 # $vcf_mutations = the mutations listed in the vcf file for the current chromosome
892  
893 sub read_vcf {
894 my ($vcf_file, $chromosome) = @_;
895 say "INFO: reading vcf file $vcf_file";
896
897 open (my $vcf_in, '<', $vcf_file) or croak "ERROR: unable to open vcf file at : $vcf_file";
898  
899 my $vcf_mutations = [];
900  
901 while (<$vcf_in>) {
902 s/\W+$//;
903 my @row = split('\t');
904 #$row[0] =~ s/chr//;
905 if ($row[0] eq $chromosome) {
906 # check that line represents a SNV
907 if (length($row[3]) == 1 && length($row[4]) == 1) {
908 push (@{$vcf_mutations}, [$chromosome, $row[1], $row[4]]);
909 }
910 }
911 }
912 close($vcf_in);
913  
914 return ($vcf_mutations);
915 }
916  
917 __END__