samplesheet-parser 1.3.0__py3-none-any.whl → 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- samplesheet_parser/__init__.py +4 -1
- samplesheet_parser/converter.py +8 -3
- samplesheet_parser/diff.py +44 -43
- samplesheet_parser/enums.py +19 -15
- samplesheet_parser/factory.py +75 -11
- samplesheet_parser/filter.py +3 -2
- samplesheet_parser/instruments.py +35 -4
- samplesheet_parser/merger.py +5 -4
- samplesheet_parser/parsers/v1.py +89 -93
- samplesheet_parser/parsers/v2.py +178 -137
- samplesheet_parser/protocol.py +102 -0
- samplesheet_parser/splitter.py +3 -2
- samplesheet_parser/validators.py +35 -28
- samplesheet_parser/writer.py +6 -4
- {samplesheet_parser-1.3.0.dist-info → samplesheet_parser-2.0.0.dist-info}/METADATA +31 -7
- samplesheet_parser-2.0.0.dist-info/RECORD +23 -0
- {samplesheet_parser-1.3.0.dist-info → samplesheet_parser-2.0.0.dist-info}/WHEEL +1 -1
- samplesheet_parser-1.3.0.dist-info/RECORD +0 -22
- {samplesheet_parser-1.3.0.dist-info → samplesheet_parser-2.0.0.dist-info}/entry_points.txt +0 -0
- {samplesheet_parser-1.3.0.dist-info → samplesheet_parser-2.0.0.dist-info}/licenses/LICENSE +0 -0
samplesheet_parser/__init__.py
CHANGED
|
@@ -41,20 +41,23 @@ from samplesheet_parser.index_utils import normalize_index_lengths
|
|
|
41
41
|
from samplesheet_parser.merger import MergeResult, SampleSheetMerger
|
|
42
42
|
from samplesheet_parser.parsers.v1 import SampleSheetV1
|
|
43
43
|
from samplesheet_parser.parsers.v2 import SampleSheetV2
|
|
44
|
+
from samplesheet_parser.protocol import SampleSheetParser
|
|
44
45
|
from samplesheet_parser.splitter import SampleSheetSplitter, SplitResult
|
|
45
|
-
from samplesheet_parser.validators import SampleSheetValidator, ValidationResult
|
|
46
|
+
from samplesheet_parser.validators import SampleSheetValidator, ValidationResult, hamming_distance
|
|
46
47
|
from samplesheet_parser.writer import SampleSheetWriter
|
|
47
48
|
|
|
48
49
|
__all__ = [
|
|
49
50
|
"SampleSheetV1",
|
|
50
51
|
"SampleSheetV2",
|
|
51
52
|
"SampleSheetFactory",
|
|
53
|
+
"SampleSheetParser",
|
|
52
54
|
"SampleSheetVersion",
|
|
53
55
|
"IndexType",
|
|
54
56
|
"InstrumentPlatform",
|
|
55
57
|
"UMILocation",
|
|
56
58
|
"SampleSheetValidator",
|
|
57
59
|
"ValidationResult",
|
|
60
|
+
"hamming_distance",
|
|
58
61
|
"SampleSheetConverter",
|
|
59
62
|
"SampleSheetDiff",
|
|
60
63
|
"DiffResult",
|
samplesheet_parser/converter.py
CHANGED
|
@@ -54,10 +54,9 @@ Examples
|
|
|
54
54
|
|
|
55
55
|
from __future__ import annotations
|
|
56
56
|
|
|
57
|
+
import logging
|
|
57
58
|
from pathlib import Path
|
|
58
59
|
|
|
59
|
-
from loguru import logger
|
|
60
|
-
|
|
61
60
|
from samplesheet_parser.enums import SampleSheetVersion
|
|
62
61
|
from samplesheet_parser.factory import SampleSheetFactory
|
|
63
62
|
from samplesheet_parser.instruments import (
|
|
@@ -69,6 +68,8 @@ from samplesheet_parser.instruments import (
|
|
|
69
68
|
from samplesheet_parser.parsers.v1 import SampleSheetV1
|
|
70
69
|
from samplesheet_parser.parsers.v2 import SampleSheetV2
|
|
71
70
|
|
|
71
|
+
logger = logging.getLogger(__name__)
|
|
72
|
+
|
|
72
73
|
# ---------------------------------------------------------------------------
|
|
73
74
|
# Field mappings
|
|
74
75
|
# ---------------------------------------------------------------------------
|
|
@@ -267,7 +268,11 @@ class SampleSheetConverter:
|
|
|
267
268
|
raise ValueError("Source sheet is already V1. No conversion needed.")
|
|
268
269
|
|
|
269
270
|
sheet = self._sheet
|
|
270
|
-
|
|
271
|
+
if not isinstance(sheet, SampleSheetV2):
|
|
272
|
+
raise TypeError(
|
|
273
|
+
f"Expected SampleSheetV2 for V2 \N{RIGHTWARDS ARROW} V1 conversion, "
|
|
274
|
+
f"got {type(sheet).__name__!r}."
|
|
275
|
+
)
|
|
271
276
|
|
|
272
277
|
# Resolve workflow up front. Prefer InstrumentPlatform; fall back
|
|
273
278
|
# to InstrumentType (some V2 sheets only declare the latter).
|
samplesheet_parser/diff.py
CHANGED
|
@@ -29,15 +29,16 @@ Examples
|
|
|
29
29
|
|
|
30
30
|
from __future__ import annotations
|
|
31
31
|
|
|
32
|
+
import logging
|
|
32
33
|
from dataclasses import dataclass, field
|
|
33
34
|
from pathlib import Path
|
|
34
35
|
from typing import Any
|
|
35
36
|
|
|
36
|
-
from loguru import logger
|
|
37
|
-
|
|
38
37
|
from samplesheet_parser.enums import SampleSheetVersion
|
|
39
38
|
from samplesheet_parser.factory import SampleSheetFactory
|
|
40
39
|
|
|
40
|
+
logger = logging.getLogger(__name__)
|
|
41
|
+
|
|
41
42
|
# ---------------------------------------------------------------------------
|
|
42
43
|
# Field normalisation map
|
|
43
44
|
# V1 and V2 use different names for the same logical field.
|
|
@@ -46,15 +47,15 @@ from samplesheet_parser.factory import SampleSheetFactory
|
|
|
46
47
|
|
|
47
48
|
_FIELD_ALIASES: dict[str, str] = {
|
|
48
49
|
# index
|
|
49
|
-
"index":
|
|
50
|
-
"i7_index_id":
|
|
50
|
+
"index": "Index",
|
|
51
|
+
"i7_index_id": "I7_Index_ID",
|
|
51
52
|
# index2
|
|
52
|
-
"index2":
|
|
53
|
-
"i5_index_id":
|
|
53
|
+
"index2": "Index2",
|
|
54
|
+
"i5_index_id": "I5_Index_ID",
|
|
54
55
|
# project
|
|
55
56
|
"sample_project": "Sample_Project",
|
|
56
57
|
# name
|
|
57
|
-
"sample_name":
|
|
58
|
+
"sample_name": "Sample_Name",
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
# Fields to skip when comparing samples — these are metadata that differ
|
|
@@ -64,18 +65,20 @@ _FIELD_ALIASES: dict[str, str] = {
|
|
|
64
65
|
# I7_Index_ID / I5_Index_ID — index name columns; V2 stores index values only
|
|
65
66
|
# Sample_Name — V1 carries a display name; V2 omits it
|
|
66
67
|
# Sample_Plate / Sample_Well / Description — IEM metadata not present in V2
|
|
67
|
-
_SKIP_FIELDS: frozenset[str] = frozenset(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
_SKIP_FIELDS: frozenset[str] = frozenset(
|
|
69
|
+
{
|
|
70
|
+
"experiment_name",
|
|
71
|
+
"run_name",
|
|
72
|
+
"iem_version",
|
|
73
|
+
# V1 IEM metadata columns absent from V2
|
|
74
|
+
"I7_Index_ID",
|
|
75
|
+
"I5_Index_ID",
|
|
76
|
+
"Sample_Name",
|
|
77
|
+
"Sample_Plate",
|
|
78
|
+
"Sample_Well",
|
|
79
|
+
"Description",
|
|
80
|
+
}
|
|
81
|
+
)
|
|
79
82
|
|
|
80
83
|
|
|
81
84
|
def _normalise_key(k: str) -> str:
|
|
@@ -96,7 +99,7 @@ def _normalise_record(record: dict[str, str]) -> dict[str, str]:
|
|
|
96
99
|
def _sample_key(record: dict[str, str]) -> tuple[str, str]:
|
|
97
100
|
"""Return a ``(lane, sample_id)`` tuple for keying samples."""
|
|
98
101
|
lane = record.get("Lane") or record.get("lane") or "1"
|
|
99
|
-
sid
|
|
102
|
+
sid = record.get("Sample_ID") or record.get("sample_id") or ""
|
|
100
103
|
return (lane.strip(), sid.strip())
|
|
101
104
|
|
|
102
105
|
|
|
@@ -104,13 +107,15 @@ def _sample_key(record: dict[str, str]) -> tuple[str, str]:
|
|
|
104
107
|
# Result dataclasses
|
|
105
108
|
# ---------------------------------------------------------------------------
|
|
106
109
|
|
|
110
|
+
|
|
107
111
|
@dataclass
|
|
108
112
|
class HeaderChange:
|
|
109
113
|
"""A single key/value change in the header or settings section."""
|
|
110
|
-
|
|
114
|
+
|
|
115
|
+
field: str
|
|
111
116
|
old_value: str | None
|
|
112
117
|
new_value: str | None
|
|
113
|
-
section:
|
|
118
|
+
section: str = "header" # "header" | "reads" | "settings"
|
|
114
119
|
|
|
115
120
|
def __str__(self) -> str:
|
|
116
121
|
old = repr(self.old_value) if self.old_value is not None else "—"
|
|
@@ -121,9 +126,10 @@ class HeaderChange:
|
|
|
121
126
|
@dataclass
|
|
122
127
|
class SampleChange:
|
|
123
128
|
"""A set of field-level changes for a single sample."""
|
|
124
|
-
|
|
129
|
+
|
|
130
|
+
lane: str
|
|
125
131
|
sample_id: str
|
|
126
|
-
changes:
|
|
132
|
+
changes: dict[str, tuple[str | None, str | None]] = field(default_factory=dict)
|
|
127
133
|
# changes maps field_name → (old_value, new_value)
|
|
128
134
|
|
|
129
135
|
def __str__(self) -> str:
|
|
@@ -155,21 +161,19 @@ class DiffResult:
|
|
|
155
161
|
sample_changes:
|
|
156
162
|
Per-sample field-level diffs for samples present in both sheets.
|
|
157
163
|
"""
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
164
|
+
|
|
165
|
+
source_version: SampleSheetVersion
|
|
166
|
+
target_version: SampleSheetVersion
|
|
167
|
+
header_changes: list[HeaderChange] = field(default_factory=list)
|
|
168
|
+
samples_added: list[dict[str, str]] = field(default_factory=list)
|
|
162
169
|
samples_removed: list[dict[str, str]] = field(default_factory=list)
|
|
163
|
-
sample_changes:
|
|
170
|
+
sample_changes: list[SampleChange] = field(default_factory=list)
|
|
164
171
|
|
|
165
172
|
@property
|
|
166
173
|
def has_changes(self) -> bool:
|
|
167
174
|
"""``True`` if any difference was detected."""
|
|
168
175
|
return bool(
|
|
169
|
-
self.header_changes
|
|
170
|
-
or self.samples_added
|
|
171
|
-
or self.samples_removed
|
|
172
|
-
or self.sample_changes
|
|
176
|
+
self.header_changes or self.samples_added or self.samples_removed or self.sample_changes
|
|
173
177
|
)
|
|
174
178
|
|
|
175
179
|
def summary(self) -> str:
|
|
@@ -180,9 +184,7 @@ class DiffResult:
|
|
|
180
184
|
f"({self.source_version.value} → {self.target_version.value})."
|
|
181
185
|
)
|
|
182
186
|
|
|
183
|
-
parts: list[str] = [
|
|
184
|
-
f"Diff ({self.source_version.value} → {self.target_version.value}):"
|
|
185
|
-
]
|
|
187
|
+
parts: list[str] = [f"Diff ({self.source_version.value} → {self.target_version.value}):"]
|
|
186
188
|
if self.header_changes:
|
|
187
189
|
parts.append(f" {len(self.header_changes)} header/settings change(s)")
|
|
188
190
|
if self.samples_added:
|
|
@@ -192,9 +194,7 @@ class DiffResult:
|
|
|
192
194
|
if self.samples_removed:
|
|
193
195
|
ids = ", ".join(r.get("Sample_ID", "?") for r in self.samples_removed[:5])
|
|
194
196
|
tail = (
|
|
195
|
-
f" … +{len(self.samples_removed) - 5} more"
|
|
196
|
-
if len(self.samples_removed) > 5
|
|
197
|
-
else ""
|
|
197
|
+
f" … +{len(self.samples_removed) - 5} more" if len(self.samples_removed) > 5 else ""
|
|
198
198
|
)
|
|
199
199
|
parts.append(f" {len(self.samples_removed)} sample(s) removed: {ids}{tail}")
|
|
200
200
|
if self.sample_changes:
|
|
@@ -224,6 +224,7 @@ class DiffResult:
|
|
|
224
224
|
# Diff engine
|
|
225
225
|
# ---------------------------------------------------------------------------
|
|
226
226
|
|
|
227
|
+
|
|
227
228
|
class SampleSheetDiff:
|
|
228
229
|
"""
|
|
229
230
|
Compare two Illumina sample sheets and return a structured diff.
|
|
@@ -348,8 +349,8 @@ class SampleSheetDiff:
|
|
|
348
349
|
|
|
349
350
|
def _diff_settings(self, result: DiffResult) -> None:
|
|
350
351
|
"""Diff the [Settings] / [BCLConvert_Settings] section."""
|
|
351
|
-
old_s = self._old
|
|
352
|
-
new_s = self._new
|
|
352
|
+
old_s = getattr(self._old, "settings", None) or {}
|
|
353
|
+
new_s = getattr(self._new, "settings", None) or {}
|
|
353
354
|
|
|
354
355
|
all_keys = set(old_s) | set(new_s)
|
|
355
356
|
for key in sorted(all_keys):
|
|
@@ -367,8 +368,8 @@ class SampleSheetDiff:
|
|
|
367
368
|
|
|
368
369
|
def _diff_samples(self, result: DiffResult) -> None:
|
|
369
370
|
"""Diff the [Data] / [BCLConvert_Data] records."""
|
|
370
|
-
old_records = self._old
|
|
371
|
-
new_records = self._new
|
|
371
|
+
old_records = getattr(self._old, "records", None) or []
|
|
372
|
+
new_records = getattr(self._new, "records", None) or []
|
|
372
373
|
|
|
373
374
|
# Build lookup dicts keyed on (lane, sample_id)
|
|
374
375
|
old_map: dict[tuple[str, str], dict[str, str]] = {
|
samplesheet_parser/enums.py
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Enumerations for samplesheet-parser.
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
|
-
from enum import
|
|
5
|
+
from enum import Enum
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
class SampleSheetVersion(
|
|
8
|
+
class SampleSheetVersion(str, Enum):
|
|
9
9
|
"""Illumina sample sheet format version.
|
|
10
10
|
|
|
11
11
|
V1 — Illumina Experiment Manager (IEM) format, used with bcl2fastq.
|
|
@@ -17,35 +17,39 @@ class SampleSheetVersion(StrEnum):
|
|
|
17
17
|
or by the presence of ``[BCLConvert_Settings]`` / ``[BCLConvert_Data]``
|
|
18
18
|
sections.
|
|
19
19
|
"""
|
|
20
|
+
|
|
20
21
|
V1 = "V1"
|
|
21
22
|
V2 = "V2"
|
|
22
23
|
|
|
23
24
|
|
|
24
|
-
class IndexType(
|
|
25
|
+
class IndexType(str, Enum):
|
|
25
26
|
"""Sequencing index configuration.
|
|
26
27
|
|
|
27
28
|
SINGLE — I7 index only (single-index libraries).
|
|
28
29
|
DUAL — I7 + I5 indexes (dual-index libraries, standard for modern workflows).
|
|
29
30
|
NONE — No index (rare; full-lane libraries).
|
|
30
31
|
"""
|
|
32
|
+
|
|
31
33
|
SINGLE = "single"
|
|
32
|
-
DUAL
|
|
33
|
-
NONE
|
|
34
|
+
DUAL = "dual"
|
|
35
|
+
NONE = "none"
|
|
34
36
|
|
|
35
37
|
|
|
36
|
-
class InstrumentPlatform(
|
|
38
|
+
class InstrumentPlatform(str, Enum):
|
|
37
39
|
"""Standard Illumina instrument platform identifiers used in V2 sample sheets."""
|
|
38
|
-
NOVASEQ_6000 = "NovaSeq6000"
|
|
39
|
-
NOVASEQ_X_SERIES = "NovaSeqXSeries"
|
|
40
|
-
NEXTSEQ_1000_2000 = "NextSeq1000/2000"
|
|
41
|
-
NEXTSEQ_550 = "NextSeq550"
|
|
42
|
-
MISEQ = "MiSeq"
|
|
43
|
-
HISEQ_X = "HiSeqX"
|
|
44
40
|
|
|
41
|
+
NOVASEQ_6000 = "NovaSeq6000"
|
|
42
|
+
NOVASEQ_X_SERIES = "NovaSeqXSeries"
|
|
43
|
+
NEXTSEQ_1000_2000 = "NextSeq1000/2000"
|
|
44
|
+
NEXTSEQ_550 = "NextSeq550"
|
|
45
|
+
MISEQ = "MiSeq"
|
|
46
|
+
HISEQ_X = "HiSeqX"
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
|
|
49
|
+
class UMILocation(str, Enum):
|
|
47
50
|
"""Where the UMI is encoded in the read structure (OverrideCycles string)."""
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
|
|
52
|
+
READ1 = "read1"
|
|
53
|
+
READ2 = "read2"
|
|
50
54
|
INDEX1 = "index1"
|
|
51
55
|
INDEX2 = "index2"
|
samplesheet_parser/factory.py
CHANGED
|
@@ -32,18 +32,27 @@ Examples
|
|
|
32
32
|
>>> factory = SampleSheetFactory()
|
|
33
33
|
>>> sheet = factory.create_parser("SampleSheet.csv")
|
|
34
34
|
>>> print(factory.version) # SampleSheetVersion.V2
|
|
35
|
+
>>>
|
|
36
|
+
>>> # Register a custom parser for a hypothetical V3 format
|
|
37
|
+
>>> def _is_v3(path):
|
|
38
|
+
... with open(path) as fh:
|
|
39
|
+
... return "FileFormatVersion,3" in fh.read(512)
|
|
40
|
+
>>> SampleSheetFactory.register(_is_v3, MyV3Parser, SampleSheetVersion.V2)
|
|
35
41
|
"""
|
|
36
42
|
|
|
37
43
|
from __future__ import annotations
|
|
38
44
|
|
|
45
|
+
import logging
|
|
46
|
+
from collections.abc import Callable
|
|
39
47
|
from pathlib import Path
|
|
40
48
|
from typing import Any
|
|
41
49
|
|
|
42
|
-
from loguru import logger
|
|
43
|
-
|
|
44
50
|
from samplesheet_parser.enums import SampleSheetVersion
|
|
45
51
|
from samplesheet_parser.parsers.v1 import SampleSheetV1
|
|
46
52
|
from samplesheet_parser.parsers.v2 import SampleSheetV2
|
|
53
|
+
from samplesheet_parser.protocol import SampleSheetParser
|
|
54
|
+
|
|
55
|
+
logger = logging.getLogger(__name__)
|
|
47
56
|
|
|
48
57
|
|
|
49
58
|
class SampleSheetFactory:
|
|
@@ -66,9 +75,50 @@ class SampleSheetFactory:
|
|
|
66
75
|
>>> print(sheet.samples())
|
|
67
76
|
"""
|
|
68
77
|
|
|
78
|
+
# Class-level registry: (detector_fn, parser_class, version) tuples, LIFO.
|
|
79
|
+
_registry: list[tuple[Callable[[Path], bool], type[Any], SampleSheetVersion]] = []
|
|
80
|
+
|
|
69
81
|
def __init__(self) -> None:
|
|
70
82
|
self.version: SampleSheetVersion | None = None
|
|
71
|
-
self.parser:
|
|
83
|
+
self.parser: SampleSheetParser | None = None
|
|
84
|
+
|
|
85
|
+
# ------------------------------------------------------------------
|
|
86
|
+
# Class-level registration
|
|
87
|
+
# ------------------------------------------------------------------
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
def register(
|
|
91
|
+
cls,
|
|
92
|
+
detector: Callable[[Path], bool],
|
|
93
|
+
parser_class: type[Any],
|
|
94
|
+
version: SampleSheetVersion,
|
|
95
|
+
) -> None:
|
|
96
|
+
"""Register a custom format detector and parser.
|
|
97
|
+
|
|
98
|
+
Registered detectors are tried before the built-in V1/V2 detection,
|
|
99
|
+
in LIFO order — the most recently registered detector wins when
|
|
100
|
+
multiple detectors match.
|
|
101
|
+
|
|
102
|
+
Parameters
|
|
103
|
+
----------
|
|
104
|
+
detector:
|
|
105
|
+
Callable that receives the file :class:`~pathlib.Path` and returns
|
|
106
|
+
``True`` if *parser_class* should handle this file.
|
|
107
|
+
parser_class:
|
|
108
|
+
Parser class to instantiate when *detector* returns ``True``.
|
|
109
|
+
version:
|
|
110
|
+
:class:`~samplesheet_parser.SampleSheetVersion` to record for
|
|
111
|
+
this format.
|
|
112
|
+
"""
|
|
113
|
+
cls._registry.insert(0, (detector, parser_class, version))
|
|
114
|
+
logger.debug(
|
|
115
|
+
f"Registered custom parser {parser_class.__name__!r} " f"for version {version.value!r}."
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
@classmethod
|
|
119
|
+
def clear_registry(cls) -> None:
|
|
120
|
+
"""Remove all custom registrations (useful in tests)."""
|
|
121
|
+
cls._registry.clear()
|
|
72
122
|
|
|
73
123
|
# ------------------------------------------------------------------
|
|
74
124
|
# Public API
|
|
@@ -81,7 +131,7 @@ class SampleSheetFactory:
|
|
|
81
131
|
clean: bool = True,
|
|
82
132
|
experiment_id: str | None = None,
|
|
83
133
|
parse: bool | None = None,
|
|
84
|
-
) ->
|
|
134
|
+
) -> SampleSheetParser:
|
|
85
135
|
"""Detect the sample sheet format and return the appropriate parser.
|
|
86
136
|
|
|
87
137
|
The returned parser shares the same interface:
|
|
@@ -104,7 +154,7 @@ class SampleSheetFactory:
|
|
|
104
154
|
|
|
105
155
|
Returns
|
|
106
156
|
-------
|
|
107
|
-
|
|
157
|
+
SampleSheetParser
|
|
108
158
|
The version-appropriate parser instance.
|
|
109
159
|
|
|
110
160
|
Raises
|
|
@@ -119,12 +169,27 @@ class SampleSheetFactory:
|
|
|
119
169
|
raise FileNotFoundError(f"Sample sheet not found: {path}")
|
|
120
170
|
|
|
121
171
|
logger.info(f"Detecting sample sheet format for: {path}")
|
|
122
|
-
|
|
172
|
+
kwargs: dict[str, Any] = dict(clean=clean, experiment_id=experiment_id, parse=parse)
|
|
123
173
|
|
|
174
|
+
# Try registered custom detectors first (LIFO order).
|
|
175
|
+
for detector, parser_class, ver in self._registry:
|
|
176
|
+
try:
|
|
177
|
+
if detector(path):
|
|
178
|
+
logger.info(
|
|
179
|
+
f"Custom detector matched — using {parser_class.__name__!r} "
|
|
180
|
+
f"(version={ver.value!r})"
|
|
181
|
+
)
|
|
182
|
+
self.version = ver
|
|
183
|
+
parser: SampleSheetParser = parser_class(path, **kwargs)
|
|
184
|
+
self.parser = parser
|
|
185
|
+
return parser
|
|
186
|
+
except Exception as exc:
|
|
187
|
+
logger.debug(f"Custom detector {detector!r} raised {exc!r} for {path} — skipping.")
|
|
188
|
+
|
|
189
|
+
# Built-in V1/V2 detection.
|
|
190
|
+
detected = self._detect_version(path)
|
|
124
191
|
self.version = detected
|
|
125
|
-
kwargs: dict[str, Any] = dict(clean=clean, experiment_id=experiment_id, parse=parse)
|
|
126
192
|
|
|
127
|
-
parser: SampleSheetV1 | SampleSheetV2
|
|
128
193
|
if detected == SampleSheetVersion.V2:
|
|
129
194
|
logger.info("Detected BCLConvert V2 format — using SampleSheetV2")
|
|
130
195
|
parser = SampleSheetV2(path, **kwargs)
|
|
@@ -154,10 +219,9 @@ class SampleSheetFactory:
|
|
|
154
219
|
if self.parser is None:
|
|
155
220
|
raise RuntimeError("Call create_parser() before get_umi_length().")
|
|
156
221
|
|
|
157
|
-
if self.version == SampleSheetVersion.V2:
|
|
158
|
-
return self.parser.get_umi_length()
|
|
222
|
+
if self.version == SampleSheetVersion.V2 and isinstance(self.parser, SampleSheetV2):
|
|
223
|
+
return self.parser.get_umi_length()
|
|
159
224
|
|
|
160
|
-
# V1: UMI length is occasionally stored as IndexUMILength in [Header]
|
|
161
225
|
if isinstance(self.parser, SampleSheetV1):
|
|
162
226
|
if self.parser.header:
|
|
163
227
|
try:
|
samplesheet_parser/filter.py
CHANGED
|
@@ -25,15 +25,16 @@ Chaitanya Kasaraneni
|
|
|
25
25
|
from __future__ import annotations
|
|
26
26
|
|
|
27
27
|
import fnmatch
|
|
28
|
+
import logging
|
|
28
29
|
from dataclasses import dataclass
|
|
29
30
|
from pathlib import Path
|
|
30
31
|
from typing import Any
|
|
31
32
|
|
|
32
|
-
from loguru import logger
|
|
33
|
-
|
|
34
33
|
from samplesheet_parser.enums import SampleSheetVersion
|
|
35
34
|
from samplesheet_parser.factory import SampleSheetFactory
|
|
36
35
|
|
|
36
|
+
logger = logging.getLogger(__name__)
|
|
37
|
+
|
|
37
38
|
# Raw record keys that are standard sample fields — not extra columns
|
|
38
39
|
_STANDARD_SAMPLE_KEYS: frozenset[str] = frozenset(
|
|
39
40
|
{
|
|
@@ -39,10 +39,10 @@ Chaitanya Kasaraneni
|
|
|
39
39
|
from __future__ import annotations
|
|
40
40
|
|
|
41
41
|
import re
|
|
42
|
-
from enum import
|
|
42
|
+
from enum import Enum
|
|
43
43
|
|
|
44
44
|
|
|
45
|
-
class Workflow(
|
|
45
|
+
class Workflow(str, Enum):
|
|
46
46
|
"""i5 orientation workflow for an Illumina instrument."""
|
|
47
47
|
|
|
48
48
|
A = "a"
|
|
@@ -117,16 +117,30 @@ def _normalize(name: str) -> str:
|
|
|
117
117
|
return _RX_NON_ALNUM.sub("", name.lower())
|
|
118
118
|
|
|
119
119
|
|
|
120
|
-
def detect_workflow(
|
|
120
|
+
def detect_workflow(
|
|
121
|
+
instrument: str | None,
|
|
122
|
+
*,
|
|
123
|
+
extra_a: frozenset[str] | None = None,
|
|
124
|
+
extra_b: frozenset[str] | None = None,
|
|
125
|
+
) -> Workflow | None:
|
|
121
126
|
"""Classify an instrument name as workflow A or B.
|
|
122
127
|
|
|
123
128
|
Parameters
|
|
124
129
|
----------
|
|
125
130
|
instrument:
|
|
126
|
-
Free-form instrument name.
|
|
131
|
+
Free-form instrument name. Matched against a normalised form of
|
|
127
132
|
:data:`WORKFLOW_A_INSTRUMENTS` and :data:`WORKFLOW_B_INSTRUMENTS`
|
|
128
133
|
so spacing, case, and punctuation are ignored
|
|
129
134
|
(``"NovaSeq X Plus"`` ≡ ``"novaseqxplus"``).
|
|
135
|
+
extra_a:
|
|
136
|
+
Optional frozenset of additional normalised instrument names to
|
|
137
|
+
treat as workflow A (forward i5). Useful for new instruments not
|
|
138
|
+
yet in the built-in tables, without needing a library update.
|
|
139
|
+
Names are normalised the same way as *instrument* (lowercase,
|
|
140
|
+
non-alphanumerics stripped) before being added to the lookup set.
|
|
141
|
+
extra_b:
|
|
142
|
+
Optional frozenset of additional normalised instrument names to
|
|
143
|
+
treat as workflow B (RC i5 on chip).
|
|
130
144
|
|
|
131
145
|
Returns
|
|
132
146
|
-------
|
|
@@ -134,10 +148,27 @@ def detect_workflow(instrument: str | None) -> Workflow | None:
|
|
|
134
148
|
:attr:`Workflow.A` or :attr:`Workflow.B` if the instrument is in
|
|
135
149
|
one of the tables. ``None`` if *instrument* is empty, ambiguous
|
|
136
150
|
(e.g. ``NovaSeq 6000`` — chemistry-dependent), or unrecognised.
|
|
151
|
+
|
|
152
|
+
Examples
|
|
153
|
+
--------
|
|
154
|
+
>>> detect_workflow("MiSeq")
|
|
155
|
+
<Workflow.A: 'a'>
|
|
156
|
+
>>> detect_workflow("NovaSeq X Plus")
|
|
157
|
+
<Workflow.B: 'b'>
|
|
158
|
+
>>> detect_workflow("MyCustomSeq", extra_b=frozenset({"mycustomseq"}))
|
|
159
|
+
<Workflow.B: 'b'>
|
|
137
160
|
"""
|
|
138
161
|
if not instrument:
|
|
139
162
|
return None
|
|
140
163
|
key = _normalize(instrument)
|
|
164
|
+
|
|
165
|
+
# Caller-supplied overrides take priority over built-in tables so that
|
|
166
|
+
# new instruments can be handled without a library update.
|
|
167
|
+
if extra_b and key in {_normalize(n) for n in extra_b}:
|
|
168
|
+
return Workflow.B
|
|
169
|
+
if extra_a and key in {_normalize(n) for n in extra_a}:
|
|
170
|
+
return Workflow.A
|
|
171
|
+
|
|
141
172
|
if key in WORKFLOW_A_INSTRUMENTS:
|
|
142
173
|
return Workflow.A
|
|
143
174
|
if key in WORKFLOW_B_INSTRUMENTS:
|
samplesheet_parser/merger.py
CHANGED
|
@@ -33,20 +33,21 @@ Illumina Experiment Manager User Guide (document # 15031320)
|
|
|
33
33
|
|
|
34
34
|
from __future__ import annotations
|
|
35
35
|
|
|
36
|
+
import logging
|
|
36
37
|
from dataclasses import dataclass, field
|
|
37
38
|
from pathlib import Path
|
|
38
39
|
from typing import Any
|
|
39
40
|
|
|
40
|
-
from loguru import logger
|
|
41
|
-
|
|
42
41
|
from samplesheet_parser.enums import SampleSheetVersion
|
|
43
42
|
from samplesheet_parser.factory import SampleSheetFactory
|
|
44
43
|
from samplesheet_parser.validators import (
|
|
45
44
|
MIN_HAMMING_DISTANCE,
|
|
46
45
|
SampleSheetValidator,
|
|
47
|
-
|
|
46
|
+
hamming_distance,
|
|
48
47
|
)
|
|
49
48
|
|
|
49
|
+
logger = logging.getLogger(__name__)
|
|
50
|
+
|
|
50
51
|
# ---------------------------------------------------------------------------
|
|
51
52
|
# Result types
|
|
52
53
|
# ---------------------------------------------------------------------------
|
|
@@ -524,7 +525,7 @@ class SampleSheetMerger:
|
|
|
524
525
|
if path_a == path_b:
|
|
525
526
|
continue
|
|
526
527
|
|
|
527
|
-
dist =
|
|
528
|
+
dist = hamming_distance(combined_a, combined_b)
|
|
528
529
|
if dist < min_distance:
|
|
529
530
|
result.add_warning(
|
|
530
531
|
"INDEX_DISTANCE_TOO_LOW",
|