-
Notifications
You must be signed in to change notification settings - Fork 0
/
pATLAS_consensus_json.py
83 lines (58 loc) · 2.21 KB
/
pATLAS_consensus_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
"""
Purpose
-------
This module is intended to generate a json output from the consensus results from
all the approaches available through options (mapping, assembly, mash screen)
Expected input
--------------
The following variables are expected whether using NextFlow or the
:py:func:`main` executor.
- ``mapping_json`` : String with the name of the json file with mapping results.
- e.g.: ``'mapping_SampleA.json'``
- ``dist_json`` : String with the name of the json file with mash dist results.
- e.g.: ``'mash_dist_SampleA.json'``
- ``screen_json`` : String with the name of the json file with mash screen results.
- e.g.: ``'mash_screen_sampleA.json'``
Code documentation
------------------
"""
__version__ = "0.1.0"
__build__ = "24022018"
__template__ = "pATLAS_consensus_json-nf"
import os
import json
from assemblerflow_utils.assemblerflow_base import get_logger, MainWrapper
logger = get_logger(__file__)
if __file__.endswith(".command.sh"):
LIST_OF_FILES = '$infile_list'.split()
logger.debug("Running {} with parameters:".format(
os.path.basename(__file__)))
logger.debug("LIST_OF_FILES: {}".format(LIST_OF_FILES))
@MainWrapper
def main(list_of_jsons):
"""
Parameters
----------
list_of_jsons: list
A list of files provided by fullConsensus process provided by nextflow
"""
# first lets gather a collection of the input and their corresponding dicts
file_correspondence = {}
for infile in list_of_jsons:
file_dict = json.load(open(infile))
file_correspondence[infile] = file_dict
json_dict = {}
for accession in list(file_correspondence.values())[0]:
if all([True if accession in f_dict else False
for f_dict in file_correspondence.values()]):
accession_dict = {}
for infile in file_correspondence.keys():
accession_dict[infile] = file_correspondence[infile][accession]
json_dict[accession] = accession_dict
out_file = open("consensus_{}.json".format(
list_of_jsons[0].split(".")[0].split("_")[-1]), "w")
out_file.write(json.dumps(json_dict))
out_file.close()
if __name__ == "__main__":
main(LIST_OF_FILES)