Debian Codegraph @

lib/Genome/Model/Tools/Music/Proximity.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::Proximity;
2
3 use warnings;
4 use strict;
5 use IO::File;
6
7 our $VERSION = $Genome::Model::Tools::Music::VERSION;
8
9 class Genome::Model::Tools::Music::Proximity {
10 is => 'Command::V2',
11 has_input => [
12 maf_file => { is => 'Text', doc => "List of mutations using TCGA MAF specifications v2.3" },
13 output_dir => { is => 'Text', doc => "Directory where output files will be written" },
14 max_proximity => { is => 'Text', doc => "Maximum allowed AA distance between 2 mutations", is_optional => 1, default => 7 },
15 skip_non_coding => { is => 'Boolean', doc => "Skip non-coding mutations from the provided MAF file", is_optional => 1, default => 1 },
16 skip_silent => { is => 'Boolean', doc => "Skip silent mutations from the provided MAF file", is_optional => 1, default => 1 },
17 ],
18 has_output => [
19 output_file => {is => 'Text', doc => "TODO"},
20 ],
21 doc => "Perform a proximity analysis on a list of mutations."
22 };
23
24 sub help_detail {
25 return <<HELP
26 This module first calculates the amino acid position of each mutation in the MAF file within its
27 respective transcript. Then, for each mutation, two values are calculated: 1) the number of other
28 mutations on the same transcript within the proximity limit set by the max-proximity input
29 parameter, and 2) the distance to the closest other mutation in this nearby set. Only mutations
30 which have another mutation within close proximity are reported in the output-file.
31
32 In addition to the standard version 2.3 MAF headers, there needs to be 3 columns appended. These
33 column headers in the MAF must have these names in the header in order for the tool to find them:
34 transcript_name - the transcript name, such as NM_000028
35 amino_acid_change - the amino acid change, such as p.R290H
36 c_position - the nucleotide position changed, such as c.869
37
38 The output is generated with the folowing column headers:
39 Mutations_Within_Proximity, Nearest_Mutation, Gene, Transcript, Affected_Amino_Acid(s), Chr,
40 Start, Stop, Ref_Allele, Var_Allele, Sample
41 HELP
42 }
43
44 sub help_synopsis {
45 return <<EOS
46 ... music proximity \\
47 --maf-file input_dir/myMAF.tsv \\
48 --output-dir output_dir/ \\
49 --max-proximity 15
50 EOS
51 }
52
53 sub _doc_authors {
54 return <<EOS
55 Nathan D. Dees, Ph.D.
56 Dan Koboldt, M.S.
57 Cyriac Kandoth, Ph.D.
58 EOS
59 }
60
61 sub execute {
62 my $self = shift;
63 my $maf_file = $self->maf_file;
64 my $output_dir = $self->output_dir;
65 my $max_proximity = $self->max_proximity;
66 my $skip_non_coding = $self->skip_non_coding;
67 my $skip_silent = $self->skip_silent;
68 $output_dir =~ s/(\/)+$//; # Remove trailing forward slashes if any
69
70 # Check on all the input data before starting work
71 print STDERR "MAF file not found or is empty: $maf_file\n" unless( -s $maf_file );
72 print STDERR "Output directory not found: $output_dir\n" unless( -e $output_dir );
73 return undef unless( -s $maf_file && -e $output_dir );
74
75 # Output of this script will be written to this location in the output directory
76 my $out_file = "$output_dir/proximity_report";
77 $self->output_file($out_file);
78
79 # Parse the header row in the MAF file
80 my $maf_fh = IO::File->new( $maf_file ) or die "Couldn't open $maf_file. $!";
81 my $maf_header = $maf_fh->getline;
82 $maf_header = $maf_fh->getline while( $maf_header =~ /^#/ ); # Skip commented lines
83 chomp( $maf_header );
84 unless( $maf_header =~ /^Hugo_Symbol/ )
85 {
86 print STDERR "Could not find column headers in $maf_file\n";
87 return undef;
88 }
89
90 # Check whether the required additional MAF columns were included in the MAF
91 unless( $maf_header =~ m/c_position/ and $maf_header =~ m/amino_acid_change/ and $maf_header =~ m/transcript_name/ )
92 {
93 print STDERR "Could not find required additional columns in $maf_file\n";
94 return undef;
95 }
96
97 # Find the indexes of all the MAF columns
98 my $idx = 0;
99 my %col_idx = map {($_, $idx++)} split( /\t/, $maf_header );
100
101 # A hash to store statuses, and a hash to store variants and AA positions
102 my %status;
103 my $status = \%status;
104 my %aa_mutations;
105
106 # Load relevant data from MAF into hash
107 while( my $line = $maf_fh->getline )
108 {
109 chomp $line;
110 my @cols = split( /\t/, $line );
111
112 # Fetch data from the generic MAF columns
113 my ( $gene, $chr, $start, $stop, $mutation_class, $mutation_type, $ref_allele, $var_allele, $var2, $sample ) =
114 ( $cols[0], $cols[4], $cols[5], $cols[6], $cols[8], $cols[9], $cols[10], $cols[11], $cols[12], $cols[15] );
115 $var_allele = $var2 if ( $var_allele eq $ref_allele ); # Different centers interpret the 2 variant columns differently
116
117 # Fetch data from the required additional MAF columns
118 my ( $c_position, $aa_change, $transcript ) = ( $cols[$col_idx{c_position}], $cols[$col_idx{amino_acid_change}], $cols[$col_idx{transcript_name}] );
119
120 # Create a key to uniquely identify each variant
121 my $variant_key = join( "\t", $gene, $chr, $start, $stop, $ref_allele, $var_allele, $sample );
122
123
124 #check that the mutation class is acceptable
125 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)$/ )
126 {
127 print STDERR "Unrecognized Variant_Classification \"$mutation_class\" in MAF file for gene $gene\n";
128 print STDERR "Please use TCGA MAF Specification v2.3.\n";
129 return undef;
130 }
131
132 # If user wants, skip Silent mutations, or those in Introns, RNA, UTRs, Flanks, IGRs, or the ubiquitous Targeted_Region
133 if(( $skip_non_coding && $mutation_class =~ m/^(Intron|RNA|3'Flank|3'UTR|5'Flank|5'UTR|IGR|Targeted_Region)$/ ) ||
134 ( $skip_silent && $mutation_class =~ m/^Silent$/ ))
135 {
136 print "Skipping $mutation_class mutation in gene $gene.\n";
137 $status{synonymous_mutations_skipped}++;
138 next;
139 }
140
141 # Determine amino acid position and load into hash
142 my @mutated_aa_positions = ();
143 @mutated_aa_positions = $self->get_amino_acid_pos( $variant_key, $mutation_class, $c_position, $aa_change, $status );
144
145 # Record data in hash if mutated aa position found
146 if( scalar( @mutated_aa_positions ) > 0 )
147 {
148 push( @{$aa_mutations{$transcript}{$variant_key}{mut_AAs}}, @mutated_aa_positions );
149 }
150 }
151 $maf_fh->close;
152
153 # Evaluate proximity of mutated amino acids for each transcript
154 for my $transcript ( keys %aa_mutations )
155 {
156 # For each variant hitting that transcript
157 for my $variant ( keys %{$aa_mutations{$transcript}} )
158 {
159 # Initialize the search
160 my @affected_amino_acids = @{$aa_mutations{$transcript}{$variant}{mut_AAs}};
161 my $mutations_within_proximity = 0; # Variable for summing # of mutations within proximity
162 my $min_proximity = $max_proximity + 1; # Current minimum proximity
163
164 # For each OTHER variant hitting the transcript
165 for my $other_variant ( keys %{$aa_mutations{$transcript}} )
166 {
167 # Ignore the current mutation
168 next if $variant eq $other_variant;
169 # Get affected amino acids from OTHER variant
170 my @other_affected_amino_acids = @{$aa_mutations{$transcript}{$other_variant}{mut_AAs}};
171
172 # Compare distances between amino acids
173 my $found_close_one = 0;
174 for my $other_variant_aa ( @other_affected_amino_acids )
175 {
176 for my $variant_aa ( @affected_amino_acids )
177 {
178 my $distance = abs($other_variant_aa - $variant_aa);
179 # If distance is within range
180 if( $distance <= $max_proximity )
181 {
182 $found_close_one++;
183 $min_proximity = $distance if $distance < $min_proximity;
184 }
185 }
186 }
187 # Note that this variant is within proximity if applicable
188 $mutations_within_proximity++ if $found_close_one;
189 }
190 # Now, save results in hash if there are any
191 if ($mutations_within_proximity)
192 {
193 $aa_mutations{$transcript}{$variant}{muts_within_range} = $mutations_within_proximity;
194 $aa_mutations{$transcript}{$variant}{min_proximity} = $min_proximity;
195 }
196 }
197 }
198
199 # Print results
200 my $out_fh = IO::File->new( $out_file, ">" ) or die "Couldn't open $out_file. $!";
201 $out_fh->print( "Mutations_Within_Proximity\tNearest_Mutation\tGene\tTranscript\tAffected_Amino_Acid(s)\tChr\tStart\tStop\tRef_Allele\tVar_Allele\tSample\n" );
202 for my $transcript ( keys %aa_mutations )
203 {
204 for my $variant ( keys %{$aa_mutations{$transcript}} )
205 {
206 if( exists $aa_mutations{$transcript}{$variant}{muts_within_range} )
207 {
208 my ( $gene, $chr, $start, $stop, $ref_allele, $var_allele, $sample ) = split( /\t/, $variant );
209 my $affected_amino_acids = join( ",", sort @{$aa_mutations{$transcript}{$variant}{mut_AAs}} );
210 my $line = join( "\t", $aa_mutations{$transcript}{$variant}{muts_within_range},
211 $aa_mutations{$transcript}{$variant}{min_proximity}, $gene, $transcript,
212 $affected_amino_acids, $chr, $start, $stop, $ref_allele, $var_allele, $sample );
213 $out_fh->print( "$line\n" );
214 }
215 }
216 }
217 $out_fh->close();
218 return 1;
219 }
220
221 ################################################################################
222
223 =head2 get_amino_acid_pos
224
225 This subroutine deducts the amino acid position within the transcript using the c_position and amino_acid_position columns in the MAF.
226
227 =cut
228
229 ################################################################################
230
231 sub get_amino_acid_pos {
232 # Parse arguments
233 my $self = shift;
234 my ( $variant_key, $mut_class, $c_position, $aa_change, $status ) = @_;
235
236 # Initialize variables
237 my $tx_start = my $tx_stop = 0;
238 my $aa_position_start = my $aa_position_stop = 0;
239 my $inferred_aa_start = my $inferred_aa_stop = 0;
240 my $aa_pos = my $inferred_aa_pos = 0;
241
242 # Amino acid position determination
243 if( $aa_change && $aa_change ne "NULL" && substr( $aa_change, 0, 1 ) ne "e" )
244 {
245 $aa_pos = $aa_change;
246 $aa_pos =~ s/[^0-9]//g;
247 }
248
249 # Parse out c_position if applicable ##
250 if( $c_position && $c_position ne "NULL" )
251 {
252 # If multiple results, parse both ##
253 if( $c_position =~ '_' && !( $mut_class =~ 'splice' ))
254 {
255 ($tx_start, $tx_stop) = split( /\_/, $c_position );
256 $tx_start =~ s/[^0-9]//g;
257 $tx_stop =~ s/[^0-9]//g;
258
259 if( $tx_stop < $tx_start )
260 {
261 $inferred_aa_start = $tx_stop / 3;
262 $inferred_aa_start = sprintf( "%d", $inferred_aa_start ) + 1 if( $tx_stop % 3 ) ;
263 $inferred_aa_stop = $tx_start / 3;
264 $inferred_aa_stop = sprintf( "%d", $inferred_aa_stop ) + 1 if( $tx_start % 3 );
265 }
266 else
267 {
268 $inferred_aa_start = $tx_start / 3;
269 $inferred_aa_start = sprintf( "%d", $inferred_aa_start ) + 1 if( $tx_start % 3 );
270 $inferred_aa_stop = $tx_stop / 3;
271 $inferred_aa_stop = sprintf( "%d", $inferred_aa_stop ) + 1 if( $tx_stop % 3 );
272 }
273 }
274 else
275 {
276 my ( $tx_pos ) = split( /[\+\-\_]/, $c_position );
277 $tx_pos =~ s/[^0-9]//g;
278 $tx_start = $tx_stop = $tx_pos;
279 if($tx_pos)
280 {
281 $inferred_aa_pos = $tx_pos / 3;
282 $inferred_aa_pos = sprintf( "%d", $inferred_aa_pos ) + 1 if( $tx_pos % 3 );
283 $inferred_aa_start = $inferred_aa_stop = $inferred_aa_pos;
284 }
285 }
286 }
287
288 # If we inferred aa start stop, proceed with it ##
289 if( $inferred_aa_start && $inferred_aa_stop )
290 {
291 $aa_position_start = $inferred_aa_start;
292 $aa_position_stop = $inferred_aa_stop;
293 $status->{aa_position_inferred}++;
294 }
295 # Otherwise if we inferred aa position ##
296 elsif( $aa_pos )
297 {
298 $aa_position_start = $aa_pos;
299 $aa_position_stop = $aa_pos;
300 $status->{c_position_not_available}++;
301 }
302 # Otherwise we were unable to infer the info ##
303 else
304 {
305 $status->{aa_position_not_found}++;
306 $self->status_message( "Amino acid position not found for variant: $variant_key" );
307 return;
308 }
309
310 # Proceed if we have aa_position_start and stop ##
311 my %mutated_aa_positions;
312 if( $aa_position_start && $aa_position_stop )
313 {
314 for( my $this_aa_pos = $aa_position_start; $this_aa_pos <= $aa_position_stop; $this_aa_pos++ )
315 {
316 $mutated_aa_positions{$this_aa_pos}++;
317 }
318 }
319
320 my @mutated_aa_positions = keys %mutated_aa_positions;
321 return @mutated_aa_positions;
322 }
323
324 1;