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
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Core data model for supported HGVS variants.
/// A parsed HGVS variant.
///
/// This is the root model returned by [`crate::parse_hgvs`]. It keeps the
/// reference, coordinate system, and biological description together.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{CoordinateSystem, VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("NM_007373.4:c.-1C>T").unwrap();
/// assert_eq!(variant.coordinate_system, CoordinateSystem::CodingDna);
///
/// match variant.description {
/// VariantDescription::Nucleotide(description) => {
/// assert_eq!(description.location.start.coordinate, -1);
/// }
/// VariantDescription::Protein(_) => unreachable!("expected nucleotide variant"),
/// }
/// ```
/// Reference metadata preceding the `:` in an HGVS expression.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::parse_hgvs;
///
/// let variant = parse_hgvs("NC_000023.11(NM_004006.2):c.3921dup").unwrap();
/// let reference = variant.reference.unwrap();
///
/// assert_eq!(reference.primary.id, "NC_000023.11");
/// assert_eq!(reference.context.unwrap().id, "NM_004006.2");
/// ```
/// A parsed accession with optional version.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::Accession;
///
/// let accession = Accession::new("NP_003997.2");
/// assert_eq!(accession.id, "NP_003997.2");
/// assert_eq!(accession.version, Some(2));
/// ```
/// HGVS coordinate system.
/// Top-level variant description for nucleotide or protein syntax.
/// Parsed nucleotide location and edit.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{NucleotideEdit, VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("NM_004006.2:c.357+1G>A").unwrap();
///
/// match variant.description {
/// VariantDescription::Nucleotide(description) => {
/// assert!(matches!(
/// description.edit,
/// NucleotideEdit::Substitution { ref reference, ref alternate }
/// if reference == "G" && alternate == "A"
/// ));
/// assert_eq!(description.location.start.coordinate, 357);
/// assert_eq!(description.location.start.offset, 1);
/// }
/// VariantDescription::Protein(_) => unreachable!("expected nucleotide variant"),
/// }
/// ```
/// Parsed protein consequence.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{ProteinEffect, VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("NP_003997.1:p.(Trp24Ter)").unwrap();
///
/// match variant.description {
/// VariantDescription::Protein(description) => {
/// assert!(description.is_predicted);
/// assert!(matches!(description.effect, ProteinEffect::Edit { .. }));
/// }
/// VariantDescription::Nucleotide(_) => unreachable!("expected protein variant"),
/// }
/// ```
/// Supported protein consequence forms.
/// Model describing a stop codon is known (long-form), or omitted (short-form),
/// or unknown (not encountered) due to a frameshift event.
///
/// - "Known" or long-form: `p.Arg97ProfsTer23`
/// - "Omitted" or short-form: `p.Arg97fs`
/// - "Unknown" or "not encountered": `p.Arg97ProfsTer?`
/// Model describing stop codon information in a protein frameshift edit.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{ProteinEdit, ProteinEffect, ProteinFrameshiftStopKind, VariantDescription, parse_hgvs};
///
/// let short = parse_hgvs("NP_0123456.1:p.Arg97fs").unwrap();
/// let known = parse_hgvs("NP_0123456.1:p.Arg97ProfsTer23").unwrap();
/// let unknown = parse_hgvs("NP_0123456.1:p.Arg97ProfsTer?").unwrap();
///
/// let extract_stop = |variant: tinyhgvs::HgvsVariant| match variant.description {
/// VariantDescription::Protein(description) => match description.effect {
/// ProteinEffect::Edit { edit: ProteinEdit::Frameshift { stop, .. }, .. } => stop,
/// _ => unreachable!("expected protein frameshift"),
/// },
/// _ => unreachable!("expected protein variant"),
/// };
///
/// let short_stop = extract_stop(short);
/// assert_eq!(short_stop.kind, ProteinFrameshiftStopKind::Omitted);
/// assert_eq!(short_stop.ordinal, None);
///
/// let known_stop = extract_stop(known);
/// assert_eq!(known_stop.kind, ProteinFrameshiftStopKind::Known);
/// assert_eq!(known_stop.ordinal, Some(23));
///
/// let unknown_stop = extract_stop(unknown);
/// assert_eq!(unknown_stop.kind, ProteinFrameshiftStopKind::Unknown);
/// assert_eq!(unknown_stop.ordinal, None);
/// ```
/// Inclusive interval used for nucleotide and protein locations.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("NP_003997.2:p.Lys23_Val25del").unwrap();
///
/// match variant.description {
/// VariantDescription::Protein(description) => {
/// let location = match description.effect {
/// tinyhgvs::ProteinEffect::Edit { ref location, .. } => location,
/// _ => unreachable!("expected protein edit"),
/// };
/// assert_eq!(location.start.residue, "Lys");
/// assert_eq!(location.end.as_ref().unwrap().residue, "Val");
/// }
/// VariantDescription::Nucleotide(_) => unreachable!("expected protein variant"),
/// }
/// ```
/// Anchor used by nucleotide coordinates.
/// Nucleotide coordinate with explicit anchor and offset.
///
/// The primary coordinate keeps the sign written in the HGVS string. For
/// example, `c.-1` becomes `coordinate == -1`, while `c.*1` becomes
/// `coordinate == 1`.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{NucleotideAnchor, VariantDescription, parse_hgvs};
///
/// let five_prime = parse_hgvs("NM_007373.4:c.-1C>T").unwrap();
/// let three_prime = parse_hgvs("NM_001272071.2:c.*1C>T").unwrap();
///
/// match five_prime.description {
/// VariantDescription::Nucleotide(description) => {
/// assert_eq!(description.location.start.anchor, NucleotideAnchor::RelativeCdsStart);
/// assert_eq!(description.location.start.coordinate, -1);
/// assert_eq!(description.location.start.offset, 0);
/// }
/// _ => unreachable!("expected nucleotide variant"),
/// }
///
/// match three_prime.description {
/// VariantDescription::Nucleotide(description) => {
/// assert_eq!(description.location.start.anchor, NucleotideAnchor::RelativeCdsEnd);
/// assert_eq!(description.location.start.coordinate, 1);
/// assert_eq!(description.location.start.offset, 0);
/// }
/// _ => unreachable!("expected nucleotide variant"),
/// }
/// ```
/// Protein coordinate written as amino-acid symbol plus ordinal.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{ProteinEffect, VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("NP_003997.1:p.Trp24Ter").unwrap();
///
/// match variant.description {
/// VariantDescription::Protein(description) => {
/// let location = match description.effect {
/// ProteinEffect::Edit { ref location, .. } => location,
/// _ => unreachable!("expected protein edit"),
/// };
/// assert_eq!(location.start.residue, "Trp");
/// assert_eq!(location.start.ordinal, 24);
/// }
/// _ => unreachable!("expected protein variant"),
/// }
/// ```
/// Supported nucleotide edit families.
/// A single sequence item inside a nucleotide insertion or deletion-insertion.
/// Literal inserted or replacement bases such as `A` or `AGGG`.
/// Repeated inserted or replacement sequence such as `N[12]`.
/// One repeated block/unit in a nucleotide repeat variant description.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{NucleotideEdit, VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("NC_000014.8:g.123CAG[23]").unwrap();
///
/// match variant.description {
/// VariantDescription::Nucleotide(description) => {
/// let NucleotideEdit::Repeat { blocks } = description.edit else {
/// unreachable!("expected nucleotide repeat");
/// };
/// assert_eq!(blocks[0].unit.as_deref(), Some("CAG"));
/// assert_eq!(blocks[0].count, 23);
/// }
/// VariantDescription::Protein(_) => unreachable!("expected nucleotide variant"),
/// }
/// ```
/// Sequence copied from the same or another reference.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{NucleotideEdit, NucleotideSequenceItem, VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("LRG_199t1:c.419_420ins[T;450_470;AGGG]").unwrap();
///
/// match variant.description {
/// VariantDescription::Nucleotide(description) => {
/// let NucleotideEdit::Insertion { items } = description.edit else {
/// unreachable!("expected insertion");
/// };
/// let NucleotideSequenceItem::Copied(item) = &items[1] else {
/// unreachable!("expected copied sequence");
/// };
/// assert!(item.is_from_same_reference());
/// assert_eq!(item.source_location.start.coordinate, 450);
/// assert_eq!(item.source_location.end.as_ref().unwrap().coordinate, 470);
/// }
/// VariantDescription::Protein(_) => unreachable!("expected nucleotide variant"),
/// }
/// ```
/// Supported protein edit families in the first release.
/// Ordered protein insertion or replacement sequence.
///
/// # Examples
///
/// ```rust
/// use tinyhgvs::{ProteinEdit, ProteinEffect, VariantDescription, parse_hgvs};
///
/// let variant = parse_hgvs("p.Lys2_Gly3insGlnSerLys").unwrap();
///
/// match variant.description {
/// VariantDescription::Protein(description) => {
/// let ProteinEffect::Edit { edit, .. } = description.effect else {
/// unreachable!("expected protein edit");
/// };
/// let ProteinEdit::Insertion { sequence } = edit else {
/// unreachable!("expected protein insertion");
/// };
/// assert_eq!(
/// sequence.residues,
/// vec!["Gln".to_string(), "Ser".to_string(), "Lys".to_string()]
/// );
/// }
/// VariantDescription::Nucleotide(_) => unreachable!("expected protein variant"),
/// }
/// ```