Skip to content

Commit 6575836

Browse files
committed
Update and fix GUI for databases
1 parent 437ba78 commit 6575836

File tree

8 files changed

+45
-193
lines changed

8 files changed

+45
-193
lines changed

beams/annotation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def annotate_adducts(source, db_out, ppm, lib, add=False):
306306
if isinstance(source, nx.classes.digraph.DiGraph):
307307
source = list(source.subgraph(c) for c in nx.weakly_connected_components(source))
308308

309-
if isinstance(source, list) and isinstance(source[0], nx.classes.digraph.DiGraph):
309+
if isinstance(source, list) and len(source) > 0 and isinstance(source[0], nx.classes.digraph.DiGraph):
310310
for i, graph in enumerate(source):
311311
for assignment in _annotate_pairs_from_graph(graph, lib_pairs=lib_pairs, ppm=ppm):
312312
cursor.execute("""INSERT OR REPLACE into adduct_pairs (peak_id_a, peak_id_b, label_a, label_b, ppm_error)
@@ -349,7 +349,7 @@ def annotate_isotopes(source, db_out, ppm, lib):
349349
if isinstance(source, nx.classes.digraph.DiGraph):
350350
source = list(source.subgraph(c) for c in nx.weakly_connected_components(source))
351351

352-
if isinstance(source, list) and isinstance(source[0], nx.classes.digraph.DiGraph):
352+
if isinstance(source, list) and len(source) > 0 and isinstance(source[0], nx.classes.digraph.DiGraph):
353353

354354
for graph in source:
355355

@@ -359,7 +359,7 @@ def annotate_isotopes(source, db_out, ppm, lib):
359359

360360
y = abundances[assignment["label_a"]]['abundance'] * peaklist[assignment["peak_id_b"]]["intensity"]
361361
x = abundances[assignment["label_b"]]['abundance'] * peaklist[assignment["peak_id_a"]]["intensity"]
362-
362+
363363
if x == 0.0 or y == 0.0:
364364
atoms = None
365365
else:
@@ -412,7 +412,7 @@ def annotate_oligomers(source, db_out, ppm, lib, maximum=2):
412412
if isinstance(source, nx.classes.digraph.DiGraph):
413413
source = list(source.subgraph(c) for c in nx.weakly_connected_components(source))
414414

415-
if isinstance(source, list) and isinstance(source[0], nx.classes.digraph.DiGraph):
415+
if isinstance(source, list) and len(source) > 0 and isinstance(source[0], nx.classes.digraph.DiGraph):
416416

417417
for graph in source:
418418

@@ -679,7 +679,7 @@ def annotate_compounds(peaklist, lib_adducts, ppm, db_out, db_name, db_in=""):
679679
conn_local = sqlite3.connect(os.path.join(path_dbs, db_local))
680680
cursor_local = conn_local.cursor()
681681
cursor_local.execute("SELECT name FROM sqlite_master WHERE type='table'")
682-
if (db_name, ) not in cursor_local.fetchall():
682+
if (db_name.strip(".sqlite"), ) not in cursor_local.fetchall():
683683
raise ValueError("Database {} not available".format(db_name))
684684
break
685685
if conn_local is None:

beams/grouping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def group_features(df, db_out, max_rt_diff=5.0, coeff_thres=0.7, pvalue_thres=1.
2929
graph = statistics.correlation_graphs(df_coeffs, df)
3030
sub_graphs = list(graph.subgraph(c) for c in nx.weakly_connected_components(graph))
3131
for i in range(len(sub_graphs)):
32-
sub_graphs[i].graph["group_id"] = i + 1 # not stored in output - place holder
32+
sub_graphs[i].graph["groupid"] = i + 1 # not stored in output - place holder
3333
sub_graph_edges = []
3434
# sort edges
3535
edges = sorted(sub_graphs[i].edges(data=True), key=lambda e: (e[0], e[1]))

beams/gui.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from PyQt5 import QtCore, QtGui, QtWidgets
1111
from beams.qt import form
1212
import sqlite3
13+
from collections import OrderedDict
1314

1415

1516
class BeamsApp(QtWidgets.QMainWindow, form.Ui_MainWindow):
@@ -51,7 +52,7 @@ def __init__(self, parent=None):
5152
self.checkBox_mz_digits.clicked.connect(self.create_summary)
5253
self.checkBox_convert_rt.clicked.connect(self.create_summary)
5354

54-
self.add_databases()
55+
self.db_names = self.add_databases()
5556

5657
self.pushButton_start.clicked.connect(self.run) # When the button is pressed
5758

@@ -134,15 +135,15 @@ def source_peak_patterns(self):
134135
def source_compounds(self):
135136
if self.checkBox_filename_reference.isChecked():
136137
self.listWidget_databases.setEnabled(False)
137-
self.listWidget_categories.setEnabled(False)
138+
# self.listWidget_categories.setEnabled(False)
138139
self.label_databases.setEnabled(False)
139140
self.pushButton_filename_reference.setEnabled(True)
140141
self.lineEdit_filename_reference.setEnabled(True)
141142
else:
142143
self.label_databases.setEnabled(True)
143144
self.listWidget_databases.setEnabled(True)
144-
self.label_categories.setEnabled(False)
145-
self.listWidget_categories.setEnabled(False)
145+
# self.label_categories.setEnabled(False)
146+
# self.listWidget_categories.setEnabled(False)
146147
self.pushButton_filename_reference.setEnabled(False)
147148
self.lineEdit_filename_reference.setEnabled(False)
148149

@@ -208,7 +209,7 @@ def annotate_molecular_formulae(self):
208209
def annotate_compounds(self):
209210
if not self.checkBox_annotate_compounds.isChecked():
210211
self.listWidget_databases.setEnabled(False)
211-
self.listWidget_categories.setEnabled(False)
212+
# self.listWidget_categories.setEnabled(False)
212213
self.label_databases.setEnabled(False)
213214
self.checkBox_filename_reference.setEnabled(False)
214215
self.pushButton_filename_reference.setEnabled(False)
@@ -249,21 +250,29 @@ def create_summary(self):
249250
self.spinBox_mz_digits.setEnabled(False)
250251

251252
def add_databases(self):
252-
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
253-
dbs = [fn for fn in os.listdir(path) if fn.endswith('.sqlite')]
254-
conn = sqlite3.connect(os.path.join(path, dbs[0]))
253+
254+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'databases')
255+
256+
list_databases = OrderedDict()
257+
with open(os.path.join(path, "compound_databases.txt"), "r") as inp:
258+
keys = inp.readline().strip().split("\t")
259+
for line in inp:
260+
line = line.split("\t")
261+
if os.path.isfile(os.path.join(path, line[keys.index("filename_sqlite")])):
262+
list_databases[line[keys.index("filename_sqlite")]] = line[keys.index("description")]
263+
conn = sqlite3.connect(os.path.join(path, list_databases.keys()[0]))
255264
cursor = conn.cursor()
256265
cursor.execute("""SELECT name FROM sqlite_master where type='table'""")
257266
db_names = [str(name[0]) for name in cursor.fetchall()]
258-
for i, db in enumerate(dbs[1:]):
267+
for i, db in enumerate(list_databases.keys()[1:]):
259268
cursor.execute("ATTACH DATABASE ? AS db?", (os.path.join(path, dbs[i+1]), i, ))
260269
cursor.execute("""SELECT name FROM db?.sqlite_master where type='table'""", (i, ))
261270
db_names.extend([str(name[0]) for name in cursor.fetchall()])
262271
self.listWidget_databases.setSelectionMode(QtWidgets.QListWidget.MultiSelection)
263-
for db_name in db_names:
264-
item = QtWidgets.QListWidgetItem(db_name)
272+
for description in list_databases.values():
273+
item = QtWidgets.QListWidgetItem(description)
265274
self.listWidget_databases.addItem(item)
266-
return db_names
275+
return dict((v, k) for k, v in list_databases.iteritems())
267276

268277
def run(self):
269278

@@ -331,6 +340,7 @@ def run(self):
331340
QtWidgets.QMessageBox.critical(None, "Select file", "Provide a valid filename for adducts", QtWidgets.QMessageBox.Ok)
332341
else:
333342
QtWidgets.QMessageBox.critical(None, "Select file", "Provide a valid filename for adducts or 'Use default'", QtWidgets.QMessageBox.Ok)
343+
334344
annotation.annotate_adducts(inp, db_out=self.lineEdit_sql_database.text(), ppm=self.doubleSpinBox_ppm_error.value(), lib=lib)
335345
print("Done")
336346

@@ -435,13 +445,12 @@ def run(self):
435445
QtWidgets.QMessageBox.critical(None, "Select file", "Provide a valid filename for adducts", QtWidgets.QMessageBox.Ok)
436446

437447
if self.checkBox_filename_reference.isChecked():
438-
annotation.annotate_compounds(df, lib_adducts=lib, ppm=self.doubleSpinBox_ppm_error.value(), db_out=self.lineEdit_sql_database.text(), db_in=self.lineEdit_filename_reference.text(), db_name=None)
448+
annotation.annotate_compounds(df, lib_adducts=lib, ppm=self.doubleSpinBox_ppm_error.value(),
449+
db_out=self.lineEdit_sql_database.text(), db_name=None, db_in=self.lineEdit_filename_reference.text())
439450
else:
440-
path = 'data/BEAMS_DB.sqlite'
441-
path_db = os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
442-
print(path_db)
443451
for db_name in self.listWidget_databases.selectedItems():
444-
annotation.annotate_compounds(df, lib_adducts=lib, ppm=self.doubleSpinBox_ppm_error.value(), db_out=self.lineEdit_sql_database.text(), db_in=path_db, db_name=db_name.text())
452+
annotation.annotate_compounds(df, lib_adducts=lib, ppm=self.doubleSpinBox_ppm_error.value(),
453+
db_out=self.lineEdit_sql_database.text(), db_name=self.db_names[db_name.text()].strip(".sqlite"))
445454
print("Done")
446455
print
447456

beams/qt/databases.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

beams/qt/databases.ui

Lines changed: 0 additions & 64 deletions
This file was deleted.

beams/qt/form.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Form implementation generated from reading ui file 'form.ui'
44
#
5-
# Created by: PyQt5 UI code generator 5.10.1
5+
# Created by: PyQt5 UI code generator 5.10
66
#
77
# WARNING! All changes made in this file will be lost!
88

@@ -12,7 +12,7 @@ class Ui_MainWindow(object):
1212
def setupUi(self, MainWindow):
1313
MainWindow.setObjectName("MainWindow")
1414
MainWindow.setEnabled(True)
15-
MainWindow.resize(795, 842)
15+
MainWindow.resize(795, 835)
1616
MainWindow.setAnimated(False)
1717
self.centralwidget = QtWidgets.QWidget(MainWindow)
1818
self.centralwidget.setObjectName("centralwidget")
@@ -331,34 +331,26 @@ def setupUi(self, MainWindow):
331331
self.checkBox_annotate_compounds.setObjectName("checkBox_annotate_compounds")
332332
self.lineEdit_filename_reference = QtWidgets.QLineEdit(self.groupBox_annotate_compounds)
333333
self.lineEdit_filename_reference.setEnabled(False)
334-
self.lineEdit_filename_reference.setGeometry(QtCore.QRect(410, 59, 161, 20))
334+
self.lineEdit_filename_reference.setGeometry(QtCore.QRect(410, 70, 161, 20))
335335
self.lineEdit_filename_reference.setText("")
336336
self.lineEdit_filename_reference.setReadOnly(True)
337337
self.lineEdit_filename_reference.setObjectName("lineEdit_filename_reference")
338338
self.pushButton_filename_reference = QtWidgets.QPushButton(self.groupBox_annotate_compounds)
339339
self.pushButton_filename_reference.setEnabled(False)
340-
self.pushButton_filename_reference.setGeometry(QtCore.QRect(580, 57, 71, 23))
340+
self.pushButton_filename_reference.setGeometry(QtCore.QRect(580, 69, 71, 23))
341341
self.pushButton_filename_reference.setMinimumSize(QtCore.QSize(63, 23))
342342
self.pushButton_filename_reference.setObjectName("pushButton_filename_reference")
343343
self.checkBox_filename_reference = QtWidgets.QCheckBox(self.groupBox_annotate_compounds)
344344
self.checkBox_filename_reference.setEnabled(True)
345-
self.checkBox_filename_reference.setGeometry(QtCore.QRect(410, 37, 251, 17))
345+
self.checkBox_filename_reference.setGeometry(QtCore.QRect(410, 50, 251, 17))
346346
self.checkBox_filename_reference.setChecked(False)
347347
self.checkBox_filename_reference.setObjectName("checkBox_filename_reference")
348348
self.listWidget_databases = QtWidgets.QListWidget(self.groupBox_annotate_compounds)
349-
self.listWidget_databases.setGeometry(QtCore.QRect(10, 50, 171, 91))
349+
self.listWidget_databases.setGeometry(QtCore.QRect(10, 50, 381, 91))
350350
self.listWidget_databases.setObjectName("listWidget_databases")
351-
self.listWidget_categories = QtWidgets.QListWidget(self.groupBox_annotate_compounds)
352-
self.listWidget_categories.setEnabled(False)
353-
self.listWidget_categories.setGeometry(QtCore.QRect(200, 50, 171, 91))
354-
self.listWidget_categories.setObjectName("listWidget_categories")
355351
self.label_databases = QtWidgets.QLabel(self.groupBox_annotate_compounds)
356352
self.label_databases.setGeometry(QtCore.QRect(10, 30, 71, 16))
357353
self.label_databases.setObjectName("label_databases")
358-
self.label_categories = QtWidgets.QLabel(self.groupBox_annotate_compounds)
359-
self.label_categories.setEnabled(False)
360-
self.label_categories.setGeometry(QtCore.QRect(200, 30, 71, 16))
361-
self.label_categories.setObjectName("label_categories")
362354
self.groupBox_create_summary = QtWidgets.QGroupBox(self.centralwidget)
363355
self.groupBox_create_summary.setGeometry(QtCore.QRect(20, 657, 761, 101))
364356
self.groupBox_create_summary.setTitle("")
@@ -443,7 +435,7 @@ def setupUi(self, MainWindow):
443435
self.checkBox_convert_rt.raise_()
444436
MainWindow.setCentralWidget(self.centralwidget)
445437
self.menubar = QtWidgets.QMenuBar(MainWindow)
446-
self.menubar.setGeometry(QtCore.QRect(0, 0, 795, 21))
438+
self.menubar.setGeometry(QtCore.QRect(0, 0, 795, 22))
447439
self.menubar.setObjectName("menubar")
448440
self.menuAdd_example_data = QtWidgets.QMenu(self.menubar)
449441
self.menuAdd_example_data.setObjectName("menuAdd_example_data")
@@ -514,7 +506,6 @@ def retranslateUi(self, MainWindow):
514506
self.pushButton_filename_reference.setText(_translate("MainWindow", "Browse..."))
515507
self.checkBox_filename_reference.setText(_translate("MainWindow", "Reference file"))
516508
self.label_databases.setText(_translate("MainWindow", "Databases"))
517-
self.label_categories.setText(_translate("MainWindow", "Categories"))
518509
self.pushButton_summary_filename.setText(_translate("MainWindow", "Save as..."))
519510
self.label_summary_filename.setText(_translate("MainWindow", "Summary:"))
520511
self.checkBox_mz_digits.setText(_translate("MainWindow", "Number of digits m/z:"))

0 commit comments

Comments
 (0)