File: isotopicPatternCalculationDlg.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 (1146 lines) | stat: -rw-r--r-- 30,653 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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
/* massXpert - the true massist's program.
   --------------------------------------
   Copyright(C) 2006,2007 Filippo Rusconi

   http://www.filomace.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.

  
   
   NOTE : This work is based on a previous work by Dirk Nolting,
   licensed under the GNU General Public License:
   
   ***************************************************************************
   *  file                 :  ipc.c                                          *
   *  copyright            :(C) 2001-2005 by Dirk Nolting                   *
   *  email                : nolting@uni-duesseldorf.de                      *
   ***************************************************************************

   ***************************************************************************
   *                                                                         *
   *   This program 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~2 of the License, or     *
   *  (at your option) any later version.                                   *
   *                                                                         *
   ***************************************************************************
   *
   */


/////////////////////// Qt includes
#include <QtGui>


/////////////////////// Std includes
#include <math.h>
#include <algorithm>
#include <limits> // for std::numeric_limits

using namespace std;

/////////////////////// Local includes
#include "isotopicPatternCalculationDlg.hpp"
#include "isotopicPeak.hpp"
#include "application.hpp"


namespace massXpert
{

  IsotopicPatternCalculationDlg::IsotopicPatternCalculationDlg 
 (QWidget *parent, const QList<Atom *> &atomList)
    : QDialog(parent), m_atomList(atomList)
  {
    Q_ASSERT(parent);

    m_aborted = false;
    m_filePath = "";
  
    m_ui.setupUi(this);

    // Set the name of the polymer chemistry definition.

    const PolChemDef & polChemDef = 
      static_cast<CalculatorWnd *>(parent)->polChemDef();
    m_ui.polChemDefLabel->setText(polChemDef.name());
  


    m_ui.minimumProbabilityDoubleSpinBox->setRange(0, 1000000);
    m_ui.minimumProbabilityDoubleSpinBox->setValue(0.0001);
    m_minimumProbability = 0.0001;
  
    m_ui.maximumPeaksSpinBox->setRange(0, 1000000);
    m_ui.maximumPeaksSpinBox->setValue(1000);
    m_maximumPeaks = 1000;
  
    m_ui.chargeSpinBox->setRange(0, 1000000);
    m_ui.chargeSpinBox->setValue(1);
    m_charge = 1;
  
    m_ui.resolutionSpinBox->setRange(0, 1000000);
    m_ui.resolutionSpinBox->setValue(15000);
    m_resolution = 15000;
  
    m_ui.progressBar->setRange(0, 100);
    m_ui.progressBar->setValue(0);

    QSettings settings 
     (static_cast<Application *>(qApp)->configSettingsFilePath(), 
       QSettings::IniFormat);
  
    settings.beginGroup("isotopic_pattern_calculation_dlg");

    restoreGeometry(settings.value("geometry").toByteArray());

    settings.endGroup();

    connect(m_ui.executePushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(execute()));
  
    connect(m_ui.abortPushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(abort()));

    connect(m_ui.outputFilePushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(outputFile()));

  }


  void 
  IsotopicPatternCalculationDlg::closeEvent(QCloseEvent *event)
  {
    if (event)
      printf("%s", "");
  
    QSettings settings 
     (static_cast<Application *>(qApp)->configSettingsFilePath(), 
       QSettings::IniFormat);
  
    settings.beginGroup("isotopic_pattern_calculation_dlg");
  
    settings.setValue("geometry", saveGeometry());
  
    settings.endGroup();
  }


  IsotopicPatternCalculationDlg::~IsotopicPatternCalculationDlg()
  {
  
  
  }


  bool
  IsotopicPatternCalculationDlg::fetchValidateInputData()
  {
    Application *application = static_cast<Application *>(qApp);

    QString formula = m_ui.formulaLineEdit->text();
  
    if (!formula.isEmpty())
      m_formula.setFormula(formula);
    else
      {
	QMessageBox::warning(0,
			      tr("massXpert: Isotopic Pattern Calculation"),
			      tr("Enter a valid formula."),
			      QMessageBox::Ok);
	return false;
      }
  
    // We want to validate the formula and in the mean time construct
    // the list of all the AtomCount objects(first true), and since
    // the formula is reused we also ensure that that list is reset
    //(second true).
  
    if (!m_formula.validate(m_atomList, true, true))
      {
	QMessageBox::warning(0,
			      tr("massXpert: Isotopic Pattern Calculation"),
			      tr("Formula(%1) is not valid.")
			      .arg(formula),
			      QMessageBox::Ok);
	return false;
      }
  
    // Shall we use localization for all the numerical output?
    bool locale = m_ui.localeCheckBox->checkState() == Qt::Checked ?
      true : false;
  

    int totAtoms = m_formula.totalAtoms();
    m_ui.progressBar->setRange(0, totAtoms);
  
    int totIsotopes = m_formula.totalIsotopes(m_atomList);

    m_mono = 0;
    m_avg = 0;
  
    if (!m_formula.accountMasses(m_atomList, &m_mono, &m_avg, 1))
      return false;
  
    QString textEditText;

    if (locale)
      textEditText += tr("INPUT\n=====\n\nformula: %1\n"
			  "Mono Mass: %2 \t Avg mass: %3\n"
			  "Total number of atoms: %4\n"
			  "Total number of isotopes: %5")
	.arg(formula)
	.arg(application->locale().
	      toString(m_mono, 'f', MXP_OLIGOMER_DEC_PLACES))
	.arg(application->locale().
	      toString(m_avg, 'f', MXP_OLIGOMER_DEC_PLACES))
	.arg(totAtoms)
	.arg(totIsotopes);
    else
      textEditText += tr("INPUT\n=====\n\nformula: %1\n"
			  "Mono mass: %2 \t Avg mass: %3\n"
			  "Total number of 1) atoms: %4 ; 2) isotopes: %5\n\n")
	.arg(formula)
	.arg(QString().setNum(m_mono, 'f', MXP_OLIGOMER_DEC_PLACES))
	.arg(QString().setNum(m_avg, 'f', MXP_OLIGOMER_DEC_PLACES))
	.arg(totAtoms)
	.arg(totIsotopes);

  
    // The list of atomCount objects has to be made with fully detailed
    // isotopes...
    if (!m_formula.deepAtomCopy(m_atomList))
      {
	QMessageBox::warning(0,
			      tr("massXpert: Isotopic Pattern Calculation"),
			      tr("Failed to deep-copy atom list."),
			      QMessageBox::Ok);

	textEditText += tr("Failed to deep-copy atom list.\n");
      
	// Out put some text in the textEdit:
	m_ui.resultTextEdit->append(textEditText);
      
	return false;
      }

    m_charge = m_ui.chargeSpinBox->value();
    m_resolution = m_ui.resolutionSpinBox->value();
    m_maximumPeaks = m_ui.maximumPeaksSpinBox->value();
 
    textEditText += tr("Charge: %1 ; Resolution: %2 ; Maximum peaks: %3 ;")
      .arg(m_charge)
      .arg(m_resolution)
      .arg(m_maximumPeaks);
    
    m_minimumProbability = m_ui.minimumProbabilityDoubleSpinBox->value();
    textEditText += tr(" Minimum probability: ");
    if (locale)
      textEditText += application->locale().
	toString(m_minimumProbability, 'f', MXP_OLIGOMER_DEC_PLACES);
    else
      textEditText += QString().
	setNum(m_minimumProbability, 'f', MXP_OLIGOMER_DEC_PLACES);;
  
    textEditText += 
      tr("\n\nGaussian simulation of a single peak is performed by "
	  "computing 25 points on each side of the isotopic mass "
	  "according to the following exponential:\n"
	  "f(x) = relativeIntensity * exp(( -(x - mono)^2 ) / c^2 )\n\n"
	  "Full Width At Half Maximum = "
	  "(mono / resolution) = 2 * sqrt(ln(2) * c \n");
  
    double fullWidthHalfMax = m_mono / m_resolution;
    textEditText += tr("FWHM = ");
    if (locale)
      textEditText += application->locale().
	toString(fullWidthHalfMax, 'f', MXP_OLIGOMER_DEC_PLACES);
    else
      textEditText += QString().
	setNum(fullWidthHalfMax, 'f', MXP_OLIGOMER_DEC_PLACES);;
  
    textEditText += "\n";

    double c = fullWidthHalfMax /(2 * sqrt(log(2)));
    double c2 = pow(c, 2);

    textEditText += tr("with c = ");
    if (locale)
      textEditText += application->locale().
	toString(c, 'f', MXP_OLIGOMER_DEC_PLACES);
    else
      textEditText += QString().setNum(c, 'f', MXP_OLIGOMER_DEC_PLACES);;
  
    textEditText += tr(" and c^2 = ");
    if (locale)
      textEditText += application->locale().
	toString(c2, 'f', MXP_OLIGOMER_DEC_PLACES);
    else
      textEditText += QString().setNum(c2, 'f', MXP_OLIGOMER_DEC_PLACES);;
  
    textEditText += "\n============================================\n\n";
  
    // Out put some text in the textEdit:
    m_ui.resultTextEdit->append(textEditText);
    
    return true;
  }


  void 
  IsotopicPatternCalculationDlg::execute()
  {
    Application *application = static_cast<Application *>(qApp);

    bool inserted = false;
    int wholeCount = 0;

    // Clear the textEdit widget so that we can start out-putting text
    // into it.
    m_ui.resultTextEdit->clear();

    if (!fetchValidateInputData())
      return;

    double fullWidthHalfMax = m_mono / m_resolution;


    QList<IsotopicPeak *> oldPeakList;
  
    IsotopicPeak *firstPeak = new IsotopicPeak();
    firstPeak->setMass(0);
    firstPeak->setProbability(1);
  
    oldPeakList.append(firstPeak);
	  
    // After the validation of the formula, it contains a list of
    // allocated AtomCount objects. Get a handy shorthand to it.

    const QList<AtomCount *> &atomCountList = m_formula.atomCountList();
  
    QTime time;
    time.start();
  
    m_ui.feedbackLineEdit->setText(tr("Computing the peaks in the pattern."));  

    for (int iter = 0; iter < atomCountList.size(); ++iter)
      {
	AtomCount *atomCount = atomCountList.at(iter);
	int isotopeCount = atomCount->isotopeList().size();
      
	//       qDebug() << "Current atom:" << atomCount->symbol()
	// 		<< "with" << isotopeCount << "isotope(s)";
      
	for(int jter = 0; jter < atomCount->count(); ++jter)
	  {
	    // 	  qDebug() << "Atom count:"<< atomCount->count()
	    // 		    << "current run:" << jter + 1
	    // 		    << "wholeCount:" << wholeCount;	  
	  
	    m_ui.progressBar->setValue(++wholeCount);
	    qApp->processEvents();
	  
	    if (m_aborted)
	      break;
	  	  
	    QList<IsotopicPeak *> newPeakList;

	    for (int kter = 0; kter < oldPeakList.size(); ++kter)
	      {
		IsotopicPeak *peakListPeak = oldPeakList.at(kter);
	      
		for(int lter = 0; lter < isotopeCount; ++lter)
		  {
		    Isotope *isotope = atomCount->isotopeList().at(lter);
		  
		    // 		  qDebug() << "Current isotope:" << isotope->mass();
		  
		    IsotopicPeak *newPeak = 
		      new IsotopicPeak(peakListPeak->mass() +
					isotope->mass(),
					0,
					peakListPeak->probability() *
					(isotope->abundance() / 100),
					0);
		  
		    // 		  qDebug() << "Newly created peak:"
		    // 			    << newPeak->mass() << "--"
		    // 			    << newPeak->probability();
		  
		    if (!newPeakList.size())
		      {
			newPeakList.append(newPeak);

			// 		      qDebug() << "Appended peak:"
			// 			    << newPeak->mass() << "--"
			// 				<< newPeak->probability();
		      }
		    else
		      {
			for(int pter = 0; pter < newPeakList.size(); ++pter)
			  {
			    IsotopicPeak *iteratedPeak = 
			      newPeakList.at(pter);
			  
			    if (newPeak->mass() > 
				iteratedPeak->mass())
			      continue;
			  
			    newPeakList.insert(pter, newPeak);
	
			    // 			  qDebug() << "Inserted peak:"
			    // 				    << newPeak->mass() << "--"
			    // 				    << newPeak->probability();
			  
			    inserted = true;

			    break;
			  }
		      
			// If we did not insert the peak, then that
			// means that we arrived here because we
			// finished iterating in the list... Thus append
			// the peak.
			if(!inserted)
			  {
			    newPeakList.append(newPeak);

			    // 			  qDebug() << "Appended peak:"
			    // 				    << newPeak->mass() << "--"
			    // 				    << newPeak->probability();	  
			  }
			else
			  {
			    // Reinitialize the bool to false.
			    inserted = false;
			  }
		      
			// 		      qDebug() << "List of peaks:";
		      
			// 		      for (int zter = 0; zter < newPeakList.size(); ++zter)
			// 			qDebug() << newPeakList.at(zter)->mass()
			// 				  << "--" 
			// 				  << newPeakList.at(zter)->probability();
		      }
		  }
	      }
	  
	    qDeleteAll(oldPeakList);
	    oldPeakList.clear();
	  
	    oldPeakList = newPeakList;
	    newPeakList.clear();
	    
	    // We now have to remove peaks that are too near one from
	    // the other.
	  
	    for (int nter = 0; nter < oldPeakList.size() - 1; ++nter)
	      {
		double absDiff = fabs(oldPeakList.at(nter)->mass() -
				       oldPeakList.at(nter + 1)->mass());
	      
		if(absDiff <= fullWidthHalfMax)
		  {
		    // 		  qDebug() << "absDiff is" << absDiff;
		  
		    // Remove the forward peak that is too close to
		    // current one, but do not forget to account for
		    // that one from the abundance point of vue.
		  
		    oldPeakList.at(nter)->
		      incrementProbability(oldPeakList.at(nter + 1)->
					    probability());

		    // 		  qDebug() << "Removed peak"
		    // 			    << oldPeakList.at(nter + 1)->mass() << "--"
		    // 			    << oldPeakList.at(nter + 1)->probability();
		  
		    delete oldPeakList.takeAt(nter + 1);
		    --nter;
		  } 
	      }

	    if (m_maximumPeaks >= 2 && oldPeakList.size() > m_maximumPeaks)
	      {
		// The user wants at most m_maximumPeaks peaks, which means
		// we have to remove all the ones in excess.
	      
		for(int pter = m_maximumPeaks; 
		     pter < oldPeakList.size(); ++pter)
		  {
		    delete oldPeakList.takeAt(pter);
		  }
	      }
	  }
            
	if(m_aborted)
	  break;
      }
    m_aborted = false;
  
    m_ui.progressBar->setRange(0, 100);
    m_ui.progressBar->setValue(0);

    qApp->processEvents();

    // Take into account the charge of the analyte ! Take advantage of
    // this run to compute the sum of all the probabilities, that we'll
    // need later. Also take advantage of this looping to get the value
    // of the most probable peak. We'll need it to compute the relative
    // intensities later.
 
    m_ui.feedbackLineEdit->setText(tr("Accounting for the charge."));
 
    double sumProbabilities = 0;
    double greatestProbability = 0;
    
    for (int iter = 0; iter < oldPeakList.size(); ++iter)
      {
	IsotopicPeak *isotopicPeak = oldPeakList.at(iter);
      
	isotopicPeak->setMass(isotopicPeak->mass() / m_charge);
      
	sumProbabilities += isotopicPeak->probability();
	if(isotopicPeak->probability() > greatestProbability)
	  greatestProbability = isotopicPeak->probability();
      }
  
    // If we have actually divided the mass by m_charge > 1, then the
    // peaks need to be reprocessed so as to remove all the ones too
    // close to each other.
  
    if (m_charge > 1)
      {
	for(int nter = 0; nter < oldPeakList.size() - 1; ++nter)
	  {
	    if (fabs(oldPeakList.at(nter)->mass() -
		      oldPeakList.at(nter + 1)->mass()) <= fullWidthHalfMax)
	      {
		// Remove the forward peak that is too close to
		// current one, but do not forget to account for
		// that one from the abundance point of vue.
	      
		oldPeakList.at(nter)->
		  incrementProbability(oldPeakList.at(nter + 1)->
					probability());
	      
		// qDebug() << "Removed peak"
		// << oldPeakList.at(nter + 1)->mass() << "--"
		// << oldPeakList.at(nter + 1)->probability();
	      
		delete oldPeakList.takeAt(nter + 1);
	      } 
	  }
      }
    
    m_ui.feedbackLineEdit->setText(tr("Computing the relative intensity."));

    // Compute the relative intensity.
  
    for (int iter = 0; iter < oldPeakList.size(); ++iter)
      {
	IsotopicPeak *isotopicPeak = oldPeakList.at(iter);
      
	double relativeIntensity = 
	 ( isotopicPeak->probability() / greatestProbability) * 100;
      
	if(relativeIntensity > std::numeric_limits<double>::min())
	  isotopicPeak->setRelativeIntensity(relativeIntensity);
      }
  
    int elapsedTime = time.elapsed() /(1000 * 60);

    // And now we should do something with the peaks !!!

    m_ui.feedbackLineEdit->setText(tr("Formatting the results"));

    bool locale = m_ui.localeCheckBox->checkState() == Qt::Checked ?
      true : false;
    
    double c = fullWidthHalfMax /(2 * sqrt(log(2)));
    double c2 = pow(c, 2);
    double increment = fullWidthHalfMax / 50;

    QString results(tr("RESULTS for this computation(duration: %1 min)\n"
			 "====================================\n\n").
		     arg(elapsedTime));
  
    for (int oter = 0; oter < oldPeakList.size(); ++oter)
      {
	double mass = oldPeakList.at(oter)->mass();
	double prob = oldPeakList.at(oter)->probability();
	double relInt = oldPeakList.at(oter)->relativeIntensity();
      
	if(prob > m_minimumProbability)
	  {
	    results += tr("Mass: ");
	    if (locale)
	      results += application->locale().
		toString(mass, 'f', MXP_OLIGOMER_DEC_PLACES);
	    else
	      results += QString().setNum(mass, 'f', MXP_OLIGOMER_DEC_PLACES);
	    results += tr(" -- Probability: ");
	    if (locale)
	      results += application->locale().
		toString(prob, 'f', MXP_OLIGOMER_DEC_PLACES);
	    else
	      results += QString().setNum(prob, 'f', MXP_OLIGOMER_DEC_PLACES);
	    results += tr(" -- Rel. intensity: ");
	    if (locale)
	      results += application->locale().
		toString(relInt, 'f', MXP_OLIGOMER_DEC_PLACES);
	    else
	      results += QString().
		setNum(relInt, 'f', MXP_OLIGOMER_DEC_PLACES);
	    results += "\n";
	  }
      }
    
    m_ui.resultTextEdit->append(results);

    //   qDebug() << "fullWidthHalfMax" << fullWidthHalfMax
    // 	    << "x-increment" << increment
    // 	    << "c" << c
    // 	    << "c^2" << c2;

    // For each isotopic peak, compute a gaussian.
    // Write the results to the file, peak after peak.

    QFile file(m_filePath);
    QTextStream stream;
  
    bool ret = file.open(QIODevice::WriteOnly |
			  QIODevice::Truncate | 
			  QIODevice::Text);
    if (ret)
      {
	stream.setDevice(&file);
	stream.setCodec("UTF-8");
      }
    else
      {
	QMessageBox::warning(0, 
			      tr("massXpert - Isotopic Pattern Calculation"),
			      tr("Failed to export "
				  "the data: file could not be opened."),
			      QMessageBox::Ok);
      
	stream.setStatus(QTextStream::ReadCorruptData);
      }
  
    for (int iter = 0; iter < oldPeakList.size(); ++iter)
      {
	double prob = oldPeakList.at(iter)->probability();
      
	// If the formula has a big number of atoms, then the first few
	// peaks will be of almost negligible probability, and we'll
	// want to skip them.
	if(prob < m_minimumProbability)
	  continue;
      
	double mono = oldPeakList.at(iter)->mass();
	double relInt = oldPeakList.at(iter)->relativeIntensity();
      
	double leftPoint = mono -(2 * fullWidthHalfMax);
	double rightPoint = mono +(2 * fullWidthHalfMax);
      
	QString outputString;
	stream << outputString;
	outputString.clear();
      
	//       outputString += "#########";
	//       if (locale)
	// 	outputString += application->locale().toString(mono, 'f', 5);
	//       else
	// 	outputString += QString().setNum(mono, 'f', 5);
	//       outputString += "--> [";
	//       if (locale)
	// 	outputString += application->locale().toString(leftPoint, 'f', 5);
	//       else
	// 	outputString += QString().setNum(leftPoint, 'f', 5);
	//       outputString += "--";
	//       if (locale)
	// 	outputString += application->locale().toString(rightPoint, 'f', 5);
	//       else
	// 	outputString += QString().setNum(rightPoint, 'f', 5);
	//       outputString += "]\n";
      
	for(double x = leftPoint; x <= rightPoint; x += increment)
	  {
	    double y = relInt * exp(( - pow((x - mono), 2) ) / c2);
	  
	    if (locale)
	      outputString += application->locale().
		toString(x, 'f', MXP_OLIGOMER_DEC_PLACES);
	    else
	      outputString += QString().
		setNum(x, 'f', MXP_OLIGOMER_DEC_PLACES);
	    outputString += "  ";
	    if (locale)
	      outputString += application->locale().
		toString(y, 'f', MXP_OLIGOMER_DEC_PLACES);
	    else
	      outputString += QString().
		setNum(y, 'f', MXP_OLIGOMER_DEC_PLACES);
	    outputString += "\n";
	  }

	if(stream.status() == QTextStream::ReadCorruptData)
	  m_ui.resultTextEdit->append(outputString);
	else
	  stream << outputString;       
      
	//       qDebug() << outputString;
	outputString.clear();
      }

    m_ui.feedbackLineEdit->setText(tr(""));

    // Finished sending all the peak data to the file. QFile will close
    // itself.
  }


  void 
  IsotopicPatternCalculationDlg::abort()
  {
    m_aborted = true;
  }


  void 
  IsotopicPatternCalculationDlg::outputFile()
  {
    QString name;
  
    m_filePath = 
      QFileDialog::getSaveFileName(this, tr("Export Raw Text File"),
				    QDir::homePath(),
				    tr("Any file type(*)"));
  }


#if 0
  /***************************************************************************
   *  file                 :  ipc.c                                          *
   *  copyright            :(C) 2001-2005 by Dirk Nolting                   *
   *  email                : nolting@uni-duesseldorf.de                      *
   ***************************************************************************/

  /***************************************************************************
   *                                                                         *
   *   This program 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~2 of the License, or     *
   *  (at your option) any later version.                                   *
   *                                                                         *
   ***************************************************************************/

#include "global.h"
#include "ipc.h"
#include "pars.h"
#include "element.h"
#include "gp_out.h"
#include <time.h>
#include <signal.h>
#include <math.h>

  //#define SUMMARIZE_LEVEL 0.00000001
  //#define SUMMARIZE_LEVEL 0.0001
  //#define SUMMARIZE_LEVEL 0.001
#define SUMMARIZE_LEVEL 0.01

  compound *verbindung=NULL;
  isotope *m_peakList;
  int fast_calc=0;

  void free_list(isotope *target)
  {
    while(target->next)
      {
	target=target->next;
	free(target->previous);
      }
    free(target);
  }

  void cut_peaks(isotope *spectrum)
  {
    int dummy=1;

    while((spectrum->next) &&(dummy<fast_calc) )
      {
	++dummy;
	spectrum=spectrum->next;
      }

    if(spectrum->next)
      {
	free_list(spectrum->next);
	spectrum->next=NULL;
      }
  }

  void summarize_peaks()
  {  isotope *dummy,*d2;

    for(dummy=m_peakList;dummy;dummy=dummy->next)
      /* Differenz wegen Rundungsfehlern */
      while( dummy->next &&(dummy->next->mass - dummy->mass < SUMMARIZE_LEVEL) )
	{
	  d2=dummy->next;
	  dummy->next=d2->next;
	  if(dummy->next)
	    dummy->next->previous=dummy;
	  dummy->p+=d2->p;
	  free(d2);
	}
  }

  isotope *add_peak(isotope *base,isotope *peak)
  {
    static isotope *IsotopeIterator;

    if(!(base->mass))
      {
	peak->next=NULL;
	peak->previous=NULL;
	IsotopeIterator = peak;
	return peak;
      }

    if( peak->mass >= IsotopeIterator->mass )
      while((IsotopeIterator->next) &&(IsotopeIterator->mass < peak->mass))
	IsotopeIterator=IsotopeIterator->next;
    else
      {
	while((IsotopeIterator->previous) &&(IsotopeIterator->mass > peak->mass) )
	  IsotopeIterator=IsotopeIterator->previous;
	IsotopeIterator=IsotopeIterator->next;
      }

    if((IsotopeIterator->mass) >=(peak->mass) )
      {
	peak->next=IsotopeIterator;
	peak->previous=IsotopeIterator->previous;
	peak->previous->next=peak;
	IsotopeIterator->previous=peak;
	return base;
      }
    else
      {
	IsotopeIterator->next=peak;
	peak->next=NULL;
	peak->previous=IsotopeIterator;
	return base;
      }
    return 0;
  }

  int calculate_peaks(){
    compound *c;
    isotope *newPeakList,*p,*i,*np1;
    int amount;

    if(!(m_peakList=malloc(sizeof(isotope))))
      return 0;
    m_peakList->mass=0;
    m_peakList->p=1;
    m_peakList->previous=NULL;
    m_peakList->next=NULL;

    for (c = verbindung; c; c = c->next)
      {
	for(amount = 0; amount < c->amount; ++amount)
	  {
	    if (!(newPeakList=malloc(sizeof(isotope))))
	      return 0;
	  
	    newPeakList->mass = 0;
	  
	    for (p = m_peakList; p; p = p->next)
	      {
		for(i = c->isotopes; i; i = i->next)
		  {
		    //printf("working on isotope of mass %f\n", i->mass);

		    if (!(np1 = malloc(sizeof(isotope))))
		      return 0;
		  
		    np1->mass=p->mass + i->mass;
		    np1->p=p->p * i->p;
		  
		    if(!(newPeakList = add_peak(newPeakList,np1)))
		      return 0;
		  }
	      }
	  
	    free_list(m_peakList);
	    m_peakList = newPeakList;
	    summarize_peaks();
	  
	    if (fast_calc)
	      cut_peaks(m_peakList);
	  }
      }
  
    return 1;
  }


  void print_result(int digits,int charge){
    isotope *d;
    double maxp=0,relint=0,sump=0;
    int permutationen=0;

    printf("\n");
 
    for(d=m_peakList;d;d=d->next)
      {
	++permutationen;
	sump+=d->p;
	d->mass=d->mass / charge;
	d->mass=(rint( d->mass * pow(10,digits) ) / pow(10,digits) );
      }

    summarize_peaks();
    for(d=m_peakList;d;d=d->next)
      if(d->p > maxp)
	maxp=d->p;

    for(d=m_peakList;d;d=d->next)
      {
	if(( relint=(d->p/maxp)*100) > MIN_INT )
	  printf("M= %f, p= %e, rel. Int.= %f%%\n",
		 d->mass,d->p,relint);
      }
    if(!(fast_calc))
      printf("\nNumber of  permutations: %i\n",permutationen);
    else
      {
	sump=(rint(sump*10000)/100); 
	printf("\nCovered Intensity: %2.2f%% \n",sump);
      }
  }

  int main(int argc,char **argv){
    long seconds;
    int d=1,zeig_summenformel=0,calc_peaks=1,gnuplot=0,use_digits=USE_DIGITS,charge=1;
    char *gnuplotfile=NULL;
  
    if(!argv[d])
      {
	usage();
	return 1;
      }

#ifdef HAVE_SIGNAL
    signal(SIGHUP,SIG_IGN);
#endif

    if(!init_elements()){
      printf("Error in init_elements\n");
      return 1;
    }
    while(argv[d])
      {
	if(!strcmp(argv[d],"-f"))
	  {
	    ++d;
	    if(!argv[d])
	      {
		printf("Missing argument for -f\n");
		return 1;
	      }
	    fast_calc=strtol(argv[d],NULL,10);
	  }

	else if(!strcmp(argv[d],"-z"))
	  {
	    ++d;
	    if(!argv[d])
	      {
		printf("Missing argument for -z\n");
		return 1;
	      }
	    charge=strtol(argv[d],NULL,10);
	  }

	else if(!strcmp(argv[d],"-d"))
	  {
	    ++d;
	    if(!argv[d])
	      {
		printf("Missing argument for -d\n");
		return 1;
	      }
	    use_digits=strtol(argv[d],NULL,10);
	  }

	else if(!strcmp(argv[d],"-c")){
	  ++d;
	  if(!argv[d])
	    {
	      printf("Missing argument for -c\n");
	      return 1;
	    }
	  if(!pars_chem_form(argv[d])){
	    printf("Parser error.\n");
	    return 1;
	  }
	}

	else if(!strcmp(argv[d],"-g")){
	  ++d;
	  if(!argv[d]){
	    printf("Missing argument for -g\n");
	    return 1;
	  }
	  gnuplot=1;
	  if(!(gnuplotfile=strdup(argv[d]))){
	    printf("Not enough memory\n");
	    return 1;
	  }
	}


	else if(!strcmp(argv[d],"-p")){
	  ++d;
	  if(!argv[d]){
	    printf("Missing argument for -p\n");
	    return 1;
	  }
	  if(!(pars_peptid(argv[d]))){
	    printf("Error in peptid parser.\n");
	    return 1;
	  }
	}

	else if(!strcmp(argv[d],"-a")){
	  ++d;
	  if(!argv[d]){
	    printf("Missing argument for -a\n");
	    return 1;
	  }
	  if(!pars_amino_acid(argv[d])){
	    printf("Error in pars_amino_acid.\n");
	    return 1;
	  }
	}

	else if(!strcmp(argv[d],"-s"))
	  zeig_summenformel=1;

	else if(!strcmp(argv[d],"-h"))
	  {
	    usage();
	    return 1;
	  }

	else if(!strcmp(argv[d],"-x"))
	  calc_peaks=0;

	else
	  {
	    printf("Unknown flag: %s\n",argv[d]);
	    usage();
	    return 1;
	  }
	++d;
      }

    if(zeig_summenformel)
      {
	if(!print_sum())
	  {
	    printf("error while showing chemical formula.\n");
	    return 1;
	  }
      }
#ifdef HAVE_TIME
    seconds=time(0);
#endif

    if(calc_peaks)
      if(!calculate_peaks()){
	printf("Error in calculate_peaks\n");
	return 1;
      }

    print_result(use_digits,charge);

#ifdef HAVE_TIME
    seconds=time(0)-seconds;
    printf("Computing time: %li seconds.\n",seconds);
#endif

    if(gnuplot)
      if(!(make_gnuplot_output(gnuplotfile))){
	printf("Error while generating gnuplot file\n");
	return 1;
      }

    return 0;
  }


  void usage()
  {
    printf("\nThis is IPC v %s\nCopyright Dirk Nolting 2001-2005\n\n",VERSION);
    printf("\nSynopsis:\n ipc -d <int> -z <int> -f <int> <-a <amino acid> -c <chemical formula> -p <File> -g <name> -s -x -h\n\n");

    printf("-c <chemical formula> calculates the isotopic pattern of the \n");
    printf("   given chemical formula. Additive with -p\n");

    printf("-p <File> reads peptide sequenz(one letter notation) from the \n");
    printf("   given file and calculates the isotopic pattern. Additive with -c\n");

    printf("-a <amino acids> calculate isotopic pattern from given amino acids\n");
    printf("   in one letter notation\n");

    printf("-s gives the chemical formula. Usefull for -a or -p\n");
    printf("-g creates gnuplot output and stores it in the file <name> and <name>.gnu\n");

    printf("-x no calculation of the isotopic pattern. Usefull for -s\n");
    printf("-f fast calc, calculates only first <int> peaks\n");
    printf("-d <int> digits significant\n");
    printf("-z assume <int> charges on ion \n");
    printf("-h show this text\n\n");
  }
#endif

} // namespace massXpert