~ubuntu-branches/ubuntu/precise/ncbi-tools6/precise

« back to all changes in this revision

Viewing changes to demo/insdseqget.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2005-03-27 12:00:15 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050327120015-embhesp32nj73p9r
Tags: 6.1.20041020-3
* Fix FTBFS under GCC 4.0 caused by inconsistent use of "static" on
  functions.  (Closes: #295110.)
* Add a watch file, now that we can.  (Upstream's layout needs version=3.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*   insdseqget.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:  insdseqget.c
 
27
*
 
28
* Author:  Jonathan Kans
 
29
*
 
30
* Version Creation Date:   11/4/02
 
31
*
 
32
* $Revision: 1.1 $
 
33
*
 
34
* File Description:  Demo to fetch by accession, write INSDSet XML
 
35
*
 
36
* Modifications:
 
37
* --------------------------------------------------------------------------
 
38
* ==========================================================================
 
39
*/
 
40
 
 
41
#include <ncbi.h>
 
42
#include <objall.h>
 
43
#include <objsset.h>
 
44
#include <objsub.h>
 
45
#include <objfdef.h>
 
46
#include <objgbseq.h>
 
47
#include <objinsdseq.h>
 
48
#include <seqport.h>
 
49
#include <sequtil.h>
 
50
#include <sqnutils.h>
 
51
#include <subutil.h>
 
52
#include <tofasta.h>
 
53
#include <explore.h>
 
54
#include <ent2api.h>
 
55
#include <pmfapi.h>
 
56
#include <asn2gnbp.h>
 
57
 
 
58
static CharPtr ReadALine (
 
59
  CharPtr str,
 
60
  size_t size,
 
61
  FILE *fp
 
62
)
 
63
 
 
64
{
 
65
  Char     ch;
 
66
  CharPtr  ptr;
 
67
  CharPtr  rsult;
 
68
 
 
69
  if (str == NULL || size < 1 || fp == NULL) return NULL;
 
70
  *str = '\0';
 
71
  rsult = FileGets (str, size, fp);
 
72
  if (rsult != NULL) {
 
73
    ptr = str;
 
74
    ch = *ptr;
 
75
    while (ch != '\0' && ch != '\n' && ch != '\r') {
 
76
      ptr++;
 
77
      ch = *ptr;
 
78
    }
 
79
    *ptr = '\0';
 
80
  }
 
81
  return rsult;
 
82
}
 
83
 
 
84
typedef struct lookforids {
 
85
  Boolean isGED;
 
86
  Boolean isNTorNW;
 
87
  Boolean isNC;
 
88
  Boolean isTPA;
 
89
  Boolean isNuc;
 
90
  Boolean isProt;
 
91
} LookForIDs, PNTR LookForIDsPtr;
 
92
 
 
93
static void LookForSeqIDs (BioseqPtr bsp, Pointer userdata)
 
94
 
 
95
{
 
96
  LookForIDsPtr  lfip;
 
97
  SeqIdPtr       sip;
 
98
  TextSeqIdPtr   tsip;
 
99
 
 
100
  lfip = (LookForIDsPtr) userdata;
 
101
  if (ISA_na (bsp->mol)) {
 
102
    lfip->isNuc = TRUE;
 
103
  }
 
104
  if (ISA_aa (bsp->mol)) {
 
105
    lfip->isProt = TRUE;
 
106
  }
 
107
  for (sip = bsp->id; sip != NULL; sip = sip->next) {
 
108
    switch (sip->choice) {
 
109
      case SEQID_GENBANK :
 
110
      case SEQID_EMBL :
 
111
      case SEQID_DDBJ :
 
112
        lfip->isGED = TRUE;
 
113
        break;
 
114
      case SEQID_TPG :
 
115
      case SEQID_TPE :
 
116
      case SEQID_TPD :
 
117
        lfip->isTPA = TRUE;
 
118
        break;
 
119
      case SEQID_OTHER :
 
120
        tsip = (TextSeqIdPtr) sip->data.ptrvalue;
 
121
        if (tsip != NULL) {
 
122
          if (StringNCmp (tsip->accession, "NC_", 3) == 0) {
 
123
            lfip->isNC = TRUE;
 
124
          } else if (StringNCmp (tsip->accession, "NT_", 3) == 0) {
 
125
            lfip->isNTorNW = TRUE;
 
126
          } else if (StringNCmp (tsip->accession, "NW_", 3) == 0) {
 
127
            lfip->isNTorNW = TRUE;
 
128
          }
 
129
        }
 
130
        break;
 
131
      default :
 
132
        break;
 
133
    }
 
134
  }
 
135
}
 
136
 
 
137
static void LookForGEDetc (
 
138
  SeqEntryPtr topsep,
 
139
  BoolPtr isGED,
 
140
  BoolPtr isNTorNW,
 
141
  BoolPtr isNC,
 
142
  BoolPtr isTPA,
 
143
  BoolPtr isNuc,
 
144
  BoolPtr isProt
 
145
)
 
146
 
 
147
{
 
148
  LookForIDs  lfi;
 
149
 
 
150
  MemSet ((Pointer) &lfi, 0, sizeof (LookForIDs));
 
151
  VisitBioseqsInSep (topsep, (Pointer) &lfi, LookForSeqIDs);
 
152
  *isGED = lfi.isGED;
 
153
  *isNTorNW = lfi.isNTorNW;
 
154
  *isNC = lfi.isNC;
 
155
  *isTPA = lfi.isTPA;
 
156
  *isNuc = lfi.isNuc;
 
157
  *isProt = lfi.isProt;
 
158
}
 
159
 
 
160
static void DoSeqEntryToGnbk (
 
161
  SeqEntryPtr sep,
 
162
  FmtType fmt,
 
163
  XtraPtr extra
 
164
)
 
165
 
 
166
{
 
167
  CstType  cust = SHOW_TRANCRIPTION | SHOW_PEPTIDE;
 
168
  FlgType  flags = SHOW_FAR_TRANSLATION | SHOW_CONTIG_AND_SEQ;
 
169
  Boolean  isGED;
 
170
  Boolean  isNTorNW;
 
171
  Boolean  isNC;
 
172
  Boolean  isNuc;
 
173
  Boolean  isProt;
 
174
  Boolean  isTPA;
 
175
  LckType  locks = LOOKUP_FAR_COMPONENTS | LOOKUP_FAR_LOCATIONS | LOOKUP_FAR_PRODUCTS;
 
176
 
 
177
  LookForGEDetc (sep, &isGED, &isNTorNW, &isNC, &isTPA, &isNuc, &isProt);
 
178
 
 
179
  if (fmt == GENBANK_FMT && (! isNuc)) return;
 
180
  if (fmt == GENPEPT_FMT && (! isProt)) return;
 
181
 
 
182
  if (isNTorNW || isTPA) {
 
183
    flags |= ONLY_NEAR_FEATURES;
 
184
  } else if (isNC) {
 
185
    flags |= NEAR_FEATURES_SUPPRESS;
 
186
  }
 
187
 
 
188
  SeqEntryToGnbk (sep, NULL, fmt, ENTREZ_MODE, SEGMENT_STYLE,
 
189
                  flags, locks, cust, extra, NULL);
 
190
}
 
191
 
 
192
static void DoQuery (
 
193
  FILE *fp,
 
194
  FILE *dfp,
 
195
  XtraPtr extra,
 
196
  Boolean get_var,
 
197
  Boolean do_nuc,
 
198
  Boolean do_prot
 
199
)
 
200
 
 
201
{
 
202
  Entrez2BooleanReplyPtr  e2br;
 
203
  Entrez2IdListPtr        e2lp;
 
204
  Entrez2RequestPtr       e2rq;
 
205
  Entrez2ReplyPtr         e2ry;
 
206
  Int4                    flags = 0;
 
207
  Int4                    i;
 
208
  Char                    line [256];
 
209
  E2ReplyPtr              reply;
 
210
  SeqEntryPtr             sep;
 
211
  CharPtr                 str;
 
212
  Uint4                   uid;
 
213
 
 
214
  if (get_var) {
 
215
    flags = 1;
 
216
  }
 
217
 
 
218
  e2rq = EntrezCreateBooleanRequest (TRUE, FALSE, "Nucleotide", NULL, 0, 0, NULL, 0, 0);
 
219
  if (e2rq == NULL) return;
 
220
 
 
221
  EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_LEFT_PAREN, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
222
  EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_LEFT_PAREN, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
223
 
 
224
  str = ReadALine (line, sizeof (line), fp);
 
225
  if (! StringHasNoText (str)) {
 
226
    EntrezAddToBooleanRequest (e2rq, NULL, 0, "ACCN", str, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
227
  }
 
228
 
 
229
  while (str != NULL) {
 
230
    if (! StringHasNoText (str)) {
 
231
      EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_OR, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
232
      EntrezAddToBooleanRequest (e2rq, NULL, 0, "ACCN", str, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
233
    }
 
234
    str = ReadALine (line, sizeof (line), fp);
 
235
  }
 
236
 
 
237
  EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_RIGHT_PAREN, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
238
  EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_AND, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
239
  EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_LEFT_PAREN, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
240
 
 
241
  str = ReadALine (line, sizeof (line), dfp);
 
242
  if (! StringHasNoText (str)) {
 
243
    EntrezAddToBooleanRequest (e2rq, NULL, 0, "MDAT", str, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
244
  }
 
245
 
 
246
  while (str != NULL) {
 
247
    if (! StringHasNoText (str)) {
 
248
      EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_OR, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
249
      EntrezAddToBooleanRequest (e2rq, NULL, 0, "MDAT", str, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
250
    }
 
251
    str = ReadALine (line, sizeof (line), dfp);
 
252
  }
 
253
 
 
254
  EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_RIGHT_PAREN, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
255
  EntrezAddToBooleanRequest (e2rq, NULL, ENTREZ_OP_RIGHT_PAREN, NULL, NULL, NULL, 0, 0, NULL, NULL, TRUE, TRUE);
 
256
 
 
257
  e2ry = EntrezSynchronousQuery (e2rq);
 
258
  e2rq = Entrez2RequestFree (e2rq);
 
259
 
 
260
  if (e2ry == NULL) return;
 
261
  reply = e2ry->reply;
 
262
  if (reply == NULL || reply->choice != E2Reply_eval_boolean) return;
 
263
  e2br = EntrezExtractBooleanReply (e2ry);
 
264
  if (e2br == NULL) return;
 
265
 
 
266
  e2lp = e2br->uids;
 
267
  if (e2lp != NULL) {
 
268
    BSSeek (e2lp->uids, 0, SEEK_SET);
 
269
    for (i = 0; i < e2lp->num; i++) {
 
270
      uid = Nlm_BSGetUint4 (e2lp->uids);
 
271
      if (uid < 1) continue;
 
272
 
 
273
      sep = PubSeqSynchronousQuery (uid, 0, flags);
 
274
      if (sep == NULL) continue;
 
275
 
 
276
      if (do_nuc) {
 
277
        DoSeqEntryToGnbk (sep, GENBANK_FMT, extra);
 
278
      }
 
279
      if (do_prot) {
 
280
        DoSeqEntryToGnbk (sep, GENPEPT_FMT, extra);
 
281
      }
 
282
 
 
283
      SeqEntryFree (sep);
 
284
    }
 
285
  }
 
286
 
 
287
  Entrez2BooleanReplyFree (e2br);
 
288
}
 
289
 
 
290
static void ProcessAccession (
 
291
  CharPtr accn,
 
292
  XtraPtr extra,
 
293
  Boolean only_new,
 
294
  Boolean get_var,
 
295
  Boolean do_nuc,
 
296
  Boolean do_prot
 
297
)
 
298
 
 
299
{
 
300
  Char         ch;
 
301
  Int4         flags = 0;
 
302
  Int4         gi = 0;
 
303
  Char         id [41];
 
304
  Boolean      is_numeric = TRUE;
 
305
  Int4         newgi = 0;
 
306
  CharPtr      ptr;
 
307
  SeqEntryPtr  sep;
 
308
  SeqIdPtr     sip;
 
309
  Char         tmp [41];
 
310
  long         val;
 
311
 
 
312
  ptr = accn;
 
313
  ch = *ptr;
 
314
  while (ch != '\0' && is_numeric) {
 
315
    if (! IS_DIGIT (ch)) {
 
316
      is_numeric = FALSE;
 
317
    }
 
318
    ptr++;
 
319
    ch = *ptr;
 
320
  }
 
321
 
 
322
  if (is_numeric) {
 
323
    if (sscanf (accn, "%ld", &val) == 1) {
 
324
      gi = (Int4) val;
 
325
      if (gi < 1) return;
 
326
      if (only_new) {
 
327
        sip = GetSeqIdForGI (gi);
 
328
        if (sip != NULL) {
 
329
          SeqIdWrite (sip, tmp, PRINTID_TEXTID_ACC_VER, sizeof (tmp));
 
330
          SeqIdFree (sip);
 
331
          ptr = StringChr (tmp, '.');
 
332
          if (ptr != NULL) {
 
333
            *ptr = '\0';
 
334
            sip = SeqIdFromAccessionDotVersion (tmp);
 
335
            newgi = GetGIForSeqId (sip);
 
336
            SeqIdFree (sip);
 
337
            if (newgi == gi) return;
 
338
          }
 
339
        }
 
340
      }
 
341
    }
 
342
  } else {
 
343
    sip = SeqIdFromAccessionDotVersion (accn);
 
344
    gi = GetGIForSeqId (sip);
 
345
    SeqIdFree (sip);
 
346
    if (only_new) {
 
347
      sip = GetSeqIdForGI (gi);
 
348
      if (sip != NULL) {
 
349
        SeqIdWrite (sip, id, PRINTID_TEXTID_ACC_VER, sizeof (id));
 
350
        SeqIdFree (sip);
 
351
        if (StringICmp (accn, id) == 0) return;
 
352
      }
 
353
    }
 
354
  }
 
355
  if (gi < 1) return;
 
356
 
 
357
  if (get_var) {
 
358
    flags = 1;
 
359
  }
 
360
  sep = PubSeqSynchronousQuery (gi, 0, flags);
 
361
  if (sep == NULL) return;
 
362
 
 
363
  if (do_nuc) {
 
364
    DoSeqEntryToGnbk (sep, GENBANK_FMT, extra);
 
365
  }
 
366
  if (do_prot) {
 
367
    DoSeqEntryToGnbk (sep, GENPEPT_FMT, extra);
 
368
  }
 
369
 
 
370
  SeqEntryFree (sep);
 
371
}
 
372
 
 
373
#define i_argInputFile  0
 
374
#define d_argDateFile   1
 
375
#define o_argOutputFile 2
 
376
#define n_argNewRecords 3
 
377
#define v_argVariations 4
 
378
#define m_argMolecule   5
 
379
 
 
380
Args myargs [] = {
 
381
  {"Sequence File Name", "stdin", NULL, NULL,
 
382
    FALSE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
 
383
  {"Date List", NULL, NULL, NULL,
 
384
    TRUE, 'd', ARG_FILE_IN, 0.0, 0, NULL},
 
385
  {"Output File Name", "stdout", NULL, NULL,
 
386
    FALSE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
 
387
  {"New Records Only", "F", NULL, NULL,
 
388
    TRUE, 'n', ARG_BOOLEAN, 0.0, 0, NULL},
 
389
  {"Fetch SNP Variations", "F", NULL, NULL,
 
390
    TRUE, 'v', ARG_BOOLEAN, 0.0, 0, NULL},
 
391
  {"Molecule (n Nucleotide, p Protein, b Both)", "n", NULL, NULL,
 
392
    FALSE, 'm', ARG_STRING, 0.0, 0, NULL},
 
393
};
 
394
 
 
395
NLM_EXTERN void AsnPrintNewLine PROTO((AsnIoPtr aip));
 
396
 
 
397
Int2 Main (void)
 
398
 
 
399
{
 
400
  AsnIoPtr    aip;
 
401
  AsnTypePtr  atp;
 
402
  FILE        *dfp = NULL;
 
403
  Boolean     do_nuc = FALSE;
 
404
  Boolean     do_prot = FALSE;
 
405
  XtraPtr     extra;
 
406
  FILE        *fp;
 
407
  GBSeq       gbsq;
 
408
  GBSet       gbst;
 
409
  Boolean     get_var;
 
410
  Char        line [256];
 
411
  Boolean     only_new;
 
412
  CharPtr     str;
 
413
  Char        xmlbuf [128];
 
414
  XtraBlock   xtra;
 
415
 
 
416
  ErrSetFatalLevel (SEV_MAX);
 
417
  ErrClearOptFlags (EO_SHOW_USERSTR);
 
418
  UseLocalAsnloadDataAndErrMsg ();
 
419
  ErrPathReset ();
 
420
 
 
421
  if (! AllObjLoad ()) {
 
422
    Message (MSG_FATAL, "AllObjLoad failed");
 
423
    return 1;
 
424
  }
 
425
  if (! SubmitAsnLoad ()) {
 
426
    Message (MSG_FATAL, "SubmitAsnLoad failed");
 
427
    return 1;
 
428
  }
 
429
  if (! SeqCodeSetLoad ()) {
 
430
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
 
431
    return 1;
 
432
  }
 
433
  if (! GeneticCodeTableLoad ()) {
 
434
    Message (MSG_FATAL, "GeneticCodeTableLoad failed");
 
435
    return 1;
 
436
  }
 
437
  if (! objgbseqAsnLoad ()) {
 
438
    Message (MSG_POSTERR, "objgbseqAsnLoad failed");
 
439
    return 1;
 
440
  }
 
441
 
 
442
  if (! GetArgs ("insdseqget", sizeof (myargs) / sizeof (Args), myargs)) {
 
443
    return 0;
 
444
  }
 
445
 
 
446
  fp = FileOpen (myargs [i_argInputFile].strvalue, "r");
 
447
  if (fp == NULL) {
 
448
    return 1;
 
449
  }
 
450
 
 
451
  if (! StringHasNoText (myargs [d_argDateFile].strvalue)) {
 
452
    dfp = FileOpen (myargs [d_argDateFile].strvalue, "r");
 
453
    if (dfp == NULL) {
 
454
      return 1;
 
455
    }
 
456
  }
 
457
 
 
458
  if (GetAppParam ("NCBI", "SETTINGS", "XMLPREFIX", NULL, xmlbuf, sizeof (xmlbuf))) {
 
459
    AsnSetXMLmodulePrefix (StringSave (xmlbuf));
 
460
  }
 
461
 
 
462
  MemSet ((Pointer) &xtra, 0, sizeof (XtraBlock));
 
463
  MemSet ((Pointer) &gbsq, 0, sizeof (GBSeq));
 
464
  xtra.gbseq = &gbsq;
 
465
  aip = AsnIoOpen (myargs [o_argOutputFile].strvalue, "wx");
 
466
 
 
467
  if (aip == NULL) {
 
468
    Message (MSG_POSTERR, "AsnIoOpen failed");
 
469
    FileClose (fp);
 
470
    return 1;
 
471
  }
 
472
 
 
473
  only_new = (Boolean) myargs [n_argNewRecords].intvalue;
 
474
  get_var = (Boolean) myargs [v_argVariations].intvalue;
 
475
 
 
476
  str = myargs [m_argMolecule].strvalue;
 
477
  if (StringICmp (str, "n") == 0) {
 
478
    do_nuc = TRUE;
 
479
  } else if (StringICmp (str, "p") == 0) {
 
480
    do_prot = TRUE;
 
481
  } else if (StringICmp (str, "b") == 0) {
 
482
    do_nuc = TRUE;
 
483
    do_prot = TRUE;
 
484
  } else {
 
485
    do_nuc = TRUE;
 
486
  }
 
487
 
 
488
  PubSeqFetchEnable ();
 
489
 
 
490
  xtra.aip = aip;
 
491
  atp = AsnLinkType (NULL, AsnFind ("INSDSet"));
 
492
  xtra.atp = AsnLinkType (NULL, AsnFind ("INSDSet.E"));
 
493
  if (atp == NULL || xtra.atp == NULL) {
 
494
    Message (MSG_POSTERR, "AsnLinkType or AsnFind failed");
 
495
    return 1;
 
496
  }
 
497
  extra = &xtra;
 
498
  MemSet ((Pointer) &gbst, 0, sizeof (GBSet));
 
499
  AsnOpenStruct (aip, atp, (Pointer) &gbst);
 
500
 
 
501
  if (dfp != NULL) {
 
502
    DoQuery (fp, dfp, extra, get_var, do_nuc, do_prot);
 
503
  } else {
 
504
    str = ReadALine (line, sizeof (line), fp);
 
505
    while (str != NULL) {
 
506
      if (! StringHasNoText (str)) {
 
507
        ProcessAccession (str, extra, only_new, get_var, do_nuc, do_prot);
 
508
      }
 
509
      str = ReadALine (line, sizeof (line), fp);
 
510
    }
 
511
  }
 
512
 
 
513
  AsnCloseStruct (aip, atp, NULL);
 
514
  AsnPrintNewLine (aip);
 
515
  AsnIoClose (aip);
 
516
 
 
517
  FileClose (dfp);
 
518
  FileClose (fp);
 
519
 
 
520
  PubSeqFetchDisable ();
 
521
 
 
522
  return 0;
 
523
}
 
524