Debian Codegraph @

lib/Genome/Model/Tools/Music/Survival.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::Survival;
2
3 use warnings;
4 use strict;
5 use Carp;
6 use Genome;
7 use IO::File;
8 use POSIX qw( WIFEXITED );
9
10 our $VERSION = $Genome::Model::Tools::Music::VERSION;
11
12 class Genome::Model::Tools::Music::Survival {
13 is => 'Genome::Model::Tools::Music::Base',
14 has_input => [
15 bam_list => {
16 is => 'Text',
17 doc => "List of sample names to be included in the analysis. (See Description)",
18 },
19 maf_file => {
20 is => 'Text', is_optional => 1,
21 doc => "List of mutations in MAF format",
22 },
23 output_dir => {
24 is => 'Text', is_output => 1,
25 doc => "Directory where output files will be written",
26 },
27 genetic_data_type => {
28 is => 'Text', is_optional => 1, default => "gene",
29 doc => "Correlate clinical data to \"gene\" or \"variant\" level data",
30 },
31 numeric_clinical_data_file => {
32 is => 'Text', is_optional => 1,
33 doc => "Table of samples (y) vs. numeric clinical data category (x)",
34 },
35 categorical_clinical_data_file => {
36 is => 'Text', is_optional => 1,
37 doc => "Table of samples (y) vs. categorical clinical data category (x)",
38 },
39 glm_clinical_data_file => {
40 is => 'Text', is_optional => 1,
41 doc => "Clinical traits, mutational profiles, other mixed clinical data (See DESCRIPTION).",
42 },
43 phenotypes_to_include => {
44 is => 'Text', is_optional => 1,
45 doc => "Include only these genes and/or phenotypes in the anlaysis. (COMMA-DELIMITED)",
46 },
47 legend_placement => {
48 is => 'Text', is_optional => 1, default => 'bottomleft',
49 doc => "Choose one of 'bottomleft', 'topleft', 'topright', or 'bottomright'.",
50 },
51 skip_non_coding => {
52 is => 'Boolean', is_optional => 1, default => 1,
53 doc => "Skip non-coding mutations from the provided MAF file",
54 },
55 skip_silent => {
56 is => 'Boolean', is_optional => 1, default => 1,
57 doc => "Skip silent mutations from the provided MAF file",
58 },
59 ],
60 doc => "Create survival plots and P-values for clinical and mutational phenotypes.",
61 };
62
63 sub help_synopsis {
64 return <<HELP
65 ... music survival \\
66 --bam-list /path/myBamList.tsv \\
67 --maf-file /path/myMAF.tsv \\
68 --numeric-clinical-data-file /path/myNumericData.tsv \\
69 --categorical-clinical-data-file /path/myClassData.tsv \\
70 --output-dir /path/output_directory
71
72 ... music survival \\
73 --bam-list /path/myBamList.tsv \\
74 --maf-file /path/myMAF.tsv \\
75 --glm-clinical-data-file /path/myGLMClinicalData.tsv \\
76 --output-dir /path/output_directory
77
78 ... music survival \\
79 --bam-list /path/myBamList.tsv \\
80 --maf-file /path/myMAF.tsv \\
81 --genetic-data-type 'gene' \\
82 --glm-clinical-data-file /path/myGlmClinicalData.tsv \\
83 --phenotypes-to-include 'Race,Gender,TP53' \\
84 --output-dir /path/output_directory
85
86 HELP
87 }
88
89 sub help_detail {
90 return <<HELP
91
92 This command performs survival analysis and plots survival curves for mutational data, as well as any clinical traits of interest as specified via the --phenotypes-to-include input parameter. The analyses performed include the Kaplan-Meier estimator followed by the Cox Proportional Hazards model. Outputs for each gene/clinical trait analyzed include survival curves, a hazard ratio (with confidence intervals), and P-values and FDRs describing the significance of the difference between survivors and non-survivors.
93
94 All clinical data files are searched for the required (case insensitive) "vital_status" and "days_to_last_followup" columns which are paired to phenotypes via sample IDs for the survival analysis. The first column of all clinical data files MUST contain the sample IDs, same as in other MuSiC tools. By default, analysis is performed on every gene present in the MAF. Optionally, the analysis may be limited to only specific genes by listing them (comma delimited) after the --phenotypes-to-include input parameter. Survival analysis may also be performed on other columns in the clinical data file by adding the column headers to the list of entries specified after the --phenotypes-to-include input parameter.
95
96 Here are some general guildelines for creating clinical data input files:
97
98 =over 4
99
100 =item * Headers are required.
101
102 =item * The first column of each clinical data file must contain sample IDs which match those in both the --bam-list and the MAF variant list (in the MAF, this is the Tumor_Sample_Barcode column, specifically).
103
104 =item * In at least one of the clinical data files input, columns with headers "vital_status" and "days_to_last_followup" (case insensitive) must exist. "vital_status" must be delineated by 1's and 0's, where 0 denotes 'living', and 1 denotes 'deceased'.
105
106 =back
107
108 Note that all input files must be tab-separated.
109
110 HELP
111 }
112
113 sub _additional_help_sections {
114 return (
115 "ARGUMENTS",
116 <<EOS
117
118 =over 4
119
120 =item --bam-list
121
122 =over 8
123
124 =item Provide a file containing sample names and normal/tumor BAM locations for each. Use the tab-
125 delimited format [sample_name normal_bam tumor_bam] per line. This tool only needs sample_name,
126 so all other columns can be skipped. The sample_name must be the same as the tumor sample names
127 used in the MAF file (16th column, with the header Tumor_Sample_Barcode).
128
129 =back
130
131 =back
132
133 EOS
134 );
135 }
136
137 sub _doc_authors {
138 return <<EOS
139 Nathan D. Dees, Ph.D.
140 Qunyuan Zhang, Ph.D.
141 EOS
142 }
143
144 sub execute {
145
146 # parse input arguments
147 my $self = shift;
148 my $bam_list = $self->bam_list;
149 my $genetic_data_type = $self->genetic_data_type;
150 my $legend_placement = $self->legend_placement;
151
152 # handle phenotype inclusions
153 my @phenotypes_to_include;
154 my @clinical_phenotypes_to_include;
155 my @mutated_genes_to_include;
156 if ($self->phenotypes_to_include) { @phenotypes_to_include = split /,/,$self->phenotypes_to_include; }
157
158 # check genetic data type
159 unless ($genetic_data_type =~ /^gene|variant$/i) {
160 $self->error_message("Please enter either \"gene\" or \"variant\" for the --genetic-data-type parameter.");
161 return;
162 }
163
164 # load clinical data and analysis types
165 my %clinical_data;
166 if ($self->numeric_clinical_data_file) {
167 $clinical_data{'numeric'} = $self->numeric_clinical_data_file;
168 }
169 if ($self->categorical_clinical_data_file) {
170 $clinical_data{'categ'} = $self->categorical_clinical_data_file;
171 }
172 if ($self->glm_clinical_data_file) {
173 $clinical_data{'glm'} = $self->glm_clinical_data_file;
174 }
175
176 # create array of all sample names possibly included from clinical data and MAF
177 my @all_sample_names; # names of all the samples, no matter if they are mutated or not
178 my $sampleFh = IO::File->new( $bam_list ) or die "Couldn't open $bam_list. $!\n";
179 while( my $line = $sampleFh->getline )
180 {
181 next if ( $line =~ m/^#/ );
182 chomp( $line );
183 my ( $sample ) = split( /\t/, $line );
184 push( @all_sample_names, $sample );
185 }
186 $sampleFh->close;
187
188 # loop through clinical data files and assemble survival data hash (vital_status and days_to_last_followup required);
189 my %survival_data;
190 my $vital_status_flag = 0;
191 my $days_to_last_follow_flag = 0;
192
193 for my $clin_file (keys %clinical_data) {
194
195 #check filehandle
196 my $clin_fh = new IO::File $clinical_data{$clin_file},"r";
197 unless ($clin_fh) {
198 $self->error_message("Failed to open $clinical_data{$clin_file} for reading: $!");
199 return;
200 }
201
202 #initiate variables to hold column info
203 my %phenotypes_to_print;
204 my $vital_status_col = 0;
205 my $days_to_last_follow_col = 0;
206
207 #parse header and record column locations for needed data
208 my $header = $clin_fh->getline;
209 my @header_fields = split /\t/,$header;
210 for (my $i = 1; $i <= $#header_fields; $i++) { #sample ID should be in first column of file
211 my $field = $header_fields[$i];
212 if ($field =~ /vital_status|vitalstatus/i) { $vital_status_col = $i; $vital_status_flag++; }
213 if ($field =~ /days_to_last_(follow_up|followup)|daystolastfollowup/i) { $days_to_last_follow_col = $i; $days_to_last_follow_flag++; }
214 if (scalar grep { /^$field$/i } @phenotypes_to_include) { $phenotypes_to_print{$field} = $i; }
215 }
216
217 #read through clinical data file and store needed data in a hash
218 while (my $line = $clin_fh->getline) {
219 chomp $line;
220 my @fields = split /\t/,$line;
221 my $sample = $fields[0];
222 unless (scalar grep { m/^$sample$/ } @all_sample_names) {
223 $self->status_message("Skipping sample $sample. (Sample is not in --bam-list).");
224 next;
225 }
226 if ($vital_status_col) {
227 my $vital_status;
228 if ($fields[$vital_status_col] =~ /^(0|living)$/i) { $vital_status = 0; }
229 elsif ($fields[$vital_status_col] =~ /^(1|deceased)$/i) { $vital_status = 1; }
230 else { $vital_status = "NA"; }
231 $survival_data{$sample}{'vital_status'} = $vital_status;
232 }
233 if ($days_to_last_follow_col) { $survival_data{$sample}{'days'} = $fields[$days_to_last_follow_col]; }
234 for my $pheno (keys %phenotypes_to_print) { $survival_data{$sample}{$pheno} = $fields[$phenotypes_to_print{$pheno}]; }
235 }
236 $clin_fh->close;
237
238 # record phenotypes included from clinical data
239 push @clinical_phenotypes_to_include, keys %phenotypes_to_print;
240
241 }
242
243 # check for necessary header fields
244 unless ($vital_status_flag) {
245 $self->error_message('Clinical data does not seem to contain a column labeled "vital_status".');
246 return;
247 }
248 unless ($days_to_last_follow_flag) {
249 $self->error_message('Clnical data does not seem to contain a column labeled "days_to_last_followup".');
250 return;
251 }
252
253 # create temporary files for R command
254 my $survival_data_file = Genome::Sys->create_temp_file_path();
255 my $mutation_matrix = Genome::Sys->create_temp_file_path();
256
257 # print survival data (temp file)
258 my $surv_fh = new IO::File $survival_data_file,"w" or die "Couldn't open survival data filehandle.";
259 print $surv_fh join("\t","Sample","Days_To_Last_Followup","Vital_Status");
260 if (@clinical_phenotypes_to_include) { print $surv_fh "\t" . join("\t",@clinical_phenotypes_to_include); }
261 print $surv_fh "\n";
262 for my $sample (keys %survival_data) {
263 unless (exists $survival_data{$sample}{'days'}) { $survival_data{$sample}{'days'} = "NA"; }
264 unless (exists $survival_data{$sample}{'vital_status'}) { $survival_data{$sample}{'vital_status'} = "NA"; }
265 print $surv_fh join("\t",$sample,$survival_data{$sample}{'days'},$survival_data{$sample}{'vital_status'});
266 for my $pheno (@clinical_phenotypes_to_include) {
267 unless (exists $survival_data{$sample}{$pheno}) { $survival_data{$sample}{$pheno} = "NA"; }
268 print $surv_fh "\t" . $survival_data{$sample}{$pheno};
269 }
270 print $surv_fh "\n";
271 }
272 $surv_fh->close;
273
274 # find if any of the "phenotypes_to_include" are genes, and if so, limit the MAF mutation matrix to those genes
275 my %clinical_pheno_to_include;
276 @clinical_pheno_to_include{@clinical_phenotypes_to_include} = ();
277 for my $item (@phenotypes_to_include) {
278 push @mutated_genes_to_include,$item unless exists $clinical_pheno_to_include{$item};
279 }
280 my $mutated_genes_to_include = \@mutated_genes_to_include;
281
282 # create mutation matrix file
283 if( $genetic_data_type =~ /^gene$/i ) {
284 $self->create_sample_gene_matrix_gene( $mutation_matrix, $mutated_genes_to_include, @all_sample_names );
285 }
286 elsif( $genetic_data_type =~ /^variant$/i ) {
287 $self->create_sample_gene_matrix_variant( $mutation_matrix, $mutated_genes_to_include, @all_sample_names );
288 }
289 else {
290 $self->error_message( "Please enter either \"gene\" or \"variant\" for the --genetic-data-type parameter." );
291 return;
292 }
293
294 # check and prepare output directory
295 my $output_dir = $self->output_dir . "/";
296 unless (-e $output_dir) {
297 $self->status_message("Creating output directory: $output_dir...");
298 unless(mkdir $output_dir) {
299 $self->error_message("Failed to create output directory: $!");
300 return;
301 }
302 }
303
304 # set up R command
305 my $R_cmd = "R --slave --args < " . __FILE__ . ".R " . join( " ", $survival_data_file, $mutation_matrix, $legend_placement, $output_dir );
306 print "R_cmd:\n$R_cmd\n";
307
308 #run R command
309 WIFEXITED( system $R_cmd ) or croak "Couldn't run: $R_cmd ($?)";
310
311 return(1);
312 }
313
314 sub create_sample_gene_matrix_gene {
315
316 my ( $self, $mutation_matrix, $mutated_genes_to_include, @all_sample_names ) = @_;
317
318 #create hash of mutations from the MAF file
319 my ( %mutations, %all_genes );
320
321 #parse the MAF file and fill up the mutation status hashes
322 my $maf_fh = IO::File->new( $self->maf_file ) or die "Couldn't open MAF file!\n";
323 while( my $line = $maf_fh->getline ) {
324 next if( $line =~ m/^(#|Hugo_Symbol)/ );
325 chomp $line;
326 my @cols = split( /\t/, $line );
327 my ( $gene, $mutation_class, $sample ) = @cols[0,8,15];
328
329 #check that the mutation class is valid
330 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'Flank|3'UTR|5'Flank|5'UTR|IGR|Targeted_Region|De_novo_Start_InFrame|De_novo_Start_OutOfFrame)$/ ) {
331 $self->error_message( "Unrecognized Variant_Classification \"$mutation_class\" in MAF file for gene $gene\nPlease use TCGA MAF v2.3.\n" );
332 return;
333 }
334
335 #check to see if this gene is on the list (if there is a list at all)
336 if( defined @{$mutated_genes_to_include} ) {
337 next unless( scalar grep { m/^$gene$/ } @{$mutated_genes_to_include} );
338 }
339
340 # If user wants, skip Silent mutations, or those in Introns, RNA, UTRs, Flanks, IGRs, or the ubiquitous Targeted_Region
341 if(( $self->skip_non_coding && $mutation_class =~ m/^(Intron|RNA|3'Flank|3'UTR|5'Flank|5'UTR|IGR|Targeted_Region)$/ ) ||
342 ( $self->skip_silent && $mutation_class =~ m/^Silent$/ )) {
343 print "Skipping $mutation_class mutation in gene $gene.\n";
344 next;
345 }
346
347 $all_genes{$gene}++;
348 $mutations{$sample}{$gene}++;
349 }
350 $maf_fh->close;
351
352 #sort @all_genes for consistency in header and loops
353 my @all_genes = sort keys %all_genes;
354
355 #write the input matrix for R code to a temp file
356 my $matrix_fh = new IO::File $mutation_matrix,"w" or die "Failed to create matrix file $mutation_matrix!: $!";
357
358 #print input matrix file header
359 my $header = join( "\t", "Sample", @all_genes );
360 $matrix_fh->print( "$header\n" );
361
362 #print mutation relation input matrix
363 for my $sample ( sort @all_sample_names ) {
364 $matrix_fh->print( $sample );
365 for my $gene ( @all_genes ) {
366 if( exists $mutations{$sample}{$gene} ) {
367 $matrix_fh->print( "\t$mutations{$sample}{$gene}" );
368 }
369 else {
370 $matrix_fh->print( "\t0" );
371 }
372 }
373 $matrix_fh->print( "\n" );
374 }
375 }
376
377 sub create_sample_gene_matrix_variant {
378
379 my ( $self, $mutation_matrix, $mutated_genes_to_include, @all_sample_names ) = @_;
380
381 #create hash of mutations from the MAF file
382 my ( %variants_hash, %all_variants );
383
384 #parse the MAF file and fill up the mutation status hashes
385 my $maf_fh = IO::File->new( $self->maf_file ) or die "Couldn't open MAF file!\n";
386 while( my $line = $maf_fh->getline ) {
387 next if( $line =~ m/^(#|Hugo_Symbol)/ );
388 chomp $line;
389 my @cols = split( /\t/, $line );
390 my ( $gene, $chr, $start, $stop, $mutation_class, $mutation_type, $ref, $var1, $var2, $sample ) = @cols[0,4..6,8..12,15];
391
392 #check that the mutation class is valid
393 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'Flank|3'UTR|5'Flank|5'UTR|IGR|Targeted_Region|De_novo_Start_InFrame|De_novo_Start_OutOfFrame)$/ ) {
394 $self->error_message( "Unrecognized Variant_Classification \"$mutation_class\" in MAF file for gene $gene\nPlease use TCGA MAF v2.3.\n" );
395 return;
396 }
397
398 #check to see if this gene is on the list (if there is a list at all)
399 if( defined @{$mutated_genes_to_include} ) {
400 next unless (scalar grep { m/^$gene$/ } @{$mutated_genes_to_include});
401 }
402
403 # If user wants, skip Silent mutations, or those in Introns, RNA, UTRs, Flanks, IGRs, or the ubiquitous Targeted_Region
404 if(( $self->skip_non_coding && $mutation_class =~ m/^(Intron|RNA|3'Flank|3'UTR|5'Flank|5'UTR|IGR|Targeted_Region)$/ ) ||
405 ( $self->skip_silent && $mutation_class =~ m/^Silent$/ )) {
406 print "Skipping $mutation_class mutation in gene $gene.\n";
407 next;
408 }
409
410 my $var;
411 my $variant_name;
412 if( $ref eq $var1 ) {
413 $var = $var2;
414 $variant_name = $gene."_".$chr."_".$start."_".$stop."_".$ref."_".$var;
415 $variants_hash{$sample}{$variant_name}++;
416 $all_variants{$variant_name}++;
417 }
418 elsif( $ref eq $var2 ) {
419 $var = $var1;
420 $variant_name = $gene."_".$chr."_".$start."_".$stop."_".$ref."_".$var;
421 $variants_hash{$sample}{$variant_name}++;
422 $all_variants{$variant_name}++;
423 }
424 elsif( $ref ne $var1 && $ref ne $var2 ) {
425 $var = $var1;
426 $variant_name = $gene."_".$chr."_".$start."_".$stop."_".$ref."_".$var;
427 $variants_hash{$sample}{$variant_name}++;
428 $all_variants{$variant_name}++;
429 $var = $var2;
430 $variant_name = $gene."_".$chr."_".$start."_".$stop."_".$ref."_".$var;
431 $variants_hash{$sample}{$variant_name}++;
432 $all_variants{$variant_name}++;
433 }
434 }
435 $maf_fh->close;
436
437 #sort variants for consistency
438 my @variant_names = sort keys %all_variants;
439
440 #write the input matrix for R code to a file
441 my $matrix_fh = new IO::File $mutation_matrix,"w" or die "Failed to create matrix file $mutation_matrix!: $!";
442
443 #print input matrix file header
444 my $header = join("\t","Sample",@variant_names);
445 $matrix_fh->print("$header\n");
446
447 #print mutation relation input matrix
448 for my $sample (sort @all_sample_names) {
449 $matrix_fh->print($sample);
450 for my $variant (@variant_names) {
451 if (exists $variants_hash{$sample}{$variant}) {
452 $matrix_fh->print("\t$variants_hash{$sample}{$variant}");
453 }
454 else {
455 $matrix_fh->print("\t0");
456 }
457 }
458 $matrix_fh->print("\n");
459 }
460 }
461
462 1;