1 | #include <memory.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <stdarg.h> |
---|
5 | #include <limits.h> |
---|
6 | #include <ctype.h> |
---|
7 | #include <string.h> |
---|
8 | |
---|
9 | #include <aw_root.hxx> |
---|
10 | |
---|
11 | #ifndef LINKED_STATIC |
---|
12 | #include <arbdb.h> |
---|
13 | #include <awtc_seq_search.hxx> |
---|
14 | #endif |
---|
15 | |
---|
16 | #define AWTC_MESSAGE_BUFFERSIZE 300 |
---|
17 | |
---|
18 | void AWTC_message(const char *format, ...) |
---|
19 | { |
---|
20 | va_list argp; |
---|
21 | char buffer[AWTC_MESSAGE_BUFFERSIZE]; |
---|
22 | |
---|
23 | va_start(argp, format); |
---|
24 | #ifndef SUN4 |
---|
25 | #if defined(ASSERTION_USED) |
---|
26 | int chars = |
---|
27 | #endif // ASSERTION_USED |
---|
28 | vsprintf(buffer, format, argp); |
---|
29 | awtc_assert(chars<AWTC_MESSAGE_BUFFERSIZE); |
---|
30 | #else |
---|
31 | vsprintf(buffer, format, argp); |
---|
32 | #endif |
---|
33 | va_end(argp); |
---|
34 | aw_message(buffer); |
---|
35 | } |
---|
36 | |
---|
37 | void AWTC_Points::append(AWTC_Points *neu) { |
---|
38 | if (Next) Next->append(neu); |
---|
39 | else Next = neu; |
---|
40 | } |
---|
41 | |
---|
42 | AWTC_CompactedSequence::AWTC_CompactedSequence(const char *Text, int Length, const char *name, int start_offset) |
---|
43 | { |
---|
44 | long cPos; |
---|
45 | long xPos; |
---|
46 | long *compPositionTab = new long[Length]; |
---|
47 | myLength = 0; |
---|
48 | int lastWasPoint = 0; |
---|
49 | |
---|
50 | myStartOffset = start_offset; |
---|
51 | myEndOffset = start_offset+Length-1; |
---|
52 | |
---|
53 | awtc_assert(name); |
---|
54 | |
---|
55 | points = NULL; |
---|
56 | myName = strdup(name); |
---|
57 | |
---|
58 | long firstBase = 0; |
---|
59 | long lastBase = Length-1; |
---|
60 | |
---|
61 | for (xPos=0; xPos<Length; xPos++) { // convert point gaps at beginning to dash gaps |
---|
62 | char c = Text[xPos]; |
---|
63 | if (!AWTC_is_gap(c)) { |
---|
64 | firstBase = xPos; |
---|
65 | break; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | for (xPos=Length-1; xPos>=0; xPos--) // same for end of sequence |
---|
70 | { |
---|
71 | char c = Text[xPos]; |
---|
72 | if (!AWTC_is_gap(c)) { |
---|
73 | lastBase = xPos; |
---|
74 | break; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | for (xPos=0; xPos<Length; xPos++) { |
---|
79 | char c = toupper(Text[xPos]); |
---|
80 | |
---|
81 | if (AWTC_is_gap(c)) { |
---|
82 | if (c=='-' || xPos<firstBase || xPos>lastBase) { |
---|
83 | compPositionTab[xPos] = -1; // a illegal index |
---|
84 | lastWasPoint = 0; |
---|
85 | } |
---|
86 | else { |
---|
87 | awtc_assert(c=='.'); |
---|
88 | |
---|
89 | if (!lastWasPoint) { |
---|
90 | lastWasPoint = 1; |
---|
91 | storePoints(myLength); |
---|
92 | } |
---|
93 | |
---|
94 | compPositionTab[xPos] = -1; |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|
98 | else { |
---|
99 | compPositionTab[xPos] = myLength++; |
---|
100 | lastWasPoint = 0; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | // if (lastWasPoint) { |
---|
105 | // storePoints(myLength+1); |
---|
106 | // } |
---|
107 | |
---|
108 | //awtc_assert(myLength); // otherwise Text does only contain gaps |
---|
109 | |
---|
110 | myText = new char[myLength+1]; |
---|
111 | myText[myLength] = 0; |
---|
112 | |
---|
113 | expdPositionTab = new int[myLength+1]; // plus one extra element |
---|
114 | expdPositionTab[myLength] = Length; // which is the original length |
---|
115 | |
---|
116 | gapsBeforePosition = new int[myLength+1]; |
---|
117 | |
---|
118 | for (xPos=0; xPos<Length; xPos++) { |
---|
119 | cPos = compPositionTab[xPos]; |
---|
120 | awtc_assert(cPos<myLength); |
---|
121 | |
---|
122 | if (cPos>=0) { |
---|
123 | myText[cPos] = toupper(Text[xPos]); |
---|
124 | expdPositionTab[cPos] = xPos; |
---|
125 | gapsBeforePosition[cPos] = |
---|
126 | cPos |
---|
127 | ? xPos-expdPositionTab[cPos-1]-1 |
---|
128 | : xPos; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | if (myLength>0) { |
---|
133 | gapsBeforePosition[myLength] = Length - expdPositionTab[myLength-1]; // gaps before end of sequence |
---|
134 | } |
---|
135 | |
---|
136 | referred = 1; |
---|
137 | |
---|
138 | delete [] compPositionTab; |
---|
139 | } |
---|
140 | |
---|
141 | AWTC_CompactedSequence::~AWTC_CompactedSequence() |
---|
142 | { |
---|
143 | awtc_assert(referred==0); |
---|
144 | |
---|
145 | delete[] expdPositionTab; |
---|
146 | if (points) delete points; |
---|
147 | // delete[] compPositionTab; |
---|
148 | |
---|
149 | free(myName); |
---|
150 | } |
---|
151 | |
---|
152 | int AWTC_CompactedSequence::compPosition(int xPos) const |
---|
153 | { |
---|
154 | int l = 0, |
---|
155 | h = length(); |
---|
156 | |
---|
157 | while (l<h) { |
---|
158 | int m = (l+h)/2; |
---|
159 | int cmp = xPos-expdPosition(m); |
---|
160 | |
---|
161 | if (cmp==0) { |
---|
162 | return m; // found! |
---|
163 | } |
---|
164 | |
---|
165 | if (cmp<0) { // xPos < expdPosition(m) |
---|
166 | h = m-1; |
---|
167 | } |
---|
168 | else { // xPos > expdPosition(m) |
---|
169 | l = m+1; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | awtc_assert(l==h); |
---|
174 | return l; |
---|
175 | } |
---|
176 | |
---|
177 | AWTC_fast_align_insertion::~AWTC_fast_align_insertion() |
---|
178 | { |
---|
179 | if (myNext) delete myNext; |
---|
180 | } |
---|
181 | |
---|
182 | AWTC_FastSearchSequence::AWTC_FastSearchSequence(const AWTC_CompactedSubSequence& seq) |
---|
183 | { |
---|
184 | memset((char*)myOffset, 0, MAX_TRIPLES*sizeof(*myOffset)); |
---|
185 | AWTC_SequencePosition triple(seq); |
---|
186 | |
---|
187 | mySequence = &seq; |
---|
188 | |
---|
189 | while (triple.rightOf()>=3) // enough text for triple? |
---|
190 | { |
---|
191 | int tidx = triple_index(triple.text()); |
---|
192 | AWTC_TripleOffset *top = new AWTC_TripleOffset(triple.leftOf(), myOffset[tidx]); |
---|
193 | |
---|
194 | myOffset[tidx] = top; |
---|
195 | ++triple; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | void AWTC_alignBuffer::correctUnalignedPositions(void) |
---|
200 | { |
---|
201 | long off = 0; |
---|
202 | long rest = used; |
---|
203 | |
---|
204 | while (rest) |
---|
205 | { |
---|
206 | char *found = (char*)memchr(myQuality+off, '?', rest); // search for next '?' in myQuality |
---|
207 | if (!found || (found-myQuality)>=rest) break; |
---|
208 | |
---|
209 | long cnt; |
---|
210 | for (cnt=0; found[cnt]=='?'; cnt++) found[cnt]='+'; // count # of unaligned positions and change them to '+' |
---|
211 | long from = found-myQuality; |
---|
212 | long b_off = from-1; // position before unaligned positions |
---|
213 | long a_off = from+cnt; // positions after unaligned positions |
---|
214 | |
---|
215 | long before, after; |
---|
216 | for (before=0; b_off>=0 && isGlobalGap(b_off); before++,b_off--) ; // count free positions before unaligned positions |
---|
217 | for (after=0; a_off<used && isGlobalGap(a_off); after++,a_off++) ; // count free positions after unaligned positions |
---|
218 | |
---|
219 | if (b_off<=0) before = LONG_MAX; |
---|
220 | if (a_off>=used) after = LONG_MAX; |
---|
221 | |
---|
222 | if (before<after) // move to the left |
---|
223 | { |
---|
224 | if (before) |
---|
225 | { |
---|
226 | long to = from-before; |
---|
227 | while (cnt--) moveUnaligned(from++,to++); |
---|
228 | } |
---|
229 | } |
---|
230 | else if (after!=LONG_MAX) // move to the right |
---|
231 | { |
---|
232 | if (after) |
---|
233 | { |
---|
234 | from += cnt-1; // copy from right to left! |
---|
235 | long to = from+after; |
---|
236 | |
---|
237 | while (cnt--) moveUnaligned(from--,to--); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | rest -= (a_off-off); |
---|
242 | off = a_off; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | void AWTC_alignBuffer::expandPoints(AWTC_CompactedSubSequence& slaveSequence) |
---|
247 | { |
---|
248 | long rest = used; |
---|
249 | long off = 0; // real position in sequence |
---|
250 | long count = 0; // # of bases left of this position |
---|
251 | long nextPoint = slaveSequence.firstPointPosition(); |
---|
252 | |
---|
253 | while (nextPoint!=-1 && rest) |
---|
254 | { |
---|
255 | unsigned char gap = myBuffer[off]; |
---|
256 | if (gap=='-' || gap=='.') { |
---|
257 | if (nextPoint==count) { |
---|
258 | myBuffer[off] = '.'; |
---|
259 | } |
---|
260 | } |
---|
261 | else |
---|
262 | { |
---|
263 | count++; |
---|
264 | if (nextPoint==count) { |
---|
265 | AWTC_message("Couldn't insert point-gap at offset %li of '%s' (gap removed by aligner)", |
---|
266 | off, slaveSequence.name()); |
---|
267 | } |
---|
268 | if (nextPoint<count) { |
---|
269 | nextPoint = slaveSequence.nextPointPosition(); |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | off++; |
---|
274 | rest--; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | void AWTC_alignBuffer::point_ends_of() |
---|
279 | { |
---|
280 | int i; |
---|
281 | |
---|
282 | for (i=0; (myBuffer[i]=='.' || myBuffer[i]=='-') && i<used; i++) { |
---|
283 | myBuffer[i] = '.'; |
---|
284 | } |
---|
285 | |
---|
286 | for (i=used-1; (myBuffer[i]=='.' || myBuffer[i]=='-') && i>=0; i--) { |
---|
287 | myBuffer[i] = '.'; |
---|
288 | } |
---|
289 | } |
---|