-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path4-InteractivePlotter.py
328 lines (279 loc) · 14.7 KB
/
4-InteractivePlotter.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 28 10:48:04 2016
@author: Will Kew
Copyright Will Kew, 2016
This file is part of FTMS Visualisation (also known as i-van Krevelen).
FTMS Visualisation is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FTMS Visualisation is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FTMS Visualisation. If not, see <http://www.gnu.org/licenses/>.
This script, like 2-StaticPlotter, reads in a pre-formatted CSV to produce scatter plots.
However, this one is designed to work on single files, and produces an HTML file with interactive graphics in it, allowing for better understanding of the data.
This functionality is novel and detailed in a recent letter (in preparation).
This script relies on the bokeh package
http://bokeh.pydata.org/en/latest/
https://github.com/bokeh/bokeh
"""
from __future__ import print_function # Python 2 compatibility
from __future__ import absolute_import # Python 2 compatibility
import os, sys
#import numpy as np
#import pandas as pd
#import matplotlib
#from matplotlib import cm
from bokeh.embed import file_html
from bokeh.util.browser import view
from bokeh.plotting import figure
from bokeh.events import ButtonClick
from bokeh.models import HoverTool, ColumnDataSource, OpenURL, TapTool, layouts, CustomJS, PrintfTickFormatter, ColorBar, LinearColorMapper, FixedTicker
from bokeh.models.widgets import Panel, Tabs, Button, DataTable, TableColumn, NumberFormatter#, Dropdown
#from bokeh.palettes import Viridis10, Inferno8
import bokeh.palettes as bp
from bokeh.resources import CDN #,JSResources,INLINE
from bokeh.io import reset_output
from jinja2 import Template
from collections import OrderedDict
"""
# We import also the FTMSVizProcessingModule which contains a few useful functions.
# here we define where the scripts are stored.
# Make sure to change this to where you have saved these scripts.
"""
try: #test if running in ipython
__IPYTHON__
except NameError: #if not running in ipython....
import FTMSVizProcessingModule as FTPM
path = os.getcwd()+"data\\" #example data location
else: #if running in ipython
scriptlocation = "/LOCAL/FTMSVis/FTMSVisualization-master/"
sys.path.append(scriptlocation)
import FTMSVizProcessingModule as FTPM
path = "/LOCAL/FTMSVis/data/"
#define the colourmap of choice
glocmap = list(bp.Viridis10)
glocmap2 = list(bp.Inferno8)
glocmap.reverse()
glocmap2.reverse()
#path = "G:/DATA/FTICRMS/KEW-20160319/data/PORg CSV/"
inputpath = path +"OutputCSV/"
outputpath = path+"OutputHTML/"
FTPM.make_sure_path_exists(outputpath)
files = os.listdir(inputpath)
def intfileplot():
filesA = os.listdir(inputpath)
filesB = []
for y in filesA:
if y[-8:] =="hits.csv" and y[-10:] != "nohits.csv" and y[-11:] !="isohits.csv":
filesB.append(y)
for y in filesB:
data,hetclassintdf = FTPM.mycsvreader(inputpath+y)
isodata = FTPM.isocsvreader(inputpath+y[:-8]+"isohits.csv")
nodata = FTPM.nohitsreader(inputpath+y[:-8]+"nohits.csv")
intplotter(data,isodata,nodata,y,hetclassintdf)
reset_output() #cleans up the cache which reduces file size
def intplotter(data,isodata,nodata,y,hetclassintdf):
linewidth = 1.5
source = ColumnDataSource(data)
s2 = ColumnDataSource(data=dict(mz=data["mz"],Error=data["Error"],RA=data["RA"],
Formula=data["Formula"],HeteroClass=data["HeteroClass"]))
isosource = ColumnDataSource(isodata)
nosource = ColumnDataSource(nodata)
url = "http://www.chemspider.com/Search.aspx?q=@Formula"
TOOLS="crosshair,pan,wheel_zoom,box_zoom,reset,tap,save,box_select,poly_select,lasso_select,hover"
figdims = (900,500) #pixel dimensions for the normal figures
msxlim = [200,700] #x limits in m/z for the mass spectra
vkxlim = [0,1]
vkylim = [0,2]
p1 = figure(tools=TOOLS, title=y[:-9]+" - Van Krevelen",width=figdims[0], height=figdims[1],
x_axis_label='O/C',y_axis_label='H/C',x_range=vkxlim,y_range=vkylim)
color_mapper = LinearColorMapper(palette=glocmap, low=msxlim[0], high=msxlim[1])
p1.scatter(x='OC', y='HC',source=source,size='VKsize', fill_color={'field': 'mz', 'transform': color_mapper},
fill_alpha=0.75,line_color=None) #use size not radius.
hover = p1.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('Formula', "@Formula"),('Mass',"@mz{1.11111}"),('Error (ppm)',"@Error{1.11}")])
taptool = p1.select(type=TapTool)
taptool.callback = OpenURL(url=url)
color_bar = ColorBar(color_mapper=color_mapper, title="m/z", border_line_color=None, location=(0,0), scale_alpha=0.7)
#orientation='horizontal',location='top_left', scale_alpha=0.7)#,ticker=FixedTicker(ticks=[2,6,10,14,18]))
p1.add_layout(color_bar,"right")
dbexlim = [0,45]
dbeylim = [0,40]
cmax = max(data["O"])
cmax = int(5 * round(float(cmax)/5))
p2 = figure(tools=TOOLS, title=y[:-9]+" - DBE vs C# Plot",width=figdims[0],
height=figdims[1], x_axis_label='C#',y_axis_label='DBE',x_range=dbexlim,y_range=dbeylim)
color_mapper2 = LinearColorMapper(palette=glocmap2, low=0, high=cmax)
p2.scatter(x='C', y='DBE',source=source,size='VKsize', fill_color={'field': 'O', 'transform': color_mapper2},
fill_alpha=0.75,line_color=None)
hover = p2.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('Formula', "@Formula"),('Mass',"@mz{1.11111}"),('Error (ppm)',"@Error{1.11}")])
taptool = p2.select(type=TapTool)
taptool.callback = OpenURL(url=url)
color_bar2 = ColorBar(color_mapper=color_mapper2, title="O#", border_line_color=None, location=(0,0),
scale_alpha=0.7,ticker=FixedTicker(ticks=[0,int(cmax/4),int(cmax/2),int(3*cmax/4),cmax]))
p2.add_layout(color_bar2,"right")
aixlim=[0,45]
aiylim= [0,1]
p3 = figure(tools=TOOLS, title=y[:-9]+" - AI(mod) vs C# Plot",width=figdims[0],
height=figdims[1], x_axis_label='C#',y_axis_label='AI(mod)',x_range=aixlim,y_range=aiylim)
color_mapper3 = LinearColorMapper(palette=glocmap2, low=0, high=cmax)
p3.scatter(x='C', y='AImod',source=source,size='VKsize', fill_color={'field': 'O', 'transform': color_mapper3},
fill_alpha=0.75,line_color=None)
hover = p3.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('Formula', "@Formula"),('Mass',"@mz{1.11111}"),('Error (ppm)',"@Error{1.11}")])
taptool = p3.select(type=TapTool)
taptool.callback = OpenURL(url=url)
color_bar3 = ColorBar(color_mapper=color_mapper3,title="O#", border_line_color=None, location=(0,0), scale_alpha=0.7,ticker=FixedTicker(ticks=[0,int(cmax/4),int(cmax/2),int(3*cmax/4),cmax]))
#orientation='horizontal',location='top_left', scale_alpha=0.7)#,ticker=FixedTicker(ticks=[2,6,10,14,18]))
p3.add_layout(color_bar3,"right")
p4 = figure(tools=TOOLS, title=y[:-9]+" - Assigned Centroid MS",width=figdims[0],
height=figdims[1], x_axis_label='m/z',y_axis_label='Abundance',y_range=[min(data["RA"]),max(data["RA"])],
x_range=msxlim)
p4.segment(x0=0,x1=800,y0=0,y1=0,line_width=1, line_color="black")
p4.segment(x0='mz',y0=0,x1='mz',y1='RA',source=source,line_width=linewidth, line_color="black")
p4.scatter(x='mz', y='RA',source=source,fill_color='black',line_color=None)
hover = p4.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('Formula', "@Formula"),('Mass',"@mz{1.11111}"),('Error (ppm)',"@Error{1.11}")])
taptool = p4.select(type=TapTool)
taptool.callback = OpenURL(url=url)
p4.yaxis[0].formatter = PrintfTickFormatter(format="%4.1e")
"""
#this is me trying to plot a barplot of heteroatomic class distributions...
p7 = figure(tools=TOOLS, title=y[:-9]+"",width=800, height=600, x_axis_label='HeteroClass',y_axis_label='Count',webgl=True)
p7.quad(left="HetClassInts",y=hetclassdf[0],source=source,width=5,height=)
t7 = layouts.Column(hist)
tab7 = Panel(child=t7,title="test")
"""
stretch = msxlim[0]*0.1
p5 = figure(tools=TOOLS, title=y[:-9]+" - Assigned Centroid MS",width=1400, height=600,
x_axis_label='m/z',y_axis_label='Abundance', y_range=[min(data["RA"]),max(data["RA"])],
x_range=(msxlim[0]-stretch,msxlim[1]+stretch))
p5.segment(x0=0,x1=800,y0=0,y1=0,line_width=1, line_color="black")
no1 =p5.segment(x0='mz',y0=0,x1='mz',y1='RA',source=nosource,line_width=linewidth, line_color="red",legend_label="Unassigned Peaks")
#no2 =p5.scatter(x='mz', y='RA',source=nosource,fill_color='red',line_color=None,legend_label="Unassigned Peaks")
#p5.scatter(x='mz', y='RA',source=source,fill_color='black',line_color=None,legend_label="Assigned Peaks")
p5.segment(x0='mz',y0=0,x1='mz',y1='RA',source=source,line_width=linewidth, line_color="black",legend_label="Assigned Peaks")
iso1 =p5.segment(x0='mz',y0=0,x1='mz',y1='RA',source=isosource,line_width=linewidth, line_color="green",legend_label="Isotologue Peaks")
#iso2 =p5.scatter(x='mz', y='RA',source=isosource,fill_color='green',line_color=None, legend_label="Isotologue Peaks")
hover = p5.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('Formula', "@Formula"),('Mass',"@mz{1.11111}"),('Error (ppm)',"@Error{1.11}")])
taptool = p5.select(type=TapTool)
taptool.callback = OpenURL(url=url)
p5.yaxis[0].formatter = PrintfTickFormatter(format="%4.1e")
#Bokeh 2 doesnt need JS buttons thanks to interactive legends
#js_code1 = "iso1.glyph.visible = false; iso2.glyph.visible = false; no1.glyph.visible = false; no2.glyph.visible = false;"
#cb1 = CustomJS(code=js_code1, args=dict(iso1=iso1,iso2=iso2,no1=no1,no2=no2))
#js_code2 = "iso1.glyph.visible = true; iso2.glyph.visible = true; no1.glyph.visible = true; no2.glyph.visible = true;"
#cb2 = CustomJS(code=js_code2, args=dict(iso1=iso1,iso2=iso2,no1=no1,no2=no2))
#toggleOn = Button(label="Hide", button_type="success")
#toggleOn.js_on_click(cb1)
#toggleOff = Button(label="Show", button_type="success")
#toggleOn.js_on_click(cb2)
#top = layouts.Row(toggleOn,toggleOff)
t3 = layouts.Column(p5)
p5.legend.location = "top_left"
p5.legend.click_policy="hide"
tab3 = Panel(child=t3,title="Centroid MS with Isotopomers and No Hits")
downloadbutton = Button(label="Download", button_type="success")
cb3 = CustomJS(args=dict(s2=s2), code="""
var data = s2.get('data');
var filetext = 'mz,Error,RA,Formula,HeteroClass\\n';
for (i=0; i < data['mz'].length; i++) {
var currRow = [data['mz'][i].toString(),
data['Error'][i].toString(),
data['RA'][i].toString(),
data['Formula'][i].toString(),
data['HeteroClass'][i].toString().concat('\\n')];
var joined = currRow.join();
filetext = filetext.concat(joined);
}
var filename = 'data_result.csv';
var blob = new Blob([filetext], { type: 'text/csv;charset=utf-8;' });
//addresses IE
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename);
}
else {
var link = document.createElement("a");
link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
link.target = "_blank";
link.style.visibility = 'hidden';
link.dispatchEvent(new MouseEvent('click'))
}
""")
downloadbutton.js_on_event(ButtonClick,cb3)
columns = [TableColumn(field="mz",title="m/z",formatter=NumberFormatter(format="0.00000")),
TableColumn(field="Error", title="Error (ppm)",formatter=NumberFormatter(format="0.00")),
TableColumn(field="RA",title="Abundance"),
TableColumn(field="Formula",title="Formula"),
TableColumn(field="HeteroClass",title="Heteroatomic Class")]
data_table = DataTable(source=s2, columns=columns, width=1400,header_row=False,fit_columns=True)
t4 = layouts.Column(data_table,downloadbutton)
tab4=Panel(child=t4,title="Selected Data Table")
cb4 = CustomJS(args=dict(s2=s2,dt=data_table), code="""
var inds = cb_obj.get('selected')['1d'].indices;
var d1 = cb_obj.get('data');
var d2 = s2.get('data');
if (inds.length == 0) {
d2['mz'] = d1['mz']
d2['Error'] = d1['Error']
d2['RA'] = d1['RA']
d2['Formula'] = d1['Formula']
d2['HeteroClass'] = d1['HeteroClass']
}
else if (inds.length != 0) {
d2['mz'] = []
d2['Error'] = []
d2['RA'] = []
d2['Formula'] = []
d2['HeteroClass'] = []
for (i = 0; i < inds.length; i++) {
d2['mz'].push(d1['mz'][inds[i]])
d2['Error'].push(d1['Error'][inds[i]])
d2['RA'].push(d1['RA'][inds[i]])
d2['Formula'].push(d1['Formula'][inds[i]])
d2['HeteroClass'].push(d1['HeteroClass'][inds[i]])
}
}
s2.trigger('change');
dt.trigger('change');
""")
source.selected.js_on_change('indices', cb4)
"""
hetclasslist = hetclassintdf["HetClass"].tolist()
hetclasslistnew = []
for x in hetclasslist:
hetclasslistnew.append([x,x])
dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=hetclasslistnew)
"""
t1 = layouts.Row(p1,p4)
t2 = layouts.Row(p2,p3)
t12 = layouts.Column(t1,t2)
tab1 = Panel(child=t12, title="Main")
tabs = Tabs(tabs=[ tab1, tab3, tab4])
for figs in [p1,p2,p3,p4,p5]:
figs.xaxis.axis_label_text_font_size = "14pt"
figs.xaxis.major_label_text_font_size = "14pt"
figs.yaxis.axis_label_text_font_size = "14pt"
figs.yaxis.major_label_text_font_size = "14pt"
figs.title.text_font_size = "14pt"
figs.toolbar_location = "above"
with open(outputpath+'templates/index.html', 'r') as f:
template = Template(f.read())
#js_resources = JSResources(mode='inline')
html = file_html(tabs,(CDN,CDN),"Interactive Van Krevelen Diagrams",template=template)
output_file2 = outputpath+y[:-9]+'-plot.html'
with open(output_file2, 'w') as f:
f.write(html)
view(output_file2)
#show(tabs) # open a browser
intfileplot()