<commit_before>package Bio::ToolBox::db_helper::hts;

# modules
require Exporter;
use strict;
use Carp;
use File::Copy;
use Bio::ToolBox::db_helper::constants;
use Bio::ToolBox::db_helper::alignment_callbacks;
use Bio::DB::HTS;
our $parallel;
eval {
	# check for parallel support, when counting bam alignments
	require Parallel::ForkManager;
	$parallel = 1;
};
our $VERSION = '1.62';

# Exported names
our @ISA = qw(Exporter);
our @EXPORT = qw(
	open_bam_db
	open_indexed_fasta
	check_bam_index
	write_new_bam_file
	collect_bam_scores
	sum_total_bam_alignments
);

# Hash of Bam chromosomes
our %BAM_CHROMOS;
	# sometimes user may request a chromosome that's not in the bigfile
	# that could lead to an exception
	# we will record the chromosomes list in this hash
	# $BAM_CHROMOS{bamfile}{chromos}
	# we also record the chromosome name variant with or without chr prefix
	# to accommodate different naming conventions

# Opened Bam db objects
our %OPENED_BAM;
	# a cache for opened Bam files 
	# caching here is only for local purposes of collecting scores
	# db_helper also provides caching of db objects but with option to force open in
	# the case of forking processes - we don't have that here

# Lookup hash for caching callback methods
our %CALLBACKS;

# The true statement
1; 


### Open a bam database connection
sub open_bam_db {
	my $bamfile = shift;
	
	# check the path
	my $path = $bamfile;
	$path =~ s/^file://; # strip the file prefix if present
	
	# check for bam index
	check_bam_index($path);
	
	# open the bam database object
	my $sam;
	eval {
		$sam = Bio::DB::HTS->new(-bam => $path);
	};
	return unless $sam;
	# we specifically do not cache the bam object or chromosome names here
	
	return $sam;
}


### Open an indexed fasta file
sub open_indexed_fasta {
	my $fasta = shift;
	eval {require Bio::DB::HTS::Faidx}; # this should be available
	my $fai = Bio::DB::HTS::Faidx->new($fasta);
# 	eval {$fai};
		# this should automatically build the fai index if possible
	return $fai if defined $fai;
}


### Check for a bam index 
sub check_bam_index {
	# HTSlib can accept either .bam.bai or .bai, but the Perl adapter 
	# still accepts just .bam.bai, I think....
	my $bamfile = shift;
	
	# we will check the modification time to make sure index is newer
	my $bam_mtime = (stat($bamfile))[9];
	
	# optional index names
	my $bam_index = "$bamfile.bai"; # .bam.bai
	my $alt_index = $bamfile;
	$alt_index =~ s/bam$/bai/i; # picard uses .bai instead of .bam.bai as samtools does
	
	# check for existing index
	if (-e $bam_index) {
		if ( (stat($bam_index))[9] < $bam_mtime) {
			# index is older than bam file
			print " index $bam_index is old. Attempting to update time stamp.\n";
			my $now = time;
			utime($now, $now, $bam_index) || Bio::DB::HTSfile->reindex($bamfile);
		}
	}
	elsif (-e $alt_index) {
		if ( (stat($alt_index))[9] < $bam_mtime) {
			# index is older than bam file
			print " index $alt_index is old.\n";
		}
		# reuse this index
		copy($alt_index, $bam_index);
	}
	else {
		# make a new index
		Bio::DB::HTSfile->reindex($bamfile);
	}
}<commit_msg>remove unnecessary time stamp update and file rename<commit_after>package Bio::ToolBox::db_helper::hts;

# modules
require Exporter;
use strict;
use Carp;
use Bio::ToolBox::db_helper::constants;
use Bio::ToolBox::db_helper::alignment_callbacks;
use Bio::DB::HTS;
our $parallel;
eval {
	# check for parallel support, when counting bam alignments
	require Parallel::ForkManager;
	$parallel = 1;
};
our $VERSION = '1.63';

# Exported names
our @ISA = qw(Exporter);
our @EXPORT = qw(
	open_bam_db
	open_indexed_fasta
	check_bam_index
	write_new_bam_file
	collect_bam_scores
	sum_total_bam_alignments
);

# Hash of Bam chromosomes
our %BAM_CHROMOS;
	# sometimes user may request a chromosome that's not in the bigfile
	# that could lead to an exception
	# we will record the chromosomes list in this hash
	# $BAM_CHROMOS{bamfile}{chromos}
	# we also record the chromosome name variant with or without chr prefix
	# to accommodate different naming conventions

# Opened Bam db objects
our %OPENED_BAM;
	# a cache for opened Bam files 
	# caching here is only for local purposes of collecting scores
	# db_helper also provides caching of db objects but with option to force open in
	# the case of forking processes - we don't have that here

# Lookup hash for caching callback methods
our %CALLBACKS;

# The true statement
1; 


### Open a bam database connection
sub open_bam_db {
	my $bamfile = shift;
	
	# check the path
	my $path = $bamfile;
	$path =~ s/^file://; # strip the file prefix if present
	
	# check for bam index
	check_bam_index($path);
	
	# open the bam database object
	my $sam;
	eval {
		$sam = Bio::DB::HTS->new(-bam => $path);
	};
	return unless $sam;
	# we specifically do not cache the bam object or chromosome names here
	
	return $sam;
}


### Open an indexed fasta file
sub open_indexed_fasta {
	my $fasta = shift;
	eval {require Bio::DB::HTS::Faidx}; # this should be available
	my $fai = Bio::DB::HTS::Faidx->new($fasta);
		# this should automatically build the fai index if possible
	return $fai if defined $fai;
}


### Check for a bam index 
sub check_bam_index {
	# HTSlib can accept either .bam.bai or .bai
	my $bamfile = shift;
	
	# optional index names
	my $bam_index = "$bamfile.bai"; # .bam.bai
	my $alt_index = $bamfile;
	$alt_index =~ s/bam$/bai/i; # .bai
	
	# check for existing index
	if (not -e $bam_index and not -e $alt_index)  {
		# make a new index
		Bio::DB::HTSfile->reindex($bamfile);
	}
}
{'fraction_non_alphanumeric': 0.10591222343126587, 'fraction_numerical': 0.0023576351106274936, 'mean_word_length': 4.11595547309833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '905940', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1938, 'n_words': 674}