Skip to content

Commit

Permalink
Allow single coodinate bedgraph files
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Dec 7, 2024
1 parent 1409060 commit 55d3c6b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
66 changes: 39 additions & 27 deletions plugins/bed/src/BedGraphTabixAdapter/BedGraphTabixAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,48 @@ export default class BedGraphAdapter extends BaseFeatureDataAdapter {

public getFeatures(query: Region, opts: BaseOptions = {}) {
return ObservableCreate<Feature>(async observer => {
const { refName, start, end } = query
const { bedGraph } = await this.configure()
const names = (await this.getNames())?.slice(3) || []
await bedGraph.getLines(refName, start, end, {
lineCallback: (line, fileOffset) => {
const [refName, s, e, ...rest] = line.split('\t')
for (let j = 0; j < rest.length; j++) {
const uniqueId = `${this.id}-${fileOffset}-${j}`
const start = +s!
const end = +e!
const score = +rest[j]!
const source = names[j] || `col${j}`
if (score) {
observer.next(
new SimpleFeature({
id: uniqueId,
data: {
refName,
start,
end,
score,
source,
},
}),
)
const meta = await bedGraph.getMetadata()
const { columnNumbers } = meta
const colRef = columnNumbers.ref - 1
const colStart = columnNumbers.start - 1
const colEnd = columnNumbers.end - 1
const same = colStart === colEnd
const names = (await this.getNames())?.slice(same ? 2 : 3) || []
await bedGraph.getLines(
query.refName,
query.start + (same ? -1 : 0),
query.end,
{
lineCallback: (line, fileOffset) => {
const cols = line.split('\t')
const refName = cols[colRef]!
const start = +cols[colStart]!
const end = +(same ? start + 1 : cols[colEnd]!)
const rest = cols.slice(colEnd + 1)
for (let j = 0; j < rest.length; j++) {
const uniqueId = `${this.id}-${fileOffset}-${j}`
const score = Math.abs(+rest[j]!)
const source = names[j] || `col${j}`
if (score) {
observer.next(
new SimpleFeature({
id: uniqueId,
data: {
refName,
start,
end,
score,
source,
},
}),
)
}
}
}
},
...opts,
},
...opts,
})
)
observer.complete()
})
}
Expand Down
8 changes: 8 additions & 0 deletions plugins/bed/src/BedTabixAdapter/BedTabixAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BaseFeatureDataAdapter } from '@jbrowse/core/data_adapters/BaseAdapter'
import { SimpleFeature } from '@jbrowse/core/util'
import { openLocation } from '@jbrowse/core/util/io'
import { ObservableCreate } from '@jbrowse/core/util/rxjs'
import { checkStopToken } from '@jbrowse/core/util/stopToken'

import { featureData } from '../util'

Expand Down Expand Up @@ -71,15 +72,22 @@ export default class BedTabixAdapter extends BaseFeatureDataAdapter {
}

public getFeatures(query: Region, opts: BaseOptions = {}) {
const { stopToken } = opts
return ObservableCreate<Feature>(async observer => {
const meta = await this.bed.getMetadata()
const { columnNumbers } = meta
const colRef = columnNumbers.ref - 1
const colStart = columnNumbers.start - 1
const colEnd = columnNumbers.end - 1
const names = await this.getNames()
let start = performance.now()
checkStopToken(stopToken)
await this.bed.getLines(query.refName, query.start, query.end, {
lineCallback: (line, fileOffset) => {
if (performance.now() - start > 200) {
checkStopToken(stopToken)
start = performance.now()
}
observer.next(
new SimpleFeature(
featureData({
Expand Down

0 comments on commit 55d3c6b

Please sign in to comment.