-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeisotope.py
62 lines (52 loc) · 1.5 KB
/
deisotope.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
#!/usr/bin/env python
"""
Author: Junda Huang
Script to reduce isotope from mgf spectra
Command line input: python3 spec.mgf deiso.mgf isotope_limit
"""
# import statement
from sys import argv
# functions
def parser(mgf):
specs = {}
with open(mgf, 'r') as f:
for i, line in enumerate(f):
items = line.strip('\n').split(' ')
if items[0] == 'BEGIN':
specs[i] = []
specs[i].append(line)
key = i
else:
specs[key].append(line)
return specs
def deisotop(dic, isotope_limit):
for key, spec in dic.items():
peaks = []
pop = []
for line in spec:
if ((line.split(' '))[0][0]).isdigit():
peaks.append(line)
for peak1 in peaks:
mz1 = float(peak1.split(' ')[0])
intens1 = float(peak1.split(' ')[1])
for peak2 in peaks:
if abs(mz1 - float(peak2.split(' ')[0])) <= isotope_limit \
and intens1 > float(peak2.split(' ')[1]):
pop.append(peak2)
pop = set(pop)
for i in pop:
spec.remove(i)
return dic
def write_mgf(dic, out):
with open(out, 'w') as f:
for k, v in dic.items():
for j in v:
f.write(j)
return
# main
if __name__ == '__main__':
# parsing files from command line
originmgf = argv[1]
newmgf = argv[2]
dic = deisotop(parser(originmgf), argv[3])
write_mgf(dic, newmgf)