|
| 1 | +# hairpin2 |
| 2 | +# |
| 3 | +# Copyright (C) 2024 Genome Research Ltd. |
| 4 | +# |
| 5 | +# Author: Alex Byrne <ab63@sanger.ac.uk> |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +from enum import IntEnum, Flag |
| 26 | +from typing import Callable |
| 27 | +import dataclasses as d |
| 28 | + |
| 29 | +EXIT_SUCCESS = 0 |
| 30 | +EXIT_FAILURE = 1 |
| 31 | + |
| 32 | +DEFAULTS: dict[str, int | float] = dict((('al_filter_threshold', 0.93), |
| 33 | + ('min_clip_quality', 35), |
| 34 | + ('min_mapping_quality', 11), |
| 35 | + ('min_base_quality', 25), |
| 36 | + ('max_read_span', 6), |
| 37 | + ('edge_definition', 0.15), |
| 38 | + ('edge_fraction', 0.9), |
| 39 | + ('min_MAD_one_strand', 0), |
| 40 | + ('min_sd_one_strand', 4), |
| 41 | + ('min_MAD_both_strand_weak', 2), |
| 42 | + ('min_sd_both_strand_weak', 2), |
| 43 | + ('min_MAD_both_strand_strong', 1), |
| 44 | + ('min_sd_both_strand_strong', 10), |
| 45 | + ('min_reads', 1))) |
| 46 | + |
| 47 | +FiltCodes = IntEnum('FiltCodes', |
| 48 | + ['SIXTYAI', |
| 49 | + 'SIXTYBI', |
| 50 | + 'ON_THRESHOLD', |
| 51 | + 'INSUFFICIENT_READS', |
| 52 | + 'NO_MUTANTS'], |
| 53 | + start=0) |
| 54 | +Ops = IntEnum('Ops', |
| 55 | + ['MATCH', |
| 56 | + 'INS', |
| 57 | + 'DEL', |
| 58 | + 'SKIP', |
| 59 | + 'SOFT', |
| 60 | + 'HARD', |
| 61 | + 'PAD', |
| 62 | + 'EQUAL', |
| 63 | + 'DIFF', |
| 64 | + 'BACK'], |
| 65 | + start=0) |
| 66 | +ValidatorFlags = Flag('ValidatorFlags', |
| 67 | + ['CLEAR', |
| 68 | + 'FLAG', |
| 69 | + 'MAPQUAL', |
| 70 | + 'READ_FIELDS_MISSING', |
| 71 | + 'NOT_ALIGNED', |
| 72 | + 'BAD_OP', |
| 73 | + 'NOT_ALT', |
| 74 | + 'BASEQUAL', |
| 75 | + 'SHORT', |
| 76 | + 'CLIPQUAL', |
| 77 | + 'OVERLAP'], |
| 78 | + start=0) |
| 79 | + |
| 80 | + |
| 81 | +class NoAlts(ValueError): |
| 82 | + pass |
| 83 | + |
| 84 | + |
| 85 | +class NoMutants(ValueError): |
| 86 | + pass |
| 87 | + |
| 88 | + |
| 89 | +@d.dataclass |
| 90 | +class FilterData: |
| 91 | + name: str |
| 92 | + flag: bool = False |
| 93 | + code: int | None = None |
| 94 | + |
| 95 | + def set(self): |
| 96 | + self.flag = True |
| 97 | + |
| 98 | + def __iter__(self): |
| 99 | + return (getattr(self, field.name) for field in d.fields(self)) |
| 100 | + |
| 101 | + |
| 102 | +@d.dataclass |
| 103 | +class ADFilter(FilterData): |
| 104 | + name: str = d.field(default='ADF') |
| 105 | + |
| 106 | + |
| 107 | +@d.dataclass |
| 108 | +class ALFilter(FilterData): |
| 109 | + name: str = d.field(default='ALF') |
| 110 | + avg_as: float | None = None |
| 111 | + |
| 112 | + |
| 113 | +@d.dataclass |
| 114 | +class Filters: |
| 115 | + AL: ALFilter |
| 116 | + HP: ADFilter |
| 117 | + |
| 118 | + def __iter__(self): |
| 119 | + return (getattr(self, field.name) for field in d.fields(self)) |
| 120 | + |
| 121 | + def fill_field(self, field_name, value): |
| 122 | + if hasattr(self, field_name): |
| 123 | + setattr(self, field_name, value) |
| 124 | + else: |
| 125 | + raise AttributeError |
| 126 | + |
| 127 | + def get_field(self, field_name): |
| 128 | + if hasattr(self, field_name): |
| 129 | + return getattr(self, field_name) |
| 130 | + else: |
| 131 | + raise AttributeError |
| 132 | + |
| 133 | + |
| 134 | +FiltReturn = Callable[..., Filters] |
| 135 | +FlagReturn = Callable[..., int] |
0 commit comments