1313
1414#include " tin_stats.h"
1515
16- std::unordered_map<int , int > getReadDepths (htsFile* bam_file, hts_idx_t * idx, bam_hdr_t * header, std::string chr, int start, int end)
16+ std::unordered_map<int , int > getReadDepths (htsFile* bam_file, hts_idx_t * idx, bam_hdr_t * header, std::string chr, int start, int end, bool transcript_strand )
1717{
1818 // Set up the region to fetch reads (1-based)
1919 std::string region = chr + " :" + std::to_string (start) + " -" + std::to_string (end);
@@ -46,6 +46,13 @@ std::unordered_map<int, int> getReadDepths(htsFile* bam_file, hts_idx_t* idx, ba
4646 continue ;
4747 }
4848
49+ // Skip if on a different strand
50+ bool read_strand = (record->core .flag & BAM_FREVERSE) ? false : true ;
51+ if (read_strand != transcript_strand) {
52+ skip_count++;
53+ continue ;
54+ }
55+
4956 read_count++;
5057
5158 // Clear positions for each read
@@ -194,10 +201,13 @@ void calculateTIN(TINStats* tin_stats, const std::string& gene_bed, const std::s
194201
195202 // Get the index for the BAM file
196203 hts_idx_t * index = sam_index_load (bam_file, bam_filepath.c_str ());
204+ if (index == NULL ) {
205+ std::cerr << " Error loading BAM index" << std::endl;
206+ exit (1 );
207+ }
197208
198209 // Read the gene BED file
199210 std::ifstream gene_bed_file (gene_bed);
200-
201211 if (!gene_bed_file.is_open ()) {
202212 std::cerr << " Error opening gene BED file" << std::endl;
203213 exit (1 );
@@ -242,6 +252,12 @@ void calculateTIN(TINStats* tin_stats, const std::string& gene_bed, const std::s
242252 continue ;
243253 }
244254
255+ std::cout << " Calculating TIN for transcript " << name << " with strand " << strand << std::endl;
256+ bool strand_bool = true ;
257+ if (strand == " -" ) {
258+ strand_bool = false ;
259+ }
260+
245261 // Remove the last comma from the exon sizes and starts strings
246262 if (exon_sizes_str.back () == ' ,' ) {
247263 exon_sizes_str.pop_back ();
@@ -278,7 +294,7 @@ void calculateTIN(TINStats* tin_stats, const std::string& gene_bed, const std::s
278294 int exon_end = exon_start + exon_sizes[i] - 1 ;
279295
280296 // Get the depths and cumulative depths for the region
281- std::unordered_map<int , int > exon_depths = getReadDepths (bam_file, index, header, chrom, exon_start, exon_end);
297+ std::unordered_map<int , int > exon_depths = getReadDepths (bam_file, index, header, chrom, exon_start, exon_end, strand_bool );
282298 for (const auto & depth : exon_depths) {
283299 C[depth.first ] = depth.second ;
284300 }
@@ -287,7 +303,7 @@ void calculateTIN(TINStats* tin_stats, const std::string& gene_bed, const std::s
287303 // Get the read depths for the transcript start+1 and end
288304 std::vector<int > transcript_positions = {start + 1 , end};
289305 for (const auto & position : transcript_positions) {
290- std::unordered_map<int , int > transcript_depths = getReadDepths (bam_file, index, header, chrom, position, position);
306+ std::unordered_map<int , int > transcript_depths = getReadDepths (bam_file, index, header, chrom, position, position, strand_bool );
291307 for (const auto & depth : transcript_depths) {
292308 C[depth.first ] = depth.second ;
293309 }
0 commit comments