summaryrefslogtreecommitdiff
path: root/demo/releasescan.c
blob: 82f53b881594cc465d6347a21c65181bd8054d12 (plain)
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
/*   releasescan.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:  releasescan.c
*
* Author:  Jonathan Kans
*
* Version Creation Date:   6/6/00
*
* $Revision: 6.2 $
*
* File Description: 
*
* Modifications:  
* --------------------------------------------------------------------------
* Date     Name        Description of modification
* -------  ----------  -----------------------------------------------------
*
*
* ==========================================================================
*/

/* scans binary ASN.1 Bioseq-set release files */

#include <ncbi.h>
#include <objall.h>
#include <objsset.h>
#include <objsub.h>
#include <objfdef.h>
#include <seqport.h>
#include <sequtil.h>
#include <sqnutils.h>
#include <subutil.h>
#include <tofasta.h>
#include <gather.h>
#include <toasn3.h>
#include <explore.h>

static Boolean LIBCALLBACK DoFeat (SeqFeatPtr sfp, SeqMgrFeatContextPtr context)

{
  DbtagPtr     dbt;
  GBQualPtr    gbq;
  GeneRefPtr   grp;
  CharPtr      label;
  ObjectIdPtr  oip;
  BioseqPtr    prod;
  SeqFeatPtr   prot;
  ProtRefPtr   prp;
  RnaRefPtr    rrp;
  SeqIdPtr     sip;
  SeqLocPtr    slp;
  Int4         start;
  Int4         stop;
  Char         str [256];
  tRNAPtr      trna;
  ValNodePtr   vnp;

  FILE  *fp;

  fp = (FILE *) context->userdata;

  label = (CharPtr) FeatDefTypeLabel (sfp);
  if (StringCmp (label, "Gene") == 0) {
    label = "gene";
  }
  if (StringHasNoText (label)) {
    label = "???";
  }

  slp = SeqLocFindNext (sfp->location, NULL);
  if (slp == NULL) return TRUE;

  start = GetOffsetInBioseq (slp, context->bsp, SEQLOC_START) + 1;
  stop = GetOffsetInBioseq (slp, context->bsp, SEQLOC_STOP) + 1;
  fprintf (fp, "%ld\t%ld\t%s\n", (long) start, (long) stop, label);

  while ((slp = SeqLocFindNext (sfp->location, slp)) != NULL) {
    start = GetOffsetInBioseq (slp, context->bsp, SEQLOC_START) + 1;
    stop = GetOffsetInBioseq (slp, context->bsp, SEQLOC_STOP) + 1;
    if (start != 0 && stop != 0) {
      fprintf (fp, "%ld\t%ld\n", (long) start, (long) stop);
    }
  }

  switch (context->seqfeattype) {
    case SEQFEAT_GENE :
      grp = (GeneRefPtr) sfp->data.value.ptrvalue;
      if (grp != NULL) {
        StringNCpy_0 (str, (CharPtr) grp->locus, sizeof (str));
        if (! StringHasNoText (str)) {
          fprintf (fp, "\t\t\tgene\t%s\n", str);
        }
        for (vnp = grp->syn; vnp != NULL; vnp = vnp->next) {
          StringNCpy_0 (str, (CharPtr) vnp->data.ptrvalue, sizeof (str));
          if (! StringHasNoText (str)) {
            fprintf (fp, "\t\t\tgene_syn\t%s\n", str);
          }
        }
      }
      break;
    case SEQFEAT_CDREGION :
      prod = BioseqFind (SeqLocId (sfp->product));
      prot = SeqMgrGetBestProteinFeature (prod, NULL);
      if (prot != NULL) {
        prp = (ProtRefPtr) prot->data.value.ptrvalue;
        if (prp != NULL) {
          if (prp->name != NULL) {
            for (vnp = prp->name; vnp != NULL; vnp = vnp->next) {
              StringNCpy_0 (str, (CharPtr) vnp->data.ptrvalue, sizeof (str));
              if (! StringHasNoText (str)) {
                fprintf (fp, "\t\t\tproduct\t%s\n", str);
              }
            }
          } else if (prp->desc != NULL) {
            StringNCpy_0 (str, prp->desc, sizeof (str));
            if (! StringHasNoText (str)) {
              fprintf (fp, "\t\t\tproduct\t%s\n", str);
            }
          }
          for (vnp = prp->activity; vnp != NULL; vnp = vnp->next) {
            StringNCpy_0 (str, (CharPtr) vnp->data.ptrvalue, sizeof (str));
            if (! StringHasNoText (str)) {
              fprintf (fp, "\t\t\tfunction\t%s\n", str);
            }
          }
          for (vnp = prp->ec; vnp != NULL; vnp = vnp->next) {
            StringNCpy_0 (str, (CharPtr) vnp->data.ptrvalue, sizeof (str));
            if (! StringHasNoText (str)) {
              fprintf (fp, "\t\t\tEC_number\t%s\n", str);
            }
          }
        }
      }
      if (prod != NULL) {
        for (sip = prod->id; sip != NULL; sip = sip->next) {
          if (sip->choice == SEQID_GENBANK ||
              sip->choice == SEQID_EMBL ||
              sip->choice == SEQID_DDBJ) {
            if (SeqIdWrite (sip, str, PRINTID_TEXTID_ACC_VER, sizeof (str)) != NULL) {
              fprintf (fp, "\t\t\tprotein_id\t%s\n", str);
            }
          }
        }
      }
      break;
    case SEQFEAT_RNA :
      rrp = (RnaRefPtr) sfp->data.value.ptrvalue;
      if (rrp != NULL) {
        switch (rrp->ext.choice) {
          case 1 :
            StringNCpy_0 (str, (CharPtr) rrp->ext.value.ptrvalue, sizeof (str));
            if (! StringHasNoText (str)) {
              fprintf (fp, "\t\t\tproduct\t%s\n", str);
            }
            break;
          case 2 :
            trna = rrp->ext.value.ptrvalue;
            if (trna != NULL) {
              FeatDefLabel (sfp, str, sizeof (str) - 1, OM_LABEL_CONTENT);
              if (! StringHasNoText (str)) {
                fprintf (fp, "\t\t\tproduct\t%s\n", str);
              }
            }
            break;
          default :
            break;
        }
      }
      break;
    default :
      break;
  }
  if (! StringHasNoText (sfp->comment)) {
    fprintf (fp, "\t\t\tnote\t%s\n", sfp->comment);
  }
  for (gbq = sfp->qual; gbq != NULL; gbq = gbq->next) {
    if (! StringHasNoText (gbq->qual)) {
      if (! StringHasNoText (gbq->val)) {
        fprintf (fp, "\t\t\t%s\t%s\n", gbq->qual, gbq->val);
      }
    }
  }
  for (vnp = sfp->dbxref; vnp != NULL; vnp = vnp->next) {
    dbt = (DbtagPtr) vnp->data.ptrvalue;
    if (dbt != NULL) {
      if (! StringHasNoText (dbt->db)) {
        oip = dbt->tag;
        if (oip->str != NULL && (! StringHasNoText (oip->str))) {
          fprintf (fp, "\t\t\tdb_xref\t%s:%s\n", dbt->db, oip->str);
        } else {
          fprintf (fp, "\t\t\tdb_xref\t%s:%ld\n", dbt->db, (long) oip->id);
        }
      }
    }
  }

  return TRUE;
}

static void DoBioseq (BioseqPtr bsp, Pointer userdata)

{
  FILE     *fp;
  Char     str [41];
  CharPtr  tmp;
  ValNode  vn;

  /* do not process protein bioseqs here */

  if (! ISA_na (bsp->mol)) return;

  fp = (FILE *) userdata;

  if (bsp->repr == Seq_repr_seg) {

    /* print FASTA ID chain and SeqLoc for segmented bioseq */

    MemSet ((Pointer) &vn, 0, sizeof (ValNode));
    vn.choice = SEQLOC_MIX;
    vn.data.ptrvalue = bsp->seq_ext;
    tmp = SeqLocPrint ((SeqLocPtr) &vn);
    if (tmp != NULL) {
      SeqIdWrite (bsp->id, str, PRINTID_FASTA_LONG, sizeof (str));
      fprintf (fp, ">%s %s\n", str, tmp);
    }
    MemFree (tmp);

  } else {

    /* normal FASTA output, including for raw parts of segmented bioseq */

    BioseqToFasta (bsp, fp, ISA_na (bsp->mol));
  }

  /* features on parts are indexed on segmented parent coordinates */

  if (SeqMgrGetParentOfPart (bsp, NULL) != NULL) return;

  /* visit features indexed on this bioseq */

  SeqMgrExploreFeatures (bsp, userdata, DoFeat, NULL, NULL, NULL);
}

static void DoRecord (SeqEntryPtr sep, Pointer userdata)

{
  /* index features on all bioseqs in current record */

  SeqMgrIndexFeatures (0, sep->data.ptrvalue);

  /* explore record, visit every bioseq */

  VisitBioseqsInSep (sep, userdata, DoBioseq);

  /* record cleaned up by ScanBioseqSetRelease */
}

/* command-line argument list */

#define p_argInputPath  0
#define o_argOutputFile 1
#define x_argFileSelect 2
#define b_argBinaryFile 3
#ifdef OS_UNIX
#define c_argCompressed 4
#endif

Args myargs [] = {
  {"Path to files", NULL, NULL, NULL,
    TRUE, 'p', ARG_STRING, 0.0, 0, NULL},
  {"Output File Name", "stdout", NULL, NULL,
    FALSE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
  {"File selection substring", ".aso", NULL, NULL,
    TRUE, 'x', ARG_STRING, 0.0, 0, NULL},
  {"Binary file", "T", NULL, NULL,
    TRUE, 'b', ARG_BOOLEAN, 0.0, 0, NULL},
#ifdef OS_UNIX
  {"Compressed file", "F", NULL, NULL,
    TRUE, 'c', ARG_BOOLEAN, 0.0, 0, NULL},
#endif
};

/* toolkit ncbimain.c has C main function, wraps application Main */

Int2 Main (void)

{
  Boolean     binary, compressed = FALSE;
  CharPtr     dir, progname, str, subfile;
  FILE        *fp;
  ValNodePtr  head, vnp;
  Char        path [PATH_MAX];

  ErrSetFatalLevel (SEV_FATAL);
  ErrClearOptFlags (EO_SHOW_USERSTR);
  UseLocalAsnloadDataAndErrMsg ();
  ErrPathReset ();

  /* resolve internal pointers in object loader parse tables */

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

  ProgramPath (path, sizeof (path));
  progname = StringRChr (path, DIRDELIMCHR);
  if (progname != NULL) {
    progname++;
  } else {
    progname = "releasescan";
  }

  /* process command-line arguments */

  if (! GetArgs (progname, sizeof (myargs) / sizeof (Args), myargs)) {
    return 0;
  }

  dir = myargs [p_argInputPath].strvalue;
  binary = (Boolean) myargs [b_argBinaryFile].intvalue;
#ifdef OS_UNIX
  compressed = (Boolean) myargs [c_argCompressed].intvalue;
#endif

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

  fp = FileOpen (myargs [o_argOutputFile].strvalue, "a");
  if (fp == NULL) {
    Message (MSG_FATAL, "FileOpen failed");
    return 1;
  }

  head = DirCatalog (dir);

  /* process appropriate files within specified directory */

  for (vnp = head; vnp != NULL; vnp = vnp->next) {

    /* vnp->choice is 0 for file, 1 for subdirectory */

    if (vnp->choice == 0) {
      str = (CharPtr) vnp->data.ptrvalue;
      if (! StringHasNoText (str)) {
        subfile = myargs [x_argFileSelect].strvalue;

        /* does filename have desired substring? */

        if (StringHasNoText (subfile) || StringStr (str, subfile) != NULL) {
#ifdef OS_UNIX
          /* printf ("%s\n", str); */
#endif

          /* open a file, read one record at a time, present it to callback */

          StringNCpy_0 (path, dir, sizeof (path));
          FileBuildPath (path, NULL, str);

          ScanBioseqSetRelease (path, binary, compressed, (Pointer) fp, DoRecord);
        }
      }
    }
  }

  ValNodeFreeData (head);

  FileClose (fp);

  return 0;
}