Skip to content

Commit

Permalink
save / load compound information into sqlite project files
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenemel committed Jun 15, 2018
1 parent d92cce2 commit b23cbc4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
14 changes: 11 additions & 3 deletions src/maven/classifierNeuralNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ void ClassifierNeuralNet::saveModel(string filename) {
}

void ClassifierNeuralNet::loadDefaultModel(){
char tmpfilename[]="/tmp/mzclassXXXXXX";
int temp_fd=mkstemp(tmpfilename);

char tmpfilename[L_tmpnam];
tmpnam(tmpfilename);
printf ("Tempname #1: %s\n",tmpfilename);
if(!temp_fd) {
printf("ClassifierNeuralNet: failed to create temp file");
return;
}

//char tmpfilename[L_tmpnam];
//tmpnam(tmpfilename);
//printf ("Tempname #1: %s\n",tmpfilename);
FILE* tmpFile = fopen(tmpfilename,"w");


fprintf(tmpFile,"%s",defaultModel.c_str());
fclose(tmpFile);

Expand Down
67 changes: 38 additions & 29 deletions src/maven/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ void Database::loadCompoundsSQL(QString databaseName, QSqlDatabase &dbConnection
}

QSqlQuery query(dbConnection);
QString sql = "select * from compounds where name not like '%DECOY%'";

if (databaseName != "ALL") sql += " and dbName='" + databaseName + "'";
QString sql = "select * from compounds";
if (databaseName != "ALL") sql += " where dbName='" + databaseName + "'";

query.prepare(sql);
if(!query.exec()) qDebug() << query.lastError();
if(!query.exec()) qDebug() << "loadCompoundsSQL: query error " << query.lastError();
MassCalculator mcalc;

int loadcount=0;
Expand All @@ -116,13 +115,19 @@ void Database::loadCompoundsSQL(QString databaseName, QSqlDatabase &dbConnection
string formula = query.value("formula").toString().toStdString();
int charge = query.value("charge").toInt();
float exactMass = query.value("mass").toDouble();
int cid = query.value("cid").toInt();
string db = query.value("dbName").toString().toStdString();
double expectedRt = query.value("expectedRt").toDouble();

//skip already exists in internal database
if ( compoundIdMap.contains(id + db) ) continue;

//the neutral mass is computated automatically inside the constructor
Compound* compound = new Compound(id,name,formula,charge);
compound->cid = cid;
compound->db = db;
compound->expectedRt = expectedRt;

compound->cid = query.value("cid").toInt();
compound->db = query.value("dbName").toString().toStdString();
compound->expectedRt = query.value("expectedRt").toDouble();

if (formula.empty()) {
if(exactMass >0) compound->setExactMass(exactMass);
Expand Down Expand Up @@ -687,7 +692,7 @@ void Database::saveCompoundsSQL(vector<Compound*> &compoundSet, QSqlDatabase& db
query0.exec("create index if not exists compound_db_idx on compounds(dbName)");

QSqlQuery query1(dbConnection);
query1.prepare("insert into compounds values(NULL, ?,?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?)");
query1.prepare("replace into compounds values(?,?,?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?)");

for(Compound* c : compoundSet) {
QStringList cat;
Expand All @@ -698,27 +703,31 @@ void Database::saveCompoundsSQL(vector<Compound*> &compoundSet, QSqlDatabase& db
for(float f : c->fragment_mzs) { fragMz << QString::number(f,'f',5); }
for(float f : c->fragment_intensity) { fragIntensity << QString::number(f,'f',5); }

query1.bindValue( 0, QString(c->db.c_str()) );
query1.bindValue( 1, QString(c->id.c_str()) );
query1.bindValue( 2, QString(c->name.c_str()));
query1.bindValue( 3, QString(c->formula.c_str()));
query1.bindValue( 4, QString(c->smileString.c_str()));
query1.bindValue( 5, QString(c->srmId.c_str()));

query1.bindValue( 6, c->getExactMass() );
query1.bindValue( 7, c->charge);
query1.bindValue( 8, c->expectedRt);
query1.bindValue( 9, c->precursorMz);
query1.bindValue( 10, c->productMz);

query1.bindValue( 11, c->collisionEnergy);
query1.bindValue( 12, c->logP);
query1.bindValue( 13, c->virtualFragmentation);
query1.bindValue( 14, c->ionizationMode);
query1.bindValue( 15, cat.join(";"));

query1.bindValue( 16, fragMz.join(";"));
query1.bindValue( 17, fragIntensity.join(";"));
QVariant cid = QVariant(QVariant::Int);
if (c->cid) cid = QVariant(c->cid);

query1.bindValue( 0, cid);
query1.bindValue( 1, QString(c->db.c_str()) );
query1.bindValue( 2, QString(c->id.c_str()) );
query1.bindValue( 3, QString(c->name.c_str()));
query1.bindValue( 4, QString(c->formula.c_str()));
query1.bindValue( 5, QString(c->smileString.c_str()));
query1.bindValue( 6, QString(c->srmId.c_str()));

query1.bindValue( 7, c->getExactMass() );
query1.bindValue( 8, c->charge);
query1.bindValue( 9, c->expectedRt);
query1.bindValue( 10, c->precursorMz);
query1.bindValue( 11, c->productMz);

query1.bindValue( 12, c->collisionEnergy);
query1.bindValue( 13, c->logP);
query1.bindValue( 14, c->virtualFragmentation);
query1.bindValue( 15, c->ionizationMode);
query1.bindValue( 16, cat.join(";"));

query1.bindValue( 17, fragMz.join(";"));
query1.bindValue( 18, fragIntensity.join(";"));

if(!query1.exec()) qDebug() << query1.lastError();
}
Expand Down
3 changes: 2 additions & 1 deletion src/maven/projectDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ 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;
//cerr << "Found compound:" << compound->cid << endl;
} else {
g.tagString = compoundName + "|" + adductName + " | id=" + compoundId;
}
Expand Down Expand Up @@ -719,6 +719,7 @@ void ProjectDB::loadSamples() {
}
}


bool ProjectDB::closeDatabaseConnection() {

if (sqlDB.isOpen()) {
Expand Down
4 changes: 4 additions & 0 deletions src/maven/projectdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,17 @@ void ProjectDockWidget::loadProjectSQLITE(QString fileName) {

ProjectDB* selectedProject = new ProjectDB(fileName);

//load compounds stored in the project file
DB.loadCompoundsSQL("ALL",selectedProject->sqlDB);

QStringList pathlist;
pathlist << projectPath
<< "."
<< ".."
<< settings->value("lastDir").value<QString>();

QSqlQuery query(selectedProject->sqlDB);

query.exec("select * from samples");

QStringList filelist;
Expand Down
2 changes: 1 addition & 1 deletion src/maven_core

0 comments on commit b23cbc4

Please sign in to comment.