Debian Codegraph @

lib/Genome/Model/Tools/Music/Smg.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package Genome::Model::Tools::Music::Smg;
2
3 use warnings;
4 use strict;
5 use Genome;
6 use IO::File;
7 use Carp;
8 use POSIX qw( WIFEXITED );
9
10 our $VERSION = $Genome::Model::Tools::Music::VERSION;
11
12 class Genome::Model::Tools::Music::Smg {
13 is => 'Command::V2',
14 has_input => [
15 gene_mr_file => { is => 'Text', doc => "File with per-gene mutation rates (Created using \"music bmr calc-bmr\")" },
16 output_file => { is => 'Text', is_output => 1, doc => "Output file that will list significantly mutated genes and their p-values" },
17 ],
18 has_optional_input => [
19 max_fdr => { is => 'Number', default => 0.20, doc => "The maximum allowed false discovery rate for a gene to be considered an SMG" },
20 skip_low_mr_genes => { is => 'Boolean', default => 1, doc => "Skip testing genes with MRs lower than the background MR" },
21 bmr_modifier_file => { is => 'Text', doc => "Tab delimited multipliers per gene that modify BMR before testing [gene_name bmr_modifier]" },
22 processors => { is => 'Integer', default => 1, doc => "Number of processors to use (requires 'foreach' and 'doMC' R packages)" },
23 ],
24 doc => "Identify significantly mutated genes."
25 };
26
27 sub help_synopsis {
28 return <<HELP
29 ... music smg \\
30 --gene-mr-file output_dir/gene_mrs \\
31 --output-file output_dir/smgs
32
33 (A "gene-mr-file" can be generated using the tool "music bmr calc-bmr".)
34 HELP
35 }
36
37 sub help_detail {
38 return <<HELP
39 This script runs R-based statistical tools to identify Significantly Mutated Genes (SMGs), when
40 given per-gene mutation rates categorized by mutation type, and the overall background mutation
41 rates (BMRs) for each of those categories (gene_mr_file, created using "music bmr calc-bmr").
42
43 P-values and false discovery rates (FDRs) for each gene in gene_mr_file is calculated using three
44 tests: Fisher\'s Combined P-value test (FCPT), Likelihood Ratio test (LRT), and the Convolution
45 test (CT). For a gene, if its FDR for at least 2 of these tests is <= max_fdr, it will be output
46 as an SMG. Another output file with prefix "_detailed" will have p-values and FDRs for all genes.
47 HELP
48 }
49
50 sub _additional_help_sections {
51 return (
52 "ARGUMENTS",
53 <<EOS
54
55 =over 4
56
57 =item --bmr-modifier-file
58
59 =over 8
60
61 =item The user can provide a BMR modifier for each gene in the ROI file, which is a multiplier for
62 the categorized background mutation rates, before testing them against the gene's categorized
63 mutation rates. Such a file can be used to correct for regional or systematic bias in mutation
64 rates across the genome that may be correlated to CpG deamination or DNA repair processes like
65 transcription-coupled repair or mismatch repair. Mutation rates have also been associated with
66 DNA replication timing, where higher mutation rates are seen in late replicating regions. Note
67 that the same per-gene multiplier is used on each mutation category of BMR. Any genes from the
68 ROI file that are not in the BMR modifier file will be tested against unmodified overall BMRs
69 per mutation category. BMR modifiers of <=0 are not permitted, because that's just silly.
70
71 =back
72
73 =item --skip-low-mr-genes
74
75 =over 8
76
77 =item Genes with consistently lower MRs than the BMRs across mutation categories, may show up in
78 the results as an SMG (by CT or LRT). If such genes are not of interest, they may be assigned a
79 p-value of 1. This should also speed things up. Genes with higher Indel or Truncation rates than
80 the background will not be skipped even if the gene's overall MR is lower than the BMR. If
81 bmr-modifiers are applied, this step uses the modified BMRs instead.
82
83 =back
84
85 =back
86 EOS
87 );
88 }
89
90 sub _doc_authors {
91 return <<EOS
92 Qunyuan Zhang, Ph.D.
93 Cyriac Kandoth, Ph.D.
94 Nathan D. Dees, Ph.D.
95 EOS
96 }
97
98 sub execute {
99 my $self = shift;
100 my $gene_mr_file = $self->gene_mr_file;
101 my $output_file = $self->output_file;
102 my $output_file_detailed = $output_file . "_detailed";
103 my $max_fdr = $self->max_fdr;
104 my $skip_low_mr_genes = $self->skip_low_mr_genes;
105 my $bmr_modifier_file = $self->bmr_modifier_file;
106 my $processors = $self->processors;
107
108 # Check on all the input data before starting work
109 print STDERR "Gene mutation rate file not found or is empty: $gene_mr_file\n" unless( -s $gene_mr_file );
110 print STDERR "BMR modifier file not found or is empty: $bmr_modifier_file\n" unless( !defined $bmr_modifier_file || -s $bmr_modifier_file );
111 return undef unless( -s $gene_mr_file && ( !defined $bmr_modifier_file || -s $bmr_modifier_file ));
112
113 # If BMR modifiers were provided, then load them, and create another gene_mr_file with modified BMRs
114 if( defined $bmr_modifier_file ) {
115 my $inBmrModFh = IO::File->new( $bmr_modifier_file ) or die "Couldn't open $bmr_modifier_file. $!\n";
116 my %bmr_modifier = ();
117 while( my $line = $inBmrModFh->getline ) {
118 next if( $line =~ m/^#/ );
119 chomp( $line );
120 my ( $gene, $modifier ) = split( /\t/, $line );
121 ( $modifier > 0 ) or die "$modifier is an invalid bmr-modifier. Please fix values in $bmr_modifier_file.\n";
122 $bmr_modifier{$gene} = $modifier;
123 }
124 $inBmrModFh->close;
125
126 my $new_gene_mr_file = Genome::Sys->create_temp_file_path;
127 ( $new_gene_mr_file ) or die "Couldn't create a temp file. $!";
128 my $inMrFh = IO::File->new( $gene_mr_file ) or die "Couldn't open $gene_mr_file. $!\n";
129 my $outMrFh = IO::File->new( $new_gene_mr_file, ">" ) or die "Couldn't open $new_gene_mr_file. $!\n";
130 while( my $line = $inMrFh->getline ) {
131 if( $line =~ m/^#/ ) {
132 $outMrFh->print( $line );
133 next;
134 }
135 chomp( $line );
136 my ( $gene, $type, $covd_bps, $mut_cnt, $bmr ) = split( /\t/, $line );
137 $bmr = $bmr * $bmr_modifier{$gene} if( defined $bmr_modifier{$gene} );
138 $outMrFh->print( "$gene\t$type\t$covd_bps\t$mut_cnt\t$bmr\n" );
139 }
140 $outMrFh->close;
141 $inMrFh->close;
142
143 $gene_mr_file = $new_gene_mr_file;
144 }
145
146 # Collect per-gene mutation rates for reporting in results later
147 my ( %gene_muts, %gene_bps, %mut_classes_hash );
148 my $inMrFh = IO::File->new( $gene_mr_file ) or die "Couldn't open $gene_mr_file. $!\n";
149 while( my $line = $inMrFh->getline ) {
150 next if( $line =~ m/^#/ );
151 my ( $gene, $type, $covd_bps, $mut_cnt, undef ) = split( /\t/, $line );
152
153 # Warn user about cases where there could be fewer covered bps than mutations detected
154 ( $mut_cnt <= $covd_bps ) or warn "More $type seen in $gene than there are bps with sufficient coverage!\n";
155
156 if( $type eq "Overall" or $type eq "Indels" or $type eq "Truncations" ) {
157 $gene_muts{$gene}{$type} = $mut_cnt;
158 $gene_bps{$gene} = $covd_bps;
159 $mut_classes_hash{$type} = 1 unless( $type eq "Overall" );
160 }
161 elsif( $type =~ m/(Transitions|Transversions)$/ ) {
162 $gene_muts{$gene}{SNVs} += $mut_cnt;
163 $mut_classes_hash{SNVs} = 1;
164 }
165 else {
166 die "Unrecognized mutation class in gene-mr-file. $!\n";
167 }
168 }
169 $inMrFh->close;
170 my @mut_classes = sort keys %mut_classes_hash;
171
172 # Create a temporary intermediate file to hold the p-values
173 my $pval_file = Genome::Sys->create_temp_file_path;
174 ( $pval_file ) or die "Couldn't create a temp file. $!";
175
176 # Call R for Fisher combined test, Likelihood ratio test, and convolution test on each gene
177 my $smg_cmd = "R --slave --args < " . __FILE__ . ".R $gene_mr_file $pval_file smg_test $processors $skip_low_mr_genes";
178 WIFEXITED( system $smg_cmd ) or croak "Couldn't run: $smg_cmd ($?)";
179
180 # Call R for calculating FDR on the p-values calculated in the SMG test
181 my $fdr_cmd = "R --slave --args < " . __FILE__ . ".R $pval_file $output_file_detailed calc_fdr $processors $skip_low_mr_genes";
182 WIFEXITED( system $fdr_cmd ) or croak "Couldn't run: $fdr_cmd ($?)";
183
184 # Parse the R output to identify the SMGs (significant by at least 2 of 3 tests)
185 my $smgFh = IO::File->new( $output_file_detailed ) or die "Couldn't open $output_file_detailed. $!\n";
186 my ( @newLines, @smgLines );
187 my $header = "#Gene\t" . join( "\t", @mut_classes );
188 $header .= "\tTot Muts\tCovd Bps\tMuts pMbp\tP-value FCPT\tP-value LRT\tP-value CT\tFDR FCPT\tFDR LRT\tFDR CT\n";
189 while( my $line = $smgFh->getline ) {
190 chomp( $line );
191 if( $line =~ m/^Gene\tp.fisher\tp.lr\tp.convol\tfdr.fisher\tfdr.lr\tfdr.convol$/ ) {
192 push( @newLines, $header );
193 push( @smgLines, $header );
194 }
195 else {
196 my ( $gene, @pq_vals ) = split( /\t/, $line );
197 my ( $p_fcpt, $p_lrt, $p_ct, $q_fcpt, $q_lrt, $q_ct ) = @pq_vals;
198 my @mut_cnts;
199 foreach( @mut_classes ) {
200 # If a mutation count is a fraction, round down the digits after the decimal point
201 push( @mut_cnts, (( $gene_muts{$gene}{$_} =~ m/\./ ) ? sprintf( "%.2f", $gene_muts{$gene}{$_} ) : $gene_muts{$gene}{$_} ));
202 }
203 my $mut_per_mbp = ( $gene_bps{$gene} ? sprintf( "%.2f", ( $gene_muts{$gene}{Overall} / $gene_bps{$gene} * 1000000 )) : 0 );
204 push( @newLines, join( "\t", $gene, @mut_cnts, $gene_muts{$gene}{Overall}, $gene_bps{$gene}, $mut_per_mbp, @pq_vals ) . "\n" );
205
206 # If the FDR of at least two of these tests is less than the maximum allowed, we consider it an SMG
207 if(( $q_fcpt <= $max_fdr && $q_lrt <= $max_fdr ) || ( $q_fcpt <= $max_fdr && $q_ct <= $max_fdr ) ||
208 ( $q_lrt <= $max_fdr && $q_ct <= $max_fdr )) {
209 push( @smgLines, join( "\t", $gene, @mut_cnts, $gene_muts{$gene}{Overall}, $gene_bps{$gene}, $mut_per_mbp, @pq_vals ) . "\n" );
210 }
211 }
212 }
213 $smgFh->close;
214
215 # Add per-gene SNV and Indel counts to the detailed R output, and make the header friendlier
216 my $outDetFh = IO::File->new( $output_file_detailed, ">" ) or die "Couldn't open $output_file_detailed. $!\n";
217 $outDetFh->print( @newLines );
218 $outDetFh->close;
219
220 # Do the same for only the genes that we consider SMGs
221 my $outFh = IO::File->new( $output_file, ">" ) or die "Couldn't open $output_file. $!\n";
222 $outFh->print( @smgLines );
223 $outFh->close;
224
225 return 1;
226 }
227
228 1;