-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_misassembly.py
246 lines (201 loc) · 8.68 KB
/
plot_misassembly.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Purpose
-------
Script to plot the location of misassembled contigs in the filtered assemblies against the reference sequences.
Produces a line scatter plot for the misassembled blocks per assembler for each reference.
Expected input
--------------
The following variables are expected whether using NextFlow or the
:py:func:`main` executor.
- ``misassembly_dataframes``: list of csv files containing the dataframe with gap location information
e.g.: ``'[SampleA.csv, SampleB.csv]'``
Expected input
--------------
This script takes the following arguments (in this order):
* List of Paths with the JSON containing the sample ID and the list of gap sizes
Authorship
----------
Inês Mendes, [email protected]
https://github.com/cimendes
"""
import os
import json
import pandas as pd
from copy import deepcopy
from itertools import groupby
from plotly.offline import plot
import plotly.graph_objects as go
from plotly.subplots import make_subplots
try:
import utils
except ImportError:
from templates import utils
__version__ = "0.0.1"
__build__ = "07.06.2021"
__template__ = "PLOT_MISASSEMBLY-nf"
logger = utils.get_logger(__file__)
if __file__.endswith(".command.sh"):
DATAFRAME_LIST = '$misassembly_dataframes'.split()
logger.debug("Running {} with parameters:".format(
os.path.basename(__file__)))
logger.debug("DATAFRAME_LIST: {}".format(DATAFRAME_LIST))
def determine_missing_intervals(intervals, total_len):
"""
"""
start = 0
missing_regions = []
for i in intervals:
diff = i[0] - start
if diff > 0:
missing_regions.append([start, start+diff-1])
start = i[1]
# add terminal region
if start != total_len:
missing_regions.append([start, total_len])
return missing_regions
def merge_intervals(intervals):
""" Merges intersecting intervals.
"""
merged = [deepcopy(intervals[0])]
for current in intervals[1:]:
previous = merged[-1]
# current and previous intervals intersect
if current[0] <= previous[1]:
# determine top position
previous[1] = max(previous[1], current[1])
# merge coverage dictionaries
previous_cov = previous[2]
current_cov = current[2]
for k, v in current_cov.items():
if k not in previous_cov:
previous_cov[k] = v
else:
previous_cov[k] += v
previous[2] = previous_cov
# current and previous intervals do not intersect
else:
merged.append(deepcopy(current))
return merged
def intervals_subgroups(intervals):
"""
"""
subgroups = {}
for i in intervals:
start = i[0]
current_interval = i[2]
# identify groups of subsequent equal values
values_groups = [list(v)
for k, v in groupby(current_interval.values())]
for g in values_groups:
# keep only the start and end points for each group
subgroups[start] = g[0]
subgroups[start+len(g)-1] = g[0]
start += len(g)
return subgroups
def main(dataframes):
li = []
for filename in dataframes:
df = pd.read_csv(filename, index_col=0, header=0)
li.append(df)
frame = pd.concat(li, ignore_index=True)
report_dict = {}
samples = sorted(frame['Sample'].unique())
for sample in samples:
print(sample)
report_dict[sample] = {"PlotData": {}}
try:
references = sorted(frame['Reference'].unique())
except TypeError:
references = frame['Reference'].unique()
for reference in references:
print(' ' + str(reference))
fig = make_subplots(rows=2, cols=1,
row_heights=[0.2, 0.8],
shared_xaxes=True,
vertical_spacing=0.02)
y = 0
gaps_intervals = []
assemblers = sorted(frame['Assembler'].unique(
), key=lambda v: v.upper(), reverse=True)
assemblers_in_plot = []
for assembler in assemblers:
print(' ' + assembler)
coords = frame[(frame['Sample'] == sample) & (frame['Reference'] == reference) &
(frame['Assembler'] == assembler)]
if coords.empty:
continue
else:
assemblers_in_plot.append(assembler)
starts = list(coords['Ref Start'])
stops = list(coords['Ref End'])
misassembly_list = list(coords['Misassembly'])
contigs_list = list(map(str, coords['Contig']))
for i in range(len(starts)):
text='<b>Misassembly</b>: ' + misassembly_list[i] + '<br> <b>Contig:</b> ' + contigs_list[i]
# trace with misassembly location - one per block
fig.add_trace(go.Scatter(x=[starts[i], stops[i]],
y=[y, y],
mode='lines',
line=dict(
color='#000000', width=12),
name=assembler,
showlegend=False,
text=[text, text],
hovertemplate='%{text}' +
'<br><b>Coord</b>: %{x}<br>'),
row=2, col=1)
gaps_dict = {i: 1 for i in range(
int(starts[i]), int(stops[i]+1))}
gaps_intervals.append(
[int(starts[i]), int(stops[i]+1), gaps_dict])
y += 1
reference_length = int(
frame['Reference Length'][frame['Reference'] == reference].unique())
if len(gaps_intervals) == 0:
data_points = gaps_intervals
else:
# sort intervals before merging
gaps_intervals = sorted(gaps_intervals, key=lambda x: x[0])
merged_intervals = merge_intervals(gaps_intervals)
# determine missing intervals
missing_intervals = determine_missing_intervals(
merged_intervals, reference_length)
# identify start and end points for gaps subgroups
gaps_points = intervals_subgroups(merged_intervals)
# add points for intervals without gaps
for i in missing_intervals:
gaps_points[i[0]] = 0
gaps_points[i[1]] = 0
data_points = sorted(gaps_points.items(), key=lambda x: x[0])
labels = [c[0] for c in data_points]
values = [c[1] for c in data_points]
# histogram-like plot for gap counts
fig.add_trace(go.Scatter(x=labels, y=values,
mode='lines',
line=dict(color='#000000', width=0.5),
fill='tozeroy',
showlegend=False),
row=1, col=1)
# style plot
fig.update_xaxes(title_text="{} Bp".format(reference),
range=[0, reference_length], row=2, col=1)
fig.update_yaxes(type='category', tickmode='array',
tickvals=list(range(0, y)),
ticktext=assemblers_in_plot, row=2, col=1)
fig.update_layout(plot_bgcolor='rgb(255,255,255)',
xaxis=dict(showline=True, zeroline=True,
linewidth=1, linecolor='black',
gridcolor='#DCDCDC'))
fig.update_xaxes(showline=True, linewidth=1, linecolor='#DCDCDC', gridcolor='#DCDCDC')
html_filename = '{0}_{1}_misassembly.html'.format(
sample, str(reference).replace(' ', '_'))
plot(fig, filename=html_filename, auto_open=False)
plot_json = fig.to_json()
report_dict[sample]['PlotData'].setdefault(
reference, []).append(plot_json)
with open('misassembly_in_reference.json', 'w') as json_report:
json_report.write(json.dumps(report_dict, separators=(",", ":")))
if __name__ == '__main__':
main(DATAFRAME_LIST)