~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
/*
* ===========================================================================
*
*                            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: twop.c
*
* Author(s): John Kuzio
*
* Version Creation Date: 98-01-01
*
* $Revision: 6.5 $
*
* File Description: best id blast of two proteins
*
* Modifications:
* --------------------------------------------------------------------------
* Date       Name        Description of modification
* --------------------------------------------------------------------------
* $Log: twop.c,v $
* Revision 6.5  1998/09/16 18:19:32  kuzio
* cvs logging
*
* ==========================================================================
*/

/*
  BLAST pair score for longest gapped overlap
*/

#include <ncbi.h>
#include <accentr.h>
#include <tofasta.h>
#include <gather.h>
#include <blast.h>
#include <urkutil.h>
#include <urkptpf.h>

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

typedef struct gather_Bioseq
{
  Int4      gi;
  BioseqPtr bsp;
} Gather_BS, PNTR Gather_BSPtr;

Args myargs[] =
{
  { "FastA protein target file (search)", NULL, NULL, NULL, TRUE,
    'F', ARG_STRING, 0.0, 0, NULL },
  { "FastA protein probe file (query)", NULL, NULL, NULL, TRUE,
    'f', ARG_STRING, 0.0, 0, NULL },
  { "List of protein gi's target file (search)", NULL, NULL, NULL, TRUE,
    'L', ARG_STRING, 0.0, 0, NULL },
  { "List of protein gi's probe file (query)", NULL, NULL, NULL, TRUE,
    'l', ARG_STRING, 0.0, 0, NULL },
  { "Output file", NULL, NULL, NULL, TRUE,
    'o', ARG_STRING, 0.0, 0, NULL },
  { "Show pattern match results", "FALSE", "FALSE", "TRUE", TRUE,
    'p', ARG_BOOLEAN, 0.0, 0, NULL }
};

static Boolean GetBioseq (GatherContextPtr gcp)
{
  Gather_BSPtr   gbsp;
  BioseqPtr      bsp;
  Int4           entrezgi;

  if (gcp == NULL)
    return FALSE;
  if ((gbsp = (Gather_BSPtr) gcp->userdata) == NULL)
    return FALSE;

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

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

Int2 Main (void)
{
  Int2           argcount;
  Boolean        flagHaveNet;

  FILE           *fioT, *fioP, *fout;
  Boolean        flagFastaT, flagFastaP, flagFastaL, flagFastal;
  Char           bufuid[256];
  Int4           giT, giP;

  SeqEntryPtr    sepT, sepP;
  BioseqPtr      bspT, bspP;
  SeqLocPtr      slpT, slpP;

  BLAST_OptionsBlkPtr blstopt;

  Int4           i, loop;
  Int4           startT, startP;
  SeqAlignPtr    sap, sapn;
  DenseSegPtr    dsp;
  SeqPortPtr     sppT, sppP;
  Uint1          seqT, seqP;
  Int4           effective_length, eff_len_max, seq_identity, seq_id_max;
  CharPtr        titleT, titleP;
  CharPtr        cptr;

  Boolean        flagPattern;
  CharPtr        pattern_file = "ncbipros.dat";
  ComPatPtr      cpp, cpph;
  SeqAlignPtr    sapT, sapP;

  static GatherScope  gs;
  GatherScopePtr      gsp;
  static Gather_BS    gbs;
  Gather_BSPtr        gbsp;

  argcount = sizeof (myargs) / sizeof (Args);
  if (!GetArgs ("twop", argcount, myargs))
    return 1;

  ErrSetLogLevel (SEV_ERROR);
  ErrSetMessageLevel (SEV_ERROR);

  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;
  gbsp = &gbs;

  flagPattern = (Boolean) myargs[5].intvalue;

  if (myargs[0].strvalue != NULL)
  {
    flagFastaT = TRUE;
    flagFastaL = FALSE;
  }
  else if (myargs[2].strvalue != NULL)
  {
    flagFastaT = FALSE;
    flagFastaL = TRUE;
  }
  else
  {
    flagFastaT = FALSE;
    flagFastaL = FALSE;
  }
  if (myargs[1].strvalue != NULL)
  {
    flagFastaP = TRUE;
    flagFastal = FALSE;
  }
  else if (myargs[3].strvalue != NULL)
  {
    flagFastaP = FALSE;
    flagFastal = TRUE;
  }
  else
  {
    flagFastaP = FALSE;
    flagFastal = FALSE;
  }

  if (!flagFastaT && !flagFastaL)
  {
    ErrPostEx (SEV_ERROR, TOP_ERROR, 100,
               "No target file given :: for help :   twop -");
    ErrShow ();
    exit (1);
  }
  if (!flagFastaP && !flagFastal)
  {
    ErrPostEx (SEV_ERROR, TOP_ERROR, 100,
               "No probe file given :: for help :   twop -");
    ErrShow ();
    exit (1);
  }

  if (flagFastaL || flagFastal)
  {
    if (!EntrezInit ("twop", FALSE, &flagHaveNet))
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
                 "Entrez init failed");
      ErrShow ();
      exit (1);
    }
  }

  giT = 0;
  giP = 0;

  if (flagFastaT)
  {
    if ((fioT = FileOpen (myargs[0].strvalue, "r")) == NULL)
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
                 "Failed to open FastA file %s", myargs[0].strvalue);
      ErrShow ();
      exit (1);
    }
    sepT = FastaToSeqEntry (fioT, FALSE);
  }

  if (flagFastaP)
  {
    if ((fioP = FileOpen (myargs[1].strvalue, "r")) == NULL)
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
                 "Failed to open FastA file %s", myargs[1].strvalue);
      ErrShow ();
      exit (1);
    }
    sepP = FastaToSeqEntry (fioP, FALSE);
  }

  if (flagFastaL)
  {
    if ((fioT = FileOpen (myargs[2].strvalue, "r")) == NULL)
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
                 "Failed to open gi list file %s", myargs[2].strvalue);
      ErrShow ();
      exit (1);
    }
    while ((FileGets (bufuid, sizeof (bufuid), fioT)) != NULL)
    {
      if (bufuid[0] == '>')
        continue;
      break;
    }
    giT = atol (bufuid);
    sepT = EntrezSeqEntryGet (giT, SEQENTRY_READ_NUC_PROT);
  }

  if (flagFastal)
  {
    if ((fioP = FileOpen (myargs[3].strvalue, "r")) == NULL)
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
                 "Failed to open gi list file %s", myargs[3].strvalue);
      ErrShow ();
      exit (1);
    }
    while ((FileGets (bufuid, sizeof (bufuid), fioP)) != NULL)
    {
      if (bufuid[0] == '>')
        continue;
      break;
    }
    giP = atol (bufuid);
    sepP = EntrezSeqEntryGet (giP, SEQENTRY_READ_NUC_PROT);
  }

  if (sepT == NULL || sepP == NULL)
  {
    ErrPostEx (SEV_ERROR, TOP_ERROR, 103,
               "Seqentry for target or probe not found");
    ErrShow ();
    exit (1);
  }

  if (myargs[4].strvalue != NULL)
  {
    if ((fout = FileOpen (myargs[4].strvalue, "w")) == NULL)
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 102,
                 "Failed to open output file %s", myargs[4].strvalue);
      ErrShow ();
      exit (1);
    }
  }
  else
  {
    fout = stdout;
  }

  if (flagPattern)
    cpph = ReadPrositePattern (pattern_file, FALSE, 25, NULL, NULL);
  else
    cpph = NULL;

  blstopt = BLASTOptionNew ("blastp", TRUE);
  blstopt->wordsize = 3;
  blstopt->expect_value = 10.0;
  blstopt->filter = 0;
  blstopt->gap_open = 9;
  blstopt->gap_extend = 2;
  blstopt->gap_x_dropoff = 50;
  blstopt->threshold_second = 9;
  blstopt->two_pass_method = FALSE;
  blstopt->multiple_hits_only = FALSE;

  while (sepT != NULL && sepP != NULL)
  {
    gbsp->gi = giT;
    gbsp->bsp = NULL;
    GatherSeqEntry (sepT, (Pointer) gbsp, GetBioseq, (Pointer) gsp);
    bspT = gbsp->bsp;
    gbsp->gi = giP;
    gbsp->bsp = NULL;
    GatherSeqEntry (sepP, (Pointer) gbsp, GetBioseq, (Pointer) gsp);
    bspP = gbsp->bsp;
    if (bspT == NULL || bspP == NULL)
    {
      ErrPostEx (SEV_ERROR, TOP_ERROR, 104,
                 "Bioseq for target or probe not found");
      ErrShow ();
      exit (1);
    }
    slpT = SeqLocIntNew (0, bspT->length-1, Seq_strand_unknown, bspT->id);
    slpP = SeqLocIntNew (0, bspP->length-1, Seq_strand_unknown, bspP->id);

    sap = BlastTwoSequencesByLoc (slpT, slpP, "blastp", blstopt);
    seq_id_max = 0;
    eff_len_max = 0;
    while (sap != NULL)
    {
      effective_length = 0;
      seq_identity = 0;
      if (sap->segtype == 2)   /* DenseSeg gapped search */
      {
        sppT = SeqPortNew (bspT, 0, bspT->length-1, 0, Seq_code_iupacaa);
        sppP = SeqPortNew (bspP, 0, bspP->length-1, 0, Seq_code_iupacaa);
        dsp = sap->segs;
        for (loop = 0; loop < (Int4) dsp->numseg; loop++)
        {
          if (dsp->starts[loop*2] == -1 || dsp->starts[(loop*2)+1] == -1)
          {
            effective_length += dsp->lens[loop];
          }
          else
          {
            effective_length += dsp->lens[loop];
            startT = dsp->starts[loop*2];
            startP = dsp->starts[(loop*2)+1];
            SeqPortSeek (sppT, startT, SEEK_SET);
            SeqPortSeek (sppP, startP, SEEK_SET);
            for (i = 0; i < dsp->lens[loop]; i++)
            {
              seqT = SeqPortGetResidue (sppT);
              seqP = SeqPortGetResidue (sppP);
              if (seqT == seqP)
                seq_identity++;
            }
          }
        }
        SeqPortFree (sppT);
        SeqPortFree (sppP);
      }
      if (effective_length > eff_len_max)
      {
        seq_id_max = seq_identity;
        eff_len_max = effective_length;
      }
      sapn = sap->next;
      SeqAlignFree (sap);
      sap = sapn;
    }

    titleT = FastaTitle (bspT, ">", NULL);
    titleP = FastaTitle (bspP, ">", NULL);

    cptr = titleT;
    while (*cptr != '\0')
    {
      if (*cptr == ' ')
        break;
      cptr++;
    }
    *cptr = '\0';
    cptr = titleP;
    while (*cptr != '\0')
    {
      if (*cptr == ' ')
        break;
      cptr++;
    }
    *cptr = '\0';

    if (eff_len_max > 0)
    {
      fprintf (fout,
 "%16s %4ld %16s %4ld -- %6.2f identity %4ld residues\n",
 titleT, (long) bspT->length, titleP, (long) bspP->length,
 (FloatHi) seq_id_max / (FloatHi) eff_len_max * 100.0,
 (long) eff_len_max);
    }
    else
    {
      fprintf (fout,
 "%16s %4ld %16s %4ld -- %6.2f identity\n",
 titleT, (long) bspT->length, titleP, (long) bspP->length, 0.0);
    }
    cpp = cpph;
    while (cpp != NULL)
    {
      sapT = PatternMatchBioseq (bspT, cpp, 0);
      sapP = PatternMatchBioseq (bspP, cpp, 0);
      if (sapT != NULL && sapP != NULL)
      {
        fprintf (fout, "target and probe:  %s\n", cpp->name);
      }
      sap = sapT;
      while (sap != NULL)
      {
        sapn = sap->next;
        SeqAlignFree (sap);
        sap = sapn;
      }
      sap = sapP;
      while (sap != NULL)
      {
        sapn = sap->next;
        SeqAlignFree (sap);
        sap = sapn;
      }
      cpp = cpp->nextpattern;
    }
    SeqLocFree (slpT);
    SeqLocFree (slpP);
    SeqEntryFree (sepT);
    SeqEntryFree (sepP);
    MemFree (titleT);
    MemFree (titleP);
    if (flagFastaT)
    {
      sepT = FastaToSeqEntry (fioT, FALSE);
    }
    if (flagFastaP)
    {
      sepP = FastaToSeqEntry (fioP, FALSE);
    }
    if (flagFastaL)
    {
      bufuid[0] = '\0';
      FileGets (bufuid, sizeof (bufuid), fioT);
      giT = atol (bufuid);
      sepT = EntrezSeqEntryGet (giT, SEQENTRY_READ_NUC_PROT);
    }
    if (flagFastal)
    {
      bufuid[0] = '\0';
      FileGets (bufuid, sizeof (bufuid), fioP);
      giP = atol (bufuid);
      sepP = EntrezSeqEntryGet (giP, SEQENTRY_READ_NUC_PROT);
    }
  }

  if (sepT != NULL)
    SeqEntryFree (sepT);
  if (sepP != NULL)
    SeqEntryFree (sepP);
  FileClose (fioT);
  FileClose (fioP);
  if (flagFastaL || flagFastal)
    EntrezFini ();
  if (fout != stdout)
    FileClose (fout);
  BLASTOptionDelete (blstopt);
  if (flagPattern)
    ComPatFree (cpph);

  return 0;
}