| 1 | /************************************************************************** |
| 2 | * This file is part of A2Amapper. |
| 3 | * Copyright (c) 2004 Applera Corporation |
| 4 | * Author: Clark Mobarry |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received (LICENSE.txt) a copy of the GNU General Public |
| 17 | * License along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | **************************************************************************/ |
| 20 | |
| 21 | |
| 22 | #ifndef CA_ALN_local_h |
| 23 | #define CA_ALN_local_h |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | |
| 28 | inline |
| 29 | void * |
| 30 | ckalloc(size_t size) { |
| 31 | void *newp = NULL; |
| 32 | assert(size>0); |
| 33 | assert(NULL != (newp = malloc(size))); |
| 34 | return(newp); |
| 35 | } |
| 36 | |
| 37 | inline |
| 38 | void * |
| 39 | ckrealloc(void* ptr, size_t size) { |
| 40 | void *newp = NULL; |
| 41 | assert(size>0); |
| 42 | assert(NULL != (newp = realloc(ptr, size))); |
| 43 | return(newp); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | /* O(kn) identity-based alignment algorithm. Find alignment between |
| 52 | a and b (of lengths alen and blen), that begins at finishing |
| 53 | boundary position *spnt. Return at *spnt the diagonal at which the |
| 54 | alignment starts. |
| 55 | */ |
| 56 | int *AS_ALN_OKNAlign(const char *a, int alen, |
| 57 | const char *b, int blen, |
| 58 | int *spnt, |
| 59 | int diff); |
| 60 | |
| 61 | |
| 62 | |
| 63 | /* Local alignment record: |
| 64 | Coordinates are in terms of the d.p. matrix that go from (0,0) |
| 65 | to (|A|,|B|), where A is the A-sequence argument and B is the |
| 66 | B-sequence argument. A coordinate is a position between chars |
| 67 | of the sequence. For example, an alignment from (3,5) to (6,9) |
| 68 | aligns characters 4-5 of A with characters 7-9 of B. |
| 69 | |
| 70 | If the B start coordinate is greater than the B end coordinate |
| 71 | then the alignment is of A versus the complement of B, i.e. the |
| 72 | alignment runs along an anti-diagonal, not a diagonal. |
| 73 | |
| 74 | Diagonal k is the set of coordinates (a,b) s.t. a-b = k. |
| 75 | Anti-diagonal k is the set of coordinates (a,b) s.t. a+b = k. |
| 76 | */ |
| 77 | |
| 78 | typedef struct { |
| 79 | int abpos, bbpos; /* Start coordinate of local alignment */ |
| 80 | int aepos, bepos; /* End coordinate of local alignment */ |
| 81 | int ldiag, hdiag; /* Alignment is between (anti)diagonals ldiag & hdiag */ |
| 82 | int score; /* Score of alignment where match = 1, difference = -3 */ |
| 83 | double error; /* Lower bound on error rate of match */ |
| 84 | } Local_Segment; |
| 85 | |
| 86 | #define LOCAL_FORW 0 /* Compare A to B only */ |
| 87 | #define LOCAL_REVR 1 /* Compare A to complement(B) only */ |
| 88 | #define LOCAL_BOTH 2 /* Compare A to both B and its complement */ |
| 89 | |
| 90 | /* Find_Local_Segments compares sequence A of length Alen against sequence B |
| 91 | of length Blen, in the same, opposite, or both orientations depending |
| 92 | on the setting of Action, and returns a pointer to an array of local |
| 93 | alignment records, where the number of such records is in the integer |
| 94 | pointed at by NumSegs. Find_Local_Segments only reports alignments that are |
| 95 | longer than MinLen bps. and contain less than MaxDiff errors as a fraction |
| 96 | of the alignment's length. Find_Local_Segments reuses the storage for the |
| 97 | array of local alignment segments with each successive call, a user should |
| 98 | copy the array if they which for it to persist beyond a given invocation. |
| 99 | |
| 100 | Find_Local_Segments finds alignments that contain at least 36bp that match |
| 101 | at 95% or better. The encompassing local alignment has to match at |
| 102 | about 75% or better as matching chars are scored SAMECOST (1) and |
| 103 | differences are scored -DIFFCOST (3). Extension of a local alignment |
| 104 | in a given direction ends at a cumulative maximum from which all |
| 105 | extensions drop by BLOCKCOST (15 = DIFFCOST(3)*MAXIGAP(5)) in score, |
| 106 | the equivalent of MAXIGAP(5) consecutive differences. |
| 107 | |
| 108 | Find_Local_Segments is most efficiently applied to large sequences. Any |
| 109 | application that is doing an all-against-all of smaller fragment |
| 110 | sequences, would best utilize Find_Local_Segments by concatenating the |
| 111 | fragments, applying Find_Local_Segments to the large concatenations, and |
| 112 | then mapping the local alignments back to the fragments and |
| 113 | coordinates to which they pertain. By separating fragments in the |
| 114 | concatentaion by MAXIGAP+1(6) N's, one guarantees that local alignments |
| 115 | do not span the boundaries between fragment sequences. |
| 116 | |
| 117 | Find_Local_Segments builds an index of the A sequence as part of its |
| 118 | acceleration method. If successive calls to Find_Local_Segments involve |
| 119 | the same A-sequence, this table is built only once, improving |
| 120 | efficiency. |
| 121 | */ |
| 122 | |
| 123 | Local_Segment *Find_Local_Segments |
| 124 | (char const * const A, int const Alen, |
| 125 | char const * const B, int const Blen, |
| 126 | int const Action, |
| 127 | int const MinLen, double const MaxDiff, int * const Seglen); |
| 128 | |
| 129 | /* Local_Overlap Record: |
| 130 | A local overlap is a chain of Local_Segments computed by |
| 131 | Find_Local_Segments that when strung together form an overlap between |
| 132 | the two sequences involved. The Local_Overlap record contains the |
| 133 | the number of segments, a pointer to an ordered array of the segments |
| 134 | in the chain, and the following parameters: |
| 135 | score: The score of a chain is the total number of indels required |
| 136 | to build an overlap out of the chained elements. |
| 137 | begpos,endpos: As for DP_Compare, the diagonals on which the overlap |
| 138 | begins and ends. |
| 139 | diffs: The number of substitutions and indels required to build an |
| 140 | overlap out of the chained elements. |
| 141 | length: (|A-seg|+|B-seg|)/2. |
| 142 | The field chain points to an array of num_pieces+1 Local_Chain records. |
| 143 | Records 0..num_pieces encode the nature of the gap between segments so |
| 144 | that record 0 gives the gap to the start border (if any), record |
| 145 | num_pieces gives the gap to the finish border (if any), and record i |
| 146 | gives the gap between the segment of record i-1 and record i. Record |
| 147 | num_pieces does not contain a segment description. The parameters |
| 148 | agap and bgap give the delta in the A- and B-coordinates if of the |
| 149 | end of the previous segment and the start of the next one. The |
| 150 | coordinates can be negative and both can be zero only for the boundary |
| 151 | gaps (first and last). Each segment is identified by its position in |
| 152 | the array of segments passed to the routine, and if the segment was |
| 153 | complemented in order to form part of the chain, then and only then is |
| 154 | the field reversed set to a non-zero value. The type field gives an |
| 155 | indication of the type of the gap as follows: |
| 156 | |
| 157 | LOCAL_BOUNDARY -- if both agap and bgap are zero at a boundary gap |
| 158 | then this indication is given. |
| 159 | LOCAL_MINOR -- if the a- and b-gaps are less than a user-supplied |
| 160 | limit "MinorThresh", then the gap is considered a |
| 161 | minor break between two segments of similarity. |
| 162 | LOCAL_INDEL -- if the gap in one sequence is minor, but major, positive, |
| 163 | and at least 4 times as large in the other, then the |
| 164 | gap is considered an indel. |
| 165 | LOCAL_REPEAT -- if the gap in one sequence is minor or negative, but |
| 166 | major and negative then the gap is considered a repeat |
| 167 | gap in the sense that a tandem repeat must occur in |
| 168 | one or both of the sequences around the junction between |
| 169 | the two adjacent segments. |
| 170 | LOCAL_REPnDEL -- if the gap in one sequence is major and negative, and |
| 171 | the other is major and positive, then there is a |
| 172 | repeated element on both sides of the sequence with |
| 173 | the inserted sequence. |
| 174 | LOCAL_DISAGREE -- anything else, i.e. both gap deltas are positive, |
| 175 | at least one is major, and if the other is minor |
| 176 | then the ration is less than 1 to 4. |
| 177 | */ |
| 178 | |
| 179 | #define LOCAL_BOUNDARY 0x0 /* No gap, at boundary */ |
| 180 | #define LOCAL_MINOR 0x1 /* Small break in alignment */ |
| 181 | #define LOCAL_DISAGREE 0x2 /* The two sequences significantly disagree */ |
| 182 | #define LOCAL_INDEL 0x3 /* One sequence has missing/added sequence */ |
| 183 | #define LOCAL_REPEAT 0x4 /* A tandem repeat occurs at the junction */ |
| 184 | #define LOCAL_REPnDEL 0x5 /* Both a tandem repeat and an indel */ |
| 185 | |
| 186 | typedef struct { |
| 187 | int agap, bgap; /* A- and B-seq deltas from last segment to this one */ |
| 188 | short type; /* Type of gap as given by the defined cons. above */ |
| 189 | short reversed; /* Is segment reversed for inclusion in chain */ |
| 190 | Local_Segment piece; /* Segment in the chain */ |
| 191 | } Local_Chain; |
| 192 | |
| 193 | typedef struct { |
| 194 | int begpos; /* Entry diagonal of boundary point (a,b) on which |
| 195 | overlap starts, where diagonal = a - b. */ |
| 196 | int endpos; /* Exit diagonal of boundary point (a,b) on which |
| 197 | overlap ends, where diagoanl = (|B|-b) - (|A|-a) */ |
| 198 | int length; /* Length of overlap (|A|+|B|)/2 */ |
| 199 | int diffs; /* Estimated number of differences in overlap */ |
| 200 | int comp; /* B sequence was complemented for this comparison */ |
| 201 | int indif; /* Estimated number of diffs in segments of overlap */ |
| 202 | int score; /* Sum of all gap lengths */ |
| 203 | int num_pieces; /* # of segments in overlap chain */ |
| 204 | int next; /* for iteration through the chain - CMM */ |
| 205 | Local_Chain *chain; /* chain[0..num_pieces] describe each gap between |
| 206 | local segments in the overlap chain */ |
| 207 | } Local_Overlap; |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | /* Find_Local_Overlap takes an array of local alignments as returned by |
| 217 | Compare_Local and finds the best scoring local overlap between the |
| 218 | underlying sequences. One must pass in the length of the two sequences |
| 219 | from which Compare_Local produced the local alignments as well as the |
| 220 | number of local alignments in the array. If the parameter comp is nonzero |
| 221 | then the comparison will effectively be between A and the complement of B. |
| 222 | Normally, the parameter nextbest is zero -- after such a call, a second |
| 223 | alternate overlap, third alternate, and so on can be generated by |
| 224 | subsequent calls with nextbest set to a nonzero value. The alternates |
| 225 | are the best scoring overlaps that starts with a segment not in any |
| 226 | previous overlap. The best overlap is returned as a pointer to local |
| 227 | overlap structure described above. Unlike many of my routines, the |
| 228 | reclamation of the storage for this data structure is the responsibility |
| 229 | of the caller and requires simply calling free on it, as the entire |
| 230 | structure, including the chain array, is in a single memory block. The |
| 231 | parameter MinorThresh determines whether a gap delta is consider minor or |
| 232 | major (see the description above on gap types). An overlap is returned |
| 233 | only if the ratio of the difference to the length of the overlap is less |
| 234 | than GapThresh, otherwise NULL is returned. |
| 235 | */ |
| 236 | |
| 237 | Local_Overlap *Find_Local_Overlap(int Alen, int Blen, int comp, int nextbest, |
| 238 | Local_Segment *Segs, int NumSegs, |
| 239 | int MinorThresh, double GapThresh); |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | /* Create a trace to be interpreted as with DP_Compare_AS, but based |
| 249 | on a Local_Overlap record. A Local_Segment within the overlap |
| 250 | will be aligned using OKNAlign(), generating a subtrace. Subtraces, |
| 251 | with their indices appropriately adjusted, will be spliced together |
| 252 | by an encoding of the gaps between segments; for now, we'll simply insert |
| 253 | gaps as follows: |
| 254 | |
| 255 | A "gap" with x bases in A and y bases in B will become a section |
| 256 | of the alignment x+y positions long, with the A fragment first |
| 257 | and the B fragment second: |
| 258 | |
| 259 | AAAAAAAAAA-------------- |
| 260 | ----------BBBBBBBBBBBBBB |
| 261 | |
| 262 | Obviously, a more compact treatment is possible! |
| 263 | |
| 264 | Assumptions: both sequences should be in the forward orientation |
| 265 | and all segments are forward. |
| 266 | |
| 267 | */ |
| 268 | int *AS_Local_Trace(Local_Overlap *local_overlap, |
| 269 | const char *aseq, |
| 270 | const char *bseq); |
| 271 | |
| 272 | |
| 273 | #endif |
| 274 |