Skip to content

Commit

Permalink
Add automatic aliases for chromosome name case differences (e.g. Chr1…
Browse files Browse the repository at this point in the history
… vs chr1 vs CHR1)
  • Loading branch information
jrobinso committed Dec 9, 2024
1 parent 4164945 commit 5a0d7ad
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion js/feature/textFeatureSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class TextFeatureSource extends BaseFeatureSource {
// indicating whole chromosome should be read at once.
if ((!visibilityWindow || visibilityWindow <= 0) && this.config.expandQuery !== false) {
// Whole chromosome
const chromosome = this.genome ? this.genome.getChromosome(queryChr) : undefined
const chromosome = this.genome ? this.genome.getChromosome(chr) : undefined
intervalStart = 0
intervalEnd = Math.max(chromosome ? chromosome.bpLength : Number.MAX_SAFE_INTEGER, end)
} else if (visibilityWindow > (end - start) && this.config.expandQuery !== false) {
Expand Down
2 changes: 2 additions & 0 deletions js/genome/chromAliasBB.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BWReader from "../bigwig/bwReader.js"
import ChromAliasDefaults from "./chromAliasDefaults.js"

/**
* Chromosome alias source backed by a UCSC bigbed file
Expand Down Expand Up @@ -62,6 +63,7 @@ class ChromAliasBB {
if (!this.aliasRecordCache.has(alias)) {
const aliasRecord = await this.reader.search(alias)
if (aliasRecord) {
ChromAliasDefaults.addCaseAliases(aliasRecord)
for (let key of Object.keys(aliasRecord)) {
if ("start" !== key && "end" !== key) {
this.aliasRecordCache.set(aliasRecord[key], aliasRecord)
Expand Down
22 changes: 20 additions & 2 deletions js/genome/chromAliasDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ class ChromAliasDefaults {
}
}


for (let rec of aliasRecords) {
ChromAliasDefaults.addCaseAliases(rec)
for (let a of Object.values(rec)) {
this.aliasRecordCache.set(a, rec)
}
this.aliasRecordCache.set(rec.chr.toLowerCase(), rec)
this.aliasRecordCache.set(rec.chr.toUpperCase(), rec)
}

}
Expand All @@ -162,6 +162,24 @@ class ChromAliasDefaults {
return tokens[tokens.length - 1]
}

static addCaseAliases(aliasRecord) {

// Add some aliases for case insensitivy
const upper = aliasRecord.chr.toUpperCase()
const lower = aliasRecord.chr.toLowerCase()
const cap = aliasRecord.chr.charAt(0).toUpperCase() + aliasRecord.chr.slice(1)
if(aliasRecord.chr !== upper) {
aliasRecord["_uppercase"] = upper
}
if(aliasRecord.chr !== lower) {
aliasRecord["_lowercase"] = lower
}
if(aliasRecord.chr !== cap) {
aliasRecord["_cap"] = cap
}

}

}

export default ChromAliasDefaults
11 changes: 7 additions & 4 deletions js/genome/chromAliasFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* @param config
* @returns {Promise<*[]>}
*/
import {isNumber, buildOptions} from "../util/igvUtils.js"
import {buildOptions} from "../util/igvUtils.js"
import {igvxhr, StringUtils} from "../../node_modules/igv-utils/src/index.js"
import ChromAliasDefaults from "./chromAliasDefaults.js"

class ChromAliasFile {

Expand Down Expand Up @@ -73,14 +74,16 @@ class ChromAliasFile {
}

const aliasRecord = {chr}
ChromAliasDefaults.addCaseAliases(aliasRecord)
for (let i = 0; i < tokens.length; i++) {
const key = this.headings ? this.headings[i] : i
aliasRecord[key] = tokens[i]
this.aliasRecordCache.set(tokens[i], aliasRecord)
}

this.aliasRecordCache.set(chr.toLowerCase(), aliasRecord)
this.aliasRecordCache.set(chr.toUpperCase(), aliasRecord)
for (let a of Object.values(aliasRecord)) {
this.aliasRecordCache.set(a, aliasRecord)
}

}
}
}
Expand Down
15 changes: 15 additions & 0 deletions js/genome/genome.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ class Genome {
if (!aliasRecord && chr !== chr.toLowerCase()) {
aliasRecord = await this.chromAlias.search(chr.toLowerCase())
}
if(aliasRecord) {
// Add some aliases for case insensitivy
const upper = aliasRecord.chr.toUpperCase()
const lower = aliasRecord.chr.toLowerCase()
const cap = aliasRecord.chr.charAt(0).toUpperCase() + aliasRecord.chr.slice(1)
if(aliasRecord.chr !== upper) {
aliasRecord["_uppercase"] = upper
}
if(aliasRecord.chr !== lower) {
aliasRecord["_lowercase"] = lower
}
if(aliasRecord.chr !== cap) {
aliasRecord["_cap"] = cap
}
}
this.#aliasRecordCache.set(chr, aliasRecord) // Set even if undefined to prevent recurrent searches
return aliasRecord
}
Expand Down

0 comments on commit 5a0d7ad

Please sign in to comment.