~ubuntu-branches/ubuntu/vivid/ncbi-tools6/vivid-proposed

« back to all changes in this revision

Viewing changes to demo/src_chk.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2009-03-19 10:17:26 UTC
  • mfrom: (1.3.1 upstream) (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090319101726-wjuj7ajnq0w5a0mg
Tags: 6.1.20090301-1
* New upstream release; uploading to unstable now that lenny is out.
* debian/lib{ncbi6,vibrant6a}.symbols: update accordingly.
* doc/man/*.1: update accordingly as well.
* debian/control: place lib*-dbg in the new debug section, per the
  current override file.
* debian/control: declare compliance with Policy 3.8.1 (no changes needed).
* api/aliread.c: merge in fix (from 6.1.20080302-4) to undefined use of
  sprintf caught by Kees Cook's scan.
* debian/watch: belatedly update regex to recognize releases like the
  previous one ([6.1.]20081116a).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*   src_chk.c
 
2
* ===========================================================================
 
3
*
 
4
*                            PUBLIC DOMAIN NOTICE
 
5
*            National Center for Biotechnology Information (NCBI)
 
6
*
 
7
*  This software/database is a "United States Government Work" under the
 
8
*  terms of the United States Copyright Act.  It was written as part of
 
9
*  the author's official duties as a United States Government employee and
 
10
*  thus cannot be copyrighted.  This software/database is freely available
 
11
*  to the public for use. The National Library of Medicine and the U.S.
 
12
*  Government do not place any restriction on its use or reproduction.
 
13
*  We would, however, appreciate having the NCBI and the author cited in
 
14
*  any work or product based on this material
 
15
*
 
16
*  Although all reasonable efforts have been taken to ensure the accuracy
 
17
*  and reliability of the software and data, the NLM and the U.S.
 
18
*  Government do not and cannot warrant the performance or results that
 
19
*  may be obtained by using this software or data. The NLM and the U.S.
 
20
*  Government disclaim all warranties, express or implied, including
 
21
*  warranties of performance, merchantability or fitness for any particular
 
22
*  purpose.
 
23
*
 
24
* ===========================================================================
 
25
*
 
26
* File Name:  src_chk.c
 
27
*
 
28
* Author:  Colleen Bollin
 
29
*
 
30
* Version Creation Date:   4/12/07
 
31
*
 
32
* $Revision: 1.10 $
 
33
*
 
34
* File Description: 
 
35
*
 
36
* Modifications:  
 
37
* --------------------------------------------------------------------------
 
38
* Date     Name        Description of modification
 
39
* -------  ----------  -----------------------------------------------------
 
40
*
 
41
*
 
42
* ==========================================================================
 
43
*/
 
44
 
 
45
#include <ncbi.h>
 
46
#include <objall.h>
 
47
#include <objsset.h>
 
48
#include <objsub.h>
 
49
#include <objfdef.h>
 
50
#include <sequtil.h>
 
51
#include <gather.h>
 
52
#include <sqnutils.h>
 
53
#include <explore.h>
 
54
#include <pmfapi.h>
 
55
#define NLM_GENERATED_CODE_PROTO
 
56
#include <asnmacro.h>
 
57
#include <objmacro.h>
 
58
#include <macroapi.h>
 
59
 
 
60
#define SRC_CHK_APP_VER "1.0"
 
61
 
 
62
CharPtr SRC_CHK_APPLICATION = SRC_CHK_APP_VER;
 
63
 
 
64
 
 
65
static ValNodePtr CollectFieldList(BioseqPtr bsp)
 
66
{
 
67
  BioSourcePtr biop;
 
68
  SeqDescrPtr sdp;
 
69
  SeqMgrDescContext dcontext;
 
70
  ValNodePtr list = NULL, vnp;
 
71
 
 
72
  for (sdp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_source, &dcontext);
 
73
       sdp != NULL;
 
74
       sdp = SeqMgrGetNextDescriptor (bsp, sdp, Seq_descr_source, &dcontext)) {
 
75
    biop = (BioSourcePtr) sdp->data.ptrvalue;
 
76
    vnp = GetSourceQualFieldListFromBioSource (biop);
 
77
    ValNodeLink (&list, vnp);
 
78
  }
 
79
  return list;
 
80
}
 
81
 
 
82
 
 
83
static void PrintHeader (FILE *fp, ValNodePtr field_list)
 
84
{
 
85
  CharPtr txt;
 
86
 
 
87
  if (fp == NULL || field_list == NULL) {
 
88
    return;
 
89
  }
 
90
  /* first field accession, second field GI, third field tax ID */
 
91
  fprintf (fp, "\t\tTaxID");
 
92
  while (field_list != NULL) {
 
93
    txt = SummarizeFieldType (field_list);
 
94
    fprintf (fp, "\t%s", txt);
 
95
    txt = MemFree (txt);
 
96
    field_list = field_list->next;
 
97
  }
 
98
  fprintf (fp, "\n");
 
99
}
 
100
 
 
101
 
 
102
static Int4 GetTaxIdFromOrgRef (OrgRefPtr orp)
 
103
{
 
104
  Int4       tax_id = -1;
 
105
  ValNodePtr vnp;
 
106
  DbtagPtr   d;
 
107
 
 
108
  if (orp != NULL)
 
109
  {
 
110
    for (vnp = orp->db; vnp != NULL; vnp = vnp->next) 
 
111
    {
 
112
      d = (DbtagPtr) vnp->data.ptrvalue;
 
113
      if (StringCmp(d->db, "taxon") == 0) 
 
114
      {
 
115
        tax_id = d->tag->id;
 
116
        break;
 
117
      }
 
118
    }
 
119
  }
 
120
  return tax_id;
 
121
}
 
122
 
 
123
 
 
124
static void PrintBioSourceLine (FILE *fp, BioSourcePtr biop, ValNodePtr field_list)
 
125
{
 
126
  CharPtr txt;
 
127
 
 
128
  if (fp == NULL || biop == NULL || field_list == NULL) {
 
129
    return;
 
130
  }
 
131
 
 
132
  fprintf (fp, "\t%d", GetTaxIdFromOrgRef(biop->org));
 
133
 
 
134
  while (field_list != NULL) {
 
135
    txt = GetSourceQualFromBioSource (biop, field_list->data.ptrvalue, NULL);
 
136
    fprintf (fp, "\t%s", txt == NULL ? "" : txt);
 
137
    txt = MemFree (txt);
 
138
    field_list = field_list->next;
 
139
  }
 
140
}
 
141
 
 
142
 
 
143
static void PrintBioseqLines (FILE *fp, BioseqPtr bsp, ValNodePtr field_list)
 
144
{
 
145
  SeqDescrPtr       sdp;
 
146
  SeqMgrDescContext dcontext;
 
147
  Char              id_txt[255], id_txt2[255];
 
148
  SeqIdPtr          sip, sip_gi = NULL, sip_gb = NULL;
 
149
 
 
150
  if (fp == NULL || bsp == NULL || field_list == NULL) {
 
151
    return;
 
152
  }
 
153
 
 
154
  for (sip = bsp->id; sip != NULL; sip = sip->next) {
 
155
    if (sip->choice == SEQID_GENBANK
 
156
        || (sip->choice == SEQID_EMBL && sip_gb == NULL)
 
157
        || (sip->choice == SEQID_SWISSPROT && sip_gb == NULL)
 
158
        || (sip->choice == SEQID_DDBJ && sip_gb == NULL)
 
159
        || (sip->choice == SEQID_PIR && sip_gb == NULL)) {
 
160
      sip_gb = sip;
 
161
    } else if (sip->choice == SEQID_GI) {
 
162
      sip_gi = sip;
 
163
    }
 
164
  }
 
165
 
 
166
  if (sip_gb == NULL && sip_gi == NULL) {
 
167
    SeqIdWrite (SeqIdFindBest (bsp->id, SEQID_GENBANK), id_txt, PRINTID_REPORT, sizeof (id_txt) - 1);
 
168
    id_txt2[0] = 0;
 
169
  } else {
 
170
    if (sip_gb == NULL) {
 
171
      id_txt[0] = 0;
 
172
    } else {
 
173
      SeqIdWrite (sip_gb, id_txt, PRINTID_REPORT, sizeof (id_txt) - 1);
 
174
    }
 
175
    if (sip_gi == NULL) {
 
176
      id_txt2[0] = 0;
 
177
    } else {
 
178
      SeqIdWrite (sip_gi, id_txt2, PRINTID_REPORT, sizeof (id_txt2) - 1);
 
179
    }
 
180
  }
 
181
 
 
182
  for (sdp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_source, &dcontext);
 
183
       sdp != NULL;
 
184
       sdp = SeqMgrGetNextDescriptor (bsp, sdp, Seq_descr_source, &dcontext)) {
 
185
    fprintf (fp, "%s\t%s", id_txt, id_txt2);
 
186
    PrintBioSourceLine (fp, sdp->data.ptrvalue, field_list);
 
187
    fprintf (fp, "\n");
 
188
  }
 
189
}
 
190
 
 
191
 
 
192
static void PrintBioseqErrorLine (FILE *fp, SeqIdPtr sip)
 
193
{
 
194
  Char              id_txt[255];
 
195
 
 
196
  if (fp == NULL || sip == NULL) {
 
197
    return;
 
198
  }
 
199
 
 
200
  SeqIdWrite (sip, id_txt, PRINTID_REPORT, sizeof (id_txt) - 1);
 
201
 
 
202
  if (sip->choice == SEQID_GI) {
 
203
    fprintf (fp, "\t%s\n", id_txt);
 
204
  } else {
 
205
    fprintf (fp, "%s\t\n", id_txt);
 
206
  }
 
207
}
 
208
 
 
209
 
 
210
static Boolean IsAllDigits (CharPtr str)
 
211
{
 
212
  CharPtr cp;
 
213
 
 
214
  if (StringHasNoText (str)) return FALSE;
 
215
 
 
216
  cp = str;
 
217
  while (*cp != 0 && isdigit (*cp)) {
 
218
    cp++;
 
219
  }
 
220
  if (*cp == 0) {
 
221
    return TRUE;
 
222
  } else {
 
223
    return FALSE;
 
224
  }
 
225
}
 
226
 
 
227
 
 
228
static SeqIdPtr SmartGuessMakeId (CharPtr str)
 
229
{
 
230
  CharPtr id_txt;
 
231
  SeqIdPtr sip = NULL;
 
232
 
 
233
  if (StringHasNoText (str)) {
 
234
    return NULL;
 
235
  } else if (StringChr (str, '|') != NULL) {
 
236
    sip = MakeSeqID (str);
 
237
  } else if (IsAllDigits (str)) {
 
238
    id_txt = (CharPtr) MemNew (sizeof (Char) * (StringLen (str) + 4));
 
239
    sprintf (id_txt, "gi|%s", str);
 
240
    sip = MakeSeqID (id_txt);
 
241
    id_txt = MemFree (id_txt);
 
242
  } else {
 
243
    id_txt = (CharPtr) MemNew (sizeof (Char) * (StringLen (str) + 4));
 
244
    sprintf (id_txt, "gb|%s", str);
 
245
    sip = MakeSeqID (id_txt);
 
246
    id_txt = MemFree (id_txt);
 
247
  }
 
248
  return sip;
 
249
}
 
250
 
 
251
 
 
252
/* Args structure contains command-line arguments */
 
253
 
 
254
#define i_argInputFile         0
 
255
#define o_argOutputFile        1
 
256
 
 
257
Args myargs [] = {
 
258
  {"Input File", NULL, NULL, NULL,
 
259
    TRUE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
 
260
  {"Output File", NULL, NULL, NULL,
 
261
    TRUE, 'o', ARG_FILE_OUT, 0.0, 0, NULL}
 
262
};
 
263
 
 
264
 
 
265
static void SortFieldListForSrcChk (ValNodePtr PNTR field_list)
 
266
{
 
267
  ValNodePtr vnp, vnp_s, vnp_prev = NULL;
 
268
 
 
269
  if (field_list == NULL || *field_list == NULL) return;
 
270
 
 
271
  SortUniqueFieldTypeList (field_list);
 
272
 
 
273
  /* move taxname to front of list */
 
274
  for (vnp = *field_list; vnp != NULL; vnp_prev = vnp, vnp = vnp->next) {
 
275
    if (vnp->choice == FieldType_source_qual) {
 
276
      vnp_s = vnp->data.ptrvalue;
 
277
      if (vnp_s != NULL
 
278
          && vnp_s->choice == SourceQualChoice_textqual
 
279
          && vnp_s->data.intvalue == Source_qual_taxname) {
 
280
        /* only need to move if not already at front of list */
 
281
        if (vnp_prev != NULL) {
 
282
          vnp_prev->next = vnp->next;
 
283
          vnp->next = *field_list;
 
284
          *field_list = vnp;
 
285
        }
 
286
        break;
 
287
      }
 
288
    }
 
289
  }       
 
290
 
 
291
 
 
292
}
 
293
 
 
294
 
 
295
Int2 Main(void)
 
296
{
 
297
  Char             app [64];
 
298
  Int4             rval = 0;
 
299
  CharPtr          id_file, line;
 
300
  ReadBufferData   rbd;
 
301
  ValNodePtr       field_list = NULL;
 
302
  SeqIdPtr         sip;
 
303
  ValNodePtr       bsp_list = NULL, vnp;
 
304
  BioseqPtr        bsp;
 
305
  FILE *fp;
 
306
 
 
307
 
 
308
  /* standard setup */
 
309
 
 
310
  ErrSetFatalLevel (SEV_MAX);
 
311
  ErrClearOptFlags (EO_SHOW_USERSTR);
 
312
  UseLocalAsnloadDataAndErrMsg ();
 
313
  ErrPathReset ();
 
314
 
 
315
  /* finish resolving internal connections in ASN.1 parse tables */
 
316
 
 
317
  if (! AllObjLoad ()) {
 
318
    Message (MSG_FATAL, "AllObjLoad failed");
 
319
    return 1;
 
320
  }
 
321
  if (! SubmitAsnLoad ()) {
 
322
    Message (MSG_FATAL, "SubmitAsnLoad failed");
 
323
    return 1;
 
324
  }
 
325
  if (! FeatDefSetLoad ()) {
 
326
    Message (MSG_FATAL, "FeatDefSetLoad failed");
 
327
    return 1;
 
328
  }
 
329
  if (! SeqCodeSetLoad ()) {
 
330
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
 
331
    return 1;
 
332
  }
 
333
  if (! GeneticCodeTableLoad ()) {
 
334
    Message (MSG_FATAL, "GeneticCodeTableLoad failed");
 
335
    return 1;
 
336
  }
 
337
 
 
338
  PubSeqFetchEnable ();
 
339
 
 
340
  /* process command line arguments */
 
341
 
 
342
  sprintf (app, "src_chk %s", SRC_CHK_APPLICATION);
 
343
  if (! GetArgs (app, sizeof (myargs) / sizeof (Args), myargs)) {
 
344
    return 0;
 
345
  }
 
346
 
 
347
  id_file = (CharPtr) myargs [i_argInputFile].strvalue;
 
348
 
 
349
  rbd.fp = FileOpen (id_file, "r");
 
350
  if (rbd.fp == NULL) {
 
351
    Message (MSG_ERROR, "Unable to open %s", (CharPtr) myargs [i_argInputFile].strvalue);
 
352
    return 1;
 
353
  }
 
354
  rbd.current_data = NULL;
 
355
  line = AbstractReadFunction (&rbd);  
 
356
  while (line != NULL && line[0] != EOF) {
 
357
    if (!StringHasNoText (line)) {
 
358
 
 
359
      sip = SmartGuessMakeId (line);
 
360
      bsp = BioseqLockById (sip);
 
361
      if (bsp == NULL) {
 
362
        printf ("Unable to download Bioseq for %s\n", line);
 
363
      } else {
 
364
        ValNodeLink (&field_list, CollectFieldList (bsp));
 
365
        BioseqUnlock (bsp);
 
366
      }
 
367
      ValNodeAddPointer (&bsp_list, 0, sip);
 
368
    }
 
369
    line = MemFree (line);
 
370
    line = AbstractReadFunction (&rbd);
 
371
  }
 
372
 
 
373
  FileClose (rbd.fp);
 
374
 
 
375
  SortFieldListForSrcChk (&field_list);
 
376
 
 
377
  fp = FileOpen ((CharPtr) myargs [o_argOutputFile].strvalue, "w");
 
378
  if (fp == NULL) {
 
379
    Message (MSG_ERROR, "Unable to open %s", (CharPtr) myargs [o_argOutputFile].strvalue);
 
380
    rval = 1;
 
381
  } else {
 
382
    PrintHeader (fp, field_list);
 
383
    for (vnp = bsp_list; vnp != NULL; vnp = vnp->next) {
 
384
      bsp = BioseqLockById (vnp->data.ptrvalue);
 
385
      if (bsp == NULL) {
 
386
        PrintBioseqErrorLine (fp, vnp->data.ptrvalue);
 
387
      } else {
 
388
        PrintBioseqLines (fp, bsp, field_list);
 
389
      }
 
390
      BioseqUnlock (bsp);
 
391
      vnp->data.ptrvalue = SeqIdFree (vnp->data.ptrvalue);
 
392
    }
 
393
  }
 
394
  FileClose (fp);
 
395
  bsp_list = ValNodeFree (bsp_list);
 
396
  field_list = FieldTypeListFree (field_list);
 
397
  return rval;
 
398
}