[7d5693]: / scripts / analyse_cic.py

Download this file

97 lines (74 with data), 3.3 kB

 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
84
85
86
87
88
89
90
91
92
93
94
95
96
import argparse
import os
import subprocess
import sys
import yaml
import papermill as pm
sys.path.append('.')
sys.path.append('..')
from pyMultiOmics.common import create_if_not_exist
def load_config_file(config_file):
with open(config_file) as f:
config = yaml.safe_load(f)
return config
def main(args):
# input excel
input_excel = os.path.abspath(args.input_excel)
print('Input Excel:', input_excel)
# annotation CSV
annot_csv = os.path.abspath(args.annot_csv)
print('Annotation CSV:', annot_csv)
# config file
config_file = os.path.abspath(args.config_file)
print('Config file:', config_file)
processing_params = load_config_file(config_file)
print('Pre-processing parameters:')
print(yaml.dump(processing_params, indent=4))
params = {}
params['file_name'] = input_excel
params['annot_csv'] = annot_csv
params.update(processing_params)
# output dir
output_dir = os.path.abspath(args.output_dir)
print('Output Directory:', output_dir)
# Check if template file exists in parent directory
template_parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
template_file = os.path.abspath(
os.path.join(template_parent_dir, 'templates', 'cic_template.ipynb'))
if os.path.isfile(template_file):
print('Template Notebook:', template_file)
else:
# Check if template file exists in current directory
template_file = os.path.abspath(os.path.join('.', 'templates', 'cic_template.ipynb'))
if os.path.isfile(template_file):
print('Template Notebook:', template_file)
else:
print('Error: template notebook not found')
return
output_notebook = os.path.abspath(os.path.join(output_dir, 'cic_analysis.ipynb'))
output_pdf = os.path.abspath(os.path.join(output_dir, 'cic_analysis.pdf'))
print('Output Notebook:', output_notebook)
print('Output PDF:', output_pdf)
create_if_not_exist(output_dir)
_ = pm.execute_notebook(template_file, output_notebook, report_mode=True, parameters=params)
command = ['jupyter', 'nbconvert', '--TemplateExporter.exclude_input=True', '--output',
output_pdf,
'--to', 'pdf', output_notebook]
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode == 0:
print(f'\nPDF output saved to {output_pdf}\n')
else:
print(f'\nFailed to create PDF: {result.stderr}\n')
if __name__ == '__main__':
print('***********************************')
print('* CIC Analysis *')
print('***********************************')
print()
parser = argparse.ArgumentParser(
description='Runs a notebook-based analysis on input data and saves the results as a PDF.')
parser.add_argument('--input_excel', required=True, help='Path to the input Excel file.')
parser.add_argument('--annot_csv', required=True, help='Path to the annotation CSV file.')
parser.add_argument('--output_dir', required=True, help='Path to the output directory.')
parser.add_argument('--config_file', required=True, help='Path to the YAML configuration file.')
args = parser.parse_args()
main(args)