Codebase list aegean / upstream/latest src / locuspocus.c
upstream/latest

Tree @upstream/latest (Download .tar.gz)

locuspocus.c @upstream/latestraw · 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
426
427
428
429
430
431
432
433
434
435
436
437
438
/**

Copyright (c) 2010-2015, Daniel S. Standage and CONTRIBUTORS

The AEGeAn Toolkit is distributed under the ISC License. See
the 'LICENSE' file in the AEGeAn source code distribution or
online at https://github.com/standage/AEGeAn/blob/master/LICENSE.

**/

#include <getopt.h>
#include <string.h>
#include "genometools.h"
#include "aegean.h"

// Data structure for program options
typedef struct
{
  bool debug;
  GtHashmap *filter;
  FILE *genestream;
  char *nameformat;
  unsigned long delta;
  GtFile *outstream;
  void (*filefreefunc)(GtFile *);
  GtHashmap *type_parents;
  int endmode;
  FILE *transstream;
  bool pseudofix;
  bool verbose;
  bool skipiiLoci;
  bool refine;
  bool by_cds;
  GtUword minoverlap;
  FILE *ilenfile;
  bool retain;
} LocusPocusOptions;

// Set default values for program
static void set_option_defaults(LocusPocusOptions *options)
{
  options->debug = false;
  options->filter = gt_hashmap_new(GT_HASH_STRING, gt_free_func, gt_free_func);
  gt_hashmap_add(options->filter, gt_cstr_dup("gene"), gt_cstr_dup("gene"));
  options->genestream = NULL;
  options->nameformat = NULL;
  options->delta = 500;
  options->outstream = gt_file_new_from_fileptr(stdout);
  options->filefreefunc = gt_file_delete_without_handle;
  options->type_parents = gt_hashmap_new(GT_HASH_STRING, gt_free_func,
                                         gt_free_func);
  options->endmode = 0;
  options->transstream = NULL;
  options->pseudofix = false;
  options->verbose = false;
  options->skipiiLoci = false;
  options->refine = false;
  options->by_cds = false;
  options->minoverlap = 1;
  options->ilenfile = NULL;
  options->retain = false;
}

static void free_option_memory(LocusPocusOptions *options)
{
  options->filefreefunc(options->outstream);
  gt_hashmap_delete(options->type_parents);
  gt_hashmap_delete(options->filter);
  if(options->genestream != NULL)
    fclose(options->genestream);
  if(options->transstream != NULL)
    fclose(options->transstream);
  if(options->nameformat != NULL)
    gt_free(options->nameformat);
  if(options->ilenfile != NULL)
    fclose(options->ilenfile);
}

// Usage statement
static void print_usage(FILE *outstream)
{
  fprintf(outstream,
"\nLocusPocus: calculate locus coordinates for the given gene annotation\n"
"Usage: locuspocus [options] gff3file1 [gff3file2 gff3file3 ...]\n"
"  Basic options:\n"
"    -d|--debug             print detailed debugging messages to terminal\n"
"                           (standard error)\n"
"    -h|--help              print this help message and exit\n"
"    -v|--version           print version number and exit\n\n"
"  iLocus parsing:\n"
"    -l|--delta: INT        when parsing interval loci, use the following\n"
"                           delta to extend gene loci and include potential\n"
"                           regulatory regions; default is 500\n"
"    -s|--skipends          when enumerating interval loci, exclude\n"
"                           unannotated (and presumably incomplete) iLoci at\n"
"                           either end of the sequence\n"
"    -e|--endsonly          report only incomplete iLocus fragments at the\n"
"                           unannotated ends of sequences (complement of\n"
"                           --skipends)\n"
"    -y|--skipiiloci        do not report intergenic iLoci\n\n"
"  Refinement options:\n"
"    -r|--refine            by default genes are grouped in the same iLocus\n"
"                           if they have any overlap; 'refine' mode allows\n"
"                           for a more nuanced handling of overlapping genes\n"
"    -c|--cds               use CDS rather than UTRs for determining gene\n"
"                           overlap; implies 'refine' mode\n"
"    -m|--minoverlap: INT   the minimum number of nucleotides two genes must\n"
"                           overlap to be grouped in the same iLocus; default\n"
"                           is 1\n\n"
"  Output options:\n"
"    -n|--namefmt: STR     provide a printf-style format string to override\n"
"                           the default ID format for newly created loci;\n"
"                           default is 'locus%%lu' (locus1, locus2, etc) for\n"
"                           loci and 'iLocus%%lu' (iLocus1, iLocus2, etc) for\n"
"                           interval loci; note the format string should\n"
"                           include a single %%lu specifier to be filled in\n"
"                           with a long unsigned integer value\n"
"    -i|--ilens: FILE       create a file with the lengths of each intergenic\n"
"                           iLocus\n"
"    -g|--genemap: FILE     print a mapping from each gene annotation to its\n"
"                           corresponding locus to the given file\n"
"    -o|--outfile: FILE     name of file to which results will be written;\n"
"                           default is terminal (standard output)\n"
"    -T|--retainids         retain original feature IDs from input files;\n"
"                           conflicts will arise if input contains duplicated\n"
"                           ID values\n"
"    -t|--transmap: FILE    print a mapping from each transcript annotation\n"
"                           to its corresponding locus to the given file\n"
"    -V|--verbose           include all locus subfeatures (genes, RNAs, etc)\n"
"                           in the GFF3 output; default includes only locus\n"
"                           features\n\n"
"  Input options:\n"
"    -f|--filter: TYPE      comma-separated list of feature types to use in\n"
"                           constructing loci/iLoci; default is 'gene'\n"
"    -p|--parent: CT:PT     if a feature of type $CT exists without a parent,\n"
"                           create a parent for this feature with type $PT;\n"
"                           for example, mRNA:gene will create a gene feature\n"
"                           as a parent for any top-level mRNA feature;\n"
"                           this option can be specified multiple times\n"
"    -u|--pseudo            correct erroneously labeled pseudogenes\n\n");
}

// Adjust program settings from command-line arguments/options
static void
parse_options(int argc, char **argv, LocusPocusOptions *options, GtError *error)
{
  int opt = 0;
  int optindex = 0;
  const char *optstr = "cdef:g:hi:l:m:n:o:p:rsTt:uVvy";
  const char *key, *value, *oldvalue;
  const struct option locuspocus_options[] =
  {
    { "cds",        no_argument,       NULL, 'c' },
    { "debug",      no_argument,       NULL, 'd' },
    { "endsonly",   no_argument,       NULL, 'e' },
    { "filter",     required_argument, NULL, 'f' },
    { "genemap",    required_argument, NULL, 'g' },
    { "help",       no_argument,       NULL, 'h' },
    { "ilens",      required_argument, NULL, 'i' },
    { "delta",      required_argument, NULL, 'l' },
    { "minoverlap", required_argument, NULL, 'm' },
    { "namefmt",    required_argument, NULL, 'n' },
    { "outfile",    required_argument, NULL, 'o' },
    { "parent",     required_argument, NULL, 'p' },
    { "refine",     no_argument,       NULL, 'r' },
    { "skipends",   no_argument,       NULL, 's' },
    { "retainids",  no_argument,       NULL, 'T' },
    { "transmap",   required_argument, NULL, 't' },
    { "pseudo",     no_argument,       NULL, 'u' },
    { "version",    no_argument,       NULL, 'v' },
    { "verbose",    no_argument,       NULL, 'V' },
    { "skipiiloci", no_argument,       NULL, 'y' },
    { NULL,         no_argument,       NULL,  0  },
  };
  for( opt = getopt_long(argc, argv + 0, optstr, locuspocus_options, &optindex);
       opt != -1;
       opt = getopt_long(argc, argv + 0, optstr, locuspocus_options, &optindex))
  {
    if(opt == 'c')
    {
      options->by_cds = 1;
      options->refine = 1;
    }
    else if(opt == 'd')
      options->debug = 1;
    else if(opt == 'e')
    {
      if(options->endmode < 0)
      {
        gt_error_set(error, "cannot set 'skipends' and 'endsonly' options"
                     "simultaneously; stubbornly refusing to proceed");
      }
      options->endmode = 1;
    }
    else if(opt == 'f')
    {
      gt_hashmap_delete(options->filter);
      options->filter = gt_hashmap_new(GT_HASH_STRING, gt_free_func,
                                       gt_free_func);

      value = strtok(optarg, ",");
      gt_hashmap_add(options->filter, gt_cstr_dup(value), gt_cstr_dup(value));
      while((value = strtok(NULL, ",")) != NULL)
      {
        oldvalue = gt_hashmap_get(options->filter, value);
        if(!oldvalue)
        {
          gt_hashmap_add(options->filter, gt_cstr_dup(value),
                         gt_cstr_dup(value));
        }
      }
    }
    else if(opt == 'g')
    {
      options->genestream = fopen(optarg, "w");
      if(options->genestream == NULL)
        gt_error_set(error, "could not open genemap file '%s'", optarg);
    }
    else if(opt == 'h')
    {
      print_usage(stdout);
      exit(0);
    }
    else if(opt == 'i')
    {
      options->ilenfile = fopen(optarg, "w");
      if(options->ilenfile == NULL)
        gt_error_set(error, "could not open ilenfile file '%s'", optarg);
    }
    else if(opt == 'l')
    {
      if(sscanf(optarg, "%lu", &options->delta) == EOF)
      {
        gt_error_set(error, "could not convert delta '%s' to an integer",
                     optarg);
      }
    }
    else if(opt == 'm')
    {
      if(sscanf(optarg, "%lu", &options->minoverlap) == EOF)
      {
        gt_error_set(error, "could not convert overlap '%s' to an integer",
                     optarg);
      }
    }
    else if(opt == 'n')
    {
      if(options->nameformat != NULL)
        gt_free(options->nameformat);
      options->nameformat = gt_cstr_dup(optarg);
    }
    else if(opt == 'o')
    {
      options->filefreefunc(options->outstream);
      options->outstream = gt_file_new(optarg, "w", error);
      options->filefreefunc = gt_file_delete;
    }
    else if(opt == 'p')
    {
      key = strtok(optarg, ":");
      value = strtok(NULL, ":");
      oldvalue = gt_hashmap_get(options->type_parents, "key");
      if(oldvalue)
      {
        gt_error_set(error, "cannot set parent type for '%s' to '%s'; "
                     "already set to '%s'", key, value, oldvalue);
      }
      else
      {
        gt_hashmap_add(options->type_parents, gt_cstr_dup(key),
                       gt_cstr_dup(value));
      }
    }
    else if(opt == 'r')
      options->refine = 1;
    else if(opt == 's')
    {
      if(options->endmode > 0)
      {
        gt_error_set(error, "cannot set 'skipends' and 'endsonly' options"
                     "simultaneously; stubbornly refusing to proceed");
      }
      options->endmode = -1;
    }
    else if(opt == 'T')
      options->retain = true;
    else if(opt == 't')
    {
      options->transstream = fopen(optarg, "w");
      if(options->transstream == NULL)
        gt_error_set(error, "could not open transmap file '%s'", optarg);
    }
    else if(opt == 'u')
      options->pseudofix = 1;
    else if(opt == 'v')
    {
      agn_print_version("LocusPocus", stdout);
      exit(0);
    }
    else if(opt == 'V')
      options->verbose = 1;
    else if(opt == 'y')
      options->skipiiLoci = true;
  }
}

// Main program
int main(int argc, char **argv)
{
  GtError *error;
  GtLogger *logger;
  GtQueue *streams;
  GtNodeStream *current_stream, *last_stream;
  gt_lib_init();

  // Parse command-line options
  LocusPocusOptions options;
  set_option_defaults(&options);
  error = gt_error_new();
  parse_options(argc, argv, &options, error);
  if(gt_error_is_set(error))
  {
    fprintf(stderr, "[LocusPocus] error: %s", gt_error_get(error));
    return 1;
  }
  int numfiles = argc - optind;
  if(numfiles < 1)
  {
    fprintf(stderr, "[LocusPocus] error: must provide at least one GFF3 file "
            "as input; use '-' if you want to provide data via standard "
            "input\n");
    print_usage(stderr);
    return 1;
  }

  logger = gt_logger_new(true, "", stderr);
  streams = gt_queue_new();


  //----- Set up the node processing stream -----//
  //---------------------------------------------//

  current_stream = gt_gff3_in_stream_new_unsorted(numfiles,
                                                  (const char **)argv + optind);
  gt_gff3_in_stream_check_id_attributes((GtGFF3InStream *)current_stream);
  gt_gff3_in_stream_enable_tidy_mode((GtGFF3InStream *)current_stream);
  gt_queue_add(streams, current_stream);
  last_stream = current_stream;

  if(options.pseudofix)
  {
    current_stream = agn_pseudogene_fix_stream_new(last_stream);
    gt_queue_add(streams, current_stream);
    last_stream = current_stream;
  }

  current_stream = agn_infer_parent_stream_new(last_stream,
                                               options.type_parents);
  gt_queue_add(streams, current_stream);
  last_stream = current_stream;

  current_stream = agn_filter_stream_new(last_stream, options.filter);
  gt_queue_add(streams, current_stream);
  last_stream = current_stream;

  current_stream = gt_sort_stream_new(last_stream);
  gt_queue_add(streams, current_stream);
  last_stream = current_stream;

  current_stream = agn_locus_stream_new(last_stream, options.delta);
  AgnLocusStream *ls = (AgnLocusStream*)current_stream;
  agn_locus_stream_set_source(ls, "AEGeAn::LocusPocus");
  agn_locus_stream_set_endmode(ls, options.endmode);
  agn_locus_stream_track_ilens(ls, options.ilenfile);
  if(options.nameformat != NULL)
    agn_locus_stream_set_name_format(ls, options.nameformat);
  if(options.skipiiLoci)
    agn_locus_stream_skip_iiLoci(ls);
  gt_queue_add(streams, current_stream);
  last_stream = current_stream;

  if(options.refine)
  {
    current_stream = agn_locus_refine_stream_new(last_stream, options.delta,
                                                 options.minoverlap,
                                                 options.by_cds);
    AgnLocusRefineStream *lrs = (AgnLocusRefineStream *)current_stream;
    agn_locus_refine_stream_set_source(lrs, "AEGeAn::LocusPocus");
    agn_locus_refine_stream_track_ilens(lrs, options.ilenfile);
    if(options.nameformat != NULL)
      agn_locus_refine_stream_set_name_format(lrs, options.nameformat);
    gt_queue_add(streams, current_stream);
    last_stream = current_stream;
  }

  if(options.genestream != NULL || options.transstream != NULL)
  {
    current_stream = agn_locus_map_stream_new(last_stream, options.genestream,
                                              options.transstream);
    gt_queue_add(streams, current_stream);
    last_stream = current_stream;
  }

  if(options.verbose == 0)
  {
    current_stream = agn_remove_children_stream_new(last_stream);
    gt_queue_add(streams, current_stream);
    last_stream = current_stream;
  }

  current_stream = gt_gff3_out_stream_new(last_stream, options.outstream);
  if(options.retain)
    gt_gff3_out_stream_retain_id_attributes((GtGFF3OutStream *)current_stream);
  gt_queue_add(streams, current_stream);
  last_stream = current_stream;


  //----- Execute the node processing stream -----//
  //----------------------------------------------//

  int result = gt_node_stream_pull(last_stream, error);
  if(result == -1)
    fprintf(stderr, "[LocusPocus] error: %s", gt_error_get(error));


  // Free memory and terminate
  while(gt_queue_size(streams) > 0)
  {
    current_stream = gt_queue_get(streams);
    gt_node_stream_delete(current_stream);
  }
  gt_queue_delete(streams);
  gt_logger_delete(logger);
  gt_error_delete(error);
  free_option_memory(&options);
  gt_lib_clean();
  return 0;
}