workerthread.cpp 26.5 KB
Newer Older
1
2
3
4
5
6
7
8
/**
 * \file /gui/workerthread.cpp
 * \date 8/5/2017
 * \author Olivier Langella
 * \brief worker thread
 */

/*******************************************************************************
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * Copyright (c) 2017 Olivier Langella <olivier.langella@u-psud.fr>.
 *
 * This file is part of XTPcpp.
 *
 *     XTPcpp is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     XTPcpp is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with XTPcpp.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Olivier Langella <olivier.langella@u-psud.fr> - initial API and
 *implementation
 ******************************************************************************/
30
31

#include "workerthread.h"
Renne Thomas's avatar
Renne Thomas committed
32

33
#include <odsstream/odsdocwriter.h>
34
35
#include <odsstream/tsvoutputstream.h>
#include <odsstream/odsexception.h>
36
#include <pappsomspp/pappsoexception.h>
37
#include <QDebug>
Renne Thomas's avatar
Renne Thomas committed
38
#include <QSettings>
39
#include "../output/exportfastafile.h"
40
#include "../output/masschroqml.h"
Langella Olivier's avatar
Langella Olivier committed
41
#include "../output/masschroqprm.h"
Olivier Langella's avatar
Olivier Langella committed
42
#include "../output/proticdbml.h"
43
#include "../output/ods/odsexport.h"
44
#include "../output/xpip.h"
45
#include "../output/mcqrspectralcount.h"
46
#include "files/xpipfile.h"
Olivier Langella's avatar
Olivier Langella committed
47
48
#include "mainwindow.h"
#include "project_view/projectwindow.h"
49
#include "../core/tandem_run/tandembatchprocess.h"
Olivier Langella's avatar
Olivier Langella committed
50
#include "../core/tandem_run/tandemcondorprocess.h"
Renne Thomas's avatar
Renne Thomas committed
51
#include "../core/masschroq_run/masschroqbatchprocess.h"
52
#include "../core/masschroq_run/masschroqcondorprocess.h"
Renne Thomas's avatar
Renne Thomas committed
53
#include "../output/ods/xicareasheet.h"
54

55
WorkerThread::WorkerThread(MainWindow *p_main_window)
56
{
Langella Olivier's avatar
Langella Olivier committed
57
  _p_main_window = p_main_window;
58
  qDebug();
59

Langella Olivier's avatar
Langella Olivier committed
60
  _p_work_monitor = new WorkMonitor();
61

Langella Olivier's avatar
Langella Olivier committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  // worker message
  connect(_p_work_monitor,
          &WorkMonitor::workerMessage,
          p_main_window,
          &MainWindow::doDisplayLoadingMessage);
  connect(_p_work_monitor,
          &WorkMonitor::workerMessagePercent,
          p_main_window,
          &MainWindow::doDisplayLoadingMessagePercent);
  connect(_p_work_monitor,
          &WorkMonitor::workerAppendText,
          p_main_window,
          &MainWindow::doWorkerAppendText);
  connect(_p_work_monitor,
          &WorkMonitor::workerSetText,
          p_main_window,
          &MainWindow::doWorkerSetText);
  connect(_p_work_monitor,
          &WorkMonitor::workerJobFinished,
          p_main_window,
          &MainWindow::doDisplayJobFinished);
Renne Thomas's avatar
Renne Thomas committed
83
84
85
86
  connect(_p_work_monitor,
          &WorkMonitor::workerJobCanceled,
          p_main_window,
          &MainWindow::doDisplayJobCanceled);
Langella Olivier's avatar
Langella Olivier committed
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
  connect(p_main_window,
          &MainWindow::operateXpipFile,
          this,
          &WorkerThread::doXpipFileLoad);
  connect(p_main_window,
          &MainWindow::operateWritingXpipFile,
          this,
          &WorkerThread::doWritingXpipFile);
  connect(this,
          &WorkerThread::projectReady,
          p_main_window,
          &MainWindow::doProjectReady);
  connect(this,
          &WorkerThread::loadingResultsFinished,
          p_main_window,
          &MainWindow::doLoadingResultsReady);

  connect(p_main_window,
          &MainWindow::operateLoadingResults,
          this,
          &WorkerThread::doLoadingResults);

  connect(p_main_window,
          &MainWindow::operateRunningXtandem,
          this,
          &WorkerThread::doRunningXtandem);
  connect(this,
          &WorkerThread::loadingMessage,
          p_main_window,
          &MainWindow::doDisplayLoadingMessage);
  connect(this,
          &WorkerThread::projectNotReady,
          p_main_window,
          &MainWindow::doProjectNotReady);

  // grouping
  connect(p_main_window,
          &MainWindow::operateGrouping,
          this,
          &WorkerThread::doGrouping);
  connect(this,
          &WorkerThread::groupingFinished,
          p_main_window,
          &MainWindow::doGroupingFinished);
  // masschroq write
  connect(p_main_window,
          &MainWindow::operateWritingMassChroqFile,
          this,
          &WorkerThread::doWritingMassChroqFile);
  connect(p_main_window,
          &MainWindow::operateWritingMassChroqPrmFile,
          this,
          &WorkerThread::doWritingMassChroqPrmFile);
  connect(p_main_window,
          &MainWindow::operateWritingMcqrSpectralCountFile,
          this,
          &WorkerThread::doWritingMcqrSpectralCountFile);
Renne Thomas's avatar
Renne Thomas committed
144
145
146
147
148
149
150
151
  connect(this,
          &WorkerThread::operateWritingMassChroqFileFinished,
          p_main_window,
          &MainWindow::doWritingMassChroQmlFileFinished);
  connect(p_main_window,
          &MainWindow::operateRunningMassChroq,
          this,
          &WorkerThread::doRunningMassChroQ);
Langella Olivier's avatar
Langella Olivier committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  // protic write
  connect(p_main_window,
          &MainWindow::operateWritingProticFile,
          this,
          &WorkerThread::doWritingProticFile);
  // writing ODS file :
  connect(p_main_window,
          &MainWindow::operateWritingOdsFile,
          this,
          &WorkerThread::doWritingOdsFile);
  connect(this,
          &WorkerThread::operationFailed,
          p_main_window,
          &MainWindow::doOperationFailed);
  connect(this,
          &WorkerThread::operationFinished,
          p_main_window,
          &MainWindow::doOperationFinished);
  // FASTA file write
  connect(p_main_window,
          &MainWindow::operateWritingFastaFile,
          this,
          &WorkerThread::doWritingFastaFile);
Olivier Langella's avatar
Olivier Langella committed
175

176
  qDebug();
177
178
}

179
180
181

void
WorkerThread::connectProjectWindow(ProjectWindow *p_project_window)
182
{
183
  qDebug();
184

185
  //_p_work_monitor = new WorkMonitor();
Olivier Langella's avatar
Olivier Langella committed
186

Langella Olivier's avatar
Langella Olivier committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  // worker message
  connect(_p_work_monitor,
          &WorkMonitor::workerMessage,
          p_project_window,
          &ProjectWindow::doDisplayLoadingMessage);
  connect(_p_work_monitor,
          &WorkMonitor::workerMessagePercent,
          p_project_window,
          &ProjectWindow::doDisplayLoadingMessagePercent);
  connect(this,
          &WorkerThread::loadingMessage,
          p_project_window,
          &ProjectWindow::doDisplayLoadingMessage);
  // grouping
  connect(p_project_window,
          &ProjectWindow::operateGrouping,
          this,
          &WorkerThread::doGrouping);
205

Langella Olivier's avatar
Langella Olivier committed
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
  connect(this,
          &WorkerThread::groupingFinished,
          p_project_window,
          &ProjectWindow::doGroupingFinished);


  // PTM grouping on IdentificationGroup
  connect(p_project_window,
          &ProjectWindow::operatePtmGroupingOnIdentification,
          this,
          &WorkerThread::doPtmGroupingOnIdentification);
  connect(this,
          &WorkerThread::ptmGroupingOnIdentificationFinished,
          p_project_window,
          &ProjectWindow::refreshPtmGroup);

  // grouping on IdentificationGroup
  connect(p_project_window,
          &ProjectWindow::operateGroupingOnIdentification,
          this,
          &WorkerThread::doGroupingOnIdentification);
  connect(this,
          &WorkerThread::groupingOnIdentificationFinished,
          p_project_window,
          &ProjectWindow::refreshGroup);

  connect(this,
          &WorkerThread::operationFailed,
          p_project_window,
          &ProjectWindow::doOperationFailed);
  connect(this,
          &WorkerThread::operationFinished,
          p_project_window,
          &ProjectWindow::doOperationFinished);
Renne Thomas's avatar
Renne Thomas committed
240
241
242
243
244
245
246
247
248

  connect(p_project_window,
          &ProjectWindow::operateWriteXicAreaInCsv,
          this,
          &WorkerThread::doWriteXicAreaInCsv);
  connect(this,
          &WorkerThread::exportXicToCsvFinished,
          p_project_window,
          &ProjectWindow::doExportXicAreaToCsvFinished);
249
  qDebug();
250
251
}

252

253
254
WorkerThread::~WorkerThread()
{
255
  qDebug();
256
257
}

258
259
260
void
WorkerThread::doXpipFileLoad(QString filename)
{
261
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
262
  try
263
    {
Langella Olivier's avatar
Langella Olivier committed
264
265
266
267
268
269
      QFileInfo new_xpip_file(filename);
      emit loadingMessage(tr("loading XPIP file"));
      qDebug() << "WorkerThread::doXpipFileLoad new_xpip_file "
               << new_xpip_file.absoluteFilePath();
      XpipFile xpip_file(new_xpip_file);
      ProjectSp project_sp = xpip_file.getProjectSp(_p_work_monitor);
270
      project_sp.get()->setProjectName(filename);
Langella Olivier's avatar
Langella Olivier committed
271
      emit projectReady(project_sp);
272
    }
Langella Olivier's avatar
Langella Olivier committed
273
  catch(pappso::PappsoException &error)
274
    {
Langella Olivier's avatar
Langella Olivier committed
275
276
      emit projectNotReady(
        tr("Error while reading XPIP file :\n%1").arg(error.qwhat()));
277
278
    }

279
  qDebug();
280
281
}

282
283
void
WorkerThread::doPtmGroupingOnIdentification(
284
  IdentificationGroup *p_identification_group, PtmMode ptm_mode)
285
{
286
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
287
  try
288
    {
Langella Olivier's avatar
Langella Olivier committed
289
290
      emit loadingMessage(tr("computing PTM islands"));
      try
291
        {
292
          p_identification_group->startPtmGrouping(ptm_mode);
Olivier Langella's avatar
Olivier Langella committed
293
        }
294
      catch(pappso::PappsoException &exception_pappso)
295
        {
Langella Olivier's avatar
Langella Olivier committed
296
          emit operationFailed(tr("Error computing PTM islands : %1")
297
                                 .arg(exception_pappso.qwhat()));
Olivier Langella's avatar
Olivier Langella committed
298
        }
299
      catch(std::exception &exception_std)
300
        {
Langella Olivier's avatar
Langella Olivier committed
301
302
          emit operationFailed(
            tr("Error computing PTM islands : %1").arg(exception_std.what()));
Olivier Langella's avatar
Olivier Langella committed
303
        }
Langella Olivier's avatar
Langella Olivier committed
304
305
      emit loadingMessage(tr("computing PTM islands finished"));
      emit ptmGroupingOnIdentificationFinished(p_identification_group);
Olivier Langella's avatar
Olivier Langella committed
306
    }
Langella Olivier's avatar
Langella Olivier committed
307
  catch(pappso::PappsoException &error)
308
    {
Langella Olivier's avatar
Langella Olivier committed
309
310
      emit operationFailed(
        tr("Error while computing PTM islands :\n%1").arg(error.qwhat()));
Olivier Langella's avatar
Olivier Langella committed
311
    }
312
  qDebug();
Olivier Langella's avatar
Olivier Langella committed
313
}
314

315
316
void
WorkerThread::doGroupingOnIdentification(
Langella Olivier's avatar
Langella Olivier committed
317
318
319
  IdentificationGroup *p_identification_group,
  ContaminantRemovalMode contaminant_removal_mode,
  GroupingType grouping_type)
320
{
321
322
  qDebug();
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
323
  try
324
    {
Langella Olivier's avatar
Langella Olivier committed
325
326
      emit loadingMessage(tr("grouping proteins"));
      try
327
        {
Langella Olivier's avatar
Langella Olivier committed
328
329
          p_identification_group->startGrouping(
            contaminant_removal_mode, grouping_type, _p_work_monitor);
330
        }
331
      catch(pappso::PappsoException &exception_pappso)
332
        {
Langella Olivier's avatar
Langella Olivier committed
333
          emit operationFailed(tr("Error grouping identification : %1")
334
                                 .arg(exception_pappso.qwhat()));
335
        }
336
      catch(std::exception &exception_std)
337
        {
Langella Olivier's avatar
Langella Olivier committed
338
339
          emit operationFailed(
            tr("Error grouping identification : %1").arg(exception_std.what()));
340
        }
Langella Olivier's avatar
Langella Olivier committed
341
342
      emit loadingMessage(tr("grouping proteins finished"));
      emit groupingOnIdentificationFinished(p_identification_group);
343
    }
Langella Olivier's avatar
Langella Olivier committed
344
  catch(pappso::PappsoException &error)
345
    {
Langella Olivier's avatar
Langella Olivier committed
346
      emit operationFailed(tr("Error while grouping :\n%1").arg(error.qwhat()));
347
    }
348
  qDebug();
349
}
350
351
352
void
WorkerThread::doGrouping(ProjectSp project_sp)
{
353
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
354
  try
355
    {
Langella Olivier's avatar
Langella Olivier committed
356
357
      emit loadingMessage(tr("grouping proteins"));
      try
358
        {
359
          qDebug();
Langella Olivier's avatar
Langella Olivier committed
360
          project_sp.get()->startGrouping(_p_work_monitor);
361
          qDebug();
362
        }
363
      catch(pappso::PappsoException &exception_pappso)
364
        {
Langella Olivier's avatar
Langella Olivier committed
365
366
          emit operationFailed(
            tr("Error grouping project : %1").arg(exception_pappso.qwhat()));
367
        }
368
      catch(std::exception &exception_std)
369
        {
Langella Olivier's avatar
Langella Olivier committed
370
371
          emit operationFailed(
            tr("Error grouping project : %1").arg(exception_std.what()));
372
        }
Langella Olivier's avatar
Langella Olivier committed
373
374
      emit loadingMessage(tr("grouping proteins finished"));
      emit groupingFinished();
375
    }
Langella Olivier's avatar
Langella Olivier committed
376
  catch(pappso::PappsoException &error)
377
    {
Langella Olivier's avatar
Langella Olivier committed
378
      emit operationFailed(tr("Error while grouping :\n%1").arg(error.qwhat()));
379
    }
380
  qDebug();
381
}
382

383
384
385
void
WorkerThread::doWritingXpipFile(QString filename, ProjectSp project_sp)
{
386

Langella Olivier's avatar
Langella Olivier committed
387
  try
388
    {
Langella Olivier's avatar
Langella Olivier committed
389
390
391
392
393
      emit loadingMessage(tr("writing XPIP file, please wait"));
      project_sp.get()->checkPsimodCompliance();
      Xpip xpip_file(filename);
      xpip_file.write(project_sp);
      xpip_file.close();
394

Langella Olivier's avatar
Langella Olivier committed
395
      emit operationFinished();
396
    }
Langella Olivier's avatar
Langella Olivier committed
397
  catch(pappso::PappsoException &error)
398
    {
Langella Olivier's avatar
Langella Olivier committed
399
400
      emit operationFailed(
        tr("Error while writing XPIP file :\n%1").arg(error.qwhat()));
401
402
    }
}
403
void
404
405
WorkerThread::doWritingOdsFile(QString filename,
                               QString format,
406
                               ProjectSp project_sp)
407
408
{

Langella Olivier's avatar
Langella Olivier committed
409
  try
410
    {
411

412
      qDebug();
Langella Olivier's avatar
Langella Olivier committed
413
414
      CalcWriterInterface *p_writer = nullptr;
      if(format == "tsv")
415
        {
Langella Olivier's avatar
Langella Olivier committed
416
417
          emit loadingMessage(tr("writing TSV files, please wait"));
          p_writer = new TsvDirectoryWriter(filename);
418
        }
Langella Olivier's avatar
Langella Olivier committed
419
      else
420
        {
Langella Olivier's avatar
Langella Olivier committed
421
422
          emit loadingMessage(tr("writing ODS file, please wait"));
          p_writer = new OdsDocWriter(filename);
423
424
        }

Langella Olivier's avatar
Langella Olivier committed
425
426
      OdsExport export_ods(project_sp);
      try
427
        {
Langella Olivier's avatar
Langella Olivier committed
428
          export_ods.write(p_writer, _p_work_monitor);
429
        }
Langella Olivier's avatar
Langella Olivier committed
430
      catch(pappso::PappsoException &error)
431
        {
Langella Olivier's avatar
Langella Olivier committed
432
433
434
          p_writer->close();
          delete p_writer;
          throw error;
435
        }
Langella Olivier's avatar
Langella Olivier committed
436
437
438
      p_writer->close();
      delete p_writer;
      emit operationFinished();
439
    }
Langella Olivier's avatar
Langella Olivier committed
440
  catch(pappso::PappsoException &error)
441
    {
442

Langella Olivier's avatar
Langella Olivier committed
443
444
      emit operationFailed(
        tr("Error while writing ODS file :\n%1").arg(error.qwhat()));
445
446
447
448
    }
}


449
void
450
451
WorkerThread::doWritingMassChroqFile(QString filename,
                                     ProjectSp project_sp,
452
                                     MasschroqFileParameters params)
453
454
{

Langella Olivier's avatar
Langella Olivier committed
455
  try
456
    {
Langella Olivier's avatar
Langella Olivier committed
457
      emit loadingMessage(tr("writing MassChroqML file, please wait"));
458

Langella Olivier's avatar
Langella Olivier committed
459
460
461
462
463
      project_sp.get()->checkPsimodCompliance();
      MassChroQml output(filename, params);
      output.write(project_sp);
      output.close();
      emit operationFinished();
Renne Thomas's avatar
Renne Thomas committed
464
      emit operateWritingMassChroqFileFinished(filename);
465
    }
Langella Olivier's avatar
Langella Olivier committed
466
  catch(pappso::PappsoException &error)
467
    {
Langella Olivier's avatar
Langella Olivier committed
468
469
      emit operationFailed(
        tr("Error while writing MassChroqML file :\n%1").arg(error.qwhat()));
470
471
    }
}
472

473
474
475
476
void
WorkerThread::doWritingMassChroqPrmFile(QString filename, ProjectSp project_sp)
{

Langella Olivier's avatar
Langella Olivier committed
477
  try
478
    {
Langella Olivier's avatar
Langella Olivier committed
479
      emit loadingMessage(tr("writing MassChroqPRM file, please wait"));
Langella Olivier's avatar
Langella Olivier committed
480

Langella Olivier's avatar
Langella Olivier committed
481
482
483
484
485
      project_sp.get()->checkPsimodCompliance();
      MassChroqPrm output(filename);
      output.write(project_sp);
      output.close();
      emit operationFinished();
Langella Olivier's avatar
Langella Olivier committed
486
    }
Langella Olivier's avatar
Langella Olivier committed
487
  catch(pappso::PappsoException &error)
488
    {
Langella Olivier's avatar
Langella Olivier committed
489
490
      emit operationFailed(
        tr("Error while writing MassChroqPRM file :\n%1").arg(error.qwhat()));
Langella Olivier's avatar
Langella Olivier committed
491
492
493
    }
}

494
495
496
497
void
WorkerThread::doWritingProticFile(QString filename, ProjectSp project_sp)
{

Langella Olivier's avatar
Langella Olivier committed
498
  try
499
    {
Langella Olivier's avatar
Langella Olivier committed
500
      emit loadingMessage(tr("writing PROTICdbML file, please wait"));
Olivier Langella's avatar
Olivier Langella committed
501

Langella Olivier's avatar
Langella Olivier committed
502
503
504
505
506
      project_sp.get()->checkPsimodCompliance();
      ProticdbMl output(filename);
      output.write(project_sp);
      output.close();
      emit operationFinished();
Olivier Langella's avatar
Olivier Langella committed
507
    }
Langella Olivier's avatar
Langella Olivier committed
508
  catch(pappso::PappsoException &error)
509
    {
Langella Olivier's avatar
Langella Olivier committed
510
511
      emit operationFailed(
        tr("Error while writing PROTICdbML file :\n%1").arg(error.qwhat()));
Olivier Langella's avatar
Olivier Langella committed
512
513
514
    }
}

515

516
517
void
WorkerThread::doWritingMcqrSpectralCountFile(QString filename,
Langella Olivier's avatar
Langella Olivier committed
518
                                             ProjectSp project_sp)
519
{
Langella Olivier's avatar
Langella Olivier committed
520
  try
521
    {
Langella Olivier's avatar
Langella Olivier committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
      emit loadingMessage(
        tr("Writing %1 spectral count file for MassChroqR").arg(filename));
      QFile outFile;
      outFile.setFileName(filename);
      outFile.open(QIODevice::WriteOnly);
      QTextStream *p_outputStream = new QTextStream(&outFile);
      TsvOutputStream *p_writer   = new TsvOutputStream(*p_outputStream);
      p_writer->setNoSheetName(true);
      McqrSpectralCount spectra_sheet(p_writer, project_sp.get());
      spectra_sheet.writeSheet();

      p_writer->close();
      delete p_writer;
      delete p_outputStream;
      outFile.close();
      emit operationFinished();
538
    }
Langella Olivier's avatar
Langella Olivier committed
539
  catch(OdsException &error)
540
    {
Langella Olivier's avatar
Langella Olivier committed
541
542
543
      emit operationFailed(
        tr("Error while writing spectral count file for MassChroqR :\n%1")
          .arg(error.qwhat()));
544
    }
Langella Olivier's avatar
Langella Olivier committed
545
  catch(pappso::PappsoException &errorb)
546
    {
Langella Olivier's avatar
Langella Olivier committed
547
548
549
      emit operationFailed(
        tr("Error while writing spectral count file for MassChroqR :\n%1")
          .arg(errorb.qwhat()));
550
    }
551
552
553
}


554
555
556
557
558
void
WorkerThread::doLoadingResults(bool is_individual,
                               AutomaticFilterParameters param,
                               QStringList file_list)
{
559

560
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
561
  try
562
    {
Langella Olivier's avatar
Langella Olivier committed
563
      if(file_list.size() == 0)
564
        {
Langella Olivier's avatar
Langella Olivier committed
565
          throw pappso::PappsoException(QObject::tr("file list is empty"));
566
        }
Langella Olivier's avatar
Langella Olivier committed
567
568
      ProjectSp project_sp = Project().makeProjectSp();
      if(is_individual)
569
        {
Langella Olivier's avatar
Langella Olivier committed
570
          project_sp.get()->setProjectMode(ProjectMode::individual);
571
        }
Langella Olivier's avatar
Langella Olivier committed
572
      else
573
        {
Langella Olivier's avatar
Langella Olivier committed
574
          project_sp.get()->setProjectMode(ProjectMode::combined);
575
        }
576

Langella Olivier's avatar
Langella Olivier committed
577
578
579
      _p_work_monitor->setProgressMaximumValue(file_list.size());
      int i = 0;
      for(QString filename : file_list)
580
        {
Langella Olivier's avatar
Langella Olivier committed
581
          _p_work_monitor->message(tr("loading result file %1 (%2 on %3)")
582
583
584
                                     .arg(filename)
                                     .arg(i)
                                     .arg(file_list.size()),
Langella Olivier's avatar
Langella Olivier committed
585
586
587
                                   i);
          project_sp.get()->readResultFile(filename);
          i++;
588
589
        }

Langella Olivier's avatar
Langella Olivier committed
590
591
      emit loadingMessage(tr("filtering proteins"));
      project_sp.get()->updateAutomaticFilters(param);
592
      qDebug();
Langella Olivier's avatar
Langella Olivier committed
593
      emit loadingResultsFinished(project_sp);
594
    }
Langella Olivier's avatar
Langella Olivier committed
595
  catch(pappso::PappsoException &error)
596
    {
597
      qDebug() << " " << error.qwhat();
Langella Olivier's avatar
Langella Olivier committed
598
599
      emit projectNotReady(
        tr("Error while reading result files :\n%1").arg(error.qwhat()));
600
    }
601
  qDebug();
602
}
603
604
605
void
WorkerThread::doRunningXtandem(TandemRunBatch tandem_run_batch)
{
606
  qDebug();
Langella Olivier's avatar
Langella Olivier committed
607
  try
608
    {
Langella Olivier's avatar
Langella Olivier committed
609
610
611
      emit loadingMessage(tr("Running X!Tandem, please wait"));
      qDebug() << "WorkerThread::doRunningXtandem tandem_run_batch "
               << tandem_run_batch._tandem_bin_path;
612

Langella Olivier's avatar
Langella Olivier committed
613
614
615
      QSettings settings;
      bool use_htcondor =
        settings.value("tandem/use_HTCondor", "false").toBool();
616

Langella Olivier's avatar
Langella Olivier committed
617
      if(use_htcondor)
618
        {
Langella Olivier's avatar
Langella Olivier committed
619
620
621
          TandemCondorProcess xt_process(
            _p_main_window, _p_work_monitor, tandem_run_batch);
          xt_process.run();
Olivier Langella's avatar
Olivier Langella committed
622
        }
Langella Olivier's avatar
Langella Olivier committed
623
      else
624
        {
Langella Olivier's avatar
Langella Olivier committed
625
626
627
          TandemBatchProcess xt_process(
            _p_main_window, _p_work_monitor, tandem_run_batch);
          xt_process.run();
628
        }
Langella Olivier's avatar
Langella Olivier committed
629

Langella Olivier's avatar
Langella Olivier committed
630
      emit operationFinished();
631
    }
Langella Olivier's avatar
Langella Olivier committed
632
  catch(pappso::PappsoException &error)
633
    {
634
      qDebug() << error.qwhat();
Langella Olivier's avatar
Langella Olivier committed
635
636
      emit operationFailed(
        tr("Error while running X!Tandem job :\n%1").arg(error.qwhat()));
637
    }
638
  qDebug();
639
}
640

Renne Thomas's avatar
Renne Thomas committed
641
642
643
644
645
646
647
648
void
WorkerThread::doRunningMassChroQ(MassChroQRunBatch masschroq_batch_param)
{
  try
    {
      emit loadingMessage(tr("Running MassChroQ, please wait"));
      qDebug();

649
650
651
      QSettings settings;
      bool use_htcondor =
        settings.value("masschroq/use_htcondor", "false").toBool();
Renne Thomas's avatar
Renne Thomas committed
652
653
654

      if(use_htcondor)
        {
655
656
657
658

          MassChroQCondorProcess mcq_process(
            _p_main_window, _p_work_monitor, masschroq_batch_param);
          mcq_process.run();
Renne Thomas's avatar
Renne Thomas committed
659
660
661
        }
      else
        {
662
663
          MassChroQBatchProcess mcq_process(
            _p_main_window, _p_work_monitor, masschroq_batch_param);
Renne Thomas's avatar
Renne Thomas committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
          mcq_process.run();
        }

      emit operationFinished();
    }
  catch(pappso::PappsoException &error)
    {
      qDebug() << error.qwhat();
      emit operationFailed(
        tr("Error while running MassChroQ job :\n%1").arg(error.qwhat()));
    }
  qDebug();
}

678
679

void
680
681
WorkerThread::doWritingFastaFile(QString filename,
                                 ProjectSp project_sp,
682
683
684
                                 ExportFastaType type)
{

Langella Olivier's avatar
Langella Olivier committed
685
  try
686
687
    {

688
      qDebug();
Langella Olivier's avatar
Langella Olivier committed
689
      emit loadingMessage(tr("writing FASTA file, please wait"));
690

691

Langella Olivier's avatar
Langella Olivier committed
692
693
694
      ExportFastaFile output(filename, type);
      output.write(project_sp);
      output.close();
695

Langella Olivier's avatar
Langella Olivier committed
696
697
      // emit operateXpipFile(filename);
      emit operationFinished();
698
    }
Langella Olivier's avatar
Langella Olivier committed
699
  catch(pappso::PappsoException &error)
700
    {
Langella Olivier's avatar
Langella Olivier committed
701
702
      emit operationFailed(
        tr("Error while writing FASTA file :\n%1").arg(error.qwhat()));
703
704
    }
}
705
706
707


void
708
709
WorkerThread::doFindBestMsrunForAlignment(ProjectSp project_sp,
                                          MsRunAlignmentGroupSp alignment_group)
710
{
Langella Olivier's avatar
Langella Olivier committed
711
  try
712
    {
Renne Thomas's avatar
Renne Thomas committed
713
      qDebug();
Langella Olivier's avatar
Langella Olivier committed
714
715
      emit loadingMessage(
        tr("looking for the best MSrun reference for alignment, please wait"));
716

Langella Olivier's avatar
Langella Olivier committed
717
718
      _p_work_monitor->message("checking MS run file path");
      _p_work_monitor->setProgressMaximumValue(
719
        alignment_group->getMsRunsInAlignmentGroup().size());
Renne Thomas's avatar
Renne Thomas committed
720

721
      checkMsrunFilePath(alignment_group->getMsRunsInAlignmentGroup());
Renne Thomas's avatar
Renne Thomas committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744

      _p_work_monitor->message("preparing MS run retention times");
      try
        {
          if(!_p_main_window->stopWorkerThread())
            {
              project_sp->prepareMsrunRetentionTimesForAlignment();
            }
        }
      catch(pappso::PappsoException &error)
        {
          throw pappso::PappsoException(
            tr("Error while preparing MS run retention times :\n%1")
              .arg(error.qwhat()));
        }

      try
        {
          MsRunSp ms_run_best_reference;

          if(!_p_main_window->stopWorkerThread())
            {
              ms_run_best_reference =
745
                chooseBestMsRun(alignment_group->getMsRunsInAlignmentGroup());
Renne Thomas's avatar
Renne Thomas committed
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777

              if(ms_run_best_reference != nullptr)
                {
                  qDebug() << " " << ms_run_best_reference.get()->getXmlId();
                }
            }
          _p_work_monitor->message("done");
          // emit operateXpipFile(filename);

          if(_p_main_window->stopWorkerThread())
            {
              _p_work_monitor->canceled("Finding the best MsRun canceled!");
            }
          else
            {
              emit findingBestMsrunForAlignmentFinished(ms_run_best_reference);
            }
        }
      catch(pappso::PappsoException &error)
        {
          throw pappso::PappsoException(
            tr("Error while choosing best MS run :\n%1").arg(error.qwhat()));
        }
    }
  catch(pappso::PappsoException &error)
    {
      emit operationFailed(
        tr("Error looking for the best MSrun reference for alignment :\n%1")
          .arg(error.qwhat()));
    }
}

778
void
779
WorkerThread::checkMsrunFilePath(std::list<MsRunSp> msruns)
Renne Thomas's avatar
Renne Thomas committed
780
781
782
783
784
785
786
{
  std::size_t i      = 0;
  bool should_i_stop = false;
  for(auto &msrun_sp : msruns)
    {
      should_i_stop = _p_main_window->stopWorkerThread();
      if(!should_i_stop)
787
        {
Langella Olivier's avatar
Langella Olivier committed
788
          _p_work_monitor->message(tr("checking MS run file path for sample %1")
789
                                     .arg(msrun_sp->getSampleName()),
Langella Olivier's avatar
Langella Olivier committed
790
                                   i);
791
792
793

          qDebug();
          pappso::MsRunReaderSPtr msrunreader_sp = msrun_sp->findMsRunFile();
794
          msrun_sp->freeMsRunReaderSp();
795
796
797

          qDebug();
          if(msrunreader_sp != nullptr)
798
799
            {
            }
Langella Olivier's avatar
Langella Olivier committed
800
          else
801
            {
Langella Olivier's avatar
Langella Olivier committed
802
              throw pappso::PappsoException(
Renne Thomas's avatar
Renne Thomas committed
803
804
                tr("Error while checking MS run file path :\nMSrun \"%1\" "
                   "for "
Langella Olivier's avatar
Langella Olivier committed
805
806
807
                   "sample named \"%2\" not found")
                  .arg(msrun_sp->getXmlId())
                  .arg(msrun_sp->getSampleName()));
808
            }
Langella Olivier's avatar
Langella Olivier committed
809
          i++;
810
        }
Renne Thomas's avatar
Renne Thomas committed
811
812
    }
}
813

Renne Thomas's avatar
Renne Thomas committed
814
MsRunSp
815
WorkerThread::chooseBestMsRun(std::list<MsRunSp> msruns)
Renne Thomas's avatar
Renne Thomas committed
816
817
818
819
820
821
822
823
824
825
826
{
  _p_work_monitor->message("choosing MS run");

  MsRunSp ms_run_best_reference;
  std::size_t best_number = 0;
  bool should_i_stop      = false;

  for(auto &msrun_ref_sp : msruns)
    {
      should_i_stop = _p_main_window->stopWorkerThread();
      if(!should_i_stop)
827
        {
Renne Thomas's avatar
Renne Thomas committed
828
          if(ms_run_best_reference == nullptr)
829
            {
Renne Thomas's avatar
Renne Thomas committed
830
831
              ms_run_best_reference = msrun_ref_sp;
            }
832

Renne Thomas's avatar
Renne Thomas committed
833
834
835
836
837
          std::size_t min_number = std::numeric_limits<std::size_t>::max();
          for(auto &msrun_sp : msruns)
            {
              should_i_stop = _p_main_window->stopWorkerThread();
              if(!should_i_stop)
838
                {
Langella Olivier's avatar
Langella Olivier committed
839
                  if(msrun_sp.get() != msrun_ref_sp.get())
840
                    {
Langella Olivier's avatar
Langella Olivier committed
841
842
843
844
845
                      pappso::Trace trace;
                      trace =
                        msrun_sp.get()
                          ->getMsRunRetentionTimePtr()
                          ->getCommonDeltaRt(msrun_ref_sp.get()
846
847
                                               ->getMsRunRetentionTimePtr()
                                               ->getSeamarks());
Langella Olivier's avatar
Langella Olivier committed
848
                      if(trace.size() < min_number)
849
                        {
Langella Olivier's avatar
Langella Olivier committed
850
                          min_number = trace.size();
851
                        }
852
853
854
                    }
                }
            }
Renne Thomas's avatar
Renne Thomas committed
855
856

          if(min_number > best_number)
857
            {
Renne Thomas's avatar
Renne Thomas committed
858
              best_number = min_number;
859

Renne Thomas's avatar
Renne Thomas committed
860
861
              ms_run_best_reference = msrun_ref_sp;
            }
862
863
        }
    }
Renne Thomas's avatar
Renne Thomas committed
864
  return ms_run_best_reference;
865
}
866
867
868
869
870
871


void
WorkerThread::doCheckMsrunFilePath(ProjectSp msp_project)
{

Langella Olivier's avatar
Langella Olivier committed
872
873
  MsRunSp msrun_sp = nullptr;
  try
874
875
    {

Langella Olivier's avatar
Langella Olivier committed
876
877
878
879
      _p_work_monitor->setProgressMaximumValue(
        msp_project->getIdentificationDataSourceStore()
          .getIdentificationDataSourceList()
          .size());
880

Langella Olivier's avatar
Langella Olivier committed
881
882
883
884
      std::size_t i = 0;
      for(IdentificationDataSourceSp p_ident_data_source :
          msp_project->getIdentificationDataSourceStore()
            .getIdentificationDataSourceList())
885
        {
Langella Olivier's avatar
Langella Olivier committed
886
887
888
889
890
891
892
          msrun_sp = p_ident_data_source->getMsRunSp();
          _p_work_monitor->message(
            tr("checking MS run file path for \"%1\", named \"%2\"")
              .arg(msrun_sp.get()->getXmlId())
              .arg(msrun_sp.get()->getSampleName()),
            i);

893
894
895
896
897
          qDebug();
          pappso::MsRunReaderSPtr msrunreader_sp = msrun_sp->findMsRunFile();

          qDebug();
          if(msrunreader_sp != nullptr)
898
            {
Langella Olivier's avatar
Langella Olivier committed
899
              // msrun_sp->checkMsRunStatistics();
900
              msrun_sp->freeMsRunReaderSp();
Langella Olivier's avatar
Langella Olivier committed
901
              msrun_sp = nullptr;
902
              qDebug() << "file found";
903
            }
Langella Olivier's avatar
Langella Olivier committed
904
          else
905
            {
Langella Olivier's avatar
Langella Olivier committed
906
              // is_ok = false;
907
              qDebug() << "file not found";
Langella Olivier's avatar
Langella Olivier committed
908
              break;
909
            }
910
          // msrun_sp->freeMsRunReaderSp();
Langella Olivier's avatar
Langella Olivier committed
911
          i++;
912
913
914
        }
    }

Langella Olivier's avatar
Langella Olivier committed
915
  catch(pappso::PappsoException &error)
916
    {
Langella Olivier's avatar
Langella Olivier committed
917
918
      emit operationFailed(
        tr("Error checking MSrun files path :\n%1").arg(error.qwhat()));
919
920
    }

Langella Olivier's avatar
Langella Olivier committed
921
  emit checkingMsrunFilePathFinished(msrun_sp);
922
}
Renne Thomas's avatar
Renne Thomas committed
923
924
925

void
WorkerThread::doWriteXicAreaInCsv(QString filename,
926
                                  QString project_name,
Renne Thomas's avatar
Renne Thomas committed
927
928
                                  std::vector<XicBox *> xic_boxs)
{
Renne Thomas's avatar
Renne Thomas committed
929
930
  CalcWriterInterface *p_writer = nullptr;
  emit loadingMessage(tr("writing TSV files, please wait"));
Renne Thomas's avatar
Renne Thomas committed
931
932
933
  QSettings settings;
  QString xic_export_file =
    settings.value("export/xic_files", "area").toString();
Renne Thomas's avatar
Renne Thomas committed
934
  p_writer = new TsvDirectoryWriter(filename);
Renne Thomas's avatar
Renne Thomas committed
935
936
  if(xic_export_file == "both")
    {
937
      XicAreaSheet(p_writer, project_name, xic_boxs);
Renne Thomas's avatar
Renne Thomas committed
938
      bool all_data = true;
939
      XicAreaSheet(p_writer, project_name, xic_boxs, all_data);
Renne Thomas's avatar
Renne Thomas committed
940
941
942
943
    }
  else if(xic_export_file == "all")
    {
      bool all_data = true;
944
      XicAreaSheet(p_writer, project_name, xic_boxs, all_data);
Renne Thomas's avatar
Renne Thomas committed
945
946
947
    }
  else // xic_export_file == "area"
    {
948
      XicAreaSheet(p_writer, project_name, xic_boxs);
Renne Thomas's avatar
Renne Thomas committed
949
    }
Renne Thomas's avatar
Renne Thomas committed
950
951
  p_writer->close();
  emit exportXicToCsvFinished();
Renne Thomas's avatar
Renne Thomas committed
952
}