1 /*
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name: searchv.c
27 *
28 * Author(s): John Kuzio
29 *
30 * Version Creation Date: 98-09-17
31 *
32 * $Revision: 6.2 $
33 *
34 * File Description: nucleotide or peptide pattern match
35 *
36 * Modifications:
37 * --------------------------------------------------------------------------
38 * Date       Name        Description of modification
39 * --------------------------------------------------------------------------
40 * $Log: searchv.c,v $
41 * Revision 6.2  1999/03/13 03:43:45  kuzio
42 * cast
43 *
44 * Revision 6.1  1998/09/21 14:49:38  kuzio
45 * vibrant srchaa srchnt
46 *
47 * ==========================================================================
48 */
49 
50 #include <ncbi.h>
51 #include <vibrant.h>
52 #include <accentr.h>
53 #include <gather.h>
54 #include <tofasta.h>
55 #include <urkptpf.h>
56 #include <urkutil.h>
57 #include <seqscrl.h>
58 
59 #define TOP_ERROR 1
60 static char _this_module[] = "searchv";
61 #undef  THIS_MODULE
62 #define THIS_MODULE _this_module
63 static char _this_file[] = __FILE__;
64 #undef  THIS_FILE
65 #define THIS_FILE _this_file
66 
67 typedef struct xtrins
68 {
69   SegmenT picture;
70 } XIS, PNTR XISPtr;
71 
72 typedef struct xtrobj
73 {
74   Boolean        flagParamWindow;
75   CharPtr        patternfile, patternnamesfile, pattern;
76   ValNodePtr     namelist;
77   Boolean        flagSkip, flagNeedCutsite;
78   Int4           cutoff;
79   CharPtr        taxon;
80   Int4           mismatchcur, mismatchmin, mismatchmax;
81   ComPatPtr      cpp;
82   Boolean        flagPatNuc;
83   Int4           gi;
84   CharPtr        filename;
85   GatherScopePtr gsp;
86   SeqEntryPtr    sep;
87   BioseqPtr      bsp;
88   Boolean        flagIsNuc;
89 } XOS, PNTR XOSPtr;
90 
91 /* get sequence common */
92 
GetBioseq(GatherContextPtr gcp)93 static Boolean GetBioseq (GatherContextPtr gcp)
94 {
95   XOSPtr         xosp;
96   BioseqPtr      bsp;
97   Int4           gi, entrezgi;
98 
99   if (gcp == NULL)
100     return FALSE;
101   if ((xosp = (XOSPtr) gcp->userdata) == NULL)
102     return FALSE;
103 
104   if (xosp->bsp != NULL)
105     return TRUE;
106   if (gcp->thistype != OBJ_BIOSEQ)
107     return TRUE;
108   if ((bsp = (BioseqPtr) (gcp->thisitem)) == NULL)
109     return TRUE;
110 
111   gi = xosp->gi;
112   if (gi > 0)
113   {
114     entrezgi = GetGIForSeqId (bsp->id);
115     if (gi == entrezgi)
116       xosp->bsp = bsp;
117     return TRUE;
118   }
119   else
120   {
121     xosp->bsp = bsp;
122     return TRUE;
123   }
124 }
125 
GetThisBioseq(XOSPtr xosp)126 static void GetThisBioseq (XOSPtr xosp)
127 {
128   GatherScopePtr  gsp;
129   CharPtr         fastafile;
130   Int4            gi;
131   FILE            *fiop;
132   Boolean         flagHaveNet;
133   SeqEntryPtr     sep;
134   Int2            moltype;
135 
136   gsp = xosp->gsp;
137   fastafile = xosp->filename;
138   gi = xosp->gi;
139   xosp->sep = SeqEntryFree (xosp->sep);
140 
141   if (gi > 0)
142   {
143     if (!EntrezInit ("epiv", FALSE, &flagHaveNet))
144     {
145       ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
146                  "Entrez init failed");
147       ErrShow ();
148       return;
149     }
150     sep = EntrezSeqEntryGet (gi, SEQENTRY_READ_BIOSEQ);
151     EntrezFini ();
152   }
153   else if (fastafile != NULL)
154   {
155     if ((fiop = FileOpen (fastafile, "r")) == NULL)
156     {
157       ErrPostEx (SEV_ERROR, TOP_ERROR, 103,
158                  "Failed to open FastA file");
159       ErrShow ();
160       return;
161     }
162     sep = FastaToSeqEntry (fiop, xosp->flagIsNuc);
163     FileClose (fiop);
164   }
165   else
166   {
167     sep = NULL;
168   }
169 
170   if (sep == NULL)
171   {
172     ErrPostEx (SEV_ERROR, TOP_ERROR, 104,
173                "No SeqEntry");
174     ErrShow ();
175     return;
176   }
177   else
178   {
179     xosp->sep = sep;
180     xosp->bsp = NULL;
181     xosp->gi = gi;
182     GatherSeqEntry (sep, (Pointer) xosp, GetBioseq, (Pointer) gsp);
183   }
184 
185   if (xosp->bsp != NULL)
186   {
187     if (ISA_na (xosp->bsp->mol))
188     {
189       xosp->flagIsNuc = TRUE;
190       moltype = 0;
191     }
192     else
193     {
194       xosp->flagIsNuc = FALSE;
195       moltype = 1;
196     }
197     if (xosp->flagIsNuc != xosp->flagPatNuc)
198     {
199       xosp->cpp = ComPatFree (xosp->cpp);
200     }
201     if (xosp->cpp == NULL)
202     {
203       if (xosp->pattern != NULL)
204       {
205         xosp->flagPatNuc = xosp->flagIsNuc;
206         if ((xosp->cpp = CompilePattern (xosp->pattern, moltype)) != NULL)
207           StrCpy (xosp->cpp->name, "User Pattern");
208       }
209       else
210       {
211         if (moltype == 0)
212         {
213           xosp->patternfile = MemFree (xosp->patternfile);
214           xosp->patternfile = StringSave ("ncbiren.dat");
215           xosp->patternnamesfile = MemFree (xosp->patternnamesfile);
216           xosp->patternnamesfile = StringSave ("ncbirnam.dat");
217           xosp->namelist = ValNodeFreeData (xosp->namelist);
218           xosp->namelist = ReadPatternNames (xosp->patternnamesfile);
219           xosp->cpp = ReadRENPattern (xosp->patternfile, xosp->flagNeedCutsite,
220                                       xosp->namelist);
221           PalindromeCheck (xosp->cpp);
222         }
223         else
224         {
225           xosp->patternfile = MemFree (xosp->patternfile);
226           xosp->patternfile = StringSave ("ncbipros.dat");
227           xosp->patternnamesfile = MemFree (xosp->patternnamesfile);
228           xosp->patternnamesfile = StringSave ("ncbipnam.dat");
229           xosp->namelist = ValNodeFreeData (xosp->namelist);
230           xosp->namelist = ReadPatternNames (xosp->patternnamesfile);
231           xosp->cpp = ReadPrositePattern (xosp->patternfile,
232                                           xosp->flagSkip, xosp->cutoff,
233                                           xosp->taxon, xosp->namelist);
234         }
235       }
236     }
237   }
238   return;
239 }
240 
241 /* button 1 */
242 
CloseGIWindowProc(WindoW w)243 static void CloseGIWindowProc (WindoW w)
244 {
245   Remove (w);
246   return;
247 }
248 
SetGIProc(ButtoN b)249 static void SetGIProc (ButtoN b)
250 {
251   TexT     pc;
252   XOSPtr   xosp;
253   Char     buf[16];
254 
255   if ((pc = (TexT) GetObjectExtra (b)) != NULL)
256   {
257     if ((xosp = (XOSPtr) GetObjectExtra (pc)) != NULL)
258     {
259       GetTitle (pc, buf, sizeof (buf));
260       xosp->gi = atol (buf);
261       xosp->filename = (CharPtr) MemFree (xosp->filename);
262       GetThisBioseq (xosp);
263     }
264   }
265   CloseGIWindowProc (ParentWindow (b));
266   return;
267 }
268 
ReadGIProc(ButtoN b)269 static void ReadGIProc (ButtoN b)
270 {
271   WindoW   w;
272   GrouP    h, g;
273   ButtoN   bt;
274   TexT     pc;
275   CharPtr  winprmt = "Entrez GI";
276   XOSPtr   xosp;
277 
278   if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
279   {
280     w = MovableModalWindow (-60, -33, -10, -10, winprmt, CloseGIWindowProc);
281     h = HiddenGroup (w, 1, 0, NULL);
282     StaticPrompt (h, winprmt, StringWidth (winprmt),
283                   dialogTextHeight, NULL, 'c');
284     g = HiddenGroup (h, 2, 0, NULL);
285     pc = DialogText (g, "", 8, NULL);
286     bt = PushButton (h, "  Okay  ", SetGIProc);
287     SetObjectExtra (pc, xosp, NULL);
288     SetObjectExtra (bt, pc, NULL);
289     Show (w);
290     Select (w);
291   }
292   return;
293 }
294 
295 /* button 2 */
296 
ReadFileProc(ButtoN b)297 static void ReadFileProc (ButtoN b)
298 {
299   XOSPtr  xosp;
300   Char    filename[PATH_MAX];
301 
302   if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
303   {
304     if (!GetInputFileName (filename, PATH_MAX, "", "TEXT"))
305     {
306       ErrPostEx (SEV_ERROR, TOP_ERROR, 101, "Could not read directory");
307       ErrShow ();
308     }
309     else
310     {
311       MemFree (xosp->filename);
312       xosp->filename = StringSave (filename);
313       xosp->gi = 0;
314       GetThisBioseq (xosp);
315     }
316   }
317   return;
318 }
319 
320 /* button 3 */
321 
CloseParamWindowProc(WindoW w)322 static void CloseParamWindowProc (WindoW w)
323 {
324   SeqScrollDataPtr ssdp;
325 
326   if ((ssdp = (SeqScrollDataPtr) GetObjectExtra (w)) != NULL)
327     SeqScrollDataFree (ssdp);
328   Remove (w);
329   return;
330 }
331 
ApplyParams(ButtoN b)332 static void ApplyParams (ButtoN b)
333 {
334   WindoW  w;
335   XOSPtr  xosp;
336 
337   SeqScrollDataPtr ssdp;
338 
339   w = ParentWindow (b);
340   if ((ssdp = (SeqScrollDataPtr) GetObjectExtra (w)) != NULL)
341   {
342     if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
343     {
344       xosp->mismatchcur = (Int4) ssdp->curval;
345     }
346   }
347   return;
348 }
349 
ClearWindowFlag(ButtoN b)350 static void ClearWindowFlag (ButtoN b)
351 {
352   XOSPtr  xosp;
353 
354   if ((xosp = (XOSPtr) GetObjectExtra (b)) != NULL)
355     xosp->flagParamWindow = FALSE;
356   return;
357 }
358 
AcceptParamProc(ButtoN b)359 static void AcceptParamProc (ButtoN b)
360 {
361   ApplyParams (b);
362   CloseParamWindowProc (ParentWindow (b));
363   ClearWindowFlag (b);
364   return;
365 }
366 
UpdateParamProc(ButtoN b)367 static void UpdateParamProc (ButtoN b)
368 {
369   ApplyParams (b);
370   return;
371 }
372 
CancelParamProc(ButtoN b)373 static void CancelParamProc (ButtoN b)
374 {
375   CloseParamWindowProc (ParentWindow (b));
376   ClearWindowFlag (b);
377   return;
378 }
379 
SearchParamProc(ButtoN b)380 static void SearchParamProc (ButtoN b)
381 {
382   WindoW           w;
383   GrouP            h, h1, g, g1;
384   ButtoN           b1, b2, b3;
385   XOSPtr           xosp;
386 
387   SeqScrollDataPtr ssdp = NULL;
388 
389   if ((xosp = (XOSPtr) GetObjectExtra (b)) == NULL)
390     return;
391   if (xosp->flagParamWindow)
392     return;
393 
394   w = FixedWindow (-50, -10, -10, -10,
395                    "Search Parameters", CloseParamWindowProc);
396   h = HiddenGroup (w, 1, 0, NULL);
397   h1 = HiddenGroup (h, 2, 0, NULL);
398 
399   g1 = HiddenGroup (h1, 1, 0, NULL);
400   g = HiddenGroup (g1, 1, 0, NULL);
401   StaticPrompt (g, "mismatch", StringWidth ("12345678"), dialogTextHeight,
402                 NULL, 'c');
403   g = HiddenGroup (g1, 2, 0, NULL);
404   AddScrollControl (&ssdp, g, (Int2) xosp->mismatchcur,
405                     (Int2) xosp->mismatchmin, (Int2) xosp->mismatchmax, 24);
406 
407   b1 = PushButton (h, "Accept", AcceptParamProc);
408   SetObjectExtra (b1, xosp, NULL);
409   b2 = PushButton (h, "Apply ", UpdateParamProc);
410   SetObjectExtra (b2, xosp, NULL);
411   b3 = PushButton (h, "Cancel", CancelParamProc);
412   SetObjectExtra (b3, xosp, NULL);
413 
414   Show (w);
415   Select (w);
416   xosp->flagParamWindow = TRUE;
417 
418   return;
419 }
420 
421 /* button 4 */
422 
423 
CleanUpGraphWindow(GraphiC g,VoidPtr data)424 static void CleanUpGraphWindow (GraphiC g, VoidPtr data)
425 {
426   XISPtr  xisp;
427 
428   if ((xisp = (XISPtr) data) != NULL)
429   {
430     DeletePicture (xisp->picture);
431   }
432   StdCleanupExtraProc (g, data);
433 }
434 
CloseGraphWindowProc(WindoW w)435 static void CloseGraphWindowProc (WindoW w)
436 {
437   Remove (w);
438   return;
439 }
440 
CloseGraphWindowButton(ButtoN b)441 static void CloseGraphWindowButton (ButtoN b)
442 {
443   CloseGraphWindowProc (ParentWindow (b));
444   return;
445 }
446 
PlotSites(BioseqPtr bsp,SeqLocPtr slp,CharPtr name,SegmenT seg,Int4 top)447 static SegmenT PlotSites (BioseqPtr bsp, SeqLocPtr slp, CharPtr name,
448                           SegmenT seg, Int4 top)
449 {
450   Int4      start, length, count;
451   Char      buffer[32], tname[1024];
452 
453   if (seg == NULL)
454     seg = CreatePicture ();
455   if (seg == NULL)
456     return seg;
457 
458   count = 0;
459   while (slp != NULL)
460   {
461     count++;
462     start = SeqLocStart (slp);
463     AddLine (seg, start, top, start, top-10, FALSE, 0);
464     slp = slp->next;
465   }
466   if (count > 0)
467   {
468     length = bsp->length;
469     AddLine (seg, 0, top, length, top, FALSE, 0);
470     AddLine (seg, 0, top-10, length, top-10, FALSE, 0);
471     length += 10;
472     StrCpy (tname, name);
473     if (StrLen (tname) > 10)
474       tname[10] = '\0';
475     sprintf (buffer, "%10s %6ld", tname, (long) count);
476     AddLabel (seg, length, top-5, buffer, SMALL_TEXT, 0, MIDDLE_RIGHT, 0);
477   }
478   return seg;
479 }
480 
SearchProc(ButtoN b)481 static void SearchProc (ButtoN b)
482 {
483   XOSPtr         xosp;
484   XISPtr         xisp;
485 
486   ComPatPtr      cpp;
487   CharPtr        title, cptr;
488   BioseqPtr      bsp;
489   SeqAlignPtr    sap;
490   SeqLocPtr      slpn, slp;
491 
492   Int4        XAxis, XScale;
493   WindoW      w;
494   VieweR      v;
495   GrouP       g;
496   SegmenT     seg;
497   Int4        top;
498 
499   if ((xosp = (XOSPtr) GetObjectExtra (b)) == NULL)
500     return;
501 
502   bsp = xosp->bsp;
503   title = FastaTitle (bsp, ">", NULL);
504   cptr = title;
505   while (*cptr != '\0')
506   {
507     if (*cptr == ' ')
508       break;
509     cptr++;
510   }
511   *cptr = '\0';
512   if (StrLen (title) > 16)
513     title[16] = '\0';
514 
515   xisp = (XISPtr) MemNew (sizeof (XIS));
516   XAxis = 600;
517   XScale = bsp->length / (XAxis-100);
518   if (bsp->length % (XAxis-100) != 0)
519     XScale++;
520   cpp = xosp->cpp;
521   top = 100;
522   seg = NULL;
523 
524   while (cpp != NULL)
525   {
526     sap = PatternMatchBioseq (bsp, cpp, xosp->mismatchcur);
527     if (sap != NULL)
528     {
529       slp = MatchSa2Sl (&sap);
530       seg = PlotSites (bsp, slp, cpp->name, seg, top);
531       top -= 10;
532       while (slp != NULL)
533       {
534         slpn = slp->next;
535         SeqLocFree (slp);
536         slp = slpn;
537       }
538     }
539     cpp = cpp->nextpattern;
540   }
541 
542   if (seg == NULL)
543   {
544     ErrPostEx (SEV_ERROR, TOP_ERROR, 501, "No Matches");
545     ErrShow ();
546     MemFree (xisp);
547     ArrowCursor ();
548     return;
549   }
550 
551   xisp->picture = seg;
552   w = FixedWindow (10, 10, -10, -10, "Search Plot", CloseGraphWindowProc);
553   SetObjectExtra (w, xisp, CleanUpGraphWindow);
554   g = HiddenGroup (w, -1, 0, NULL);
555   v = CreateViewer (g, (Int2) XAxis, 500, TRUE, TRUE);
556   AttachPicture (v, seg, INT4_MIN, INT4_MAX, UPPER_LEFT, XScale, 1, NULL);
557   PushButton (g, "Close", CloseGraphWindowButton);
558   RealizeWindow (w);
559   Show (w);
560 
561   SeqLocFree (slp);
562   MemFree (title);
563 
564   ArrowCursor ();
565   return;
566 }
567 
568 /* button 5 */
569 
CloseSearchParentProc(WindoW w)570 static void CloseSearchParentProc (WindoW w)
571 {
572   XOSPtr  xosp;
573 
574   if ((xosp = (XOSPtr) GetObjectExtra (w)) != NULL)
575   {
576     MemFree (xosp->filename);
577     xosp->gsp = NULL;          /* is static */
578     xosp->sep = SeqEntryFree (xosp->sep);
579     xosp->bsp = NULL;          /* should be in seqentry */
580     xosp->cpp = ComPatFree (xosp->cpp);
581     MemFree (xosp);
582   }
583   Remove (w);
584   QuitProgram ();
585   return;
586 }
587 
EndProg(ButtoN b)588 static void EndProg (ButtoN b)
589 {
590   CloseSearchParentProc (ParentWindow (b));
591   return;
592 }
593 
594 /* parent */
595 
SetUp(void)596 static XOSPtr SetUp (void)
597 {
598   static GatherScope  gs;
599   GatherScopePtr      gsp;
600   XOSPtr              xosp;
601 
602   gsp = &gs;
603   MemSet ((Pointer) gsp, 0, sizeof (GatherScope));
604   MemSet ((Pointer) gsp->ignore, (int) (TRUE),
605           (size_t) (OBJ_MAX * sizeof (Boolean)));
606   gsp->ignore[OBJ_BIOSEQ] = FALSE;
607 
608   xosp = MemNew (sizeof (XOS));
609 
610   xosp->flagParamWindow = FALSE;
611   xosp->patternfile = NULL;
612   xosp->patternnamesfile = NULL;
613   xosp->pattern = NULL;
614   xosp->namelist = NULL;
615   xosp->flagSkip = FALSE;
616   xosp->flagNeedCutsite = TRUE;
617   xosp->cutoff = 1000000;
618   xosp->taxon = NULL;
619 
620   xosp->mismatchcur = 0;
621   xosp->mismatchmin = 0;
622   xosp->mismatchmax = 2;
623   xosp->cpp = NULL;
624   xosp->flagPatNuc = TRUE;
625 
626   xosp->gi = 0;
627   xosp->filename = NULL;
628   xosp->gsp = gsp;
629   xosp->sep = NULL;
630   xosp->bsp = NULL;
631   xosp->flagIsNuc = TRUE;
632 
633   return xosp;
634 }
635 
Main(void)636 Int2 Main (void)
637 {
638   WindoW  w;
639   GrouP   g;
640   ButtoN  b1, b2, b3, b4, b5;
641 
642   XOSPtr  xosp;
643 
644   ProcessUpdatesFirst (FALSE);
645 
646   xosp = SetUp ();
647 
648   w = FixedWindow (-50, -33, -10, -10,
649                    "AA/NT Pattern Search", CloseSearchParentProc);
650   g = HiddenGroup (w, 5, 0, NULL);
651   b1 = PushButton (g, "  Input GI ", ReadGIProc);
652   b2 = PushButton (g, "Input FastA", ReadFileProc);
653   b3 = PushButton (g, " Parameters", SearchParamProc);
654   b4 = PushButton (g, "   Search  ", SearchProc);
655   b5 = PushButton (g, "   Finish  ", EndProg);
656 
657   SetObjectExtra (w,  xosp, NULL);
658   SetObjectExtra (b1, xosp, NULL);
659   SetObjectExtra (b2, xosp, NULL);
660   SetObjectExtra (b3, xosp, NULL);
661   SetObjectExtra (b4, xosp, NULL);
662   SetObjectExtra (b5, xosp, NULL);
663 
664   Show (w);
665   ProcessEvents ();
666   return 0;
667 }
668