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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! This module contains code to the primer editor, QC etc.
use eframe::egui::{Align, Color32, Layout, RichText, ScrollArea, TextEdit, Ui};
use egui_extras::{Column, TableBuilder};
use na_seq::{seq_from_str, seq_to_str_lower};
use crate::{
Selection,
gui::{
COL_SPACING, ROW_SPACING,
theme::{COLOR_ACTION, COLOR_INFO},
},
primer::{IonConcentrations, Primer, TuneSetting, make_amplification_primers},
state::State,
};
const TABLE_ROW_HEIGHT: f32 = 60.;
const COLOR_GOOD: Color32 = Color32::GREEN;
const COLOR_MARGINAL: Color32 = Color32::GOLD;
const COLOR_BAD: Color32 = Color32::LIGHT_RED;
pub const DEFAULT_TRIM_AMT: usize = 32 - 20;
// const TM_IDEAL: f32 = 59.; // todo: Fill thi sin
//
// const THRESHOLDS_TM: (f32, f32) = (59., 60.);
// const THRESHOLDS_GC: (f32, f32) = (59., 60.);
/// Color scores in each category according to these thresholds. These scores should be on a scale
/// between 0 and 1.
fn color_from_score(score: f32) -> Color32 {
const SCORE_COLOR_THRESH: (f32, f32) = (0.5, 0.8);
if score > SCORE_COLOR_THRESH.1 {
COLOR_GOOD
} else if score > SCORE_COLOR_THRESH.0 {
COLOR_MARGINAL
} else {
COLOR_BAD
}
}
/// Allows editing ion concentration, including float manip. Return if the response changed,
/// so we can redo TM calcs downstream.
fn ion_edit(val: &mut f32, label: &str, ui: &mut Ui) -> bool {
ui.label(label);
let mut v = format!("{:.1}", val);
let response = ui.add(TextEdit::singleline(&mut v).desired_width(30.));
if response.changed() {
*val = v.parse().unwrap_or(0.);
true
} else {
false
}
}
fn primer_table(state: &mut State, ui: &mut Ui) {
let mut run_match_sync = None; // Avoids a double-mutation error.
TableBuilder::new(ui)
.column(Column::initial(650.).resizable(true)) // Sequence
.column(Column::initial(160.).resizable(true)) // Description
.column(Column::auto().resizable(true))// Len
.column(Column::auto().resizable(true))// Len
.column(Column::auto().resizable(true))// Matches
.column(Column::auto().resizable(true))// Quality
.column(Column::initial(40.).resizable(true)) // TM
.column(Column::initial(36.).resizable(true))// GC %
.column(Column::auto().resizable(true))// 3' GC content
.column(Column::auto().resizable(true))// Complexity
.column(Column::auto().resizable(true))// Dimer formation
.column(Column::auto().resizable(true)) // Repeats
.column(Column::remainder())
.header(20.0, |mut header| {
header.col(|ui| {
ui.heading("Primer sequence (5' ⏵ 3')");
});
header.col(|ui| {
ui.heading("Name");
});
header.col(|ui| {
ui.heading("Len").on_hover_text("Number of nucleotides in the (tuned, if applicable) primer");
});
header.col(|ui| {
ui.heading("Weight").on_hover_text("The weight of this primer, in Daltons.");
});
header.col(|ui| {
ui.heading("Mt").on_hover_text("Number of matches with the target sequence");
});
header.col(|ui| {
ui.heading("Qual").on_hover_text("Overall primer quality. This is an abstract estimate, taking all other listed factors into account.");
});
header.col(|ui| {
ui.heading("TM").on_hover_text("Primer melting temperature, in °C. Calculated using base a base stacking method, where\
enthalpy and entropy of neighboring base pairs are added. See the readme for calculations and assumptions.");
});
header.col(|ui| {
ui.heading("GC").on_hover_text("The percentage of nucleotides that are C or G.");
});
header.col(|ui| {
ui.heading("3'GC").on_hover_text("3' end stability: The number of G or C nucleotides in the last 5 nucleotides. 2-3 is ideal. Sources differ on if 4 is acceptable.");
});
// header.col(|ui| {
// ui.heading("Cplx").on_hover_text("Sequence complexity. See the readme for calculations and assumptions.");
// });
header.col(|ui| {
ui.heading("Dmr").on_hover_text("Potential of forming a self-end dimer. See the readme for calculations and assumptions.");
});
header.col(|ui| {
ui.heading("Rep").on_hover_text("Count of repeats of a single or double nt sequence >4 in a row, and count of triplet \
repeats anywhere in the sequence.");
});
// For selecting the row.
header.col(|_ui| {});
})
.body(|mut body| {
for (i, primer) in state.generic[state.active].primers.iter_mut().enumerate() {
// println!("Primer: 5p: {:?} 3p: {:?} Rem5: {:?} Rem3: {:?}", primer.volatile.tunable_5p,
// primer.volatile.tunable_5p, primer.volatile.seq_removed_5p, primer.volatile.seq_removed_3p);
body.row(TABLE_ROW_HEIGHT, |mut row| {
row.col(|ui| {
ui.horizontal(|ui| {
let color = match primer.volatile.tune_setting {
TuneSetting::Only5(_) | TuneSetting::Both(_) => Color32::GREEN,
_ => Color32::LIGHT_GRAY,
};
if ui
.button(RichText::new("T").color(color))
.clicked()
{
primer.volatile.tune_setting.toggle_5p();
// if primer.volatile.tunable_5p == TuneSetting::Disabled {
// primer.run_calcs(&state.ion_concentrations[state.active]); // To re-sync the sequence without parts removed.
primer.run_calcs(&state.ion_concentrations); // To re-sync the sequence without parts removed.
// }
run_match_sync = Some(i);
}
let response = ui.add(
TextEdit::singleline(&mut primer.volatile.sequence_input).desired_width(400.),
);
if response.changed() {
primer.sequence = seq_from_str(&primer.volatile.sequence_input);
primer.volatile.sequence_input =
seq_to_str_lower(&primer.sequence);
// primer.run_calcs(&state.ion_concentrations[state.active]);
primer.run_calcs(&state.ion_concentrations);
run_match_sync = Some(i);
}
let color = match primer.volatile.tune_setting {
TuneSetting::Only3(_) | TuneSetting::Both(_) => Color32::GREEN,
_ => Color32::LIGHT_GRAY,
};
if ui
.button(RichText::new("T").color(color))
.clicked()
{
primer.volatile.tune_setting.toggle_3p();
// if primer.volatile.tunable_3p == TuneSetting::Disabled {
// primer.run_calcs(&state.ion_concentrations[state.active]); // To re-sync the sequence without parts removed.
primer.run_calcs(&state.ion_concentrations); // To re-sync the sequence without parts removed.
// }
run_match_sync = Some(i);
}
ui.add_space(COL_SPACING);
match primer.volatile.tune_setting {
TuneSetting::Both(_) | TuneSetting::Only5(_) | TuneSetting::Only3(_) => {
if ui
.button(RichText::new("Tune")).on_hover_text("Tune selected ends for this primer").clicked()
{
// primer.tune(&state.ion_concentrations[state.active]);
primer.tune(&state.ion_concentrations);
run_match_sync = Some(i);
}
}
_ => (),
}
});
// let updated_seq = primer_tune_display(primer, &state.ion_concentrations[state.active], ui);
let updated_seq = primer_tune_display(primer, &state.ion_concentrations, ui);
if updated_seq {
run_match_sync = Some(i);
}
});
row.col(|ui| {
ui.add(TextEdit::singleline(&mut primer.name).text_color(COLOR_INFO));
});
row.col(|ui| {
let text = match &primer.volatile.metrics {
Some(m) => {
RichText::new(primer.sequence.len().to_string())
.color(color_from_score(m.len_score))
},
None => RichText::new(primer.sequence.len().to_string()),
};
ui.label(text);
});
row.col(|ui| {
ui.label(format!("{:.1}", primer.volatile.weight));
});
row.col(|ui| {
ui.label(primer.volatile.matches.len().to_string());
});
row.col(|ui| {
let text = match & primer.volatile.metrics {
// todo: PRe-compute the * 100?
Some(m) => RichText::new(format!("{:.0}", m.quality_score * 100.))
.color(color_from_score(m.quality_score)),
None => RichText::new("-"),
};
ui.label(text);
});
row.col(|ui| {
let text = match & primer.volatile.metrics {
Some(m) => RichText::new(format!("{:.1}°C", m.melting_temp))
.color(color_from_score(m.tm_score)),
None => RichText::new("-"),
};
ui.label(text);
});
row.col(|ui| {
let text = match & primer.volatile.metrics {
// todo: Cache this calc?
Some(m) => RichText::new(format!("{:.0}%", m.gc_portion * 100.))
.color(color_from_score(m.gc_score)),
None => RichText::new("-"),
};
ui.label(text);
});
row.col(|ui| {
let text = match & primer.volatile.metrics {
Some(m) => RichText::new(format!("{}", m.gc_3p_count))
.color(color_from_score(m.gc_3p_score)),
None => RichText::new("-"),
};
ui.label(text);
});
row.col(|ui| {
let text = match & primer.volatile.metrics {
Some(m) => RichText::new(format!("{}", m.self_end_dimer))
.color(color_from_score(m.dimer_score)),
None => RichText::new("-"),
};
ui.label(text);
});
row.col(|ui| {
let text = match & primer.volatile.metrics {
Some(m) => RichText::new(format!("{}", m.repeats))
.color(color_from_score(m.repeats_score)),
None => RichText::new("-"),
};
ui.label(text);
});
row.col(|ui| {
let mut selected = false;
if let Selection::Primer(sel_i) = state.ui.selected_item {
if sel_i == i {
selected = true;
}
}
if selected {
if ui.button(RichText::new("🔘").color(Color32::GREEN)).clicked() {
state.ui.selected_item = Selection::None;
}
} else if ui.button("🔘").clicked() {
state.ui.selected_item = Selection::Primer(i);
}
});
});
}
});
if run_match_sync.is_some() {
state.sync_seq_related(run_match_sync);
}
}
pub fn primer_details(state: &mut State, ui: &mut Ui) {
ScrollArea::vertical().show(ui, |ui| {
ui.horizontal(|ui| {
let add_btn = ui
.button("➕ Add primer")
.on_hover_text("Adds a primer to the list below.");
if add_btn.clicked() {
state.generic[state.active].primers.push(Default::default())
}
if ui
.button("➕ Make whole seq primers")
.on_hover_text("Adds a primer pair that amplify the entire loaded sequence.")
.clicked()
{
make_amplification_primers(state);
}
let mut sync_primer_matches = false; // Prevents a double-borrow error.
if ui.button("Tune all").clicked() {
for primer in &mut state.generic[state.active].primers {
// primer.tune(&state.ion_concentrations[state.active]);
primer.tune(&state.ion_concentrations);
sync_primer_matches = true;
}
}
if sync_primer_matches {
state.sync_primer_matches(None);
}
ui.add_space(COL_SPACING * 2.);
ui.add_space(2. * COL_SPACING);
ui.heading("Ions: (mMol)");
// if ion_edit(&mut state.ion_concentrations.monovalent, "Na+ and K+", ui)
if ion_edit(&mut state.ion_concentrations.monovalent, "Na+ and K+", ui)
|| ion_edit(&mut state.ion_concentrations.divalent, "mg2+", ui)
|| ion_edit(&mut state.ion_concentrations.dntp, "dNTP", ui)
|| ion_edit(&mut state.ion_concentrations.primer, "primer (nM)", ui)
{
for primer in &mut state.generic[state.active].primers {
// primer.run_calcs(&state.ion_concentrations[state.active]); // Note: We only need to run the TM calc.
primer.run_calcs(&state.ion_concentrations); // Note: We only need to run the TM calc.
}
}
});
ui.label("Tuning instructions: Include more of the target sequence than required on the end[s] that can be tuned. These are the \
ends that do not define your insert, gene of interest, insertion point etc. Mark that end as tunable using the \"T\" button. \
To learn about a table column, mouse over it.");
ui.add_space(ROW_SPACING);
if let Selection::Primer(sel_i) = state.ui.selected_item {
ui.horizontal(|ui| {
if sel_i + 1 > state.generic[state.active].primers.len() {
// This currently happens if deleting the bottom-most primer.
// If so, select the primer above it.
state.ui.selected_item = if !state.generic[state.active].primers.is_empty() {
Selection::Primer(state.generic[state.active].primers.len() - 1)
} else {
Selection::None
};
return;
}
if ui.button(RichText::new("⏶")).clicked() {
// todo: Arrow icons
if sel_i != 0 {
state.generic[state.active].primers.swap(sel_i, sel_i - 1);
state.ui.selected_item = Selection::Primer(sel_i - 1);
}
}
if ui.button(RichText::new("⏷")).clicked() && sel_i != state.generic[state.active].primers.len() - 1
{
state.generic[state.active].primers.swap(sel_i, sel_i + 1);
state.ui.selected_item = Selection::Primer(sel_i + 1);
}
if ui
.button(RichText::new("Delete 🗑").color(Color32::RED))
.clicked()
{
state.generic[state.active].primers.remove(sel_i);
}
if ui
.button(RichText::new("Deselect").color(COLOR_ACTION))
.clicked()
{
state.ui.selected_item = Selection::None;
}
ui.add_space(COL_SPACING);
if sel_i + 1 > state.generic[state.active].primers.len() {
state.ui.selected_item = Selection::None;
return;
}
ui.heading(&format!("Selected: {}", &state.generic[state.active].primers[sel_i].name));
});
ui.add_space(ROW_SPACING);
}
primer_table(state, ui);
});
}
/// Shows below each primer sequence. Data and controls on trimming primer size for optimization.
/// Returns wheather a button was clicked.
fn primer_tune_display(
primer: &mut Primer,
ion_concentrations: &IonConcentrations,
ui: &mut Ui,
) -> bool {
// This avoids a double-mutable error
let mut tuned = false;
let len_full = primer.volatile.sequence_input.len();
// Section for tuning primer length.
ui.horizontal(|ui| {
// todo: We only need this when the button is clicked, but getting borrow errors.
// This tune limit controls the maximum value of this tune; only applicable if both ends are tunable.
let (tune_limit_5p, tune_limit_3p) = match primer.volatile.tune_setting {
// Note: The 3p end is minus two, both due to our normal indexing logic, and since the anchor is technically
// between two nucleotides.
TuneSetting::Both((anchor, _, _)) => {
let p3 = if len_full - anchor >= 2 {
len_full - anchor - 2
} else {
len_full - anchor
};
(anchor, p3)
}, // -2
_ => {
if len_full > 0 {
(len_full - 1, len_full - 1)
} else {
(len_full, len_full)
}
} // No limit other than primer size.
};
if let Some(i) = primer.volatile.tune_setting.val_5p_mut() {
ui.label("5'");
if ui.button("⏴").clicked() {
if *i > 0 {
*i -= 1;
}
tuned = true;
};
if ui.button("⏵").clicked() {
if *i < tune_limit_5p {
*i += 1;
}
tuned = true;
};
}
// Allow setting the anchor. (Eg insertion point when cloning)
if let TuneSetting::Both((anchor, _, _)) = &mut primer.volatile.tune_setting {
ui.label(format!("Anchor ({}):", *anchor + 1)).on_hover_text("Generally for cloning insert primers; the point of insertion. Can not be tuned out, and primer size is assessed on both sides of it.");
if ui.button("⏴").clicked() {
if *anchor > 0 {
*anchor -= 1;
}
tuned = true;
};
if ui.button("⏵").clicked() {
if *anchor + 1 < len_full {
*anchor += 1;
}
tuned = true;
};
}
// This section shows the trimmed sequence, with the removed parts visible to the left and right.
ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
ui.label(RichText::new(&primer.volatile.seq_removed_5p).color(Color32::GRAY));
ui.add_space(COL_SPACING / 2.);
// if primer.volatile.tunable_5p != TuneSetting::Disabled
// || primer.volatile.tunable_3p != TuneSetting::Disabled
// {
if primer.volatile.tune_setting.tunable() {
ui.label(RichText::new(seq_to_str_lower(&primer.sequence)).color(COLOR_INFO));
}
ui.add_space(COL_SPACING / 2.);
ui.label(RichText::new(&primer.volatile.seq_removed_3p).color(Color32::GRAY));
});
// Note: We need to reverse the item order for this method of right-justifying to work.
// This is kind of OK with the intent here though.
ui.with_layout(Layout::right_to_left(Align::Max), |ui| {
if let Some(i) = primer.volatile.tune_setting.val_3p_mut() {
ui.label("3'");
if ui.button("⏵").clicked() {
if *i > 0 {
*i -= 1;
}
tuned = true;
};
if ui.button("⏴").clicked() {
if *i < tune_limit_3p {
*i += 1;
}
tuned = true;
};
}
});
if tuned {
primer.run_calcs(ion_concentrations);
}
});
tuned
}