-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracks.mjs
246 lines (200 loc) · 7.28 KB
/
tracks.mjs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* tracks.js
*/
import path from "node:path";
import fs from "node:fs";
import md5 from "md5";
import {groupBy, map, path as prop} from "rambda";
import config from "../config.js";
import {NODE_BASE_URL, MERGED_TRACKS_DIR} from "../envConfig.js";
import Metadata from "./metadata.js";
import Samples from "./samples.mjs";
import {ASSAY_RNA_SEQ} from "../helpers/assays.mjs";
import bigWigMerge from "../helpers/bigwig-merge.js";
import bigWigChromosomeLength from "../helpers/bigwig-chromosome-length.js";
import {boxPlot, getDomain, PLOT_HEIGHT, PLOT_WIDTH} from "../helpers/boxplot.mjs";
import cache from "../helpers/cache.mjs";
import {belowThreshold} from "../helpers/censorship.mjs";
import {DEFAULT_CONDITIONS} from "../helpers/defaultValues.mjs";
import {donorLookup} from "../helpers/donors.mjs";
import {normalizeChrom, GENOTYPE_STATES, GENOTYPE_STATE_NAMES} from "../helpers/genome.mjs";
import valueAt from "../helpers/value-at.mjs";
export default {
get,
values,
group,
merge,
calculate,
plot,
};
const strandToView = {
"+": "signal_forward",
"-": "signal_reverse",
};
const groupByEthnicity = groupBy(prop("ethnicity"));
const mapToData = map(prop("data"));
const conditions = config.conditions ?? DEFAULT_CONDITIONS;
const TRACK_VALUES_CACHE_EXPIRY = 60 * 60 * 24 * 180; // 180 days TODO: config variable
// Methods
function get(peak) {
const {snp: {chrom, position}} = peak;
return Samples.queryMap(chrom, position)
.then(info => Metadata.getTracks(info.samples, peak));
}
async function values(peak, usePrecomputed = false) {
const k = `values:${peak.id}${usePrecomputed ? ':pre' : ''}`;
const chrom = normalizeChrom(peak.feature.chrom); // TODO: bigWigChrTransform
await cache.open();
// noinspection JSCheckFunctionSignatures
const cv = await cache.getJSON(k);
if (cv) return cv;
const tracks = await get(peak);
let getValueForTrack = track => valueAt(track.path, {
chrom,
start: peak.feature.start,
end: peak.feature.end,
});
if (usePrecomputed) {
// Replace getter function with one which extracts the precomputed point value.
getValueForTrack = track => {
const pointIdx = donorLookup.indexOf(`${track.donor}_${track.condition}`);
if (pointIdx === -1) return Promise.resolve(undefined);
// Return the point value, turning SQL NULLs into undefined if the value isn't present for this sample
return Promise.resolve(peak.feature.points?.[pointIdx] ?? undefined);
};
}
const result = (await Promise.all(tracks.filter(track =>
// RNA-seq results are either forward or reverse strand; we only want tracks from the direction
// of the selected peak (otherwise results will appear incorrectly, and we'll have 2x the # of
// values we should in some cases.)
track.assay !== ASSAY_RNA_SEQ || track.view === strandToView[peak.feature.strand]
).map(track =>
getValueForTrack(track).then(value => (value === undefined ? undefined : {
donor: track.donor,
assay: track.assay,
condition: track.condition,
ethnicity: track.ethnicity,
type: track.type, // Genotype: REF/HET/HOM
data: value,
}))
))).filter(v => v !== undefined);
await cache.setJSON(k, result, TRACK_VALUES_CACHE_EXPIRY);
return result;
}
function group(tracks) {
return Object.fromEntries(
Object.entries(groupBy(x => x.condition, tracks))
.map(([condition, tracks]) =>
[condition, groupBy(prop('type'), tracks)])
);
}
function calculate(tracksByCondition) {
Object.keys(tracksByCondition).forEach(condition => {
const tracksByType = tracksByCondition[condition]
GENOTYPE_STATES.forEach(g => {
tracksByType[g] = derive(tracksByType[g] ?? []);
});
});
return tracksByCondition;
}
const MERGE_WINDOW_EXTENT = 100000; // in bases
function merge(tracks, session) {
const tracksByCondition = group(tracks);
const chrom = normalizeChrom(session.peak.feature.chrom);
const mergeTracksByType = tracksByType =>
Promise.all(
GENOTYPE_STATES
.map(g => tracksByType[g] ?? [])
.map(async tracks => {
if (belowThreshold(tracks.length)) {
return undefined;
}
const filePaths = tracks.map(prop('path'));
const maxSize = await bigWigChromosomeLength(filePaths[0], chrom);
return mergeFiles(filePaths, {
chrom,
start: Math.max(session.peak.feature.start - MERGE_WINDOW_EXTENT, 0),
end: Math.min(session.peak.feature.end + MERGE_WINDOW_EXTENT, maxSize),
});
})
)
const promisedTracks =
Object.entries(tracksByCondition).map(([condition, tracksByType]) =>
mergeTracksByType(tracksByType)
.then(output => ({
assay: session.peak.assay,
condition,
tracks,
output: Object.fromEntries(GENOTYPE_STATES.map((g, gi) => [g, output[gi]])),
})
)
);
return Promise.all(promisedTracks)
.then(results => results.filter(Boolean));
}
function plot(tracksByCondition) {
const data = conditions.map(c => tracksByCondition[c.id] ? getDataFromValues(tracksByCondition[c.id]) : []);
const domains = data.map(d => getDomain(d));
return Promise.all(
conditions.map((c, ci) => boxPlot({
title: c.name,
data: data[ci],
domain: domains[ci],
transform: `translate(${((PLOT_WIDTH / conditions.length) * ci).toFixed(0)} 0)`,
}))
).then(plots =>
`<svg width="${PLOT_WIDTH}" height="${PLOT_HEIGHT}">
${plots.join("")}
</svg>`
);
}
// Helpers
function getDataFromValues(values) {
return GENOTYPE_STATES.map(s => ({
name: GENOTYPE_STATE_NAMES[s],
data: values[s] ?? [],
}));
}
function mergeFiles(paths, { chrom, start, end }) {
paths.sort(Intl.Collator().compare);
const mergeHash = md5(JSON.stringify({ paths, chrom, start, end }));
const mergeName = `${mergeHash}.bw`;
const url = `${NODE_BASE_URL}/api/merged/${mergeName}`;
const mergePath = path.join(MERGED_TRACKS_DIR, mergeName);
console.info(`mergeFiles called with ${paths.length} files; start=${start}; end=${end}`);
return new Promise((resolve) => {
fs.access(mergePath, fs.constants.F_OK, (err) => {
// If the file doesn't exist yet, we'll get an error. Otherwise, we won't and we can resolve 'vacuously' in order
// to reuse the existing file.
resolve(err ? bigWigMerge(paths, mergePath, chrom, start, end) : true);
});
}).then(() => ({ path: mergePath, url }));
}
function derive(list) {
const points = list.map(d => d.data).sort((a, b) => a - b)
const pointsByEthnicity = map(mapToData, groupByEthnicity(list))
// noinspection JSCheckFunctionSignatures
return {
n: list.length,
stats: getStats(points),
statsByEthnicity: Object.fromEntries(
Object.entries(pointsByEthnicity)
.map(([eth, ethPoints]) => [
eth,
getStats(ethPoints.sort((a, b) => a - b))
])
),
// Note: Do not send points to the front end – it is too easy to re-identify genotypes
// from a public bigWig file here.
points: pointsByEthnicity,
}
}
function getStats(points) {
return {
min: Math.min(...points),
quartile_1: points[~~(points.length * 1/4)],
median: points[~~(points.length * 2/4)],
quartile_3: points[~~(points.length * 3/4)],
max: Math.max(...points),
};
}