~ubuntu-branches/ubuntu/quantal/ncbi-tools6/quantal

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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* ===========================================================================
*
*                            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: dustv.c
*
* Author(s): John Kuzio
*
* Version Creation Date: 98-01-01
*
* $Revision: 6.7 $
*
* File Description: a dust utility
*
* Modifications:
* --------------------------------------------------------------------------
* Date       Name        Description of modification
* --------------------------------------------------------------------------
* $Log: dustv.c,v $
* Revision 6.7  1998/11/12 20:04:11  kuzio
* .
*
* Revision 6.6  1998/09/16 18:40:35  kuzio
* cvs logging
*
* ==========================================================================
*/

#include <ncbi.h>
#include <vibrant.h>
#include <accentr.h>
#include <gather.h>
#include <tofasta.h>
#include <urkutil.h>
#include <urkdust.h>
#include <seqscrl.h>
#include <gphdraw.h>

#define TOP_ERROR 1
static char _this_module[] = "dustv";
#undef  THIS_MODULE
#define THIS_MODULE _this_module
static char _this_file[] = __FILE__;
#undef  THIS_FILE
#define THIS_FILE _this_file

typedef struct xtrins
{
  SeqGraphPtr    sgp;
  Int4           Xscale, Yscale;
  Int4           Xaxislen, Yaxislen;
  Int4           Xprelen, Xpostlen, Yprelen, Ypostlen;
  RecT           dr;
} XIS, PNTR XISPtr;

typedef struct xtrobj
{
  Boolean        flagParamWindow;
  Int4           gi;
  CharPtr        filename;
  GatherScopePtr gsp;
  SeqEntryPtr    sep;
  BioseqPtr      bsp;
  DustDataPtr    ddp;
} XOS, PNTR XOSPtr;

/* button 1 */

static void CloseGIWindowProc (WindoW w)
{
  Remove (w);
  return;
}

static void SetGIProc (ButtoN b)
{
  TexT     pc;
  XOSPtr   xosp;
  Char     buf[16];

  if ((pc = (TexT) GetObjectExtra (b)) != NULL)
  {
    if ((xosp = (XOSPtr) GetObjectExtra (pc)) != NULL)
    {
      GetTitle (pc, buf, sizeof (buf));
      xosp->gi = atol (buf);
      xosp->filename = (CharPtr) MemFree (xosp->filename);
    }
  }
  CloseGIWindowProc (ParentWindow (b));
  return;
}

static void ReadGIProc (ButtoN b)
{
  WindoW   w;
  GrouP    h, g;
  ButtoN   bt;
  TexT     pc;
  CharPtr  winprmt = "Entrez GI";
  XOSPtr   xosp;

  if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
  {
    w = MovableModalWindow (-60, -33, -10, -10, winprmt, CloseGIWindowProc);
    h = HiddenGroup (w, 1, 0, NULL);
    StaticPrompt (h, winprmt, StringWidth (winprmt),
                  dialogTextHeight, NULL, 'c');
    g = HiddenGroup (h, 2, 0, NULL);
    pc = DialogText (g, "", 8, NULL);
    bt = PushButton (h, "  Okay  ", SetGIProc);
    SetObjectExtra (pc, xosp, NULL);
    SetObjectExtra (bt, pc, NULL);
    Show (w);
    Select (w);
  }
  return;
}

/* button 2 */

static void ReadFileProc (ButtoN b)
{
  XOSPtr  xosp;
  Char    filename[PATH_MAX];

  if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
  {
    if (!GetInputFileName (filename, PATH_MAX, "", "TEXT"))
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 101, "Could not read directory");
      ErrShow ();
    }
    else
    {
      MemFree (xosp->filename);
      xosp->filename = StringSave (filename);
      xosp->gi = 0;
    }
  }
  return;
}

/* button 3 */

static void CloseParamWindowProc (WindoW w)
{
  SeqScrollDataPtr ssdp;

  if ((ssdp = (SeqScrollDataPtr) GetObjectExtra (w)) != NULL)
    SeqScrollDataFree (ssdp);
  Remove (w);
  return;
}

static void ApplyParams (ButtoN b)
{
  WindoW  w;
  XOSPtr  xosp;

  SeqScrollDataPtr ssdp;

  w = ParentWindow (b);
  if ((ssdp = (SeqScrollDataPtr) GetObjectExtra (w)) != NULL)
  {
    if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
    {
      if (xosp->ddp != NULL)
      {
        xosp->ddp->windowsize = (Int4) ssdp->curval;
        ssdp = ssdp->next;
        xosp->ddp->percent = (FloatHi) ssdp->curval;
        xosp->ddp->level = (Int4) ssdp->curval;
        ssdp = ssdp->next;
        xosp->ddp->minwin = (Int4) ssdp->curval;
        ssdp = ssdp->next;
        xosp->ddp->linker = (Int4) ssdp->curval;
      }
    }
  }
  return;
}

static void ClearWindowFlag (ButtoN b)
{
  XOSPtr  xosp;

  if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
    xosp->flagParamWindow = FALSE;
  return;
}

static void AcceptParamProc (ButtoN b)
{
  ApplyParams (b);
  CloseParamWindowProc (ParentWindow (b));
  ClearWindowFlag (b);
  return;
}

static void UpdateParamProc (ButtoN b)
{
  ApplyParams (b);
  return;
}

static void CancelParamProc (ButtoN b)
{
  CloseParamWindowProc (ParentWindow (b));
  ClearWindowFlag (b);
  return;
}

static void DustParamProc (ButtoN b)
{
  WindoW           w;
  GrouP            h, g;
  ButtoN           b1, b2, b3;
  XOSPtr           xosp;

  SeqScrollDataPtr ssdp_dust = NULL;

  if ((xosp = (XOSPtr) GetObjectExtra (b)) == NULL)
    return;
  if (xosp->flagParamWindow)
    return;

  w = FixedWindow (-50, -10, -10, -10,
                   "Dust Parameters", CloseParamWindowProc);

  h = HiddenGroup (w, 4, 0, NULL);
  StaticPrompt (h, "window", StringWidth ("window"), dialogTextHeight,
                NULL, 'c');
  StaticPrompt (h, "cutoff", StringWidth ("cutoff"), dialogTextHeight,
                NULL, 'c');
  StaticPrompt (h, "minwin", StringWidth ("minwin"), dialogTextHeight,
                NULL, 'c');
  StaticPrompt (h, "linker", StringWidth ("linker"), dialogTextHeight,
                NULL, 'c');
  g = HiddenGroup (h, 2, 0, NULL);
  AddScrollControl (&ssdp_dust, g, 64, 8, 256, 24);
  g = HiddenGroup (h, 2, 0, NULL);
  AddScrollControl (&ssdp_dust, g, 20, 2, 64, 24);
  g = HiddenGroup (h, 2, 0, NULL);
  AddScrollControl (&ssdp_dust, g, 4, 4, 128, 24);
  g = HiddenGroup (h, 2, 0, NULL);
  AddScrollControl (&ssdp_dust, g, 1, 1, 32, 24);
  SetObjectExtra (w, ssdp_dust, NULL);

  b1 = PushButton (h, "Accept", AcceptParamProc);
  SetObjectExtra (b1, xosp, NULL);
  b2 = PushButton (h, "Apply ", UpdateParamProc);
  SetObjectExtra (b2, xosp, NULL);
  b3 = PushButton (h, "Cancel", CancelParamProc);
  SetObjectExtra (b3, xosp, NULL);

  Show (w);
  Select (w);
  xosp->flagParamWindow = TRUE;

  return;
}

/* button 4 */

static Boolean GetBioseq (GatherContextPtr gcp)
{
  XOSPtr         xosp;
  BioseqPtr      bsp;
  Int4           gi, entrezgi;

  if (gcp == NULL)
    return FALSE;
  if ((xosp = (XOSPtr) gcp->userdata) == NULL)
    return FALSE;

  if (xosp->bsp != NULL)
    return TRUE;
  if (gcp->thistype != OBJ_BIOSEQ)
    return TRUE;
  if ((bsp = (BioseqPtr) (gcp->thisitem)) == NULL)
    return TRUE;

  gi = xosp->gi;
  if (gi > 0)
  {
    entrezgi = GetGIForSeqId (bsp->id);
    if (gi == entrezgi)
      xosp->bsp = bsp;
    return TRUE;
  }
  else
  {
    xosp->bsp = bsp;
    return TRUE;
  }
}

static void CloseGraphWindowProc (WindoW w)
{
  XISPtr  xisp;

  if ((xisp = (XISPtr) GetObjectExtra (w)) != NULL)
  {
    xisp->sgp = SeqGraphFree (xisp->sgp);
    MemFree (xisp);
  }
  Remove (w);
  return;
}

static void CloseGraphPanelProc (PaneL p, PoinT pt)
{
  CloseGraphWindowProc (ParentWindow (p));
  return;
}

/* not yet

static void AttachGraphProc (ButtoN b)
{
  XOSPtr      xosp;
  XISPtr      xisp;
  SeqAnnotPtr annot, annotg;

  if ((xosp = (XOSPtr) GetObjectExtra (ParentWindow (b))) == NULL)
    return;
  if ((xisp = (XISPtr) GetObjectExtra (b)) == NULL)
    return;

  if ((annotg = SeqAnnotNew ()) != NULL)
  {
    annotg->type = 3;
    annotg->name = StringSave ("dust");
    annotg->data = (Pointer) (xisp->sgp);
    xisp->sgp = NULL;
    annot = xosp->bsp->annot;
    if (annot == NULL)
    {
      xosp->bsp->annot = annotg;
    }
    else
    {
      while (annot->next != NULL)
        annot = annot->next;
      annot->next = annotg;
    }
  }
  CloseGraphWindowProc (ParentWindow (b));
  return;
}

 not yet */

static void DrawGraph (PaneL p)
{
  XISPtr  xisp;
  WindoW  w;
  RecT    r;
  Int4    len;

  w = ParentWindow (p);
  if ((xisp = (XISPtr) GetObjectExtra (w)) == NULL)
    return;

  len = xisp->sgp->numval;

/* by two's
  xisp->Xscale = 1;
  while (len > 256)
  {
    len /= 2;
    xisp->Xscale *= 2;
  }
 by two's */

  xisp->Xscale = len / 256;
  if ((len % 256) != 0)
    (xisp->Xscale)++;

  ObjectRect (p, &r);
  xisp->dr.left   = r.left;
  xisp->dr.top    = r.top;
  xisp->dr.right  = r.right;
  xisp->dr.bottom = r.bottom - (Int2) (xisp->Ypostlen);
  DrawGraphToPanel (xisp->sgp, xisp->Xscale, xisp->Yscale,
                    xisp->Yaxislen, &(xisp->dr), 1,
                    xisp->sgp->numval);
  return;
}

static void DustProc (ButtoN b)
{
  Boolean     flagHaveNet;

  SeqEntryPtr sep;
  Int4        gi;
  CharPtr     fastafile;
  FILE        *fiop;
  CharPtr     title;

  GatherScopePtr      gsp;
  XOSPtr              xosp;
  XISPtr              xisp;

  Int4           i;
  DustRegionPtr  drp;
  FloatHiPtr     fhi, fhit;
  FloatHi        minscore, maxscore, deltascore;
  Int4           Yscale;

  SeqGraphPtr    sgp;

  WindoW     w;
  PaneL      p;

  if ((xosp = (XOSPtr) GetObjectExtra (b)) == NULL)
    return;

  gsp = xosp->gsp;
  gi = xosp->gi;
  fastafile = xosp->filename;

  if (gi > 0)
  {
    if (!EntrezInit ("dustv", FALSE, &flagHaveNet))
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
                 "Entrez init failed");
      ErrShow ();
      return;
    }
  }

  if (gi > 0)
  {
    sep = EntrezSeqEntryGet (gi, SEQENTRY_READ_BIOSEQ);
  }
  else if (fastafile != NULL)
  {
    if ((fiop = FileOpen (fastafile, "r")) == NULL)
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 103,
                 "Failed to open FastA file");
      ErrShow ();
      return;
    }
    sep = FastaToSeqEntry (fiop, TRUE);
  }
  else
  {
    sep = NULL;
  }

  if (sep == NULL)
  {
    ErrPostEx (SEV_ERROR, TOP_ERROR, 104,
               "No seqentry");
    ErrShow ();
    return;
  }
  else
  {
    xosp->sep = sep;
    xosp->bsp = NULL;
    xosp->gi = gi;
    GatherSeqEntry (sep, (Pointer) xosp, GetBioseq,
                    (Pointer) gsp);
    if (xosp->bsp != NULL)
    {
      if (ISA_na (xosp->bsp->mol))
      {
        drp = DustBioseq (xosp->bsp, 0, xosp->bsp->length-1, xosp->ddp);
        fhit = fhi = DustForGraph (drp, xosp->bsp->length, 0,
                                   xosp->bsp->length-1);
        DustRegionFree (drp);
        if (fhi == NULL)
        {
          return;
        }
        minscore = 0.0;
        maxscore = 0.0;
        for (i = 0; i < xosp->bsp->length; i++)
        {
          if (*fhit > maxscore)
            maxscore = *fhit;
          fhit++;
        }

        title = FastaTitle (xosp->bsp, "DUST: >", NULL);

        if ((sgp = SeqGraphNew ()) == NULL)
        {
          MemFree (fhi);
          MemFree (title);
          return;
        }

        xisp = MemNew (sizeof (XIS));
        xisp->sgp = sgp;
        xisp->Xscale = 1;
        xisp->Yscale = 1;
        xisp->Xaxislen = 250;
        xisp->Yaxislen = 100;
        xisp->Xprelen = 0;
        xisp->Yprelen = 50;
        xisp->Xpostlen = 50;
        xisp->Ypostlen = 50;
        sgp->loc = SeqLocIntNew (0, xosp->bsp->length-1, xosp->bsp->strand,
                                 xosp->bsp->id);
        sgp->title = title;
        sgp->flags[2] = 1;
        sgp->numval = xosp->bsp->length;
        sgp->values = (Pointer) fhi;

        fhit = fhi;
        deltascore = maxscore - minscore;
        if ((Yscale = 100 / (Int4) deltascore) > 0)
        {
          minscore *= Yscale;
          maxscore *= Yscale;
          for (i = 0; i < xosp->bsp->length; i++)
          {
            *fhit *= Yscale;
            fhit++;
          }
          xisp->Yscale = Yscale;
        }
        sgp->max.realvalue = maxscore;
        sgp->min.realvalue = minscore;

        w = FixedWindow (-50, -50, -10, -10, "Dust", CloseGraphWindowProc);
        SetObjectExtra (w, xisp, NULL);
        p = SimplePanel (w,
                         (Int2) (xisp->Xprelen+xisp->Xaxislen+xisp->Xpostlen),
                         (Int2) (xisp->Yprelen+xisp->Yaxislen+xisp->Ypostlen),
                         DrawGraph);
        SetPanelClick (p, NULL, NULL, NULL, CloseGraphPanelProc);
        RealizeWindow (w);
        Show (w);
      }
      else
      {
        ErrPostEx (SEV_ERROR, TOP_ERROR, 101, "Not a nucleic acid Bioseq");
        ErrShow ();
      }
    }
    else
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 101, "No Bioseq in SeqEntry");
      ErrShow ();
    }
    xosp->sep = sep = SeqEntryFree (sep);
  }

  if (gi > 0)
    EntrezFini ();
  else
    FileClose (fiop);
  return;
}

/* button 5 */

static void CloseDustParentProc (WindoW w)
{
  XOSPtr  xosp;

  if ((xosp = (XOSPtr) GetObjectExtra (w)) != NULL)
  {
    MemFree (xosp->filename);
    xosp->gsp = NULL;          /* is static */
    xosp->sep = SeqEntryFree (xosp->sep);
    xosp->bsp = NULL;          /* should be in seqentry */
    xosp->ddp = DustDataFree (xosp->ddp);
    MemFree (xosp);
  }
  Remove (w);
  QuitProgram ();
  return;
}

static void EndProg (ButtoN b)
{
  CloseDustParentProc (ParentWindow (b));
  return;
}

/* parent */

static XOSPtr SetUp (void)
{
  static GatherScope  gs;
  GatherScopePtr      gsp;
  XOSPtr              xosp;

  gsp = &gs;
  MemSet ((Pointer) gsp, 0, sizeof (GatherScope));
  MemSet ((Pointer) gsp->ignore, (int) (TRUE),
          (size_t) (OBJ_MAX * sizeof (Boolean)));
  gsp->ignore[OBJ_BIOSEQ] = FALSE;

  xosp = MemNew (sizeof (XOS));

  xosp->flagParamWindow = FALSE;
  xosp->gi = 0;
  xosp->filename = NULL;
  xosp->gsp = gsp;
  xosp->sep = NULL;
  xosp->bsp = NULL;

  xosp->ddp = DustDataNew ();

  return xosp;
}

Int2 Main (void)
{
  WindoW  w;
  GrouP   g;
  ButtoN  b1, b2, b3, b4, b5;

  XOSPtr  xosp;

  ProcessUpdatesFirst (FALSE);

  xosp = SetUp ();

  w = FixedWindow (-50, -33, -10, -10,
                   "Dust Low Complexity", CloseDustParentProc);
  g = HiddenGroup (w, 5, 0, NULL);
  b1 = PushButton (g, "  Input GI ", ReadGIProc);
  b2 = PushButton (g, "Input FastA", ReadFileProc);
  b3 = PushButton (g, " Parameters", DustParamProc);
  b4 = PushButton (g, "    Dust   ", DustProc);
  b5 = PushButton (g, "   Finish  ", EndProg);

  SetObjectExtra (w,  xosp, NULL);
  SetObjectExtra (b1, xosp, NULL);
  SetObjectExtra (b2, xosp, NULL);
  SetObjectExtra (b3, xosp, NULL);
  SetObjectExtra (b4, xosp, NULL);
  SetObjectExtra (b5, xosp, NULL);

  Show (w);
  ProcessEvents ();
  return 0;
}