uniprotlib 0.1.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.
- uniprotlib/__init__.py +11 -0
- uniprotlib/models.py +47 -0
- uniprotlib/parser.py +193 -0
- uniprotlib/py.typed +0 -0
- uniprotlib-0.1.0.dist-info/METADATA +72 -0
- uniprotlib-0.1.0.dist-info/RECORD +8 -0
- uniprotlib-0.1.0.dist-info/WHEEL +4 -0
- uniprotlib-0.1.0.dist-info/licenses/LICENSE +21 -0
uniprotlib/__init__.py
ADDED
uniprotlib/models.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass(slots=True)
|
|
5
|
+
class Organism:
|
|
6
|
+
scientific_name: str | None
|
|
7
|
+
common_name: str | None
|
|
8
|
+
tax_id: str | None
|
|
9
|
+
lineage: list[str]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(slots=True)
|
|
13
|
+
class Gene:
|
|
14
|
+
primary: str | None
|
|
15
|
+
synonyms: list[str]
|
|
16
|
+
ordered_locus_names: list[str]
|
|
17
|
+
orf_names: list[str]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(slots=True)
|
|
21
|
+
class Sequence:
|
|
22
|
+
value: str
|
|
23
|
+
length: int
|
|
24
|
+
mass: int
|
|
25
|
+
checksum: str
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(slots=True)
|
|
29
|
+
class DbReference:
|
|
30
|
+
type: str
|
|
31
|
+
id: str
|
|
32
|
+
molecule: str | None
|
|
33
|
+
properties: dict[str, str]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(slots=True)
|
|
37
|
+
class UniProtEntry:
|
|
38
|
+
primary_accession: str
|
|
39
|
+
accessions: list[str]
|
|
40
|
+
entry_name: str
|
|
41
|
+
dataset: str
|
|
42
|
+
protein_name: str | None
|
|
43
|
+
gene: Gene | None
|
|
44
|
+
organism: Organism
|
|
45
|
+
sequence: Sequence
|
|
46
|
+
keywords: list[str]
|
|
47
|
+
db_references: list[DbReference]
|
uniprotlib/parser.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import gzip
|
|
4
|
+
from collections.abc import Iterator
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from lxml import etree
|
|
8
|
+
|
|
9
|
+
from .models import DbReference, Gene, Organism, Sequence, UniProtEntry
|
|
10
|
+
|
|
11
|
+
_NS_HTTP = "http://uniprot.org/uniprot"
|
|
12
|
+
_NS_HTTPS = "https://uniprot.org/uniprot"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _detect_namespace(path: Path) -> str:
|
|
16
|
+
"""Detect which namespace variant a UniProt XML file uses.
|
|
17
|
+
|
|
18
|
+
Single-entry downloads use http://, bulk FTP downloads use https://.
|
|
19
|
+
"""
|
|
20
|
+
opener = gzip.open if path.suffix == ".gz" else open
|
|
21
|
+
with opener(path, "rb") as f: # type: ignore[arg-type]
|
|
22
|
+
chunk = f.read(2048)
|
|
23
|
+
if b"https://uniprot.org/uniprot" in chunk:
|
|
24
|
+
return _NS_HTTPS
|
|
25
|
+
return _NS_HTTP
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _tag(ns: str, name: str) -> str:
|
|
29
|
+
return f"{{{ns}}}{name}"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _parse_gene(ns: str, entry: etree._Element) -> Gene | None:
|
|
33
|
+
gene_elem = entry.find(_tag(ns, "gene"))
|
|
34
|
+
if gene_elem is None:
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
primary = None
|
|
38
|
+
synonyms: list[str] = []
|
|
39
|
+
ordered_locus: list[str] = []
|
|
40
|
+
orfs: list[str] = []
|
|
41
|
+
|
|
42
|
+
for name_elem in gene_elem.findall(_tag(ns, "name")):
|
|
43
|
+
name_type = name_elem.attrib.get("type")
|
|
44
|
+
text = name_elem.text or ""
|
|
45
|
+
if name_type == "primary":
|
|
46
|
+
primary = text
|
|
47
|
+
elif name_type == "synonym":
|
|
48
|
+
synonyms.append(text)
|
|
49
|
+
elif name_type == "ordered locus":
|
|
50
|
+
ordered_locus.append(text)
|
|
51
|
+
elif name_type == "ORF":
|
|
52
|
+
orfs.append(text)
|
|
53
|
+
|
|
54
|
+
return Gene(
|
|
55
|
+
primary=primary,
|
|
56
|
+
synonyms=synonyms,
|
|
57
|
+
ordered_locus_names=ordered_locus,
|
|
58
|
+
orf_names=orfs,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _parse_organism(ns: str, entry: etree._Element) -> Organism:
|
|
63
|
+
org_elem = entry.find(_tag(ns, "organism"))
|
|
64
|
+
|
|
65
|
+
scientific_name = None
|
|
66
|
+
common_name = None
|
|
67
|
+
tax_id = None
|
|
68
|
+
lineage: list[str] = []
|
|
69
|
+
|
|
70
|
+
if org_elem is not None:
|
|
71
|
+
for name_elem in org_elem.findall(_tag(ns, "name")):
|
|
72
|
+
name_type = name_elem.attrib.get("type")
|
|
73
|
+
if name_type == "scientific":
|
|
74
|
+
scientific_name = name_elem.text
|
|
75
|
+
elif name_type == "common":
|
|
76
|
+
common_name = name_elem.text
|
|
77
|
+
|
|
78
|
+
tax_ref = org_elem.find(
|
|
79
|
+
f"{_tag(ns, 'dbReference')}[@type='NCBI Taxonomy']"
|
|
80
|
+
)
|
|
81
|
+
if tax_ref is not None:
|
|
82
|
+
tax_id = tax_ref.attrib.get("id")
|
|
83
|
+
|
|
84
|
+
lineage_elem = org_elem.find(_tag(ns, "lineage"))
|
|
85
|
+
if lineage_elem is not None:
|
|
86
|
+
lineage = [
|
|
87
|
+
t.text or ""
|
|
88
|
+
for t in lineage_elem.findall(_tag(ns, "taxon"))
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
return Organism(
|
|
92
|
+
scientific_name=scientific_name,
|
|
93
|
+
common_name=common_name,
|
|
94
|
+
tax_id=tax_id,
|
|
95
|
+
lineage=lineage,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _parse_sequence(ns: str, entry: etree._Element) -> Sequence:
|
|
100
|
+
seq_elem = entry.find(_tag(ns, "sequence"))
|
|
101
|
+
raw = seq_elem.text or ""
|
|
102
|
+
value = raw.replace("\n", "").replace(" ", "")
|
|
103
|
+
|
|
104
|
+
return Sequence(
|
|
105
|
+
value=value,
|
|
106
|
+
length=int(seq_elem.attrib.get("length", 0)),
|
|
107
|
+
mass=int(seq_elem.attrib.get("mass", 0)),
|
|
108
|
+
checksum=seq_elem.attrib.get("checksum", ""),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _parse_db_references(ns: str, entry: etree._Element) -> list[DbReference]:
|
|
113
|
+
refs: list[DbReference] = []
|
|
114
|
+
for elem in entry.findall(_tag(ns, "dbReference")):
|
|
115
|
+
molecule_elem = elem.find(_tag(ns, "molecule"))
|
|
116
|
+
molecule = molecule_elem.attrib.get("id") if molecule_elem is not None else None
|
|
117
|
+
|
|
118
|
+
properties = {
|
|
119
|
+
prop.attrib["type"]: prop.attrib["value"]
|
|
120
|
+
for prop in elem.findall(_tag(ns, "property"))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
refs.append(DbReference(
|
|
124
|
+
type=elem.attrib["type"],
|
|
125
|
+
id=elem.attrib["id"],
|
|
126
|
+
molecule=molecule,
|
|
127
|
+
properties=properties,
|
|
128
|
+
))
|
|
129
|
+
return refs
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _parse_entry(ns: str, entry: etree._Element) -> UniProtEntry:
|
|
133
|
+
accessions = [
|
|
134
|
+
acc.text or ""
|
|
135
|
+
for acc in entry.findall(_tag(ns, "accession"))
|
|
136
|
+
]
|
|
137
|
+
|
|
138
|
+
names = entry.findall(_tag(ns, "name"))
|
|
139
|
+
entry_name = names[0].text or "" if names else ""
|
|
140
|
+
|
|
141
|
+
protein_name = None
|
|
142
|
+
name_elem = entry.find(
|
|
143
|
+
f"{_tag(ns, 'protein')}/{_tag(ns, 'recommendedName')}/{_tag(ns, 'fullName')}"
|
|
144
|
+
)
|
|
145
|
+
if name_elem is not None:
|
|
146
|
+
protein_name = name_elem.text
|
|
147
|
+
|
|
148
|
+
keywords = [
|
|
149
|
+
kw.text or ""
|
|
150
|
+
for kw in entry.findall(_tag(ns, "keyword"))
|
|
151
|
+
]
|
|
152
|
+
|
|
153
|
+
return UniProtEntry(
|
|
154
|
+
primary_accession=accessions[0] if accessions else "",
|
|
155
|
+
accessions=accessions,
|
|
156
|
+
entry_name=entry_name,
|
|
157
|
+
dataset=entry.attrib.get("dataset", ""),
|
|
158
|
+
protein_name=protein_name,
|
|
159
|
+
gene=_parse_gene(ns, entry),
|
|
160
|
+
organism=_parse_organism(ns, entry),
|
|
161
|
+
sequence=_parse_sequence(ns, entry),
|
|
162
|
+
keywords=keywords,
|
|
163
|
+
db_references=_parse_db_references(ns, entry),
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _parse_single_file(path: Path) -> Iterator[UniProtEntry]:
|
|
168
|
+
ns = _detect_namespace(path)
|
|
169
|
+
opener = gzip.open if path.suffix == ".gz" else open
|
|
170
|
+
|
|
171
|
+
with opener(path, "rb") as f: # type: ignore[arg-type]
|
|
172
|
+
context = etree.iterparse(f, events=("end",), tag=_tag(ns, "entry"))
|
|
173
|
+
|
|
174
|
+
for _event, entry in context:
|
|
175
|
+
yield _parse_entry(ns, entry)
|
|
176
|
+
|
|
177
|
+
# memory cleanup — critical for multi-GB files
|
|
178
|
+
entry.clear()
|
|
179
|
+
while entry.getprevious() is not None:
|
|
180
|
+
del entry.getparent()[0]
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def parse_xml(*paths: str | Path) -> Iterator[UniProtEntry]:
|
|
184
|
+
"""Stream-parse one or more UniProt XML files, yielding UniProtEntry objects.
|
|
185
|
+
|
|
186
|
+
Accepts plain XML or gzip-compressed files (auto-detected from .gz extension).
|
|
187
|
+
Files are processed sequentially. Memory stays bounded regardless of file size.
|
|
188
|
+
"""
|
|
189
|
+
if not paths:
|
|
190
|
+
raise ValueError("At least one path is required")
|
|
191
|
+
|
|
192
|
+
for p in paths:
|
|
193
|
+
yield from _parse_single_file(Path(p))
|
uniprotlib/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: uniprotlib
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python library for parsing UniProt XML data
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.12
|
|
8
|
+
Requires-Dist: lxml>=6.0.2
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# uniprotlib
|
|
12
|
+
|
|
13
|
+
> **Note:** This library was vibe coded with Claude. It works, it's tested, but review accordingly.
|
|
14
|
+
|
|
15
|
+
Python library for parsing UniProt XML files. Handles both single-entry downloads and multi-GB gzip-compressed database dumps with bounded memory usage.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install uniprotlib
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or with [uv](https://docs.astral.sh/uv/):
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
uv add uniprotlib
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from uniprotlib import parse_xml
|
|
33
|
+
|
|
34
|
+
# single file
|
|
35
|
+
for entry in parse_xml("Q9Y261.xml"):
|
|
36
|
+
print(entry.primary_accession, entry.protein_name)
|
|
37
|
+
|
|
38
|
+
# gzipped bulk download
|
|
39
|
+
for entry in parse_xml("uniprot_sprot.xml.gz"):
|
|
40
|
+
print(entry.gene.primary, entry.organism.scientific_name)
|
|
41
|
+
|
|
42
|
+
# multiple files
|
|
43
|
+
for entry in parse_xml("human.xml.gz", "mouse.xml.gz"):
|
|
44
|
+
print(entry.primary_accession)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`parse_xml()` returns an iterator that yields `UniProtEntry` objects. Gzip detection is automatic based on the `.gz` extension. Memory stays bounded regardless of file size.
|
|
48
|
+
|
|
49
|
+
## Parsed fields
|
|
50
|
+
|
|
51
|
+
| Model | Fields |
|
|
52
|
+
|---|---|
|
|
53
|
+
| `UniProtEntry` | primary_accession, accessions, entry_name, dataset, protein_name, gene, organism, sequence, keywords, db_references |
|
|
54
|
+
| `Gene` | primary, synonyms, ordered_locus_names, orf_names |
|
|
55
|
+
| `Organism` | scientific_name, common_name, tax_id, lineage |
|
|
56
|
+
| `Sequence` | value, length, mass, checksum |
|
|
57
|
+
| `DbReference` | type, id, molecule, properties |
|
|
58
|
+
|
|
59
|
+
All model classes are dataclasses with full type annotations and `py.typed` support.
|
|
60
|
+
|
|
61
|
+
## Development
|
|
62
|
+
|
|
63
|
+
Requires Python >= 3.12 and [uv](https://docs.astral.sh/uv/).
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
uv sync
|
|
67
|
+
uv run pytest tests/ -v
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
uniprotlib/__init__.py,sha256=54LDhaTc2DNW8BexoamdJCojpP3WAx3P2q3hsK64tLQ,217
|
|
2
|
+
uniprotlib/models.py,sha256=U5dUj5OT9IMIlF8CLLn_DXGw1KZhP8bfakAz1uQqhp8,849
|
|
3
|
+
uniprotlib/parser.py,sha256=f78uHZ8KkTHSKindS528TIfT_mmJ8J0wNM6l1f0AJmA,5827
|
|
4
|
+
uniprotlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
uniprotlib-0.1.0.dist-info/METADATA,sha256=uSNFnNrd71xvjDCSCZuKrpobuBcKXBbrxxduVtwSd74,1861
|
|
6
|
+
uniprotlib-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
7
|
+
uniprotlib-0.1.0.dist-info/licenses/LICENSE,sha256=7CcQ1452DPAwleU8s7slt_D3J4pn8DTpfljLwk5nQQo,1071
|
|
8
|
+
uniprotlib-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Martin Preusse
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|