File: gene2xml.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (599 lines) | stat: -rw-r--r-- 14,861 bytes parent folder | download | duplicates (2)
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*   gene2xml.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  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 do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  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.
*
* ===========================================================================
*
* File Name:  gene2xml.c
*
* Author:  Jonathan Kans
*
* Version Creation Date:   1/27/05
*
* $Revision: 1.10 $
*
* File Description:
*
* Modifications:
* --------------------------------------------------------------------------
* ==========================================================================
*/

#include <ncbi.h>
#include <objall.h>
#include <objsset.h>
#include <objsub.h>
#include <objfdef.h>
#include <sqnutils.h>

#define NLM_GENERATED_CODE_PROTO
#include <objentgene.h>

#define GENE2XMLAPP_VER "1.3"

CharPtr GENE2XMLAPPLICATION = GENE2XMLAPP_VER;

NLM_EXTERN void AsnPrintNewLine PROTO((AsnIoPtr aip));

static Boolean CombineEG (
  AsnIoPtr aip,
  AsnIoPtr aop
)

{
  AsnTypePtr     atp_egs, atp_egse;
  EntrezgenePtr  egp;
  EntrezgeneSet  es;

  if (aip == NULL || aop == NULL) return FALSE;

  atp_egs = AsnFind ("Entrezgene-Set");
  if (atp_egs == NULL) {
    Message (MSG_POSTERR, "AsnFind failure on Entrezgene-Set");
    return FALSE;
  }

  atp_egse = AsnFind ("Entrezgene-Set.E");
  if (atp_egse == NULL) {
    Message (MSG_POSTERR, "AsnFind failure on Entrezgene-Set.E");
    return FALSE;
  }

  MemSet ((Pointer) &es, 0, sizeof (EntrezgeneSet));

  if (! AsnOpenStruct (aop, atp_egs, (Pointer) &es)) return FALSE;

  egp = EntrezgeneAsnRead (aip, NULL);
  while (egp != NULL) {
    EntrezgeneAsnWrite (egp, aop, atp_egse);
    EntrezgeneFree (egp);
    egp = EntrezgeneAsnRead (aip, NULL);
  }

  if (! AsnCloseStruct (aop, atp_egs, (Pointer) &es)) return FALSE;

  return TRUE;
}

static Boolean ExtractEG (
  AsnIoPtr aip,
  AsnIoPtr aop
)

{
  AsnModulePtr   amp;
  AsnTypePtr     atp, atp_egs, atp_egse;
  EntrezgenePtr  egp;

  if (aip == NULL || aop == NULL) return FALSE;

  amp = AsnAllModPtr ();
  if (amp == NULL) {
    Message (MSG_POSTERR, "Unable to load AsnAllModPtr");
    return 1;
  }

  atp_egs = AsnFind ("Entrezgene-Set");
  if (atp_egs == NULL) {
    Message (MSG_POSTERR, "AsnFind failure on Entrezgene-Set");
    return FALSE;
  }

  atp_egse = AsnFind ("Entrezgene-Set.E");
  if (atp_egse == NULL) {
    Message (MSG_POSTERR, "AsnFind failure on Entrezgene-Set.E");
    return FALSE;
  }

  atp = atp_egs;

  while ((atp = AsnReadId (aip, amp, atp)) != NULL) {
    if (atp == atp_egse) {
      egp = EntrezgeneAsnRead (aip, atp);
      EntrezgeneAsnWrite (egp, aop, NULL);
      EntrezgeneFree (egp);
      AsnPrintNewLine (aop);
      AsnIoFlush (aop);
      AsnIoReset (aop);
    } else {
      AsnReadVal (aip, atp, NULL);
    }
  }

  return TRUE;
}

static Boolean HasDesiredTaxId (
  EntrezgenePtr egp,
  Int4 taxid
)


{
  BioSourcePtr  biop;
  DbtagPtr      dbt;
  ObjectIdPtr   oip;
  OrgRefPtr     orp;
  ValNodePtr    vnp;

  if (egp == NULL) return FALSE;

  biop = (BioSourcePtr) egp->source;
  if (biop == NULL) return FALSE;
  orp = biop->org;
  if (orp == NULL) return FALSE;

  for (vnp = orp->db; vnp != NULL; vnp = vnp->next) {
    dbt = (DbtagPtr) vnp->data.ptrvalue;
    if (dbt == NULL) continue;
    if (StringCmp (dbt->db, "taxon") != 0) continue;
    oip = dbt->tag;
    if (oip == NULL) continue;
    if (oip->id == taxid) return TRUE;
  }

  return FALSE;
}

static Boolean GenerateXML (
  AsnIoPtr aip,
  AsnIoPtr aop,
  Int4 taxid
)

{
  AsnModulePtr   amp;
  AsnTypePtr     atp, atp_egs, atp_egse;
  DataVal        dv;
  EntrezgenePtr  egp;

  if (aip == NULL || aop == NULL) return FALSE;

  amp = AsnAllModPtr ();
  if (amp == NULL) {
    Message (MSG_POSTERR, "Unable to load AsnAllModPtr");
    return 1;
  }

  atp_egs = AsnFind ("Entrezgene-Set");
  if (atp_egs == NULL) {
    Message (MSG_POSTERR, "AsnFind failure on Entrezgene-Set");
    return FALSE;
  }

  atp_egse = AsnFind ("Entrezgene-Set.E");
  if (atp_egse == NULL) {
    Message (MSG_POSTERR, "AsnFind failure on Entrezgene-Set.E");
    return FALSE;
  }

  atp = atp_egs;

  /*
  while ((atp = AsnReadId (aip, amp, atp)) != NULL) {
    AsnReadVal (aip, atp, &dv);
    AsnWrite (aop, atp, &dv);
    AsnKillValue (atp, &dv);
  }
  */

  while ((atp = AsnReadId (aip, amp, atp)) != NULL) {
    if (atp == atp_egse) {
      egp = EntrezgeneAsnRead (aip, atp);
      if (taxid == 0 || HasDesiredTaxId (egp, taxid)) {
        if (! EntrezgeneAsnWrite (egp, aop, atp)) {
          Message (MSG_POSTERR, "EntrezgeneAsnWrite failure");
        }
      }
      EntrezgeneFree (egp);
    } else {
      AsnReadVal (aip, atp, &dv);
      AsnWrite (aop, atp, &dv);
      AsnKillValue (atp, &dv);
    }
  }

  return TRUE;
}

static void ProcessOneRecord (
  CharPtr infile,
  CharPtr outfile,
  Int2 type,
  Boolean binary,
  Boolean compressed,
  Int4 taxid,
  Boolean zip
)

{
  AsnIoPtr  aip, aop;
  FILE      *fp;
  CharPtr   mode;
#ifdef OS_UNIX
  Char      cmmd [256];
  CharPtr   gzcatprog;
  CharPtr   gzipprog;
  int       ret;
  Boolean   usedPopen = FALSE;
#endif

#ifndef OS_UNIX
  if (compressed) {
    Message (MSG_POSTERR, "Can only decompress on-the-fly on UNIX machines");
    return;
  }
#endif

#ifdef OS_UNIX
  if (compressed) {
    gzcatprog = getenv ("NCBI_UNCOMPRESS_BINARY");
    if (gzcatprog != NULL) {
      sprintf (cmmd, "%s %s", gzcatprog, infile);
    } else {
      ret = system ("gzcat -h >/dev/null 2>&1");
      if (ret == 0) {
        sprintf (cmmd, "gzcat %s", infile);
      } else if (ret == -1) {
        Message (MSG_POSTERR, "Unable to fork or exec gzcat in ScanBioseqSetRelease");
        return;
      } else {
        ret = system ("zcat -h >/dev/null 2>&1");
        if (ret == 0) {
          sprintf (cmmd, "zcat %s", infile);
        } else if (ret == -1) {
          Message (MSG_POSTERR, "Unable to fork or exec zcat in ScanBioseqSetRelease");
          return;
        } else {
          Message (MSG_POSTERR, "Unable to find zcat or gzcat in ScanBioseqSetRelease - please edit your PATH environment variable");
          return;
        }
      }
    }
    fp = popen (cmmd, /* binary? "rb" : */ "r");
    usedPopen = TRUE;
  } else {
    fp = FileOpen (infile, binary? "rb" : "r");
  }
#else
  fp = FileOpen (infile, binary? "rb" : "r");
#endif
  if (fp == NULL) {
    Message (MSG_POSTERR, "FileOpen failed for input file '%s'", infile);
    return;
  }

  aip = AsnIoNew (binary? ASNIO_BIN_IN : ASNIO_TEXT_IN, fp, NULL, NULL, NULL);
  if (aip == NULL) {
    Message (MSG_ERROR, "AsnIoNew failed for input file '%s'", infile);
    return;
  }

  if (type == 1 && zip) {
    mode = "wb";
  } else if (type == 3) {
    mode = "wx";
  } else {
    mode = "w";
  }
  aop = AsnIoOpen (outfile, mode);
  if (aop == NULL) {
    AsnIoClose (aip);
    Message (MSG_POSTERR, "Unable to open output file");
    return;
  }

  /* process input file */

  switch (type) {
    case 1 :
      CombineEG (aip, aop);
      break;
    case 2 :
      ExtractEG (aip, aop);
      break;
    case 3 :
      GenerateXML (aip, aop, taxid);
      break;
    default :
      break;
  }

  AsnIoClose (aop);

  AsnIoFree (aip, FALSE);

#ifdef OS_UNIX
  if (usedPopen) {
    pclose (fp);
  } else {
    FileClose (fp);
  }
#else
  FileClose (fp);
#endif

#ifdef OS_UNIX
  if (zip) {
    gzipprog = getenv ("NCBI_COMPRESS_BINARY");
    if (gzipprog != NULL) {
      sprintf (cmmd, "%s -c %s > %s.gz", gzipprog, outfile, outfile);
    } else {
      ret = system ("gzip -h >/dev/null 2>&1");
      if (ret == 0) {
        sprintf (cmmd, "gzip -c %s > %s.gz", outfile, outfile);
        system (cmmd);
      } else if (ret == -1) {
        Message (MSG_POSTERR, "Unable to fork or exec gzip in gene2xml");
        return;
      }
    }
  }
#endif
}

static void FileRecurse (
  CharPtr directory,
  CharPtr results,
  CharPtr insuffix,
  CharPtr outsuffix,
  Int2 type,
  Boolean binary,
  Boolean compressed,
  Int4 taxid,
  Boolean zip,
  Boolean log
)

{
  Char        infile [FILENAME_MAX], inpath [PATH_MAX],
              outfile [FILENAME_MAX], outpath [PATH_MAX],
              path [PATH_MAX];
  CharPtr     ptr, str;
  ValNodePtr  head, vnp;

  if (insuffix == NULL) {
    insuffix = "";
  }
  if (outsuffix == NULL) {
    outsuffix = "";
  }

  /* get list of all files in source directory */

  head = DirCatalog (directory);

  for (vnp = head; vnp != NULL; vnp = vnp->next) {
    if (vnp->choice == 0) {
      str = (CharPtr) vnp->data.ptrvalue;
      if (! StringHasNoText (str)) {

        /* does filename have desired substring? */

        ptr = StringStr (str, insuffix);
        if (ptr != NULL) {
          *ptr = '\0';

          /* process file that has desired input suffix (.ags or .agc) */

          StringNCpy_0 (inpath, directory, sizeof (inpath));
          sprintf (infile, "%s%s", str, insuffix);
          FileBuildPath (inpath, NULL, infile);

          if (StringDoesHaveText (results)) {
            StringNCpy_0 (outpath, results, sizeof (outpath));
          } else {
            StringNCpy_0 (outpath, directory, sizeof (outpath));
          }
          sprintf (outfile, "%s%s", str, outsuffix);
          FileBuildPath (outpath, NULL, outfile);

          if (log) {
            Message (MSG_POST, "Processing %s", infile);
          }

          ProcessOneRecord (inpath, outpath, type, binary, compressed, taxid, zip);
        }
      }
    } else if (vnp->choice == 1) {

      /* recurse into subdirectory */

      StringNCpy_0 (path, directory, sizeof (path));
      str = (CharPtr) vnp->data.ptrvalue;
      FileBuildPath (path, str, NULL);
      FileRecurse (path, results, insuffix, outsuffix, type, binary, compressed, taxid, zip, log);
    }
  }

  /* clean up file list */

  ValNodeFreeData (head);
}

/* Args structure contains command-line arguments */

#define p_argInputPath   0
#define r_argOutputPath  1
#define i_argInputFile   2
#define o_argOutputFile  3
#define b_argBinary      4
#define c_argCompressed  5
#define t_argTaxonID     6
#define x_argExtract     7
#define y_argCombine     8
#define z_argZipResult   9
#define l_argLog        10


Args myargs [] = {
  {"Path to Files", NULL, NULL, NULL,
    TRUE, 'p', ARG_STRING, 0.0, 0, NULL},
  {"Path for Results", NULL, NULL, NULL,
    TRUE, 'r', ARG_STRING, 0.0, 0, NULL},
  {"Single Input File", "stdin", NULL, NULL,
    TRUE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
  {"Single Output File", "stdout", NULL, NULL,
    TRUE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
  {"File is Binary", "F", NULL, NULL,
    TRUE, 'b', ARG_BOOLEAN, 0.0, 0, NULL},
  {"File is Compressed", "F", NULL, NULL,
    TRUE, 'c', ARG_BOOLEAN, 0.0, 0, NULL},
  {"Taxon ID to Filter", "0", NULL, NULL,
    TRUE, 't', ARG_INT, 0.0, 0, NULL},
  {"Extract .ags -> text .agc", "F", NULL, NULL,
    TRUE, 'x', ARG_BOOLEAN, 0.0, 0, NULL},
  {"Combine .agc -> text .ags (for testing)", "F", NULL, NULL,
    TRUE, 'y', ARG_BOOLEAN, 0.0, 0, NULL},
  {"Combine .agc -> binary .ags, then gzip", "F", NULL, NULL,
    TRUE, 'z', ARG_BOOLEAN, 0.0, 0, NULL},
  {"Log Processing", "F", NULL, NULL,
    TRUE, 'l', ARG_BOOLEAN, 0.0, 0, NULL},
};

Int2 Main (void)

{
  Char     app [64], xmlbuf [128];
  Boolean  binary, compressed, combine, log, extract, zip;
  CharPtr  directory, infile, outfile, results, insuffix, outsuffix;
  Int4     taxid;
  Int2     type = 0;

  /* standard setup */

  ErrSetFatalLevel (SEV_MAX);
  ErrClearOptFlags (EO_SHOW_USERSTR);
  ErrSetLogfile ("stderr", ELOG_APPEND);
  UseLocalAsnloadDataAndErrMsg ();
  ErrPathReset ();

  if (! AllObjLoad ()) {
    Message (MSG_FATAL, "AllObjLoad failed");
    return 1;
  }
  if (! SubmitAsnLoad ()) {
    Message (MSG_FATAL, "SubmitAsnLoad failed");
    return 1;
  }
  if (! FeatDefSetLoad ()) {
    Message (MSG_FATAL, "FeatDefSetLoad failed");
    return 1;
  }
  if (! SeqCodeSetLoad ()) {
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
    return 1;
  }
  if (! GeneticCodeTableLoad ()) {
    Message (MSG_FATAL, "GeneticCodeTableLoad failed");
    return 1;
  }

  /* process command line arguments */

  sprintf (app, "gene2xml %s", GENE2XMLAPPLICATION);
  if (! GetArgs (app, sizeof (myargs) / sizeof (Args), myargs)) {
    return 0;
  }

  /* additional setup modifications */

  if (! objentgeneAsnLoad ()) {
    Message (MSG_POSTERR, "objentgeneAsnLoad failed");
    return 1;
  }

  if (GetAppParam ("NCBI", "SETTINGS", "XMLPREFIX", NULL, xmlbuf, sizeof (xmlbuf))) {
    AsnSetXMLmodulePrefix (StringSave (xmlbuf));
  }

  directory = (CharPtr) myargs [p_argInputPath].strvalue;
  results = (CharPtr) myargs [r_argOutputPath].strvalue;
  if (StringHasNoText (results)) {
    results = NULL;
  }
  infile = (CharPtr) myargs [i_argInputFile].strvalue;
  outfile = (CharPtr) myargs [o_argOutputFile].strvalue;

  binary = (Boolean) myargs [b_argBinary].intvalue;
  compressed = (Boolean) myargs [c_argCompressed].intvalue;
  taxid = (Int4) myargs [t_argTaxonID].intvalue;

  combine = (Boolean) myargs [y_argCombine].intvalue;
  extract = (Boolean) myargs [x_argExtract].intvalue;
  zip = (Boolean) myargs [z_argZipResult].intvalue;
  log = (Boolean) myargs [l_argLog].intvalue;

  if (zip) {
    if ((! combine) && (! extract)) {
      combine = TRUE;
    }
  }

  if (combine) {
    type = 1;
    insuffix = ".agc";
    outsuffix = ".ags";
  } else if (extract) {
    type = 2;
    insuffix = ".ags";
    outsuffix = ".agc";
  } else {
    type = 3;
    insuffix = ".ags";
    outsuffix = ".xgs";
  }

  /* process input file */

  if (StringDoesHaveText (directory)) {

    FileRecurse (directory, results, insuffix, outsuffix, type, binary, compressed, taxid, zip, log);

  } else if (StringDoesHaveText (infile) && StringDoesHaveText (outfile)) {

    ProcessOneRecord (infile, outfile, type, binary, compressed, taxid, zip);
  }

  return 0;
}