Codebase list tigr-glimmer / 9bc5b1f SimpleMake / anomaly.cc
9bc5b1f

Tree @9bc5b1f (Download .tar.gz)

anomaly.cc @9bc5b1fraw · history · blame

  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
//  A. L. Delcher
//
//  File:  anomaly.cc
//
//  Last Modified:  Fri May 19 17:10:05 EDT 2006
//
//  This program reads the sequence in the first command-line
//  file and then takes the start and end positions specified in
//  the second command-line file and checks for anomalous
//  start/stop codons and frame shifts.


#include  "anomaly.hh"


// Global variables

static bool  Check_Previous_Stop = false;
  // Determines whether to check if codon before start coordinate
  // is a valid stop codon
static bool  Check_Start_Codon = true;
  // Determines whether to check if first codon is a valid start
static char  * Coord_File_Name = NULL;
  // From the second command-line argument
static int  Num_Start_Codons;
  // Number of different start codon patterns
static int  Num_Stop_Codons;
  // Number of different stop codon patterns
static char  * Sequence_File_Name = NULL;
  // From the first command-line argument
static vector <const char *>  Start_Codon;
  // Sequences assumed to be start codons
static vector <const char *>  Stop_Codon;
  // Sequences assumed to be stop codons


int  main
    (int argc, char * argv [])

  {
   FILE  * fp;
   string  Data, hdr;
   char  * Buffer, Line [MAX_LINE], Name [MAX_LINE];
   char  Codon [4] = "tag";
   int  Direction, Frame_Shift;
   long int  Buffer_Len, Gene_Len;
   long int  i, j, Begin, End, Len, Start, Stop;
   int  problem_ct = 0, ok_ct = 0;
   
   try
     {
      Parse_Command_Line (argc, argv);

      Set_Start_And_Stop_Codons ();

      fp = File_Open (Sequence_File_Name, "r");

      Buffer = (char *) Safe_malloc (INIT_SIZE);
      Buffer_Len = INIT_SIZE;

      Fasta_Read (fp, Data, hdr);

      fclose (fp);

      Len = Data . length ();
      Data . insert (Data . begin (), 'x');
        // Put a dummy character at the front of Data so subscripts
        // will start at 1

      fp = File_Open (Coord_File_Name, "r");

      while  (fgets (Line, MAX_LINE, fp) != NULL)
        {
         bool  problem = false;

         if  (sscanf (Line, "%s %ld %ld", Name, & Start, & End) != 3)
             {
              printf ("Bad line:  %s\n...Skipping\n", Line);
              continue;
             }

         if  (Start < End && End - Start <= Len / 2 || Start - End > Len / 2)
             {
              Direction = +1;
              Gene_Len = 1 + End - Start;
              if  (Gene_Len < 0)
                  Gene_Len += Len;

              if  (Buffer_Len < Gene_Len + 1)
                  Buffer = (char *) Safe_realloc (Buffer, 1 + Gene_Len);
              Buffer_Len = 1 + Gene_Len;
              for  (i = 0;  i < Gene_Len;  i ++)
                {
                 if  (Start + i <= Len)
                     j = Start + i;
                   else
                     j = Start + i - Len;
                 Buffer [i] = tolower (Data [j]);
                }
              Buffer [i] = '\0';
             }
           else
             {
              Direction = -1;
              Gene_Len = 1 + Start - End;
              if  (Gene_Len < 0)
                  Gene_Len += Len;

              if  (Buffer_Len < Gene_Len + 1)
                  Buffer = (char *) Safe_realloc (Buffer, 1 + Gene_Len);
              Buffer_Len = 1 + Gene_Len;
              for  (i = 0;  i < Gene_Len;  i ++)
                {
                 if  (Start - i >= 1)
                     j = Start - i;
                   else
                     j = Start - i + Len;
                 Buffer [i] = Complement (tolower (Data [j]));
                }
              Buffer [i] = '\0';
             }

         if  (Check_Previous_Stop)
             {
              if  (Direction == +1)
                  {
                   for  (i = 3;  i > 0;  i --)
                     if  (Start - i < 1)
                         Codon [i] = tolower (Data [Start - i + Len]);
                       else
                         Codon [i] = tolower (Data [Start - i]);
                  }
                else
                  {
                   for  (i = 3;  i > 0;  i --)
                     if  (Start + i > Len)
                         Codon [i] = Complement (tolower (Data [Start + i - Len]));
                       else
                         Codon [i] = Complement (tolower (Data [Start + i]));
                  }
              if  (! Is_Stop_Codon (Codon))
                  {
                   printf ("%-10s %8ld %8ld no stop before start\n",
                                  Name, Start, End);
                   problem = true;
                  }
             }
         if  (Check_Start_Codon && ! Is_Start_Codon (Buffer))
             {
              printf ("%-10s has bad start codon = %.3s\n", Name, Buffer);
              problem = true;
             }
         if  (! Is_Stop_Codon (Buffer + Gene_Len - 3))
             {
              printf ("%-10s has bad stop codon = %s\n", Name, Buffer + Gene_Len - 3);
              problem = true;
              for  (j = Gene_Len;  j < Len;  j += 3)
                {
                 for  (i = 0;  i < 3;  i ++)
                   if  (Direction == +1)
                       {
                        if  (Start + i + j > Len)
                            Codon [i] = tolower (Data [Start + i + j - Len]);
                          else
                            Codon [i] = tolower (Data [Start + i + j]);
                       }
                     else
                       {
                        if  (Start - i - j < 1)
                            Codon [i] = Complement (tolower (Data [Start - i - j + Len]));
                          else
                            Codon [i] = Complement (tolower (Data [Start - i - j]));
                       }
                 if  (Is_Stop_Codon (Codon))
                     break;
                }
              assert (j < Len);
              printf ("           next stop occurs at offset %ld"
                   "  Gene_Len = %ld  diff = %+ld\n",
                   j, Gene_Len, j - Gene_Len + 3);
             }

         Frame_Shift = (Gene_Len % 3);
         if  (Frame_Shift)
             {
              printf ("%-10s %8ld %8ld has %+d frame shift\n",
                              Name, Start, End, Frame_Shift);
              problem = true;

              for  (i = 0;  i < Gene_Len - 3;  i += 3)
                if  (Is_Stop_Codon (Buffer + i))
                    break;
              if  (i < Gene_Len - 3)
                  {
                   Stop = Start + Direction * (i - 1);
                   if  (Stop < 1)
                       Stop += Len;
                   else if  (Stop > Len)
                       Stop -= Len;
                   printf ("   Best prefix is %8ld %8ld   Len = %ld\n",
                                Start, Stop, i);
                  }
                else
                  {
                   printf ("   No stop found in start frame\n");
                   continue;
                  }

              for  (i = Gene_Len - 6;  i >= 0;  i -= 3)
                if  (Is_Stop_Codon (Buffer + i))
                    break;
              i += 3;
              Begin = Start + Direction * i;
              if  (Begin < 1)
                  Begin += Len;
              else if  (Stop > Len)
                  Begin -= Len;
              printf ("   Best suffix is %8ld %8ld   Len = %ld\n",
                           Begin, End, Gene_Len - i - 3);

             }
           else
             {
              for  (i = 0;  i < Gene_Len - 3;  i += 3)
                if  (Is_Stop_Codon (Buffer + i))
                    {
                     printf ("%-10s has stop codon %.3s at offset %ld"
                          "  Gene_Len = %ld  diff = %+ld\n",
                          Name, Buffer + i, i, Gene_Len, Gene_Len - 3 - i);
                     problem = true;
                    }
             }
         if  (problem)
             problem_ct ++;
           else
             ok_ct ++;
        }

      fprintf (stderr, "     OK orfs = %7d\n", ok_ct);
      fprintf (stderr, "Problem orfs = %7d\n", problem_ct);
     }
   catch (std :: exception & e)
     {
      cerr << "** Standard Exception **" << endl;
      cerr << e << endl;
      exit (EXIT_FAILURE);
     }

   return  0;
  }



static bool  Is_Start_Codon
    (const char * p)

//  Return true iff the first 3 characters of p match a
//  string in global  Start_Codon .

  {
   int  i;

   for  (i = 0;  i < Num_Start_Codons;  i ++)
     if  (strncmp (p, Start_Codon [i], 3) == 0)
         return  true;

   return  false;
  }



static bool  Is_Stop_Codon
    (const char * p)

//  Return true iff the first 3 characters of p match a
//  string in global  Stop_Codon .

  {
   int  i;

   for  (i = 0;  i < Num_Stop_Codons;  i ++)
     if  (strncmp (p, Stop_Codon [i], 3) == 0)
         return  true;

   return  false;
  }



static void  Parse_Command_Line
    (int argc, char * argv [])

//  Get options and parameters from command line with  argc
//  arguments in  argv [0 .. (argc - 1)] .

  {
   char  * p, * q;
   bool  errflg = false;
   int  ch;

   optarg = NULL;

   while  (! errflg && ((ch = getopt (argc, argv, "A:stZ:")) != EOF))
     switch  (ch)
       {
        case  'A' :
          Start_Codon . clear ();
          for  (p = strtok (optarg, ",");  p != NULL;  p = strtok (NULL, ","))
            {
             q = strdup (p);
             Make_Lower_Case (q);
             Start_Codon . push_back (q);
            }
          break;

        case  's' :
          Check_Start_Codon = FALSE;
          break;

        case  't' :
          Check_Previous_Stop = TRUE;
          break;

        case  'Z' :
          Stop_Codon . clear ();
          for  (p = strtok (optarg, ",");  p != NULL;  p = strtok (NULL, ","))
            {
             q = strdup (p);
             Make_Lower_Case (q);
             Stop_Codon . push_back (q);
            }
          break;

        case  '?' :
          fprintf (stderr, "Unrecognized option -%c\n", optopt);

        default :
          errflg = true;
       }

   if  (errflg)
       {
        Usage ();
        exit (EXIT_FAILURE);
       }

   if  (optind > argc - 2)
       {
        Usage ();
        exit (EXIT_FAILURE);
       }

   Sequence_File_Name = argv [optind ++];
   Coord_File_Name = argv [optind ++];

   return;
  }



static void  Set_Start_And_Stop_Codons
    (void)

//  Set globals  Start_Codon  and  Stop_Codon  to the sequences
//  that are allowed to be start and stop codons for genes.

  {
   int  i, n;

   if  (Start_Codon . size () == 0)
       {
        n = sizeof (DEFAULT_START_CODON) / sizeof (char *);
        for  (i = 0;  i < n;  i ++)
          Start_Codon . push_back (DEFAULT_START_CODON [i]);
       }

   if  (Stop_Codon . size () == 0)
       {
        n = sizeof (DEFAULT_STOP_CODON) / sizeof (char *);
        for  (i = 0;  i < n;  i ++)
          Stop_Codon . push_back (DEFAULT_STOP_CODON [i]);
       }

   Num_Start_Codons = Start_Codon . size ();
   Num_Stop_Codons = Stop_Codon . size ();

   return;
  }



static void  Usage
    (void)

//  Print to stderr description of options and command line for
//  this program.

  {
   fprintf (stderr,
       "USAGE:  anomaly [options] <sequence-file> <coord-file>\n"
       "\n"
       "Read DNA sequence in <sequence-file> and for each region specified\n"
       "by the coordinates in <coord-file>, check whether the region\n"
       "represents a normal gene, i.e., it begins with a start codon, ends\n"
       "with a stop codon, and has no frame shifts.\n"
       "Output goes to standard output.\n"
       "\n"
       "Options:\n"
       " -A <codon-list>\n"
       "    Use comma-separated list of codons as start codons\n"
       "    Sample format:  -A atg,gtg\n"
       "    Default start codons are atg,gtg,ttg\n"
       " -s\n"
       "    Omit the check that the first codon is a start codon.\n"
       " -t\n"
       "    Check whether the codon preceding the start coordinate position\n"
       "    is a stop codon.  This is useful if the coordinates represent\n"
       "    the entire region between stop codons.\n"
       " -Z <codon-list>\n"
       "    Use comma-separated list of codons as stop codons\n"
       "    Sample format:  -Z tag,tga,taa\n"
       "\n");

   return;
  }