js/bam.js
function parseBamHeader(r) {
        if (!r) {
            return callback(null, "Couldn't access BAM");
        }

        var unc = unbgzf(r, r.byteLength);
        var uncba = new Uint8Array(unc);

        var magic = readInt(uncba, 0);
        if (magic != BAM_MAGIC) {
            return callback(null, "Not a BAM file, magic=0x" + magic.toString(16));
        }
        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;

        bam.chrToIndex = {};
        bam.indexToChr = [];
        for (var i = 0; i < nRef; ++i) {
            var lName = readInt(uncba, p);
            var name = '';
            for (var j = 0; j < lName-1; ++j) {
                name += String.fromCharCode(uncba[p + 4 + j]);
            }
            var lRef = readInt(uncba, p + lName + 4);
            bam.chrToIndex[name] = i;
            if (name.indexOf('chr') == 0) {
                bam.chrToIndex[name.substring(3)] = i;
            } else {
                bam.chrToIndex['chr' + name] = i;
            }
            bam.indexToChr.push(name);

            p = p + 8 + lName;
        }

        if (bam.indices) {
            return callback(bam);
        }
    }
function parseBamHeader(r) {
        if (!r) {
            return callback(null, "Couldn't access BAM");
        }

        var unc = unbgzf(r, r.byteLength);
        var uncba = new Uint8Array(unc);

        var magic = readInt(uncba, 0);
        if (magic != BAM_MAGIC) {
            return callback(null, "Not a BAM file, magic=0x" + magic.toString(16));
        }
        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;

        bam.chrToIndex = {};
        bam.indexToChr = [];
        for (var i = 0; i < nRef; ++i) {
            var lName = readInt(uncba, p);
            var name = '';
            for (var j = 0; j < lName-1; ++j) {
                name += String.fromCharCode(uncba[p + 4 + j]);
            }
            var lRef = readInt(uncba, p + lName + 4);
            bam.chrToIndex[name] = i;
            if (name.indexOf('chr') == 0) {
                bam.chrToIndex[name.substring(3)] = i;
            } else {
                bam.chrToIndex['chr' + name] = i;
            }
            bam.indexToChr.push(name);

            p = p + 8 + lName;
        }

        if (bam.indices) {
            return callback(bam);
        }
    }
function parseBamHeader ( r ) { if ( ! r ) { return callback ( null , "Couldn't access BAM" ) ; } var unc = unbgzf ( r , r . byteLength ) ; var uncba = new Uint8Array ( unc ) ; var magic = readInt ( uncba , 0 ) ; if ( magic != BAM_MAGIC ) { return callback ( null , "Not a BAM file, magic=0x" + magic . toString ( 16 ) ) ; } 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 ; bam . chrToIndex = { } ; bam . indexToChr = [ ] ; for ( var i = 0 ; i < nRef ; ++ i ) { var lName = readInt ( uncba , p ) ; var name = '' ; for ( var j = 0 ; j < lName - 1 ; ++ j ) { name += String . fromCharCode ( uncba [ p + 4 + j ] ) ; } var lRef = readInt ( uncba , p + lName + 4 ) ; bam . chrToIndex [ name ] = i ; if ( name . indexOf ( 'chr' ) == 0 ) { bam . chrToIndex [ name . substring ( 3 ) ] = i ; } else { bam . chrToIndex [ 'chr' + name ] = i ; } bam . indexToChr . push ( name ) ; p = p + 8 + lName ; } if ( bam . indices ) { return callback ( bam ) ; } }
Fills out bam.chrToIndex and bam.indexToChr based on the first few bytes of the BAM.
Fills out bam . chrToIndex and bam . indexToChr based on the first few bytes of the BAM .