.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Genome::Model::Tools::Music::Bmr::CalcBmr 3pm" .TH Genome::Model::Tools::Music::Bmr::CalcBmr 3pm "2016-01-01" "perl v5.22.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .IP "\-\-roi\-file" 4 .IX Item "--roi-file" .RS 4 .PD 0 .IP "The regions of interest (ROIs) of each gene are typically regions targeted for sequencing or are merged exon loci (from multiple transcripts) of genes with 2\-bp flanks (splice junctions). ROIs from the same chromosome must be listed adjacent to each other in this file. This allows the underlying C\-based code to run much more efficiently and avoid re-counting bases seen in overlapping ROIs (for overall covered base counts). For per-gene base counts, an overlapping base will be counted each time it appears in an \s-1ROI\s0 of the same gene. To avoid this, be sure to merge together overlapping ROIs of the same gene. BEDtools' mergeBed can help if used per gene." 8 .IX Item "The regions of interest (ROIs) of each gene are typically regions targeted for sequencing or are merged exon loci (from multiple transcripts) of genes with 2-bp flanks (splice junctions). ROIs from the same chromosome must be listed adjacent to each other in this file. This allows the underlying C-based code to run much more efficiently and avoid re-counting bases seen in overlapping ROIs (for overall covered base counts). For per-gene base counts, an overlapping base will be counted each time it appears in an ROI of the same gene. To avoid this, be sure to merge together overlapping ROIs of the same gene. BEDtools' mergeBed can help if used per gene." .RE .RS 4 .RE .IP "\-\-reference\-sequence" 4 .IX Item "--reference-sequence" .RS 4 .IP "The reference sequence in \s-1FASTA\s0 format. If a reference sequence index is not found next to this file (a .fai file), it will be created." 8 .IX Item "The reference sequence in FASTA format. If a reference sequence index is not found next to this file (a .fai file), it will be created." .RE .RS 4 .RE .IP "\-\-bam\-list" 4 .IX Item "--bam-list" .RS 4 .IP "Provide a file containing sample names and normal/tumor \s-1BAM\s0 locations for each. Use the tab\- delimited format [sample_name normal_bam tumor_bam] per line. Additional columns like clinical data are allowed, but ignored. The sample_name must be the same as the tumor sample names used in the \s-1MAF\s0 file (16th column, with the header Tumor_Sample_Barcode)." 8 .IX Item "Provide a file containing sample names and normal/tumor BAM locations for each. Use the tab- delimited format [sample_name normal_bam tumor_bam] per line. Additional columns like clinical data are allowed, but ignored. The sample_name must be the same as the tumor sample names used in the MAF file (16th column, with the header Tumor_Sample_Barcode)." .RE .RS 4 .RE .IP "\-\-bmr\-groups" 4 .IX Item "--bmr-groups" .RS 4 .IP "Ideally, we want to test the mutation rate (\s-1MR\s0) of a gene in a sample against the background mutation rate (\s-1BMR\s0) across that sample. But if the BMRs of some samples are comparable, we can instead test the \s-1MR\s0 of a gene across a group of samples with comparable \s-1BMR,\s0 against the overall \s-1BMR\s0 of that group. This argument specifies how many such groups you want to cluster samples into. By default, it is assumed that all samples have comparable BMRs (bmr-groups = 1)." 8 .IX Item "Ideally, we want to test the mutation rate (MR) of a gene in a sample against the background mutation rate (BMR) across that sample. But if the BMRs of some samples are comparable, we can instead test the MR of a gene across a group of samples with comparable BMR, against the overall BMR of that group. This argument specifies how many such groups you want to cluster samples into. By default, it is assumed that all samples have comparable BMRs (bmr-groups = 1)." .RE .RS 4 .RE .IP "\-\-output\-dir" 4 .IX Item "--output-dir" .RS 4 .ie n .IP "This should be the same output directory used when running ""music bmr calc-covg"". The following outputs of this script will also be created/written: overall_bmrs: File containing categorized overall background mutation rates. gene_mrs: File containing categorized per-gene mutation rates." 8 .el .IP "This should be the same output directory used when running ``music bmr calc-covg''. The following outputs of this script will also be created/written: overall_bmrs: File containing categorized overall background mutation rates. gene_mrs: File containing categorized per-gene mutation rates." 8 .IX Item "This should be the same output directory used when running music bmr calc-covg. The following outputs of this script will also be created/written: overall_bmrs: File containing categorized overall background mutation rates. gene_mrs: File containing categorized per-gene mutation rates." .RE .RS 4 .RE .IP "\-\-genes\-to\-ignore" 4 .IX Item "--genes-to-ignore" .RS 4 .IP "A comma-delimited list of genes to ignore for overall \s-1BMR\s0 calculations. List genes that are known factors in this disease and whose mutations should not be classified as background." 8 .IX Item "A comma-delimited list of genes to ignore for overall BMR calculations. List genes that are known factors in this disease and whose mutations should not be classified as background." .RE .RS 4 .RE .PD .PP \&\s-1EOS \s0); } .PP sub _doc_authors { return \*(L" Cyriac Kandoth, Ph.D.\*(R"; } .PP sub _doc_see_also { return <<\s-1EOS \&\s0\fBgenome-music-bmr\fR(1), \&\fBgenome-music\fR(1), \&\fBgenome\fR(1) \&\s-1EOS \&\s0} .PP sub execute { my \f(CW$self\fR = shift; my \f(CW$roi_file\fR = \f(CW$self\fR\->roi_file; my \f(CW$ref_seq\fR = \f(CW$self\fR\->reference_sequence; my \f(CW$bam_list\fR = \f(CW$self\fR\->bam_list; my \f(CW$output_dir\fR = \f(CW$self\fR\->output_dir; my \f(CW$maf_file\fR = \f(CW$self\fR\->maf_file; my \f(CW$show_skipped\fR = \f(CW$self\fR\->show_skipped; my \f(CW$bmr_groups\fR = \f(CW$self\fR\->bmr_groups; my \f(CW$separate_truncations\fR = \f(CW$self\fR\->separate_truncations; my \f(CW$merge_concurrent_muts\fR = \f(CW$self\fR\->merge_concurrent_muts; my \f(CW$genes_to_ignore\fR = \f(CW$self\fR\->genes_to_ignore; my \f(CW$skip_non_coding\fR = \f(CW$self\fR\->skip_non_coding; my \f(CW$skip_silent\fR = \f(CW$self\fR\->skip_silent; .PP .Vb 7 \& # Check on all the input data before starting work \& print STDERR "ROI file not found or is empty: $roi_file\en" unless( \-s $roi_file ); \& print STDERR "Reference sequence file not found: $ref_seq\en" unless( \-e $ref_seq ); \& print STDERR "List of BAMs not found or is empty: $bam_list\en" unless( \-s $bam_list ); \& print STDERR "Output directory not found: $output_dir\en" unless( \-e $output_dir ); \& print STDERR "MAF file not found or is empty: $maf_file\en" unless( \-s $maf_file ); \& return undef unless( \-s $roi_file && \-e $ref_seq && \-s $bam_list && \-e $output_dir && \-s $maf_file ); \& \& # Check on the files we expect to find within the provided output directory \& $output_dir =~ s/(\e/)+$//; # Remove trailing forward slashes if any \& my $gene_covg_dir = "$output_dir/gene_covgs"; # Should contain per\-gene coverage files per sample \& my $total_covgs_file = "$output_dir/total_covgs"; # Should contain overall coverages per sample \& print STDERR "Directory with per\-gene coverages not found: $gene_covg_dir\en" unless( \-e $gene_covg_dir ); \& print STDERR "Total coverages file not found or is empty: $total_covgs_file\en" unless( \-s $total_covgs_file ); \& return undef unless( \-e $gene_covg_dir && \-s $total_covgs_file ); \& \& # Outputs of this script will be written to these locations in the output directory \& my $overall_bmr_file = "$output_dir/overall_bmrs"; \& my $gene_mr_file = "$output_dir/gene_mrs"; \& $self\->gene_mr_file( $gene_mr_file ); \& \& # Build a hash to quickly lookup the genes to be ignored for overall BMRs \& my %ignored_genes = (); \& if( defined $genes_to_ignore ) { \& %ignored_genes = map { $_ => 1 } split( /,/, $genes_to_ignore ); \& } \& \& # Parse out the names of the samples which should match the names of the coverage files needed \& my ( @all_sample_names, %sample_idx ); \& my $idx = 0; \& my $sampleFh = IO::File\->new( $bam_list ) or die "Couldn\*(Aqt open $bam_list. $!"; \& while( my $line = $sampleFh\->getline ) { \& next if ( $line =~ m/^#/ ); \& chomp( $line ); \& my ( $sample ) = split( /\et/, $line ); \& push( @all_sample_names, $sample ); \& $sample_idx{$sample} = $idx++; \& } \& $sampleFh\->close; \& \& # If the reference sequence FASTA file hasn\*(Aqt been indexed, do it \& my $ref_seq_idx = "$ref_seq.fai"; \& Genome::Sys\->shellcmd( cmd => "samtools faidx $ref_seq" ) unless( \-e $ref_seq_idx ); \& \& # Parse gene names and ROIs. Mutations outside these ROIs will be skipped \& my ( @all_gene_names, %gene_idx ); \& $idx = 0; \& my $roi_bitmask = $self\->create_empty_genome_bitmask( $ref_seq_idx ); \& my $roiFh = IO::File\->new( $roi_file ) or die "Couldn\*(Aqt open $roi_file. $!"; \& while( my $line = $roiFh\->getline ) { \& next if( $line =~ m/^#/ ); \& chomp $line; \& my ( $chr, $start, $stop, $gene ) = split( /\et/, $line ); \& if( !$roi_bitmask\->{$chr} or $start > $roi_bitmask\->{$chr}\->Size ) { \& print STDERR "Skipping invalid ROI bitmask $chr:$start\-$stop\en"; \& next; \& } \& $roi_bitmask\->{$chr}\->Interval_Fill( $start, $stop ); \& unless( defined $gene_idx{$gene} ) { \& push( @all_gene_names, $gene ); \& $gene_idx{$gene} = $idx++; \& } \& } \& $roiFh\->close; \& \& # These are the various categories that each mutation will be classified into \& my @mut_classes = ( AT_Transitions, AT_Transversions, CG_Transitions, CG_Transversions, CpG_Transitions, CpG_Transversions, Indels ); \& push( @mut_classes, Truncations ) if( $separate_truncations ); \& # Save the actual class names for reporting purposes, because the elements above are really just numerical constants \& my @mut_class_names = qw( AT_Transitions AT_Transversions CG_Transitions CG_Transversions CpG_Transitions CpG_Transversions Indels ); \& push( @mut_class_names, \*(AqTruncations\*(Aq ) if( $separate_truncations ); \& \& my @sample_mr; # Stores per sample covg and mutation information \& foreach my $sample ( @all_sample_names ) { \& $sample_mr[$sample_idx{$sample}][$_][mutations] = 0 foreach( @mut_classes ); \& $sample_mr[$sample_idx{$sample}][$_][covd_bases] = 0 foreach( @mut_classes ); \& } \& \& # Load the covered base\-counts per sample from the output of "music bmr calc\-covg" \& print STDERR "Loading per\-sample coverages stored in $total_covgs_file\en"; \& my $sample_cnt_in_total_covgs_file = 0; \& my $totCovgFh = IO::File\->new( $total_covgs_file ) or die "Couldn\*(Aqt open $total_covgs_file. $!"; \& while( my $line = $totCovgFh\->getline ) { \& next unless( $line =~ m/^\eS+\et\ed+\et\ed+\et\ed+\et\ed+$/ and $line !~ m/^#/ ); \& chomp( $line ); \& ++$sample_cnt_in_total_covgs_file; \& my ( $sample, $covd_bases, $covd_at_bases, $covd_cg_bases, $covd_cpg_bases ) = split( /\et/, $line ); \& $sample_mr[$sample_idx{$sample}][AT_Transitions][covd_bases] = $covd_at_bases; \& $sample_mr[$sample_idx{$sample}][AT_Transversions][covd_bases] = $covd_at_bases; \& $sample_mr[$sample_idx{$sample}][CG_Transitions][covd_bases] = $covd_cg_bases; \& $sample_mr[$sample_idx{$sample}][CG_Transversions][covd_bases] = $covd_cg_bases; \& $sample_mr[$sample_idx{$sample}][CpG_Transitions][covd_bases] = $covd_cpg_bases; \& $sample_mr[$sample_idx{$sample}][CpG_Transversions][covd_bases] = $covd_cpg_bases; \& $sample_mr[$sample_idx{$sample}][Indels][covd_bases] = $covd_bases; \& $sample_mr[$sample_idx{$sample}][Truncations][covd_bases] = $covd_bases if( $separate_truncations ); \& } \& $totCovgFh\->close; \& \& unless( $sample_cnt_in_total_covgs_file == scalar( @all_sample_names )) { \& print STDERR "Mismatching number of samples in $total_covgs_file and $bam_list\en"; \& return undef; \& } \& \& my @gene_mr; # Stores per gene covg and mutation information \& foreach my $gene ( @all_gene_names ) { \& foreach my $sample ( @all_sample_names ) { \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$_][mutations] = 0 foreach( @mut_classes ); \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$_][covd_bases] = 0 foreach( @mut_classes ); \& } \& } \& \& # Sum up the per\-gene covered base\-counts across samples from the output of "music bmr calc\-covg" \& print STDERR "Loading per\-gene coverage files stored under $gene_covg_dir/\en"; \& foreach my $sample ( @all_sample_names ) { \& my $sample_covg_file = "$gene_covg_dir/$sample.covg"; \& my $sampleCovgFh = IO::File\->new( $sample_covg_file ) or die "Couldn\*(Aqt open $sample_covg_file. $!"; \& while( my $line = $sampleCovgFh\->getline ) { \& next unless( $line =~ m/^\eS+\et\ed+\et\ed+\et\ed+\et\ed+\et\ed+$/ and $line !~ m/^#/ ); \& chomp( $line ); \& my ( $gene, undef, $covd_bases, $covd_at_bases, $covd_cg_bases, $covd_cpg_bases ) = split( /\et/, $line ); \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][AT_Transitions][covd_bases] += $covd_at_bases; \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][AT_Transversions][covd_bases] += $covd_at_bases; \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][CG_Transitions][covd_bases] += $covd_cg_bases; \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][CG_Transversions][covd_bases] += $covd_cg_bases; \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][CpG_Transitions][covd_bases] += $covd_cpg_bases; \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][CpG_Transversions][covd_bases] += $covd_cpg_bases; \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][Indels][covd_bases] += $covd_bases; \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][Truncations][covd_bases] += $covd_bases if( $separate_truncations ); \& } \& $sampleCovgFh\->close; \& } \& \& # Run "joinx ref\-stats" to classify SNVs as being at AT, CG, or CpG sites in the reference \& print STDERR "Running \*(Aqjoinx ref\-stats\*(Aq to read reference FASTA and identify SNVs at AT, CG, CpG sites\en"; \& my $maf_bed = Genome::Sys\->create_temp_file_path(); \& my $mafBedFh = IO::File\->new( $maf_bed, ">" ) or die "Temporary file could not be created. $!"; \& my $mafFh = IO::File\->new( $maf_file ) or die "Couldn\*(Aqt open $maf_file. $!"; \& while( my $line = $mafFh\->getline ) { \& next if( $line =~ m/^(#|Hugo_Symbol)/ ); \& chomp $line; \& my @cols = split( /\et/, $line ); \& my ( $chr, $start, $stop, $mutation_type, $ref, $var1, $var2 ) = @cols[4..6,9..12]; \& if( $mutation_type =~ m/^(SNP|DNP|ONP|TNP)$/ ) { \& $mafBedFh\->print( "$chr\et" . ( $start \- 2 ) . "\et" . ( $start + 1 ) . "\en" ); \& } \& } \& $mafFh\->close; \& $mafBedFh\->close; \& my $refstats_file = Genome::Sys\->create_temp_file_path(); \& Genome::Sys\->shellcmd( cmd => "joinx ref\-stats \-\-ref\-bases \-\-bed $maf_bed \-\-fasta $ref_seq \-\-output $refstats_file" ); \& \& # Parse through the ref\-stats output and load it into hashes for quick lookup later \& my ( %ref_base, %cpg_site ); \& my $refStatsFh = IO::File\->new( $refstats_file ) or die "Couldn\*(Aqt open $refstats_file. $!"; \& while( my $line = $refStatsFh\->getline ) { \& next if( $line =~ m/^#/ ); \& chomp $line; \& my ( $chr, undef, $pos, undef, undef, undef, $ref ) = split( /\et/, $line ); \& my $locus = "$chr\et" . ( $pos \- 1 ); \& $ref_base{$locus} = substr( $ref, 1, 1 ); \& $cpg_site{$locus} = 1 if( $ref =~ m/CG/ ); \& } \& $refStatsFh\->close; \& \& # Create a hash to help classify SNVs \& my %classify; \& $classify{$_} = AT_Transitions foreach( qw( AG TC )); \& $classify{$_} = AT_Transversions foreach( qw( AC AT TA TG )); \& $classify{$_} = CG_Transitions foreach( qw( CT GA )); \& $classify{$_} = CG_Transversions foreach( qw( CA CG GC GT )); \& \& # Parse through the MAF file and categorize each somatic mutation \& print STDERR "Parsing MAF file to classify mutations\en"; \& my %skip_cnts; \& $mafFh = IO::File\->new( $maf_file ) or die "Couldn\*(Aqt open $maf_file. $!"; \& while( my $line = $mafFh\->getline ) { \& next if( $line =~ m/^(#|Hugo_Symbol)/ ); \& chomp $line; \& my @cols = split( /\et/, $line ); \& my ( $gene, $chr, $start, $stop, $mutation_class, $mutation_type, $ref, $var1, $var2, $sample ) = @cols[0,4..6,8..12,15]; \& \& # Skip mutations in samples that are not in the provided bam list \& unless( defined $sample_idx{$sample} ) { \& $skip_cnts{"belong to unrecognized samples"}++; \& print STDERR "Skipping unrecognized sample ($sample not in BAM list): $gene, $chr:$start\-$stop\en" if( $show_skipped ); \& next; \& } \& \& # If the mutation classification is odd, quit with error \& if( $mutation_class !~ m/^(Missense_Mutation|Nonsense_Mutation|Nonstop_Mutation|Splice_Site|Translation_Start_Site|Frame_Shift_Del|Frame_Shift_Ins|In_Frame_Del|In_Frame_Ins|Silent|Intron|RNA|3\*(AqFlank|3\*(AqUTR|5\*(AqFlank|5\*(AqUTR|IGR|Targeted_Region|De_novo_Start_InFrame|De_novo_Start_OutOfFrame)$/ ) { \& print STDERR "Unrecognized Variant_Classification \e"$mutation_class\e" in MAF file: $gene, $chr:$start\-$stop\en"; \& print STDERR "Please use TCGA MAF Specification v2.3.\en"; \& return undef; \& } \& \& # If user wants, skip Silent mutations, or those in Introns, RNA, UTRs, Flanks, IGRs, or the ubiquitous Targeted_Region \& if(( $skip_non_coding && $mutation_class =~ m/^(Intron|RNA|3\*(AqFlank|3\*(AqUTR|5\*(AqFlank|5\*(AqUTR|IGR|Targeted_Region)$/ ) || \& ( $skip_silent && $mutation_class =~ m/^Silent$/ )) { \& $skip_cnts{"are classified as $mutation_class"}++; \& print STDERR "Skipping $mutation_class mutation: $gene, $chr:$start\-$stop\en" if( $show_skipped ); \& next; \& } \& \& # If the mutation type is odd, quit with error \& if( $mutation_type !~ m/^(SNP|DNP|TNP|ONP|INS|DEL|Consolidated)$/ ) { \& print STDERR "Unrecognized Variant_Type \e"$mutation_type\e" in MAF file: $gene, $chr:$start\-$stop\en"; \& print STDERR "Please use TCGA MAF Specification v2.3.\en"; \& return undef; \& } \& \& # Skip mutations that were consolidated into others (E.g. SNP consolidated into a TNP) \& if( $mutation_type =~ m/^Consolidated$/ ) { \& $skip_cnts{"are consolidated into another"}++; \& print STDERR "Skipping consolidated mutation: $gene, $chr:$start\-$stop\en" if( $show_skipped ); \& next; \& } \& \& # Skip mutations that fall completely outside any of the provided regions of interest \& if( $self\->count_bits( $roi_bitmask\->{$chr}, $start, $stop ) == 0 ) { \& $skip_cnts{"are outside any ROIs"}++; \& print STDERR "Skipping mutation that falls outside ROIs: $gene, $chr:$start\-$stop\en" if( $show_skipped ); \& next; \& } \& \& # Skip mutations whose gene names don\*(Aqt match any of those in the ROI list \& unless( defined $gene_idx{$gene} ) { \& $skip_cnts{"have unrecognized gene names"}++; \& print STDERR "Skipping unrecognized gene name (not in ROI file): $gene, $chr:$start\-$stop\en" if( $show_skipped ); \& next; \& } \& \& my $class = \*(Aq\*(Aq; \& # Check if the mutation is the truncating type, if the user wanted a separate category of those \& if( $separate_truncations && $mutation_class =~ m/^(Nonsense_Mutation|Splice_Site|Frame_Shift_Del|Frame_Shift_Ins)/ ) { \& $class = Truncations; \& } \& # Classify the mutation as AT/CG/CpG Transition, AT/CG/CpG Transversion \& elsif( $mutation_type =~ m/^(SNP|DNP|ONP|TNP)$/ ) { \& # ::TBD:: For DNPs and TNPs, we use only the first base for mutation classification \& $ref = substr( $ref, 0, 1 ); \& $var1 = substr( $var1, 0, 1 ); \& $var2 = substr( $var2, 0, 1 ); \& \& # If the alleles are anything but A, C, G, or T then quit with error \& if( $ref !~ m/[ACGT]/ || $var1 !~ m/[ACGT]/ || $var2 !~ m/[ACGT]/ ) { \& print STDERR "Unrecognized allele in column Reference_Allele, Tumor_Seq_Allele1, or Tumor_Seq_Allele2: $gene, $chr:$start\-$stop\en"; \& print STDERR "Please use TCGA MAF Specification v2.3.\en"; \& return undef; \& } \& \& # Use the classify hash to find whether this SNV is an AT/CG Transition/Transversion \& $class = $classify{ "$ref$var1" } if( defined $classify{ "$ref$var1" } ); \& $class = $classify{ "$ref$var2" } if( defined $classify{ "$ref$var2" } ); \& \& # Check if the ref base in the MAF matched what we fetched from the ref\-seq \& my $locus = "$chr\et$start"; \& if( defined $ref_base{$locus} && $ref_base{$locus} ne $ref ) { \& print STDERR "Reference allele $ref for $gene variant at $chr:$start\-$stop is " . $ref_base{$locus} . " in the FASTA. Using it anyway.\en"; \& } \& \& # Check if a C or G reference allele belongs to a CpG pair in the refseq \& if(( $ref eq \*(AqC\*(Aq || $ref eq \*(AqG\*(Aq ) && defined $cpg_site{$locus} ) { \& $class = (( $class == CG_Transitions ) ? CpG_Transitions : CpG_Transversions ); \& } \& } \& # Classify it as an indel (excludes splice\-site and frame\-shift if user wanted truncations separately) \& elsif( $mutation_type =~ m/^(INS|DEL)$/ ) { \& $class = Indels; \& } \& \& # The user\*(Aqs gene exclusion list affects only the overall BMR calculations \& $sample_mr[$sample_idx{$sample}][$class][mutations]++ unless( defined $ignored_genes{$gene} ); \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$class][mutations]++; \& } \& $mafFh\->close; \& \& # Display statistics related to parsing the MAF \& print STDERR "Finished Parsing the MAF file to classify mutations\en"; \& foreach my $skip_type ( sort {$skip_cnts{$b} <=> $skip_cnts{$a}} keys %skip_cnts ) { \& print STDERR "Skipped " . $skip_cnts{$skip_type} . " mutation(s) that $skip_type\en" if( defined $skip_cnts{$skip_type} ); \& } \& \& # If the user wants, merge together concurrent mutations of a gene in the same sample \& if( $merge_concurrent_muts ) { \& foreach my $sample ( @all_sample_names ) { \& foreach my $gene ( @all_gene_names ) { \& next unless( defined $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}] ); \& my $num_muts = 0; \& $num_muts += $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$_][mutations] foreach( @mut_classes ); \& if( $num_muts > 1 ) { \& foreach my $class ( @mut_classes ) { \& my $muts_in_class = $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$class][mutations]; # Num of muts of gene in this class \& $sample_mr[$sample_idx{$sample}][$class][mutations] \-= $muts_in_class; # Take it out of the sample total \& $muts_in_class /= $num_muts; # Turn it into a fraction of the total number of muts in this gene \& $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$class][mutations] = $muts_in_class; # Use the fraction as the num muts of gene in this class \& $sample_mr[$sample_idx{$sample}][$class][mutations] += $muts_in_class; # Add the same fraction to the sample total \& } \& } \& } \& } \& } \& \& # Calculate per\-sample BMRs, and also subtract out covered bases in genes the user wants ignored \& foreach my $sample ( @all_sample_names ) { \& my $tot_muts = 0; \& foreach my $class ( @mut_classes ) { \& # Subtract the covered bases in this class that belong to the genes to be ignored \& # ::TBD:: Some of these bases may also belong to another gene (on the other strand maybe?), and those should not be subtracted \& foreach my $ignored_gene ( keys %ignored_genes ) { \& $sample_mr[$sample_idx{$sample}][$class][covd_bases] \-= $gene_mr[$sample_idx{$sample}][$gene_idx{$ignored_gene}][$class][covd_bases] if( defined $gene_mr[$sample_idx{$sample}][$gene_idx{$ignored_gene}] ); \& } \& $tot_muts += $sample_mr[$sample_idx{$sample}][$class][mutations]; \& } \& $sample_mr[$sample_idx{$sample}][Overall][bmr] = $tot_muts / $sample_mr[$sample_idx{$sample}][Indels][covd_bases]; \& } \& \& # Cluster samples into bmr\-groups using k\-means clustering \& my @sample_bmrs = map { $sample_mr[$sample_idx{$_}][Overall][bmr] } @all_sample_names; \& my @bmr_clusters = k_means( $bmr_groups, \e@sample_bmrs ); \& \& # Calculate overall BMRs for each cluster of samples, and print them to file \& my %cluster_bmr; # Stores per cluster categorized BMR \& my $totBmrFh = IO::File\->new( $overall_bmr_file, ">" ) or die "Couldn\*(Aqt open $overall_bmr_file. $!"; \& $totBmrFh\->print( "#User\-specified genes skipped in these calculations: $genes_to_ignore\en" ) if( defined $genes_to_ignore ); \& my ( $covered_bases_sum, $mutations_sum ) = ( 0, 0 ); \& for( my $i = 0; $i < scalar( @bmr_clusters ); ++$i ) { \& my @samples_in_cluster = map { $all_sample_names[$_] } @{$bmr_clusters[$i]}; \& unless( $bmr_groups == 1 ) { \& $totBmrFh\->print( "#BMR sub\-group ", $i + 1, " (", scalar( @{$bmr_clusters[$i]} ), " samples)\en" ); \& $totBmrFh\->print( "#Samples: ", join( ",", @samples_in_cluster ), "\en" ); \& } \& $totBmrFh\->print( "#Mutation_Class\etCovered_Bases\etMutations\etOverall_BMR\en" ); \& \& my ( $tot_covd_bases, $tot_muts ) = ( 0, 0 ); \& foreach my $class ( @mut_classes ) { \& my ( $covd_bases, $mutations ) = ( 0, 0 ); \& foreach my $sample ( @samples_in_cluster ) { \& $covd_bases += $sample_mr[$sample_idx{$sample}][$class][covd_bases]; \& $mutations += $sample_mr[$sample_idx{$sample}][$class][mutations]; \& } \& $tot_covd_bases = $covd_bases if( $class == Indels ); # Save this to calculate overall BMR below \& # Calculate overall BMR for this mutation class and print it to file \& $cluster_bmr{$i}[$class][bmr] = ( $covd_bases == 0 ? 0 : ( $mutations / $covd_bases )); \& $totBmrFh\->print( join( "\et", $mut_class_names[$class], $covd_bases, $mutations, $cluster_bmr{$i}[$class][bmr] ), "\en" ); \& $tot_muts += $mutations; \& } \& $totBmrFh\->print( join( "\et", "Overall_BMR", $tot_covd_bases, $tot_muts, $tot_muts / $tot_covd_bases ), "\en\en" ); \& $covered_bases_sum += $tot_covd_bases; \& $mutations_sum += $tot_muts; \& } \& $totBmrFh\->close; \& $self\->bmr_output( $mutations_sum / $covered_bases_sum ); \& \& # Print out a file containing per\-gene mutation counts and covered bases for use by "music smg" \& my $geneBmrFh = IO::File\->new( $gene_mr_file, ">" ) or die "Couldn\*(Aqt open $gene_mr_file. $!"; \& $geneBmrFh\->print( "#Gene\etMutation_Class\etCovered_Bases\etMutations\etBMR\en" ); \& foreach my $gene ( sort @all_gene_names ) { \& my ( $tot_covd_bases, $tot_muts ) = ( 0, 0 ); \& for( my $i = 0; $i < scalar( @bmr_clusters ); ++$i ) { \& my @samples_in_cluster = map { $all_sample_names[$_] } @{$bmr_clusters[$i]}; \& foreach my $class ( @mut_classes ) { \& my ( $covd_bases, $mutations ) = ( 0, 0 ); \& foreach my $sample( @samples_in_cluster ) { \& if( defined $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}] ) { \& $covd_bases += $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$class][covd_bases]; \& $mutations += $gene_mr[$sample_idx{$sample}][$gene_idx{$gene}][$class][mutations]; \& } \& } \& my $rename_class = $mut_class_names[$class]; \& $rename_class = ( $rename_class . "_SubGroup" . ( $i + 1 )) if( $bmr_groups > 1 ); \& $geneBmrFh\->print( join( "\et", $gene, $rename_class, $covd_bases, $mutations, $cluster_bmr{$i}[$class][bmr] ), "\en" ); \& $tot_muts += $mutations; \& $tot_covd_bases += $covd_bases if( $class == Indels ); \& } \& } \& $geneBmrFh\->print( join( "\et", $gene, "Overall", $tot_covd_bases, $tot_muts, $self\->bmr_output ), "\en" ); \& } \& $geneBmrFh\->close; \& \& return 1; \&} .Ve .PP # Creates an empty whole genome bitmask based on the given reference sequence index sub create_empty_genome_bitmask { my ( \f(CW$self\fR, \f(CW$ref_seq_idx_file\fR ) = \f(CW@_\fR; my \f(CW%genome\fR; my \f(CW$refFh\fR = IO::File\->new( \f(CW$ref_seq_idx_file\fR ) or die \*(L"Couldn't open \f(CW$ref_seq_idx_file\fR. $!\*(R"; while( my \f(CW$line\fR = \f(CW$refFh\fR\->getline ) { my ( \f(CW$chr\fR, \f(CW$length\fR ) = split( /\et/, \f(CW$line\fR ); \f(CW$genome\fR{$chr} = Bit::Vector\->new( \f(CW$length\fR + 1 ); # Adding a base for 1\-based coordinates } \f(CW$refFh\fR\->close; return \e%genome; } .PP # Counts the number of bits that are set in the given region of a Bit:Vector sub count_bits { my ( \f(CW$self\fR, \f(CW$vector\fR, \f(CW$start\fR, \f(CW$stop\fR ) = \f(CW@_\fR; my \f(CW$count\fR = 0; for my \f(CW$pos\fR ( \f(CW$start\fR..$stop ) { ++$count if( \f(CW$vector\fR\->bit_test( \f(CW$pos\fR )); } return \f(CW$count\fR; } .PP # Given a list of numerical values, returns k clusters based on k\-means clustering sub k_means { my ( \f(CW$k\fR, \f(CW$list_ref\fR ) = \f(CW@_\fR; my \f(CW@vals\fR = @{$list_ref}; my \f(CW$num_vals\fR = scalar( \f(CW@vals\fR ); .PP .Vb 4 \& # Start with the first k values as the centroids \& my @centroids = @vals[0..($k\-1)]; \& my @prev_centroids = map { 0 } @centroids; \& my @groups = (); \& \& my $diff_means = 1; # Arbitrary non\-zero value \& # Repeat until the difference between these centroids and the previous ones, converges to zero \& while( $diff_means > 0 ) { \& @groups = (); \& # Group values into clusters based on closest centroid \& for( my $i = 0; $i < $num_vals; ++$i ) { \& my @distances = map { abs( $vals[$i] \- $_ ) } @centroids; \& my $closest = min( @distances ); \& for( my $j = 0; $j < $k; ++$j ) { \& if( $distances[$j] == $closest ) { push( @{$groups[$j]}, $i ); last; } \& } \& } \& \& # Calculate means to be the new centroids, and the sum of differences \& $diff_means = 0; \& for( my $i = 0; $i < $k; ++$i ) { \& $centroids[$i] = sum( map {$vals[$_]} @{$groups[$i]} ); \& $centroids[$i] /= scalar( @{$groups[$i]} ); \& $diff_means += abs( $centroids[$i] \- $prev_centroids[$i] ); \& } \& \& # Save the current centroids for comparisons with those in the next iteration \& @prev_centroids = @centroids; \& } \& return @groups; \&} .Ve .PP 1;