-
Notifications
You must be signed in to change notification settings - Fork 38
/
json2isatab.py
61 lines (54 loc) · 2.72 KB
/
json2isatab.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
# -*- coding: utf-8 -*
"""Convert ISA-JSON to ISA-Tab"""
import logging
import os
import shutil
from isatools import isajson, isatab
log = logging.getLogger('isatools')
def convert(json_fp, path, i_file_name='i_investigation.txt',
config_dir=isajson.default_config_dir,
validate_first=True, write_factor_values_in_assay_table=False):
"""
Converter for ISA JSON to ISA Tab. Currently only converts
investigation file contents
:param json_fp: File pointer to ISA JSON input
:param path: Directory to ISA tab output
:param i_file_name: Investigation file name, default is
i_investigation.txt
:param config_dir: Directory to config directory
:param validate_first: Validate JSON before conversion, default is True
:param write_factor_values_in_assay_table: Whether or not to write out Factor values in the Assay table, default is False
Example usage:
Read from a JSON and write to an investigation file, make sure to
create/open relevant Python file objects.
from isatools.convert import json2isatab
json_file = open('BII-I-1.json', 'r')
tab_file = open('i_investigation.txt', 'w')
json2isatab.convert(json_file, path)
"""
if validate_first:
log.info("Validating input JSON before conversion")
report = isajson.validate(fp=json_fp, config_dir=config_dir,
log_level=logging.ERROR)
if len(report['errors']) > 0:
log.fatal("Could not proceed with conversion as there are some "
"fatal validation errors. Check log.")
return
json_fp.seek(0) # reset file pointer after validation
log.info("Loading ISA-JSON from %s", json_fp.name)
isa_obj = isajson.load(fp=json_fp)
log.info("Dumping ISA-Tab to %s", path)
log.debug("Using configuration from %s", config_dir)
isatab.dump(isa_obj=isa_obj, output_path=path, i_file_name=i_file_name,
write_factor_values_in_assay_table=write_factor_values_in_assay_table)
# copy data files across from source directory where JSON is located
log.info("Copying data files from source to target")
for file in [f for f in os.listdir(os.path.dirname(json_fp.name))
if not (f.endswith('.txt') and (f.startswith('i_') or
f.startswith('s_') or
f.startswith('a_'))) and
not (f.endswith('.json'))]:
filepath = os.path.join(os.path.dirname(json_fp.name), file)
if os.path.isfile(filepath):
log.debug("Copying %s to %s", filepath, path)
shutil.copy(filepath, path)