|
a |
|
b/scripts/analyse_cic.py |
|
|
1 |
import argparse |
|
|
2 |
import os |
|
|
3 |
import subprocess |
|
|
4 |
import sys |
|
|
5 |
import yaml |
|
|
6 |
import papermill as pm |
|
|
7 |
|
|
|
8 |
sys.path.append('.') |
|
|
9 |
sys.path.append('..') |
|
|
10 |
|
|
|
11 |
from pyMultiOmics.common import create_if_not_exist |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
def load_config_file(config_file): |
|
|
15 |
with open(config_file) as f: |
|
|
16 |
config = yaml.safe_load(f) |
|
|
17 |
return config |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
def main(args): |
|
|
21 |
|
|
|
22 |
# input excel |
|
|
23 |
input_excel = os.path.abspath(args.input_excel) |
|
|
24 |
print('Input Excel:', input_excel) |
|
|
25 |
|
|
|
26 |
# annotation CSV |
|
|
27 |
annot_csv = os.path.abspath(args.annot_csv) |
|
|
28 |
print('Annotation CSV:', annot_csv) |
|
|
29 |
|
|
|
30 |
# config file |
|
|
31 |
config_file = os.path.abspath(args.config_file) |
|
|
32 |
print('Config file:', config_file) |
|
|
33 |
processing_params = load_config_file(config_file) |
|
|
34 |
|
|
|
35 |
print('Pre-processing parameters:') |
|
|
36 |
print(yaml.dump(processing_params, indent=4)) |
|
|
37 |
params = {} |
|
|
38 |
params['file_name'] = input_excel |
|
|
39 |
params['annot_csv'] = annot_csv |
|
|
40 |
params.update(processing_params) |
|
|
41 |
|
|
|
42 |
# output dir |
|
|
43 |
output_dir = os.path.abspath(args.output_dir) |
|
|
44 |
print('Output Directory:', output_dir) |
|
|
45 |
|
|
|
46 |
# Check if template file exists in parent directory |
|
|
47 |
template_parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
|
|
48 |
template_file = os.path.abspath( |
|
|
49 |
os.path.join(template_parent_dir, 'templates', 'cic_template.ipynb')) |
|
|
50 |
|
|
|
51 |
if os.path.isfile(template_file): |
|
|
52 |
print('Template Notebook:', template_file) |
|
|
53 |
else: |
|
|
54 |
# Check if template file exists in current directory |
|
|
55 |
template_file = os.path.abspath(os.path.join('.', 'templates', 'cic_template.ipynb')) |
|
|
56 |
|
|
|
57 |
if os.path.isfile(template_file): |
|
|
58 |
print('Template Notebook:', template_file) |
|
|
59 |
else: |
|
|
60 |
print('Error: template notebook not found') |
|
|
61 |
return |
|
|
62 |
|
|
|
63 |
output_notebook = os.path.abspath(os.path.join(output_dir, 'cic_analysis.ipynb')) |
|
|
64 |
output_pdf = os.path.abspath(os.path.join(output_dir, 'cic_analysis.pdf')) |
|
|
65 |
|
|
|
66 |
print('Output Notebook:', output_notebook) |
|
|
67 |
print('Output PDF:', output_pdf) |
|
|
68 |
|
|
|
69 |
create_if_not_exist(output_dir) |
|
|
70 |
_ = pm.execute_notebook(template_file, output_notebook, report_mode=True, parameters=params) |
|
|
71 |
|
|
|
72 |
command = ['jupyter', 'nbconvert', '--TemplateExporter.exclude_input=True', '--output', |
|
|
73 |
output_pdf, |
|
|
74 |
'--to', 'pdf', output_notebook] |
|
|
75 |
result = subprocess.run(command, capture_output=True, text=True) |
|
|
76 |
|
|
|
77 |
if result.returncode == 0: |
|
|
78 |
print(f'\nPDF output saved to {output_pdf}\n') |
|
|
79 |
else: |
|
|
80 |
print(f'\nFailed to create PDF: {result.stderr}\n') |
|
|
81 |
|
|
|
82 |
|
|
|
83 |
if __name__ == '__main__': |
|
|
84 |
print('***********************************') |
|
|
85 |
print('* CIC Analysis *') |
|
|
86 |
print('***********************************') |
|
|
87 |
print() |
|
|
88 |
|
|
|
89 |
parser = argparse.ArgumentParser( |
|
|
90 |
description='Runs a notebook-based analysis on input data and saves the results as a PDF.') |
|
|
91 |
parser.add_argument('--input_excel', required=True, help='Path to the input Excel file.') |
|
|
92 |
parser.add_argument('--annot_csv', required=True, help='Path to the annotation CSV file.') |
|
|
93 |
parser.add_argument('--output_dir', required=True, help='Path to the output directory.') |
|
|
94 |
parser.add_argument('--config_file', required=True, help='Path to the YAML configuration file.') |
|
|
95 |
args = parser.parse_args() |
|
|
96 |
main(args) |