Skip to content

Commit

Permalink
Hub track configs (#1724)
Browse files Browse the repository at this point in the history
* Add method to genome to get track configurations for menu. 
* Add genome change event
  • Loading branch information
jrobinso authored Oct 27, 2023
1 parent f330ef4 commit 948f8d4
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 59 deletions.
20 changes: 10 additions & 10 deletions dev/ucsc/hub.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@
}

const hub = await Hub.loadHub("https://hgdownload.soe.ucsc.edu/hubs/GCA/009/914/755/GCA_009914755.4/hub.txt", hubOptions)
const ref = hub.getGenomeConfig()

const igvConfig = {
reference: ref
reference: hub.getGenomeConfig()
}

// for(let tc of hub.getTrackConfigurations()) {
// for(let t of tc.tracks) {
// if(t.url.endsWith("undefined")) console.log(`${tc.label} ${t.name} ${t.url}`)
// }
// }

const browser = await igv.createBrowser(document.getElementById('igvDiv'), igvConfig)

browser.on('genomechange', (genome) => {
console.log(genome.getTrackConfigurations())
})

const selector = document.getElementById("select")
selector.addEventListener("change", () => document.getElementById("hub-input").value = selector.value)

document.getElementById("hub-input").value = "https://hgdownload.soe.ucsc.edu/hubs/GCA/009/914/755/GCA_009914755.4/hub.txt"

document.getElementById("load-genome").addEventListener("click", () =>
browser.loadGenome({url: document.getElementById("hub-input").value}))
document.getElementById("load-genome").addEventListener("click", async () => {
await browser.loadGenome({url: document.getElementById("hub-input").value})
console.log(browser.genome.getTrackConfigurations())
})


document.getElementById("load-session").addEventListener("click", () =>
browser.loadSession({url: document.getElementById("hub-input").value}))
Expand Down
6 changes: 5 additions & 1 deletion js/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ class Browser {
*/
async loadReference(genomeConfig, initialLocus) {

const genome = await Genome.loadGenome(genomeConfig)
const genome = await Genome.createGenome(genomeConfig)

const genomeChange = undefined === this.genome || (this.genome.id !== genome.id)

Expand All @@ -702,6 +702,10 @@ class Browser {
throw new Error(`Cannot set initial locus ${locus}`)
}

if(genomeChange) {
this.fireEvent('genomechange', [genome])
}

if (genomeChange && this.circularView) {
this.circularView.setAssembly({
name: this.genome.id,
Expand Down
8 changes: 6 additions & 2 deletions js/genome/genome.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {loadChromSizes} from "./chromSizes.js"

class Genome {

static async loadGenome(options) {
static async createGenome(options) {

const genome = new Genome(options)
await genome.init()
Expand All @@ -30,9 +30,9 @@ class Genome {
this.id = config.id || generateGenomeID(config)
this.name = config.name
this.nameSet = config.nameSet

}


async init() {

const config = this.config
Expand Down Expand Up @@ -278,6 +278,10 @@ class Genome {
const l = this.wgChromosomeNames.reduce((accumulator, currentValue) => accumulator += this.chromosomes.get(currentValue).bpLength, 0)
this.chromosomes.set("all", new Chromosome("all", 0, l))
}

getTrackConfigurations() {
return this.config.trackConfigurations
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {igvxhr} from "../node_modules/igv-utils/src/index.js"
import {registerTrackClass, registerTrackCreatorFunction} from "./trackFactory.js"
import TrackBase from "./trackBase.js"
import Hub from "./ucsc/ucscHub.js"
import Browser from "./browser.js"

const setApiKey = igvxhr.setApiKey

Expand Down Expand Up @@ -47,6 +48,7 @@ export default {
registerTrackClass,
registerTrackCreatorFunction,
registerFileFormats,
Hub
Hub,
uncompressSession: Browser.uncompressSession
}

58 changes: 30 additions & 28 deletions js/ucsc/ucscHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,6 @@ class Hub {
return this.genomeStanza.getProperty("defaultPos")
}

getTrackConfigurations() {

// Organize track configs by group
const trackConfigMap = new Map()
for (let c of this.#getTracksConfigs()) {
const groupName = c.group || "other"
if (trackConfigMap.has(groupName)) {
trackConfigMap.get(groupName).push(c)
} else {
trackConfigMap.set(groupName, [c])
}
}

// Build group structure
const groupStanazMap = this.groupStanzas ?
new Map(this.groupStanzas.map(groupStanza => [groupStanza.getProperty("name"), groupStanza])) :
new Map()

return Array.from(trackConfigMap.keys()).map(groupName => {
return {
label: groupStanazMap.has(groupName) ? groupStanazMap.get(groupName).getProperty("label") : groupName,
tracks: trackConfigMap.get(groupName)
}
})

}

/* Example genome stanza
genome GCF_000186305.1
taxId 176946
Expand Down Expand Up @@ -205,11 +178,40 @@ isPcr dynablat-01.soe.ucsc.edu 4040 dynamic GCF/000/186/305/GCF_000186305.1
config.tracks = this.#getTracksConfigs(filter)
}

config.trackConfigurations = this.#getGroupedTrackConfigurations()

return config
}

#getGroupedTrackConfigurations() {

// Organize track configs by group
const trackConfigMap = new Map()
for (let c of this.#getTracksConfigs()) {
const groupName = c.group || "other"
if (trackConfigMap.has(groupName)) {
trackConfigMap.get(groupName).push(c)
} else {
trackConfigMap.set(groupName, [c])
}
}

// Build group structure
const groupStanazMap = this.groupStanzas ?
new Map(this.groupStanzas.map(groupStanza => [groupStanza.getProperty("name"), groupStanza])) :
new Map()

return Array.from(trackConfigMap.keys()).map(groupName => {
return {
label: groupStanazMap.has(groupName) ? groupStanazMap.get(groupName).getProperty("label") : groupName,
tracks: trackConfigMap.get(groupName)
}
})

}

/**
* Return collection of igv track config object, organized by "group*
* Return an array of igv track config objects that satisfy the filter
*/
#getTracksConfigs(filter) {
return this.trackStanzas.filter(t => {
Expand Down
2 changes: 1 addition & 1 deletion test/testBED.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ suite("testBed", function () {
this.timeout(20000)

// Need an actual genome object for this test, not a mock object
const genome = await Genome.loadGenome({
const genome = await Genome.createGenome({
id: "hg38",
name: "Human (GRCh38/hg38)",
fastaURL: "https://s3.dualstack.us-east-1.amazonaws.com/igv.broadinstitute.org/genomes/seq/hg38/hg38.fa",
Expand Down
4 changes: 2 additions & 2 deletions test/testGenome.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suite("testGenome", function () {
wholeGenomeView: true
}

const genome = await Genome.loadGenome(reference)
const genome = await Genome.createGenome(reference)
assert.ok(genome)
assert.equal(86, genome.chromosomeNames.length)
assert.equal(genome.getCumulativeOffset("2"), 249250621)
Expand All @@ -35,7 +35,7 @@ suite("testGenome", function () {
chromSizes: "https://hgdownload.gi.ucsc.edu/hubs//GCA/011/100/615/GCA_011100615.1/GCA_011100615.1.chrom.sizes.txt"
}

const genome = await Genome.loadGenome(reference)
const genome = await Genome.createGenome(reference)
assert.ok(genome.chromosomes.size > 0)
assert.ok(genome.chromosomeNames.length > 0)

Expand Down
15 changes: 2 additions & 13 deletions test/testHubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {isString} from "../node_modules/igv-utils/src/stringUtils.js"
suite("hub.txt", function () {


test("genome", async function () {
test("genome config", async function () {

const hub = await Hub.loadHub("test/data/hubs/hub.txt")
assert.ok(hub.hub)
Expand All @@ -27,20 +27,9 @@ suite("hub.txt", function () {
assert.ok(genomeConfig.twoBitURL)
assert.ok(genomeConfig.chromAliasBbURL)
assert.ok(genomeConfig.cytobandBbURL)
assert.ok(genomeConfig.trackConfigurations.length > 0)
})

test("track configs", async function () {

const hub = await Hub.loadHub("test/data/hubs/hub.txt")
assert.ok(hub.hub)
assert.ok(hub.genomeStanza)
assert.equal(22, hub.trackStanzas.length)

const trackConfigs = hub.getTrackConfigurations()

assert.ok(trackConfigs.length > 0)

})


})
Expand Down
2 changes: 1 addition & 1 deletion test/testSeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ suite("testSeg", function () {
cytobandURL: "https://s3.amazonaws.com/igv.broadinstitute.org/genomes/seq/b37/b37_cytoband.txt"
}

const genome = await Genome.loadGenome(reference)
const genome = await Genome.createGenome(reference)
const url = "https://www.dropbox.com/s/h1rotg4xgn1bq8a/segmented_data_080520.seg.gz?dl=0"
const featureSource = FeatureSource({
format: 'seg',
Expand Down

0 comments on commit 948f8d4

Please sign in to comment.