<commit_before>define( [
            'dojo/_base/declare',
            'dojo/_base/array',
            'JBrowse/Util',
            'JBrowse/Store/LRUCache',
            './Util',
            './Feature'
        ],
        function( declare, array, Util, LRUCache, BAMUtil, BAMFeature ) {

var BAM_MAGIC = 21840194;
var BAI_MAGIC = 21578050;

var dlog = function(){ console.error.apply(console, arguments); };

//
// Binning (transliterated from SAM1.3 spec)
//

/* calculate bin given an alignment covering [beg,end) (zero-based, half-close-half-open) */
function reg2bin(beg, end)
{
    --end;
    if (beg>>14 == end>>14) return ((1<<15)-1)/7 + (beg>>14);
    if (beg>>17 == end>>17) return ((1<<12)-1)/7 + (beg>>17);
    if (beg>>20 == end>>20) return ((1<<9)-1)/7 + (beg>>20);
    if (beg>>23 == end>>23) return ((1<<6)-1)/7 + (beg>>23);
    if (beg>>26 == end>>26) return ((1<<3)-1)/7 + (beg>>26);
    return 0;
}

/* calculate the list of bins that may overlap with region [beg,end) (zero-based) */
var MAX_BIN = (((1<<18)-1)/7);
function reg2bins(beg, end)
{
    var k, list = [];
    --end;
    list.push(0);
    for (k = 1 + (beg>>26); k <= 1 + (end>>26); ++k) list.push(k);
    for (k = 9 + (beg>>23); k <= 9 + (end>>23); ++k) list.push(k);
    for (k = 73 + (beg>>20); k <= 73 + (end>>20); ++k) list.push(k);
    for (k = 585 + (beg>>17); k <= 585 + (end>>17); ++k) list.push(k);
    for (k = 4681 + (beg>>14); k <= 4681 + (end>>14); ++k) list.push(k);
    return list;
}

var Chunk = function(minv,maxv) {
        this.minv = minv;
        this.maxv = maxv;
};

var readInt   = BAMUtil.readInt;
var readVirtualOffset = BAMUtil.readVirtualOffset;

var BamFile = declare( null,


/**
 * @lends JBrowse.Store.SeqFeature.BAM.File
 */
{

    /**
     * Low-level BAM file reading code.
     *
     * Adapted by <NAME> from bam.js in the Dalliance Genome
     * Explorer which is copyright <NAME> 2006-2010
     * @constructs
     */
    constructor: function( args ) {
        this.store = args.store;
        this.data  = args.data;
        this.bai   = args.bai;
    },

    init: function( args ) {
        var bam = this;
        var successCallback = args.success || function() {};
        var failCallback = args.failure || function() {};

        this._readBAI( dojo.hitch( this, function() {
            this._readBAMheader( function() {
                successCallback();
            }, failCallback );
        }), failCallback );
    },

    _readBAI: function( successCallback, failCallback ) {
        // Do we really need to fetch the whole thing? :-(
        this.bai.fetch( dojo.hitch( this, function(header) {
            if (!header) {
                dlog("Couldn't access BAI");
                failCallback();
                return;
            }

            var uncba = new Uint8Array(header);
            if( readInt(uncba, 0) != BAI_MAGIC) {
                dlog('Not a BAI file');
                failCallback();
                return;
            }

            var nref = readInt(uncba, 4);

            this.indices = [];

            var p = 8;
            var minVO; // get smallest virtual offset in the indexes, which indicates where the BAM header ends
            for (var ref = 0; ref < nref; ++ref) {
                var blockStart = p;
                var nbin = readInt(uncba, p); p += 4;
                for (var b = 0; b < nbin; ++b) {
                    var bin = readInt(uncba, p);
                    var nchnk = readInt(uncba, p+4);
                    p += 8 + (nchnk * 16);
                }
                var nintv = readInt(uncba, p); p += 4;
                var firstVO = readVirtualOffset(uncba,p);
                if( ! this.minAlignmentVO || this.minAlignmentVO.cmp( firstVO ) < 0 )
                    this.minAlignmentVO = firstVO;
                //console.log( ref, ''+firstVO );
                p += (nintv * 8);
                if (nbin > 0) {
                    this.indices[ref] = new Uint8Array(header, blockStart, p - blockStart);
                }
            }

            successCallback( this.indices, this.minAlignmentVO );
        }));
    },

    _readBAMheader: function( successCallback, failCallback ) {
        this.data.read( 0, this.minAlignmentVO.block, dojo.hitch( this, function(r) {
//        this.data.read(0, (1<<16)-1, dojo.hitch( this, function(r) {
            var unc = BAMUtil.unbgzf(r);
            var uncba = new Uint8Array(unc);

            if( readInt(uncba, 0) != BAM_MAGIC) {
                dlog('Not a BAM file');
                failCallback();
                return;
            }

            var headLen = readInt(uncba, 4);
            // var header = '';
            // for (var i = 0; i < headLen; ++i) {
            //     header += String.fromCharCode(uncba[i + 8]);
            // }

            var nRef = readInt(uncba, headLen + 8);
            var p = headLen + 12;

            this.chrToIndex = {};
            this.indexToChr = [];
            for (var i = 0; i < nRef; ++i) {<commit_msg>improve BAM backend error handling<commit_after>define( [
            'dojo/_base/declare',
            'dojo/_base/array',
            'JBrowse/Util',
            'JBrowse/Store/LRUCache',
            './Util',
            './Feature'
        ],
        function( declare, array, Util, LRUCache, BAMUtil, BAMFeature ) {

var BAM_MAGIC = 21840194;
var BAI_MAGIC = 21578050;

var dlog = function(){ console.error.apply(console, arguments); };

//
// Binning (transliterated from SAM1.3 spec)
//

/* calculate bin given an alignment covering [beg,end) (zero-based, half-close-half-open) */
function reg2bin(beg, end)
{
    --end;
    if (beg>>14 == end>>14) return ((1<<15)-1)/7 + (beg>>14);
    if (beg>>17 == end>>17) return ((1<<12)-1)/7 + (beg>>17);
    if (beg>>20 == end>>20) return ((1<<9)-1)/7 + (beg>>20);
    if (beg>>23 == end>>23) return ((1<<6)-1)/7 + (beg>>23);
    if (beg>>26 == end>>26) return ((1<<3)-1)/7 + (beg>>26);
    return 0;
}

/* calculate the list of bins that may overlap with region [beg,end) (zero-based) */
var MAX_BIN = (((1<<18)-1)/7);
function reg2bins(beg, end)
{
    var k, list = [];
    --end;
    list.push(0);
    for (k = 1 + (beg>>26); k <= 1 + (end>>26); ++k) list.push(k);
    for (k = 9 + (beg>>23); k <= 9 + (end>>23); ++k) list.push(k);
    for (k = 73 + (beg>>20); k <= 73 + (end>>20); ++k) list.push(k);
    for (k = 585 + (beg>>17); k <= 585 + (end>>17); ++k) list.push(k);
    for (k = 4681 + (beg>>14); k <= 4681 + (end>>14); ++k) list.push(k);
    return list;
}

var Chunk = function(minv,maxv) {
        this.minv = minv;
        this.maxv = maxv;
};

var readInt   = BAMUtil.readInt;
var readVirtualOffset = BAMUtil.readVirtualOffset;

var BamFile = declare( null,


/**
 * @lends JBrowse.Store.SeqFeature.BAM.File
 */
{

    /**
     * Low-level BAM file reading code.
     *
     * Adapted by <NAME> from bam.js in the Dalliance Genome
     * Explorer which is copyright <NAME> 2006-2010
     * @constructs
     */
    constructor: function( args ) {
        this.store = args.store;
        this.data  = args.data;
        this.bai   = args.bai;
    },

    init: function( args ) {
        var bam = this;
        var successCallback = args.success || function() {};
        var failCallback = args.failure || function() {};

        this._readBAI( dojo.hitch( this, function() {
            this._readBAMheader( function() {
                successCallback();
            }, failCallback );
        }), failCallback );
    },

    _readBAI: function( successCallback, failCallback ) {
        // Do we really need to fetch the whole thing? :-(
        this.bai.fetch( dojo.hitch( this, function(header) {
            if (!header) {
                dlog("No data read from BAM index (BAI) file");
                failCallback("No data read from BAM index (BAI) file");
                return;
            }

            if( ! Uint8Array ) {
                dlog('Browser does not support typed arrays');
                failCallback('Browser does not support typed arrays');
                return;
            }

            var uncba = new Uint8Array(header);
            if( readInt(uncba, 0) != BAI_MAGIC) {
                dlog('Not a BAI file');
                failCallback('Not a BAI file');
                return;
            }

            var nref = readInt(uncba, 4);

            this.indices = [];

            var p = 8;
            var minVO; // get smallest virtual offset in the indexes, which indicates where the BAM header ends
            for (var ref = 0; ref < nref; ++ref) {
                var blockStart = p;
                var nbin = readInt(uncba, p); p += 4;
                for (var b = 0; b < nbin; ++b) {
                    var bin = readInt(uncba, p);
                    var nchnk = readInt(uncba, p+4);
                    p += 8 + (nchnk * 16);
                }
                var nintv = readInt(uncba, p); p += 4;
                var firstVO = readVirtualOffset(uncba,p);
                if( ! this.minAlignmentVO || this.minAlignmentVO.cmp( firstVO ) < 0 )
                    this.minAlignmentVO = firstVO;
                //console.log( ref, ''+firstVO );
                p += (nintv * 8);
                if (nbin > 0) {
                    this.indices[ref] = new Uint8Array(header, blockStart, p - blockStart);
                }
            }

            successCallback( this.indices, this.minAlignmentVO );
        }), failCallback );
    },

    _readBAMheader: function( successCallback, failCallback ) {
        this.data.read( 0, this.minAlignmentVO.block, dojo.hitch( this, function(r) {
//        this.data.read(0, (1<<16)-1, dojo.hitch( this, function(r) {
            var unc = BAMUtil.unbgzf(r);
            var uncba = new Uint8Array(unc);

            if( readInt(uncba, 0) != BAM_MAGIC) {
                dlog('Not a BAM file');
                failCallback();
                return;
            }

            var headLen = readInt(uncba, 4);
            // var header = '';
            // for (var i = 0; i < headLen; ++i) {
            //     header += String.fromCharCode(uncba[i + 8]);
            // }

            var nRef = readInt(uncba, headLen + 8);
            var p = headLen + 12;

            this.chrToIndex = {};
            this.indexToChr = [];
            for (var i = 0; i < nRef; ++i) {
{'fraction_non_alphanumeric': 0.15618053542089494, 'fraction_numerical': 0.03102348506813569, 'mean_word_length': 1.7089005235602095, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 109, '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': '3856885', 'n_tokens_mistral': 3637, 'n_tokens_neox': 3312, 'n_words': 863}