pypappsocli 0.0.15__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.
@@ -0,0 +1,15 @@
1
+ from .mcqcbor2align import mcqcbor2align
2
+ from .mcqcbor2json import mcqcbor2json
3
+ from .mcqcbor2tsv import mcqcbor2tsv
4
+ from .mcqcbor2xic import mcqcbor2xic
5
+ from .mz2scan import mz2scan
6
+ from .mztic import mztic
7
+ from .psmcbor2json import psmcbor2json
8
+ from .psmcbor2ods import psmcbor2ods
9
+ from .psmcbor2psimod import psmcbor2psimod
10
+ from .psmcbor2scanreport import psmcbor2scanreport
11
+ from .psmcborfdr import psmcborfdr
12
+ from .psmcborfilter import psmcborfilter
13
+ from .psmcborms2pip import psmcborms2pip
14
+ from .psmcborproteinfilter import psmcborproteinfilter
15
+ from .xicextractor import xicextractor
@@ -0,0 +1,57 @@
1
+ import json
2
+ import os
3
+ import tempfile
4
+
5
+ import click
6
+ import importlib_resources as resources
7
+ import typst
8
+ from pymasschroqcbor import HarvestMasschroqCbor
9
+
10
+
11
+ def get_typst_cmd():
12
+ ref = resources.files("pymasschroqcbor") / "data/typst/alignment_report.typ"
13
+ return ref.read_text(encoding="utf-8")
14
+
15
+
16
+ @click.command()
17
+ @click.argument("input", nargs=1)
18
+ @click.option(
19
+ "--pdf",
20
+ help="The alignment report PDF file.",
21
+ default="alignment_report.pdf",
22
+ )
23
+ def mcqcbor2align(input, pdf):
24
+ """MassChroQ CBOR data alignment report."""
25
+ with open(input, "rb") as fp:
26
+ harvester = HarvestMasschroqCbor(fp)
27
+ alignment = {}
28
+ alignment["methods"] = harvester.masschroq_methods()
29
+ alignment["identification_data"] = harvester.identification_data()
30
+ alignment["alignment_data"] = harvester.alignment_data()
31
+ assert alignment is not None
32
+ json_string = json.dumps(alignment, indent=4)
33
+ # print(json_string)
34
+
35
+ with (
36
+ tempfile.TemporaryDirectory() as tmpdirname,
37
+ open(
38
+ os.path.join(tmpdirname, "alignment_data.json"), "w"
39
+ ) as json_data_file,
40
+ ):
41
+ # print("created temporary directory", tmpdirname)
42
+
43
+ json_data_file.write(json_string)
44
+ with open(
45
+ os.path.join(tmpdirname, "alignment_report.typ"), "w"
46
+ ) as align_typ_file:
47
+ align_typ_file.write(get_typst_cmd())
48
+ align_typ_file.close()
49
+ typst.compile(
50
+ os.path.join(tmpdirname, "alignment_report.typ"),
51
+ output=pdf,
52
+ )
53
+ print("write pdf output file : ", pdf)
54
+
55
+
56
+ if __name__ == "__main__":
57
+ mcqcbor2align()
@@ -0,0 +1,47 @@
1
+ import json
2
+
3
+ import click
4
+
5
+ from pymasschroqcbor import HarvestMasschroqCbor
6
+
7
+
8
+ @click.command()
9
+ @click.argument("input", nargs=1)
10
+ @click.option(
11
+ "--informations", is_flag=True, help="get the MassChroQ informations data."
12
+ )
13
+ @click.option("--alignment", is_flag=True, help="get the MassChroQ alignment data.")
14
+ @click.option("--methods", is_flag=True, help="get the MassChroQ methods.")
15
+ @click.option("--actions", is_flag=True, help="get the MassChroQ actions section")
16
+ def mcqcbor2json(input, informations, alignment, methods, actions):
17
+ """MassChroQ CBOR data file extractor to JSON."""
18
+ with open(input, "rb") as fp:
19
+ harvester = HarvestMasschroqCbor(fp)
20
+
21
+ if informations:
22
+ informations = harvester.informations()
23
+ assert informations is not None
24
+ json_string = json.dumps(informations, indent=4)
25
+ print(json_string)
26
+
27
+ if alignment:
28
+ alignment = harvester.alignment_data()
29
+ assert alignment is not None
30
+ json_string = json.dumps(alignment, indent=4)
31
+ print(json_string)
32
+
33
+ if methods:
34
+ methods = harvester.masschroq_methods()
35
+ assert methods is not None
36
+ json_string = json.dumps(methods, indent=4)
37
+ print(json_string)
38
+
39
+ if actions:
40
+ actions = harvester.actions()
41
+ assert actions is not None
42
+ json_string = json.dumps(actions, indent=4)
43
+ print(json_string)
44
+
45
+
46
+ if __name__ == "__main__":
47
+ mcqcbor2json()
@@ -0,0 +1,20 @@
1
+ import click
2
+ from pymasschroqcbor import HarvestMasschroqCbor
3
+
4
+
5
+ @click.command()
6
+ @click.argument("input", nargs=1)
7
+ @click.option(
8
+ "--tsv",
9
+ help="tsv output file.",
10
+ )
11
+ def mcqcbor2tsv(input, tsv):
12
+ """MassChroQ CBOR data file extractor to JSON."""
13
+ with open(input, "rb") as fp:
14
+ harvester = HarvestMasschroqCbor(fp)
15
+ for measurement in harvester.yield_msruns_peptide_measurements():
16
+ print(measurement)
17
+
18
+
19
+ if __name__ == "__main__":
20
+ mcqcbor2tsv()
@@ -0,0 +1,80 @@
1
+ import json
2
+ import os
3
+ import tempfile
4
+
5
+ import click
6
+ import importlib_resources as resources
7
+ import typst
8
+
9
+ from pymasschroqcbor import HarvestMasschroqCbor
10
+
11
+
12
+ def get_typst_cmd():
13
+ ref = resources.files("pymasschroqcbor") / "data/typst/xic_report.typ"
14
+ return ref.read_text(encoding="utf-8")
15
+
16
+
17
+ @click.command()
18
+ @click.argument("input", nargs=1)
19
+ @click.option(
20
+ "--peptides",
21
+ prompt="list of peptides id to display",
22
+ help="list of peptides id to display.",
23
+ )
24
+ @click.option(
25
+ "--pdf",
26
+ help="The XIC report PDF file.",
27
+ default="xic_report.pdf",
28
+ )
29
+ def mcqcbor2xic(input, peptides, pdf):
30
+ """MassChroQ CBOR data file extractor to JSON."""
31
+ with open(input, "rb") as fp:
32
+ harvester = HarvestMasschroqCbor(fp)
33
+ xic_json = {}
34
+ xic_json["msrun_list"] = harvester.identification_data().get("msrun_list")
35
+ # xic_json["alignment_data"] = harvester.alignment_data()
36
+
37
+ xic_json["quantification_data"] = {}
38
+ harvester.populate_quantification_data_typst(
39
+ xic_json["quantification_data"], peptides.split()
40
+ )
41
+ # for xic in harvester.yield_msruns_peptide_measurements():
42
+ # # print(xic["peptide_id"])
43
+ # if xic.get("peptide_id") == peptides:
44
+ # if alignment["alignment_data"].get(xic.get("peptide_id")) is None:
45
+ # alignment["alignment_data"][xic.get("peptide_id")] = []
46
+ # alignment["alignment_data"][xic.get("peptide_id")].append(
47
+ # {
48
+ # "group": xic.get("group"),
49
+ # "mods": xic.get("mods"),
50
+ # "msrun": xic.get("msrun"),
51
+ # "proforma": xic.get("proforma"),
52
+ # "rt_target":
53
+ # }
54
+ # )
55
+
56
+ # print(xic)
57
+
58
+ assert xic_json is not None
59
+ json_string = json.dumps(xic_json, indent=4)
60
+ # print(json_string)
61
+
62
+ with tempfile.TemporaryDirectory() as tmpdirname:
63
+ # print("created temporary directory", tmpdirname)
64
+ with open(os.path.join(tmpdirname, "xic_data.json"), "w") as json_data_file:
65
+ json_data_file.write(json_string)
66
+ json_data_file.close()
67
+ with open(
68
+ os.path.join(tmpdirname, "xic_report.typ"), "w"
69
+ ) as align_typ_file:
70
+ align_typ_file.write(get_typst_cmd())
71
+ align_typ_file.close()
72
+ typst.compile(
73
+ os.path.join(tmpdirname, "xic_report.typ"),
74
+ output=pdf,
75
+ )
76
+ print("write pdf output file : ", pdf)
77
+
78
+
79
+ if __name__ == "__main__":
80
+ mcqcbor2xic()
pypappsocli/mz2scan.py ADDED
@@ -0,0 +1,106 @@
1
+ import json as json_ns
2
+ import os
3
+ import tempfile
4
+
5
+ import click
6
+ import importlib_resources as resources
7
+ import typst
8
+
9
+ from pymzcbor import MzReader
10
+ from pymzcbor.model.mzspectrum import MzSpectrum
11
+
12
+
13
+ def get_typst_cmd():
14
+ ref = resources.files("pymzcbor") / "data/typst/scan_report.typ"
15
+ return ref.read_text(encoding="utf-8")
16
+
17
+
18
+ @click.command()
19
+ @click.argument("input", nargs=1)
20
+ @click.option(
21
+ "--index",
22
+ prompt="list of scan index to extract",
23
+ help="list of scan index to extract.",
24
+ )
25
+ @click.option("--json", help="The JSON file containing scans.")
26
+ @click.option("--mgf", help="The MGF file containing scans.")
27
+ @click.option(
28
+ "--pdf",
29
+ help="The PDF output to display scans.",
30
+ default="scans.pdf",
31
+ )
32
+ def mz2scan(input, index, json, pdf, mgf):
33
+ """MassChroQ CBOR data file extractor to JSON."""
34
+
35
+ scan_json = []
36
+ index_num_list = []
37
+ precursor_scan_json = {}
38
+
39
+ for str_num in index.split():
40
+ index_num_list.append(int(str_num))
41
+ mz_reader = MzReader(input)
42
+
43
+ mgf_data_file = None
44
+
45
+ if mgf is not None:
46
+ # print(json_string)
47
+ print("write MGF output file : ", mgf)
48
+ mgf_data_file = open(mgf, "w")
49
+
50
+ for spectrum_index in index_num_list:
51
+ mz_spectrum = mz_reader.get_mz_spectrum_by_index(0, spectrum_index)
52
+ if mz_spectrum is not None:
53
+ if mgf_data_file is not None:
54
+ mgf_data_file.write(mz_spectrum.to_mgf())
55
+ scan_json.append(mz_spectrum.to_dict())
56
+ precursor_list = mz_spectrum.get_precursor_list()
57
+ if precursor_list is not None:
58
+ for precursor_mz in precursor_list:
59
+ try:
60
+ precursor_id = precursor_mz.spectrum_ref
61
+ precursor_spectrum = mz_reader.get_mz_spectrum_by_native_id(
62
+ 0, precursor_id
63
+ )
64
+ if precursor_spectrum is not None:
65
+ precursor_scan_json[precursor_id] = (
66
+ precursor_spectrum.to_dict()
67
+ )
68
+ except ValueError:
69
+ pass
70
+
71
+ if mgf_data_file is not None:
72
+ mgf_data_file.close()
73
+
74
+ json_string = json_ns.dumps(
75
+ {"precursor_spectrum_map": precursor_scan_json, "scan_list": scan_json},
76
+ indent=4,
77
+ )
78
+ if json is not None:
79
+ # print(json_string)
80
+
81
+ print("write json output file : ", json)
82
+ with open(json, "w") as json_data_file:
83
+ json_data_file.write(json_string)
84
+ json_data_file.close()
85
+ else:
86
+ with tempfile.TemporaryDirectory() as tmpdirname:
87
+ print("created temporary directory", tmpdirname)
88
+ with open(
89
+ os.path.join(tmpdirname, "scan_data.json"), "w"
90
+ ) as json_data_file:
91
+ json_data_file.write(json_string)
92
+ json_data_file.close()
93
+ with open(
94
+ os.path.join(tmpdirname, "scan_report.typ"), "w"
95
+ ) as align_typ_file:
96
+ print("write pdf output file : ", pdf)
97
+ align_typ_file.write(get_typst_cmd())
98
+ align_typ_file.close()
99
+ typst.compile(
100
+ os.path.join(tmpdirname, "scan_report.typ"),
101
+ output=pdf,
102
+ )
103
+
104
+
105
+ if __name__ == "__main__":
106
+ mz2scan()
pypappsocli/mztic.py ADDED
@@ -0,0 +1,59 @@
1
+ import json as json_ns
2
+ import os
3
+ import tempfile
4
+
5
+ import click
6
+ import importlib_resources as resources
7
+ import typst
8
+ from pyteomics import mzml
9
+
10
+ from pymzcbor import MzReader
11
+
12
+
13
+ def get_typst_cmd():
14
+ ref = resources.files("pymzcbor") / "data/typst/tic.typ"
15
+ return ref.read_text(encoding="utf-8")
16
+
17
+
18
+ @click.command()
19
+ @click.argument("input", nargs=1)
20
+ @click.option(
21
+ "--pdf",
22
+ help="The PDF output to display total ion count chromatogram.",
23
+ default="tic.pdf",
24
+ )
25
+ def mztic(input, pdf):
26
+ """MassChroQ CBOR data file extractor to JSON.
27
+ reading mzML using pyteomics"""
28
+
29
+ mz_reader = MzReader(input)
30
+
31
+ source_file_list = mz_reader.get_source_file_list()
32
+ title = "Total Ion Count"
33
+ if len(source_file_list) > 0:
34
+ title = source_file_list[0].get("name")
35
+
36
+ tic_dict = mz_reader.get_stats_tic_chromatogram()
37
+ tic_dict["title"] = title
38
+ json_string = json_ns.dumps(
39
+ tic_dict,
40
+ indent=4,
41
+ )
42
+ if tic_dict is not None:
43
+ with tempfile.TemporaryDirectory() as tmpdirname:
44
+ print("created temporary directory", tmpdirname)
45
+ with open(os.path.join(tmpdirname, "tic.json"), "w") as json_data_file:
46
+ json_data_file.write(json_string)
47
+ json_data_file.close()
48
+ with open(os.path.join(tmpdirname, "tic.typ"), "w") as tic_file:
49
+ print("write pdf output file : ", pdf)
50
+ tic_file.write(get_typst_cmd())
51
+ tic_file.close()
52
+ typst.compile(
53
+ os.path.join(tmpdirname, "tic.typ"),
54
+ output=pdf,
55
+ )
56
+
57
+
58
+ if __name__ == "__main__":
59
+ mztic()
@@ -0,0 +1,92 @@
1
+ # This file is part of pypsmcbor.
2
+ #
3
+ # pypsmcbor is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # pypsmcbor is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with pypsmcbor. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ import json
17
+ import sys
18
+
19
+ import click
20
+
21
+ from pypsmcbor import HarvestPsmCbor
22
+
23
+
24
+ @click.command()
25
+ @click.option(
26
+ "--input",
27
+ "-i",
28
+ help="input file name. if not defined INPUT is read from the standard input",
29
+ )
30
+ @click.option(
31
+ "--parameters",
32
+ is_flag=True,
33
+ help="parameters to json.",
34
+ )
35
+ @click.option(
36
+ "--protein_map",
37
+ is_flag=True,
38
+ help="extract protein map to json.",
39
+ )
40
+ @click.version_option(package_name="pypsmcbor")
41
+ def psmcbor2json(input, protein_map, parameters):
42
+ """PSM CBOR data file extractor to JSON."""
43
+
44
+ fp = None
45
+ stdin = False
46
+ if input is None:
47
+ print("reading PSM CBOR data from standard input")
48
+ fp = open(sys.stdin.fileno(), "rb")
49
+ stdin = True
50
+ else:
51
+ fp = open(input, "rb")
52
+
53
+ if fp is not None:
54
+ harvester = HarvestPsmCbor(fp)
55
+ export_json = {}
56
+ cbor_value = ""
57
+
58
+ harvester._goto_root_tag("informations")
59
+ export_json["informations"] = harvester._cbor_stream_decoder.to_dict()
60
+ if harvester._cbor_stream_decoder.has_next():
61
+ cbor_value = harvester._cbor_stream_decoder.value()
62
+ if cbor_value == "log":
63
+ export_json["log"] = harvester._cbor_stream_decoder.to_dict()
64
+
65
+ if parameters:
66
+ param_map_dict_json = {}
67
+
68
+ if cbor_value == "parameter_map":
69
+ export_json["parameter_map"] = harvester._cbor_stream_decoder.to_dict()
70
+ else:
71
+ for param in harvester.yield_parameter():
72
+ param_map_dict_json[param.get("software")] = param.get("parameters")
73
+ export_json["parameter_map"] = param_map_dict_json
74
+
75
+ if protein_map:
76
+ protein_dict_json = {}
77
+ for protein in harvester.yield_protein():
78
+ protein_dict_json[protein.get("accession")] = protein.get("protein")
79
+ export_json["protein_map"] = protein_dict_json
80
+
81
+ print(json.dumps(export_json, indent=4))
82
+
83
+ if stdin:
84
+ while True:
85
+ chunk = fp.read(1000)
86
+ if not chunk:
87
+ break
88
+ # fp.close()
89
+
90
+
91
+ if __name__ == "__main__":
92
+ psmcbor2json()
@@ -0,0 +1,91 @@
1
+ # This file is part of pypsmcbor.
2
+ #
3
+ # pypsmcbor is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # pypsmcbor is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with pypsmcbor. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ # pixi clean && pixi reinstall && pixi run psmcbor2ods data/sage/small/sage_output.cbor -o test.ods
17
+ import json
18
+ import os
19
+ import sys
20
+
21
+ import click
22
+
23
+ from pypsmcbor import Ms2pipPsmEval, PsmCborStreamIn, PsmCborStreamOut
24
+
25
+
26
+ @click.command()
27
+ @click.option(
28
+ "--cpus",
29
+ "-c",
30
+ help="maximum number of threads to use (integer) 1 by default",
31
+ default=1,
32
+ )
33
+ @click.option(
34
+ "--input",
35
+ "-i",
36
+ help="input CBOR PSM file name. if not defined INPUT is read from the standard input",
37
+ )
38
+ @click.option(
39
+ "--output",
40
+ "-o",
41
+ help="output CBOR PSM file. if not defined OUTPUT is written to the standard output",
42
+ )
43
+ @click.version_option(package_name="pypsmcbor")
44
+ def psmcbor2mzcbor(input, output, param, cpus):
45
+ """TODO launch mzML peaklist conversion to mzCBOR for all samples"""
46
+ param_dict = None
47
+ if param is None:
48
+ # https://ms2pip.readthedocs.io/en/latest/prediction-models/
49
+ param_dict = {"precision": {"unit": "dalton", "value": 0.02}, "model": "HCD"}
50
+ else:
51
+ if Path(param).exists():
52
+ with open(param, "r") as file:
53
+ param_dict = json.load(file)
54
+ else:
55
+ param_dict = json.loads(param)
56
+
57
+ fp = None
58
+ if input is None:
59
+ # print("reading PSM CBOR data from standard input")
60
+ fp = open(sys.stdin.fileno(), "rb")
61
+ else:
62
+ fp = open(input, "rb")
63
+
64
+ fp_out = None
65
+ report = None
66
+ if output is None:
67
+ # print("writing PSM CBOR data to standard output")
68
+ fp_out = open(sys.stdout.fileno(), "wb")
69
+ else:
70
+ fp_out = open(output, "wb")
71
+
72
+ def report_sample(sample_name: str):
73
+ print(f"starting sample {sample_name}")
74
+
75
+ def report_scan(scan_id: str):
76
+ print(f"starting scan {scan_id}")
77
+
78
+ report = {"sample": report_sample, "scan": report_scan}
79
+ if fp is not None:
80
+ replacer = Ms2pipPsmEval(param_dict)
81
+ stream_in = PsmCborStreamIn(fp)
82
+ if fp_out is not None:
83
+ stream_out = PsmCborStreamOut(fp_out)
84
+ stream_out.copy_and_replace_threaded(stream_in, replacer, cpus, report)
85
+ if output is None:
86
+ sys.stdout.flush()
87
+ fp.close()
88
+
89
+
90
+ if __name__ == "__main__":
91
+ psmcbor2mzcbor()
@@ -0,0 +1,76 @@
1
+ # This file is part of pypsmcbor.
2
+ #
3
+ # pypsmcbor is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # pypsmcbor is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with pypsmcbor. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ # pixi clean && pixi reinstall && pixi run psmcbor2ods data/sage/small/sage_output.cbor -o test.ods
17
+ import os
18
+ import sys
19
+
20
+ import click
21
+ from pyodsstream import (
22
+ CalcWriterInterface,
23
+ OdsDocWriter,
24
+ TsvDirectoryWriter,
25
+ )
26
+
27
+ from pypsmcbor import HarvestPsmCbor, PsmCborSoftware
28
+ from pypsmcbor.ods.ods_all_tables import write_psm_tables
29
+
30
+
31
+ @click.command()
32
+ @click.option(
33
+ "--input",
34
+ "-i",
35
+ help="input file name. if not defined INPUT is read from the standard input",
36
+ )
37
+ @click.option(
38
+ "--output",
39
+ "-o",
40
+ help="output file : ods extension for ODS, TSV directory otherwise",
41
+ )
42
+ @click.option(
43
+ "--engines",
44
+ "-e",
45
+ default="sage spoms features grouping",
46
+ help="default: sage spoms features grouping",
47
+ )
48
+ @click.version_option(package_name="pypsmcbor")
49
+ def psmcbor2ods(input, output, engines):
50
+ """PSM CBOR data file to ODS."""
51
+ fp = None
52
+ if input is None:
53
+ print("reading PSM CBOR data from standard input")
54
+ fp = open(sys.stdin.fileno(), "rb")
55
+ else:
56
+ fp = open(input, "rb")
57
+ if fp is not None:
58
+ harvester = HarvestPsmCbor(fp)
59
+ # Split the file name and extension
60
+ file_name, file_extension = os.path.splitext(output)
61
+ writer = None
62
+ if file_extension == ".ods":
63
+ fp = open(output, "wb")
64
+ writer = OdsDocWriter(fp)
65
+ else:
66
+ writer = TsvDirectoryWriter(output)
67
+
68
+ enum_engines = []
69
+ for engine in engines.split(" "):
70
+ enum_engines.append(PsmCborSoftware(engine))
71
+
72
+ write_psm_tables(writer, harvester, enum_engines)
73
+
74
+
75
+ if __name__ == "__main__":
76
+ psmcbor2ods()