File: uncovered.cc

package info (click to toggle)
tigr-glimmer 3.02b-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 13,948 kB
  • sloc: cpp: 24,416; awk: 232; csh: 220; makefile: 147; sh: 51
file content (423 lines) | stat: -rw-r--r-- 10,408 bytes parent folder | download | duplicates (12)
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
//  A. L. Delcher
//
//  File:  uncovered.cc
//
//  Last Modified:  10 June 2005
//
//  This program reads the sequence in the file named as the
//  first command-line argument and then extracts from it
//  sequences that are not covered by the list of regions
//  specified in the file named as the second command-line argument.
//  Output is a multifasta file sent to stdout.



#include  "uncovered.hh"


// External variables

extern int  Verbose;
extern int  Global_Debug_Flag;


// Global variables

static char  * Coord_Info = NULL;
  // Name of file with list of coordinates (or a single coordinate
  // specification).
static bool  Fasta_Output_Format = true;
  // Determines format of output.
static bool  Is_Circular = true;
  // Determines whether the input sequence is regarded as a circular
  // genome.
static long int  Min_Len = 0;
  // Output sequences shorter than this are not printed.
static char  * Sequence_File_Name = NULL;
  // Name of file with input sequence
static bool  Skip_Start = false;
  // If set true (by -s option) then omit the first 3 letters
  // of each output sequence.
static bool  Skip_Stop = false;
  // If set true (by -t option) then omit the last 3 letters
  // of each output sequence.
static bool  Use_Direction = false;
  // If set true (by -d option) then use 4th field of coords
  // to determine direction in a circular genome.



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

  {
   FILE  * fp;
   vector <Region_t>  region_list;
   string  sequence, hdr;
   char  line [MAX_LINE], tag [MAX_LINE];
   long int  seq_len;

   Verbose = 0;

   Parse_Command_Line (argc, argv);

   fp = File_Open (Sequence_File_Name, "r");

   if  (! Fasta_Read (fp, sequence, hdr))
       {
        sprintf (Clean_Exit_Msg_Line, "ERROR:  Failed to read file %s",
             Sequence_File_Name);
        Clean_Exit (Clean_Exit_Msg_Line, __FILE__, __LINE__);
       }
   fclose (fp);

   seq_len = sequence . length ();

   if  (strcmp (Coord_Info, "-") == 0)
       fp = stdin;
     else
       fp = File_Open (Coord_Info, "r");

   while  (fgets (line, MAX_LINE, fp) != NULL)
     {
      Region_t  region;
      long int  i, j, start, end, extract_len;
      int  dir;

      if  (Use_Direction)
          {
           if  (sscanf (line, "%s %ld %ld %d", tag, & start, & end, & dir) != 4)
               {
                fprintf (stderr, "ERROR:  Skipped following coord line\n");
                fputs (line, stderr);
                continue;
               }
          }
        else
          {
           if  (sscanf (line, "%s %ld %ld", tag, & start, & end) != 3)
               {
                fprintf (stderr, "ERROR:  Skipped following coord line\n");
                fputs (line, stderr);
                continue;
               }
           if  ((start < end && (! Is_Circular || end - start <= seq_len / 2))
                    || (Is_Circular && start - end > seq_len / 2))
               dir = +1;
             else
               dir = -1;
          }

      if  (dir > 0)
          {
           extract_len = 1 + end - start;
           if  (extract_len < 0)
               extract_len += seq_len;

           i = start - 1;
           if  (Skip_Start)
               {
                i += 3;
                extract_len -= 3;
               }
           if  (Skip_Stop)
               extract_len -= 3;

           j = i + extract_len;
           if  (j <= seq_len)
               {
                region . lo = i;
                region . hi = j;
                region_list . push_back (region);
               }
             else  // wraparound end
               {
                region . lo = i;
                region . hi = seq_len;
                region_list . push_back (region);
                region . lo = 0;
                region . hi = j - seq_len;
                region_list . push_back (region);
               }
          }
        else
          {
           extract_len = 1 + start - end;
           if  (extract_len < 0)
               extract_len += seq_len;

           i = start;
           if  (Skip_Start)
               {
                i -= 3;
                extract_len -= 3;
               }
           if  (Skip_Stop)
               extract_len -= 3;

           j = i - extract_len;
           if  (j >= 0)
               {
                region . lo = j;
                region . hi = i;
                region_list . push_back (region);
               }
             else  // wraparound beginning
               {
                region . lo = 0;
                region . hi = i;
                region_list . push_back (region);
                region . lo = seq_len + j;
                region . hi = seq_len;
                region_list . push_back (region);
               }
          }
     }

   Coalesce_Regions (region_list);

   Output_Uncovered (sequence, seq_len, region_list);

   return  0;
  }



static void  Coalesce_Regions
    (vector <Region_t> & list)

//  Merge overlapping regions in  list  changing list to
//  a sorted list of the merged regions.

  {
   int  i, j, n;

   n = list . size ();
   if  (n == 0)
       return;

   sort (list . begin (), list . end (), Region_Cmp);

   j = 0;
   for  (i = 1;  i < n;  i ++)
     if  (list [i] . lo <= list [j] . hi)
         {
          if  (list [j] . hi < list [i] . hi)
              list [j] . hi = list [i] . hi;
         }
       else
         list [++ j] = list [i];

   list . resize (j + 1);

   return;
  }



static void  Output_Subsequence
     (const string & seq, long int i, long int len,
      const char * tag, long int start, long int end)

//  Print to stdout the subsequence of  seq  beginning at subscript
//   i  with length  len .  Use  tag ,  start  and  end  to label the output.

  {
   const int  fasta_width = 60;
   long int  ct = 0, line_ct = 0;

   if  (Fasta_Output_Format)
       printf (">%s  %ld %ld  len=%ld\n", tag, start, end, len);
     else
       printf ("%-10s ", tag);

   while  (ct < len)
     {
      if  (Fasta_Output_Format && line_ct == fasta_width)
          {
           putchar ('\n');
           line_ct = 0;
          }

      putchar (seq [i]);
      i ++;
      ct ++;
      line_ct ++;
     }
   putchar ('\n');

   return;
  }



static void  Output_Uncovered
    (const string & seq, long int seq_len, const vector <Region_t> & list)

//  Output the portions of  seq  that are not in  list  if they
//  are long enough.

  {
   long int  a, b, len;
   char  tag [100];
   int  i, n, ct = 0;

   n = list . size ();
   a = 0;

   for  (i = 0;  i < n;  i ++)
     {
      b = list [i] . lo;
      len = b - a;
      if  (len > 0 && len >= Min_Len)
          {
           sprintf (tag, "seq%05d", ++ ct);
           Output_Subsequence (seq, a, len, tag, a + 1, b);
          }
      a = list [i] . hi;
     }

   len = seq_len - a;
   if  (len > 0 && len >= Min_Len)
       {
        sprintf (tag, "seq%05d", ++ ct);
        Output_Subsequence (seq, a, len, tag, a + 1, seq_len);
       }

   return;
  }



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

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

  {
   bool  errflg = false;
   int  ch;

   optarg = NULL;

#if  ALLOW_LONG_OPTIONS
   int  option_index = 0;
   static struct option  long_options [] = {
        {"dir", 0, 0, 'd'},
        {"minlen", 1, 0, 'l'},
        {"nostart", 0, 0, 's'},
        {"nostop", 0, 0, 't'},
        {"nowrap", 0, 0, 'w'},
        {0, 0, 0, 0}
      };

   while  (! errflg
        && ((ch = getopt_long (argc, argv, "2dhl:sw",
                     long_options, & option_index)) != EOF))
#else
   while  (! errflg
        && ((ch = getopt (argc, argv, "2dhl:sw")) != EOF))
#endif

     switch  (ch)
       {
        case  '2' :
          Fasta_Output_Format = false;
          break;

        case  'd' :
          Use_Direction = true;
          break;

        case  'h' :
          Usage ();
          exit (EXIT_SUCCESS);

        case  'l' :
          Min_Len = strtol (optarg, NULL, 10);
          break;

        case  's' :
          Skip_Start = true;
          break;

        case  't' :
          Skip_Stop = true;
          break;

        case  'w' :
          Is_Circular = false;
          break;

        default :
          errflg = true;
       }

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

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

   return;
  }



static void  Usage
    (void)

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

  {
   fprintf (stderr,
       "USAGE:  uncovered [options] <sequence-file> <coords>\n"
       "\n"
       "Read fasta-format <sequence-file> and extract from it the\n"
       "subsequences not covered by regions specified in <coords>.\n"
       "By default, <coords>\n"
       "is the name of a file containing lines of the form\n"
       "  <tag>  <start>  <stop>  [<frame>] ...\n"
       "Coordinates are inclusive counting from 1, e.g., \"1 3\"\n"
       "represents the 1st 3 characters of the sequence.\n"
       "Output goes to stdout in multi-fasta format, unless the -2 option\n"
       "is specified\n"
       "\n"
       "Options:\n"
       " -2"
       "    Output each sequence as 2 fields (tag and sequence) on a single line\n"
       " -d\n"
       " --dir\n"
       "    Use the 4th column of each input line to specify the direction\n"
       "    of the sequence.  Positive is forward, negative is reverse\n"
       "    The input sequence is assumed to be circular\n"
       " -h\n"
       "    Print this message\n"
       " -l <n>\n"
       " --minlen <n>\n"
       "    Don't output any sequence shorter than <n> characters\n"
       " -s\n"
       " --nostart\n"
       "    Omit the first 3 characters of each <coords> region\n"
       " -t\n"
       " --nostop\n"
       "    Omit the last 3 characters of each <coords> region\n"
       " -w\n"
       " --nowrap\n"
       "    Use the actual input coordinates without any wraparound\n"
       "    that would be needed by a circular genome.  Without this\n"
       "    option, the output sequence is the shorter of the two ways\n"
       "    around the circle.  Use the -d option to specify direction\n"
       "    explicitly.\n"
       "\n");

   return;
  }