Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package Genome::Model::Tools::Music::Bmr::CalcCovgHelperscip-perl cpan Genome-Model-Tools-Music 0.04 Genome/Model/Tools/Music/Bmr/CalcCovgHelper/;
2
3 use warningsscip-perl cpan Genome-Model-Tools-Music 0.04 warnings/;
4 use strictscip-perl cpan Genome-Model-Tools-Music 0.04 strict/;
5 use IO::Filescip-perl cpan Genome-Model-Tools-Music 0.04 IO/File/;
6
7 our $VERSIONscip-perl cpan Genome-Model-Tools-Music 0.04 Genome/Model/Tools/Music/Bmr/CalcCovgHelper/$VERSION. = $Genome::Model::Tools::Music::VERSIONscip-perl cpan Genome-Model-Tools-Music 0.04 Genome/Model/Tools/Music/$VERSION.;
8
9 class Genome::Model::Tools::Music::Bmr::CalcCovgHelperscip-perl cpan Genome-Model-Tools-Music 0.04 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_synopsisscip-perl cpan Genome-Model-Tools-Music 0.04 Genome/Model/Tools/Music/Bmr/CalcCovgHelper/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_detailscip-perl cpan Genome-Model-Tools-Music 0.04 Genome/Model/Tools/Music/Bmr/CalcCovgHelper/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 HELP
64 }
65
66 sub _additional_help_sectionsscip-perl cpan Genome-Model-Tools-Music 0.04 Genome/Model/Tools/Music/Bmr/CalcCovgHelper/_additional_help_sections(). {
67 return (
68 "ARGUMENTS",
69 <<EOS
70
71 =over 4
72
73 =item --roi-file
74
75 =over 8
76
77 =item The regions of interest (ROIs) of each gene are typically regions targeted for sequencing or are
78 merged exon loci (from multiple transcripts) of genes with 2-bp flanks (splice junctions). ROIs
79 from the same chromosome must be listed adjacent to each other in this file. This allows the
80 underlying C-based code to run much more efficiently and avoid re-counting bases seen in
81 overlapping ROIs (for overall covered base counts). For per-gene base counts, an overlapping
82 base will be counted each time it appears in an ROI of the same gene. To avoid this, be sure to
83 merge together overlapping ROIs of the same gene. BEDtools' mergeBed can help if used per gene.
84
85 =back
86
87 =item --reference-sequence
88
89 =over 8
90
91 =item The reference sequence in FASTA format. If a reference sequence index is not found next to this
92 file (a .fai file), it will be created.
93
94 =back
95
96 =item --normal-tumor-bam-pair
97
98 =over 8
99
100 =item "sample-name path/to/normal_bam path/to/tumor_bam"
101
102 =back
103
104 =item --output-file
105
106 =over 8
107
108 =item Specify an output file where the per-ROI covered base counts will be written
109
110 =back
111
•112 =back
113
114 EOS
115 );
116 }
117
118 sub _doc_authors {
119 return " Cyriac Kandoth, Ph.D.";
120 }
121
122 sub _doc_see_also {
123 return <<EOS
124 B<genome-music-bmr>(1),
125 B<genome-music>(1),
126 B<genome>(1)
127 EOS
128 }
129
130 sub execute {
131 my $self = shift;
132 my $roi_file = $self->roi_file;
133 my $ref_seq = $self->reference_sequence;
134 my $tumor_bam = $self->tumor_bam;
135 my $normal_bam = $self->normal_bam;
136 my $output_file = $self->final_output_file;
137 my $normal_min_depth = $self->normal_min_depth;
138 my $tumor_min_depth = $self->tumor_min_depth;
139 my $min_mapq = $self->min_mapq;
140
141 # Check on all the input data before starting work
142 print STDERR "ROI file not found or is empty: $roi_file\n" unless( -s $roi_file );
143 print STDERR "Reference sequence file not found: $ref_seq\n" unless( -e $ref_seq );
144 print STDERR "Normal BAM file not found or is empty: $normal_bam\n" unless( -s $normal_bam );
145 print STDERR "Tumor BAM file not found or is empty: $tumor_bam\n" unless( -s $tumor_bam );
146 return undef unless( -s $roi_file && -e $ref_seq && -s $normal_bam && -s $tumor_bam );
147
148 # Check whether the annotated regions of interest are clumped together by chromosome
149 my $roiFh = IO::File->new( $roi_file ) or die "ROI file could not be opened. $!\n";
150 my @chroms = ( "" );
151 while( my $line = $roiFh->getline ) # Emulate Unix's uniq command on the chromosome column
152 {
153 my ( $chrom ) = ( $line =~ m/^(\S+)/ );
154 push( @chroms, $chrom ) if( $chrom ne $chroms[-1] );
155 }
156 $roiFh->close;
157 my %chroms = map { $_ => 1 } @chroms; # Get the actual number of unique chromosomes
158 if( scalar( @chroms ) != scalar( keys %chroms ))
159 {
160 print STDERR "ROIs from the same chromosome must be listed adjacent to each other in file. ";
161 print STDERR "If in UNIX, try:\nsort -k 1,1 $roi_file\n";
162 return undef;
163 }
164
165 # If the reference sequence FASTA file hasn't been indexed, do it
166 my $ref_seq_idx = "$ref_seq.fai";
167 system( "samtools faidx $ref_seq" ) unless( -e $ref_seq_idx );
168
169 $normal_bam = '' unless( defined $normal_bam );
170 $tumor_bam = '' unless( defined $tumor_bam );
171 print STDERR "Normal BAM not found: \"$normal_bam\"\n" unless( -e $normal_bam );
172 print STDERR "Tumor BAM not found: \"$tumor_bam\"\n" unless( -e $tumor_bam );
173 next unless( -e $normal_bam && -e $tumor_bam );
174
175 # Construct the command that calculates coverage per ROI
176 my $calcRoiCovg_cmd = "calcRoiCovg $normal_bam $tumor_bam $roi_file $ref_seq $output_file $normal_min_depth $tumor_min_depth $min_mapq";
177
178 # If the calcRoiCovg output was already generated, then don't rerun it
179 if( -s $output_file )
180 {
181 print "Output file $output_file found. Skipping re-calculation.\n";
182 }
183 # Run the calcRoiCovg command on this tumor-normal pair. This could take a while
184 elsif( system( "$calcRoiCovg_cmd" ) != 0 )
185 {
186 print STDERR "Failed to execute: $calcRoiCovg_cmd\n";
187 return;
188 }
189 else
190 {
191 print "$output_file generated and stored.\n";
192 return 1;
193 }
194
195 }
196
197 1;