File: LutefiskXCorr.c

package info (click to toggle)
lutefisk 1.0.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 1,896 kB
  • ctags: 1,229
  • sloc: ansic: 24,297; makefile: 18; sh: 15
file content (1038 lines) | stat: -rw-r--r-- 31,946 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
/*********************************************************************************************
Lutefisk is software for de novo sequencing of peptides from tandem mass spectra.
Copyright (C) 1995  Richard S. Johnson

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.

This program 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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact:

Richard S Johnson
4650 Forest Ave SE
Mercer Island, WA 98040

jsrichar@alum.mit.edu
*********************************************************************************************/

/*
	Richard S. Johnson
	6/96 - ?
	
		LutefiskXP is a program designed to aid in the interpretation of CID data of peptides.  
	The main assumptions are that the data is of reasonable quality, the N- and C-terminal
	modifications (if any) are known, and the precursor ion charge (and therefore the 
	peptide molecular weight) are known.  The ultimate goal here is to develop code that
	can utilize msms data in conjunction with ambiguous and incomplete Edman sequencing data,
	sequence tags, peptide derivatization, and protein or est database searches.  An older 
	version of LutefiskXP has been written in FORTRAN and runs on 68K Macs that have an fpu
	(1991, 39th ASMS Conference on Mass Spectrometry and Allied Topics, Nashville, TN, pp 1233-
	1234).  This is a different and improved algorithm partly inspired by Fernandez-de-Cossjo, 
	et al. (1995) CABIOS Vol. 11 No. 4 pp 427-434.  Combining this msms interpretation algorithm
	with Edman sequencing, database searches, and derivatization is entirely of my own design;
	J. Alex Taylor implemented the changes in the FASTA code (Bill Pearson, U. of VA) so that
	the LutefiskXP output can be read directly by the modified FASTA program.  In addition, there
	were a number of additional critical changes made to FASTA to make it more compatible with
	msms sequencing data.
	
		The trademark LutefiskXP was chosen at random, and is not meant to imply any similarity
	between this computer program and the partially base-hydrolzyed cod fish of the same name
	(minus XP). 
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/* Lutefisk headers*/
#include "LutefiskPrototypes.h"
#include "LutefiskDefinitions.h"


#define MAXPARENTCHARGE 9
#define SIDE_PEAK_ATT	0.75	/*Peak heights on sides of main peak in mock spectrum.*/
#define PLUS1_NEUT_LOSS_ATT	0.5	/*Peak height for neutral losses of singly charged precursors.*/
#define NEUT_LOSS_ATT 0.1	/*Peak height for neutral losses of multiply charged precursors.*/
#define BAD_B_ATT	0.05	/*Peak heights for b ions that are not very likely.*/
#define A_ATT	0.5	/*Peak heights for a ions.*/
#define BAD_A_ATT 0.05	/*Peak heights for a ions that are not very likely.*/
#define INT_FRAG_ATT 0.1	/*peak heights for internal fragment ions.*/
#define BAD_Y_ATT	0.05	/*Peak heights for y ions that are not very likely.*/

extern REAL_4 	*spectrum1;
extern REAL_4 	*spectrum2;
extern REAL_4 	*tau;

UINT_4 SIZEOF_SPECTRA;	/* Smallest power of 2 (for cross-correlation)*/
REAL_4 gSidePeakAtt = SIDE_PEAK_ATT;


/*Here's some globals that are specific to this file.  They are two amino acid nominal masses
*times 100 for Cys, Arg, His, and Lys.  These get modified at the start of the ScoreSequences
*function in order to accomodate different alkyl groups on cysteine.
*/
INT_4 gCysPlusXCorr[AMINO_ACID_NUMBER] = {
	174, 259, 217, 218, 206, 232, 231, 160, 240, 216,
	216, 231, 234, 250, 200, 190, 204, 289, 266, 202,
	0,0,0,0,0
};

INT_4 gArgPlusXCorr[AMINO_ACID_NUMBER] = {
	227, 312, 270, 271, 259, 285, 284, 213, 293, 269,
	269, 284, 287, 303, 253, 243, 257, 342, 319, 255,
	0,0,0,0,0
};

INT_4 gHisPlusXCorr[AMINO_ACID_NUMBER] = {
	208, 293, 251, 252, 240, 266, 265, 194, 274, 250,
	250, 265, 268, 284, 234, 224, 238, 323, 300, 236,
	0,0,0,0,0
};

INT_4 gLysPlusXCorr[AMINO_ACID_NUMBER] = {
	199, 284, 242, 243, 231, 257, 256, 185, 265, 241,
	241, 256, 259, 275, 225, 215, 229, 314, 291, 227,
	0,0,0,0,0
};

INT_4 lowMassIonMass[AMINO_ACID_NUMBER] = {
	44, 112, 87, 88, 76, 102, 102, 30, 110, 86, 
	86, 129, 104, 120, 70, 60, 74, 159, 136, 72,
	0,0,0,0,0
};

REAL_4 lowMassIonIntFactor[AMINO_ACID_NUMBER] = {
	0.1, 0.1, 0.1, 0.1, 0.0, 0.1, 0.1, 0.0, 0.5, 0.5, 
	0.5, 0.25, 0.1, 0.5, 0.3, 0.1, 0.1, 0.2, 0.3, 0.2,
	0,0,0,0,0
};

extern void CrossCorrScoreTheSeq(struct SequenceScore *currScorePtr);

/***************************YXCorrCalc*****************************************************
*
*	This function calculates the mass of singly charged y ions for a given cleavage,
*	and a given sequence.  It returns a REAL_4.
*
*/
REAL_4 YXCorrCalc(INT_4 i, struct SequenceScore *currScorePtr, REAL_4 YionStart, 
					INT_4 seqLength)
{
	REAL_4 Yion;
	INT_4 j, k;
	char test;
	REAL_8 mToAFactor;
	
	Yion = YionStart;
	for(j = i; j < seqLength; j++)
	{
		test = TRUE;
		for(k = 0; k < gAminoAcidNumber; k++)
		{
			if(currScorePtr->peptide[j] == gNomMass[k])
			{
				test = FALSE;
				Yion += gMonoMass[k];
				break;
			}
		}
		if(test)	/*its a 2 aa extension*/
		{
			Yion = Yion + ((currScorePtr->peptide[j])) + 0.07;	/*0.07 is a guess at the mass defect*/
		}
	}
	
	if(Yion > gParam.monoToAv)
	{
		mToAFactor = 0;
	}
	else
	{
		if(Yion >= (gParam.monoToAv - AV_MONO_TRANSITION))
		{
			mToAFactor = (gParam.monoToAv - Yion) / AV_MONO_TRANSITION;
		}
		else
		{
			mToAFactor = 1;
		}
	}
	mToAFactor = MONO_TO_AV - ((MONO_TO_AV - 1) * mToAFactor);
	if(Yion >= (gParam.monoToAv - AV_MONO_TRANSITION))
	{
		Yion = Yion * mToAFactor;
	}


	return(Yion);
}
/******************************BXCorrCalc***************************************************
*
*	This function calculates the mass of singly charged b ions for a given cleavage,
*	and a given sequence.  It returns a REAL_4.
*
*/
REAL_4 BXCorrCalc(INT_4 i, struct SequenceScore *currScorePtr, REAL_4 BionStart)
{
	REAL_4 Bion;
	INT_4 j, k;
	char test;
	REAL_8 mToAFactor;
	
	Bion = BionStart;
	for(j = 0; j < i; j++)
	{
		test = TRUE;
		for(k = 0; k < gAminoAcidNumber; k++)
		{
			if(currScorePtr->peptide[j] == gNomMass[k])
			{
				test = FALSE;
				Bion += gMonoMass[k];
				break;
			}
		}
		if(test)	/*its a 2 aa extension*/
		{
			Bion = Bion + ((currScorePtr->peptide[j])) + 0.07;	/*0.07 is a guess at the mass defect*/
		}
	}
	
	if(Bion > gParam.monoToAv)
	{
		mToAFactor = 0;
	}
	else
	{
		if(Bion >= (gParam.monoToAv - AV_MONO_TRANSITION))
		{
			mToAFactor = (gParam.monoToAv - Bion) / AV_MONO_TRANSITION;
		}
		else
		{
			mToAFactor = 1;
		}
	}
	mToAFactor = MONO_TO_AV - ((MONO_TO_AV - 1) * mToAFactor);
	if(Bion >= (gParam.monoToAv - AV_MONO_TRANSITION))
	{
		Bion = Bion * mToAFactor;
	}
		
	return(Bion);
}


/*********************************FindNChargeXCorr********************************************
*
*	Counts the number of charged residues in the sequence.
*
*/
INT_4 FindNChargeXCorr(struct SequenceScore *currScorePtr)
{
	INT_4 j, k, seqLength, nChargeCount, *pSeq;
	
/*	Initialize.*/
	nChargeCount = 1;
	seqLength = 0;

/*
	Determine the sequence length.
*/
	pSeq = &currScorePtr->peptide[0];
        while(*pSeq != '\x0')
	{
		seqLength++;
		pSeq++;
	}
/*
	Find one aa extensions that are either R, H, or K.
*/
	for(j = 0; j < seqLength; j++)
	{
		if((currScorePtr->peptide[j] == gNomMass[R]) || (currScorePtr->peptide[j] == gNomMass[H]) || 
			(currScorePtr->peptide[j] == gNomMass[K]))
		{
			nChargeCount += 1;
		}
	}
/*
	Here I look for two amino acid extensions containing Arg, His, or Lys.
*/
	for(j = 0; j < seqLength; j++)	
	{
		for(k = 0; k < gAminoAcidNumber; k++)
		{
			if(currScorePtr->peptide[j] == gArgPlusXCorr[k] || 
				currScorePtr->peptide[j] == gHisPlusXCorr[k] || 
				currScorePtr->peptide[j] == gLysPlusXCorr[k])
			{
				nChargeCount += 1;
			}
		}
	}
	return(nChargeCount);
}


/**************************	DoCrossCorrelationScoring*******************************************
*
*
*/
void DoCrossCorrelationScoring(struct SequenceScore *firstScorePtr, struct MSData *firstMassPtr) 
{

	INT_4 		i, seqNum;						
	REAL_4		normalizedScore;		/* The max absolute cross-correlation value
										*   (Later used as the normalizing factor)*/
	REAL_4		tauDiff, intensityAccountedFor, autocorrelation;
	struct SequenceScore *currSeqPtr;


	if (!firstScorePtr) 
	{
		return;
	}
	
	/*For high accuracy data, make peaks w/ no extra width*/
	if(gParam.qtofErr != 0)
	{
		if(gParam.qtofErr < 0.25)
		{
			gSidePeakAtt = 0;
		}
		else
		{
			gSidePeakAtt = SIDE_PEAK_ATT;
		}
	}
	else
	{	
		gSidePeakAtt = SIDE_PEAK_ATT;
	}

/*Do the funny normalization of intensities that Eng does in Sequest.*/
	CalcNormalizedExptPeaks(firstMassPtr);

/*Set aside memory for the spectra that are cross-correlated.*/
	SetupCrossCorrelation();

	if (!spectrum1 || !spectrum2 || !tau) 
	{
		/* Do not proceed with cross-correlation because we couldn't get the memory*/
		return;
	}
	else  
	{
		FillInSpectrum1(firstMassPtr);	/*Using the list of ions from firstMassPtr, generate
										a "real" spectrum for cross-correlating.  These mass
										values will be 2 times the actual number in order to
										add to the specificity of the cross-correlation.*/
	}
	
	/*Do an autocorrelation of the spectrum.  This is the normalizing factor used later*/
	
	CrossCorrelate(spectrum1-1, spectrum1-1, (UINT_4) SIZEOF_SPECTRA, tau-1);
	for(i = 0; i < SIZEOF_SPECTRA; i++)
	{
		if(tau[i] < 1)
		{
			tau[i] = 0;
		}
	}
	
	
	intensityAccountedFor = 0.0;
	for(i = 1; i < 250; i++)
	{
		tauDiff = tau[i] - tau[SIZEOF_SPECTRA - i];
		if(tauDiff < 0)
		{
			tauDiff = tauDiff * -1;
		}
		intensityAccountedFor += tauDiff;
	}
	autocorrelation = tau[0] - intensityAccountedFor/250; /*should be equal to tau(0)*/

	
	/*debug spectrum1*/
	/*printf("data spectrum \n");	
	for(i = 0; i < SIZEOF_SPECTRA; i++)	
	{
		if(spectrum1[i] != 0)
		{
			printf("%f  %f \n",(REAL_4)i/2,spectrum1[i]);
		}
	}*/

/*Count the number of sequences.*/
	seqNum = 0;
	currSeqPtr = firstScorePtr;
	while(currSeqPtr != NULL)
	{
		seqNum++;
		currSeqPtr = currSeqPtr->next;
	}
	
/*If there are too many, then cut the number of sequences to be cross-correlated.*/
	if(seqNum > MAX_X_CORR_NUM)
	{
		seqNum = MAX_X_CORR_NUM;
	}
	
/*Cross-correlate the sequences.  Only do the top intensity-scorers.*/
	for(i = 1; i <= seqNum; i++)
	{	
		currSeqPtr = firstScorePtr;
		while(currSeqPtr != NULL)
		{
			if(i == currSeqPtr->rank)
			{
				CrossCorrScoreTheSeq(currSeqPtr);
			}
			currSeqPtr = currSeqPtr->next;
		}

	}
		
	/* Normalize the cross-correlation results to 1.0*/
	normalizedScore = 0.0 ;	/* First find the highest score*/
	currSeqPtr = firstScorePtr;
	while(currSeqPtr != NULL)
	{
		if (currSeqPtr->crossDressingScore > normalizedScore) 
		{
			normalizedScore = currSeqPtr->crossDressingScore;
		}
		currSeqPtr = currSeqPtr->next;	/* Point to the next struct in the linked list to 
										* continue the while loop.	*/
	}	
	/* Use the highest score to normalize them all*/
	if (normalizedScore > 0.0) 
	{
		normalizedScore = 1/normalizedScore;
	}
	
	/*normalizedScore = 1;*/	/*to keep from normalizing*/
	/*normalizedScore = 1 / gParam.peptideMW;*/ 
	if(autocorrelation == 0)
	{	
		printf("Avoiding divide by zero.");
                autocorrelation += 0.0001;  // JAT 2006.08.28
//		exit(1);
	}
	normalizedScore = 1 / autocorrelation;	/*another way to normalize*/
	
	currSeqPtr = firstScorePtr;
	while(currSeqPtr != NULL)
	{
		currSeqPtr->crossDressingScore = normalizedScore * currSeqPtr->crossDressingScore;

		currSeqPtr = currSeqPtr->next;	/* Point to the next struct in the linked list to 
										*  continue the while loop.	*/
	}	
	
	if (spectrum1)	free(spectrum1);	spectrum1 = NULL;
	if (tau)        free(tau);	          tau = NULL;
	
	return;
}
/**************************	CrossCorrScoreTheSeq*******************************************
*
*  This function calculates the cross-correlation score for ea. peptide passed to it.
*/
void CrossCorrScoreTheSeq(struct SequenceScore *currScorePtr) 
{

	INT_4 			i, j, k;					/* Loop indicies. */
	INT_4			chargeLimit;				/* Max # of daughter charges to consider */
	REAL_4 			intensityAccountedFor = 0.0;/* Sum of the intensities of the ions matched with fragment ions. */
	REAL_4 			Bion;						/* Mass of the singly charged B ion. */         
	REAL_4 			Yion;						/* Mass of the singly charged Y ion. */
	REAL_4			BionStart;					/* Mass of N-terminal group (H, Ac, etc)*/
	REAL_4 			YionStart;					/* Mass of Y-terminal group (free or amidated)*/
	REAL_4 			BionMass;					/* Mass of the B ion in a particular charge state. */
	REAL_4 			AionMass;					/* Mass of the A ion (B ion - CO) in a particular charge state. */
	REAL_4 			YionMass;					/* Mass of the Y ion in a particular charge state. */
	REAL_4			proAttenuation;				/* Attenuations intensity for y ions next to Pro*/
	REAL_4			glyAttenuation;				/* Attenuates intensity for y ions next to Gly*/
	REAL_4 			fragmentMass;				/* Mass of the internal fragment ion. */
	REAL_4 			parent;				 		/* The m/z of the parent ion. */
	REAL_4 			tolerance;					/* The daughter ion error tolerance. */
	REAL_4 			offset; 					/* The mass offset for daughter ions. */
	REAL_4 	parentMinH2O, parentMinNH3, parentMin2H2O, parentMin2NH3;
	REAL_4	tauDiff;
	REAL_4	highMassRange, lowMassRange;

	REAL_4 			fullIntensity;				/* Intensity used by cross-correlation when creating dummy spectra. */
	INT_4			seqLength, nChargeCount, cChargeCount;
	INT_4			*pSeq;
	char			NTermTest, widePeak;

	parent    = (gParam.peptideMW + (gParam.chargeState * gElementMass[HYDROGEN])) / gParam.chargeState;
	tolerance = gParam.fragmentErr;
	if(gParam.fragmentPattern == 'L')	/*ion masses outside of this range are not penalized if not found*/
	{
		lowMassRange = parent * 0.333;	/*so-called 1/3 rule*/
		highMassRange = 2000;	/*mass limit for Deca*/
	}
	else
	{
		lowMassRange = 146;	/*y1 for Lys*/
		highMassRange = 2 * parent;	/*often the very high mass ions are missing*/
	}
	offset    = 0;	/*the offset has already been applied*/
	chargeLimit = gParam.chargeState;
	if(gParam.fragmentErr <= 0.75)
	{
		widePeak = FALSE;	/*mock peak widths are 1.5 dalton*/
	}
	else
	{
		widePeak = TRUE;	/*mock peak widths are 2.5 daltons*/
	}
		
	if (spectrum2) {
		free(spectrum2);   /* Throw away the old data (if any exists) */
		spectrum2 = NULL;
	}	
	spectrum2 = (REAL_4 *) malloc(SIZEOF_SPECTRA*sizeof(REAL_4));
	if(spectrum2 == NULL)
	{
		printf("Out of memory");
		exit(1);
	}
	for(i = 0; i < SIZEOF_SPECTRA; i++)
	{
		spectrum2[i] = 0;
	}
	if (!spectrum2) return;

	seqLength = 0;
	
/* 
	Determine the mass of the N and C-termini.
*/
	BionStart = gParam.modifiedNTerm;
	
	YionStart = gParam.modifiedCTerm + (2 * gElementMass[HYDROGEN]);

	
/*	
	Setup the values for nChargeCount and cChargeCount; FindNChargeXCorr also 
	removes the factor of 100 from the sequence masses.
*/
	cChargeCount = 1;
	nChargeCount = FindNChargeXCorr(currScorePtr);
	
/*
	Test if N-terminal amino acid is one or two residues.
*/
		NTermTest = TRUE;	
		for(k = 0; k < gAminoAcidNumber; k++)
		{
			if(currScorePtr->peptide[0] == gNomMass[k])
			{	
				NTermTest = FALSE;	/*TRUE if a two amino acid step.*/
			}
		}
/*	
	Find the sequence length, so I can loop through it.
*/
	pSeq = &currScorePtr->peptide[0];
	seqLength = 0;
        while(*pSeq != '\x0')
        {
		seqLength++;
		pSeq++;
	}

/*
	Here's where I loop through the sequence.
	=================================================================
*/
	for (i = seqLength - 1; i > 0; i--) 
	{
		fullIntensity = 50;	/*Arbitrary, but it should match the spectrum1 max value.*/

/*	Figure out what Bion and Yion should be.*/
		Bion = BXCorrCalc(i, currScorePtr, BionStart);
		Yion = YXCorrCalc(i, currScorePtr, YionStart, seqLength);

/*	Figure out what cChargeCount and nChargeCount should be.*/

		if((currScorePtr->peptide[i] == gNomMass[R]) || 
			(currScorePtr->peptide[i] == gNomMass[H]) || 
			(currScorePtr->peptide[i] == gNomMass[K]))
		{
			cChargeCount += 1;
			nChargeCount -= 1;
		}
		else	/*Check to see if its a two amino acid combo that could contain Arg, His, or Lys.*/
		{
			for(j = 0; j < gAminoAcidNumber; j++)
			{
				if(currScorePtr->peptide[i] == gArgPlusXCorr[j] || 
					currScorePtr->peptide[i] == gHisPlusXCorr[j] || 
					currScorePtr->peptide[i] == gLysPlusXCorr[j])
				{
					cChargeCount += 1;
					nChargeCount -= 1;
					break;
				}
			}
		}
		
/* 
	Look for ea. possible charge state.>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*/
		for (j = 1; j <= chargeLimit; j++) {   
			BionMass = (Bion + (j - 1))/j;
			AionMass = (Bion - CO + (j - 1))/j;
			YionMass = (Yion + (j - 1))/j;
		 	/* Once we are looking for fragment ions of charge 3 or above these should be considered
		 	*  less likely and so, if cross-correlation is on, these peaks will be given an intensity
		 	*  quarter of what it would be for a charge 1 or 2 fragment ion. */
		 	if (j == 3) fullIntensity = fullIntensity / 4;

		 	
		 	/* Dummy up a theoretical spectrum for the peptide.
			*  Maker sure there are chargeable amino acids for the given charge - j - and don't
			*  bother with b and a ions that have just a proton or have just a proton and one aa.*/
				
			if(nChargeCount >= j && i != seqLength && (i != 1 || NTermTest))
			{
				if((BionMass * j) > (j-1) * 500)	/*Make sure there is enough mass for the charge.*/
				{
	 				if (BionMass < msms.scanMassHigh && BionMass < highMassRange
	 					&& BionMass > lowMassRange) 
					{
						if(gParam.chargeState == 1)	/*If the precursor is singly charged.*/
						{
							BionMass = BionMass * 2;
							NH3 = NH3 * 2;
							H2O = H2O * 2;
							
							AddPeakToSpectrum(spectrum2, BionMass, fullIntensity);
							AddPeakToSpectrum(spectrum2, BionMass - NH3/j, fullIntensity * PLUS1_NEUT_LOSS_ATT);
							AddPeakToSpectrum(spectrum2, BionMass - H2O/j, fullIntensity * PLUS1_NEUT_LOSS_ATT);

							BionMass = BionMass * 0.5;
							NH3 = NH3 * 0.5;
							H2O = H2O * 0.5;
						}
						else	/*For multiply charged precursors.*/
						{
							/*If the +1 b ion has an m/z less than the precursor ion or if the number of chargeable
							amino acids in the +1 b ion equals the charge state of the precursor, then
							give these guys full intensity.*/
							if((j == 1 || j <= (gParam.chargeState - 1)) && 
								(BionMass < parent || nChargeCount == gParam.chargeState
								|| gParam.fragmentPattern == 'L'))
							{
								BionMass = BionMass * 2;
								NH3 = NH3 * 2;
								H2O = H2O * 2;
								
								AddPeakToSpectrum(spectrum2, BionMass, fullIntensity);
								AddPeakToSpectrum(spectrum2, BionMass - NH3/j, fullIntensity * NEUT_LOSS_ATT);
								AddPeakToSpectrum(spectrum2, BionMass - H2O/j, fullIntensity * NEUT_LOSS_ATT);
								
								BionMass = BionMass * 0.5;
								NH3 = NH3 * 0.5;
								H2O = H2O * 0.5;
							}
							else	/*Otherwise... (ie, >+1 b ions or +1 bions > precursor)*/
							{
								BionMass = BionMass * 2;
								NH3 = NH3 * 2;
								H2O = H2O * 2;
								
								AddPeakToSpectrum(spectrum2, BionMass, fullIntensity * BAD_B_ATT);
								AddPeakToSpectrum(spectrum2, BionMass - NH3/j, fullIntensity * BAD_B_ATT * NEUT_LOSS_ATT);
								AddPeakToSpectrum(spectrum2, BionMass - H2O/j, fullIntensity * BAD_B_ATT * NEUT_LOSS_ATT);
								
								BionMass = BionMass * 0.5;
								NH3 = NH3 * 0.5;
								H2O = H2O * 0.5;
							}
						}
	 				}
		
			 		if (AionMass < msms.scanMassHigh && AionMass < highMassRange && AionMass > lowMassRange) 
			 		{
						if(gParam.chargeState == 1)
						{
							AionMass = AionMass * 2;
							
							AddPeakToSpectrum(spectrum2, AionMass, fullIntensity * A_ATT);
							
			 				AionMass = AionMass * 0.5;
						}
						else
						{
/*	Give higher intensity for singly charged a2 ions.  Skip if LCQ data (?).*/
							if(j == 1 && ((NTermTest == TRUE && i == 1) || 
											(NTermTest == FALSE && i == 2)))
							{
								AionMass = AionMass * 2;
								
								AddPeakToSpectrum(spectrum2, AionMass, fullIntensity * A_ATT);
								
								AionMass = AionMass * 0.5;
							}
							else
							{
								if((j == 1 || j <= (gParam.chargeState - 1)) && 
									(BionMass < parent ||  nChargeCount == gParam.chargeState))
								{
									AionMass = AionMass * 2;
									
									AddPeakToSpectrum(spectrum2, AionMass, fullIntensity * A_ATT * BAD_A_ATT);
									
			 						AionMass = AionMass * 0.5;
								}
								else
								{
									AionMass = AionMass * 2;
									
									AddPeakToSpectrum(spectrum2, AionMass, fullIntensity * A_ATT * BAD_A_ATT * BAD_A_ATT);

			 						AionMass = AionMass * 0.5;
								}
							}
						}
			 		}	
				}
			}

			if(cChargeCount >= j && i != 0)
			{
				if((YionMass * j) > (j-1) * 500)
				{
	 				if (YionMass < msms.scanMassHigh && YionMass < highMassRange && YionMass > lowMassRange) 
					{
						proAttenuation = 1.0;
						glyAttenuation = 1.0;
						if(i > 2 && i < seqLength - 2)	/*don't attenuate at the ends*/
						{
							if(currScorePtr->peptide[i-1] == gNomMass[P])
							{
								proAttenuation = 0.2;	/*Attenuates y ions followin P*/
							}
							if(currScorePtr->peptide[i-1] == gNomMass[G])
							{
								/*glyAttenuation = 0.2;*/	/*Attenuates y ions followin G*/
							}
						}
						
						fullIntensity = fullIntensity * proAttenuation * glyAttenuation;
										
						if(gParam.chargeState == 1)
						{
							YionMass = YionMass * 2;
							NH3 = NH3 * 2;
							H2O = H2O * 2;
							
							AddPeakToSpectrum(spectrum2, YionMass, fullIntensity);
							AddPeakToSpectrum(spectrum2, YionMass - NH3/j, fullIntensity * PLUS1_NEUT_LOSS_ATT);
							AddPeakToSpectrum(spectrum2, YionMass - H2O/j, fullIntensity * PLUS1_NEUT_LOSS_ATT);

	 						YionMass = YionMass * 0.5;
	 						NH3 = NH3 * 0.5;
							H2O = H2O * 0.5;
						}
						else
						{	/*LCQ data has a preponderance of multiply charged y ions*/
							if(j == 1 || j <= (gParam.chargeState - 1) || 
								(i <= 2 && !NTermTest) || (i <= 1 && NTermTest) ||
								(i <= (INT_4)seqLength / 4 && gParam.fragmentPattern == 'L'))
							{
								YionMass = YionMass * 2;
	 							NH3 = NH3 * 2;
								H2O = H2O * 2;
								if(j == 1 || j <= (gParam.chargeState - 1) || 
								(i <= 2 && !NTermTest) || (i <= 1 && NTermTest))
								{
									AddPeakToSpectrum(spectrum2, YionMass, fullIntensity);	/*add some width to multiply charged*/
									if(j > 1)
									{
										AddPeakToSpectrum(spectrum2, YionMass - 1, fullIntensity);
										AddPeakToSpectrum(spectrum2, YionMass + 1, fullIntensity);
									}
									AddPeakToSpectrum(spectrum2, YionMass - NH3/j, fullIntensity * NEUT_LOSS_ATT);
									if(j > 1)
									{
										AddPeakToSpectrum(spectrum2, YionMass - NH3/j - 1, fullIntensity * NEUT_LOSS_ATT);
										AddPeakToSpectrum(spectrum2, YionMass - NH3/j + 1, fullIntensity * NEUT_LOSS_ATT);
									}
									
									AddPeakToSpectrum(spectrum2, YionMass - H2O/j, fullIntensity * NEUT_LOSS_ATT);
									if(j > 1)
									{
										AddPeakToSpectrum(spectrum2, YionMass - H2O/j - 1, fullIntensity * NEUT_LOSS_ATT);
										AddPeakToSpectrum(spectrum2, YionMass - H2O/j + 1, fullIntensity * NEUT_LOSS_ATT);
									}
								}
								else
								{
									AddPeakToSpectrum(spectrum2, YionMass, fullIntensity * 1/i);	/*add some width to multiply charged*/
									if(j > 1)
									{
										AddPeakToSpectrum(spectrum2, YionMass - 1, fullIntensity * 1/(i+ 1));
										AddPeakToSpectrum(spectrum2, YionMass + 1, fullIntensity * 1/(i+ 1));
									}
									
									AddPeakToSpectrum(spectrum2, YionMass - NH3/j, fullIntensity * NEUT_LOSS_ATT);
									if(j > 1)
									{
										AddPeakToSpectrum(spectrum2, YionMass - NH3/j - 1, fullIntensity * 1/(i+ 1) * NEUT_LOSS_ATT);
										AddPeakToSpectrum(spectrum2, YionMass - NH3/j + 1, fullIntensity * 1/(i+ 1) * NEUT_LOSS_ATT);
									}
									
									AddPeakToSpectrum(spectrum2, YionMass - H2O/j, fullIntensity * NEUT_LOSS_ATT);
									if(j > 1)
									{
										AddPeakToSpectrum(spectrum2, YionMass - H2O/j - 1, fullIntensity * 1/(i+ 1) * NEUT_LOSS_ATT);
										AddPeakToSpectrum(spectrum2, YionMass - H2O/j + 1, fullIntensity * 1/(i+ 1) * NEUT_LOSS_ATT);
									}
								}
								
								YionMass = YionMass * 0.5;
	 							NH3 = NH3 * 0.5;
								H2O = H2O * 0.5;
							}
							else
							{
								YionMass = YionMass * 2;
	 							NH3 = NH3 * 2;
								H2O = H2O * 2;
								
								AddPeakToSpectrum(spectrum2, YionMass, fullIntensity * BAD_Y_ATT);
								AddPeakToSpectrum(spectrum2, YionMass - NH3/j, fullIntensity * BAD_Y_ATT * NEUT_LOSS_ATT);
								AddPeakToSpectrum(spectrum2, YionMass - H2O/j, fullIntensity * BAD_Y_ATT * NEUT_LOSS_ATT);

								YionMass = YionMass * 0.5;
	 							NH3 = NH3 * 0.5;
								H2O = H2O * 0.5;
	 						}	
						}
						if(proAttenuation == 0)
							exit(1);
						fullIntensity = fullIntensity / proAttenuation;
	 				}
				}
			}
		}
	}
	
	/* If the peptide is 4 residues or INT_4er, look for internal fragment ions. 
	*  (Only +1 right now).  Skip if LCQ data.*/
	fullIntensity = 50;
	if (seqLength > 4 && gParam.fragmentPattern != 'L') {
		for (i = 1; i < seqLength - 2; i++) {
			for (j = i+1; j < seqLength - 1; j++) 
			{
				if(j <= i + 3)
				{
					fragmentMass = gElementMass[HYDROGEN];
					for (k = i; k <= j; k++) fragmentMass += currScorePtr->peptide[k];
			
					if (fragmentMass < msms.scanMassHigh && fragmentMass < parent) 
					{
						if(fragmentMass > lowMassRange && fragmentMass < highMassRange)
						{
							fragmentMass = fragmentMass * 2;
						
							AddPeakToSpectrum(spectrum2, fragmentMass, fullIntensity * INT_FRAG_ATT);
						
							fragmentMass = fragmentMass * 0.5;
						}
					}
				}
			}
		}
	}
	
/*	Add the low mass immonium ions.*/
	for (i = 0; i < seqLength; i++) 
	{
		for(j = 0; j < gAminoAcidNumber; j++)
		{
			if(gNomMass[j] == currScorePtr->peptide[i])
			{
				if(lowMassIonMass[j] > lowMassRange)
				{
					lowMassIonMass[j] = lowMassIonMass[j] * 2;
					
					AddPeakToSpectrum(spectrum2, lowMassIonMass[j], fullIntensity * lowMassIonIntFactor[j]);
					
					lowMassIonMass[j] = lowMassIonMass[j] * 0.5;
				}
				
				break;
			}
		}
	}
	

/*	Wipe out region where the precursor and derivatives are located; these don't count.*/
	parent    = (gParam.peptideMW + (gParam.chargeState * gElementMass[HYDROGEN])) / gParam.chargeState;
	parentMinH2O = (gParam.peptideMW - H2O + (gParam.chargeState * gElementMass[HYDROGEN])) / gParam.chargeState;
	parentMinNH3 = (gParam.peptideMW - NH3 + (gParam.chargeState * gElementMass[HYDROGEN])) / gParam.chargeState;
	parentMin2H2O = (gParam.peptideMW - H2O - H2O + (gParam.chargeState * gElementMass[HYDROGEN])) / gParam.chargeState;
	parentMin2NH3 = (gParam.peptideMW - NH3 - NH3 + (gParam.chargeState * gElementMass[HYDROGEN])) / gParam.chargeState;
	
	parent = parent * 2;
	parentMinH2O = parentMinH2O * 2;
	parentMinNH3 = parentMinNH3 * 2;
	parentMin2H2O = parentMin2H2O * 2;
	parentMin2NH3 = parentMin2NH3 * 2;
	
	spectrum2[((INT_4)(parent + 0.5))] = 0;
	spectrum2[((INT_4)(parent + 0.5)) - 1] = 0;
	spectrum2[((INT_4)(parent + 0.5)) + 1] = 0;
	spectrum2[((INT_4)(parentMinH2O + 0.5))] = 0;
	spectrum2[((INT_4)(parentMinH2O + 0.5)) - 1] = 0;
	spectrum2[((INT_4)(parentMinH2O + 0.5)) + 1] = 0;
	spectrum2[((INT_4)(parentMinNH3 + 0.5))] = 0;
	spectrum2[((INT_4)(parentMinNH3 + 0.5)) - 1] = 0;
	spectrum2[((INT_4)(parentMinNH3 + 0.5)) + 1] = 0;
	spectrum2[((INT_4)(parentMin2H2O + 0.5))] = 0;
	spectrum2[((INT_4)(parentMin2H2O + 0.5)) - 1] = 0;
	spectrum2[((INT_4)(parentMin2H2O + 0.5)) + 1] = 0;
	spectrum2[((INT_4)(parentMin2NH3 + 0.5))] = 0;
	spectrum2[((INT_4)(parentMin2NH3 + 0.5)) - 1] = 0;
	spectrum2[((INT_4)(parentMin2NH3 + 0.5)) + 1] = 0;
	
	if(widePeak)
	{
		spectrum2[((INT_4)(parent + 0.5)) - 2] = 0;
		spectrum2[((INT_4)(parent + 0.5)) + 2] = 0;
		spectrum2[((INT_4)(parentMinH2O + 0.5)) - 2] = 0;
		spectrum2[((INT_4)(parentMinH2O + 0.5)) + 2] = 0;
		spectrum2[((INT_4)(parentMinNH3 + 0.5)) - 2] = 0;
		spectrum2[((INT_4)(parentMinNH3 + 0.5)) + 2] = 0;
		spectrum2[((INT_4)(parentMin2H2O + 0.5)) - 2] = 0;
		spectrum2[((INT_4)(parentMin2H2O + 0.5)) + 2] = 0;
		spectrum2[((INT_4)(parentMin2NH3 + 0.5)) - 2] = 0;
		spectrum2[((INT_4)(parentMin2NH3 + 0.5)) + 2] = 0;
	}
	
/*	Wipe out regions outside of scan range.*/
	for(i = 0; i < SIZEOF_SPECTRA; i++)
	{
		if(i < ((INT_4)msms.scanMassLow)*2 - 1)
		{
			spectrum2[i] = 0;
		}
		if(i > ((INT_4)msms.scanMassHigh)*2 + 1)
		{
			spectrum2[i] = 0;
		}
	}
	
	/*debug spectrum2*/
	/*if(currScorePtr->rank == 2)
	{
		printf("sequence spectrum \n");	
		for(i = 0; i < SIZEOF_SPECTRA; i++)	
		{
			if(spectrum2[i] != 0)
			{
				printf("%f  %f \n",(REAL_4)i/2,spectrum2[i]);
			}
		}
	}	*/
	
	
	/* Cross-correlation analysis */	
	CrossCorrelate(spectrum2-1, spectrum1-1,(UINT_4) SIZEOF_SPECTRA, tau-1);
		
	/* The cross-correlation score is tau[0] minus the mean of -75 < tau < 75.
	   tau[-1 to -75] are stored in wrapped around order at the end of tau.  */
/*	memcpy(&tau[76], &tau[SIZEOF_SPECTRA - 75], 75 * sizeof(REAL_4));
	intensityAccountedFor = 0.0;
	for (i = 0; i < 150; i++) {
		intensityAccountedFor += tau[i];
	}	
	currScorePtr->crossDressingScore = tau[0] - intensityAccountedFor/150;*/
	
	for(i = 0; i < SIZEOF_SPECTRA; i++)
	{
		if(tau[i] < 1)
		{
			tau[i] = 0;
		}
	}
	
	/*Since exact matches are exactly symmetrical, I no longer subtract out the average tau from
	-75 to 75, but instead subtract the sum of the differences between points that differ by a factor
	of -1.*/
	intensityAccountedFor = 0;
	for(i = 1; i < 250; i++)	/*compare +1/-1 up to +250/-250*/
	{
		tauDiff = tau[i] - tau[SIZEOF_SPECTRA - i];	/*tau(-250) minus tau(250), etc*/
		if(tauDiff < 0)
		{
			tauDiff = tauDiff * -1;	/*absolute value*/
		}
		intensityAccountedFor += tauDiff;	/*add it all up*/
	}
	currScorePtr->crossDressingScore = tau[0] - intensityAccountedFor/250;/*divide by 250*/
	
	if (spectrum2)	free(spectrum2);	spectrum2 = NULL;	
}
/**************************	AddPeakToSpectrum *******************************************
*
*  
*/
void AddPeakToSpectrum( REAL_4 *spectrum, REAL_4 mass, REAL_4 intensity) {

	char widePeak;
	
	if(intensity < 2)
		return;	/*don't sweat the small stuff*/
		
	if(gParam.fragmentErr <= 0.75)
	{
		widePeak = FALSE;	/*mock peak widths are 1.5 dalton*/
	}
	else
	{
		widePeak = TRUE;	/*mock peak widths are 2.5 daltons*/
	}
	
	/* Make sure that the mass is within the spectrum's range */
	if ( (INT_4)(mass + 0.5) > 2 && (INT_4)(mass + 0.5) < SIZEOF_SPECTRA - 2 ) {
		/* Set the intensity of the peak center */
		if (spectrum[(INT_4) (mass + 0.5)] < intensity)
			spectrum[(INT_4) (mass + 0.5)] = intensity;
			
		/* Set the intensity of the peak sides */
		if (spectrum[((INT_4) (mass + 0.5)) - 1] < intensity * gSidePeakAtt)
			spectrum[((INT_4) (mass + 0.5)) - 1] = intensity * gSidePeakAtt;
		if (spectrum[((INT_4) (mass + 0.5)) + 1] < intensity * gSidePeakAtt)
			spectrum[((INT_4) (mass + 0.5)) + 1] = intensity * gSidePeakAtt;

		if(widePeak)
		{
			/* Set the intensity of the peak side's side*/
			if (spectrum[((INT_4) (mass + 0.5)) - 2] < intensity * gSidePeakAtt * gSidePeakAtt)
				spectrum[((INT_4) (mass + 0.5)) - 2] = intensity * gSidePeakAtt * gSidePeakAtt;
			if (spectrum[((INT_4) (mass + 0.5)) + 2] < intensity * gSidePeakAtt * gSidePeakAtt)
				spectrum[((INT_4) (mass + 0.5)) + 2] = intensity * gSidePeakAtt * gSidePeakAtt;
		}
	}
}