File: proteincode_lib.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 (330 lines) | stat: -rw-r--r-- 9,559 bytes parent folder | download | duplicates (2)
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
#include "proteincode_lib.h"
#include <pappsomspp/core/utils.h>
#include "tests/tests-config.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <pappsomspp/core/pappsoexception.h>
#include <pappsomspp/core/protein/proteinintegercode.h>


void
FastaProteinArrayHandler::setSequence(const QString &description_in, const QString &sequence_in)
{
  m_proteinArray.push_back({description_in, sequence_in});
}

const std::vector<pappso::Protein> &
FastaProteinArrayHandler::getProteinArray() const
{
  return m_proteinArray;
};


void
writeProteinConvolutionScore(const QString &filename,
                             const pappso::Protein &protein,
                             const pappso::ProteinPresenceAbsenceMatrix *pmatrix,
                             const std::vector<double> &convolution_score)
{

  QFile file(filename);
  // file.open(QIODevice::WriteOnly);
  OdsDocWriter writer(&file);

  writer.writeSheet(protein.getAccession());
  writer.writeCell("sequence");
  for(long long i = 0; i < protein.getSequence().size(); i++)
    {
      writer.writeCell(protein.getSequence()[i]);
    }
  writer.writeLine();
  writer.writeCell("convolution score");
  for(long long i = 0; i < protein.getSequence().size(); i++)
    {
      writer.writeCell(convolution_score[i]);
    }
  writer.writeLine();
  if(pmatrix != nullptr)
    {


      auto matrix = pmatrix->getPresenceAbsenceMatrix();
      for(long long j = 0; j < 5; j++)
        {

          writer.writeCell(QString("%1aa").arg(j + 1));
          for(long long i = 0; i < protein.getSequence().size(); i++)
            {

              if(matrix(i, j) ==
                 pappso::ProteinPresenceAbsenceMatrix::ProteinPresenceAbsenceMatrixElement::present)
                {
                  writer.writeCell("X");
                }
              else
                {
                  writer.writeEmptyCell();
                }
            }

          writer.writeLine();
        }
    }

  writer.close();
}

void
writeProteinConvolutionScoreJson(const QString &filein,
                                 const pappso::Protein &protein,
                                 const pappso::ProteinIntegerCode &protein_code,
                                 const pappso::ProteinPresenceAbsenceMatrix *pmatrix,
                                 const std::vector<double> &convolution_score)
{
  qDebug() << filein;
  QString filepath = QString(CMAKE_SOURCE_DIR).append("/tests/typst/data/").append(filein);
  QFile jsonf(filepath);


  if(jsonf.open(QIODevice::WriteOnly))
    {
      qWarning();
      QJsonDocument doc;
      QJsonObject protein_conv;
      protein_conv.insert("accession", protein.getAccession());
      protein_conv.insert("description", protein.getDescription());

      QJsonArray sequence_json;
      for(long long i = 0; i < protein.getSequence().size(); i++)
        {
          sequence_json.push_back((QString)protein.getSequence()[i].toLatin1());
        }
      qWarning();
      protein_conv.insert("sequence", sequence_json);

      QJsonArray matrix_code_json;
      QJsonArray matrix_single_code_json;
      for(int code : protein_code.getSeqAaCode())
        {
          matrix_single_code_json.push_back(code);
        }
      matrix_code_json.push_back(matrix_single_code_json);
        
     
      QJsonArray matrix_2mer_code_json;
      for (int code: protein_code.getPeptideCodedFragment(2)) {
          matrix_2mer_code_json.push_back(code);
      }
      matrix_code_json.push_back(matrix_2mer_code_json);
      
      
      QJsonArray matrix_3mer_code_json;
      for (int code: protein_code.getPeptideCodedFragment(3)) {
          matrix_3mer_code_json.push_back(code);
      }
      matrix_code_json.push_back(matrix_3mer_code_json);
      
      
      QJsonArray matrix_4mer_code_json;
      for (int code: protein_code.getPeptideCodedFragment(4)) {
          matrix_4mer_code_json.push_back(code);
      }
      matrix_code_json.push_back(matrix_4mer_code_json);

      protein_conv.insert("protein_matrix_code", matrix_code_json);
      qWarning();
      QJsonArray convolution_score_json;

      for(long long i = 0; i < protein.getSequence().size(); i++)
        {
          convolution_score_json.push_back(convolution_score[i]);
        }
      protein_conv.insert("convolution_score", convolution_score_json);

      qWarning();
      if(pmatrix != nullptr)
        {


          qWarning() << "coucou";
          QJsonArray pmatrix_json;
          auto matrix = pmatrix->getPresenceAbsenceMatrix();
          for(long long j = 0; j < 5; j++)
            {
              QJsonArray pmatrix_seq_kmer_json;
              for(long long i = 0; i < protein.getSequence().size(); i++)
                {

                  if(matrix(i, j) == pappso::ProteinPresenceAbsenceMatrix::
                                       ProteinPresenceAbsenceMatrixElement::present)
                    {
                      pmatrix_seq_kmer_json.push_back(true);
                    }
                  else
                    {
                      pmatrix_seq_kmer_json.push_back(false);
                    }
                }
              pmatrix_json.push_back(pmatrix_seq_kmer_json);
            }
          protein_conv.insert("presence_matrix", pmatrix_json);
        }

      doc.setObject(protein_conv);
      jsonf.write(doc.toJson());
      jsonf.close();

      qWarning() << "coucou";
    }
  else
    {
      throw pappso::PappsoException(
        QObject::tr("Unable to write JSON output file %1").arg(filepath));
    }
}

void
writeSelfSpectrum(const QString &filename,
                  const pappso::specself::SelfSpectrum &self_spectrum,
                  const pappso::AaStringCodec &aa_codec)
{
  qDebug();
  QFile file(filename);
  // file.open(QIODevice::WriteOnly);
  OdsDocWriter writer(&file);

  writer.writeSheet("spectrum_matrix");

  const pappso::specself::SelfSpectrumMatrix &matrix = self_spectrum.getMatrix();
  const pappso::Trace &trace                         = self_spectrum.getTrace();


  writer.writeLine();
  writer.writeEmptyCell();
  writer.writeEmptyCell();
  for(auto datapoint : trace)
    {
      writer.writeCell(datapoint.x);
    }
  auto it_trace = trace.begin();
  for(auto itmi = ++matrix.begin1(); itmi != matrix.end1(); ++itmi, ++it_trace)
    {
      writer.writeLine();
      writer.writeCell(it_trace->x);
      writer.writeCell(it_trace->y);
      for(auto itmj = ++itmi.begin(); itmj != itmi.end(); itmj++)
        {
          writer.writeCell(aa_codec.decode((*itmj).aaCodeList).join(" "));
        }
    }
  qDebug();
  writer.close();
}


void
writeSpectralAlignmentMatrix(const QString &filename,
                             const pappso::specglob::SpectralAlignment &spectral_alignment)
{

  QFile file(filename);
  // file.open(QIODevice::WriteOnly);
  OdsDocWriter writer(&file);

  const matrix<pappso::specglob::SpectralAlignmentDataPoint> &matrix =
    spectral_alignment.getMatrix();

  pappso::PeptideSp peptide_sp = spectral_alignment.getPeptideSpectraCsp().get()->getPeptideSp();

  writer.writeSheet("score");
  std::size_t i = 0;
  // output_stream << "\n" << getExpeSpec().getMassList());
  for(auto itr1 = matrix.begin1(); itr1 != matrix.end1(); ++itr1, i++)
    {
      if(i < peptide_sp.get()->size())
        writer.writeCell(peptide_sp.get()->getConstAa(i).toProForma());
      else
        writer.writeEmptyCell();
      for(auto itr2 = itr1.begin(); itr2 != itr1.end(); itr2++)
        {
          writer.writeCell((*itr2).score);
        }
      writer.writeLine();
    }

  writer.writeSheet("alignment_type");
  for(auto itr1 = matrix.begin1(); itr1 != matrix.end1(); ++itr1)
    {
      for(auto itr2 = itr1.begin(); itr2 != itr1.end(); itr2++)
        {
          writer.writeCell(QString("[%1,%2]")
                             .arg(pappso::Utils::toString((*itr2).alignment_type))
                             .arg((*itr2).origin_column_indices));
        }
      writer.writeLine();
    }

  writer.writeSheet("mass_difference");
  for(auto itr1 = matrix.begin1(); itr1 != matrix.end1(); ++itr1)
    {
      for(auto itr2 = itr1.begin(); itr2 != itr1.end(); itr2++)
        {
          writer.writeCell((*itr2).mass_difference);
        }
      writer.writeLine();
    }
  writer.close();
}


void
writeJsonAaCode(const QString &filein, const pappso::AaCode &aa_code)
{
  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 aacode_json;


      QJsonArray aa_list;
      aa_list.push_back("");
      for(auto &aa : aa_code.getAaCollection())
        {
          aa_list.push_back((QString)aa.getLetter());
        }
      QJsonArray code_list;
      code_list.push_back(0);
      for(auto &aa : aa_code.getAaCollection())
        {
          code_list.push_back(aa_code.getAaCode(aa.getLetter()));
        }
      QJsonArray mass_list;
      mass_list.push_back(0);
      for(auto &aa : aa_code.getAaCollection())
        {
          mass_list.push_back(aa.getMass());
        }

      aacode_json.insert("aa", aa_list);
      aacode_json.insert("code", code_list);
      aacode_json.insert("mass", mass_list);


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

  qDebug() << filepath;
}