OpenMS  2.4.0
OpenSwathOSWWriter.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2018.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: George Rosenberger $
32 // $Authors: George Rosenberger $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 // Interfaces
39 
41 
43 
44 #include <sqlite3.h>
45 
46 #include <fstream>
47 
48 namespace OpenMS
49 {
50 
58  class OPENMS_DLLAPI OpenSwathOSWWriter
59  {
63  bool doWrite_;
65  bool sonar_;
67 
68  public:
69 
70  OpenSwathOSWWriter(const String& output_filename,
71  const String& input_filename = "inputfile",
72  bool ms1_scores = false,
73  bool sonar = false,
74  bool uis_scores = false) :
75  output_filename_(output_filename),
76  input_filename_(input_filename),
77  run_id_(OpenMS::UniqueIdGenerator::getUniqueId()),
78  doWrite_(!output_filename.empty()),
79  use_ms1_traces_(ms1_scores),
80  sonar_(sonar),
81  enable_uis_scoring_(uis_scores)
82  {}
83 
84  static int callback(void * /* NotUsed */, int argc, char **argv, char **azColName)
85  {
86  int i;
87  for(i=0; i<argc; i++)
88  {
89  printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
90  }
91  printf("\n");
92  return(0);
93  }
94 
95  bool isActive() const
96  {
97  return doWrite_;
98  }
99 
104  void writeHeader()
105  {
106  sqlite3 *db;
107  char *zErrMsg = nullptr;
108  int rc;
109 
110  // Open database
111  rc = sqlite3_open(output_filename_.c_str(), &db);
112  if( rc )
113  {
114  fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
115  }
116 
117  // Create SQL structure
118  const char * create_sql =
119  "CREATE TABLE RUN(" \
120  "ID INT PRIMARY KEY NOT NULL," \
121  "FILENAME TEXT NOT NULL); " \
122 
123  "CREATE TABLE FEATURE(" \
124  "ID INT PRIMARY KEY NOT NULL," \
125  "RUN_ID INT NOT NULL," \
126  "PRECURSOR_ID INT NOT NULL," \
127  "EXP_RT REAL NOT NULL," \
128  "NORM_RT REAL NOT NULL," \
129  "DELTA_RT REAL NOT NULL," \
130  "LEFT_WIDTH REAL NOT NULL," \
131  "RIGHT_WIDTH REAL NOT NULL); " \
132 
133  "CREATE TABLE FEATURE_MS1(" \
134  "FEATURE_ID INT NOT NULL," \
135  "AREA_INTENSITY REAL NOT NULL," \
136  "APEX_INTENSITY REAL NOT NULL," \
137  "VAR_MASSDEV_SCORE REAL NOT NULL," \
138  "VAR_MI_SCORE REAL NULL," \
139  "VAR_ISOTOPE_CORRELATION_SCORE REAL NOT NULL," \
140  "VAR_ISOTOPE_OVERLAP_SCORE REAL NOT NULL," \
141  "VAR_XCORR_COELUTION REAL NOT NULL," \
142  "VAR_XCORR_SHAPE REAL NOT NULL); " \
143 
144  "CREATE TABLE FEATURE_MS2(" \
145  "FEATURE_ID INT NOT NULL," \
146  "AREA_INTENSITY REAL NOT NULL," \
147  "TOTAL_AREA_INTENSITY REAL NOT NULL," \
148  "APEX_INTENSITY REAL NOT NULL," \
149  "TOTAL_MI REAL NULL," \
150  "VAR_BSERIES_SCORE REAL NOT NULL," \
151  "VAR_DOTPROD_SCORE REAL NOT NULL," \
152  "VAR_INTENSITY_SCORE REAL NOT NULL," \
153  "VAR_ISOTOPE_CORRELATION_SCORE REAL NOT NULL," \
154  "VAR_ISOTOPE_OVERLAP_SCORE REAL NOT NULL," \
155  "VAR_LIBRARY_CORR REAL NOT NULL," \
156  "VAR_LIBRARY_DOTPROD REAL NOT NULL," \
157  "VAR_LIBRARY_MANHATTAN REAL NOT NULL," \
158  "VAR_LIBRARY_RMSD REAL NOT NULL," \
159  "VAR_LIBRARY_ROOTMEANSQUARE REAL NOT NULL," \
160  "VAR_LIBRARY_SANGLE REAL NOT NULL," \
161  "VAR_LOG_SN_SCORE REAL NOT NULL," \
162  "VAR_MANHATTAN_SCORE REAL NOT NULL," \
163  "VAR_MASSDEV_SCORE REAL NOT NULL," \
164  "VAR_MASSDEV_SCORE_WEIGHTED REAL NOT NULL," \
165  "VAR_MI_SCORE REAL NULL," \
166  "VAR_MI_WEIGHTED_SCORE REAL NULL," \
167  "VAR_MI_RATIO_SCORE REAL NULL," \
168  "VAR_NORM_RT_SCORE REAL NOT NULL," \
169  "VAR_XCORR_COELUTION REAL NOT NULL," \
170  "VAR_XCORR_COELUTION_WEIGHTED REAL NOT NULL," \
171  "VAR_XCORR_SHAPE REAL NOT NULL," \
172  "VAR_XCORR_SHAPE_WEIGHTED REAL NOT NULL," \
173  "VAR_YSERIES_SCORE REAL NOT NULL," \
174  "VAR_ELUTION_MODEL_FIT_SCORE REAL NULL," \
175  "VAR_SONAR_LAG REAL NULL," \
176  "VAR_SONAR_SHAPE REAL NULL," \
177  "VAR_SONAR_LOG_SN REAL NULL," \
178  "VAR_SONAR_LOG_DIFF REAL NULL," \
179  "VAR_SONAR_LOG_TREND REAL NULL," \
180  "VAR_SONAR_RSQ REAL NULL); " \
181 
182  "CREATE TABLE FEATURE_TRANSITION(" \
183  "FEATURE_ID INT NOT NULL," \
184  "TRANSITION_ID INT NOT NULL," \
185  "AREA_INTENSITY REAL NOT NULL," \
186  "TOTAL_AREA_INTENSITY REAL NOT NULL," \
187  "APEX_INTENSITY REAL NOT NULL," \
188  "TOTAL_MI REAL NULL," \
189  "VAR_INTENSITY_SCORE REAL NULL," \
190  "VAR_INTENSITY_RATIO_SCORE REAL NULL," \
191  "VAR_LOG_INTENSITY REAL NULL," \
192  "VAR_XCORR_COELUTION REAL NULL," \
193  "VAR_XCORR_SHAPE REAL NULL," \
194  "VAR_LOG_SN_SCORE REAL NULL," \
195  "VAR_MASSDEV_SCORE REAL NULL," \
196  "VAR_MI_SCORE REAL NULL," \
197  "VAR_MI_RATIO_SCORE REAL NULL," \
198  "VAR_ISOTOPE_CORRELATION_SCORE REAL NULL," \
199  "VAR_ISOTOPE_OVERLAP_SCORE REAL NULL); " ;
200 
201 
202  // Execute SQL create statement
203  rc = sqlite3_exec(db, create_sql, callback, nullptr, &zErrMsg);
204  if( rc != SQLITE_OK )
205  {
206  std::string error_message = zErrMsg;
207  sqlite3_free(zErrMsg);
208  throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
209  error_message);
210  }
211 
212  // Insert run_id information
213  std::stringstream sql_run;
214  sql_run << "INSERT INTO RUN (ID, FILENAME) VALUES ("
215  << *(int64_t*)&run_id_ << ", '" // Conversion from UInt64 to int64_t to support SQLite
216  << input_filename_ << "'); ";
217 
218  // Execute SQL insert statement
219  rc = sqlite3_exec(db, sql_run.str().c_str(), callback, nullptr, &zErrMsg);
220  if( rc != SQLITE_OK )
221  {
222  std::string error_message = zErrMsg;
223  sqlite3_free(zErrMsg);
224  throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
225  error_message);
226  }
227 
228  sqlite3_close(db);
229  }
230 
246  const OpenSwath::LightTransition* /* transition */,
247  FeatureMap& output, String id) const
248  {
249  std::stringstream sql, sql_feature, sql_feature_ms1, sql_feature_ms2, sql_feature_ms2_transition, sql_feature_uis_transition;
250 
251  for (FeatureMap::iterator feature_it = output.begin(); feature_it != output.end(); ++feature_it)
252  {
253  UInt64 uint64_feature_id = feature_it->getUniqueId();
254  int64_t feature_id = *(int64_t*)&uint64_feature_id; // Conversion from UInt64 to int64_t to support SQLite
255 
256  for (std::vector<Feature>::iterator sub_it = feature_it->getSubordinates().begin(); sub_it != feature_it->getSubordinates().end(); ++sub_it)
257  {
258  if (sub_it->metaValueExists("FeatureLevel") && sub_it->getMetaValue("FeatureLevel") == "MS2")
259  {
260  std::string total_mi = "NULL"; // total_mi is not guaranteed to be set
261  if (!sub_it->getMetaValue("total_mi").isEmpty())
262  {
263  total_mi = sub_it->getMetaValue("total_mi").toString();
264  }
265  sql_feature_ms2_transition << "INSERT INTO FEATURE_TRANSITION (FEATURE_ID, TRANSITION_ID, AREA_INTENSITY, TOTAL_AREA_INTENSITY, APEX_INTENSITY, TOTAL_MI) VALUES ("
266  << feature_id << ", "
267  << sub_it->getMetaValue("native_id") << ", "
268  << sub_it->getIntensity() << ", "
269  << sub_it->getMetaValue("total_xic") << ", "
270  << sub_it->getMetaValue("peak_apex_int") << ", "
271  << total_mi << "); ";
272  }
273  else if (sub_it->metaValueExists("FeatureLevel") && sub_it->getMetaValue("FeatureLevel") == "MS1")
274  {
275  std::string var_ms1_mi_score = "NULL"; // var_ms1_mi_score is not guaranteed to be set
276  if (!feature_it->getMetaValue("var_ms1_mi_score").isEmpty())
277  {
278  var_ms1_mi_score = feature_it->getMetaValue("var_ms1_mi_score").toString();
279  }
280  sql_feature_ms1 << "INSERT INTO FEATURE_MS1 (FEATURE_ID, AREA_INTENSITY, APEX_INTENSITY, VAR_MASSDEV_SCORE, VAR_MI_SCORE, VAR_ISOTOPE_CORRELATION_SCORE, VAR_ISOTOPE_OVERLAP_SCORE, VAR_XCORR_COELUTION, VAR_XCORR_SHAPE) VALUES ("
281  << feature_id << ", "
282  << sub_it->getIntensity() << ", "
283  << sub_it->getMetaValue("peak_apex_int") << ", "
284  << feature_it->getMetaValue("var_ms1_ppm_diff") << ", "
285  << var_ms1_mi_score << ", "
286  << feature_it->getMetaValue("var_ms1_isotope_correlation") << ", "
287  << feature_it->getMetaValue("var_ms1_isotope_overlap") << ", "
288  << feature_it->getMetaValue("var_ms1_xcorr_coelution") << ", "
289  << feature_it->getMetaValue("var_ms1_xcorr_shape") << "); ";
290  }
291  }
292 
293  sql_feature << "INSERT INTO FEATURE (ID, RUN_ID, PRECURSOR_ID, EXP_RT, NORM_RT, DELTA_RT, LEFT_WIDTH, RIGHT_WIDTH) VALUES ("
294  << feature_id << ", '"
295  << *(int64_t*)&run_id_ << "', "
296  << id << ", "
297  << feature_it->getRT() << ", "
298  << feature_it->getMetaValue("norm_RT") << ", "
299  << feature_it->getMetaValue("delta_rt") << ", "
300  << feature_it->getMetaValue("leftWidth") << ", "
301  << feature_it->getMetaValue("rightWidth") << "); ";
302 
303  // these variables are not guaranteed to be set
304  std::string var_elution_model_fit_score = "NULL", var_sonar_lag = "NULL", var_sonar_shape = "NULL", var_sonar_log_sn = "NULL", var_sonar_log_diff = "NULL", var_sonar_log_trend = "NULL", var_sonar_rsq = "NULL", total_mi = "NULL", var_mi_score = "NULL", var_mi_weighted_score = "NULL", var_mi_ratio_score = "NULL";
305 
306  if (!feature_it->getMetaValue("var_elution_model_fit_score").isEmpty())
307  {
308  var_elution_model_fit_score = feature_it->getMetaValue("var_elution_model_fit_score").toString();
309  }
310  if (!feature_it->getMetaValue("var_sonar_lag").isEmpty())
311  {
312  var_sonar_lag = feature_it->getMetaValue("var_sonar_lag").toString();
313  }
314  if (!feature_it->getMetaValue("var_sonar_shape").isEmpty())
315  {
316  var_sonar_shape = feature_it->getMetaValue("var_sonar_shape").toString();
317  }
318  if (!feature_it->getMetaValue("var_sonar_log_sn").isEmpty())
319  {
320  var_sonar_log_sn = feature_it->getMetaValue("var_sonar_log_sn").toString();
321  }
322  if (!feature_it->getMetaValue("var_sonar_log_diff").isEmpty())
323  {
324  var_sonar_log_diff = feature_it->getMetaValue("var_sonar_log_diff").toString();
325  }
326  if (!feature_it->getMetaValue("var_sonar_log_trend").isEmpty())
327  {
328  var_sonar_log_trend = feature_it->getMetaValue("var_sonar_log_trend").toString();
329  }
330  if (!feature_it->getMetaValue("var_sonar_rsq").isEmpty())
331  {
332  var_sonar_rsq = feature_it->getMetaValue("var_sonar_rsq").toString();
333  }
334  if (!feature_it->getMetaValue("total_mi").isEmpty())
335  {
336  total_mi = feature_it->getMetaValue("total_mi").toString();
337  }
338  if (!feature_it->getMetaValue("var_mi_score").isEmpty())
339  {
340  var_mi_score = feature_it->getMetaValue("var_mi_score").toString();
341  }
342  if (!feature_it->getMetaValue("var_mi_weighted_score").isEmpty())
343  {
344  var_mi_weighted_score = feature_it->getMetaValue("var_mi_weighted_score").toString();
345  }
346  if (!feature_it->getMetaValue("var_mi_ratio_score").isEmpty())
347  {
348  var_mi_ratio_score = feature_it->getMetaValue("var_mi_ratio_score").toString();
349  }
350 
351  sql_feature_ms2 << "INSERT INTO FEATURE_MS2 (FEATURE_ID, AREA_INTENSITY, TOTAL_AREA_INTENSITY, APEX_INTENSITY, TOTAL_MI, VAR_BSERIES_SCORE, VAR_DOTPROD_SCORE, VAR_INTENSITY_SCORE, VAR_ISOTOPE_CORRELATION_SCORE, VAR_ISOTOPE_OVERLAP_SCORE, VAR_LIBRARY_CORR, VAR_LIBRARY_DOTPROD, VAR_LIBRARY_MANHATTAN, VAR_LIBRARY_RMSD, VAR_LIBRARY_ROOTMEANSQUARE, VAR_LIBRARY_SANGLE, VAR_LOG_SN_SCORE, VAR_MANHATTAN_SCORE, VAR_MASSDEV_SCORE, VAR_MASSDEV_SCORE_WEIGHTED, VAR_MI_SCORE, VAR_MI_WEIGHTED_SCORE, VAR_MI_RATIO_SCORE, VAR_NORM_RT_SCORE, VAR_XCORR_COELUTION,VAR_XCORR_COELUTION_WEIGHTED, VAR_XCORR_SHAPE, VAR_XCORR_SHAPE_WEIGHTED, VAR_YSERIES_SCORE, VAR_ELUTION_MODEL_FIT_SCORE, VAR_SONAR_LAG, VAR_SONAR_SHAPE, VAR_SONAR_LOG_SN, VAR_SONAR_LOG_DIFF, VAR_SONAR_LOG_TREND, VAR_SONAR_RSQ) VALUES ("
352  << feature_id << ", "
353  << feature_it->getIntensity() << ", "
354  << feature_it->getMetaValue("total_xic") << ", "
355  << feature_it->getMetaValue("peak_apices_sum") << ", "
356  << total_mi << ", "
357  << feature_it->getMetaValue("var_bseries_score") << ", "
358  << feature_it->getMetaValue("var_dotprod_score") << ", "
359  << feature_it->getMetaValue("var_intensity_score") << ", "
360  << feature_it->getMetaValue("var_isotope_correlation_score") << ", "
361  << feature_it->getMetaValue("var_isotope_overlap_score") << ", "
362  << feature_it->getMetaValue("var_library_corr") << ", "
363  << feature_it->getMetaValue("var_library_dotprod") << ", "
364  << feature_it->getMetaValue("var_library_manhattan") << ", "
365  << feature_it->getMetaValue("var_library_rmsd") << ", "
366  << feature_it->getMetaValue("var_library_rootmeansquare") << ", "
367  << feature_it->getMetaValue("var_library_sangle") << ", "
368  << feature_it->getMetaValue("var_log_sn_score") << ", "
369  << feature_it->getMetaValue("var_manhatt_score") << ", "
370  << feature_it->getMetaValue("var_massdev_score") << ", "
371  << feature_it->getMetaValue("var_massdev_score_weighted") << ", "
372  << var_mi_score << ", "
373  << var_mi_weighted_score << ", "
374  << var_mi_ratio_score << ", "
375  << feature_it->getMetaValue("var_norm_rt_score") << ", "
376  << feature_it->getMetaValue("var_xcorr_coelution") << ", "
377  << feature_it->getMetaValue("var_xcorr_coelution_weighted") << ", "
378  << feature_it->getMetaValue("var_xcorr_shape") << ", "
379  << feature_it->getMetaValue("var_xcorr_shape_weighted") << ", "
380  << feature_it->getMetaValue("var_yseries_score") << ", "
381  << var_elution_model_fit_score << ", "
382  << var_sonar_lag << ", "
383  << var_sonar_shape << ", "
384  << var_sonar_log_sn << ", "
385  << var_sonar_log_diff << ", "
386  << var_sonar_log_trend << ", "
387  << var_sonar_rsq << "); ";
388 
389  if (enable_uis_scoring_)
390  {
391  std::vector<String> id_target_transition_names = ListUtils::create<String>((String)feature_it->getMetaValue("id_target_transition_names"),';');
392  std::vector<double> id_target_area_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_area_intensity"),';');
393  std::vector<double> id_target_total_area_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_total_area_intensity"),';');
394  std::vector<double> id_target_apex_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_apex_intensity"),';');
395  std::vector<double> id_target_intensity_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_intensity_score"),';');
396  std::vector<double> id_target_intensity_ratio_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_intensity_ratio_score"),';');
397  std::vector<double> id_target_log_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_ind_log_intensity"),';');
398  std::vector<double> id_target_ind_xcorr_coelution = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_ind_xcorr_coelution"),';');
399  std::vector<double> id_target_ind_xcorr_shape = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_ind_xcorr_shape"),';');
400  std::vector<double> id_target_ind_log_sn_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_ind_log_sn_score"),';');
401  std::vector<double> id_target_ind_massdev_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_ind_massdev_score"),';');
402  std::vector<double> id_target_ind_isotope_correlation = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_ind_isotope_correlation"),';');
403  std::vector<double> id_target_ind_isotope_overlap = ListUtils::create<double>((String)feature_it->getMetaValue("id_target_ind_isotope_overlap"),';');
404 
405  if ((String)feature_it->getMetaValue("id_target_num_transitions") != "")
406  {
407  int id_target_num_transitions = feature_it->getMetaValue("id_target_num_transitions").toString().toInt();
408 
409  std::vector<std::string> id_target_total_mi(id_target_num_transitions, "NULL");
410  if (!feature_it->getMetaValue("id_target_total_mi").isEmpty())
411  {
412  id_target_total_mi = ListUtils::create<std::string>((String)feature_it->getMetaValue("id_target_total_mi"),';');
413  }
414 
415  std::vector<std::string> id_target_ind_mi_score(id_target_num_transitions, "NULL");
416  if (!feature_it->getMetaValue("id_target_ind_mi_score").isEmpty() && feature_it->getMetaValue("id_target_ind_mi_score") != "")
417  {
418  id_target_ind_mi_score = ListUtils::create<std::string>((String)feature_it->getMetaValue("id_target_ind_mi_score"),';');
419  }
420 
421  std::vector<std::string> id_target_ind_mi_ratio_score(id_target_num_transitions, "NULL");
422  if (!feature_it->getMetaValue("id_target_ind_mi_ratio_score").isEmpty())
423  {
424  id_target_ind_mi_ratio_score = ListUtils::create<std::string>((String)feature_it->getMetaValue("id_target_ind_mi_ratio_score"),';');
425  }
426 
427  for (int i = 0; i < id_target_num_transitions; ++i)
428  {
429  if (id_target_total_mi[i] == "")
430  {
431  id_target_total_mi[i] = "NULL";
432  }
433  if (id_target_ind_mi_score[i] == "")
434  {
435  id_target_ind_mi_score[i] = "NULL";
436  }
437  if (id_target_ind_mi_ratio_score[i] == "")
438  {
439  id_target_ind_mi_ratio_score[i] = "NULL";
440  }
441 
442  sql_feature_uis_transition << "INSERT INTO FEATURE_TRANSITION (FEATURE_ID, TRANSITION_ID, AREA_INTENSITY, TOTAL_AREA_INTENSITY, APEX_INTENSITY, TOTAL_MI, VAR_INTENSITY_SCORE, VAR_INTENSITY_RATIO_SCORE, VAR_LOG_INTENSITY, VAR_XCORR_COELUTION, VAR_XCORR_SHAPE, VAR_LOG_SN_SCORE, VAR_MASSDEV_SCORE, VAR_MI_SCORE, VAR_MI_RATIO_SCORE, VAR_ISOTOPE_CORRELATION_SCORE, VAR_ISOTOPE_OVERLAP_SCORE) VALUES ("
443  << feature_id << ", "
444  << id_target_transition_names[i] << ", "
445  << id_target_area_intensity[i] << ", "
446  << id_target_total_area_intensity[i] << ", "
447  << id_target_apex_intensity[i] << ", "
448  << id_target_total_mi[i] << ", "
449  << id_target_intensity_score[i] << ", "
450  << id_target_intensity_ratio_score[i] << ", "
451  << id_target_log_intensity[i] << ", "
452  << id_target_ind_xcorr_coelution[i] << ", "
453  << id_target_ind_xcorr_shape[i] << ", "
454  << id_target_ind_log_sn_score[i] << ", "
455  << id_target_ind_massdev_score[i] << ", "
456  << id_target_ind_mi_score[i] << ", "
457  << id_target_ind_mi_ratio_score[i] << ", "
458  << id_target_ind_isotope_correlation[i] << ", "
459  << id_target_ind_isotope_overlap[i] << "); ";
460  }
461  }
462 
463  std::vector<String> id_decoy_transition_names = ListUtils::create<String>((String)feature_it->getMetaValue("id_decoy_transition_names"),';');
464  std::vector<double> id_decoy_area_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_area_intensity"),';');
465  std::vector<double> id_decoy_total_area_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_total_area_intensity"),';');
466  std::vector<double> id_decoy_apex_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_apex_intensity"),';');
467  std::vector<double> id_decoy_intensity_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_intensity_score"),';');
468  std::vector<double> id_decoy_intensity_ratio_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_intensity_ratio_score"),';');
469  std::vector<double> id_decoy_log_intensity = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_ind_log_intensity"),';');
470  std::vector<double> id_decoy_ind_xcorr_coelution = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_ind_xcorr_coelution"),';');
471  std::vector<double> id_decoy_ind_xcorr_shape = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_ind_xcorr_shape"),';');
472  std::vector<double> id_decoy_ind_log_sn_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_ind_log_sn_score"),';');
473  std::vector<double> id_decoy_ind_massdev_score = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_ind_massdev_score"),';');
474  std::vector<double> id_decoy_ind_isotope_correlation = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_ind_isotope_correlation"),';');
475  std::vector<double> id_decoy_ind_isotope_overlap = ListUtils::create<double>((String)feature_it->getMetaValue("id_decoy_ind_isotope_overlap"),';');
476 
477  if ((String)feature_it->getMetaValue("id_decoy_num_transitions") != "")
478  {
479  int id_decoy_num_transitions = feature_it->getMetaValue("id_decoy_num_transitions").toString().toInt();
480 
481  std::vector<std::string> id_decoy_total_mi(id_decoy_num_transitions, "NULL");
482  if (!feature_it->getMetaValue("id_decoy_total_mi").isEmpty())
483  {
484  id_decoy_total_mi = ListUtils::create<std::string>((String)feature_it->getMetaValue("id_decoy_total_mi"),';');
485  }
486 
487  std::vector<std::string> id_decoy_ind_mi_score(id_decoy_num_transitions, "NULL");
488  if (!feature_it->getMetaValue("id_decoy_ind_mi_score").isEmpty() && feature_it->getMetaValue("id_decoy_ind_mi_score") != "")
489  {
490  id_decoy_ind_mi_score = ListUtils::create<std::string>((String)feature_it->getMetaValue("id_decoy_ind_mi_score"),';');
491  }
492 
493  std::vector<std::string> id_decoy_ind_mi_ratio_score(id_decoy_num_transitions, "NULL");
494  if (!feature_it->getMetaValue("id_decoy_ind_mi_ratio_score").isEmpty())
495  {
496  id_decoy_ind_mi_ratio_score = ListUtils::create<std::string>((String)feature_it->getMetaValue("id_decoy_ind_mi_ratio_score"),';');
497  }
498 
499  for (int i = 0; i < feature_it->getMetaValue("id_decoy_num_transitions").toString().toInt(); ++i)
500  {
501  if (id_decoy_total_mi[i] == "")
502  {
503  id_decoy_total_mi[i] = "NULL";
504  }
505  if (id_decoy_ind_mi_score[i] == "")
506  {
507  id_decoy_ind_mi_score[i] = "NULL";
508  }
509  if (id_decoy_ind_mi_ratio_score[i] == "")
510  {
511  id_decoy_ind_mi_ratio_score[i] = "NULL";
512  }
513 
514  sql_feature_uis_transition << "INSERT INTO FEATURE_TRANSITION (FEATURE_ID, TRANSITION_ID, AREA_INTENSITY, TOTAL_AREA_INTENSITY, APEX_INTENSITY, TOTAL_MI, VAR_INTENSITY_SCORE, VAR_INTENSITY_RATIO_SCORE, VAR_LOG_INTENSITY, VAR_XCORR_COELUTION, VAR_XCORR_SHAPE, VAR_LOG_SN_SCORE, VAR_MASSDEV_SCORE, VAR_MI_SCORE, VAR_MI_RATIO_SCORE, VAR_ISOTOPE_CORRELATION_SCORE, VAR_ISOTOPE_OVERLAP_SCORE) VALUES ("
515  << feature_id << ", "
516  << id_decoy_transition_names[i] << ", "
517  << id_decoy_area_intensity[i] << ", "
518  << id_decoy_total_area_intensity[i] << ", "
519  << id_decoy_apex_intensity[i] << ", "
520  << id_decoy_total_mi[i] << ", "
521  << id_decoy_intensity_score[i] << ", "
522  << id_decoy_intensity_ratio_score[i] << ", "
523  << id_decoy_log_intensity[i] << ", "
524  << id_decoy_ind_xcorr_coelution[i] << ", "
525  << id_decoy_ind_xcorr_shape[i] << ", "
526  << id_decoy_ind_log_sn_score[i] << ", "
527  << id_decoy_ind_massdev_score[i] << ", "
528  << id_decoy_ind_mi_score[i] << ", "
529  << id_decoy_ind_mi_ratio_score[i] << ", "
530  << id_decoy_ind_isotope_correlation[i] << ", "
531  << id_decoy_ind_isotope_overlap[i] << "); ";
532  }
533  }
534  }
535  }
536 
537  if (enable_uis_scoring_)
538  {
539  sql << sql_feature.str() << sql_feature_ms1.str() << sql_feature_ms2.str() << sql_feature_uis_transition.str();
540  }
541  else
542  {
543  sql << sql_feature.str() << sql_feature_ms1.str() << sql_feature_ms2.str() << sql_feature_ms2_transition.str();
544  }
545 
546  return(sql.str());
547  }
548 
562  void writeLines(const std::vector<String>& to_osw_output)
563  {
564  sqlite3 *db;
565  char *zErrMsg = nullptr;
566  int rc;
567  // char *create_sql;
568 
569  // Open database
570  rc = sqlite3_open(output_filename_.c_str(), &db);
571  if( rc )
572  {
573  fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
574  }
575 
576  sqlite3_exec(db, "BEGIN TRANSACTION", nullptr, nullptr, &zErrMsg);
577 
578  for (Size i = 0; i < to_osw_output.size(); i++)
579  {
580  rc = sqlite3_exec(db, to_osw_output[i].c_str(), callback, nullptr, &zErrMsg);
581  if( rc != SQLITE_OK )
582  {
583  std::string error_message = zErrMsg;
584  sqlite3_free(zErrMsg);
585  throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
586  error_message);
587  }
588  }
589 
590  sqlite3_exec(db, "END TRANSACTION", nullptr, nullptr, &zErrMsg);
591 
592  sqlite3_close(db);
593  }
594 
595  };
596 
597 }
598 
String input_filename_
Definition: OpenSwathOSWWriter.h:61
String output_filename_
Definition: OpenSwathOSWWriter.h:60
A more convenient string class.
Definition: String.h:57
void writeLines(const std::vector< String > &to_osw_output)
Write data to disk.
Definition: OpenSwathOSWWriter.h:562
A container for features.
Definition: FeatureMap.h:93
bool sonar_
Definition: OpenSwathOSWWriter.h:65
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
bool use_ms1_traces_
Definition: OpenSwathOSWWriter.h:64
static int callback(void *, int argc, char **argv, char **azColName)
Definition: OpenSwathOSWWriter.h:84
Class to write out an OpenSwath OSW SQLite output (PyProphet input)
Definition: OpenSwathOSWWriter.h:58
void writeHeader()
Initializes file by generating SQLite tables.
Definition: OpenSwathOSWWriter.h:104
bool enable_uis_scoring_
Definition: OpenSwathOSWWriter.h:66
A generator for unique ids.
Definition: UniqueIdGenerator.h:59
A method or algorithm argument contains illegal values.
Definition: Exception.h:648
String prepareLine(const OpenSwath::LightCompound &, const OpenSwath::LightTransition *, FeatureMap &output, String id) const
Prepare a single line (feature) for output.
Definition: OpenSwathOSWWriter.h:245
Int toInt() const
Conversion to int.
bool doWrite_
Definition: OpenSwathOSWWriter.h:63
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
Definition: TransitionExperiment.h:149
OpenMS::UInt64 run_id_
Definition: OpenSwathOSWWriter.h:62
Definition: TransitionExperiment.h:46
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenSwathOSWWriter(const String &output_filename, const String &input_filename="inputfile", bool ms1_scores=false, bool sonar=false, bool uis_scores=false)
Definition: OpenSwathOSWWriter.h:70
bool isActive() const
Definition: OpenSwathOSWWriter.h:95