-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmgf2svg.py
More file actions
executable file
·49 lines (38 loc) · 1.1 KB
/
mgf2svg.py
File metadata and controls
executable file
·49 lines (38 loc) · 1.1 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
#!/usr/bin/python
import sys
import matplotlib
# plot to svg
matplotlib.use('SVG')
import pylab
from pyteomics import mgf
import urllib
import tempfile
import os.path
def createSpectrumFigure(spectrum, device, title=True, color="black"):
if title:
device.title(spectrum["params"]["title"])
device.xlabel("m/z")
device.ylabel("Intensity")
device.bar(spectrum["m/z array"], spectrum["intensity array"],
width=0.5, linewidth=1, edgecolor=color)
return
def plotSingleSpectrum(filename, spectrum):
pylab.figure()
createSpectrumFigure(spectrum, pylab)
pylab.savefig(filename)
return
## main
if len(sys.argv) == 2:
## download file
filename, header = urllib.urlretrieve (sys.argv[1])
spectra = mgf.read(filename)
## we only support the first spectrum now
spectrum = next(spectra)
tmpdir = tempfile.mkdtemp()
filename = os.path.join(tmpdir, "output.svg")
plotSingleSpectrum(filename, spectrum)
## write to stdout
with open(filename, "r") as fin:
print(fin.read())
else:
print("Usage:\n\t" + sys.argv[0] + " URL")