diff --git a/src/maven/database.cpp b/src/maven/database.cpp index c1caa364..54b0a48d 100644 --- a/src/maven/database.cpp +++ b/src/maven/database.cpp @@ -21,7 +21,7 @@ void Database::reloadAll() { const std::string EmptyString; compoundIdMap.clear(); compoundsDB.clear(); - loadCompoundsSQL("ALL"); + loadCompoundsSQL("ALL",ligandDB); cerr << "compoundsDB=" << compoundsDB.size() << " " << compoundIdMap.size() << endl; cerr << "adductsDB=" << adductsDB.size() << endl; @@ -55,8 +55,8 @@ int Database::loadCompoundsFile(QString filename) { compounds = loadCompoundCSVFile(filename); } - deleteCompoundsSQL(dbname.c_str()); - saveCompoundsSQL(compounds); + deleteCompoundsSQL(dbname.c_str(),ligandDB); + saveCompoundsSQL(compounds,ligandDB); sort(compoundsDB.begin(),compoundsDB.end(), Compound::compMass); return compounds.size(); @@ -93,14 +93,14 @@ void Database::addCompound(Compound* c) { compoundsDB.push_back(c); } -void Database::loadCompoundsSQL(QString databaseName) { +void Database::loadCompoundsSQL(QString databaseName, QSqlDatabase &dbConnection) { if (loadedDatabase.count(databaseName)) { qDebug() << databaseName << "already loaded"; return; } - QSqlQuery query(ligandDB); + QSqlQuery query(dbConnection); QString sql = "select * from compounds where name not like '%DECOY%'"; if (databaseName != "ALL") sql += " and dbName='" + databaseName + "'"; @@ -197,7 +197,7 @@ Adduct* Database::findAdductByName(string id) { Compound* Database::findSpeciesById(string id,string db) { - if (!loadedDatabase.count(db.c_str())) loadCompoundsSQL(db.c_str()); + if (!loadedDatabase.count(db.c_str())) loadCompoundsSQL(db.c_str(),ligandDB); //cerr << "searching for " << id << " " << compoundIdMap.size() << " " << db << endl; if ( compoundIdMap.contains(id + db) ) return compoundIdMap[id + db]; @@ -215,7 +215,7 @@ Compound* Database::findSpeciesById(string id,string db) { vector Database::findSpeciesByName(string name, string dbname) { - if (!loadedDatabase.count(dbname.c_str())) loadCompoundsSQL(dbname.c_str()); + if (!loadedDatabase.count(dbname.c_str())) loadCompoundsSQL(dbname.c_str(),ligandDB); vector set; qDebug() << "findSpeciesByName" << name.c_str(); @@ -634,17 +634,17 @@ vector Database::loadNISTLibrary(QString fileName) { return compoundSet; } -void Database::deleteAllCompoundsSQL() { - QSqlQuery query(ligandDB); +void Database::deleteAllCompoundsSQL(QSqlDatabase& dbConnection) { + QSqlQuery query(dbConnection); query.prepare("drop table compounds"); if (!query.exec()) qDebug() << query.lastError(); - ligandDB.commit(); + dbConnection.commit(); qDebug() << "deleteAllCompounds: done"; } -void Database::deleteCompoundsSQL(QString dbName) { - QSqlQuery query(ligandDB); +void Database::deleteCompoundsSQL(QString dbName, QSqlDatabase& dbConnection) { + QSqlQuery query(dbConnection); //create index based on database name. query.exec("create index if not exists compound_db_idx on compounds(dbName)"); @@ -652,14 +652,15 @@ void Database::deleteCompoundsSQL(QString dbName) { query.prepare("delete from compounds where dbName = ?"); query.bindValue( 0, dbName ); if (!query.exec()) qDebug() << query.lastError(); - ligandDB.commit(); + dbConnection.commit(); qDebug() << "deleteCompoundsSQL" << dbName << " " << query.numRowsAffected(); } -void Database::saveCompoundsSQL(vector &compoundSet) { +void Database::saveCompoundsSQL(vector &compoundSet, QSqlDatabase& dbConnection) { + qDebug() << "saveCompoundsSQL()" << compoundSet.size(); - QSqlQuery query0(ligandDB); + QSqlQuery query0(dbConnection); query0.exec("begin transaction"); if(!query0.exec("create table IF NOT EXISTS compounds(\ cid integer primary key AUTOINCREMENT,\ @@ -685,7 +686,7 @@ void Database::saveCompoundsSQL(vector &compoundSet) { query0.exec("create index if not exists compound_db_idx on compounds(dbName)"); - QSqlQuery query1(ligandDB); + QSqlQuery query1(dbConnection); query1.prepare("insert into compounds values(NULL, ?,?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?)"); for(Compound* c : compoundSet) { diff --git a/src/maven/database.h b/src/maven/database.h index 40a493a2..25c101d4 100644 --- a/src/maven/database.h +++ b/src/maven/database.h @@ -40,10 +40,11 @@ class Database { int loadCompoundsFile(QString filename); vector loadCompoundCSVFile(QString fileName); vector loadNISTLibrary(QString fileName); - void loadCompoundsSQL(QString databaseName); - void saveCompoundsSQL(vector &compoundSet); - void deleteCompoundsSQL( QString dbName); - void deleteAllCompoundsSQL(); + + void loadCompoundsSQL(QString databaseName,QSqlDatabase& dbConnection); + void saveCompoundsSQL(vector &compoundSet, QSqlDatabase& dbConnection); + void deleteCompoundsSQL(QString dbName, QSqlDatabase& dbConnection); + void deleteAllCompoundsSQL(QSqlDatabase& dbConnection); multimap keywordSearch(string needle); vector getCompoundReactions(string compound_id); diff --git a/src/maven/librarydialog.cpp b/src/maven/librarydialog.cpp index 0d6a5635..6535be0f 100644 --- a/src/maven/librarydialog.cpp +++ b/src/maven/librarydialog.cpp @@ -35,7 +35,7 @@ void LibraryMangerDialog::loadLibrary() { foreach(QTreeWidgetItem* item, treeWidget->selectedItems() ) { QString libraryName = item->text(0); qDebug() << "Load Library" << libraryName; - DB.loadCompoundsSQL(libraryName); + DB.loadCompoundsSQL(libraryName,DB.getLigandDB()); } updateLibraryStats(); } @@ -45,7 +45,7 @@ void LibraryMangerDialog::deleteLibrary() { foreach(QTreeWidgetItem* item, treeWidget->selectedItems() ) { QString libraryName = item->text(0); qDebug() << "Delete Library" << libraryName; - DB.deleteCompoundsSQL(libraryName); + DB.deleteCompoundsSQL(libraryName,DB.getLigandDB()); } updateLibraryStats(); } @@ -55,7 +55,7 @@ void LibraryMangerDialog::loadCompoundsFile(QString filename){ string dbname = mzUtils::cleanFilename(dbfilename); int compoundCount = DB.loadCompoundsFile(filename); - DB.loadCompoundsSQL(dbname.c_str()); + DB.loadCompoundsSQL(dbname.c_str(),DB.getLigandDB()); if (compoundCount > 0 && mainwindow->ligandWidget) { mainwindow->ligandWidget->updateDatabaseList(); @@ -92,7 +92,7 @@ void LibraryMangerDialog::reloadMethodsFolder() { methodsFolder = QFileDialog::getExistingDirectory(this,methodsFolder); if (not methodsFolder.isEmpty()) { - DB.deleteAllCompoundsSQL(); + DB.deleteAllCompoundsSQL(DB.getLigandDB()); DB.loadMethodsFolder(methodsFolder); mainwindow->ligandWidget->updateDatabaseList(); mainwindow->massCalcWidget->updateDatabaseList(); diff --git a/src/maven/ligandwidget.cpp b/src/maven/ligandwidget.cpp index c2a384c6..e68eb9f9 100644 --- a/src/maven/ligandwidget.cpp +++ b/src/maven/ligandwidget.cpp @@ -91,7 +91,7 @@ void LigandWidget::setDatabase(QString dbname) { _mw->getSettings()->setValue("lastCompoundDatabase", getDatabaseName()); emit databaseChanged(getDatabaseName()); - DB.loadCompoundsSQL(dbname); + DB.loadCompoundsSQL(dbname,DB.getLigandDB()); showTable(); if(!filterString.isEmpty()) showMatches(filterString); diff --git a/src/maven/mainwindow.cpp b/src/maven/mainwindow.cpp index 97ccc664..a7019688 100644 --- a/src/maven/mainwindow.cpp +++ b/src/maven/mainwindow.cpp @@ -67,8 +67,7 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) { if(!QFile::exists(writeLocation)) { QDir dir; dir.mkdir(writeLocation); } qDebug() << "WRITE FOLDER=" << writeLocation; DB.connect(writeLocation + "/ligand.db"); - - DB.loadCompoundsSQL("KNOWNS"); + DB.loadCompoundsSQL("KNOWNS", DB.getLigandDB()); //DB.loadCompoundsSQL("ALL"); //QString commonFragments = methodsFolder + "/" + "FRAGMENTS.csv"; diff --git a/src/maven/projectDB.cpp b/src/maven/projectDB.cpp index 6eb39577..522c70a6 100644 --- a/src/maven/projectDB.cpp +++ b/src/maven/projectDB.cpp @@ -9,6 +9,24 @@ void ProjectDB::saveGroups(vector& allgroups, QString setName) { } } +void ProjectDB::saveCompounds(set& compoundSet) { + //save seen compounds in to project database + std::vectorcompounds(compoundSet.begin(), compoundSet.end()); + DB.saveCompoundsSQL(compounds,sqlDB); +} + +void ProjectDB::saveCompounds(vector& allgroups) { + set seenCompounds; + + //find linked compounds + for(unsigned int i=0; i < allgroups.size(); i++ ) { + Compound* c = allgroups[i].compound; + if(c) seenCompounds.insert(c); + } + saveCompounds(seenCompounds); +} + + void ProjectDB::deleteAll() { QSqlQuery query(sqlDB); query.exec("drop table samples"); @@ -424,6 +442,7 @@ void ProjectDB::loadPeakGroups(QString tableName) { if (!compoundId.empty()){ Compound* compound = DB.findSpeciesById(compoundId,compoundDB); + cerr << "Locate:" << compoundId << " in " << compoundDB << "->" << compound; if (compound) { g.compound = compound; diff --git a/src/maven/projectDB.h b/src/maven/projectDB.h index ac0b746c..eedcff28 100644 --- a/src/maven/projectDB.h +++ b/src/maven/projectDB.h @@ -24,6 +24,8 @@ class ProjectDB { void clearLoadedPeakGroups() { allgroups.clear(); } void saveSamples(vector &sampleSet); void saveGroups(vector &allgroups, QString setName); + void saveCompounds(vector &allgroups); + void saveCompounds(set&compounds); void writeSearchResultsToDB(); int writeGroupSqlite(PeakGroup* group, int parentGroupId, QString tableName); void loadPeakGroups(QString tableName); diff --git a/src/maven/projectdockwidget.cpp b/src/maven/projectdockwidget.cpp index f5f28f99..fb2524c0 100644 --- a/src/maven/projectdockwidget.cpp +++ b/src/maven/projectdockwidget.cpp @@ -643,13 +643,18 @@ void ProjectDockWidget::saveProjectSQLITE(QString filename) { project->saveSamples(sampleSet); project->saveAlignment(); + set compoundSet; + int groupCount=0; for(TableDockWidget* peakTable : _mainwindow->getAllPeakTables() ) { for(PeakGroup* group : peakTable->getGroups()) { groupCount++; project->writeGroupSqlite(group,0,peakTable->windowTitle()); + if(group->compound) compoundSet.insert(group->compound); } } + project->saveCompounds(compoundSet); + _mainwindow->setStatusText(tr("Saved %1 groups to %2").arg(QString::number(groupCount),filename)); } else { qDebug() << "Can't write to closed project" << filename;