-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
6,023 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Class for parsing chromosome bands for idiograms. | ||
* Format taken from https://github.com/hammerlab/idiogrammatik. | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
import ContigInterval from '../ContigInterval'; | ||
import type {CoverageCount} from '../viz/pileuputils'; | ||
import _ from 'underscore'; | ||
|
||
// chromosomal band (see https://github.com/hammerlab/idiogrammatik/blob/master/data/basic-chromosomes.json) | ||
// for an example | ||
export type Band = { | ||
start: number; | ||
end: number; | ||
name: string; | ||
value: string; | ||
} | ||
|
||
class Chromosome { | ||
name: string; | ||
bands: Band[]; | ||
position: ContigInterval<string>; | ||
|
||
constructor(chromosome: Object) { | ||
this.name = chromosome.name; | ||
this.bands = chromosome.bands; | ||
// create region for chromosome | ||
var start = _.min(this.bands, band => band.start).start; | ||
|
||
|
||
var stop = _.max(this.bands, band => band.end).end; | ||
this.position = new ContigInterval(this.name, start, stop); | ||
} | ||
|
||
getKey(): string { | ||
return this.name; | ||
} | ||
|
||
getInterval(): ContigInterval<string> { | ||
return this.position; | ||
} | ||
|
||
getCoverage(referenceSource: Object): CoverageCount { | ||
return { | ||
range: this.getInterval(), | ||
opInfo: null | ||
}; | ||
} | ||
|
||
intersects(range: ContigInterval<string>): boolean { | ||
return range.intersects(this.position); | ||
} | ||
} | ||
|
||
module.exports = Chromosome; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Fetcher/parser for gzipped Cytoband files. Cytoband files can be downloaded | ||
* or accessed from http://hgdownload.cse.ucsc.edu/goldenpath for a genome build. | ||
* | ||
* extracts CONTIG, START, END, NAME and VALUE | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
import type AbstractFile from '../AbstractFile'; | ||
import type Q from 'q'; | ||
import _ from 'underscore'; | ||
import ContigInterval from '../ContigInterval'; | ||
import Chromosome from './chromosome'; | ||
import pako from 'pako/lib/inflate'; // for gzip inflation | ||
|
||
function extractLine(cytoBandLine: string): Object { | ||
var split = cytoBandLine.split('\t'); | ||
return { | ||
contig: split[0], | ||
band: { | ||
start: Number(split[1]), | ||
end: Number(split[2]), | ||
name: split[3], | ||
value: split[4] | ||
} | ||
}; | ||
} | ||
|
||
function groupedBandsToChromosome(grouped: Object[]): Chromosome { | ||
var bands = _.map(grouped, g => g.band); | ||
return new Chromosome( | ||
{name: grouped[0].contig, | ||
bands: bands, | ||
position: new ContigInterval(grouped[0].contig, bands[0].start, bands.slice(-1)[0].end)} | ||
); | ||
} | ||
|
||
class ImmediateCytoBandFile { | ||
chrs: {[key:string]:Chromosome}; | ||
|
||
constructor(chrs: {[key:string]:Chromosome}) { | ||
this.chrs = chrs; | ||
} | ||
|
||
getFeaturesInRange(range: ContigInterval<string>): Chromosome { | ||
return this.chrs[range.contig]; | ||
} | ||
} | ||
|
||
class CytoBandFile { | ||
remoteFile: AbstractFile; | ||
immediate: Q.Promise<ImmediateCytoBandFile>; | ||
|
||
constructor(remoteFile: AbstractFile) { | ||
this.remoteFile = remoteFile; | ||
|
||
this.immediate = this.remoteFile.getAll().then(bytes => { | ||
var txt = pako.inflate(bytes, {to: 'string'}); | ||
var txtLines = _.filter(txt.split('\n'), i => i); // gets rid of empty lines | ||
var lines = txtLines.map(extractLine); | ||
return lines; | ||
}).then(lines => { | ||
// group bands by contig | ||
var grouped = _.groupBy(lines, l => l.contig); | ||
var chrs = _.mapObject(grouped, g => groupedBandsToChromosome(g)); | ||
return new ImmediateCytoBandFile(chrs); | ||
}); | ||
this.immediate.done(); | ||
} | ||
|
||
getFeaturesInRange(range: ContigInterval<string>): Q.Promise<Chromosome[]> { | ||
return this.immediate.then(immediate => { | ||
return immediate.getFeaturesInRange(range); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
CytoBandFile | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* A data source which implements generic JSON protocol. | ||
* Currently only used to load alignments. | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
import type {DataSource} from '../sources/DataSource'; | ||
import Chromosome from '../data/chromosome'; | ||
|
||
import _ from 'underscore'; | ||
import {Events} from 'backbone'; | ||
|
||
import ContigInterval from '../ContigInterval'; | ||
import type {GenomeRange} from '../types'; | ||
|
||
function create(json: string): DataSource<Chromosome> { | ||
|
||
// parse json | ||
var parsedJson = JSON.parse(json); | ||
var chromosomes: Chromosome[] = []; | ||
|
||
// fill chromosomes with json | ||
if (!_.isEmpty(parsedJson)) { | ||
chromosomes = _.values(parsedJson).map(chr => new Chromosome(chr)); | ||
} | ||
|
||
function rangeChanged(newRange: GenomeRange) { | ||
// Data is already parsed, so immediately return | ||
var range = new ContigInterval(newRange.contig, newRange.start, newRange.stop); | ||
o.trigger('newdata', range); | ||
o.trigger('networkdone'); | ||
return; | ||
} | ||
|
||
function getFeaturesInRange(range: ContigInterval<string>): Chromosome[] { | ||
return _.filter(chromosomes, chr => range.chrOnContig(chr.name)); | ||
} | ||
|
||
var o = { | ||
rangeChanged, | ||
getFeaturesInRange, | ||
|
||
on: () => {}, | ||
once: () => {}, | ||
off: () => {}, | ||
trigger: (string, any) => {} | ||
}; | ||
_.extend(o, Events); | ||
return o; | ||
} | ||
|
||
module.exports = { | ||
create | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* The "glue" between cytoBand.js and IdiogramTrack.js. | ||
* | ||
* Allows loading remote gzipped cytoband files into an Idiogram visualization. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
import _ from 'underscore'; | ||
import {Events} from 'backbone'; | ||
|
||
import ContigInterval from '../ContigInterval'; | ||
import Chromosome from '../data/chromosome'; | ||
import type {GenomeRange} from '../types'; | ||
import {CytoBandFile} from '../data/cytoBand'; | ||
import type {DataSource} from '../sources/DataSource'; | ||
|
||
import RemoteFile from '../RemoteFile'; | ||
import utils from '../utils'; | ||
|
||
var createFromCytoBandFile = function(remoteSource: CytoBandFile): DataSource<Chromosome> { | ||
// Local cache of genomic data. | ||
var contigMap: {[key:string]: Chromosome} = {}; | ||
|
||
// This either adds or removes a 'chr' as needed. | ||
function normalizeRange(range: ContigInterval<string>): ContigInterval<string> { | ||
if (contigMap[range.contig] !== undefined) { | ||
return range; | ||
} | ||
var altContig = utils.altContigName(range.contig); | ||
if (contigMap[altContig] !== undefined) { | ||
return new ContigInterval(altContig, range.start(), range.stop()); | ||
} | ||
return range; | ||
} | ||
|
||
function fetch(range: ContigInterval<string>) { | ||
remoteSource.getFeaturesInRange(range) | ||
.then(chr => { | ||
contigMap[chr.name] = chr; | ||
}).then(() => { | ||
o.trigger('newdata', range); | ||
}).done(); | ||
} | ||
|
||
function getFeaturesInRange(range: ContigInterval<string>): Chromosome[] { | ||
return [contigMap[normalizeRange(range).contig]]; | ||
} | ||
|
||
var o = { | ||
// The range here is 0-based, inclusive | ||
rangeChanged: function(newRange: GenomeRange) { | ||
// Check if this interval is already in the cache. | ||
if ( contigMap[newRange.contig] !== undefined ) { | ||
return; | ||
} | ||
fetch(new ContigInterval(newRange.contig, newRange.start, newRange.stop)); | ||
}, | ||
getFeaturesInRange, | ||
// These are here to make Flow happy. | ||
on: () => {}, | ||
once: () => {}, | ||
off: () => {}, | ||
trigger: (status: string, param: any) => {} | ||
}; | ||
_.extend(o, Events); // Make this an event emitter | ||
|
||
return o; | ||
}; | ||
|
||
function create(url:string): DataSource<Chromosome> { | ||
if (!url) { | ||
throw new Error(`Missing URL from track: ${url}`); | ||
} | ||
return createFromCytoBandFile(new CytoBandFile(new RemoteFile(url))); | ||
} | ||
|
||
|
||
module.exports = { | ||
create, | ||
createFromCytoBandFile, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.