Skip to content

Commit

Permalink
saving compounds into project files
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenemel committed May 22, 2018
1 parent 6bd62d8 commit d92cce2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 27 deletions.
33 changes: 17 additions & 16 deletions src/maven/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 + "'";
Expand Down Expand Up @@ -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];
Expand All @@ -215,7 +215,7 @@ Compound* Database::findSpeciesById(string id,string db) {


vector<Compound*> 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<Compound*> set;
qDebug() << "findSpeciesByName" << name.c_str();
Expand Down Expand Up @@ -634,32 +634,33 @@ vector<Compound*> 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)");

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<Compound*> &compoundSet) {
void Database::saveCompoundsSQL(vector<Compound*> &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,\
Expand All @@ -685,7 +686,7 @@ void Database::saveCompoundsSQL(vector<Compound*> &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) {
Expand Down
9 changes: 5 additions & 4 deletions src/maven/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ class Database {
int loadCompoundsFile(QString filename);
vector<Compound*> loadCompoundCSVFile(QString fileName);
vector<Compound*> loadNISTLibrary(QString fileName);
void loadCompoundsSQL(QString databaseName);
void saveCompoundsSQL(vector<Compound*> &compoundSet);
void deleteCompoundsSQL( QString dbName);
void deleteAllCompoundsSQL();

void loadCompoundsSQL(QString databaseName,QSqlDatabase& dbConnection);
void saveCompoundsSQL(vector<Compound*> &compoundSet, QSqlDatabase& dbConnection);
void deleteCompoundsSQL(QString dbName, QSqlDatabase& dbConnection);
void deleteAllCompoundsSQL(QSqlDatabase& dbConnection);

multimap<string,Compound*> keywordSearch(string needle);
vector<string> getCompoundReactions(string compound_id);
Expand Down
8 changes: 4 additions & 4 deletions src/maven/librarydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/maven/ligandwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/maven/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
19 changes: 19 additions & 0 deletions src/maven/projectDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ void ProjectDB::saveGroups(vector<PeakGroup>& allgroups, QString setName) {
}
}

void ProjectDB::saveCompounds(set<Compound*>& compoundSet) {
//save seen compounds in to project database
std::vector<Compound*>compounds(compoundSet.begin(), compoundSet.end());
DB.saveCompoundsSQL(compounds,sqlDB);
}

void ProjectDB::saveCompounds(vector<PeakGroup>& allgroups) {
set<Compound*> 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");
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/maven/projectDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ProjectDB {
void clearLoadedPeakGroups() { allgroups.clear(); }
void saveSamples(vector<mzSample *> &sampleSet);
void saveGroups(vector<PeakGroup> &allgroups, QString setName);
void saveCompounds(vector<PeakGroup> &allgroups);
void saveCompounds(set<Compound*>&compounds);
void writeSearchResultsToDB();
int writeGroupSqlite(PeakGroup* group, int parentGroupId, QString tableName);
void loadPeakGroups(QString tableName);
Expand Down
5 changes: 5 additions & 0 deletions src/maven/projectdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,13 +643,18 @@ void ProjectDockWidget::saveProjectSQLITE(QString filename) {
project->saveSamples(sampleSet);
project->saveAlignment();

set<Compound*> 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;
Expand Down

0 comments on commit d92cce2

Please sign in to comment.