File: common.cpp

package info (click to toggle)
libpappsomspp 0.11.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,032 kB
  • sloc: cpp: 78,332; xml: 44,164; python: 668; sql: 186; sh: 33; makefile: 31
file content (377 lines) | stat: -rw-r--r-- 11,058 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "common.h"
#include <pappsomspp/core/exception/exceptionnotrecognized.h>
#include <pappsomspp/core/utils.h>
#include <QJsonObject>
#include <QJsonArray>
#include "tests/tests-config.h"

using namespace pappso;

MassSpectrum
readMgf(const QString &filename)
{
  try
    {
      qDebug();
      MsFileAccessor accessor(filename, "msrun");
      qDebug();

      std::vector<MsRunIdCstSPtr> msrun_ids = accessor.getMsRunIds();
      if(accessor.getFileFormat() == Enums::MsDataFormat::unknown)
        {
          throw pappso::ExceptionNotRecognized("file format not recognized");
        }
      if(msrun_ids.size() == 0)
        {
          throw pappso::PappsoException("msrun_ids.size() == 0");
        }
      MsRunReaderSPtr reader = accessor.msRunReaderSPtr(msrun_ids.front());
      qDebug() << msrun_ids.front().get()->getXmlId();
      std::cout << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
                << reader->spectrumListSize() << std::endl;
      MassSpectrumSPtr spectrum_sp = reader->massSpectrumSPtr(0);
      std::cout << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
                << spectrum_sp.get()->size() << std::endl;
      // spectrum_sp.get()->debugPrintValues();
      spectrum_sp.get()->sortMz();
      return *(spectrum_sp.get());
    }
  catch(pappso::PappsoException &error)
    {
      std::cout << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
                << QString("ERROR reading file %1 :").arg(filename).toStdString().c_str() << " "
                << error.what();
      throw error;
    }
}

QualifiedMassSpectrum
readQualifiedMassSpectrumMgf(const QString &filename)
{
  try
    {
      qDebug();
      MsFileAccessor accessor(filename, "msrun");
      std::vector<MsRunIdCstSPtr> msrun_ids = accessor.getMsRunIds();
      if(accessor.getFileFormat() == Enums::MsDataFormat::unknown)
        {
          throw pappso::ExceptionNotRecognized("file format not recognized");
        }
      if(msrun_ids.size() == 0)
        {
          throw pappso::PappsoException("msrun_ids.size() == 0");
        }
      qDebug();
      MsRunReaderSPtr reader = accessor.msRunReaderSPtr(msrun_ids.front());
      qDebug() << msrun_ids.front().get()->getXmlId();
      std::cout << reader->spectrumListSize() << std::endl;
      QualifiedMassSpectrum spectrum_sp = reader->qualifiedMassSpectrum(0, true);
      return spectrum_sp;
    }
  catch(pappso::PappsoException &error)
    {
      std::cout << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
                << QString("ERROR reading file %1 : %2")
                     .arg(filename)
                     .arg(error.qwhat())
                     .toStdString()
                     .c_str();
      throw error;
    }
}

bool
compareTextFiles(const QString &file1, const QString &file2)
{

  QFile fnew(file1);
  fnew.open(QFile::ReadOnly | QFile::Text);
  // REQUIRE(fnew.errorString().toStdString() == "");
  QTextStream infnew(&fnew);

  QFile fold(file2);
  fold.open(QFile::ReadOnly | QFile::Text);
  QTextStream infold(&fold);

  return (infold.readAll().toStdString() == infnew.readAll().toStdString());
}


std::vector<std::size_t>
getMapKeys(const std::map<std::size_t, std::size_t> &map_table)
{
  std::vector<std::size_t> vector;

  for(auto &&pair : map_table)
    vector.push_back(pair.first);

  return vector;
}


std::vector<std::size_t>
getMapValues(const std::map<std::size_t, std::size_t> &map_table)
{
  std::vector<std::size_t> vector;

  for(auto &&pair : map_table)
    vector.push_back(pair.second);

  return vector;
}

QJsonArray
toJson(const std::vector<double> &myVec)
{
  QJsonArray result;
  for(auto i = myVec.begin(); i != myVec.end(); i++)
    {
      result.push_back((*i));
    }
  return result;
}

void
writeJsonTrace(const QString &filein, Trace &xic)
{
  QString filepath = QString(CMAKE_SOURCE_DIR).append("/tests/typst/data/").append(filein);
  qDebug() << filepath;
  QFile jsonf(filepath);


  if(jsonf.open(QIODevice::WriteOnly))
    {
      QJsonDocument doc;
      QJsonObject trace_json;
      trace_json.insert("x", toJson(xic.xValues()));
      trace_json.insert("y", toJson(xic.yValues()));
      doc.setObject(trace_json);
      jsonf.write(doc.toJson());
      jsonf.close();
    }
  else
    {
      throw pappso::PappsoException(
        QObject::tr("Unable to write JSON output file %1").arg(filepath));
    }

  qDebug() << filepath;
}

void
writeJsonPsm(const QString &filein,
             const MassSpectrum &mass_spectrum,
             const pappso::PeptideSpectrumMatch &psm)
{
  qDebug() << filein;
  QString filepath = QString(CMAKE_SOURCE_DIR).append("/tests/typst/data/").append(filein);
  QFile jsonf(filepath);


  if(jsonf.open(QIODevice::WriteOnly))
    {
      QJsonDocument doc;
      QJsonObject psm_json;


      struct data_plot
      {
        pappso::DataPoint point;
        double mz_th;
        int charge;
        int size;
      };

      std::map<Enums::PeptideIon, std::vector<data_plot>> ion_map;

      QJsonObject spectrum_json;
      spectrum_json.insert("mz", toJson(mass_spectrum.xValues()));
      spectrum_json.insert("intensity", toJson(mass_spectrum.yValues()));


      for(const PeakIonMatch &peakion_match : psm)
        {
          auto it = ion_map.insert({peakion_match.getPeptideIonType(), {}});
          it.first->second.push_back(
            {peakion_match.getPeak(),
             peakion_match.getPeptideFragmentIonSp().get()->getMz(peakion_match.getCharge()),
             (int)peakion_match.getCharge(),
             (int)peakion_match.getPeptideFragmentIonSp().get()->size()});
        }

      QJsonObject psm_ion_json;
      for(auto &cle_valeur : ion_map)
        {
          std::vector<data_plot> &current_vector = cle_valeur.second;

          std::sort(current_vector.begin(),
                    current_vector.end(),
                    [](const data_plot &a, const data_plot &b) { return a.point.x < b.point.y; });

          QJsonArray current_ion_json_arr;

          for(const data_plot &one_point : current_vector)
            {
              QJsonObject current_ion;
              current_ion.insert("mz", one_point.point.x);
              current_ion.insert("mzth", one_point.mz_th);
              current_ion.insert("intensity", one_point.point.y);
              current_ion.insert("size", one_point.size);
              current_ion.insert("charge", one_point.charge);

              current_ion_json_arr.push_back(current_ion);
            }


          psm_ion_json.insert(Utils::toString(cle_valeur.first), current_ion_json_arr);
        }
      psm_json.insert("ion-series", psm_ion_json);
      psm_json.insert("spectra", spectrum_json);

      doc.setObject(psm_json);
      jsonf.write(doc.toJson());
      jsonf.close();
    }
  else
    {
      throw pappso::PappsoException(
        QObject::tr("Unable to write JSON output file %1").arg(filepath));
    }

  qDebug() << filepath;
}

void
writeJsonPsmIsotope(const QString &filein,
                    const pappso::MassSpectrum &mass_spectrum,
                    const pappso::PeptideIsotopeSpectrumMatch &psm)
{
  qDebug() << filein;
  QString filepath = QString(CMAKE_SOURCE_DIR).append("/tests/typst/data/").append(filein);
  QFile jsonf(filepath);
  QJsonDocument doc;
  QJsonObject psm_json;
  QJsonObject spectrum_json;
  QJsonObject psm_ion_json;
  struct data_plot
  {
    pappso::DataPoint point;
    double mz_th;
    int charge;
    int size;
    int isotope_number;
    int isotope_rank;
  };
  std::map<Enums::PeptideIon, std::vector<data_plot>> ion_map;

  if(jsonf.open(QIODevice::WriteOnly))
    {


      spectrum_json.insert("mz", toJson(mass_spectrum.xValues()));
      spectrum_json.insert("intensity", toJson(mass_spectrum.yValues()));
      psm_json.insert("spectra", spectrum_json);

      for(const PeakIonIsotopeMatch &peakion_match : psm)
        {
          auto it = ion_map.insert({peakion_match.getPeptideIonType(), {}});
          it.first->second.push_back({
            peakion_match.getPeak(),
            peakion_match.getPeptideFragmentIonSp().get()->getMz(peakion_match.getCharge()),
            (int)peakion_match.getCharge(),
            (int)peakion_match.getPeptideFragmentIonSp().get()->size(),
            (int)peakion_match.getPeptideNaturalIsotopeAverageSp().get()->getIsotopeNumber(),
            (int)peakion_match.getPeptideNaturalIsotopeAverageSp().get()->getIsotopeRank(),
          });
        }

      for(auto &cle_valeur : ion_map)
        {
          std::vector<data_plot> &current_vector = cle_valeur.second;

          std::sort(current_vector.begin(),
                    current_vector.end(),
                    [](const data_plot &a, const data_plot &b) { return a.point.x < b.point.y; });

          QJsonArray current_ion_json_arr;

          for(const data_plot &one_point : current_vector)
            {
              QJsonObject current_ion;
              current_ion.insert("mz", one_point.point.x);
              current_ion.insert("mzth", one_point.mz_th);
              current_ion.insert("intensity", one_point.point.y);
              current_ion.insert("size", one_point.size);
              current_ion.insert("charge", one_point.charge);
              current_ion.insert("isotope", one_point.isotope_number);
              current_ion.insert("rank", one_point.isotope_rank);

              current_ion_json_arr.push_back(current_ion);
            }


          psm_ion_json.insert(Utils::toString(cle_valeur.first), current_ion_json_arr);
        }
      psm_json.insert("ion-series", psm_ion_json);

      qWarning();
      doc.setObject(psm_json);
      qWarning();
      jsonf.write(doc.toJson());
      qWarning();
      jsonf.flush();
      qWarning();
      jsonf.close();
      qWarning();
    }
  else
    {
      throw pappso::PappsoException(
        QObject::tr("Unable to write JSON output file %1").arg(filepath));
    }

  qWarning();
  qDebug() << filepath;
}


void
writeJsonCodeList(const QString &filein,
                  const std::vector<uint32_t> code_list,
                  const pappso::AaStringCodec &aa_codec)
{
  qDebug() << filein;
  QString filepath = QString(CMAKE_SOURCE_DIR).append("/tests/typst/data/").append(filein);
  QFile jsonf(filepath);


  if(jsonf.open(QIODevice::WriteOnly))
    {
      QJsonDocument doc;
      QJsonObject code_list_json;

      QJsonArray arr_code;
      QJsonArray arr_mass;
      QJsonArray arr_str;
      for(auto &code : code_list)
        {
          arr_code.push_back((int32_t)code);
          arr_mass.push_back(aa_codec.getMass(code));
          arr_str.push_back(aa_codec.decode(code));
        }

      code_list_json.insert("arr_code", arr_code);

      code_list_json.insert("arr_mass", arr_mass);

      code_list_json.insert("arr_sequence", arr_str);
      doc.setObject(code_list_json);
      jsonf.write(doc.toJson());
      jsonf.close();
    }
  else
    {
      throw pappso::PappsoException(
        QObject::tr("Unable to write JSON output file %1").arg(filepath));
    }
}