~ubuntu-branches/ubuntu/oneiric/ncbi-tools6/oneiric

« back to all changes in this revision

Viewing changes to demo/cspeedtest.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2008-07-14 19:43:15 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080714194315-ed44u9ek7txva2rz
Tags: 6.1.20080302-3
tools/readdb.c: enable madvise()-based code on all glibc (hence all
Debian) systems, not just Linux.  (Closes: #490437.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*   cspeedtest.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:  cspeedtest.c
 
27
*
 
28
* Author:  Jonathan Kans
 
29
*
 
30
* Version Creation Date:   12/17/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 <objfdef.h>
 
49
#include <objsub.h>
 
50
#include <sequtil.h>
 
51
#include <sqnutils.h>
 
52
#include <explore.h>
 
53
#include <toasn3.h>
 
54
#include <pmfapi.h>
 
55
#include <tofasta.h>
 
56
#include <asn2gnbk.h>
 
57
#include <valid.h>
 
58
 
 
59
#define CSPEEDTEST_APP_VER "1.3"
 
60
 
 
61
CharPtr CSPEEDTEST_APPLICATION = CSPEEDTEST_APP_VER;
 
62
 
 
63
typedef struct cspeedflags {
 
64
  Boolean       batch;
 
65
  Boolean       binary;
 
66
  Boolean       compressed;
 
67
  Boolean       lock;
 
68
  Int2          type;
 
69
  Int4          maxcount;
 
70
  CharPtr       io;
 
71
  CharPtr       clean;
 
72
  CharPtr       index;
 
73
  CharPtr       seq;
 
74
  CharPtr       feat;
 
75
  CharPtr       desc;
 
76
  CharPtr       verify;
 
77
  AsnModulePtr  amp;
 
78
  AsnTypePtr    atp_bss;
 
79
  AsnTypePtr    atp_bsss;
 
80
  AsnTypePtr    atp_se;
 
81
  AsnTypePtr    atp_bsc;
 
82
  AsnTypePtr    bssp_atp;
 
83
  BioseqSet     bss;
 
84
  FILE          *ofp;
 
85
  FILE          *logfp;
 
86
} CSpeedFlagData, PNTR CSpeedFlagPtr;
 
87
 
 
88
static void DoVisitFeaturesTest (
 
89
  SeqFeatPtr sfp,
 
90
  Pointer userdata
 
91
)
 
92
 
 
93
{
 
94
  /* empty visit callback */
 
95
}
 
96
 
 
97
static void DoGeneOverlapPrintTest (
 
98
  SeqFeatPtr sfp,
 
99
  Pointer userdata
 
100
)
 
101
 
 
102
{
 
103
  CSpeedFlagPtr      cfp;
 
104
  SeqMgrFeatContext  gcontext;
 
105
  SeqFeatPtr         gene;
 
106
  CharPtr            str1, str2;
 
107
 
 
108
  if (sfp == NULL) return;
 
109
  cfp = (CSpeedFlagPtr) userdata;
 
110
  if (cfp == NULL || cfp->ofp == NULL) return;
 
111
 
 
112
  if (sfp->data.choice == SEQFEAT_GENE) return;
 
113
  gene = SeqMgrGetOverlappingGene (sfp->location, &gcontext);
 
114
  if (gene == NULL) return;
 
115
 
 
116
  str1 = SeqLocPrint (sfp->location);
 
117
  str2 = SeqLocPrint (gene->location);
 
118
  if (str1 != NULL && str2 != NULL) {
 
119
    fprintf (cfp->ofp, "[%s] -> [%s]\n", str1, str2);
 
120
  } else {
 
121
    fprintf (cfp->ofp, "? -> ?\n");
 
122
  }
 
123
  MemFree (str1);
 
124
  MemFree (str2);
 
125
}
 
126
 
 
127
static void DoGeneOverlapSpeedTest (
 
128
  SeqFeatPtr sfp,
 
129
  Pointer userdata
 
130
)
 
131
 
 
132
{
 
133
  CSpeedFlagPtr      cfp;
 
134
  SeqMgrFeatContext  gcontext;
 
135
  SeqFeatPtr         gene;
 
136
 
 
137
  if (sfp == NULL) return;
 
138
  cfp = (CSpeedFlagPtr) userdata;
 
139
  if (cfp == NULL || cfp->ofp == NULL) return;
 
140
 
 
141
  if (sfp->data.choice == SEQFEAT_GENE) return;
 
142
  gene = SeqMgrGetOverlappingGene (sfp->location, &gcontext);
 
143
  if (gene == NULL) return;
 
144
}
 
145
 
 
146
static void LIBCALLBACK EmptyStreamProc (
 
147
  CharPtr sequence,
 
148
  Pointer userdata
 
149
)
 
150
 
 
151
{
 
152
  /* empty stream callback */
 
153
}
 
154
 
 
155
static void DoFastaSeq (
 
156
  BioseqPtr bsp,
 
157
  Pointer userdata
 
158
)
 
159
 
 
160
{
 
161
  CSpeedFlagPtr  cfp;
 
162
 
 
163
  if (bsp == NULL) return;
 
164
  cfp = (CSpeedFlagPtr) userdata;
 
165
  if (cfp == NULL) return;
 
166
 
 
167
  if (cfp->ofp != NULL) {
 
168
    BioseqFastaStream (bsp, cfp->ofp, STREAM_EXPAND_GAPS, 70, 0, 0, TRUE);
 
169
  } else {
 
170
    SeqPortStream (bsp, STREAM_EXPAND_GAPS, NULL, EmptyStreamProc);
 
171
  }
 
172
}
 
173
 
 
174
static void DoFastaRaw (
 
175
  BioseqPtr bsp,
 
176
  Pointer userdata
 
177
)
 
178
 
 
179
{
 
180
  CSpeedFlagPtr  cfp;
 
181
 
 
182
  if (bsp == NULL) return;
 
183
  cfp = (CSpeedFlagPtr) userdata;
 
184
  if (cfp == NULL) return;
 
185
 
 
186
  if (cfp->ofp != NULL) {
 
187
    fprintf (cfp->ofp, ">\n");
 
188
    BioseqFastaStream (bsp, cfp->ofp, STREAM_EXPAND_GAPS, 70, 0, 0, FALSE);
 
189
  } else {
 
190
    SeqPortStream (bsp, STREAM_EXPAND_GAPS, NULL, EmptyStreamProc);
 
191
  }
 
192
}
 
193
 
 
194
static void DoFastaDefline (
 
195
  BioseqPtr bsp,
 
196
  Pointer userdata
 
197
)
 
198
 
 
199
{
 
200
  Char           buf [4096];
 
201
  CSpeedFlagPtr  cfp;
 
202
  Char           id [128];
 
203
 
 
204
  if (bsp == NULL) return;
 
205
  cfp = (CSpeedFlagPtr) userdata;
 
206
  if (cfp == NULL) return;
 
207
 
 
208
  id [0] = '\0';
 
209
  SeqIdWrite (bsp->id, id, PRINTID_FASTA_LONG, sizeof (id) - 1);
 
210
  buf [0] = '\0';
 
211
  CreateDefLine (NULL, bsp, buf, sizeof (buf) - 1, 0, NULL, NULL);
 
212
 
 
213
  if (cfp->ofp != NULL) {
 
214
    fprintf (cfp->ofp, ">%s %s\n", id, buf);
 
215
  }
 
216
}
 
217
 
 
218
static void DoFastaFeat (
 
219
  SeqFeatPtr sfp,
 
220
  Pointer userdata
 
221
)
 
222
 
 
223
{
 
224
  CSpeedFlagPtr  cfp;
 
225
  CharPtr        str;
 
226
 
 
227
  if (sfp == NULL) return;
 
228
  cfp = (CSpeedFlagPtr) userdata;
 
229
  if (cfp == NULL) return;
 
230
 
 
231
  if (cfp->ofp != NULL) {
 
232
    str = SeqLocPrint (sfp->location);
 
233
    if (str != NULL) {
 
234
      fprintf (cfp->ofp, "> [%s]\n", str);
 
235
      MemFree (str);
 
236
    }
 
237
    SeqLocFastaStream (sfp->location, cfp->ofp, STREAM_EXPAND_GAPS, 70, 0, 0);
 
238
  } else {
 
239
    SeqPortStreamLoc (sfp->location, STREAM_EXPAND_GAPS, NULL, EmptyStreamProc);
 
240
  }
 
241
}
 
242
 
 
243
static void DoFastaTrans (
 
244
  SeqFeatPtr sfp,
 
245
  Pointer userdata
 
246
)
 
247
 
 
248
{
 
249
  ByteStorePtr   bs;
 
250
  CSpeedFlagPtr  cfp;
 
251
  CharPtr        seq, str;
 
252
 
 
253
  if (sfp == NULL) return;
 
254
  cfp = (CSpeedFlagPtr) userdata;
 
255
  if (cfp == NULL) return;
 
256
 
 
257
  if (sfp->data.choice != SEQFEAT_CDREGION) return;
 
258
  bs = ProteinFromCdRegion (sfp, FALSE);
 
259
  if (bs == NULL) return;
 
260
 
 
261
  seq = (CharPtr) BSMerge (bs, NULL);
 
262
  BSFree (bs);
 
263
  if (seq == NULL) return;
 
264
 
 
265
  if (cfp->ofp != NULL) {
 
266
    str = SeqLocPrint (sfp->location);
 
267
    if (str != NULL) {
 
268
      fprintf (cfp->ofp, "> (%s)\n", str);
 
269
      MemFree (str);
 
270
    }
 
271
    fprintf (cfp->ofp, "%s\n", seq);
 
272
  }
 
273
 
 
274
  MemFree (seq);
 
275
}
 
276
 
 
277
static CharPtr compatSeverityLabel [] = {
 
278
  "NONE", "NOTE: valid", "WARNING: valid", "ERROR: valid", "REJECT: valid", "FATAL: valid", "MAX", NULL
 
279
};
 
280
 
 
281
static void LIBCALLBACK ValidCallback (
 
282
  ErrSev severity,
 
283
  int errcode,
 
284
  int subcode,
 
285
  Uint2 entityID,
 
286
  Uint2 itemtype,
 
287
  Uint4 itemID,
 
288
  CharPtr accession,
 
289
  CharPtr message,
 
290
  CharPtr objtype,
 
291
  CharPtr label,
 
292
  CharPtr context,
 
293
  CharPtr location,
 
294
  CharPtr product,
 
295
  Pointer userdata
 
296
)
 
297
 
 
298
{
 
299
  CharPtr  catname, errname;
 
300
  FILE     *fp;
 
301
 
 
302
  fp = (FILE *) userdata;
 
303
  if (fp == NULL) return;
 
304
 
 
305
  if (severity < SEV_NONE || severity > SEV_MAX) {
 
306
    severity = SEV_MAX;
 
307
  }
 
308
 
 
309
  catname = GetValidCategoryName (errcode);
 
310
  errname = GetValidErrorName (errcode, subcode);
 
311
 
 
312
  if (catname == NULL) {
 
313
    catname = "?";
 
314
  }
 
315
  if (errname == NULL) {
 
316
    errname = "?";
 
317
  }
 
318
 
 
319
  if (accession == NULL) {
 
320
    accession = "";
 
321
  }
 
322
  if (message == NULL) {
 
323
    message = "";
 
324
  }
 
325
  if (objtype == NULL) {
 
326
    objtype = "";
 
327
  }
 
328
  if (label == NULL) {
 
329
    label = "";
 
330
  }
 
331
 
 
332
  fprintf (fp, "%s [%s.%s] %s %s: %s",
 
333
           compatSeverityLabel [severity],
 
334
           catname, errname, message, objtype, label);
 
335
  if (location != NULL) {
 
336
    fprintf (fp, " %s", location);
 
337
  }
 
338
  if (context != NULL) {
 
339
    fprintf (fp, " %s", context);
 
340
  }
 
341
  if (product != NULL) {
 
342
    fprintf (fp, " -> %s", product);
 
343
  }
 
344
  fprintf (fp, "\n");
 
345
}
 
346
 
 
347
static void DoProcess (
 
348
  SeqEntryPtr sep,
 
349
  Uint2 entityID,
 
350
  CSpeedFlagPtr cfp
 
351
)
 
352
 
 
353
{
 
354
  ErrSev          oldErrSev;
 
355
  ValidStructPtr  vsp;
 
356
 
 
357
  if (sep == NULL || cfp == NULL) return;
 
358
 
 
359
  if (StringChr (cfp->clean, 'b') != NULL) {
 
360
    BasicSeqEntryCleanup (sep);
 
361
  }
 
362
  if (StringChr (cfp->clean, 's') != NULL) {
 
363
    SeriousSeqEntryCleanup (sep, NULL, NULL);
 
364
  }
 
365
 
 
366
  if (StringChr (cfp->index, 'f') != NULL) {
 
367
    SeqMgrIndexFeatures (entityID, 0);
 
368
  }
 
369
 
 
370
  if (StringChr (cfp->seq, 's') != NULL) {
 
371
    VisitBioseqsInSep (sep, (Pointer) cfp, DoFastaSeq);
 
372
  }
 
373
  if (StringChr (cfp->seq, 'r') != NULL) {
 
374
    VisitBioseqsInSep (sep, (Pointer) cfp, DoFastaRaw);
 
375
  }
 
376
  if (StringChr (cfp->seq, 'd') != NULL) {
 
377
    VisitBioseqsInSep (sep, (Pointer) cfp, DoFastaDefline);
 
378
  }
 
379
  if (StringChr (cfp->seq, 'f') != NULL) {
 
380
    VisitFeaturesInSep (sep, (Pointer) cfp, DoFastaFeat);
 
381
  }
 
382
  if (StringChr (cfp->seq, 't') != NULL) {
 
383
    VisitFeaturesInSep (sep, (Pointer) cfp, DoFastaTrans);
 
384
  }
 
385
 
 
386
  if (StringChr (cfp->feat, 'v') != NULL) {
 
387
    VisitFeaturesInSep (sep, NULL, DoVisitFeaturesTest);
 
388
  }
 
389
  if (StringChr (cfp->feat, 'g') != NULL) {
 
390
    if (SeqMgrFeaturesAreIndexed (entityID) == 0) {
 
391
      SeqMgrIndexFeatures (entityID, 0);
 
392
    }
 
393
    VisitFeaturesInSep (sep, (Pointer) cfp, DoGeneOverlapPrintTest);
 
394
  }
 
395
  if (StringChr (cfp->feat, 'h') != NULL) {
 
396
    if (SeqMgrFeaturesAreIndexed (entityID) == 0) {
 
397
      SeqMgrIndexFeatures (entityID, 0);
 
398
    }
 
399
    VisitFeaturesInSep (sep, (Pointer) cfp, DoGeneOverlapSpeedTest);
 
400
  }
 
401
  if (StringChr (cfp->feat, 'x') != NULL) {
 
402
  }
 
403
  if (StringChr (cfp->feat, 'o') != NULL) {
 
404
  }
 
405
  if (StringChr (cfp->feat, 'd') != NULL) {
 
406
  }
 
407
  if (StringChr (cfp->feat, 't') != NULL) {
 
408
    SeqEntryToGnbk (sep, NULL, FTABLE_FMT, SEQUIN_MODE, NORMAL_STYLE,
 
409
                    0, 0, SHOW_PROT_FTABLE, NULL, cfp->ofp);
 
410
  }
 
411
 
 
412
  if (StringChr (cfp->desc, 'b') != NULL) {
 
413
  }
 
414
  if (StringChr (cfp->desc, 't') != NULL) {
 
415
  }
 
416
 
 
417
  if (StringChr (cfp->verify, 'v') != NULL) {
 
418
    if (SeqMgrFeaturesAreIndexed (entityID) == 0) {
 
419
      SeqMgrIndexFeatures (entityID, 0);
 
420
    }
 
421
    vsp = ValidStructNew ();
 
422
    if (vsp != NULL) {
 
423
      vsp->useSeqMgrIndexes = TRUE;
 
424
      vsp->suppressContext = TRUE;
 
425
      vsp->seqSubmitParent = TRUE;
 
426
      vsp->testLatLonSubregion = TRUE;
 
427
      oldErrSev = ErrSetMessageLevel (SEV_NONE);
 
428
      vsp->errfunc = ValidCallback;
 
429
      vsp->userdata = (Pointer) cfp->ofp;
 
430
      /* vsp->convertGiToAccn = FALSE; */
 
431
      ValidateSeqEntry (sep, vsp);
 
432
      ValidStructFree (vsp);
 
433
      ErrSetMessageLevel (oldErrSev);
 
434
    }
 
435
  }
 
436
  if (StringChr (cfp->verify, 'b') != NULL) {
 
437
    if (SeqMgrFeaturesAreIndexed (entityID) == 0) {
 
438
      SeqMgrIndexFeatures (entityID, 0);
 
439
    }
 
440
    SeqEntryToGnbk (sep, NULL, GENBANK_FMT, SEQUIN_MODE, NORMAL_STYLE,
 
441
                    0, 0, 0, NULL, cfp->ofp);
 
442
  }
 
443
 
 
444
  if (cfp->ofp != NULL) {
 
445
    fflush (cfp->ofp);
 
446
  }
 
447
}
 
448
 
 
449
static void ProcessSingleRecord (
 
450
  CharPtr filename,
 
451
  CSpeedFlagPtr cfp
 
452
)
 
453
 
 
454
{
 
455
  AsnIoPtr      aip;
 
456
  BioseqPtr     bsp;
 
457
  ValNodePtr    bsplist = NULL;
 
458
  BioseqSetPtr  bssp;
 
459
  Pointer       dataptr = NULL;
 
460
  Uint2         datatype, entityID = 0;
 
461
  FileCache     fc;
 
462
  FILE          *fp;
 
463
  Int1          iotype;
 
464
  Char          line [512];
 
465
  Int4          maxio = 1;
 
466
  SeqEntryPtr   sep;
 
467
  time_t        starttime, stoptime, worsttime;
 
468
  CharPtr       str;
 
469
  Int4          x;
 
470
 
 
471
  if (cfp == NULL) return;
 
472
 
 
473
  if (StringHasNoText (filename)) return;
 
474
 
 
475
  if (StringChr (cfp->io, 'r') != NULL) {
 
476
    maxio = cfp->maxcount;
 
477
  }
 
478
 
 
479
  starttime = GetSecs ();
 
480
 
 
481
  for (x = 0; x < maxio; x++) {
 
482
    if (entityID != 0) {
 
483
      ObjMgrFreeByEntityID (entityID);
 
484
      entityID = 0;
 
485
      dataptr = NULL;
 
486
    }
 
487
 
 
488
    if (cfp->type == 1) {
 
489
 
 
490
      fp = FileOpen (filename, "r");
 
491
      if (fp == NULL) {
 
492
        Message (MSG_POSTERR, "Failed to open '%s'", filename);
 
493
        return;
 
494
      }
 
495
 
 
496
      dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, FALSE, FALSE);
 
497
 
 
498
      FileClose (fp);
 
499
 
 
500
      entityID = ObjMgrRegister (datatype, dataptr);
 
501
 
 
502
    } else if (cfp->type >= 2 && cfp->type <= 5) {
 
503
 
 
504
      aip = AsnIoOpen (filename, cfp->binary? "rb" : "r");
 
505
      if (aip == NULL) {
 
506
        Message (MSG_POSTERR, "AsnIoOpen failed for input file '%s'", filename);
 
507
        return;
 
508
      }
 
509
 
 
510
      switch (cfp->type) {
 
511
        case 2 :
 
512
          dataptr = (Pointer) SeqEntryAsnRead (aip, NULL);
 
513
          datatype = OBJ_SEQENTRY;
 
514
          break;
 
515
        case 3 :
 
516
          dataptr = (Pointer) BioseqAsnRead (aip, NULL);
 
517
          datatype = OBJ_BIOSEQ;
 
518
          break;
 
519
        case 4 :
 
520
          dataptr = (Pointer) BioseqSetAsnRead (aip, NULL);
 
521
          datatype = OBJ_BIOSEQSET;
 
522
          break;
 
523
        case 5 :
 
524
          dataptr = (Pointer) SeqSubmitAsnRead (aip, NULL);
 
525
          datatype = OBJ_SEQSUB;
 
526
          break;
 
527
        default :
 
528
          break;
 
529
      }
 
530
 
 
531
      AsnIoClose (aip);
 
532
 
 
533
      entityID = ObjMgrRegister (datatype, dataptr);
 
534
 
 
535
    } else if (cfp->type == 6) {
 
536
 
 
537
      fp = FileOpen (filename, "r");
 
538
      if (fp == NULL) {
 
539
        Message (MSG_POSTERR, "Failed to open '%s'", filename);
 
540
        return;
 
541
      }
 
542
 
 
543
      dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, FALSE, FALSE);
 
544
 
 
545
      FileClose (fp);
 
546
 
 
547
      entityID = ObjMgrRegister (datatype, dataptr);
 
548
 
 
549
    } else if (cfp->type == 7) {
 
550
 
 
551
      fp = FileOpen (filename, "r");
 
552
      if (fp == NULL) {
 
553
        Message (MSG_POSTERR, "Failed to open '%s'", filename);
 
554
        return;
 
555
      }
 
556
 
 
557
      FileCacheSetup (&fc, fp);
 
558
 
 
559
      str = FileCacheReadLine (&fc, line, sizeof (line), NULL);
 
560
      while (str != NULL) {
 
561
        str = FileCacheReadLine (&fc, line, sizeof (line), NULL);
 
562
      }
 
563
 
 
564
      FileClose (fp);
 
565
 
 
566
      return;
 
567
 
 
568
    } else {
 
569
      Message (MSG_POSTERR, "Input format type '%d' unrecognized", (int) cfp->type);
 
570
      return;
 
571
    }
 
572
  }
 
573
 
 
574
  if (entityID < 1 || dataptr == NULL) {
 
575
    Message (MSG_POSTERR, "Data read failed for input file '%s'", filename);
 
576
    return;
 
577
  }
 
578
 
 
579
  if (datatype == OBJ_SEQSUB || datatype == OBJ_SEQENTRY ||
 
580
        datatype == OBJ_BIOSEQ || datatype == OBJ_BIOSEQSET) {
 
581
 
 
582
    stoptime = GetSecs ();
 
583
    worsttime = stoptime - starttime;
 
584
    if (cfp->logfp != NULL) {
 
585
      fprintf (cfp->logfp, "ASN reading time %ld seconds\n", (long) worsttime);
 
586
      fflush (cfp->logfp);
 
587
    }
 
588
 
 
589
    sep = GetTopSeqEntryForEntityID (entityID);
 
590
 
 
591
    if (sep == NULL) {
 
592
      sep = SeqEntryNew ();
 
593
      if (sep != NULL) {
 
594
        if (datatype == OBJ_BIOSEQ) {
 
595
          bsp = (BioseqPtr) dataptr;
 
596
          sep->choice = 1;
 
597
          sep->data.ptrvalue = bsp;
 
598
          SeqMgrSeqEntry (SM_BIOSEQ, (Pointer) bsp, sep);
 
599
        } else if (datatype == OBJ_BIOSEQSET) {
 
600
          bssp = (BioseqSetPtr) dataptr;
 
601
          sep->choice = 2;
 
602
          sep->data.ptrvalue = bssp;
 
603
          SeqMgrSeqEntry (SM_BIOSEQSET, (Pointer) bssp, sep);
 
604
        } else {
 
605
          sep = SeqEntryFree (sep);
 
606
        }
 
607
      }
 
608
      sep = GetTopSeqEntryForEntityID (entityID);
 
609
    }
 
610
 
 
611
    if (sep != NULL) {
 
612
 
 
613
      if (cfp->lock) {
 
614
        starttime = GetSecs ();
 
615
 
 
616
        bsplist = LockFarComponents (sep);
 
617
 
 
618
        stoptime = GetSecs ();
 
619
        worsttime = stoptime - starttime;
 
620
        if (cfp->logfp != NULL) {
 
621
          fprintf (cfp->logfp, "Far component locking time %ld seconds\n", (long) worsttime);
 
622
          fflush (cfp->logfp);
 
623
        }
 
624
      }
 
625
 
 
626
      if (StringChr (cfp->io, 'w') != NULL) {
 
627
        starttime = GetSecs ();
 
628
 
 
629
        iotype = ASNIO_TEXT_OUT;
 
630
        if (StringChr (cfp->io, 'b') != NULL) {
 
631
          iotype = ASNIO_BIN_OUT;
 
632
        }
 
633
 
 
634
        for (x = 0; x < cfp->maxcount; x++) {
 
635
          aip = AsnIoNew (iotype, cfp->ofp, NULL, NULL, NULL);
 
636
          if (aip != NULL) {
 
637
            SeqEntryAsnWrite (sep, aip, NULL);
 
638
            AsnIoFree (aip, FALSE);
 
639
          }
 
640
        }
 
641
 
 
642
        stoptime = GetSecs ();
 
643
        worsttime = stoptime - starttime;
 
644
        if (cfp->logfp != NULL) {
 
645
          fprintf (cfp->logfp, "ASN writing time %ld seconds\n", (long) worsttime);
 
646
          fflush (cfp->logfp);
 
647
        }
 
648
      }
 
649
 
 
650
      starttime = GetSecs ();
 
651
 
 
652
      for (x = 0; x < cfp->maxcount; x++) {
 
653
        DoProcess (sep, entityID, cfp);
 
654
      }
 
655
 
 
656
      stoptime = GetSecs ();
 
657
      worsttime = stoptime - starttime;
 
658
      if (cfp->logfp != NULL) {
 
659
        fprintf (cfp->logfp, "Internal processing time %ld seconds\n", (long) worsttime);
 
660
        fflush (cfp->logfp);
 
661
      }
 
662
 
 
663
      ObjMgrFreeByEntityID (entityID);
 
664
 
 
665
      bsplist = UnlockFarComponents (bsplist);
 
666
    }
 
667
 
 
668
  } else {
 
669
 
 
670
    Message (MSG_POSTERR, "Datatype %d not recognized", (int) datatype);
 
671
  }
 
672
}
 
673
 
 
674
static void ProcessMultipleRecord (
 
675
  CharPtr filename,
 
676
  CSpeedFlagPtr cfp
 
677
)
 
678
 
 
679
{
 
680
  AsnIoPtr     aip;
 
681
  AsnTypePtr   atp;
 
682
  BioseqPtr    bsp;
 
683
  Char         buf [41];
 
684
  Uint2        entityID;
 
685
  FILE         *fp;
 
686
  SeqEntryPtr  fsep;
 
687
  Char         longest [41];
 
688
  Int4         numrecords, x;
 
689
  SeqEntryPtr  sep;
 
690
  time_t       starttime, stoptime, worsttime;
 
691
#ifdef OS_UNIX
 
692
  Char         cmmd [256];
 
693
  CharPtr      gzcatprog;
 
694
  int          ret;
 
695
  Boolean      usedPopen = FALSE;
 
696
#endif
 
697
 
 
698
  if (cfp == NULL) return;
 
699
 
 
700
  if (StringHasNoText (filename)) return;
 
701
 
 
702
#ifndef OS_UNIX
 
703
  if (cfp->compressed) {
 
704
    Message (MSG_POSTERR, "Can only decompress on-the-fly on UNIX machines");
 
705
    return;
 
706
  }
 
707
#endif
 
708
 
 
709
#ifdef OS_UNIX
 
710
  if (cfp->compressed) {
 
711
    gzcatprog = getenv ("NCBI_UNCOMPRESS_BINARY");
 
712
    if (gzcatprog != NULL) {
 
713
      sprintf (cmmd, "%s %s", gzcatprog, filename);
 
714
    } else {
 
715
      ret = system ("gzcat -h >/dev/null 2>&1");
 
716
      if (ret == 0) {
 
717
        sprintf (cmmd, "gzcat %s", filename);
 
718
      } else if (ret == -1) {
 
719
        Message (MSG_POSTERR, "Unable to fork or exec gzcat in ScanBioseqSetRelease");
 
720
        return;
 
721
      } else {
 
722
        ret = system ("zcat -h >/dev/null 2>&1");
 
723
        if (ret == 0) {
 
724
          sprintf (cmmd, "zcat %s", filename);
 
725
        } else if (ret == -1) {
 
726
          Message (MSG_POSTERR, "Unable to fork or exec zcat in ScanBioseqSetRelease");
 
727
          return;
 
728
        } else {
 
729
          Message (MSG_POSTERR, "Unable to find zcat or gzcat in ScanBioseqSetRelease - please edit your PATH environment variable");
 
730
          return;
 
731
        }
 
732
      }
 
733
    }
 
734
    fp = popen (cmmd, /* cfp->binary? "rb" : */ "r");
 
735
    usedPopen = TRUE;
 
736
  } else {
 
737
    fp = FileOpen (filename, cfp->binary? "rb" : "r");
 
738
  }
 
739
#else
 
740
  fp = FileOpen (filename, cfp->binary? "rb" : "r");
 
741
#endif
 
742
  if (fp == NULL) {
 
743
    Message (MSG_POSTERR, "FileOpen failed for input file '%s'", filename);
 
744
    return;
 
745
  }
 
746
 
 
747
  aip = AsnIoNew (cfp->binary? ASNIO_BIN_IN : ASNIO_TEXT_IN, fp, NULL, NULL, NULL);
 
748
  if (aip == NULL) {
 
749
    Message (MSG_ERROR, "AsnIoNew failed for input file '%s'", filename);
 
750
    return;
 
751
  }
 
752
 
 
753
  if (cfp->logfp != NULL) {
 
754
    fprintf (cfp->logfp, "%s\n\n", filename);
 
755
    fflush (cfp->logfp);
 
756
  }
 
757
 
 
758
  longest [0] = '\0';
 
759
  worsttime = 0;
 
760
  numrecords = 0;
 
761
 
 
762
  atp = cfp->atp_bss;
 
763
 
 
764
  while ((atp = AsnReadId (aip, cfp->amp, atp)) != NULL) {
 
765
    if (atp == cfp->atp_se) {
 
766
 
 
767
      sep = SeqEntryAsnRead (aip, atp);
 
768
      if (sep != NULL) {
 
769
 
 
770
        entityID = ObjMgrGetEntityIDForChoice (sep);
 
771
 
 
772
        fsep = FindNthBioseq (sep, 1);
 
773
        if (fsep != NULL && fsep->choice == 1) {
 
774
          bsp = (BioseqPtr) fsep->data.ptrvalue;
 
775
          if (bsp != NULL) {
 
776
            SeqIdWrite (bsp->id, buf, PRINTID_FASTA_LONG, sizeof (buf));
 
777
            if (cfp->logfp != NULL) {
 
778
              fprintf (cfp->logfp, "%s\n", buf);
 
779
              fflush (cfp->logfp);
 
780
            }
 
781
          }
 
782
        }
 
783
 
 
784
        starttime = GetSecs ();
 
785
        for (x = 0; x < cfp->maxcount; x++) {
 
786
          DoProcess (sep, entityID, cfp);
 
787
        }
 
788
        stoptime = GetSecs ();
 
789
 
 
790
        if (stoptime - starttime > worsttime) {
 
791
          worsttime = stoptime - starttime;
 
792
          StringCpy (longest, buf);
 
793
        }
 
794
        numrecords++;
 
795
 
 
796
        ObjMgrFreeByEntityID (entityID);
 
797
      }
 
798
 
 
799
    } else {
 
800
 
 
801
      AsnReadVal (aip, atp, NULL);
 
802
    }
 
803
  }
 
804
 
 
805
  AsnIoFree (aip, FALSE);
 
806
 
 
807
#ifdef OS_UNIX
 
808
  if (usedPopen) {
 
809
    pclose (fp);
 
810
  } else {
 
811
    FileClose (fp);
 
812
  }
 
813
#else
 
814
  FileClose (fp);
 
815
#endif
 
816
  if (cfp->logfp != NULL && (! StringHasNoText (longest))) {
 
817
    fprintf (cfp->logfp, "Longest processing time %ld seconds on %s\n",
 
818
             (long) worsttime, longest);
 
819
    fprintf (cfp->logfp, "Total number of records %ld\n", (long) numrecords);
 
820
    fflush (cfp->logfp);
 
821
  }
 
822
}
 
823
 
 
824
static void ProcessOneRecord (
 
825
  CharPtr filename,
 
826
  Pointer userdata
 
827
)
 
828
 
 
829
{
 
830
  CSpeedFlagPtr  cfp;
 
831
 
 
832
  if (StringHasNoText (filename)) return;
 
833
  cfp = (CSpeedFlagPtr) userdata;
 
834
  if (cfp == NULL) return;
 
835
 
 
836
  if (cfp->batch) {
 
837
    ProcessMultipleRecord (filename, cfp);
 
838
  } else {
 
839
    ProcessSingleRecord (filename, cfp);
 
840
  }
 
841
}
 
842
 
 
843
/* Args structure contains command-line arguments */
 
844
 
 
845
#define p_argInputPath     0
 
846
#define i_argInputFile     1
 
847
#define o_argOutputFile    2
 
848
#define f_argFilter        3
 
849
#define x_argSuffix        4
 
850
#define a_argType          5
 
851
#define b_argBinary        6
 
852
#define c_argCompressed    7
 
853
#define l_argLockFar       8
 
854
#define L_argLogFile       9
 
855
#define R_argRemote       10
 
856
#define X_argMaxCount     11
 
857
#define O_argInOut        12
 
858
#define K_argClean        13
 
859
#define I_argIndex        14
 
860
#define S_argSeq          15
 
861
#define F_argFeat         16
 
862
#define D_argDesc         17
 
863
#define V_argVerify       18
 
864
 
 
865
Args myargs [] = {
 
866
  {"Path to Files", NULL, NULL, NULL,
 
867
    TRUE, 'p', ARG_STRING, 0.0, 0, NULL},
 
868
  {"Single Input File", "stdin", NULL, NULL,
 
869
    TRUE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
 
870
  {"Output File", "stdout", NULL, NULL,
 
871
    TRUE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
 
872
  {"Substring Filter", NULL, NULL, NULL,
 
873
    TRUE, 'f', ARG_STRING, 0.0, 0, NULL},
 
874
  {"File Selection Suffix", ".ent", NULL, NULL,
 
875
    TRUE, 'x', ARG_STRING, 0.0, 0, NULL},
 
876
  {"ASN.1 Type\n"
 
877
   "      a Any\n"
 
878
   "      e Seq-entry\n"
 
879
   "      b Bioseq\n"
 
880
   "      s Bioseq-set\n"
 
881
   "      m Seq-submit\n"
 
882
   "      t Batch Processing\n"
 
883
   "      f FASTA\n"
 
884
   "      l Read by Lines", "a", NULL, NULL,
 
885
    TRUE, 'a', ARG_STRING, 0.0, 0, NULL},
 
886
  {"Bioseq-set is Binary", "F", NULL, NULL,
 
887
    TRUE, 'b', ARG_BOOLEAN, 0.0, 0, NULL},
 
888
  {"Bioseq-set is Compressed", "F", NULL, NULL,
 
889
    TRUE, 'c', ARG_BOOLEAN, 0.0, 0, NULL},
 
890
  {"Lock Components in Advance", "F", NULL, NULL,
 
891
    TRUE, 'l', ARG_BOOLEAN, 0.0, 0, NULL},
 
892
  {"Log File", NULL, NULL, NULL,
 
893
    TRUE, 'L', ARG_FILE_OUT, 0.0, 0, NULL},
 
894
  {"Remote Fetching from ID", "F", NULL, NULL,
 
895
    TRUE, 'R', ARG_BOOLEAN, 0.0, 0, NULL},
 
896
  {"Max Repeat Count", "1", NULL, NULL,
 
897
    TRUE, 'X', ARG_INT, 0.0, 0, NULL},
 
898
  {"Input Output\n"
 
899
   "      r Read ASN.1\n"
 
900
   "      w Write Text ASN.1\n"
 
901
   "      wb Write Binary ASN.1", NULL, NULL, NULL,
 
902
    TRUE, 'O', ARG_STRING, 0.0, 0, NULL},
 
903
  {"Cleanup\n"
 
904
   "      b BasicSeqEntryCleanup\n"
 
905
   "      s SeriousSeqEntryCleanup", NULL, NULL, NULL,
 
906
    TRUE, 'K', ARG_STRING, 0.0, 0, NULL},
 
907
  {"Index\n"
 
908
   "      f Feature Indexing", NULL, NULL, NULL,
 
909
    TRUE, 'I', ARG_STRING, 0.0, 0, NULL},
 
910
  {"Sequence\n"
 
911
   "      s FASTA of Sequence\n"
 
912
   "      r Raw FASTA no Defline\n"
 
913
   "      d Just FASTA Defline\n"
 
914
   "      f FASTA by Feature\n"
 
915
   "      t FASTA of Translation", NULL, NULL, NULL,
 
916
    TRUE, 'S', ARG_STRING, 0.0, 0, NULL},
 
917
  {"Feature\n"
 
918
   "      v Visit Features\n"
 
919
   "      g Gene Overlap Print\n"
 
920
   "      h Gene Overlap Speed\n"
 
921
   "      x Gene by Xref\n"
 
922
   "      o Operon by Overlap\n"
 
923
   "      d Feature by ID\n"
 
924
   "      t Feature Table", NULL, NULL, NULL,
 
925
    TRUE, 'F', ARG_STRING, 0.0, 0, NULL},
 
926
  {"Descriptor\n"
 
927
   "      b BioSource\n"
 
928
   "      t Title", NULL, NULL, NULL,
 
929
    TRUE, 'D', ARG_STRING, 0.0, 0, NULL},
 
930
  {"Verification\n"
 
931
   "      v Validate with Normal Stringency\n"
 
932
   "      b Generate GenBank Flatfile\n", NULL, NULL, NULL,
 
933
    TRUE, 'V', ARG_STRING, 0.0, 0, NULL},
 
934
};
 
935
 
 
936
Int2 Main (void)
 
937
 
 
938
{
 
939
  Char            app [64], type;
 
940
  CSpeedFlagData  cfd;
 
941
  CharPtr         directory, filter, infile, logfile, outfile, str, suffix;
 
942
  Boolean         remote;
 
943
  time_t          runtime, starttime, stoptime;
 
944
 
 
945
  /* standard setup */
 
946
 
 
947
  ErrSetFatalLevel (SEV_MAX);
 
948
  ErrClearOptFlags (EO_SHOW_USERSTR);
 
949
  UseLocalAsnloadDataAndErrMsg ();
 
950
  ErrPathReset ();
 
951
 
 
952
  /* finish resolving internal connections in ASN.1 parse tables */
 
953
 
 
954
  if (! AllObjLoad ()) {
 
955
    Message (MSG_FATAL, "AllObjLoad failed");
 
956
    return 1;
 
957
  }
 
958
  if (! SubmitAsnLoad ()) {
 
959
    Message (MSG_FATAL, "SubmitAsnLoad failed");
 
960
    return 1;
 
961
  }
 
962
  if (! FeatDefSetLoad ()) {
 
963
    Message (MSG_FATAL, "FeatDefSetLoad failed");
 
964
    return 1;
 
965
  }
 
966
  if (! SeqCodeSetLoad ()) {
 
967
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
 
968
    return 1;
 
969
  }
 
970
  if (! GeneticCodeTableLoad ()) {
 
971
    Message (MSG_FATAL, "GeneticCodeTableLoad failed");
 
972
    return 1;
 
973
  }
 
974
 
 
975
  /* process command line arguments */
 
976
 
 
977
  sprintf (app, "cspeedtest %s", CSPEEDTEST_APPLICATION);
 
978
  if (! GetArgs (app, sizeof (myargs) / sizeof (Args), myargs)) {
 
979
    return 0;
 
980
  }
 
981
 
 
982
  MemSet ((Pointer) &cfd, 0, sizeof (CSpeedFlagData));
 
983
 
 
984
  directory = (CharPtr) myargs [p_argInputPath].strvalue;
 
985
  infile = (CharPtr) myargs [i_argInputFile].strvalue;
 
986
  outfile = (CharPtr) myargs [o_argOutputFile].strvalue;
 
987
  filter = (CharPtr) myargs [f_argFilter].strvalue;
 
988
  suffix = (CharPtr) myargs [x_argSuffix].strvalue;
 
989
 
 
990
  cfd.batch = FALSE;
 
991
  cfd.binary = (Boolean) myargs [b_argBinary].intvalue;
 
992
  cfd.compressed = (Boolean) myargs [c_argCompressed].intvalue;
 
993
  cfd.lock = (Boolean) myargs [l_argLockFar].intvalue;
 
994
  cfd.type = 1;
 
995
 
 
996
  str = myargs [a_argType].strvalue;
 
997
  TrimSpacesAroundString (str);
 
998
  if (StringDoesHaveText (str)) {
 
999
    type = str [0];
 
1000
  } else {
 
1001
    type = 'a';
 
1002
  }
 
1003
 
 
1004
  type = TO_LOWER (type);
 
1005
  switch (type) {
 
1006
    case 'a' :
 
1007
      cfd.type = 1;
 
1008
      break;
 
1009
    case 'e' :
 
1010
      cfd.type = 2;
 
1011
      break;
 
1012
    case 'b' :
 
1013
      cfd.type = 3;
 
1014
      break;
 
1015
    case 's' :
 
1016
      cfd.type = 4;
 
1017
      break;
 
1018
    case 'm' :
 
1019
      cfd.type = 5;
 
1020
      break;
 
1021
    case 't' :
 
1022
      cfd.type = 1;
 
1023
      cfd.batch = TRUE;
 
1024
      break;
 
1025
    case 'f' :
 
1026
      cfd.type = 6;
 
1027
      break;
 
1028
    case 'l' :
 
1029
      cfd.type = 7;
 
1030
      break;
 
1031
    default :
 
1032
      cfd.type = 1;
 
1033
      break;
 
1034
  }
 
1035
 
 
1036
  remote = (Boolean) myargs [R_argRemote].intvalue;
 
1037
 
 
1038
  cfd.maxcount = myargs [X_argMaxCount].intvalue;
 
1039
  if (cfd.maxcount < 1) {
 
1040
    cfd.maxcount = 1;
 
1041
  }
 
1042
 
 
1043
  cfd.io = myargs [O_argInOut].strvalue;
 
1044
  cfd.clean = myargs [K_argClean].strvalue;
 
1045
  cfd.index = myargs [I_argIndex].strvalue;
 
1046
  cfd.seq = myargs [S_argSeq].strvalue;
 
1047
  cfd.feat = myargs [F_argFeat].strvalue;
 
1048
  cfd.desc = myargs [D_argDesc].strvalue;
 
1049
  cfd.verify = myargs [V_argVerify].strvalue;
 
1050
 
 
1051
  cfd.amp = AsnAllModPtr ();
 
1052
  cfd.atp_bss = AsnFind ("Bioseq-set");
 
1053
  cfd.atp_bsss = AsnFind ("Bioseq-set.seq-set");
 
1054
  cfd.atp_se = AsnFind ("Bioseq-set.seq-set.E");
 
1055
  cfd.atp_bsc = AsnFind ("Bioseq-set.class");
 
1056
  cfd.bssp_atp = AsnLinkType (NULL, cfd.atp_bss);
 
1057
 
 
1058
  logfile = (CharPtr) myargs [L_argLogFile].strvalue;
 
1059
  if (StringDoesHaveText (logfile)) {
 
1060
    cfd.logfp = FileOpen (logfile, "w");
 
1061
  }
 
1062
 
 
1063
  if (remote) {
 
1064
    PubSeqFetchEnable ();
 
1065
  }
 
1066
 
 
1067
  if (StringDoesHaveText (outfile)) {
 
1068
    cfd.ofp = FileOpen (outfile, "w");
 
1069
  }
 
1070
 
 
1071
  starttime = GetSecs ();
 
1072
 
 
1073
  if (StringDoesHaveText (directory)) {
 
1074
 
 
1075
    DirExplore (directory, NULL, suffix, FALSE, ProcessOneRecord, (Pointer) &cfd);
 
1076
 
 
1077
  } else if (StringDoesHaveText (infile)) {
 
1078
 
 
1079
    ProcessOneRecord (infile, (Pointer) &cfd);
 
1080
  }
 
1081
 
 
1082
  if (cfd.ofp != NULL) {
 
1083
    FileClose (cfd.ofp);
 
1084
  }
 
1085
 
 
1086
  stoptime = GetSecs ();
 
1087
  runtime = stoptime - starttime;
 
1088
  if (cfd.logfp != NULL) {
 
1089
    fprintf (cfd.logfp, "Finished in %ld seconds\n", (long) runtime);
 
1090
    FileClose (cfd.logfp);
 
1091
  }
 
1092
 
 
1093
  if (remote) {
 
1094
    PubSeqFetchDisable ();
 
1095
  }
 
1096
 
 
1097
  return 0;
 
1098
}
 
1099