ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/seqalign/GXAlign.h
Revision: 74
Committed: Tue Sep 27 01:22:37 2011 UTC (9 years, 7 months ago) by gpertea
File size: 11518 byte(s)
Log Message:
added seqalign files

Line User Rev File contents
1 gpertea 74 #ifndef _GXALIGN_H
2    
3     #include "GBase.h"
4     #include "GList.hh"
5    
6     #define GDEBUG 1
7    
8     #define ERROR_FRACTION 6 // 1/this : the higher it is, the smaller the diagonal band
9     #define MAX_SPACE 1000000
10     #define LARGE 100000000
11    
12     enum {
13     EDIT_OP_MASK = 0x3,
14     EDIT_OP_ERR = 0x0,
15     EDIT_OP_INS = 0x1,
16     EDIT_OP_DEL = 0x2,
17     EDIT_OP_REP = 0x3
18     };
19    
20     #define GX_EDITOP_VAL(op) ((op) >> 2)
21     #define GX_EDITOP_GET(op) ((op) & EDIT_OP_MASK)
22     #define GX_EDITOP_CONS(op, val) (((val) << 2) | ((op) & EDIT_OP_MASK))
23    
24     #ifdef GDEBUG
25     enum {c_black=0,
26     c_red, c_green,c_brown,c_blue,c_magenta,c_cyan,c_white
27     };
28    
29     void color_fg(int c, FILE* f=stderr);
30     void color_bg(int c, FILE* f=stderr);
31     void color_resetfg(FILE* f=stderr);
32     void color_resetbg(FILE* f=stderr);
33     void color_reset(FILE* f=stderr);
34     void color_normal(FILE* f=stderr);
35     #endif
36    
37     struct GXEditScript{
38     uint32 *ops; // array of edit operations
39     uint32 opsize, opnum; // size of allocation, number in use
40     uint32 oplast; // most recent operation added
41     //methods
42    
43     GXEditScript() {
44     init();
45     }
46     ~GXEditScript() {
47     GFREE(ops);
48     }
49     void init() {
50     ops = NULL;
51     opsize = 0;
52     opnum = 0;
53     oplast = 0;
54     getReady(8);
55     }
56    
57     int getReady(uint32 n) {
58     uint32 m = n + n/2;
59     if (opsize <= n) {
60     GREALLOC(ops, m*sizeof(uint32));
61     opsize = m;
62     }
63     return 1;
64     }
65    
66     int getReady2(uint32 n) {
67     if (opsize - opnum <= n)
68     return getReady(n + opnum);
69     return 1;
70     }
71    
72     int Put(uint32 op, uint32 n) {
73     if (!getReady2(2))
74     return 0;
75     oplast = op;
76     ops[opnum] = GX_EDITOP_CONS(op, n);
77     opnum += 1;
78     ops[opnum] = 0; // sentinel
79     return 1;
80     }
81     uint32* First() {
82     return opnum > 0 ? & ops[0] : NULL;
83     }
84    
85     uint32* Next(uint32 *op) {
86     // assumes flat address space !
87     if (&ops[0] <= op && op < &ops[opnum-1])
88     return op+1;
89     else
90     return 0;
91     }
92    
93     int More(uint32 op, uint32 k) {
94     if (op == EDIT_OP_ERR) {
95     GError("GXEditScript::opMore: bad opcode %d:%d", op, k);
96     return -1;
97     }
98    
99     if (GX_EDITOP_GET(oplast) == op) {
100     uint32 l=ops[opnum-1];
101     ops[opnum-1]=GX_EDITOP_CONS((GX_EDITOP_GET(l)),
102     (GX_EDITOP_VAL(l) + k));
103     }
104     else {
105     Put(op, k);
106     }
107    
108     return 0;
109     }
110    
111     GXEditScript* Append(GXEditScript *et) {
112     uint32 *op;
113     for (op = et->First(); op; op = et->Next(op))
114     More(GX_EDITOP_GET(*op), GX_EDITOP_VAL(*op));
115     return this;
116     }
117    
118     int opDel(uint32 k) {
119     return More(EDIT_OP_DEL, k);
120     }
121     int opIns(uint32 k) {
122     return More(EDIT_OP_INS, k);
123     }
124     int opRep(uint32 k) {
125     return More(EDIT_OP_REP, k);
126     }
127    
128     GXEditScript *reverse() {
129     const uint32 mid = opnum/2;
130     const uint32 end = opnum-1;
131     for (uint32 i = 0; i < mid; ++i) {
132     const uint32 t = ops[i];
133     ops[i] = ops[end-i];
134     ops[end-i] = t;
135     }
136     return this;
137     }
138     };
139    
140    
141     /* ----- pool allocator ----- */
142     struct GX3Val {
143     int I, C, D;
144     };
145    
146     struct GXAlnSpace {
147     GX3Val* space_array;
148     int used, size;
149     GXAlnSpace *next;
150     //methods
151     GXAlnSpace() {
152     uint32 amount=MAX_SPACE;
153     GCALLOC(space_array, sizeof(GX3Val)*amount);
154     if (space_array == NULL) {
155     GError("Failed to allocated GXAlnSpace for greedy extension!\n");
156     return;
157     }
158     used = 0;
159     size = amount;
160     next = NULL;
161     }
162     void refresh() {
163     GXAlnSpace* sp=this;
164     while (sp) {
165     sp->used = 0;
166     sp = sp->next;
167     }
168     }
169     ~GXAlnSpace() {
170     GXAlnSpace* next_sp;
171     GXAlnSpace* sp=this->next;
172     while (sp) {
173     next_sp = sp->next;
174     GFREE(sp->space_array);
175     delete sp;
176     sp = next_sp;
177     }
178     }
179     GX3Val* getSpace(int amount) {
180     GX3Val* v;
181     if (amount < 0) return NULL;
182     GXAlnSpace* S=this;
183     while (used+amount > S->size) {
184     if (next == NULL) {
185     next=new GXAlnSpace();
186     }
187     S = S->next;
188     }
189     v = S->space_array+S->used;
190     S->used += amount;
191     return v;
192     }
193     };
194    
195     struct GXAlnMem { //GreedyAlignMem
196     int** flast_d;
197     int* max_row_free;
198     int* uplow_free;
199     GXAlnSpace* space;
200     int max_d;
201     GXAlnMem(int max_len, int reward, int penalty, int Xdrop) {
202     flast_d=NULL;
203     max_row_free=NULL;
204     max_d=0;
205     uplow_free=NULL;
206     space=NULL;
207     max_d = (int)(max_len / ERROR_FRACTION + 1);
208     int d_diff = (Xdrop+reward/2)/(penalty+reward) + 1;
209     GCALLOC(flast_d, (max_d+2)* sizeof(int));
210     if (flast_d == NULL) {
211     GError("Error: cannot allocate GXAlnMem structure!\n");
212     }
213     flast_d[0] = (int*)malloc((max_d + max_d + 6) * sizeof(int) * 2);
214     if (flast_d[0] == NULL) {
215     GError("Error: failed to allocate flast_d[0] in GXAlnMem!\n");
216     }
217     flast_d[1] = flast_d[0] + max_d + max_d + 6;
218     uplow_free = NULL;
219     max_row_free = (int*) malloc(sizeof(int) * (max_d + 1 + d_diff));
220     space = new GXAlnSpace();
221     }
222     ~GXAlnMem() {
223     delete space;
224     GFREE(max_row_free);
225     //GFREE(flast_d[0]); // FIXME
226     //TODO: check with valgrind
227     //for (int i=0;i<max_d+2;i++) {
228     // GFREE(flast_d[i]);
229     // }
230     //GFREE(flast_d);
231     }
232     };
233    
234     int GXGreedyAlign(const char* s1, int len1,
235     const char* s2, int len2,
236     bool reverse, int xdrop_threshold,
237     int match_cost, int mismatch_cost,
238     int* e1, int* e2,
239     GXAlnMem* abmp, GXEditScript *S=NULL);
240    
241     #define GAPALIGN_SUB ((unsigned char)0) /*op types within the edit script*/
242     #define GAPALIGN_INS ((unsigned char)1)
243     #define GAPALIGN_DEL ((unsigned char)2)
244     #define GAPALIGN_DECLINE ((unsigned char)3)
245    
246     struct GapXEditScript {
247     unsigned char op_type; // GAPALIGN_SUB, GAPALIGN_INS, or GAPALIGN_DEL
248     int num; // Number of operations
249     GapXEditScript* next;
250     GapXEditScript() {
251     op_type=0;
252     num=0;
253     next=NULL;
254     }
255     void print();
256     };
257    
258     class CSeqGap { //
259     public:
260     int offset;
261     int len;
262     CSeqGap(int gofs=0,int glen=1) {
263     offset=gofs;
264     len=glen;
265     }
266     };
267    
268     class CAlnGapInfo {
269     int a_ofs; //alignment start on seq a (0 based)
270     int b_ofs; //alignment start on seq b (0 based)
271     int a_len; //length of alignment on seq a
272     int b_len; //length of alignment on seq b
273     public:
274     GVec<CSeqGap> a_gaps;
275     GVec<CSeqGap> b_gaps;
276     CAlnGapInfo(GXEditScript* ed_script, int astart=0, int bstart=0):a_gaps(),b_gaps() {
277     a_ofs=astart;
278     b_ofs=bstart;
279     a_len=0;
280     b_len=0;
281     for (uint32 i=0; i<ed_script->opnum; i++) {
282     int num=((ed_script->ops[i]) >> 2);
283     char op_type = 3 - ( ed_script->ops[i] & EDIT_OP_MASK );
284     if (op_type == 3 || op_type < 0 )
285     GError("Error: encountered op_type %d in ed_script?!\n", (int)op_type);
286     CSeqGap gap;
287     switch (op_type) {
288     case GAPALIGN_SUB: a_len+=num;
289     b_len+=num;
290     break;
291     case GAPALIGN_INS: a_len+=num;
292     gap.offset=b_ofs+b_len;
293     gap.len=num;
294     b_gaps.Add(gap);
295     break;
296     case GAPALIGN_DEL: b_len+=num;
297     gap.offset=a_ofs+a_len;
298     gap.len=num;
299     a_gaps.Add(gap);
300     break;
301     }
302     }
303     }
304     #ifdef GDEBUG
305     void printAlignment(FILE* f, const char* sa, const char* sb) {
306     //print seq A
307     char al[1024]; //display buffer for seq A
308     int ap=0; //index in al[] for current character printed
309     int g=0;
310     int aend=a_ofs+a_len;
311     if (a_ofs<b_ofs) {
312     for (int i=0;i<b_ofs-a_ofs;i++) {
313     fprintf(f, " ");
314     al[++ap]=' ';
315     }
316     }
317     for (int i=0;i<aend;i++) {
318     if (g<a_gaps.Count() && a_gaps[g].offset==i) {
319     for (int j=0;j<a_gaps[g].len;j++) {
320     fprintf(f, "-");
321     al[++ap]='-';
322     }
323     g++;
324     }
325     if (i==a_ofs) color_bg(c_blue,f);
326     fprintf(f, "%c", sa[i]);
327     al[++ap]=sa[i];
328     }
329     color_reset(f);
330     fprintf(f, &sa[aend]);
331     fprintf(f, "\n");
332     //print seq B
333     ap=0;
334     g=0;
335     int bend=b_ofs+b_len;
336     if (a_ofs>b_ofs) {
337     for (int i=0;i<a_ofs-b_ofs;i++) {
338     fprintf(f, " ");
339     ap++;
340     }
341     }
342     for (int i=0;i<b_ofs;i++) {
343     fprintf(f, "%c", sb[i]);
344     ap++;
345     }
346     for (int i=b_ofs;i<bend;i++) {
347     if (g<b_gaps.Count() && b_gaps[g].offset==i) {
348     for (int j=0;j<b_gaps[g].len;j++) {
349     fprintf(f, "-");
350     ap++;
351     }
352     g++;
353     }
354     if (i==b_ofs) color_bg(c_blue,f);
355     ap++;
356     bool mismatch=(sb[i]!=al[ap] && al[ap]!='-');
357     if (mismatch) color_bg(c_red,f);
358     fprintf(f, "%c", sb[i]);
359     if (mismatch) color_bg(c_blue,f);
360     }
361     color_reset(f);
362     fprintf(f, &sb[bend]);
363     fprintf(f, "\n");
364     }
365     #endif
366     };
367    
368    
369     struct GXAlnInfo {
370     const char *qseq;
371     int ql,qr;
372     const char *sseq;
373     int sl,sr;
374     int score;
375     double pid;
376     GXEditScript* editscript;
377     GXAlnMem* abmp;
378     CAlnGapInfo* gapinfo;
379     GXAlnInfo(const char* q, int q_l, int q_r, const char* s, int s_l, int s_r,
380     int sc=0, double percid=0) {
381     qseq=q;
382     sseq=s;
383     ql=q_l;
384     qr=q_r;
385     sl=s_l;
386     sr=s_r;
387     score=sc;
388     pid=percid;
389     //editscript=NULL;
390     gapinfo=NULL;
391     }
392     ~GXAlnInfo() {
393     delete editscript;
394     delete abmp;
395     delete gapinfo;
396     }
397     };
398    
399    
400    
401     struct GXSeed {
402     int a_ofs; //0-based coordinate on seq a
403     int b_ofs; //0-based coordinate on seq a
404     int len; //length of exact match after extension
405     int diag; //b_ofs-a_ofs
406     bool operator<(GXSeed& d){
407     return (len<d.len);
408     }
409     bool operator>(GXSeed& d){
410     return (len>d.len);
411     }
412     bool operator==(GXSeed& d){
413     return (len==d.len);
414     }
415     GXSeed(int aofs=0, int bofs=0, int l=4) {
416     a_ofs=aofs;
417     b_ofs=bofs;
418     len=l;
419     diag=b_ofs-a_ofs;
420     }
421     };
422    
423     void collectSeeds_R(GList<GXSeed>& seeds, const char* seqa, int a_len, const char* seqb, int b_len); //overlap of 3' end of seqb
424     //void collectSeeds_L(GList<GXSeed>& seeds, const char* seqa, const char* seqb); //overlap of 5' end of seqb
425    
426     void printEditScript(GXEditScript* ed_script);
427    
428     //GapXEditScript* EditScript2GapX(GXEditScript* ed_script);
429    
430     GXAlnInfo* GreedyAlignRegion(const char* q_seq, int q_alnstart, int q_max,
431     const char* s_seq, int s_alnstart, int s_max,
432     bool editscript=false, int reward=2, int penalty=-3, int xdrop=22);
433     GXAlnInfo* GreedyAlign(const char* q_seq, int q_alnstart, const char* s_seq, int s_alnstart,
434     bool editscript=false, int reward=2, int penalty=-3, int xdrop=22);
435    
436     /*
437     GXAlnInfo* GreedyExtendRight(const char* q_seq, int q_off, const char* s_seq, int s_off,
438     bool editscript=true, int reward=2, int penalty=-3, int xdrop=22);
439     */
440    
441     #endif