-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathngx_plot.py
91 lines (70 loc) · 3.64 KB
/
ngx_plot.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
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""
"""
import os
import pandas as pd
import json
import plotly.graph_objs as go
from plotly.offline import plot
try:
import utils
except ImportError:
from templates import utils
__version__ = "0.0.1"
__build__ = "14.12.2020"
__template__ = "PROCESS_C90-nf"
logger = utils.get_logger(__file__)
if __file__.endswith(".command.sh"):
NGX_FILES = '$ngx_files '.split()
N_TARGET = float("$params.n_target")
SCALE = '$scale'
logger.debug("Running {} with parameters:".format(
os.path.basename(__file__)))
logger.debug("NAX_FILES: {}".format(NGX_FILES))
logger.debug("N_TARGET: {}".format(N_TARGET))
logger.debug("SCALE: {}".format(SCALE))
def main(ngx_files, n_target, scale):
df_ngx = pd.DataFrame(columns=['Sample', 'Reference', 'Assembler', 'NGx', 'Basepairs'])
for file_ngx in ngx_files:
sample_name = os.path.basename(file_ngx).split('_')[0]
data = pd.read_csv(file_ngx)
data['Sample'] = sample_name
df_ngx = pd.concat([df_ngx, data], ignore_index=True)
# Create plot - Lx per reference for each sample
report_dict = {}
for sample in sorted(df_ngx['Sample'].unique()):
for reference in sorted(df_ngx['Reference'].unique()):
fig_ngx = go.Figure()
i = 0
for assembler in sorted(df_ngx['Assembler'].unique(), key=lambda v: v.upper()):
if df_ngx['Basepairs'][(df_ngx['Sample'] == sample) & (df_ngx['Reference'] == reference) & (df_ngx['Assembler'] == assembler)].nunique() > 1:
fig_ngx.add_trace(go.Scatter(x=df_ngx['NGx'][(df_ngx['Sample'] == sample) &
(df_ngx['Reference'] == reference) &
(df_ngx['Assembler'] == assembler)],
y=df_ngx['Basepairs'][(df_ngx['Sample'] == sample) &
(df_ngx['Reference'] == reference) &
(df_ngx['Assembler'] == assembler)],
name=assembler, line=dict(color=utils.COLOURS[i], width=2)))
i += 1
fig_ngx.add_shape(type="line", yref="paper",
x0=n_target*100, y0=0, x1=n_target*100, y1=1,
line=dict(color="#D3D3D3", width=4,dash="dashdot"))
fig_ngx.update_layout(xaxis_title="NG(x) %",
yaxis_title='Basepairs',
plot_bgcolor='rgb(255,255,255)',
xaxis=dict(showline=True, zeroline=False, linewidth=1, linecolor='black',
gridcolor='#DCDCDC'))
fig_ngx.update_xaxes(type=scale)
plot(fig_ngx, filename='{0}_{1}_ngx.html'.format(sample, reference.replace(' ', '_')), auto_open=False)
plot_species = fig_ngx.to_json()
if sample not in report_dict.keys():
report_dict[sample] = {"PlotData": {reference: [plot_species]}}
else:
if reference not in report_dict[sample]["PlotData"].keys():
report_dict[sample]["PlotData"][reference] = [plot_species]
else:
report_dict[sample]["PlotData"][reference].append(plot_species)
with open("ngx.json", "w") as json_report:
json_report.write(json.dumps(report_dict, separators=(",", ":")))
if __name__ == '__main__':
main(NGX_FILES, N_TARGET, SCALE)