-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
135 lines (123 loc) · 4.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
var _ = require('lodash');
var levels = ['genome', 'gene', 'transcript', 'protein'];
module.exports = {
remap: function recursiveRemap(gene, pos, source, dest, transcript_id) {
var myGene = _.pick(gene,['location','gene_structure']);
var source_idx = levels.indexOf(source);
var dest_idx = levels.indexOf(dest);
// invalid level or position < 1 supplied
if (source_idx === -1 || dest_idx === -1 || pos < 1) {
return -1;
}
if (source === dest) {
return pos;
}
var next_idx = (source_idx < dest_idx) ? source_idx + 1 : source_idx - 1;
if (next_idx === dest_idx) {
return calculatePos(myGene, pos, source, dest, transcript_id);
} else { // can't map to dest directly
var next = levels[next_idx];
return recursiveRemap(myGene, calculatePos(myGene, pos, source, next, transcript_id), next, dest, transcript_id);
}
}
};
function makeKeys(gene) {
if (!gene._transcripts) {
gene._transcripts = _.keyBy(gene.gene_structure.transcripts,'id');
}
if (!gene._exons) {
gene._exons = _.keyBy(gene.gene_structure.exons,'id');
}
}
function calculatePos(gene, pos, source, dest, transcript_id) {
switch (source) {
case 'genome':
return genomeToGene(gene, pos);
case 'gene':
return (dest === 'genome') ? geneToGenome(gene, pos) : geneToTranscript(gene, pos, transcript_id);
case 'transcript':
return (dest === 'gene') ? transcriptToGene(gene, pos, transcript_id) : transcriptToProtein(gene, pos, transcript_id);
case 'protein':
return proteinToTranscript(gene, pos, transcript_id);
}
}
function genomeToGene(gene, pos) {
if (!gene.location && gene.gene_structure.location) {
gene.location = gene.gene_structure.location;
}
if (pos < gene.location.start || pos > gene.location.end) {
return -1;
}
if (gene.location.strand === 1) {
return pos - gene.location.start + 1;
}
else {
return gene.location.end - pos + 1;
}
}
function geneToGenome(gene, pos) {
if (!gene.location && gene.gene_structure.location) {
gene.location = gene.gene_structure.location;
}
if (pos > (gene.location.end - gene.location.start + 1)) {
return -1;
}
if (gene.location.strand === 1) {
return gene.location.start + pos - 1;
}
return gene.location.end - pos + 1;
}
function geneToTranscript(gene, pos, transcript_id) {
makeKeys(gene);
transcript_id = transcript_id || gene.gene_structure.canonical_transcript;
var exonIds = gene._transcripts[transcript_id].exons;
var tpos = 0;
for (var i=0; i<exonIds.length; i++) {
var exon = gene._exons[exonIds[i]];
if (pos >= exon.start && pos <= exon.end) {
return tpos + pos - exon.start + 1;
}
tpos += exon.end - exon.start + 1;
}
return -1; // gene pos not in the transcript
}
function transcriptToGene(gene, pos, transcript_id) {
makeKeys(gene);
transcript_id = transcript_id || gene.gene_structure.canonical_transcript;
var exonIds = gene._transcripts[transcript_id].exons;
var pos_in_transcript = 0;
for (var i=0; i<exonIds.length; i++) {
var exon = gene._exons[exonIds[i]];
pos_in_transcript += exon.end - exon.start + 1;
if (pos <= pos_in_transcript) {
return exon.end - (pos_in_transcript - pos);
}
}
return -1;
}
function transcriptToProtein(gene, pos, transcript_id) {
makeKeys(gene);
transcript_id = transcript_id || gene.gene_structure.canonical_transcript;
if (!gene._transcripts[transcript_id].hasOwnProperty('cds')) {
return -1; // no CDS
}
var cds = gene._transcripts[transcript_id].cds;
if (pos > cds.end || pos < cds.start) {
return -1; // pos not in CDS
}
return (pos - cds.start) / 3 + 1;
}
function proteinToTranscript(gene, pos, transcript_id) {
makeKeys(gene);
transcript_id = transcript_id || gene.gene_structure.canonical_transcript;
if (!gene._transcripts[transcript_id].hasOwnProperty('cds')) {
return -1; // no CDS
}
var cds = gene._transcripts[transcript_id].cds;
var tpos = Math.floor(3 * (pos - 1) + 0.5) + cds.start;
if (tpos > cds.end || tpos < cds.start) {
return -1; // position out of range of CDS
}
return tpos;
}