gain-demo-annotator 2026.11.0__tar.gz
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.
- gain_demo_annotator-2026.11.0/.gitignore +113 -0
- gain_demo_annotator-2026.11.0/LICENSE +21 -0
- gain_demo_annotator-2026.11.0/PKG-INFO +14 -0
- gain_demo_annotator-2026.11.0/demo_annotator/__init__.py +0 -0
- gain_demo_annotator-2026.11.0/demo_annotator/adapter.py +204 -0
- gain_demo_annotator-2026.11.0/demo_annotator/annotate_length.py +44 -0
- gain_demo_annotator-2026.11.0/demo_annotator/demo_annotate_gm_res.py +73 -0
- gain_demo_annotator-2026.11.0/demo_annotator/demo_annotate_gm_res_adapter.py +133 -0
- gain_demo_annotator-2026.11.0/demo_annotator/demo_annotate_ref_res.py +68 -0
- gain_demo_annotator-2026.11.0/demo_annotator/demo_annotate_ref_res_adapter.py +124 -0
- gain_demo_annotator-2026.11.0/pyproject.toml +71 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# A .claude directory at any depth is ignored (Claude Code started from a
|
|
2
|
+
# member project writes <member>/.claude/settings.local.json). The root one is
|
|
3
|
+
# un-ignored only so that its committed skills/ can be re-included;
|
|
4
|
+
# .claude/worktrees/ and the rest of the root .claude stay ignored.
|
|
5
|
+
**/.claude/
|
|
6
|
+
!/.claude/
|
|
7
|
+
/.claude/*
|
|
8
|
+
!/.claude/skills/
|
|
9
|
+
# verify-gain run output (.claude/skills/verify-gain)
|
|
10
|
+
.verify/
|
|
11
|
+
.specify
|
|
12
|
+
*.pyc
|
|
13
|
+
__pycache__
|
|
14
|
+
.cache
|
|
15
|
+
.DS_Store
|
|
16
|
+
._.DS_Store
|
|
17
|
+
*.log
|
|
18
|
+
*.cache
|
|
19
|
+
*.db
|
|
20
|
+
*.tmp
|
|
21
|
+
*.temp
|
|
22
|
+
*.swp
|
|
23
|
+
*.retry
|
|
24
|
+
*.egg-info
|
|
25
|
+
|
|
26
|
+
.project
|
|
27
|
+
.pydevproject
|
|
28
|
+
.settings
|
|
29
|
+
.ipynb_checkpoints
|
|
30
|
+
.vagrant
|
|
31
|
+
.vscode/
|
|
32
|
+
.idea/
|
|
33
|
+
|
|
34
|
+
.coverage
|
|
35
|
+
.coverage*
|
|
36
|
+
coverage/
|
|
37
|
+
coverage.xml
|
|
38
|
+
htmlcov
|
|
39
|
+
junit.xml
|
|
40
|
+
.pytest_cache
|
|
41
|
+
.mypy_cache
|
|
42
|
+
|
|
43
|
+
logs
|
|
44
|
+
output
|
|
45
|
+
tmp/
|
|
46
|
+
builds
|
|
47
|
+
downloads
|
|
48
|
+
results
|
|
49
|
+
test_data/
|
|
50
|
+
/test_data/
|
|
51
|
+
test-results/
|
|
52
|
+
/test-results
|
|
53
|
+
test_results
|
|
54
|
+
comp-data/
|
|
55
|
+
comp
|
|
56
|
+
cache
|
|
57
|
+
import
|
|
58
|
+
/import
|
|
59
|
+
_build/
|
|
60
|
+
_html
|
|
61
|
+
denovo-cache
|
|
62
|
+
dask-worker-space/
|
|
63
|
+
.grr
|
|
64
|
+
/data/
|
|
65
|
+
/build-env/
|
|
66
|
+
build-env/*
|
|
67
|
+
projects
|
|
68
|
+
/work
|
|
69
|
+
|
|
70
|
+
Jenkinsfile.generated-stages
|
|
71
|
+
Jenkinsfile.generated-stages.all
|
|
72
|
+
/.build-scripts-tmp-sessions
|
|
73
|
+
|
|
74
|
+
monkeytype.sqlite3
|
|
75
|
+
monkeytype_config.py
|
|
76
|
+
|
|
77
|
+
VERSION
|
|
78
|
+
BUILD
|
|
79
|
+
|
|
80
|
+
CLAUDE*.md
|
|
81
|
+
|
|
82
|
+
.task-progress
|
|
83
|
+
|
|
84
|
+
# uv-managed virtual environment
|
|
85
|
+
.venv/
|
|
86
|
+
|
|
87
|
+
# Generated by hatch-vcs at build/install time
|
|
88
|
+
core/gain/_version.py
|
|
89
|
+
|
|
90
|
+
# Root-level build artefact dirs created by the root Jenkinsfile
|
|
91
|
+
# (uv build wheels, rattler-build conda packages). Cleaned and
|
|
92
|
+
# re-created in CI's `Prepare workspace` stage; never checked in.
|
|
93
|
+
/dist/
|
|
94
|
+
/conda/
|
|
95
|
+
/reports/
|
|
96
|
+
web_api/data/db.sqlite3
|
|
97
|
+
|
|
98
|
+
# Linked git worktrees used by Claude Code for isolated
|
|
99
|
+
# feature development (see superpowers `using-git-worktrees`).
|
|
100
|
+
/.worktrees/
|
|
101
|
+
|
|
102
|
+
# Sphinx docs build artefacts. The tarball is consumed by
|
|
103
|
+
# docs/deploy/docs_deploy.yaml; both are produced by
|
|
104
|
+
# docs/build_docs.sh.
|
|
105
|
+
/docs/build/
|
|
106
|
+
/docs/gaindocs-html.tar.gz
|
|
107
|
+
/docs/.tmp/
|
|
108
|
+
|
|
109
|
+
# Running the spliceai tests opens this fixture directory as a GRR, which
|
|
110
|
+
# writes a compressed contents index beside the authored `.CONTENTS.json`.
|
|
111
|
+
# Only this one path: `web_api`'s fixture GRR tracks its `.CONTENTS.*.gz`
|
|
112
|
+
# on purpose, so this must not be a repo-wide pattern.
|
|
113
|
+
/spliceai_annotator/tests/fixtures/.CONTENTS.json.gz
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Cold Spring Harbor Laboratory
|
|
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.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: gain-demo-annotator
|
|
3
|
+
Version: 2026.11.0
|
|
4
|
+
Summary: GAIn External Demo Annotator
|
|
5
|
+
Project-URL: Homepage, https://github.com/IossifovLab/gain
|
|
6
|
+
Author-email: Lubomir Chorbadjiev <lubomir.chorbadjiev@gmail.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Requires-Python: <3.15,>=3.12
|
|
13
|
+
Requires-Dist: fsspec>=2026.2
|
|
14
|
+
Requires-Dist: gain-core
|
|
File without changes
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import csv
|
|
4
|
+
import os
|
|
5
|
+
import select
|
|
6
|
+
import subprocess
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any, TextIO
|
|
10
|
+
|
|
11
|
+
from gain.annotation.annotatable import Annotatable
|
|
12
|
+
from gain.annotation.annotation_config import (
|
|
13
|
+
AnnotationConfigParser,
|
|
14
|
+
AnnotatorInfo,
|
|
15
|
+
)
|
|
16
|
+
from gain.annotation.annotation_pipeline import (
|
|
17
|
+
AnnotationPipeline,
|
|
18
|
+
Annotator,
|
|
19
|
+
AttributeSpec,
|
|
20
|
+
)
|
|
21
|
+
from gain.annotation.annotator_base import AnnotatedValues
|
|
22
|
+
from gain.annotation.docker_annotator import DockerAnnotator
|
|
23
|
+
|
|
24
|
+
# ruff: file-ignore[start-process-with-partial-path]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class DemoAnnotatorAdapter(DockerAnnotator):
|
|
28
|
+
"""Annotation pipeline adapter for dummy_annotate using tempfiles."""
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self, pipeline: AnnotationPipeline | None,
|
|
32
|
+
info: AnnotatorInfo,
|
|
33
|
+
):
|
|
34
|
+
if not info.attributes:
|
|
35
|
+
info.attributes = AnnotationConfigParser.parse_raw_attributes([
|
|
36
|
+
"annotatable_length",
|
|
37
|
+
])
|
|
38
|
+
super().__init__(
|
|
39
|
+
pipeline, info,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
def get_attribute_specs(self) -> dict[str, AttributeSpec]:
|
|
43
|
+
return {
|
|
44
|
+
"annotatable_length": AttributeSpec(
|
|
45
|
+
source="annotatable_length",
|
|
46
|
+
value_type="int",
|
|
47
|
+
description="Positional length of the annotatable",
|
|
48
|
+
),
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
def _do_annotate(
|
|
52
|
+
self,
|
|
53
|
+
annotatable: Annotatable | None,
|
|
54
|
+
context: dict[str, Any],
|
|
55
|
+
) -> AnnotatedValues:
|
|
56
|
+
raise NotImplementedError(
|
|
57
|
+
"External annotator supports only batch mode",
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def annotate(
|
|
61
|
+
self,
|
|
62
|
+
annotatable: Annotatable | None,
|
|
63
|
+
context: dict[str, Any],
|
|
64
|
+
) -> dict[str, Any]:
|
|
65
|
+
raise NotImplementedError(
|
|
66
|
+
"External annotator supports only batch mode",
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def prepare_input(
|
|
70
|
+
self, file: TextIO, annotatables: Sequence[Annotatable | None],
|
|
71
|
+
) -> None:
|
|
72
|
+
writer = csv.writer(file, delimiter="\t")
|
|
73
|
+
for annotatable in annotatables:
|
|
74
|
+
writer.writerow([repr(annotatable)])
|
|
75
|
+
file.flush()
|
|
76
|
+
|
|
77
|
+
def read_output(
|
|
78
|
+
self, file: TextIO, contexts: list[dict[str, Any]],
|
|
79
|
+
) -> None:
|
|
80
|
+
"""Read and return subprocess output contents."""
|
|
81
|
+
reader = csv.reader(
|
|
82
|
+
file, delimiter="\t",
|
|
83
|
+
)
|
|
84
|
+
for idx, row in enumerate(reader):
|
|
85
|
+
contexts[idx]["annotatable_length"] = int(row[-1])
|
|
86
|
+
|
|
87
|
+
def _do_batch_annotate(
|
|
88
|
+
self,
|
|
89
|
+
annotatables: Sequence[Annotatable | None],
|
|
90
|
+
contexts: list[dict[str, Any]],
|
|
91
|
+
batch_work_dir: str | None = None,
|
|
92
|
+
) -> list[AnnotatedValues]:
|
|
93
|
+
if batch_work_dir is None:
|
|
94
|
+
work_dir = self.work_dir
|
|
95
|
+
else:
|
|
96
|
+
work_dir = self.work_dir / batch_work_dir
|
|
97
|
+
os.makedirs(work_dir, exist_ok=True)
|
|
98
|
+
|
|
99
|
+
with (work_dir / "input.tsv").open("w+t") as input_file:
|
|
100
|
+
self.prepare_input(input_file, annotatables)
|
|
101
|
+
self.run(
|
|
102
|
+
input_filename="input.tsv",
|
|
103
|
+
output_filename="output.tsv",
|
|
104
|
+
work_dir=str(work_dir.absolute()),
|
|
105
|
+
)
|
|
106
|
+
subprocess.run(
|
|
107
|
+
["annotate_length", input_file.name, "output.tsv"],
|
|
108
|
+
check=True,
|
|
109
|
+
)
|
|
110
|
+
with (work_dir / "output.tsv").open("r") as out_file:
|
|
111
|
+
self.read_output(out_file, contexts)
|
|
112
|
+
return [
|
|
113
|
+
self._from_sources(context)
|
|
114
|
+
for context in contexts
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
def run(self, **kwargs: Any) -> None:
|
|
118
|
+
args = [
|
|
119
|
+
"annotate_length",
|
|
120
|
+
str(Path("/work", kwargs["input_filename"])),
|
|
121
|
+
str(Path("/work", kwargs["output_filename"])),
|
|
122
|
+
]
|
|
123
|
+
self.client.containers.run(
|
|
124
|
+
"gpf_docker_test", args,
|
|
125
|
+
volumes={
|
|
126
|
+
kwargs["work_dir"]: {"bind": "/work", "mode": "rw"},
|
|
127
|
+
},
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# The base raises NotImplementedError: this annotator is batch-only.
|
|
132
|
+
class DemoAnnotatorStreamAdapter( # pylint: disable=abstract-method
|
|
133
|
+
DemoAnnotatorAdapter,
|
|
134
|
+
):
|
|
135
|
+
"""Annotation pipeline adapter for annotate_length using streams."""
|
|
136
|
+
|
|
137
|
+
def _do_batch_annotate(
|
|
138
|
+
self,
|
|
139
|
+
annotatables: Sequence[Annotatable | None],
|
|
140
|
+
contexts: list[dict[str, Any]], # ruff: ignore[unused-method-argument]
|
|
141
|
+
batch_work_dir: str | None = None, # ruff: ignore[unused-method-argument]
|
|
142
|
+
) -> list[AnnotatedValues]:
|
|
143
|
+
results: list[int] = []
|
|
144
|
+
with subprocess.Popen(
|
|
145
|
+
["annotate_length"],
|
|
146
|
+
stdin=subprocess.PIPE,
|
|
147
|
+
stdout=subprocess.PIPE,
|
|
148
|
+
text=True,
|
|
149
|
+
) as proc:
|
|
150
|
+
writer = csv.writer(
|
|
151
|
+
proc.stdin, delimiter="\t") # type: ignore
|
|
152
|
+
reader = csv.reader(
|
|
153
|
+
proc.stdout, delimiter="\t") # type: ignore
|
|
154
|
+
poll = select.poll()
|
|
155
|
+
poll.register(
|
|
156
|
+
proc.stdin, select.POLLOUT) # type: ignore
|
|
157
|
+
poll.register(
|
|
158
|
+
proc.stdout, select.POLLIN | select.POLLPRI) # type: ignore
|
|
159
|
+
annotatable_idx = 0
|
|
160
|
+
done = False
|
|
161
|
+
|
|
162
|
+
while True:
|
|
163
|
+
if done:
|
|
164
|
+
break
|
|
165
|
+
fd_states = poll.poll(500)
|
|
166
|
+
for _fd, state in fd_states:
|
|
167
|
+
if state & select.POLLOUT:
|
|
168
|
+
annotatable = annotatables[annotatable_idx]
|
|
169
|
+
writer.writerow([repr(annotatable)])
|
|
170
|
+
annotatable_idx += 1
|
|
171
|
+
if annotatable_idx == len(annotatables):
|
|
172
|
+
poll.unregister(proc.stdin) # type: ignore
|
|
173
|
+
proc.stdin.close() # type: ignore
|
|
174
|
+
|
|
175
|
+
elif state & select.POLLIN or state & select.POLLPRI:
|
|
176
|
+
row = next(reader)
|
|
177
|
+
results.append(int(row[-1]))
|
|
178
|
+
if len(results) == len(annotatables):
|
|
179
|
+
poll.unregister(proc.stdout) # type: ignore
|
|
180
|
+
done = True
|
|
181
|
+
elif state & select.POLLHUP:
|
|
182
|
+
results.extend(int(row[-1]) for row in reader)
|
|
183
|
+
poll.unregister(proc.stdout) # type: ignore
|
|
184
|
+
done = True
|
|
185
|
+
proc.wait()
|
|
186
|
+
|
|
187
|
+
return [
|
|
188
|
+
self._every(value)
|
|
189
|
+
for value in results
|
|
190
|
+
]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def build_demo_external_annotator_adapter(
|
|
194
|
+
pipeline: AnnotationPipeline,
|
|
195
|
+
info: AnnotatorInfo,
|
|
196
|
+
) -> Annotator:
|
|
197
|
+
return DemoAnnotatorAdapter(pipeline, info)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def build_demo_external_annotator_stream_adapter(
|
|
201
|
+
pipeline: AnnotationPipeline,
|
|
202
|
+
info: AnnotatorInfo,
|
|
203
|
+
) -> Annotator:
|
|
204
|
+
return DemoAnnotatorStreamAdapter(pipeline, info)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import csv
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from gain.annotation.annotatable import Annotatable
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def annotate_length_cli(raw_args: list[str] | None = None) -> None:
|
|
10
|
+
"""Dummy tool which outputs annotatable length."""
|
|
11
|
+
if raw_args is None:
|
|
12
|
+
raw_args = sys.argv[1:]
|
|
13
|
+
parser = argparse.ArgumentParser(
|
|
14
|
+
description="Annotate columns",
|
|
15
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
16
|
+
)
|
|
17
|
+
parser.add_argument(
|
|
18
|
+
"input", default="-", nargs="?",
|
|
19
|
+
help="the input file")
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"output", default="-", nargs="?",
|
|
22
|
+
help="the output file")
|
|
23
|
+
|
|
24
|
+
args = parser.parse_args(raw_args)
|
|
25
|
+
|
|
26
|
+
# pylint: disable=consider-using-with
|
|
27
|
+
infile = sys.stdin if args.input == "-" else \
|
|
28
|
+
Path(args.input).open("r") # ruff: ignore[open-file-with-context-handler]
|
|
29
|
+
outfile = sys.stdout if args.output == "-" else \
|
|
30
|
+
Path(args.output).open("w") # ruff: ignore[open-file-with-context-handler]
|
|
31
|
+
|
|
32
|
+
with infile, outfile:
|
|
33
|
+
reader = csv.reader(infile, delimiter="\t")
|
|
34
|
+
writer = csv.writer(outfile, delimiter="\t")
|
|
35
|
+
for row in reader:
|
|
36
|
+
if len(row) == 0:
|
|
37
|
+
continue
|
|
38
|
+
annotatable = Annotatable.from_string(row[0])
|
|
39
|
+
length = annotatable.pos_end - annotatable.pos
|
|
40
|
+
writer.writerow([*row, length])
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__":
|
|
44
|
+
annotate_length_cli(sys.argv[1:])
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import csv
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from gain.annotation.annotatable import Annotatable
|
|
7
|
+
from gain.genomic_resources.gene_models import (
|
|
8
|
+
build_gene_models_from_file,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def annotate_genes_cli(raw_args: list[str] | None = None) -> None:
|
|
13
|
+
"""Dummy tool which outputs genes for an annotatable."""
|
|
14
|
+
if raw_args is None:
|
|
15
|
+
raw_args = sys.argv[1:]
|
|
16
|
+
parser = argparse.ArgumentParser(
|
|
17
|
+
description="Annotate genes",
|
|
18
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"input", nargs="?",
|
|
22
|
+
help="the input file",
|
|
23
|
+
)
|
|
24
|
+
parser.add_argument(
|
|
25
|
+
"gene_models", nargs="?",
|
|
26
|
+
help="Path to gene models file",
|
|
27
|
+
)
|
|
28
|
+
parser.add_argument(
|
|
29
|
+
"output", nargs="?",
|
|
30
|
+
help="the output file",
|
|
31
|
+
)
|
|
32
|
+
parser.add_argument(
|
|
33
|
+
"--format", type=str,
|
|
34
|
+
default=None,
|
|
35
|
+
help="Gene models file format",
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
args = parser.parse_args(raw_args)
|
|
39
|
+
|
|
40
|
+
# pylint: disable=consider-using-with
|
|
41
|
+
infile = sys.stdin if args.input == "-" else \
|
|
42
|
+
Path(args.input).open("r") # ruff: ignore[open-file-with-context-handler]
|
|
43
|
+
outfile = sys.stdout if args.output == "-" else \
|
|
44
|
+
Path(args.output).open("w") # ruff: ignore[open-file-with-context-handler]
|
|
45
|
+
|
|
46
|
+
if args.gene_models == "-":
|
|
47
|
+
raise ValueError("Gene models cannot be streamed!")
|
|
48
|
+
|
|
49
|
+
gene_models = build_gene_models_from_file(
|
|
50
|
+
args.gene_models, file_format=args.format,
|
|
51
|
+
)
|
|
52
|
+
gene_models.load()
|
|
53
|
+
|
|
54
|
+
with infile, outfile:
|
|
55
|
+
reader = csv.reader(infile, delimiter="\t")
|
|
56
|
+
writer = csv.writer(outfile, delimiter="\t")
|
|
57
|
+
for row in reader:
|
|
58
|
+
if len(row) == 0:
|
|
59
|
+
continue
|
|
60
|
+
annotatable = Annotatable.from_string(row[0])
|
|
61
|
+
gene_syms = {
|
|
62
|
+
tr.gene for tr in
|
|
63
|
+
gene_models.gene_models_by_location(
|
|
64
|
+
annotatable.chrom,
|
|
65
|
+
annotatable.pos,
|
|
66
|
+
annotatable.pos_end,
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
writer.writerow([*row, ",".join(gene_syms)])
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
if __name__ == "__main__":
|
|
73
|
+
annotate_genes_cli(sys.argv[1:])
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import csv
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from typing import Any, TextIO
|
|
8
|
+
|
|
9
|
+
import fsspec
|
|
10
|
+
from gain.annotation.annotatable import Annotatable
|
|
11
|
+
from gain.annotation.annotation_pipeline import (
|
|
12
|
+
AnnotationPipeline,
|
|
13
|
+
Annotator,
|
|
14
|
+
AnnotatorInfo,
|
|
15
|
+
AttributeSpec,
|
|
16
|
+
)
|
|
17
|
+
from gain.annotation.annotator_base import AnnotatedValues, AnnotatorBase
|
|
18
|
+
from gain.annotation.utils import configured_resource_id
|
|
19
|
+
from gain.genomic_resources.cached_repository import GenomicResourceCachedRepo
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class DemoAnnotateGeneModelsAdapter(AnnotatorBase):
|
|
23
|
+
"""Annotation pipeline adapter for dummy_annotate using tempfiles."""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self, pipeline: AnnotationPipeline,
|
|
27
|
+
info: AnnotatorInfo,
|
|
28
|
+
):
|
|
29
|
+
super().__init__(pipeline, info)
|
|
30
|
+
self.cache_repo = GenomicResourceCachedRepo(
|
|
31
|
+
pipeline.repository, str(self.work_dir / "grr_cache"),
|
|
32
|
+
)
|
|
33
|
+
gene_models_id = configured_resource_id(info, "gene_models")
|
|
34
|
+
if gene_models_id is None:
|
|
35
|
+
raise ValueError(
|
|
36
|
+
f"The {info} annotator needs a 'gene_models' parameter.",
|
|
37
|
+
)
|
|
38
|
+
self.gene_models_resource = self.cache_repo.get_resource(
|
|
39
|
+
gene_models_id,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
self.gene_model_filename = \
|
|
43
|
+
self.gene_models_resource.get_config()["filename"]
|
|
44
|
+
self.gene_model_format = \
|
|
45
|
+
self.gene_models_resource.get_config()["format"]
|
|
46
|
+
|
|
47
|
+
def get_attribute_specs(self) -> dict[str, AttributeSpec]:
|
|
48
|
+
return {
|
|
49
|
+
"gene_symbols": AttributeSpec(
|
|
50
|
+
source="gene_symbols",
|
|
51
|
+
value_type="object",
|
|
52
|
+
description="Gene symbols overlapping with the "
|
|
53
|
+
"annotatable",
|
|
54
|
+
internal_default=False,
|
|
55
|
+
is_default=True,
|
|
56
|
+
),
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
def _do_annotate(
|
|
60
|
+
self,
|
|
61
|
+
annotatable: Annotatable | None,
|
|
62
|
+
context: dict[str, Any],
|
|
63
|
+
) -> AnnotatedValues:
|
|
64
|
+
raise NotImplementedError(
|
|
65
|
+
"External annotator supports only batch mode",
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def annotate(
|
|
69
|
+
self,
|
|
70
|
+
annotatable: Annotatable | None,
|
|
71
|
+
context: dict[str, Any],
|
|
72
|
+
) -> dict[str, Any]:
|
|
73
|
+
raise NotImplementedError(
|
|
74
|
+
"External annotator supports only batch mode",
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def prepare_input(
|
|
78
|
+
self, file: TextIO, annotatables: Sequence[Annotatable | None],
|
|
79
|
+
) -> None:
|
|
80
|
+
writer = csv.writer(file, delimiter="\t")
|
|
81
|
+
for annotatable in annotatables:
|
|
82
|
+
writer.writerow([repr(annotatable)])
|
|
83
|
+
file.flush()
|
|
84
|
+
|
|
85
|
+
def read_output(
|
|
86
|
+
self, file: TextIO, contexts: list[dict[str, Any]],
|
|
87
|
+
) -> None:
|
|
88
|
+
"""Read and return subprocess output contents."""
|
|
89
|
+
reader = csv.reader(
|
|
90
|
+
file, delimiter="\t",
|
|
91
|
+
)
|
|
92
|
+
for idx, row in enumerate(reader):
|
|
93
|
+
contexts[idx]["gene_symbols"] = row[-1]
|
|
94
|
+
|
|
95
|
+
def _do_batch_annotate(
|
|
96
|
+
self,
|
|
97
|
+
annotatables: Sequence[Annotatable | None],
|
|
98
|
+
contexts: list[dict[str, Any]],
|
|
99
|
+
batch_work_dir: str | None = None,
|
|
100
|
+
) -> list[AnnotatedValues]:
|
|
101
|
+
if batch_work_dir is None:
|
|
102
|
+
work_dir = self.work_dir
|
|
103
|
+
else:
|
|
104
|
+
work_dir = self.work_dir / batch_work_dir
|
|
105
|
+
os.makedirs(work_dir, exist_ok=True)
|
|
106
|
+
|
|
107
|
+
gene_model_filepath = fsspec.url_to_fs(
|
|
108
|
+
self.gene_models_resource.get_file_url(self.gene_model_filename),
|
|
109
|
+
)[1]
|
|
110
|
+
|
|
111
|
+
with (work_dir / "input.tsv").open("w+t") as input_file, \
|
|
112
|
+
(work_dir / "output.tsv").open("w+t") as out_file:
|
|
113
|
+
self.prepare_input(input_file, annotatables)
|
|
114
|
+
args = [
|
|
115
|
+
"demo_annotate_gm_res", input_file.name,
|
|
116
|
+
gene_model_filepath, out_file.name,
|
|
117
|
+
]
|
|
118
|
+
if self.gene_model_format:
|
|
119
|
+
args += ["--format", self.gene_model_format]
|
|
120
|
+
subprocess.run(args, check=True)
|
|
121
|
+
out_file.flush()
|
|
122
|
+
self.read_output(out_file, contexts)
|
|
123
|
+
return [
|
|
124
|
+
self._from_sources(context)
|
|
125
|
+
for context in contexts
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def build_demo_external_gene_annotator_adapter(
|
|
130
|
+
pipeline: AnnotationPipeline,
|
|
131
|
+
info: AnnotatorInfo,
|
|
132
|
+
) -> Annotator:
|
|
133
|
+
return DemoAnnotateGeneModelsAdapter(pipeline, info)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import csv
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from gain.annotation.annotatable import Annotatable
|
|
7
|
+
from gain.genomic_resources.reference_genome import (
|
|
8
|
+
build_reference_genome_from_file,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def annotate_genome_cli(raw_args: list[str] | None = None) -> None:
|
|
13
|
+
"""Dummy tool which outputs genes for an annotatable."""
|
|
14
|
+
if raw_args is None:
|
|
15
|
+
raw_args = sys.argv[1:]
|
|
16
|
+
parser = argparse.ArgumentParser(
|
|
17
|
+
description="Annotate genes",
|
|
18
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"input", nargs="?",
|
|
22
|
+
help="the input file",
|
|
23
|
+
)
|
|
24
|
+
parser.add_argument(
|
|
25
|
+
"genome", nargs="?",
|
|
26
|
+
help="Path to genome file",
|
|
27
|
+
)
|
|
28
|
+
parser.add_argument(
|
|
29
|
+
"output", nargs="?",
|
|
30
|
+
help="the output file",
|
|
31
|
+
)
|
|
32
|
+
parser.add_argument(
|
|
33
|
+
"--format", type=str,
|
|
34
|
+
default=None,
|
|
35
|
+
help="Gene models file format",
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
args = parser.parse_args(raw_args)
|
|
39
|
+
|
|
40
|
+
# pylint: disable=consider-using-with
|
|
41
|
+
infile = sys.stdin if args.input == "-" else \
|
|
42
|
+
Path(args.input).open("r") # ruff: ignore[open-file-with-context-handler]
|
|
43
|
+
outfile = sys.stdout if args.output == "-" else \
|
|
44
|
+
Path(args.output).open("w") # ruff: ignore[open-file-with-context-handler]
|
|
45
|
+
|
|
46
|
+
if args.genome == "-":
|
|
47
|
+
raise ValueError("Reference genome can't be streamed!")
|
|
48
|
+
|
|
49
|
+
genome = build_reference_genome_from_file(
|
|
50
|
+
args.genome,
|
|
51
|
+
)
|
|
52
|
+
genome.open()
|
|
53
|
+
|
|
54
|
+
with infile, outfile, genome:
|
|
55
|
+
reader = csv.reader(infile, delimiter="\t")
|
|
56
|
+
writer = csv.writer(outfile, delimiter="\t")
|
|
57
|
+
for row in reader:
|
|
58
|
+
if len(row) == 0:
|
|
59
|
+
continue
|
|
60
|
+
annotatable = Annotatable.from_string(row[0])
|
|
61
|
+
sequence = list(genome.fetch(
|
|
62
|
+
annotatable.chrom, annotatable.pos, annotatable.pos_end,
|
|
63
|
+
))
|
|
64
|
+
writer.writerow([*row, sequence])
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
annotate_genome_cli(sys.argv[1:])
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import csv
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from typing import Any, TextIO
|
|
8
|
+
|
|
9
|
+
import fsspec
|
|
10
|
+
from gain.annotation.annotatable import Annotatable
|
|
11
|
+
from gain.annotation.annotation_pipeline import (
|
|
12
|
+
AnnotationPipeline,
|
|
13
|
+
Annotator,
|
|
14
|
+
AnnotatorInfo,
|
|
15
|
+
AttributeSpec,
|
|
16
|
+
)
|
|
17
|
+
from gain.annotation.annotator_base import AnnotatedValues, AnnotatorBase
|
|
18
|
+
from gain.annotation.utils import configured_resource_id
|
|
19
|
+
from gain.genomic_resources.cached_repository import GenomicResourceCachedRepo
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class DemoAnnotateGenomeAdapter(AnnotatorBase):
|
|
23
|
+
"""Annotation pipeline adapter for dummy_annotate using tempfiles."""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self, pipeline: AnnotationPipeline,
|
|
27
|
+
info: AnnotatorInfo,
|
|
28
|
+
):
|
|
29
|
+
super().__init__(pipeline, info)
|
|
30
|
+
self.cache_repo = GenomicResourceCachedRepo(
|
|
31
|
+
pipeline.repository, str(self.work_dir / "grr_cache"),
|
|
32
|
+
)
|
|
33
|
+
genome_id = configured_resource_id(info, "reference_genome")
|
|
34
|
+
if genome_id is None:
|
|
35
|
+
raise ValueError(
|
|
36
|
+
f"The {info} annotator needs a 'reference_genome' parameter.",
|
|
37
|
+
)
|
|
38
|
+
self.genome_resource = self.cache_repo.get_resource(genome_id)
|
|
39
|
+
|
|
40
|
+
self.genome_filename = \
|
|
41
|
+
self.genome_resource.get_config()["filename"]
|
|
42
|
+
|
|
43
|
+
def get_attribute_specs(self) -> dict[str, AttributeSpec]:
|
|
44
|
+
return {
|
|
45
|
+
"ref_sequence": AttributeSpec(
|
|
46
|
+
source="ref_sequence",
|
|
47
|
+
value_type="object",
|
|
48
|
+
description="Sequence in the reference genome",
|
|
49
|
+
internal_default=False,
|
|
50
|
+
is_default=True,
|
|
51
|
+
),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
def _do_annotate(
|
|
55
|
+
self, annotatable: Annotatable | None,
|
|
56
|
+
context: dict[str, Any],
|
|
57
|
+
) -> AnnotatedValues:
|
|
58
|
+
raise NotImplementedError(
|
|
59
|
+
"External annotator supports only batch mode",
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def annotate(
|
|
63
|
+
self, annotatable: Annotatable | None,
|
|
64
|
+
context: dict[str, Any],
|
|
65
|
+
) -> dict[str, Any]:
|
|
66
|
+
raise NotImplementedError(
|
|
67
|
+
"External annotator supports only batch mode",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
def prepare_input(
|
|
71
|
+
self, file: TextIO, annotatables: Sequence[Annotatable | None],
|
|
72
|
+
) -> None:
|
|
73
|
+
writer = csv.writer(file, delimiter="\t")
|
|
74
|
+
for annotatable in annotatables:
|
|
75
|
+
writer.writerow([repr(annotatable)])
|
|
76
|
+
file.flush()
|
|
77
|
+
|
|
78
|
+
def read_output(
|
|
79
|
+
self, file: TextIO, contexts: list[dict[str, Any]],
|
|
80
|
+
) -> None:
|
|
81
|
+
"""Read and return subprocess output contents."""
|
|
82
|
+
reader = csv.reader(
|
|
83
|
+
file, delimiter="\t",
|
|
84
|
+
)
|
|
85
|
+
for idx, row in enumerate(reader):
|
|
86
|
+
contexts[idx]["ref_sequence"] = row[-1]
|
|
87
|
+
|
|
88
|
+
def _do_batch_annotate(
|
|
89
|
+
self, annotatables: Sequence[Annotatable | None],
|
|
90
|
+
contexts: list[dict[str, Any]],
|
|
91
|
+
batch_work_dir: str | None = None,
|
|
92
|
+
) -> list[AnnotatedValues]:
|
|
93
|
+
if batch_work_dir is None:
|
|
94
|
+
work_dir = self.work_dir
|
|
95
|
+
else:
|
|
96
|
+
work_dir = self.work_dir / batch_work_dir
|
|
97
|
+
os.makedirs(work_dir, exist_ok=True)
|
|
98
|
+
|
|
99
|
+
genome_filepath = fsspec.url_to_fs(
|
|
100
|
+
self.genome_resource.get_file_url(self.genome_filename),
|
|
101
|
+
)[1]
|
|
102
|
+
self.genome_resource.get_file_url(f"{self.genome_filename}.fai")
|
|
103
|
+
|
|
104
|
+
with (work_dir / "input.tsv").open("w+t") as input_file, \
|
|
105
|
+
(work_dir / "output.tsv").open("w+t") as out_file:
|
|
106
|
+
self.prepare_input(input_file, annotatables)
|
|
107
|
+
args = [
|
|
108
|
+
"demo_annotate_ref_res", input_file.name,
|
|
109
|
+
genome_filepath, out_file.name,
|
|
110
|
+
]
|
|
111
|
+
subprocess.run(args, check=True)
|
|
112
|
+
out_file.flush()
|
|
113
|
+
self.read_output(out_file, contexts)
|
|
114
|
+
return [
|
|
115
|
+
self._from_sources(context)
|
|
116
|
+
for context in contexts
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def build_demo_external_genome_annotator_adapter(
|
|
121
|
+
pipeline: AnnotationPipeline,
|
|
122
|
+
info: AnnotatorInfo,
|
|
123
|
+
) -> Annotator:
|
|
124
|
+
return DemoAnnotateGenomeAdapter(pipeline, info)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "gain-demo-annotator"
|
|
7
|
+
description = "GAIn External Demo Annotator"
|
|
8
|
+
requires-python = "<3.15,>=3.12"
|
|
9
|
+
license = { text = "MIT" }
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "Lubomir Chorbadjiev", email = "lubomir.chorbadjiev@gmail.com" },
|
|
12
|
+
]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Programming Language :: Python :: 3.12",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
dependencies = [
|
|
20
|
+
"gain-core",
|
|
21
|
+
"fsspec>=2026.2",
|
|
22
|
+
]
|
|
23
|
+
dynamic = ["version"]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/IossifovLab/gain"
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
annotate_length = "demo_annotator.annotate_length:annotate_length_cli"
|
|
30
|
+
demo_annotate_gm_res = "demo_annotator.demo_annotate_gm_res:annotate_genes_cli"
|
|
31
|
+
demo_annotate_ref_res = "demo_annotator.demo_annotate_ref_res:annotate_genome_cli"
|
|
32
|
+
|
|
33
|
+
[project.entry-points."gain.annotation.annotators"]
|
|
34
|
+
external_demo_annotator = "demo_annotator.adapter:build_demo_external_annotator_adapter"
|
|
35
|
+
external_demo_stream_annotator = "demo_annotator.adapter:build_demo_external_annotator_stream_adapter"
|
|
36
|
+
external_demo_gene_annotator = "demo_annotator.demo_annotate_gm_res_adapter:build_demo_external_gene_annotator_adapter"
|
|
37
|
+
external_demo_genome_annotator = "demo_annotator.demo_annotate_ref_res_adapter:build_demo_external_genome_annotator_adapter"
|
|
38
|
+
|
|
39
|
+
[dependency-groups]
|
|
40
|
+
dev = [
|
|
41
|
+
"mypy==1.15.0",
|
|
42
|
+
"pylint",
|
|
43
|
+
"pylint-junit",
|
|
44
|
+
"pytest",
|
|
45
|
+
"pytest-cov",
|
|
46
|
+
"pytest-mock",
|
|
47
|
+
"pytest-xdist",
|
|
48
|
+
"ruff==0.16.5",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
[tool.hatch.version]
|
|
52
|
+
source = "vcs"
|
|
53
|
+
fallback-version = "0.0.0.dev0"
|
|
54
|
+
|
|
55
|
+
[tool.hatch.version.raw-options]
|
|
56
|
+
root = ".."
|
|
57
|
+
version_scheme = "no-guess-dev"
|
|
58
|
+
|
|
59
|
+
[tool.hatch.build.targets.wheel]
|
|
60
|
+
packages = ["demo_annotator"]
|
|
61
|
+
exclude = ["demo_annotator/tests"]
|
|
62
|
+
|
|
63
|
+
[tool.hatch.build.targets.sdist]
|
|
64
|
+
include = ["/demo_annotator"]
|
|
65
|
+
exclude = ["demo_annotator/tests"]
|
|
66
|
+
|
|
67
|
+
# The repository LICENSE sits one level up. The sdist copies it to its
|
|
68
|
+
# top level as a regular file, and the wheel, which uv builds from the
|
|
69
|
+
# unpacked sdist, picks it up there as a PEP 639 license file (#1961).
|
|
70
|
+
[tool.hatch.build.targets.sdist.force-include]
|
|
71
|
+
"../LICENSE" = "LICENSE"
|