00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_FORMAT_DB_DBADAPTER_H
00036 #define OPENMS_FORMAT_DB_DBADAPTER_H
00037
00038
00039 #include <OpenMS/KERNEL/MSExperiment.h>
00040 #include <OpenMS/KERNEL/RichPeak1D.h>
00041 #include <OpenMS/FORMAT/OPTIONS/PeakFileOptions.h>
00042
00043
00044 #include <QtSql/QSqlQuery>
00045 #include <QtCore/QVariant>
00046 #include <QtCore/QDate>
00047
00048
00049 #include <string>
00050 #include <map>
00051
00052 namespace OpenMS
00053 {
00054 class DBConnection;
00055
00069 class OPENMS_DLLAPI DBAdapter
00070 {
00071 public:
00073 DBAdapter(DBConnection & db_con);
00074
00076 ~DBAdapter();
00077
00079 template <class ExperimentType>
00080 void storeExperiment(ExperimentType & exp);
00081
00083 template <class ExperimentType>
00084 void loadExperiment(UID id, ExperimentType & exp);
00085
00086 template <class SpectrumType>
00088 void loadSpectrum(UID id, SpectrumType & spec);
00089
00091 PeakFileOptions & getOptions();
00092
00094 const PeakFileOptions & getOptions() const;
00095
00101 bool checkDBVersion(bool warning);
00102
00104 void createDB();
00105
00106 private:
00108 DBConnection & db_con_;
00109
00111 DBAdapter();
00112
00118 UID storeMetaInfo_(const String & parent_table, UID parent_id, const MetaInfoInterface & info);
00120 UID storeMetaInfo_(const String & parent_table, UID parent_id, const Peak1D & peak);
00122 UID storeMetaInfo_(const String & parent_table, UID parent_id, const RichPeak1D & peak);
00124 UID storeMetaInfo_(const String & parent_table, UID parent_id, const Precursor & peak);
00125
00127 void loadMetaInfo_(UID id, MetaInfoInterface & info);
00129 void loadMetaInfo_(UID id, Peak1D & peak);
00131 void loadMetaInfo_(UID id, RichPeak1D & peak);
00133 void loadMetaInfo_(UID id, Precursor & peak);
00134
00139 void deleteMetaInfo_(const String & parent_table, const String & condition);
00140
00146 UID storeFile_(const String & parent_table, UID parent_id, const SourceFile & file);
00147
00152 void loadFile_(UID id, SourceFile & file);
00153
00159 UID storeSample_(const Sample & sample, UID exp_id, UID parent_id);
00160
00165 void loadSample_(UID id, Sample & sample);
00166
00167 PeakFileOptions options_;
00168 };
00169
00170
00171
00172
00173 template <class ExperimentType>
00174 void DBAdapter::storeExperiment(ExperimentType & exp)
00175 {
00176 std::stringstream query;
00177 String end;
00178 String tmp;
00179 bool new_entry(false);
00180 QSqlQuery result;
00181 int parent_id(-1);
00182 UID acquisition_info_id(0);
00183 UID meta_id(0);
00184
00185
00186
00187
00188 if (!checkDBVersion(true))
00189 return;
00190
00191
00192
00193
00194
00195
00196
00197 query.str("");
00198 new_entry = (exp.getPersistenceId() == 0);
00199 if (new_entry)
00200 {
00201 query << "INSERT INTO META_MSExperiment SET ";
00202 end = "";
00203 }
00204 else
00205 {
00206 query << "UPDATE META_MSExperiment SET ";
00207 end = " WHERE id='" + String(exp.getPersistenceId()) + "'";
00208 }
00209
00210
00211 query << "Date='" << exp.getDateTime().get() << "'";
00212
00213 query << ",Description='" << exp.getComment() << "'";
00214
00215 query << ",FractionIdentifier='" << exp.getFractionIdentifier() << "'";
00216
00217 query << end;
00218 result = db_con_.executeQuery(query.str());
00219 if (new_entry)
00220 {
00221 exp.setPersistenceId(db_con_.getAutoId());
00222 }
00223
00224 storeMetaInfo_("META_MSExperiment", exp.getPersistenceId(), exp);
00225
00226
00227
00228
00229
00230 std::vector<ProteinIdentification> & pi = exp.getProteinIdentifications();
00231
00232
00233
00234 query.str("");
00235 query << "DELETE FROM ID_ProteinIdentification WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00236 result = db_con_.executeQuery(query.str());
00237
00238 for (std::vector<ProteinIdentification>::const_iterator pi_it = pi.begin(); pi_it != pi.end(); pi_it++)
00239 {
00240 query.str("");
00241 query << "INSERT INTO ID_ProteinIdentification SET ";
00242 query << "fid_MSExperiment='" << exp.getPersistenceId() << "'";
00243 query << ",SearchEngine='" << pi_it->getSearchEngine() << "'";
00244 query << ",SearchEngineVersion='" << pi_it->getSearchEngineVersion() << "'";
00245 query << ",Date='" << pi_it->getDateTime().get() << "'";
00246 query << ",ScoreType='" << pi_it->getScoreType() << "'";
00247 query << ",HigherScoreBetter='" << pi_it->isHigherScoreBetter() << "'";
00248 query << ",SignificanceThreshold='" << pi_it->getSignificanceThreshold() << "'";
00249
00250 result = db_con_.executeQuery(query.str());
00251 parent_id = db_con_.getAutoId();
00252
00253 storeMetaInfo_("ID_ProteinIdentification", parent_id, *pi_it);
00254
00255
00256
00257
00258 for (std::vector<ProteinHit>::const_iterator ph_it = pi_it->getHits().begin(); ph_it != pi_it->getHits().end(); ph_it++)
00259 {
00260 query.str("");
00261 query << "INSERT INTO ID_ProteinHit SET ";
00262 query << "fid_ProteinIdentification='" << parent_id << "'";
00263 query << ",Score='" << ph_it->getScore() << "'";
00264 query << ",Accession='" << ph_it->getAccession() << "'";
00265 query << ",Sequence='" << ph_it->getSequence() << "'";
00266 query << ",Rank='" << ph_it->getRank() << "'";
00267
00268 result = db_con_.executeQuery(query.str());
00269 meta_id = db_con_.getAutoId();
00270
00271 storeMetaInfo_("ID_ProteinHit", meta_id, *ph_it);
00272 }
00273
00274
00275 query.str("");
00276 query << "DELETE FROM ID_SearchParameters WHERE fid_ProteinIdentification='" << parent_id << "'";
00277 result = db_con_.executeQuery(query.str());
00278
00279 query.str("");
00280 query << "INSERT INTO ID_SearchParameters SET ";
00281 query << "fid_ProteinIdentification='" << parent_id << "'";
00282 query << ",DB='" << pi_it->getSearchParameters().db << "'";
00283 query << ",DBVersion='" << pi_it->getSearchParameters().db_version << "'";
00284 query << ",Taxonomy='" << pi_it->getSearchParameters().taxonomy << "'";
00285 query << ",Charges='" << pi_it->getSearchParameters().charges << "'";
00286 query << ",MassType='" << (1u + pi_it->getSearchParameters().mass_type) << "'";
00287 query << ",Enzyme='" << (1u + pi_it->getSearchParameters().enzyme) << "'";
00288 query << ",MissedCleavages='" << pi_it->getSearchParameters().missed_cleavages << "'";
00289 query << ",PeakMassTolerance='" << pi_it->getSearchParameters().peak_mass_tolerance << "'";
00290 query << ",PrecursorTolerance='" << pi_it->getSearchParameters().precursor_tolerance << "'";
00291
00292 result = db_con_.executeQuery(query.str());
00293
00294 meta_id = db_con_.getAutoId();
00295 storeMetaInfo_("ID_SearchParameters", meta_id, pi_it->getSearchParameters());
00296
00297
00298 query.str("");
00299 query << "DELETE FROM ID_FixedModifications WHERE fid_SearchParameters='" << meta_id << "'";
00300 result = db_con_.executeQuery(query.str());
00301
00302 for (std::vector<String>::const_iterator mod_it = pi_it->getSearchParameters().fixed_modifications.begin(); mod_it != pi_it->getSearchParameters().fixed_modifications.end(); mod_it++)
00303 {
00304
00305 query.str("");
00306 query << "INSERT INTO ID_FixedModifications SET ";
00307 query << "fid_SearchParameters='" << meta_id << "'";
00308 query << ",name='" << *mod_it << "'";
00309
00310 result = db_con_.executeQuery(query.str());
00311 }
00312 for (std::vector<String>::const_iterator mod_it = pi_it->getSearchParameters().variable_modifications.begin(); mod_it != pi_it->getSearchParameters().variable_modifications.end(); mod_it++)
00313 {
00314 query.str("");
00315 query << "INSERT INTO ID_VariableModifications SET ";
00316 query << "fid_SearchParameters='" << meta_id << "'";
00317 query << ",name='" << *mod_it << "'";
00318
00319 result = db_con_.executeQuery(query.str());
00320 }
00321 }
00322
00323
00324
00325
00326
00327
00328 query.str("");
00329 deleteMetaInfo_("META_Sample", "fid_MSExperiment=" + String(exp.getPersistenceId()));
00330
00331 query << "DELETE FROM META_Sample WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00332 storeSample_(exp.getSample(), exp.getPersistenceId(), 0);
00333
00334
00335
00336
00337
00338 const std::vector<ContactPerson> & contacts = exp.getContacts();
00339
00340 query.str("");
00341 deleteMetaInfo_("META_ContactPerson", "fid_MSExperiment=" + String(exp.getPersistenceId()));
00342 query << "DELETE FROM META_ContactPerson WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00343 result = db_con_.executeQuery(query.str());
00344
00345 for (std::vector<ContactPerson>::const_iterator contact_it = contacts.begin(); contact_it != contacts.end(); contact_it++)
00346 {
00347 query.str("");
00348 query << "INSERT INTO META_ContactPerson SET ";
00349 query << "fid_MSExperiment='" << exp.getPersistenceId() << "'";
00350 query << ",PreName='" << contact_it->getFirstName() << "'";
00351 query << ",LastName='" << contact_it->getLastName() << "'";
00352 query << ",Affiliation='" << contact_it->getInstitution() << "'";
00353 query << ",Email='" << contact_it->getEmail() << "'";
00354 query << ",Comment='" << contact_it->getContactInfo() << "'";
00355
00356 result = db_con_.executeQuery(query.str());
00357 parent_id = db_con_.getAutoId();
00358
00359 storeMetaInfo_("META_ContactPerson", parent_id, *contact_it);
00360 }
00361
00362
00363
00364
00365
00366 const HPLC & hplc = exp.getHPLC();
00367 query.str("");
00368
00369 if (new_entry)
00370 {
00371 query << "INSERT INTO META_HPLC SET ";
00372 query << "fid_MSExperiment='" << exp.getPersistenceId() << "',";
00373 end = "";
00374 }
00375 else
00376 {
00377 query << "SELECT id FROM META_HPLC WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00378 result = db_con_.executeQuery(query.str(), true);
00379 parent_id = result.value(0).toInt();
00380
00381 query.str("");
00382 query << "UPDATE META_HPLC SET ";
00383 end = " WHERE fid_MSExperiment='" + String(exp.getPersistenceId()) + "'";
00384 }
00385
00386 query << "InstrumentName='" << hplc.getInstrument() << "'";
00387 query << ",ColumnName='" << hplc.getColumn() << "'";
00388 query << ",Description='" << hplc.getComment() << "'";
00389 query << ",Flux=" << hplc.getFlux();
00390 query << ",Pressure=" << hplc.getPressure();
00391 query << ",Temperature=" << hplc.getTemperature();
00392
00393 query << end;
00394 result = db_con_.executeQuery(query.str());
00395
00396 if (new_entry)
00397 {
00398 parent_id = db_con_.getAutoId();
00399 }
00400
00401
00402
00403
00404
00405 const Gradient & gradient = exp.getHPLC().getGradient();
00406 const std::vector<String> & eluents = gradient.getEluents();
00407 const std::vector<Int> & time = gradient.getTimepoints();
00408 const std::vector<std::vector<UInt> > & percentages = gradient.getPercentages();
00409 std::stringstream query_eluents, query_time, query_percentages;
00410 UID eluents_id(0), time_id(0);
00411
00412
00413 query.str("");
00414 query << "DELETE FROM META_GradientEluent WHERE fid_HPLC=" << parent_id;
00415 result = db_con_.executeQuery(query.str());
00416 query.str("");
00417 query << "DELETE FROM META_GradientTime WHERE fid_HPLC=" << parent_id;
00418 result = db_con_.executeQuery(query.str());
00419
00420 if (!eluents.empty())
00421 {
00422 query_eluents.str("");
00423 query_eluents << "INSERT INTO META_GradientEluent (fid_HPLC, Name) VALUES ";
00424 for (std::vector<String>::const_iterator eluents_it = eluents.begin(); eluents_it != eluents.end(); eluents_it++)
00425 {
00426 query_eluents << "(";
00427 query_eluents << parent_id;
00428 query_eluents << ",'" << *eluents_it << "'";
00429 query_eluents << "),";
00430 }
00431
00432 result = db_con_.executeQuery(String(query_eluents.str()).chop(1));
00433 eluents_id = db_con_.getAutoId();
00434 }
00435
00436 if (!time.empty())
00437 {
00438 query_time.str("");
00439 query_time << "INSERT INTO META_GradientTime (fid_HPLC, Time) VALUES ";
00440 for (std::vector<Int>::const_iterator time_it = time.begin(); time_it != time.end(); time_it++)
00441 {
00442 query_time << "(";
00443 query_time << parent_id;
00444 query_time << "," << *time_it;
00445 query_time << "),";
00446 }
00447
00448 result = db_con_.executeQuery(String(query_time.str()).chop(1));
00449 time_id = db_con_.getAutoId();
00450 }
00451
00452 if (!percentages.empty() && !eluents.empty() && !time.empty())
00453 {
00454 query_percentages.str("");
00455 query_percentages << "INSERT INTO META_GradientPercentage (fid_GradientEluent, fid_GradientTime, Percentage) VALUES ";
00456 int i = 0;
00457
00458 for (std::vector<std::vector<UInt> >::const_iterator percent_outer_it = percentages.begin(); percent_outer_it != percentages.end(); percent_outer_it++)
00459 {
00460 int j = 0;
00461
00462 for (std::vector<UInt>::const_iterator percent_inner_it = (*percent_outer_it).begin(); percent_inner_it != (*percent_outer_it).end(); percent_inner_it++)
00463 {
00464 query_percentages << "(";
00465 query_percentages << eluents_id + i;
00466 query_percentages << "," << time_id + j;
00467 query_percentages << "," << *percent_inner_it;
00468 query_percentages << "),";
00469 j++;
00470 }
00471 i++;
00472 }
00473
00474 result = db_con_.executeQuery(String(query_percentages.str()).chop(1));
00475 }
00476
00477
00478
00479
00480
00481 const Instrument & instrument = exp.getInstrument();
00482 query.str("");
00483
00484 if (new_entry)
00485 {
00486 query << "INSERT INTO META_MSInstrument SET ";
00487 query << "fid_MSExperiment='" << exp.getPersistenceId() << "',";
00488 end = "";
00489 }
00490 else
00491 {
00492 query << "SELECT id FROM META_MSInstrument WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00493 result = db_con_.executeQuery(query.str(), true);
00494 parent_id = result.value(0).toInt();
00495
00496 query.str("");
00497 query << "UPDATE META_MSInstrument SET ";
00498 end = " WHERE fid_MSExperiment='" + String(exp.getPersistenceId()) + "'";
00499 }
00500
00501 query << "Model='" << instrument.getModel() << "'";
00502 query << ",Vendor='" << instrument.getVendor() << "'";
00503 query << ",Description='" << instrument.getCustomizations() << "'";
00504 query << ",IonOpticsType='" << (1u + instrument.getIonOptics()) << "'";
00505
00506 query << end;
00507 result = db_con_.executeQuery(query.str());
00508
00509 if (new_entry)
00510 {
00511 parent_id = db_con_.getAutoId();
00512 }
00513
00514 storeMetaInfo_("META_MSInstrument", parent_id, instrument);
00515
00516 deleteMetaInfo_("META_Software", "SoftwareApplicator='META_MSInstrument' AND fid_SoftwareApplicator=" + String(parent_id));
00517 query.str("");
00518 query << "DELETE FROM META_Software WHERE fid_SoftwareApplicator='" << parent_id << "' AND SoftwareApplicator='META_MSInstrument'";
00519 result = db_con_.executeQuery(query.str());
00520 query.str("");
00521 query << "INSERT INTO META_Software SET ";
00522 query << "fid_SoftwareApplicator='" << parent_id << "'";
00523 query << ",SoftwareApplicator='META_MSInstrument'";
00524 query << ",Name='" << instrument.getSoftware().getName() << "'";
00525 query << ",Version='" << instrument.getSoftware().getVersion() << "'";
00526 result = db_con_.executeQuery(query.str());
00527
00528 UID software_id = db_con_.getAutoId();
00529 storeMetaInfo_("META_Software", software_id, instrument.getSoftware());
00530
00531
00532
00533
00534 const std::vector<IonDetector> & detectors = exp.getInstrument().getIonDetectors();
00535 query.str("");
00536
00537 deleteMetaInfo_("META_IonDetector", "fid_MSInstrument=" + String(parent_id));
00538 query << "DELETE FROM META_IonDetector WHERE fid_MSInstrument='" << parent_id << "'";
00539 result = db_con_.executeQuery(query.str());
00540
00541 for (std::vector<IonDetector>::const_iterator detectors_it = detectors.begin(); detectors_it != detectors.end(); detectors_it++)
00542 {
00543 query.str("");
00544 query << "INSERT INTO META_IonDetector SET ";
00545 query << "fid_MSInstrument='" << parent_id << "'";
00546 query << ",AcquisitionMode=" << (1u + detectors_it->getAcquisitionMode());
00547 query << ",Type=" << (1u + detectors_it->getType());
00548 query << ",Resolution=" << detectors_it->getResolution();
00549 query << ",ADCSamplingFrequency=" << detectors_it->getADCSamplingFrequency();
00550 query << ",InstrumentOrder=" << (detectors_it->getOrder());
00551
00552 result = db_con_.executeQuery(query.str());
00553 storeMetaInfo_("META_IonDetector", db_con_.getAutoId(), *detectors_it);
00554 }
00555
00556
00557
00558
00559
00560 const std::vector<IonSource> & sources = exp.getInstrument().getIonSources();
00561 query.str("");
00562
00563 deleteMetaInfo_("META_IonSource", "fid_MSInstrument=" + String(parent_id));
00564 query << "DELETE FROM META_IonSource WHERE fid_MSInstrument='" << parent_id << "'";
00565 result = db_con_.executeQuery(query.str());
00566
00567 for (std::vector<IonSource>::const_iterator sources_it = sources.begin(); sources_it != sources.end(); sources_it++)
00568 {
00569 query.str("");
00570 query << "INSERT INTO META_IonSource SET ";
00571 query << "fid_MSInstrument='" << parent_id << "'";
00572 query << ",InletType=" << (1u + sources_it->getInletType());
00573 query << ",IonizationMethod=" << (1u + sources_it->getIonizationMethod());
00574 query << ",IonizationMode=" << (1u + sources_it->getPolarity());
00575 query << ",InstrumentOrder=" << (sources_it->getOrder());
00576
00577 result = db_con_.executeQuery(query.str());
00578 storeMetaInfo_("META_IonSource", db_con_.getAutoId(), *sources_it);
00579 }
00580
00581
00582
00583
00584
00585 const std::vector<MassAnalyzer> & analyzers = exp.getInstrument().getMassAnalyzers();
00586 query.str("");
00587
00588 deleteMetaInfo_("META_MassAnalyzer", "fid_MSInstrument=" + String(parent_id));
00589 query << "DELETE FROM META_MassAnalyzer WHERE fid_MSInstrument='" << parent_id << "'";
00590 result = db_con_.executeQuery(query.str());
00591
00592 for (std::vector<MassAnalyzer>::const_iterator analyzer_it = analyzers.begin(); analyzer_it != analyzers.end(); analyzer_it++)
00593 {
00594 query.str("");
00595 query << "INSERT INTO META_MassAnalyzer SET ";
00596 query << "fid_MSInstrument='" << parent_id << "'";
00597 query << ",Accuracy=" << analyzer_it->getAccuracy();
00598 query << ",FinalMSExponent=" << analyzer_it->getFinalMSExponent();
00599 query << ",IsolationWidth=" << analyzer_it->getIsolationWidth();
00600 query << ",MagneticFieldStrength=" << analyzer_it->getMagneticFieldStrength();
00601 query << ",ReflectronState=" << (1u + analyzer_it->getReflectronState());
00602 query << ",Resolution=" << analyzer_it->getResolution();
00603 query << ",ResolutionMethod=" << (1u + analyzer_it->getResolutionMethod());
00604 query << ",ResolutionType=" << (1u + analyzer_it->getResolutionType());
00605 query << ",ScanDirection=" << (1u + analyzer_it->getScanDirection());
00606 query << ",ScanLaw=" << (1u + analyzer_it->getScanLaw());
00607 query << ",ScanRate=" << analyzer_it->getScanRate();
00608 query << ",ScanTime=" << analyzer_it->getScanTime();
00609 query << ",TOFPathLength=" << analyzer_it->getTOFTotalPathLength();
00610 query << ",Type=" << (1u + analyzer_it->getType());
00611 query << ",InstrumentOrder=" << analyzer_it->getOrder();
00612
00613 result = db_con_.executeQuery(query.str());
00614 storeMetaInfo_("META_MassAnalyzer", db_con_.getAutoId(), *analyzer_it);
00615 }
00616
00617
00618
00619
00620 for (typename ExperimentType::Iterator exp_it = exp.begin(); exp_it != exp.end(); ++exp_it)
00621 {
00622 query.str("");
00623 new_entry = (exp_it->getPersistenceId() == 0);
00624 if (new_entry)
00625 {
00626 query << "INSERT INTO DATA_Spectrum SET ";
00627 end = "";
00628 }
00629 else
00630 {
00631 query << "UPDATE DATA_Spectrum SET ";
00632 end = " WHERE id='" + String(exp_it->getPersistenceId()) + "'";
00633 }
00634
00635 query << "fid_MSExperiment='" << exp.getPersistenceId() << "'";
00636
00637 query << ",Type=" << (1u + exp_it->getType());
00638
00639 query << ",RetentionTime='" << exp_it->getRT() << "'";
00640
00641 query << ",MSLevel='" << exp_it->getMSLevel() << "'";
00642
00643 query << ",Description='" << exp_it->getComment() << "'";
00644
00645 query << ",NativeID='" << exp_it->getNativeID() << "'";
00646
00647
00648
00649
00650
00651 query << end;
00652 result = db_con_.executeQuery(query.str());
00653 if (new_entry)
00654 {
00655 exp_it->setPersistenceId(db_con_.getAutoId());
00656 }
00657 storeFile_("DATA_Spectrum", exp_it->getPersistenceId(), exp_it->getSourceFile());
00658 meta_id = storeMetaInfo_("DATA_Spectrum", exp_it->getPersistenceId(), *exp_it);
00659
00660
00661
00662
00663
00664
00665 std::vector<PeptideIdentification> & pei = exp_it->getPeptideIdentifications();
00666
00667
00668
00669 query.str("");
00670 query << "DELETE FROM ID_PeptideIdentification WHERE fid_Spectrum='";
00671 query << exp_it->getPersistenceId() << "'";
00672 result = db_con_.executeQuery(query.str());
00673
00674 for (std::vector<PeptideIdentification>::const_iterator pei_it = pei.begin(); pei_it != pei.end(); pei_it++)
00675 {
00676 query.str("");
00677 query << "INSERT INTO ID_PeptideIdentification SET ";
00678 query << "fid_Spectrum='" << exp_it->getPersistenceId() << "'";
00679 query << ",SignificanceThreshold='" << pei_it->getSignificanceThreshold() << "'";
00680 query << ",ScoreType='" << pei_it->getScoreType() << "'";
00681 query << ",HigherScoreBetter='" << pei_it->isHigherScoreBetter() << "'";
00682
00683 result = db_con_.executeQuery(query.str());
00684 parent_id = db_con_.getAutoId();
00685
00686 storeMetaInfo_("ID_PeptideIdentification", parent_id, *pei_it);
00687
00688
00689
00690 for (std::vector<PeptideHit>::const_iterator peh_it = pei_it->getHits().begin(); peh_it != pei_it->getHits().end(); peh_it++)
00691 {
00692 query.str("");
00693 query << "INSERT INTO ID_PeptideHit SET ";
00694 query << "fid_Identification='" << parent_id << "'";
00695 query << ",Score='" << peh_it->getScore() << "'";
00696 query << ",charge='" << peh_it->getCharge() << "'";
00697 query << ",Sequence='" << peh_it->getSequence() << "'";
00698 query << ",AABefore='" << peh_it->getAABefore() << "'";
00699 query << ",AAAfter='" << peh_it->getAAAfter() << "'";
00700
00701 result = db_con_.executeQuery(query.str());
00702 meta_id = db_con_.getAutoId();
00703
00704 storeMetaInfo_("ID_PeptideHit", meta_id, *peh_it);
00705 }
00706 }
00707
00708
00709
00710
00711
00712
00713
00714 query.str("");
00715 query << "DELETE FROM DATA_Precursor WHERE fid_Spectrum='";
00716 query << exp_it->getPersistenceId() << "'";
00717 result = db_con_.executeQuery(query.str());
00718
00719
00720 for (Size precs_it = 0; precs_it < exp_it->getPrecursors().size(); precs_it++)
00721 {
00722 query.str("");
00723 query << "INSERT INTO DATA_Precursor SET ";
00724 query << "fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00725
00726 query << ",Intensity='" << exp_it->getPrecursors()[precs_it].getIntensity() << "'";
00727
00728 query << ",WindowMz='" << exp_it->getPrecursors()[precs_it].getMZ() << "'";
00729
00730 query << ",Charge='" << exp_it->getPrecursors()[precs_it].getCharge() << "'";
00731
00732 query << ",ActivationEnergy='" << exp_it->getPrecursors()[precs_it].getActivationEnergy() << "'";
00733
00734 query << ",WindowLow='" << exp_it->getPrecursors()[precs_it].getIsolationWindowLowerOffset() << "'";
00735
00736 query << ",WindowUp='" << exp_it->getPrecursors()[precs_it].getIsolationWindowUpperOffset() << "'";
00737
00738 result = db_con_.executeQuery(query.str());
00739 parent_id = db_con_.getAutoId();
00740 storeMetaInfo_("DATA_Precursor", parent_id, exp_it->getPrecursors()[precs_it]);
00741
00742
00743 for (Size pcs_it = 0; pcs_it < exp_it->getPrecursors()[precs_it].getPossibleChargeStates().size(); ++pcs_it)
00744 {
00745 query.str("");
00746 query << "INSERT INTO DATA_PrecursorPCS SET ";
00747 query << "fid_Precursor='" + String(parent_id) + "'";
00748
00749 query << ",PossibleChargeStates='" << (exp_it->getPrecursors()[precs_it].getPossibleChargeStates()[pcs_it]) << "'";
00750 result = db_con_.executeQuery(query.str());
00751 }
00752
00753 for (std::set<Precursor::ActivationMethod>::iterator am_it = exp_it->getPrecursors()[precs_it].getActivationMethods().begin(); am_it != exp_it->getPrecursors()[precs_it].getActivationMethods().end(); ++am_it)
00754 {
00755 query.str("");
00756 query << "INSERT INTO DATA_PrecursorAM SET ";
00757 query << "fid_Precursor='" + String(parent_id) + "'";
00758
00759 query << ",ActivationMethods=" << (1u + *(am_it));
00760 result = db_con_.executeQuery(query.str());
00761 }
00762
00763 }
00764
00765
00766
00767
00768
00769
00770
00771 query.str("");
00772 query << "DELETE FROM DATA_Products WHERE fid_Spectrum='";
00773 query << exp_it->getPersistenceId() << "'";
00774 result = db_con_.executeQuery(query.str());
00775
00776 for (Size precs_it = 0; precs_it < exp_it->getProducts().size(); precs_it++)
00777 {
00778 query.str("");
00779 query << "INSERT INTO DATA_Products SET ";
00780 query << "fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00781
00782 query << ",WindowMz='" << exp_it->getProducts()[precs_it].getMZ() << "'";
00783
00784 query << ",WindowLow='" << exp_it->getProducts()[precs_it].getIsolationWindowLowerOffset() << "'";
00785
00786 query << ",WindowUp='" << exp_it->getProducts()[precs_it].getIsolationWindowUpperOffset() << "'";
00787
00788 result = db_con_.executeQuery(query.str());
00789 parent_id = db_con_.getAutoId();
00790 storeMetaInfo_("DATA_Products", parent_id, exp_it->getProducts()[precs_it]);
00791 }
00792
00793
00794
00795
00796 query.str("");
00797 deleteMetaInfo_("DATA_Peak", "fid_Spectrum=" + String(exp_it->getPersistenceId()));
00798 result = db_con_.executeQuery("DELETE FROM DATA_Peak WHERE fid_Spectrum=" + String(exp_it->getPersistenceId()));
00799 if (exp_it->size() != 0)
00800 {
00801 query << "INSERT INTO DATA_Peak (fid_Spectrum,Intensity,mz) VALUES ";
00802 tmp = "(" + String(exp_it->getPersistenceId()) + ",'";
00803 for (typename ExperimentType::SpectrumType::Iterator spec_it = exp_it->begin(); spec_it != exp_it->end(); ++spec_it)
00804 {
00805
00806 query << tmp;
00807
00808 query << spec_it->getIntensity() << "','";
00809
00810 query << spec_it->getPosition() << "'),";
00811 }
00812
00813 result = db_con_.executeQuery(String(query.str()).chop(1));
00814 }
00815
00816
00817 UID insert_id = db_con_.getAutoId();
00818 for (typename ExperimentType::SpectrumType::Iterator spec_it = exp_it->begin(); spec_it != exp_it->end(); ++spec_it)
00819 {
00820 storeMetaInfo_("DATA_Peak", insert_id, *spec_it);
00821 insert_id++;
00822 }
00823
00824
00825
00826
00827
00828 const typename ExperimentType::SpectrumType::FloatDataArrays & meta_data_arrays = exp_it->getFloatDataArrays();
00829
00830 for (typename ExperimentType::SpectrumType::FloatDataArrays::const_iterator mdarrays_it = meta_data_arrays.begin(); mdarrays_it != meta_data_arrays.end(); ++mdarrays_it)
00831 {
00832
00833
00834
00835 query.str("");
00836 query << "SELECT id FROM META_MetaInfoDescription WHERE fid_Spectrum=";
00837 query << exp_it->getPersistenceId();
00838 query << " AND Name='" << mdarrays_it->getName() << "'";
00839 result = db_con_.executeQuery(query.str());
00840
00841 query.str("");
00842
00843 if (result.size() > 0)
00844 {
00845 parent_id = result.value(0).toInt();
00846 new_entry = false;
00847 query << "UPDATE META_MetaInfoDescription SET ";
00848 query << "Name='" + mdarrays_it->getName() + "' ";
00849 end = " WHERE fid_Spectrum=" + String(exp_it->getPersistenceId());
00850 end += " AND Name='" + mdarrays_it->getName() + "'";
00851 }
00852 else
00853 {
00854 new_entry = true;
00855 query << "INSERT INTO META_MetaInfoDescription SET ";
00856 query << "fid_Spectrum=" << exp_it->getPersistenceId() << ", ";
00857 query << "Name='" << mdarrays_it->getName() << "'";
00858 end = "";
00859 }
00860
00861 query << end;
00862
00863 result = db_con_.executeQuery(query.str());
00864 if (new_entry)
00865 {
00866 parent_id = db_con_.getAutoId();
00867 }
00868
00869 storeMetaInfo_("META_MetaInfoDescription", parent_id, *mdarrays_it);
00870
00871
00872 query.str("");
00873 query << "DELETE FROM DATA_PeakMetaData WHERE fid_MetaInfoDescription=";
00874 query << parent_id;
00875 result = db_con_.executeQuery(query.str());
00876
00877 query.str("");
00878 query << "SELECT id FROM DATA_Peak WHERE fid_Spectrum=" << exp_it->getPersistenceId();
00879 result = db_con_.executeQuery(query.str(), true);
00880
00881 query.str("");
00882 query << "INSERT INTO DATA_PeakMetaData (fid_Peak,fid_MetaInfoDescription,Value) VALUES ";
00883 for (typename ExperimentType::SpectrumType::FloatDataArray::const_iterator meta_array_it = mdarrays_it->begin(); meta_array_it != mdarrays_it->end(); meta_array_it++)
00884 {
00885 if (result.isValid())
00886 {
00887 query << "(" << result.value(0).toInt() << "," << parent_id << "," << *meta_array_it << "),";
00888 result.next();
00889 }
00890 else
00891 {
00892 break;
00893 }
00894 }
00895
00896 result = db_con_.executeQuery(String(query.str()).chop(1));
00897 }
00898
00899
00900
00901
00902
00903
00904 const InstrumentSettings & settings = exp_it->getInstrumentSettings();
00905
00906 query.str("");
00907
00908 if (new_entry)
00909 {
00910 query << "INSERT INTO META_InstrumentSettings SET fid_Spectrum=" << exp_it->getPersistenceId() << ",";
00911 end = "";
00912 }
00913 else
00914 {
00915 query << "SELECT id FROM META_InstrumentSettings WHERE fid_Spectrum='" << exp_it->getPersistenceId() << "'";
00916 result = db_con_.executeQuery(query.str(), true);
00917 parent_id = result.value(0).toInt();
00918
00919 query.str("");
00920 query << "UPDATE META_InstrumentSettings SET ";
00921 end = " WHERE fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00922 }
00923
00924 query << "Polarity=" << (1u + settings.getPolarity()) << ",";
00925 query << "ScanMode=" << (1u + settings.getScanMode()) << ",";
00926 query << "ZoomScan=" << (settings.getZoomScan());
00927 query << end;
00928
00929 result = db_con_.executeQuery(query.str());
00930
00931 if (new_entry)
00932 parent_id = db_con_.getAutoId();
00933 storeMetaInfo_("META_InstrumentSettings", parent_id, exp_it->getInstrumentSettings());
00934
00935
00936
00937
00938
00939
00940 const std::vector<ScanWindow> & wins = settings.getScanWindows();
00941
00942
00943
00944 query.str("");
00945 query << "DELETE FROM META_ScanWindows WHERE fid_Spectrum='";
00946 query << exp_it->getPersistenceId() << "'";
00947 result = db_con_.executeQuery(query.str());
00948
00949 for (Size wins_it = 0; wins_it < wins.size(); wins_it++)
00950 {
00951 query.str("");
00952 query << "INSERT INTO META_ScanWindows SET ";
00953 query << "fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00954 query << ",MZRangeBegin=" << settings.getScanWindows()[wins_it].begin;
00955 query << ",MZRangeEnd=" << settings.getScanWindows()[wins_it].end;
00956 result = db_con_.executeQuery(query.str());
00957 storeMetaInfo_("META_ScanWindows", parent_id, settings.getScanWindows()[wins_it]);
00958
00959 }
00960
00961
00962
00963
00964
00965 const AcquisitionInfo & info = exp_it->getAcquisitionInfo();
00966
00967 query.str("");
00968
00969 if (new_entry)
00970 {
00971 query << "INSERT INTO META_AcquisitionInfo SET fid_Spectrum='" << exp_it->getPersistenceId() << "',";
00972 end = "";
00973 }
00974 else
00975 {
00976 query << "SELECT id FROM META_AcquisitionInfo WHERE fid_Spectrum='" << exp_it->getPersistenceId() << "'";
00977 result = db_con_.executeQuery(query.str(), true);
00978 acquisition_info_id = result.value(0).toInt();
00979
00980 query.str("");
00981 query << "UPDATE META_AcquisitionInfo SET ";
00982 end = " WHERE fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00983 }
00984
00985 query << "MethodOfCombination='" << info.getMethodOfCombination() << "'";
00986 query << end;
00987
00988 result = db_con_.executeQuery(query.str());
00989 if (new_entry)
00990 {
00991 acquisition_info_id = db_con_.getAutoId();
00992 }
00993
00994
00995
00996
00997
00998 query.str("");
00999 deleteMetaInfo_("META_Acquisition", "fid_AcquisitionInfo='" + String(parent_id) + "'");
01000 query << "DELETE FROM META_Acquisition WHERE fid_AcquisitionInfo='" << parent_id << "'";
01001 result = db_con_.executeQuery(query.str());
01002
01003 for (std::vector<Acquisition>::const_iterator info_it = info.begin(); info_it != info.end(); info_it++)
01004 {
01005 query.str("");
01006 query << "INSERT INTO META_Acquisition SET fid_AcquisitionInfo='" << acquisition_info_id << "',";
01007 query << "Number='" << info_it->getIdentifier() << "'";
01008
01009 result = db_con_.executeQuery(query.str());
01010 parent_id = db_con_.getAutoId();
01011
01012 storeMetaInfo_("META_Acquisition", parent_id, *info_it);
01013 }
01014
01015
01016
01017
01018
01019 const std::vector<DataProcessing> & processings = exp_it->getDataProcessing();
01020
01021 deleteMetaInfo_("META_DataProcessing", "fid_Spectrum=" + String(exp_it->getPersistenceId()));
01022 query.str("");
01023 query << "DELETE FROM META_DataProcessing WHERE fid_Spectrum='" << exp_it->getPersistenceId() << "'";
01024 result = db_con_.executeQuery(query.str());
01025
01026 for (std::vector<DataProcessing>::const_iterator processings_it = processings.begin(); processings_it != processings.end(); processings_it++)
01027 {
01028 query.str("");
01029 query << "INSERT INTO META_DataProcessing SET ";
01030 query << "fid_Spectrum='" << exp_it->getPersistenceId() << "'";
01031 query << ",CompletionTime='" << processings_it->getCompletionTime().get() << "'";
01032
01033 result = db_con_.executeQuery(query.str());
01034
01035 UID dataprocessing_id = db_con_.getAutoId();
01036 storeMetaInfo_("META_DataProcessing", dataprocessing_id, *processings_it);
01037
01038 deleteMetaInfo_("META_Software", "SoftwareApplicator='META_DataProcessing' AND fid_SoftwareApplicator=" + String(dataprocessing_id));
01039 query.str("");
01040 query << "DELETE FROM META_Software WHERE fid_SoftwareApplicator='" << dataprocessing_id << "' AND SoftwareApplicator='META_DataProcessing'";
01041 result = db_con_.executeQuery(query.str());
01042 query.str("");
01043 query << "INSERT INTO META_Software SET ";
01044 query << "fid_SoftwareApplicator='" << dataprocessing_id << "'";
01045 query << ",SoftwareApplicator='META_DataProcessing'";
01046 query << ",Name='" << processings_it->getSoftware().getName() << "'";
01047 query << ",Version='" << processings_it->getSoftware().getVersion() << "'";
01048 result = db_con_.executeQuery(query.str());
01049
01050 UID software_id = db_con_.getAutoId();
01051 storeMetaInfo_("META_Software", software_id, processings_it->getSoftware());
01052
01053
01054
01055
01056 for (std::set<DataProcessing::ProcessingAction>::const_iterator acts_it = processings_it->getProcessingActions().begin(); acts_it != processings_it->getProcessingActions().end(); acts_it++)
01057 {
01058 query.str("");
01059 query << "INSERT INTO META_ProcessingActions SET ";
01060 query << "ProcessingActionType='" << (1u + (*acts_it)) << "'";
01061 query << ",fid_DataProcessing='" << dataprocessing_id << "'";
01062 result = db_con_.executeQuery(query.str());
01063 }
01064 }
01065
01066 }
01067 }
01068
01069 template <class ExperimentType>
01070 void DBAdapter::loadExperiment(UID id, ExperimentType & exp)
01071 {
01072
01073
01074
01075 if (!checkDBVersion(true))
01076 return;
01077
01078 std::stringstream query;
01079 String tmp;
01080 QSqlQuery result, sub_result;
01081 UID parent_id;
01082
01083
01084
01085
01086
01087 query << "SELECT Date,fid_MetaInfo,Description,FractionIdentifier FROM META_MSExperiment WHERE id='" << id << "'";
01088 result = db_con_.executeQuery(query.str(), true);
01089
01090
01091 try
01092 {
01093 DateTime d;
01094 d.set(result.value(0).toDateTime().toString(Qt::ISODate));
01095 exp.setDateTime(d);
01096 }
01097 catch (Exception::ParseError &)
01098 {
01099
01100 }
01101 exp.setComment(result.value(2).toString());
01102 exp.setFractionIdentifier(result.value(3).toString());
01103 loadMetaInfo_(result.value(1).toInt(), exp);
01104
01105 std::vector<ProteinIdentification> pi_vec;
01106 std::vector<ProteinHit> ph_vec;
01107 ProteinIdentification pi;
01108 ProteinHit ph;
01109
01110 query.str("");
01111 query << "SELECT id, SearchEngine, SearchEngineVersion, Date, ScoreType, HigherScoreBetter, SignificanceThreshold, fid_MetaInfo, fid_File FROM ID_ProteinIdentification WHERE fid_MSExperiment='"<< id << "'";
01112 result = db_con_.executeQuery(query.str());
01113 while (result.next())
01114 {
01115 parent_id = result.value(0).toInt();
01116 pi.setSearchEngine(result.value(1).toString());
01117 pi.setSearchEngineVersion(result.value(2).toString());
01118 pi.setDateTime(DateTime(result.value(3).toDateTime()));
01119 pi.setScoreType(result.value(4).toString());
01120 pi.setHigherScoreBetter(result.value(5).toInt());
01121 pi.setSignificanceThreshold(result.value(6).toDouble());
01122
01123 loadMetaInfo_(result.value(7).toInt(), pi);
01124
01125
01126
01127
01128 query.str("");
01129 query << "SELECT id,DB,DBVersion,Taxonomy,Charges,MassType-1,Enzyme-1,MissedCleavages,PeakMassTolerance,PrecursorTolerance,fid_MetaInfo FROM ID_SearchParameters WHERE fid_ProteinIdentification='" << parent_id << "'";
01130 sub_result = db_con_.executeQuery(query.str(), true);
01131
01132 UID sub_id = sub_result.value(0).toInt();
01133 ProteinIdentification::SearchParameters params(pi.getSearchParameters());
01134 params.db = sub_result.value(1).toString();
01135 params.db_version = sub_result.value(2).toString();
01136 params.taxonomy = sub_result.value(3).toString();
01137 params.charges = sub_result.value(4).toString();
01138 params.mass_type = ((ProteinIdentification::PeakMassType)sub_result.value(5).toInt());
01139 params.enzyme = ((ProteinIdentification::DigestionEnzyme)sub_result.value(6).toInt());
01140 params.missed_cleavages = sub_result.value(7).toInt();
01141 params.peak_mass_tolerance = sub_result.value(8).toDouble();
01142 params.precursor_tolerance = sub_result.value(9).toDouble();
01143 loadMetaInfo_(sub_result.value(10).toInt(), params);
01144
01145
01146 query.str("");
01147 query << "SELECT name FROM ID_VariableModifications WHERE fid_SearchParameters='" << sub_id << "'";
01148 sub_result = db_con_.executeQuery(query.str());
01149 while (sub_result.next())
01150 {
01151 params.variable_modifications.push_back(sub_result.value(0).toString());
01152 }
01153 query.str("");
01154 query << "SELECT name FROM ID_FixedModifications WHERE fid_SearchParameters='" << sub_id << "'";
01155 sub_result = db_con_.executeQuery(query.str());
01156 while (sub_result.next())
01157 {
01158 params.fixed_modifications.push_back(sub_result.value(0).toString());
01159 }
01160 pi.setSearchParameters(params);
01161
01162
01163 query.str("");
01164 query << "SELECT Score, Accession, Sequence, Rank, fid_MetaInfo FROM ID_ProteinHit WHERE fid_ProteinIdentification='" << parent_id << "'";
01165 sub_result = db_con_.executeQuery(query.str());
01166 while (sub_result.next())
01167 {
01168 ph.setScore(sub_result.value(0).toDouble());
01169 ph.setAccession(sub_result.value(1).toString());
01170 ph.setSequence(sub_result.value(2).toString());
01171 ph.setRank(sub_result.value(3).toInt());
01172
01173 loadMetaInfo_(sub_result.value(4).toInt(), ph);
01174
01175 ph_vec.push_back(ph);
01176 }
01177
01178 pi.setHits(ph_vec);
01179
01180 pi_vec.push_back(pi);
01181 }
01182
01183 exp.setProteinIdentifications(pi_vec);
01184
01185
01186 Sample sample;
01187 query.str("");
01188
01189 query << "SELECT id FROM META_Sample WHERE fid_MSExperiment='" << id << "' AND fid_Sample IS NULL";
01190 result = db_con_.executeQuery(query.str(), true);
01191 loadSample_(result.value(0).toInt(), sample);
01192 exp.setSample(sample);
01193
01194
01195 ContactPerson contact;
01196 query.str("");
01197 query << "SELECT PreName,LastName,Affiliation,Email,Comment,fid_MetaInfo FROM META_ContactPerson WHERE fid_MSExperiment='" << id << "'";
01198 result = db_con_.executeQuery(query.str());
01199 while (result.next())
01200 {
01201 contact.setFirstName(result.value(0).toString());
01202 contact.setLastName(result.value(1).toString());
01203 contact.setInstitution(result.value(2).toString());
01204 contact.setEmail(result.value(3).toString());
01205 contact.setContactInfo(result.value(4).toString());
01206 loadMetaInfo_(result.value(5).toInt(), contact);
01207 exp.getContacts().push_back(contact);
01208 }
01209
01210
01211 query.str("");
01212 query << "SELECT id,InstrumentName,ColumnName,Description,Flux,Pressure,Temperature FROM META_HPLC WHERE fid_MSExperiment='" << id << "'";
01213 result = db_con_.executeQuery(query.str(), true);
01214 parent_id = result.value(0).toInt();
01215 exp.getHPLC().setInstrument(result.value(1).toString());
01216 exp.getHPLC().setColumn(result.value(2).toString());
01217 exp.getHPLC().setComment(result.value(3).toString());
01218 exp.getHPLC().setFlux(result.value(4).toInt());
01219 exp.getHPLC().setPressure(result.value(5).toInt());
01220 exp.getHPLC().setTemperature(result.value(6).toInt());
01221
01222
01223
01224
01225
01226
01227 String last_name;
01228 bool timepoints_done = false;
01229 query.str("");
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247 query << "SELECT Name,Time,Percentage FROM META_GradientEluent, META_GradientTime, META_GradientPercentage WHERE META_GradientEluent.fid_HPLC=" << parent_id << " AND fid_GradientEluent=META_GradientEluent.id AND fid_GradientTime=META_GradientTime.id";
01248 result = db_con_.executeQuery(query.str(), true);
01249 if (result.isValid())
01250 {
01251 last_name = result.value(0).toString();
01252 exp.getHPLC().getGradient().addEluent(last_name);
01253 }
01254
01255 while (result.isValid())
01256 {
01257 if (result.value(0).toString() != last_name.toQString())
01258 {
01259 exp.getHPLC().getGradient().addEluent(result.value(0).toString());
01260 timepoints_done = true;
01261 }
01262
01263 if (timepoints_done == false)
01264 {
01265 exp.getHPLC().getGradient().addTimepoint(result.value(1).toInt());
01266 }
01267
01268 exp.getHPLC().getGradient().setPercentage(result.value(0).toString(), result.value(1).toInt(), result.value(2).toInt());
01269
01270 last_name = result.value(0).toString();
01271 result.next();
01272 }
01273
01274
01275
01276
01277
01278 query.str("");
01279 query << "SELECT id,Model,Vendor,Description,IonOpticsType-1,fid_MetaInfo FROM META_MSInstrument WHERE fid_MSExperiment='" << id << "'";
01280 result = db_con_.executeQuery(query.str(), true);
01281
01282 parent_id = result.value(0).toInt();
01283 exp.getInstrument().setModel(result.value(1).toString());
01284 exp.getInstrument().setVendor(result.value(2).toString());
01285 exp.getInstrument().setCustomizations(result.value(3).toString());
01286 exp.getInstrument().setIonOptics((Instrument::IonOpticsType) result.value(4).toInt());
01287 loadMetaInfo_(result.value(5).toInt(), exp.getInstrument());
01288
01289 query.str("");
01290 query << "SELECT Name,Version,fid_MetaInfo, id FROM META_Software WHERE fid_SoftwareApplicator='" << result.value(0).toInt() << "' AND SoftwareApplicator = 'META_MSInstrument'";
01291 result = db_con_.executeQuery(query.str(), true);
01292 if (result.isValid())
01293 {
01294 Software sw;
01295 sw.setName(result.value(0).toString());
01296 sw.setVersion(result.value(1).toString());
01297 loadMetaInfo_(result.value(2).toInt(), sw);
01298
01299 exp.getInstrument().setSoftware(sw);
01300 }
01301
01302
01303
01304
01305 std::vector<IonDetector> detectors;
01306 query.str("");
01307 query << "SELECT AcquisitionMode-1,Type-1,Resolution,ADCSamplingFrequency,InstrumentOrder,fid_MetaInfo FROM META_IonDetector WHERE fid_MSInstrument='" << parent_id << "'";
01308 result = db_con_.executeQuery(query.str());
01309 while (result.next())
01310 {
01311 IonDetector detector;
01312 detector.setAcquisitionMode((IonDetector::AcquisitionMode) result.value(0).toInt());
01313 detector.setType((IonDetector::Type) result.value(1).toInt());
01314 detector.setResolution(result.value(2).toDouble());
01315 detector.setADCSamplingFrequency(result.value(3).toDouble());
01316 detector.setOrder(result.value(4).toInt());
01317 loadMetaInfo_(result.value(5).toInt(), detector);
01318
01319 detectors.push_back(detector);
01320 }
01321 exp.getInstrument().setIonDetectors(detectors);
01322
01323
01324
01325
01326 std::vector<IonSource> sources;
01327 query.str("");
01328 query << "SELECT InletType-1,IonizationMethod-1,IonizationMode-1,InstrumentOrder,fid_MetaInfo FROM META_IonSource WHERE fid_MSInstrument='" << parent_id << "'";
01329 result = db_con_.executeQuery(query.str());
01330 while (result.next())
01331 {
01332 IonSource source;
01333 source.setInletType((IonSource::InletType) result.value(0).toInt());
01334 source.setIonizationMethod((IonSource::IonizationMethod) result.value(1).toInt());
01335 source.setPolarity((IonSource::Polarity)(Int) result.value(2).toDouble());
01336 source.setOrder(result.value(3).toInt());
01337 loadMetaInfo_(result.value(4).toInt(), source);
01338
01339 sources.push_back(source);
01340 }
01341 exp.getInstrument().setIonSources(sources);
01342
01343
01344
01345
01346 std::vector<MassAnalyzer> analyzers;
01347 query.str("");
01348 query << "SELECT Accuracy,FinalMSExponent,IsolationWidth,MagneticFieldStrength,ReflectronState-1,Resolution,ResolutionMethod-1,ResolutionType-1,ScanDirection-1,ScanLaw-1,ScanRate,ScanTime,TOFPathLength,Type-1,InstrumentOrder,fid_MetaInfo FROM META_MassAnalyzer WHERE fid_MSInstrument='" << parent_id << "'";
01349 result = db_con_.executeQuery(query.str());
01350 while (result.next())
01351 {
01352 MassAnalyzer analyzer;
01353 analyzer.setAccuracy(result.value(0).toDouble());
01354 analyzer.setFinalMSExponent(result.value(1).toInt());
01355 analyzer.setIsolationWidth(result.value(2).toDouble());
01356 analyzer.setMagneticFieldStrength(result.value(3).toDouble());
01357 analyzer.setReflectronState((MassAnalyzer::ReflectronState) result.value(4).toInt());
01358 analyzer.setResolution(result.value(5).toDouble());
01359 analyzer.setResolutionMethod((MassAnalyzer::ResolutionMethod) result.value(6).toInt());
01360 analyzer.setResolutionType((MassAnalyzer::ResolutionType) result.value(7).toInt());
01361 analyzer.setScanDirection((MassAnalyzer::ScanDirection) result.value(8).toInt());
01362 analyzer.setScanLaw((MassAnalyzer::ScanLaw) result.value(9).toInt());
01363 analyzer.setScanRate(result.value(10).toDouble());
01364 analyzer.setScanTime(result.value(11).toDouble());
01365 analyzer.setTOFTotalPathLength(result.value(12).toDouble());
01366 analyzer.setType((MassAnalyzer::AnalyzerType) result.value(13).toInt());
01367 analyzer.setOrder(result.value(14).toInt());
01368 loadMetaInfo_(result.value(15).toInt(), analyzer);
01369
01370 analyzers.push_back(analyzer);
01371 }
01372 exp.getInstrument().setMassAnalyzers(analyzers);
01373
01374
01375 exp.setPersistenceId(id);
01376
01377
01378 if (options_.getMetadataOnly())
01379 {
01380 return;
01381 }
01382
01383
01384
01385
01386 query.str("");
01387 query << "SELECT id FROM DATA_Spectrum WHERE fid_MSExperiment=" << id;
01388 if (options_.hasRTRange())
01389 {
01390 query << " AND RetentionTime > " << options_.getRTRange().minPosition() << " AND RetentionTime < " << options_.getRTRange().maxPosition();
01391 }
01392 if (options_.hasMSLevels())
01393 {
01394 const std::vector<int> & levels = options_.getMSLevels();
01395 query << " AND (";
01396 for (std::vector<int>::const_iterator it = levels.begin(); it != levels.end(); it++)
01397 {
01398 query << "MSLevel=" << *it;
01399 if (it + 1 != levels.end())
01400 {
01401 query << " OR ";
01402 }
01403 }
01404 query << ")";
01405 }
01406 query << " ORDER BY id ASC";
01407
01408 result = db_con_.executeQuery(query.str());
01409 exp.resize(result.size());
01410 UInt i = 0;
01411 while (result.next())
01412 {
01413 loadSpectrum(result.value(0).toInt(), exp[i]);
01414 ++i;
01415 }
01416
01417 }
01418
01419 template <class SpectrumType>
01420 void DBAdapter::loadSpectrum(UID id, SpectrumType & spec)
01421 {
01422
01423
01424
01425
01426 if (!checkDBVersion(true))
01427 return;
01428
01429 spec = SpectrumType();
01430
01431 std::stringstream query;
01432 QSqlQuery result, sub_result;
01433 InstrumentSettings settings;
01434 UID parent_id;
01435
01436 query << "SELECT Type-1,NativeID, RetentionTime,MSLevel,Description,fid_MetaInfo,fid_File FROM DATA_Spectrum WHERE id='" << id << "'";
01437 result = db_con_.executeQuery(query.str(), true);
01438
01439
01440 spec.setType((SpectrumSettings::SpectrumType)(result.value(0).toInt()));
01441 spec.setNativeID(result.value(1).toString());
01442 spec.setRT(result.value(2).toDouble());
01443 spec.setMSLevel(result.value(3).toInt());
01444 spec.setComment(result.value(4).toString());
01445 loadMetaInfo_(result.value(5).toInt(), spec);
01446 loadFile_(result.value(6).toInt(), spec.getSourceFile());
01447
01448
01449
01450
01451 query.str("");
01452 query << "SELECT Polarity-1, ScanMode-1, ZoomScan, fid_MetaInfo FROM META_InstrumentSettings WHERE fid_Spectrum=" << id;
01453 result = db_con_.executeQuery(query.str(), true);
01454
01455 settings.setPolarity((IonSource::Polarity) (result.value(0).toInt()));
01456 settings.setScanMode((InstrumentSettings::ScanMode) (result.value(1).toInt()));
01457 settings.setZoomScan(result.value(2).toBool());
01458 spec.setInstrumentSettings(settings);
01459 loadMetaInfo_(result.value(3).toInt(), spec.getInstrumentSettings());
01460
01461
01462
01463
01464 query.str("");
01465 query << "SELECT MZRangeBegin,MZRangeEnd,fid_MetaInfo FROM META_ScanWindows WHERE fid_Spectrum=" << id;
01466 result = db_con_.executeQuery(query.str());
01467 while (result.next())
01468 {
01469 ScanWindow window;
01470 window.begin = result.value(0).toDouble();
01471 window.end = result.value(1).toDouble();
01472 loadMetaInfo_(result.value(2).toInt(), window);
01473 spec.getInstrumentSettings().getScanWindows().push_back(window);
01474 }
01475
01476
01477
01478
01479
01480
01481 std::vector<PeptideIdentification> pei_vec;
01482 std::vector<PeptideHit> peh_vec;
01483 PeptideIdentification pei;
01484 PeptideHit peh;
01485
01486 query.str("");
01487 query << "SELECT id, SignificanceThreshold, ScoreType, HigherScoreBetter, fid_MetaInfo, fid_File FROM ID_PeptideIdentification WHERE fid_Spectrum='" << id << "'";
01488
01489 result = db_con_.executeQuery(query.str());
01490 while (result.next())
01491 {
01492 parent_id = result.value(0).toInt();
01493 pei.setSignificanceThreshold(result.value(1).toDouble());
01494 pei.setScoreType(result.value(2).toString());
01495 pei.setHigherScoreBetter(result.value(3).toInt());
01496
01497 loadMetaInfo_(result.value(4).toInt(), pei);
01498
01499
01500
01501 query.str("");
01502 query << "SELECT Score, Sequence, Charge, AABefore, AAAfter, fid_MetaInfo FROM ID_PeptideHit WHERE fid_Identification='" << parent_id << "'";
01503 sub_result = db_con_.executeQuery(query.str());
01504
01505 while (sub_result.next())
01506 {
01507 peh.setScore(sub_result.value(0).toDouble());
01508 peh.setSequence(String(sub_result.value(1).toString()));
01509 peh.setCharge(sub_result.value(2).toInt());
01510 peh.setAABefore(sub_result.value(3).toString().toStdString()[0]);
01511 peh.setAAAfter(sub_result.value(4).toString().toStdString()[0]);
01512
01513 loadMetaInfo_(sub_result.value(5).toInt(), peh);
01514
01515 peh_vec.push_back(peh);
01516 }
01517
01518 pei.setHits(peh_vec);
01519
01520 pei_vec.push_back(pei);
01521 }
01522
01523 spec.setPeptideIdentifications(pei_vec);
01524
01525
01526
01527
01528
01529 query.str("");
01530 query << "SELECT id, MethodOfCombination FROM META_AcquisitionInfo WHERE fid_Spectrum=" << id;
01531 result = db_con_.executeQuery(query.str(), true);
01532
01533 spec.getAcquisitionInfo().setMethodOfCombination(result.value(1).toString());
01534 parent_id = result.value(0).toInt();
01535
01536
01537
01538
01539 query.str("");
01540 query << "SELECT Number,fid_MetaInfo FROM META_Acquisition WHERE fid_AcquisitionInfo='" << parent_id << "' ORDER BY id ASC";
01541 result = db_con_.executeQuery(query.str());
01542
01543 while (result.next())
01544 {
01545 Acquisition acquisition;
01546 acquisition.setIdentifier(result.value(0).toString());
01547 loadMetaInfo_(result.value(1).toInt(), acquisition);
01548 spec.getAcquisitionInfo().push_back(acquisition);
01549 }
01550
01551
01552
01553
01554 query.str("");
01555 query << "SELECT CompletionTime,fid_MetaInfo, id FROM META_DataProcessing WHERE fid_Spectrum='" << id << "'";
01556 result = db_con_.executeQuery(query.str());
01557
01558 while (result.next())
01559 {
01560 DataProcessing processings;
01561
01562 try
01563 {
01564 DateTime d;
01565 d.set(result.value(0).toDateTime().toString(Qt::ISODate));
01566 processings.setCompletionTime(d);
01567 }
01568 catch (Exception::ParseError &)
01569 {
01570
01571 }
01572
01573 loadMetaInfo_(result.value(1).toInt(), processings);
01574
01575 query.str("");
01576 query << "SELECT ProcessingActionType-1 FROM META_ProcessingActions WHERE fid_DataProcessing='" << result.value(2).toInt() << "'";
01577 sub_result = db_con_.executeQuery(query.str());
01578 while (sub_result.next())
01579 {
01580 processings.getProcessingActions().insert((DataProcessing::ProcessingAction)sub_result.value(0).toInt());
01581 }
01582
01583 query.str("");
01584 query << "SELECT Name,Version,fid_MetaInfo, id FROM META_Software WHERE fid_SoftwareApplicator='" << result.value(2).toInt() << "' AND SoftwareApplicator = 'META_DataProcessing'";
01585 sub_result = db_con_.executeQuery(query.str(), true);
01586 if (sub_result.isValid())
01587 {
01588 Software sw;
01589 sw.setName(sub_result.value(0).toString());
01590 sw.setVersion(sub_result.value(1).toString());
01591 loadMetaInfo_(sub_result.value(2).toInt(), sw);
01592
01593 processings.setSoftware(sw);
01594 }
01595
01596 spec.getDataProcessing().push_back(processings);
01597 }
01598
01599
01600
01601
01602
01603 query.str("");
01604 query << "SELECT Name, fid_MetaInfo FROM META_MetaInfoDescription WHERE fid_Spectrum=" << id;
01605 result = db_con_.executeQuery(query.str());
01606
01607 while (result.next())
01608 {
01609 typename SpectrumType::FloatDataArray meta_array;
01610 meta_array.setName(result.value(0).toString());
01611 loadMetaInfo_(result.value(1).toInt(), meta_array);
01612
01613 spec.getFloatDataArrays().push_back(meta_array);
01614 }
01615
01616
01617
01618
01619 if (spec.getMSLevel() > 1)
01620 {
01621 query.str("");
01622 query << "SELECT WindowMz,Intensity,Charge,ActivationEnergy,WindowLow,WindowUp,fid_MetaInfo,id FROM DATA_Precursor WHERE fid_Spectrum='" << id << "' HAVING Intensity IS NOT NULL";
01623 result = db_con_.executeQuery(query.str());
01624 spec.getPrecursors().resize(result.size());
01625 UInt res = 0;
01626 while (result.next())
01627 {
01628 spec.getPrecursors()[res].setMZ(result.value(0).toDouble());
01629 spec.getPrecursors()[res].setIntensity(result.value(1).toDouble());
01630 spec.getPrecursors()[res].setCharge(result.value(2).toInt());
01631 spec.getPrecursors()[res].setActivationEnergy(result.value(3).toDouble());
01632 spec.getPrecursors()[res].setIsolationWindowLowerOffset(result.value(4).toDouble());
01633 spec.getPrecursors()[res].setIsolationWindowUpperOffset(result.value(5).toDouble());
01634 loadMetaInfo_(result.value(6).toInt(), spec.getPrecursors()[res]);
01635
01636 UID prec_id = result.value(7).toInt();
01637 query.str("");
01638 query << "SELECT PossibleChargeStates FROM DATA_PrecursorPCS WHERE fid_Precursor='" << prec_id << "' HAVING PossibleChargeStates IS NOT NULL";
01639 QSqlQuery subresult = db_con_.executeQuery(query.str());
01640 while (subresult.next())
01641 {
01642 spec.getPrecursors()[res].getPossibleChargeStates().push_back(subresult.value(0).toInt());
01643 }
01644
01645 query.str("");
01646 query << "SELECT ActivationMethods-1 FROM DATA_PrecursorAM WHERE fid_Precursor='" << prec_id << "'";
01647 subresult = db_con_.executeQuery(query.str());
01648 std::set<Precursor::ActivationMethod> tmp_set;
01649 while (subresult.next())
01650 {
01651 tmp_set.insert((Precursor::ActivationMethod)(subresult.value(0).toInt()));
01652 }
01653 spec.getPrecursors()[res].setActivationMethods(tmp_set);
01654 ++res;
01655 }
01656 }
01657
01658
01659
01660
01661 query.str("");
01662 query << "SELECT WindowMz,WindowLow,WindowUp,fid_MetaInfo FROM DATA_Products WHERE fid_Spectrum='" << id << "'";
01663 result = db_con_.executeQuery(query.str());
01664 spec.getProducts().resize(result.size());
01665 UInt res = 0;
01666 while (result.next())
01667 {
01668 spec.getProducts()[res].setMZ(result.value(0).toDouble());
01669 spec.getProducts()[res].setIsolationWindowLowerOffset(result.value(1).toDouble());
01670 spec.getProducts()[res].setIsolationWindowUpperOffset(result.value(2).toDouble());
01671 loadMetaInfo_(result.value(3).toInt(), spec.getProducts()[res]);
01672 ++res;
01673 }
01674
01675
01676
01677
01678
01679 query.str("");
01680 query << "SELECT mz,Intensity,fid_MetaInfo,id FROM DATA_Peak WHERE fid_Spectrum='" << id << "' ";
01681 if (options_.hasMZRange())
01682 {
01683 query << " AND mz > " << options_.getMZRange().minPosition() << " AND mz < " << options_.getMZRange().maxPosition();
01684 }
01685 if (options_.hasIntensityRange())
01686 {
01687 query << " AND Intensity > " << options_.getIntensityRange().minPosition() << " AND Intensity < " << options_.getIntensityRange().maxPosition();
01688 }
01689 query << " ORDER BY mz ASC";
01690 result = db_con_.executeQuery(query.str());
01691
01692 while (result.next())
01693 {
01694 typename SpectrumType::PeakType p;
01695 p.setPosition(result.value(0).toDouble());
01696 p.setIntensity(result.value(1).toDouble());
01697 loadMetaInfo_(result.value(2).toInt(), p);
01698 spec.push_back(p);
01699 for (typename SpectrumType::FloatDataArrays::iterator mdarrays_it = spec.getFloatDataArrays().begin(); mdarrays_it != spec.getFloatDataArrays().end(); mdarrays_it++)
01700 {
01701 query.str("");
01702 query << "SELECT id FROM META_MetaInfoDescription WHERE Name='";
01703 query << mdarrays_it->getName() << "' AND fid_Spectrum=" << id;
01704 sub_result = db_con_.executeQuery(query.str(), true);
01705 query.str("");
01706 query << "SELECT Value FROM DATA_PeakMetaData WHERE fid_Peak=";
01707 query << result.value(3).toInt() << " AND fid_MetaInfoDescription=" << sub_result.value(0).toInt();
01708 sub_result = db_con_.executeQuery(query.str(), true);
01709 mdarrays_it->push_back(sub_result.value(0).toDouble());
01710 }
01711 }
01712
01713
01714 spec.setPersistenceId(id);
01715 }
01716
01717 }
01718
01719 #endif