File: sequencePurificationDlg.cpp

package info (click to toggle)
massxpert 2.3.6-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 20,736 kB
  • ctags: 3,541
  • sloc: cpp: 44,108; xml: 7,381; sh: 604; makefile: 108; ansic: 7
file content (565 lines) | stat: -rw-r--r-- 14,459 bytes parent folder | download | duplicates (4)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* massXpert - the true massist's program.
   --------------------------------------
   Copyright(C) 2006,2007 Filippo Rusconi

   http://www.massxpert.org/massXpert

   This file is part of the massXpert project.

   The massxpert project is the successor to the "GNU polyxmass"
   project that is an official GNU project package(see
   www.gnu.org). The massXpert project is not endorsed by the GNU
   project, although it is released ---in its entirety--- under the
   GNU General Public License. A huge part of the code in massXpert
   is actually a C++ rewrite of code in GNU polyxmass. As such
   massXpert was started at the Centre National de la Recherche
   Scientifique(FRANCE), that granted me the formal authorization to
   publish it under this Free Software License.

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU  General Public
   License version 3, as published by the Free Software Foundation.
   

   This software 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 this software; if not, write to the

   Free Software Foundation, Inc.,

   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/


/////////////////////// Qt includes
#include <QMessageBox>
#include <QTextDocumentFragment>
#include <QProgressDialog>


/////////////////////// Local includes
#include "sequencePurificationDlg.hpp"


namespace massXpert
{

  enum 
    {
      NONE = 0 << 1,
      LEFT = 1 << 1,
      RIGHT = 2 << 1,
    };


  SequencePurificationDlg::SequencePurificationDlg(QWidget *parent,
						    Sequence *
						    sequence,
						    QList<int> *
						    errorList)
    : QDialog(parent)
  {
    Q_ASSERT(parent);
    Q_ASSERT(sequence);
    Q_ASSERT(errorList);
  
    mp_editorWnd = static_cast<SequenceEditorWnd *>(parent);
    
    mpa_origSequence = new Sequence(*sequence->monomerText());
    m_errorList = *errorList;
    m_firstRound = true;
  
    m_ui.setupUi(this);

    // Make the connections between QTextEdits and QTextDocuments.
    setupDocuments();
  
    // Seed the system by putting the original sequence text into the
    // left QTextEdit.
    setOrigText();
  
    connect(m_ui.purifyPushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(purify()));

    connect(m_ui.removeTaggedPushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(removeTagged()));
  
    connect(m_ui.testPushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(test()));

    connect(m_ui.resetPushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(reset()));
  }


  SequencePurificationDlg::~SequencePurificationDlg()
  {
    delete mpa_origSequence;
  }


  void 
  SequencePurificationDlg::setupDocuments()
  {
    m_leftTextDocument.setParent(this);
    m_rightTextDocument.setParent(this);
  
    m_ui.leftSequenceTextEdit->setDocument(&m_leftTextDocument);
    m_ui.rightSequenceTextEdit->setDocument(&m_rightTextDocument);
  }


  void
  SequencePurificationDlg::setOrigText()
  {
    const QString *text = mpa_origSequence->monomerText();
    
    bool res = text->isEmpty();

    if(res)
      qFatal("Fatal error at %s@%d. Program aborted.",
	      __FILE__, __LINE__);
    
    m_leftTextDocument.setPlainText(*text);
    
    // And now make the highlighting of the invalid characters, using
    // the list of indices of invalid chars.
    setCursor(Qt::WaitCursor);

    doTest(m_leftTextDocument);
  
    setCursor(Qt::ArrowCursor);

    m_firstRound = true;
  }


  void 
  SequencePurificationDlg::doTest(QTextDocument &document)
  {
    if (document.isEmpty())
      return;
  
    QTextCursor cursor(&document);

    QTextCharFormat normalFormat;
    normalFormat.setFontUnderline(false);
  
    QTextCharFormat underlinedFormat;
    underlinedFormat.setFontUnderline(true);

    // List of int indices where errors were found.
    QList<int> errorList;

    // Create a sequence with the document text.
    Sequence sequence(document.toPlainText());
 
    if (sequence.makeMonomerList(mp_editorWnd->polymer()->polChemDef(),
				  true, &errorList) != -1)
      {
	// No errors were found, we can set all the text in normal font.
	cursor.select(QTextCursor::Document);
      
	cursor.setCharFormat(normalFormat);
      
	return;
      }

    // Errors were found, format all single erroneous positions.

    QProgressDialog progress(tr("Task already performed..."),
			      tr("Abort"),
			      0, errorList.size(), this);
  
    progress.setWindowModality(Qt::WindowModal);
    progress.setWindowTitle(tr("Formatting progression"));
  
    int listSize = errorList.size();
  
    for (int iter = 0 ; iter < listSize; ++iter)
      {
	progress.setValue(iter);
	qApp->processEvents();
	if(progress.wasCanceled())
	  break;

	// There should be room for optimization by looking for
	// continuous stretches of invalid characters.  For example,
	// imagine we have the following indices in the errorList:

	// 1 5 6 7 8 9 12 16 17 18 19 20

	// We may joing [5-9] in one move position. Same applies to
	// [16-20].
      
	int endIndex = 0;
      
	// This optimization brought an increase in performance of a
	// factor 10 !!! The other solution was to only perform task
	// in the other else block.
      
	if(findContinuum(errorList, iter, endIndex))
	  {
	    int span = errorList.at(endIndex) - errorList.at(iter) + 1;
	  
	    cursor.setPosition(errorList.at(iter), 
				QTextCursor::MoveAnchor);

	    cursor.movePosition(QTextCursor::NextCharacter, 
				 QTextCursor::KeepAnchor,
				 span);

	    cursor.setCharFormat(underlinedFormat);

	    iter = endIndex;
	  
	    continue;
	  }
	else
	  {
	    cursor.setPosition(errorList.at(iter), 
				QTextCursor::MoveAnchor);
	  
	    cursor.movePosition(QTextCursor::NextCharacter, 
				 QTextCursor::KeepAnchor);
	  
	    cursor.setCharFormat(underlinedFormat);
	  }
      }
    
    progress.setValue(errorList.size());
  }


  void
  SequencePurificationDlg::purify()
  {
    if (m_firstRound)
      {
	// This is the first round of purification. Which means, the
	// original text is in the left QTextEdit(see
	// setOrigText()). We copy that text in m_rightTextDocument and
	// purify that last copy and put the resulting text into the
	// right QTextEdit.

	// Select all the text in the left text document.
	QTextCursor leftCursor(&m_leftTextDocument);
	leftCursor.select(QTextCursor::Document);

	// Make a fragment out of that selected text.
	QTextDocumentFragment fragment(leftCursor.selection());

	// Copy that fragment into the right text document, after having
	// removed all its initial contents.

	// Select all the text in the right text document. And remove all the
	// selection, so that the documents ends empty.
	QTextCursor rightCursor(&m_rightTextDocument);
	rightCursor.select(QTextCursor::Document);
	rightCursor.removeSelectedText();
      
	// And now copy the fragment from the left text document into the
	// right text document.
	rightCursor.insertFragment(fragment);
      }
    else
      {
	// This is not the first round of purification. That means that we
	// first have to copy the right text in the left text. Then, we'll
	// modify the right text.
      
	// Select all the text in the right text document.
	QTextCursor rightCursor(&m_rightTextDocument);
	rightCursor.select(QTextCursor::Document);
      
	// Make a fragment out of that selected text.
	QTextDocumentFragment fragment(rightCursor.selection());
      
	// Copy that fragment into the left text document, after having
	// removed all its initial contents.
      
	// Select all the text in the left text document. And remove all the
	// selection, so that the documents ends empty.
	QTextCursor leftCursor(&m_leftTextDocument);
	leftCursor.select(QTextCursor::Document);
	leftCursor.removeSelectedText();
      
	// And now copy the fragment from the right text document into the
	// left text document.
	leftCursor.insertFragment(fragment);
      }
  
    if (m_rightTextDocument.isEmpty())
      return;

    // And now we can perform the purification in the right text
    // document.

    bool numerals = m_ui.numeralsCheckBox->checkState();
    bool punctuation = m_ui.punctuationCheckBox->checkState();
    bool spaces = m_ui.spacesCheckBox->checkState();
    bool lowercase = m_ui.lowercaseCheckBox->checkState();
    bool uppercase = m_ui.uppercaseCheckBox->checkState();
    QString others = m_ui.othersLineEdit->text();
  
    QTextCursor cursor(&m_rightTextDocument);
  
    if (numerals)
      {
	QRegExp regExp("\\d+", Qt::CaseInsensitive, QRegExp::RegExp2);
      
	cursor = m_rightTextDocument.find(regExp, cursor);
      
	while(!cursor.isNull())
	  {
	    cursor.deleteChar();
	    cursor = m_rightTextDocument.find(regExp, cursor);
	  }
      }
  
    cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
  
    if (spaces)
      {
	QRegExp regExp("\\s+", Qt::CaseInsensitive, QRegExp::RegExp2);

	cursor = m_rightTextDocument.find(regExp, cursor);
      
	while(!cursor.isNull())
	  {
	    cursor.deleteChar();
	    cursor = m_rightTextDocument.find(regExp, cursor);
	  }
      }
  
    cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
  
    if (punctuation)
      {
	QRegExp regExp("@|~|&|\\(|\\)|\\{|\\}|\\[|\\]|=|<|>|#|="
			"|\\\\|/|%|\\+|_|-|;|:|\\.|'|!|\\?|\\\"|\\|",
			Qt::CaseInsensitive, QRegExp::RegExp2);
      
	cursor = m_rightTextDocument.find(regExp, cursor);
      
	while(!cursor.isNull())
	  {
	    cursor.deleteChar();
	    cursor = m_rightTextDocument.find(regExp, cursor);
	  }
      }
  
    cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
  
    if (lowercase)
      {
	QRegExp regExp("[a-z]",
			Qt::CaseSensitive, QRegExp::RegExp2);
      
	cursor = m_rightTextDocument.find(regExp, cursor);
      
	while(!cursor.isNull())
	  {
	    cursor.deleteChar();
	    cursor = m_rightTextDocument.find(regExp, cursor);
	  }
      }

    cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
  
    if (uppercase)
      {
	QRegExp regExp("[A-Z]",
			Qt::CaseSensitive, QRegExp::RegExp2);
      
	cursor = m_rightTextDocument.find(regExp, cursor);
      
	while(!cursor.isNull())
	  {
	    cursor.deleteChar();
	    cursor = m_rightTextDocument.find(regExp, cursor);
	  }
      }

    cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
  
    if (!others.isEmpty())
      {
	QRegExp regExp(others, Qt::CaseSensitive, QRegExp::RegExp2);
      
	if(regExp.isValid())
	  {
	    cursor = m_rightTextDocument.find(regExp, cursor);
	  
	    while(!cursor.isNull())
	      {
		cursor.deleteChar();
		cursor = m_rightTextDocument.find(regExp, cursor);
	      }
	  }
	else
	  qDebug() << "RegExp:" << others << "is not valid";
      }
  
    m_firstRound = false;
  
    return;
  }


  void
  SequencePurificationDlg::removeTagged()
  {
    // The original text is in the left QTextEdit(see
    // setOrigText()). We copy that text in m_rightTextDocument and
    // purify that last copy and put the resulting text into the right
    // QTextEdit.
  
    // Select all the text in the left text document.
    QTextCursor leftCursor(&m_leftTextDocument);
    leftCursor.select(QTextCursor::Document);
  
    // Make a fragment out of that selected text.
    QTextDocumentFragment fragment(leftCursor.selection());
  
    // Copy that fragment into the right text document, after having
    // removed all its initial contents.
  
    // Select all the text in the right text document. And remove all the
    // selection, so that the documents ends empty.
    QTextCursor rightCursor(&m_rightTextDocument);
    rightCursor.select(QTextCursor::Document);
    rightCursor.removeSelectedText();
  
    // And now copy the fragment from the left text document into the
    // right text document.
    rightCursor.insertFragment(fragment);
  
    if (m_rightTextDocument.isEmpty())
      return;

    // And now we can perform the purification in the right text
    // document. Note that this work is extremely resource-intensive. We
    // better give progress feedback to the user... and allow her to
    // abort the process!
  
    QTextCursor cursor(&m_rightTextDocument);

    bool ret = cursor.movePosition(QTextCursor::EndOfBlock, 
				    QTextCursor::MoveAnchor);
  
    int position = cursor.position();
  
    QProgressDialog progress(tr("Task remaining to perform..."),
			      tr("Abort"),
			      0, position, this);
  
    progress.setWindowModality(Qt::WindowModal);
    progress.setWindowTitle(tr("Formatting progression"));

    while(ret)
      {
	progress.setValue(position);
	qApp->processEvents();
	if(progress.wasCanceled())
	  break;

	// Check char format of char right before the cursor.
      
	QTextCharFormat format = cursor.charFormat();
      
	if(format.fontUnderline())
	  {
	    cursor.deletePreviousChar();
	  
	    continue;
	  }
      
	ret = cursor.movePosition(QTextCursor::Left,
				   QTextCursor::MoveAnchor);
      
	position = cursor.position();
      
	if(!position)
	  break;
      }

    progress.setValue(position);
  }


  void
  SequencePurificationDlg::test()
  {
    doTest(m_rightTextDocument);
  }


  void
  SequencePurificationDlg::reset()
  {
    setOrigText();
    m_ui.rightSequenceTextEdit->clear();
  }


  bool
  SequencePurificationDlg::findContinuum(QList<int> &errorList,
					  int firstIndex, 
					  int &secondIndex)
  {
    int listSize = errorList.size();
  
    Q_ASSERT(firstIndex >= 0 && firstIndex < listSize);
  
    if (firstIndex == listSize - 2)
      return false;
  
    int currentValue = 0;
    int previousValue = errorList.at(firstIndex);
  
    int startIndex = firstIndex + 1;
    int iter = 0;

    for (iter = startIndex; iter < listSize; ++iter)
      {
	currentValue = errorList.at(iter);
      
	if(currentValue == previousValue + 1)
	  {
	    previousValue = currentValue;
	    continue;
	  }
	else
	  {
	    --iter;
	    break;
	  }
      }

    if (iter == listSize)
      // We got to the end of the list. Decrement iter by one fake run.
      --iter;
  
    if (iter - startIndex >= 1)
      {
	secondIndex = iter;
	return true;
      }
  
    return false;
  }

} // namespace massXpert