Debian Codegraph @

w4/alignment.c

raw
1 /* File: alignment.c
2 * Author: Richard Durbin ([email protected])
3 * Copyright (C) J Thierry-Mieg and R Durbin, 1995
4 * -------------------------------------------------------------------
5 * Acedb is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
19 * -------------------------------------------------------------------
20 * This file is part of the ACEDB genome database package, written by
21 * Richard Durbin (MRC LMB, UK) [email protected], and
22 * Jean Thierry-Mieg (CRBM du CNRS, France) [email protected]
23 *
24 * Description:
25 * Exported functions:
26 * (public) BOOL alignDumpKey (ACEOUT dump_out, KEY key)
27 * (public) int alignDumpKeySet (ACEOUT dump_out, KEYSET kSet)
28 *
29 * (private) ALIGNMENT *alignGet (KEY key)
30 * HISTORY:
31 * Last edited: Jul 3 14:26 2003 (edgrif)
32 * Created: Thu Sep 7 21:41:15 1995 (rd)
33 *-------------------------------------------------------------------
34 */
35
36 /* $Id: alignment.c,v 1.10 2003/07/04 10:48:26 edgrif Exp $ */
37
38 #include "acedb.h"
39 #include "aceio.h"
40 #include "lex.h"
41 #include "bs.h"
42 #include "dna.h"
43 #include "peptide.h"
44
45 #include "alignment_.h"
46
47 #include "whooks/systags.h"
48
49 /*********************************************/
50
51 static KEY _Alignment, _Verbatim, _Extract, _DNA, _Gap, _Insert ;
52 static KEY _Peptide, _Seg, _Translate, _Protein ;
53
54 static void initialise (void)
55 {
56 static BOOL isDone = FALSE ;
57
58 if (isDone)
59 return ;
60
61 lexaddkey ("Alignment", &_Alignment, 0) ;
62 lexaddkey ("Verbatim", &_Verbatim, 0) ;
63 lexaddkey ("Extract", &_Extract, 0) ;
64 lexaddkey ("DNA", &_DNA, 0) ;
65 lexaddkey ("Gap", &_Gap, 0) ;
66 lexaddkey ("Insert", &_Insert, 0) ;
67 lexaddkey ("Peptide", &_Peptide, 0) ;
68 lexaddkey ("Seg", &_Seg, 0) ;
69 lexaddkey ("Translate", &_Translate, 0) ;
70 lexaddkey ("Protein", &_Protein, 0) ;
71
72 isDone = TRUE ;
73 }
74
75 /************************************************/
76
77 ALIGNMENT *alignGet (KEY key)
78 /* private to alignment package */
79 {
80 OBJ obj = 0 ;
81 ALIGNMENT *al = 0 ;
82 STORE_HANDLE handle ;
83 KEY tag, item ;
84 int start, end ;
85 BOOL isReverse ;
86 BSMARK mark1 = 0, mark2 = 0, mark3 = 0 ;
87 Array translationTable = NULL ;
88
89 initialise () ;
90
91 if (!(obj = bsCreate (key)))
92 goto abort ;
93 if (!bsFindTag (obj, _Alignment))
94 goto abort ;
95
96 if (!(translationTable = pepGetTranslationTable (key, 0)))
97 goto abort ;
98
99 handle = handleCreate () ;
100 al = (ALIGNMENT*) halloc (sizeof(ALIGNMENT), handle) ;
101 al->key = key ;
102 al->handle = handle ;
103 al->comp = arrayHandleCreate (64, ALIGN_COMP, handle) ;
104
105 if (bsGetKeyTags (obj, _bsRight, 0))
106 do
107 {
108 mark1 = bsMark(obj, mark1) ;
109 if (bsGetKey (obj, _bsRight, &item))
110 do
111 {
112 mark2 = bsMark(obj, mark2) ;
113
114 if (bsGetData (obj, _bsRight, _Int, &start))
115 do
116 {
117 mark3 = bsMark(obj, mark3) ;
118
119 if (bsGetData (obj, _bsRight, _Int, &end) && bsPushObj (obj))
120 {
121 /* MAKE A NEW COMPONENT */
122
123 ALIGN_COMP *c = arrayp (al->comp, arrayMax(al->comp), ALIGN_COMP) ;
124 char *cp ;
125
126 c->key = item ;
127 c->start = start ;
128 c->end = end ;
129
130 isReverse = (end < start) ;
131 if (isReverse && tag == _Peptide)
132 {
133 messerror ("Can not reverse peptide %s from %d to %d in alignment %s",
134 name(item), start, end, name(key)) ;
135 goto abort ;
136 }
137
138 if (bsGetData (obj, _Verbatim, _Text, &cp))
139 {
140 /* SIMPLE CASE - VERBATIM */
141
142 c->seq = strnew (cp, al->handle) ;
143 }
144 else if (bsGetKeyTags (obj, _Extract, &tag))
145 {
146 Array a ; /* to build in */
147 ALIGNMENT *ali ;
148 KEY action ;
149
150 if (((tag == _DNA || tag == _Translate) && (a = dnaGet (item))) ||
151 (tag == _Peptide && (a = peptideGet (item))))
152 {
153 /* EXTRACT FROM DNA/PROTEIN */
154 int i ;
155 Array b = arrayCreate (2*(end-start+1), char) ;
156
157 while (bsPushObj (obj) && bsGetKeyTags (obj, _bsRight, &action))
158 if (action == _Gap && bsGetData (obj, _bsRight, _Int, &start))
159 while (start-- > 0)
160 array(b,arrayMax(b),char) = GAP ;
161 else if (action == _Insert && bsGetData (obj, _bsRight, _Int, &start))
162 while (start-- > 0)
163 array(b,arrayMax(b),char) = (tag == _Peptide) ? 20 : N_ ;
164 else if (action == _Seg &&
165 bsGetData (obj, _bsRight, _Int, &start) &&
166 bsGetData (obj, _bsRight, _Int, &end))
167 {
168 if (isReverse)
169 for (i = --start ; i >= end-1 ; --i)
170 array(b,arrayMax(b),char) = complementBase[(int)arr(a,i,char)] ;
171 else
172 for (i = --start ; i < end ; ++i)
173 array(b,arrayMax(b),char) = arr(a,i,char) ;
174 }
175 else
176 {
177 messerror ("Unknown or incomplete Extract action for "
178 "%s in alignment %s", name(item), name(key)) ;
179 goto abort ;
180 }
181 if (tag == _Translate)
182 {
183 cp = c->seq = halloc (1 + arrayMax(b)/3, al->handle) ;
184 for (i = 0 ; i < arrayMax(b)-2 ; i += 3)
185 {
186 if (arr(b,i,char) == GAP)
187 *cp++ = '.' ;
188 else
189 *cp++ = e_codon (arrp (b, i, char), translationTable) ;
190 }
191 *cp = 0 ;
192 }
193 else
194 {
195 if (tag == _Peptide)
196 pepDecodeArray (b) ;
197 else if (tag == _DNA)
198 dnaDecodeArray (b) ;
199 else
200 messcrash ("Oops in alignGet() - unknown tag") ;
201 array(b, arrayMax(b), char) = 0 ;
202 c->seq = strnew (arrp(b,0,char), al->handle) ;
203 }
204 arrayDestroy (a) ;
205 arrayDestroy (b) ;
206 }
207 else if (tag == _Alignment && (ali = alignGet (item)))
208 {
209 /* EXTRACT FROM ALIGNMENT */
210 int i, j, n = 0 ;
211 int start0 = start, end0 = end ;
212 int j0 = arrayMax(al->comp) - 1 ;
213 ALIGN_COMP *ci ;
214
215 BSMARK mark = bsMark (obj, 0) ;
216 while (bsPushObj (obj) && bsGetKeyTags (obj, _bsRight, &action))
217 if (action == _Gap &&
218 bsGetData (obj, _bsRight, _Int, &start))
219 n += start ;
220 else if (action == _Insert &&
221 bsGetData (obj, _bsRight, _Int, &start))
222 n += start ;
223 else if (action == _Seg &&
224 bsGetData (obj, _bsRight, _Int, &start) &&
225 bsGetData (obj, _bsRight, _Int, &end))
226 n += end - start + 1 ;
227 else
228 { messerror ("Unknown or incomplete Extract action for "
229 "%s in alignment %s", name(item), name(key)) ;
230 goto abort ;
231 }
232 bsGoto (obj, mark) ; bsMarkFree (mark) ;
233
234 for (j = 0 ; j < arrayMax(ali->comp) ; ++j)
235 {
236 c = arrayp (al->comp, j0+j, ALIGN_COMP) ;
237 ci = arrp(ali->comp, j, ALIGN_COMP) ;
238 c->key = ci->key ;
239 c->seq = (char*) halloc (n+1, al->handle) ;
240 c->start = start0 ;
241 c->end = end0 ;
242 /* how to do start/end? - need pos */
243 }
244
245 n = 0 ;
246 while (bsPushObj (obj) && bsGetKeyTags (obj, _bsRight, &action))
247 if (action == _Gap &&
248 bsGetData (obj, _bsRight, _Int, &start))
249 {
250 for (j = 0 ; j < arrayMax(ali->comp) ; ++j)
251 {
252 c = arrp(al->comp, j0+j, ALIGN_COMP) ;
253 for (i = 0 ; i < start ; ++i)
254 c->seq[n+i] = '.' ;
255 }
256 n += start ;
257 }
258 else if (action == _Insert &&
259 bsGetData (obj, _bsRight, _Int, &start))
260 {
261 for (j = 0 ; j < arrayMax(ali->comp) ; ++j)
262 {
263 c = arrp(al->comp, j0+j, ALIGN_COMP) ;
264 for (i = 0 ; i < start ; ++i)
265 c->seq[n+i] = 'X' ;
266 }
267 n += start ;
268 }
269 else if (action == _Seg &&
270 bsGetData (obj, _bsRight, _Int, &start) &&
271 bsGetData (obj, _bsRight, _Int, &end))
272 {
273 --start ;
274 for (j = 0 ; j < arrayMax(ali->comp) ; ++j)
275 {
276 c = arrp(al->comp, j0+j, ALIGN_COMP) ;
277 ci = arrp(ali->comp, j, ALIGN_COMP) ;
278 for (i = start ; i < end ; ++i)
279 c->seq[n+i-start] = ci->seq[i] ;
280 }
281 n += end - start ;
282 }
283 messfree (ali) ;
284 }
285 }
286
287 }
288 bsGoto (obj, mark3) ;
289 } while (bsGetData (obj, _bsDown, _Int, &start)) ;
290
291 bsGoto (obj, mark2) ;
292 } while (bsGetKey (obj, _bsDown, &item)) ;
293
294 bsGoto (obj, mark1) ;
295 } while (bsGetKeyTags (obj, _bsDown, 0)) ;
296
297 bsMarkFree (mark1) ; bsMarkFree (mark2) ; bsMarkFree (mark3) ;
298
299 return al ;
300
301 /* Clear up after errors. */
302 abort:
303 if (obj)
304 bsDestroy (obj) ;
305 if (al)
306 messfree (al) ;
307
308 return 0 ;
309 }
310
311 /***************************************************************/
312
313 BOOL alignDumpKey (ACEOUT dump_out, KEY key)
314 {
315 ALIGNMENT *al ;
316 ALIGN_COMP *c ;
317 int i, xmax = 0, ymax = 0, x, y ;
318
319 if (!(al = alignGet (key)))
320 return FALSE ;
321
322 aceOutPrint (dump_out, "#NAME %s\n", name(key)) ;
323
324 c = arrp(al->comp, 0, ALIGN_COMP) ;
325 for (i = arrayMax(al->comp) ; i-- ; ++c)
326 { x = strlen (messprintf ("%d - %d", c->start, c->end)) ;
327 if (x > xmax)
328 xmax = x ;
329 y = strlen (name (c->key)) ;
330 if (y > ymax)
331 ymax = y ;
332 }
333 xmax += ymax + 2 ;
334
335 c = arrp(al->comp, 0, ALIGN_COMP) ;
336 for (i = arrayMax(al->comp) ; i-- ; ++c)
337 {
338 long int before, after;
339 int num_bytes_written;
340
341 y = strlen (messprintf ("%d - %d", c->start, c->end)) ;
342
343 before = aceOutByte(dump_out);
344 aceOutPrint (dump_out, "%-*s %d - %d",
345 ymax, name(c->key), c->start, c->end) ;
346 after = aceOutByte(dump_out);
347 num_bytes_written = (int)(after - before);
348 aceOutPrint (dump_out, "%*s %s\n", xmax-num_bytes_written, "",
349 c->seq ? c->seq : "") ;
350 }
351
352 messfree (al) ;
353
354 return TRUE ;
355 } /* alignDumpKey */
356
357 /**************/
358
359 int alignDumpKeySet (ACEOUT dump_out, KEYSET kSet)
360 {
361 KEYSET alpha ;
362 int i, n = 0 ;
363
364 if (!kSet || !keySetMax(kSet))
365 return 0 ;
366
367 alpha = keySetAlphaHeap (kSet, keySetMax(kSet)) ;
368 for (i = 0 ; i < keySetMax(alpha) ; ++i)
369 if (alignDumpKey (dump_out, keySet(alpha, i)))
370 ++n ;
371 keySetDestroy (alpha) ;
372
373 messout ("I wrote %d alignments", n) ;
374 return n ;
375 }
376
377 /********************* end of file *********************/
378
379