Bio-ToolBox

 view release on metacpan or  search on metacpan

lib/Bio/ToolBox/db_helper/bam.pm  view on Meta::CPAN

sub check_bam_index {

	# the old samtools always expects a .bam.bai index file
	# I find that relying on -autoindex yields a flaky Bio::DB::Sam object that
	# doesn't always work as expected. Best to create the index BEFORE opening

	my $bamfile = shift;
	return if ( $bamfile =~ /^(?:http | ftp)/xi );    # I can't do much with remote files

	# 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::Bam->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



( run in 0.939 second using v1.01-cache-2.11-cpan-49f99fa48dc )