Genome...lcCovg(3pm)

🌸 Genome...lcCovg(3pm) 🌸

okay bestie, welcome to the User Contributed Perl Documentation for Genome...lcCovg(3pm). this is part 1 and we're gonna get into it. no cap. πŸ’…


πŸ’– HELP πŸ’–

  • Define cmd-list-file and cmd-prefix to generate a file with commands that you can submit to a cluster or run manually. These jobs will write per-ROI base counts in a subdirectory called roi_covgs. πŸ’…
  • After all the parallelized calcRoiCovg jobs are completed, run this script again to add them up and generate the final per-gene base counts in a subdirectory called gene_covgs. But bestie, remember to remove the cmd-list-file and cmd-prefix arguments or you will just be re-creating a list of commands. not the vibe. 😭
       HELP }

🧚 _additional_help_sections 🧚

okay so this subroutine returns the ARGUMENTS help section. Here's the whole thing, with the descriptions rewritten so they actually make sense:

       sub _additional_help_sections {
         return (
           "ARGUMENTS", <<EOS

       --roi-file
           bestie, the regions of interest (ROIs) of each gene are usually the regions you targeted for sequencing, or they're merged exon loci (from multiple transcripts) of genes with 2-bp flanks (splice junctions). ROIs from the same chromosome have to be listed adjacent to each other in this file. That way the underlying C-based code can run way more efficiently and avoid re-counting bases seen in overlapping ROIs (for overall covered base counts). For per-gene base counts, an overlapping base will get counted every time it shows up in an ROI of the same gene. To dodge that, make sure you merge together overlapping ROIs of the same gene. BEDtools' mergeBed can help if you use it per gene.
       --reference-sequence
           the reference sequence in FASTA format. If a reference sequence index isn't found next to this file (a .fai file), it's gonna get created.
       --bam-list
           give it a file that has sample names and normal/tumor BAM locations for each. Use the tab-delimited format [sample_name normal_bam tumor_bam] per line. You can add extra columns like clinical data, but they're ignored. The sample_name has to match the tumor sample names used in the MAF file (16th column, with the header Tumor_Sample_Barcode).
       --output-dir
           tell it where to put stuff. It'll create/write: roi_covgs: a subdirectory with per-ROI covered base counts for each sample. gene_covgs: a subdirectory with per-gene covered base counts for each sample. total_covgs: a file with the overall non-overlapping coverages per sample.
       --cmd-list-file
           give it a file and it'll write a list of calcRoiCovg jobs into it. You can schedule those in parallel, and they'll write per-ROI covered base-counts into the output subdirectory roi_covgs. If you leave cmd-list-file unspecified, this script runs calcRoiCovg per sample one after another, taking ~30 mins per sample, but it skips samples whose output is already in roi_covgs.
       --cmd-prefix
           give it a job submission command that will be prefixed to each command in cmd-list-file. This makes batch submission easier. Just run the cmd-list-file file as a shell script to submit jobs. cmd-prefix is "bsub" if your cluster uses the LSF job scheduler, or "qsub" in Torque. Add arguments as necessary. For example, "bsub -M 4GB" sets a soft memory limit of 4GB.

       EOS
         ); }

πŸ‘‘ AUTHORS πŸ‘‘

       sub _doc_authors {
         return " Cyriac Kandoth, Ph.D."; }

🌈 SEE ALSO 🌈

       sub _doc_see_also {
         return <<EOS genome-music-bmr(1), genome-music(1), genome(1) EOS }

πŸ”₯ execute πŸ”₯

and now the main character: the execute subroutine. it's giving main character energy. here's the whole thing, no edits, periodt. πŸ’…

       sub execute {
         my $self = shift;
         my $roi_file = $self->roi_file;
         my $ref_seq = $self->reference_sequence;
         my $bam_list = $self->bam_list;
         my $output_dir = $self->output_dir;
         my $cmd_list_file = $self->cmd_list_file;
         my $cmd_prefix = $self->cmd_prefix;
         my $normal_min_depth = $self->normal_min_depth;
         my $tumor_min_depth = $self->tumor_min_depth;
         my $min_mapq = $self->min_mapq;

         my $optional_params = "";

         if ($normal_min_depth) {
           $optional_params .= " --normal-min-depth $normal_min_depth";
         }
         if ($tumor_min_depth) {
           $optional_params .= " --tumor-min-depth $tumor_min_depth";
         }
         if ($min_mapq) {
           $optional_params .= " --min-mapq $min_mapq";
         }

         # Check on all the input data before starting work
         print STDERR "ROI file not found or is empty: $roi_file\n" unless( -s $roi_file );
         print STDERR "Reference sequence file not found: $ref_seq\n" unless( -e $ref_seq );
         print STDERR "List of BAMs not found or is empty: $bam_list\n" unless( -s $bam_list );
         print STDERR "Output directory not found: $output_dir\n" unless( -e $output_dir );
         return undef unless( -s $roi_file && -e $ref_seq && -s $bam_list && -e $output_dir );

         # Outputs of this script will be written to these locations in the output directory
         $output_dir =~ s/(\/)+$//; # Remove trailing forward slashes if any
         my $roi_covg_dir = "$output_dir/roi_covgs"; # Stores output from calcRoiCovg per sample
         my $gene_covg_dir = "$output_dir/gene_covgs"; # Stores per-gene coverages per sample
         my $tot_covg_file = "$output_dir/total_covgs"; # Stores total coverages per sample

         $self->gene_covg_dir($gene_covg_dir);

         # Check whether the annotated regions of interest are clumped together by chromosome
         my $roiFh = IO::File->new( $roi_file ) or die "ROI file could not be opened. $!\n";
         my @chroms = ( "" );
         while( my $line = $roiFh->getline ) # Emulate Unix's uniq command on the chromosome column
         {
           my ( $chrom ) = ( $line =~ m/^(\S+)/ );
           push( @chroms, $chrom ) if( $chrom ne $chroms[-1] );
         }
         $roiFh->close;
         my %chroms = map { $_ => 1 } @chroms; # Get the actual number of unique chromosomes
         if( scalar( @chroms ) != scalar( keys %chroms ))
         {
           print STDERR "ROIs from the same chromosome must be listed adjacent to each other in file. ";
           print STDERR "If in UNIX, try:\nsort -k 1,1 $roi_file\n";
           return undef;
         }

         # If the reference sequence FASTA file hasn't been indexed, do it
         my $ref_seq_idx = "$ref_seq.fai";
         system( "samtools faidx $ref_seq" ) unless( -e $ref_seq_idx );

         # Create the output directories unless they already exist
         mkdir $roi_covg_dir unless( -e $roi_covg_dir );
         mkdir $gene_covg_dir unless( -e $gene_covg_dir );

         my ( $cmdFh, $totCovgFh );
         if( defined $cmd_list_file )
         {
           $cmdFh = IO::File->new( $cmd_list_file, ">" );
           print "Creating a list of parallelizable jobs at $cmd_list_file.\n";
           print "After successfully running all the jobs in $cmd_list_file,\n",
                 "be sure to run this script a second time (without defining the cmd-list-file argument) to merge results in roi_covgs.\n";
         }
         else
         {
           $totCovgFh = IO::File->new( $tot_covg_file, ">" );
           $totCovgFh->print( "#Sample\tCovered_Bases\tAT_Bases_Covered\tCG_Bases_Covered\tCpG_Bases_Covered\n" );
         }

         # Parse through each pair of BAM files provided and run calcRoiCovg as necessary
         my $bamFh = IO::File->new( $bam_list );
         while( my $line = $bamFh->getline )
         {
           next if( $line =~ m/^#/ );
           chomp( $line );
           my ( $sample, $normal_bam, $tumor_bam ) = split( /\t/, $line );
           $normal_bam = '' unless( defined $normal_bam );
           $tumor_bam = '' unless( defined $tumor_bam );
           print STDERR "Normal BAM for $sample not found: \"$normal_bam\"\n" unless( -e $normal_bam );
           print STDERR "Tumor BAM for $sample not found: \"$tumor_bam\"\n" unless( -e $tumor_bam );
           next unless( -e $normal_bam && -e $tumor_bam );

           # Construct the command that calculates coverage per ROI
           my $calcRoiCovg_cmd = "\'gmt music bmr calc-covg-helper --normal-tumor-bam-pair \"$line\" --roi-file \"$roi_file\" ".
           "--reference-sequence \"$ref_seq\" --output-file \"$roi_covg_dir/$sample.covg\"$optional_params\'";

           # If user only wants the calcRoiCovg commands, write them to file and skip running calcRoiCovg
           if( defined $cmd_list_file )
           {
             $calcRoiCovg_cmd = $cmd_prefix . " $calcRoiCovg_cmd" if( defined $cmd_prefix );
             $cmdFh->print( "$calcRoiCovg_cmd\n" );
             next;
           }

           # If the calcRoiCovg output was already generated, then don't rerun it
           if( -s "$roi_covg_dir/$sample.covg" )
           {
             print "$sample.covg found in $roi_covg_dir. Skipping re-calculation.\n";
           }
           # Run the calcRoiCovg command on this tumor-normal pair. This could take a while
           else {
             my %params = (
               normal_tumor_bam_pair => $line,
               roi_file => $roi_file,
               reference_sequence => $ref_seq,
               output_file => $roi_covg_dir."/".$sample.".covg",
             );
             if ($normal_min_depth) {
               $params{"normal_min_depth"} = $normal_min_depth;
             }
             if ($tumor_min_depth) {
               $params{"tumor_min_depth"} = $tumor_min_depth;
             }
             if ($min_mapq) {
               $params{"min_mapq"} = $min_mapq;
             }
             my $cmd = Genome::Model::Tools::Music::Bmr::CalcCovgHelper->create(%params);
             my $rv = $cmd->execute;
             if(!$rv)
             {
               print STDERR "Failed to execute: $calcRoiCovg_cmd\n";
               next;
             }
             else
             {
               print "$sample.covg generated and stored to $roi_covg_dir.\n";
             }
           }

           # Read the calcRoiCovg output and count covered bases per gene
           my %geneCovg = ();
           my ( $tot_covd, $tot_at_covd, $tot_cg_covg, $tot_cpg_covd );
           my $roiCovgFh = IO::File->new( "$roi_covg_dir/$sample.covg" );
           while( my $line = $roiCovgFh->getline )
           {
             chomp( $line );
             if( $line =~ m/^#NonOverlappingTotals/ )
             {
               ( undef, undef, undef, $tot_covd, $tot_at_covd, $tot_cg_covg, $tot_cpg_covd ) = split( /\t/, $line );
             }
             elsif( $line !~ m/^#/ )
             {
               my ( $gene, undef, $length, $covd, $at_covd, $cg_covd, $cpg_covd ) = split( /\t/, $line );
               $geneCovg{$gene}{len} += $length;
               $geneCovg{$gene}{covd_len} += $covd;
               $geneCovg{$gene}{at} += $at_covd;
               $geneCovg{$gene}{cg} += $cg_covd;
               $geneCovg{$gene}{cpg} += $cpg_covd;
             }
           }
           $roiCovgFh->close;

           # Write the per-gene coverages to a file named after this sample_name
           my $geneCovgFh = IO::File->new( "$gene_covg_dir/$sample.covg", ">" );
           $geneCovgFh->print( "#Gene\tLength\tCovered\tAT_covd\tCG_covd\tCpG_covd\n" );
           foreach my $gene ( sort keys %geneCovg )
           {
             $geneCovgFh->print( join( "\t", $gene, $geneCovg{$gene}{len}, $geneCovg{$gene}{covd_len},
                $geneCovg{$gene}{at}, $geneCovg{$gene}{cg}, $geneCovg{$gene}{cpg} ), "\n" );
           }
           $geneCovgFh->close;

okay bestie, final stretch β€” here's the tail end of the code, and yes, every line is staying EXACTLY as written because we do not touch the code, we only hype it up πŸ’…

  # Write total coverages for this sample to a file
  $totCovgFh->print( "$sample\t$tot_covd\t$tot_at_covd\t$tot_cg_covg\t$tot_cpg_covd\n" );
}
$bamFh->close;
$cmdFh->close if( defined $cmd_list_file );
$totCovgFh->close unless( defined $cmd_list_file );

return 1;
      }

1;

so what's happening here, fr fr 🌸

  • That print line is the moment of truth: it's writing this sample's totals out to the file, and it's a tab-separated lineup of $sample, then $tot_covd, $tot_at_covd, $tot_cg_covg, and $tot_cpg_covd β€” five fields, tab-separated, one newline, no cap. 🫢
  • Then the closing braces wrap up the loop, and the filehandles get their goodbyes: $bamFh->close always happens. $cmdFh->close only fires if( defined $cmd_list_file ). And $totCovgFh->close only fires unless( defined $cmd_list_file ) β€” the way those two are mirror images of each other is honestly iconic, one closes when the command list exists, the other closes when it doesn't. πŸŽ€
  • return 1; is the lil victory lap β€” success, we did it, main character energy only. ✨
  • And then 1; at the very end, alone, vibing, because that's what a Perl module needs to be considered loaded and valid. It's giving "the last piece of the puzzle" and it always will be. πŸ‘‘
lowkey important: don't go moving that trailing 1; around or deleting it. It's not decoration, it's the whole "yes this module compiled successfully" signal. Leave it be, she's doing her job. πŸ’–

and that's a wrap on the whole page, bestie!! πŸ¦‹

This little beauty lives in the perl v5.30.3 docs and it's dated 2020-11-06 β€” shoutout to the version and the date for showing up and being part of the lore. 🌈 Go forth, compute your coverages, and remember: you understood a whole manpage today, that's genuinely slay of you. πŸŽ€πŸ’œ