File: PlasmidCanvasCircular.cpp

package info (click to toggle)
gentle 1.9+cvs20100605+dfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 12,264 kB
  • ctags: 5,235
  • sloc: cpp: 41,571; ansic: 3,978; sh: 1,420; makefile: 291
file content (628 lines) | stat: -rwxr-xr-x 21,642 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include "PlasmidCanvas.h"

// DRAWING

int PlasmidCanvas::deg2x ( float deg , int r )
{
    float f = sin ( (180-deg)*PI/180 ) * r ;
    return int ( f ) ;
}

int PlasmidCanvas::deg2y ( float deg , int r )
{
    float f = cos ( (180-deg)*PI/180 ) * r ;
    return int ( f ) ;
}

float PlasmidCanvas::xy2deg ( float x , float y )
{
    float f ;
    f = atan2(x,y) ;
    f = 180-f*180/PI ;
    return f ;
}

float PlasmidCanvas::xy2r ( float x , float y )
{
    float f ;
    f = sqrt(x*x+y*y) ;
    return f ;
}

void PlasmidCanvas::arrangeRestrictionSitesCircular ( wxDC &dc )
    {
    int a ;
    int l = p->vec->getSequenceLength() ;
    if ( p->vec->rc.size() == 0 ) return ;
    
    vector <TRestrictionCut> trc ;
    trc.reserve ( p->vec->rc.size() ) ;
    for ( a = 0 ; a < p->vec->rc.size() ; a++ ) // Removing invisible
        {
        if ( p->vec->rc[a].isHidden ( p->vec ) )
           {
           trc.push_back ( p->vec->rc[a] ) ;
           p->vec->rc[a] = p->vec->rc[p->vec->rc.size()-1] ;
           p->vec->rc.pop_back() ;
           a-- ;
           }
        }
    
    p->vec->sortRestrictionSites() ;
    
    for ( a = 0 ; a < p->vec->rc.size() ; a++ ) // Init
        {
        TRestrictionCut *c = &p->vec->rc[a] ;
        if ( c->isHidden ( p->vec ) ) continue ;
        float angle = (float) c->getPos() * 360 / l ;
        c->angle = angle ;
        c->angle3 = angle ; //(float) (a+1) * 360 / p->vec->rc.size() ;
        c->r1 = STANDARDRADIUS ;
        c->r2 = STANDARDRADIUS*22/20 ;
        c->r3 = STANDARDRADIUS*(22+zoom/50)/20 ;
        
        recalc_rc ( a , dc ) ;
        }

    for ( a = 1 ; a < p->vec->rc.size() ; a++ ) // Ensure different angle
        {
        if ( p->vec->rc[a].isHidden ( p->vec ) ) continue ;
        if ( p->vec->rc[a-1].isHidden ( p->vec ) ) continue ;
        while ( p->vec->rc[a].angle3 <= p->vec->rc[a-1].angle3 )
           p->vec->rc[a].angle3 += 0.001 ;
        }

    bool redo = true ;
    int cnt = 200 ;
    while ( redo && cnt > 0 )
        {
        redo = false ;    
        for ( a = 0 ; a < p->vec->rc.size() ; a++ ) // Optimize
           redo |= optimizeCircularRestrictionSites ( a , dc ) ;
        cnt-- ;
        }
        
    // Appending hidden ones
    p->vec->rc.reserve ( trc.size() ) ;
    for ( a = 0 ; a < trc.size() ; a++ )
        p->vec->rc.push_back ( trc[a] ) ;
    p->vec->sortRestrictionSites() ;
    }
    
bool PlasmidCanvas::optimizeCircularRestrictionSites ( int a , wxDC &dc )
    {
    TRestrictionCut *c = &p->vec->rc[a] ;
    if ( p->vec->rc[a].isHidden ( p->vec ) ) return false ;
    bool ret = false ;
    if ( a > 0 && !p->vec->rc[a-1].isHidden ( p->vec ) &&
            intersects ( c->lastrect , p->vec->rc[a-1].lastrect ) )
        {
        push_rc_left ( a-1 , dc ) ; 
        push_rc_right ( a , dc ) ; 
        ret = true ; 
        }
    if ( a < p->vec->rc.size()-1 && !p->vec->rc[a+1].isHidden ( p->vec ) &&
            intersects ( c->lastrect , p->vec->rc[a+1].lastrect ) )
        {
        push_rc_right ( a+1 , dc ) ; 
        push_rc_left ( a , dc ) ; 
        ret = true ; 
        }
    return ret ;
    }
    
void PlasmidCanvas::push_rc_left ( int a , wxDC &dc )
    {
    if ( p->vec->rc.size() < 2 ) return ;
    TRestrictionCut *c = &p->vec->rc[a] ;
    c->angle3 -= 1 ;
    recalc_rc ( a , dc ) ;
    float add = 0 ;
    int b = a - 1 ;
    if ( a == 0 ) { b = p->vec->rc.size()-1 ; add = 360 ; }
    TRestrictionCut *cl = &p->vec->rc[b] ;
    while ( c->angle3 + add < cl->angle3 ) push_rc_left ( b , dc ) ;
    }
    
void PlasmidCanvas::push_rc_right ( int a , wxDC &dc )
    {
    if ( p->vec->rc.size() < 2 ) return ;
    TRestrictionCut *c = &p->vec->rc[a] ;
    c->angle3 += 1 ;
    recalc_rc ( a , dc ) ;
    float add = 0 ;
    int b = a + 1 ;
    if ( a == p->vec->rc.size()-1 ) { b = 0 ; add = 360 ; }
    TRestrictionCut *cr = &p->vec->rc[b] ;
    while ( c->angle3 > cr->angle3 + add ) push_rc_right ( b , dc ) ;
    }
    
void PlasmidCanvas::recalc_rc ( int a, wxDC &dc )
    {
    TRestrictionCut *c = &p->vec->rc[a] ;
    wxPoint p2 ( deg2x ( c->angle , (int)c->r2 )+w/2 , deg2y ( c->angle , (int)c->r2 )+h/2 ) ;
    wxPoint p3 ( deg2x ( c->angle3 , (int)c->r3 )+w/2 , deg2y ( c->angle3 , (int)c->r3 )+h/2 ) ;
    c->lp = p3 ;
    makeLastRect ( a , dc ) ;    
    }
    
void PlasmidCanvas::drawCircularORFs ( wxDC &dc )
    {
    int a ;
    int l = p->vec->getSequenceLength() ;
    for ( a = 0 ; a < p->vec->countORFs() ; a++ )
        {
        float mf = p->vec->getORF(a)->get_from() ;
        float mt = p->vec->getORF(a)->get_to() ;
        int rf = p->vec->getORF(a)->get_rf() ;
        
        if ( mf > mt )
        {
        mt += l ;
        }
        
        float ro , roi = r / 10 ;
        ro = roi * 7 + roi * rf ;
        
        mf *= 360 ;
        mt *= 360 ;
        mf /= (float) l ;
        mt /= (float) l ;

        wxColour col ;
        if ( rf == 1 || rf == -1 ) col = wxColour ( 200 , 0 , 0 ) ;
        if ( rf == 2 || rf == -2 ) col = wxColour ( 0 , 200 , 0 ) ;
        if ( rf == 3 || rf == -3 ) col = wxColour ( 0 , 0 , 200 ) ;
        dc.SetBackgroundMode ( wxSOLID ) ;
        dc.SetBrush ( *MYBRUSH ( col ) ) ;
        dc.SetPen ( *MYPEN ( col ) ) ;

        int dir ;
        float mm , mn ;
        if ( rf < 0 )
           {
           dir = -1 ;
           mm = mf ;
           mn = mt ;
           }
        else
           {
           dir = 1 ;
           mm = mt ;
           mn = mf ;
           }
        dc.DrawCircle ( deg2x ( mn , (int)(r+ro/2) ) + w/2 ,
                        deg2y ( mn , (int)(r+ro/2) ) + h/2 ,
                        4 ) ;
        dc.DrawLine ( deg2x ( mm , (int)(r+ro/2) ) + w/2 ,
                      deg2y ( mm , (int)(r+ro/2) ) + h/2 ,
                      deg2x ( mm - dir*2 , (int)(r+ro/2-roi/4) ) + w/2 ,
                      deg2y ( mm - dir*2 , (int)(r+ro/2-roi/4) ) + h/2 ) ;
        dc.DrawLine ( deg2x ( mm , (int)(r+ro/2) ) + w/2 ,
                      deg2y ( mm , (int)(r+ro/2) ) + h/2 ,
                      deg2x ( mm - dir*2 , (int)(r+ro/2+roi/4) ) + w/2 ,
                      deg2y ( mm - dir*2 , (int)(r+ro/2+roi/4) ) + h/2 ) ;
                      
        dc.SetBackgroundMode ( wxTRANSPARENT ) ;    
        dc.SetBrush ( *wxTRANSPARENT_BRUSH ) ;    
        dc.DrawEllipticArc ( (int)(w/2-r-ro/2) , (int)(h/2-r-ro/2) , (int)(r*2+ro) , (int)(r*2+ro) , 90-mt , 90-mf ) ;

        p->vec->getORF(a)->dist1 = r+ro/2-roi/4 ;
        p->vec->getORF(a)->dist2 = r+ro/2+roi/4 ;
        p->vec->getORF(a)->deg1 = mf;
        p->vec->getORF(a)->deg2 = mt ;
        if ( mt < mf ) p->vec->getORF(a)->deg2 = mt + 360 ;
        }
    dc.SetPen(*wxBLACK_PEN);
    }

void PlasmidCanvas::OnDrawCircular(wxDC& dc)
{
    // Initial calculations    
    char t[10000] ;
    wxCoord dx , dy ;
    int l = p->vec->getSequenceLength();
    int a , mwh ;
    mwh = w<h?w:h ;

    if ( printing ) mwh -= mwh / 5 ;

    int fontfactor = MYFONTSIZE * 10 / 8 ;
    if ( printing ) fontfactor = mwh/80;
    wxFont *tinyFont = MYFONT ( fontfactor*4/5 , wxSWISS , wxNORMAL , wxNORMAL ) ;
    wxFont *smallFont = MYFONT ( fontfactor , wxSWISS , wxNORMAL , wxNORMAL ) ;
    wxFont *normalFont = MYFONT ( fontfactor*6/5 , wxSWISS , wxNORMAL , wxNORMAL ) ;
    wxFont *bigFont = MYFONT ( fontfactor*7/5 , wxSWISS , wxNORMAL , wxNORMAL ) ;
//    wxFont *hugeFont = MYFONT ( fontfactor*9/5 , wxSWISS , wxNORMAL , wxBOLD ) ;
    
    int d ;
    for ( d = 1 ; d*10 < l ; d *= 10 ) ;
    r = (mwh*2/3)/2 ;

    // Basic elements
    dc.SetPen(*wxBLACK_PEN);
    if ( printing ) dc.SetBrush ( *wxWHITE_BRUSH ) ;
    
    // Baseline
    dc.SetBackgroundMode ( wxTRANSPARENT ) ;
    if ( p->vec->showGC() > 0 ) // %GC
    	{
        int r1 = r + r/40 ;
        int r2 = r - r/40 ;
//    	dc.DrawEllipse ( w/2-r - r/40 , h/2-r - r/40 , r*2 + r*2/40 , r*2 + r*2/40 ) ;
//	    int dh = h / 80 ;
	    int b , nob = p->vec->showGC() ;
	    for ( a = 0 ; a < nob ; a++ )
	    	{
 	    	int at = 0 , gc = 0 , other = 0 ;
	    	for ( b = l * a / nob ; b < l * ( a + 1 ) / nob ; b++ )
	    		{
 		        char c = p->vec->getSequenceChar ( b ) ;
 		    	if ( c == 'A' || c == 'T' ) at++ ;
 		    	else if ( c == 'G' || c == 'C' ) gc++ ;
 		    	else other++ ;
	    		}    
    		int sum = at + gc + other ;
    		if ( sum == 0 ) continue ;
    		int per = gc * 100 / sum ;
    		wxColour col ;
    		makeGCcolor ( per , col ) ;
    		dc.SetPen(*MYPEN(col));
    		dc.SetBrush(*MYBRUSH(col));

            // Drawing polygon
            vector <wxPoint> p ;
            int b = a + 1 , c ;
            p.push_back ( wxPoint ( deg2x ( 360*a/nob , r1 ) , deg2y ( 360*a/nob , r1 ) ) ) ;
            for ( c = int(360*a/nob) ; c%361 != int(360*b/nob)%361 ; c++ )
            	p.push_back ( wxPoint ( deg2x ( c , r1 ) , deg2y ( c , r1 ) ) ) ;
            p.push_back ( wxPoint ( deg2x ( 360*b/nob , r1 ) , deg2y ( 360*b/nob , r1 ) ) ) ;
            p.push_back ( wxPoint ( deg2x ( 360*b/nob , r2 ) , deg2y ( 360*b/nob , r2 ) ) ) ;
            for ( c = int(360*b/nob) ; c%361 != int(360*a/nob)%361 ; c-- )
            	p.push_back ( wxPoint ( deg2x ( c , r2 ) , deg2y ( c , r2 ) ) ) ;
            p.push_back ( wxPoint ( deg2x ( 360*a/nob , r2 ) , deg2y ( 360*a/nob , r2 ) ) ) ;
            p.push_back ( wxPoint ( deg2x ( 360*a/nob , r1 ) , deg2y ( 360*a/nob , r1 ) ) ) ;
            wxPoint *wp ;
            wp = (wxPoint*) malloc ( sizeof ( wxPoint ) * (p.size()+1) ) ;
            for ( b = 0 ; b < p.size() ; b++ ) wp[b] = p[b] ;
            dc.DrawPolygon ( p.size() , wp , w/2 , h/2 ) ;
            free ( wp ) ;

        	dc.SetPen(*wxBLACK_PEN);
        	dc.DrawLine ( w/2+p[0].x , h/2+p[0].y , w/2+p[1].x , h/2+p[1].y ) ;
        	dc.DrawLine ( w/2+p[2].x , h/2+p[2].y , w/2+p[3].x , h/2+p[3].y ) ;
	    	}
	    showGClegend ( dc ) ;
    	dc.SetPen(*wxBLACK_PEN);
    	dc.SetBackgroundMode ( wxTRANSPARENT ) ;
    	dc.SetBrush ( *wxWHITE_BRUSH ) ;
//    	dc.DrawEllipse ( w/2-r + r/40 , h/2-r + r/40 , r*2 - r*2/40 , r*2 - r*2/40 ) ;
    	}
    else
    	{    
    	dc.DrawEllipse ( w/2-r , h/2-r , r*2 , r*2 ) ;
     	}   	
    dc.SetBackgroundMode ( wxSOLID ) ;
    
    // Marking
    if ( getMarkFrom() != -1 )
        {
        float mt = getMarkFrom() ;
        float mf = getMarkTo() ;
        mt /= (float) l ;
        mf /= (float) l ;
        mt *= 360 ;
        mf *= 360 ;
        mt = 90 - mt ;
        mf = 90 - mf ;
        dc.SetBackgroundMode ( wxSOLID ) ;
        dc.SetBrush ( *wxLIGHT_GREY_BRUSH ) ;
        if ( getMarkFrom() == getMarkTo() ) mf = mt - 0.1 ;
        else if ( mf > mt ) { float mm = mf ; mf = mt ; mt = mm ; }
        int r1 = p->vec->showGC() > 0 ? r - r/40 : r ;
		dc.DrawEllipticArc ( w/2-r1 , h/2-r1 , r1*2 , r1*2 , mf , mt ) ;
        dc.DrawLine ( w/2 , h/2 , deg2x(90-mf,r1)+w/2 , deg2y(90-mf,r1)+h/2 ) ;
        dc.DrawLine ( w/2 , h/2 , deg2x(90-mt,r1)+w/2 , deg2y(90-mt,r1)+h/2 ) ;
        dc.SetPen(*wxBLACK_PEN);
        dc.SetBackgroundMode ( wxTRANSPARENT ) ;
        }

    // Show title
    if ( myapp()->frame->showVectorTitle )
        {
        char *c1 , *c2 ;
        strcpy ( t , p->vec->getName().mb_str() ) ;
        strcat ( t , " " ) ;
        a = 0 ;
        dc.SetFont ( *bigFont ) ;
        dc.GetTextExtent ( wxString ( t , *wxConvCurrent ) , &dx , &dy ) ;
        for ( c1 = t ; *c1 ; c1++ )
           if ( *c1 == ' ' ) a -= dy ;
        for ( c1 = c2 = t ; *c1 ; c1++ )
           {
           if ( *c1 == ' ' )
              {
              *c1 = 0 ;
				  wxString t2 ( c2 , *wxConvCurrent ) ;
              dc.GetTextExtent ( t2 , &dx , &dy ) ;
              dc.DrawText ( t2 , w/2-dx/2 , h/2-dy/2+a ) ;
              c2 = c1+1 ;
              a += dy ;
              }
           }
        }

	 wxString t2 ;
	 
    // Show length
    if ( myapp()->frame->showVectorLength )
        {
		  t2 = wxString::Format ( txt("#bp") , p->vec->getSequenceLength() ) ;
//        sprintf ( t , txt("#bp") , p->vec->getSequenceLength() ) ;
        dc.SetFont ( *normalFont ) ;
        dc.GetTextExtent ( t2 , &dx , &dy ) ;
        dc.DrawText ( t2 , w/2-dx/2 , h/2-dy/2 ) ;
        }
    
    // Numbers
    dc.SetFont(*smallFont);
    dc.SetBackgroundMode ( wxTRANSPARENT ) ;
    
	 for ( a = 0 ; a <= l ; a += d )
        {
            float deg = a*360/l ;
            int r1 = r*17/20 ;
            int r2 = r*20/20 ;
            dc.DrawLine ( deg2x ( deg , r1 ) + w/2 ,
                          deg2y ( deg , r1 ) + h/2 ,
                          deg2x ( deg , r2 ) + w/2 ,
                          deg2y ( deg , r2 ) + h/2 ) ;
                          
            // Numbers
				t2 = wxString::Format ( _T("%d") , a ) ;
//				sprintf ( t , "%d" , a ) ;
            dc.GetTextExtent ( t2 , &dx , &dy ) ;
            if ( deg > 180 && deg < 350 ) dx = 0 ;
            if ( deg < 15 ) dx = 0 ;
            if ( deg < 90 || deg > 270 ) dy = 0 ;
            if ( a > 0 )
               dc.DrawText ( t2 ,
                             deg2x ( deg , r1 ) + w/2 - dx ,
                             deg2y ( deg , r1 ) + h/2 - dy ) ;
        }

    // Methylation sites
    dc.SetPen(*wxRED_PEN);
    for ( a = 0 ; a < p->vec->countMethylationSites() ; a++ )
    	{
        float deg = p->vec->getMethylationSite(a)*360/l ;
        int r1 = r*19/20 ;
        int r2 = r*20/20 ;
        dc.DrawLine ( deg2x ( deg , r1 ) + w/2 ,
                      deg2y ( deg , r1 ) + h/2 ,
                      deg2x ( deg , r2 ) + w/2 ,
                      deg2y ( deg , r2 ) + h/2 ) ;	    
    	}    
    dc.SetPen(*wxBLACK_PEN);

    if ( p->vec->displayUpdate() )
        {
        // Items (Genes etc.)
        for ( a = 0 ; a < p->vec->items.size() ; a++ )
            {
            TVectorItem *i = &p->vec->items[a] ;
            float df = i->from*360.0/((float)l) ;
            float dt = i->to*360.0/((float)l) ;
            if ( dt < df ) dt += 360.0 ;
//            float dd = (dt-df)*5.0/100.0 ; // Arrow length in degrees
//            float ds = 1 ; // Paint steps for genes, in degrees
            int tf = 19 ;
            tf += ( i->type % 3 ) * 2 - 2 ;
            int r1 = STANDARDRADIUS*tf/20 ;
            int r2 = STANDARDRADIUS*(tf+2)/20 ;
            if ( i->r1 == -1 || i->r2 == -1 )
               {
               i->r1 = r1 ;
               i->r2 = r2 ;
               }
            i->a1 = df ;
            i->a2 = dt ;
            }

        // Restriction sites
        dc.SetFont ( *tinyFont ) ;
        arrangeRestrictionSitesCircular ( dc ) ;
        dc.SetFont ( *smallFont ) ;

        // ORFs
        if ( p->showORFs )
           {
           p->showORFs = false ;
           wxCommandEvent event ;
           p->OnORFs ( event ) ;
           }

        p->vec->updateDisplay ( false ) ;
        }

    // ORFs
    drawCircularORFs ( dc ) ;
         
    // Drawing items
    for ( a = 0 ; a < p->vec->items.size() ; a++ )
        {
        TVectorItem *i = &p->vec->items[a] ;
        if ( i->isVisible() )
            {
            int r1 , r2 ;
            float df , dt ;
            r1 = i->r1 * 100 * r / ( STANDARDRADIUS * 100 ) ;
            r2 = i->r2 * 100 * r / ( STANDARDRADIUS * 100 ) ;
            df = i->a1 ;
            dt = i->a2 ;
            
            float dd = (dt-df)*5/100 ; // Arrow length in degrees
            float ds = 1 ; // Paint steps for genes, in degrees
            
            // Calculating polygon
            vector <wxPoint> p ;
            
            if ( i->getParam ( _T("AUTOMATIC") ) == _T("SEQUENCING PRIMER") )
            	dd = ( dt - df ) / 2 ;
            
            if ( i->direction ==  1 && abs ( (int) ( dt - df ) ) > 3 ) 
                {
                dt -= dd ;
                p.push_back ( wxPoint ( deg2x ( df+dd , (r1+r2)/2 ) , 
                                        deg2y ( df+dd , (r1+r2)/2 ) ) ) ;
                }
            if ( i->direction == -1 ) 
                {
                df += dd ;
                p.push_back ( wxPoint ( deg2x ( df-dd , (r1+r2)/2 ) , 
                                        deg2y ( df-dd , (r1+r2)/2 ) ) ) ;
                }

            p.reserve ( (int)(( dt - df ) / ds * 2 + 10) ) ;
            for ( float b = df ; b < dt ; b += ds )
                    p.push_back ( wxPoint ( deg2x ( b , r1 ) , deg2y ( b , r1 ) ) ) ;
                    
                    
            if ( i->direction != 0 )
            	p.push_back ( wxPoint ( deg2x ( dt+dd*i->direction , (r1+r2)/2 ) , 
             							deg2y ( dt+dd*i->direction , (r1+r2)/2 ) ) ) ;

            for ( float b = dt ; b > df ; b -= ds )
                    p.push_back ( wxPoint ( deg2x ( b , r2 ) , deg2y ( b , r2 ) ) ) ;

            // Drawing polygon
            wxPoint *wp ;
            wp = (wxPoint*) malloc ( sizeof ( wxPoint ) * (p.size()+1) ) ;
            for ( int b = 0 ; b < p.size() ; b++ ) wp[b] = p[b] ;
            if ( !this->p->vec->getGenomeMode() ) dc.SetPen(*wxBLACK_PEN);
            else dc.SetPen ( *MYPEN((wxColour)i->getBrush()->GetColour()) ) ;
            dc.SetBrush ( *(i->getBrush()) ) ;
            dc.DrawPolygon ( p.size() , wp , w/2 , h/2 ) ;
            free ( wp ) ;
            }
        }

    for ( a = 0 ; !p->vec->getGenomeMode() && a < p->vec->items.size() ; a++ ) // Item titles
        {
        TVectorItem *i = &p->vec->items[a] ;
        if ( i->isVisible() && i->getParam ( _T("PREDECESSOR") ).IsEmpty() )
            {
            int r1 = i->r1 * 100 * r / ( STANDARDRADIUS * 100 ) ;
//            int r2 = i->r2 * 100 * r / ( STANDARDRADIUS * 100 ) ;
            float df = i->a1 ;
            float dt = i->a2 ;
            
            float dd = (dt-df)*5/100 ; // Arrow length in degrees
//            float ds = 1 ; // Paint steps for genes, in degrees
            // Drawing name
            wxColor fc = dc.GetTextForeground () ;
            dc.SetTextForeground ( i->getFontColor() ) ;
            dc.SetFont(*normalFont);
            dc.GetTextExtent ( i->name , &dx , &dy ) ;
            dd = (df+(dt-df)/2) ;
            while ( dd >= 360 ) dd -= 360 ;
            if ( dd > 180 ) dx = 0 ;
            if ( dd < 90 || dd > 270 ) dy = 0 ;
            dc.DrawText ( i->name ,
                          w/2+deg2x(dd,r1-2)-dx ,
                          h/2+deg2y(dd,r1-2)-dy ) ;
            dc.SetTextForeground ( fc ) ;
            dc.SetPen(*wxBLACK_PEN);
            }
        }

    // Restriction sites        
    dc.SetFont ( *tinyFont ) ;
    for ( a = 0 ; a < p->vec->rc.size() ; a++ )
        {
        TRestrictionCut c = p->vec->rc[a] ;
        if ( isEnzymeVisible ( c.e->getName() ) )
           {
           wxColour *col = p->vec->getEnzymeRule()->getColor ( p->vec->countCuts ( c.e->getName() ) ) ;
           dc.SetTextForeground ( *col ) ;
           dc.SetPen ( *MYPEN(*col) ) ;
           c.r1 = c.r1 * 100 * r / ( STANDARDRADIUS * 100 ) ;
           c.r2 = c.r2 * 100 * r / ( STANDARDRADIUS * 100 ) ;
           c.r3 = c.r3 * 100 * r / ( STANDARDRADIUS * 100 ) ;
           wxPoint p1 ( deg2x ( c.angle , (int)c.r1 )+w/2 , deg2y ( c.angle , (int)c.r1 )+h/2 ) ;
           wxPoint p2 ( deg2x ( c.angle , (int)c.r2 )+w/2 , deg2y ( c.angle , (int)c.r2 )+h/2 ) ;
           dc.DrawLine ( p1 , p2 ) ;
           wxPoint p3 = p->vec->rc[a].lp ;
            
           p3 = makeLastRect ( a , dc ) ;
           if ( p3.x < w/2 )
              p3.x = p->vec->rc[a].lastrect.GetRight() ;
           p3.y = ( p->vec->rc[a].lastrect.GetTop() + p->vec->rc[a].lastrect.GetBottom() ) / 2 ;
                    
           dc.DrawLine ( p2 , p3 ) ;
    
           wxString u = c.getNameAndPosition () ;
//           char u[100] ;
//           sprintf ( u , "%s (%d)" , c.e->name.c_str() , c.pos ) ;
           p3.x = p->vec->rc[a].lastrect.GetLeft() ;
           p3.y = p->vec->rc[a].lastrect.GetTop() ;

           dc.DrawText ( u , p3.x , p3.y ) ;
           }    
        }
    dc.SetFont(*smallFont);    
    dc.SetPen ( *wxBLACK_PEN ) ;
    dc.SetTextForeground ( *wxBLACK ) ;
    }

int PlasmidCanvas::findORFcircular ( float angle , float radius )
    {
    int a , found = -1 ;
    for ( a = 0 ; a < p->vec->countORFs() ; a++ )
        {
        if ( angle >= p->vec->getORF(a)->deg1 &&
             angle <= p->vec->getORF(a)->deg2 &&
             radius >= p->vec->getORF(a)->dist1 &&
             radius <= p->vec->getORF(a)->dist2 )
             found = a ;
        if ( p->vec->getORF(a)->deg2 > 360 &&
             angle <= p->vec->getORF(a)->deg2-360 &&
             radius >= p->vec->getORF(a)->dist1 &&
             radius <= p->vec->getORF(a)->dist2 )
             found = a ;
        }
    return found ;
    }

wxPoint PlasmidCanvas::makeLastRect ( int a , wxDC &dc )
    {
    wxPoint p3 = p->vec->rc[a].lp ;
    p3.x = p3.x * 100 * r / ( STANDARDRADIUS * 100 ) + w/2 ;
    p3.y = p3.y * 100 * r / ( STANDARDRADIUS * 100 ) + h/2 ;

//    char u[100] ;
//    sprintf ( u , "%s (%d)" , p->vec->rc[a].e->name.c_str() , p->vec->rc[a].pos ) ;
	wxString u = p->vec->rc[a].getNameAndPosition () ;
    int te_x , te_y ;
    dc.GetTextExtent ( u , &te_x , &te_y ) ;
    p3.y -= te_y / 2 ;
//    if ( p3.x < w/2 ) p3.x -= te_x + 4 ;
    if ( p->vec->rc[a].angle3 > 180 ) p3.x -= te_x + 4 ;
    else p3.x += 4 ;
    p->vec->rc[a].lastrect = wxRect ( p3.x , p3.y , te_x , te_y ) ;

    return p3 ;
    }
    
int PlasmidCanvas::circular_pos ( float angle )
    {
    int l = p->vec->getSequenceLength() ;
    float a = l ;
    a /= 360.0 ;
    a *= angle ;
    int b = (int) a ;
    if ( b < 0 ) b = 0 ;
    if ( b >= l ) b = l - 1 ;
    return b+1 ;
    }