File: gxydraw.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (500 lines) | stat: -rw-r--r-- 13,495 bytes parent folder | download | duplicates (13)
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
/*
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name: dxydraw.c
*
* Author(s): John Kuzio
*
* Version Creation Date: 98-01-01
*
* $Revision: 6.6 $
*
* File Description: sentinel dot plots
*
* Modifications:
* --------------------------------------------------------------------------
* Date       Name        Description of modification
* --------------------------------------------------------------------------
* $Log: gxydraw.c,v $
* Revision 6.6  1999/10/04 17:46:23  kans
* include vibrant.h
*
* Revision 6.5  1998/09/16 19:00:37  kuzio
* cvs logs
*
* ==========================================================================
*/

#include <vibrant.h>
#include <seqport.h>
#include <drawingp.h>
#include <gxydraw.h>

/* common */

static void CleanGSP (BigScalar calldata)
{
  GraphXYSentPtr gsp;

  if ((gsp = (GraphXYSentPtr) calldata) == NULL)
    return;
  MemFree (gsp->endpointarray);
  MemFree (gsp->endpointvalues);
  MemFree (gsp);
  return;
}

static void TickLabel (Int4 tickvalue, Int2 xcoord, Int2 ycoord)
{
  Char numberbuffer[32];

  sprintf (numberbuffer, "%8ld", (long) tickvalue);
  PaintStringEx (numberbuffer, xcoord, ycoord);

  return;
}

/* dot plot */

static void PlotTheDotXYPlot (Int4Ptr epa, Int4 xaxis, Int4 yaxis,
                              Int4 xscale, Int4 yscale, Int4Ptr endpoints,
                              Int4 cutoff, AttPData PNTR curattrib,
                              Boolean flagIsGUI)
{
  Int4    ix, iy;
  PoinT   point;
  Int4    xoffset, yoffset, xcorrect, ycorrect;
  Int4    loop, curcut;
  Uint1   curR, curG, curB;
  Int4    window;
  Int4Ptr curepa;

  xoffset = 50;
  yoffset = 50;

  curR = curattrib->color[0];
  curG = curattrib->color[1];
  curB = curattrib->color[2];

  window = 8;
  curepa = epa;
  for (loop = 0; loop <= window; loop++)
  {
    if (loop >= cutoff)
    {
      epa = curepa;
      curcut = loop * ((256/window) - 1);
      curcut = 255 - curcut;
      if (curcut < 0)
        curcut = 0;
      Nlm_SelectColor (255, (Uint1) curcut, 0);
      for (iy = 0; iy < yaxis; iy++)
      {
        for (ix = 0; ix < xaxis; ix++)
        {
          if (*epa == loop)
          {
            point.x = (Int2) (ix + ((window-1)/xscale) + xoffset);
            point.y = (Int2) (iy + ((window-1)/yscale) + yoffset);
            DrawLine (point, point);
          }
          epa++;
        }
      }
    }
  }
  Nlm_SelectColor (curR, curG, curB);

  if (endpoints != NULL)
  {
    if (flagIsGUI)
    {
      xcorrect = 25;
      ycorrect = 50;
    }
    else
    {
      xcorrect = 50;
      ycorrect = 60;
    }
    TickLabel (endpoints[0], (Int2) (xoffset-xcorrect),
                             (Int2) (yoffset-10));
    TickLabel (endpoints[1], (Int2) (xoffset-xcorrect+xaxis),
                             (Int2) (yoffset-10));
    TickLabel (endpoints[2], (Int2) (xoffset-ycorrect),
                             (Int2) (yoffset+10));
    TickLabel (endpoints[3], (Int2) (xoffset-ycorrect),
                             (Int2) (yoffset+yaxis));
  }

  return;
}

static void DotXYSentProc (BigScalar calldata, PrimDrawContext pdc)
{
  GraphXYSentPtr gsp;
  DrawInfoPtr    dInfoPtr;

  if ((gsp = (GraphXYSentPtr) calldata) == NULL)
    return;
  if (gsp->endpointarray == NULL)
    return;

  dInfoPtr = (DrawInfoPtr) pdc;
  PlotTheDotXYPlot (gsp->endpointarray, gsp->xaxiscount, gsp->yaxiscount,
                    gsp->xscale, gsp->yscale, gsp->endpointvalues,
                    gsp->dotcutoff, &(dInfoPtr->curattrib),
                    gsp->flagIsGUI);
  return;
}

static GraphXYSentPtr AddDotXYSentinelToPicture (BioseqPtr bspT,
                                                 BioseqPtr bspP,
                                                 SegmenT seg,
                                                 Int4 cutoff,
                                                 Int4 xscale,
                                                 Int4 yscale,
                                                 Boolean flagLabels)
{
  GraphXYSentPtr  gsp;
  Int4            xaxiscount, yaxiscount;
  Int4            i, imatch, ix, iy, xstop, ystop, ixs, iys;
  Int4            window;
  Int4Ptr         epa, epap;
  SeqPortPtr      spp;
  Uint1Ptr        seqT, seqP, pseqT, pseqP;

  if (bspT == NULL || bspP == NULL || seg == NULL)
    return NULL;
  if ((gsp = (GraphXYSentPtr) MemNew (sizeof (GraphXYSentData))) == NULL)
    return NULL;
  gsp->flagIsGUI = VibrantIsGUI ();

  if ((epa = (Int4Ptr) MemNew (sizeof (Int4) * bspT->length * bspP->length))
      == NULL)
  {
    MemFree (gsp);
    return NULL;
  }
  gsp->endpointarray = epa;

  seqT = (Uint1Ptr) MemNew (sizeof (Uint1) * (bspT->length + 1));
  seqP = (Uint1Ptr) MemNew (sizeof (Uint1) * (bspP->length + 1));

  spp = SeqPortNew (bspT, 0, bspT->length-1, 0, Seq_code_iupacaa);
  SeqPortRead (spp, seqT, (Int2) bspT->length);
  SeqPortFree (spp);
  spp = SeqPortNew (bspP, 0, bspP->length-1, 0, Seq_code_iupacaa);
  SeqPortRead (spp, seqP, (Int2) bspP->length);
  SeqPortFree (spp);

  window = 8;
  xstop = bspT->length - window;
  ystop = bspP->length - window;
  yaxiscount = 0;
  iy = 0;
  while (iy < ystop)
  {
    epap = epa;
    for (iys = 0; iys < yscale; iys++)
    {
      epa = epap;
      xaxiscount = 0;
      ix = 0;
      while (ix < xstop)
      {
        for (ixs = 0; ixs < xscale; ixs++)
        {
          imatch = 0;
          pseqT = &(seqT[ix]);
          pseqP = &(seqP[iy]);
          for (i = 0; i < window; i++)
          {
            if (*pseqT == *pseqP)
              imatch++;
            pseqT++;
            pseqP++;
          }
          if (imatch > *epa)
            *epa = imatch;
          ix++;
          if (ix >= xstop)
            break;
        }
        epa++;
        xaxiscount++;
      }
      iy++;
      if (iy >= ystop)
        break;
    }
    yaxiscount++;
  }

  MemFree (seqT);
  MemFree (seqP);

  gsp->xaxiscount = xaxiscount;
  gsp->yaxiscount = yaxiscount;
  gsp->dotcutoff = cutoff;
  gsp->xscale = xscale;
  gsp->yscale = yscale;
  if (flagLabels)
  {
    gsp->endpointvalues = (Int4Ptr) MemNew (sizeof (Int4) * 4);
    gsp->endpointvalues[0] = 0;
    gsp->endpointvalues[1] = bspT->length;
    gsp->endpointvalues[2] = 0;
    gsp->endpointvalues[3] = bspP->length;
  }
  gsp->snt = AddSntRectangle (seg, 0, 0, 400, 400,
                              DotXYSentProc, (BigScalar) gsp,
                              CleanGSP, 0);
  return gsp;
}

/* align */

static void PlotTheXYPlot (Int4Ptr epa, Int4 linenumber,
                           Int4 xaxis, Int4 yaxis, Int4Ptr endpoints,
                           AttPData PNTR curattrib, Boolean flagIsGUI,
                           Uint1Ptr color)
{
  Int4  i;
  Int4  startT, startP, stopT, stopP;
  Int4  xoffset, yoffset, xcorrect, ycorrect;
  Uint1 curR, curG, curB;
  Int1  penW;

  xoffset = 50;
  yoffset = 50;

  curR = curattrib->color[0];
  curG = curattrib->color[1];
  curB = curattrib->color[2];
  penW = curattrib->penwidth;

  Nlm_SelectColor (color[0], color[1], color[2]);
  Nlm_WidePen (2);

  for (i = 0; i < linenumber; i++)
  {
    startT = *epa++ + xoffset;
    startP = *epa++ + yoffset;
    stopT = *epa++ + xoffset;
    stopP = *epa++ + yoffset;
    MoveTo ((Int2) startT, (Int2) startP);
    LineTo ((Int2) stopT, (Int2) stopP);
    if (!flagIsGUI)
    {
      MoveTo ((Int2) (startT-1), (Int2) (startP+1));
      LineTo ((Int2) (stopT-1), (Int2) (stopP+1));
      MoveTo ((Int2) (startT+1), (Int2) (startP-1));
      LineTo ((Int2) (stopT+1), (Int2) (stopP-1));
    }
  }

  Nlm_SelectColor (curR, curG, curB);
  Nlm_WidePen (penW);

  if (endpoints != NULL)
  {
    if (flagIsGUI)
    {
      xcorrect = 25;
      ycorrect = 50;
    }
    else
    {
      xcorrect = 50;
      ycorrect = 60;
    }
    TickLabel (endpoints[0], (Int2) (xoffset-xcorrect),
                             (Int2) (yoffset-10));
    TickLabel (endpoints[1], (Int2) (xoffset-xcorrect+xaxis),
                             (Int2) (yoffset-10));
    TickLabel (endpoints[2], (Int2) (xoffset-ycorrect),
                             (Int2) (yoffset+10));
    TickLabel (endpoints[3], (Int2) (xoffset-ycorrect),
                             (Int2) (yoffset+yaxis));
  }

  return;
}

static void XYSentProc (BigScalar calldata, PrimDrawContext pdc)
{
  GraphXYSentPtr gsp;
  DrawInfoPtr    dInfoPtr;

  if ((gsp = (GraphXYSentPtr) calldata) == NULL)
    return;
  if (gsp->endpointarray == NULL)
    return;

  dInfoPtr = (DrawInfoPtr) pdc;
  PlotTheXYPlot (gsp->endpointarray, gsp->linenumber,
                 gsp->xaxiscount, gsp->yaxiscount, gsp->endpointvalues,
                 &(dInfoPtr->curattrib), gsp->flagIsGUI, gsp->color);
  return;
}

static GraphXYSentPtr AddXYSentinelToPicture (BioseqPtr bspT, BioseqPtr bspP,
                                              SeqAlignPtr saph, SegmenT seg,
                                              Int4 xscale, Int4 yscale,
                                              Uint1Ptr color,
                                              Boolean flagLabels)
{
  GraphXYSentPtr  gsp;
  Int4            linenumber;
  Int4Ptr         epa;
  SeqAlignPtr     sap;
  DenseSegPtr     dsp;
  Int4            loop;
  Int4            startT, stopT, startP, stopP;

  if (bspT == NULL || bspP == NULL || saph == NULL || seg == NULL)
    return NULL;
  if ((gsp = (GraphXYSentPtr) MemNew (sizeof (GraphXYSentData))) == NULL)
    return NULL;
  gsp->flagIsGUI = VibrantIsGUI ();
  gsp->color = color;

  sap = saph;
  linenumber = 0;
  while (sap != NULL)
  {
    if (sap->segtype == 2)   /* DenseSeg gapped search */
    {
      dsp = sap->segs;
      for (loop = 0; loop < (Int4) dsp->numseg; loop++)
      {
        if (!(dsp->starts[loop*2] == -1 || dsp->starts[(loop*2)+1] == -1))
        {
          linenumber++;
        }
      }
    }
    sap = sap->next;
  }

  if ((epa = (Int4Ptr) MemNew (sizeof (Int4) * 4 * linenumber)) == NULL)
  {
    MemFree (gsp);
    return NULL;
  }

  gsp->linenumber = linenumber;
  gsp->endpointarray = epa;
  gsp->xscale = xscale;
  gsp->yscale = yscale;
  gsp->xaxiscount = bspT->length / xscale;
  gsp->yaxiscount = bspP->length / yscale;

  sap = saph;
  while (sap != NULL)
  {
    if (sap->segtype == 2)   /* DenseSeg gapped search */
    {
      dsp = sap->segs;
      for (loop = 0; loop < (Int4) dsp->numseg; loop++)
      {
        if (!(dsp->starts[loop*2] == -1 || dsp->starts[(loop*2)+1] == -1))
        {
          startT = dsp->starts[loop*2];
          startP = dsp->starts[(loop*2)+1];
          stopT = startT + dsp->lens[loop] - 1;
          stopP = startP + dsp->lens[loop] - 1;
          *epa++ = startT/xscale;
          *epa++ = startP/yscale;
          *epa++ = stopT/xscale;
          *epa++ = stopP/yscale;
        }
      }
    }
    sap = sap->next;
  }
  if (flagLabels)
  {
    gsp->endpointvalues = (Int4Ptr) MemNew (sizeof (Int4) * 4);
    gsp->endpointvalues[0] = 0;
    gsp->endpointvalues[1] = bspT->length;
    gsp->endpointvalues[2] = 0;
    gsp->endpointvalues[3] = bspP->length;
  }
  gsp->snt = AddSntRectangle (seg, 0, 0, 400, 400,
                              XYSentProc, (BigScalar) gsp, CleanGSP, 0);
  return gsp;
}

extern Int4 ScalePlot (Int4 xlen, Int4 ylen, Int4 maxlen)
{
  Int4 scale, max, trim;

  if (xlen > ylen)
    max = xlen;
  else
    max = ylen;

  scale = 1;
  trim = max;
  while (trim > maxlen)
  {
    scale++;
    trim = max / scale;
  }
  return scale;
}

extern SegmenT DotXYPlot (BioseqPtr bspT, BioseqPtr bspP, Int4 cutoff,
                          Int4 xscale, Int4 yscale, SegmenT seg,
                          Boolean flagLabels)
{
  if (bspT == NULL || bspP == NULL)
    return seg;

  if (seg == NULL)
    seg = CreatePicture ();

  AddDotXYSentinelToPicture (bspT, bspP, seg, cutoff, xscale, yscale,
                             flagLabels);
  return seg;
}

extern SegmenT XYPlot (BioseqPtr bspT, BioseqPtr bspP, SeqAlignPtr sap,
                       Int4 xscale, Int4 yscale, Uint1Ptr color, SegmenT seg,
                       Boolean flagLabels)
{
  if (bspT == NULL || bspP == NULL || sap == NULL)
    return seg;

  if (seg == NULL)
    seg = CreatePicture ();

  AddXYSentinelToPicture (bspT, bspP, sap, seg, xscale, yscale,
                          color, flagLabels);

  return seg;
}