~ubuntu-branches/debian/experimental/ncbi-tools6/experimental

« back to all changes in this revision

Viewing changes to demo/asn2all.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:
29
29
*
30
30
* Version Creation Date:   7/26/04
31
31
*
32
 
* $Revision: 1.28 $
 
32
* $Revision: 1.37 $
33
33
*
34
34
* File Description:
35
35
*
53
53
#include <pmfapi.h>
54
54
#include <lsqfetch.h>
55
55
 
56
 
#define ASN2ALL_APP_VER "1.8"
 
56
#define ASN2ALL_APP_VER "2.4"
57
57
 
58
58
CharPtr ASN2ALL_APPLICATION = ASN2ALL_APP_VER;
59
59
 
60
 
static ValNodePtr  requested_uid_list = NULL;
61
 
static TNlmMutex   requested_uid_mutex = NULL;
62
 
 
63
 
static ValNodePtr  locked_bsp_list = NULL;
64
 
static TNlmMutex   locked_bsp_mutex = NULL;
65
 
 
66
 
static void AddUidToQueue (
67
 
  SeqIdPtr sip
68
 
)
69
 
 
70
 
{
71
 
  ValNodePtr  last = NULL, vnp;
72
 
  Int4        ret;
73
 
  Int4        uid;
74
 
 
75
 
  if (sip == NULL || sip->choice != SEQID_GI) return;
76
 
  uid = (Int4) sip->data.intvalue;
77
 
  if (uid < 1) return;
78
 
 
79
 
  ret = NlmMutexLockEx (&requested_uid_mutex);
80
 
  if (ret) {
81
 
    ErrPostEx (SEV_FATAL, 0, 0, "AddUidToQueue mutex failed [%ld]", (long) ret);
82
 
    return;
83
 
  }
84
 
 
85
 
  /* check against uids already in queue */
86
 
 
87
 
  last = NULL;
88
 
  for (vnp = requested_uid_list; vnp != NULL; vnp = vnp->next) {
89
 
    last = vnp;
90
 
    if ((Int4) vnp->data.intvalue == uid) break;
91
 
  }
92
 
 
93
 
  /* add uid to queue */
94
 
 
95
 
  if (vnp == NULL) {
96
 
    if (last != NULL) {
97
 
      vnp = ValNodeAddInt (&last, 0, uid);
98
 
      last = vnp;
99
 
    } else {
100
 
      requested_uid_list = ValNodeAddInt (NULL, 0, uid);
101
 
      last = requested_uid_list;
102
 
    }
103
 
  }
104
 
 
105
 
  NlmMutexUnlock (requested_uid_mutex);
106
 
}
107
 
 
108
 
static Int4 RemoveUidFromQueue (
109
 
  void
110
 
)
111
 
 
112
 
{
113
 
  Int4        ret, uid = 0;
114
 
  ValNodePtr  vnp;
115
 
 
116
 
  ret = NlmMutexLockEx (&requested_uid_mutex);
117
 
  if (ret) {
118
 
    ErrPostEx (SEV_FATAL, 0, 0, "RemoveUidFromQueue mutex failed [%ld]", (long) ret);
119
 
    return 0;
120
 
  }
121
 
 
122
 
  /* extract next requested uid from queue */
123
 
 
124
 
  if (requested_uid_list != NULL) {
125
 
    vnp = requested_uid_list;
126
 
    requested_uid_list = vnp->next;
127
 
    vnp->next = NULL;
128
 
    uid = (Int4) vnp->data.intvalue;
129
 
    ValNodeFree (vnp);
130
 
  }
131
 
 
132
 
  NlmMutexUnlock (requested_uid_mutex);
133
 
 
134
 
  return uid;
135
 
}
136
 
 
137
 
static void QueueFarSegments (SeqLocPtr slp)
138
 
 
139
 
{
140
 
  BioseqPtr   bsp;
141
 
  SeqLocPtr   loc;
142
 
  SeqIdPtr    sip;
143
 
  ValNodePtr  vnp;
144
 
 
145
 
  if (slp == NULL) return;
146
 
 
147
 
  sip = SeqLocId (slp);
148
 
  if (sip == NULL) {
149
 
    loc = SeqLocFindNext (slp, NULL);
150
 
    if (loc != NULL) {
151
 
      sip = SeqLocId (loc);
152
 
    }
153
 
  }
154
 
  if (sip == NULL) return;
155
 
 
156
 
  /* if packaged in record, no need to fetch it */
157
 
 
158
 
  if (BioseqFindCore (sip) != NULL) return;
159
 
 
160
 
  /* check against currently locked records */
161
 
 
162
 
  for (vnp = locked_bsp_list; vnp != NULL; vnp = vnp->next) {
163
 
    bsp = (BioseqPtr) vnp->data.ptrvalue;
164
 
    if (bsp == NULL) continue;
165
 
    if (SeqIdIn (sip, bsp->id)) return;
166
 
  }
167
 
 
168
 
  AddUidToQueue (sip);
169
 
}
170
 
 
171
 
static void QueueFarBioseqs (BioseqPtr bsp, Pointer userdata)
172
 
 
173
 
{
174
 
  DeltaSeqPtr  dsp;
175
 
  SeqLocPtr    slp = NULL;
176
 
  ValNode      vn;
177
 
 
178
 
  if (bsp == NULL) return;
179
 
 
180
 
  if (bsp->repr == Seq_repr_seg) {
181
 
    vn.choice = SEQLOC_MIX;
182
 
    vn.extended = 0;
183
 
    vn.data.ptrvalue = bsp->seq_ext;
184
 
    vn.next = NULL;
185
 
    while ((slp = SeqLocFindNext (&vn, slp)) != NULL) {
186
 
      if (slp != NULL && slp->choice != SEQLOC_NULL) {
187
 
        QueueFarSegments (slp);
188
 
      }
189
 
    }
190
 
  } else if (bsp->repr == Seq_repr_delta) {
191
 
    for (dsp = (DeltaSeqPtr) (bsp->seq_ext); dsp != NULL; dsp = dsp->next) {
192
 
      if (dsp->choice == 1) {
193
 
        slp = (SeqLocPtr) dsp->data.ptrvalue;
194
 
        if (slp != NULL && slp->choice != SEQLOC_NULL) {
195
 
          QueueFarSegments (slp);
196
 
        }
197
 
      }
198
 
    }
199
 
  }
200
 
}
201
 
 
202
 
static void AddBspToList (
203
 
  BioseqPtr bsp
204
 
)
205
 
 
206
 
{
207
 
  Int4        ret;
208
 
  ValNodePtr  vnp;
209
 
 
210
 
  if (bsp == NULL) return;
211
 
 
212
 
  ret = NlmMutexLockEx (&locked_bsp_mutex);
213
 
  if (ret) {
214
 
    ErrPostEx (SEV_FATAL, 0, 0, "AddBspToList mutex failed [%ld]", (long) ret);
215
 
    return;
216
 
  }
217
 
 
218
 
  vnp = ValNodeAddPointer (&locked_bsp_list, 0, (Pointer) bsp);
219
 
 
220
 
  NlmMutexUnlock (locked_bsp_mutex);
221
 
}
222
 
 
223
 
static ValNodePtr ExtractBspList (
224
 
  void
225
 
)
226
 
 
227
 
{
228
 
  Int4        ret;
229
 
  ValNodePtr  vnp;
230
 
 
231
 
  ret = NlmMutexLockEx (&locked_bsp_mutex);
232
 
  if (ret) {
233
 
    ErrPostEx (SEV_FATAL, 0, 0, "ExtractBspList mutex failed [%ld]", (long) ret);
234
 
    return NULL;
235
 
  }
236
 
 
237
 
  vnp = locked_bsp_list;
238
 
  locked_bsp_list = NULL;
239
 
 
240
 
  NlmMutexUnlock (locked_bsp_mutex);
241
 
 
242
 
  return vnp;
243
 
}
244
 
 
245
 
static VoidPtr DoAsyncLookup (
246
 
  VoidPtr arg
247
 
)
248
 
 
249
 
{
250
 
  BioseqPtr  bsp;
251
 
  Int4       uid;
252
 
  ValNode    vn;
253
 
 
254
 
  MemSet ((Pointer) &vn, 0, sizeof (ValNode));
255
 
 
256
 
  uid = RemoveUidFromQueue ();
257
 
  while (uid > 0) {
258
 
 
259
 
    vn.choice = SEQID_GI;
260
 
    vn.data.intvalue = uid;
261
 
    vn.next = NULL;
262
 
 
263
 
    bsp = BioseqLockById (&vn);
264
 
    if (bsp != NULL) {
265
 
      AddBspToList (bsp);
266
 
    }
267
 
 
268
 
    uid = RemoveUidFromQueue ();
269
 
  }
270
 
 
271
 
  return NULL;
272
 
}
273
 
 
274
 
#define NUM_ASYNC_LOOKUP_THREADS 5
275
 
 
276
 
static void ProcessAsyncLookups (
277
 
  void
278
 
)
279
 
 
280
 
{
281
 
  Int2        i;
282
 
  VoidPtr     status;
283
 
  TNlmThread  thds [NUM_ASYNC_LOOKUP_THREADS];
284
 
 
285
 
  /* spawn several threads for individual BioseqLockById requests */
286
 
 
287
 
  for (i = 0; i < NUM_ASYNC_LOOKUP_THREADS; i++) {
288
 
    thds [i] = NlmThreadCreate (DoAsyncLookup, NULL);
289
 
  }
290
 
 
291
 
  /* wait for all fetching threads to complete */
292
 
 
293
 
  for (i = 0; i < NUM_ASYNC_LOOKUP_THREADS; i++) {
294
 
    NlmThreadJoin (thds [i], &status);
295
 
  }
296
 
}
297
 
 
298
 
static ValNodePtr AsyncLockFarComponents (
299
 
  SeqEntryPtr sep
300
 
)
301
 
 
302
 
{
303
 
  BioseqPtr    bsp;
304
 
  ValNodePtr   bsplist = NULL, sublist, vnp;
305
 
  SeqEntryPtr  oldsep;
306
 
 
307
 
  if (sep == NULL) return NULL;
308
 
  oldsep = SeqEntrySetScope (sep);
309
 
 
310
 
  /* add far uids to queue */
311
 
 
312
 
  VisitBioseqsInSep (sep, NULL, QueueFarBioseqs);
313
 
 
314
 
  /* fetching from uid list using several threads */
315
 
 
316
 
  ProcessAsyncLookups ();
317
 
 
318
 
  sublist = ExtractBspList ();
319
 
 
320
 
  /* take list, look for seg or delta, recurse */
321
 
 
322
 
  while (sublist != NULL) {
323
 
    for (vnp = sublist; vnp != NULL; vnp = vnp->next) {
324
 
      bsp = (BioseqPtr) vnp->data.ptrvalue;
325
 
      if (bsp == NULL) continue;
326
 
      QueueFarBioseqs (bsp, NULL);
327
 
    }
328
 
 
329
 
    ValNodeLink (&bsplist, sublist);
330
 
    sublist = NULL;
331
 
 
332
 
    ProcessAsyncLookups ();
333
 
 
334
 
    sublist = ExtractBspList ();
335
 
  }
336
 
 
337
 
  SeqEntrySetScope (oldsep);
338
 
  return bsplist;
339
 
}
340
 
 
341
60
static ValNodePtr DoLockFarComponents (
342
61
  SeqEntryPtr sep,
343
62
  Boolean useThreads
350
69
  start_time = GetSecs ();
351
70
 
352
71
  if (NlmThreadsAvailable () && useThreads) {
353
 
    rsult = AsyncLockFarComponents (sep);
 
72
    rsult = AdvcLockFarComponents (sep, TRUE, FALSE, FALSE, NULL, TRUE);
354
73
  } else if (useThreads) {
355
74
    Message (MSG_POST, "Threads not available in this executable");
356
 
    rsult = LockFarComponents (sep);
 
75
    rsult = AdvcLockFarComponents (sep, TRUE, FALSE, FALSE, NULL, FALSE);
357
76
  } else {
358
 
    rsult = LockFarComponents (sep);
 
77
    rsult = AdvcLockFarComponents (sep, TRUE, FALSE, FALSE, NULL, FALSE);
359
78
  }
360
79
 
361
80
  stop_time = GetSecs ();
737
456
 
738
457
  omp = ObjMgrGet ();
739
458
  ObjMgrReapOne (omp);
 
459
  SeqMgrClearBioseqIndex ();
740
460
  ObjMgrFreeCache (0);
741
461
  FreeSeqIdGiCache ();
 
462
 
 
463
  SeqEntrySetScope (NULL);
742
464
}
743
465
 
744
466
static void ProcessMultipleRecord (
751
473
  AsnTypePtr     atp;
752
474
  BioseqPtr      bsp;
753
475
  ValNodePtr     bsplist;
754
 
  Char           cmmd [256];
755
476
  DataVal        dv;
756
477
  FILE           *fp;
 
478
  Boolean        io_failure = FALSE;
757
479
  ObjMgrPtr      omp;
758
480
  SeqEntryPtr    sep;
759
481
#ifdef OS_UNIX
 
482
  Char           cmmd [256];
760
483
  CharPtr        gzcatprog;
761
484
  int            ret;
762
485
  Boolean        usedPopen = FALSE;
813
536
 
814
537
  aip = AsnIoNew (afp->binary? ASNIO_BIN_IN : ASNIO_TEXT_IN, fp, NULL, NULL, NULL);
815
538
  if (aip == NULL) {
816
 
    Message (MSG_ERROR, "AsnIoNew failed for input file '%s'", filename);
 
539
    Message (MSG_POSTERR, "AsnIoNew failed for input file '%s'", filename);
817
540
    return;
818
541
  }
819
542
 
833
556
  if (aop != NULL) {
834
557
 
835
558
    if (afp->format == XML_FORMAT) {
836
 
      while ((atp = AsnReadId (aip, afp->amp, atp)) != NULL) {
 
559
      while ((! io_failure) && (atp = AsnReadId (aip, afp->amp, atp)) != NULL) {
 
560
        if (aip->io_failure) {
 
561
          io_failure = TRUE;
 
562
          aip->io_failure = FALSE;
 
563
        }
837
564
        if (atp == afp->atp_inst) {
838
565
          /* converts compressed sequences to iupac like asn2xml */
839
566
          bsp = BioseqNew ();
845
572
          AsnWrite (aop, atp, &dv);
846
573
          AsnKillValue (atp, &dv);
847
574
        }
 
575
        if (aip->io_failure) {
 
576
          io_failure = TRUE;
 
577
          aip->io_failure = FALSE;
 
578
        }
848
579
      }
849
580
    } else {
850
 
      while ((atp = AsnReadId (aip, afp->amp, atp)) != NULL) {
 
581
      while ((! io_failure) && (atp = AsnReadId (aip, afp->amp, atp)) != NULL) {
 
582
        if (aip->io_failure) {
 
583
          io_failure = TRUE;
 
584
          aip->io_failure = FALSE;
 
585
        }
851
586
        AsnReadVal (aip, atp, &dv);
852
587
        AsnWrite (aop, atp, &dv);
853
588
        AsnKillValue (atp, &dv);
 
589
        if (aip->io_failure) {
 
590
          io_failure = TRUE;
 
591
          aip->io_failure = FALSE;
 
592
        }
854
593
      }
855
594
    }
856
595
 
857
596
  } else {
858
597
 
859
 
    while ((atp = AsnReadId (aip, afp->amp, atp)) != NULL) {
 
598
    while ((! io_failure) && (atp = AsnReadId (aip, afp->amp, atp)) != NULL) {
 
599
      if (aip->io_failure) {
 
600
        io_failure = TRUE;
 
601
        aip->io_failure = FALSE;
 
602
      }
860
603
      if (atp == afp->atp_se) {
861
604
 
862
605
        sep = SeqEntryAsnRead (aip, atp);
875
618
        SeqEntryFree (sep);
876
619
        omp = ObjMgrGet ();
877
620
        ObjMgrReapOne (omp);
 
621
        SeqMgrClearBioseqIndex ();
878
622
        ObjMgrFreeCache (0);
879
623
        FreeSeqIdGiCache ();
880
624
 
 
625
        SeqEntrySetScope (NULL);
 
626
 
881
627
      } else {
882
628
 
883
629
        AsnReadVal (aip, atp, NULL);
884
630
      }
 
631
 
 
632
      if (aip->io_failure) {
 
633
        io_failure = TRUE;
 
634
        aip->io_failure = FALSE;
 
635
      }
885
636
    }
886
637
  }
887
638
 
 
639
  if (aip->io_failure) {
 
640
    io_failure = TRUE;
 
641
  }
 
642
 
 
643
  if (io_failure) {
 
644
    Message (MSG_POSTERR, "Asn io_failure for input file '%s'", filename);
 
645
  }
 
646
 
888
647
  AsnIoFree (aip, FALSE);
889
648
 
890
649
#ifdef OS_UNIX
1045
804
#define b_argBinary       7
1046
805
#define c_argCompressed   8
1047
806
#define r_argRemote       9
1048
 
#define d_argAsnIdx      10
1049
 
#define l_argLockFar     11
1050
 
#define T_argThreads     12
1051
 
#define n_argNear        13
1052
 
#define X_argExtended    14
1053
 
#define A_argAccession   15
1054
 
#define h_argHelp        16
 
807
#define k_argLocal       10
 
808
#define d_argAsnIdx      11
 
809
#define l_argLockFar     12
 
810
#define T_argThreads     13
 
811
#define n_argNear        14
 
812
#define X_argExtended    15
 
813
#define A_argAccession   16
 
814
#define h_argHelp        17
1055
815
 
1056
816
 
1057
817
Args myargs [] = {
1075
835
    TRUE, 'c', ARG_BOOLEAN, 0.0, 0, NULL},
1076
836
  {"Remote Fetching", "F", NULL, NULL,
1077
837
    TRUE, 'r', ARG_BOOLEAN, 0.0, 0, NULL},
 
838
  {"Local Fetching", "F", NULL, NULL,
 
839
    TRUE, 'k', ARG_BOOLEAN, 0.0, 0, NULL},
1078
840
  {"Path to Indexed Binary ASN.1 Data", NULL, NULL, NULL,
1079
841
    TRUE, 'd', ARG_STRING, 0.0, 0, NULL},
1080
842
  {"Lock Components in Advance", "F", NULL, NULL,
1099
861
  Char         app [64], format, nearpolicy, type, xmlbuf [128];
1100
862
  DataVal      av;
1101
863
  ValNodePtr   bsplist;
1102
 
  Boolean      help, local, remote;
 
864
  Boolean      help, indexed, local, remote;
1103
865
  SeqEntryPtr  sep;
1104
866
 
1105
867
  /* standard setup */
1162
924
  MemSet ((Pointer) &afd, 0, sizeof (AppFlagData));
1163
925
 
1164
926
  remote = (Boolean ) myargs [r_argRemote].intvalue;
 
927
  local = (Boolean) myargs [k_argLocal].intvalue;
1165
928
  asnidx = (CharPtr) myargs [d_argAsnIdx].strvalue;
1166
 
  local = (Boolean) StringDoesHaveText (asnidx);
 
929
  indexed = (Boolean) StringDoesHaveText (asnidx);
1167
930
  accn = (CharPtr) myargs [A_argAccession].strvalue;
1168
931
 
1169
932
  directory = (CharPtr) myargs [p_argInputPath].strvalue;
1386
1149
  }
1387
1150
 
1388
1151
  if (local) {
 
1152
    LocalSeqFetchInit (FALSE);
 
1153
  }
 
1154
 
 
1155
  if (indexed) {
1389
1156
    AsnIndexedLibFetchEnable (asnidx, TRUE);
1390
1157
  }
1391
1158
 
1507
1274
  }
1508
1275
 
1509
1276
  if (afd.format == CACHE_COMPONENTS) {
1510
 
    CreateAsnIndex (ntout, TRUE);
 
1277
    CreateAsnIndex (ntout, NULL, TRUE);
1511
1278
  }
1512
1279
 
1513
1280
  /* close fetch functions */
1514
1281
 
1515
 
  if (local) {
 
1282
  if (indexed) {
1516
1283
    AsnIndexedLibFetchDisable ();
1517
1284
  }
1518
1285
 
 
1286
  if (local) {
 
1287
    LocalSeqFetchDisable ();
 
1288
  }
 
1289
 
1519
1290
  if (remote) {
1520
1291
    PubMedFetchDisable ();
1521
1292
    PubSeqFetchDisable ();