Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify cosine computation #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions libmetgem/cosine.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ def _compare_spectra(spectrum1_mz: float, spectrum1_data: np.ndarray,
dm = spectrum1_mz - spectrum2_mz

scores = []
if dm == 0.:
for i in range(spectrum1_data.shape[0]):
for j in range(spectrum2_data.shape[0]):
diff = spectrum2_data[j, MZ] - spectrum1_data[i, MZ]
if abs(diff) <= mz_tolerance:
scores.append((spectrum1_data[i, INTENSITY] * spectrum2_data[j, INTENSITY], i, j, SpectraMatchState.fragment))
else:
for i in range(spectrum1_data.shape[0]):
for j in range(spectrum2_data.shape[0]):
diff = spectrum2_data[j, MZ] - spectrum1_data[i, MZ]
if abs(diff) <= mz_tolerance:
scores.append((spectrum1_data[i, INTENSITY] * spectrum2_data[j, INTENSITY], i, j, SpectraMatchState.fragment))
elif abs(diff + dm) <= mz_tolerance:
scores.append((spectrum1_data[i, INTENSITY] * spectrum2_data[j, INTENSITY], i, j, SpectraMatchState.neutral_loss))

for i in range(spectrum1_data.shape[0]):
for j in range(spectrum2_data.shape[0]):
diff = spectrum2_data[j, MZ] - spectrum1_data[i, MZ]
# Check if fragments match
if abs(diff) <= mz_tolerance:
scores.append((spectrum1_data[i, INTENSITY] * spectrum2_data[j, INTENSITY], i, j, SpectraMatchState.fragment))
# Check if neutral losses match
elif abs(diff + dm) <= mz_tolerance:
# Reordering the terms yields:
# diff + dm
# = spectrum2_data[j, MZ] - spectrum1_data[i, MZ] + spectrum1_mz - spectrum2_mz
# = (spectrum1_mz - spectrum1_data[i, MZ]) + (spectrum2_data[j, MZ] - spectrum2_mz)
# = neutral_loss_spectrum1 - neutral_loss_spectrum2
scores.append((spectrum1_data[i, INTENSITY] * spectrum2_data[j, INTENSITY], i, j, SpectraMatchState.neutral_loss))

if not scores:
return (0., 0, scores)
Expand Down