Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

DBAdapter.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_FORMAT_DB_DBADAPTER_H
00028 #define OPENMS_FORMAT_DB_DBADAPTER_H
00029 
00030 //OpenMS includes
00031 #include <OpenMS/DATASTRUCTURES/String.h>
00032 #include <OpenMS/KERNEL/MSExperiment.h>
00033 #include <OpenMS/FORMAT/DB/DBConnection.h>
00034 #include <OpenMS/METADATA/Digestion.h>
00035 #include <OpenMS/METADATA/Modification.h>
00036 #include <OpenMS/METADATA/Tagging.h>
00037 #include <OpenMS/FORMAT/PeakFileOptions.h>
00038 
00039 //QT includes
00040 #include <QtSql/QSqlQuery>
00041 #include <QtCore/QVariant>
00042 #include <QtCore/QDate>
00043 
00044 //std and STL includes
00045 #include <string>
00046 #include <map>
00047 
00048 namespace OpenMS
00049 { 
00050   class DBConnection;
00051   
00060   class DBAdapter
00061   {
00062     public:
00064       DBAdapter(DBConnection& db_con);
00065 
00067       ~DBAdapter();
00068 
00070       template <class ExperimentType>
00071       void storeExperiment(ExperimentType& exp);
00072 
00074       template <class ExperimentType>
00075       void loadExperiment(UID id, ExperimentType& exp);
00076 
00077       template <class SpectrumType>
00079       void loadSpectrum(UID id, SpectrumType& spec);
00080 
00082       PeakFileOptions& getOptions();
00083 
00085       const PeakFileOptions& getOptions() const;
00086 
00092       bool checkDBVersion(bool warning);
00093       
00095       void createDB();
00096       
00097     private:
00099       DBConnection& db_con_;
00100       
00102       DBAdapter();
00103       
00109       UID storeMetaInfo_(const String& parent_table, UID parent_id, const MetaInfoInterface& info); 
00110       
00115       void loadMetaInfo_(UID id, MetaInfoInterface& info);
00116       
00121       void deleteMetaInfo_(const String& parent_table, const String& condition);
00122       
00128       UID storeFile_(const String& parent_table, UID parent_id, const SourceFile& file);
00129       
00134       void loadFile_(UID id, SourceFile& file);
00135   
00141       UID storeSample_(const Sample& sample, UID exp_id, UID parent_id);
00142       
00147       void loadSample_(UID id, Sample& sample);
00148 
00149       PeakFileOptions options_;
00150 };
00151 
00152 
00153 //------------------------------------------- IMPLEMENTATION OF TEMPLATE METHODS ----------------------------------
00154 
00155   template <class ExperimentType>
00156   void DBAdapter::storeExperiment(ExperimentType& exp)
00157   {
00158     std::stringstream query; // query to build
00159     String end;              // end of the query that is added afer all fields
00160     String tmp;              // temporary data
00161     bool new_entry(false);          // stores if the current object is already in the DB
00162     QSqlQuery result;        // place to store the query results in
00163     int parent_id(-1);           // stores parent_id of meta information
00164     UID acquisition_info_id(0); // stores id of acquisition_info
00165     UID meta_id(0);             // stores MetaInfo id of meta information that was just stored
00166     UID meta_parent_id(0);      // stores parent ID of MetaInfo that will be stored
00167 
00168     //----------------------------------------------------------------------------------------
00169     //------------------------------- CHECK DB VERSION --------------------------------------- 
00170     //----------------------------------------------------------------------------------------
00171     if (!checkDBVersion(true)) return;
00172     
00173     
00174     
00175     //----------------------------------------------------------------------------------------
00176     //------------------------------- EXPERIMENT --------------------------------------------- 
00177     //----------------------------------------------------------------------------------------
00178     
00179     query.str("");
00180     new_entry = (exp.getPersistenceId()==0);
00181     if (new_entry)
00182     {
00183       query << "INSERT INTO META_MSExperiment SET ";
00184       end = "";
00185     }
00186     else
00187     {
00188       query << "UPDATE META_MSExperiment SET ";
00189       end = " WHERE id='" + String(exp.getPersistenceId()) + "'";
00190     }
00191     //type
00192     query << "Type=" << (1u+exp.getType());
00193     //date
00194     exp.getDate().get(tmp);
00195     query << ",Date='" << tmp << "'";
00196     //description
00197     query << ",Description='" << exp.getComment() << "'";
00198     
00199     query << end;
00200     db_con_.executeQuery(query.str(),result);
00201     if (new_entry)
00202     {
00203       exp.setPersistenceId(db_con_.getAutoId());
00204     }
00205     
00206     storeMetaInfo_("META_MSExperiment", exp.getPersistenceId(), exp);
00207     
00208     //----------------------------------------------------------------------------------------
00209     //------------------------------- PROTEIN IDENTIFICATIONS / HITS-------------------------- 
00210     //----------------------------------------------------------------------------------------
00211 
00212     std::vector<ProteinIdentification>& pi = exp.getProteinIdentifications();
00213     String date;
00214     
00215     // first delete all old values, no matter whether we're updating or not
00216     // do we have to delete the MetaInfo as well?
00217     query.str("");
00218     query << "DELETE FROM ID_ProteinIdentification WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00219     db_con_.executeQuery(query.str(), result);
00220 
00221     for (std::vector<ProteinIdentification>::const_iterator pi_it = pi.begin(); pi_it != pi.end(); pi_it++)
00222     {
00223       query.str("");
00224       query << "INSERT INTO ID_ProteinIdentification SET ";
00225       query << "fid_MSExperiment='" << exp.getPersistenceId() << "'";
00226       query << ",SearchEngine='" << pi_it->getSearchEngine() << "'";
00227       query << ",SearchEngineVersion='" << pi_it->getSearchEngineVersion() << "'";
00228       pi_it->getDateTime().get(date);
00229       query << ",Date='" << date << "'";
00230       query << ",ScoreType='" << pi_it->getScoreType() << "'";
00231       query << ",HigherScoreBetter='" << pi_it->isHigherScoreBetter() << "'";
00232       query << ",SignificanceThreshold='" << pi_it->getSignificanceThreshold() << "'";
00233       
00234       db_con_.executeQuery(query.str(), result);
00235       parent_id = db_con_.getAutoId();
00236       
00237       storeMetaInfo_("ID_ProteinIdentification", parent_id, *pi_it);
00238       //needs getter and setter methods first
00239       //storeFile_("ID_ProteinIdentification", parent_id, pi_it->getSourceFile());
00240 
00241       for (std::vector<ProteinHit>::const_iterator ph_it = pi_it->getHits().begin(); ph_it != pi_it->getHits().end(); ph_it++)
00242       {
00243         query.str("");
00244         query << "INSERT INTO ID_ProteinHit SET ";
00245         query << "fid_ProteinIdentification='" << parent_id << "'";
00246         query << ",Score='" << ph_it->getScore() << "'";
00247         query << ",Accession='" << ph_it->getAccession() << "'";
00248         query << ",Sequence='" << ph_it->getSequence() << "'";
00249         
00250         db_con_.executeQuery(query.str(), result);
00251         meta_id = db_con_.getAutoId();
00252         
00253         storeMetaInfo_("ID_ProteinHit", meta_id, *ph_it);
00254       }
00255     }
00256     
00257     
00258     //----------------------------------------------------------------------------------------
00259     //-------------------------------------- SAMPLE ------------------------------------------ 
00260     //----------------------------------------------------------------------------------------
00261     
00262     query.str("");
00263     deleteMetaInfo_("META_Sample", "fid_MSExperiment=" + String(exp.getPersistenceId()));
00264     // this also deletes all references in META_SampleTreatment, META_Digestion and META_Modification by constraint
00265     query << "DELETE FROM META_Sample WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00266     storeSample_(exp.getSample(), exp.getPersistenceId(), 0);
00267     
00268     //----------------------------------------------------------------------------------------
00269     //-------------------------------------- CONTACTPERSON ----------------------------------- 
00270     //----------------------------------------------------------------------------------------
00271     
00272     const std::vector<ContactPerson>& contacts = exp.getContacts();
00273     
00274     query.str("");
00275     deleteMetaInfo_("META_ContactPerson", "fid_MSExperiment=" + String(exp.getPersistenceId()));
00276     query << "DELETE FROM META_ContactPerson WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00277     db_con_.executeQuery(query.str(), result);
00278       
00279     for (std::vector<ContactPerson>::const_iterator contact_it = contacts.begin(); contact_it != contacts.end(); contact_it++)
00280     {
00281       query.str("");
00282       query << "INSERT INTO META_ContactPerson SET ";
00283       query << "fid_MSExperiment='" << exp.getPersistenceId() << "'";
00284       query << ",PreName='" << contact_it->getFirstName() << "'";
00285       query << ",LastName='" << contact_it->getLastName() << "'";
00286       query << ",Affiliation='" << contact_it->getInstitution() << "'";
00287       query << ",Email='" << contact_it->getEmail() << "'";
00288       query << ",Comment='" << contact_it->getContactInfo() << "'";
00289       
00290       db_con_.executeQuery(query.str(), result);
00291       parent_id = db_con_.getAutoId();
00292       
00293       storeMetaInfo_("META_ContactPerson", parent_id, *contact_it);
00294     }
00295     
00296     //----------------------------------------------------------------------------------------
00297     //-------------------------------------- HPLC -------------------------------------------- 
00298     //----------------------------------------------------------------------------------------
00299     
00300     const HPLC& hplc = exp.getHPLC();
00301     query.str("");
00302     
00303     if (new_entry)
00304     {
00305       query << "INSERT INTO META_HPLC SET ";
00306       query << "fid_MSExperiment='" << exp.getPersistenceId() << "',";
00307       end = "";
00308     }
00309     else
00310     {
00311       query << "SELECT id FROM META_HPLC WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00312       db_con_.executeQuery(query.str(),result);
00313       result.first();
00314       parent_id = result.value(0).toInt();
00315       
00316       query.str("");
00317       query << "UPDATE META_HPLC SET ";
00318       end = " WHERE fid_MSExperiment='" + String (exp.getPersistenceId()) + "'";
00319     }
00320     
00321     query << "InstrumentName='" << hplc.getInstrument() << "'";
00322     query << ",ColumnName='" << hplc.getColumn() << "'";
00323     query << ",Description='" << hplc.getComment() << "'";
00324     query << ",Flux=" << hplc.getFlux();
00325     query << ",Pressure=" << hplc.getPressure();
00326     query << ",Temperature=" << hplc.getTemperature();
00327     
00328     query << end;
00329     db_con_.executeQuery(query.str(),result);
00330     
00331     if (new_entry)
00332     {
00333       parent_id = db_con_.getAutoId();
00334     }
00335     
00336     //----------------------------------------------------------------------------------------
00337     //-------------------------------------- GRADIENT* --------------------------------------- 
00338     //----------------------------------------------------------------------------------------
00339     
00340     const Gradient& gradient = exp.getHPLC().getGradient();
00341     const std::vector <String>& eluents = gradient.getEluents();
00342     const std::vector <Int>& time = gradient.getTimepoints();
00343     const std::vector< std::vector< UInt > >& percentages = gradient.getPercentages();
00344     std::stringstream query_eluents, query_time, query_percentages;
00345     UID eluents_id(0), time_id(0);
00346     
00347     // this also deletes all references in META_GradientPercentage by constraint
00348     query.str("");
00349     query << "DELETE FROM META_GradientEluent WHERE fid_HPLC=" << parent_id;
00350     db_con_.executeQuery(query.str(),result);
00351     query.str("");
00352     query << "DELETE FROM META_GradientTime WHERE fid_HPLC=" << parent_id;
00353     db_con_.executeQuery(query.str(),result);
00354     
00355     if (! eluents.empty())
00356     {
00357       query_eluents.str("");
00358       query_eluents << "INSERT INTO META_GradientEluent (fid_HPLC, Name) VALUES ";
00359       for (std::vector<String>::const_iterator eluents_it = eluents.begin(); eluents_it != eluents.end(); eluents_it++)
00360       {
00361         query_eluents << "(";
00362         query_eluents << parent_id;
00363         query_eluents << ",'" << *eluents_it << "'";
00364         query_eluents << "),";
00365       }
00366       db_con_.executeQuery(String(query_eluents.str()).substr(0,-1),result);
00367       eluents_id = db_con_.getAutoId();
00368     }
00369     
00370     if (! time.empty())
00371     {
00372       query_time.str("");
00373       query_time << "INSERT INTO META_GradientTime (fid_HPLC, Time) VALUES ";
00374       for (std::vector<Int>::const_iterator time_it = time.begin(); time_it != time.end(); time_it++)
00375       {
00376         query_time << "(";
00377         query_time << parent_id;
00378         query_time << "," << *time_it;
00379         query_time << "),";
00380       }
00381       db_con_.executeQuery(String(query_time.str()).substr(0,-1),result);
00382       time_id = db_con_.getAutoId();
00383     }
00384     
00385     if (! percentages.empty() && ! eluents.empty() && ! time.empty())
00386     {
00387       query_percentages.str("");
00388       query_percentages << "INSERT INTO META_GradientPercentage (fid_GradientEluent, fid_GradientTime, Percentage) VALUES ";
00389       int i = 0;
00390       // iterate over eluents
00391       for (std::vector< std::vector< UInt> >::const_iterator percent_outer_it = percentages.begin(); percent_outer_it != percentages.end(); percent_outer_it++)
00392       {
00393         int j = 0;
00394         // iterate over timepoints
00395         for (std::vector< UInt>::const_iterator percent_inner_it = (*percent_outer_it).begin(); percent_inner_it != (*percent_outer_it).end(); percent_inner_it++)
00396         {
00397           query_percentages << "(";
00398           query_percentages << eluents_id + i;
00399           query_percentages << "," << time_id + j;
00400           query_percentages << "," << *percent_inner_it;
00401           query_percentages << "),";
00402           j++;
00403         }
00404         i++;
00405       }
00406       db_con_.executeQuery(String(query_percentages.str()).substr(0,-1),result);
00407     }
00408       
00409     
00410     //----------------------------------------------------------------------------------------
00411     //-------------------------------------- INSTRUMENT -------------------------------------- 
00412     //----------------------------------------------------------------------------------------
00413     
00414     const Instrument& instrument = exp.getInstrument();
00415     query.str("");
00416     
00417     if (new_entry)
00418     {
00419       query << "INSERT INTO META_MSInstrument SET ";
00420       query << "fid_MSExperiment='" << exp.getPersistenceId() << "',";
00421       end = "";
00422     }
00423     else
00424     {
00425       query << "SELECT id FROM META_MSInstrument WHERE fid_MSExperiment='" << exp.getPersistenceId() << "'";
00426       db_con_.executeQuery(query.str(),result);
00427       result.first();
00428       parent_id = result.value(0).toInt();
00429       
00430       query.str("");
00431       query << "UPDATE META_MSInstrument SET ";
00432       end = " WHERE fid_MSExperiment='" + String (exp.getPersistenceId()) + "'";
00433     }
00434     
00435     query << "Model='" << instrument.getModel() << "'";
00436     query << ",Vendor='" << instrument.getVendor() << "'";
00437     query << ",Description='" << instrument.getCustomizations() << "'";
00438     
00439     query << end;
00440     db_con_.executeQuery(query.str(),result);
00441     
00442     if (new_entry)
00443     {
00444       parent_id = db_con_.getAutoId();
00445     }
00446 
00447     storeMetaInfo_("META_MSInstrument", parent_id, instrument);
00448     
00449     //----------------------------------------------------------------------------------------
00450     //-------------------------------------- IONDETECTOR ------------------------------------- 
00451     //----------------------------------------------------------------------------------------
00452     
00453     const IonDetector& detector = exp.getInstrument().getIonDetector();
00454     query.str("");
00455     
00456     if (new_entry)
00457     {
00458       query << "INSERT INTO META_IonDetector SET ";
00459       query << "fid_MSInstrument='" << parent_id << "',";
00460       end = "";
00461     }
00462     else
00463     {
00464       query << "SELECT id FROM META_IonDetector WHERE fid_MSInstrument='" << parent_id << "'";
00465       db_con_.executeQuery(query.str(),result);
00466       result.first();
00467       meta_parent_id = result.value(0).toInt();
00468       
00469       query.str("");
00470       query << "UPDATE META_IonDetector SET ";
00471       end = " WHERE fid_MSInstrument='" + String (parent_id) + "'";
00472     }
00473     
00474     query << "AcquisitionMode=" << (1u+detector.getAcquisitionMode());
00475     query << ",Type=" << (1u+detector.getType());
00476     query << ",Resolution=" << detector.getResolution();
00477     query << ",ADCSamplingFrequency=" << detector.getADCSamplingFrequency();
00478     
00479     query << end;
00480     db_con_.executeQuery(query.str(),result);
00481     
00482     if (new_entry)
00483     {
00484       meta_parent_id = db_con_.getAutoId();
00485     }
00486 
00487     storeMetaInfo_("META_IonDetector", meta_parent_id, detector);
00488     
00489     //----------------------------------------------------------------------------------------
00490     //-------------------------------------- IONSOURCE --------------------------------------- 
00491     //----------------------------------------------------------------------------------------
00492     
00493     const IonSource& source = exp.getInstrument().getIonSource();
00494     query.str("");
00495     
00496     if (new_entry)
00497     {
00498       query << "INSERT INTO META_IonSource SET ";
00499       query << "fid_MSInstrument='" << parent_id << "',";
00500       end = "";
00501     }
00502     else
00503     {
00504       query << "SELECT id FROM META_IonSource WHERE fid_MSInstrument='" << parent_id << "'";
00505       db_con_.executeQuery(query.str(),result);
00506       
00507       query.str("");
00508       query << "UPDATE META_IonSource SET ";
00509       end = " WHERE fid_MSInstrument='" + String (parent_id) + "'";
00510       
00511       result.first();
00512       meta_parent_id = result.value(0).toInt();
00513     }
00514     
00515     query << "InletType=" << (1u+source.getInletType());
00516     query << ",IonizationMethod=" << (1u+source.getIonizationMethod());
00517     query << ",IonizationMode=" << (1u+source.getPolarity());
00518     
00519     query << end;
00520     db_con_.executeQuery(query.str(),result);
00521     
00522     if (new_entry)
00523     {
00524       meta_parent_id = db_con_.getAutoId();
00525     }
00526 
00527     storeMetaInfo_("META_IonSource", meta_parent_id, source);
00528     
00529     //----------------------------------------------------------------------------------------
00530     //-------------------------------------- MASSANALYZER ------------------------------------ 
00531     //----------------------------------------------------------------------------------------
00532     
00533     const std::vector<MassAnalyzer>& analyzers = exp.getInstrument().getMassAnalyzers();
00534     query.str("");
00535     
00536     deleteMetaInfo_("META_MassAnalyzer", "fid_MSInstrument=" + String(parent_id));
00537     query << "DELETE FROM META_MassAnalyzer WHERE fid_MSInstrument='" << parent_id << "'";
00538     db_con_.executeQuery(query.str(),result);
00539       
00540     for (std::vector<MassAnalyzer>::const_iterator analyzer_it = analyzers.begin(); analyzer_it != analyzers.end(); analyzer_it++)
00541     {
00542       query.str("");
00543       query << "INSERT INTO META_MassAnalyzer SET ";
00544       query << "fid_MSInstrument='" << parent_id << "'";
00545       query << ",Accuracy=" << analyzer_it->getAccuracy();
00546       query << ",FinalMSExponent=" << analyzer_it->getFinalMSExponent();
00547       query << ",IsolationWidth=" << analyzer_it->getIsolationWidth();
00548       query << ",MagneticFieldStrength=" << analyzer_it->getMagneticFieldStrength();
00549       query << ",ReflectronState=" << (1u+analyzer_it->getReflectronState());
00550       query << ",Resolution=" << analyzer_it->getResolution();
00551       query << ",ResolutionMethod=" << (1u+analyzer_it->getResolutionMethod());
00552       query << ",ResolutionType=" << (1u+analyzer_it->getResolutionType());
00553       query << ",ScanDirection=" << (1u+analyzer_it->getScanDirection());
00554       query << ",ScanFunction=" << (1u+analyzer_it->getScanFunction());
00555       query << ",ScanLaw=" << (1u+analyzer_it->getScanLaw());
00556       query << ",ScanRate=" << analyzer_it->getScanRate();
00557       query << ",ScanTime=" << analyzer_it->getScanTime();
00558       query << ",TandemScanningMethod=" << (1u+analyzer_it->getTandemScanMethod());
00559       query << ",TOFPathLength=" << analyzer_it->getTOFTotalPathLength();
00560       query << ",Type=" << (1u+analyzer_it->getType());
00561       
00562       db_con_.executeQuery(query.str(),result);
00563       storeMetaInfo_("META_MassAnalyzer", db_con_.getAutoId(), *analyzer_it);
00564     }
00565     
00566     //----------------------------------------------------------------------------------------
00567     //-------------------------------------- SPECTRUM ---------------------------------------- 
00568     //----------------------------------------------------------------------------------------
00569     for (typename ExperimentType::Iterator exp_it = exp.begin(); exp_it != exp.end(); ++exp_it)
00570     {
00571       query.str("");
00572       new_entry = (exp_it->getPersistenceId()==0);
00573       if (new_entry)
00574       {
00575         query << "INSERT INTO DATA_Spectrum SET ";
00576         end = "";
00577       }
00578       else
00579       {
00580         query << "UPDATE DATA_Spectrum SET ";
00581         end = " WHERE id='" + String (exp_it->getPersistenceId()) + "'";
00582       }
00583       //FC (MSExperiment)
00584       query << "fid_MSExperiment='" << exp.getPersistenceId() << "'";
00585       //type
00586       query << ",Type=" << (1u+exp_it->getType());
00587       //RT
00588       query << ",RetentionTime='" << exp_it->getRT() << "'";
00589       //MS-Level
00590       query << ",MSLevel='" << exp_it->getMSLevel() << "'";
00591       //Description
00592       query << ",Description='" << exp_it->getComment() << "'";
00593       
00594       //TODO: MassType (average/monoisotopic)
00595       //TODO: TIC
00596       
00597       
00598       query << end;
00599       db_con_.executeQuery(query.str(),result);
00600       if (new_entry)
00601       {
00602         exp_it->setPersistenceId(db_con_.getAutoId());
00603       }
00604       storeFile_("DATA_Spectrum", exp_it->getPersistenceId(), exp_it->getSourceFile());
00605       meta_id = storeMetaInfo_("DATA_Spectrum", exp_it->getPersistenceId(), *exp_it);
00606       
00607       
00608       //----------------------------------------------------------------------------------------
00609       //--------------------------------- METAINFODESCRIPTION ---------------------------------- 
00610       //----------------------------------------------------------------------------------------
00611       
00612       const std::map<String,MetaInfoDescription>& desc = exp_it->getMetaInfoDescriptions();
00613       
00614       for (std::map<String,MetaInfoDescription>::const_iterator desc_it = desc.begin(); desc_it != desc.end(); ++desc_it)
00615       {
00616         // first check if there is already an entry in META_MetaInfoDescription for this spectrum and this name
00617         // We cannot simply delete all entries for the spectrum because this might leave unreferenced META_TNVs 
00618         // and META_MetaInfos in the database.
00619         query.str("");
00620         query << "SELECT id FROM META_MetaInfoDescription WHERE fid_Spectrum=";
00621         query << exp_it->getPersistenceId();
00622         query << " AND Name='" << desc_it->first << "'";
00623         
00624         db_con_.executeQuery(query.str(), result);
00625         
00626         query.str("");
00627         
00628         if (result.size() > 0)
00629         {
00630           parent_id = result.value(0).toInt();
00631           new_entry = false;
00632           query << "UPDATE META_MetaInfoDescription SET ";
00633           end  = " WHERE fid_Spectrum=" + String(exp_it->getPersistenceId());
00634           end += " AND Name='" + String(desc_it->first) + "'";
00635         }
00636         else
00637         {
00638           new_entry = true;
00639           query << "INSERT INTO META_MetaInfoDescription SET ";
00640           query << "fid_Spectrum='" << exp_it->getPersistenceId() << "', ";
00641           query << "Name='" << desc_it->first << "',";
00642           end = "";
00643         }
00644         
00645         query << "Description='" << desc_it->second.getComment() << "'";
00646         query << end;
00647         
00648         db_con_.executeQuery(query.str(), result);
00649         if (new_entry)
00650         {
00651           parent_id = db_con_.getAutoId();
00652         }
00653         
00654         storeFile_("META_MetaInfoDescription", parent_id, desc_it->second.getSourceFile());
00655         storeMetaInfo_("META_MetaInfoDescription", parent_id, desc_it->second);
00656       }
00657       
00658 
00659       //----------------------------------------------------------------------------------------
00660       //------------------------------- PEPTIDE IDENTIFICATIONS / HITS-------------------------- 
00661       //----------------------------------------------------------------------------------------
00662   
00663       std::vector<PeptideIdentification>& pei = exp_it->getPeptideIdentifications();
00664 
00665       // first delete all old values, no matter whether we're updating or not
00666       // do we have to delete the MetaInfo as well?
00667       query.str("");
00668       query << "DELETE FROM ID_PeptideIdentification WHERE fid_Spectrum='";
00669       query << exp_it->getPersistenceId() << "'";
00670       db_con_.executeQuery(query.str(), result);
00671       
00672       for (std::vector<PeptideIdentification>::const_iterator pei_it = pei.begin(); pei_it != pei.end(); pei_it++)
00673       {
00674         query.str("");
00675         query << "INSERT INTO ID_PeptideIdentification SET ";
00676         query << "fid_Spectrum='" << exp_it->getPersistenceId() << "'";
00677         query << ",SignificanceThreshold='" << pei_it->getSignificanceThreshold() << "'";
00678         query << ",ScoreType='" << pei_it->getScoreType() << "'";
00679         query << ",HigherScoreBetter='" << pei_it->isHigherScoreBetter() << "'";
00680         
00681         db_con_.executeQuery(query.str(), result);
00682         parent_id = db_con_.getAutoId();
00683         
00684         storeMetaInfo_("ID_PeptideIdentification", parent_id, *pei_it);
00685         //needs getter and setter methods first
00686         //storeFile_("ID_PeptideIdentification", parent_id, pei_it->getSourceFile());
00687   
00688         for (std::vector<PeptideHit>::const_iterator peh_it = pei_it->getHits().begin(); peh_it != pei_it->getHits().end(); peh_it++)
00689         {
00690           query.str("");
00691           query << "INSERT INTO ID_PeptideHit SET ";
00692           query << "fid_Identification='" << parent_id << "'";
00693           query << ",Score='" << peh_it->getScore() << "'";
00694           query << ",charge='" << peh_it->getCharge() << "'";
00695           query << ",Sequence='" << peh_it->getSequence() << "'";
00696           query << ",AABefore='" << peh_it->getAABefore() << "'";
00697           query << ",AAAfter='" << peh_it->getAAAfter() << "'";
00698           
00699           db_con_.executeQuery(query.str(), result);
00700           meta_id = db_con_.getAutoId();
00701           
00702           storeMetaInfo_("ID_PeptideHit", meta_id, *peh_it);
00703         }
00704       }
00705   
00706       //----------------------------------------------------------------------------------------
00707       //-------------------------------------- PRECURSOR --------------------------------------- 
00708       //----------------------------------------------------------------------------------------
00709       
00710       if (exp_it->getMSLevel()>1)
00711       {
00712         query.str("");
00713         if (new_entry)
00714         {
00715           query << "INSERT INTO DATA_Precursor SET fid_Spectrum='" << exp_it->getPersistenceId() << "',";
00716           end = "";
00717         }
00718         else
00719         {
00720           // first determine parent_id for meta information.
00721           // TODO: determination of parent_id can be optimized as soon as precursor becomes a PersistentObject
00722           query << "SELECT id FROM DATA_Precursor WHERE fid_Spectrum='" << exp_it->getPersistenceId() << "'";
00723           db_con_.executeQuery(query.str(),result);
00724           result.first();
00725           parent_id = result.value(0).toInt();
00726           
00727           query.str("");
00728           query << "UPDATE DATA_Precursor SET ";
00729           end = " WHERE fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00730         }
00731         //Intensity
00732         query << "Intensity='" << exp_it->getPrecursorPeak().getIntensity() << "'";
00733         //mz
00734         query << ",mz='" << exp_it->getPrecursorPeak().getPosition()[0] << "'";
00735         //charge
00736         query << ",Charge='" << exp_it->getPrecursorPeak().getCharge() << "'";
00737         //activation method
00738         query << ",ActivationMethod=" << (1u+exp_it->getPrecursor().getActivationMethod());   
00739         //activation energy unit
00740         query << ",ActivationEnergyUnit=" << (1u+exp_it->getPrecursor().getActivationEnergyUnit());   
00741         //activation energy
00742         query << ",ActivationEnergy='" << exp_it->getPrecursor().getActivationEnergy() << "'";
00743         //window size
00744         query << ",WindowSize='" << exp_it->getPrecursor().getWindowSize() << "'";
00745         
00746         query << end;
00747         db_con_.executeQuery(query.str(),result);
00748         if (new_entry) parent_id = db_con_.getAutoId();
00749         storeMetaInfo_("DATA_Precursor",parent_id, exp_it->getPrecursor());
00750         //TODO store persistence ID => Precusor class a persistent object
00751       }
00752       
00753       //----------------------------------------------------------------------------------------
00754       //---------------------------------------- PEAKS ----------------------------------------- 
00755       //----------------------------------------------------------------------------------------
00756       query.str("");
00757       deleteMetaInfo_("DATA_Peak", "fid_Spectrum=" + String(exp_it->getPersistenceId()));
00758       db_con_.executeQuery("DELETE FROM DATA_Peak WHERE fid_Spectrum=" + String(exp_it->getPersistenceId()), result);
00759       if (exp_it->size()!=0)
00760       {
00761         query << "INSERT INTO DATA_Peak (fid_Spectrum,Intensity,mz) VALUES ";
00762         tmp = "(" + String(exp_it->getPersistenceId()) + ",'";
00763         for (typename ExperimentType::SpectrumType::Iterator spec_it = exp_it->begin(); spec_it != exp_it->end(); ++spec_it)
00764         {
00765           //Foreign Key (Spectrum)
00766           query << tmp;
00767           //Intensity
00768           query << spec_it->getIntensity() << "','";
00769           //mz
00770           query << spec_it->getPosition() << "'),";
00771         }
00772         db_con_.executeQuery(String(query.str()).substr(0,-1),result);
00773       }
00774       // We know that all inserted peaks have IDs beginning from last_insert_id() (= ID of first inserted entry
00775       // of last insert operation), so we can insert Meta Information without actually fetching the ID
00776       UID insert_id = db_con_.getAutoId();
00777       for (typename ExperimentType::SpectrumType::Iterator spec_it = exp_it->begin(); spec_it != exp_it->end(); ++spec_it)
00778       {
00779         storeMetaInfo_("DATA_Peak", insert_id, *spec_it);
00780         insert_id++;
00781       }
00782       
00783       //----------------------------------------------------------------------------------------
00784       //---------------------------------- INSTRUMENT SETTINGS --------------------------------- 
00785       //----------------------------------------------------------------------------------------
00786       
00787       const InstrumentSettings & settings = exp_it->getInstrumentSettings();
00788       
00789       query.str("");
00790       
00791       if (new_entry)
00792       {
00793         query << "INSERT INTO META_InstrumentSettings SET fid_Spectrum=" << exp_it->getPersistenceId() << ",";
00794         end = "";
00795       }
00796       else
00797       {
00798         query << "SELECT id FROM META_InstrumentSettings WHERE fid_Spectrum='" << exp_it->getPersistenceId() << "'";
00799         db_con_.executeQuery(query.str(),result);
00800         result.first();
00801         parent_id = result.value(0).toInt();
00802           
00803         query.str("");
00804         query << "UPDATE META_InstrumentSettings SET ";
00805         end = " WHERE fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00806       }
00807 
00808       query << "MZRangeBegin=" << settings.getMzRangeStart() << ",";
00809       query << "MZRangeEnd=" << settings.getMzRangeStop() << ",";
00810       query << "Polarity=" << (1u+settings.getPolarity()) << ",";
00811       query << "ScanMode=" << (1u+settings.getScanMode());
00812       query << end;
00813       
00814       db_con_.executeQuery(query.str(), result);
00815     
00816       if (new_entry) parent_id = db_con_.getAutoId();
00817       storeMetaInfo_("META_InstrumentSettings", parent_id, exp_it->getInstrumentSettings());
00818       
00819       //----------------------------------------------------------------------------------------
00820       //--------------------------------- ACQUISITIONINFO -------------------------------------- 
00821       //----------------------------------------------------------------------------------------
00822       
00823       const AcquisitionInfo & info = exp_it->getAcquisitionInfo();
00824       
00825       query.str("");
00826       
00827       if (new_entry)
00828       {
00829         query << "INSERT INTO META_AcquisitionInfo SET fid_Spectrum=" << exp_it->getPersistenceId() << ",";
00830         end = "";
00831       }
00832       else
00833       {
00834         query << "SELECT id FROM META_AcquisitionInfo WHERE fid_Spectrum='" << exp_it->getPersistenceId() << "'";
00835         db_con_.executeQuery(query.str(),result);
00836         result.first();
00837         acquisition_info_id = result.value(0).toInt();
00838           
00839         query.str("");
00840         query << "UPDATE META_AcquisitionInfo SET ";
00841         end = " WHERE fid_Spectrum='" + String(exp_it->getPersistenceId()) + "'";
00842       }
00843 
00844       query << "MethodOfCombination='" << info.getMethodOfCombination() << "'";
00845       query << end;
00846       
00847       db_con_.executeQuery(query.str(), result);
00848       if (new_entry)
00849       {
00850         acquisition_info_id = db_con_.getAutoId();
00851       }
00852     
00853       //----------------------------------------------------------------------------------------
00854       //----------------------------------- ACQUISITION ---------------------------------------- 
00855       //----------------------------------------------------------------------------------------
00856       
00857       query.str("");
00858       deleteMetaInfo_("META_Acquisition", "fid_AcquisitionInfo=" + String(parent_id));
00859       query << "DELETE FROM META_Acquisition WHERE fid_AcquisitionInfo=" << parent_id;
00860       db_con_.executeQuery(query.str(), result);
00861         
00862       for (std::vector<Acquisition>::const_iterator info_it = info.begin(); info_it != info.end(); info_it++)
00863       {
00864         query.str("");
00865         query << "INSERT INTO META_Acquisition SET fid_AcquisitionInfo=" << acquisition_info_id << ",";
00866         query << "Number=" << info_it->getNumber();
00867         
00868         db_con_.executeQuery(query.str(), result);
00869         parent_id = db_con_.getAutoId();
00870         
00871         storeMetaInfo_("META_Acquisition", parent_id, *info_it);
00872       }
00873     }
00874   }
00875 
00876   template <class ExperimentType>
00877   void DBAdapter::loadExperiment(UID id, ExperimentType& exp)
00878   {
00879     //----------------------------------------------------------------------------------------
00880     //------------------------------- CHECK DB VERSION --------------------------------------- 
00881     //----------------------------------------------------------------------------------------
00882     if (!checkDBVersion(true)) return;
00883     
00884     std::stringstream query; // query to build
00885     String tmp;              // temporary data
00886     QSqlQuery result, sub_result;        // place to store the query results in
00887     UID parent_id;           // holds ID of parent data set
00888     
00889     query << "SELECT Type-1,Date,fid_MetaInfo,Description FROM META_MSExperiment WHERE id='" << id << "'";
00890     db_con_.executeQuery(query.str(),result);
00891     result.first();
00892     
00893     //Experiment meta info
00894     exp.setType((ExperimentalSettings::ExperimentType)(result.value(0).toInt()));
00895     if (result.value(1).toDate().isValid())
00896     {
00897       Date d;
00898       d.set(result.value(1).toDate().toString(Qt::ISODate).toAscii().data());
00899       exp.setDate(d);
00900     }
00901     exp.setComment(result.value(3).toString().toAscii().data());
00902     loadMetaInfo_(result.value(2).toInt(),exp);
00903     
00904     std::vector<ProteinIdentification> pi_vec;
00905     std::vector<ProteinHit> ph_vec;
00906     ProteinIdentification pi;
00907     ProteinHit ph;
00908     
00909     query.str("");
00910     query << "SELECT id, SearchEngine, SearchEngineVersion, Date, ScoreType, HigherScoreBetter, SignificanceThreshold, fid_MetaInfo, fid_File FROM ID_ProteinIdentification WHERE fid_MSExperiment='" << id << "'";
00911 
00912     db_con_.executeQuery(query.str(),result);
00913     
00914     result.first();
00915     while(result.isValid())
00916     {
00917       parent_id = result.value(0).toInt();
00918       pi.setSearchEngine(result.value(1).toString().toAscii().data());
00919       pi.setSearchEngineVersion(result.value(2).toString().toAscii().data());
00920       pi.setDateTime(DateTime(result.value(3).toDateTime()));
00921       pi.setScoreType(result.value(4).toString().toAscii().data());
00922       pi.setHigherScoreBetter(result.value(5).toInt());
00923       pi.setSignificanceThreshold(result.value(6).toDouble());
00924       
00925       loadMetaInfo_(result.value(7).toInt(), pi);
00926       //needs getter and setter methods first
00927       //      loadFile_(result.value(8).toInt(), pi.getSourceFile());
00928       
00929       query.str("");
00930       query << "SELECT Score, Accession, Sequence, fid_MetaInfo FROM ID_ProteinHit WHERE fid_ProteinIdentification='" << parent_id << "'";
00931       db_con_.executeQuery(query.str(),sub_result);
00932     
00933       sub_result.first();
00934       while(sub_result.isValid())
00935       {
00936         ph.setScore(sub_result.value(0).toDouble());
00937         ph.setAccession(sub_result.value(1).toString().toAscii().data());
00938         ph.setSequence(sub_result.value(2).toString().toAscii().data());
00939 
00940         loadMetaInfo_(sub_result.value(3).toInt(), ph);
00941 
00942         ph_vec.push_back(ph);
00943         
00944         sub_result.next();
00945       }
00946 
00947       pi.setHits(ph_vec);
00948 
00949       pi_vec.push_back(pi);
00950 
00951       result.next();
00952     }
00953     
00954     exp.setProteinIdentifications(pi_vec);
00955 
00956     // Sample
00957     Sample sample;
00958     query.str("");
00959     // finding root of recursive sample tree
00960     query << "SELECT id FROM META_Sample WHERE fid_MSExperiment='" << id << "' AND fid_Sample IS NULL";
00961     db_con_.executeQuery(query.str(),result);
00962     result.first();
00963     loadSample_ (result.value(0).toInt(), sample);
00964     exp.setSample(sample);
00965     
00966     // ContactPerson
00967     ContactPerson contact;
00968     query.str("");
00969     query << "SELECT PreName,LastName,Affiliation,Email,Comment,fid_MetaInfo FROM META_ContactPerson WHERE fid_MSExperiment='" << id << "'";
00970     db_con_.executeQuery(query.str(),result);
00971     result.first();
00972     
00973     result.first();
00974     while(result.isValid())
00975     {
00976       contact.setFirstName(result.value(0).toString().toAscii().data());
00977       contact.setLastName(result.value(1).toString().toAscii().data());
00978       contact.setInstitution(result.value(2).toString().toAscii().data());
00979       contact.setEmail(result.value(3).toString().toAscii().data());
00980       contact.setContactInfo(result.value(4).toString().toAscii().data());
00981       loadMetaInfo_(result.value(5).toInt(),contact);
00982       result.next();
00983       exp.getContacts().push_back(contact);
00984     }
00985     
00986     // HPLC
00987     query.str("");
00988     query << "SELECT id,InstrumentName,ColumnName,Description,Flux,Pressure,Temperature FROM META_HPLC WHERE fid_MSExperiment='" << id << "'";
00989     db_con_.executeQuery(query.str(),result);
00990     result.first();
00991     
00992     parent_id=result.value(0).toInt();
00993     exp.getHPLC().setInstrument(result.value(1).toString().toAscii().data());
00994     exp.getHPLC().setColumn(result.value(2).toString().toAscii().data());
00995     exp.getHPLC().setComment(result.value(3).toString().toAscii().data());
00996     exp.getHPLC().setFlux(result.value(4).toInt());
00997     exp.getHPLC().setPressure(result.value(5).toInt());
00998     exp.getHPLC().setTemperature(result.value(6).toInt());
00999     
01000     // Gradient*
01001     // I tried taking the big query apart in order to skip the double join, but this leads to
01002     // the problem of saving all requested keys in a vector in order to request the percentages (complex).
01003     // I'll still preserve the code in order to optimize in the future. Maybe I was just being blind. ;-)
01004     
01005     String last_name;
01006     bool timepoints_done = false;
01007     query.str("");
01008     /*
01009     query << "SELECT id,Name FROM META_GradientEluent WHERE fid_HPLC=" << parent_id;
01010     db_con_.executeQuery(query.str(),result);
01011     result.first();
01012     while(result.isValid())
01013     {
01014       exp.getHPLC().getGradient().addEluent(result.value(0).toString().toAscii().data());
01015       result.next();
01016     }
01017     
01018     query.str("");
01019     query << "SELECT id,Time FROM META_GradientTime WHERE fid_HPLC=" << parent_id;
01020     db_con_.executeQuery(query.str(),result);
01021     result.first();
01022     while(result.isValid())
01023     {
01024       exp.getHPLC().getGradient().addTimepoint(result.value(0).toInt());
01025       result.next();
01026     }
01027     */
01028     
01029     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";
01030     db_con_.executeQuery(query.str(),result);   
01031     result.first();
01032     
01033     if (result.isValid())
01034     {
01035       last_name = result.value(0).toString().toAscii().data();
01036       exp.getHPLC().getGradient().addEluent(last_name);
01037     }
01038     
01039     while(result.isValid())
01040     {
01041       if (result.value(0).toString().toAscii().data() != last_name)
01042       {
01043         exp.getHPLC().getGradient().addEluent(result.value(0).toString().toAscii().data());
01044         timepoints_done = true;
01045       }
01046       
01047       if (timepoints_done == false)
01048       {
01049         exp.getHPLC().getGradient().addTimepoint(result.value(1).toInt());
01050       }
01051       
01052       exp.getHPLC().getGradient().setPercentage(result.value(0).toString().toAscii().data(), result.value(1).toInt(), result.value(2).toInt());
01053       
01054       last_name = result.value(0).toString().toAscii().data();
01055       result.next();
01056     }
01057     
01058     // Instrument
01059     query.str("");
01060     query << "SELECT id,Model,Vendor,Description,fid_MetaInfo FROM META_MSInstrument WHERE fid_MSExperiment='" << id << "'";
01061     db_con_.executeQuery(query.str(),result);
01062     result.first();
01063     
01064     parent_id = result.value(0).toInt();
01065     exp.getInstrument().setModel(result.value(1).toString().toAscii().data());
01066     exp.getInstrument().setVendor(result.value(2).toString().toAscii().data());
01067     exp.getInstrument().setCustomizations(result.value(3).toString().toAscii().data());
01068     loadMetaInfo_(result.value(4).toInt(),exp.getInstrument());
01069     
01070     // IonDetector
01071     query.str("");
01072     query << "SELECT AcquisitionMode-1,Type-1,Resolution,ADCSamplingFrequency,fid_MetaInfo FROM META_IonDetector WHERE fid_MSInstrument='" << parent_id << "'";
01073     db_con_.executeQuery(query.str(),result);
01074     result.first();
01075     
01076     exp.getInstrument().getIonDetector().setAcquisitionMode((IonDetector::AcquisitionMode) result.value(0).toInt());
01077     exp.getInstrument().getIonDetector().setType((IonDetector::Type) result.value(1).toInt());
01078     exp.getInstrument().getIonDetector().setResolution(result.value(2).toDouble());
01079     exp.getInstrument().getIonDetector().setADCSamplingFrequency(result.value(3).toDouble());
01080     loadMetaInfo_(result.value(4).toInt(),exp.getInstrument().getIonDetector());
01081     
01082     // IonSource
01083     query.str("");
01084     query << "SELECT InletType-1,IonizationMethod-1,IonizationMode-1,fid_MetaInfo FROM META_IonSource WHERE fid_MSInstrument='" << parent_id << "'";
01085     db_con_.executeQuery(query.str(),result);
01086     result.first();
01087     
01088     exp.getInstrument().getIonSource().setInletType((IonSource::InletType) result.value(0).toInt());
01089     exp.getInstrument().getIonSource().setIonizationMethod((IonSource::IonizationMethod) result.value(1).toInt());
01090     exp.getInstrument().getIonSource().setPolarity((IonSource::Polarity) result.value(2).toDouble());
01091     loadMetaInfo_(result.value(3).toInt(),exp.getInstrument().getIonSource());
01092     
01093     // MassAnalyzers
01094     MassAnalyzer analyzer;
01095     std::vector<MassAnalyzer> analyzers;
01096     query.str("");
01097     query << "SELECT Accuracy,FinalMSExponent,IsolationWidth,MagneticFieldStrength,ReflectronState-1,Resolution,ResolutionMethod-1,ResolutionType-1,ScanDirection-1,ScanFunction-1,ScanLaw-1,ScanRate,ScanTime,TandemScanningMethod-1,TOFPathLength,Type-1,fid_MetaInfo FROM META_MassAnalyzer WHERE fid_MSInstrument='" << parent_id << "'";
01098     db_con_.executeQuery(query.str(),result);
01099     
01100     result.first();
01101     while(result.isValid())
01102     {
01103       analyzer.setAccuracy(result.value(0).toDouble());
01104       analyzer.setFinalMSExponent(result.value(1).toInt());
01105       analyzer.setIsolationWidth(result.value(2).toDouble());
01106       analyzer.setMagneticFieldStrength(result.value(3).toDouble());
01107       analyzer.setReflectronState((MassAnalyzer::ReflectronState) result.value(4).toInt());
01108       analyzer.setResolution(result.value(5).toDouble());
01109       analyzer.setResolutionMethod((MassAnalyzer::ResolutionMethod) result.value(6).toInt());
01110       analyzer.setResolutionType((MassAnalyzer::ResolutionType) result.value(7).toInt());
01111       analyzer.setScanDirection((MassAnalyzer::ScanDirection) result.value(8).toInt());
01112       analyzer.setScanFunction((MassAnalyzer::ScanFunction) result.value(9).toInt());
01113       analyzer.setScanLaw((MassAnalyzer::ScanLaw) result.value(10).toInt());
01114       analyzer.setScanRate(result.value(11).toDouble());
01115       analyzer.setScanTime(result.value(12).toDouble());
01116       analyzer.setTandemScanMethod((MassAnalyzer::TandemScanningMethod) result.value(13).toInt());
01117       analyzer.setTOFTotalPathLength(result.value(14).toDouble());
01118       analyzer.setType((MassAnalyzer::AnalyzerType) result.value(15).toInt());
01119       loadMetaInfo_(result.value(16).toInt(), analyzer);
01120       
01121       analyzers.push_back(analyzer);
01122       result.next();
01123     }
01124     exp.getInstrument().setMassAnalyzers(analyzers);
01125 
01126     //id
01127     exp.setPersistenceId(id);
01128 
01129     // if we don't have to load the spectra, we're already done
01130     if (options_.getMetadataOnly())
01131     {
01132       return;
01133     }
01134 
01135     //spectra
01136     query.str("");
01137     query << "SELECT id FROM DATA_Spectrum WHERE fid_MSExperiment=" << id;
01138     if (options_.hasRTRange())
01139     {
01140       query << " AND RetentionTime > " << options_.getRTRange().min() << " AND RetentionTime < " << options_.getRTRange().max();
01141     }
01142     if (options_.hasMSLevels())
01143     {
01144       const std::vector<int>& levels = options_.getMSLevels();
01145       query << " AND (";
01146       for (std::vector<int>::const_iterator it = levels.begin(); it != levels.end(); it++)
01147       {
01148         query << "MSLevel=" << *it;
01149         if (it+1 != levels.end())
01150         {
01151           query << " OR ";
01152         }
01153       }
01154       query << ")";
01155     }
01156     query << " ORDER BY id ASC";
01157     
01158     db_con_.executeQuery(query.str(),result);
01159     exp.resize(result.size());
01160     UInt i = 0;
01161     result.first();
01162     while (result.isValid())
01163     {
01164       loadSpectrum(result.value(0).toInt(), exp[i]);
01165       ++i;
01166       result.next();
01167     }
01168   }
01169 
01170   template <class SpectrumType>
01171   void DBAdapter::loadSpectrum(UID id, SpectrumType& spec)
01172   {
01173     //----------------------------------------------------------------------------------------
01174     //------------------------------- CHECK DB VERSION --------------------------------------- 
01175     //----------------------------------------------------------------------------------------
01176     if (!checkDBVersion(true)) return;
01177     
01178     spec = SpectrumType();
01179     
01180     std::stringstream query;     // query to build
01181     QSqlQuery result, sub_result;// place to store the query results in
01182     InstrumentSettings settings; // stores settings that are read from DB
01183     UID parent_id;               // stores parent_id of Acquisition
01184     
01185     query << "SELECT Type-1,RetentionTime,MSLevel,Description,fid_MetaInfo,fid_File FROM DATA_Spectrum WHERE id='" << id << "'";
01186     db_con_.executeQuery(query.str(),result);
01187     result.first();
01188     
01189     //Spectrum meta info
01190     spec.setType((SpectrumSettings::SpectrumType)(result.value(0).toInt()));
01191     spec.setRT(result.value(1).toDouble());
01192     spec.setMSLevel(result.value(2).toInt());
01193     spec.setComment(result.value(3).toString().toAscii().data());
01194     loadMetaInfo_(result.value(4).toInt(),spec);
01195     loadFile_(result.value(5).toInt(),spec.getSourceFile());
01196     
01197     // Instrument settings
01198     query.str("");
01199     query << "SELECT MZRangeBegin, MZRangeEnd, Polarity-1, ScanMode-1, fid_MetaInfo FROM META_InstrumentSettings WHERE fid_Spectrum=" << id;
01200     db_con_.executeQuery(query.str(),result);
01201     result.first();
01202     
01203     settings.setMzRangeStart(result.value(0).toDouble());
01204     settings.setMzRangeStop(result.value(1).toDouble());
01205     settings.setPolarity((IonSource::Polarity) (result.value(2).toInt()));
01206     settings.setScanMode((InstrumentSettings::ScanMode) (result.value(3).toInt()));
01207     spec.setInstrumentSettings(settings);
01208     loadMetaInfo_(result.value(4).toInt(),spec.getInstrumentSettings());
01209 
01210 
01211     // PeptideIdentification / PeptideHits
01212 
01213     std::vector<PeptideIdentification> pei_vec;
01214     std::vector<PeptideHit> peh_vec;
01215     PeptideIdentification pei;
01216     PeptideHit peh;
01217     
01218     query.str("");
01219     query << "SELECT id, SignificanceThreshold, ScoreType, HigherScoreBetter, fid_MetaInfo, fid_File  FROM ID_PeptideIdentification WHERE fid_Spectrum='" << id << "'";
01220 
01221     db_con_.executeQuery(query.str(),result);
01222     
01223     result.first();
01224     while(result.isValid())
01225     {
01226       parent_id = result.value(0).toInt();
01227       pei.setSignificanceThreshold(result.value(1).toDouble());
01228       pei.setScoreType(result.value(2).toString().toAscii().data());
01229       pei.setHigherScoreBetter(result.value(3).toInt());
01230       
01231       loadMetaInfo_(result.value(4).toInt(), pei);
01232       //needs getter and setter methods first
01233       //      loadFile_(result.value(5).toInt(), pei.getSourceFile());
01234       
01235       query.str("");
01236       query << "SELECT Score, Sequence, Charge, AABefore, AAAfter, fid_MetaInfo FROM ID_PeptideHit WHERE fid_Identification='" << parent_id << "'";
01237       db_con_.executeQuery(query.str(),sub_result);
01238     
01239       sub_result.first();
01240       while(sub_result.isValid())
01241       {
01242         peh.setScore(sub_result.value(0).toDouble());
01243         peh.setSequence(sub_result.value(1).toString().toAscii().data());
01244         peh.setCharge(sub_result.value(2).toInt());
01245         peh.setAABefore(sub_result.value(3).toString().toAscii().data()[0]);
01246         peh.setAAAfter(sub_result.value(4).toString().toAscii().data()[0]);
01247 
01248         loadMetaInfo_(sub_result.value(5).toInt(), peh);
01249 
01250         peh_vec.push_back(peh);
01251         
01252         sub_result.next();
01253       }
01254 
01255       pei.setHits(peh_vec);
01256 
01257       pei_vec.push_back(pei);
01258 
01259       result.next();
01260     }
01261 
01262     spec.setPeptideIdentifications(pei_vec);
01263 
01264     // AcquisitionInfo
01265     query.str("");
01266     query << "SELECT id, MethodOfCombination FROM META_AcquisitionInfo WHERE fid_Spectrum=" << id;
01267     db_con_.executeQuery(query.str(),result);
01268     result.first();
01269     
01270     spec.getAcquisitionInfo().setMethodOfCombination(result.value(1).toString().toAscii().data());
01271     parent_id = result.value(0).toInt();
01272     
01273     // Acquisition
01274     query.str("");
01275     query << "SELECT Number,fid_MetaInfo FROM META_Acquisition WHERE fid_AcquisitionInfo='" << parent_id << "' ORDER BY id ASC";
01276     db_con_.executeQuery(query.str(),result);
01277 
01278     Acquisition acquisition;
01279     
01280     result.first();
01281     while(result.isValid())
01282     {
01283       acquisition.setNumber(result.value(0).toInt());
01284       loadMetaInfo_(result.value(1).toInt(), acquisition);
01285       spec.getAcquisitionInfo().push_back(acquisition);
01286       result.next();
01287     }
01288     
01289     // MetaInfoDescription
01290     query.str("");
01291     query << "SELECT Name, Description, fid_MetaInfo, fid_File FROM META_MetaInfoDescription WHERE fid_Spectrum=" << id;
01292 
01293     db_con_.executeQuery(query.str(), result);
01294     result.first();
01295     
01296     std::map<String,MetaInfoDescription> descriptions;
01297     MetaInfoDescription desc;
01298     
01299     while(result.isValid())
01300     {
01301       desc.setName(result.value(0).toString().toAscii().data());
01302       desc.setComment(result.value(1).toString().toAscii().data());
01303       loadMetaInfo_(result.value(2).toInt(), desc);
01304       loadFile_(result.value(3).toInt(),desc.getSourceFile());
01305       
01306       descriptions[result.value(0).toString().toAscii().data()] = desc;
01307       result.next();
01308     }
01309     spec.setMetaInfoDescriptions (descriptions);
01310 
01311     //precursor
01312     if(spec.getMSLevel()>1)
01313     {
01314       query.str("");
01315       query << "SELECT mz,Intensity,Charge,ActivationMethod-1,ActivationEnergyUnit-1,ActivationEnergy,WindowSize,fid_MetaInfo FROM DATA_Precursor WHERE fid_Spectrum='" << id << "'";
01316       db_con_.executeQuery(query.str(),result);
01317       result.first();
01318       spec.getPrecursorPeak().getPosition()[0] = (result.value(0).toDouble());
01319       spec.getPrecursorPeak().setIntensity(result.value(1).toDouble());
01320       spec.getPrecursorPeak().setCharge(result.value(2).toInt());
01321       spec.getPrecursor().setActivationMethod((Precursor::ActivationMethod)(result.value(3).toInt()));
01322       spec.getPrecursor().setActivationEnergyUnit((Precursor::EnergyUnits)(result.value(4).toInt()));
01323       spec.getPrecursor().setActivationEnergy(result.value(5).toDouble());
01324       spec.getPrecursor().setWindowSize(result.value(6).toDouble());
01325       loadMetaInfo_(result.value(7).toInt(),spec.getPrecursor());
01326     }
01327     
01328     //Peaks
01329     query.str("");
01330     query << "SELECT mz,Intensity,fid_MetaInfo FROM DATA_Peak WHERE fid_Spectrum='" << id << "' ";
01331     if (options_.hasMZRange())
01332     {
01333       query << " AND mz > " << options_.getMZRange().min() << " AND mz < " << options_.getMZRange().max();
01334     }
01335     if (options_.hasIntensityRange())
01336     {
01337       query << " AND Intensity > " << options_.getIntensityRange().min() << " AND Intensity < " << options_.getIntensityRange().max();
01338     }
01339     query << " ORDER BY mz ASC";
01340     db_con_.executeQuery(query.str(),result);
01341 
01342     typename SpectrumType::PeakType p;
01343     result.first();
01344     while(result.isValid())
01345     {
01346       p.setPosition(result.value(0).toDouble());
01347       p.setIntensity(result.value(1).toDouble());
01348       loadMetaInfo_(result.value(2).toInt(), p);
01349       spec.push_back(p);
01350       result.next();
01351     }
01352     
01353     //id
01354     spec.setPersistenceId(id);
01355   }
01356 }
01357 
01358 #endif

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1