|
| 1 | +import os |
1 | 2 | import psycopg
|
2 |
| - |
3 | 3 | from matchms import set_matchms_logger_level, Spectrum
|
4 | 4 | import numpy as np
|
5 | 5 |
|
6 |
| -# TODO: Move either consts or postgresql url to env file |
7 |
| -DB_PORT = 5432 |
8 |
| -DB_USER = "massbank3" |
9 |
| -DB_PASSWORD = "massbank3password" |
10 |
| -DB_NAME = "massbank3" |
11 |
| -DB_HOST = "localhost" |
12 |
| - |
| 6 | +DB_PORT = os.environ.get('DB_PORT', 5432) |
| 7 | +DB_USER = os.environ.get('DB_USER', "massbank3") |
| 8 | +DB_PASSWORD = os.environ.get('DB_PASSWORD', "massbank3password") |
| 9 | +DB_HOST = os.environ.get('DB_HOST', "localhost") |
| 10 | +DB_NAME = os.environ.get('DB_NAME', "massbank3") |
13 | 11 | spectra = []
|
14 | 12 |
|
| 13 | + |
15 | 14 | # Load all (non-deprecated) spectra from the database for faster lookup
|
16 | 15 | def load_spectra():
|
17 | 16 | global spectra
|
18 |
| - |
| 17 | + |
19 | 18 | spectra = []
|
20 | 19 |
|
21 | 20 | # Prevent matchms from complaining about spectra not having a precursor_mz
|
22 | 21 | set_matchms_logger_level("ERROR")
|
23 | 22 |
|
24 | 23 | with psycopg.connect(f"postgresql://{DB_NAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}") as conn:
|
25 | 24 | with conn.cursor() as cur:
|
26 |
| - cur.execute("select document->'accession', document->'peak' from massbank where document->>'deprecated' is null;") |
| 25 | + cur.execute( |
| 26 | + "select document->'accession', document->'peak' from massbank where document->>'deprecated' is null;") |
27 | 27 |
|
28 | 28 | for spectrum in cur:
|
29 |
| - id = spectrum[0] |
| 29 | + accession = spectrum[0] |
30 | 30 | peak = spectrum[1]["peak"]
|
31 | 31 |
|
32 | 32 | mz = peak["mz"]
|
33 | 33 | intensities = peak["rel"]
|
34 | 34 |
|
35 |
| - spectra += [Spectrum(mz=np.array(mz).astype(float), intensities=np.array(intensities).astype(float), metadata={"id": id})] |
| 35 | + #metadata key "accession" gets silently converted to spectrum_id, so we can use spectrum_id right away |
| 36 | + spectra += [Spectrum(mz=np.array(mz).astype(float), intensities=np.array(intensities).astype(float), |
| 37 | + metadata={'spectrum_id': accession})] |
0 commit comments