altamisa 1.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.
altamisa/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .version import __version__ # noqa
@@ -0,0 +1 @@
1
+ # -*- coding: utf-8 -*-
@@ -0,0 +1,133 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Conversion of ISA-Tab to dot."""
3
+
4
+ from contextlib import ExitStack
5
+ import json
6
+ import os
7
+ import sys
8
+ from typing import Annotated
9
+
10
+ import attrs
11
+ import typer
12
+
13
+ from altamisa.isatab import AssayReader, InvestigationReader, StudyReader
14
+
15
+ #: Typer application instance.
16
+ app = typer.Typer()
17
+
18
+
19
+ @attrs.define
20
+ class Arguments:
21
+ investigation_file: str
22
+ output_file: str
23
+
24
+
25
+ def print_dot(
26
+ obj,
27
+ outf,
28
+ indent=" ",
29
+ mat_shape="box",
30
+ mat_color="black",
31
+ proc_shape="ellipse",
32
+ proc_color="blue",
33
+ ):
34
+ print(indent + "/* materials */", file=outf)
35
+ for name, mat in obj.materials.items():
36
+ label = json.dumps(f"{mat.type}:\n{mat.name if mat.name else '-'}\n({name})")
37
+ print(
38
+ f"{indent}{json.dumps(name)} [label={label},shape={mat_shape},color={mat_color},fontcolor={mat_color}]",
39
+ file=outf,
40
+ )
41
+ print(indent + "/* processes */", file=outf)
42
+ for name, proc in obj.processes.items():
43
+ label = json.dumps(
44
+ "Process:\n{}\n{}\n({})".format(
45
+ proc.protocol_ref if proc.protocol_ref else "-",
46
+ proc.name if proc.name else "-",
47
+ name,
48
+ )
49
+ )
50
+ print(
51
+ "{}{} [label={},shape={},color={},fontcolor={}]".format(
52
+ indent, json.dumps(name), label, proc_shape, proc_color, proc_color
53
+ ),
54
+ file=outf,
55
+ )
56
+ print(indent + "/* arcs */", file=outf)
57
+ for arc in obj.arcs:
58
+ print(f"{indent}{json.dumps(arc.tail)} -> {json.dumps(arc.head)};", file=outf)
59
+
60
+
61
+ def run(args: Arguments):
62
+ with open(args.investigation_file, "rt") as inputf:
63
+ investigation = InvestigationReader.from_stream(inputf).read()
64
+
65
+ path = os.path.dirname(args.investigation_file)
66
+
67
+ with ExitStack() as stack:
68
+ if args.output_file == "-":
69
+ output_file = sys.stdout
70
+ else:
71
+ output_file = stack.enter_context(open(args.output_file, "wt"))
72
+
73
+ print("digraph investigation {", file=output_file)
74
+ print(' rankdir = "LR";', file=output_file)
75
+
76
+ for s, study_info in enumerate(investigation.studies):
77
+ if not study_info.info.path:
78
+ print(f" /* no file for study {s + 1} */", file=output_file)
79
+ continue
80
+ with open(os.path.join(path, study_info.info.path), "rt") as inputf:
81
+ study = StudyReader.from_stream(f"S{s + 1}", inputf).read()
82
+ print(f" /* study {study_info.info.path} */", file=output_file)
83
+ print(f" subgraph clusterStudy{s} {{", file=output_file)
84
+ print(f' label = "Study: {study_info.info.path}"', file=output_file)
85
+ print_dot(study, output_file)
86
+ print(" }", file=output_file)
87
+
88
+ for a, assay_info in enumerate(study_info.assays):
89
+ if not assay_info.path:
90
+ print(f" /* no file for assay {a + 1} */", file=output_file)
91
+ continue
92
+ with open(os.path.join(path, assay_info.path), "rt") as inputf:
93
+ assay = AssayReader.from_stream(f"S{s + 1}", f"A{a + 1}", inputf).read()
94
+ print(f" /* assay {assay_info.path} */", file=output_file)
95
+ print(f" subgraph clusterAssayS{s}A{a} {{", file=output_file)
96
+ print(f' label = "Assay: {assay_info.path}"', file=output_file)
97
+ print_dot(assay, output_file)
98
+ print(" }", file=output_file)
99
+
100
+ print("}", file=output_file)
101
+
102
+
103
+ @app.command()
104
+ def main(
105
+ investigation_file: Annotated[
106
+ str,
107
+ typer.Option(
108
+ "--investigation-file",
109
+ "-i",
110
+ help="Path to input investigation file",
111
+ ),
112
+ ],
113
+ output_file: Annotated[
114
+ str,
115
+ typer.Option(
116
+ "--output-file",
117
+ "-o",
118
+ help="Path to output file, stdout ('-') by default",
119
+ ),
120
+ ] = "-",
121
+ ):
122
+ """Main entry point."""
123
+ # Convert to `Arguments` object.
124
+ args = Arguments(
125
+ investigation_file=investigation_file,
126
+ output_file=output_file,
127
+ )
128
+ # Actually run.
129
+ run(args)
130
+
131
+
132
+ if __name__ == "__main__": # pragma: no cover
133
+ typer.run(main)
@@ -0,0 +1,207 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Read from ISA-Tab and directly write to ISA-Tab."""
3
+
4
+ from contextlib import ExitStack
5
+ import os
6
+ import sys
7
+ import typing
8
+ from typing import Annotated, Dict, Optional, Tuple
9
+ import warnings
10
+
11
+ import attrs
12
+ import typer
13
+
14
+ from altamisa.exceptions import IsaException
15
+ from altamisa.isatab import (
16
+ AssayReader,
17
+ AssayValidator,
18
+ AssayWriter,
19
+ InvestigationReader,
20
+ InvestigationValidator,
21
+ InvestigationWriter,
22
+ StudyReader,
23
+ StudyValidator,
24
+ StudyWriter,
25
+ )
26
+ from altamisa.isatab.models import Assay, InvestigationInfo, Study
27
+
28
+ #: Typer application instance.
29
+ app = typer.Typer()
30
+
31
+
32
+ @attrs.define
33
+ class Arguments:
34
+ input_investigation_file: str
35
+ output_investigation_file: str
36
+ quotes: Optional[str]
37
+ warnings: bool
38
+
39
+
40
+ def run(args: Arguments):
41
+ # Collect warnings
42
+ with warnings.catch_warnings(record=True) as records:
43
+ run_warnings_caught(args)
44
+
45
+ # Print warnings
46
+ if args.warnings:
47
+ for record in records:
48
+ warnings.showwarning(
49
+ record.message,
50
+ record.category,
51
+ record.filename,
52
+ lineno=record.lineno,
53
+ line=record.line,
54
+ )
55
+
56
+
57
+ def run_warnings_caught(args: Arguments):
58
+ # Check if input and output directory are different
59
+ path_in = os.path.realpath(os.path.dirname(args.input_investigation_file))
60
+ path_out = os.path.realpath(os.path.dirname(args.output_investigation_file))
61
+ if path_in == path_out:
62
+ msg = f"Can't output ISA-tab files to same directory as as input: {path_in} == {path_out}"
63
+ raise IsaException(msg)
64
+
65
+ with ExitStack() as stack:
66
+ if args.output_investigation_file == "-": # pragma: no cover
67
+ output_investigation_file = sys.stdout
68
+ else:
69
+ output_investigation_file = stack.push(open(args.output_investigation_file, "wt"))
70
+
71
+ investigation, studies, assays = run_reading(args, path_in)
72
+ run_writing(
73
+ args,
74
+ path_out,
75
+ output_investigation_file,
76
+ investigation,
77
+ studies,
78
+ assays,
79
+ )
80
+
81
+
82
+ def run_reading(
83
+ args, path_in
84
+ ) -> Tuple[InvestigationInfo, Dict[int, Study], Dict[int, Dict[int, Assay]]]:
85
+ # Read investigation
86
+ with open(args.input_investigation_file, "rt") as inputf:
87
+ investigation = InvestigationReader.from_stream(inputf).read()
88
+
89
+ # Validate investigation
90
+ InvestigationValidator(investigation).validate()
91
+
92
+ # Read studies and assays
93
+ studies: Dict[int, Study] = {}
94
+ assays: Dict[int, Dict[int, Assay]] = {}
95
+ for s, study_info in enumerate(investigation.studies):
96
+ if study_info.info.path:
97
+ with open(os.path.join(path_in, study_info.info.path), "rt") as inputf:
98
+ studies[s] = StudyReader.from_stream(f"S{s + 1}", inputf).read()
99
+ if study_info.assays:
100
+ assays[s] = {}
101
+ for a, assay_info in enumerate(study_info.assays):
102
+ if assay_info.path:
103
+ with open(os.path.join(path_in, assay_info.path), "rt") as inputf:
104
+ assays[s][a] = AssayReader.from_stream(f"S{s + 1}", f"A{a + 1}", inputf).read()
105
+
106
+ # Validate studies and assays
107
+ for s, study_info in enumerate(investigation.studies):
108
+ if study_info.info.path:
109
+ StudyValidator(investigation, study_info, studies[s]).validate()
110
+ for a, assay_info in enumerate(study_info.assays):
111
+ if assay_info.path:
112
+ AssayValidator(investigation, study_info, assay_info, assays[s][a]).validate()
113
+
114
+ return investigation, studies, assays
115
+
116
+
117
+ def run_writing(
118
+ args,
119
+ path_out,
120
+ output_investigation_file: typing.TextIO,
121
+ investigation: InvestigationInfo,
122
+ studies: Dict[int, Study],
123
+ assays: Dict[int, Dict[int, Assay]],
124
+ ):
125
+ # Write investigation
126
+ if output_investigation_file.name == "<stdout>":
127
+ InvestigationWriter.from_stream(
128
+ investigation, output_investigation_file, quote=args.quotes
129
+ ).write()
130
+ else:
131
+ with open(output_investigation_file.name, "wt", newline="") as outputf:
132
+ InvestigationWriter.from_stream(investigation, outputf, quote=args.quotes).write()
133
+
134
+ # Write studies and assays
135
+ for s, study_info in enumerate(investigation.studies):
136
+ if output_investigation_file.name == "<stdout>":
137
+ if study_info.info.path:
138
+ StudyWriter.from_stream(
139
+ studies[s], output_investigation_file, quote=args.quotes
140
+ ).write()
141
+ for a, assay_info in enumerate(study_info.assays):
142
+ if assay_info.path:
143
+ AssayWriter.from_stream(
144
+ assays[s][a], output_investigation_file, quote=args.quotes
145
+ ).write()
146
+ else:
147
+ if study_info.info.path:
148
+ with open(
149
+ os.path.join(path_out, study_info.info.path), "wt", newline=""
150
+ ) as outputf:
151
+ StudyWriter.from_stream(studies[s], outputf, quote=args.quotes).write()
152
+ for a, assay_info in enumerate(study_info.assays):
153
+ if assay_info.path:
154
+ with open(os.path.join(path_out, assay_info.path), "wt", newline="") as outputf:
155
+ AssayWriter.from_stream(assays[s][a], outputf, quote=args.quotes).write()
156
+
157
+
158
+ @app.command()
159
+ def main(
160
+ input_investigation_file: Annotated[
161
+ str,
162
+ typer.Option(
163
+ "--input-investigation-file",
164
+ "-i",
165
+ help="Path to input investigation file",
166
+ ),
167
+ ],
168
+ output_investigation_file: Annotated[
169
+ str,
170
+ typer.Option(
171
+ "--output-investigation-file",
172
+ "-o",
173
+ help=(
174
+ 'Path to output investigation file, stdout ("-") by default. '
175
+ "Needs to be in a different directory!"
176
+ ),
177
+ ),
178
+ ],
179
+ quotes: Annotated[
180
+ Optional[str],
181
+ typer.Option(
182
+ "--quotes",
183
+ "-q",
184
+ help='Character for quoting, e.g. "\\"" (None by default)',
185
+ ),
186
+ ] = None,
187
+ warnings: Annotated[
188
+ bool,
189
+ typer.Option(
190
+ "--warnings/--no-warnings",
191
+ help="Show ISA-tab related warnings (default is to show)",
192
+ ),
193
+ ] = True,
194
+ ):
195
+ # Convert to `Arguments` object.
196
+ args = Arguments(
197
+ input_investigation_file=input_investigation_file,
198
+ output_investigation_file=output_investigation_file,
199
+ quotes=quotes,
200
+ warnings=warnings,
201
+ )
202
+ # Start application
203
+ return run(args)
204
+
205
+
206
+ if __name__ == "__main__": # pragma: no cover
207
+ typer.run(main)
@@ -0,0 +1,104 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Read from ISA-Tab and print validation warnings, if any."""
3
+
4
+ import os
5
+ from typing import Annotated
6
+ import warnings
7
+
8
+ import attrs
9
+ import typer
10
+
11
+ from altamisa.isatab import (
12
+ AssayReader,
13
+ AssayValidator,
14
+ InvestigationReader,
15
+ InvestigationValidator,
16
+ StudyReader,
17
+ StudyValidator,
18
+ )
19
+
20
+ #: Typer application instance.
21
+ app = typer.Typer()
22
+
23
+
24
+ @attrs.define
25
+ class Arguments:
26
+ input_investigation_file: str
27
+ show_duplicate_warnings: bool
28
+
29
+
30
+ @app.command()
31
+ def main(
32
+ input_investigation_file: Annotated[
33
+ str,
34
+ typer.Option(
35
+ "--input-investigation-file",
36
+ "-i",
37
+ help="Path to input investigation file",
38
+ ),
39
+ ],
40
+ show_duplicate_warnings: Annotated[
41
+ bool,
42
+ typer.Option(
43
+ "--show-duplicate-warnings/--no-show-duplicate_warnings",
44
+ help="Show duplicated warnings, i.e. with same message and same category (False by default)",
45
+ ),
46
+ ] = False,
47
+ ):
48
+ """Main entry point."""
49
+ # Convert to `Arguments` object.
50
+ args = Arguments(
51
+ input_investigation_file=input_investigation_file,
52
+ show_duplicate_warnings=show_duplicate_warnings,
53
+ )
54
+ # Show all warnings of same type and content
55
+ if args.show_duplicate_warnings:
56
+ warnings.simplefilter("always")
57
+
58
+ # Collect warnings
59
+ with warnings.catch_warnings(record=True) as records:
60
+ run_warnings_caught(args)
61
+
62
+ # Print warnings
63
+ for record in records:
64
+ warnings.showwarning(
65
+ record.message, record.category, record.filename, lineno=record.lineno, line=record.line
66
+ )
67
+
68
+
69
+ def run_warnings_caught(args: Arguments):
70
+ # Read investigation
71
+ with open(args.input_investigation_file, "rt") as inputf:
72
+ investigation = InvestigationReader.from_stream(inputf).read()
73
+
74
+ # Validate investigation
75
+ InvestigationValidator(investigation).validate()
76
+
77
+ # Read studies and assays
78
+ path_in = os.path.normpath(os.path.dirname(args.input_investigation_file))
79
+ studies = {}
80
+ assays = {}
81
+ for s, study_info in enumerate(investigation.studies):
82
+ if study_info.info.path:
83
+ with open(os.path.join(path_in, study_info.info.path), "rt") as inputf:
84
+ studies[s] = StudyReader.from_stream(f"S{s + 1}", inputf).read()
85
+ if study_info.assays:
86
+ assays[s] = {}
87
+ for a, assay_info in enumerate(study_info.assays):
88
+ if assay_info.path:
89
+ with open(os.path.join(path_in, assay_info.path), "rt") as inputf:
90
+ assays[s][a] = AssayReader.from_stream(f"S{s + 1}", f"A{a + 1}", inputf).read()
91
+
92
+ # Validate studies and assays
93
+ for s, study_info in enumerate(investigation.studies):
94
+ if study_info.info.path:
95
+ StudyValidator(investigation, study_info, studies[s]).validate()
96
+ for a, assay_info in enumerate(study_info.assays):
97
+ if assay_info.path:
98
+ AssayValidator(
99
+ investigation, study_info, assay_info, assays[s][a], studies[s]
100
+ ).validate()
101
+
102
+
103
+ if __name__ == "__main__": # pragma: no cover
104
+ typer.run(main)
File without changes