File: SequenceTypeABI.cpp

package info (click to toggle)
gentle 1.9+cvs20100605+dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,224 kB
  • ctags: 6,214
  • sloc: cpp: 41,569; ansic: 3,978; sh: 1,420; makefile: 243
file content (503 lines) | stat: -rw-r--r-- 14,819 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/** \file
	\brief Contains the SeqABI class members
*/
#include "SequenceCanvas.h"

SeqABI::~SeqABI ()
    {
    if ( at ) delete at ;
    }

int SeqABI::get_bx ( int id , int idx )
	{
	if ( at )
		{
	    int bx1 = (char) at->vf[id].data[idx*2] ;
	    int bx2 = (unsigned char) at->vf[id].data[idx*2+1] ;
	    int bx = bx1 * 256 + bx2 ;
	    return bx ;
		}
	else
		{
		if ( id < 0 || id >= 4 ) return 0 ;
		if ( idx < 0 || idx >= sd.sequence.size() ) return 0 ;
        return sd.sequence[idx].peak_index ;
		}
	}

int SeqABI::arrange_sd ( int n )
	{
    int a , x , y , w , h , l = 0 , bo = can->border , lowy = 0 ;
    int lasta = 0 ;

    // Setting basic values
    can->SetFont(*can->font);
    int wx = can->charwidth , wy = can->charheight ;
    endnumberlength = 0 ;
    force_add_line_number = view_from > 0 ? view_from - 1 : 0 ;
    int ox = bo+wx , oy = n*wy+bo , endnumber = offset + s.length() ;
    while ( endnumber > 0 ) { endnumber /= 10 ; ox += wx ; endnumberlength++ ; }
    can->MyGetSize ( &w , &h ) ;
    w -= 20 ; // Scrollbar dummy
    scalex = screenScale ;
    if ( can->isPrinting() )
       {
       scalex = w / 500 ;
       w = w * 9 / 10 ;
       }

    itemsperline = ( w - ox ) / ( ( can->blocksize + 1 ) * wx ) ;
    itemsperline *= can->blocksize ;

    int id = 0 ;

    // Initiate Color coding
    int intensity = 200 ;
    colors[0] = wxColour ( 0 , 0 , 0 ) ;
    colors[1] = wxColour ( intensity , 0 , 0 ) ;
    colors[2] = wxColour ( 0 , intensity , 0 ) ;
    colors[3] = wxColour ( 0 , 0 , intensity ) ;

    // Base order
//    id = at->getRecord ( _T("FWO_") , 1 ) ;
//    myass ( id != -1 , "SeqABI::arrange_1" ) ;
    
    // GATC
    base2color[0] = 'T' ;//AGC
    base2color[1] = 'G' ;//CT
    base2color[2] = 'C' ;//TAT
    base2color[3] = 'A' ;//GC
    
    // For painting
    minx = ox ;
    maxx = w - ox - wx ;
    int diffx = 0 ;
//    id = at->getRecord ( _T("PLOC") , 2 ) ;
//    myass ( id != -1 , "SeqABI::arrange_2" ) ;

    pos.cleanup() ;
    x = ox ;
    y = oy ;
    pos.add ( -(++l) , bo , y , ox-wx-bo , wy-1 ) ; // Line number

//    int id2 = at->getRecord ( _T("DATA") , 9 ) ;
//    int lastx = at->vf[id2].nrecords ;
//	int lastx = 0 ; //!!!!
    int lastx = sd.tracer2[0].size() ;
    strange_compensation_factor = 0 ;
    
    if ( view_from > 0 ) diffx = get_bx ( id , view_from-1 ) * scalex ;
/*    if ( view_from > 0 )
    {
        diffx = sd.sequence[a].peak_index;
        if ( inv_compl ) diffx = lastx - diffx ;
        diffx *= scalex ;
    }
*/
	bool first = true ;    
    for ( a = 0 ; a < s.length() ; a++ )
        {
		if ( view_from > 0 && a+1 < view_from ) continue ;
		if ( view_to > 0 && a >= view_to ) break ;
        int idx = a ;
        if ( inv_compl ) idx = s.length() - a - 1 ;

		int bx = get_bx ( 0 , idx ) ;
        if ( inv_compl ) bx = lastx - bx ;
        x = bx * scalex - diffx ;
        
        // Ugly hack for partial inverse/complementary view
        if ( first && view_from > 0 && inv_compl && x < 0 )
        	{
			strange_compensation_factor = wx - x ;
			x += strange_compensation_factor ;
			diffx -= strange_compensation_factor ;
			}
		first = false ;

        if ( x > maxx )
           {
           pos.addline ( lasta , pos.p.GetCount() , y , y+wy-1 ) ;
           lasta = pos.p.GetCount()+1 ;
           y += wy * ( can->seq.GetCount() + can->blankline ) ;
           if ( a+1 < s.length() )
              pos.add ( -(++l) , bo , y , ox-wx-5 , wy-1 ) ; // Line number
           diffx += maxx ;
           x = bx * scalex - diffx ;
           }

        pos.add ( a+1 , x + minx - wx / 2 , y , wx-1 , wy-1 ) ;
        can->setLowX ( x + minx + wx*2 ) ;
        lowy = y+wy ;

        }
    if ( lasta != pos.p.GetCount()+1 ) 
        pos.addline ( lasta , pos.p.GetCount() , y , y+wy-1 ) ;
    
    lowy += wy * ( can->blankline ) ; // For sequence data
    
    return lowy + bo*2 ;
	}

int SeqABI::arrange ( int n )
    {
	if ( !at ) return arrange_sd ( n ) ;
    int a , x , y , w , h , l = 0 , bo = can->border , lowy = 0 ;
    int lasta = 0 ;

    // Setting basic values
    can->SetFont(*can->font);
    int wx = can->charwidth , wy = can->charheight ;
    endnumberlength = 0 ;
    force_add_line_number = view_from > 0 ? view_from - 1 : 0 ;
    int ox = bo+wx , oy = n*wy+bo , endnumber = offset + s.length() ;
    while ( endnumber > 0 ) { endnumber /= 10 ; ox += wx ; endnumberlength++ ; }
    can->MyGetSize ( &w , &h ) ;
    w -= 20 ; // Scrollbar dummy
    scalex = screenScale ;
    if ( can->isPrinting() )
       {
       scalex = w / 500 ;
       w = w * 9 / 10 ;
       }

    itemsperline = ( w - ox ) / ( ( can->blocksize + 1 ) * wx ) ;
    itemsperline *= can->blocksize ;

    int id ;

    // Initiate Color coding
    int intensity = 200 ;
    colors[0] = wxColour ( 0 , 0 , 0 ) ;
    colors[1] = wxColour ( intensity , 0 , 0 ) ;
    colors[2] = wxColour ( 0 , intensity , 0 ) ;
    colors[3] = wxColour ( 0 , 0 , intensity ) ;

    // Base order
    id = at->getRecord ( _T("FWO_") , 1 ) ;
    myass ( id != -1 , "SeqABI::arrange_1" ) ;
    
    // GATC
    int *ii = (int*) base2color ;
    *ii = at->vf[id].value ;
    
    // For painting
    minx = ox ;
    maxx = w - ox - wx ;
    int diffx = 0 ;
    id = at->getRecord ( _T("PLOC") , 2 ) ;
    myass ( id != -1 , "SeqABI::arrange_2" ) ;

    pos.cleanup() ;
    x = ox ;
    y = oy ;
    pos.add ( -(++l) , bo , y , ox-wx-bo , wy-1 ) ; // Line number
    
    int id2 = at->getRecord ( _T("DATA") , 9 ) ;
    int lastx = at->vf[id2].nrecords ;
    strange_compensation_factor = 0 ;
    
    if ( view_from > 0 ) diffx = get_bx ( id , view_from-1 ) * scalex ;

	bool first = true ;    
    for ( a = 0 ; a < s.length() ; a++ )
        {
		if ( view_from > 0 && a+1 < view_from ) continue ;
		if ( view_to > 0 && a >= view_to ) break ;
        int idx = a ;
        if ( inv_compl ) idx = s.length() - a - 1 ;

		int bx = get_bx ( id , idx ) ;
        if ( inv_compl ) bx = lastx - bx ;
        x = bx * scalex - diffx ;
        
        // Ugly hack for partial inverse/complementary view
        if ( first && view_from > 0 && inv_compl && x < 0 )
        	{
			strange_compensation_factor = wx - x ;
			x += strange_compensation_factor ;
			diffx -= strange_compensation_factor ;
			}
		first = false ;

        if ( x > maxx )
           {
           pos.addline ( lasta , pos.p.GetCount() , y , y+wy-1 ) ;
           lasta = pos.p.GetCount()+1 ;
           y += wy * ( can->seq.GetCount() + can->blankline ) ;
           if ( a+1 < s.length() )
              pos.add ( -(++l) , bo , y , ox-wx-5 , wy-1 ) ; // Line number
           diffx += maxx ;
           x = bx * scalex - diffx ;
           }

        pos.add ( a+1 , x + minx - wx / 2 , y , wx-1 , wy-1 ) ;
        can->setLowX ( x + minx + wx*2 ) ;
        lowy = y+wy ;

        }
    if ( lasta != pos.p.GetCount()+1 ) 
        pos.addline ( lasta , pos.p.GetCount() , y , y+wy-1 ) ;
    
    lowy += wy * ( can->blankline ) ; // For sequence data
    
    return lowy + bo*2 ;
    }
    
void SeqABI::setInvCompl ( bool x )
    {
    if ( inv_compl != x ) // Inverting and complementing sequence
        {
        TVector v2 ;
        wxString s2 ;
        for ( int a = 0 ; a < s.length() ; a++ ) s2 = ((wxChar)v2.getComplement( s.GetChar(a) ) ) + s2 ;
        s = s2 ;
        can->child->vec->setSequence ( s ) ;
        }
    
    inv_compl = x ;
    }
    
void SeqABI::show_sd ( wxDC& dc )
    {
    SeqDNA::show ( dc ) ;

    unsigned long  data ;

    // Char-to-curve (the thin grey lines;-)
    int wx = can->charwidth ;
    int wy = can->charheight ;
    dc.SetPen(*wxLIGHT_GREY_PEN);
    for ( int a = 0 ; showHelpLines && a < pos.p.GetCount() ; a++ )
        {
        if ( pos.p[a] > 0 )
           {
           int tx = pos.r[a].x + wx / 2 , ty = pos.r[a].y ;
           dc.DrawLine ( tx , ty + wy , tx , ty + wy * can->blankline ) ;
           }
        }

    // Determining height scaling factors
    int hy = ( can->blankline - 1 ) * can->charheight ;
    unsigned short int high = 0 ;
    
    for ( data = 0 ; data < 4 ; data++ )
        {
        for ( int a = 0 ; a < sd.tracer2[data].size() ; a++ )
           {
           unsigned short int b = sd.tracer2[data][a] ;
           if ( b > high ) high = b ;
           }
        }

//    int limit_abs_x = ( view_to > 0 ) ? get_bx ( at->getRecord ( _T("PLOC") , 2 ) , view_to ) * scalex : -1 ;
    int limit_abs_x = ( view_to > 0 ) ? get_bx ( 0 , view_to ) * scalex : -1 ;
//	int limit_abs_x = -1 ; //!!!!
    
    // Painting
    for ( data = 0 ; data < 4 ; data++ )
        {
        if ( can->isPrinting() && !can->getPrintToColor() )
           dc.SetPen ( *wxBLACK_PEN ) ;
        else
           dc.SetPen ( *MYPEN ( colors[data] ) ) ;
        
        long a ;
//        int id = at->getRecord ( _T("DATA") , 9 + data ) ;
//        myass ( id > -1 , "SeqABI::show_sd" ) ;
        long diffx = 0 ;
        long l = 0 ;
        wxPoint last ( minx , pos.l[l].height + hy ) ;
//		wxPoint last ( 0 , 0 ) ;
        long x = 0 , abs_x = 0 ;
        if ( view_from > 0 )
			{
//			x = -get_bx ( at->getRecord ( _T("PLOC") , 2 ) , view_from-1 ) * scalex ;
			x = -get_bx ( 0 , view_from-1 ) * scalex ;
			if ( inv_compl ) x += strange_compensation_factor ;
			}
        for ( a = 0 ; a < sd.tracer2[data].size() ; a++ )
           {
           int idx = a ;
           if ( inv_compl ) idx = ( sd.tracer2[data].size() - a - 1 ) ;
           int b = sd.tracer2[data][idx] ;
           x += scalex ;
           abs_x += scalex ;
           while ( x > maxx )
              {
              x -= maxx ;
              diffx += maxx ;
              if ( l > 0 ) drawTopLine ( dc , pos.l[l].width - wy / 2 ) ;
              l++ ;
              if ( l < pos.l.size() )
                 {
                 int yo = last.y - pos.l[l-1].height ;
                 last = wxPoint ( minx , pos.l[l].height + yo ) ;
                 }
              }
           wxPoint cur ;
           if ( l < pos.l.size() )
              {
              b *= zoom ;
              if ( b > high ) b = high ;
              cur = wxPoint ( x + minx , hy - b * hy / high + pos.l[l].height ) ;
              if ( limit_abs_x != -1 && abs_x >= limit_abs_x ) break ;
              if ( cur.x >= minx )
              	dc.DrawLine ( last , cur ) ;
              }
           last = cur ;
           }
        if ( l > 0 ) drawTopLine ( dc , pos.l[l].width - wy / 2 ) ;
        }
	}

void SeqABI::show ( wxDC& dc )
    {
	if ( !at ) { show_sd ( dc ) ; return ; }
    SeqDNA::show ( dc ) ;

    int data ;

    // Char-to-curve (the thin grey lines;-)
    int wx = can->charwidth ;
    int wy = can->charheight ;
    dc.SetPen(*wxLIGHT_GREY_PEN);
    for ( int a = 0 ; showHelpLines && a < pos.p.GetCount() ; a++ )
        {
        if ( pos.p[a] > 0 )
           {
           int tx = pos.r[a].x + wx / 2 , ty = pos.r[a].y ;
           dc.DrawLine ( tx , ty + wy , tx , ty + wy * can->blankline ) ;
           }
        }
    
    // Determining height scaling factors
    int hy = ( can->blankline - 1 ) * can->charheight ;
    int high = 0 ;
    for ( data = 0 ; data < 4 ; data++ )
        {
        int id = at->getRecord ( _T("DATA") , 9 + data ) ;
        for ( int a = 0 ; a < at->vf[id].nrecords ; a++ )
           {
           int b1 = (char) at->vf[id].data[a*2] ;
           int b2 = (unsigned char) at->vf[id].data[a*2+1] ;
           int b = b1 * 256 + b2 ;
           if ( b > high ) high = b ;
           }
        }
    
    int limit_abs_x = ( view_to > 0 ) ? get_bx ( at->getRecord ( _T("PLOC") , 2 ) , view_to ) * scalex : -1 ;
    
    // Painting
    for ( data = 0 ; data < 4 ; data++ )
        {
        if ( can->isPrinting() && !can->getPrintToColor() )
           dc.SetPen ( *wxBLACK_PEN ) ;
        else
           dc.SetPen ( *MYPEN ( colors[data] ) ) ;
        
        int a ;
        int id = at->getRecord ( _T("DATA") , 9 + data ) ;
        myass ( id > -1 , "SeqABI::show" ) ;
        int diffx = 0 ;
        int l = 0 ;
        wxPoint last ( minx , pos.l[l].height + hy ) ;
        int x = 0 , abs_x = 0 ;
        if ( view_from > 0 )
			{
			x = -get_bx ( at->getRecord ( _T("PLOC") , 2 ) , view_from-1 ) * scalex ;
			if ( inv_compl ) x += strange_compensation_factor ;
			}
        for ( a = 0 ; a < at->vf[id].nrecords ; a++ )
           {
           int idx = a ;
           if ( inv_compl ) idx = ( at->vf[id].nrecords - a - 1 ) ;
           int b = get_bx ( id , idx ) ;
/*           int b1 = (unsigned char) at->vf[id].data[idx] ;
           int b2 = (unsigned char) at->vf[id].data[idx+1] ;
           int b = b1 * 256 + b2 ;*/
           x += scalex ;
           abs_x += scalex ;
           while ( x > maxx )
              {
              x -= maxx ;
              diffx += maxx ;
              if ( l > 0 ) drawTopLine ( dc , pos.l[l].width - wy / 2 ) ;
              l++ ;
              if ( l < pos.l.size() )
                 {
                 int yo = last.y - pos.l[l-1].height ;
                 last = wxPoint ( minx , pos.l[l].height + yo ) ;
                 }
              }
           wxPoint cur ;
           if ( l < pos.l.size() )
              {
              b *= zoom ;
              if ( b > high ) b = high ;
              cur = wxPoint ( x + minx , hy - b * hy / high + pos.l[l].height ) ;
              if ( limit_abs_x != -1 && abs_x >= limit_abs_x ) break ;
              if ( cur.x >= minx )
              	dc.DrawLine ( last , cur ) ;
              }
           last = cur ;
           }
        if ( l > 0 ) drawTopLine ( dc , pos.l[l].width - wy / 2 ) ;
        }

    }
    
void SeqABI::drawTopLine ( wxDC &dc , int y )
    {
    wxPen p = dc.GetPen () ;
    dc.SetPen(*wxLIGHT_GREY_PEN);
    dc.DrawLine ( 4 , y , minx + maxx , y ) ;
    dc.SetPen(p);
    }
    
void SeqABI::initFromFile ( wxString filename )
    {
    zoom = 1 ;
    showHelpLines = true ;
    screenScale = 2 ;
    
    SCFtype sc ;
    if ( sc.parse ( filename ) ) // SCF
    	{
		if ( at ) delete at ;
		at = NULL ;
		sd = sc.sd ;
		s = sd.seq ;
		s.Replace ( _T(" ") , _T("N") ) ;
		s.Replace ( _T("-") , _T("N") ) ;
//		wxMessageBox ( s ) ;
		}
	else // ABI
		{
	    if ( !at ) at = new ABItype ;
	    at->parse ( filename ) ;
	    s = at->getSequence() ;
		}
    vec->setSequence ( s ) ;
    showNumbers = true ;
    fontColor.Set ( 0 , 0 , 0 ) ;
    }
    
wxColor SeqABI::getBaseColor ( char b )
    {
    for ( int a = 0 ; a < 4 ; a++ )
       {
       if ( b == base2color[a] )
          {
#ifdef __WXMAC__
          if ( !inv_compl ) return colors[a] ;
          else return colors[3-a] ;
#else
          if ( inv_compl ) return colors[a] ;
          else return colors[3-a] ;
#endif
          }
       }
    return wxColor ( _T("magenta") ) ;
    }