Skip to content

Commit

Permalink
Use BAM record parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Sep 18, 2018
1 parent 55b0a9e commit 51751bd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/JBrowse/Model/BlobFilehandleWrapper.js
@@ -1,5 +1,4 @@
const { Buffer } = cjsRequire('buffer')

/**
* Wraps a XHRBlob or FileBlob in a new promise-based API (which is
* the upcoming node fs.promises API) for use by newer code.
Expand All @@ -12,6 +11,7 @@ class BlobFilehandleWrapper {
async read(buffer, offset = 0, length, position) {
const data = await this.blob.readBufferPromise(position, length)
data.copy(buffer, offset)
return data.length
}

async readFile() {
Expand Down
4 changes: 2 additions & 2 deletions src/JBrowse/Model/XHRBlob.js
Expand Up @@ -147,15 +147,15 @@ var XHRBlob = declare( FileBlob,
},

read( offset, length, callback, failCallback ) {
globalCache.getRange(this.url, this.start + offset, length)
globalCache.getRange(this.url, this.start + offset||0, length)
.then(
this._getResponseArrayBuffer.bind(this,callback),
failCallback,
)
},

async readBufferPromise(offset, length) {
const range = await globalCache.getRange(this.url, this.start + offset, length)
const range = await globalCache.getRange(this.url, this.start + offset||0, length)
return range.buffer
},

Expand Down
124 changes: 58 additions & 66 deletions src/JBrowse/Store/SeqFeature/BAM.js
Expand Up @@ -9,16 +9,16 @@ const BlobFilehandleWrapper = cjsRequire('../../Model/BlobFilehandleWrapper')

class BamSlightlyLazyFeature {

_get_name() { return this.record.readName }
_get_start() { return this.record.alignmentStart-1 }
_get_end() { return this.record.alignmentStart+this.record.lengthOnRef-1 }
_get_name() { return this.record.get('name') }
_get_start() { return this.record.get('start') }
_get_end() { return this.record.get('end') }
_get_type() { return 'match'}
_get_mapping_quality() { return this.record.mappingQuality}
_get_flags() { return `0x${this.record.flags.toString(16)}`}
_get_strand() { return this.record.isReverseComplemented() ? -1 : 1 }
_get_read_group_id() { return this.record.readGroupId }
_get_qual() { return (this.record.qualityScores || []).map(q => q+33).join(' ')}
_get_seq() { return this.record.readBases}
_get_qual() { return this.record.get('qual')}
_get_cigar() { return this.record.get('cigar')}
_get_seq_id() { return this._store._refIdToName(this.record.sequenceId)}
_get_qc_failed() { return this.record.isFailedQc()}
_get_secondary_alignment() { return this.record.isSecondary()}
Expand All @@ -33,8 +33,9 @@ class BamSlightlyLazyFeature {
_get_next_seq_id() { return this.record.mate ? this._store._refIdToName(this.record.mate.sequenceId) : undefined }
_get_next_segment_position() { return this.record.mate
? ( this._store._refIdToName(this.record.mate.sequenceId)+':'+this.record.mate.alignmentStart) : undefined}
_get_tags() { return this.record.tags }
_get_tags() { console.log('tags'); return this.record.tags }
_get_seq() { return this.record.getReadBases() }
_get_md() { return this.record.get('md') }

constructor(record, store) {
this.record = record
Expand All @@ -49,7 +50,7 @@ class BamSlightlyLazyFeature {
}

id() {
return this.record.uniqueId + 1
return this.record.get('id')
}

get(field) {
Expand Down Expand Up @@ -84,18 +85,7 @@ define( [
SimpleFeature,
) {

return declare( [ SeqFeatureStore, DeferredStatsMixin, DeferredFeaturesMixin, GlobalStatsEstimationMixin ],

/**
* @lends JBrowse.Store.SeqFeature.CRAM
*/
{
/**
* Data backend for reading feature data directly from a
* web-accessible CRAM file.
*
* @constructs
*/
return declare( [ SeqFeatureStore, DeferredStatsMixin, DeferredFeaturesMixin, GlobalStatsEstimationMixin ], {
constructor: function( args ) {

let dataBlob
Expand Down Expand Up @@ -138,7 +128,8 @@ return declare( [ SeqFeatureStore, DeferredStatsMixin, DeferredFeaturesMixin, Gl
// estimation doesn't time out
this.bam.hasDataForReferenceSequence(0)
.then(() => this.bam.getHeader())
.then(() => {
.then((header) => this._setSamHeader(header))
.then((res) => {
this._deferred.features.resolve({success:true});
})
.then(() => this._estimateGlobalStats())
Expand All @@ -154,50 +145,51 @@ return declare( [ SeqFeatureStore, DeferredStatsMixin, DeferredFeaturesMixin, Gl
this.storeTimeout = args.storeTimeout || 3000;
},

// // process the parsed SAM header from the cram file
// _setSamHeader(samHeader) {
// this._samHeader = {}

// // use the @SQ lines in the header to figure out the
// // mapping between ref seq ID numbers and names
// const refSeqIdToName = []
// const refSeqNameToId = {}
// const sqLines = samHeader.filter(l => l.tag === 'SQ')
// sqLines.forEach((sqLine, seqId) => {
// sqLine.data.forEach(item => {
// if (item.tag === 'SN') {
// // this is the seq name
// const seqName = item.value
// refSeqNameToId[seqName] = seqId
// refSeqIdToName[seqId] = seqName
// }
// })
// })
// if (refSeqIdToName.length) {
// this._samHeader.refSeqIdToName = refSeqIdToName
// this._samHeader.refSeqNameToId = refSeqNameToId
// }
// },
// process the parsed SAM header from the cram file
_setSamHeader(samHeader) {
console.log('here',samHeader)
this._samHeader = {}

// use the @SQ lines in the header to figure out the
// mapping between ref seq ID numbers and names
const refSeqIdToName = []
const refSeqNameToId = {}
const sqLines = samHeader.filter(l => l.tag === 'SQ')
sqLines.forEach((sqLine, seqId) => {
sqLine.data.forEach(item => {
if (item.tag === 'SN') {
// this is the seq name
const seqName = item.value
refSeqNameToId[seqName] = seqId
refSeqIdToName[seqId] = seqName
}
})
})
if (refSeqIdToName.length) {
this._samHeader.refSeqIdToName = refSeqIdToName
this._samHeader.refSeqNameToId = refSeqNameToId
}
},

// _refNameToId(refName) {
// // use info from the SAM header if possible, but fall back to using
// // the ref seq order from when the browser's refseqs were loaded
// if (this._samHeader.refSeqNameToId)
// return this._samHeader.refSeqNameToId[refName]
// else
// return this.browser.getRefSeqNumber(refName)
// },
_refNameToId(refName) {
// use info from the SAM header if possible, but fall back to using
// the ref seq order from when the browser's refseqs were loaded
if (this._samHeader.refSeqNameToId)
return this._samHeader.refSeqNameToId[refName]
else
return this.browser.getRefSeqNumber(refName)
},

// _refIdToName(refId) {
// // use info from the SAM header if possible, but fall back to using
// // the ref seq order from when the browser's refseqs were loaded
// if (this._samHeader.refSeqIdToName) {
// return this._samHeader.refSeqIdToName[refId]
// } else {
// let ref = this.browser.getRefSeqById(refId)
// return ref ? ref.name : undefined
// }
// },
_refIdToName(refId) {
// use info from the SAM header if possible, but fall back to using
// the ref seq order from when the browser's refseqs were loaded
if (this._samHeader.refSeqIdToName) {
return this._samHeader.refSeqIdToName[refId]
} else {
let ref = this.browser.getRefSeqById(refId)
return ref ? ref.name : undefined
}
},

// _getRefSeqStore() {
// return new Promise((resolve,reject) => {
Expand Down Expand Up @@ -275,10 +267,10 @@ return declare( [ SeqFeatureStore, DeferredStatsMixin, DeferredFeaturesMixin, Gl
endCallback()
})
.catch(err => {
// map the BamSizeLimitError to JBrowse Errors.DataOverflow
if (err instanceof BamSizeLimitError) {
err = new Errors.DataOverflow(err)
}
// // map the BamSizeLimitError to JBrowse Errors.DataOverflow
// if (err instanceof BamSizeLimitError) {
// err = new Errors.DataOverflow(err)
// }

errorCallback(err)
})
Expand Down

0 comments on commit 51751bd

Please sign in to comment.