Bio-ToolBox

 view release on metacpan or  search on metacpan

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

package Bio::ToolBox::db_helper::bam;

use warnings;
use strict;
use Carp;
use File::Copy;
use English qw( -no_match_vars );
use Bio::ToolBox::db_helper::constants;
use Bio::ToolBox::db_helper::alignment_callbacks;
use Bio::DB::Sam;
require Exporter;

my $parallel;
eval {
	# check for parallel support, when counting bam alignments
	require Parallel::ForkManager;
	$parallel = 1;
};

our $VERSION = '2.03';

# Exported names
our @ISA = qw(Exporter);
## no critic
## this is never intended to be used directly by end users
## and exporting everything is required
our @EXPORT = qw(
	open_bam_db
	open_indexed_fasta
	check_bam_index
	write_new_bam_file
	collect_bam_scores
	sum_total_bam_alignments
);
## use critic

# Hash of Bam chromosomes
my %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
my %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

### 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
	# we specifically do not cache the bam object or chromosome names here
	my $sam;
	eval { $sam = Bio::DB::Sam->new( -bam => $path ); };



( run in 0.717 second using v1.01-cache-2.11-cpan-140bd7fdf52 )