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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::sync::Arc;
use igv_core::region::Region;
use igv_core::source::bam::AlignmentRow;
use igv_core::source::vcf::VariantRecord;
use igv_core::render::RenderMode;
use igv_core::source::{BamSource, FastaSource, FetchOpts, VcfSource};
use igv_core::source::{FetchSignalOpts, SignalBin, SignalSource};
use igv_core::source::link::{FetchLinkOpts, LinkSource, VisibleLink};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::warn;
#[derive(Debug, Clone)]
pub struct LoadRequest {
pub generation: u64,
pub region: Region,
pub fetch_opts: FetchOpts,
/// Max bins to request from each signal source for this fetch. Driven by
/// terminal width so zoom-level selection roughly matches what the widget
/// can actually render.
pub signal_max_bins: u32,
/// Min score filter passed to LinkSource::query; None = no filter.
pub link_min_score: Option<f64>,
/// Current render mode (derived from view width + thresholds). Drives
/// loader-side gating: at wide zoom we skip reference / BAM / VCF fetches
/// entirely so chromosome-scale views don't OOM.
pub render_mode: RenderMode,
}
#[derive(Debug)]
pub enum LoadResult {
Reference {
generation: u64,
region: Region,
bytes: Vec<u8>,
},
Variants {
generation: u64,
records: Vec<VariantRecord>,
},
Bam {
generation: u64,
bam_index: usize,
rows: Vec<AlignmentRow>,
},
Annotation {
generation: u64,
track_index: usize,
transcripts: Vec<igv_core::source::AnnotationTranscript>,
},
Signal {
generation: u64,
track_index: usize,
bins: Vec<SignalBin>,
},
Link {
generation: u64,
track_index: usize,
visible: Vec<VisibleLink>,
total_record_count: usize,
},
Error {
generation: u64,
message: String,
},
}
impl LoadResult {
pub fn generation(&self) -> u64 {
match self {
LoadResult::Reference { generation, .. }
| LoadResult::Variants { generation, .. }
| LoadResult::Bam { generation, .. }
| LoadResult::Annotation { generation, .. }
| LoadResult::Signal { generation, .. }
| LoadResult::Link { generation, .. }
| LoadResult::Error { generation, .. } => *generation,
}
}
}
pub struct Loader {
pub fasta: Arc<dyn FastaSource>,
pub vcf: Option<Arc<dyn VcfSource>>,
pub bams: Vec<Arc<dyn BamSource>>,
pub annotations: Vec<std::sync::Arc<dyn igv_core::source::AnnotationSource>>,
pub signals: Vec<Arc<dyn SignalSource>>,
pub links: Vec<Arc<dyn LinkSource>>,
pub tx: mpsc::Sender<LoadResult>,
pub current: Vec<JoinHandle<()>>,
}
impl Loader {
pub fn new(
fasta: Arc<dyn igv_core::source::FastaSource>,
vcf: Option<Arc<dyn igv_core::source::VcfSource>>,
bams: Vec<Arc<dyn igv_core::source::BamSource>>,
annotations: Vec<Arc<dyn igv_core::source::AnnotationSource>>,
signals: Vec<Arc<dyn SignalSource>>,
links: Vec<Arc<dyn LinkSource>>,
tx: tokio::sync::mpsc::Sender<LoadResult>,
) -> Self {
Self {
fasta,
vcf,
bams,
annotations,
signals,
links,
tx,
current: Vec::new(),
}
}
/// Cancel any in-flight tasks and dispatch fresh ones for `req`.
pub fn dispatch(&mut self, req: LoadRequest) {
for h in self.current.drain(..) {
h.abort();
}
// Render-mode gates: at wide zoom, skip per-base fetches that would
// otherwise pull hundreds of MB of reference / millions of reads.
let needs_per_base = matches!(
req.render_mode,
RenderMode::PerBase | RenderMode::DetailedReads
);
let suppress_overview = matches!(req.render_mode, RenderMode::OverviewOnly);
// Reference fetch — only at zoom levels where the sequence widget can
// actually render bases (widget gates on the same modes).
if needs_per_base {
let fasta = Arc::clone(&self.fasta);
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
match fasta.fetch(&r.region).await {
Ok(bytes) => {
let _ = tx
.send(LoadResult::Reference {
generation: r.generation,
region: r.region,
bytes,
})
.await;
}
Err(e) => {
let _ = tx
.send(LoadResult::Error {
generation: r.generation,
message: e.to_string(),
})
.await;
}
}
}));
} else {
// Send an empty reference result so any state machine waiting on a
// reference response settles (and stale bytes from a prior fetch
// are explicitly cleared).
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
let _ = tx
.send(LoadResult::Reference {
generation: r.generation,
region: r.region,
bytes: Vec::new(),
})
.await;
}));
}
// VCF fetch — variants widget hides itself in OverviewOnly, so skip.
if !suppress_overview {
if let Some(vcf) = &self.vcf {
let vcf = Arc::clone(vcf);
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
match vcf.fetch(&r.region).await {
Ok(records) => {
let _ = tx
.send(LoadResult::Variants {
generation: r.generation,
records,
})
.await;
}
Err(e) => {
warn!("vcf fetch failed: {e}");
let _ = tx
.send(LoadResult::Variants {
generation: r.generation,
records: Vec::new(),
})
.await;
}
}
}));
}
}
// BAM fetches — alignments widget can't render at wider modes anyway,
// and a chr-scale BAM fetch would pull millions of reads.
if needs_per_base {
for (idx, bam) in self.bams.iter().enumerate() {
let bam = Arc::clone(bam);
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
match bam.fetch(&r.region, &r.fetch_opts).await {
Ok(rows) => {
let _ = tx
.send(LoadResult::Bam {
generation: r.generation,
bam_index: idx,
rows,
})
.await;
}
Err(e) => {
warn!("bam fetch failed: {e}");
let _ = tx
.send(LoadResult::Bam {
generation: r.generation,
bam_index: idx,
rows: Vec::new(),
})
.await;
}
}
}));
}
} else {
// Clear stale rows so the alignments widget doesn't show old data
// after zooming out past the BAM gate.
for idx in 0..self.bams.len() {
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
let _ = tx
.send(LoadResult::Bam {
generation: r.generation,
bam_index: idx,
rows: Vec::new(),
})
.await;
}));
}
}
// Annotations always fetch — at OverviewOnly the widget renders gene
// density, not individual transcripts.
for (idx, ann) in self.annotations.iter().enumerate() {
let ann = std::sync::Arc::clone(ann);
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
match ann.fetch(&r.region).await {
Ok(transcripts) => {
let _ = tx
.send(LoadResult::Annotation {
generation: r.generation,
track_index: idx,
transcripts,
})
.await;
}
Err(e) => {
tracing::warn!("annotation fetch failed: {e}");
let _ = tx
.send(LoadResult::Annotation {
generation: r.generation,
track_index: idx,
transcripts: Vec::new(),
})
.await;
}
}
}));
}
for (idx, sig) in self.signals.iter().enumerate() {
let sig = Arc::clone(sig);
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
let opts = FetchSignalOpts {
max_bins: r.signal_max_bins.max(1),
..FetchSignalOpts::default()
};
match sig.fetch(&r.region, &opts).await {
Ok(bins) => {
let _ = tx
.send(LoadResult::Signal {
generation: r.generation,
track_index: idx,
bins,
})
.await;
}
Err(e) => {
tracing::warn!("signal fetch failed: {e}");
let _ = tx
.send(LoadResult::Signal {
generation: r.generation,
track_index: idx,
bins: Vec::new(),
})
.await;
}
}
}));
}
for (idx, lk) in self.links.iter().enumerate() {
let lk = Arc::clone(lk);
let tx = self.tx.clone();
let r = req.clone();
self.current.push(tokio::spawn(async move {
let opts = FetchLinkOpts { min_score: r.link_min_score };
match lk.query(&r.region, &opts).await {
Ok(visible) => {
let count = lk.record_count();
let _ = tx
.send(LoadResult::Link {
generation: r.generation,
track_index: idx,
visible,
total_record_count: count,
})
.await;
}
Err(e) => {
tracing::warn!("link query failed: {e}");
let _ = tx
.send(LoadResult::Link {
generation: r.generation,
track_index: idx,
visible: Vec::new(),
total_record_count: 0,
})
.await;
}
}
}));
}
}
}