File: scoring.c

package info (click to toggle)
sift 6.2.1-2
  • links: PTS, VCS
  • area: non-free
  • in suites: sid
  • size: 4,784 kB
  • sloc: ansic: 18,272; perl: 219; csh: 164; makefile: 152
file content (513 lines) | stat: -rwxr-xr-x 16,874 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
/* (C) Copyright 1993-9, Fred Hutchinson Cancer Research Center */
/* Use, modification or distribution of these programs is subject to */
/* the terms of the non-commercial licensing agreement in license.h. */

/* scoring.c: functions for different methods of scoring a sequence against */
/*            a matrix */
/* Written by: Bill Alford */
/* Change log information is at the end of the file. */

/*	system headers not in global.h  */
#include <math.h>
/*	blimps library headers */
#include <global.h>
#include <blocks.h>	/* includes sequences.h, output.h */
#include <matrix.h>	/* includes pattern.h */
#include <residues.h>
/*	headers in current directory */
#include "scoring.h"
#include "blimps.h"
#include "scores.h"
#include "lists.h"
/* #include <files.h>	*/	/* for DoHistogram */



/*
 * Exported variables and data structures
 */

double Alignments_Done = 0.0;
double Scores_Done = 0.0;
int histogram[NUM_HIST_COLS];  /* according to ansii C these initialize as 0 */
Boolean DoHistogram = FALSE;

/*
 * Local variables and data structures
 */

static void enter_score();
static Score *make_score();


/*
 * Function definitions
 */

/*************************************************************************
 *************************************************************************
 *                                                                       *
 *                    I M P O R T A N T   N O T E ! ! !                  *
 *                                                                       *
 *      All scoring methods need to check if UsePatterns is set.         *
 *      If it is set, the scoring method must only score the first       *
 *      full alignment.  eg pos. 1- pos. 1, 2-2, 3-3, etc...             *
 *                                                                       *
 *************************************************************************
 *************************************************************************
 */
/*
compile block ! ! ! @ # $ % ^ &* * ( ) _ + @^ *#&^$ 
stuck...
Need to decide if the repeats is needed for the scoring of patterns
  If not, continue with the current stuff, otherwise, back to the chalkboard

Note, currently planning on using the optional integer field in the sequence
structure to carry the information on where the pattern matching is to start.
There will also be another parameter to tell if there is pattern matching
involved (note, this invalidates the above comment note).
*/
/* most recent approach is to tell the scoring method if it is a */
/* patterns search.  If it is then we treat it as a score all repeats. */
/* The method to try for not scoring repeats is this.  Make the */
/* scoring return info about the score if it was a patterns search. */
/* This way, since the patterns score is only a single score, the */
/* calling function can keep track of the scores and enter only the */
/* best one into the scores list.  Not that it will need to keep track */
/* of the other information needed also. */


/*
 * default_scoring_method
 *   The default scoring method.  Basically a score is the sum of the scores 
 *   in the matrix at each position-amino acid pair for one alignment.
 *   Parameters:
 *     Sequence *seq: the sequence to score
 *     Matrix *matrix: the matrix used for scoring
 *     int frame: the frame we are looking at (0 = aa seq.,
 *                1, 2, 3, -1, -2, -3 = na seqs.)
 *     Boolean repeats: enter repeats into the score_list?
 *     int search_type: the type of search we are doing.  If it is a 
 *                      SEARCH_TYPE_MATRIX search (block vs sequences), 
 *                      do NOT divide by the block percentile.  If it is a 
 *                      SEARCH_TYPE_BLOCK search (sequence vs blocks), DO 
 *                      divide by the block percentile. 
 *     Boolean pattern_search: whether or not to use the pattern search stuff
 *   Error codes: none
 */

void default_scoring_method(seq, matrix, frame, repeats, search_type,
			    pattern_search) 
     Sequence *seq;
     Matrix *matrix;
     int frame;
     Boolean repeats;
     int search_type;
     Boolean pattern_search;
{
  int aa, mat_pos, seq_pos;
  int matrix_length, seq_length;
  int scan_pos;
  double max_aa_score, max_score;
  double seq_score=0, max_seq_score=0;
  int best_position=0;
  Boolean seen_pattern;

  /* get the lengths, gets rid of the indirrection */
  matrix_length = matrix->width;
  seq_length    = seq->length;
  
  /* Make sure these lengths are > 0 */
  if (matrix_length <= 0) {
    sprintf(ErrorBuffer,
	    "default_scoring_method(): matrix %s has length <= 0",
		matrix->number);
    ErrorReport(PROGRAM_ERR_LVL);
    sprintf(ErrorBuffer,
	    "default_scoring_method(): Not scoring the sequence.\n");
    ErrorReport(PROGRAM_ERR_LVL);
    return;
  }
  if (seq_length <= 0) {
    sprintf(ErrorBuffer,
      "default_scoring_method(): sequence %s has length (amino acids) <= 0",
		seq->name);
    ErrorReport(PROGRAM_ERR_LVL);
    sprintf(ErrorBuffer,
	    "default_scoring_method(): Not scoring the sequence.\n");
    ErrorReport(PROGRAM_ERR_LVL);
    return;
  }

  /* compute the maximum possible score for this matrix */
  /* this is used in standardizing the scores in a BLOCK search with a  */
  /* block without a percentile ranking (only calulate max_score if this */
  /* is the case) */
  max_score = 0.0;
  if ((search_type == SEARCH_TYPE_BLOCK) &&
      (matrix->percentile <= 0)) {
    for (mat_pos=0; mat_pos < matrix_length; mat_pos++) {
      max_aa_score = 0.0;
      for (aa=0; aa < MATRIX_AA_WIDTH; aa++) {
	if (matrix->weights[aa][mat_pos] > max_aa_score) {
	  max_aa_score = matrix->weights[aa][mat_pos];
	}
      }
      max_score = max_score + max_aa_score;
    }
  }

  

  /* 
   * score the matrix vs the sequence for each pairwise line up 
   */
  max_seq_score = 0.0;
  
  /* for every alignment of the matrix and sequence score the alignment. */
  /* there are (seq_length + matrix_length  - 1) different alignments. */
  /* Note: seq_pos is the relative position of the 1st matrix column to the */
  /*       1st sequence column */
  /* Note: the indexing is shifted to make the calculation of scan_pos */
  /*       (see below) easier/faster */
  seen_pattern = FALSE;
  for (seq_pos= -matrix_length+1; seq_pos < seq_length; seq_pos++) 
  {
    if (pattern_search) {
      /* This is a pattern!  Only score the first full alignment. */
      if (seen_pattern) {
	break; /* break out of the for loop.  I know it is a hack, but this *
		* does the least damage to the other logic. */
      }
      else {
	seen_pattern = TRUE; /* stop after this one time through the loop */
	seq_pos = seq->undefined;
      }
    }
    

    /* 
     * score this alignment
     */
    seq_score = 0.0;

    /* for each position in the matrix add the weight to the total score */
    /* Note: mat_pos is the current column of the matrix */
    for (mat_pos=0; mat_pos < matrix_length; mat_pos++) 
    {
      /* make sure the sequence and the matrix overlap at this point */
      /* Note: scan_pos is where the current matrix column is in the */
      /*       sequence */
      scan_pos = seq_pos + mat_pos;
      if ((scan_pos >= 0) && (scan_pos < seq_length)) { /* if in the seq */
	seq_score += matrix->weights[seq->sequence[scan_pos]][mat_pos];
      }
      else {		/* not in the sequence, score as gap */
	seq_score += matrix->weights[aa_atob['-']][mat_pos]; 
      }
      
    } /* end of for mat_pos: score this alignment */
    Alignments_Done += 1.0;
    
    /*
     * adjust the score if it is a SEARCH_TYPE_BLOCK search 
     */
    if (search_type == SEARCH_TYPE_BLOCK) {
      if (matrix->percentile > 0) {
	seq_score = (int) ((seq_score/matrix->percentile ) 
			   * 1000 );
      }
      else {
	seq_score = (int) (((seq_score/max_score) 
			    * log10((double) matrix->width))
			   * 1000 );
      }
    } /* end adjust score */

    if (seq_score > max_seq_score) {
	max_seq_score = seq_score;
	best_position = seq_pos;
    }

    /*   Save all scores for repeats */   
    if (repeats && (!SavedScoresFlag || seq_score >= MIN_SAVE_SCORE) )
    {
         enter_score((int)seq_score, seq_pos, frame, matrix, seq);
    }
  } /* end of for seq_pos */ 
    
  /*
   * if repeats were not allowed then save the best score seen
   */
 if (!repeats && (!SavedScoresFlag || seq_score >= MIN_SAVE_SCORE) )
 {
    enter_score((int)max_seq_score, best_position, frame, matrix, seq);
 }
  
}  /*  end of default_scoring_method */


/*
 * enter_score
 *   takes the data for a score entry and enters it into the list if it is
 *   ok to enter it. Note that not entering the score if it will be deleted 
 *   anyway saves time with the allocating and deallocating of score records.
 *   Parameters:
 *     int score:          the score
 *     int position:       the position
 *     int frame:          the frame we are looking at 
 *                             (0 = aa seq., 1, 2, 3, -1, -2, -3 na seqs.)
 *     Matrix *matrix:     the matrix
 *     Sequence *sequence: the sequence
 *   Error codes: none
 */

static void enter_score(score, position, frame, 
			matrix, sequence)
     int score;
     int position;
     int frame;
     Matrix *matrix;
     Sequence *sequence;
{  
  int result;

  /* increase the number of scores done */
  Scores_Done += 1.0;

  /* enter into the histogram array if keeping track */
  if (DoHistogram) {
    histogram[min((max(0, (int)score/HIST_BOX_SIZE)), (NUM_HIST_COLS-1))]++;
  }

  /* if score is worth entering into the list, enter it */
  /*   if the number of scores in the list is not yet to the max, we */
  /*   still need to enter the score, no matter what */
  if (!(score < MinScoreOfList) || 
      ((NumberToReport >= 0) && 
       (NumInSL(Scores) < NumberToReport))) {
    /* save score into score_list */
    result = 
      insert_in_score_list(make_score(score, position, frame, 
				      matrix, sequence));
    if (result == FALSE) {
      sprintf(ErrorBuffer, 
	      "enter_score(): Error placing matrix score into list\n");
      ErrorReport(PROGRAM_ERR_LVL);
    }
  }
}



/*
 * make_score
 *   allocates space for the structure that contains the score, sequence
 *   position matrix, and sequence information to be added to the Scores list.
 *   Parameters:
 *     int score:          the score
 *     int position:       the position
 *     int frame:          the frame we are looking at 
 *                             (0 = aa seq., 1, 2, 3, -1, -2, -3 na seqs.)
 *     Matrix *matrix:     the matrix
 *     Sequence *sequence: the sequence
 *   Return codes: returns a ScoreListEntry to be used later.
 *   Error codes:
 */

static Score *make_score(score, position, frame, matrix, sequence)
     int score;
     int position;
     int frame;
     Matrix *matrix;
     Sequence *sequence;
{  
  int block_length;		/* the length of the block */
  int seq_length;		/* the length of the sequence */
  int block_pos;		/* the position in the block */
  int scan_pos;			/* where we are related to block[0] */
  int seq_loop;			/* looping index through all the sequences */
				/* in the block*/
  char *consensus;
  Residue block_res, seq_res;	/* temporary places for the residues that */
				/* are being compared */
  Block *block;

  Score *new_score;

  CheckMem(
	   new_score = (Score *) malloc(sizeof(struct score_struct))
	   );

  new_score->score = score;
  new_score->strength = matrix->strength;
  new_score->position = position;
  new_score->seq_length = sequence->length;
  new_score->frame = frame;
  strncpy(new_score->sequence_number, sequence->name, NUMBER_WIDTH);
  strncpy(new_score->sequence_desc, sequence->info, DESC_WIDTH);
  if (matrix->block != NULL) {
    /* there is a block to use */
    strncpy(new_score->matrix_number, matrix->block->number, NUMBER_WIDTH);
    strncpy(new_score->matrix_desc, matrix->block->de, DESC_WIDTH);
  }
  else {			
    /* no block to use */
    strncpy(new_score->matrix_number, matrix->number, NUMBER_WIDTH);
    strncpy(new_score->matrix_desc, matrix->de, DESC_WIDTH);
  }

  /* if there is a block to look at and use to make the consensus sequence */
  /* then make the consensus sequence */
  /* otherwise, just use the sequence segment as the consensus    JGH*/
  consensus = new_score->consensus;
  seq_length = sequence->length;
  
  if (matrix->block != NULL) {
    block = matrix->block;

    block_length = block->width;
    
    /* for each position in the block figure out what case to use in the */
    /* consensus sequence to be printed out */
    /* Note: block_pos is the current column of the block */
    for (block_pos=0; block_pos < min(block_length, DESC_WIDTH); block_pos++) {

      /* make sure the sequence and the block overlap at this point */
      /* Note: scan_pos is where the current block column is in the */
      /*       sequence */
      scan_pos = position + block_pos;
      if (scan_pos < 0) {	/* before the sequence */
	consensus[block_pos] = ' ';
      }
      else if ((scan_pos >= 0) && (scan_pos < seq_length)) { /* in the seq */
	/*
	 * check for a consensus 
	 */
	consensus[block_pos] = tolower(aa_btoa[sequence->sequence[scan_pos]]);
	/* for every sequence in the block, check the residue.  if there */
	/* is one that is the same as the sequence, then change the case */
	seq_res = sequence->sequence[scan_pos];
	for (seq_loop=0; seq_loop< block->num_sequences; seq_loop++) {
	  block_res = block->sequences[seq_loop].sequence[block_pos];
	  if (seq_res == block_res) {
	    consensus[block_pos] = toupper(aa_btoa[seq_res]);
	    break;
	  }
	  /* if it is an ambiguity code, change the case */
	  else if (((seq_res == aa_atob['B']) &&
		    ((block_res == aa_atob['D']) || 
		     (block_res == aa_atob['N']))) ||
		   ((seq_res == aa_atob['Z']) &&
		    ((block_res == aa_atob['E']) || 
		     (block_res == aa_atob['Q']))) ||
		   ((block_res == aa_atob['B']) &&
		    ((seq_res == aa_atob['D']) || 
		     (seq_res == aa_atob['N']))) ||
		   ((block_res == aa_atob['Z']) &&
		    ((seq_res == aa_atob['E']) || 
		     (seq_res == aa_atob['Q'])))) {
	    consensus[block_pos] = toupper(aa_btoa[seq_res]);
	    break;
	  }
	}
	/* make sure that all X's are lower case */
	if (seq_res == aa_atob['X']) {
	  consensus[block_pos] = tolower(aa_btoa[seq_res]);
	}
      }
      else {			/* after the sequence */
	break;			/* don't need to keep going */
      }
    }
    consensus[block_pos] = '\0';
  }
  else { /* no block to make a consensus sequence from */
 
    block_length = matrix->width;
     
    /* for each position in the block figure out what case to use in the */
    /* consensus sequence to be printed out */
    /* Note: block_pos is the current column of the block */
    for (block_pos=0; block_pos < min(block_length, DESC_WIDTH); block_pos++) {
 
      /* make sure the sequence and the block overlap at this point */
      /* Note: scan_pos is where the current block column is in the */
      /*       sequence */
      scan_pos = position + block_pos;
      if (scan_pos < 0) {	/* before the sequence */
 	consensus[block_pos] = ' ';
      }
      else if ((scan_pos >= 0) && (scan_pos < seq_length)) { /* in the seq */
 	/*
 	 * copy the sequence as the "consensus"
 	 */
 	consensus[block_pos] = tolower(aa_btoa[sequence->sequence[scan_pos]]);
 	/* for every sequence in the block, check the residue.  if there */
 	/* is one that is the same as the sequence, then change the case */
      }
      else {			/* after the sequence */
 	break;			/* don't need to keep going */
      }
    }
    consensus[block_pos] = '\0';
  }

  return new_score;
}




/* 
 * print_histogram
 *   ouputs the histogram array from scoring.[ch] to the output stream ofp.
 *   Parameters:
 *     FILE *ofp: the output file pointer
 *   Return codes: none
 *   Error codes: none
 */

void print_histogram(ofp)
     FILE *ofp;
{
  int i, maxi;

  /*  Find highest non-empty cell   JGH */
  maxi = NUM_HIST_COLS - 1;
  while (histogram[maxi] == 0) {
    maxi--;
  }

  fprintf(ofp, "Histogram:\n");
/*  fprintf(ofp, "         < %-5d : %d\n", HIST_BOX_SIZE, histogram[0]);*/

  for (i=0; i<=maxi; i++) {
    fprintf(ofp, "   %5d - %-5d : %d\n", i*HIST_BOX_SIZE, 
	    (i+1)*HIST_BOX_SIZE-1,
	    histogram[i]);
  }

  fprintf(ofp, "   %5d -       : %d\n", 
	  (i)*HIST_BOX_SIZE, 
	  0);
  fprintf(ofp, "\n");
}





/* Change log information follows. */
/*
  Changes since version 3.3.1:
  10/ 2/99 Added includes for more standard C libraries.
  Changes since version 3.2.5:
  5/25/99 Scores_Done & Alignments_Done changed to double.
  2/3/99  default_scoring_method():
          If SavedScoresFlag, don't save scores < MIN_SAVE_SCORE
  Changes since version 3.1:
  1/16/97  Clarified error message. JGH
  Changes since version 3.0.0:
  3/25/96  Print out sequence name if error reading db.  JGH
*/