|
| 1 | +"""Shared position handling functions for HGVS.""" |
| 2 | + |
| 3 | +import hgvs.location |
| 4 | + |
| 5 | + |
| 6 | +def get_start_end( |
| 7 | + var, outer_confidence=True |
| 8 | +) -> tuple[ |
| 9 | + hgvs.location.SimplePosition | hgvs.location.BaseOffsetPosition, |
| 10 | + hgvs.location.SimplePosition | hgvs.location.BaseOffsetPosition, |
| 11 | +]: |
| 12 | + """Get start and end positions from a variant or interval. |
| 13 | +
|
| 14 | + This function handles all position types (SimplePosition, BaseOffsetPosition, |
| 15 | + Interval, BaseOffsetInterval) and returns the appropriate start and end positions. |
| 16 | + It can be expected that the returned positions have a base and an uncertain property. |
| 17 | +
|
| 18 | + By default we return the outer confidence positions. However, if that position |
| 19 | + does not have a base, we return the inner confidence positions. |
| 20 | +
|
| 21 | + TODO: add a new optional parameter that allows to define the strictness of the returned positions. |
| 22 | + The current behavior is more alike to an "auto" mode, since we might fall back to the inner confidence positions |
| 23 | + if the outer confidence positions do not have a base. A potential "strict" mode would only return the outer confidence positions, |
| 24 | + and raise an error if the outer confidence positions do not have a base. |
| 25 | +
|
| 26 | + Args: |
| 27 | + var: A variant object with posedit.pos attribute, or an Interval object |
| 28 | + outer_confidence: If True, return the outer confidence positions, otherwise return the inner confidence positions |
| 29 | +
|
| 30 | + Returns: |
| 31 | + tuple: (start_position, end_position) where positions can be SimplePosition or BaseOffsetPosition |
| 32 | + """ |
| 33 | + |
| 34 | + # Handle Interval objects directly |
| 35 | + if isinstance(var, hgvs.location.Interval): |
| 36 | + s = var.start |
| 37 | + e = var.end |
| 38 | + if not isinstance( |
| 39 | + s, (hgvs.location.SimplePosition, hgvs.location.BaseOffsetPosition) |
| 40 | + ): |
| 41 | + if outer_confidence and not s.start.base is not None: |
| 42 | + s = s.start |
| 43 | + else: |
| 44 | + s = s.end |
| 45 | + if not isinstance( |
| 46 | + e, (hgvs.location.SimplePosition, hgvs.location.BaseOffsetPosition) |
| 47 | + ): |
| 48 | + if outer_confidence and not e.end.base is not None: |
| 49 | + e = e.end |
| 50 | + else: |
| 51 | + e = e.start |
| 52 | + return s, e |
| 53 | + |
| 54 | + # Handle position objects directly |
| 55 | + if isinstance( |
| 56 | + var, (hgvs.location.SimplePosition, hgvs.location.BaseOffsetPosition) |
| 57 | + ): |
| 58 | + return var, var |
| 59 | + |
| 60 | + # if there is no posedit, return None, all steps below this would fail |
| 61 | + if var.posedit is None: |
| 62 | + return None, None |
| 63 | + |
| 64 | + # Handle variants with posedit |
| 65 | + pos = var.posedit.pos |
| 66 | + |
| 67 | + if isinstance(pos, hgvs.location.SimplePosition): |
| 68 | + return pos, pos |
| 69 | + elif isinstance(pos, hgvs.location.BaseOffsetInterval): |
| 70 | + return pos.start, pos.end |
| 71 | + elif isinstance(pos, hgvs.location.Interval): |
| 72 | + s = pos.start |
| 73 | + e = pos.end |
| 74 | + |
| 75 | + if isinstance(s, hgvs.location.AAPosition) and isinstance( |
| 76 | + e, hgvs.location.AAPosition |
| 77 | + ): |
| 78 | + s = s.base |
| 79 | + e = e.base |
| 80 | + return s, e |
| 81 | + |
| 82 | + if not isinstance( |
| 83 | + s, (hgvs.location.SimplePosition, hgvs.location.BaseOffsetPosition) |
| 84 | + ): |
| 85 | + orig_s = s |
| 86 | + |
| 87 | + if outer_confidence and s.start.base is not None: |
| 88 | + s = s.start |
| 89 | + else: |
| 90 | + s = s.end |
| 91 | + if s.is_uncertain: |
| 92 | + s = orig_s.end |
| 93 | + |
| 94 | + if not isinstance( |
| 95 | + e, (hgvs.location.SimplePosition, hgvs.location.BaseOffsetPosition) |
| 96 | + ): |
| 97 | + orig_e = e |
| 98 | + if outer_confidence and e.end.base is not None: |
| 99 | + e = e.end |
| 100 | + else: |
| 101 | + e = e.start |
| 102 | + if e.is_uncertain: |
| 103 | + e = orig_e.start |
| 104 | + return s, e |
| 105 | + else: # BaseOffsetPosition |
| 106 | + return pos, pos |
| 107 | + |
| 108 | + |
| 109 | +def get_start_end_interbase( |
| 110 | + pos: hgvs.location.BaseOffsetPosition | hgvs.location.Interval, |
| 111 | + outer_confidence=True, |
| 112 | +) -> tuple[int, int]: |
| 113 | + """Get start and end integer positions from a SequenceVariant. |
| 114 | +
|
| 115 | + This function extracts integer positions from a SequenceVariant, handling uncertain positions |
| 116 | + and intervals. For uncertain positions, it uses the more confident boundary. |
| 117 | +
|
| 118 | + Args: |
| 119 | + var: A SequenceVariant object |
| 120 | + outer_confidence: If True, return the outer confidence positions, otherwise return the inner confidence positions |
| 121 | +
|
| 122 | + Returns: |
| 123 | + tuple: (start_int, end_int) where both are integer positions (0-based) |
| 124 | + """ |
| 125 | + |
| 126 | + # Handle start position |
| 127 | + if pos.start.uncertain: |
| 128 | + # For uncertain start, use the more confident boundary |
| 129 | + if isinstance(pos.start, hgvs.location.Interval): |
| 130 | + if outer_confidence and pos.start.start.base is not None: |
| 131 | + seq_start = pos.start.start.base - 1 |
| 132 | + else: |
| 133 | + seq_start = pos.start.end.base - 1 |
| 134 | + else: |
| 135 | + seq_start = pos.start.base - 1 |
| 136 | + else: |
| 137 | + # For certain start, use the start boundary |
| 138 | + if isinstance(pos.start, hgvs.location.Interval): |
| 139 | + if outer_confidence: |
| 140 | + seq_start = pos.start.start.base - 1 |
| 141 | + else: |
| 142 | + seq_start = pos.start.end.base - 1 |
| 143 | + else: |
| 144 | + seq_start = pos.start.base - 1 |
| 145 | + |
| 146 | + # Handle end position |
| 147 | + if pos.end.uncertain: |
| 148 | + # For uncertain end, use the more confident boundary |
| 149 | + if isinstance(pos.end, hgvs.location.Interval): |
| 150 | + if outer_confidence and pos.end.end.base is not None: |
| 151 | + seq_end = pos.end.end.base |
| 152 | + else: |
| 153 | + seq_end = pos.end.start.base |
| 154 | + else: |
| 155 | + seq_end = pos.end.base |
| 156 | + else: |
| 157 | + # For certain end, use the end boundary |
| 158 | + if isinstance(pos.end, hgvs.location.Interval): |
| 159 | + if outer_confidence: |
| 160 | + seq_end = pos.end.end.base |
| 161 | + else: |
| 162 | + seq_end = pos.end.start.base |
| 163 | + else: |
| 164 | + seq_end = pos.end.base |
| 165 | + |
| 166 | + return seq_start, seq_end |
0 commit comments