-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdxtool.py
More file actions
executable file
·167 lines (138 loc) · 3.6 KB
/
jdxtool.py
File metadata and controls
executable file
·167 lines (138 loc) · 3.6 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 2 19:48:37 2020
@author: shunyang
"""
import pandas as pd
import numpy as np
import os
import sys
debug = False
def cos_sim(a, b):
'''
calculate cosine similarity of two vetcors
Input
a: np.array
b: np.array
Output
res: float
'''
dot_product = np.dot(a, b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
res = dot_product / (norm_a * norm_b)
return res *1000
def dot_sim(x,y):
'''
calculate dot product of two mass spectrums
Input
a: np.array
b: np.array
Output
dot: float normalized in 1000
'''
m=0.5
n=3
#mast use different parameters!!!otherwise you will destory the initial array!!!!!!
a = np.copy(x)
b = np.copy(y)
for i in range(0, len(a)-1):
a[i] = (i**n)*(a[i]**m)
for i in range(0, len(b)-1):
b[i] = i**n*(b[i]**m)
dot = np.dot(a, b)/(np.linalg.norm(a)*np.linalg.norm(b))*1000
return dot
def readspec(x):
np.putmask(x, x >= 0.01, 1)
np.putmask(x, x < 0.01, 0)
#!!!!watch out here, you will change the original x array!!!!
return x
def peak(x,y):
'''
Parameters
----------
x : ARRAY
computational results.
y : ARRAy
experimental results.
Returns
-------
confusion_matrix
'''
x = readspec(x)
x = np.copy(2 * x)
y = readspec(y)
tmp = x-y
if debug == True:
print('x',x)
print('y',y)
print('tmp',tmp)
FP = np.count_nonzero(tmp == 2)# only in computational FalsePositive
TP = np.count_nonzero(tmp == 1)# both have TruePositive
FN = np.count_nonzero(tmp == -1)#only in experimental FalseNegative
TN = np.count_nonzero(tmp == 0)# No peak TrueNegative
return np.array([[TP, FP, FN, TN]])
def match(x, y):
'''
NIST match scores
'''
dot = dot_sim(x,y)
N_y = np.count_nonzero(y)
N = 0
SUM = 0
a = []
b = []
for i in range(1, len(y)):
if (x[i] != 0) & (y[i] != 0):
N += 1
a.append(x[i])#a is lib, while b is unknown
b.append(y[i])
for j in range(1, N):
temp = a[j]*b[j-1]/(a[j-1]*b[j])
if temp > 1:
temp = 1/temp
SUM += temp
return (N_y*dot + SUM*1000)/(N_y + N)
def readjdx(filename):
'''
read and clean computational mass spectrum result jdx file
Input
filename: str, name and path of the jdx file
Output
x: normalized mass spectrum
'''
df = pd.read_csv(filename, header=6)
df['Mass'],df['intensity']= df.iloc[:-1,0].str.split().str
df = df.drop(columns='##PEAK TABLE=(XY..XY) 1').drop(df.tail(1).index)
#get mass and intensity
mass = df.values[:,0].astype(float).astype(np.int32)
intensity = df.values[:,1].astype(np.int32)
#generate computational spec
x=np.zeros([699,])# mass from 0 to 699
for i in range(0,len(mass)-1):
x[mass[i]] = intensity[i]
print('max',np.amax(x))
x = x/np.amax(x)
if debug:
print('x norm', x)
return x
# another way to drop last column
#df = df.drop(df.columns[len(df.columns)-1],axis = 1)
def readdatabase(database):
'''
read database of experimental mass spectrum
Input
filename: str, name and path of the jdx file
Output
dataframe
'''
df = pd.read_csv(database)#mast have[0],to remove OrderedDict in header
return df
def trans(y):
y_out = []
for i in range(1, len(y)):
if y[i] !=0:
y_out.append([i,y[i]])
y_out = np.array(y_out)
return y_out