~ubuntu-branches/ubuntu/edgy/ncbi-tools6/edgy

« back to all changes in this revision

Viewing changes to algo/blast/composition_adjustment/compo_mode_condition.c

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-07-19 23:28:07 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060719232807-et3cdmcjgmnyleyx
Tags: 6.1.20060507-3ubuntu1
Re-merge with Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
/**
26
26
 * @file compo_mode_condition.c
27
 
 *
28
 
 * Authors: Alejandro Schaffer, Yi-Kuo Yu
29
 
 *
30
27
 * Functions to test whether conditional score matrix adjustment
31
28
 * should be applied for a pair of matching sequences.
 
29
 *
 
30
 * Authors: Alejandro Schaffer, Yi-Kuo Yu
32
31
 */
33
32
 
34
33
#ifndef SKIP_DOXYGEN_PROCESSING
35
34
static char const rcsid[] =
36
 
    "$Id: compo_mode_condition.c,v 1.5 2005/12/01 13:49:43 gertz Exp $";
 
35
    "$Id: compo_mode_condition.c,v 1.10 2006/05/03 14:09:52 gertz Exp $";
37
36
#endif /* SKIP_DOXYGEN_PROCESSING */
38
37
 
39
38
#include <algo/blast/core/ncbi_std.h>
41
40
#include <algo/blast/composition_adjustment/compo_mode_condition.h>
42
41
#include <algo/blast/composition_adjustment/matrix_frequency_data.h>
43
42
 
44
 
/* 180 degrees in half a circle */
 
43
/** 180 degrees in half a circle */
45
44
#define HALF_CIRCLE_DEGREES 180
46
 
/* some digits of PI */
 
45
/** some digits of PI */
47
46
#define PI 3.1415926543
48
 
/* thresholds used to determine which composition mode to use */
 
47
/** @{ thresholds used to determine which composition mode to use */
49
48
#define QUERY_MATCH_DISTANCE_THRESHOLD 0.16
50
49
#define LENGTH_RATIO_THRESHOLD 3.0
51
50
#define ANGLE_DEGREE_THRESHOLD 70.0
52
 
 
53
 
 
54
 
/* type of function used to choose a mode for composition-based
 
51
#define HIGH_PAIR_THRESHOLD 0.4
 
52
#define LENGTH_LOWER_THRESHOLD 50
 
53
/** @} */
 
54
 
 
55
/** type of function used to choose a mode for composition-based
55
56
 * statistics. The variables are Queryseq_length, Matchseq_length,
56
57
 * query_amino_count, match_amino_account and matrix_name.*/
57
 
typedef ECompoAdjustModes
 
58
typedef EMatrixAdjustRule
58
59
(*Condition) (int, int, const double *, const double *,
59
60
              const char *);
60
61
 
61
62
 
62
 
/* A function used to choose a mode for composition-based statistics.
 
63
/** Return true if length > 50 and the two most frequent letters
 
64
 * occur a total of more that 40% of the time. */
 
65
static int
 
66
s_HighPairFrequencies(const double * letterProbs, int length)
 
67
{
 
68
    int i; /*index*/
 
69
    double max, second; /*two highest letter probabilities*/
 
70
 
 
71
    if (length <= LENGTH_LOWER_THRESHOLD) {
 
72
        return FALSE;
 
73
    }
 
74
    max = 0;
 
75
    second = 0;
 
76
    for (i = 0;  i < COMPO_NUM_TRUE_AA;  i++) {
 
77
        if (letterProbs[i] > second) {
 
78
            second = letterProbs[i];
 
79
            if (letterProbs[i] > max) {
 
80
                second = max;
 
81
                max = letterProbs[i];
 
82
            }
 
83
        }
 
84
    }
 
85
    return (max + second) > HIGH_PAIR_THRESHOLD;
 
86
}
 
87
 
 
88
/**
 
89
 * Return true if either the query or the matching sequences
 
90
 * passes the test in s_HighPairFrequencies. */
 
91
static int
 
92
s_HighPairEitherSeq(const double * P_query, int length1,
 
93
                    const double * P_match, int length2)
 
94
{
 
95
    int result1, result2;
 
96
 
 
97
    result1 = s_HighPairFrequencies(P_query, length1);
 
98
    result2 = s_HighPairFrequencies(P_match, length2);
 
99
 
 
100
    return result1 || result2;
 
101
}
 
102
 
 
103
 
 
104
/** Return eDontAdjustMatrix unconditionally */
 
105
static EMatrixAdjustRule
 
106
s_NeverAdjustMatrix(int Len_query, int Len_match,
 
107
                  const double * P_query, const double * P_match,
 
108
                  const char *matrix_name)
 
109
{
 
110
    /* Suppress unused variable warnings */
 
111
    (void) Len_query;
 
112
    (void) Len_match;
 
113
    (void) P_query;
 
114
    (void) P_match;
 
115
    (void) matrix_name;
 
116
 
 
117
     return eDontAdjustMatrix;
 
118
}
 
119
 
 
120
 
 
121
/** Return eCompoScaleOldMatrix unconditionally */
 
122
static EMatrixAdjustRule
 
123
s_JustScaleOldMatrix(int Len_query, int Len_match,
 
124
                     const double * P_query, const double * P_match,
 
125
                     const char *matrix_name)
 
126
{
 
127
    /* Suppress unused variable warnings */
 
128
    (void) Len_query;
 
129
    (void) Len_match;
 
130
    (void) P_query;
 
131
    (void) P_match;
 
132
    (void) matrix_name;
 
133
 
 
134
    return eCompoScaleOldMatrix;
 
135
}
 
136
 
 
137
 
 
138
/** A function used to choose a mode for composition-based statistics.
63
139
 * If this function is used relative-entropy score adjustment is
64
140
 * always applied, with a fixed value as the target relative entropy*/
65
 
static ECompoAdjustModes
66
 
TestToApplyREAdjustmentUnconditional(int Len_query,
67
 
                                     int Len_match,
68
 
                                     const double * P_query,
69
 
                                     const double * P_match,
70
 
                                     const char *matrix_name)
 
141
static EMatrixAdjustRule
 
142
s_TestToApplyREAdjustmentUnconditional(int Len_query,
 
143
                                       int Len_match,
 
144
                                       const double * P_query,
 
145
                                       const double * P_match,
 
146
                                       const char *matrix_name)
71
147
{
72
148
    /* Suppress unused variable warnings */
73
149
    (void) Len_query;
86
162
 * based on lengths and letter counts of the two matched sequences;
87
163
 * matrix_name is the underlying score matrix; for now only BLOSUM62
88
164
 * is supported */
89
 
static ECompoAdjustModes
90
 
TestToApplyREAdjustmentConditional(int Len_query,
91
 
                                   int Len_match,
92
 
                                   const double * P_query,
93
 
                                   const double * P_match,
94
 
                                   const char *matrix_name)
 
165
static EMatrixAdjustRule
 
166
s_TestToApplyREAdjustmentConditional(int Len_query,
 
167
                                     int Len_match,
 
168
                                     const double * P_query,
 
169
                                     const double * P_match,
 
170
                                     const char *matrix_name)
95
171
{
96
 
    ECompoAdjustModes mode_value; /* which relative entropy mode to
 
172
    EMatrixAdjustRule which_rule; /* which relative entropy mode to
97
173
                                     return */
98
174
    int i;                       /* loop indices */
99
175
    double p_query[COMPO_NUM_TRUE_AA];
142
218
        len_large = len_m;
143
219
        len_small = len_q;
144
220
    }
145
 
    if ((D_m_q > QUERY_MATCH_DISTANCE_THRESHOLD) &&
 
221
    if (s_HighPairEitherSeq(P_query, Len_query, P_match, Len_match)) {
 
222
        which_rule = eUserSpecifiedRelEntropy;
 
223
    } else {
 
224
      if ((D_m_q > QUERY_MATCH_DISTANCE_THRESHOLD) &&
146
225
        (len_large / len_small > LENGTH_RATIO_THRESHOLD) &&
147
226
        (angle > ANGLE_DEGREE_THRESHOLD)) {
148
 
        mode_value = eCompoKeepOldMatrix;
149
 
    } else {
150
 
        mode_value = eUserSpecifiedRelEntropy;
 
227
        which_rule = eCompoScaleOldMatrix;
 
228
      } else {
 
229
        which_rule = eUserSpecifiedRelEntropy;
 
230
      }
151
231
    }
152
 
    return mode_value;
 
232
    return which_rule;
153
233
}
154
234
 
155
235
 
157
237
 * An array of functions that can be used to decide which optimization
158
238
 * formulation should be used for score adjustment */
159
239
static Condition Cond_func[] = {
160
 
    TestToApplyREAdjustmentConditional,
161
 
    TestToApplyREAdjustmentUnconditional,
 
240
    s_NeverAdjustMatrix,
 
241
    s_JustScaleOldMatrix,
 
242
    s_TestToApplyREAdjustmentConditional,
 
243
    s_TestToApplyREAdjustmentUnconditional,
162
244
    NULL
163
245
};
164
246
 
165
247
 
166
 
/**
167
 
 * Choose how the relative entropy should be constrained based on
168
 
 * properties of the two sequences to be aligned.
169
 
 *
170
 
 * @param length1     length of the first sequence
171
 
 * @param length2     length of the second sequence
172
 
 * @param probArray1  arrays of probabilities for the first sequence, in
173
 
 *                    a 20 letter amino-acid alphabet
174
 
 * @param probArray2  arrays of probabilities for the other sequence
175
 
 * @param matrixName  name of the scoring matrix
176
 
 * @param testFunctionIndex    allows different rules to be tested
177
 
 *                             for the relative entropy decision.
178
 
 */
179
 
ECompoAdjustModes
180
 
Blast_ChooseCompoAdjustMode(int length1,
181
 
                            int length2,
182
 
                            const double * probArray1,
183
 
                            const double * probArray2,
184
 
                            const char *matrixName,
185
 
                            int testFunctionIndex)
 
248
/* Documented in compo_mode_condition.h. */
 
249
EMatrixAdjustRule
 
250
Blast_ChooseMatrixAdjustRule(int length1,
 
251
                             int length2,
 
252
                             const double * probArray1,
 
253
                             const double * probArray2,
 
254
                             const char *matrixName,
 
255
                             ECompoAdjustModes composition_adjust_mode)
186
256
{
 
257
    int testFunctionIndex = (int) composition_adjust_mode;
 
258
 
187
259
    return
188
260
        Cond_func[testFunctionIndex] (length1,    length2,
189
261
                                      probArray1, probArray2, matrixName);