Debian Codegraph @

lib/Genome/Model/Tools/Music/Bmr/CalcCovgHelper.pm

raw symbols ◀ ▶
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::Bmr::CalcCovgHelper;
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::Bmr::CalcCovgHelper {
10 is => 'Genome::Model::Tools::Music::Bmr::Base',
11 has_input => [
12 roi_file => { is => 'Text', doc => "Tab delimited list of ROIs [chr start stop gene_name] (See Description)" },
13 reference_sequence => { is => 'Text', doc => "Path to reference sequence in FASTA format" },
14 normal_tumor_bam_pair => { is => 'Text', doc => "Tab delimited line with sample name, path to normal bam file, and path to tumor bam file (See Description)" },
15 output_file => { is => 'Text', doc => "Output file path. Specify either output-file or output-directory.", is_optional => 1},
16 output_dir => { is => 'Text', doc => "Output directory path. Specify either output-file or output-directory", is_optional => 1},
17 normal_min_depth => { is => 'Integer', doc => "The minimum read depth to consider a Normal BAM base as covered", is_optional => 1, default => 6},
18 tumor_min_depth => { is => 'Integer', doc => "The minimum read depth to consider a Tumor BAM base as covered", is_optional => 1, default => 8},
19 min_mapq => { is => 'Integer', doc => "The minimum mapping quality of reads to consider towards read depth counts", is_optional => 1, default => 20},
20 ],
21 has_calculated_optional => [
22 sample_name => {
23 calculate_from => ['normal_tumor_bam_pair'],
24 calculate => q {my @bams = split /\t/, $normal_tumor_bam_pair; return $bams[0];},
25 },
26 final_output_file => {
27 is_output => 1,
28 calculate_from => ['output_file','output_dir','sample_name'],
29 calculate => q {if ($output_file) {return $output_file;} elsif ($output_dir){return $output_dir."/".$sample_name;} else {die "Either output-file or output-dir must be specified."}},
30 },
31 normal_bam => {
32 calculate_from => ['normal_tumor_bam_pair'],
33 calculate => q {my @bams = split /\t/, $normal_tumor_bam_pair; return $bams[1];},
34 },
35 tumor_bam => {
36 calculate_from => ['normal_tumor_bam_pair'],
37 calculate => q {my @bams = split /\t/, $normal_tumor_bam_pair; return $bams[2];},
38 },
39 ],
40 doc => "Uses calcRoiCovg.c to count covered bases per-gene for a tumor-normal pair of BAMs."
41 };
42
43 sub help_synopsis {
44 return <<HELP
45 General usage:
46
47 ... music bmr calc-covg-helper \\
48 --normal-tumor-bam-pair "sample-name path/to/normal_bam path/to/tumor_bam" \\
49 --reference-sequence input_dir/all_sequences.fa \\
50 --output-file output_file \\
51 --roi-file input_dir/all_coding_exons.tsv
52
53 HELP
54 }
55
56 sub help_detail {
57 return <<HELP;
58 This script counts bases with sufficient coverage in the ROIs of each gene in the given pair of
59 tumor-normal BAM files and categorizes them into - AT, CG (non-CpG), and CpG counts. It also adds
60 up these base-counts across all ROIs of each gene in the sample, but covered bases that lie
61 within overlapping ROIs are not counted more than once towards these total counts.
62
63 =back
64 HELP
65 }
66
67 sub _additional_help_sections {
68 return (
69 "ARGUMENTS",
70 <<EOS
71
72 =over 4
73
74 =item --roi-file
75
76 =over 8
77
78 =item The regions of interest (ROIs) of each gene are typically regions targeted for sequencing or are
79 merged exon loci (from multiple transcripts) of genes with 2-bp flanks (splice junctions). ROIs
80 from the same chromosome must be listed adjacent to each other in this file. This allows the
81 underlying C-based code to run much more efficiently and avoid re-counting bases seen in
82 overlapping ROIs (for overall covered base counts). For per-gene base counts, an overlapping
83 base will be counted each time it appears in an ROI of the same gene. To avoid this, be sure to
84 merge together overlapping ROIs of the same gene. BEDtools' mergeBed can help if used per gene.
85
86 =back
87
88 =item --reference-sequence
89
90 =over 8
91
92 =item The reference sequence in FASTA format. If a reference sequence index is not found next to this
93 file (a .fai file), it will be created.
94
95 =back
96
97 =item --normal-tumor-bam-pair
98
99 =over 8
100
101 =item "sample-name path/to/normal_bam path/to/tumor_bam"
102
103 =back
104
105 =item --output-file
106
107 =over 8
108
109 =item Specify an output file where the per-ROI covered base counts will be written
110
111 =back
112
113 EOS
114 );
115 }
116
117 sub _doc_authors {
118 return " Cyriac Kandoth, Ph.D.";
119 }
120
121 sub _doc_see_also {
122 return <<EOS
123 B<genome-music-bmr>(1),
124 B<genome-music>(1),
125 B<genome>(1)
126 EOS
127 }
128
129 sub execute {
130 my $self = shift;
131 my $roi_file = $self->roi_file;
132 my $ref_seq = $self->reference_sequence;
133 my $tumor_bam = $self->tumor_bam;
134 my $normal_bam = $self->normal_bam;
135 my $output_file = $self->final_output_file;
136 my $normal_min_depth = $self->normal_min_depth;
137 my $tumor_min_depth = $self->tumor_min_depth;
138 my $min_mapq = $self->min_mapq;
139
140 # Check on all the input data before starting work
141 print STDERR "ROI file not found or is empty: $roi_file\n" unless( -s $roi_file );
142 print STDERR "Reference sequence file not found: $ref_seq\n" unless( -e $ref_seq );
143 print STDERR "Normal BAM file not found or is empty: $normal_bam\n" unless( -s $normal_bam );
144 print STDERR "Tumor BAM file not found or is empty: $tumor_bam\n" unless( -s $tumor_bam );
145 return undef unless( -s $roi_file && -e $ref_seq && -s $normal_bam && -s $tumor_bam );
146
147 # Check whether the annotated regions of interest are clumped together by chromosome
148 my $roiFh = IO::File->new( $roi_file ) or die "ROI file could not be opened. $!\n";
149 my @chroms = ( "" );
150 while( my $line = $roiFh->getline ) # Emulate Unix's uniq command on the chromosome column
151 {
152 my ( $chrom ) = ( $line =~ m/^(\S+)/ );
153 push( @chroms, $chrom ) if( $chrom ne $chroms[-1] );
154 }
155 $roiFh->close;
156 my %chroms = map { $_ => 1 } @chroms; # Get the actual number of unique chromosomes
157 if( scalar( @chroms ) != scalar( keys %chroms ))
158 {
159 print STDERR "ROIs from the same chromosome must be listed adjacent to each other in file. ";
160 print STDERR "If in UNIX, try:\nsort -k 1,1 $roi_file\n";
161 return undef;
162 }
163
164 # If the reference sequence FASTA file hasn't been indexed, do it
165 my $ref_seq_idx = "$ref_seq.fai";
166 system( "samtools faidx $ref_seq" ) unless( -e $ref_seq_idx );
167
168 $normal_bam = '' unless( defined $normal_bam );
169 $tumor_bam = '' unless( defined $tumor_bam );
170 print STDERR "Normal BAM not found: \"$normal_bam\"\n" unless( -e $normal_bam );
171 print STDERR "Tumor BAM not found: \"$tumor_bam\"\n" unless( -e $tumor_bam );
172 next unless( -e $normal_bam && -e $tumor_bam );
173
174 # Construct the command that calculates coverage per ROI
175 my $calcRoiCovg_cmd = "calcRoiCovg $normal_bam $tumor_bam $roi_file $ref_seq $output_file $normal_min_depth $tumor_min_depth $min_mapq";
176
177 # If the calcRoiCovg output was already generated, then don't rerun it
178 if( -s $output_file )
179 {
180 print "Output file $output_file found. Skipping re-calculation.\n";
181 }
182 # Run the calcRoiCovg command on this tumor-normal pair. This could take a while
183 elsif( system( "$calcRoiCovg_cmd" ) != 0 )
184 {
185 print STDERR "Failed to execute: $calcRoiCovg_cmd\n";
186 return;
187 }
188 else
189 {
190 print "$output_file generated and stored.\n";
191 return 1;
192 }
193
194 }
195
196 1;